@embedpdf/plugin-annotation 2.2.0 → 2.3.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 (97) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.js +1196 -92
  4. package/dist/index.js.map +1 -1
  5. package/dist/lib/actions.d.ts +31 -2
  6. package/dist/lib/annotation-plugin.d.ts +177 -2
  7. package/dist/lib/handlers/types.d.ts +1 -1
  8. package/dist/lib/helpers.d.ts +6 -1
  9. package/dist/lib/selectors.d.ts +97 -8
  10. package/dist/lib/tools/default-tools.d.ts +39 -11
  11. package/dist/lib/tools/types.d.ts +7 -1
  12. package/dist/lib/types.d.ts +265 -1
  13. package/dist/preact/adapter.d.ts +3 -3
  14. package/dist/preact/index.cjs +1 -1
  15. package/dist/preact/index.cjs.map +1 -1
  16. package/dist/preact/index.js +857 -403
  17. package/dist/preact/index.js.map +1 -1
  18. package/dist/react/adapter.d.ts +1 -1
  19. package/dist/react/index.cjs +1 -1
  20. package/dist/react/index.cjs.map +1 -1
  21. package/dist/react/index.js +857 -403
  22. package/dist/react/index.js.map +1 -1
  23. package/dist/shared/components/annotation-container.d.ts +8 -1
  24. package/dist/shared/components/annotation-layer.d.ts +4 -2
  25. package/dist/shared/components/annotations/ink.d.ts +3 -3
  26. package/dist/shared/components/annotations/link.d.ts +28 -0
  27. package/dist/shared/components/annotations.d.ts +2 -1
  28. package/dist/shared/components/group-selection-box.d.ts +32 -0
  29. package/dist/shared/components/index.d.ts +1 -0
  30. package/dist/shared/components/text-markup/highlight.d.ts +3 -2
  31. package/dist/shared/components/text-markup/squiggly.d.ts +3 -2
  32. package/dist/shared/components/text-markup/strikeout.d.ts +3 -2
  33. package/dist/shared/components/text-markup/underline.d.ts +3 -2
  34. package/dist/shared/components/types.d.ts +7 -0
  35. package/dist/shared-preact/components/annotation-container.d.ts +8 -1
  36. package/dist/shared-preact/components/annotation-layer.d.ts +4 -2
  37. package/dist/shared-preact/components/annotations/ink.d.ts +3 -3
  38. package/dist/shared-preact/components/annotations/link.d.ts +28 -0
  39. package/dist/shared-preact/components/annotations.d.ts +2 -1
  40. package/dist/shared-preact/components/group-selection-box.d.ts +32 -0
  41. package/dist/shared-preact/components/index.d.ts +1 -0
  42. package/dist/shared-preact/components/text-markup/highlight.d.ts +3 -2
  43. package/dist/shared-preact/components/text-markup/squiggly.d.ts +3 -2
  44. package/dist/shared-preact/components/text-markup/strikeout.d.ts +3 -2
  45. package/dist/shared-preact/components/text-markup/underline.d.ts +3 -2
  46. package/dist/shared-preact/components/types.d.ts +7 -0
  47. package/dist/shared-react/components/annotation-container.d.ts +8 -1
  48. package/dist/shared-react/components/annotation-layer.d.ts +4 -2
  49. package/dist/shared-react/components/annotations/ink.d.ts +3 -3
  50. package/dist/shared-react/components/annotations/link.d.ts +28 -0
  51. package/dist/shared-react/components/annotations.d.ts +2 -1
  52. package/dist/shared-react/components/group-selection-box.d.ts +32 -0
  53. package/dist/shared-react/components/index.d.ts +1 -0
  54. package/dist/shared-react/components/text-markup/highlight.d.ts +3 -2
  55. package/dist/shared-react/components/text-markup/squiggly.d.ts +3 -2
  56. package/dist/shared-react/components/text-markup/strikeout.d.ts +3 -2
  57. package/dist/shared-react/components/text-markup/underline.d.ts +3 -2
  58. package/dist/shared-react/components/types.d.ts +7 -0
  59. package/dist/svelte/components/AnnotationLayer.svelte.d.ts +5 -1
  60. package/dist/svelte/components/Annotations.svelte.d.ts +5 -1
  61. package/dist/svelte/components/GroupSelectionBox.svelte.d.ts +32 -0
  62. package/dist/svelte/components/annotations/Ink.svelte.d.ts +2 -1
  63. package/dist/svelte/components/annotations/Link.svelte.d.ts +24 -0
  64. package/dist/svelte/components/annotations/index.d.ts +1 -0
  65. package/dist/svelte/components/index.d.ts +1 -0
  66. package/dist/svelte/components/text-markup/Highlight.svelte.d.ts +2 -1
  67. package/dist/svelte/components/text-markup/Squiggly.svelte.d.ts +2 -1
  68. package/dist/svelte/components/text-markup/Strikeout.svelte.d.ts +2 -1
  69. package/dist/svelte/components/text-markup/Underline.svelte.d.ts +2 -1
  70. package/dist/svelte/components/types.d.ts +2 -0
  71. package/dist/svelte/index.cjs +1 -1
  72. package/dist/svelte/index.cjs.map +1 -1
  73. package/dist/svelte/index.js +905 -255
  74. package/dist/svelte/index.js.map +1 -1
  75. package/dist/svelte/types.d.ts +7 -0
  76. package/dist/vue/components/annotation-container.vue.d.ts +2 -0
  77. package/dist/vue/components/annotation-layer.vue.d.ts +28 -5
  78. package/dist/vue/components/annotations/index.d.ts +1 -0
  79. package/dist/vue/components/annotations/ink.vue.d.ts +2 -2
  80. package/dist/vue/components/annotations/line.vue.d.ts +1 -1
  81. package/dist/vue/components/annotations/link.vue.d.ts +29 -0
  82. package/dist/vue/components/annotations/polygon.vue.d.ts +1 -1
  83. package/dist/vue/components/annotations/polyline.vue.d.ts +1 -1
  84. package/dist/vue/components/annotations.vue.d.ts +65 -1
  85. package/dist/vue/components/group-selection-box.vue.d.ts +73 -0
  86. package/dist/vue/components/index.d.ts +1 -0
  87. package/dist/vue/components/text-markup/highlight.vue.d.ts +2 -2
  88. package/dist/vue/components/text-markup/squiggly.vue.d.ts +2 -2
  89. package/dist/vue/components/text-markup/strikeout.vue.d.ts +2 -2
  90. package/dist/vue/components/text-markup/underline.vue.d.ts +2 -2
  91. package/dist/vue/hooks/use-annotation.d.ts +2 -0
  92. package/dist/vue/index.cjs +1 -1
  93. package/dist/vue/index.cjs.map +1 -1
  94. package/dist/vue/index.js +946 -406
  95. package/dist/vue/index.js.map +1 -1
  96. package/dist/vue/types.d.ts +7 -0
  97. package/package.json +10 -10
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("@embedpdf/core"),e=require("@embedpdf/models"),n="annotation",o={id:n,name:"Annotation Plugin",version:"1.0.0",provides:["annotation"],requires:["interaction-manager","selection"],optional:["history"],defaultConfig:{autoCommit:!0,annotationAuthor:"Guest",deactivateToolAfterCreate:!1,selectAfterCreate:!0}},i="ANNOTATION/INIT_STATE",s="ANNOTATION/CLEANUP_STATE",a="ANNOTATION/SET_ANNOTATIONS",r="ANNOTATION/SELECT_ANNOTATION",c="ANNOTATION/DESELECT_ANNOTATION",d="ANNOTATION/SET_ACTIVE_TOOL_ID",l="ANNOTATION/CREATE_ANNOTATION",u="ANNOTATION/PATCH_ANNOTATION",h="ANNOTATION/DELETE_ANNOTATION",g="ANNOTATION/COMMIT",p="ANNOTATION/PURGE_ANNOTATION",f="ANNOTATION/ADD_COLOR_PRESET",y="ANNOTATION/SET_TOOL_DEFAULTS",m="ANNOTATION/ADD_TOOL";const A=t=>({type:c,payload:{documentId:t}}),P=(t,e)=>({type:d,payload:{documentId:t,toolId:e}}),x=(t,e,n)=>({type:l,payload:{documentId:t,pageIndex:e,annotation:n}}),v=(t,e,n,o)=>({type:u,payload:{documentId:t,pageIndex:e,id:n,patch:o}}),I=(t,e,n)=>({type:h,payload:{documentId:t,pageIndex:e,id:n}}),S=(t,e)=>({type:p,payload:{documentId:t,uid:e}});function b(t){return t.object.type===e.PdfAnnotationSubtype.INK}function T(t){return t.object.type===e.PdfAnnotationSubtype.CIRCLE}function C(t){return t.object.type===e.PdfAnnotationSubtype.POLYGON}function E(t){return t.object.type===e.PdfAnnotationSubtype.SQUARE}function k(t){return t.object.type===e.PdfAnnotationSubtype.LINE}function N(t){return t.object.type===e.PdfAnnotationSubtype.POLYLINE}function w(t){return t.object.type===e.PdfAnnotationSubtype.HIGHLIGHT}function D(t){return t.object.type===e.PdfAnnotationSubtype.UNDERLINE}function M(t){return t.object.type===e.PdfAnnotationSubtype.STRIKEOUT}function L(t){return t.object.type===e.PdfAnnotationSubtype.SQUIGGLY}function O(t){return w(t)||D(t)||M(t)||L(t)}function z(t){return t.object.type===e.PdfAnnotationSubtype.FREETEXT}function R(t){return t.object.type===e.PdfAnnotationSubtype.STAMP}function F(t){return t.object.type===e.PdfAnnotationSubtype.TEXT}function U(t){return O(t)||b(t)||E(t)||T(t)||C(t)||k(t)||N(t)||z(t)||R(t)}const $=(t,e)=>(t.pages[e]??[]).map(e=>t.byUid[e]),j=t=>t.selectedUid?t.byUid[t.selectedUid]:null,W=(t,e)=>t.byUid[e]??null;const B=t=>{const e={};for(const o of Object.values(t.pages))for(const n of o){const o=t.byUid[n];if(o&&F(o)){const t=o.object.inReplyToId;t&&(e[t]||(e[t]=[])).push(o)}}const n={};for(const[o,i]of Object.entries(t.pages)){const s=Number(o),a=[];for(const n of i){const o=t.byUid[n];o&&U(o)&&a.push({page:s,annotation:o,replies:e[o.object.id]??[]})}a.length>0&&(n[s]=a)}return n},H=[{id:"highlight",name:"Highlight",matchScore:t=>t.type===e.PdfAnnotationSubtype.HIGHLIGHT?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1},defaults:{type:e.PdfAnnotationSubtype.HIGHLIGHT,color:"#FFCD45",opacity:1,blendMode:e.PdfBlendMode.Multiply}},{id:"underline",name:"Underline",matchScore:t=>t.type===e.PdfAnnotationSubtype.UNDERLINE?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1},defaults:{type:e.PdfAnnotationSubtype.UNDERLINE,color:"#E44234",opacity:1}},{id:"strikeout",name:"Strikeout",matchScore:t=>t.type===e.PdfAnnotationSubtype.STRIKEOUT?1:0,interaction:{exclusive:!1,textSelection:!0},defaults:{type:e.PdfAnnotationSubtype.STRIKEOUT,color:"#E44234",opacity:1}},{id:"squiggly",name:"Squiggly",matchScore:t=>t.type===e.PdfAnnotationSubtype.SQUIGGLY?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1},defaults:{type:e.PdfAnnotationSubtype.SQUIGGLY,color:"#E44234",opacity:1}},{id:"ink",name:"Pen",matchScore:t=>t.type===e.PdfAnnotationSubtype.INK&&"InkHighlight"!==t.intent?5:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.INK,color:"#E44234",opacity:1,strokeWidth:6}},{id:"inkHighlighter",name:"Ink Highlighter",matchScore:t=>t.type===e.PdfAnnotationSubtype.INK&&"InkHighlight"===t.intent?10:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.INK,intent:"InkHighlight",color:"#FFCD45",opacity:1,strokeWidth:14,blendMode:e.PdfBlendMode.Multiply}},{id:"circle",name:"Circle",matchScore:t=>t.type===e.PdfAnnotationSubtype.CIRCLE?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.CIRCLE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234",strokeStyle:e.PdfAnnotationBorderStyle.SOLID},clickBehavior:{enabled:!0,defaultSize:{width:100,height:100}}},{id:"square",name:"Square",matchScore:t=>t.type===e.PdfAnnotationSubtype.SQUARE?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.SQUARE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234",strokeStyle:e.PdfAnnotationBorderStyle.SOLID},clickBehavior:{enabled:!0,defaultSize:{width:100,height:100}}},{id:"line",name:"Line",matchScore:t=>t.type===e.PdfAnnotationSubtype.LINE&&"LineArrow"!==t.intent?5:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.LINE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234"},clickBehavior:{enabled:!0,defaultLength:100,defaultAngle:0}},{id:"lineArrow",name:"Arrow",matchScore:t=>t.type===e.PdfAnnotationSubtype.LINE&&"LineArrow"===t.intent?10:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.LINE,intent:"LineArrow",color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234",lineEndings:{start:e.PdfAnnotationLineEnding.None,end:e.PdfAnnotationLineEnding.OpenArrow}},clickBehavior:{enabled:!0,defaultLength:100,defaultAngle:0}},{id:"polyline",name:"Polyline",matchScore:t=>t.type===e.PdfAnnotationSubtype.POLYLINE?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.POLYLINE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234"}},{id:"polygon",name:"Polygon",matchScore:t=>t.type===e.PdfAnnotationSubtype.POLYGON?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.POLYGON,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234"}},{id:"freeText",name:"Free Text",matchScore:t=>t.type===e.PdfAnnotationSubtype.FREETEXT?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.FREETEXT,contents:"Insert text",fontSize:14,fontColor:"#E44234",fontFamily:e.PdfStandardFont.Helvetica,textAlign:e.PdfTextAlignment.Left,verticalAlign:e.PdfVerticalAlignment.Top,backgroundColor:"transparent",opacity:1},clickBehavior:{enabled:!0,defaultSize:{width:100,height:20},defaultContent:"Insert text"}},{id:"stamp",name:"Image",matchScore:t=>t.type===e.PdfAnnotationSubtype.STAMP?1:0,interaction:{exclusive:!1,cursor:"copy",isDraggable:!0,isResizable:!0,lockAspectRatio:!0},defaults:{type:e.PdfAnnotationSubtype.STAMP}}],_=["#E44234","#FF8D00","#FFCD45","#5CC96E","#25D2D1","#597CE2","#C544CE","#7D2E25","#000000","#FFFFFF"],G=()=>({pages:{},byUid:{},selectedUid:null,activeToolId:null,hasPendingChanges:!1}),Y=(t,e,n)=>{const o=t.byUid[e];return o?{...t,byUid:{...t.byUid,[e]:{...o,commitState:"synced"===o.commitState?"dirty":o.commitState,object:{...o.object,...n}}},hasPendingChanges:!0}:t},Q=t=>{const e=new Map;return H.forEach(t=>e.set(t.id,t)),(t.tools||[]).forEach(t=>e.set(t.id,t)),{documents:{},activeDocumentId:null,tools:Array.from(e.values()),colorPresets:t.colorPresets??_}};function V(t){let e=t;return[()=>e,t=>{e=t}]}const q={annotationType:e.PdfAnnotationSubtype.INK,create(n){const{onCommit:o,onPreview:i,getTool:s,pageSize:a}=n,[r,c]=V([]),[d,l]=V(!1),u={current:null},h=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),g=()=>{const t=s();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??1,color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,flags:t.defaults.flags??["print"]}:null},p=()=>{const t=r();if(0===t.length||0===t[0].points.length)return null;const n=g();if(!n)return null;const o=t.flatMap(t=>t.points),i=e.expandRect(e.rectFromPoints(o),n.strokeWidth/2);return{type:e.PdfAnnotationSubtype.INK,bounds:i,data:{...n,rect:i,inkList:t}}};return{onPointerDown:(t,e)=>{var n;const o=h(t);l(!0),u.current&&clearTimeout(u.current);const s=[...r(),{points:[o]}];c(s),i(p()),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{if(!d())return;const e=r();if(0===e.length)return;const n=h(t);e[e.length-1].points.push(n),c(e),i(p())},onPointerUp:(t,s)=>{var a;l(!1),null==(a=s.releasePointerCapture)||a.call(s),u.current&&clearTimeout(u.current),u.current=setTimeout(()=>{const t=r();if(t.length>0&&t[0].points.length>1){const i=g();if(!i)return;const s=t.flatMap(t=>t.points),a=e.expandRect(e.rectFromPoints(s),i.strokeWidth/2);o({...i,inkList:t,rect:a,type:e.PdfAnnotationSubtype.INK,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date})}c([]),i(null)},800)},onPointerCancel:(t,e)=>{var n;c([]),l(!1),i(null),u.current&&clearTimeout(u.current),null==(n=e.releasePointerCapture)||n.call(e)}}}};function K({threshold:t=5,getTool:e,onClickDetected:n}){const[o,i]=V(null),[s,a]=V(!1);return{onStart:t=>{i(t),a(!1)},onMove:e=>{const n=o();if(!n||s())return;Math.sqrt(Math.pow(e.x-n.x,2)+Math.pow(e.y-n.y,2))>t&&a(!0)},onEnd:t=>{var r;if(o()&&!s()){const o=e();o&&"clickBehavior"in o&&(null==(r=o.clickBehavior)?void 0:r.enabled)&&n(t,o)}i(null),a(!1)},hasMoved:s,reset:()=>{i(null),a(!1)}}}const X={annotationType:e.PdfAnnotationSubtype.FREETEXT,create(n){const{onCommit:o,onPreview:i,getTool:s,pageSize:a,pageIndex:r}=n,[c,d]=V(null),l=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),u=()=>{const t=s();return t?{...t.defaults,fontColor:t.defaults.fontColor??"#000000",opacity:t.defaults.opacity??1,fontSize:t.defaults.fontSize??12,fontFamily:t.defaults.fontFamily??e.PdfStandardFont.Helvetica,backgroundColor:t.defaults.backgroundColor??"transparent",textAlign:t.defaults.textAlign??e.PdfTextAlignment.Left,verticalAlign:t.defaults.verticalAlign??e.PdfVerticalAlignment.Top,contents:t.defaults.contents??"Insert text here",flags:t.defaults.flags??["print"]}:null},h=K({threshold:5,getTool:s,onClickDetected:(n,i)=>{const s=u();if(!s)return;const c=i.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,h=d/2,g=l/2,p={origin:{x:t.clamp(n.x-h,0,a.width-d),y:t.clamp(n.y-g,0,a.height-l)},size:{width:d,height:l}},f=c.defaultContent??s.contents,y={...s,contents:f,type:e.PdfAnnotationSubtype.FREETEXT,rect:p,pageIndex:r,id:e.uuidV4(),created:new Date};o(y)}}),g=t=>{const n=c();if(!n)return null;const o=u();if(!o)return null;const i={origin:{x:Math.min(n.x,t.x),y:Math.min(n.y,t.y)},size:{width:Math.abs(n.x-t.x),height:Math.abs(n.y-t.y)}};return{type:e.PdfAnnotationSubtype.FREETEXT,bounds:i,data:{...o,rect:i}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),h.onStart(o),i(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&i(g(e))},onPointerUp:(t,s)=>{var a;const r=c();if(!r)return;const g=u();if(!g)return;const p=l(t);if(h.hasMoved()){const t={origin:{x:Math.min(r.x,p.x),y:Math.min(r.y,p.y)},size:{width:Math.abs(r.x-p.x),height:Math.abs(r.y-p.y)}},i={...g,type:e.PdfAnnotationSubtype.FREETEXT,rect:t,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};o(i)}else h.onEnd(p);d(null),i(null),h.reset(),null==(a=s.releasePointerCapture)||a.call(s)},onPointerLeave:(t,e)=>{var n;d(null),i(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),i(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}};function Z(t){const e=t=>{const e=9*t,n=Math.PI/6;return{x:-e*Math.cos(n),y:e*Math.sin(n)}};return{getSvgPath:n=>{const{x:o,y:i}=e(n);return t?`M 0 0 L ${o} ${i} L ${o} ${-i} Z`:`M ${o} ${i} L 0 0 L ${o} ${-i}`},getLocalPoints:t=>{const{x:n,y:o}=e(t);return[{x:0,y:0},{x:n,y:o},{x:n,y:-o}]},getRotation:t=>t,filled:t}}function J(t,e){const n=e=>e*t/2;return{getSvgPath:t=>{const e=n(t);return`M ${-e} 0 L ${e} 0`},getLocalPoints:t=>{const e=n(t);return[{x:-e,y:0},{x:e,y:0}]},getRotation:e,filled:!1}}const tt=Z(!1),et=Z(!0),nt={[e.PdfAnnotationLineEnding.OpenArrow]:tt,[e.PdfAnnotationLineEnding.ClosedArrow]:et,[e.PdfAnnotationLineEnding.ROpenArrow]:{...tt,getRotation:t=>t+Math.PI},[e.PdfAnnotationLineEnding.RClosedArrow]:{...et,getRotation:t=>t+Math.PI},[e.PdfAnnotationLineEnding.Circle]:{getSvgPath:t=>{const e=5*t/2;return`M ${e} 0 A ${e} ${e} 0 1 1 ${-e} 0 A ${e} ${e} 0 1 1 ${e} 0`},getLocalPoints:t=>{const e=5*t/2;return[{x:-e,y:-e},{x:e,y:e}]},getRotation:()=>0,filled:!0},[e.PdfAnnotationLineEnding.Square]:{getSvgPath:t=>{const e=6*t/2;return`M ${-e} ${-e} L ${e} ${-e} L ${e} ${e} L ${-e} ${e} Z`},getLocalPoints:t=>{const e=6*t/2;return[{x:-e,y:-e},{x:e,y:-e},{x:e,y:e},{x:-e,y:e}]},getRotation:t=>t,filled:!0},[e.PdfAnnotationLineEnding.Diamond]:{getSvgPath:t=>{const e=6*t/2;return`M 0 ${-e} L ${e} 0 L 0 ${e} L ${-e} 0 Z`},getLocalPoints:t=>{const e=6*t/2;return[{x:0,y:-e},{x:e,y:0},{x:0,y:e},{x:-e,y:0}]},getRotation:t=>t,filled:!0},[e.PdfAnnotationLineEnding.Butt]:J(6,t=>t+Math.PI/2),[e.PdfAnnotationLineEnding.Slash]:J(18,t=>t+Math.PI/1.5)};function ot(t,n,o){if(!t||0===t.length)return{origin:{x:0,y:0},size:{width:0,height:0}};const i=[...t],s=(t,e)=>Math.atan2(e.y-t.y,e.x-t.x),a=(t,o,s)=>{if(!t)return;const a=nt[t];if(!a)return;const r=a.getLocalPoints(n),c=a.getRotation(s),d=r.map(t=>e.rotateAndTranslatePoint(t,c,o));i.push(...d)};if(t.length>=2){const e=s(t[1],t[0]);a(null==o?void 0:o.start,t[0],e);const n=t.length-1,i=s(t[n-1],t[n]);a(null==o?void 0:o.end,t[n],i)}if(i.length<=1){const e=t[0]||{x:0,y:0},o=n;return{origin:{x:e.x-o,y:e.y-o},size:{width:2*o,height:2*o}}}const r=e.rectFromPoints(i),c=n/2+1.2*n;return e.expandRect(r,c)}class it{constructor(){this.patches=new Map}register(t,e){this.patches.set(t,e)}transform(t,e){const n=this.patches.get(t.type);return n?n(t,e):e.changes}}const st=new it,at=Object.freeze(Object.defineProperty({__proto__:null,LINE_ENDING_HANDLERS:nt,PatchRegistry:it,createEnding:function(t,e,n,o,i){if(!t)return null;const s=nt[t];if(!s)return null;const a=s.getRotation(n);return{d:s.getSvgPath(e),transform:`translate(${o} ${i}) rotate(${r=a,180*r/Math.PI})`,filled:s.filled};var r},lineRectWithEndings:ot,patchRegistry:st},Symbol.toStringTag,{value:"Module"})),rt={annotationType:e.PdfAnnotationSubtype.LINE,create(n){const{pageIndex:o,onCommit:i,onPreview:s,getTool:a,pageSize:r}=n,[c,d]=V(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=a();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??1,lineEndings:t.defaults.lineEndings??{start:e.PdfAnnotationLineEnding.None,end:e.PdfAnnotationLineEnding.None},color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],strokeColor:t.defaults.strokeColor??"#000000",flags:t.defaults.flags??["print"]}:null},h=K({threshold:5,getTool:a,onClickDetected:(t,n)=>{const s=u();if(!s)return;const a=n.clickBehavior;if(!(null==a?void 0:a.enabled))return;const r=a.defaultAngle??0,c=a.defaultLength/2,d=t.x-c*Math.cos(r),h=t.y-c*Math.sin(r),g=t.x+c*Math.cos(r),p=t.y+c*Math.sin(r),f=l({x:d,y:h}),y=l({x:g,y:p}),m=ot([f,y],s.strokeWidth,s.lineEndings);i({...s,rect:m,linePoints:{start:f,end:y},pageIndex:o,id:e.uuidV4(),created:new Date,type:e.PdfAnnotationSubtype.LINE})}}),g=t=>{const n=c();if(!n)return null;const o=u();if(!o)return null;const i=ot([n,t],o.strokeWidth,o.lineEndings);return{type:e.PdfAnnotationSubtype.LINE,bounds:i,data:{...o,rect:i,linePoints:{start:n,end:t}}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),h.onStart(o),s(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var a;const r=c();if(!r)return;const g=l(t);if(h.hasMoved()){const t=u();if(!t)return;if(Math.abs(g.x-r.x)>2||Math.abs(g.y-r.y)>2){const n=ot([r,g],t.strokeWidth,t.lineEndings);i({...t,rect:n,linePoints:{start:r,end:g},pageIndex:o,id:e.uuidV4(),flags:["print"],created:new Date,type:e.PdfAnnotationSubtype.LINE})}}else h.onEnd(g);d(null),s(null),h.reset(),null==(a=n.releasePointerCapture)||a.call(n)},onPointerLeave:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},ct={annotationType:e.PdfAnnotationSubtype.POLYLINE,create(n){const{onCommit:o,onPreview:i,getTool:s,pageSize:a}=n,[r,c]=V([]),[d,l]=V(null),u=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),h=()=>{const t=s();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??1,lineEndings:t.defaults.lineEndings??{start:e.PdfAnnotationLineEnding.None,end:e.PdfAnnotationLineEnding.None},color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],flags:t.defaults.flags??["print"]}:null},g=()=>{const t=r(),n=d();if(0===t.length||!n)return null;const o=h();if(!o)return null;const i=[...t,n],s=ot(i,o.strokeWidth,o.lineEndings);return{type:e.PdfAnnotationSubtype.POLYLINE,bounds:s,data:{...o,rect:s,vertices:i,currentVertex:n}}};return{onClick:t=>{const e=u(t),n=r(),o=n[n.length-1];o&&Math.abs(o.x-e.x)<1&&Math.abs(o.y-e.y)<1||(c([...n,e]),l(e),i(g()))},onDoubleClick:()=>{(()=>{const t=r();if(t.length<2)return;const s=h();if(!s)return;const a=ot(t,s.strokeWidth,s.lineEndings),d={...s,vertices:t,rect:a,type:e.PdfAnnotationSubtype.POLYLINE,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};o(d),c([]),l(null),i(null)})()},onPointerMove:t=>{if(r().length>0){const e=u(t);l(e),i(g())}},onPointerCancel:()=>{c([]),l(null),i(null)}}}},dt={annotationType:e.PdfAnnotationSubtype.POLYGON,create(n){const{onCommit:o,onPreview:i,getTool:s,scale:a,pageSize:r}=n,[c,d]=V([]),[l,u]=V(null),h=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),g=()=>{const t=s();return t?{...t.defaults,color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,strokeWidth:t.defaults.strokeWidth??1,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],flags:t.defaults.flags??["print"]}:null},p=()=>{const t=c();if(t.length<3)return;const s=g();if(!s)return;const a=e.expandRect(e.rectFromPoints(t),s.strokeWidth/2),r={...s,vertices:t,rect:a,type:e.PdfAnnotationSubtype.POLYGON,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};o(r),d([]),u(null),i(null)},f=()=>{const t=c(),n=l();if(0===t.length||!n)return null;const o=g();if(!o)return null;const i=[...t,n],s=e.expandRect(e.rectFromPoints(i),o.strokeWidth/2);return{type:e.PdfAnnotationSubtype.POLYGON,bounds:s,data:{...o,rect:s,vertices:t,currentVertex:n}}};return{onClick:t=>{const e=h(t);if((t=>{const e=c();if(e.length<2)return!1;const n=14/a/2,o=e[0];return t.x>=o.x-n&&t.x<=o.x+n&&t.y>=o.y-n&&t.y<=o.y+n})(e)&&c().length>=3)return void p();const n=c(),o=n[n.length-1];o&&Math.abs(o.x-e.x)<1&&Math.abs(o.y-e.y)<1||(d([...n,e]),u(e),i(f()))},onDoubleClick:t=>{p()},onPointerMove:t=>{if(c().length>0){const e=h(t);u(e),i(f())}},onPointerCancel:t=>{d([]),u(null),i(null)}}}},lt={annotationType:e.PdfAnnotationSubtype.SQUARE,create(n){const{pageIndex:o,onCommit:i,onPreview:s,getTool:a,pageSize:r}=n,[c,d]=V(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=a();return t?{...t.defaults,flags:t.defaults.flags??["print"],strokeWidth:t.defaults.strokeWidth??2,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1}:null},h=K({threshold:5,getTool:a,onClickDetected:(n,s)=>{const a=u();if(!a)return;const c=s.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,h=d/2,g=l/2,p=t.clamp(n.x-h,0,r.width-d),f=t.clamp(n.y-g,0,r.height-l),y=a.strokeWidth,m=y/2,A={origin:{x:p-m,y:f-m},size:{width:d+y,height:l+y}},P={...a,type:e.PdfAnnotationSubtype.SQUARE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:A};i(P)}}),g=t=>{const n=c();if(!n)return null;const o=Math.min(n.x,t.x),i=Math.min(n.y,t.y),s=Math.abs(n.x-t.x),a=Math.abs(n.y-t.y),r=u();if(!r)return null;const d=r.strokeWidth,l=d/2,h={origin:{x:o-l,y:i-l},size:{width:s+d,height:a+d}};return{type:e.PdfAnnotationSubtype.SQUARE,bounds:h,data:{rect:h,...r}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),h.onStart(o),s(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var a;if(!c())return;if(!u())return;const r=l(t);if(h.hasMoved()){const t=u();if(!t)return;const n=g(r);if(n){const s={...t,type:e.PdfAnnotationSubtype.SQUARE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:n.data.rect};i(s)}}else h.onEnd(r);d(null),s(null),h.reset(),null==(a=n.releasePointerCapture)||a.call(n)},onPointerLeave:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},ut={annotationType:e.PdfAnnotationSubtype.STAMP,create(n){const{services:o,onCommit:i,getTool:s,pageSize:a}=n;return{onPointerDown:r=>{const c=s();if(!c)return;const{imageSrc:d,imageSize:l}=c.defaults,u=(o,s,d)=>{const l=r.x-s/2,u=r.y-d/2,h={origin:{x:t.clamp(l,0,a.width-s),y:t.clamp(u,0,a.height-d)},size:{width:s,height:d}},g={...c.defaults,rect:h,type:e.PdfAnnotationSubtype.STAMP,icon:c.defaults.icon??e.PdfAnnotationIcon.Draft,subject:c.defaults.subject??"Stamp",flags:c.defaults.flags??["print"],pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};i(g,{imageData:o})};d?o.processImage({source:d,maxWidth:a.width,maxHeight:a.height,onComplete:t=>u(t.imageData,(null==l?void 0:l.width)??t.width,(null==l?void 0:l.height)??t.height)}):o.requestFile({accept:"image/png,image/jpeg",onFile:t=>{o.processImage({source:t,maxWidth:a.width,maxHeight:a.height,onComplete:t=>u(t.imageData,t.width,t.height)})}})}}}},ht={annotationType:e.PdfAnnotationSubtype.CIRCLE,create(n){const{pageIndex:o,onCommit:i,onPreview:s,getTool:a,pageSize:r}=n,[c,d]=V(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=a();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??2,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,flags:t.defaults.flags??["print"]}:null},h=K({threshold:5,getTool:a,onClickDetected:(n,s)=>{const a=u();if(!a)return;const c=s.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,h=d/2,g=l/2,p=t.clamp(n.x-h,0,r.width-d),f=t.clamp(n.y-g,0,r.height-l),y=a.strokeWidth,m=y/2,A={origin:{x:p-m,y:f-m},size:{width:d+y,height:l+y}},P={...a,type:e.PdfAnnotationSubtype.CIRCLE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:A};i(P)}}),g=t=>{const n=c();if(!n)return null;const o=Math.min(n.x,t.x),i=Math.min(n.y,t.y),s=Math.abs(n.x-t.x),a=Math.abs(n.y-t.y),r=u();if(!r)return null;const d=r.strokeWidth,l=d/2,h={origin:{x:o-l,y:i-l},size:{width:s+d,height:a+d}};return{type:e.PdfAnnotationSubtype.CIRCLE,bounds:h,data:{rect:h,...r}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),h.onStart(o),s(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var a;if(!c())return;if(!u())return;const r=l(t);if(h.hasMoved()){const t=u();if(!t)return;const n=g(r);if(n){const s={...t,type:e.PdfAnnotationSubtype.CIRCLE,flags:["print"],created:new Date,id:e.uuidV4(),pageIndex:o,rect:n.data.rect};i(s)}}else h.onEnd(r);d(null),s(null),h.reset(),null==(a=n.releasePointerCapture)||a.call(n)},onPointerLeave:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},gt=(t,n)=>{var o;switch(n.type){case"vertex-edit":default:return n.changes;case"move":if(n.changes.rect){const e=n.changes.rect.origin.x-t.rect.origin.x,o=n.changes.rect.origin.y-t.rect.origin.y,i=t.inkList.map(t=>({points:t.points.map(t=>({x:t.x+e,y:t.y+o}))}));return{rect:n.changes.rect,inkList:i}}return n.changes;case"resize":if(n.changes.rect){const e=t.rect,i=n.changes.rect;let s=i.size.width/e.size.width,a=i.size.height/e.size.height;const r=10;if((i.size.width<r||i.size.height<r)&&(s=Math.max(s,r/e.size.width),a=Math.max(a,r/e.size.height),n.changes.rect={origin:i.origin,size:{width:e.size.width*s,height:e.size.height*a}}),null==(o=n.metadata)?void 0:o.maintainAspectRatio){const t=Math.min(s,a);s=t,a=t,n.changes.rect.size={width:e.size.width*t,height:e.size.height*t}}const c=(t,e)=>({origin:{x:t.origin.x+e,y:t.origin.y+e},size:{width:Math.max(1,t.size.width-2*e),height:Math.max(1,t.size.height-2*e)}}),d=Math.min(n.changes.rect.size.width/e.size.width,n.changes.rect.size.height/e.size.height),l=Math.max(1,Math.round(t.strokeWidth*d)),u=c(e,t.strokeWidth/2),h=c(n.changes.rect,l/2),g=h.size.width/Math.max(u.size.width,1e-6),p=h.size.height/Math.max(u.size.height,1e-6),f=t.inkList.map(t=>({points:t.points.map(t=>({x:h.origin.x+(t.x-u.origin.x)*g,y:h.origin.y+(t.y-u.origin.y)*p}))}));return{rect:n.changes.rect,inkList:f,strokeWidth:l}}return n.changes;case"property-update":if(void 0!==n.changes.strokeWidth){const o={...t,...n.changes},i=o.inkList.flatMap(t=>t.points),s=e.expandRect(e.rectFromPoints(i),o.strokeWidth/2);return{...n.changes,rect:s}}return n.changes}},pt=(t,e)=>{var n;switch(e.type){case"vertex-edit":if(e.changes.linePoints){const{start:n,end:o}=e.changes.linePoints;return{rect:ot([n,o],t.strokeWidth,t.lineEndings),linePoints:{start:n,end:o}}}return e.changes;case"move":if(e.changes.rect){const n=e.changes.rect.origin.x-t.rect.origin.x,o=e.changes.rect.origin.y-t.rect.origin.y;return{rect:e.changes.rect,linePoints:{start:{x:t.linePoints.start.x+n,y:t.linePoints.start.y+o},end:{x:t.linePoints.end.x+n,y:t.linePoints.end.y+o}}}}return e.changes;case"resize":if(e.changes.rect){const o=t.rect,i=e.changes.rect;let s=i.size.width/o.size.width,a=i.size.height/o.size.height;const r=10;if((i.size.width<r||i.size.height<r)&&(s=Math.max(s,r/o.size.width),a=Math.max(a,r/o.size.height),e.changes.rect={origin:i.origin,size:{width:o.size.width*s,height:o.size.height*a}}),null==(n=e.metadata)?void 0:n.maintainAspectRatio){const t=Math.min(s,a);s=t,a=t,e.changes.rect.size={width:o.size.width*t,height:o.size.height*t}}const c={start:{x:e.changes.rect.origin.x+(t.linePoints.start.x-o.origin.x)*s,y:e.changes.rect.origin.y+(t.linePoints.start.y-o.origin.y)*a},end:{x:e.changes.rect.origin.x+(t.linePoints.end.x-o.origin.x)*s,y:e.changes.rect.origin.y+(t.linePoints.end.y-o.origin.y)*a}};return{rect:e.changes.rect,linePoints:c}}return e.changes;case"property-update":if(e.changes.strokeWidth||e.changes.lineEndings){const n={...t,...e.changes},o=ot([n.linePoints.start,n.linePoints.end],n.strokeWidth,n.lineEndings);return{...e.changes,rect:o}}return e.changes;default:return e.changes}},ft=(t,e)=>{var n;switch(e.type){case"vertex-edit":return e.changes.vertices&&e.changes.vertices.length?{rect:ot(e.changes.vertices,t.strokeWidth,t.lineEndings),vertices:e.changes.vertices}:e.changes;case"move":if(e.changes.rect){const n=e.changes.rect.origin.x-t.rect.origin.x,o=e.changes.rect.origin.y-t.rect.origin.y,i=t.vertices.map(t=>({x:t.x+n,y:t.y+o}));return{rect:e.changes.rect,vertices:i}}return e.changes;case"resize":if(e.changes.rect){const o=t.rect,i=e.changes.rect;let s=i.size.width/o.size.width,a=i.size.height/o.size.height;const r=10;if((i.size.width<r||i.size.height<r)&&(s=Math.max(s,r/o.size.width),a=Math.max(a,r/o.size.height),e.changes.rect={origin:i.origin,size:{width:o.size.width*s,height:o.size.height*a}}),null==(n=e.metadata)?void 0:n.maintainAspectRatio){const t=Math.min(s,a);s=t,a=t,e.changes.rect.size={width:o.size.width*t,height:o.size.height*t}}const c=t.vertices.map(t=>({x:e.changes.rect.origin.x+(t.x-o.origin.x)*s,y:e.changes.rect.origin.y+(t.y-o.origin.y)*a}));return{rect:e.changes.rect,vertices:c}}return e.changes;case"property-update":if(void 0!==e.changes.strokeWidth||void 0!==e.changes.lineEndings){const n={...t,...e.changes},o=ot(n.vertices,n.strokeWidth,n.lineEndings);return{...e.changes,rect:o}}return e.changes;default:return e.changes}},yt=(t,n)=>{var o;switch(n.type){case"vertex-edit":if(n.changes.vertices&&n.changes.vertices.length){const o=t.strokeWidth/2;return{rect:e.expandRect(e.rectFromPoints(n.changes.vertices),o),vertices:n.changes.vertices}}return n.changes;case"move":if(n.changes.rect){const e=n.changes.rect.origin.x-t.rect.origin.x,o=n.changes.rect.origin.y-t.rect.origin.y,i=t.vertices.map(t=>({x:t.x+e,y:t.y+o}));return{rect:n.changes.rect,vertices:i}}return n.changes;case"resize":if(n.changes.rect){const e=t.rect,i=n.changes.rect;let s=i.size.width/e.size.width,a=i.size.height/e.size.height;const r=10;if((i.size.width<r||i.size.height<r)&&(s=Math.max(s,r/e.size.width),a=Math.max(a,r/e.size.height),n.changes.rect={origin:i.origin,size:{width:e.size.width*s,height:e.size.height*a}}),null==(o=n.metadata)?void 0:o.maintainAspectRatio){const t=Math.min(s,a);s=t,a=t,n.changes.rect.size={width:e.size.width*t,height:e.size.height*t}}const c=t.vertices.map(t=>({x:n.changes.rect.origin.x+(t.x-e.origin.x)*s,y:n.changes.rect.origin.y+(t.y-e.origin.y)*a}));return{rect:n.changes.rect,vertices:c}}return n.changes;case"property-update":if(void 0!==n.changes.strokeWidth){const o={...t,...n.changes},i=o.strokeWidth/2,s=e.expandRect(e.rectFromPoints(o.vertices),i);return{...n.changes,rect:s}}return n.changes;default:return n.changes}},mt=class extends t.BasePlugin{constructor(e,n,o){var i,s,a;super(e,n),this.ANNOTATION_HISTORY_TOPIC="annotations",this.state$=t.createBehaviorEmitter(),this.pendingContexts=new Map,this.isInitialLoadComplete=new Map,this.importQueue=new Map,this.handlerFactories=new Map,this.activeTool$=t.createBehaviorEmitter(),this.events$=t.createBehaviorEmitter(),this.toolsChange$=t.createBehaviorEmitter(),this.patchRegistry=new it,this.config=o,this.selection=(null==(i=n.getPlugin("selection"))?void 0:i.provides())??null,this.history=(null==(s=n.getPlugin("history"))?void 0:s.provides())??null,this.interactionManager=(null==(a=n.getPlugin("interaction-manager"))?void 0:a.provides())??null,this.registerHandlerFactories(),this.registerBuiltInPatches()}onDocumentLoadingStarted(t){this.dispatch(function(t,e){return{type:i,payload:{documentId:t,state:e}}}(t,{pages:{},byUid:{},selectedUid:null,activeToolId:null,hasPendingChanges:!1})),this.pendingContexts.set(t,new Map),this.isInitialLoadComplete.set(t,!1),this.importQueue.set(t,[]),this.logger.debug("AnnotationPlugin","DocumentOpened",`Initialized annotation state for document: ${t}`)}onDocumentLoaded(t){const e=this.getCoreDocument(t);if((null==e?void 0:e.document)&&this.getAllAnnotations(t,e.document),this.selection)for(const n of this.state.tools)n.interaction.textSelection&&this.selection.enableForMode(n.interaction.mode??n.id)}onDocumentClosed(t){this.dispatch(function(t){return{type:s,payload:t}}(t)),this.pendingContexts.delete(t),this.isInitialLoadComplete.delete(t),this.importQueue.delete(t),this.logger.debug("AnnotationPlugin","DocumentClosed",`Cleaned up annotation state for document: ${t}`)}registerHandlerFactories(){this.handlerFactories.set(e.PdfAnnotationSubtype.CIRCLE,ht),this.handlerFactories.set(e.PdfAnnotationSubtype.SQUARE,lt),this.handlerFactories.set(e.PdfAnnotationSubtype.STAMP,ut),this.handlerFactories.set(e.PdfAnnotationSubtype.POLYGON,dt),this.handlerFactories.set(e.PdfAnnotationSubtype.POLYLINE,ct),this.handlerFactories.set(e.PdfAnnotationSubtype.LINE,rt),this.handlerFactories.set(e.PdfAnnotationSubtype.INK,q),this.handlerFactories.set(e.PdfAnnotationSubtype.FREETEXT,X)}registerBuiltInPatches(){this.patchRegistry.register(e.PdfAnnotationSubtype.INK,gt),this.patchRegistry.register(e.PdfAnnotationSubtype.LINE,pt),this.patchRegistry.register(e.PdfAnnotationSubtype.POLYLINE,ft),this.patchRegistry.register(e.PdfAnnotationSubtype.POLYGON,yt)}async initialize(){var t,n;this.state.tools.forEach(t=>this.registerInteractionForTool(t)),this.history&&this.history.onHistoryChange(t=>{t.topic===this.ANNOTATION_HISTORY_TOPIC&&!1!==this.config.autoCommit&&this.commit(t.documentId)}),null==(t=this.interactionManager)||t.onModeChange(t=>{var e,n;const o=(null==(e=this.state.tools.find(e=>(e.interaction.mode??e.id)===t.activeMode))?void 0:e.id)??null;o!==((null==(n=this.state.documents[t.documentId])?void 0:n.activeToolId)??null)&&t.documentId&&this.dispatch(P(t.documentId,o))}),null==(n=this.selection)||n.onEndSelection(({documentId:t})=>{var n,o,i;if(!this.checkPermission(t,e.PdfPermissionFlag.ModifyAnnotations))return;const s=this.getActiveTool(t);if(!s||!s.interaction.textSelection)return;const a=null==(n=this.selection)?void 0:n.getFormattedSelection(),r=null==(o=this.selection)?void 0:o.getSelectedText();if(a&&r){for(const n of a)r.wait(o=>{const i=e.uuidV4();this.createAnnotation(n.pageIndex,{...s.defaults,rect:n.rect,segmentRects:n.segmentRects,pageIndex:n.pageIndex,created:new Date,id:i,custom:{text:o.join("\n")}},void 0,t),this.getToolBehavior(s,"deactivateToolAfterCreate")&&this.setActiveTool(null,t),this.getToolBehavior(s,"selectAfterCreate")&&this.selectAnnotation(n.pageIndex,i,t)},e.ignore);null==(i=this.selection)||i.clear()}})}registerInteractionForTool(t){var e;null==(e=this.interactionManager)||e.registerMode({id:t.interaction.mode??t.id,scope:"page",exclusive:t.interaction.exclusive,cursor:t.interaction.cursor})}buildCapability(){return{getActiveTool:()=>this.getActiveTool(),setActiveTool:t=>this.setActiveTool(t),getState:()=>this.getDocumentState(),getPageAnnotations:t=>this.getPageAnnotations(t),getSelectedAnnotation:()=>this.getSelectedAnnotation(),getAnnotationById:t=>this.getAnnotationById(t),selectAnnotation:(t,e)=>this.selectAnnotation(t,e),deselectAnnotation:()=>this.deselectAnnotation(),importAnnotations:t=>this.importAnnotations(t),createAnnotation:(t,e,n)=>this.createAnnotation(t,e,n),updateAnnotation:(t,e,n)=>this.updateAnnotation(t,e,n),deleteAnnotation:(t,e)=>this.deleteAnnotation(t,e),renderAnnotation:t=>this.renderAnnotation(t),commit:()=>this.commit(),forDocument:t=>this.createAnnotationScope(t),getTools:()=>this.state.tools,getTool:t=>this.getTool(t),addTool:t=>{this.dispatch((t=>({type:m,payload:t}))(t)),this.registerInteractionForTool(t)},findToolForAnnotation:t=>this.findToolForAnnotation(t),setToolDefaults:(t,e)=>this.dispatch(((t,e)=>({type:y,payload:{toolId:t,patch:e}}))(t,e)),getColorPresets:()=>[...this.state.colorPresets],addColorPreset:t=>this.dispatch({type:f,payload:t}),transformAnnotation:(t,e)=>this.transformAnnotation(t,e),registerPatchFunction:(t,e)=>this.registerPatchFunction(t,e),onStateChange:this.state$.on,onActiveToolChange:this.activeTool$.on,onAnnotationEvent:this.events$.on,onToolsChange:this.toolsChange$.on}}createAnnotationScope(t){return{getState:()=>this.getDocumentState(t),getPageAnnotations:e=>this.getPageAnnotations(e,t),getSelectedAnnotation:()=>this.getSelectedAnnotation(t),getAnnotationById:e=>this.getAnnotationById(e,t),selectAnnotation:(e,n)=>this.selectAnnotation(e,n,t),deselectAnnotation:()=>this.deselectAnnotation(t),getActiveTool:()=>this.getActiveTool(t),setActiveTool:e=>this.setActiveTool(e,t),findToolForAnnotation:t=>this.findToolForAnnotation(t),importAnnotations:e=>this.importAnnotations(e,t),createAnnotation:(e,n,o)=>this.createAnnotation(e,n,o,t),updateAnnotation:(e,n,o)=>this.updateAnnotation(e,n,o,t),deleteAnnotation:(e,n)=>this.deleteAnnotation(e,n,t),renderAnnotation:e=>this.renderAnnotation(e,t),commit:()=>this.commit(t),onStateChange:e=>this.state$.on(n=>{n.documentId===t&&e(n.state)}),onAnnotationEvent:e=>this.events$.on(n=>{n.documentId===t&&e(n)}),onActiveToolChange:e=>this.activeTool$.on(n=>{n.documentId===t&&e(n.tool)})}}onStoreUpdated(t,e){for(const n in e.documents){const o=t.documents[n],i=e.documents[n];o!==i&&(this.state$.emit({documentId:n,state:i}),o&&o.activeToolId!==i.activeToolId&&this.activeTool$.emit({documentId:n,tool:this.getActiveTool(n)}))}if(t.tools!==e.tools){for(const t in e.documents)this.activeTool$.emit({documentId:t,tool:this.getActiveTool(t)});this.toolsChange$.emit({tools:e.tools})}}registerPatchFunction(t,e){this.patchRegistry.register(t,e)}transformAnnotation(t,e){const n={type:e.type,changes:e.changes,metadata:e.metadata};return this.patchRegistry.transform(t,n)}registerPageHandlers(t,e,n,o){var i;const s=this.getCoreDocument(t),a=null==(i=null==s?void 0:s.document)?void 0:i.pages[e];if(!a)return()=>{};if(!this.interactionManager)return()=>{};const r=[];for(const c of this.state.tools){if(!c.defaults.type)continue;const i=this.handlerFactories.get(c.defaults.type);if(!i)continue;const s={pageIndex:e,pageSize:a.size,scale:n,services:o.services,onPreview:t=>o.onPreview(c.id,t),onCommit:(n,o)=>{this.createAnnotation(e,n,o,t),this.getToolBehavior(c,"deactivateToolAfterCreate")&&this.setActiveTool(null,t),this.getToolBehavior(c,"selectAfterCreate")&&this.selectAnnotation(e,n.id,t)},getTool:()=>this.state.tools.find(t=>t.id===c.id)},d=this.interactionManager.registerHandlers({documentId:t,modeId:c.interaction.mode??c.id,handlers:i.create(s),pageIndex:e});r.push(d)}return()=>r.forEach(t=>t())}getDocumentState(t){const e=t??this.getActiveDocumentId(),n=this.state.documents[e];if(!n)throw new Error(`Annotation state not found for document: ${e}`);return n}getAllAnnotations(t,n){this.engine.getAllAnnotations(n).wait(e=>{this.dispatch(((t,e)=>({type:a,payload:{documentId:t,annotations:e}}))(t,e)),this.isInitialLoadComplete.set(t,!0);const n=this.importQueue.get(t);n&&n.length>0&&this.processImportQueue(t),this.events$.emit({type:"loaded",documentId:t,total:Object.values(e).reduce((t,e)=>t+e.length,0)})},e.ignore)}getPageAnnotations(t,n){const{pageIndex:o}=t,i=n??this.getActiveDocumentId(),s=this.getCoreDocument(i),a=null==s?void 0:s.document;if(!a)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const r=a.pages.find(t=>t.index===o);return r?this.engine.getPageAnnotations(a,r):e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"})}getSelectedAnnotation(t){return j(this.getDocumentState(t))}getAnnotationById(t,e){const n=this.getDocumentState(e);return W(n,t)}renderAnnotation({pageIndex:t,annotation:n,options:o},i){const s=i??this.getActiveDocumentId(),a=this.getCoreDocument(s),r=null==a?void 0:a.document;if(!r)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const c=r.pages.find(e=>e.index===t);return c?this.engine.renderPageAnnotation(r,c,n,o):e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"})}importAnnotations(t,e){const n=e??this.getActiveDocumentId();if(!this.isInitialLoadComplete.get(n)){const e=this.importQueue.get(n)||[];return e.push(...t),void this.importQueue.set(n,e)}this.processImportItems(n,t)}processImportQueue(t){const e=this.importQueue.get(t);if(!e||0===e.length)return;const n=[...e];this.importQueue.set(t,[]),this.processImportItems(t,n)}processImportItems(t,e){const n=this.pendingContexts.get(t);if(n){for(const o of e){const{annotation:e,ctx:i}=o,s=e.pageIndex,a=e.id;this.dispatch(x(t,s,e)),i&&n.set(a,i)}!1!==this.config.autoCommit&&this.commit(t)}}createAnnotation(t,n,o,i){const s=i??this.getActiveDocumentId();if(!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","CreateAnnotation",`Cannot create annotation: document ${s} lacks ModifyAnnotations permission`);const a=n.id,r=this.pendingContexts.get(s);if(!r)return;const c={...n,author:n.author??this.config.annotationAuthor},d=()=>{this.dispatch(x(s,t,c)),o&&r.set(a,o),this.events$.emit({type:"create",documentId:s,annotation:c,pageIndex:t,ctx:o,committed:!1})};if(!this.history)return d(),void(this.config.autoCommit&&this.commit(s));const l={execute:d,undo:()=>{r.delete(a),this.dispatch(A(s)),this.dispatch(I(s,t,a)),this.events$.emit({type:"delete",documentId:s,annotation:c,pageIndex:t,committed:!1})}};this.history.forDocument(s).register(l,this.ANNOTATION_HISTORY_TOPIC)}buildPatch(t,e){return"rect"in e?e:this.transformAnnotation(t,{type:"property-update",changes:e})}updateAnnotation(t,n,o,i){const s=i??this.getActiveDocumentId();if(!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","UpdateAnnotation",`Cannot update annotation: document ${s} lacks ModifyAnnotations permission`);const a=this.getDocumentState(s).byUid[n].object,r=this.buildPatch(a,{...o,author:o.author??this.config.annotationAuthor}),c=()=>{this.dispatch(v(s,t,n,r)),this.events$.emit({type:"update",documentId:s,annotation:a,pageIndex:t,patch:r,committed:!1})};if(!this.history)return c(),void(!1!==this.config.autoCommit&&this.commit(s));const d=Object.fromEntries(Object.keys(o).map(t=>[t,a[t]])),l={execute:c,undo:()=>{this.dispatch(v(s,t,n,d)),this.events$.emit({type:"update",documentId:s,annotation:a,pageIndex:t,patch:d,committed:!1})}};this.history.forDocument(s).register(l,this.ANNOTATION_HISTORY_TOPIC)}deleteAnnotation(t,n,o){var i;const s=o??this.getActiveDocumentId();if(!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","DeleteAnnotation",`Cannot delete annotation: document ${s} lacks ModifyAnnotations permission`);const a=null==(i=this.getDocumentState(s).byUid[n])?void 0:i.object;if(!a)return;const r=()=>{this.dispatch(A(s)),this.dispatch(I(s,t,n)),this.events$.emit({type:"delete",documentId:s,annotation:a,pageIndex:t,committed:!1})};if(!this.history)return r(),void(!1!==this.config.autoCommit&&this.commit(s));const c={execute:r,undo:()=>{this.dispatch(x(s,t,a)),this.events$.emit({type:"create",documentId:s,annotation:a,pageIndex:t,committed:!1})}};this.history.forDocument(s).register(c,this.ANNOTATION_HISTORY_TOPIC)}selectAnnotation(t,e,n){const o=n??this.getActiveDocumentId();this.dispatch(((t,e,n)=>({type:r,payload:{documentId:t,pageIndex:e,id:n}}))(o,t,e))}deselectAnnotation(t){const e=t??this.getActiveDocumentId();this.dispatch(A(e))}getActiveTool(t){const e=this.getDocumentState(t);return e.activeToolId?this.state.tools.find(t=>t.id===e.activeToolId)??null:null}setActiveTool(t,n){var o,i;const s=n??this.getActiveDocumentId();if(null!==t&&!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","SetActiveTool",`Cannot activate tool: document ${s} lacks ModifyAnnotations permission`);const a=this.getDocumentState(s);if(t===a.activeToolId)return;this.dispatch(P(s,t));const r=this.state.tools.find(e=>e.id===t);r?null==(o=this.interactionManager)||o.forDocument(s).activate(r.interaction.mode??r.id):null==(i=this.interactionManager)||i.forDocument(s).activateDefaultMode()}getTool(t){return this.state.tools.find(e=>e.id===t)}findToolForAnnotation(t){let e=null,n=0;for(const o of this.state.tools){const i=o.matchScore(t);i>n&&(n=i,e=o)}return e}commit(t){const n=new e.Task,o=t??this.getActiveDocumentId(),i=this.getDocumentState(o);if(!i.hasPendingChanges)return e.PdfTaskHelper.resolve(!0);const s=this.getCoreDocument(o),a=null==s?void 0:s.document;if(!a)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const r=this.pendingContexts.get(o);if(!r)return e.PdfTaskHelper.resolve(!0);const c=[],d=[],l=[];for(const[g,p]of Object.entries(i.byUid)){if("synced"===p.commitState)continue;const t=a.pages.find(t=>t.index===p.object.pageIndex);if(t)switch(p.commitState){case"new":const n=r.get(p.object.id),i=this.engine.createPageAnnotation(a,t,p.object,n);i.wait(()=>{this.events$.emit({type:"create",documentId:o,annotation:p.object,pageIndex:p.object.pageIndex,ctx:n,committed:!0}),r.delete(p.object.id)},e.ignore),c.push(i);break;case"dirty":const s=this.engine.updatePageAnnotation(a,t,p.object);s.wait(()=>{this.events$.emit({type:"update",documentId:o,annotation:p.object,pageIndex:p.object.pageIndex,patch:p.object,committed:!0})},e.ignore),d.push(s);break;case"deleted":l.push({ta:p,uid:g})}}const u=[];for(const{ta:g,uid:p}of l){const t=a.pages.find(t=>t.index===g.object.pageIndex);if("deleted"===g.commitState&&g.object.id){const n=new e.Task;this.engine.removePageAnnotation(a,t,g.object).wait(()=>{this.dispatch(S(o,p)),this.events$.emit({type:"delete",documentId:o,annotation:g.object,pageIndex:g.object.pageIndex,committed:!0}),n.resolve(!0)},n.fail),u.push(n)}else this.dispatch(S(o,p))}const h=[...c,...d,...u];return e.Task.allSettled(h).wait(()=>{this.dispatch((t=>({type:g,payload:{documentId:t}}))(o)),n.resolve(!0)},n.fail),n}getToolBehavior(t,e){var n;return void 0!==(null==(n=t.behavior)?void 0:n[e])?t.behavior[e]:!1!==this.config[e]}};mt.id="annotation";let At=mt;function Pt(t){return e=>(null==e?void 0:e.id)===t}const xt=Pt("highlight"),vt=Pt("squiggly"),It=Pt("underline"),St=Pt("strikeout"),bt=Pt("ink"),Tt=Pt("inkHighlighter"),Ct=Pt("square"),Et=Pt("circle"),kt=Pt("line"),Nt=Pt("polyline"),wt=Pt("polygon"),Dt=Pt("freeText"),Mt=Pt("stamp"),Lt={manifest:o,create:(t,e)=>new At(n,t,e),reducer:(t,e)=>{switch(e.type){case i:{const{documentId:n,state:o}=e.payload;return{...t,documents:{...t.documents,[n]:o},activeDocumentId:t.activeDocumentId??n}}case s:{const n=e.payload,{[n]:o,...i}=t.documents;return{...t,documents:i,activeDocumentId:t.activeDocumentId===n?null:t.activeDocumentId}}case"ANNOTATION/SET_ACTIVE_DOCUMENT":return{...t,activeDocumentId:e.payload};case a:{const{documentId:n,annotations:o}=e.payload,i=t.documents[n];if(!i)return t;const s={},a={};for(const[t,e]of Object.entries(o)){const n=Number(t),o=i.pages[n]||[];for(const t of o)delete a[t];const r=e.map(t=>{const e=t.id;return a[e]={commitState:"synced",object:t},e});s[n]=r}return{...t,documents:{...t.documents,[n]:{...i,pages:s,byUid:a}}}}case r:{const{documentId:n,id:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,selectedUid:o}}}:t}case c:{const{documentId:n}=e.payload,o=t.documents[n];return o?{...t,documents:{...t.documents,[n]:{...o,selectedUid:null}}}:t}case d:{const{documentId:n,toolId:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,activeToolId:o}}}:t}case l:{const{documentId:n,pageIndex:o,annotation:i}=e.payload,s=t.documents[n];if(!s)return t;const a=i.id;return{...t,documents:{...t.documents,[n]:{...s,pages:{...s.pages,[o]:[...s.pages[o]??[],a]},byUid:{...s.byUid,[a]:{commitState:"new",object:i}},hasPendingChanges:!0}}}}case h:{const{documentId:n,pageIndex:o,id:i}=e.payload,s=t.documents[n];return s&&s.byUid[i]?{...t,documents:{...t.documents,[n]:{...s,pages:{...s.pages,[o]:(s.pages[o]??[]).filter(t=>t!==i)},byUid:{...s.byUid,[i]:{...s.byUid[i],commitState:"deleted"}},hasPendingChanges:!0}}}:t}case u:{const{documentId:n,id:o,patch:i}=e.payload,s=t.documents[n];return s?{...t,documents:{...t.documents,[n]:Y(s,o,i)}}:t}case g:{const{documentId:n}=e.payload,o=t.documents[n];if(!o)return t;const i={};for(const[t,e]of Object.entries(o.byUid))i[t]={...e,commitState:"dirty"===e.commitState||"new"===e.commitState?"synced":e.commitState};return{...t,documents:{...t.documents,[n]:{...o,byUid:i,hasPendingChanges:!1}}}}case p:{const{documentId:n,uid:o}=e.payload,i=t.documents[n];if(!i)return t;const{[o]:s,...a}=i.byUid;return{...t,documents:{...t.documents,[n]:{...i,byUid:a}}}}case m:{const n=new Map(t.tools.map(t=>[t.id,t]));return n.set(e.payload.id,e.payload),{...t,tools:Array.from(n.values())}}case y:{const{toolId:n,patch:o}=e.payload;return{...t,tools:t.tools.map(t=>t.id===n?{...t,defaults:{...t.defaults,...o}}:t)}}case f:return t.colorPresets.includes(e.payload)?t:{...t,colorPresets:[...t.colorPresets,e.payload]};default:return t}},initialState:(t,e)=>Q(e)};exports.ANNOTATION_PLUGIN_ID=n,exports.AnnotationPlugin=At,exports.AnnotationPluginPackage=Lt,exports.createToolPredicate=Pt,exports.getAnnotationByUid=W,exports.getAnnotations=t=>{const e={};for(const n of Object.keys(t.pages).map(Number))e[n]=$(t,n);return e},exports.getAnnotationsByPageIndex=$,exports.getSelectedAnnotation=j,exports.getSelectedAnnotationByPageIndex=(t,e)=>{if(!t.selectedUid)return null;return(t.pages[e]??[]).includes(t.selectedUid)?t.byUid[t.selectedUid]:null},exports.getSidebarAnnotationsWithReplies=t=>{const e=B(t),n=[],o=Object.keys(e).map(Number).sort((t,e)=>t-e);for(const i of o)n.push(...e[i]);return n},exports.getSidebarAnnotationsWithRepliesGroupedByPage=B,exports.getToolDefaultsById=function(t,e){const n=t.tools.find(t=>t.id===e);return null==n?void 0:n.defaults},exports.initialDocumentState=G,exports.initialState=Q,exports.isAnnotationSelected=(t,e)=>t.selectedUid===e,exports.isCircle=T,exports.isCircleTool=Et,exports.isFreeText=z,exports.isFreeTextTool=Dt,exports.isHighlight=w,exports.isHighlightTool=xt,exports.isInk=b,exports.isInkHighlighterTool=Tt,exports.isInkTool=bt,exports.isLine=k,exports.isLineTool=kt,exports.isPolygon=C,exports.isPolygonTool=wt,exports.isPolyline=N,exports.isPolylineTool=Nt,exports.isSidebarAnnotation=U,exports.isSquare=E,exports.isSquareTool=Ct,exports.isSquiggly=L,exports.isSquigglyTool=vt,exports.isStamp=R,exports.isStampTool=Mt,exports.isStrikeout=M,exports.isStrikeoutTool=St,exports.isText=F,exports.isTextMarkup=O,exports.isUnderline=D,exports.isUnderlineTool=It,exports.manifest=o,exports.patching=at;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("@embedpdf/core"),e=require("@embedpdf/models"),n="annotation",o={id:n,name:"Annotation Plugin",version:"1.0.0",provides:["annotation"],requires:["interaction-manager","selection"],optional:["history"],defaultConfig:{autoCommit:!0,annotationAuthor:"Guest",deactivateToolAfterCreate:!1,selectAfterCreate:!0}},i="ANNOTATION/INIT_STATE",s="ANNOTATION/CLEANUP_STATE",a="ANNOTATION/SET_ANNOTATIONS",r="ANNOTATION/SELECT_ANNOTATION",c="ANNOTATION/DESELECT_ANNOTATION",d="ANNOTATION/ADD_TO_SELECTION",l="ANNOTATION/REMOVE_FROM_SELECTION",h="ANNOTATION/SET_SELECTION",u="ANNOTATION/SET_ACTIVE_TOOL_ID",g="ANNOTATION/CREATE_ANNOTATION",p="ANNOTATION/PATCH_ANNOTATION",f="ANNOTATION/DELETE_ANNOTATION",m="ANNOTATION/COMMIT",y="ANNOTATION/PURGE_ANNOTATION",A="ANNOTATION/ADD_COLOR_PRESET",x="ANNOTATION/SET_TOOL_DEFAULTS",I="ANNOTATION/ADD_TOOL";const P=t=>({type:c,payload:{documentId:t}}),b=(t,e,n)=>({type:d,payload:{documentId:t,pageIndex:e,id:n}}),v=(t,e)=>({type:l,payload:{documentId:t,id:e}}),S=(t,e)=>({type:h,payload:{documentId:t,ids:e}}),T=(t,e)=>({type:u,payload:{documentId:t,toolId:e}}),M=(t,e,n)=>({type:g,payload:{documentId:t,pageIndex:e,annotation:n}}),k=(t,e,n,o)=>({type:p,payload:{documentId:t,pageIndex:e,id:n,patch:o}}),C=(t,e,n)=>({type:f,payload:{documentId:t,pageIndex:e,id:n}}),z=(t,e)=>({type:y,payload:{documentId:t,uid:e}});function D(t,e){return!(t.origin.x+t.size.width<e.origin.x||e.origin.x+e.size.width<t.origin.x||t.origin.y+t.size.height<e.origin.y||e.origin.y+e.size.height<t.origin.y)}function E(t){return t.object.type===e.PdfAnnotationSubtype.INK}function R(t){return t.object.type===e.PdfAnnotationSubtype.CIRCLE}function w(t){return t.object.type===e.PdfAnnotationSubtype.POLYGON}function L(t){return t.object.type===e.PdfAnnotationSubtype.SQUARE}function N(t){return t.object.type===e.PdfAnnotationSubtype.LINE}function O(t){return t.object.type===e.PdfAnnotationSubtype.POLYLINE}function j(t){return t.object.type===e.PdfAnnotationSubtype.HIGHLIGHT}function U(t){return t.object.type===e.PdfAnnotationSubtype.UNDERLINE}function F(t){return t.object.type===e.PdfAnnotationSubtype.STRIKEOUT}function $(t){return t.object.type===e.PdfAnnotationSubtype.SQUIGGLY}function G(t){return j(t)||U(t)||F(t)||$(t)}function B(t){return t.object.type===e.PdfAnnotationSubtype.FREETEXT}function W(t){return t.object.type===e.PdfAnnotationSubtype.STAMP}function H(t){return t.object.type===e.PdfAnnotationSubtype.TEXT}function _(t){return t.object.type===e.PdfAnnotationSubtype.LINK}function Y(t){return G(t)||E(t)||L(t)||R(t)||w(t)||N(t)||O(t)||B(t)||W(t)}const K=(t,e)=>(t.pages[e]??[]).map(e=>t.byUid[e]),Q=t=>t.selectedUids.length>0?t.byUid[t.selectedUids[0]]??null:null,V=t=>t.selectedUids.map(e=>t.byUid[e]).filter(t=>void 0!==t),q=t=>t.selectedUids,X=(t,e)=>t.byUid[e]??null;const Z=t=>{const e={};for(const o of Object.values(t.pages))for(const n of o){const o=t.byUid[n];if(o&&H(o)){const t=o.object.inReplyToId;t&&(e[t]||(e[t]=[])).push(o)}}const n={};for(const[o,i]of Object.entries(t.pages)){const s=Number(o),a=[];for(const n of i){const o=t.byUid[n];o&&Y(o)&&a.push({page:s,annotation:o,replies:e[o.object.id]??[]})}a.length>0&&(n[s]=a)}return n},J=(t,e)=>{const n=[];for(const o of Object.values(t.pages))for(const i of o){const o=t.byUid[i];o&&"inReplyToId"in o.object&&o.object.inReplyToId===e&&n.push({id:o.object.id,pageIndex:o.object.pageIndex})}return n},tt=(t,e,n)=>{const o=[];for(const i of Object.values(t.pages))for(const s of i){const i=t.byUid[s];i&&"inReplyToId"in i.object&&i.object.inReplyToId===e&&n.includes(i.object.type)&&o.push(i)}return o},et=(t,n)=>tt(t,n,[e.PdfAnnotationSubtype.LINK]),nt=(t,n)=>{const o=t.byUid[n];if(o)return o.object.inReplyToId&&o.object.replyType===e.PdfAnnotationReplyType.Group?o.object.inReplyToId:n},ot=(t,n)=>{const o=nt(t,n);if(!o)return[];const i=[],s=t.byUid[o];s&&s.object.type!==e.PdfAnnotationSubtype.LINK&&i.push(s);for(const a of Object.values(t.pages))for(const n of a){const s=t.byUid[n];s&&s.object.inReplyToId===o&&s.object.replyType===e.PdfAnnotationReplyType.Group&&s.object.type!==e.PdfAnnotationSubtype.LINK&&i.push(s)}return i},it=(t,n)=>{const o=t.byUid[n];if(!o)return!1;if(o.object.type===e.PdfAnnotationSubtype.LINK)return!1;if(o.object.inReplyToId&&o.object.replyType===e.PdfAnnotationReplyType.Group)return!0;for(const i of Object.values(t.pages))for(const o of i){const i=t.byUid[o];if(i&&i.object.inReplyToId===n&&i.object.replyType===e.PdfAnnotationReplyType.Group&&i.object.type!==e.PdfAnnotationSubtype.LINK)return!0}return!1},st=t=>{const e=V(t);if(0===e.length)return"disabled";const n=e[0].object.id;if(it(t,n)){const o=ot(t,n),i=new Set(o.map(t=>t.object.id));if(e.every(t=>i.has(t.object.id)))return"ungroup"}return e.length>=2?"group":"disabled"},at=[{id:"highlight",name:"Highlight",matchScore:t=>t.type===e.PdfAnnotationSubtype.HIGHLIGHT?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.HIGHLIGHT,strokeColor:"#FFCD45",color:"#FFCD45",opacity:1,blendMode:e.PdfBlendMode.Multiply}},{id:"underline",name:"Underline",matchScore:t=>t.type===e.PdfAnnotationSubtype.UNDERLINE?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.UNDERLINE,strokeColor:"#E44234",color:"#E44234",opacity:1}},{id:"strikeout",name:"Strikeout",matchScore:t=>t.type===e.PdfAnnotationSubtype.STRIKEOUT?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.STRIKEOUT,strokeColor:"#E44234",color:"#E44234",opacity:1}},{id:"squiggly",name:"Squiggly",matchScore:t=>t.type===e.PdfAnnotationSubtype.SQUIGGLY?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.SQUIGGLY,strokeColor:"#E44234",color:"#E44234",opacity:1}},{id:"ink",name:"Pen",matchScore:t=>t.type===e.PdfAnnotationSubtype.INK&&"InkHighlight"!==t.intent?5:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.INK,strokeColor:"#E44234",color:"#E44234",opacity:1,strokeWidth:6}},{id:"inkHighlighter",name:"Ink Highlighter",matchScore:t=>t.type===e.PdfAnnotationSubtype.INK&&"InkHighlight"===t.intent?10:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.INK,intent:"InkHighlight",strokeColor:"#FFCD45",color:"#FFCD45",opacity:1,strokeWidth:14,blendMode:e.PdfBlendMode.Multiply}},{id:"circle",name:"Circle",matchScore:t=>t.type===e.PdfAnnotationSubtype.CIRCLE?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.CIRCLE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234",strokeStyle:e.PdfAnnotationBorderStyle.SOLID},clickBehavior:{enabled:!0,defaultSize:{width:100,height:100}}},{id:"square",name:"Square",matchScore:t=>t.type===e.PdfAnnotationSubtype.SQUARE?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.SQUARE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234",strokeStyle:e.PdfAnnotationBorderStyle.SOLID},clickBehavior:{enabled:!0,defaultSize:{width:100,height:100}}},{id:"line",name:"Line",matchScore:t=>t.type===e.PdfAnnotationSubtype.LINE&&"LineArrow"!==t.intent?5:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1,isGroupResizable:!0},defaults:{type:e.PdfAnnotationSubtype.LINE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234"},clickBehavior:{enabled:!0,defaultLength:100,defaultAngle:0}},{id:"lineArrow",name:"Arrow",matchScore:t=>t.type===e.PdfAnnotationSubtype.LINE&&"LineArrow"===t.intent?10:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1,isGroupResizable:!0},defaults:{type:e.PdfAnnotationSubtype.LINE,intent:"LineArrow",color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234",lineEndings:{start:e.PdfAnnotationLineEnding.None,end:e.PdfAnnotationLineEnding.OpenArrow}},clickBehavior:{enabled:!0,defaultLength:100,defaultAngle:0}},{id:"polyline",name:"Polyline",matchScore:t=>t.type===e.PdfAnnotationSubtype.POLYLINE?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1,isGroupResizable:!0},defaults:{type:e.PdfAnnotationSubtype.POLYLINE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234"}},{id:"polygon",name:"Polygon",matchScore:t=>t.type===e.PdfAnnotationSubtype.POLYGON?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1,isGroupResizable:!0},defaults:{type:e.PdfAnnotationSubtype.POLYGON,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234"}},{id:"freeText",name:"Free Text",matchScore:t=>t.type===e.PdfAnnotationSubtype.FREETEXT?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.FREETEXT,contents:"Insert text",fontSize:14,fontColor:"#E44234",fontFamily:e.PdfStandardFont.Helvetica,textAlign:e.PdfTextAlignment.Left,verticalAlign:e.PdfVerticalAlignment.Top,color:"transparent",backgroundColor:"transparent",opacity:1},clickBehavior:{enabled:!0,defaultSize:{width:100,height:20},defaultContent:"Insert text"}},{id:"stamp",name:"Image",matchScore:t=>t.type===e.PdfAnnotationSubtype.STAMP?1:0,interaction:{exclusive:!1,cursor:"copy",isDraggable:!0,isResizable:!0,lockAspectRatio:!0},defaults:{type:e.PdfAnnotationSubtype.STAMP}}],rt=["#E44234","#FF8D00","#FFCD45","#5CC96E","#25D2D1","#597CE2","#C544CE","#7D2E25","#000000","#FFFFFF"],ct=t=>1===t.length?t[0]:null,dt=()=>({pages:{},byUid:{},selectedUids:[],selectedUid:null,activeToolId:null,hasPendingChanges:!1}),lt=(t,e,n)=>{const o=t.byUid[e];return o?{...t,byUid:{...t.byUid,[e]:{...o,commitState:"synced"===o.commitState?"dirty":o.commitState,object:{...o.object,...n}}},hasPendingChanges:!0}:t},ht=t=>{const e=new Map;return at.forEach(t=>e.set(t.id,t)),(t.tools||[]).forEach(t=>e.set(t.id,t)),{documents:{},activeDocumentId:null,tools:Array.from(e.values()),colorPresets:t.colorPresets??rt}};function ut(t){let e=t;return[()=>e,t=>{e=t}]}const gt={annotationType:e.PdfAnnotationSubtype.INK,create(n){const{onCommit:o,onPreview:i,getTool:s,pageSize:a}=n,[r,c]=ut([]),[d,l]=ut(!1),h={current:null},u=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),g=()=>{const t=s();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??1,strokeColor:t.defaults.strokeColor??t.defaults.color??"#000000",opacity:t.defaults.opacity??1,flags:t.defaults.flags??["print"]}:null},p=()=>{const t=r();if(0===t.length||0===t[0].points.length)return null;const n=g();if(!n)return null;const o=t.flatMap(t=>t.points),i=e.expandRect(e.rectFromPoints(o),n.strokeWidth/2);return{type:e.PdfAnnotationSubtype.INK,bounds:i,data:{...n,rect:i,inkList:t}}};return{onPointerDown:(t,e)=>{var n;const o=u(t);l(!0),h.current&&clearTimeout(h.current);const s=[...r(),{points:[o]}];c(s),i(p()),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{if(!d())return;const e=r();if(0===e.length)return;const n=u(t);e[e.length-1].points.push(n),c(e),i(p())},onPointerUp:(t,s)=>{var a;l(!1),null==(a=s.releasePointerCapture)||a.call(s),h.current&&clearTimeout(h.current),h.current=setTimeout(()=>{const t=r();if(t.length>0&&t[0].points.length>1){const i=g();if(!i)return;const s=t.flatMap(t=>t.points),a=e.expandRect(e.rectFromPoints(s),i.strokeWidth/2);o({...i,inkList:t,rect:a,type:e.PdfAnnotationSubtype.INK,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date})}c([]),i(null)},800)},onPointerCancel:(t,e)=>{var n;c([]),l(!1),i(null),h.current&&clearTimeout(h.current),null==(n=e.releasePointerCapture)||n.call(e)}}}};function pt({threshold:t=5,getTool:e,onClickDetected:n}){const[o,i]=ut(null),[s,a]=ut(!1);return{onStart:t=>{i(t),a(!1)},onMove:e=>{const n=o();if(!n||s())return;Math.sqrt(Math.pow(e.x-n.x,2)+Math.pow(e.y-n.y,2))>t&&a(!0)},onEnd:t=>{var r;if(o()&&!s()){const o=e();o&&"clickBehavior"in o&&(null==(r=o.clickBehavior)?void 0:r.enabled)&&n(t,o)}i(null),a(!1)},hasMoved:s,reset:()=>{i(null),a(!1)}}}const ft={annotationType:e.PdfAnnotationSubtype.FREETEXT,create(n){const{onCommit:o,onPreview:i,getTool:s,pageSize:a,pageIndex:r}=n,[c,d]=ut(null),l=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),h=()=>{const t=s();return t?{...t.defaults,fontColor:t.defaults.fontColor??"#000000",opacity:t.defaults.opacity??1,fontSize:t.defaults.fontSize??12,fontFamily:t.defaults.fontFamily??e.PdfStandardFont.Helvetica,color:t.defaults.color??t.defaults.backgroundColor??"transparent",textAlign:t.defaults.textAlign??e.PdfTextAlignment.Left,verticalAlign:t.defaults.verticalAlign??e.PdfVerticalAlignment.Top,contents:t.defaults.contents??"Insert text here",flags:t.defaults.flags??["print"]}:null},u=pt({threshold:5,getTool:s,onClickDetected:(n,i)=>{const s=h();if(!s)return;const c=i.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,u=d/2,g=l/2,p={origin:{x:t.clamp(n.x-u,0,a.width-d),y:t.clamp(n.y-g,0,a.height-l)},size:{width:d,height:l}},f=c.defaultContent??s.contents,m={...s,contents:f,type:e.PdfAnnotationSubtype.FREETEXT,rect:p,pageIndex:r,id:e.uuidV4(),created:new Date};o(m)}}),g=t=>{const n=c();if(!n)return null;const o=h();if(!o)return null;const i={origin:{x:Math.min(n.x,t.x),y:Math.min(n.y,t.y)},size:{width:Math.abs(n.x-t.x),height:Math.abs(n.y-t.y)}};return{type:e.PdfAnnotationSubtype.FREETEXT,bounds:i,data:{...o,rect:i}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),u.onStart(o),i(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);u.onMove(e),c()&&u.hasMoved()&&i(g(e))},onPointerUp:(t,s)=>{var a;const r=c();if(!r)return;const g=h();if(!g)return;const p=l(t);if(u.hasMoved()){const t={origin:{x:Math.min(r.x,p.x),y:Math.min(r.y,p.y)},size:{width:Math.abs(r.x-p.x),height:Math.abs(r.y-p.y)}},i={...g,type:e.PdfAnnotationSubtype.FREETEXT,rect:t,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};o(i)}else u.onEnd(p);d(null),i(null),u.reset(),null==(a=s.releasePointerCapture)||a.call(s)},onPointerLeave:(t,e)=>{var n;d(null),i(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),i(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}};function mt(t){const e=t=>{const e=9*t,n=Math.PI/6;return{x:-e*Math.cos(n),y:e*Math.sin(n)}};return{getSvgPath:n=>{const{x:o,y:i}=e(n);return t?`M 0 0 L ${o} ${i} L ${o} ${-i} Z`:`M ${o} ${i} L 0 0 L ${o} ${-i}`},getLocalPoints:t=>{const{x:n,y:o}=e(t);return[{x:0,y:0},{x:n,y:o},{x:n,y:-o}]},getRotation:t=>t,filled:t}}function yt(t,e){const n=e=>e*t/2;return{getSvgPath:t=>{const e=n(t);return`M ${-e} 0 L ${e} 0`},getLocalPoints:t=>{const e=n(t);return[{x:-e,y:0},{x:e,y:0}]},getRotation:e,filled:!1}}const At=mt(!1),xt=mt(!0),It={[e.PdfAnnotationLineEnding.OpenArrow]:At,[e.PdfAnnotationLineEnding.ClosedArrow]:xt,[e.PdfAnnotationLineEnding.ROpenArrow]:{...At,getRotation:t=>t+Math.PI},[e.PdfAnnotationLineEnding.RClosedArrow]:{...xt,getRotation:t=>t+Math.PI},[e.PdfAnnotationLineEnding.Circle]:{getSvgPath:t=>{const e=5*t/2;return`M ${e} 0 A ${e} ${e} 0 1 1 ${-e} 0 A ${e} ${e} 0 1 1 ${e} 0`},getLocalPoints:t=>{const e=5*t/2;return[{x:-e,y:-e},{x:e,y:e}]},getRotation:()=>0,filled:!0},[e.PdfAnnotationLineEnding.Square]:{getSvgPath:t=>{const e=6*t/2;return`M ${-e} ${-e} L ${e} ${-e} L ${e} ${e} L ${-e} ${e} Z`},getLocalPoints:t=>{const e=6*t/2;return[{x:-e,y:-e},{x:e,y:-e},{x:e,y:e},{x:-e,y:e}]},getRotation:t=>t,filled:!0},[e.PdfAnnotationLineEnding.Diamond]:{getSvgPath:t=>{const e=6*t/2;return`M 0 ${-e} L ${e} 0 L 0 ${e} L ${-e} 0 Z`},getLocalPoints:t=>{const e=6*t/2;return[{x:0,y:-e},{x:e,y:0},{x:0,y:e},{x:-e,y:0}]},getRotation:t=>t,filled:!0},[e.PdfAnnotationLineEnding.Butt]:yt(6,t=>t+Math.PI/2),[e.PdfAnnotationLineEnding.Slash]:yt(18,t=>t+Math.PI/1.5)};function Pt(t,n,o){if(!t||0===t.length)return{origin:{x:0,y:0},size:{width:0,height:0}};const i=[...t],s=(t,e)=>Math.atan2(e.y-t.y,e.x-t.x),a=(t,o,s)=>{if(!t)return;const a=It[t];if(!a)return;const r=a.getLocalPoints(n),c=a.getRotation(s),d=r.map(t=>e.rotateAndTranslatePoint(t,c,o));i.push(...d)};if(t.length>=2){const e=s(t[1],t[0]);a(null==o?void 0:o.start,t[0],e);const n=t.length-1,i=s(t[n-1],t[n]);a(null==o?void 0:o.end,t[n],i)}if(i.length<=1){const e=t[0]||{x:0,y:0},o=n;return{origin:{x:e.x-o,y:e.y-o},size:{width:2*o,height:2*o}}}const r=e.rectFromPoints(i),c=n/2+1.2*n;return e.expandRect(r,c)}class bt{constructor(){this.patches=new Map}register(t,e){this.patches.set(t,e)}transform(t,e){const n=this.patches.get(t.type);return n?n(t,e):e.changes}}const vt=new bt,St=Object.freeze(Object.defineProperty({__proto__:null,LINE_ENDING_HANDLERS:It,PatchRegistry:bt,createEnding:function(t,e,n,o,i){if(!t)return null;const s=It[t];if(!s)return null;const a=s.getRotation(n);return{d:s.getSvgPath(e),transform:`translate(${o} ${i}) rotate(${r=a,180*r/Math.PI})`,filled:s.filled};var r},lineRectWithEndings:Pt,patchRegistry:vt},Symbol.toStringTag,{value:"Module"})),Tt={annotationType:e.PdfAnnotationSubtype.LINE,create(n){const{pageIndex:o,onCommit:i,onPreview:s,getTool:a,pageSize:r}=n,[c,d]=ut(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),h=()=>{const t=a();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??1,lineEndings:t.defaults.lineEndings??{start:e.PdfAnnotationLineEnding.None,end:e.PdfAnnotationLineEnding.None},color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],strokeColor:t.defaults.strokeColor??"#000000",flags:t.defaults.flags??["print"]}:null},u=pt({threshold:5,getTool:a,onClickDetected:(t,n)=>{const s=h();if(!s)return;const a=n.clickBehavior;if(!(null==a?void 0:a.enabled))return;const r=a.defaultAngle??0,c=a.defaultLength/2,d=t.x-c*Math.cos(r),u=t.y-c*Math.sin(r),g=t.x+c*Math.cos(r),p=t.y+c*Math.sin(r),f=l({x:d,y:u}),m=l({x:g,y:p}),y=Pt([f,m],s.strokeWidth,s.lineEndings);i({...s,rect:y,linePoints:{start:f,end:m},pageIndex:o,id:e.uuidV4(),created:new Date,type:e.PdfAnnotationSubtype.LINE})}}),g=t=>{const n=c();if(!n)return null;const o=h();if(!o)return null;const i=Pt([n,t],o.strokeWidth,o.lineEndings);return{type:e.PdfAnnotationSubtype.LINE,bounds:i,data:{...o,rect:i,linePoints:{start:n,end:t}}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),u.onStart(o),s(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);u.onMove(e),c()&&u.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var a;const r=c();if(!r)return;const g=l(t);if(u.hasMoved()){const t=h();if(!t)return;if(Math.abs(g.x-r.x)>2||Math.abs(g.y-r.y)>2){const n=Pt([r,g],t.strokeWidth,t.lineEndings);i({...t,rect:n,linePoints:{start:r,end:g},pageIndex:o,id:e.uuidV4(),flags:["print"],created:new Date,type:e.PdfAnnotationSubtype.LINE})}}else u.onEnd(g);d(null),s(null),u.reset(),null==(a=n.releasePointerCapture)||a.call(n)},onPointerLeave:(t,e)=>{var n;d(null),s(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),s(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},Mt={annotationType:e.PdfAnnotationSubtype.POLYLINE,create(n){const{onCommit:o,onPreview:i,getTool:s,pageSize:a}=n,[r,c]=ut([]),[d,l]=ut(null),h=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),u=()=>{const t=s();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??1,lineEndings:t.defaults.lineEndings??{start:e.PdfAnnotationLineEnding.None,end:e.PdfAnnotationLineEnding.None},color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],flags:t.defaults.flags??["print"]}:null},g=()=>{const t=r(),n=d();if(0===t.length||!n)return null;const o=u();if(!o)return null;const i=[...t,n],s=Pt(i,o.strokeWidth,o.lineEndings);return{type:e.PdfAnnotationSubtype.POLYLINE,bounds:s,data:{...o,rect:s,vertices:i,currentVertex:n}}};return{onClick:(t,e)=>{if(e.metaKey||e.ctrlKey)return;const n=h(t),o=r(),s=o[o.length-1];s&&Math.abs(s.x-n.x)<1&&Math.abs(s.y-n.y)<1||(c([...o,n]),l(n),i(g()))},onDoubleClick:()=>{(()=>{const t=r();if(t.length<2)return;const s=u();if(!s)return;const a=Pt(t,s.strokeWidth,s.lineEndings),d={...s,vertices:t,rect:a,type:e.PdfAnnotationSubtype.POLYLINE,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};o(d),c([]),l(null),i(null)})()},onPointerMove:t=>{if(r().length>0){const e=h(t);l(e),i(g())}},onPointerCancel:()=>{c([]),l(null),i(null)}}}},kt={annotationType:e.PdfAnnotationSubtype.POLYGON,create(n){const{onCommit:o,onPreview:i,getTool:s,scale:a,pageSize:r}=n,[c,d]=ut([]),[l,h]=ut(null),u=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),g=()=>{const t=s();return t?{...t.defaults,color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,strokeWidth:t.defaults.strokeWidth??1,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],flags:t.defaults.flags??["print"]}:null},p=()=>{const t=c();if(t.length<3)return;const s=g();if(!s)return;const a=e.expandRect(e.rectFromPoints(t),s.strokeWidth/2),r={...s,vertices:t,rect:a,type:e.PdfAnnotationSubtype.POLYGON,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};o(r),d([]),h(null),i(null)},f=()=>{const t=c(),n=l();if(0===t.length||!n)return null;const o=g();if(!o)return null;const i=[...t,n],s=e.expandRect(e.rectFromPoints(i),o.strokeWidth/2);return{type:e.PdfAnnotationSubtype.POLYGON,bounds:s,data:{...o,rect:s,vertices:t,currentVertex:n}}};return{onClick:(t,e)=>{if(e.metaKey||e.ctrlKey)return;const n=u(t);if((t=>{const e=c();if(e.length<2)return!1;const n=14/a/2,o=e[0];return t.x>=o.x-n&&t.x<=o.x+n&&t.y>=o.y-n&&t.y<=o.y+n})(n)&&c().length>=3)return void p();const o=c(),s=o[o.length-1];s&&Math.abs(s.x-n.x)<1&&Math.abs(s.y-n.y)<1||(d([...o,n]),h(n),i(f()))},onDoubleClick:t=>{p()},onPointerMove:t=>{if(c().length>0){const e=u(t);h(e),i(f())}},onPointerCancel:t=>{d([]),h(null),i(null)}}}},Ct={annotationType:e.PdfAnnotationSubtype.SQUARE,create(n){const{pageIndex:o,onCommit:i,onPreview:s,getTool:a,pageSize:r}=n,[c,d]=ut(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),h=()=>{const t=a();return t?{...t.defaults,flags:t.defaults.flags??["print"],strokeWidth:t.defaults.strokeWidth??2,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1}:null},u=pt({threshold:5,getTool:a,onClickDetected:(n,s)=>{const a=h();if(!a)return;const c=s.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,u=d/2,g=l/2,p=t.clamp(n.x-u,0,r.width-d),f=t.clamp(n.y-g,0,r.height-l),m=a.strokeWidth,y=m/2,A={origin:{x:p-y,y:f-y},size:{width:d+m,height:l+m}},x={...a,type:e.PdfAnnotationSubtype.SQUARE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:A};i(x)}}),g=t=>{const n=c();if(!n)return null;const o=Math.min(n.x,t.x),i=Math.min(n.y,t.y),s=Math.abs(n.x-t.x),a=Math.abs(n.y-t.y),r=h();if(!r)return null;const d=r.strokeWidth,l=d/2,u={origin:{x:o-l,y:i-l},size:{width:s+d,height:a+d}};return{type:e.PdfAnnotationSubtype.SQUARE,bounds:u,data:{rect:u,...r}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),u.onStart(o),s(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);u.onMove(e),c()&&u.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var a;if(!c())return;if(!h())return;const r=l(t);if(u.hasMoved()){const t=h();if(!t)return;const n=g(r);if(n){const s={...t,type:e.PdfAnnotationSubtype.SQUARE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:n.data.rect};i(s)}}else u.onEnd(r);d(null),s(null),u.reset(),null==(a=n.releasePointerCapture)||a.call(n)},onPointerLeave:(t,e)=>{var n;d(null),s(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),s(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},zt={annotationType:e.PdfAnnotationSubtype.STAMP,create(n){const{services:o,onCommit:i,getTool:s,pageSize:a}=n;return{onPointerDown:r=>{const c=s();if(!c)return;const{imageSrc:d,imageSize:l}=c.defaults,h=(o,s,d)=>{const l=r.x-s/2,h=r.y-d/2,u={origin:{x:t.clamp(l,0,a.width-s),y:t.clamp(h,0,a.height-d)},size:{width:s,height:d}},g={...c.defaults,rect:u,type:e.PdfAnnotationSubtype.STAMP,icon:c.defaults.icon??e.PdfAnnotationIcon.Draft,subject:c.defaults.subject??"Stamp",flags:c.defaults.flags??["print"],pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};i(g,{imageData:o})};d?o.processImage({source:d,maxWidth:a.width,maxHeight:a.height,onComplete:t=>h(t.imageData,(null==l?void 0:l.width)??t.width,(null==l?void 0:l.height)??t.height)}):o.requestFile({accept:"image/png,image/jpeg",onFile:t=>{o.processImage({source:t,maxWidth:a.width,maxHeight:a.height,onComplete:t=>h(t.imageData,t.width,t.height)})}})}}}},Dt={annotationType:e.PdfAnnotationSubtype.CIRCLE,create(n){const{pageIndex:o,onCommit:i,onPreview:s,getTool:a,pageSize:r}=n,[c,d]=ut(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),h=()=>{const t=a();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??2,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,flags:t.defaults.flags??["print"]}:null},u=pt({threshold:5,getTool:a,onClickDetected:(n,s)=>{const a=h();if(!a)return;const c=s.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,u=d/2,g=l/2,p=t.clamp(n.x-u,0,r.width-d),f=t.clamp(n.y-g,0,r.height-l),m=a.strokeWidth,y=m/2,A={origin:{x:p-y,y:f-y},size:{width:d+m,height:l+m}},x={...a,type:e.PdfAnnotationSubtype.CIRCLE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:A};i(x)}}),g=t=>{const n=c();if(!n)return null;const o=Math.min(n.x,t.x),i=Math.min(n.y,t.y),s=Math.abs(n.x-t.x),a=Math.abs(n.y-t.y),r=h();if(!r)return null;const d=r.strokeWidth,l=d/2,u={origin:{x:o-l,y:i-l},size:{width:s+d,height:a+d}};return{type:e.PdfAnnotationSubtype.CIRCLE,bounds:u,data:{rect:u,...r}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),u.onStart(o),s(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);u.onMove(e),c()&&u.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var a;if(!c())return;if(!h())return;const r=l(t);if(u.hasMoved()){const t=h();if(!t)return;const n=g(r);if(n){const s={...t,type:e.PdfAnnotationSubtype.CIRCLE,flags:["print"],created:new Date,id:e.uuidV4(),pageIndex:o,rect:n.data.rect};i(s)}}else u.onEnd(r);d(null),s(null),u.reset(),null==(a=n.releasePointerCapture)||a.call(n)},onPointerLeave:(t,e)=>{var n;d(null),s(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),s(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},Et=(t,n)=>{var o;switch(n.type){case"vertex-edit":default:return n.changes;case"move":if(n.changes.rect){const e=n.changes.rect.origin.x-t.rect.origin.x,o=n.changes.rect.origin.y-t.rect.origin.y,i=t.inkList.map(t=>({points:t.points.map(t=>({x:t.x+e,y:t.y+o}))}));return{rect:n.changes.rect,inkList:i}}return n.changes;case"resize":if(n.changes.rect){const e=t.rect,i=n.changes.rect;let s=i.size.width/e.size.width,a=i.size.height/e.size.height;const r=10;if((i.size.width<r||i.size.height<r)&&(s=Math.max(s,r/e.size.width),a=Math.max(a,r/e.size.height),n.changes.rect={origin:i.origin,size:{width:e.size.width*s,height:e.size.height*a}}),null==(o=n.metadata)?void 0:o.maintainAspectRatio){const t=Math.min(s,a);s=t,a=t,n.changes.rect.size={width:e.size.width*t,height:e.size.height*t}}const c=(t,e)=>({origin:{x:t.origin.x+e,y:t.origin.y+e},size:{width:Math.max(1,t.size.width-2*e),height:Math.max(1,t.size.height-2*e)}}),d=Math.min(n.changes.rect.size.width/e.size.width,n.changes.rect.size.height/e.size.height),l=Math.max(1,Math.round(t.strokeWidth*d)),h=c(e,t.strokeWidth/2),u=c(n.changes.rect,l/2),g=u.size.width/Math.max(h.size.width,1e-6),p=u.size.height/Math.max(h.size.height,1e-6),f=t.inkList.map(t=>({points:t.points.map(t=>({x:u.origin.x+(t.x-h.origin.x)*g,y:u.origin.y+(t.y-h.origin.y)*p}))}));return{rect:n.changes.rect,inkList:f,strokeWidth:l}}return n.changes;case"property-update":if(void 0!==n.changes.strokeWidth){const o={...t,...n.changes},i=o.inkList.flatMap(t=>t.points),s=e.expandRect(e.rectFromPoints(i),o.strokeWidth/2);return{...n.changes,rect:s}}return n.changes}},Rt=(t,e)=>{var n;switch(e.type){case"vertex-edit":if(e.changes.linePoints){const{start:n,end:o}=e.changes.linePoints;return{rect:Pt([n,o],t.strokeWidth,t.lineEndings),linePoints:{start:n,end:o}}}return e.changes;case"move":if(e.changes.rect){const n=e.changes.rect.origin.x-t.rect.origin.x,o=e.changes.rect.origin.y-t.rect.origin.y;return{rect:e.changes.rect,linePoints:{start:{x:t.linePoints.start.x+n,y:t.linePoints.start.y+o},end:{x:t.linePoints.end.x+n,y:t.linePoints.end.y+o}}}}return e.changes;case"resize":if(e.changes.rect){const o=t.rect,i=e.changes.rect;let s=i.size.width/o.size.width,a=i.size.height/o.size.height;const r=10;if((i.size.width<r||i.size.height<r)&&(s=Math.max(s,r/o.size.width),a=Math.max(a,r/o.size.height),e.changes.rect={origin:i.origin,size:{width:o.size.width*s,height:o.size.height*a}}),null==(n=e.metadata)?void 0:n.maintainAspectRatio){const t=Math.min(s,a);s=t,a=t,e.changes.rect.size={width:o.size.width*t,height:o.size.height*t}}const c={start:{x:e.changes.rect.origin.x+(t.linePoints.start.x-o.origin.x)*s,y:e.changes.rect.origin.y+(t.linePoints.start.y-o.origin.y)*a},end:{x:e.changes.rect.origin.x+(t.linePoints.end.x-o.origin.x)*s,y:e.changes.rect.origin.y+(t.linePoints.end.y-o.origin.y)*a}};return{rect:e.changes.rect,linePoints:c}}return e.changes;case"property-update":if(e.changes.strokeWidth||e.changes.lineEndings){const n={...t,...e.changes},o=Pt([n.linePoints.start,n.linePoints.end],n.strokeWidth,n.lineEndings);return{...e.changes,rect:o}}return e.changes;default:return e.changes}},wt=(t,e)=>{var n;switch(e.type){case"vertex-edit":return e.changes.vertices&&e.changes.vertices.length?{rect:Pt(e.changes.vertices,t.strokeWidth,t.lineEndings),vertices:e.changes.vertices}:e.changes;case"move":if(e.changes.rect){const n=e.changes.rect.origin.x-t.rect.origin.x,o=e.changes.rect.origin.y-t.rect.origin.y,i=t.vertices.map(t=>({x:t.x+n,y:t.y+o}));return{rect:e.changes.rect,vertices:i}}return e.changes;case"resize":if(e.changes.rect){const o=t.rect,i=e.changes.rect;let s=i.size.width/o.size.width,a=i.size.height/o.size.height;const r=10;if((i.size.width<r||i.size.height<r)&&(s=Math.max(s,r/o.size.width),a=Math.max(a,r/o.size.height),e.changes.rect={origin:i.origin,size:{width:o.size.width*s,height:o.size.height*a}}),null==(n=e.metadata)?void 0:n.maintainAspectRatio){const t=Math.min(s,a);s=t,a=t,e.changes.rect.size={width:o.size.width*t,height:o.size.height*t}}const c=t.vertices.map(t=>({x:e.changes.rect.origin.x+(t.x-o.origin.x)*s,y:e.changes.rect.origin.y+(t.y-o.origin.y)*a}));return{rect:e.changes.rect,vertices:c}}return e.changes;case"property-update":if(void 0!==e.changes.strokeWidth||void 0!==e.changes.lineEndings){const n={...t,...e.changes},o=Pt(n.vertices,n.strokeWidth,n.lineEndings);return{...e.changes,rect:o}}return e.changes;default:return e.changes}},Lt=(t,n)=>{var o;switch(n.type){case"vertex-edit":if(n.changes.vertices&&n.changes.vertices.length){const o=t.strokeWidth/2;return{rect:e.expandRect(e.rectFromPoints(n.changes.vertices),o),vertices:n.changes.vertices}}return n.changes;case"move":if(n.changes.rect){const e=n.changes.rect.origin.x-t.rect.origin.x,o=n.changes.rect.origin.y-t.rect.origin.y,i=t.vertices.map(t=>({x:t.x+e,y:t.y+o}));return{rect:n.changes.rect,vertices:i}}return n.changes;case"resize":if(n.changes.rect){const e=t.rect,i=n.changes.rect;let s=i.size.width/e.size.width,a=i.size.height/e.size.height;const r=10;if((i.size.width<r||i.size.height<r)&&(s=Math.max(s,r/e.size.width),a=Math.max(a,r/e.size.height),n.changes.rect={origin:i.origin,size:{width:e.size.width*s,height:e.size.height*a}}),null==(o=n.metadata)?void 0:o.maintainAspectRatio){const t=Math.min(s,a);s=t,a=t,n.changes.rect.size={width:e.size.width*t,height:e.size.height*t}}const c=t.vertices.map(t=>({x:n.changes.rect.origin.x+(t.x-e.origin.x)*s,y:n.changes.rect.origin.y+(t.y-e.origin.y)*a}));return{rect:n.changes.rect,vertices:c}}return n.changes;case"property-update":if(void 0!==n.changes.strokeWidth){const o={...t,...n.changes},i=o.strokeWidth/2,s=e.expandRect(e.rectFromPoints(o.vertices),i);return{...n.changes,rect:s}}return n.changes;default:return n.changes}},Nt=class extends t.BasePlugin{constructor(e,n,o){var i,s,a;super(e,n),this.ANNOTATION_HISTORY_TOPIC="annotations",this.state$=t.createBehaviorEmitter(),this.pendingContexts=new Map,this.isInitialLoadComplete=new Map,this.importQueue=new Map,this.commitInProgress=new Map,this.handlerFactories=new Map,this.activeTool$=t.createBehaviorEmitter(),this.events$=t.createBehaviorEmitter(),this.toolsChange$=t.createBehaviorEmitter(),this.patchRegistry=new bt,this.unifiedDragStates=new Map,this.unifiedDrag$=t.createBehaviorEmitter(),this.unifiedResizeStates=new Map,this.unifiedResize$=t.createBehaviorEmitter(),this.config=o,this.selection=(null==(i=n.getPlugin("selection"))?void 0:i.provides())??null,this.history=(null==(s=n.getPlugin("history"))?void 0:s.provides())??null,this.interactionManager=(null==(a=n.getPlugin("interaction-manager"))?void 0:a.provides())??null,this.registerHandlerFactories(),this.registerBuiltInPatches()}onDocumentLoadingStarted(t){this.dispatch(function(t,e){return{type:i,payload:{documentId:t,state:e}}}(t,{pages:{},byUid:{},selectedUids:[],selectedUid:null,activeToolId:null,hasPendingChanges:!1})),this.pendingContexts.set(t,new Map),this.isInitialLoadComplete.set(t,!1),this.importQueue.set(t,[]),this.logger.debug("AnnotationPlugin","DocumentOpened",`Initialized annotation state for document: ${t}`)}onDocumentLoaded(t){const e=this.getCoreDocument(t);if((null==e?void 0:e.document)&&this.getAllAnnotations(t,e.document),this.selection)for(const n of this.state.tools)n.interaction.textSelection&&this.selection.enableForMode(n.interaction.mode??n.id,{showRects:!1})}onDocumentClosed(t){this.dispatch(function(t){return{type:s,payload:t}}(t)),this.pendingContexts.delete(t),this.isInitialLoadComplete.delete(t),this.importQueue.delete(t),this.logger.debug("AnnotationPlugin","DocumentClosed",`Cleaned up annotation state for document: ${t}`)}registerHandlerFactories(){this.handlerFactories.set(e.PdfAnnotationSubtype.CIRCLE,Dt),this.handlerFactories.set(e.PdfAnnotationSubtype.SQUARE,Ct),this.handlerFactories.set(e.PdfAnnotationSubtype.STAMP,zt),this.handlerFactories.set(e.PdfAnnotationSubtype.POLYGON,kt),this.handlerFactories.set(e.PdfAnnotationSubtype.POLYLINE,Mt),this.handlerFactories.set(e.PdfAnnotationSubtype.LINE,Tt),this.handlerFactories.set(e.PdfAnnotationSubtype.INK,gt),this.handlerFactories.set(e.PdfAnnotationSubtype.FREETEXT,ft)}registerBuiltInPatches(){this.patchRegistry.register(e.PdfAnnotationSubtype.INK,Et),this.patchRegistry.register(e.PdfAnnotationSubtype.LINE,Rt),this.patchRegistry.register(e.PdfAnnotationSubtype.POLYLINE,wt),this.patchRegistry.register(e.PdfAnnotationSubtype.POLYGON,Lt)}async initialize(){var t,n,o;this.state.tools.forEach(t=>this.registerInteractionForTool(t)),this.history&&this.history.onHistoryChange(t=>{t.topic===this.ANNOTATION_HISTORY_TOPIC&&!1!==this.config.autoCommit&&this.commit(t.documentId)}),null==(t=this.interactionManager)||t.onModeChange(t=>{var e,n;const o=(null==(e=this.state.tools.find(e=>(e.interaction.mode??e.id)===t.activeMode))?void 0:e.id)??null;o!==((null==(n=this.state.documents[t.documentId])?void 0:n.activeToolId)??null)&&t.documentId&&this.dispatch(T(t.documentId,o))}),null==(n=this.selection)||n.onMarqueeEnd(({documentId:t,pageIndex:e,rect:n})=>{const o=this.state.documents[t];if(!o)return;const i=(o.pages[e]??[]).map(t=>o.byUid[t]).filter(t=>void 0!==t).filter(t=>!_(t)).filter(t=>D(n,t.object.rect)).map(t=>t.object.id);if(i.length>0){const e=new Set;for(const n of i)if(this.isInGroupMethod(n,t)){const o=this.getGroupMembersMethod(n,t);for(const t of o)e.add(t.object.id)}else e.add(n);this.setSelectionMethod([...e],t)}}),null==(o=this.selection)||o.onEndSelection(({documentId:t})=>{var n,o,i;if(!this.checkPermission(t,e.PdfPermissionFlag.ModifyAnnotations))return;const s=this.getActiveTool(t);if(!s||!s.interaction.textSelection)return;const a=null==(n=this.selection)?void 0:n.getFormattedSelection(),r=null==(o=this.selection)?void 0:o.getSelectedText();if(a&&r){for(const n of a)r.wait(o=>{const i=e.uuidV4();this.createAnnotation(n.pageIndex,{...s.defaults,rect:n.rect,segmentRects:n.segmentRects,pageIndex:n.pageIndex,created:new Date,id:i,custom:{text:o.join("\n")}},void 0,t),this.getToolBehavior(s,"deactivateToolAfterCreate")&&this.setActiveTool(null,t),this.getToolBehavior(s,"selectAfterCreate")&&this.selectAnnotation(n.pageIndex,i,t)},e.ignore);null==(i=this.selection)||i.clear()}})}registerInteractionForTool(t){var e;null==(e=this.interactionManager)||e.registerMode({id:t.interaction.mode??t.id,scope:"page",exclusive:t.interaction.exclusive,cursor:t.interaction.cursor})}buildCapability(){return{getActiveTool:()=>this.getActiveTool(),setActiveTool:t=>this.setActiveTool(t),getState:()=>this.getDocumentState(),getPageAnnotations:t=>this.getPageAnnotations(t),getSelectedAnnotation:()=>this.getSelectedAnnotation(),getSelectedAnnotations:()=>this.getSelectedAnnotationsMethod(),getSelectedAnnotationIds:()=>this.getSelectedAnnotationIdsMethod(),getAnnotationById:t=>this.getAnnotationById(t),selectAnnotation:(t,e)=>this.selectAnnotation(t,e),toggleSelection:(t,e)=>this.toggleSelectionMethod(t,e),addToSelection:(t,e)=>this.addToSelectionMethod(t,e),removeFromSelection:t=>this.removeFromSelectionMethod(t),setSelection:t=>this.setSelectionMethod(t),deselectAnnotation:()=>this.deselectAnnotation(),importAnnotations:t=>this.importAnnotations(t),createAnnotation:(t,e,n)=>this.createAnnotation(t,e,n),updateAnnotation:(t,e,n)=>this.updateAnnotation(t,e,n),updateAnnotations:t=>this.updateAnnotationsMethod(t),deleteAnnotation:(t,e)=>this.deleteAnnotation(t,e),deleteAnnotations:(t,e)=>this.deleteAnnotationsMethod(t,e),renderAnnotation:t=>this.renderAnnotation(t),commit:()=>this.commit(),getAttachedLinks:(t,e)=>this.getAttachedLinksMethod(t,e),hasAttachedLinks:(t,e)=>this.hasAttachedLinksMethod(t,e),deleteAttachedLinks:(t,e)=>this.deleteAttachedLinksMethod(t,e),groupAnnotations:t=>this.groupAnnotationsMethod(t),ungroupAnnotations:(t,e)=>this.ungroupAnnotationsMethod(t,e),getGroupMembers:(t,e)=>this.getGroupMembersMethod(t,e),isInGroup:(t,e)=>this.isInGroupMethod(t,e),forDocument:t=>this.createAnnotationScope(t),getTools:()=>this.state.tools,getTool:t=>this.getTool(t),addTool:t=>{this.dispatch((t=>({type:I,payload:t}))(t)),this.registerInteractionForTool(t)},findToolForAnnotation:t=>this.findToolForAnnotation(t),setToolDefaults:(t,e)=>this.dispatch(((t,e)=>({type:x,payload:{toolId:t,patch:e}}))(t,e)),getColorPresets:()=>[...this.state.colorPresets],addColorPreset:t=>this.dispatch({type:A,payload:t}),transformAnnotation:(t,e)=>this.transformAnnotation(t,e),registerPatchFunction:(t,e)=>this.registerPatchFunction(t,e),onStateChange:this.state$.on,onActiveToolChange:this.activeTool$.on,onAnnotationEvent:this.events$.on,onToolsChange:this.toolsChange$.on}}createAnnotationScope(t){return{getState:()=>this.getDocumentState(t),getPageAnnotations:e=>this.getPageAnnotations(e,t),getSelectedAnnotation:()=>this.getSelectedAnnotation(t),getSelectedAnnotations:()=>this.getSelectedAnnotationsMethod(t),getSelectedAnnotationIds:()=>this.getSelectedAnnotationIdsMethod(t),getAnnotationById:e=>this.getAnnotationById(e,t),selectAnnotation:(e,n)=>this.selectAnnotation(e,n,t),toggleSelection:(e,n)=>this.toggleSelectionMethod(e,n,t),addToSelection:(e,n)=>this.addToSelectionMethod(e,n,t),removeFromSelection:e=>this.removeFromSelectionMethod(e,t),setSelection:e=>this.setSelectionMethod(e,t),deselectAnnotation:()=>this.deselectAnnotation(t),getActiveTool:()=>this.getActiveTool(t),setActiveTool:e=>this.setActiveTool(e,t),findToolForAnnotation:t=>this.findToolForAnnotation(t),importAnnotations:e=>this.importAnnotations(e,t),createAnnotation:(e,n,o)=>this.createAnnotation(e,n,o,t),updateAnnotation:(e,n,o)=>this.updateAnnotation(e,n,o,t),updateAnnotations:e=>this.updateAnnotationsMethod(e,t),deleteAnnotation:(e,n)=>this.deleteAnnotation(e,n,t),deleteAnnotations:e=>this.deleteAnnotationsMethod(e,t),renderAnnotation:e=>this.renderAnnotation(e,t),commit:()=>this.commit(t),getAttachedLinks:e=>this.getAttachedLinksMethod(e,t),hasAttachedLinks:e=>this.hasAttachedLinksMethod(e,t),deleteAttachedLinks:e=>this.deleteAttachedLinksMethod(e,t),groupAnnotations:()=>this.groupAnnotationsMethod(t),ungroupAnnotations:e=>this.ungroupAnnotationsMethod(e,t),getGroupMembers:e=>this.getGroupMembersMethod(e,t),isInGroup:e=>this.isInGroupMethod(e,t),getGroupingAction:()=>this.getGroupingActionMethod(t),onStateChange:e=>this.state$.on(n=>{n.documentId===t&&e(n.state)}),onAnnotationEvent:e=>this.events$.on(n=>{n.documentId===t&&e(n)}),onActiveToolChange:e=>this.activeTool$.on(n=>{n.documentId===t&&e(n.tool)})}}onStoreUpdated(t,e){for(const n in e.documents){const o=t.documents[n],i=e.documents[n];o!==i&&(this.state$.emit({documentId:n,state:i}),o&&o.activeToolId!==i.activeToolId&&this.activeTool$.emit({documentId:n,tool:this.getActiveTool(n)}))}if(t.tools!==e.tools){for(const t in e.documents)this.activeTool$.emit({documentId:t,tool:this.getActiveTool(t)});this.toolsChange$.emit({tools:e.tools})}}registerPatchFunction(t,e){this.patchRegistry.register(t,e)}transformAnnotation(t,e){const n={type:e.type,changes:e.changes,metadata:e.metadata};return this.patchRegistry.transform(t,n)}registerPageHandlers(t,e,n,o){var i;const s=this.getCoreDocument(t),a=null==(i=null==s?void 0:s.document)?void 0:i.pages[e];if(!a)return()=>{};if(!this.interactionManager)return()=>{};const r=[];for(const c of this.state.tools){if(!c.defaults.type)continue;const i=this.handlerFactories.get(c.defaults.type);if(!i)continue;const s={pageIndex:e,pageSize:a.size,scale:n,services:o.services,onPreview:t=>o.onPreview(c.id,t),onCommit:(n,o)=>{this.createAnnotation(e,n,o,t),this.getToolBehavior(c,"deactivateToolAfterCreate")&&this.setActiveTool(null,t),this.getToolBehavior(c,"selectAfterCreate")&&this.selectAnnotation(e,n.id,t)},getTool:()=>this.state.tools.find(t=>t.id===c.id)},d=this.interactionManager.registerHandlers({documentId:t,modeId:c.interaction.mode??c.id,handlers:i.create(s),pageIndex:e});r.push(d)}return()=>r.forEach(t=>t())}getDocumentState(t){const e=t??this.getActiveDocumentId(),n=this.state.documents[e];if(!n)throw new Error(`Annotation state not found for document: ${e}`);return n}getAllAnnotations(t,n){this.engine.getAllAnnotations(n).wait(e=>{this.dispatch(((t,e)=>({type:a,payload:{documentId:t,annotations:e}}))(t,e)),this.isInitialLoadComplete.set(t,!0);const n=this.importQueue.get(t);n&&n.length>0&&this.processImportQueue(t),this.events$.emit({type:"loaded",documentId:t,total:Object.values(e).reduce((t,e)=>t+e.length,0)})},e.ignore)}getPageAnnotations(t,n){const{pageIndex:o}=t,i=n??this.getActiveDocumentId(),s=this.getCoreDocument(i),a=null==s?void 0:s.document;if(!a)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const r=a.pages.find(t=>t.index===o);return r?this.engine.getPageAnnotations(a,r):e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"})}getSelectedAnnotation(t){return Q(this.getDocumentState(t))}getAnnotationById(t,e){const n=this.getDocumentState(e);return X(n,t)}renderAnnotation({pageIndex:t,annotation:n,options:o},i){const s=i??this.getActiveDocumentId(),a=this.getCoreDocument(s),r=null==a?void 0:a.document;if(!r)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const c=r.pages.find(e=>e.index===t);return c?this.engine.renderPageAnnotation(r,c,n,o):e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"})}importAnnotations(t,e){const n=e??this.getActiveDocumentId();if(!this.isInitialLoadComplete.get(n)){const e=this.importQueue.get(n)||[];return e.push(...t),void this.importQueue.set(n,e)}this.processImportItems(n,t)}processImportQueue(t){const e=this.importQueue.get(t);if(!e||0===e.length)return;const n=[...e];this.importQueue.set(t,[]),this.processImportItems(t,n)}processImportItems(t,e){const n=this.pendingContexts.get(t);if(n){for(const o of e){const{annotation:e,ctx:i}=o,s=e.pageIndex,a=e.id;this.dispatch(M(t,s,e)),i&&n.set(a,i)}!1!==this.config.autoCommit&&this.commit(t)}}createAnnotation(t,n,o,i){const s=i??this.getActiveDocumentId();if(!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","CreateAnnotation",`Cannot create annotation: document ${s} lacks ModifyAnnotations permission`);const a=n.id,r=this.pendingContexts.get(s);if(!r)return;const c={...n,author:n.author??this.config.annotationAuthor},d=()=>{this.dispatch(M(s,t,c)),o&&r.set(a,o),this.events$.emit({type:"create",documentId:s,annotation:c,pageIndex:t,ctx:o,committed:!1})};if(!this.history)return d(),void(this.config.autoCommit&&this.commit(s));const l={execute:d,undo:()=>{r.delete(a),this.dispatch(P(s)),this.dispatch(C(s,t,a)),this.events$.emit({type:"delete",documentId:s,annotation:c,pageIndex:t,committed:!1})}};this.history.forDocument(s).register(l,this.ANNOTATION_HISTORY_TOPIC)}buildPatch(t,e){return"rect"in e?e:this.transformAnnotation(t,{type:"property-update",changes:e})}updateAnnotation(t,n,o,i){const s=i??this.getActiveDocumentId();if(!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","UpdateAnnotation",`Cannot update annotation: document ${s} lacks ModifyAnnotations permission`);const a=this.getDocumentState(s).byUid[n].object,r=this.buildPatch(a,{...o,author:o.author??this.config.annotationAuthor}),c=()=>{this.dispatch(k(s,t,n,r)),this.events$.emit({type:"update",documentId:s,annotation:a,pageIndex:t,patch:r,committed:!1})};if(!this.history)return c(),void(!1!==this.config.autoCommit&&this.commit(s));const d=Object.fromEntries(Object.keys(o).map(t=>[t,a[t]])),l={execute:c,undo:()=>{this.dispatch(k(s,t,n,d)),this.events$.emit({type:"update",documentId:s,annotation:a,pageIndex:t,patch:d,committed:!1})}};this.history.forDocument(s).register(l,this.ANNOTATION_HISTORY_TOPIC)}deleteAnnotation(t,n,o){var i;const s=o??this.getActiveDocumentId();if(!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","DeleteAnnotation",`Cannot delete annotation: document ${s} lacks ModifyAnnotations permission`);const a=this.getDocumentState(s),r=null==(i=a.byUid[n])?void 0:i.object;if(!r)return;const c=J(a,n),d=c.map(t=>{var e;return null==(e=a.byUid[t.id])?void 0:e.object}).filter(t=>void 0!==t),l=()=>{var e;for(const t of c){const n=null==(e=a.byUid[t.id])?void 0:e.object;n&&(this.dispatch(C(s,t.pageIndex,t.id)),this.events$.emit({type:"delete",documentId:s,annotation:n,pageIndex:t.pageIndex,committed:!1}))}this.dispatch(P(s)),this.dispatch(C(s,t,n)),this.events$.emit({type:"delete",documentId:s,annotation:r,pageIndex:t,committed:!1})};if(!this.history)return l(),void(!1!==this.config.autoCommit&&this.commit(s));const h={execute:l,undo:()=>{this.dispatch(M(s,t,r)),this.events$.emit({type:"create",documentId:s,annotation:r,pageIndex:t,committed:!1});for(const t of d)this.dispatch(M(s,t.pageIndex,t)),this.events$.emit({type:"create",documentId:s,annotation:t,pageIndex:t.pageIndex,committed:!1})}};this.history.forDocument(s).register(h,this.ANNOTATION_HISTORY_TOPIC)}deleteAnnotationsMethod(t,e){for(const{pageIndex:n,id:o}of t)this.deleteAnnotation(n,o,e)}selectAnnotation(t,e,n){const o=n??this.getActiveDocumentId();if(this.isInGroupMethod(e,o)){const t=this.getGroupMembersMethod(e,o).map(t=>t.object.id);this.dispatch(S(o,t))}else this.dispatch(((t,e,n)=>({type:r,payload:{documentId:t,pageIndex:e,id:n}}))(o,t,e))}deselectAnnotation(t){const e=t??this.getActiveDocumentId();this.dispatch(P(e))}getSelectedAnnotationsMethod(t){return V(this.getDocumentState(t))}getSelectedAnnotationIdsMethod(t){return q(this.getDocumentState(t))}toggleSelectionMethod(t,e,n){const o=n??this.getActiveDocumentId(),i=this.getDocumentState(o);if(i.selectedUids.includes(e))this.dispatch(v(o,e));else if(this.isInGroupMethod(e,o)){const t=this.getGroupMembersMethod(e,o);for(const e of t)i.selectedUids.includes(e.object.id)||this.dispatch(b(o,e.object.pageIndex,e.object.id))}else this.dispatch(b(o,t,e))}addToSelectionMethod(t,e,n){const o=n??this.getActiveDocumentId();this.dispatch(b(o,t,e))}removeFromSelectionMethod(t,e){const n=e??this.getActiveDocumentId();this.dispatch(v(n,t))}setSelectionMethod(t,e){const n=e??this.getActiveDocumentId();this.dispatch(S(n,t))}getAttachedLinksMethod(t,e){return et(this.getDocumentState(e),t)}hasAttachedLinksMethod(t,e){return this.getAttachedLinksMethod(t,e).length>0}deleteAttachedLinksMethod(t,e){const n=this.getAttachedLinksMethod(t,e);for(const o of n)this.deleteAnnotation(o.object.pageIndex,o.object.id,e)}groupAnnotationsMethod(t){const n=t??this.getActiveDocumentId();if(!this.checkPermission(n,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","GroupAnnotations",`Cannot group annotations: document ${n} lacks ModifyAnnotations permission`);const o=this.getSelectedAnnotationsMethod(n);if(o.length<2)return void this.logger.debug("AnnotationPlugin","GroupAnnotations","Need at least 2 annotations to group");const i=o[0],s=o.slice(1).map(t=>({pageIndex:t.object.pageIndex,id:t.object.id,patch:{inReplyToId:i.object.id,replyType:e.PdfAnnotationReplyType.Group}}));this.updateAnnotationsMethod(s,n)}ungroupAnnotationsMethod(t,n){const o=n??this.getActiveDocumentId();if(!this.checkPermission(o,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","UngroupAnnotations",`Cannot ungroup annotations: document ${o} lacks ModifyAnnotations permission`);const i=this.getGroupMembersMethod(t,o).filter(t=>t.object.inReplyToId&&t.object.replyType===e.PdfAnnotationReplyType.Group).map(t=>({pageIndex:t.object.pageIndex,id:t.object.id,patch:{inReplyToId:void 0,replyType:void 0}}));i.length>0&&this.updateAnnotationsMethod(i,o)}getGroupMembersMethod(t,e){return ot(this.getDocumentState(e),t)}isInGroupMethod(t,e){return it(this.getDocumentState(e),t)}getGroupingActionMethod(t){return st(this.getDocumentState(t))}computeCombinedConstraints(t){let e=1/0,n=1/0,o=1/0,i=1/0;for(const s of t){const t=s.rect.origin.y,a=s.pageSize.height-(s.rect.origin.y+s.rect.size.height),r=s.rect.origin.x,c=s.pageSize.width-(s.rect.origin.x+s.rect.size.width);e=Math.min(e,t),n=Math.min(n,a),o=Math.min(o,r),i=Math.min(i,c)}return isFinite(e)||(e=0),isFinite(n)||(n=0),isFinite(o)||(o=0),isFinite(i)||(i=0),{maxUp:e,maxDown:n,maxLeft:o,maxRight:i}}clampDelta(t,e){return{x:Math.max(-e.maxLeft,Math.min(e.maxRight,t.x)),y:Math.max(-e.maxUp,Math.min(e.maxDown,t.y))}}startDrag(t,e){const{annotationIds:n,pageSize:o}=e,i=[];for(const d of n){const e=this.getAttachedLinksMethod(d,t);for(const t of e)i.includes(t.object.id)||i.push(t.object.id)}const s=[...n,...i],a=new Map,r=[];for(const d of s){const e=this.getAnnotationById(d,t);e&&(a.set(d,{...e.object.rect}),r.push({id:d,rect:e.object.rect,pageIndex:e.object.pageIndex,pageSize:o}))}const c={documentId:t,isDragging:!0,primaryIds:n,attachedLinkIds:i,allParticipantIds:s,originalRects:a,delta:{x:0,y:0},combinedConstraints:this.computeCombinedConstraints(r)};this.unifiedDragStates.set(t,c),this.unifiedDrag$.emit({documentId:t,type:"start",state:c,previewPatches:{}})}computeDragPreviewPatches(t,e){const n={};for(const o of t.allParticipantIds){const i=this.getAnnotationById(o,e);if(!i)continue;const s=t.originalRects.get(o);if(!s)continue;const a={...s,origin:{x:s.origin.x+t.delta.x,y:s.origin.y+t.delta.y}};n[o]=this.transformAnnotation(i.object,{type:"move",changes:{rect:a}})}return n}updateDrag(t,e){const n=this.unifiedDragStates.get(t);if(!(null==n?void 0:n.isDragging))return{x:0,y:0};const o=this.clampDelta(e,n.combinedConstraints),i={...n,delta:o};this.unifiedDragStates.set(t,i);const s=this.computeDragPreviewPatches(i,t);return this.unifiedDrag$.emit({documentId:t,type:"update",state:i,previewPatches:s}),o}commitDrag(t){const e=this.unifiedDragStates.get(t);if(!e)return;const n=e.delta;if(0!==n.x||0!==n.y){const o=[];for(const i of e.allParticipantIds){const s=this.getAnnotationById(i,t);if(!s)continue;const a=e.originalRects.get(i)??s.object.rect,r={...a,origin:{x:a.origin.x+n.x,y:a.origin.y+n.y}},c=this.transformAnnotation(s.object,{type:"move",changes:{rect:r}});o.push({pageIndex:s.object.pageIndex,id:i,patch:c})}o.length>0&&this.updateAnnotationsMethod(o,t)}const o=this.computeDragPreviewPatches(e,t);this.unifiedDrag$.emit({documentId:t,type:"end",state:{...e,isDragging:!1},previewPatches:o}),this.unifiedDragStates.delete(t)}cancelDrag(t){const e=this.unifiedDragStates.get(t);e&&(this.unifiedDrag$.emit({documentId:t,type:"cancel",state:{...e,isDragging:!1,delta:{x:0,y:0}},previewPatches:{}}),this.unifiedDragStates.delete(t))}getDragState(t){return this.unifiedDragStates.get(t)??null}get onDragChange(){return this.unifiedDrag$.on}computeUnifiedGroupBoundingBox(t){if(0===t.length)return{origin:{x:0,y:0},size:{width:0,height:0}};let e=1/0,n=1/0,o=-1/0,i=-1/0;for(const s of t)e=Math.min(e,s.origin.x),n=Math.min(n,s.origin.y),o=Math.max(o,s.origin.x+s.size.width),i=Math.max(i,s.origin.y+s.size.height);return{origin:{x:e,y:n},size:{width:o-e,height:i-n}}}computeUnifiedRelativePositions(t,e){return t.map(t=>({id:t.id,originalRect:t.rect,pageIndex:t.pageIndex,isAttachedLink:t.isAttachedLink,parentId:t.parentId,relativeX:e.size.width>0?(t.rect.origin.x-e.origin.x)/e.size.width:0,relativeY:e.size.height>0?(t.rect.origin.y-e.origin.y)/e.size.height:0,relativeWidth:e.size.width>0?t.rect.size.width/e.size.width:1,relativeHeight:e.size.height>0?t.rect.size.height/e.size.height:1}))}computeUnifiedResizedRects(t,e,n=10){const o=new Map;for(const i of t){const t=Math.max(n,i.relativeWidth*e.size.width),s=Math.max(n,i.relativeHeight*e.size.height);o.set(i.id,{origin:{x:e.origin.x+i.relativeX*e.size.width,y:e.origin.y+i.relativeY*e.size.height},size:{width:t,height:s}})}return o}computeResizePreviewPatches(t,e){const n={};for(const[o,i]of t){const t=this.getAnnotationById(o,e);t&&(n[o]=this.transformAnnotation(t.object,{type:"resize",changes:{rect:i}}))}return n}startResize(t,e){const{annotationIds:n,pageSize:o,resizeHandle:i}=e,s=[],a=[];for(const p of n){const e=this.getAnnotationById(p,t);if(e){a.push({id:p,rect:e.object.rect,pageIndex:e.object.pageIndex,isAttachedLink:!1});const n=this.getAttachedLinksMethod(p,t);for(const t of n)s.includes(t.object.id)||(s.push(t.object.id),a.push({id:t.object.id,rect:t.object.rect,pageIndex:t.object.pageIndex,isAttachedLink:!0,parentId:p}))}}const r=[...n,...s],c=a.map(t=>t.rect),d=this.computeUnifiedGroupBoundingBox(c),l=this.computeUnifiedRelativePositions(a,d),h=this.computeUnifiedResizedRects(l,d),u={documentId:t,isResizing:!0,primaryIds:n,attachedLinkIds:s,allParticipantIds:r,originalGroupBox:d,currentGroupBox:d,participatingAnnotations:l,resizeHandle:i,computedRects:h};this.unifiedResizeStates.set(t,u);const g=this.computeResizePreviewPatches(h,t);this.unifiedResize$.emit({documentId:t,type:"start",state:u,computedRects:Object.fromEntries(h),previewPatches:g})}updateResize(t,e){const n=this.unifiedResizeStates.get(t);if(!(null==n?void 0:n.isResizing))return{};const o=this.computeUnifiedResizedRects(n.participatingAnnotations,e),i={...n,currentGroupBox:e,computedRects:o};this.unifiedResizeStates.set(t,i);const s=Object.fromEntries(o),a=this.computeResizePreviewPatches(o,t);return this.unifiedResize$.emit({documentId:t,type:"update",state:i,computedRects:s,previewPatches:a}),s}commitResize(t){const e=this.unifiedResizeStates.get(t);if(!e)return;const n=this.computeUnifiedResizedRects(e.participatingAnnotations,e.currentGroupBox),o=[];for(const[s,a]of n){const e=this.getAnnotationById(s,t);if(!e)continue;const n=this.transformAnnotation(e.object,{type:"resize",changes:{rect:a}});o.push({pageIndex:e.object.pageIndex,id:s,patch:n})}o.length>0&&this.updateAnnotationsMethod(o,t);const i=this.computeResizePreviewPatches(n,t);this.unifiedResize$.emit({documentId:t,type:"end",state:{...e,isResizing:!1},computedRects:Object.fromEntries(n),previewPatches:i}),this.unifiedResizeStates.delete(t)}cancelResize(t){const e=this.unifiedResizeStates.get(t);if(!e)return;const n=this.computeUnifiedResizedRects(e.participatingAnnotations,e.originalGroupBox);this.unifiedResize$.emit({documentId:t,type:"cancel",state:{...e,isResizing:!1,currentGroupBox:e.originalGroupBox},computedRects:Object.fromEntries(n),previewPatches:{}}),this.unifiedResizeStates.delete(t)}getResizeState(t){return this.unifiedResizeStates.get(t)??null}get onResizeChange(){return this.unifiedResize$.on}updateAnnotationsMethod(t,n){const o=n??this.getActiveDocumentId();if(!this.checkPermission(o,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","UpdateAnnotations",`Cannot update annotations: document ${o} lacks ModifyAnnotations permission`);const i=this.getDocumentState(o),s=t.map(({pageIndex:t,id:e,patch:n})=>{var o;const s=null==(o=i.byUid[e])?void 0:o.object;if(!s)return null;return{pageIndex:t,id:e,patch:this.buildPatch(s,{...n,author:n.author??this.config.annotationAuthor}),originalObject:s}}).filter(t=>null!==t);if(0===s.length)return;const a=()=>{for(const{pageIndex:t,id:e,patch:n,originalObject:i}of s)this.dispatch(k(o,t,e,n)),this.events$.emit({type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})};if(!this.history)return a(),void(!1!==this.config.autoCommit&&this.commit(o));const r=s.map(({pageIndex:t,id:e,patch:n,originalObject:o})=>({pageIndex:t,id:e,originalPatch:Object.fromEntries(Object.keys(n).map(t=>[t,o[t]])),originalObject:o})),c={execute:a,undo:()=>{for(const{pageIndex:t,id:e,originalPatch:n,originalObject:i}of r)this.dispatch(k(o,t,e,n)),this.events$.emit({type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})}};this.history.forDocument(o).register(c,this.ANNOTATION_HISTORY_TOPIC)}getActiveTool(t){const e=this.getDocumentState(t);return e.activeToolId?this.state.tools.find(t=>t.id===e.activeToolId)??null:null}setActiveTool(t,n){var o,i;const s=n??this.getActiveDocumentId();if(null!==t&&!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","SetActiveTool",`Cannot activate tool: document ${s} lacks ModifyAnnotations permission`);const a=this.getDocumentState(s);if(t===a.activeToolId)return;this.dispatch(T(s,t));const r=this.state.tools.find(e=>e.id===t);r?null==(o=this.interactionManager)||o.forDocument(s).activate(r.interaction.mode??r.id):null==(i=this.interactionManager)||i.forDocument(s).activateDefaultMode()}getTool(t){return this.state.tools.find(e=>e.id===t)}findToolForAnnotation(t){let e=null,n=0;for(const o of this.state.tools){const i=o.matchScore(t);i>n&&(n=i,e=o)}return e}collectPendingChanges(t,e){const n=this.getDocumentState(t),o=this.pendingContexts.get(t),i={creations:[],updates:[],deletions:[],committedUids:[],isEmpty:!0};for(const[s,a]of Object.entries(n.byUid)){if("synced"===a.commitState)continue;if(e.pages.find(t=>t.index===a.object.pageIndex))switch(i.committedUids.push(s),i.isEmpty=!1,a.commitState){case"new":i.creations.push({uid:s,ta:a,ctx:null==o?void 0:o.get(a.object.id)});break;case"dirty":i.updates.push({uid:s,ta:a});break;case"deleted":i.deletions.push({uid:s,ta:a})}}return i}executeCommitBatch(t,n,o){const i=new e.Task,s=this.pendingContexts.get(t),a=[];for(const{uid:e,ta:c,ctx:d}of o.creations){const t=n.pages.find(t=>t.index===c.object.pageIndex);if(!t)continue;const o=this.engine.createPageAnnotation(n,t,c.object,d);a.push({type:"create",task:o,ta:c,uid:e,ctx:d})}for(const{uid:e,ta:c}of o.updates){const t=n.pages.find(t=>t.index===c.object.pageIndex);if(!t)continue;const o=this.engine.updatePageAnnotation(n,t,c.object);a.push({type:"update",task:o,ta:c,uid:e})}for(const{uid:c,ta:d}of o.deletions){const o=n.pages.find(t=>t.index===d.object.pageIndex);if(o)if(d.object.id){const t=new e.Task;this.engine.removePageAnnotation(n,o,d.object).wait(()=>t.resolve(!0),t.fail),a.push({type:"delete",task:t,ta:d,uid:c})}else this.dispatch(z(t,c))}const r=a.map(t=>t.task);return e.Task.allSettled(r).wait(()=>{var e,n;this.emitCommitEvents(t,a,s),this.dispatch((e=t,n=o.committedUids,{type:m,payload:{documentId:e,committedUids:n}})),i.resolve(!0)},t=>i.fail(t)),i}emitCommitEvents(t,n,o){for(const i of n)if(i.task.state.stage===e.TaskStage.Resolved)switch(i.type){case"create":this.events$.emit({type:"create",documentId:t,annotation:i.ta.object,pageIndex:i.ta.object.pageIndex,ctx:i.ctx,committed:!0}),null==o||o.delete(i.ta.object.id);break;case"update":this.events$.emit({type:"update",documentId:t,annotation:i.ta.object,pageIndex:i.ta.object.pageIndex,patch:i.ta.object,committed:!0});break;case"delete":this.dispatch(z(t,i.uid)),this.events$.emit({type:"delete",documentId:t,annotation:i.ta.object,pageIndex:i.ta.object.pageIndex,committed:!0})}}acquireCommitLock(t){return!this.commitInProgress.get(t)&&(this.commitInProgress.set(t,!0),!0)}releaseCommitLock(t){this.commitInProgress.set(t,!1)}commit(t){const n=t??this.getActiveDocumentId();if(!this.getDocumentState(n).hasPendingChanges)return e.PdfTaskHelper.resolve(!0);if(!this.acquireCommitLock(n))return e.PdfTaskHelper.resolve(!0);const o=this.getCoreDocument(n),i=null==o?void 0:o.document;if(!i)return this.releaseCommitLock(n),e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const s=this.collectPendingChanges(n,i);if(s.isEmpty)return this.releaseCommitLock(n),e.PdfTaskHelper.resolve(!0);const a=new e.Task;return this.executeCommitBatch(n,i,s).wait(()=>{this.releaseCommitLock(n);this.getDocumentState(n).hasPendingChanges?this.commit(n).wait(t=>a.resolve(t),t=>a.fail(t)):a.resolve(!0)},t=>{this.releaseCommitLock(n),a.fail(t)}),a}getToolBehavior(t,e){var n;return void 0!==(null==(n=t.behavior)?void 0:n[e])?t.behavior[e]:!1!==this.config[e]}};Nt.id="annotation";let Ot=Nt;function jt(t){return e=>(null==e?void 0:e.id)===t}const Ut=jt("highlight"),Ft=jt("squiggly"),$t=jt("underline"),Gt=jt("strikeout"),Bt=jt("ink"),Wt=jt("inkHighlighter"),Ht=jt("square"),_t=jt("circle"),Yt=jt("line"),Kt=jt("polyline"),Qt=jt("polygon"),Vt=jt("freeText"),qt=jt("stamp"),Xt={manifest:o,create:(t,e)=>new Ot(n,t,e),reducer:(t,e)=>{switch(e.type){case i:{const{documentId:n,state:o}=e.payload;return{...t,documents:{...t.documents,[n]:o},activeDocumentId:t.activeDocumentId??n}}case s:{const n=e.payload,{[n]:o,...i}=t.documents;return{...t,documents:i,activeDocumentId:t.activeDocumentId===n?null:t.activeDocumentId}}case"ANNOTATION/SET_ACTIVE_DOCUMENT":return{...t,activeDocumentId:e.payload};case a:{const{documentId:n,annotations:o}=e.payload,i=t.documents[n];if(!i)return t;const s={},a={};for(const[t,e]of Object.entries(o)){const n=Number(t),o=i.pages[n]||[];for(const t of o)delete a[t];const r=e.map(t=>{const e=t.id;return a[e]={commitState:"synced",object:t},e});s[n]=r}return{...t,documents:{...t.documents,[n]:{...i,pages:s,byUid:a}}}}case r:{const{documentId:n,id:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,selectedUids:[o],selectedUid:o}}}:t}case c:{const{documentId:n}=e.payload,o=t.documents[n];return o?{...t,documents:{...t.documents,[n]:{...o,selectedUids:[],selectedUid:null}}}:t}case d:{const{documentId:n,id:o}=e.payload,i=t.documents[n];if(!i)return t;if(i.selectedUids.includes(o))return t;const s=[...i.selectedUids,o];return{...t,documents:{...t.documents,[n]:{...i,selectedUids:s,selectedUid:ct(s)}}}}case l:{const{documentId:n,id:o}=e.payload,i=t.documents[n];if(!i)return t;const s=i.selectedUids.filter(t=>t!==o);return{...t,documents:{...t.documents,[n]:{...i,selectedUids:s,selectedUid:ct(s)}}}}case h:{const{documentId:n,ids:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,selectedUids:o,selectedUid:ct(o)}}}:t}case u:{const{documentId:n,toolId:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,activeToolId:o}}}:t}case g:{const{documentId:n,pageIndex:o,annotation:i}=e.payload,s=t.documents[n];if(!s)return t;const a=i.id;return{...t,documents:{...t.documents,[n]:{...s,pages:{...s.pages,[o]:[...s.pages[o]??[],a]},byUid:{...s.byUid,[a]:{commitState:"new",object:i}},hasPendingChanges:!0}}}}case f:{const{documentId:n,pageIndex:o,id:i}=e.payload,s=t.documents[n];return s&&s.byUid[i]?{...t,documents:{...t.documents,[n]:{...s,pages:{...s.pages,[o]:(s.pages[o]??[]).filter(t=>t!==i)},byUid:{...s.byUid,[i]:{...s.byUid[i],commitState:"deleted"}},hasPendingChanges:!0}}}:t}case p:{const{documentId:n,id:o,patch:i}=e.payload,s=t.documents[n];return s?{...t,documents:{...t.documents,[n]:lt(s,o,i)}}:t}case m:{const{documentId:n,committedUids:o}=e.payload,i=t.documents[n];if(!i)return t;const s=new Set(o),a={};let r=!1;for(const[t,e]of Object.entries(i.byUid))s.has(t)?a[t]={...e,commitState:"dirty"===e.commitState||"new"===e.commitState?"synced":e.commitState}:(a[t]=e,"new"!==e.commitState&&"dirty"!==e.commitState&&"deleted"!==e.commitState||(r=!0));return{...t,documents:{...t.documents,[n]:{...i,byUid:a,hasPendingChanges:r}}}}case y:{const{documentId:n,uid:o}=e.payload,i=t.documents[n];if(!i)return t;const{[o]:s,...a}=i.byUid;return{...t,documents:{...t.documents,[n]:{...i,byUid:a}}}}case I:{const n=new Map(t.tools.map(t=>[t.id,t]));return n.set(e.payload.id,e.payload),{...t,tools:Array.from(n.values())}}case x:{const{toolId:n,patch:o}=e.payload;return{...t,tools:t.tools.map(t=>t.id===n?{...t,defaults:{...t.defaults,...o}}:t)}}case A:return t.colorPresets.includes(e.payload)?t:{...t,colorPresets:[...t.colorPresets,e.payload]};default:return t}},initialState:(t,e)=>ht(e)};exports.ANNOTATION_PLUGIN_ID=n,exports.AnnotationPlugin=Ot,exports.AnnotationPluginPackage=Xt,exports.createToolPredicate=jt,exports.getAnnotationByUid=X,exports.getAnnotations=t=>{const e={};for(const n of Object.keys(t.pages).map(Number))e[n]=K(t,n);return e},exports.getAnnotationsByPageIndex=K,exports.getAttachedLinks=et,exports.getGroupLeaderId=nt,exports.getGroupMembers=ot,exports.getIRTChildIds=J,exports.getIRTChildrenByType=tt,exports.getSelectedAnnotation=Q,exports.getSelectedAnnotationByPageIndex=(t,e)=>{const n=t.pages[e]??[];for(const o of t.selectedUids)if(n.includes(o))return t.byUid[o]??null;return null},exports.getSelectedAnnotationIds=q,exports.getSelectedAnnotations=V,exports.getSelectedAnnotationsByPageIndex=(t,e)=>{const n=new Set(t.pages[e]??[]);return t.selectedUids.filter(t=>n.has(t)).map(e=>t.byUid[e]).filter(t=>void 0!==t)},exports.getSelectionGroupingAction=st,exports.getSidebarAnnotationsWithReplies=t=>{const e=Z(t),n=[],o=Object.keys(e).map(Number).sort((t,e)=>t-e);for(const i of o)n.push(...e[i]);return n},exports.getSidebarAnnotationsWithRepliesGroupedByPage=Z,exports.getToolDefaultsById=function(t,e){const n=t.tools.find(t=>t.id===e);return null==n?void 0:n.defaults},exports.hasAttachedLinks=(t,e)=>et(t,e).length>0,exports.hasIRTChildren=(t,e)=>{for(const n of Object.values(t.pages))for(const o of n){const n=t.byUid[o];if(n&&"inReplyToId"in n.object&&n.object.inReplyToId===e)return!0}return!1},exports.hasMultipleSelected=t=>t.selectedUids.length>1,exports.initialDocumentState=dt,exports.initialState=ht,exports.isAnnotationSelected=(t,e)=>t.selectedUids.includes(e),exports.isCircle=R,exports.isCircleTool=_t,exports.isFreeText=B,exports.isFreeTextTool=Vt,exports.isHighlight=j,exports.isHighlightTool=Ut,exports.isInGroup=it,exports.isInk=E,exports.isInkHighlighterTool=Wt,exports.isInkTool=Bt,exports.isLine=N,exports.isLineTool=Yt,exports.isLink=_,exports.isPolygon=w,exports.isPolygonTool=Qt,exports.isPolyline=O,exports.isPolylineTool=Kt,exports.isSidebarAnnotation=Y,exports.isSquare=L,exports.isSquareTool=Ht,exports.isSquiggly=$,exports.isSquigglyTool=Ft,exports.isStamp=W,exports.isStampTool=qt,exports.isStrikeout=F,exports.isStrikeoutTool=Gt,exports.isText=H,exports.isTextMarkup=G,exports.isUnderline=U,exports.isUnderlineTool=$t,exports.manifest=o,exports.patching=St,exports.rectsIntersect=D;
2
2
  //# sourceMappingURL=index.cjs.map