@arbidocs/react 0.4.56 → 0.4.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -478,6 +478,19 @@ function clipRange(range, container) {
478
478
  }
479
479
  return clipped;
480
480
  }
481
+ function textClientRects(range, container) {
482
+ const walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT);
483
+ const rects = [];
484
+ for (let node = walker.nextNode(); node; node = walker.nextNode()) {
485
+ if (!node.textContent?.trim() || !range.intersectsNode(node)) continue;
486
+ const part = document.createRange();
487
+ part.selectNodeContents(node);
488
+ if (node === range.startContainer) part.setStart(node, range.startOffset);
489
+ if (node === range.endContainer) part.setEnd(node, range.endOffset);
490
+ rects.push(...Array.from(part.getClientRects()));
491
+ }
492
+ return rects;
493
+ }
481
494
  function readSelection(selection, root = document) {
482
495
  if (selection.rangeCount === 0 || selection.isCollapsed) return [];
483
496
  const range = selection.getRangeAt(0);
@@ -491,7 +504,7 @@ function readSelection(selection, root = document) {
491
504
  pages.push({
492
505
  page,
493
506
  box: toPixelRect(overlay.getBoundingClientRect()),
494
- rects: Array.from(clipped.getClientRects(), toPixelRect),
507
+ rects: textClientRects(clipped, container).map(toPixelRect),
495
508
  text: clipped.toString()
496
509
  });
497
510
  });
@@ -501,6 +514,9 @@ function readSelection(selection, root = document) {
501
514
  // src/annotations/useTextSelectionMarks.ts
502
515
  var PAGE_SELECTOR = "[data-page-number]";
503
516
  var TOUCH_SETTLE_MS = 700;
517
+ var MOUSE_SETTLE_MS = 80;
518
+ var STABLE_CHECKS = 3;
519
+ var MAX_SETTLE_ROUNDS = 12;
504
520
  function useTextSelectionMarks(controller) {
505
521
  const latest = useRef(controller);
506
522
  latest.current = controller;
@@ -508,6 +524,7 @@ function useTextSelectionMarks(controller) {
508
524
  let startedOnPage = false;
509
525
  let touchSelecting = false;
510
526
  let settleTimer;
527
+ const settleTimers = /* @__PURE__ */ new Set();
511
528
  const markSelection = () => {
512
529
  const c = latest.current;
513
530
  const selection = window.getSelection();
@@ -541,9 +558,23 @@ function useTextSelectionMarks(controller) {
541
558
  const onUp = () => {
542
559
  if (!startedOnPage || !TEXT_TOOLS.includes(latest.current.tool)) return;
543
560
  startedOnPage = false;
544
- setTimeout(() => {
545
- if (markSelection()) touchSelecting = false;
546
- }, 0);
561
+ let last = window.getSelection()?.toString() ?? "";
562
+ let stable = 0;
563
+ let rounds = 0;
564
+ const check = () => {
565
+ const now = window.getSelection()?.toString() ?? "";
566
+ if (now === last) stable++;
567
+ else {
568
+ last = now;
569
+ stable = 0;
570
+ }
571
+ if (stable >= STABLE_CHECKS || ++rounds >= MAX_SETTLE_ROUNDS) {
572
+ if (markSelection()) touchSelecting = false;
573
+ return;
574
+ }
575
+ settleTimers.add(setTimeout(check, MOUSE_SETTLE_MS));
576
+ };
577
+ settleTimers.add(setTimeout(check, MOUSE_SETTLE_MS));
547
578
  };
548
579
  const onSelectionChange = () => {
549
580
  if (!touchSelecting || !TEXT_TOOLS.includes(latest.current.tool)) return;
@@ -557,6 +588,7 @@ function useTextSelectionMarks(controller) {
557
588
  document.addEventListener("selectionchange", onSelectionChange);
558
589
  return () => {
559
590
  clearTimeout(settleTimer);
591
+ settleTimers.forEach(clearTimeout);
560
592
  document.removeEventListener("pointerdown", onDown, true);
561
593
  document.removeEventListener("pointerup", onUp, true);
562
594
  document.removeEventListener("selectionchange", onSelectionChange);