@embedpdf/plugin-annotation 1.5.0 → 2.0.0-next.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 (106) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.js +1760 -1444
  4. package/dist/index.js.map +1 -1
  5. package/dist/lib/actions.d.ts +58 -19
  6. package/dist/lib/annotation-plugin.d.ts +14 -5
  7. package/dist/lib/index.d.ts +1 -1
  8. package/dist/lib/reducer.d.ts +2 -1
  9. package/dist/lib/selectors.d.ts +10 -8
  10. package/dist/lib/types.d.ts +53 -10
  11. package/dist/preact/index.cjs +1 -1
  12. package/dist/preact/index.cjs.map +1 -1
  13. package/dist/preact/index.js +96 -48
  14. package/dist/preact/index.js.map +1 -1
  15. package/dist/react/index.cjs +1 -1
  16. package/dist/react/index.cjs.map +1 -1
  17. package/dist/react/index.js +96 -48
  18. package/dist/react/index.js.map +1 -1
  19. package/dist/shared/components/annotation-container.d.ts +4 -3
  20. package/dist/shared/components/annotation-layer.d.ts +7 -7
  21. package/dist/shared/components/annotation-paint-layer.d.ts +2 -1
  22. package/dist/shared/components/annotations/stamp.d.ts +2 -1
  23. package/dist/shared/components/annotations.d.ts +3 -2
  24. package/dist/shared/components/render-annotation.d.ts +2 -1
  25. package/dist/shared/components/text-markup.d.ts +2 -1
  26. package/dist/shared/components/types.d.ts +7 -7
  27. package/dist/shared/hooks/use-annotation.d.ts +8 -4
  28. package/dist/shared/index.d.ts +1 -0
  29. package/dist/shared-preact/components/annotation-container.d.ts +4 -3
  30. package/dist/shared-preact/components/annotation-layer.d.ts +7 -7
  31. package/dist/shared-preact/components/annotation-paint-layer.d.ts +2 -1
  32. package/dist/shared-preact/components/annotations/stamp.d.ts +2 -1
  33. package/dist/shared-preact/components/annotations.d.ts +3 -2
  34. package/dist/shared-preact/components/render-annotation.d.ts +2 -1
  35. package/dist/shared-preact/components/text-markup.d.ts +2 -1
  36. package/dist/shared-preact/components/types.d.ts +7 -7
  37. package/dist/shared-preact/hooks/use-annotation.d.ts +8 -4
  38. package/dist/shared-preact/index.d.ts +1 -0
  39. package/dist/shared-react/components/annotation-container.d.ts +4 -3
  40. package/dist/shared-react/components/annotation-layer.d.ts +7 -7
  41. package/dist/shared-react/components/annotation-paint-layer.d.ts +2 -1
  42. package/dist/shared-react/components/annotations/stamp.d.ts +2 -1
  43. package/dist/shared-react/components/annotations.d.ts +3 -2
  44. package/dist/shared-react/components/render-annotation.d.ts +2 -1
  45. package/dist/shared-react/components/text-markup.d.ts +2 -1
  46. package/dist/shared-react/components/types.d.ts +7 -7
  47. package/dist/shared-react/hooks/use-annotation.d.ts +8 -4
  48. package/dist/shared-react/index.d.ts +1 -0
  49. package/dist/svelte/components/AnnotationContainer.svelte.d.ts +26 -0
  50. package/dist/svelte/components/AnnotationLayer.svelte.d.ts +27 -0
  51. package/dist/svelte/components/AnnotationPaintLayer.svelte.d.ts +8 -0
  52. package/dist/svelte/components/Annotations.svelte.d.ts +22 -0
  53. package/dist/svelte/components/PreviewRenderer.svelte.d.ts +8 -0
  54. package/dist/svelte/components/RenderAnnotation.svelte.d.ts +13 -0
  55. package/dist/svelte/components/TextMarkup.svelte.d.ts +8 -0
  56. package/dist/svelte/components/annotations/Circle.svelte.d.ts +26 -0
  57. package/dist/svelte/components/annotations/FreeText.svelte.d.ts +15 -0
  58. package/dist/svelte/components/annotations/Ink.svelte.d.ts +14 -0
  59. package/dist/svelte/components/annotations/Line.svelte.d.ts +18 -0
  60. package/dist/svelte/components/annotations/Polygon.svelte.d.ts +19 -0
  61. package/dist/svelte/components/annotations/Polyline.svelte.d.ts +17 -0
  62. package/dist/svelte/components/annotations/Square.svelte.d.ts +16 -0
  63. package/dist/svelte/components/annotations/Stamp.svelte.d.ts +13 -0
  64. package/dist/svelte/components/annotations/index.d.ts +8 -0
  65. package/dist/svelte/components/index.d.ts +9 -0
  66. package/dist/svelte/components/text-markup/Highlight.svelte.d.ts +13 -0
  67. package/dist/svelte/components/text-markup/Squiggly.svelte.d.ts +13 -0
  68. package/dist/svelte/components/text-markup/Strikeout.svelte.d.ts +13 -0
  69. package/dist/svelte/components/text-markup/Underline.svelte.d.ts +13 -0
  70. package/dist/svelte/components/text-markup/index.d.ts +4 -0
  71. package/dist/svelte/components/types.d.ts +32 -0
  72. package/dist/svelte/hooks/index.d.ts +1 -0
  73. package/dist/svelte/hooks/use-annotation.svelte.d.ts +21 -0
  74. package/dist/svelte/index.cjs +2 -0
  75. package/dist/svelte/index.cjs.map +1 -0
  76. package/dist/svelte/index.d.ts +4 -0
  77. package/dist/svelte/index.js +3083 -0
  78. package/dist/svelte/index.js.map +1 -0
  79. package/dist/svelte/types.d.ts +54 -0
  80. package/dist/vue/components/annotation-container.vue.d.ts +10 -5
  81. package/dist/vue/components/annotation-layer.vue.d.ts +13 -7
  82. package/dist/vue/components/annotation-paint-layer.vue.d.ts +3 -1
  83. package/dist/vue/components/annotations/circle.vue.d.ts +2 -1
  84. package/dist/vue/components/annotations/free-text.vue.d.ts +2 -1
  85. package/dist/vue/components/annotations/ink.vue.d.ts +2 -1
  86. package/dist/vue/components/annotations/line.vue.d.ts +2 -1
  87. package/dist/vue/components/annotations/polygon.vue.d.ts +2 -1
  88. package/dist/vue/components/annotations/polyline.vue.d.ts +2 -1
  89. package/dist/vue/components/annotations/square.vue.d.ts +2 -1
  90. package/dist/vue/components/annotations/stamp.vue.d.ts +3 -1
  91. package/dist/vue/components/annotations.vue.d.ts +7 -3
  92. package/dist/vue/components/preview-renderer.vue.d.ts +2 -1
  93. package/dist/vue/components/render-annotation.vue.d.ts +3 -1
  94. package/dist/vue/components/text-markup/highlight.vue.d.ts +2 -1
  95. package/dist/vue/components/text-markup/squiggly.vue.d.ts +2 -1
  96. package/dist/vue/components/text-markup/strikeout.vue.d.ts +2 -1
  97. package/dist/vue/components/text-markup/underline.vue.d.ts +2 -1
  98. package/dist/vue/components/text-markup.vue.d.ts +3 -1
  99. package/dist/vue/hooks/use-annotation.d.ts +21 -2682
  100. package/dist/vue/index.cjs +1 -1
  101. package/dist/vue/index.cjs.map +1 -1
  102. package/dist/vue/index.d.ts +1 -0
  103. package/dist/vue/index.js +354 -250
  104. package/dist/vue/index.js.map +1 -1
  105. package/dist/vue/types.d.ts +23 -0
  106. package/package.json +19 -11
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",i={id:n,name:"Annotation Plugin",version:"1.0.0",provides:["annotation"],requires:["interaction-manager","selection"],optional:["history"],defaultConfig:{enabled:!0,autoCommit:!0,annotationAuthor:"Guest",deactivateToolAfterCreate:!1,selectAfterCreate:!0}},o="ANNOTATION/SET_ANNOTATIONS",s="ANNOTATION/SELECT_ANNOTATION",r="ANNOTATION/DESELECT_ANNOTATION",a="ANNOTATION/ADD_COLOR_PRESET",c="ANNOTATION/CREATE_ANNOTATION",l="ANNOTATION/PATCH_ANNOTATION",d="ANNOTATION/DELETE_ANNOTATION",h="ANNOTATION/COMMIT",u="ANNOTATION/PURGE_ANNOTATION",g="ANNOTATION/SET_ACTIVE_TOOL_ID",p="ANNOTATION/SET_TOOL_DEFAULTS",f="ANNOTATION/ADD_TOOL",y=()=>({type:r}),m=(t,e)=>({type:c,payload:{pageIndex:t,annotation:e}}),P=(t,e,n)=>({type:l,payload:{pageIndex:t,id:e,patch:n}}),x=(t,e)=>({type:d,payload:{pageIndex:t,id:e}}),A=t=>({type:u,payload:{uid:t}});function S(t){return t.object.type===e.PdfAnnotationSubtype.INK}function b(t){return t.object.type===e.PdfAnnotationSubtype.CIRCLE}function v(t){return t.object.type===e.PdfAnnotationSubtype.POLYGON}function T(t){return t.object.type===e.PdfAnnotationSubtype.SQUARE}function I(t){return t.object.type===e.PdfAnnotationSubtype.LINE}function E(t){return t.object.type===e.PdfAnnotationSubtype.POLYLINE}function k(t){return t.object.type===e.PdfAnnotationSubtype.HIGHLIGHT}function C(t){return t.object.type===e.PdfAnnotationSubtype.UNDERLINE}function N(t){return t.object.type===e.PdfAnnotationSubtype.STRIKEOUT}function w(t){return t.object.type===e.PdfAnnotationSubtype.SQUIGGLY}function L(t){return k(t)||C(t)||N(t)||w(t)}function M(t){return t.object.type===e.PdfAnnotationSubtype.FREETEXT}function O(t){return t.object.type===e.PdfAnnotationSubtype.STAMP}function z(t){return t.object.type===e.PdfAnnotationSubtype.TEXT}function R(t){return L(t)||S(t)||T(t)||b(t)||v(t)||I(t)||E(t)||M(t)||O(t)}const D=(t,e)=>(t.pages[e]??[]).map((e=>t.byUid[e])),F=t=>t.selectedUid?t.byUid[t.selectedUid]:null;const U=t=>{const e={};for(const i of Object.values(t.pages))for(const n of i){const i=t.byUid[n];if(i&&z(i)){const t=i.object.inReplyToId;t&&(e[t]||(e[t]=[])).push(i)}}const n={};for(const[i,o]of Object.entries(t.pages)){const s=Number(i),r=[];for(const n of o){const i=t.byUid[n];i&&R(i)&&r.push({page:s,annotation:i,replies:e[i.object.id]??[]})}r.length>0&&(n[s]=r)}return n};function j(t){let e=t;return[()=>e,t=>{e=t}]}const W={annotationType:e.PdfAnnotationSubtype.INK,create(n){const{onCommit:i,onPreview:o,getTool:s,pageSize:r}=n,[a,c]=j([]),[l,d]=j(!1),h={current: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,strokeWidth:t.defaults.strokeWidth??1,color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,flags:t.defaults.flags??["print"]}:null},p=()=>{const t=a();if(0===t.length||0===t[0].points.length)return null;const n=g();if(!n)return null;const i=t.flatMap((t=>t.points)),o=e.expandRect(e.rectFromPoints(i),n.strokeWidth/2);return{type:e.PdfAnnotationSubtype.INK,bounds:o,data:{...n,rect:o,inkList:t}}};return{onPointerDown:(t,e)=>{var n;const i=u(t);d(!0),h.current&&clearTimeout(h.current);const s=[...a(),{points:[i]}];c(s),o(p()),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{if(!l())return;const e=a();if(0===e.length)return;const n=u(t);e[e.length-1].points.push(n),c(e),o(p())},onPointerUp:(t,s)=>{var r;d(!1),null==(r=s.releasePointerCapture)||r.call(s),h.current&&clearTimeout(h.current),h.current=setTimeout((()=>{const t=a();if(t.length>0&&t[0].points.length>1){const o=g();if(!o)return;const s=t.flatMap((t=>t.points)),r=e.expandRect(e.rectFromPoints(s),o.strokeWidth/2);i({...o,inkList:t,rect:r,type:e.PdfAnnotationSubtype.INK,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date})}c([]),o(null)}),800)},onPointerCancel:(t,e)=>{var n;c([]),d(!1),o(null),h.current&&clearTimeout(h.current),null==(n=e.releasePointerCapture)||n.call(e)}}}};function $({threshold:t=5,getTool:e,onClickDetected:n}){const[i,o]=j(null),[s,r]=j(!1);return{onStart:t=>{o(t),r(!1)},onMove:e=>{const n=i();if(!n||s())return;Math.sqrt(Math.pow(e.x-n.x,2)+Math.pow(e.y-n.y,2))>t&&r(!0)},onEnd:t=>{var a;if(i()&&!s()){const i=e();i&&"clickBehavior"in i&&(null==(a=i.clickBehavior)?void 0:a.enabled)&&n(t,i)}o(null),r(!1)},hasMoved:s,reset:()=>{o(null),r(!1)}}}const H={annotationType:e.PdfAnnotationSubtype.FREETEXT,create(n){const{onCommit:i,onPreview:o,getTool:s,pageSize:r,pageIndex:a}=n,[c,l]=j(null),d=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.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,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},u=$({threshold:5,getTool:s,onClickDetected:(n,o)=>{const s=h();if(!s)return;const c=o.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:l,height:d}=c.defaultSize,u=l/2,g=d/2,p={origin:{x:t.clamp(n.x-u,0,r.width-l),y:t.clamp(n.y-g,0,r.height-d)},size:{width:l,height:d}},f=c.defaultContent??s.contents,y={...s,contents:f,type:e.PdfAnnotationSubtype.FREETEXT,rect:p,pageIndex:a,id:e.uuidV4(),created:new Date};i(y)}}),g=t=>{const n=c();if(!n)return null;const i=h();if(!i)return null;const o={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:o,data:{...i,rect:o}}};return{onPointerDown:(t,e)=>{var n;const i=d(t);l(i),u.onStart(i),o(g(i)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=d(t);u.onMove(e),c()&&u.hasMoved()&&o(g(e))},onPointerUp:(t,s)=>{var r;const a=c();if(!a)return;const g=h();if(!g)return;const p=d(t);if(u.hasMoved()){const t={origin:{x:Math.min(a.x,p.x),y:Math.min(a.y,p.y)},size:{width:Math.abs(a.x-p.x),height:Math.abs(a.y-p.y)}},o={...g,type:e.PdfAnnotationSubtype.FREETEXT,rect:t,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};i(o)}else u.onEnd(p);l(null),o(null),u.reset(),null==(r=s.releasePointerCapture)||r.call(s)},onPointerLeave:(t,e)=>{var n;l(null),o(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;l(null),o(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}};function B(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:i,y:o}=e(n);return t?`M 0 0 L ${i} ${o} L ${i} ${-o} Z`:`M ${i} ${o} L 0 0 L ${i} ${-o}`},getLocalPoints:t=>{const{x:n,y:i}=e(t);return[{x:0,y:0},{x:n,y:i},{x:n,y:-i}]},getRotation:t=>t,filled:t}}function _(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 G=B(!1),Y=B(!0),Q={[e.PdfAnnotationLineEnding.OpenArrow]:G,[e.PdfAnnotationLineEnding.ClosedArrow]:Y,[e.PdfAnnotationLineEnding.ROpenArrow]:{...G,getRotation:t=>t+Math.PI},[e.PdfAnnotationLineEnding.RClosedArrow]:{...Y,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]:_(6,(t=>t+Math.PI/2)),[e.PdfAnnotationLineEnding.Slash]:_(18,(t=>t+Math.PI/1.5))};function V(t,n,i){if(!t||0===t.length)return{origin:{x:0,y:0},size:{width:0,height:0}};const o=[...t],s=(t,e)=>Math.atan2(e.y-t.y,e.x-t.x),r=(t,i,s)=>{if(!t)return;const r=Q[t];if(!r)return;const a=r.getLocalPoints(n),c=r.getRotation(s),l=a.map((t=>e.rotateAndTranslatePoint(t,c,i)));o.push(...l)};if(t.length>=2){const e=s(t[1],t[0]);r(null==i?void 0:i.start,t[0],e);const n=t.length-1,o=s(t[n-1],t[n]);r(null==i?void 0:i.end,t[n],o)}if(o.length<=1){const e=t[0]||{x:0,y:0},i=n;return{origin:{x:e.x-i,y:e.y-i},size:{width:2*i,height:2*i}}}const a=e.rectFromPoints(o),c=n/2+1.2*n;return e.expandRect(a,c)}class q{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 K=new q,X=Object.freeze(Object.defineProperty({__proto__:null,LINE_ENDING_HANDLERS:Q,PatchRegistry:q,createEnding:function(t,e,n,i,o){if(!t)return null;const s=Q[t];if(!s)return null;const r=s.getRotation(n);return{d:s.getSvgPath(e),transform:`translate(${i} ${o}) rotate(${a=r,180*a/Math.PI})`,filled:s.filled};var a},lineRectWithEndings:V,patchRegistry:K},Symbol.toStringTag,{value:"Module"})),Z={annotationType:e.PdfAnnotationSubtype.LINE,create(n){const{pageIndex:i,onCommit:o,onPreview:s,getTool:r,pageSize:a}=n,[c,l]=j(null),d=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),h=()=>{const t=r();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=$({threshold:5,getTool:r,onClickDetected:(t,n)=>{const s=h();if(!s)return;const r=n.clickBehavior;if(!(null==r?void 0:r.enabled))return;const a=r.defaultAngle??0,c=r.defaultLength/2,l=t.x-c*Math.cos(a),u=t.y-c*Math.sin(a),g=t.x+c*Math.cos(a),p=t.y+c*Math.sin(a),f=d({x:l,y:u}),y=d({x:g,y:p}),m=V([f,y],s.strokeWidth,s.lineEndings);o({...s,rect:m,linePoints:{start:f,end:y},pageIndex:i,id:e.uuidV4(),created:new Date,type:e.PdfAnnotationSubtype.LINE})}}),g=t=>{const n=c();if(!n)return null;const i=h();if(!i)return null;const o=V([n,t],i.strokeWidth,i.lineEndings);return{type:e.PdfAnnotationSubtype.LINE,bounds:o,data:{...i,rect:o,linePoints:{start:n,end:t}}}};return{onPointerDown:(t,e)=>{var n;const i=d(t);l(i),u.onStart(i),s(g(i)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=d(t);u.onMove(e),c()&&u.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var r;const a=c();if(!a)return;const g=d(t);if(u.hasMoved()){const t=h();if(!t)return;if(Math.abs(g.x-a.x)>2||Math.abs(g.y-a.y)>2){const n=V([a,g],t.strokeWidth,t.lineEndings);o({...t,rect:n,linePoints:{start:a,end:g},pageIndex:i,id:e.uuidV4(),flags:["print"],created:new Date,type:e.PdfAnnotationSubtype.LINE})}}else u.onEnd(g);l(null),s(null),u.reset(),null==(r=n.releasePointerCapture)||r.call(n)},onPointerLeave:(t,e)=>{var n;l(null),s(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;l(null),s(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},J={annotationType:e.PdfAnnotationSubtype.POLYLINE,create(n){const{onCommit:i,onPreview:o,getTool:s,pageSize:r}=n,[a,c]=j([]),[l,d]=j(null),h=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.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=a(),n=l();if(0===t.length||!n)return null;const i=u();if(!i)return null;const o=[...t,n],s=V(o,i.strokeWidth,i.lineEndings);return{type:e.PdfAnnotationSubtype.POLYLINE,bounds:s,data:{...i,rect:s,vertices:o,currentVertex:n}}};return{onClick:t=>{const e=h(t),n=a(),i=n[n.length-1];i&&Math.abs(i.x-e.x)<1&&Math.abs(i.y-e.y)<1||(c([...n,e]),d(e),o(g()))},onDoubleClick:()=>{(()=>{const t=a();if(t.length<2)return;const s=u();if(!s)return;const r=V(t,s.strokeWidth,s.lineEndings),l={...s,vertices:t,rect:r,type:e.PdfAnnotationSubtype.POLYLINE,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};i(l),c([]),d(null),o(null)})()},onPointerMove:t=>{if(a().length>0){const e=h(t);d(e),o(g())}},onPointerCancel:()=>{c([]),d(null),o(null)}}}},tt={annotationType:e.PdfAnnotationSubtype.POLYGON,create(n){const{onCommit:i,onPreview:o,getTool:s,scale:r,pageSize:a}=n,[c,l]=j([]),[d,h]=j(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,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 r=e.expandRect(e.rectFromPoints(t),s.strokeWidth/2),a={...s,vertices:t,rect:r,type:e.PdfAnnotationSubtype.POLYGON,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};i(a),l([]),h(null),o(null)},f=()=>{const t=c(),n=d();if(0===t.length||!n)return null;const i=g();if(!i)return null;const o=[...t,n],s=e.expandRect(e.rectFromPoints(o),i.strokeWidth/2);return{type:e.PdfAnnotationSubtype.POLYGON,bounds:s,data:{...i,rect:s,vertices:t,currentVertex:n}}};return{onClick:t=>{const e=u(t);if((t=>{const e=c();if(e.length<2)return!1;const n=14/r/2,i=e[0];return t.x>=i.x-n&&t.x<=i.x+n&&t.y>=i.y-n&&t.y<=i.y+n})(e)&&c().length>=3)return void p();const n=c(),i=n[n.length-1];i&&Math.abs(i.x-e.x)<1&&Math.abs(i.y-e.y)<1||(l([...n,e]),h(e),o(f()))},onDoubleClick:t=>{p()},onPointerMove:t=>{if(c().length>0){const e=u(t);h(e),o(f())}},onPointerCancel:t=>{l([]),h(null),o(null)}}}},et={annotationType:e.PdfAnnotationSubtype.SQUARE,create(n){const{pageIndex:i,onCommit:o,onPreview:s,getTool:r,pageSize:a}=n,[c,l]=j(null),d=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),h=()=>{const t=r();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=$({threshold:5,getTool:r,onClickDetected:(n,s)=>{const r=h();if(!r)return;const c=s.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:l,height:d}=c.defaultSize,u=l/2,g=d/2,p=t.clamp(n.x-u,0,a.width-l),f=t.clamp(n.y-g,0,a.height-d),y=r.strokeWidth,m=y/2,P={origin:{x:p-m,y:f-m},size:{width:l+y,height:d+y}},x={...r,type:e.PdfAnnotationSubtype.SQUARE,created:new Date,id:e.uuidV4(),pageIndex:i,rect:P};o(x)}}),g=t=>{const n=c();if(!n)return null;const i=Math.min(n.x,t.x),o=Math.min(n.y,t.y),s=Math.abs(n.x-t.x),r=Math.abs(n.y-t.y),a=h();if(!a)return null;const l=a.strokeWidth,d=l/2,u={origin:{x:i-d,y:o-d},size:{width:s+l,height:r+l}};return{type:e.PdfAnnotationSubtype.SQUARE,bounds:u,data:{rect:u,...a}}};return{onPointerDown:(t,e)=>{var n;const i=d(t);l(i),u.onStart(i),s(g(i)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=d(t);u.onMove(e),c()&&u.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var r;if(!c())return;if(!h())return;const a=d(t);if(u.hasMoved()){const t=h();if(!t)return;const n=g(a);if(n){const s={...t,type:e.PdfAnnotationSubtype.SQUARE,created:new Date,id:e.uuidV4(),pageIndex:i,rect:n.data.rect};o(s)}}else u.onEnd(a);l(null),s(null),u.reset(),null==(r=n.releasePointerCapture)||r.call(n)},onPointerLeave:(t,e)=>{var n;l(null),s(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;l(null),s(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},nt={annotationType:e.PdfAnnotationSubtype.STAMP,create(n){const{services:i,onCommit:o,getTool:s,pageSize:r}=n;return{onPointerDown:a=>{const c=s();if(!c)return;const{imageSrc:l,imageSize:d}=c.defaults,h=(i,s,l)=>{const d=a.x-s/2,h=a.y-l/2,u={origin:{x:t.clamp(d,0,r.width-s),y:t.clamp(h,0,r.height-l)},size:{width:s,height:l}},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};o(g,{imageData:i})};l?i.processImage({source:l,maxWidth:r.width,maxHeight:r.height,onComplete:t=>h(t.imageData,(null==d?void 0:d.width)??t.width,(null==d?void 0:d.height)??t.height)}):i.requestFile({accept:"image/png,image/jpeg",onFile:t=>{i.processImage({source:t,maxWidth:r.width,maxHeight:r.height,onComplete:t=>h(t.imageData,t.width,t.height)})}})}}}},it={annotationType:e.PdfAnnotationSubtype.CIRCLE,create(n){const{pageIndex:i,onCommit:o,onPreview:s,getTool:r,pageSize:a}=n,[c,l]=j(null),d=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),h=()=>{const t=r();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=$({threshold:5,getTool:r,onClickDetected:(n,s)=>{const r=h();if(!r)return;const c=s.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:l,height:d}=c.defaultSize,u=l/2,g=d/2,p=t.clamp(n.x-u,0,a.width-l),f=t.clamp(n.y-g,0,a.height-d),y=r.strokeWidth,m=y/2,P={origin:{x:p-m,y:f-m},size:{width:l+y,height:d+y}},x={...r,type:e.PdfAnnotationSubtype.CIRCLE,created:new Date,id:e.uuidV4(),pageIndex:i,rect:P};o(x)}}),g=t=>{const n=c();if(!n)return null;const i=Math.min(n.x,t.x),o=Math.min(n.y,t.y),s=Math.abs(n.x-t.x),r=Math.abs(n.y-t.y),a=h();if(!a)return null;const l=a.strokeWidth,d=l/2,u={origin:{x:i-d,y:o-d},size:{width:s+l,height:r+l}};return{type:e.PdfAnnotationSubtype.CIRCLE,bounds:u,data:{rect:u,...a}}};return{onPointerDown:(t,e)=>{var n;const i=d(t);l(i),u.onStart(i),s(g(i)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=d(t);u.onMove(e),c()&&u.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var r;if(!c())return;if(!h())return;const a=d(t);if(u.hasMoved()){const t=h();if(!t)return;const n=g(a);if(n){const s={...t,type:e.PdfAnnotationSubtype.CIRCLE,flags:["print"],created:new Date,id:e.uuidV4(),pageIndex:i,rect:n.data.rect};o(s)}}else u.onEnd(a);l(null),s(null),u.reset(),null==(r=n.releasePointerCapture)||r.call(n)},onPointerLeave:(t,e)=>{var n;l(null),s(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;l(null),s(null),u.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},ot=(t,n)=>{var i;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,i=n.changes.rect.origin.y-t.rect.origin.y,o=t.inkList.map((t=>({points:t.points.map((t=>({x:t.x+e,y:t.y+i})))})));return{rect:n.changes.rect,inkList:o}}return n.changes;case"resize":if(n.changes.rect){const e=t.rect,o=n.changes.rect;let s=o.size.width/e.size.width,r=o.size.height/e.size.height;const a=10;if((o.size.width<a||o.size.height<a)&&(s=Math.max(s,a/e.size.width),r=Math.max(r,a/e.size.height),n.changes.rect={origin:o.origin,size:{width:e.size.width*s,height:e.size.height*r}}),null==(i=n.metadata)?void 0:i.maintainAspectRatio){const t=Math.min(s,r);s=t,r=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)}}),l=Math.min(n.changes.rect.size.width/e.size.width,n.changes.rect.size.height/e.size.height),d=Math.max(1,Math.round(t.strokeWidth*l)),h=c(e,t.strokeWidth/2),u=c(n.changes.rect,d/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:d}}return n.changes;case"property-update":if(void 0!==n.changes.strokeWidth){const i={...t,...n.changes},o=i.inkList.flatMap((t=>t.points)),s=e.expandRect(e.rectFromPoints(o),i.strokeWidth/2);return{...n.changes,rect:s}}return n.changes}},st=(t,e)=>{var n;switch(e.type){case"vertex-edit":if(e.changes.linePoints){const{start:n,end:i}=e.changes.linePoints;return{rect:V([n,i],t.strokeWidth,t.lineEndings),linePoints:{start:n,end:i}}}return e.changes;case"move":if(e.changes.rect){const n=e.changes.rect.origin.x-t.rect.origin.x,i=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+i},end:{x:t.linePoints.end.x+n,y:t.linePoints.end.y+i}}}}return e.changes;case"resize":if(e.changes.rect){const i=t.rect,o=e.changes.rect;let s=o.size.width/i.size.width,r=o.size.height/i.size.height;const a=10;if((o.size.width<a||o.size.height<a)&&(s=Math.max(s,a/i.size.width),r=Math.max(r,a/i.size.height),e.changes.rect={origin:o.origin,size:{width:i.size.width*s,height:i.size.height*r}}),null==(n=e.metadata)?void 0:n.maintainAspectRatio){const t=Math.min(s,r);s=t,r=t,e.changes.rect.size={width:i.size.width*t,height:i.size.height*t}}const c={start:{x:e.changes.rect.origin.x+(t.linePoints.start.x-i.origin.x)*s,y:e.changes.rect.origin.y+(t.linePoints.start.y-i.origin.y)*r},end:{x:e.changes.rect.origin.x+(t.linePoints.end.x-i.origin.x)*s,y:e.changes.rect.origin.y+(t.linePoints.end.y-i.origin.y)*r}};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},i=V([n.linePoints.start,n.linePoints.end],n.strokeWidth,n.lineEndings);return{...e.changes,rect:i}}return e.changes;default:return e.changes}},rt=(t,e)=>{var n;switch(e.type){case"vertex-edit":return e.changes.vertices&&e.changes.vertices.length?{rect:V(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,i=e.changes.rect.origin.y-t.rect.origin.y,o=t.vertices.map((t=>({x:t.x+n,y:t.y+i})));return{rect:e.changes.rect,vertices:o}}return e.changes;case"resize":if(e.changes.rect){const i=t.rect,o=e.changes.rect;let s=o.size.width/i.size.width,r=o.size.height/i.size.height;const a=10;if((o.size.width<a||o.size.height<a)&&(s=Math.max(s,a/i.size.width),r=Math.max(r,a/i.size.height),e.changes.rect={origin:o.origin,size:{width:i.size.width*s,height:i.size.height*r}}),null==(n=e.metadata)?void 0:n.maintainAspectRatio){const t=Math.min(s,r);s=t,r=t,e.changes.rect.size={width:i.size.width*t,height:i.size.height*t}}const c=t.vertices.map((t=>({x:e.changes.rect.origin.x+(t.x-i.origin.x)*s,y:e.changes.rect.origin.y+(t.y-i.origin.y)*r})));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},i=V(n.vertices,n.strokeWidth,n.lineEndings);return{...e.changes,rect:i}}return e.changes;default:return e.changes}},at=(t,n)=>{var i;switch(n.type){case"vertex-edit":if(n.changes.vertices&&n.changes.vertices.length){const i=t.strokeWidth/2;return{rect:e.expandRect(e.rectFromPoints(n.changes.vertices),i),vertices:n.changes.vertices}}return n.changes;case"move":if(n.changes.rect){const e=n.changes.rect.origin.x-t.rect.origin.x,i=n.changes.rect.origin.y-t.rect.origin.y,o=t.vertices.map((t=>({x:t.x+e,y:t.y+i})));return{rect:n.changes.rect,vertices:o}}return n.changes;case"resize":if(n.changes.rect){const e=t.rect,o=n.changes.rect;let s=o.size.width/e.size.width,r=o.size.height/e.size.height;const a=10;if((o.size.width<a||o.size.height<a)&&(s=Math.max(s,a/e.size.width),r=Math.max(r,a/e.size.height),n.changes.rect={origin:o.origin,size:{width:e.size.width*s,height:e.size.height*r}}),null==(i=n.metadata)?void 0:i.maintainAspectRatio){const t=Math.min(s,r);s=t,r=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)*r})));return{rect:n.changes.rect,vertices:c}}return n.changes;case"property-update":if(void 0!==n.changes.strokeWidth){const i={...t,...n.changes},o=i.strokeWidth/2,s=e.expandRect(e.rectFromPoints(i.vertices),o);return{...n.changes,rect:s}}return n.changes;default:return n.changes}},ct=class extends t.BasePlugin{constructor(e,n,i){var o,s,r;super(e,n),this.ANNOTATION_HISTORY_TOPIC="annotations",this.state$=t.createBehaviorEmitter(),this.pendingContexts=new Map,this.handlerFactories=new Map,this.activeTool$=t.createBehaviorEmitter(null),this.events$=t.createBehaviorEmitter(),this.patchRegistry=new q,this.isInitialLoadComplete=!1,this.importQueue=[],this.config=i,this.selection=(null==(o=n.getPlugin("selection"))?void 0:o.provides())??null,this.history=(null==(s=n.getPlugin("history"))?void 0:s.provides())??null,this.interactionManager=(null==(r=n.getPlugin("interaction-manager"))?void 0:r.provides())??null,this.coreStore.onAction(t.SET_DOCUMENT,((t,e)=>{const n=e.core.document;this.isInitialLoadComplete=!1,n&&this.getAllAnnotations(n)})),this.registerHandlerFactories(),this.registerBuiltInPatches()}registerHandlerFactories(){this.handlerFactories.set(e.PdfAnnotationSubtype.CIRCLE,it),this.handlerFactories.set(e.PdfAnnotationSubtype.SQUARE,et),this.handlerFactories.set(e.PdfAnnotationSubtype.STAMP,nt),this.handlerFactories.set(e.PdfAnnotationSubtype.POLYGON,tt),this.handlerFactories.set(e.PdfAnnotationSubtype.POLYLINE,J),this.handlerFactories.set(e.PdfAnnotationSubtype.LINE,Z),this.handlerFactories.set(e.PdfAnnotationSubtype.INK,W),this.handlerFactories.set(e.PdfAnnotationSubtype.FREETEXT,H)}registerBuiltInPatches(){this.patchRegistry.register(e.PdfAnnotationSubtype.INK,ot),this.patchRegistry.register(e.PdfAnnotationSubtype.LINE,st),this.patchRegistry.register(e.PdfAnnotationSubtype.POLYLINE,rt),this.patchRegistry.register(e.PdfAnnotationSubtype.POLYGON,at)}async initialize(){var t,n,i;this.state.tools.forEach((t=>this.registerInteractionForTool(t))),null==(t=this.history)||t.onHistoryChange((t=>{t===this.ANNOTATION_HISTORY_TOPIC&&!1!==this.config.autoCommit&&this.commit()})),null==(n=this.interactionManager)||n.onModeChange((t=>{var e;const n=(null==(e=this.state.tools.find((e=>(e.interaction.mode??e.id)===t.activeMode)))?void 0:e.id)??null;n!==this.state.activeToolId&&this.dispatch({type:g,payload:n})})),null==(i=this.selection)||i.onEndSelection((()=>{var t,n,i;const o=this.getActiveTool();if(!o||!o.interaction.textSelection)return;const s=null==(t=this.selection)?void 0:t.getFormattedSelection(),r=null==(n=this.selection)?void 0:n.getSelectedText();if(s&&r){for(const t of s)r.wait((n=>{const i=e.uuidV4();this.createAnnotation(t.pageIndex,{...o.defaults,rect:t.rect,segmentRects:t.segmentRects,pageIndex:t.pageIndex,created:new Date,id:i,custom:{text:n.join("\n")}}),this.getToolBehavior(o,"deactivateToolAfterCreate")&&this.setActiveTool(null),this.getToolBehavior(o,"selectAfterCreate")&&this.selectAnnotation(t.pageIndex,i)}),e.ignore);null==(i=this.selection)||i.clear()}}))}registerInteractionForTool(t){var e,n;null==(e=this.interactionManager)||e.registerMode({id:t.interaction.mode??t.id,scope:"page",exclusive:t.interaction.exclusive,cursor:t.interaction.cursor}),t.interaction.textSelection&&(null==(n=this.selection)||n.enableForMode(t.interaction.mode??t.id))}buildCapability(){return{getPageAnnotations:t=>this.getPageAnnotations(t),getSelectedAnnotation:()=>F(this.state),selectAnnotation:(t,e)=>this.selectAnnotation(t,e),deselectAnnotation:()=>this.dispatch(y()),getActiveTool:()=>this.getActiveTool(),setActiveTool:t=>this.setActiveTool(t),getTools:()=>this.state.tools,getTool:t=>this.getTool(t),addTool:t=>{this.dispatch((t=>({type:f,payload:t}))(t)),this.registerInteractionForTool(t)},transformAnnotation:(t,e)=>this.transformAnnotation(t,e),registerPatchFunction:(t,e)=>this.registerPatchFunction(t,e),findToolForAnnotation:t=>this.findToolForAnnotation(t),setToolDefaults:(t,e)=>this.dispatch(((t,e)=>({type:p,payload:{toolId:t,patch:e}}))(t,e)),getColorPresets:()=>[...this.state.colorPresets],addColorPreset:t=>this.dispatch({type:a,payload:t}),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),onStateChange:this.state$.on,onActiveToolChange:this.activeTool$.on,onAnnotationEvent:this.events$.on,commit:()=>this.commit()}}onStoreUpdated(t,e){this.state$.emit(e),t.activeToolId===e.activeToolId&&t.tools===e.tools||this.activeTool$.emit(this.getActiveTool())}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){var i;const o=null==(i=this.coreState.core.document)?void 0:i.pages[t];if(!o)return()=>{};if(!this.interactionManager)return()=>{};const s=[];for(const r of this.state.tools){if(!r.defaults.type)continue;const i=this.handlerFactories.get(r.defaults.type);if(!i)continue;const a={pageIndex:t,pageSize:o.size,scale:e,services:n.services,onPreview:t=>n.onPreview(r.id,t),onCommit:(e,n)=>{this.createAnnotation(t,e,n),this.getToolBehavior(r,"deactivateToolAfterCreate")&&this.setActiveTool(null),this.getToolBehavior(r,"selectAfterCreate")&&this.selectAnnotation(t,e.id)},getTool:()=>this.state.tools.find((t=>t.id===r.id))},c=this.interactionManager.registerHandlers({modeId:r.interaction.mode??r.id,handlers:i.create(a),pageIndex:t});s.push(c)}return()=>s.forEach((t=>t()))}getAllAnnotations(t){this.engine.getAllAnnotations(t).wait((t=>{this.dispatch({type:o,payload:t}),this.isInitialLoadComplete=!0,this.importQueue.length>0&&this.processImportQueue(),this.events$.emit({type:"loaded",total:Object.values(t).reduce(((t,e)=>t+e.length),0)})}),e.ignore)}getPageAnnotations(t){const{pageIndex:n}=t,i=this.coreState.core.document;if(!i)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const o=i.pages.find((t=>t.index===n));return o?this.engine.getPageAnnotations(i,o):e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"})}renderAnnotation({pageIndex:t,annotation:n,options:i}){const o=this.coreState.core;if(!o.document)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const s=o.document.pages.find((e=>e.index===t));return s?this.engine.renderPageAnnotation(o.document,s,n,i):e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"})}importAnnotations(t){this.isInitialLoadComplete?this.processImportItems(t):this.importQueue.push(...t)}processImportQueue(){if(0===this.importQueue.length)return;const t=[...this.importQueue];this.importQueue=[],this.processImportItems(t)}processImportItems(t){for(const e of t){const{annotation:t,ctx:n}=e,i=t.pageIndex,o=t.id;this.dispatch(m(i,t)),n&&this.pendingContexts.set(o,n)}!1!==this.config.autoCommit&&this.commit()}createAnnotation(t,e,n){const i=e.id,o={...e,author:e.author??this.config.annotationAuthor},s=()=>{this.dispatch(m(t,o)),n&&this.pendingContexts.set(i,n),this.events$.emit({type:"create",annotation:o,pageIndex:t,ctx:n,committed:!1})};if(!this.history)return s(),void(this.config.autoCommit&&this.commit());const r={execute:s,undo:()=>{this.pendingContexts.delete(i),this.dispatch(y()),this.dispatch(x(t,i)),this.events$.emit({type:"delete",annotation:o,pageIndex:t,committed:!1})}};this.history.register(r,this.ANNOTATION_HISTORY_TOPIC)}buildPatch(t,e){return"rect"in e?e:this.transformAnnotation(t,{type:"property-update",changes:e})}updateAnnotation(t,e,n){const i=this.state.byUid[e].object,o=this.buildPatch(i,{...n,author:n.author??this.config.annotationAuthor}),s=()=>{this.dispatch(P(t,e,o)),this.events$.emit({type:"update",annotation:i,pageIndex:t,patch:o,committed:!1})};if(!this.history)return s(),void(!1!==this.config.autoCommit&&this.commit());const r=Object.fromEntries(Object.keys(n).map((t=>[t,i[t]]))),a={execute:s,undo:()=>{this.dispatch(P(t,e,r)),this.events$.emit({type:"update",annotation:i,pageIndex:t,patch:r,committed:!1})}};this.history.register(a,this.ANNOTATION_HISTORY_TOPIC)}deleteAnnotation(t,e){var n;const i=null==(n=this.state.byUid[e])?void 0:n.object;if(!i)return;const o=()=>{this.dispatch(y()),this.dispatch(x(t,e)),this.events$.emit({type:"delete",annotation:i,pageIndex:t,committed:!1})};if(!this.history)return o(),void(!1!==this.config.autoCommit&&this.commit());const s={execute:o,undo:()=>{this.dispatch(m(t,i)),this.events$.emit({type:"create",annotation:i,pageIndex:t,committed:!1})}};this.history.register(s,this.ANNOTATION_HISTORY_TOPIC)}selectAnnotation(t,e){this.dispatch(((t,e)=>({type:s,payload:{pageIndex:t,id:e}}))(t,e))}getActiveTool(){return this.state.activeToolId?this.state.tools.find((t=>t.id===this.state.activeToolId))??null:null}setActiveTool(t){var e,n;if(t===this.state.activeToolId)return;const i=this.state.tools.find((e=>e.id===t));i?null==(e=this.interactionManager)||e.activate(i.interaction.mode??i.id):null==(n=this.interactionManager)||n.activateDefaultMode()}getTool(t){return this.state.tools.find((e=>e.id===t))}findToolForAnnotation(t){let e=null,n=0;for(const i of this.state.tools){const o=i.matchScore(t);o>n&&(n=o,e=i)}return e}commit(){const t=new e.Task;if(!this.state.hasPendingChanges)return e.PdfTaskHelper.resolve(!0);const n=this.coreState.core.document;if(!n)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const i=[],o=[],s=[];for(const[c,l]of Object.entries(this.state.byUid)){if("synced"===l.commitState)continue;const t=n.pages.find((t=>t.index===l.object.pageIndex));if(t)switch(l.commitState){case"new":const r=this.pendingContexts.get(l.object.id),a=this.engine.createPageAnnotation(n,t,l.object,r);a.wait((()=>{this.events$.emit({type:"create",annotation:l.object,pageIndex:l.object.pageIndex,ctx:r,committed:!0}),this.pendingContexts.delete(l.object.id)}),e.ignore),i.push(a);break;case"dirty":const d=this.engine.updatePageAnnotation(n,t,l.object);d.wait((()=>{this.events$.emit({type:"update",annotation:l.object,pageIndex:l.object.pageIndex,patch:l.object,committed:!0})}),e.ignore),o.push(d);break;case"deleted":s.push({ta:l,uid:c})}}const r=[];for(const{ta:c,uid:l}of s){const t=n.pages.find((t=>t.index===c.object.pageIndex));if("deleted"===c.commitState&&c.object.id){const i=new e.Task;this.engine.removePageAnnotation(n,t,c.object).wait((()=>{this.dispatch(A(l)),this.events$.emit({type:"delete",annotation:c.object,pageIndex:c.object.pageIndex,committed:!0}),i.resolve(!0)}),i.fail),r.push(i)}else this.dispatch(A(l))}const a=[...i,...o,...r];return e.Task.allSettled(a).wait((()=>{this.dispatch({type:h}),t.resolve(!0)}),t.fail),t}getToolBehavior(t,e){var n;return void 0!==(null==(n=t.behavior)?void 0:n[e])?t.behavior[e]:!1!==this.config[e]}};ct.id="annotation";let lt=ct;const dt=[{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}}],ht=["#E44234","#FF8D00","#FFCD45","#5CC96E","#25D2D1","#597CE2","#C544CE","#7D2E25","#000000","#FFFFFF"],ut=t=>{const e=new Map;return dt.forEach((t=>e.set(t.id,t))),(t.tools||[]).forEach((t=>e.set(t.id,t))),{pages:{},byUid:{},selectedUid:null,activeToolId:null,tools:Array.from(e.values()),colorPresets:t.colorPresets??ht,hasPendingChanges:!1}};function gt(t){return e=>(null==e?void 0:e.id)===t}const pt=gt("highlight"),ft=gt("squiggly"),yt=gt("underline"),mt=gt("strikeout"),Pt=gt("ink"),xt=gt("inkHighlighter"),At=gt("square"),St=gt("circle"),bt=gt("line"),vt=gt("polyline"),Tt=gt("polygon"),It=gt("freeText"),Et=gt("stamp"),kt={manifest:i,create:(t,e)=>new lt(n,t,e),reducer:(t,e)=>{switch(e.type){case o:{const n={...t.pages},i={...t.byUid};for(const[o,s]of Object.entries(e.payload)){const e=Number(o),r=t.pages[e]||[];for(const t of r)delete i[t];const a=s.map((t=>{const e=t.id;return i[e]={commitState:"synced",object:t},e}));n[e]=a}return{...t,pages:n,byUid:i}}case g:return{...t,activeToolId:e.payload};case f:{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 p:{const{toolId:n,patch:i}=e.payload;return{...t,tools:t.tools.map((t=>t.id===n?{...t,defaults:{...t.defaults,...i}}:t))}}case s:return{...t,selectedUid:e.payload.id};case r:return{...t,selectedUid:null};case a:return t.colorPresets.includes(e.payload)?t:{...t,colorPresets:[...t.colorPresets,e.payload]};case c:{const{pageIndex:n,annotation:i}=e.payload,o=i.id;return{...t,pages:{...t.pages,[n]:[...t.pages[n]??[],o]},byUid:{...t.byUid,[o]:{commitState:"new",object:i}},hasPendingChanges:!0}}case d:{const{pageIndex:n,id:i}=e.payload;return t.byUid[i]?{...t,pages:{...t.pages,[n]:(t.pages[n]??[]).filter((t=>t!==i))},byUid:{...t.byUid,[i]:{...t.byUid[i],commitState:"deleted"}},hasPendingChanges:!0}:t}case l:return((t,e,n)=>{const i=t.byUid[e];return i?{...t,byUid:{...t.byUid,[e]:{...i,commitState:"synced"===i.commitState?"dirty":i.commitState,object:{...i.object,...n}}},hasPendingChanges:!0}:t})(t,e.payload.id,e.payload.patch);case h:{const e={};for(const[n,i]of Object.entries(t.byUid))e[n]={...i,commitState:"dirty"===i.commitState||"new"===i.commitState?"synced":i.commitState};return{...t,byUid:e,hasPendingChanges:!1}}case u:{const{uid:n}=e.payload,{[n]:i,...o}=t.byUid;return{...t,byUid:o}}default:return t}},initialState:(t,e)=>ut(e)};exports.ANNOTATION_PLUGIN_ID=n,exports.AnnotationPlugin=lt,exports.AnnotationPluginPackage=kt,exports.createToolPredicate=gt,exports.getAnnotations=t=>{const e={};for(const n of Object.keys(t.pages).map(Number))e[n]=D(t,n);return e},exports.getAnnotationsByPageIndex=D,exports.getSelectedAnnotation=F,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=U(t),n=[],i=Object.keys(e).map(Number).sort(((t,e)=>t-e));for(const o of i)n.push(...e[o]);return n},exports.getSidebarAnnotationsWithRepliesGroupedByPage=U,exports.getToolDefaultsById=function(t,e){const n=t.tools.find((t=>t.id===e));return null==n?void 0:n.defaults},exports.initialState=ut,exports.isAnnotationSelected=(t,e)=>t.selectedUid===e,exports.isCircle=b,exports.isCircleTool=St,exports.isFreeText=M,exports.isFreeTextTool=It,exports.isHighlight=k,exports.isHighlightTool=pt,exports.isInk=S,exports.isInkHighlighterTool=xt,exports.isInkTool=Pt,exports.isLine=I,exports.isLineTool=bt,exports.isPolygon=v,exports.isPolygonTool=Tt,exports.isPolyline=E,exports.isPolylineTool=vt,exports.isSidebarAnnotation=R,exports.isSquare=T,exports.isSquareTool=At,exports.isSquiggly=w,exports.isSquigglyTool=ft,exports.isStamp=O,exports.isStampTool=Et,exports.isStrikeout=N,exports.isStrikeoutTool=mt,exports.isText=z,exports.isTextMarkup=L,exports.isUnderline=C,exports.isUnderlineTool=yt,exports.manifest=i,exports.patching=X;
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:{enabled:!0,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 L(t){return t.object.type===e.PdfAnnotationSubtype.STRIKEOUT}function O(t){return t.object.type===e.PdfAnnotationSubtype.SQUIGGLY}function M(t){return w(t)||D(t)||L(t)||O(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 M(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;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,e,n,o){const i=e.id,s=o??this.getActiveDocumentId(),a=this.pendingContexts.get(s);if(!a)return;const r={...e,author:e.author??this.config.annotationAuthor},c=()=>{this.dispatch(x(s,t,r)),n&&a.set(i,n),this.events$.emit({type:"create",documentId:s,annotation:r,pageIndex:t,ctx:n,committed:!1})};if(!this.history)return c(),void(this.config.autoCommit&&this.commit(s));const d={execute:c,undo:()=>{a.delete(i),this.dispatch(A(s)),this.dispatch(I(s,t,i)),this.events$.emit({type:"delete",documentId:s,annotation:r,pageIndex:t,committed:!1})}};this.history.forDocument(s).register(d,this.ANNOTATION_HISTORY_TOPIC)}buildPatch(t,e){return"rect"in e?e:this.transformAnnotation(t,{type:"property-update",changes:e})}updateAnnotation(t,e,n,o){const i=o??this.getActiveDocumentId(),s=this.getDocumentState(i).byUid[e].object,a=this.buildPatch(s,{...n,author:n.author??this.config.annotationAuthor}),r=()=>{this.dispatch(v(i,t,e,a)),this.events$.emit({type:"update",documentId:i,annotation:s,pageIndex:t,patch:a,committed:!1})};if(!this.history)return r(),void(!1!==this.config.autoCommit&&this.commit(i));const c=Object.fromEntries(Object.keys(n).map(t=>[t,s[t]])),d={execute:r,undo:()=>{this.dispatch(v(i,t,e,c)),this.events$.emit({type:"update",documentId:i,annotation:s,pageIndex:t,patch:c,committed:!1})}};this.history.forDocument(i).register(d,this.ANNOTATION_HISTORY_TOPIC)}deleteAnnotation(t,e,n){var o;const i=n??this.getActiveDocumentId(),s=null==(o=this.getDocumentState(i).byUid[e])?void 0:o.object;if(!s)return;const a=()=>{this.dispatch(A(i)),this.dispatch(I(i,t,e)),this.events$.emit({type:"delete",documentId:i,annotation:s,pageIndex:t,committed:!1})};if(!this.history)return a(),void(!1!==this.config.autoCommit&&this.commit(i));const r={execute:a,undo:()=>{this.dispatch(x(i,t,s)),this.events$.emit({type:"create",documentId:i,annotation:s,pageIndex:t,committed:!1})}};this.history.forDocument(i).register(r,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,e){var n,o;const i=e??this.getActiveDocumentId(),s=this.getDocumentState(i);if(t===s.activeToolId)return;this.dispatch(P(i,t));const a=this.state.tools.find(e=>e.id===t);a?null==(n=this.interactionManager)||n.forDocument(i).activate(a.interaction.mode??a.id):null==(o=this.interactionManager)||o.forDocument(i).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"),Lt=Pt("stamp"),Ot={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=Ot,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=O,exports.isSquigglyTool=vt,exports.isStamp=R,exports.isStampTool=Lt,exports.isStrikeout=L,exports.isStrikeoutTool=St,exports.isText=F,exports.isTextMarkup=M,exports.isUnderline=D,exports.isUnderlineTool=It,exports.manifest=o,exports.patching=at;
2
2
  //# sourceMappingURL=index.cjs.map