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