Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send selection to editor.select on android #3024

Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions packages/slate-react/src/plugins/android/composition-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ function renderSync(editor, fn) {
})
}

/**
* Takes a the range and the editor. It will take the selection from the editor
* and apply the range's anchor and focus on it.
*
* @param {Editor} editor
* @param {Range} range
*/

function selectionFromRange(editor, range) {
return editor.value.selection
.moveAnchorTo(range.anchor)
.moveFocusTo(range.focus)
}

/**
* Takes text from a dom node and an offset within that text and returns an
* object with fixed text and fixed offset which removes zero width spaces
Expand Down Expand Up @@ -191,7 +205,7 @@ function CompositionManager(editor) {
applyDiff()

if (last.range) {
editor.select(last.range)
editor.select(selectionFromRange(editor, last.range))
} else {
debug('splitBlock:NO-SELECTION')
}
Expand Down Expand Up @@ -232,7 +246,7 @@ function CompositionManager(editor) {
applyDiff()

editor
.select(last.range)
.select(selectionFromRange(editor, last.range))
.deleteBackward()
.focus()
.restoreDOM()
Expand Down Expand Up @@ -308,7 +322,7 @@ function CompositionManager(editor) {
if (last.range && !last.range.isCollapsed) {
renderSync(editor, () => {
editor
.select(last.range)
.select(selectionFromRange(editor, last.range))
.deleteBackward()
.focus()
.restoreDOM()
Expand Down Expand Up @@ -437,7 +451,7 @@ function CompositionManager(editor) {

renderSync(editor, () => {
editor
.select(nodeSelection)
.select(selectionFromRange(editor, nodeSelection))
.delete()
.restoreDOM()
})
Expand Down Expand Up @@ -504,7 +518,7 @@ function CompositionManager(editor) {
*/

editor
.select(range)
.select(selectionFromRange(editor, range))
.focus()
.restoreDOM()
})
Expand Down Expand Up @@ -582,6 +596,28 @@ function CompositionManager(editor) {
clearAction()
}

const isPointsEqual = (point1, point2) => {
return point1.offset === point2.offset && point1.key === point2.key
}

const isRangesEqual = (range1, range2) => {
return (
isPointsEqual(range1.anchor, range2.anchor) &&
isPointsEqual(range1.focus, range2.focus)
)
}

// It's important that we don't run the select middleware or commit the
// selection to the value when the diff has a value. When the diff is
// non-null, the text structure in the value is different compared to the DOM.
if (
range.isSet &&
last.diff === null &&
!isRangesEqual(editor.value.selection, range)
) {
editor.select(selectionFromRange(editor, range))
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are mostly from #3023. These should disappear when merging it.


last.range = range
last.node = domSelection.anchorNode
})
Expand Down