Repository: mozilla/pdf.js
File: test/integration/test_utils.mjs (JavaScript)

Code before:
}

function waitForPositionChange(page, selector, xy) {
  return page.waitForFunction(
    (sel, currentXY) => {
      const bbox = document.querySelector(sel).getBoundingClientRect();
      return `${bbox.x}::${bbox.y}` !== currentXY;
    },
    {},
    selector,
    xy
  );
}

async function moveEditor(page, selector, n, pressKey) {
  let xy = await getXY(page, selector);
  for (let i = 0; i < n; i++) {
    const handle = await waitForEditorMovedInDOM(page);
    await pressKey();
    await awaitPromise(handle);
    await waitForPositionChange(page, selector, xy);
    xy = await getXY(page, selector);
  }
}

async function getNextIdEditor(page) {
  return page.evaluate(() =>
    window.PDFViewerApplication.pdfViewer._layerProperties.annotationEditorUIManager.getNextEditorId()
  );
}

async function highlightSpan(
  page,
  pageIndex,
  text,
  xRatio = 0.5,
  yRatio = 0.5
) {
  const nextId = await getNextIdEditor(page);
  const rect = await getSpanRectFromText(page, pageIndex, text);
  const x = rect.x + rect.width * xRatio;
  const y = rect.y + rect.height * yRatio;
  // We add a small delay between press and release to make sure that a
  // pointerup event is triggered after selectionchange.
  // It works with a value of 1ms, but we use 100ms to be sure.
  await page.mouse.click(x, y, { count: 2, delay: 100 });
  await page.waitForSelector(`#${nextId}`);
}

// Unicode bidi isolation characters, Fluent adds these markers to the text.
const FSI = "\u2068";

Review comment (suggestion):
Let's rename this to `getNextEditorId` since that is a bit easier to read and matches the method name on the UI manager.

Code after:
}

function waitForPositionChange(page, selector, xy) {
  return page.waitForFunction(
    (sel, currentXY) => {
      const bbox = document.querySelector(sel).getBoundingClientRect();
      return `${bbox.x}::${bbox.y}` !== currentXY;
    },
    {},
    selector,
    xy
  );
}

async function moveEditor(page, selector, n, pressKey) {
  let xy = await getXY(page, selector);
  for (let i = 0; i < n; i++) {
    const handle = await waitForEditorMovedInDOM(page);
    await pressKey();
    await awaitPromise(handle);
    await waitForPositionChange(page, selector, xy);
    xy = await getXY(page, selector);
  }
}

async function getNextEditorId(page) {
  return page.evaluate(() =>
    window.PDFViewerApplication.pdfViewer._layerProperties.annotationEditorUIManager.getNextEditorId()
  );
}

async function highlightSpan(
  page,
  pageIndex,
  text,
  xRatio = 0.5,
  yRatio = 0.5
) {
  const nextId = await getNextEditorId(page);
  const rect = await getSpanRectFromText(page, pageIndex, text);
  const x = rect.x + rect.width * xRatio;
  const y = rect.y + rect.height * yRatio;
  // We add a small delay between press and release to make sure that a
  // pointerup event is triggered after selectionchange.
  // It works with a value of 1ms, but we use 100ms to be sure.
  await page.mouse.click(x, y, { count: 2, delay: 100 });
  await page.waitForSelector(getEditorSelector(nextId));
}

// Unicode bidi isolation characters, Fluent adds these markers to the text.
const FSI = "\u2068";