UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

35 lines (34 loc) 1.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.saveSelection = exports.unit = exports.getCursorPosition = void 0; exports.getCursorPosition = typeof document !== 'undefined' && document.caretPositionFromPoint ? (x, y) => { const range = document.caretPositionFromPoint(x, y); return range ? [range.offsetNode, range.offset] : null; } : (x, y) => { const range = document.caretRangeFromPoint(x, y); return range ? [range.startContainer, range.startOffset] : null; }; const unit = (event) => event.metaKey ? 'vline' : event.altKey || event.ctrlKey ? 'word' : ''; exports.unit = unit; /** * Save the current browser selection, so that it can be restored later. Returns * a callback to restore the selection. * * @returns Callback to restore the selection. */ const saveSelection = () => { const selection = window?.getSelection(); if (!selection) return; const ranges = []; for (let i = 0; i < selection.rangeCount; i++) ranges.push(selection.getRangeAt(i)); return () => { selection.removeAllRanges(); for (const range of ranges) selection.addRange(range); }; }; exports.saveSelection = saveSelection;