Skip to content

Commit 0421fb5

Browse files
Only update the selection range if needed (improves behavior in Android Chrome)
1 parent e1d94f9 commit 0421fb5

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

demo.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
}
2828
img { width: 120px; height: 120px; float: right; }
2929
a { color: var(--gray-50); }
30-
button { cursor: pointer; }
30+
button {
31+
cursor: pointer;
32+
user-select: none;
33+
}
3134

3235
#annotation-menu {
3336
font-family: "Helvetica", sans-serif;

highlight-helper.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,19 @@ function Highlighter(options = hhDefaultOptions) {
489489

490490
activeHighlightId = highlightId;
491491
updateSelectionUi('appearance');
492-
selection.setBaseAndExtent(highlightRange.startContainer, highlightRange.startOffset, highlightRange.endContainer, highlightRange.endOffset);
492+
493+
// Update the selection range if needed
494+
// In Android Chrome, sometimes selection handles don't show when a selection is updated programmatically, so it's best to only update the selection if needed.
495+
let selectionRange = selection.type === 'Range' ? selection.getRangeAt(0) : null;
496+
if (!selectionRange || !(
497+
selectionRange.startContainer === highlightRange.startContainer &&
498+
selectionRange.startOffset === highlightRange.startOffset &&
499+
selectionRange.endContainer === highlightRange.endContainer &&
500+
selectionRange.endOffset === highlightRange.endOffset)
501+
) {
502+
selection.setBaseAndExtent(highlightRange.startContainer, highlightRange.startOffset, highlightRange.endContainer, highlightRange.endOffset);
503+
}
504+
493505
this.annotatableContainer.dispatchEvent(new CustomEvent('hh:highlightactivate', { detail: { highlight: highlightToActivate } }));
494506
}
495507

@@ -730,7 +742,18 @@ function Highlighter(options = hhDefaultOptions) {
730742
const selection = window.getSelection();
731743
if (selection.type === 'Range' && activeHighlightId && this.annotatableContainer.contains(selection.anchorNode)) {
732744
const adjustedSelectionRange = snapRangeToBoundaries(selection.getRangeAt(0), selection.anchorNode);
733-
selection.setBaseAndExtent(adjustedSelectionRange.startContainer, adjustedSelectionRange.startOffset, adjustedSelectionRange.endContainer, adjustedSelectionRange.endOffset);
745+
746+
// Update the selection range if needed
747+
// In Android Chrome, sometimes selection handles don't show when a selection is updated programmatically, so it's best to only update the selection if needed.
748+
const selectionRange = selection.getRangeAt(0);
749+
if (!(
750+
adjustedSelectionRange.startContainer === selectionRange.startContainer &&
751+
adjustedSelectionRange.startOffset === selectionRange.startOffset &&
752+
adjustedSelectionRange.endContainer === selectionRange.endContainer &&
753+
adjustedSelectionRange.endOffset === selectionRange.endOffset)
754+
) {
755+
selection.setBaseAndExtent(adjustedSelectionRange.startContainer, adjustedSelectionRange.startOffset, adjustedSelectionRange.endContainer, adjustedSelectionRange.endOffset);
756+
}
734757
}
735758
tapResult = null;
736759
longPressTimeoutId = clearTimeout(longPressTimeoutId);

0 commit comments

Comments
 (0)