Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
fix snap to word properly
Browse files Browse the repository at this point in the history
  • Loading branch information
chunliu committed Jul 5, 2020
1 parent 8f740ff commit b8c92fe
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions code/src/content/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,32 @@
range.detach();

// modify() works on the focus of the selection
const endNode = sel.focusNode;
const endOffset = sel.focusOffset;
let endNode = sel.focusNode;
let endOffset = sel.focusOffset;
sel.collapse(sel.anchorNode, sel.anchorOffset);
sel.modify('move', direction[0], 'character');
sel.modify('move', direction[1], 'word');
// remove the whitespace after the word
if (direction[0] === 'backward') {
sel.modify('move', 'backward', 'character');
}
sel.extend(endNode, endOffset);
sel.modify('extend', direction[1], 'character');
sel.modify('extend', direction[0], 'word');
// remove the whitespace after the word
if (direction[0] === 'forward') {

// It seems in Chrome when moving a word, it would count the following whitespace of the word.
// A problem with the whitespace is if users repeat the action, the selection would be extended unexpectedly.
// The following code removes that whitespace.
if (direction[0] === 'forward' &&
sel.focusNode.textContent.charAt(sel.focusOffset - 1) === ' ') {
// for the forward selection, the whitespace could appear in the focusNode
sel.modify('extend', 'backward', 'character');
} else if (direction[0] === 'backward' &&
sel.anchorNode.textContent.charAt(sel.anchorOffset - 1) === ' ') {
// for the backward selection, the whitespace could appear in the anchorNode
endNode = sel.focusNode;
endOffset = sel.focusOffset;
sel.collapse(sel.anchorNode, sel.anchorOffset - 1);
sel.extend(endNode, endOffset);
}
}
return sel.toString().trim();
return sel.toString();
};

browser.runtime.onMessage.addListener((request, _, sendResponse) => {
Expand Down

0 comments on commit b8c92fe

Please sign in to comment.