export const sleep = (t: number) => new Promise(r => setTimeout(r, t));

export async function selectTextInElement({
  element,
  startOffset,
  endOffset,
}: {
  element: HTMLElement;
  startOffset: number;
  endOffset: number;
}) {
  // For some reason, it is always good to sleep a bit before trying this
  await sleep(1);

  const firstChild = element.firstChild as ChildNode;
  const range = document.createRange();
  range.setStart(firstChild, startOffset);
  range.setEnd(firstChild, endOffset);

  const selection = document.getSelection() as Selection;
  selection.removeAllRanges();
  selection.addRange(range);
}
