@embedpdf/plugin-annotation 2.7.0 → 2.8.0

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.
Files changed (128) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.js +335 -82
  4. package/dist/index.js.map +1 -1
  5. package/dist/lib/annotation-plugin.d.ts +2 -0
  6. package/dist/lib/handlers/index.d.ts +4 -0
  7. package/dist/lib/handlers/insert-text.handler.d.ts +8 -0
  8. package/dist/lib/handlers/replace-text.handler.d.ts +9 -0
  9. package/dist/lib/handlers/selection-utils.d.ts +7 -0
  10. package/dist/lib/handlers/text-markup.handler.d.ts +7 -0
  11. package/dist/lib/handlers/text.handler.d.ts +3 -0
  12. package/dist/lib/handlers/types.d.ts +12 -0
  13. package/dist/lib/helpers.d.ts +2 -1
  14. package/dist/lib/selectors.d.ts +6 -1
  15. package/dist/lib/tools/default-tools.d.ts +151 -6
  16. package/dist/lib/tools/tools-utils.d.ts +2 -0
  17. package/dist/lib/tools/types.d.ts +2 -0
  18. package/dist/lib/types.d.ts +1 -0
  19. package/dist/preact/index.cjs +1 -1
  20. package/dist/preact/index.cjs.map +1 -1
  21. package/dist/preact/index.js +277 -43
  22. package/dist/preact/index.js.map +1 -1
  23. package/dist/react/index.cjs +1 -1
  24. package/dist/react/index.cjs.map +1 -1
  25. package/dist/react/index.js +277 -43
  26. package/dist/react/index.js.map +1 -1
  27. package/dist/shared/annotation-bounds.d.ts +14 -0
  28. package/dist/shared/components/annotations/caret.d.ts +24 -0
  29. package/dist/shared/components/annotations/circle.d.ts +2 -2
  30. package/dist/shared/components/annotations/free-text.d.ts +2 -2
  31. package/dist/shared/components/annotations/ink.d.ts +2 -2
  32. package/dist/shared/components/annotations/line.d.ts +2 -2
  33. package/dist/shared/components/annotations/link.d.ts +2 -2
  34. package/dist/shared/components/annotations/polygon.d.ts +2 -2
  35. package/dist/shared/components/annotations/polyline.d.ts +8 -4
  36. package/dist/shared/components/annotations/square.d.ts +2 -2
  37. package/dist/shared/components/annotations/stamp.d.ts +2 -2
  38. package/dist/shared/components/annotations/text.d.ts +14 -0
  39. package/dist/shared/components/text-markup/highlight.d.ts +2 -2
  40. package/dist/shared/components/text-markup/squiggly.d.ts +2 -2
  41. package/dist/shared/components/text-markup/strikeout.d.ts +2 -2
  42. package/dist/shared/components/text-markup/underline.d.ts +2 -2
  43. package/dist/shared/components/types.d.ts +2 -2
  44. package/dist/shared-preact/annotation-bounds.d.ts +14 -0
  45. package/dist/shared-preact/components/annotations/caret.d.ts +24 -0
  46. package/dist/shared-preact/components/annotations/circle.d.ts +2 -2
  47. package/dist/shared-preact/components/annotations/free-text.d.ts +2 -2
  48. package/dist/shared-preact/components/annotations/ink.d.ts +2 -2
  49. package/dist/shared-preact/components/annotations/line.d.ts +2 -2
  50. package/dist/shared-preact/components/annotations/link.d.ts +2 -2
  51. package/dist/shared-preact/components/annotations/polygon.d.ts +2 -2
  52. package/dist/shared-preact/components/annotations/polyline.d.ts +8 -4
  53. package/dist/shared-preact/components/annotations/square.d.ts +2 -2
  54. package/dist/shared-preact/components/annotations/stamp.d.ts +2 -2
  55. package/dist/shared-preact/components/annotations/text.d.ts +14 -0
  56. package/dist/shared-preact/components/text-markup/highlight.d.ts +2 -2
  57. package/dist/shared-preact/components/text-markup/squiggly.d.ts +2 -2
  58. package/dist/shared-preact/components/text-markup/strikeout.d.ts +2 -2
  59. package/dist/shared-preact/components/text-markup/underline.d.ts +2 -2
  60. package/dist/shared-preact/components/types.d.ts +2 -2
  61. package/dist/shared-react/annotation-bounds.d.ts +14 -0
  62. package/dist/shared-react/components/annotations/caret.d.ts +24 -0
  63. package/dist/shared-react/components/annotations/circle.d.ts +2 -2
  64. package/dist/shared-react/components/annotations/free-text.d.ts +2 -2
  65. package/dist/shared-react/components/annotations/ink.d.ts +2 -2
  66. package/dist/shared-react/components/annotations/line.d.ts +2 -2
  67. package/dist/shared-react/components/annotations/link.d.ts +2 -2
  68. package/dist/shared-react/components/annotations/polygon.d.ts +2 -2
  69. package/dist/shared-react/components/annotations/polyline.d.ts +8 -4
  70. package/dist/shared-react/components/annotations/square.d.ts +2 -2
  71. package/dist/shared-react/components/annotations/stamp.d.ts +2 -2
  72. package/dist/shared-react/components/annotations/text.d.ts +14 -0
  73. package/dist/shared-react/components/text-markup/highlight.d.ts +2 -2
  74. package/dist/shared-react/components/text-markup/squiggly.d.ts +2 -2
  75. package/dist/shared-react/components/text-markup/strikeout.d.ts +2 -2
  76. package/dist/shared-react/components/text-markup/underline.d.ts +2 -2
  77. package/dist/shared-react/components/types.d.ts +2 -2
  78. package/dist/shared-vue/annotation-bounds.d.ts +14 -0
  79. package/dist/svelte/components/annotations/Caret.svelte.d.ts +13 -0
  80. package/dist/svelte/components/annotations/Circle.svelte.d.ts +1 -1
  81. package/dist/svelte/components/annotations/FreeText.svelte.d.ts +1 -1
  82. package/dist/svelte/components/annotations/Ink.svelte.d.ts +1 -1
  83. package/dist/svelte/components/annotations/Line.svelte.d.ts +1 -1
  84. package/dist/svelte/components/annotations/Link.svelte.d.ts +1 -1
  85. package/dist/svelte/components/annotations/Polygon.svelte.d.ts +1 -1
  86. package/dist/svelte/components/annotations/Polyline.svelte.d.ts +4 -2
  87. package/dist/svelte/components/annotations/Square.svelte.d.ts +1 -1
  88. package/dist/svelte/components/annotations/Stamp.svelte.d.ts +1 -1
  89. package/dist/svelte/components/annotations/Text.svelte.d.ts +10 -0
  90. package/dist/svelte/components/annotations/index.d.ts +1 -0
  91. package/dist/svelte/components/renderers/CaretRenderer.svelte.d.ts +5 -0
  92. package/dist/svelte/components/renderers/TextRenderer.svelte.d.ts +5 -0
  93. package/dist/svelte/components/text-markup/Highlight.svelte.d.ts +1 -1
  94. package/dist/svelte/components/text-markup/Squiggly.svelte.d.ts +1 -1
  95. package/dist/svelte/components/text-markup/Strikeout.svelte.d.ts +1 -1
  96. package/dist/svelte/components/text-markup/Underline.svelte.d.ts +1 -1
  97. package/dist/svelte/context/types.d.ts +1 -1
  98. package/dist/svelte/index.cjs +1 -1
  99. package/dist/svelte/index.cjs.map +1 -1
  100. package/dist/svelte/index.js +350 -147
  101. package/dist/svelte/index.js.map +1 -1
  102. package/dist/vue/components/annotation-container.vue.d.ts +2 -2
  103. package/dist/vue/components/annotation-layer.vue.d.ts +1 -1
  104. package/dist/vue/components/annotations/caret.vue.d.ts +24 -0
  105. package/dist/vue/components/annotations/circle.vue.d.ts +2 -2
  106. package/dist/vue/components/annotations/free-text.vue.d.ts +2 -2
  107. package/dist/vue/components/annotations/index.d.ts +1 -0
  108. package/dist/vue/components/annotations/ink.vue.d.ts +2 -2
  109. package/dist/vue/components/annotations/line.vue.d.ts +2 -2
  110. package/dist/vue/components/annotations/link.vue.d.ts +2 -2
  111. package/dist/vue/components/annotations/polygon.vue.d.ts +2 -2
  112. package/dist/vue/components/annotations/polyline.vue.d.ts +8 -3
  113. package/dist/vue/components/annotations/square.vue.d.ts +2 -2
  114. package/dist/vue/components/annotations/stamp.vue.d.ts +2 -2
  115. package/dist/vue/components/annotations/text.vue.d.ts +14 -0
  116. package/dist/vue/components/annotations.vue.d.ts +1 -1
  117. package/dist/vue/components/renderers/caret-renderer.vue.d.ts +6 -0
  118. package/dist/vue/components/renderers/text-renderer.vue.d.ts +6 -0
  119. package/dist/vue/components/text-markup/highlight.vue.d.ts +2 -2
  120. package/dist/vue/components/text-markup/squiggly.vue.d.ts +2 -2
  121. package/dist/vue/components/text-markup/strikeout.vue.d.ts +2 -2
  122. package/dist/vue/components/text-markup/underline.vue.d.ts +2 -2
  123. package/dist/vue/context/types.d.ts +1 -1
  124. package/dist/vue/index.cjs +1 -1
  125. package/dist/vue/index.cjs.map +1 -1
  126. package/dist/vue/index.js +535 -249
  127. package/dist/vue/index.js.map +1 -1
  128. package/package.json +10 -10
package/dist/index.js CHANGED
@@ -156,8 +156,11 @@ function isLink(a) {
156
156
  function isRedact(a) {
157
157
  return a.object.type === PdfAnnotationSubtype.REDACT;
158
158
  }
159
+ function isCaret(a) {
160
+ return a.object.type === PdfAnnotationSubtype.CARET;
161
+ }
159
162
  function isSidebarAnnotation(a) {
160
- return isTextMarkup(a) || isInk(a) || isSquare(a) || isCircle(a) || isPolygon(a) || isLine(a) || isPolyline(a) || isFreeText(a) || isStamp(a) || isRedact(a);
163
+ return isText(a) && !a.object.inReplyToId || isTextMarkup(a) || isInk(a) || isSquare(a) || isCircle(a) || isPolygon(a) || isLine(a) || isPolyline(a) || isFreeText(a) || isStamp(a) || isRedact(a) || isCaret(a);
161
164
  }
162
165
  const getAnnotationsByPageIndex = (s, page) => (s.pages[page] ?? []).map((uid) => s.byUid[uid]);
163
166
  const getAnnotations = (s) => {
@@ -199,19 +202,33 @@ const getSidebarAnnotationsWithRepliesGroupedByPage = (s) => {
199
202
  }
200
203
  }
201
204
  }
205
+ const membersByLeader = {};
206
+ const consumedAsGroupMember = /* @__PURE__ */ new Set();
207
+ for (const uidList of Object.values(s.pages)) {
208
+ for (const uid of uidList) {
209
+ const ta = s.byUid[uid];
210
+ if (ta && ta.object.inReplyToId && ta.object.replyType === PdfAnnotationReplyType.Group && ta.object.type !== PdfAnnotationSubtype.LINK && isSidebarAnnotation(ta)) {
211
+ const leaderId = ta.object.inReplyToId;
212
+ (membersByLeader[leaderId] || (membersByLeader[leaderId] = [])).push(ta);
213
+ consumedAsGroupMember.add(ta.object.id);
214
+ }
215
+ }
216
+ }
202
217
  const out = {};
203
218
  for (const [pageStr, uidList] of Object.entries(s.pages)) {
204
219
  const page = Number(pageStr);
205
220
  const pageAnnotations = [];
206
221
  for (const uid of uidList) {
207
222
  const ta = s.byUid[uid];
208
- if (ta && isSidebarAnnotation(ta)) {
209
- pageAnnotations.push({
210
- page,
211
- annotation: ta,
212
- replies: repliesByParent[ta.object.id] ?? []
213
- });
214
- }
223
+ if (!ta || !isSidebarAnnotation(ta)) continue;
224
+ if (consumedAsGroupMember.has(ta.object.id)) continue;
225
+ const members = membersByLeader[ta.object.id];
226
+ pageAnnotations.push({
227
+ page,
228
+ annotation: ta,
229
+ replies: repliesByParent[ta.object.id] ?? [],
230
+ ...members && members.length > 0 ? { groupMembers: members } : {}
231
+ });
215
232
  }
216
233
  if (pageAnnotations.length > 0) {
217
234
  out[page] = pageAnnotations;
@@ -334,6 +351,7 @@ const defaultTools = [
334
351
  textSelection: true,
335
352
  isDraggable: false,
336
353
  isResizable: false,
354
+ isRotatable: false,
337
355
  // Text markup annotations are anchored to text and should not move/resize in groups
338
356
  isGroupDraggable: false,
339
357
  isGroupResizable: false
@@ -356,6 +374,7 @@ const defaultTools = [
356
374
  textSelection: true,
357
375
  isDraggable: false,
358
376
  isResizable: false,
377
+ isRotatable: false,
359
378
  isGroupDraggable: false,
360
379
  isGroupResizable: false
361
380
  },
@@ -376,6 +395,7 @@ const defaultTools = [
376
395
  textSelection: true,
377
396
  isDraggable: false,
378
397
  isResizable: false,
398
+ isRotatable: false,
379
399
  isGroupDraggable: false,
380
400
  isGroupResizable: false
381
401
  },
@@ -396,6 +416,7 @@ const defaultTools = [
396
416
  textSelection: true,
397
417
  isDraggable: false,
398
418
  isResizable: false,
419
+ isRotatable: false,
399
420
  isGroupDraggable: false,
400
421
  isGroupResizable: false
401
422
  },
@@ -407,6 +428,59 @@ const defaultTools = [
407
428
  opacity: 1
408
429
  }
409
430
  },
431
+ // Insert Text (Caret with intent Insert)
432
+ {
433
+ id: "insertText",
434
+ name: "Insert Text",
435
+ matchScore: (a) => {
436
+ var _a;
437
+ if (a.type !== PdfAnnotationSubtype.CARET) return 0;
438
+ return ((_a = a.intent) == null ? void 0 : _a.includes("Insert")) ? 2 : 1;
439
+ },
440
+ interaction: {
441
+ exclusive: false,
442
+ textSelection: true,
443
+ showSelectionRects: true,
444
+ isDraggable: false,
445
+ isResizable: false,
446
+ isRotatable: false,
447
+ isGroupDraggable: false,
448
+ isGroupResizable: false
449
+ },
450
+ defaults: {
451
+ type: PdfAnnotationSubtype.CARET,
452
+ strokeColor: "#E44234",
453
+ opacity: 1,
454
+ intent: "Insert"
455
+ }
456
+ },
457
+ // Replace Text (StrikeOut + Caret group)
458
+ {
459
+ id: "replaceText",
460
+ name: "Replace Text",
461
+ matchScore: (a) => {
462
+ var _a, _b;
463
+ if (a.type === PdfAnnotationSubtype.STRIKEOUT && ((_a = a.intent) == null ? void 0 : _a.includes("StrikeOutTextEdit")))
464
+ return 2;
465
+ if (a.type === PdfAnnotationSubtype.CARET && ((_b = a.intent) == null ? void 0 : _b.includes("Replace"))) return 2;
466
+ return 0;
467
+ },
468
+ interaction: {
469
+ exclusive: false,
470
+ textSelection: true,
471
+ isDraggable: false,
472
+ isResizable: false,
473
+ isRotatable: false,
474
+ isGroupDraggable: false,
475
+ isGroupResizable: false
476
+ },
477
+ defaults: {
478
+ type: PdfAnnotationSubtype.STRIKEOUT,
479
+ strokeColor: "#E44234",
480
+ opacity: 1,
481
+ intent: "StrikeOutTextEdit"
482
+ }
483
+ },
410
484
  // Drawing Tools
411
485
  {
412
486
  id: "ink",
@@ -631,6 +705,26 @@ const defaultTools = [
631
705
  }
632
706
  },
633
707
  // Text & Stamp
708
+ {
709
+ id: "textComment",
710
+ name: "Comment",
711
+ matchScore: (a) => a.type === PdfAnnotationSubtype.TEXT && !a.inReplyToId ? 1 : 0,
712
+ interaction: {
713
+ exclusive: false,
714
+ cursor: "crosshair",
715
+ isDraggable: true,
716
+ isResizable: false,
717
+ isRotatable: false
718
+ },
719
+ defaults: {
720
+ type: PdfAnnotationSubtype.TEXT,
721
+ strokeColor: "#FFCD45",
722
+ opacity: 1
723
+ },
724
+ behavior: {
725
+ selectAfterCreate: true
726
+ }
727
+ },
634
728
  {
635
729
  id: "freeText",
636
730
  name: "Free Text",
@@ -952,7 +1046,7 @@ const reducer = (state, action) => {
952
1046
  },
953
1047
  byUid: {
954
1048
  ...docState.byUid,
955
- [uid]: { commitState: "new", object: annotation }
1049
+ [uid]: { commitState: "new", object: annotation, dictMode: true }
956
1050
  },
957
1051
  hasPendingChanges: true
958
1052
  }
@@ -1182,45 +1276,6 @@ const inkHandlerFactory = {
1182
1276
  };
1183
1277
  }
1184
1278
  };
1185
- function useClickDetector({
1186
- threshold = 5,
1187
- getTool,
1188
- onClickDetected
1189
- }) {
1190
- const [getStartPos, setStartPos] = useState(null);
1191
- const [getHasMoved, setHasMoved] = useState(false);
1192
- return {
1193
- onStart: (pos) => {
1194
- setStartPos(pos);
1195
- setHasMoved(false);
1196
- },
1197
- onMove: (pos) => {
1198
- const start = getStartPos();
1199
- if (!start || getHasMoved()) return;
1200
- const distance = Math.sqrt(Math.pow(pos.x - start.x, 2) + Math.pow(pos.y - start.y, 2));
1201
- if (distance > threshold) {
1202
- setHasMoved(true);
1203
- }
1204
- },
1205
- onEnd: (pos) => {
1206
- var _a;
1207
- const start = getStartPos();
1208
- if (start && !getHasMoved()) {
1209
- const tool = getTool();
1210
- if (tool && "clickBehavior" in tool && ((_a = tool.clickBehavior) == null ? void 0 : _a.enabled)) {
1211
- onClickDetected(pos, tool);
1212
- }
1213
- }
1214
- setStartPos(null);
1215
- setHasMoved(false);
1216
- },
1217
- hasMoved: getHasMoved,
1218
- reset: () => {
1219
- setStartPos(null);
1220
- setHasMoved(false);
1221
- }
1222
- };
1223
- }
1224
1279
  function createArrowHandler(isClosed) {
1225
1280
  const calculateGeometry = (sw) => {
1226
1281
  const len = sw * 9;
@@ -1601,6 +1656,75 @@ const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.definePropert
1601
1656
  rotatePointAroundCenter: rotatePointAround,
1602
1657
  rotateVertices
1603
1658
  }, Symbol.toStringTag, { value: "Module" }));
1659
+ const COMMENT_SIZE = 24;
1660
+ const textHandlerFactory = {
1661
+ annotationType: PdfAnnotationSubtype.TEXT,
1662
+ create(context) {
1663
+ const { onCommit, getTool, pageSize } = context;
1664
+ return {
1665
+ onPointerDown: (pos) => {
1666
+ const tool = getTool();
1667
+ if (!tool) return;
1668
+ const rect = {
1669
+ origin: { x: pos.x - COMMENT_SIZE / 2, y: pos.y - COMMENT_SIZE / 2 },
1670
+ size: { width: COMMENT_SIZE, height: COMMENT_SIZE }
1671
+ };
1672
+ let anno = {
1673
+ ...tool.defaults,
1674
+ rect,
1675
+ type: PdfAnnotationSubtype.TEXT,
1676
+ icon: tool.defaults.icon ?? PdfAnnotationIcon.Comment,
1677
+ contents: tool.defaults.contents ?? "",
1678
+ flags: tool.defaults.flags ?? ["print", "noRotate", "noZoom"],
1679
+ pageIndex: context.pageIndex,
1680
+ id: uuidV4(),
1681
+ created: /* @__PURE__ */ new Date()
1682
+ };
1683
+ anno = clampAnnotationToPage(anno, pageSize);
1684
+ onCommit(anno);
1685
+ }
1686
+ };
1687
+ }
1688
+ };
1689
+ function useClickDetector({
1690
+ threshold = 5,
1691
+ getTool,
1692
+ onClickDetected
1693
+ }) {
1694
+ const [getStartPos, setStartPos] = useState(null);
1695
+ const [getHasMoved, setHasMoved] = useState(false);
1696
+ return {
1697
+ onStart: (pos) => {
1698
+ setStartPos(pos);
1699
+ setHasMoved(false);
1700
+ },
1701
+ onMove: (pos) => {
1702
+ const start = getStartPos();
1703
+ if (!start || getHasMoved()) return;
1704
+ const distance = Math.sqrt(Math.pow(pos.x - start.x, 2) + Math.pow(pos.y - start.y, 2));
1705
+ if (distance > threshold) {
1706
+ setHasMoved(true);
1707
+ }
1708
+ },
1709
+ onEnd: (pos) => {
1710
+ var _a;
1711
+ const start = getStartPos();
1712
+ if (start && !getHasMoved()) {
1713
+ const tool = getTool();
1714
+ if (tool && "clickBehavior" in tool && ((_a = tool.clickBehavior) == null ? void 0 : _a.enabled)) {
1715
+ onClickDetected(pos, tool);
1716
+ }
1717
+ }
1718
+ setStartPos(null);
1719
+ setHasMoved(false);
1720
+ },
1721
+ hasMoved: getHasMoved,
1722
+ reset: () => {
1723
+ setStartPos(null);
1724
+ setHasMoved(false);
1725
+ }
1726
+ };
1727
+ }
1604
1728
  const freeTextHandlerFactory = {
1605
1729
  annotationType: PdfAnnotationSubtype.FREETEXT,
1606
1730
  create(context) {
@@ -2453,6 +2577,135 @@ const circleHandlerFactory = {
2453
2577
  };
2454
2578
  }
2455
2579
  };
2580
+ const textMarkupSelectionHandler = {
2581
+ toolId: "__textMarkup__",
2582
+ handle(context, selections, getText) {
2583
+ const tool = context.getTool();
2584
+ if (!tool) return;
2585
+ for (const selection of selections) {
2586
+ const id = uuidV4();
2587
+ getText().then((text) => {
2588
+ var _a;
2589
+ context.createAnnotation(selection.pageIndex, {
2590
+ ...tool.defaults,
2591
+ rect: selection.rect,
2592
+ segmentRects: selection.segmentRects,
2593
+ pageIndex: selection.pageIndex,
2594
+ created: /* @__PURE__ */ new Date(),
2595
+ id,
2596
+ ...text != null && { custom: { text } }
2597
+ });
2598
+ if ((_a = tool.behavior) == null ? void 0 : _a.selectAfterCreate) {
2599
+ context.selectAnnotation(selection.pageIndex, id);
2600
+ }
2601
+ });
2602
+ }
2603
+ }
2604
+ };
2605
+ function computeCaretRect(lastSegRect) {
2606
+ const lineHeight = lastSegRect.size.height;
2607
+ const height = lineHeight / 2;
2608
+ const width = height;
2609
+ const lineEndX = lastSegRect.origin.x + lastSegRect.size.width;
2610
+ return {
2611
+ origin: {
2612
+ x: lineEndX - width / 2,
2613
+ y: lastSegRect.origin.y + lineHeight / 2
2614
+ },
2615
+ size: { width, height }
2616
+ };
2617
+ }
2618
+ const insertTextSelectionHandler = {
2619
+ toolId: "insertText",
2620
+ handle(context, selections, getText) {
2621
+ const tool = context.getTool();
2622
+ if (!tool) return;
2623
+ const getDefaults = () => ({
2624
+ strokeColor: tool.defaults.strokeColor ?? "#E44234",
2625
+ opacity: tool.defaults.opacity ?? 1,
2626
+ flags: tool.defaults.flags ?? ["print"]
2627
+ });
2628
+ for (const selection of selections) {
2629
+ const lastSegRect = selection.segmentRects[selection.segmentRects.length - 1];
2630
+ if (!lastSegRect) continue;
2631
+ const caretRect = computeCaretRect(lastSegRect);
2632
+ const caretId = uuidV4();
2633
+ const defaults = getDefaults();
2634
+ getText().then((text) => {
2635
+ var _a;
2636
+ context.createAnnotation(selection.pageIndex, {
2637
+ type: PdfAnnotationSubtype.CARET,
2638
+ id: caretId,
2639
+ pageIndex: selection.pageIndex,
2640
+ rect: caretRect,
2641
+ strokeColor: defaults.strokeColor,
2642
+ opacity: defaults.opacity,
2643
+ intent: "Insert",
2644
+ rectangleDifferences: { left: 0.5, top: 0.5, right: 0.5, bottom: 0.5 },
2645
+ created: /* @__PURE__ */ new Date(),
2646
+ flags: defaults.flags,
2647
+ ...text != null && { custom: { text } }
2648
+ });
2649
+ if ((_a = tool.behavior) == null ? void 0 : _a.selectAfterCreate) {
2650
+ context.selectAnnotation(selection.pageIndex, caretId);
2651
+ }
2652
+ });
2653
+ }
2654
+ }
2655
+ };
2656
+ const replaceTextSelectionHandler = {
2657
+ toolId: "replaceText",
2658
+ handle(context, selections, getText) {
2659
+ const tool = context.getTool();
2660
+ if (!tool) return;
2661
+ const getDefaults = () => ({
2662
+ strokeColor: tool.defaults.strokeColor ?? "#E44234",
2663
+ opacity: tool.defaults.opacity ?? 1,
2664
+ flags: tool.defaults.flags ?? ["print"]
2665
+ });
2666
+ for (const selection of selections) {
2667
+ const lastSegRect = selection.segmentRects[selection.segmentRects.length - 1];
2668
+ if (!lastSegRect) continue;
2669
+ const caretRect = computeCaretRect(lastSegRect);
2670
+ const caretId = uuidV4();
2671
+ const strikeoutId = uuidV4();
2672
+ const defaults = getDefaults();
2673
+ getText().then((text) => {
2674
+ var _a;
2675
+ context.createAnnotation(selection.pageIndex, {
2676
+ type: PdfAnnotationSubtype.CARET,
2677
+ id: caretId,
2678
+ pageIndex: selection.pageIndex,
2679
+ rect: caretRect,
2680
+ strokeColor: defaults.strokeColor,
2681
+ opacity: defaults.opacity,
2682
+ intent: "Replace",
2683
+ rectangleDifferences: { left: 0.5, top: 0.5, right: 0.5, bottom: 0.5 },
2684
+ created: /* @__PURE__ */ new Date(),
2685
+ flags: defaults.flags
2686
+ });
2687
+ context.createAnnotation(selection.pageIndex, {
2688
+ type: PdfAnnotationSubtype.STRIKEOUT,
2689
+ id: strikeoutId,
2690
+ pageIndex: selection.pageIndex,
2691
+ rect: selection.rect,
2692
+ segmentRects: selection.segmentRects,
2693
+ strokeColor: defaults.strokeColor,
2694
+ opacity: defaults.opacity,
2695
+ intent: "StrikeOutTextEdit",
2696
+ inReplyToId: caretId,
2697
+ replyType: PdfAnnotationReplyType.Group,
2698
+ created: /* @__PURE__ */ new Date(),
2699
+ flags: defaults.flags,
2700
+ ...text != null && { custom: { text } }
2701
+ });
2702
+ if ((_a = tool.behavior) == null ? void 0 : _a.selectAfterCreate) {
2703
+ context.selectAnnotation(selection.pageIndex, caretId);
2704
+ }
2705
+ });
2706
+ }
2707
+ }
2708
+ };
2456
2709
  const patchInk = (original, ctx) => {
2457
2710
  switch (ctx.type) {
2458
2711
  case "vertex-edit":
@@ -2881,6 +3134,7 @@ const _AnnotationPlugin = class _AnnotationPlugin extends BasePlugin {
2881
3134
  this.importQueue = /* @__PURE__ */ new Map();
2882
3135
  this.commitInProgress = /* @__PURE__ */ new Map();
2883
3136
  this.handlerFactories = /* @__PURE__ */ new Map();
3137
+ this.selectionHandlerFactories = /* @__PURE__ */ new Map();
2884
3138
  this.activeTool$ = createBehaviorEmitter();
2885
3139
  this.events$ = createBehaviorEmitter();
2886
3140
  this.toolsChange$ = createBehaviorEmitter();
@@ -2897,6 +3151,7 @@ const _AnnotationPlugin = class _AnnotationPlugin extends BasePlugin {
2897
3151
  this.history = ((_b = registry.getPlugin("history")) == null ? void 0 : _b.provides()) ?? null;
2898
3152
  this.interactionManager = ((_c = registry.getPlugin("interaction-manager")) == null ? void 0 : _c.provides()) ?? null;
2899
3153
  this.registerHandlerFactories();
3154
+ this.registerSelectionHandlerFactories();
2900
3155
  this.registerBuiltInPatches();
2901
3156
  }
2902
3157
  // ─────────────────────────────────────────────────────────
@@ -2922,7 +3177,7 @@ const _AnnotationPlugin = class _AnnotationPlugin extends BasePlugin {
2922
3177
  for (const tool of this.state.tools) {
2923
3178
  if (tool.interaction.textSelection) {
2924
3179
  this.selection.enableForMode(tool.interaction.mode ?? tool.id, {
2925
- showSelectionRects: false,
3180
+ showSelectionRects: tool.interaction.showSelectionRects ?? false,
2926
3181
  enableSelection: true,
2927
3182
  enableMarquee: false
2928
3183
  });
@@ -2951,6 +3206,11 @@ const _AnnotationPlugin = class _AnnotationPlugin extends BasePlugin {
2951
3206
  this.handlerFactories.set(PdfAnnotationSubtype.LINE, lineHandlerFactory);
2952
3207
  this.handlerFactories.set(PdfAnnotationSubtype.INK, inkHandlerFactory);
2953
3208
  this.handlerFactories.set(PdfAnnotationSubtype.FREETEXT, freeTextHandlerFactory);
3209
+ this.handlerFactories.set(PdfAnnotationSubtype.TEXT, textHandlerFactory);
3210
+ }
3211
+ registerSelectionHandlerFactories() {
3212
+ this.selectionHandlerFactories.set("insertText", insertTextSelectionHandler);
3213
+ this.selectionHandlerFactories.set("replaceText", replaceTextSelectionHandler);
2954
3214
  }
2955
3215
  registerBuiltInPatches() {
2956
3216
  this.patchRegistry.register(PdfAnnotationSubtype.INK, patchInk);
@@ -2984,7 +3244,7 @@ const _AnnotationPlugin = class _AnnotationPlugin extends BasePlugin {
2984
3244
  if (modeId !== "pointerMode") return;
2985
3245
  const docState = this.state.documents[documentId];
2986
3246
  if (!docState) return;
2987
- const pageAnnotations = (docState.pages[pageIndex] ?? []).map((uid) => docState.byUid[uid]).filter((ta) => ta !== void 0).filter((ta) => !isLink(ta));
3247
+ const pageAnnotations = (docState.pages[pageIndex] ?? []).map((uid) => docState.byUid[uid]).filter((ta) => ta !== void 0).filter((ta) => isSidebarAnnotation(ta));
2988
3248
  const selectedIds = pageAnnotations.filter((ta) => rectsIntersect(rect, ta.object.rect)).map((ta) => ta.object.id);
2989
3249
  if (selectedIds.length > 0) {
2990
3250
  const expandedIds = /* @__PURE__ */ new Set();
@@ -3002,44 +3262,32 @@ const _AnnotationPlugin = class _AnnotationPlugin extends BasePlugin {
3002
3262
  }
3003
3263
  });
3004
3264
  (_c = this.selection) == null ? void 0 : _c.onEndSelection(({ documentId }) => {
3005
- var _a2, _b2, _c2;
3006
- if (!this.checkPermission(documentId, PdfPermissionFlag.ModifyAnnotations)) {
3007
- return;
3008
- }
3265
+ var _a2, _b2, _c2, _d;
3266
+ if (!this.checkPermission(documentId, PdfPermissionFlag.ModifyAnnotations)) return;
3009
3267
  const activeTool = this.getActiveTool(documentId);
3010
3268
  if (!activeTool || !activeTool.interaction.textSelection) return;
3011
3269
  const formattedSelection = (_a2 = this.selection) == null ? void 0 : _a2.getFormattedSelection();
3012
3270
  const selectionText = (_b2 = this.selection) == null ? void 0 : _b2.getSelectedText();
3013
3271
  if (!formattedSelection || !selectionText) return;
3014
- for (const selection of formattedSelection) {
3015
- selectionText.wait((text) => {
3016
- var _a3, _b3;
3017
- const annotationId = uuidV4();
3018
- this.createAnnotation(
3019
- selection.pageIndex,
3020
- {
3021
- ...activeTool.defaults,
3022
- rect: selection.rect,
3023
- segmentRects: selection.segmentRects,
3024
- pageIndex: selection.pageIndex,
3025
- created: /* @__PURE__ */ new Date(),
3026
- id: annotationId,
3027
- custom: {
3028
- text: text.join("\n")
3029
- }
3030
- },
3031
- void 0,
3032
- documentId
3033
- );
3034
- if ((_a3 = activeTool.behavior) == null ? void 0 : _a3.deactivateToolAfterCreate) {
3035
- this.setActiveTool(null, documentId);
3036
- }
3037
- if ((_b3 = activeTool.behavior) == null ? void 0 : _b3.selectAfterCreate) {
3038
- this.selectAnnotation(selection.pageIndex, annotationId, documentId);
3039
- }
3040
- }, ignore);
3272
+ const getText = () => new Promise((resolve) => {
3273
+ selectionText.wait(
3274
+ (text) => resolve(text.join("\n")),
3275
+ () => resolve(void 0)
3276
+ );
3277
+ });
3278
+ const context = {
3279
+ toolId: activeTool.id,
3280
+ documentId,
3281
+ getTool: () => this.getActiveTool(documentId) ?? null,
3282
+ createAnnotation: (pageIndex, annotation) => this.createAnnotation(pageIndex, annotation, void 0, documentId),
3283
+ selectAnnotation: (pageIndex, id) => this.selectAnnotation(pageIndex, id, documentId)
3284
+ };
3285
+ const handler = this.selectionHandlerFactories.get(activeTool.id) ?? textMarkupSelectionHandler;
3286
+ handler.handle(context, formattedSelection, getText);
3287
+ if ((_c2 = activeTool.behavior) == null ? void 0 : _c2.deactivateToolAfterCreate) {
3288
+ this.setActiveTool(null, documentId);
3041
3289
  }
3042
- (_c2 = this.selection) == null ? void 0 : _c2.clear();
3290
+ (_d = this.selection) == null ? void 0 : _d.clear();
3043
3291
  });
3044
3292
  }
3045
3293
  registerInteractionForTool(tool) {
@@ -4867,6 +5115,8 @@ const isPolylineTool = createToolPredicate("polyline");
4867
5115
  const isPolygonTool = createToolPredicate("polygon");
4868
5116
  const isFreeTextTool = createToolPredicate("freeText");
4869
5117
  const isStampTool = createToolPredicate("stamp");
5118
+ const isInsertTextTool = createToolPredicate("insertText");
5119
+ const isReplaceTextTool = createToolPredicate("replaceText");
4870
5120
  const AnnotationPluginPackage = {
4871
5121
  manifest,
4872
5122
  create: (registry, config) => new AnnotationPlugin(ANNOTATION_PLUGIN_ID, registry, config),
@@ -4906,6 +5156,7 @@ export {
4906
5156
  initialDocumentState,
4907
5157
  initialState,
4908
5158
  isAnnotationSelected,
5159
+ isCaret,
4909
5160
  isCircle,
4910
5161
  isCircleTool,
4911
5162
  isFreeText,
@@ -4916,6 +5167,7 @@ export {
4916
5167
  isInk,
4917
5168
  isInkHighlighterTool,
4918
5169
  isInkTool,
5170
+ isInsertTextTool,
4919
5171
  isLine,
4920
5172
  isLineTool,
4921
5173
  isLink,
@@ -4924,6 +5176,7 @@ export {
4924
5176
  isPolyline,
4925
5177
  isPolylineTool,
4926
5178
  isRedact,
5179
+ isReplaceTextTool,
4927
5180
  isSidebarAnnotation,
4928
5181
  isSquare,
4929
5182
  isSquareTool,