All files / util dom.ts

15.38% Statements 2/13
0% Branches 0/1
0% Functions 0/2
12.5% Lines 1/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17            1x                    
/**
 * 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.
 */
export const saveSelection = (): (() => void) | undefined => {
  const selection = window?.getSelection();
  Iif (!selection) return;
  const ranges: Range[] = [];
  for (let i = 0; i < selection.rangeCount; i++) ranges.push(selection.getRangeAt(i));
  return () => {
    selection.removeAllRanges();
    for (const range of ranges) selection.addRange(range);
  };
};