MCPcopy Create free account
hub / github.com/adobe/react-spectrum / createDOMRange

Function createDOMRange

packages/@react-spectrum/ai/src/TokenField.tsx:643–664  ·  view source on GitHub ↗
(root: Element, start: Position, end: Position)

Source from the content-addressed store, hash-verified

641}
642
643function createDOMRange(root: Element, start: Position, end: Position): Range {
644 let range = document.createRange();
645 let child = root.childNodes[start.index];
646 if (!child) {
647 range.setStart(root, Math.min(root.childNodes.length, start.index));
648 } else if (child.nodeType === Node.ELEMENT_NODE) {
649 // Place the cursor in one of the zero width space nodes.
650 range.setStart(child, start.offset > 0 ? 2 : 0);
651 } else {
652 // Place the cursor in the text node.
653 range.setStart(child, start.offset);
654 }
655 child = root.childNodes[end.index];
656 if (!child) {
657 range.setEnd(root, Math.min(root.childNodes.length, end.index));
658 } else if (child.nodeType === Node.ELEMENT_NODE) {
659 range.setEnd(child, end.offset > 0 ? 2 : 0);
660 } else {
661 range.setEnd(child, end.offset);
662 }
663 return range;
664}
665
666function useSelectionChange(ref: React.RefObject<Element | null>, handler: () => void) {
667 useEvent(useRef(document), 'selectionchange', () => {

Callers 2

setSelectionFunction · 0.85
positionToDOMRangeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected