Skip to content

Commit 0558345

Browse files
delijahRaffael Wannenmacher
andauthored
fix: Search backward and forward for leaf nodes in non contenteditable elements (#5936)
* Search backward and forward for leaf nodes in non contenteditable elements * Add changeset --------- Co-authored-by: Raffael Wannenmacher <[email protected]>
1 parent 672779d commit 0558345

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

.changeset/real-clouds-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'slate-dom': patch
3+
---
4+
5+
Search backward and forward for leaf nodes in non contenteditable elements inside `toSlatePoint`

packages/slate-dom/src/plugin/dom-editor.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ export const DOMEditor: DOMEditorInterface = {
697697
searchDirection?: 'forward' | 'backward'
698698
}
699699
): T extends true ? Point | null : Point => {
700-
const { exactMatch, suppressThrow, searchDirection = 'backward' } = options
700+
const { exactMatch, suppressThrow, searchDirection } = options
701701
const [nearestNode, nearestOffset] = exactMatch
702702
? domPoint
703703
: normalizeDOMPoint(domPoint)
@@ -812,20 +812,32 @@ export const DOMEditor: DOMEditorInterface = {
812812
'[data-slate-node="element"]'
813813
)
814814

815-
if (searchDirection === 'forward') {
815+
if (searchDirection === 'backward' || !searchDirection) {
816816
const leafNodes = [
817+
...getLeafNodes(elementNode?.previousElementSibling),
817818
...getLeafNodes(elementNode),
818-
...getLeafNodes(elementNode?.nextElementSibling),
819819
]
820+
820821
leafNode =
821-
leafNodes.find(leaf => isAfter(nonEditableNode, leaf)) ?? null
822-
} else {
822+
leafNodes.findLast(leaf => isBefore(nonEditableNode, leaf)) ?? null
823+
824+
if (leafNode) {
825+
searchDirection === 'backward'
826+
}
827+
}
828+
829+
if (searchDirection === 'forward' || !searchDirection) {
823830
const leafNodes = [
824-
...getLeafNodes(elementNode?.previousElementSibling),
825831
...getLeafNodes(elementNode),
832+
...getLeafNodes(elementNode?.nextElementSibling),
826833
]
834+
827835
leafNode =
828-
leafNodes.findLast(leaf => isBefore(nonEditableNode, leaf)) ?? null
836+
leafNodes.find(leaf => isAfter(nonEditableNode, leaf)) ?? null
837+
838+
if (leafNode) {
839+
searchDirection === 'forward'
840+
}
829841
}
830842

831843
if (leafNode) {

0 commit comments

Comments
 (0)