Skip to content

Commit

Permalink
Possible fix to scroll cursor into view on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtcode committed Apr 11, 2024
1 parent 4ceec11 commit c10b06b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,34 @@ document.addEventListener('dblclick', function(event) {
event.preventDefault();
}, { passive: false });

document.addEventListener('selectionchange', () => {
const selection = window.getSelection();
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
// Get the parent element of the cursor's position
const parentElement = range.commonAncestorContainer.nodeType === 3
? range.commonAncestorContainer.parentNode
: range.commonAncestorContainer;
setTimeout(() => {
parentElement.scrollIntoView();
}, 50);
}
});

document.addEventListener('click', () => {
const selection = window.getSelection();
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
// Get the parent element of the cursor's position
const parentElement = range.commonAncestorContainer.nodeType === 3
? range.commonAncestorContainer.parentNode
: range.commonAncestorContainer;
setTimeout(() => {
parentElement.scrollIntoView();
}, 1000);
}
});

window.addEventListener('message', function (e) {
console.log('message', e.data)
let message = e.data.message;
Expand Down

0 comments on commit c10b06b

Please sign in to comment.