Skip to content

Commit

Permalink
Fix move_node triggers nodes re-render (#5513) (#5514)
Browse files Browse the repository at this point in the history
* Fix move_node triggers nodes re-render (#5513)

* Add changeset
  • Loading branch information
YaoKaiLun committed Sep 25, 2023
1 parent 300dc57 commit ff7db22
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-grapes-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

Fix move_node triggers nodes re-render
24 changes: 24 additions & 0 deletions packages/slate-react/src/plugin/with-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Node,
Operation,
Path,
PathRef,
Point,
Range,
Transforms,
Expand Down Expand Up @@ -122,6 +123,7 @@ export const withReact = <T extends BaseEditor>(
// as apply() changes the object reference and hence invalidates the NODE_TO_KEY entry
e.apply = (op: Operation) => {
const matches: [Path, Key][] = []
const pathRefMatches: [PathRef, Key][] = []

const pendingDiffs = EDITOR_TO_PENDING_DIFFS.get(e)
if (pendingDiffs?.length) {
Expand Down Expand Up @@ -183,6 +185,21 @@ export const withReact = <T extends BaseEditor>(
Path.parent(op.newPath)
)
matches.push(...getMatches(e, commonPath))

let changedPath: Path
if (Path.isBefore(op.path, op.newPath)) {
matches.push(...getMatches(e, Path.parent(op.path)))
changedPath = op.newPath
} else {
matches.push(...getMatches(e, Path.parent(op.newPath)))
changedPath = op.path
}

const changedNode = Node.get(editor, Path.parent(changedPath))
const changedNodeKey = ReactEditor.findKey(e, changedNode)
const changedPathRef = Editor.pathRef(e, Path.parent(changedPath))
pathRefMatches.push([changedPathRef, changedNodeKey])

break
}
}
Expand All @@ -193,6 +210,13 @@ export const withReact = <T extends BaseEditor>(
const [node] = Editor.node(e, path)
NODE_TO_KEY.set(node, key)
}

for (const [pathRef, key] of pathRefMatches) {
if (pathRef.current) {
const [node] = Editor.node(e, pathRef.current)
NODE_TO_KEY.set(node, key)
}
}
}

e.setFragmentData = (data: Pick<DataTransfer, 'getData' | 'setData'>) => {
Expand Down

0 comments on commit ff7db22

Please sign in to comment.