WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 94fb04a

Browse files
authored
Fix: editor.selection is sometimes replaced with a new object even if the selection did not change (#5899)
* Fix: `editor.selection` replaced despite being unchanged * Document earlier breaking change
1 parent f2fa279 commit 94fb04a

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

.changeset/eleven-days-thank.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'slate': patch
3+
---
4+
5+
Fix: `editor.selection` is sometimes replaced with a new object even if the selection did not change

packages/slate-react/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
- If this change impacts you, consider changing your `decorate` function to work on the node's parent instead.
1818
- For example, if your `decorate` function decorates a `code-line` based on the parent `code-block`'s language, decorate the `code-block` instead.
1919
- This is unlikely to result in any performance detriment, since in previous versions of `slate-react`, the decorations of all siblings were recomputed when one sibling was modified.
20+
- **BREAKING CHANGE:** Elements no longer re-render due to selection changes.
21+
- To re-render whenever an element becomes selected or deselected, subscribe to `useSelected`.
22+
- To re-render whenever the selection changes anywhere in the editor, subscribe to `useSlateSelection`.
23+
- To re-render whenever the intersection of the selection with an element changes (the previous behaviour), use `useSlateSelector` to compute this intersection using `Range.intersection`. Ensure you provide a suitable equality function using `Range.equals`.
2024
- Increase minimum `slate-dom` version to `0.116.0`.
2125
- Deprecate the `useSlateWithV` hook
2226
- PERF: Use subscribable pattern for `useSlate`, `useSelected` and decorations to reduce re-renders.

packages/slate/src/interfaces/transforms/general.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,9 @@ export const GeneralTransforms: GeneralTransforms = {
265265
}
266266
}
267267

268-
editor.selection = selection
268+
if (!selection || !Range.equals(selection, editor.selection)) {
269+
editor.selection = selection
270+
}
269271
}
270272

271273
break
@@ -422,7 +424,9 @@ export const GeneralTransforms: GeneralTransforms = {
422424
selection[key] = Point.transform(point, op)!
423425
}
424426

425-
editor.selection = selection
427+
if (!Range.equals(selection, editor.selection)) {
428+
editor.selection = selection
429+
}
426430
}
427431
},
428432
}

0 commit comments

Comments
 (0)