1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-20 22:21:20 +02:00

Merge branch 'master' of github.com:ianstormtaylor/slate

This commit is contained in:
Ian Storm Taylor
2021-03-31 22:07:27 -04:00

View File

@@ -107,16 +107,21 @@ export const normalizeDOMPoint = (domPoint: DOMPoint): DOMPoint => {
// If it's an element node, its offset refers to the index of its children // If it's an element node, its offset refers to the index of its children
// including comment nodes, so try to find the right text child node. // including comment nodes, so try to find the right text child node.
if (isDOMElement(node) && node.childNodes.length) { if (isDOMElement(node) && node.childNodes.length) {
const isLast = offset === node.childNodes.length let isLast = offset === node.childNodes.length
const direction = isLast ? 'backward' : 'forward' let index = isLast ? offset - 1 : offset
const index = isLast ? offset - 1 : offset ;[node, index] = getEditableChildAndIndex(
node = getEditableChild(node, index, direction) node,
index,
isLast ? 'backward' : 'forward'
)
// If the editable child found is in front of input offset, we instead seek to its end
isLast = index < offset
// If the node has children, traverse until we have a leaf node. Leaf nodes // If the node has children, traverse until we have a leaf node. Leaf nodes
// can be either text nodes, or other void DOM nodes. // can be either text nodes, or other void DOM nodes.
while (isDOMElement(node) && node.childNodes.length) { while (isDOMElement(node) && node.childNodes.length) {
const i = isLast ? node.childNodes.length - 1 : 0 const i = isLast ? node.childNodes.length - 1 : 0
node = getEditableChild(node, i, direction) node = getEditableChild(node, i, isLast ? 'backward' : 'forward')
} }
// Determine the new offset inside the text node. // Determine the new offset inside the text node.
@@ -138,15 +143,15 @@ export const hasShadowRoot = () => {
} }
/** /**
* Get the nearest editable child at `index` in a `parent`, preferring * Get the nearest editable child and index at `index` in a `parent`, preferring
* `direction`. * `direction`.
*/ */
export const getEditableChild = ( export const getEditableChildAndIndex = (
parent: DOMElement, parent: DOMElement,
index: number, index: number,
direction: 'forward' | 'backward' direction: 'forward' | 'backward'
): DOMNode => { ): [DOMNode, number] => {
const { childNodes } = parent const { childNodes } = parent
let child = childNodes[index] let child = childNodes[index]
let i = index let i = index
@@ -179,9 +184,24 @@ export const getEditableChild = (
} }
child = childNodes[i] child = childNodes[i]
index = i
i += direction === 'forward' ? 1 : -1 i += direction === 'forward' ? 1 : -1
} }
return [child, index]
}
/**
* Get the nearest editable child at `index` in a `parent`, preferring
* `direction`.
*/
export const getEditableChild = (
parent: DOMElement,
index: number,
direction: 'forward' | 'backward'
): DOMNode => {
const [child] = getEditableChildAndIndex(parent, index, direction)
return child return child
} }