captureSelection = (
  callback,
  { granularity, beforeCleanup, window } = {
    granularity: "symbol",
  },
) => {
  const selection = window.getSelection();

  if (selection.isCollapsed) return;
  if (granularity !== "symbol") {
    trimSelection(selection);
  }

  if (selection.isCollapsed) return;

  applyTextGranularity(selection, granularity);

  const selectionText = selection.toString().replace(/[\n\r]/g, "\\n");

  for (let i = 0; i < selection.rangeCount; i++) {
    const range = fixRange(selection.getRangeAt(i));

    callback({ selectionText, range });
  }

  // eslint-disable-next-line no-unused-expressions
  beforeCleanup?.();

  selection.removeAllRanges();
}
Captures current selection
@param {(response: {selectionText: string, range: Range}) => void} callback