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

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 <raffael.wannenmacher@tx.group>
This commit is contained in:
Raffael
2025-08-25 23:45:11 +02:00
committed by GitHub
parent 672779d6cc
commit 0558345703
2 changed files with 26 additions and 9 deletions

View File

@@ -0,0 +1,5 @@
---
'slate-dom': patch
---
Search backward and forward for leaf nodes in non contenteditable elements inside `toSlatePoint`

View File

@@ -697,7 +697,7 @@ export const DOMEditor: DOMEditorInterface = {
searchDirection?: 'forward' | 'backward' searchDirection?: 'forward' | 'backward'
} }
): T extends true ? Point | null : Point => { ): T extends true ? Point | null : Point => {
const { exactMatch, suppressThrow, searchDirection = 'backward' } = options const { exactMatch, suppressThrow, searchDirection } = options
const [nearestNode, nearestOffset] = exactMatch const [nearestNode, nearestOffset] = exactMatch
? domPoint ? domPoint
: normalizeDOMPoint(domPoint) : normalizeDOMPoint(domPoint)
@@ -812,20 +812,32 @@ export const DOMEditor: DOMEditorInterface = {
'[data-slate-node="element"]' '[data-slate-node="element"]'
) )
if (searchDirection === 'forward') { if (searchDirection === 'backward' || !searchDirection) {
const leafNodes = [
...getLeafNodes(elementNode),
...getLeafNodes(elementNode?.nextElementSibling),
]
leafNode =
leafNodes.find(leaf => isAfter(nonEditableNode, leaf)) ?? null
} else {
const leafNodes = [ const leafNodes = [
...getLeafNodes(elementNode?.previousElementSibling), ...getLeafNodes(elementNode?.previousElementSibling),
...getLeafNodes(elementNode), ...getLeafNodes(elementNode),
] ]
leafNode = leafNode =
leafNodes.findLast(leaf => isBefore(nonEditableNode, leaf)) ?? null leafNodes.findLast(leaf => isBefore(nonEditableNode, leaf)) ?? null
if (leafNode) {
searchDirection === 'backward'
}
}
if (searchDirection === 'forward' || !searchDirection) {
const leafNodes = [
...getLeafNodes(elementNode),
...getLeafNodes(elementNode?.nextElementSibling),
]
leafNode =
leafNodes.find(leaf => isAfter(nonEditableNode, leaf)) ?? null
if (leafNode) {
searchDirection === 'forward'
}
} }
if (leafNode) { if (leafNode) {