|
|
import { |
|
|
BaseEditor, |
|
|
Editor, |
|
|
Element, |
|
|
Node, |
|
|
Path, |
|
|
Point, |
|
|
Range, |
|
|
Scrubber, |
|
|
Transforms, |
|
|
} from 'slate' |
|
|
import { TextDiff } from '../utils/diff-text' |
|
|
import { |
|
|
DOMElement, |
|
|
DOMNode, |
|
|
DOMPoint, |
|
|
DOMRange, |
|
|
DOMSelection, |
|
|
DOMStaticRange, |
|
|
DOMText, |
|
|
getSelection, |
|
|
hasShadowRoot, |
|
|
isAfter, |
|
|
isBefore, |
|
|
isDOMElement, |
|
|
isDOMNode, |
|
|
isDOMSelection, |
|
|
normalizeDOMPoint, |
|
|
} from '../utils/dom' |
|
|
import { IS_ANDROID, IS_CHROME, IS_FIREFOX } from '../utils/environment' |
|
|
|
|
|
import { Key } from '../utils/key' |
|
|
import { |
|
|
EDITOR_TO_ELEMENT, |
|
|
EDITOR_TO_KEY_TO_ELEMENT, |
|
|
EDITOR_TO_PENDING_DIFFS, |
|
|
EDITOR_TO_SCHEDULE_FLUSH, |
|
|
EDITOR_TO_WINDOW, |
|
|
ELEMENT_TO_NODE, |
|
|
IS_COMPOSING, |
|
|
IS_FOCUSED, |
|
|
IS_READ_ONLY, |
|
|
NODE_TO_INDEX, |
|
|
NODE_TO_KEY, |
|
|
NODE_TO_PARENT, |
|
|
} from '../utils/weak-maps' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export interface DOMEditor extends BaseEditor { |
|
|
hasEditableTarget: ( |
|
|
editor: DOMEditor, |
|
|
target: EventTarget | null |
|
|
) => target is DOMNode |
|
|
hasRange: (editor: DOMEditor, range: Range) => boolean |
|
|
hasSelectableTarget: ( |
|
|
editor: DOMEditor, |
|
|
target: EventTarget | null |
|
|
) => boolean |
|
|
hasTarget: ( |
|
|
editor: DOMEditor, |
|
|
target: EventTarget | null |
|
|
) => target is DOMNode |
|
|
insertData: (data: DataTransfer) => void |
|
|
insertFragmentData: (data: DataTransfer) => boolean |
|
|
insertTextData: (data: DataTransfer) => boolean |
|
|
isTargetInsideNonReadonlyVoid: ( |
|
|
editor: DOMEditor, |
|
|
target: EventTarget | null |
|
|
) => boolean |
|
|
setFragmentData: ( |
|
|
data: DataTransfer, |
|
|
originEvent?: 'drag' | 'copy' | 'cut' |
|
|
) => void |
|
|
} |
|
|
|
|
|
export interface DOMEditorInterface { |
|
|
|
|
|
|
|
|
|
|
|
androidPendingDiffs: (editor: Editor) => TextDiff[] | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
androidScheduleFlush: (editor: Editor) => void |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
blur: (editor: DOMEditor) => void |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
deselect: (editor: DOMEditor) => void |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
findDocumentOrShadowRoot: (editor: DOMEditor) => Document | ShadowRoot |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
findEventRange: (editor: DOMEditor, event: any) => Range |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
findKey: (editor: DOMEditor, node: Node) => Key |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
findPath: (editor: DOMEditor, node: Node) => Path |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
focus: (editor: DOMEditor, options?: { retries: number }) => void |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getWindow: (editor: DOMEditor) => Window |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hasDOMNode: ( |
|
|
editor: DOMEditor, |
|
|
target: DOMNode, |
|
|
options?: { editable?: boolean } |
|
|
) => boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hasEditableTarget: ( |
|
|
editor: DOMEditor, |
|
|
target: EventTarget | null |
|
|
) => target is DOMNode |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hasRange: (editor: DOMEditor, range: Range) => boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hasSelectableTarget: ( |
|
|
editor: DOMEditor, |
|
|
target: EventTarget | null |
|
|
) => boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hasTarget: ( |
|
|
editor: DOMEditor, |
|
|
target: EventTarget | null |
|
|
) => target is DOMNode |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
insertData: (editor: DOMEditor, data: DataTransfer) => void |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
insertFragmentData: (editor: DOMEditor, data: DataTransfer) => boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
insertTextData: (editor: DOMEditor, data: DataTransfer) => boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isComposing: (editor: DOMEditor) => boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isFocused: (editor: DOMEditor) => boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isReadOnly: (editor: DOMEditor) => boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isTargetInsideNonReadonlyVoid: ( |
|
|
editor: DOMEditor, |
|
|
target: EventTarget | null |
|
|
) => boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setFragmentData: ( |
|
|
editor: DOMEditor, |
|
|
data: DataTransfer, |
|
|
originEvent?: 'drag' | 'copy' | 'cut' |
|
|
) => void |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
toDOMNode: (editor: DOMEditor, node: Node) => HTMLElement |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
toDOMPoint: (editor: DOMEditor, point: Point) => DOMPoint |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
toDOMRange: (editor: DOMEditor, range: Range) => DOMRange |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
toSlateNode: (editor: DOMEditor, domNode: DOMNode) => Node |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
toSlatePoint: <T extends boolean>( |
|
|
editor: DOMEditor, |
|
|
domPoint: DOMPoint, |
|
|
options: { |
|
|
exactMatch: boolean |
|
|
suppressThrow: T |
|
|
/** |
|
|
* The direction to search for Slate leaf nodes if `domPoint` is |
|
|
* non-editable and non-void. |
|
|
*/ |
|
|
searchDirection?: 'forward' | 'backward' |
|
|
} |
|
|
) => T extends true ? Point | null : Point |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
toSlateRange: <T extends boolean>( |
|
|
editor: DOMEditor, |
|
|
domRange: DOMRange | DOMStaticRange | DOMSelection, |
|
|
options: { |
|
|
exactMatch: boolean |
|
|
suppressThrow: T |
|
|
} |
|
|
) => T extends true ? Range | null : Range |
|
|
} |
|
|
|
|
|
|
|
|
export const DOMEditor: DOMEditorInterface = { |
|
|
androidPendingDiffs: editor => EDITOR_TO_PENDING_DIFFS.get(editor), |
|
|
|
|
|
androidScheduleFlush: editor => { |
|
|
EDITOR_TO_SCHEDULE_FLUSH.get(editor)?.() |
|
|
}, |
|
|
|
|
|
blur: editor => { |
|
|
const el = DOMEditor.toDOMNode(editor, editor) |
|
|
const root = DOMEditor.findDocumentOrShadowRoot(editor) |
|
|
IS_FOCUSED.set(editor, false) |
|
|
|
|
|
if (root.activeElement === el) { |
|
|
el.blur() |
|
|
} |
|
|
}, |
|
|
|
|
|
deselect: editor => { |
|
|
const { selection } = editor |
|
|
const root = DOMEditor.findDocumentOrShadowRoot(editor) |
|
|
const domSelection = getSelection(root) |
|
|
|
|
|
if (domSelection && domSelection.rangeCount > 0) { |
|
|
domSelection.removeAllRanges() |
|
|
} |
|
|
|
|
|
if (selection) { |
|
|
Transforms.deselect(editor) |
|
|
} |
|
|
}, |
|
|
|
|
|
findDocumentOrShadowRoot: editor => { |
|
|
const el = DOMEditor.toDOMNode(editor, editor) |
|
|
const root = el.getRootNode() |
|
|
|
|
|
if (root instanceof Document || root instanceof ShadowRoot) { |
|
|
return root |
|
|
} |
|
|
|
|
|
return el.ownerDocument |
|
|
}, |
|
|
|
|
|
findEventRange: (editor, event) => { |
|
|
if ('nativeEvent' in event) { |
|
|
event = event.nativeEvent |
|
|
} |
|
|
|
|
|
const { clientX: x, clientY: y, target } = event |
|
|
|
|
|
if (x == null || y == null) { |
|
|
throw new Error(`Cannot resolve a Slate range from a DOM event: ${event}`) |
|
|
} |
|
|
|
|
|
const node = DOMEditor.toSlateNode(editor, event.target) |
|
|
const path = DOMEditor.findPath(editor, node) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Element.isElement(node) && Editor.isVoid(editor, node)) { |
|
|
const rect = target.getBoundingClientRect() |
|
|
const isPrev = editor.isInline(node) |
|
|
? x - rect.left < rect.left + rect.width - x |
|
|
: y - rect.top < rect.top + rect.height - y |
|
|
|
|
|
const edge = Editor.point(editor, path, { |
|
|
edge: isPrev ? 'start' : 'end', |
|
|
}) |
|
|
const point = isPrev |
|
|
? Editor.before(editor, edge) |
|
|
: Editor.after(editor, edge) |
|
|
|
|
|
if (point) { |
|
|
const range = Editor.range(editor, point) |
|
|
return range |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
let domRange |
|
|
const { document } = DOMEditor.getWindow(editor) |
|
|
|
|
|
|
|
|
if (document.caretRangeFromPoint) { |
|
|
domRange = document.caretRangeFromPoint(x, y) |
|
|
} else { |
|
|
const position = document.caretPositionFromPoint(x, y) |
|
|
|
|
|
if (position) { |
|
|
domRange = document.createRange() |
|
|
domRange.setStart(position.offsetNode, position.offset) |
|
|
domRange.setEnd(position.offsetNode, position.offset) |
|
|
} |
|
|
} |
|
|
|
|
|
if (!domRange) { |
|
|
throw new Error(`Cannot resolve a Slate range from a DOM event: ${event}`) |
|
|
} |
|
|
|
|
|
|
|
|
const range = DOMEditor.toSlateRange(editor, domRange, { |
|
|
exactMatch: false, |
|
|
suppressThrow: false, |
|
|
}) |
|
|
return range |
|
|
}, |
|
|
|
|
|
findKey: (editor, node) => { |
|
|
let key = NODE_TO_KEY.get(node) |
|
|
|
|
|
if (!key) { |
|
|
key = new Key() |
|
|
NODE_TO_KEY.set(node, key) |
|
|
} |
|
|
|
|
|
return key |
|
|
}, |
|
|
|
|
|
findPath: (editor, node) => { |
|
|
const path: Path = [] |
|
|
let child = node |
|
|
|
|
|
while (true) { |
|
|
const parent = NODE_TO_PARENT.get(child) |
|
|
|
|
|
if (parent == null) { |
|
|
if (Editor.isEditor(child)) { |
|
|
return path |
|
|
} else { |
|
|
break |
|
|
} |
|
|
} |
|
|
|
|
|
const i = NODE_TO_INDEX.get(child) |
|
|
|
|
|
if (i == null) { |
|
|
break |
|
|
} |
|
|
|
|
|
path.unshift(i) |
|
|
child = parent |
|
|
} |
|
|
|
|
|
throw new Error( |
|
|
`Unable to find the path for Slate node: ${Scrubber.stringify(node)}` |
|
|
) |
|
|
}, |
|
|
|
|
|
focus: (editor, options = { retries: 5 }) => { |
|
|
|
|
|
if (IS_FOCUSED.get(editor)) { |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!EDITOR_TO_ELEMENT.get(editor)) { |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (options.retries <= 0) { |
|
|
throw new Error( |
|
|
'Could not set focus, editor seems stuck with pending operations' |
|
|
) |
|
|
} |
|
|
if (editor.operations.length > 0) { |
|
|
setTimeout(() => { |
|
|
DOMEditor.focus(editor, { retries: options.retries - 1 }) |
|
|
}, 10) |
|
|
return |
|
|
} |
|
|
|
|
|
const el = DOMEditor.toDOMNode(editor, editor) |
|
|
const root = DOMEditor.findDocumentOrShadowRoot(editor) |
|
|
if (root.activeElement !== el) { |
|
|
|
|
|
if (editor.selection && root instanceof Document) { |
|
|
const domSelection = getSelection(root) |
|
|
const domRange = DOMEditor.toDOMRange(editor, editor.selection) |
|
|
domSelection?.removeAllRanges() |
|
|
domSelection?.addRange(domRange) |
|
|
} |
|
|
|
|
|
if (!editor.selection) { |
|
|
Transforms.select(editor, Editor.start(editor, [])) |
|
|
} |
|
|
|
|
|
|
|
|
IS_FOCUSED.set(editor, true) |
|
|
el.focus({ preventScroll: true }) |
|
|
} |
|
|
}, |
|
|
|
|
|
getWindow: editor => { |
|
|
const window = EDITOR_TO_WINDOW.get(editor) |
|
|
if (!window) { |
|
|
throw new Error('Unable to find a host window element for this editor') |
|
|
} |
|
|
return window |
|
|
}, |
|
|
|
|
|
hasDOMNode: (editor, target, options = {}) => { |
|
|
const { editable = false } = options |
|
|
const editorEl = DOMEditor.toDOMNode(editor, editor) |
|
|
let targetEl |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
targetEl = ( |
|
|
isDOMElement(target) ? target : target.parentElement |
|
|
) as HTMLElement |
|
|
} catch (err) { |
|
|
if ( |
|
|
err instanceof Error && |
|
|
!err.message.includes('Permission denied to access property "nodeType"') |
|
|
) { |
|
|
throw err |
|
|
} |
|
|
} |
|
|
|
|
|
if (!targetEl) { |
|
|
return false |
|
|
} |
|
|
|
|
|
return ( |
|
|
targetEl.closest(`[data-slate-editor]`) === editorEl && |
|
|
(!editable || targetEl.isContentEditable |
|
|
? true |
|
|
: (typeof targetEl.isContentEditable === 'boolean' && |
|
|
|
|
|
targetEl.closest('[contenteditable="false"]') === editorEl) || |
|
|
!!targetEl.getAttribute('data-slate-zero-width')) |
|
|
) |
|
|
}, |
|
|
|
|
|
hasEditableTarget: (editor, target): target is DOMNode => |
|
|
isDOMNode(target) && |
|
|
DOMEditor.hasDOMNode(editor, target, { editable: true }), |
|
|
|
|
|
hasRange: (editor, range) => { |
|
|
const { anchor, focus } = range |
|
|
return ( |
|
|
Editor.hasPath(editor, anchor.path) && Editor.hasPath(editor, focus.path) |
|
|
) |
|
|
}, |
|
|
|
|
|
hasSelectableTarget: (editor, target) => |
|
|
DOMEditor.hasEditableTarget(editor, target) || |
|
|
DOMEditor.isTargetInsideNonReadonlyVoid(editor, target), |
|
|
|
|
|
hasTarget: (editor, target): target is DOMNode => |
|
|
isDOMNode(target) && DOMEditor.hasDOMNode(editor, target), |
|
|
|
|
|
insertData: (editor, data) => { |
|
|
editor.insertData(data) |
|
|
}, |
|
|
|
|
|
insertFragmentData: (editor, data) => editor.insertFragmentData(data), |
|
|
|
|
|
insertTextData: (editor, data) => editor.insertTextData(data), |
|
|
|
|
|
isComposing: editor => { |
|
|
return !!IS_COMPOSING.get(editor) |
|
|
}, |
|
|
|
|
|
isFocused: editor => !!IS_FOCUSED.get(editor), |
|
|
|
|
|
isReadOnly: editor => !!IS_READ_ONLY.get(editor), |
|
|
|
|
|
isTargetInsideNonReadonlyVoid: (editor, target) => { |
|
|
if (IS_READ_ONLY.get(editor)) return false |
|
|
|
|
|
const slateNode = |
|
|
DOMEditor.hasTarget(editor, target) && |
|
|
DOMEditor.toSlateNode(editor, target) |
|
|
return Element.isElement(slateNode) && Editor.isVoid(editor, slateNode) |
|
|
}, |
|
|
|
|
|
setFragmentData: (editor, data, originEvent) => |
|
|
editor.setFragmentData(data, originEvent), |
|
|
|
|
|
toDOMNode: (editor, node) => { |
|
|
const KEY_TO_ELEMENT = EDITOR_TO_KEY_TO_ELEMENT.get(editor) |
|
|
const domNode = Editor.isEditor(node) |
|
|
? EDITOR_TO_ELEMENT.get(editor) |
|
|
: KEY_TO_ELEMENT?.get(DOMEditor.findKey(editor, node)) |
|
|
|
|
|
if (!domNode) { |
|
|
throw new Error( |
|
|
`Cannot resolve a DOM node from Slate node: ${Scrubber.stringify(node)}` |
|
|
) |
|
|
} |
|
|
|
|
|
return domNode |
|
|
}, |
|
|
|
|
|
toDOMPoint: (editor, point) => { |
|
|
const [node] = Editor.node(editor, point.path) |
|
|
const el = DOMEditor.toDOMNode(editor, node) |
|
|
let domPoint: DOMPoint | undefined |
|
|
|
|
|
|
|
|
|
|
|
if (Editor.void(editor, { at: point })) { |
|
|
point = { path: point.path, offset: 0 } |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const selector = `[data-slate-string], [data-slate-zero-width]` |
|
|
const texts = Array.from(el.querySelectorAll(selector)) |
|
|
let start = 0 |
|
|
|
|
|
for (let i = 0; i < texts.length; i++) { |
|
|
const text = texts[i] |
|
|
const domNode = text.childNodes[0] as HTMLElement |
|
|
|
|
|
if (domNode == null || domNode.textContent == null) { |
|
|
continue |
|
|
} |
|
|
|
|
|
const { length } = domNode.textContent |
|
|
const attr = text.getAttribute('data-slate-length') |
|
|
const trueLength = attr == null ? length : parseInt(attr, 10) |
|
|
const end = start + trueLength |
|
|
|
|
|
|
|
|
|
|
|
const nextText = texts[i + 1] |
|
|
if ( |
|
|
point.offset === end && |
|
|
nextText?.hasAttribute('data-slate-mark-placeholder') |
|
|
) { |
|
|
const domText = nextText.childNodes[0] |
|
|
|
|
|
domPoint = [ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
domText instanceof DOMText ? domText : nextText, |
|
|
nextText.textContent?.startsWith('\uFEFF') ? 1 : 0, |
|
|
] |
|
|
break |
|
|
} |
|
|
|
|
|
if (point.offset <= end) { |
|
|
const offset = Math.min(length, Math.max(0, point.offset - start)) |
|
|
domPoint = [domNode, offset] |
|
|
break |
|
|
} |
|
|
|
|
|
start = end |
|
|
} |
|
|
|
|
|
if (!domPoint) { |
|
|
throw new Error( |
|
|
`Cannot resolve a DOM point from Slate point: ${Scrubber.stringify( |
|
|
point |
|
|
)}` |
|
|
) |
|
|
} |
|
|
|
|
|
return domPoint |
|
|
}, |
|
|
|
|
|
toDOMRange: (editor, range) => { |
|
|
const { anchor, focus } = range |
|
|
const isBackward = Range.isBackward(range) |
|
|
const domAnchor = DOMEditor.toDOMPoint(editor, anchor) |
|
|
const domFocus = Range.isCollapsed(range) |
|
|
? domAnchor |
|
|
: DOMEditor.toDOMPoint(editor, focus) |
|
|
|
|
|
const window = DOMEditor.getWindow(editor) |
|
|
const domRange = window.document.createRange() |
|
|
const [startNode, startOffset] = isBackward ? domFocus : domAnchor |
|
|
const [endNode, endOffset] = isBackward ? domAnchor : domFocus |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const startEl = ( |
|
|
isDOMElement(startNode) ? startNode : startNode.parentElement |
|
|
) as HTMLElement |
|
|
const isStartAtZeroWidth = !!startEl.getAttribute('data-slate-zero-width') |
|
|
const endEl = ( |
|
|
isDOMElement(endNode) ? endNode : endNode.parentElement |
|
|
) as HTMLElement |
|
|
const isEndAtZeroWidth = !!endEl.getAttribute('data-slate-zero-width') |
|
|
|
|
|
domRange.setStart(startNode, isStartAtZeroWidth ? 1 : startOffset) |
|
|
domRange.setEnd(endNode, isEndAtZeroWidth ? 1 : endOffset) |
|
|
return domRange |
|
|
}, |
|
|
|
|
|
toSlateNode: (editor, domNode) => { |
|
|
let domEl = isDOMElement(domNode) ? domNode : domNode.parentElement |
|
|
|
|
|
if (domEl && !domEl.hasAttribute('data-slate-node')) { |
|
|
domEl = domEl.closest(`[data-slate-node]`) |
|
|
} |
|
|
|
|
|
const node = domEl ? ELEMENT_TO_NODE.get(domEl as HTMLElement) : null |
|
|
|
|
|
if (!node) { |
|
|
throw new Error(`Cannot resolve a Slate node from DOM node: ${domEl}`) |
|
|
} |
|
|
|
|
|
return node |
|
|
}, |
|
|
|
|
|
toSlatePoint: <T extends boolean>( |
|
|
editor: DOMEditor, |
|
|
domPoint: DOMPoint, |
|
|
options: { |
|
|
exactMatch: boolean |
|
|
suppressThrow: T |
|
|
searchDirection?: 'forward' | 'backward' |
|
|
} |
|
|
): T extends true ? Point | null : Point => { |
|
|
const { exactMatch, suppressThrow, searchDirection = 'backward' } = options |
|
|
const [nearestNode, nearestOffset] = exactMatch |
|
|
? domPoint |
|
|
: normalizeDOMPoint(domPoint) |
|
|
const parentNode = nearestNode.parentNode as DOMElement |
|
|
let textNode: DOMElement | null = null |
|
|
let offset = 0 |
|
|
|
|
|
if (parentNode) { |
|
|
const editorEl = DOMEditor.toDOMNode(editor, editor) |
|
|
const potentialVoidNode = parentNode.closest('[data-slate-void="true"]') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const voidNode = |
|
|
potentialVoidNode && editorEl.contains(potentialVoidNode) |
|
|
? potentialVoidNode |
|
|
: null |
|
|
const potentialNonEditableNode = parentNode.closest( |
|
|
'[contenteditable="false"]' |
|
|
) |
|
|
const nonEditableNode = |
|
|
potentialNonEditableNode && editorEl.contains(potentialNonEditableNode) |
|
|
? potentialNonEditableNode |
|
|
: null |
|
|
let leafNode = parentNode.closest('[data-slate-leaf]') |
|
|
let domNode: DOMElement | null = null |
|
|
|
|
|
|
|
|
|
|
|
if (leafNode) { |
|
|
textNode = leafNode.closest('[data-slate-node="text"]') |
|
|
|
|
|
if (textNode) { |
|
|
const window = DOMEditor.getWindow(editor) |
|
|
const range = window.document.createRange() |
|
|
range.setStart(textNode, 0) |
|
|
range.setEnd(nearestNode, nearestOffset) |
|
|
|
|
|
const contents = range.cloneContents() |
|
|
const removals = [ |
|
|
...Array.prototype.slice.call( |
|
|
contents.querySelectorAll('[data-slate-zero-width]') |
|
|
), |
|
|
...Array.prototype.slice.call( |
|
|
contents.querySelectorAll('[contenteditable=false]') |
|
|
), |
|
|
] |
|
|
|
|
|
removals.forEach(el => { |
|
|
|
|
|
|
|
|
if ( |
|
|
IS_ANDROID && |
|
|
!exactMatch && |
|
|
el.hasAttribute('data-slate-zero-width') && |
|
|
el.textContent.length > 0 && |
|
|
el.textContext !== '\uFEFF' |
|
|
) { |
|
|
if (el.textContent.startsWith('\uFEFF')) { |
|
|
el.textContent = el.textContent.slice(1) |
|
|
} |
|
|
|
|
|
return |
|
|
} |
|
|
|
|
|
el!.parentNode!.removeChild(el) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
offset = contents.textContent!.length |
|
|
domNode = textNode |
|
|
} |
|
|
} else if (voidNode) { |
|
|
|
|
|
|
|
|
|
|
|
const leafNodes = voidNode.querySelectorAll('[data-slate-leaf]') |
|
|
for (let index = 0; index < leafNodes.length; index++) { |
|
|
const current = leafNodes[index] |
|
|
if (DOMEditor.hasDOMNode(editor, current)) { |
|
|
leafNode = current |
|
|
break |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!leafNode) { |
|
|
offset = 1 |
|
|
} else { |
|
|
textNode = leafNode.closest('[data-slate-node="text"]')! |
|
|
domNode = leafNode |
|
|
offset = domNode.textContent!.length |
|
|
domNode.querySelectorAll('[data-slate-zero-width]').forEach(el => { |
|
|
offset -= el.textContent!.length |
|
|
}) |
|
|
} |
|
|
} else if (nonEditableNode) { |
|
|
|
|
|
const getLeafNodes = (node: DOMElement | null | undefined) => |
|
|
node |
|
|
? node.querySelectorAll( |
|
|
|
|
|
'[data-slate-leaf]:not(:scope [data-slate-editor] [data-slate-leaf])' |
|
|
) |
|
|
: [] |
|
|
const elementNode = nonEditableNode.closest( |
|
|
'[data-slate-node="element"]' |
|
|
) |
|
|
|
|
|
if (searchDirection === 'forward') { |
|
|
const leafNodes = [ |
|
|
...getLeafNodes(elementNode), |
|
|
...getLeafNodes(elementNode?.nextElementSibling), |
|
|
] |
|
|
leafNode = |
|
|
leafNodes.find(leaf => isAfter(nonEditableNode, leaf)) ?? null |
|
|
} else { |
|
|
const leafNodes = [ |
|
|
...getLeafNodes(elementNode?.previousElementSibling), |
|
|
...getLeafNodes(elementNode), |
|
|
] |
|
|
leafNode = |
|
|
leafNodes.findLast(leaf => isBefore(nonEditableNode, leaf)) ?? null |
|
|
} |
|
|
|
|
|
if (leafNode) { |
|
|
textNode = leafNode.closest('[data-slate-node="text"]')! |
|
|
domNode = leafNode |
|
|
if (searchDirection === 'forward') { |
|
|
offset = 0 |
|
|
} else { |
|
|
offset = domNode.textContent!.length |
|
|
domNode.querySelectorAll('[data-slate-zero-width]').forEach(el => { |
|
|
offset -= el.textContent!.length |
|
|
}) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if ( |
|
|
domNode && |
|
|
offset === domNode.textContent!.length && |
|
|
|
|
|
|
|
|
IS_ANDROID && |
|
|
domNode.getAttribute('data-slate-zero-width') === 'z' && |
|
|
domNode.textContent?.startsWith('\uFEFF') && |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(parentNode.hasAttribute('data-slate-zero-width') || |
|
|
|
|
|
|
|
|
|
|
|
(IS_FIREFOX && domNode.textContent?.endsWith('\n\n'))) |
|
|
) { |
|
|
offset-- |
|
|
} |
|
|
} |
|
|
|
|
|
if (IS_ANDROID && !textNode && !exactMatch) { |
|
|
const node = parentNode.hasAttribute('data-slate-node') |
|
|
? parentNode |
|
|
: parentNode.closest('[data-slate-node]') |
|
|
|
|
|
if (node && DOMEditor.hasDOMNode(editor, node, { editable: true })) { |
|
|
const slateNode = DOMEditor.toSlateNode(editor, node) |
|
|
let { path, offset } = Editor.start( |
|
|
editor, |
|
|
DOMEditor.findPath(editor, slateNode) |
|
|
) |
|
|
|
|
|
if (!node.querySelector('[data-slate-leaf]')) { |
|
|
offset = nearestOffset |
|
|
} |
|
|
|
|
|
return { path, offset } as T extends true ? Point | null : Point |
|
|
} |
|
|
} |
|
|
|
|
|
if (!textNode) { |
|
|
if (suppressThrow) { |
|
|
return null as T extends true ? Point | null : Point |
|
|
} |
|
|
throw new Error( |
|
|
`Cannot resolve a Slate point from DOM point: ${domPoint}` |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const slateNode = DOMEditor.toSlateNode(editor, textNode!) |
|
|
const path = DOMEditor.findPath(editor, slateNode) |
|
|
return { path, offset } as T extends true ? Point | null : Point |
|
|
}, |
|
|
|
|
|
toSlateRange: <T extends boolean>( |
|
|
editor: DOMEditor, |
|
|
domRange: DOMRange | DOMStaticRange | DOMSelection, |
|
|
options: { |
|
|
exactMatch: boolean |
|
|
suppressThrow: T |
|
|
} |
|
|
): T extends true ? Range | null : Range => { |
|
|
const { exactMatch, suppressThrow } = options |
|
|
const el = isDOMSelection(domRange) |
|
|
? domRange.anchorNode |
|
|
: domRange.startContainer |
|
|
let anchorNode |
|
|
let anchorOffset |
|
|
let focusNode |
|
|
let focusOffset |
|
|
let isCollapsed |
|
|
|
|
|
if (el) { |
|
|
if (isDOMSelection(domRange)) { |
|
|
|
|
|
|
|
|
if (IS_FIREFOX && domRange.rangeCount > 1) { |
|
|
focusNode = domRange.focusNode |
|
|
const firstRange = domRange.getRangeAt(0) |
|
|
const lastRange = domRange.getRangeAt(domRange.rangeCount - 1) |
|
|
|
|
|
|
|
|
if ( |
|
|
focusNode instanceof HTMLTableRowElement && |
|
|
firstRange.startContainer instanceof HTMLTableRowElement && |
|
|
lastRange.startContainer instanceof HTMLTableRowElement |
|
|
) { |
|
|
|
|
|
function getLastChildren(element: HTMLElement): HTMLElement { |
|
|
if (element.childElementCount > 0) { |
|
|
return getLastChildren(<HTMLElement>element.children[0]) |
|
|
} else { |
|
|
return element |
|
|
} |
|
|
} |
|
|
|
|
|
const firstNodeRow = <HTMLTableRowElement>firstRange.startContainer |
|
|
const lastNodeRow = <HTMLTableRowElement>lastRange.startContainer |
|
|
|
|
|
|
|
|
const firstNode = getLastChildren( |
|
|
<HTMLElement>firstNodeRow.children[firstRange.startOffset] |
|
|
) |
|
|
const lastNode = getLastChildren( |
|
|
<HTMLElement>lastNodeRow.children[lastRange.startOffset] |
|
|
) |
|
|
|
|
|
|
|
|
focusOffset = 0 |
|
|
|
|
|
if (lastNode.childNodes.length > 0) { |
|
|
anchorNode = lastNode.childNodes[0] |
|
|
} else { |
|
|
anchorNode = lastNode |
|
|
} |
|
|
|
|
|
if (firstNode.childNodes.length > 0) { |
|
|
focusNode = firstNode.childNodes[0] |
|
|
} else { |
|
|
focusNode = firstNode |
|
|
} |
|
|
|
|
|
if (lastNode instanceof HTMLElement) { |
|
|
anchorOffset = (<HTMLElement>lastNode).innerHTML.length |
|
|
} else { |
|
|
|
|
|
anchorOffset = 0 |
|
|
} |
|
|
} else { |
|
|
|
|
|
|
|
|
if (firstRange.startContainer === focusNode) { |
|
|
anchorNode = lastRange.endContainer |
|
|
anchorOffset = lastRange.endOffset |
|
|
focusOffset = firstRange.startOffset |
|
|
} else { |
|
|
|
|
|
anchorNode = firstRange.startContainer |
|
|
anchorOffset = firstRange.endOffset |
|
|
focusOffset = lastRange.startOffset |
|
|
} |
|
|
} |
|
|
} else { |
|
|
anchorNode = domRange.anchorNode |
|
|
anchorOffset = domRange.anchorOffset |
|
|
focusNode = domRange.focusNode |
|
|
focusOffset = domRange.focusOffset |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((IS_CHROME && hasShadowRoot(anchorNode)) || IS_FIREFOX) { |
|
|
isCollapsed = |
|
|
domRange.anchorNode === domRange.focusNode && |
|
|
domRange.anchorOffset === domRange.focusOffset |
|
|
} else { |
|
|
isCollapsed = domRange.isCollapsed |
|
|
} |
|
|
} else { |
|
|
anchorNode = domRange.startContainer |
|
|
anchorOffset = domRange.startOffset |
|
|
focusNode = domRange.endContainer |
|
|
focusOffset = domRange.endOffset |
|
|
isCollapsed = domRange.collapsed |
|
|
} |
|
|
} |
|
|
|
|
|
if ( |
|
|
anchorNode == null || |
|
|
focusNode == null || |
|
|
anchorOffset == null || |
|
|
focusOffset == null |
|
|
) { |
|
|
throw new Error( |
|
|
`Cannot resolve a Slate range from DOM range: ${domRange}` |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
IS_FIREFOX && |
|
|
focusNode.textContent?.endsWith('\n\n') && |
|
|
focusOffset === focusNode.textContent.length |
|
|
) { |
|
|
focusOffset-- |
|
|
} |
|
|
|
|
|
const anchor = DOMEditor.toSlatePoint(editor, [anchorNode, anchorOffset], { |
|
|
exactMatch, |
|
|
suppressThrow, |
|
|
}) |
|
|
if (!anchor) { |
|
|
return null as T extends true ? Range | null : Range |
|
|
} |
|
|
|
|
|
const focusBeforeAnchor = |
|
|
isBefore(anchorNode, focusNode) || |
|
|
(anchorNode === focusNode && focusOffset < anchorOffset) |
|
|
const focus = isCollapsed |
|
|
? anchor |
|
|
: DOMEditor.toSlatePoint(editor, [focusNode, focusOffset], { |
|
|
exactMatch, |
|
|
suppressThrow, |
|
|
searchDirection: focusBeforeAnchor ? 'forward' : 'backward', |
|
|
}) |
|
|
if (!focus) { |
|
|
return null as T extends true ? Range | null : Range |
|
|
} |
|
|
|
|
|
let range: Range = { anchor: anchor as Point, focus: focus as Point } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
Range.isExpanded(range) && |
|
|
Range.isForward(range) && |
|
|
isDOMElement(focusNode) && |
|
|
Editor.void(editor, { at: range.focus, mode: 'highest' }) |
|
|
) { |
|
|
range = Editor.unhangRange(editor, range, { voids: true }) |
|
|
} |
|
|
|
|
|
return range as unknown as T extends true ? Range | null : Range |
|
|
}, |
|
|
} |
|
|
|