@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.
@@ -479,6 +479,19 @@ function clipRange(range, container) {
479
479
  }
480
480
  return clipped;
481
481
  }
482
+ function textClientRects(range, container) {
483
+ const walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT);
484
+ const rects = [];
485
+ for (let node = walker.nextNode(); node; node = walker.nextNode()) {
486
+ if (!node.textContent?.trim() || !range.intersectsNode(node)) continue;
487
+ const part = document.createRange();
488
+ part.selectNodeContents(node);
489
+ if (node === range.startContainer) part.setStart(node, range.startOffset);
490
+ if (node === range.endContainer) part.setEnd(node, range.endOffset);
491
+ rects.push(...Array.from(part.getClientRects()));
492
+ }
493
+ return rects;
494
+ }
482
495
  function readSelection(selection, root = document) {
483
496
  if (selection.rangeCount === 0 || selection.isCollapsed) return [];
484
497
  const range = selection.getRangeAt(0);
@@ -492,7 +505,7 @@ function readSelection(selection, root = document) {
492
505
  pages.push({
493
506
  page,
494
507
  box: toPixelRect(overlay.getBoundingClientRect()),
495
- rects: Array.from(clipped.getClientRects(), toPixelRect),
508
+ rects: textClientRects(clipped, container).map(toPixelRect),
496
509
  text: clipped.toString()
497
510
  });
498
511
  });
@@ -502,6 +515,9 @@ function readSelection(selection, root = document) {
502
515
  // src/annotations/useTextSelectionMarks.ts
503
516
  var PAGE_SELECTOR = "[data-page-number]";
504
517
  var TOUCH_SETTLE_MS = 700;
518
+ var MOUSE_SETTLE_MS = 80;
519
+ var STABLE_CHECKS = 3;
520
+ var MAX_SETTLE_ROUNDS = 12;
505
521
  function useTextSelectionMarks(controller) {
506
522
  const latest = react.useRef(controller);
507
523
  latest.current = controller;
@@ -509,6 +525,7 @@ function useTextSelectionMarks(controller) {
509
525
  let startedOnPage = false;
510
526
  let touchSelecting = false;
511
527
  let settleTimer;
528
+ const settleTimers = /* @__PURE__ */ new Set();
512
529
  const markSelection = () => {
513
530
  const c = latest.current;
514
531
  const selection = window.getSelection();
@@ -542,9 +559,23 @@ function useTextSelectionMarks(controller) {
542
559
  const onUp = () => {
543
560
  if (!startedOnPage || !TEXT_TOOLS.includes(latest.current.tool)) return;
544
561
  startedOnPage = false;
545
- setTimeout(() => {
546
- if (markSelection()) touchSelecting = false;
547
- }, 0);
562
+ let last = window.getSelection()?.toString() ?? "";
563
+ let stable = 0;
564
+ let rounds = 0;
565
+ const check = () => {
566
+ const now = window.getSelection()?.toString() ?? "";
567
+ if (now === last) stable++;
568
+ else {
569
+ last = now;
570
+ stable = 0;
571
+ }
572
+ if (stable >= STABLE_CHECKS || ++rounds >= MAX_SETTLE_ROUNDS) {
573
+ if (markSelection()) touchSelecting = false;
574
+ return;
575
+ }
576
+ settleTimers.add(setTimeout(check, MOUSE_SETTLE_MS));
577
+ };
578
+ settleTimers.add(setTimeout(check, MOUSE_SETTLE_MS));
548
579
  };
549
580
  const onSelectionChange = () => {
550
581
  if (!touchSelecting || !TEXT_TOOLS.includes(latest.current.tool)) return;
@@ -558,6 +589,7 @@ function useTextSelectionMarks(controller) {
558
589
  document.addEventListener("selectionchange", onSelectionChange);
559
590
  return () => {
560
591
  clearTimeout(settleTimer);
592
+ settleTimers.forEach(clearTimeout);
561
593
  document.removeEventListener("pointerdown", onDown, true);
562
594
  document.removeEventListener("pointerup", onUp, true);
563
595
  document.removeEventListener("selectionchange", onSelectionChange);