Skip to content

Commit

Permalink
fix(portable-text-editor): previousChildren must be stored sperately …
Browse files Browse the repository at this point in the history
…for incoming patch adjustments

For adjustSelection previousChildren could sometimes be wrong (the current children)
  • Loading branch information
skogsmaskin committed Sep 8, 2021
1 parent d66b7c2 commit 53d5ff6
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export function createWithPatches(
): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
const patchToOperations = createPatchToOperations(portableTextFeatures)
let previousChildren: (Node | Partial<Node>)[]
let previousChildrenOnPatch: (Node | Partial<Node>)[]
let isThrottling = false
return function withPatches(editor: PortableTextSlateEditor) {
PATCHING.set(editor, true)
Expand All @@ -72,19 +73,19 @@ export function createWithPatches(
// Inspect incoming patches and adjust editor selection accordingly.
if (incomingPatches$) {
incomingPatches$.subscribe((patch: Patch) => {
previousChildren = editor.children
previousChildrenOnPatch = previousChildren
debug('Handling incoming patch', patch.type)
if (isThrottling) {
withoutPatching(editor, () => {
if (patchToOperations(editor, patch)) {
debug('Applied patch in the throttled state', patch.type)
} else {
adjustSelection(editor, patch, previousChildren)
adjustSelection(editor, patch, previousChildrenOnPatch, portableTextFeatures)
}
})
} else {
debug('Adjusting selection for patch', patch.type)
adjustSelection(editor, patch, previousChildren)
adjustSelection(editor, patch, previousChildrenOnPatch, portableTextFeatures)
}
})
}
Expand Down Expand Up @@ -217,7 +218,8 @@ export function createWithPatches(
function adjustSelection(
editor: Editor,
patch: Patch,
previousChildren: (Node | Partial<Node>)[]
previousChildren: (Node | Partial<Node>)[],
portableTextFeatures: PortableTextFeatures
): Range | null {
const selection = editor.selection
if (selection === null) {
Expand Down

0 comments on commit 53d5ff6

Please sign in to comment.