@embedpdf/plugin-redaction 2.5.0 → 2.6.1
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 +88 -133
- package/dist/index.js.map +1 -1
- package/dist/lib/handlers/index.d.ts +0 -1
- package/dist/lib/redaction-plugin.d.ts +8 -2
- package/dist/lib/tools.d.ts +4 -1
- package/dist/lib/types.d.ts +0 -10
- package/dist/preact/index.cjs +1 -1
- package/dist/preact/index.cjs.map +1 -1
- package/dist/preact/index.js +9 -17
- package/dist/preact/index.js.map +1 -1
- package/dist/react/index.cjs +1 -1
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.js +9 -17
- package/dist/react/index.js.map +1 -1
- package/dist/svelte/index.cjs +1 -1
- package/dist/svelte/index.cjs.map +1 -1
- package/dist/svelte/index.js +11 -12
- package/dist/svelte/index.js.map +1 -1
- package/dist/vue/index.cjs +1 -1
- package/dist/vue/index.cjs.map +1 -1
- package/dist/vue/index.js +9 -16
- package/dist/vue/index.js.map +1 -1
- package/package.json +13 -13
- package/dist/lib/handlers/marquee-redact.handler.d.ts +0 -9
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("@embedpdf/core"),t=require("@embedpdf/models");var n=(e=>(e.Redact="redact",e.MarqueeRedact="marqueeRedact",e.RedactSelection="redactSelection",e))(n||{});const o="REDACTION/INIT_STATE",i="REDACTION/CLEANUP_STATE",a="START_REDACTION",c="END_REDACTION",s="ADD_PENDING",d="REMOVE_PENDING",r="UPDATE_PENDING",l="CLEAR_PENDING",u="SELECT_PENDING",g="DESELECT_PENDING";const m=(e,t)=>({type:s,payload:{documentId:e,items:t}}),h=(e,t,n)=>({type:d,payload:{documentId:e,page:t,id:n}}),p=e=>({type:l,payload:e}),f=(e,t,n,o)=>({type:r,payload:{documentId:e,page:t,id:n,patch:o}}),y=(e,t,n)=>({type:u,payload:{documentId:e,page:t,id:n}}),v=e=>({type:g,payload:e});const P=e=>Object.values(e).reduce((e,t)=>e+t.length,0),R={isRedacting:!1,activeType:null,pending:{},pendingCount:0,selected:null},C={documents:{},activeDocumentId:null},A={id:"redact",name:"Redact",matchScore:e=>e.type===t.PdfAnnotationSubtype.REDACT?10:0,interaction:{mode:n.Redact,exclusive:!1,cursor:"crosshair",textSelection:!0,isDraggable:e=>{var n;return e.type!==t.PdfAnnotationSubtype.REDACT||!(null==(n=e.segmentRects)?void 0:n.length)},isResizable:e=>{var n;return e.type!==t.PdfAnnotationSubtype.REDACT||!(null==(n=e.segmentRects)?void 0:n.length)},lockAspectRatio:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:t.PdfAnnotationSubtype.REDACT,color:"#E44234",overlayColor:"#000000",strokeColor:"#E44234",opacity:1}},b=[A],I=class extends e.BasePlugin{constructor(t,n,o){var i,a,c,s;if(super(t,n),this.redactionSelection$=new Map,this.pending$=e.createBehaviorEmitter(),this.selected$=e.createBehaviorEmitter(),this.state$=e.createBehaviorEmitter(),this.events$=e.createBehaviorEmitter(),this.documentUnsubscribers=new Map,this.config=o,this.selectionCapability=null==(i=this.registry.getPlugin("selection"))?void 0:i.provides(),this.interactionManagerCapability=null==(a=this.registry.getPlugin("interaction-manager"))?void 0:a.provides(),this.annotationCapability=null==(c=this.registry.getPlugin("annotation"))?void 0:c.provides(),this.historyCapability=null==(s=this.registry.getPlugin("history"))?void 0:s.provides(),this.config.useAnnotationMode?this.annotationCapability?this.useAnnotationMode=!0:(this.logger.warn("RedactionPlugin","ConfigError","useAnnotationMode is enabled but annotation plugin is not available. Falling back to legacy mode."),this.useAnnotationMode=!1):this.useAnnotationMode=!1,this.useAnnotationMode)for(const e of b)this.annotationCapability.addTool(e);this.setupRedactionModes(),!this.useAnnotationMode&&this.annotationCapability&&this.logger.info("RedactionPlugin","LegacyMode","Using legacy redaction mode. Set useAnnotationMode: true in config to use annotation-based redactions."),this.setupModeChangeListener()}setupRedactionModes(){this.interactionManagerCapability&&(this.interactionManagerCapability.registerMode({id:n.Redact,scope:"page",exclusive:!1,cursor:"crosshair"}),this.interactionManagerCapability.registerMode({id:n.MarqueeRedact,scope:"page",exclusive:!1,cursor:"crosshair"}),this.interactionManagerCapability.registerMode({id:n.RedactSelection,scope:"page",exclusive:!1}))}setupModeChangeListener(){var e;null==(e=this.interactionManagerCapability)||e.onModeChange(e=>{const t=e.documentId;if(e.activeMode===n.Redact||e.activeMode===n.MarqueeRedact||e.activeMode===n.RedactSelection)this.dispatch(((e,t)=>({type:a,payload:{documentId:e,mode:t}}))(t,e.activeMode));else{const e=this.getDocumentState(t);(null==e?void 0:e.isRedacting)&&this.dispatch((e=>({type:c,payload:e}))(t))}})}onDocumentLoadingStarted(n){this.dispatch(function(e,t){return{type:o,payload:{documentId:e,state:t}}}(n,{...R})),this.redactionSelection$.set(n,e.createBehaviorEmitter());const i=[];if(this.selectionCapability){const e=this.selectionCapability.forDocument(n),o=e.onSelectionChange(()=>{const t=this.getDocumentState(n);if(!(null==t?void 0:t.isRedacting))return;const o=e.getFormattedSelection(),i=this.redactionSelection$.get(n);null==i||i.emit(o)}),a=e.onEndSelection(()=>{const o=this.getDocumentState(n);if(!(null==o?void 0:o.isRedacting))return;if(!this.checkPermission(n,t.PdfPermissionFlag.ModifyContents))return;const i=e.getFormattedSelection();if(!i.length)return;const a=e.getSelectedText(),c=this.redactionSelection$.get(n);null==c||c.emit([]),e.clear(),a.wait(e=>{const t=e.join(" ");this.createRedactionsFromSelection(n,i,t)},()=>{this.createRedactionsFromSelection(n,i)})});i.push(o,a)}if(this.useAnnotationMode&&this.annotationCapability){const e=this.annotationCapability.forDocument(n),o=e.onAnnotationEvent(e=>{var o;if("loaded"===e.type)return void this.syncFromAnnotationLoad(n);if((null==(o=e.annotation)?void 0:o.type)!==t.PdfAnnotationSubtype.REDACT)return;const i=e.annotation;"create"===e.type?(this.syncFromAnnotationCreate(n,i),this.events$.emit({type:"add",documentId:n,items:[this.annotationToRedactionItem(i)]})):"update"===e.type?(this.logger.debug("RedactionPlugin","AnnotationUpdated",{documentId:n,redactAnno:i,patch:e.patch}),this.syncFromAnnotationUpdate(n,i,e.patch)):"delete"===e.type&&(this.syncFromAnnotationDelete(n,i),this.events$.emit({type:"remove",documentId:n,page:i.pageIndex,id:i.id}))}),a=e.onStateChange(()=>{this.syncSelectionFromAnnotation(n)});i.push(o,a)}this.documentUnsubscribers.set(n,i),this.logger.debug("RedactionPlugin","DocumentOpened",`Initialized redaction state for document: ${n}`)}onDocumentLoaded(e){var t,o;null==(t=this.selectionCapability)||t.enableForMode(n.Redact,{showRects:!1},e),null==(o=this.selectionCapability)||o.enableForMode(n.RedactSelection,{showRects:!1},e)}onDocumentClosed(e){this.dispatch(function(e){return{type:i,payload:e}}(e));const t=this.redactionSelection$.get(e);null==t||t.clear(),this.redactionSelection$.delete(e);const n=this.documentUnsubscribers.get(e);n&&(n.forEach(e=>e()),this.documentUnsubscribers.delete(e)),this.logger.debug("RedactionPlugin","DocumentClosed",`Cleaned up redaction state for document: ${e}`)}async initialize(e){this.logger.info("RedactionPlugin","Initialize","Redaction plugin initialized")}buildCapability(){return{queueCurrentSelectionAsPending:()=>this.queueCurrentSelectionAsPending(),enableRedact:()=>this.enableRedact(),toggleRedact:()=>this.toggleRedact(),isRedactActive:()=>this.isRedactActive(),endRedact:()=>this.endRedact(),enableMarqueeRedact:()=>this.enableMarqueeRedact(),toggleMarqueeRedact:()=>this.toggleMarqueeRedact(),isMarqueeRedactActive:()=>this.isMarqueeRedactActive(),enableRedactSelection:()=>this.enableRedactSelection(),toggleRedactSelection:()=>this.toggleRedactSelection(),isRedactSelectionActive:()=>this.isRedactSelectionActive(),addPending:e=>this.addPendingItems(e),removePending:(e,t)=>this.removePendingItem(e,t),clearPending:()=>this.clearPendingItems(),commitAllPending:()=>this.commitAllPending(),commitPending:(e,t)=>this.commitPendingOne(e,t),selectPending:(e,t)=>this.selectPending(e,t),getSelectedPending:()=>this.getSelectedPending(),deselectPending:()=>this.deselectPending(),getState:()=>this.getDocumentStateOrThrow(),forDocument:e=>this.createRedactionScope(e),onPendingChange:this.pending$.on,onSelectedChange:this.selected$.on,onRedactionEvent:this.events$.on,onStateChange:this.state$.on}}createRedactionScope(e){return{queueCurrentSelectionAsPending:()=>this.queueCurrentSelectionAsPending(e),enableRedact:()=>this.enableRedact(e),toggleRedact:()=>this.toggleRedact(e),isRedactActive:()=>this.isRedactActive(e),endRedact:()=>this.endRedact(e),enableMarqueeRedact:()=>this.enableMarqueeRedact(e),toggleMarqueeRedact:()=>this.toggleMarqueeRedact(e),isMarqueeRedactActive:()=>this.isMarqueeRedactActive(e),enableRedactSelection:()=>this.enableRedactSelection(e),toggleRedactSelection:()=>this.toggleRedactSelection(e),isRedactSelectionActive:()=>this.isRedactSelectionActive(e),addPending:t=>this.addPendingItems(t,e),removePending:(t,n)=>this.removePendingItem(t,n,e),clearPending:()=>this.clearPendingItems(e),commitAllPending:()=>this.commitAllPending(e),commitPending:(t,n)=>this.commitPendingOne(t,n,e),selectPending:(t,n)=>this.selectPending(t,n,e),getSelectedPending:()=>this.getSelectedPending(e),deselectPending:()=>this.deselectPending(e),getState:()=>this.getDocumentStateOrThrow(e),onPendingChange:t=>this.pending$.on(n=>{n.documentId===e&&t(n.pending)}),onSelectedChange:t=>this.selected$.on(n=>{n.documentId===e&&t(n.selected)}),onRedactionEvent:t=>this.events$.on(n=>{n.documentId===e&&t(n)}),onStateChange:t=>this.state$.on(n=>{n.documentId===e&&t(n.state)})}}getPendingFromAnnotations(e){if(!this.annotationCapability)return{};try{const n=this.annotationCapability.forDocument(e).getState(),o={};for(const e of Object.values(n.byUid))if(e.object.type===t.PdfAnnotationSubtype.REDACT){const t=this.annotationToRedactionItem(e.object),n=e.object.pageIndex;(o[n]??(o[n]=[])).push(t)}return o}catch{return{}}}getDocumentState(e){const t=e??this.getActiveDocumentId();return this.state.documents[t]??null}getDocumentStateOrThrow(e){const t=this.getDocumentState(e);if(!t)throw new Error(`Redaction state not found for document: ${e??"active"}`);return t}syncFromAnnotationCreate(e,t){const n=this.annotationToRedactionItem(t);this.dispatch(m(e,[n]))}syncFromAnnotationUpdate(e,t,n){if(!("rect"in n||"segmentRects"in n||"strokeColor"in n||"color"in n))return;const o={};n.rect&&(o.rect=n.rect),n.segmentRects&&(o.rects=n.segmentRects),n.strokeColor&&(o.markColor=n.strokeColor),n.color&&(o.redactionColor=n.color),this.logger.debug("RedactionPlugin","AnnotationUpdated",{documentId:e,annotation:t,patch:o}),this.dispatch(f(e,t.pageIndex,t.id,o))}syncFromAnnotationDelete(e,t){this.dispatch(h(e,t.pageIndex,t.id))}syncFromAnnotationLoad(e){const t=this.getPendingFromAnnotations(e);this.dispatch(p(e));for(const[,n]of Object.entries(t))n.length>0&&this.dispatch(m(e,n))}syncSelectionFromAnnotation(e){var n;const o=null==(n=this.annotationCapability)?void 0:n.forDocument(e).getState();if(!o)return;const i=o.selectedUids.map(e=>o.byUid[e]).find(e=>(null==e?void 0:e.object.type)===t.PdfAnnotationSubtype.REDACT);if(i){const t=i.object;this.dispatch(y(e,t.pageIndex,t.id))}else{const t=this.getDocumentState(e);(null==t?void 0:t.selected)&&this.dispatch(v(e))}}addPendingItems(e,n){const o=n??this.getActiveDocumentId();if(this.checkPermission(o,t.PdfPermissionFlag.ModifyContents)){if(this.useAnnotationMode){const t=this.annotationCapability.forDocument(o);for(const n of e){const e=this.redactionItemToAnnotation(n);t.createAnnotation(n.page,e)}if(e.length>0){const n=e[e.length-1];t.selectAnnotation(n.page,n.id)}}else this.dispatch(m(o,e));this.events$.emit({type:"add",documentId:o,items:e})}else this.logger.debug("RedactionPlugin","AddPendingItems",`Cannot add redactions: document ${o} lacks ModifyContents permission`)}removePendingItem(e,t,n){var o;const i=n??this.getActiveDocumentId();this.useAnnotationMode?null==(o=this.annotationCapability)||o.forDocument(i).deleteAnnotation(e,t):this.dispatch(h(i,e,t)),this.events$.emit({type:"remove",documentId:i,page:e,id:t})}clearPendingItems(e){var t;const n=e??this.getActiveDocumentId();if(this.useAnnotationMode){const e=this.getPendingFromAnnotations(n),o=null==(t=this.annotationCapability)?void 0:t.forDocument(n);for(const[t,n]of Object.entries(e)){const e=Number(t);for(const t of n)null==o||o.deleteAnnotation(e,t.id)}}else this.dispatch(p(n));this.events$.emit({type:"clear",documentId:n})}selectPending(e,t,n){var o,i;const a=n??this.getActiveDocumentId();this.useAnnotationMode?null==(o=this.annotationCapability)||o.forDocument(a).selectAnnotation(e,t):this.dispatch(y(a,e,t)),null==(i=this.selectionCapability)||i.forDocument(a).clear()}getSelectedPending(e){var t;const n=e??this.getActiveDocumentId();return(null==(t=this.getDocumentState(n))?void 0:t.selected)??null}deselectPending(e){var t;const n=e??this.getActiveDocumentId();this.useAnnotationMode?null==(t=this.annotationCapability)||t.forDocument(n).deselectAnnotation():this.dispatch(v(n))}enableRedactSelection(e){var o;const i=e??this.getActiveDocumentId();this.checkPermission(i,t.PdfPermissionFlag.ModifyContents)?null==(o=this.interactionManagerCapability)||o.forDocument(i).activate(n.RedactSelection):this.logger.debug("RedactionPlugin","EnableRedactSelection",`Cannot enable redact selection: document ${i} lacks ModifyContents permission`)}toggleRedactSelection(e){var o;const i=e??this.getActiveDocumentId(),a=null==(o=this.interactionManagerCapability)?void 0:o.forDocument(i);if((null==a?void 0:a.getActiveMode())===n.RedactSelection)null==a||a.activateDefaultMode();else{if(!this.checkPermission(i,t.PdfPermissionFlag.ModifyContents))return;null==a||a.activate(n.RedactSelection)}}isRedactSelectionActive(e){var t;const o=e??this.getActiveDocumentId(),i=null==(t=this.interactionManagerCapability)?void 0:t.forDocument(o).getActiveMode();return i===n.Redact||i===n.RedactSelection}enableMarqueeRedact(e){var o;const i=e??this.getActiveDocumentId();this.checkPermission(i,t.PdfPermissionFlag.ModifyContents)?null==(o=this.interactionManagerCapability)||o.forDocument(i).activate(n.MarqueeRedact):this.logger.debug("RedactionPlugin","EnableMarqueeRedact",`Cannot enable marquee redact: document ${i} lacks ModifyContents permission`)}toggleMarqueeRedact(e){var o;const i=e??this.getActiveDocumentId(),a=null==(o=this.interactionManagerCapability)?void 0:o.forDocument(i);if((null==a?void 0:a.getActiveMode())===n.MarqueeRedact)null==a||a.activateDefaultMode();else{if(!this.checkPermission(i,t.PdfPermissionFlag.ModifyContents))return;null==a||a.activate(n.MarqueeRedact)}}isMarqueeRedactActive(e){var t;const o=e??this.getActiveDocumentId(),i=null==(t=this.interactionManagerCapability)?void 0:t.forDocument(o).getActiveMode();return i===n.Redact||i===n.MarqueeRedact}enableRedact(e){var o;const i=e??this.getActiveDocumentId();this.checkPermission(i,t.PdfPermissionFlag.ModifyContents)?null==(o=this.interactionManagerCapability)||o.forDocument(i).activate(n.Redact):this.logger.debug("RedactionPlugin","EnableRedact",`Cannot enable redact mode: document ${i} lacks ModifyContents permission`)}toggleRedact(e){var o;const i=e??this.getActiveDocumentId(),a=null==(o=this.interactionManagerCapability)?void 0:o.forDocument(i);if((null==a?void 0:a.getActiveMode())===n.Redact)null==a||a.activateDefaultMode();else{if(!this.checkPermission(i,t.PdfPermissionFlag.ModifyContents))return;null==a||a.activate(n.Redact)}}isRedactActive(e){var t;const o=e??this.getActiveDocumentId();return(null==(t=this.interactionManagerCapability)?void 0:t.forDocument(o).getActiveMode())===n.Redact}endRedact(e){var t;const n=e??this.getActiveDocumentId();null==(t=this.interactionManagerCapability)||t.forDocument(n).activateDefaultMode()}onRedactionSelectionChange(e,t){const n=this.redactionSelection$.get(e);return(null==n?void 0:n.on(t))??(()=>{})}getPreviewStrokeColor(){if(this.useAnnotationMode&&this.annotationCapability){const e=this.annotationCapability.getTool("redact");return(null==e?void 0:e.defaults.strokeColor)??"#FF0000"}return"#FF0000"}registerMarqueeOnPage(o){if(!this.interactionManagerCapability)return this.logger.warn("RedactionPlugin","MissingDependency","Interaction manager plugin not loaded, marquee redaction disabled"),()=>{};const i=this.coreState.core.documents[o.documentId];if(!(null==i?void 0:i.document))return this.logger.warn("RedactionPlugin","DocumentNotFound","Document not found"),()=>{};const a=i.document.pages[o.pageIndex];if(!a)return this.logger.warn("RedactionPlugin","PageNotFound",`Page ${o.pageIndex} not found`),()=>{};const c=function(t){const{pageSize:n,scale:o,minDragPx:i=5,onPreview:a,onCommit:c}=t;let s=null,d=null;return{onPointerDown:(e,t)=>{var n;s=e,d={origin:{x:e.x,y:e.y},size:{width:0,height:0}},null==a||a(d),null==(n=t.setPointerCapture)||n.call(t)},onPointerMove:t=>{if(!s)return;const o=e.clamp(t.x,0,n.width),i=e.clamp(t.y,0,n.height);d={origin:{x:Math.min(s.x,o),y:Math.min(s.y,i)},size:{width:Math.abs(o-s.x),height:Math.abs(i-s.y)}},null==a||a(d)},onPointerUp:(e,t)=>{var n;d&&Math.max(d.size.width,d.size.height)*o>i&&(null==c||c(d)),s=null,d=null,null==a||a(null),null==(n=t.releasePointerCapture)||n.call(t)},onPointerCancel:(e,t)=>{var n;s=null,d=null,null==a||a(null),null==(n=t.releasePointerCapture)||n.call(t)}}}({pageSize:a.size,scale:o.scale,onPreview:o.callback.onPreview,onCommit:e=>{var n,i;if(null==(i=(n=o.callback).onCommit)||i.call(n,e),this.useAnnotationMode)this.createRedactAnnotationFromArea(o.documentId,o.pageIndex,e);else{const n=this.config.drawBlackBoxes?"#000000":"transparent",i={id:t.uuidV4(),kind:"area",page:o.pageIndex,rect:e,source:"legacy",markColor:"#FF0000",redactionColor:n};this.dispatch(m(o.documentId,[i])),this.selectPending(o.pageIndex,i.id,o.documentId)}}}),s=this.interactionManagerCapability.registerAlways({handlers:{onPointerDown:(e,t)=>{t.target===t.currentTarget&&this.deselectPending(o.documentId)}},scope:{type:"page",documentId:o.documentId,pageIndex:o.pageIndex}}),d=this.interactionManagerCapability.registerHandlers({documentId:o.documentId,modeId:n.Redact,handlers:c,pageIndex:o.pageIndex}),r=this.interactionManagerCapability.registerHandlers({documentId:o.documentId,modeId:n.MarqueeRedact,handlers:c,pageIndex:o.pageIndex});return()=>{s(),d(),r()}}queueCurrentSelectionAsPending(e){const n=e??this.getActiveDocumentId();if(!this.selectionCapability)return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"[RedactionPlugin] selection plugin required"});const o=this.coreState.core.documents[n];if(!(null==o?void 0:o.document))return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Document not found"});const i=this.selectionCapability.forDocument(n),a=i.getFormattedSelection();if(!a.length)return t.PdfTaskHelper.resolve(!0);const c=i.getSelectedText(),s=this.redactionSelection$.get(n);null==s||s.emit([]),i.clear(),this.useAnnotationMode||this.enableRedactSelection(n);const d=new t.Task;return c.wait(e=>{const t=e.join(" ");this.createRedactionsFromSelection(n,a,t),d.resolve(!0)},()=>{this.createRedactionsFromSelection(n,a),d.resolve(!0)}),d}commitPendingOne(e,n,o){const i=o??this.getActiveDocumentId();if(!this.checkPermission(i,t.PdfPermissionFlag.ModifyContents))return this.logger.debug("RedactionPlugin","CommitPendingOne",`Cannot commit redaction: document ${i} lacks ModifyContents permission`),t.PdfTaskHelper.reject({code:t.PdfErrorCode.Security,message:"Document lacks ModifyContents permission"});const a=this.coreState.core.documents[i];if(!(null==a?void 0:a.document))return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Document not found"});const c=a.document.pages[e];if(!c)return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Page not found"});if(this.useAnnotationMode)return this.logger.debug("RedactionPlugin","CommitPendingOne",`Applying redaction in annotation mode: page ${e}, id ${n}`),this.applyRedactionAnnotationMode(i,a.document,c,n);const s=this.getDocumentState(i);if(!s)return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Document state not found"});const d=(s.pending[e]??[]).find(e=>e.id===n);return d?this.commitPendingOneLegacy(i,a.document,c,e,d):(this.logger.debug("RedactionPlugin","CommitPendingOne",`No pending item found for page ${e}, id ${n}`),t.PdfTaskHelper.resolve(!0))}commitPendingOneLegacy(n,o,i,a,c){const s="text"===c.kind?c.rects:[c.rect],d=new t.Task;return this.engine.redactTextInRects(o,i,s,{drawBlackBoxes:this.config.drawBlackBoxes}).wait(()=>{this.dispatch(h(n,a,c.id)),this.dispatchCoreAction(e.refreshPages(n,[a])),this.events$.emit({type:"commit",documentId:n,success:!0}),d.resolve(!0)},e=>{this.events$.emit({type:"commit",documentId:n,success:!1,error:e.reason}),d.reject({code:t.PdfErrorCode.Unknown,message:"Failed to commit redactions"})}),d}applyRedactionAnnotationMode(n,o,i,a){var c;const s=new t.Task,d=null==(c=this.annotationCapability)?void 0:c.forDocument(n).getAnnotationById(a);return this.logger.debug("RedactionPlugin","ApplyRedactionAnnotationMode",`Looking for annotation ${a}, found: ${!!d}, type: ${null==d?void 0:d.object.type}`),d&&d.object.type===t.PdfAnnotationSubtype.REDACT?(this.logger.debug("RedactionPlugin","ApplyRedactionAnnotationMode",`Calling engine.applyRedaction for annotation ${a} on page ${i.index}`),this.engine.applyRedaction(o,i,d.object).wait(()=>{var t,o;this.logger.debug("RedactionPlugin","ApplyRedactionAnnotationMode",`Successfully applied redaction ${a} on page ${i.index}`),null==(t=this.annotationCapability)||t.forDocument(n).purgeAnnotation(i.index,a),this.dispatch(h(n,i.index,a)),null==(o=this.historyCapability)||o.forDocument(n).purgeByMetadata(e=>{var t;return(null==(t=null==e?void 0:e.annotationIds)?void 0:t.includes(a))??!1},"annotations"),this.dispatchCoreAction(e.refreshPages(n,[i.index])),this.events$.emit({type:"commit",documentId:n,success:!0}),s.resolve(!0)},e=>{var o;this.logger.error("RedactionPlugin","ApplyRedactionAnnotationMode",`Failed to apply redaction ${a}: ${(null==(o=e.reason)?void 0:o.message)??"Unknown error"}`),this.events$.emit({type:"commit",documentId:n,success:!1,error:e.reason}),s.reject({code:t.PdfErrorCode.Unknown,message:"Failed to apply redaction"})}),s):(this.logger.warn("RedactionPlugin","ApplyRedactionAnnotationMode",`Redaction annotation not found or wrong type: ${a}`),t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Redaction annotation not found"}))}commitAllPending(e){const n=e??this.getActiveDocumentId();if(!this.checkPermission(n,t.PdfPermissionFlag.ModifyContents))return this.logger.debug("RedactionPlugin","CommitAllPending",`Cannot commit redactions: document ${n} lacks ModifyContents permission`),t.PdfTaskHelper.reject({code:t.PdfErrorCode.Security,message:"Document lacks ModifyContents permission"});const o=this.coreState.core.documents[n];if(!(null==o?void 0:o.document))return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Document not found"});const i=this.getDocumentState(n);return i?this.useAnnotationMode?this.applyAllRedactionsAnnotationMode(n,o.document):this.commitAllPendingLegacy(n,o.document,i):t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Document state not found"})}commitAllPendingLegacy(n,o,i){const a=new Map;for(const[e,t]of Object.entries(i.pending)){const n=Number(e),o=a.get(n)??[];for(const e of t)"text"===e.kind?o.push(...e.rects):o.push(e.rect);a.set(n,o)}const c=Array.from(a.entries()).filter(([e,t])=>t.length>0).map(([e])=>e),s=[];for(const[e,t]of a){const n=o.pages[e];n&&(t.length&&s.push(this.engine.redactTextInRects(o,n,t,{drawBlackBoxes:this.config.drawBlackBoxes})))}const d=new t.Task;return t.Task.all(s).wait(()=>{this.dispatch(p(n)),this.dispatchCoreAction(e.refreshPages(n,c)),this.events$.emit({type:"commit",documentId:n,success:!0}),d.resolve(!0)},e=>{this.events$.emit({type:"commit",documentId:n,success:!1,error:e.reason}),d.reject({code:t.PdfErrorCode.Unknown,message:"Failed to commit redactions"})}),d}applyAllRedactionsAnnotationMode(n,o){const i=this.annotationCapability.forDocument(n).getState(),a=new Map;for(const e of Object.values(i.byUid))if(e.object.type===t.PdfAnnotationSubtype.REDACT){const t=a.get(e.object.pageIndex)??[];t.push(e.object.id),a.set(e.object.pageIndex,t)}const c=Array.from(a.keys());if(0===c.length)return t.PdfTaskHelper.resolve(!0);const s=[];for(const e of c){const t=o.pages[e];t&&s.push(this.engine.applyAllRedactions(o,t))}const d=new t.Task;return t.Task.all(s).wait(()=>{var t,o;const i=null==(t=this.annotationCapability)?void 0:t.forDocument(n),s=[];for(const[e,c]of a)for(const t of c)null==i||i.purgeAnnotation(e,t),this.dispatch(h(n,e,t)),s.push(t);s.length>0&&(null==(o=this.historyCapability)||o.forDocument(n).purgeByMetadata(e=>{var t;return(null==(t=null==e?void 0:e.annotationIds)?void 0:t.some(e=>s.includes(e)))??!1},"annotations")),this.dispatchCoreAction(e.refreshPages(n,c)),this.events$.emit({type:"commit",documentId:n,success:!0}),d.resolve(!0)},e=>{this.events$.emit({type:"commit",documentId:n,success:!1,error:e.reason}),d.reject({code:t.PdfErrorCode.Unknown,message:"Failed to apply redactions"})}),d}createRedactAnnotationsFromSelection(e,n,o){if(!this.annotationCapability)return[];const i=this.annotationCapability.forDocument(e),a=this.annotationCapability.getTool("redact"),c=null==a?void 0:a.defaults,s=[];for(const d of n){const e=t.uuidV4();s.push(e);const a={...c,id:e,type:t.PdfAnnotationSubtype.REDACT,pageIndex:d.pageIndex,rect:d.rect,segmentRects:d.segmentRects,...o?{custom:{text:o}}:{},created:new Date};i.createAnnotation(d.pageIndex,a),d===n[n.length-1]&&i.selectAnnotation(d.pageIndex,e)}if(o)for(let t=0;t<s.length;t++){const i=n[t].pageIndex;this.dispatch(f(e,i,s[t],{text:o}))}return s}createLegacyRedactionsFromSelection(e,n,o){const i=this.config.drawBlackBoxes?"#000000":"transparent",a=n.map(e=>({id:t.uuidV4(),kind:"text",page:e.pageIndex,rect:e.rect,rects:e.segmentRects,source:"legacy",markColor:"#FF0000",redactionColor:i,text:o}));this.dispatch(m(e,a)),a.length&&this.selectPending(a[a.length-1].page,a[a.length-1].id,e)}createRedactionsFromSelection(e,t,n){this.useAnnotationMode?this.createRedactAnnotationsFromSelection(e,t,n):this.createLegacyRedactionsFromSelection(e,t,n)}createRedactAnnotationFromArea(e,n,o){if(!this.annotationCapability)return;const i=this.annotationCapability.forDocument(e),a=this.annotationCapability.getTool("redact"),c=null==a?void 0:a.defaults,s=t.uuidV4(),d={...c,id:s,type:t.PdfAnnotationSubtype.REDACT,pageIndex:n,rect:o,segmentRects:[],created:new Date};i.createAnnotation(n,d),i.selectAnnotation(n,s)}redactionItemToAnnotation(e){var n;const o=null==(n=this.annotationCapability)?void 0:n.getTool("redact");return{...(null==o?void 0:o.defaults)??{},id:e.id,type:t.PdfAnnotationSubtype.REDACT,pageIndex:e.page,rect:e.rect,segmentRects:"text"===e.kind?e.rects:[],created:new Date}}annotationToRedactionItem(e){var t;const n=e.strokeColor??"#FF0000",o=e.color??"transparent";return e.segmentRects&&e.segmentRects.length>0?{id:e.id,kind:"text",page:e.pageIndex,rect:e.rect,rects:e.segmentRects,source:"annotation",markColor:n,redactionColor:o,...(null==(t=e.custom)?void 0:t.text)?{text:e.custom.text}:{}}:{id:e.id,kind:"area",page:e.pageIndex,rect:e.rect,source:"annotation",markColor:n,redactionColor:o}}emitPendingChange(e){const t=this.getDocumentState(e);t&&this.pending$.emit({documentId:e,pending:t.pending})}emitSelectedChange(e){const t=this.getDocumentState(e);t&&this.selected$.emit({documentId:e,selected:t.selected})}emitStateChange(e){const t=this.getDocumentState(e);t&&this.state$.emit({documentId:e,state:t})}onStoreUpdated(e,t){for(const n in t.documents){t.documents[n]&&(this.emitPendingChange(n),this.emitSelectedChange(n),this.emitStateChange(n))}}async destroy(){this.pending$.clear(),this.selected$.clear(),this.state$.clear(),this.events$.clear(),this.redactionSelection$.forEach(e=>e.clear()),this.redactionSelection$.clear(),this.documentUnsubscribers.forEach(e=>{e.forEach(e=>e())}),this.documentUnsubscribers.clear(),await super.destroy()}};I.id="redaction";let S=I;const D="redaction",M={id:D,name:"Redaction Plugin",version:"1.0.0",provides:["redaction"],requires:[],optional:["interaction-manager","selection","annotation"],defaultConfig:{drawBlackBoxes:!0}},x={manifest:M,create:(e,t)=>new S(D,e,t),reducer:(e=C,t)=>{switch(t.type){case o:{const{documentId:n,state:o}=t.payload;return{...e,documents:{...e.documents,[n]:o},activeDocumentId:e.activeDocumentId??n}}case i:{const n=t.payload,{[n]:o,...i}=e.documents;return{...e,documents:i,activeDocumentId:e.activeDocumentId===n?null:e.activeDocumentId}}case"REDACTION/SET_ACTIVE_DOCUMENT":return{...e,activeDocumentId:t.payload};case s:{const{documentId:n,items:o}=t.payload,i=e.documents[n];if(!i)return e;const a={...i.pending};for(const e of o){const t=a[e.page]??[];t.some(t=>t.id===e.id)||(a[e.page]=t.concat(e))}return{...e,documents:{...e.documents,[n]:{...i,pending:a,pendingCount:P(a)}}}}case d:{const{documentId:n,page:o,id:i}=t.payload,a=e.documents[n];if(!a)return e;const c=(a.pending[o]??[]).filter(e=>e.id!==i),s={...a.pending,[o]:c},d=a.selected&&!(a.selected.page===o&&a.selected.id===i);return{...e,documents:{...e.documents,[n]:{...a,pending:s,pendingCount:P(s),selected:d?a.selected:null}}}}case r:{const{documentId:n,page:o,id:i,patch:a}=t.payload,c=e.documents[n];if(!c)return e;const s=(c.pending[o]??[]).map(e=>e.id===i?{...e,...a}:e);return{...e,documents:{...e.documents,[n]:{...c,pending:{...c.pending,[o]:s}}}}}case l:{const n=t.payload,o=e.documents[n];return o?{...e,documents:{...e.documents,[n]:{...o,pending:{},pendingCount:0,selected:null}}}:e}case u:{const{documentId:n,page:o,id:i}=t.payload,a=e.documents[n];return a?{...e,documents:{...e.documents,[n]:{...a,selected:{page:o,id:i}}}}:e}case g:{const n=t.payload,o=e.documents[n];return o?{...e,documents:{...e.documents,[n]:{...o,selected:null}}}:e}case a:{const{documentId:n,mode:o}=t.payload,i=e.documents[n];return i?{...e,documents:{...e.documents,[n]:{...i,isRedacting:!0,activeType:o}}}:e}case c:{const n=t.payload,o=e.documents[n];return o?{...e,documents:{...e.documents,[n]:{...o,isRedacting:!1,activeType:null}}}:e}case"SET_ACTIVE_TYPE":{const{documentId:n,mode:o}=t.payload,i=e.documents[n];return i?{...e,documents:{...e.documents,[n]:{...i,activeType:o}}}:e}default:return e}},initialState:C};exports.REDACTION_PLUGIN_ID=D,exports.RedactionMode=n,exports.RedactionPlugin=S,exports.RedactionPluginPackage=x,exports.getDocumentPendingCount=(e,t)=>{var n;return(null==(n=e.documents[t])?void 0:n.pendingCount)??0},exports.getPendingRedactionsCount=e=>e.pendingCount,exports.getTotalPendingCount=e=>Object.values(e.documents).reduce((e,t)=>e+t.pendingCount,0),exports.hasPendingRedactions=e=>e.pendingCount>0,exports.initialDocumentState=R,exports.initialState=C,exports.manifest=M,exports.redactTool=A,exports.redactTools=b;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("@embedpdf/core"),t=require("@embedpdf/models");var n=(e=>(e.Redact="redact",e.MarqueeRedact="marqueeRedact",e.RedactSelection="redactSelection",e))(n||{});const o="REDACTION/INIT_STATE",i="REDACTION/CLEANUP_STATE",a="START_REDACTION",c="END_REDACTION",s="ADD_PENDING",d="REMOVE_PENDING",r="UPDATE_PENDING",l="CLEAR_PENDING",u="SELECT_PENDING",g="DESELECT_PENDING";const h=(e,t)=>({type:s,payload:{documentId:e,items:t}}),m=(e,t,n)=>({type:d,payload:{documentId:e,page:t,id:n}}),p=e=>({type:l,payload:e}),f=(e,t,n,o)=>({type:r,payload:{documentId:e,page:t,id:n,patch:o}}),y=(e,t,n)=>({type:u,payload:{documentId:e,page:t,id:n}}),v=e=>({type:g,payload:e}),R=e=>Object.values(e).reduce((e,t)=>e+t.length,0),C={isRedacting:!1,activeType:null,pending:{},pendingCount:0,selected:null},P={documents:{},activeDocumentId:null},A={id:"redact",name:"Redact",matchScore:e=>e.type===t.PdfAnnotationSubtype.REDACT?10:0,interaction:{mode:n.Redact,exclusive:!1,cursor:"crosshair",textSelection:!0,isDraggable:e=>{var n;return e.type!==t.PdfAnnotationSubtype.REDACT||!(null==(n=e.segmentRects)?void 0:n.length)},isResizable:e=>{var n;return e.type!==t.PdfAnnotationSubtype.REDACT||!(null==(n=e.segmentRects)?void 0:n.length)},isRotatable:!1,lockAspectRatio:!1,isGroupDraggable:!1,isGroupResizable:!1,isGroupRotatable:!1},defaults:{type:t.PdfAnnotationSubtype.REDACT,color:"#000000",overlayColor:"#FFFFFF",strokeColor:"#E44234",opacity:1}},b=[A],M=class extends e.BasePlugin{constructor(t,n,o){var i,a,c,s;if(super(t,n),this.redactionSelection$=new Map,this.redactionMarquee$=new Map,this.pending$=e.createBehaviorEmitter(),this.selected$=e.createBehaviorEmitter(),this.state$=e.createBehaviorEmitter(),this.events$=e.createBehaviorEmitter(),this.documentUnsubscribers=new Map,this.config=o,this.selectionCapability=null==(i=this.registry.getPlugin("selection"))?void 0:i.provides(),this.interactionManagerCapability=null==(a=this.registry.getPlugin("interaction-manager"))?void 0:a.provides(),this.annotationCapability=null==(c=this.registry.getPlugin("annotation"))?void 0:c.provides(),this.historyCapability=null==(s=this.registry.getPlugin("history"))?void 0:s.provides(),this.config.useAnnotationMode?this.annotationCapability?this.useAnnotationMode=!0:(this.logger.warn("RedactionPlugin","ConfigError","useAnnotationMode is enabled but annotation plugin is not available. Falling back to legacy mode."),this.useAnnotationMode=!1):this.useAnnotationMode=!1,this.useAnnotationMode)for(const e of b)this.annotationCapability.addTool(e);this.setupRedactionModes(),!this.useAnnotationMode&&this.annotationCapability&&this.logger.info("RedactionPlugin","LegacyMode","Using legacy redaction mode. Set useAnnotationMode: true in config to use annotation-based redactions."),this.setupModeChangeListener()}setupRedactionModes(){this.interactionManagerCapability&&(this.interactionManagerCapability.registerMode({id:n.Redact,scope:"page",exclusive:!1,cursor:"crosshair"}),this.interactionManagerCapability.registerMode({id:n.MarqueeRedact,scope:"page",exclusive:!1,cursor:"crosshair"}),this.interactionManagerCapability.registerMode({id:n.RedactSelection,scope:"page",exclusive:!1}))}setupModeChangeListener(){var e;null==(e=this.interactionManagerCapability)||e.onModeChange(e=>{const t=e.documentId;if(e.activeMode===n.Redact||e.activeMode===n.MarqueeRedact||e.activeMode===n.RedactSelection)this.dispatch(((e,t)=>({type:a,payload:{documentId:e,mode:t}}))(t,e.activeMode));else{const e=this.getDocumentState(t);(null==e?void 0:e.isRedacting)&&this.dispatch((e=>({type:c,payload:e}))(t))}})}onDocumentLoadingStarted(n){this.dispatch(function(e,t){return{type:o,payload:{documentId:e,state:t}}}(n,{...C})),this.redactionSelection$.set(n,e.createBehaviorEmitter()),this.redactionMarquee$.set(n,e.createBehaviorEmitter());const i=[];if(this.selectionCapability){const e=this.selectionCapability.forDocument(n),o=e.onSelectionChange(()=>{const t=this.getDocumentState(n);if(!(null==t?void 0:t.isRedacting))return;const o=e.getFormattedSelection(),i=this.redactionSelection$.get(n);null==i||i.emit(o)}),a=e.onEndSelection(()=>{const o=this.getDocumentState(n);if(!(null==o?void 0:o.isRedacting))return;if(!this.checkPermission(n,t.PdfPermissionFlag.ModifyContents))return;const i=e.getFormattedSelection();if(!i.length)return;const a=e.getSelectedText(),c=this.redactionSelection$.get(n);null==c||c.emit([]),e.clear(),a.wait(e=>{const t=e.join(" ");this.createRedactionsFromSelection(n,i,t)},()=>{this.createRedactionsFromSelection(n,i)})}),c=e.onMarqueeChange(e=>{var t;const o=this.getDocumentState(n);(null==o?void 0:o.isRedacting)&&(null==(t=this.redactionMarquee$.get(n))||t.emit({pageIndex:e.pageIndex,rect:e.rect,modeId:e.modeId}))}),s=e.onMarqueeEnd(e=>{const o=this.getDocumentState(n);if((null==o?void 0:o.isRedacting)&&this.checkPermission(n,t.PdfPermissionFlag.ModifyContents))if(this.useAnnotationMode)this.createRedactAnnotationFromArea(n,e.pageIndex,e.rect);else{const o=this.config.drawBlackBoxes?"#000000":"transparent",i={id:t.uuidV4(),kind:"area",page:e.pageIndex,rect:e.rect,source:"legacy",markColor:"#FF0000",redactionColor:o};this.dispatch(h(n,[i])),this.selectPending(e.pageIndex,i.id,n)}}),d=e.onEmptySpaceClick(()=>{this.deselectPending(n)});i.push(o,a,c,s,d)}if(this.useAnnotationMode&&this.annotationCapability){const e=this.annotationCapability.forDocument(n),o=e.onAnnotationEvent(e=>{var o;if("loaded"===e.type)return void this.syncFromAnnotationLoad(n);if((null==(o=e.annotation)?void 0:o.type)!==t.PdfAnnotationSubtype.REDACT)return;const i=e.annotation;"create"===e.type?(this.syncFromAnnotationCreate(n,i),this.events$.emit({type:"add",documentId:n,items:[this.annotationToRedactionItem(i)]})):"update"===e.type?(this.logger.debug("RedactionPlugin","AnnotationUpdated",{documentId:n,redactAnno:i,patch:e.patch}),this.syncFromAnnotationUpdate(n,i,e.patch)):"delete"===e.type&&(this.syncFromAnnotationDelete(n,i),this.events$.emit({type:"remove",documentId:n,page:i.pageIndex,id:i.id}))}),a=e.onStateChange(()=>{this.syncSelectionFromAnnotation(n)});i.push(o,a)}this.documentUnsubscribers.set(n,i),this.logger.debug("RedactionPlugin","DocumentOpened",`Initialized redaction state for document: ${n}`)}onDocumentLoaded(e){var t,o,i;null==(t=this.selectionCapability)||t.enableForMode(n.Redact,{enableSelection:!0,showSelectionRects:!1,enableMarquee:!0,showMarqueeRects:!1},e),null==(o=this.selectionCapability)||o.enableForMode(n.MarqueeRedact,{enableSelection:!1,enableMarquee:!0,showMarqueeRects:!1},e),null==(i=this.selectionCapability)||i.enableForMode(n.RedactSelection,{enableSelection:!0,showSelectionRects:!1},e)}onDocumentClosed(e){this.dispatch(function(e){return{type:i,payload:e}}(e));const t=this.redactionSelection$.get(e);null==t||t.clear(),this.redactionSelection$.delete(e);const n=this.redactionMarquee$.get(e);null==n||n.clear(),this.redactionMarquee$.delete(e);const o=this.documentUnsubscribers.get(e);o&&(o.forEach(e=>e()),this.documentUnsubscribers.delete(e)),this.logger.debug("RedactionPlugin","DocumentClosed",`Cleaned up redaction state for document: ${e}`)}async initialize(e){this.logger.info("RedactionPlugin","Initialize","Redaction plugin initialized")}buildCapability(){return{queueCurrentSelectionAsPending:()=>this.queueCurrentSelectionAsPending(),enableRedact:()=>this.enableRedact(),toggleRedact:()=>this.toggleRedact(),isRedactActive:()=>this.isRedactActive(),endRedact:()=>this.endRedact(),enableMarqueeRedact:()=>this.enableMarqueeRedact(),toggleMarqueeRedact:()=>this.toggleMarqueeRedact(),isMarqueeRedactActive:()=>this.isMarqueeRedactActive(),enableRedactSelection:()=>this.enableRedactSelection(),toggleRedactSelection:()=>this.toggleRedactSelection(),isRedactSelectionActive:()=>this.isRedactSelectionActive(),addPending:e=>this.addPendingItems(e),removePending:(e,t)=>this.removePendingItem(e,t),clearPending:()=>this.clearPendingItems(),commitAllPending:()=>this.commitAllPending(),commitPending:(e,t)=>this.commitPendingOne(e,t),selectPending:(e,t)=>this.selectPending(e,t),getSelectedPending:()=>this.getSelectedPending(),deselectPending:()=>this.deselectPending(),getState:()=>this.getDocumentStateOrThrow(),forDocument:e=>this.createRedactionScope(e),onPendingChange:this.pending$.on,onSelectedChange:this.selected$.on,onRedactionEvent:this.events$.on,onStateChange:this.state$.on}}createRedactionScope(e){return{queueCurrentSelectionAsPending:()=>this.queueCurrentSelectionAsPending(e),enableRedact:()=>this.enableRedact(e),toggleRedact:()=>this.toggleRedact(e),isRedactActive:()=>this.isRedactActive(e),endRedact:()=>this.endRedact(e),enableMarqueeRedact:()=>this.enableMarqueeRedact(e),toggleMarqueeRedact:()=>this.toggleMarqueeRedact(e),isMarqueeRedactActive:()=>this.isMarqueeRedactActive(e),enableRedactSelection:()=>this.enableRedactSelection(e),toggleRedactSelection:()=>this.toggleRedactSelection(e),isRedactSelectionActive:()=>this.isRedactSelectionActive(e),addPending:t=>this.addPendingItems(t,e),removePending:(t,n)=>this.removePendingItem(t,n,e),clearPending:()=>this.clearPendingItems(e),commitAllPending:()=>this.commitAllPending(e),commitPending:(t,n)=>this.commitPendingOne(t,n,e),selectPending:(t,n)=>this.selectPending(t,n,e),getSelectedPending:()=>this.getSelectedPending(e),deselectPending:()=>this.deselectPending(e),getState:()=>this.getDocumentStateOrThrow(e),onPendingChange:t=>this.pending$.on(n=>{n.documentId===e&&t(n.pending)}),onSelectedChange:t=>this.selected$.on(n=>{n.documentId===e&&t(n.selected)}),onRedactionEvent:t=>this.events$.on(n=>{n.documentId===e&&t(n)}),onStateChange:t=>this.state$.on(n=>{n.documentId===e&&t(n.state)})}}getPendingFromAnnotations(e){if(!this.annotationCapability)return{};try{const n=this.annotationCapability.forDocument(e).getState(),o={};for(const e of Object.values(n.byUid))if(e.object.type===t.PdfAnnotationSubtype.REDACT){const t=this.annotationToRedactionItem(e.object),n=e.object.pageIndex;(o[n]??(o[n]=[])).push(t)}return o}catch{return{}}}getDocumentState(e){const t=e??this.getActiveDocumentId();return this.state.documents[t]??null}getDocumentStateOrThrow(e){const t=this.getDocumentState(e);if(!t)throw new Error(`Redaction state not found for document: ${e??"active"}`);return t}syncFromAnnotationCreate(e,t){const n=this.annotationToRedactionItem(t);this.dispatch(h(e,[n]))}syncFromAnnotationUpdate(e,t,n){if(!("rect"in n||"segmentRects"in n||"strokeColor"in n||"color"in n))return;const o={};n.rect&&(o.rect=n.rect),n.segmentRects&&(o.rects=n.segmentRects),n.strokeColor&&(o.markColor=n.strokeColor),n.color&&(o.redactionColor=n.color),this.logger.debug("RedactionPlugin","AnnotationUpdated",{documentId:e,annotation:t,patch:o}),this.dispatch(f(e,t.pageIndex,t.id,o))}syncFromAnnotationDelete(e,t){this.dispatch(m(e,t.pageIndex,t.id))}syncFromAnnotationLoad(e){const t=this.getPendingFromAnnotations(e);this.dispatch(p(e));for(const[,n]of Object.entries(t))n.length>0&&this.dispatch(h(e,n))}syncSelectionFromAnnotation(e){var n;const o=null==(n=this.annotationCapability)?void 0:n.forDocument(e).getState();if(!o)return;const i=o.selectedUids.map(e=>o.byUid[e]).find(e=>(null==e?void 0:e.object.type)===t.PdfAnnotationSubtype.REDACT);if(i){const t=i.object;this.dispatch(y(e,t.pageIndex,t.id))}else{const t=this.getDocumentState(e);(null==t?void 0:t.selected)&&this.dispatch(v(e))}}addPendingItems(e,n){const o=n??this.getActiveDocumentId();if(this.checkPermission(o,t.PdfPermissionFlag.ModifyContents)){if(this.useAnnotationMode){const t=this.annotationCapability.forDocument(o);for(const n of e){const e=this.redactionItemToAnnotation(n);t.createAnnotation(n.page,e)}if(e.length>0){const n=e[e.length-1];t.selectAnnotation(n.page,n.id)}}else this.dispatch(h(o,e));this.events$.emit({type:"add",documentId:o,items:e})}else this.logger.debug("RedactionPlugin","AddPendingItems",`Cannot add redactions: document ${o} lacks ModifyContents permission`)}removePendingItem(e,t,n){var o;const i=n??this.getActiveDocumentId();this.useAnnotationMode?null==(o=this.annotationCapability)||o.forDocument(i).deleteAnnotation(e,t):this.dispatch(m(i,e,t)),this.events$.emit({type:"remove",documentId:i,page:e,id:t})}clearPendingItems(e){var t;const n=e??this.getActiveDocumentId();if(this.useAnnotationMode){const e=this.getPendingFromAnnotations(n),o=null==(t=this.annotationCapability)?void 0:t.forDocument(n);for(const[t,n]of Object.entries(e)){const e=Number(t);for(const t of n)null==o||o.deleteAnnotation(e,t.id)}}else this.dispatch(p(n));this.events$.emit({type:"clear",documentId:n})}selectPending(e,t,n){var o,i,a;const c=n??this.getActiveDocumentId();this.useAnnotationMode?null==(o=this.annotationCapability)||o.forDocument(c).selectAnnotation(e,t):(this.dispatch(y(c,e,t)),null==(i=this.interactionManagerCapability)||i.claimPageActivity(c,"redaction-selection",e)),null==(a=this.selectionCapability)||a.forDocument(c).clear()}getSelectedPending(e){var t;const n=e??this.getActiveDocumentId();return(null==(t=this.getDocumentState(n))?void 0:t.selected)??null}deselectPending(e){var t,n;const o=e??this.getActiveDocumentId();this.useAnnotationMode?null==(t=this.annotationCapability)||t.forDocument(o).deselectAnnotation():(this.dispatch(v(o)),null==(n=this.interactionManagerCapability)||n.releasePageActivity(o,"redaction-selection"))}enableRedactSelection(e){var o;const i=e??this.getActiveDocumentId();this.checkPermission(i,t.PdfPermissionFlag.ModifyContents)?null==(o=this.interactionManagerCapability)||o.forDocument(i).activate(n.RedactSelection):this.logger.debug("RedactionPlugin","EnableRedactSelection",`Cannot enable redact selection: document ${i} lacks ModifyContents permission`)}toggleRedactSelection(e){var o;const i=e??this.getActiveDocumentId(),a=null==(o=this.interactionManagerCapability)?void 0:o.forDocument(i);if((null==a?void 0:a.getActiveMode())===n.RedactSelection)null==a||a.activateDefaultMode();else{if(!this.checkPermission(i,t.PdfPermissionFlag.ModifyContents))return;null==a||a.activate(n.RedactSelection)}}isRedactSelectionActive(e){var t;const o=e??this.getActiveDocumentId(),i=null==(t=this.interactionManagerCapability)?void 0:t.forDocument(o).getActiveMode();return i===n.Redact||i===n.RedactSelection}enableMarqueeRedact(e){var o;const i=e??this.getActiveDocumentId();this.checkPermission(i,t.PdfPermissionFlag.ModifyContents)?null==(o=this.interactionManagerCapability)||o.forDocument(i).activate(n.MarqueeRedact):this.logger.debug("RedactionPlugin","EnableMarqueeRedact",`Cannot enable marquee redact: document ${i} lacks ModifyContents permission`)}toggleMarqueeRedact(e){var o;const i=e??this.getActiveDocumentId(),a=null==(o=this.interactionManagerCapability)?void 0:o.forDocument(i);if((null==a?void 0:a.getActiveMode())===n.MarqueeRedact)null==a||a.activateDefaultMode();else{if(!this.checkPermission(i,t.PdfPermissionFlag.ModifyContents))return;null==a||a.activate(n.MarqueeRedact)}}isMarqueeRedactActive(e){var t;const o=e??this.getActiveDocumentId(),i=null==(t=this.interactionManagerCapability)?void 0:t.forDocument(o).getActiveMode();return i===n.Redact||i===n.MarqueeRedact}enableRedact(e){var o;const i=e??this.getActiveDocumentId();this.checkPermission(i,t.PdfPermissionFlag.ModifyContents)?null==(o=this.interactionManagerCapability)||o.forDocument(i).activate(n.Redact):this.logger.debug("RedactionPlugin","EnableRedact",`Cannot enable redact mode: document ${i} lacks ModifyContents permission`)}toggleRedact(e){var o;const i=e??this.getActiveDocumentId(),a=null==(o=this.interactionManagerCapability)?void 0:o.forDocument(i);if((null==a?void 0:a.getActiveMode())===n.Redact)null==a||a.activateDefaultMode();else{if(!this.checkPermission(i,t.PdfPermissionFlag.ModifyContents))return;null==a||a.activate(n.Redact)}}isRedactActive(e){var t;const o=e??this.getActiveDocumentId();return(null==(t=this.interactionManagerCapability)?void 0:t.forDocument(o).getActiveMode())===n.Redact}endRedact(e){var t;const n=e??this.getActiveDocumentId();null==(t=this.interactionManagerCapability)||t.forDocument(n).activateDefaultMode()}onRedactionSelectionChange(e,t){const n=this.redactionSelection$.get(e);return(null==n?void 0:n.on(t))??(()=>{})}onRedactionMarqueeChange(e,t){const n=this.redactionMarquee$.get(e);return(null==n?void 0:n.on(t))??(()=>{})}getPreviewStrokeColor(){if(this.useAnnotationMode&&this.annotationCapability){const e=this.annotationCapability.getTool("redact");return(null==e?void 0:e.defaults.strokeColor)??"#FF0000"}return"#FF0000"}queueCurrentSelectionAsPending(e){const n=e??this.getActiveDocumentId();if(!this.selectionCapability)return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"[RedactionPlugin] selection plugin required"});const o=this.coreState.core.documents[n];if(!(null==o?void 0:o.document))return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Document not found"});const i=this.selectionCapability.forDocument(n),a=i.getFormattedSelection();if(!a.length)return t.PdfTaskHelper.resolve(!0);const c=i.getSelectedText(),s=this.redactionSelection$.get(n);null==s||s.emit([]),i.clear(),this.useAnnotationMode||this.enableRedactSelection(n);const d=new t.Task;return c.wait(e=>{const t=e.join(" ");this.createRedactionsFromSelection(n,a,t),d.resolve(!0)},()=>{this.createRedactionsFromSelection(n,a),d.resolve(!0)}),d}commitPendingOne(e,n,o){const i=o??this.getActiveDocumentId();if(!this.checkPermission(i,t.PdfPermissionFlag.ModifyContents))return this.logger.debug("RedactionPlugin","CommitPendingOne",`Cannot commit redaction: document ${i} lacks ModifyContents permission`),t.PdfTaskHelper.reject({code:t.PdfErrorCode.Security,message:"Document lacks ModifyContents permission"});const a=this.coreState.core.documents[i];if(!(null==a?void 0:a.document))return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Document not found"});const c=a.document.pages[e];if(!c)return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Page not found"});if(this.useAnnotationMode)return this.logger.debug("RedactionPlugin","CommitPendingOne",`Applying redaction in annotation mode: page ${e}, id ${n}`),this.applyRedactionAnnotationMode(i,a.document,c,n);const s=this.getDocumentState(i);if(!s)return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Document state not found"});const d=(s.pending[e]??[]).find(e=>e.id===n);return d?this.commitPendingOneLegacy(i,a.document,c,e,d):(this.logger.debug("RedactionPlugin","CommitPendingOne",`No pending item found for page ${e}, id ${n}`),t.PdfTaskHelper.resolve(!0))}commitPendingOneLegacy(n,o,i,a,c){const s="text"===c.kind?c.rects:[c.rect],d=new t.Task;return this.engine.redactTextInRects(o,i,s,{drawBlackBoxes:this.config.drawBlackBoxes}).wait(()=>{this.dispatch(m(n,a,c.id)),this.dispatchCoreAction(e.refreshPages(n,[a])),this.events$.emit({type:"commit",documentId:n,success:!0}),d.resolve(!0)},e=>{this.events$.emit({type:"commit",documentId:n,success:!1,error:e.reason}),d.reject({code:t.PdfErrorCode.Unknown,message:"Failed to commit redactions"})}),d}applyRedactionAnnotationMode(n,o,i,a){var c;const s=new t.Task,d=null==(c=this.annotationCapability)?void 0:c.forDocument(n).getAnnotationById(a);return this.logger.debug("RedactionPlugin","ApplyRedactionAnnotationMode",`Looking for annotation ${a}, found: ${!!d}, type: ${null==d?void 0:d.object.type}`),d&&d.object.type===t.PdfAnnotationSubtype.REDACT?(this.logger.debug("RedactionPlugin","ApplyRedactionAnnotationMode",`Calling engine.applyRedaction for annotation ${a} on page ${i.index}`),this.engine.applyRedaction(o,i,d.object).wait(()=>{var t,o;this.logger.debug("RedactionPlugin","ApplyRedactionAnnotationMode",`Successfully applied redaction ${a} on page ${i.index}`),null==(t=this.annotationCapability)||t.forDocument(n).purgeAnnotation(i.index,a),this.dispatch(m(n,i.index,a)),null==(o=this.historyCapability)||o.forDocument(n).purgeByMetadata(e=>{var t;return(null==(t=null==e?void 0:e.annotationIds)?void 0:t.includes(a))??!1},"annotations"),this.dispatchCoreAction(e.refreshPages(n,[i.index])),this.events$.emit({type:"commit",documentId:n,success:!0}),s.resolve(!0)},e=>{var o;this.logger.error("RedactionPlugin","ApplyRedactionAnnotationMode",`Failed to apply redaction ${a}: ${(null==(o=e.reason)?void 0:o.message)??"Unknown error"}`),this.events$.emit({type:"commit",documentId:n,success:!1,error:e.reason}),s.reject({code:t.PdfErrorCode.Unknown,message:"Failed to apply redaction"})}),s):(this.logger.warn("RedactionPlugin","ApplyRedactionAnnotationMode",`Redaction annotation not found or wrong type: ${a}`),t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Redaction annotation not found"}))}commitAllPending(e){const n=e??this.getActiveDocumentId();if(!this.checkPermission(n,t.PdfPermissionFlag.ModifyContents))return this.logger.debug("RedactionPlugin","CommitAllPending",`Cannot commit redactions: document ${n} lacks ModifyContents permission`),t.PdfTaskHelper.reject({code:t.PdfErrorCode.Security,message:"Document lacks ModifyContents permission"});const o=this.coreState.core.documents[n];if(!(null==o?void 0:o.document))return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Document not found"});const i=this.getDocumentState(n);return i?this.useAnnotationMode?this.applyAllRedactionsAnnotationMode(n,o.document):this.commitAllPendingLegacy(n,o.document,i):t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Document state not found"})}commitAllPendingLegacy(n,o,i){const a=new Map;for(const[e,t]of Object.entries(i.pending)){const n=Number(e),o=a.get(n)??[];for(const e of t)"text"===e.kind?o.push(...e.rects):o.push(e.rect);a.set(n,o)}const c=Array.from(a.entries()).filter(([e,t])=>t.length>0).map(([e])=>e),s=[];for(const[e,t]of a){const n=o.pages[e];n&&(t.length&&s.push(this.engine.redactTextInRects(o,n,t,{drawBlackBoxes:this.config.drawBlackBoxes})))}const d=new t.Task;return t.Task.all(s).wait(()=>{this.dispatch(p(n)),this.dispatchCoreAction(e.refreshPages(n,c)),this.events$.emit({type:"commit",documentId:n,success:!0}),d.resolve(!0)},e=>{this.events$.emit({type:"commit",documentId:n,success:!1,error:e.reason}),d.reject({code:t.PdfErrorCode.Unknown,message:"Failed to commit redactions"})}),d}applyAllRedactionsAnnotationMode(n,o){const i=this.annotationCapability.forDocument(n).getState(),a=new Map;for(const e of Object.values(i.byUid))if(e.object.type===t.PdfAnnotationSubtype.REDACT){const t=a.get(e.object.pageIndex)??[];t.push(e.object.id),a.set(e.object.pageIndex,t)}const c=Array.from(a.keys());if(0===c.length)return t.PdfTaskHelper.resolve(!0);const s=[];for(const e of c){const t=o.pages[e];t&&s.push(this.engine.applyAllRedactions(o,t))}const d=new t.Task;return t.Task.all(s).wait(()=>{var t,o;const i=null==(t=this.annotationCapability)?void 0:t.forDocument(n),s=[];for(const[e,c]of a)for(const t of c)null==i||i.purgeAnnotation(e,t),this.dispatch(m(n,e,t)),s.push(t);s.length>0&&(null==(o=this.historyCapability)||o.forDocument(n).purgeByMetadata(e=>{var t;return(null==(t=null==e?void 0:e.annotationIds)?void 0:t.some(e=>s.includes(e)))??!1},"annotations")),this.dispatchCoreAction(e.refreshPages(n,c)),this.events$.emit({type:"commit",documentId:n,success:!0}),d.resolve(!0)},e=>{this.events$.emit({type:"commit",documentId:n,success:!1,error:e.reason}),d.reject({code:t.PdfErrorCode.Unknown,message:"Failed to apply redactions"})}),d}createRedactAnnotationsFromSelection(e,n,o){if(!this.annotationCapability)return[];const i=this.annotationCapability.forDocument(e),a=this.annotationCapability.getTool("redact"),c=null==a?void 0:a.defaults,s=[];for(const d of n){const e=t.uuidV4();s.push(e);const a={...c,id:e,type:t.PdfAnnotationSubtype.REDACT,pageIndex:d.pageIndex,rect:d.rect,segmentRects:d.segmentRects,...o?{custom:{text:o}}:{},created:new Date};i.createAnnotation(d.pageIndex,a),d===n[n.length-1]&&i.selectAnnotation(d.pageIndex,e)}if(o)for(let t=0;t<s.length;t++){const i=n[t].pageIndex;this.dispatch(f(e,i,s[t],{text:o}))}return s}createLegacyRedactionsFromSelection(e,n,o){const i=this.config.drawBlackBoxes?"#000000":"transparent",a=n.map(e=>({id:t.uuidV4(),kind:"text",page:e.pageIndex,rect:e.rect,rects:e.segmentRects,source:"legacy",markColor:"#FF0000",redactionColor:i,text:o}));this.dispatch(h(e,a)),a.length&&this.selectPending(a[a.length-1].page,a[a.length-1].id,e)}createRedactionsFromSelection(e,t,n){this.useAnnotationMode?this.createRedactAnnotationsFromSelection(e,t,n):this.createLegacyRedactionsFromSelection(e,t,n)}createRedactAnnotationFromArea(e,n,o){if(!this.annotationCapability)return;const i=this.annotationCapability.forDocument(e),a=this.annotationCapability.getTool("redact"),c=null==a?void 0:a.defaults,s=t.uuidV4(),d={...c,id:s,type:t.PdfAnnotationSubtype.REDACT,pageIndex:n,rect:o,segmentRects:[],created:new Date};i.createAnnotation(n,d),i.selectAnnotation(n,s)}redactionItemToAnnotation(e){var n;const o=null==(n=this.annotationCapability)?void 0:n.getTool("redact");return{...(null==o?void 0:o.defaults)??{},id:e.id,type:t.PdfAnnotationSubtype.REDACT,pageIndex:e.page,rect:e.rect,segmentRects:"text"===e.kind?e.rects:[],created:new Date}}annotationToRedactionItem(e){var t;const n=e.strokeColor??"#FF0000",o=e.color??"transparent";return e.segmentRects&&e.segmentRects.length>0?{id:e.id,kind:"text",page:e.pageIndex,rect:e.rect,rects:e.segmentRects,source:"annotation",markColor:n,redactionColor:o,...(null==(t=e.custom)?void 0:t.text)?{text:e.custom.text}:{}}:{id:e.id,kind:"area",page:e.pageIndex,rect:e.rect,source:"annotation",markColor:n,redactionColor:o}}emitPendingChange(e){const t=this.getDocumentState(e);t&&this.pending$.emit({documentId:e,pending:t.pending})}emitSelectedChange(e){const t=this.getDocumentState(e);t&&this.selected$.emit({documentId:e,selected:t.selected})}emitStateChange(e){const t=this.getDocumentState(e);t&&this.state$.emit({documentId:e,state:t})}onStoreUpdated(e,t){for(const n in t.documents){t.documents[n]&&(this.emitPendingChange(n),this.emitSelectedChange(n),this.emitStateChange(n))}}async destroy(){this.pending$.clear(),this.selected$.clear(),this.state$.clear(),this.events$.clear(),this.redactionSelection$.forEach(e=>e.clear()),this.redactionSelection$.clear(),this.redactionMarquee$.forEach(e=>e.clear()),this.redactionMarquee$.clear(),this.documentUnsubscribers.forEach(e=>{e.forEach(e=>e())}),this.documentUnsubscribers.clear(),await super.destroy()}};M.id="redaction";let S=M;const D="redaction",I={id:D,name:"Redaction Plugin",version:"1.0.0",provides:["redaction"],requires:[],optional:["interaction-manager","selection","annotation"],defaultConfig:{drawBlackBoxes:!0}},E={manifest:I,create:(e,t)=>new S(D,e,t),reducer:(e=P,t)=>{switch(t.type){case o:{const{documentId:n,state:o}=t.payload;return{...e,documents:{...e.documents,[n]:o},activeDocumentId:e.activeDocumentId??n}}case i:{const n=t.payload,{[n]:o,...i}=e.documents;return{...e,documents:i,activeDocumentId:e.activeDocumentId===n?null:e.activeDocumentId}}case"REDACTION/SET_ACTIVE_DOCUMENT":return{...e,activeDocumentId:t.payload};case s:{const{documentId:n,items:o}=t.payload,i=e.documents[n];if(!i)return e;const a={...i.pending};for(const e of o){const t=a[e.page]??[];t.some(t=>t.id===e.id)||(a[e.page]=t.concat(e))}return{...e,documents:{...e.documents,[n]:{...i,pending:a,pendingCount:R(a)}}}}case d:{const{documentId:n,page:o,id:i}=t.payload,a=e.documents[n];if(!a)return e;const c=(a.pending[o]??[]).filter(e=>e.id!==i),s={...a.pending,[o]:c},d=a.selected&&!(a.selected.page===o&&a.selected.id===i);return{...e,documents:{...e.documents,[n]:{...a,pending:s,pendingCount:R(s),selected:d?a.selected:null}}}}case r:{const{documentId:n,page:o,id:i,patch:a}=t.payload,c=e.documents[n];if(!c)return e;const s=(c.pending[o]??[]).map(e=>e.id===i?{...e,...a}:e);return{...e,documents:{...e.documents,[n]:{...c,pending:{...c.pending,[o]:s}}}}}case l:{const n=t.payload,o=e.documents[n];return o?{...e,documents:{...e.documents,[n]:{...o,pending:{},pendingCount:0,selected:null}}}:e}case u:{const{documentId:n,page:o,id:i}=t.payload,a=e.documents[n];return a?{...e,documents:{...e.documents,[n]:{...a,selected:{page:o,id:i}}}}:e}case g:{const n=t.payload,o=e.documents[n];return o?{...e,documents:{...e.documents,[n]:{...o,selected:null}}}:e}case a:{const{documentId:n,mode:o}=t.payload,i=e.documents[n];return i?{...e,documents:{...e.documents,[n]:{...i,isRedacting:!0,activeType:o}}}:e}case c:{const n=t.payload,o=e.documents[n];return o?{...e,documents:{...e.documents,[n]:{...o,isRedacting:!1,activeType:null}}}:e}case"SET_ACTIVE_TYPE":{const{documentId:n,mode:o}=t.payload,i=e.documents[n];return i?{...e,documents:{...e.documents,[n]:{...i,activeType:o}}}:e}default:return e}},initialState:P};exports.REDACTION_PLUGIN_ID=D,exports.RedactionMode=n,exports.RedactionPlugin=S,exports.RedactionPluginPackage=E,exports.getDocumentPendingCount=(e,t)=>{var n;return(null==(n=e.documents[t])?void 0:n.pendingCount)??0},exports.getPendingRedactionsCount=e=>e.pendingCount,exports.getTotalPendingCount=e=>Object.values(e.documents).reduce((e,t)=>e+t.pendingCount,0),exports.hasPendingRedactions=e=>e.pendingCount>0,exports.initialDocumentState=C,exports.initialState=P,exports.manifest=I,exports.redactTool=A,exports.redactTools=b;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|