Skip to content

Commit

Permalink
Fixed #2107 extract a common function to locate focus node for modes …
Browse files Browse the repository at this point in the history
…insert and visual
  • Loading branch information
brookhong committed Jul 15, 2024
1 parent 5940fb2 commit 446209d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 43 deletions.
18 changes: 7 additions & 11 deletions src/content_scripts/common/hints.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,20 +390,16 @@ div.hint-scrollable {
};

self.genLabels = function(total) {
var ch, hint, hints, i, len, offset;
hints = [""];
offset = 0;
while (hints.length - offset < total || hints.length === 1) {
hint = hints[offset++];
for (i = 0, len = characters.length; i < len; i++) {
ch = characters[i];
hints.push(ch + hint);
let chars = characters.toUpperCase();
var hints = [""], offset = 0;
while (hints.length - offset < total || offset == 0) {
var prefix = hints[offset++];
for (var i = 0; i < chars.length; i++) {
hints.push(prefix + chars[i]);
}
}
hints = hints.slice(offset, offset + total);
return hints.map(function(str) {
return str.reverse().toUpperCase();
});
return hints
};

self.coordinate = function() {
Expand Down
16 changes: 2 additions & 14 deletions src/content_scripts/common/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createElementWithContent,
getRealEdit,
isEditable,
locateFocusNode,
scrollIntoViewIfNeeded,
setSanitizedContent,
} from './utils.js';
Expand Down Expand Up @@ -39,9 +40,6 @@ function createInsert() {
document.getSelection().setPosition(node, node.childNodes.length);
}
}
// blink cursor to bring cursor into view
Visual.showCursor();
Visual.hideCursor();
}
}
}
Expand Down Expand Up @@ -74,9 +72,6 @@ function createInsert() {
// for contenteditable div
var selection = document.getSelection();
selection.setPosition(selection.focusNode, 0);
// blink cursor to bring cursor into view
Visual.showCursor();
Visual.hideCursor();
}
}
});
Expand Down Expand Up @@ -236,14 +231,7 @@ function createInsert() {
document.body.append(_emojiDiv);
_emojiDiv.style.display = "";
_emojiDiv.querySelector('#sk_emoji>div').classList.add("selected");
var br;
if (isInput) {
br = getCursorPixelPos(input);
} else {
Visual.showCursor();
br = Visual.getCursorPixelPos();
Visual.hideCursor();
}
var br = isInput ? getCursorPixelPos(input) : locateFocusNode(document.getSelection());
var top = br.top + br.height + 4;
if (window.innerHeight - top < _emojiDiv.offsetHeight) {
top = br.top - _emojiDiv.offsetHeight;
Expand Down
28 changes: 28 additions & 0 deletions src/content_scripts/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,33 @@ function getTextRect() {
return rects;
}

function locateFocusNode(selection) {
let se = selection.focusNode.parentElement
scrollIntoViewIfNeeded(se, true);
var r = getTextRect(selection.focusNode, selection.focusOffset)[0];
if (!r) {
r = selection.focusNode.getBoundingClientRect();
}
if (r) {
r = {
left: r.left,
top: r.top,
width: r.width,
height: r.height
};
if (r.left < 0 || r.left >= window.innerWidth) {
se.scrollLeft += r.left - window.innerWidth / 2;
r.left = window.innerWidth / 2;
}
if (r.top < 0 || r.top >= window.innerHeight) {
se.scrollTop += r.top - window.innerHeight / 2;
r.top = window.innerHeight / 2;
}
return r;
}
return null;
}

function getNearestWord(text, offset) {
var ret = [0, text.length];
var nonWord = /\W/;
Expand Down Expand Up @@ -912,6 +939,7 @@ export {
isElementPartiallyInViewport,
isInUIFrame,
listElements,
locateFocusNode,
mapInMode,
parseAnnotation,
refreshHints,
Expand Down
21 changes: 3 additions & 18 deletions src/content_scripts/common/visual.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getTextRect,
getVisibleElements,
getWordUnderCursor,
locateFocusNode,
scrollIntoViewIfNeeded,
setSanitizedContent,
tabOpenLink,
Expand Down Expand Up @@ -463,27 +464,11 @@ function createVisual(clipboard, hints) {
if (selection.focusNode && (selection.focusNode.offsetHeight > 0 || selection.focusNode.parentNode.offsetHeight > 0)) {
// https://developer.mozilla.org/en-US/docs/Web/API/Selection
// If focusNode is a text node, this is the number of characters within focusNode preceding the focus. If focusNode is an element, this is the number of child nodes of the focusNode preceding the focus.
scrollIntoViewIfNeeded(selection.focusNode.parentElement, true);

var r = getTextRect(selection.focusNode, selection.focusOffset)[0];
if (!r) {
r = selection.focusNode.getBoundingClientRect();
}
let r = locateFocusNode(selection)
if (r) {
cursor.style.position = "fixed";
cursor.style.left = r.left + 'px';
if (r.left < 0 || r.left >= window.innerWidth) {
document.scrollingElement.scrollLeft += r.left - window.innerWidth / 2;
cursor.style.left = window.innerWidth / 2 + 'px';
} else {
cursor.style.left = r.left + 'px';
}
if (r.top < 0 || r.top >= window.innerHeight) {
document.scrollingElement.scrollTop += r.top - window.innerHeight / 2;
cursor.style.top = window.innerHeight / 2 + 'px';
} else {
cursor.style.top = r.top + 'px';
}
cursor.style.top = r.top + 'px';
cursor.style.height = r.height + 'px';
}

Expand Down

0 comments on commit 446209d

Please sign in to comment.