MCPcopy Create free account
hub / github.com/editablejs/editable / findClosestPoint

Function findClosestPoint

packages/editor/src/plugin/editable.ts:524–667  ·  view source on GitHub ↗
(editor: Editor, domNode: DOMNode, x: number, y: number)

Source from the content-addressed store, hash-verified

522 },
523
524 findClosestPoint(editor: Editor, domNode: DOMNode, x: number, y: number): Point | null {
525 const domEl = isDOMElement(domNode) ? domNode : domNode.parentElement
526 if (!domEl) return null
527 const elements: DOMElement[] = []
528 let element: DOMElement | null = domEl.hasAttribute(DATA_EDITABLE_NODE)
529 ? domEl
530 : domEl.closest(`[${DATA_EDITABLE_NODE}]`)
531
532 const addToElements = (node: Node) => {
533 if (!NODE_TO_ELEMENT.get(node)) return
534 const children = Editable.findLowestDOMElements(editor, node)
535 for (const child of children) {
536 if (~elements.indexOf(child)) continue
537 elements.push(child)
538 }
539 }
540
541 if (!element) {
542 const nodes = Node.nodes(editor)
543 for (const [node] of nodes) {
544 addToElements(node)
545 }
546 } else {
547 const node = Editable.toEditorNode(editor, element)
548 if (Text.isText(node) || Editor.isVoid(editor, node)) {
549 addToElements(node)
550 } else {
551 if (!Editor.isSolidVoid(editor, node)) {
552 const rect = element.getBoundingClientRect()
553 const reverse = x < rect.left + rect.width / 2
554 const adjacent = (reverse ? Editor.previous : Editor.next)(editor, {
555 at: Editable.findPath(editor, node),
556 })
557 if (adjacent) {
558 addToElements(adjacent[0])
559 }
560 } else {
561 const isGrid = Editor.isGrid(editor, node)
562 const nodes = Editor.nodes(editor, {
563 at: Editable.findPath(editor, node),
564 match: n => (isGrid && Editor.isGridCell(editor, n)) || Text.isText(n),
565 mode: 'highest',
566 })
567 for (const [child] of nodes) {
568 if (Editor.isBlock(editor, child)) {
569 elements.push(Editable.toDOMNode(editor, child))
570 } else addToElements(child)
571 }
572 }
573 }
574 }
575 let top = y,
576 left = x
577 const nodes = findNearbyNodes(elements, x, y)
578 if (!nodes) return null
579 let offsetNode: DOMElement | null = null
580 if (isDOMNode(nodes)) {
581 offsetNode = nodes

Callers

nothing calls this directly

Calls 7

isDOMElementFunction · 0.90
isDOMNodeFunction · 0.90
isAlignYFunction · 0.90
isDOMTextFunction · 0.90
getTextOffsetFunction · 0.90
addToElementsFunction · 0.85
findNearbyNodesFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…