Skip to content

Commit

Permalink
Android related improvements (#5315)
Browse files Browse the repository at this point in the history
* Allow consumer handling of `onInput` event

* Avoid restoring the DOM for characterData mutations
  • Loading branch information
clauderic authored Feb 27, 2023
1 parent c753e68 commit 5784a38
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/android-text-mutations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

The `RestoreDOM` manager that is used Android no longer restores the DOM to its previous state for text mutations. This allows the editor state to be reconciled during a composition without interrupting the composition, as programatically updating the `textContent` of a text node ends the current composition.
5 changes: 5 additions & 0 deletions .changeset/consumer-oninput-event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

Fixed consumer defined `onInput` event handler not being invoked when passed to the `<Editable>` component.
6 changes: 5 additions & 1 deletion packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,11 @@ export const Editable = (props: EditableProps) => {
},
[readOnly]
)}
onInput={useCallback((event: React.SyntheticEvent) => {
onInput={useCallback((event: React.FormEvent<HTMLDivElement>) => {
if (isEventHandled(event, attributes.onInput)) {
return
}

if (androidInputManager) {
androidInputManager.handleInput()
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,26 @@ export const createRestoreDomManager = (
}

function restoreDOM() {
bufferedMutations.reverse().forEach(mutation => {
if (mutation.type === 'characterData') {
mutation.target.textContent = mutation.oldValue
return
}

mutation.removedNodes.forEach(node => {
mutation.target.insertBefore(node, mutation.nextSibling)
if (bufferedMutations.length > 0) {
bufferedMutations.reverse().forEach(mutation => {
if (mutation.type === 'characterData') {
// We don't want to restore the DOM for characterData mutations
// because this interrupts the composition.
return
}

mutation.removedNodes.forEach(node => {
mutation.target.insertBefore(node, mutation.nextSibling)
})

mutation.addedNodes.forEach(node => {
mutation.target.removeChild(node)
})
})

mutation.addedNodes.forEach(node => {
mutation.target.removeChild(node)
})
})

// Clear buffered mutations to ensure we don't undo them twice
clear()
// Clear buffered mutations to ensure we don't undo them twice
clear()
}
}

return {
Expand Down

0 comments on commit 5784a38

Please sign in to comment.