Download node_modules/@tiptap/react/src/captureDOMSelection.ts from sitcod3/modul-s: direct link, hf CLI and curl.
- Browser
- Download file 1 kB
-
https://huggingface.co/spaces/sitcod3/modul-s/resolve/c06e4783a2be21cd7ca9fedf1ede4d4f0156ba3a/node_modules/%40tiptap/react/src/captureDOMSelection.ts
- Command line
-
hf download hf://spaces/sitcod3/modul-s@c06e4783a2be21cd7ca9fedf1ede4d4f0156ba3a/node_modules/@tiptap/react/src/captureDOMSelection.ts
-
curl -L -o captureDOMSelection.ts https://huggingface.co/spaces/sitcod3/modul-s/resolve/c06e4783a2be21cd7ca9fedf1ede4d4f0156ba3a/node_modules/%40tiptap/react/src/captureDOMSelection.ts
1 kB
| type SelectionRoot = Node & { getSelection?: () => Selection | null } | |
| /** | |
| * Saves the DOM selection inside `element`, returns a function to put it back. | |
| * Browsers drop the selection when an element moves, so restore it after the move. | |
| */ | |
| export function captureDOMSelection(element: HTMLElement): (() => void) | null { | |
| const root = element.getRootNode() as SelectionRoot | |
| const selection = | |
| typeof root.getSelection === 'function' | |
| ? root.getSelection() | |
| : element.ownerDocument?.defaultView?.getSelection() | |
| if (!selection || selection.rangeCount === 0) { | |
| return null | |
| } | |
| const { anchorNode, anchorOffset, focusNode, focusOffset } = selection | |
| if (!anchorNode || !focusNode) { | |
| return null | |
| } | |
| if (!element.contains(anchorNode) || !element.contains(focusNode)) { | |
| return null | |
| } | |
| return () => { | |
| try { | |
| selection.setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset) | |
| } catch { | |
| // The saved nodes can be gone by now. | |
| } | |
| } | |
| } | |