1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-12 10:14:02 +02:00

Fix move_node triggers nodes re-render (#5513) (#5514)

* Fix move_node triggers nodes re-render (#5513)

* Add changeset
This commit is contained in:
yaokailun
2023-09-25 22:55:13 +08:00
committed by GitHub
parent 300dc57a00
commit ff7db22120
2 changed files with 29 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
'slate-react': patch
---
Fix move_node triggers nodes re-render

View File

@@ -6,6 +6,7 @@ import {
Node,
Operation,
Path,
PathRef,
Point,
Range,
Transforms,
@@ -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) {
@@ -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
}
}
@@ -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'>) => {