@embedpdf/plugin-selection 2.6.1 → 2.6.2
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 +364 -30
- package/dist/index.js.map +1 -1
- package/dist/lib/actions.d.ts +10 -1
- package/dist/lib/handlers/text-selection.handler.d.ts +29 -6
- package/dist/lib/selection-plugin.d.ts +13 -0
- package/dist/lib/types.d.ts +18 -0
- package/dist/lib/utils.d.ts +44 -6
- package/package.json +9 -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"),n="selection",i={id:n,name:"Selection Plugin",version:"1.0.0",provides:["selection"],requires:["interaction-manager"],optional:["viewport","scroll"],defaultConfig:{menuHeight:40}},o="SELECTION/INIT_STATE",a="SELECTION/CLEANUP_STATE",s="SELECTION/CACHE_PAGE_GEOMETRY",c="SELECTION/SET_SELECTION",r="SELECTION/START_SELECTION",l="SELECTION/END_SELECTION",d="SELECTION/CLEAR_SELECTION",g="SELECTION/SET_RECTS",u="SELECTION/SET_SLICES",h={geometry:{},rects:{},slices:{},selection:null,active:!1,selecting:!1},p={documents:{}},m=(e,t,n)=>({...e,documents:{...e.documents,[t]:n}});function S(e,t){return e.rects[t]??[]}function y(e,n){return t.boundingRect(S(e,n))}function C(e){const n=[],i=e.rects;for(const o in i){const e=Number(o),a=t.boundingRect(i[e]);a&&n.push({page:e,rect:a})}return n}function b(e,t){const n=e.rects[t]||[];if(0===n.length)return null;const i=y(e,t);return i?{pageIndex:t,rect:i,segmentRects:n}:null}function f(e){const t=[],n=Object.keys(e.rects).map(Number);for(const i of n){const n=e.rects[i]||[];if(0===n.length)continue;const o=y(e,i);o&&t.push({pageIndex:i,rect:o,segmentRects:n})}return t}function x(e,t){for(const n of e.runs){if(!(t.y>=n.rect.y&&t.y<=n.rect.y+n.rect.height&&t.x>=n.rect.x&&t.x<=n.rect.x+n.rect.width))continue;const e=n.glyphs.findIndex(e=>t.x>=e.x&&t.x<=e.x+e.width&&t.y>=e.y&&t.y<=e.y+e.height);if(-1!==e)return n.charStart+e}return-1}function M(e,t,n){if(!e||!t)return null;if(n<e.start.page||n>e.end.page)return null;const i=n===e.start.page?e.start.index:0,o=t.runs[t.runs.length-1],a=o.charStart+o.glyphs.length-1;return{from:i,to:n===e.end.page?e.end.index:a}}function P(e,t,n,i=!0){const o=[];for(const a of e.runs){const e=a.charStart,i=e+a.glyphs.length-1;if(i<t||e>n)continue;const s=Math.max(t,e)-e,c=Math.min(n,i)-e;let r=1/0,l=-1/0,d=1/0,g=-1/0,u=0;for(let t=s;t<=c;t++){const e=a.glyphs[t];2!==e.flags&&(r=Math.min(r,e.x),l=Math.max(l,e.x+e.width),d=Math.min(d,e.y),g=Math.max(g,e.y+e.height),u++)}r!==1/0&&u>0&&o.push({rect:{origin:{x:r,y:d},size:{width:l-r,height:g-d}},charCount:u})}return i?w(o):o.map(e=>e.rect)}function E(e,t){const n=Math.min(e.origin.x,t.origin.x),i=Math.min(e.origin.y,t.origin.y);return{origin:{x:n,y:i},size:{width:Math.max(e.origin.x+e.size.width,t.origin.x+t.size.width)-n,height:Math.max(e.origin.y+e.size.height,t.origin.y+t.size.height)-i}}}function I(e,t){const n=Math.max(e.origin.x,t.origin.x),i=Math.max(e.origin.y,t.origin.y),o=Math.min(e.origin.x+e.size.width,t.origin.x+t.size.width),a=Math.min(e.origin.y+e.size.height,t.origin.y+t.size.height);return{origin:{x:n,y:i},size:{width:Math.max(0,o-n),height:Math.max(0,a-i)}}}function v(e){return e.size.width<=0||e.size.height<=0}function T(e,t){if(v(e)||v(t))return 0;const n=E(e,t);if(n.size.height===e.size.height||n.size.height===t.size.height)return 1;return I(e,t).size.height/n.size.height}function $(e,t){const n=e.rect,i=t.rect;if(T(n,i)<.8)return!1;const o=1*n.size.width/e.charCount,a=1*i.size.width/t.charCount,s=n.origin.x-o,c=n.origin.x+n.size.width+o,r=i.origin.x-a;return s<i.origin.x+i.size.width+a&&c>r}function w(e){const t=[];let n=null,i=null;for(const o of e)n&&i?$(n,o)?i=E(i,o.rect):(t.push(i),i=o.rect):i=o.rect,n=o;return i&&!v(i)&&t.push(i),t}const q=class extends e.BasePlugin{constructor(n,i,o){var a,s,c;super(n,i),this.enabledModesPerDoc=new Map,this.selecting=new Map,this.anchor=new Map,this.marqueePage=new Map,this.pageCallbacks=new Map,this.menuPlacement$=e.createScopedEmitter((e,t)=>({documentId:e,placement:t})),this.selChange$=e.createScopedEmitter((e,t)=>({documentId:e,selection:t,modeId:this.interactionManagerCapability.forDocument(e).getActiveMode()})),this.textRetrieved$=e.createScopedEmitter((e,t)=>({documentId:e,text:t})),this.copyToClipboard$=e.createScopedEmitter((e,t)=>({documentId:e,text:t}),{cache:!1}),this.beginSelection$=e.createScopedEmitter((e,t)=>({documentId:e,page:t.page,index:t.index,modeId:t.modeId}),{cache:!1}),this.endSelection$=e.createScopedEmitter((e,t)=>({documentId:e,modeId:t.modeId}),{cache:!1}),this.marqueeChange$=e.createScopedEmitter((e,t)=>({documentId:e,pageIndex:t.pageIndex,rect:t.rect,modeId:t.modeId}),{cache:!1}),this.marqueeEnd$=e.createScopedEmitter((e,t)=>({documentId:e,pageIndex:t.pageIndex,rect:t.rect,modeId:t.modeId}),{cache:!1}),this.emptySpaceClick$=e.createScopedEmitter((e,t)=>({documentId:e,pageIndex:t.pageIndex,modeId:t.modeId}),{cache:!1}),this.viewportCapability=null,this.scrollCapability=null,this.config=o,this.menuHeight=o.menuHeight??40;const r=i.getPlugin("interaction-manager");if(!r)throw new Error("SelectionPlugin: InteractionManagerPlugin is required.");this.interactionManagerCapability=r.provides(),this.viewportCapability=(null==(a=i.getPlugin("viewport"))?void 0:a.provides())??null,this.scrollCapability=(null==(s=i.getPlugin("scroll"))?void 0:s.provides())??null,this.coreStore.onAction(e.REFRESH_PAGES,e=>{const{documentId:n,pageIndexes:i}=e.payload,o=i.map(e=>this.getNewPageGeometryAndCache(n,e));t.Task.all(o).wait(()=>{i.forEach(e=>{this.notifyPage(n,e)})},t.ignore)}),null==(c=this.viewportCapability)||c.onViewportChange(e=>{this.recalculateMenuPlacement(e.documentId)},{mode:"throttle",wait:100})}onDocumentLoadingStarted(e){var t;this.dispatch(((e,t)=>({type:o,payload:{documentId:e,state:t}}))(e,h));const n=!1!==(null==(t=this.config.marquee)?void 0:t.enabled);this.enabledModesPerDoc.set(e,new Map([["pointerMode",{enableSelection:!0,showSelectionRects:!0,enableMarquee:n,showMarqueeRects:!0}]])),this.pageCallbacks.set(e,new Map),this.selecting.set(e,!1),this.anchor.set(e,void 0)}onDocumentClosed(e){this.dispatch((e=>({type:a,payload:e}))(e)),this.enabledModesPerDoc.delete(e),this.pageCallbacks.delete(e),this.selecting.delete(e),this.anchor.delete(e),this.marqueePage.delete(e),this.selChange$.clearScope(e),this.textRetrieved$.clearScope(e),this.copyToClipboard$.clearScope(e),this.beginSelection$.clearScope(e),this.endSelection$.clearScope(e),this.menuPlacement$.clearScope(e),this.marqueeChange$.clearScope(e),this.marqueeEnd$.clearScope(e),this.emptySpaceClick$.clearScope(e)}async initialize(){}async destroy(){this.selChange$.clear(),this.textRetrieved$.clear(),this.copyToClipboard$.clear(),this.beginSelection$.clear(),this.endSelection$.clear(),this.menuPlacement$.clear(),this.marqueeChange$.clear(),this.marqueeEnd$.clear(),this.emptySpaceClick$.clear(),super.destroy()}buildCapability(){const e=e=>e??this.getActiveDocumentId();return{getFormattedSelection:t=>f(this.getDocumentState(e(t))),getFormattedSelectionForPage:(t,n)=>b(this.getDocumentState(e(n)),t),getHighlightRectsForPage:(t,n)=>S(this.getDocumentState(e(n)),t),getHighlightRects:t=>this.getDocumentState(e(t)).rects,getBoundingRectForPage:(t,n)=>y(this.getDocumentState(e(n)),t),getBoundingRects:t=>C(this.getDocumentState(e(t))),getSelectedText:t=>this.getSelectedText(e(t)),clear:t=>this.clearSelection(e(t)),copyToClipboard:t=>this.copyToClipboard(e(t)),getState:t=>this.getDocumentState(e(t)),enableForMode:(t,n,i)=>{var o;return null==(o=this.enabledModesPerDoc.get(e(i)))?void 0:o.set(t,{...n})},isEnabledForMode:(t,n)=>{var i;return(null==(i=this.enabledModesPerDoc.get(e(n)))?void 0:i.has(t))??!1},setMarqueeEnabled:(t,n)=>this.setMarqueeEnabled(e(n),t),isMarqueeEnabled:t=>this.isMarqueeEnabled(e(t)),forDocument:this.createSelectionScope.bind(this),onCopyToClipboard:this.copyToClipboard$.onGlobal,onSelectionChange:this.selChange$.onGlobal,onTextRetrieved:this.textRetrieved$.onGlobal,onBeginSelection:this.beginSelection$.onGlobal,onEndSelection:this.endSelection$.onGlobal,onMarqueeChange:this.marqueeChange$.onGlobal,onMarqueeEnd:this.marqueeEnd$.onGlobal,onEmptySpaceClick:this.emptySpaceClick$.onGlobal}}createSelectionScope(e){return{getFormattedSelection:()=>f(this.getDocumentState(e)),getFormattedSelectionForPage:t=>b(this.getDocumentState(e),t),getHighlightRectsForPage:t=>S(this.getDocumentState(e),t),getHighlightRects:()=>this.getDocumentState(e).rects,getBoundingRectForPage:t=>y(this.getDocumentState(e),t),getBoundingRects:()=>C(this.getDocumentState(e)),getSelectedText:()=>this.getSelectedText(e),clear:()=>this.clearSelection(e),copyToClipboard:()=>this.copyToClipboard(e),getState:()=>this.getDocumentState(e),setMarqueeEnabled:t=>this.setMarqueeEnabled(e,t),isMarqueeEnabled:()=>this.isMarqueeEnabled(e),onSelectionChange:this.selChange$.forScope(e),onTextRetrieved:this.textRetrieved$.forScope(e),onCopyToClipboard:this.copyToClipboard$.forScope(e),onBeginSelection:this.beginSelection$.forScope(e),onEndSelection:this.endSelection$.forScope(e),onMarqueeChange:this.marqueeChange$.forScope(e),onMarqueeEnd:this.marqueeEnd$.forScope(e),onEmptySpaceClick:this.emptySpaceClick$.forScope(e)}}getDocumentState(e){const t=this.state.documents[e];if(!t)throw new Error(`Selection state not found for document: ${e}`);return t}onMenuPlacement(e,t){return this.menuPlacement$.forScope(e)(t)}registerSelectionOnPage(e){var n;const{documentId:i,pageIndex:o,onRectsChange:a}=e,s=this.state.documents[i];if(!s)return this.logger.warn("SelectionPlugin","RegisterFailed",`Cannot register selection on page ${o} for document ${i}: document state not initialized.`),()=>{};null==(n=this.pageCallbacks.get(i))||n.set(o,a);const c=this.getOrLoadGeometry(i,o),r=this.interactionManagerCapability.forDocument(i),l=this.enabledModesPerDoc.get(i);a({rects:S(s,o),boundingRect:y(s,o)});const d=function(e){return{onPointerDown:(t,n,i)=>{var o;if(n.target===n.currentTarget&&(null==(o=e.onEmptySpaceClick)||o.call(e,i)),!e.isEnabled(i))return;e.onClear(i);const a=e.getGeometry();if(a){const n=x(a,t);-1!==n&&e.onBegin(n,i)}},onPointerMove:(t,n,i)=>{if(!e.isEnabled(i))return;const o=e.getGeometry();if(o){const n=x(o,t);e.setCursor(-1!==n?"text":null),e.isSelecting()&&-1!==n&&e.onUpdate(n,i)}},onPointerUp:(t,n,i)=>{e.isEnabled(i)&&e.onEnd(i)},onHandlerActiveEnd:t=>{e.isEnabled(t)&&e.onClear(t)}}}({getGeometry:()=>this.getDocumentState(i).geometry[o],isEnabled:e=>{const t=null==l?void 0:l.get(e);return!!t&&!1!==t.enableSelection},onBegin:(e,t)=>this.beginSelection(i,o,e,t),onUpdate:(e,t)=>this.updateSelection(i,o,e,t),onEnd:e=>this.endSelection(i,e),onClear:e=>this.clearSelection(i,e),isSelecting:()=>this.selecting.get(i)??!1,setCursor:e=>e?r.setCursor("selection-text",e,10):r.removeCursor("selection-text"),onEmptySpaceClick:e=>this.emptySpaceClick$.emit(i,{pageIndex:o,modeId:e})}),g=this.interactionManagerCapability.registerAlways({scope:{type:"page",documentId:i,pageIndex:o},handlers:d});return()=>{var e;g(),null==(e=this.pageCallbacks.get(i))||e.delete(o),c.abort({code:t.PdfErrorCode.Cancelled,message:"Cleanup"})}}registerMarqueeOnPage(t){var n;const{documentId:i,pageIndex:o,scale:a,onRectChange:s}=t;if(!this.state.documents[i])return this.logger.warn("SelectionPlugin","RegisterMarqueeFailed",`Cannot register marquee on page ${o} for document ${i}: document state not initialized.`),()=>{};const c=this.coreState.core.documents[i];if(!c||!c.document)return this.logger.warn("SelectionPlugin","DocumentNotFound",`Cannot register marquee on page ${o}: document not found`),()=>{};const r=c.document.pages[o];if(!r)return this.logger.warn("SelectionPlugin","PageNotFound",`Cannot register marquee on page ${o}: page not found`),()=>{};const l=r.size,d=(null==(n=this.config.marquee)?void 0:n.minDragPx)??5,g=()=>{var e;const t=this.interactionManagerCapability.forDocument(i).getActiveMode(),n=null==(e=this.enabledModesPerDoc.get(i))?void 0:e.get(t);return!1!==(null==n?void 0:n.showMarqueeRects)},u=function(t){const{pageSize:n,scale:i,minDragPx:o=5}=t;let a=null,s=null;return{onPointerDown:(e,n,i)=>{var o,c;t.isEnabled(i)&&((null==(o=t.isTextSelecting)?void 0:o.call(t))||(a=e,s={origin:{x:e.x,y:e.y},size:{width:0,height:0}},t.onBegin(e,i),null==(c=n.setPointerCapture)||c.call(n)))},onPointerMove:(i,o,c)=>{if(!a||!t.isEnabled(c))return;const r=e.clamp(i.x,0,n.width),l=e.clamp(i.y,0,n.height);s={origin:{x:Math.min(a.x,r),y:Math.min(a.y,l)},size:{width:Math.abs(r-a.x),height:Math.abs(l-a.y)}},t.onChange(s,c)},onPointerUp:(e,n,c)=>{var r;t.isEnabled(c)&&(s&&a&&(Math.max(s.size.width,s.size.height)*i>o?t.onEnd(s,c):t.onCancel(c)),a=null,s=null,null==(r=n.releasePointerCapture)||r.call(n))},onPointerCancel:(e,n,i)=>{var o;t.isEnabled(i)&&(a=null,s=null,t.onCancel(i),null==(o=n.releasePointerCapture)||o.call(n))}}}({pageSize:l,scale:a,minDragPx:d,isEnabled:e=>{var t;const n=null==(t=this.enabledModesPerDoc.get(i))?void 0:t.get(e);return!0===(null==n?void 0:n.enableMarquee)},isTextSelecting:()=>this.selecting.get(i)??!1,onBegin:(e,t)=>this.beginMarquee(i,o,e,t),onChange:(e,t)=>{this.updateMarquee(i,o,e,t),s(g()?e:null)},onEnd:(e,t)=>{this.endMarquee(i,o,e,t),s(null)},onCancel:e=>{this.cancelMarquee(i,e),s(null)}});return this.interactionManagerCapability.registerAlways({scope:{type:"page",documentId:i,pageIndex:o},handlers:u})}getPlacementMetrics(e,t,n,i){var o;const a=null==(o=this.scrollCapability)?void 0:o.forDocument(e),s=null==a?void 0:a.getRectPositionForPage(t,n);if(!s)return null;const c=s.origin.y-i.scrollTop,r=s.origin.y+s.size.height-i.scrollTop;return{pageIndex:t,rect:n,spaceAbove:c,spaceBelow:i.clientHeight-r,isBottomVisible:r>0&&r<=i.clientHeight,isTopVisible:c>=0&&c<i.clientHeight}}emitMenuPlacement(e,t){this.menuPlacement$.emit(e,t),t?this.interactionManagerCapability.claimPageActivity(e,"selection-menu",t.pageIndex):this.interactionManagerCapability.releasePageActivity(e,"selection-menu")}recalculateMenuPlacement(e){const t=this.state.documents[e];if(!t)return;if(t.selecting||null===t.selection)return void this.emitMenuPlacement(e,null);const n=C(t);if(0===n.length)return void this.emitMenuPlacement(e,null);const i=n[n.length-1];if(!this.viewportCapability||!this.scrollCapability)return void this.emitMenuPlacement(e,{pageIndex:i.page,rect:i.rect,spaceAbove:0,spaceBelow:1/0,suggestTop:!1,isVisible:!0});const o=this.viewportCapability.forDocument(e).getMetrics(),a=n[0],s=this.getPlacementMetrics(e,i.page,i.rect,o),c=this.getPlacementMetrics(e,a.page,a.rect,o);s&&s.isBottomVisible&&s.spaceBelow>this.menuHeight?this.emitMenuPlacement(e,{...s,suggestTop:!1,isVisible:!0}):c&&c.isTopVisible?this.emitMenuPlacement(e,{...c,suggestTop:!0,isVisible:!0}):s&&s.isBottomVisible?this.emitMenuPlacement(e,{...s,suggestTop:!1,isVisible:!0}):this.emitMenuPlacement(e,null)}notifyPage(e,t){var n,i;const o=null==(n=this.pageCallbacks.get(e))?void 0:n.get(t);if(o){const n=this.getDocumentState(e),a=this.interactionManagerCapability.forDocument(e).getActiveMode(),s=null==(i=this.enabledModesPerDoc.get(e))?void 0:i.get(a);o(s&&!1!==(s.showSelectionRects??s.showRects)?{rects:S(n,t),boundingRect:y(n,t)}:{rects:[],boundingRect:null})}}notifyAllPages(e){var t;null==(t=this.pageCallbacks.get(e))||t.forEach((t,n)=>{this.notifyPage(e,n)})}getNewPageGeometryAndCache(e,n){const i=this.getCoreDocument(e);if(!i||!i.document)return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Doc Not Found"});const o=i.document.pages.find(e=>e.index===n),a=this.engine.getPageGeometry(i.document,o);return a.wait(t=>{this.dispatch(((e,t,n)=>({type:s,payload:{documentId:e,page:t,geo:n}}))(e,n,t))},t.ignore),a}getOrLoadGeometry(e,n){const i=this.getDocumentState(e).geometry[n];return i?t.PdfTaskHelper.resolve(i):this.getNewPageGeometryAndCache(e,n)}beginSelection(e,t,n,i){this.selecting.set(e,!0),this.anchor.set(e,{page:t,index:n}),this.dispatch((e=>({type:r,payload:{documentId:e}}))(e)),this.beginSelection$.emit(e,{page:t,index:n,modeId:i}),this.recalculateMenuPlacement(e)}endSelection(e,t){this.selecting.set(e,!1),this.anchor.set(e,void 0),this.dispatch((e=>({type:l,payload:{documentId:e}}))(e)),this.endSelection$.emit(e,{modeId:t}),this.recalculateMenuPlacement(e)}clearSelection(e,t){this.selecting.set(e,!1),this.anchor.set(e,void 0),this.dispatch((e=>({type:d,payload:{documentId:e}}))(e)),this.selChange$.emit(e,null),this.emitMenuPlacement(e,null),this.notifyAllPages(e)}updateSelection(e,t,n,i){if(!this.selecting.get(e)||!this.anchor.get(e))return;const o=this.anchor.get(e),a=t>o.page||t===o.page&&n>=o.index,s={start:a?o:{page:t,index:n},end:a?{page:t,index:n}:o};this.dispatch(((e,t)=>({type:c,payload:{documentId:e,selection:t}}))(e,s)),this.updateRectsAndSlices(e,s),this.selChange$.emit(e,s);for(let c=s.start.page;c<=s.end.page;c++)this.notifyPage(e,c)}updateRectsAndSlices(e,t){const n=this.getDocumentState(e),i={},o={};for(let a=t.start.page;a<=t.end.page;a++){const e=n.geometry[a],s=M(t,e,a);s&&(i[a]=P(e,s.from,s.to),o[a]={start:s.from,count:s.to-s.from+1})}this.dispatch(((e,t)=>({type:g,payload:{documentId:e,rects:t}}))(e,i)),this.dispatch(((e,t)=>({type:u,payload:{documentId:e,slices:t}}))(e,o))}getSelectedText(e){if(!this.checkPermission(e,t.PdfPermissionFlag.CopyContents))return this.logger.debug("SelectionPlugin","GetSelectedText",`Cannot get selected text: document ${e} lacks CopyContents permission`),t.PdfTaskHelper.reject({code:t.PdfErrorCode.Security,message:"Document lacks CopyContents permission"});const n=this.getCoreDocument(e),i=this.getDocumentState(e);if(!(null==n?void 0:n.document)||!i.selection)return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Doc Not Found or No Selection"});const o=i.selection,a=[];for(let t=o.start.page;t<=o.end.page;t++){const e=i.slices[t];e&&a.push({pageIndex:t,charIndex:e.start,charCount:e.count})}if(0===a.length)return t.PdfTaskHelper.resolve([]);const s=this.engine.getTextSlices(n.document,a);return s.wait(t=>{this.textRetrieved$.emit(e,t)},t.ignore),s}copyToClipboard(e){if(!this.checkPermission(e,t.PdfPermissionFlag.CopyContents))return void this.logger.debug("SelectionPlugin","CopyToClipboard",`Cannot copy to clipboard: document ${e} lacks CopyContents permission`);this.getSelectedText(e).wait(t=>{this.copyToClipboard$.emit(e,t.join("\n"))},t.ignore)}beginMarquee(e,t,n,i){this.marqueePage.set(e,t)}updateMarquee(e,t,n,i){this.marqueeChange$.emit(e,{pageIndex:t,rect:n,modeId:i})}endMarquee(e,t,n,i){this.marqueeEnd$.emit(e,{pageIndex:t,rect:n,modeId:i}),this.marqueeChange$.emit(e,{pageIndex:t,rect:null,modeId:i}),this.marqueePage.delete(e)}cancelMarquee(e,t){const n=this.marqueePage.get(e);void 0!==n&&(this.marqueeChange$.emit(e,{pageIndex:n,rect:null,modeId:t}),this.marqueePage.delete(e))}setMarqueeEnabled(e,t){const n=this.enabledModesPerDoc.get(e);if(!n)return;const i=n.get("pointerMode");i?i.enableMarquee=t:t&&n.set("pointerMode",{enableMarquee:!0})}isMarqueeEnabled(e){var t;const n=null==(t=this.enabledModesPerDoc.get(e))?void 0:t.get("pointerMode");return!1!==(null==n?void 0:n.enableMarquee)}};q.id="selection";let D=q;const R={manifest:i,create:(e,t)=>new D(n,e,t),reducer:(e=p,t)=>{switch(t.type){case o:{const{documentId:n,state:i}=t.payload;return m(e,n,i)}case a:{const n=t.payload,{[n]:i,...o}=e.documents;return{...e,documents:o}}case s:{const{documentId:n,page:i,geo:o}=t.payload,a=e.documents[n];return a?m(e,n,{...a,geometry:{...a.geometry,[i]:o}}):e}case c:{const{documentId:n,selection:i}=t.payload,o=e.documents[n];return o?m(e,n,{...o,selection:i,active:!0}):e}case r:{const{documentId:n}=t.payload,i=e.documents[n];return i?m(e,n,{...i,selecting:!0,selection:null,rects:{}}):e}case l:{const{documentId:n}=t.payload,i=e.documents[n];return i?m(e,n,{...i,selecting:!1}):e}case d:{const{documentId:n}=t.payload,i=e.documents[n];return i?m(e,n,{...i,selecting:!1,selection:null,rects:{},active:!1}):e}case g:{const{documentId:n,rects:i}=t.payload,o=e.documents[n];return o?m(e,n,{...o,rects:i}):e}case u:{const{documentId:n,slices:i}=t.payload,o=e.documents[n];return o?m(e,n,{...o,slices:i}):e}case"SELECTION/RESET":{const{documentId:n}=t.payload;return e.documents[n]?m(e,n,h):e}default:return e}},initialState:p};exports.SELECTION_PLUGIN_ID=n,exports.SelectionPlugin=D,exports.SelectionPluginPackage=R,exports.getVerticalOverlap=T,exports.glyphAt=x,exports.manifest=i,exports.mergeAdjacentRects=w,exports.rectIntersect=I,exports.rectIsEmpty=v,exports.rectUnion=E,exports.rectsWithinSlice=P,exports.shouldMergeHorizontalRects=$,exports.sliceBounds=M;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("@embedpdf/core"),t=require("@embedpdf/models"),n="selection",i={id:n,name:"Selection Plugin",version:"1.0.0",provides:["selection"],requires:["interaction-manager"],optional:["viewport","scroll"],defaultConfig:{menuHeight:40}},o="SELECTION/INIT_STATE",s="SELECTION/CLEANUP_STATE",r="SELECTION/CACHE_PAGE_GEOMETRY",c="SELECTION/SET_SELECTION",a="SELECTION/START_SELECTION",l="SELECTION/END_SELECTION",d="SELECTION/CLEAR_SELECTION",h="SELECTION/SET_RECTS",u="SELECTION/SET_SLICES",g="SELECTION/EVICT_PAGE_GEOMETRY",p=(e,t)=>({type:c,payload:{documentId:e,selection:t}}),m=e=>({type:a,payload:{documentId:e}}),f=e=>({type:l,payload:{documentId:e}}),S=(e,t)=>({type:h,payload:{documentId:e,rects:t}}),y=(e,t)=>({type:u,payload:{documentId:e,slices:t}}),x={geometry:{},rects:{},slices:{},selection:null,active:!1,selecting:!1},C={documents:{}},b=(e,t,n)=>({...e,documents:{...e.documents,[t]:n}});function M(e,t){return e.rects[t]??[]}function I(e,n){return t.boundingRect(M(e,n))}function E(e){const n=[],i=e.rects;for(const o in i){const e=Number(o),s=t.boundingRect(i[e]);s&&n.push({page:e,rect:s})}return n}function P(e,t){const n=e.rects[t]||[];if(0===n.length)return null;const i=I(e,t);return i?{pageIndex:t,rect:i,segmentRects:n}:null}function v(e){const t=[],n=Object.keys(e.rects).map(Number);for(const i of n){const n=e.rects[i]||[];if(0===n.length)continue;const o=I(e,i);o&&t.push({pageIndex:i,rect:o,segmentRects:n})}return t}function T(e,t,n=1.5){for(const c of e.runs){if(!(t.y>=c.rect.y&&t.y<=c.rect.y+c.rect.height&&t.x>=c.rect.x&&t.x<=c.rect.x+c.rect.width))continue;const e=c.glyphs.findIndex(e=>{const n=e.tightX??e.x,i=e.tightY??e.y,o=e.tightWidth??e.width,s=e.tightHeight??e.height;return t.x>=n&&t.x<=n+o&&t.y>=i&&t.y<=i+s});if(-1!==e)return c.charStart+e}if(n<=0)return-1;const i=function(e,t){let n=0,i=0;for(const o of e.runs)for(const e of o.glyphs)2!==e.flags&&(n+=e.height,i++);return 0===i?0:n/i*t}(e,n),o=i/2;let s=-1,r=1/0;for(const c of e.runs)if(!(t.y<c.rect.y-o||t.y>c.rect.y+c.rect.height+o||t.x<c.rect.x-o||t.x>c.rect.x+c.rect.width+o))for(let e=0;e<c.glyphs.length;e++){const n=c.glyphs[e];if(2===n.flags)continue;const i=n.tightX??n.x,a=n.tightY??n.y,l=n.tightWidth??n.width,d=n.tightHeight??n.height,h=i-o,u=i+l+o,g=a-o,p=a+d+o;if(t.x<h||t.x>u||t.y<g||t.y>p)continue;const m=Math.min(Math.abs(t.x-i),Math.abs(t.x-(i+l)))+Math.min(Math.abs(t.y-a),Math.abs(t.y-(a+d)));m<r&&(r=m,s=c.charStart+e)}return s}function w(e,t,n){if(!e||!t)return null;if(n<e.start.page||n>e.end.page)return null;const i=n===e.start.page?e.start.index:0,o=t.runs[t.runs.length-1],s=o.charStart+o.glyphs.length-1;return{from:i,to:n===e.end.page?e.end.index:s}}function $(e,t,n,i=!0){const o=[];for(const s of e.runs){const e=s.charStart,i=e+s.glyphs.length-1;if(i<t||e>n)continue;const r=Math.max(t,e)-e,c=Math.min(n,i)-e;let a=1/0,l=-1/0,d=1/0,h=-1/0,u=0,g=0,p=-1/0;const m=()=>{a!==1/0&&u>0&&o.push({rect:{origin:{x:a,y:d},size:{width:l-a,height:h-d}},charCount:u,fontSize:s.fontSize}),a=1/0,l=-1/0,d=1/0,h=-1/0,u=0,g=0,p=-1/0};for(let t=r;t<=c;t++){const e=s.glyphs[t];if(2!==e.flags){if(u>0&&p>-1/0){const t=Math.abs(e.x-p),n=g/u;n>0&&t>2.5*n&&m()}a=Math.min(a,e.x),l=Math.max(l,e.x+e.width),d=Math.min(d,e.y),h=Math.max(h,e.y+e.height),u++,g+=e.width,p=e.x+e.width}}m()}return i?k(o):o.map(e=>e.rect)}function D(e,t){const n=Math.min(e.origin.x,t.origin.x),i=Math.min(e.origin.y,t.origin.y);return{origin:{x:n,y:i},size:{width:Math.max(e.origin.x+e.size.width,t.origin.x+t.size.width)-n,height:Math.max(e.origin.y+e.size.height,t.origin.y+t.size.height)-i}}}function q(e,t){const n=Math.max(e.origin.x,t.origin.x),i=Math.max(e.origin.y,t.origin.y),o=Math.min(e.origin.x+e.size.width,t.origin.x+t.size.width),s=Math.min(e.origin.y+e.size.height,t.origin.y+t.size.height);return{origin:{x:n,y:i},size:{width:Math.max(0,o-n),height:Math.max(0,s-i)}}}function R(e){return e.size.width<=0||e.size.height<=0}function z(e,t){if(R(e)||R(t))return 0;const n=D(e,t);if(n.size.height===e.size.height||n.size.height===t.size.height)return 1;return q(e,t).size.height/n.size.height}function A(e,t){if(null!=e.fontSize&&null!=t.fontSize&&e.fontSize>0&&t.fontSize>0){if(Math.max(e.fontSize,t.fontSize)/Math.min(e.fontSize,t.fontSize)>1.5)return!1}const n=e.rect,i=t.rect;if(z(n,i)<.8)return!1;const o=1*n.size.width/e.charCount,s=1*i.size.width/t.charCount,r=n.origin.x-o,c=n.origin.x+n.size.width+o,a=i.origin.x-s;return r<i.origin.x+i.size.width+s&&c>a}function k(e){const t=[];let n=null,i=null;for(const o of e)n&&i?A(n,o)?i=D(i,o.rect):(t.push(i),i=o.rect):i=o.rect,n=o;return i&&!R(i)&&t.push(i),t}function N(e,t){for(let n=0;n<e.runs.length;n++){const i=e.runs[n],o=t-i.charStart;if(o>=0&&o<i.glyphs.length)return{runIdx:n,localIdx:o}}return null}function G(e){return 1===e||2===e}function O(e,t){if(!N(e,t))return null;const n=function(e){if(0===e.runs.length)return 0;const t=e.runs[e.runs.length-1];return t.charStart+t.glyphs.length}(e);if(0===n)return null;let i=t;for(;i>0;){const t=N(e,i-1);if(!t)break;if(G(e.runs[t.runIdx].glyphs[t.localIdx].flags))break;i--}let o=t;for(;o<n-1;){const t=N(e,o+1);if(!t)break;if(G(e.runs[t.runIdx].glyphs[t.localIdx].flags))break;o++}return{from:i,to:o}}function L(e,t){const n=N(e,t);if(!n)return null;const i=e.runs[n.runIdx],o=i.rect.y,s=i.rect.y+i.rect.height;let r=i.charStart,c=i.charStart+i.glyphs.length-1;for(let a=n.runIdx-1;a>=0;a--){const t=e.runs[a];if(!F(t)){if(!H(t.rect.y,t.rect.y+t.rect.height,o,s))break;r=t.charStart}}for(let a=n.runIdx+1;a<e.runs.length;a++){const t=e.runs[a];if(!F(t)){if(!H(t.rect.y,t.rect.y+t.rect.height,o,s))break;c=t.charStart+t.glyphs.length-1}}return{from:r,to:c}}function F(e){return 0===e.rect.width&&0===e.rect.height}function H(e,t,n,i){const o=Math.max(t,i)-Math.min(e,n),s=Math.max(0,Math.min(t,i)-Math.max(e,n));return 0!==o&&s/o>=.5}const B=class extends e.BasePlugin{constructor(n,i,o){var s,r,c;super(n,i),this.enabledModesPerDoc=new Map,this.selecting=new Map,this.anchor=new Map,this.hasTextAnchor=new Map,this.marqueePage=new Map,this.pageCallbacks=new Map,this.geoAccessOrder=new Map,this.menuPlacement$=e.createScopedEmitter((e,t)=>({documentId:e,placement:t})),this.selChange$=e.createScopedEmitter((e,t)=>({documentId:e,selection:t,modeId:this.interactionManagerCapability.forDocument(e).getActiveMode()})),this.textRetrieved$=e.createScopedEmitter((e,t)=>({documentId:e,text:t})),this.copyToClipboard$=e.createScopedEmitter((e,t)=>({documentId:e,text:t}),{cache:!1}),this.beginSelection$=e.createScopedEmitter((e,t)=>({documentId:e,page:t.page,index:t.index,modeId:t.modeId}),{cache:!1}),this.endSelection$=e.createScopedEmitter((e,t)=>({documentId:e,modeId:t.modeId}),{cache:!1}),this.marqueeChange$=e.createScopedEmitter((e,t)=>({documentId:e,pageIndex:t.pageIndex,rect:t.rect,modeId:t.modeId}),{cache:!1}),this.marqueeEnd$=e.createScopedEmitter((e,t)=>({documentId:e,pageIndex:t.pageIndex,rect:t.rect,modeId:t.modeId}),{cache:!1}),this.emptySpaceClick$=e.createScopedEmitter((e,t)=>({documentId:e,pageIndex:t.pageIndex,modeId:t.modeId}),{cache:!1}),this.viewportCapability=null,this.scrollCapability=null,this.config=o,this.menuHeight=o.menuHeight??40;const a=i.getPlugin("interaction-manager");if(!a)throw new Error("SelectionPlugin: InteractionManagerPlugin is required.");this.interactionManagerCapability=a.provides(),this.viewportCapability=(null==(s=i.getPlugin("viewport"))?void 0:s.provides())??null,this.scrollCapability=(null==(r=i.getPlugin("scroll"))?void 0:r.provides())??null,this.coreStore.onAction(e.REFRESH_PAGES,e=>{const{documentId:n,pageIndexes:i}=e.payload,o=i.map(e=>this.getNewPageGeometryAndCache(n,e));t.Task.all(o).wait(()=>{i.forEach(e=>{this.notifyPage(n,e)})},t.ignore)}),null==(c=this.viewportCapability)||c.onViewportChange(e=>{this.recalculateMenuPlacement(e.documentId)},{mode:"throttle",wait:100})}onDocumentLoadingStarted(e){var t;this.dispatch(((e,t)=>({type:o,payload:{documentId:e,state:t}}))(e,x));const n=!1!==(null==(t=this.config.marquee)?void 0:t.enabled);this.enabledModesPerDoc.set(e,new Map([["pointerMode",{enableSelection:!0,showSelectionRects:!0,enableMarquee:n,showMarqueeRects:!0}]])),this.pageCallbacks.set(e,new Map),this.geoAccessOrder.set(e,[]),this.selecting.set(e,!1),this.anchor.set(e,void 0),this.hasTextAnchor.set(e,!1)}onDocumentClosed(e){this.dispatch((e=>({type:s,payload:e}))(e)),this.enabledModesPerDoc.delete(e),this.pageCallbacks.delete(e),this.geoAccessOrder.delete(e),this.selecting.delete(e),this.hasTextAnchor.delete(e),this.anchor.delete(e),this.marqueePage.delete(e),this.selChange$.clearScope(e),this.textRetrieved$.clearScope(e),this.copyToClipboard$.clearScope(e),this.beginSelection$.clearScope(e),this.endSelection$.clearScope(e),this.menuPlacement$.clearScope(e),this.marqueeChange$.clearScope(e),this.marqueeEnd$.clearScope(e),this.emptySpaceClick$.clearScope(e)}async initialize(){}async destroy(){this.selChange$.clear(),this.textRetrieved$.clear(),this.copyToClipboard$.clear(),this.beginSelection$.clear(),this.endSelection$.clear(),this.menuPlacement$.clear(),this.marqueeChange$.clear(),this.marqueeEnd$.clear(),this.emptySpaceClick$.clear(),super.destroy()}buildCapability(){const e=e=>e??this.getActiveDocumentId();return{getFormattedSelection:t=>v(this.getDocumentState(e(t))),getFormattedSelectionForPage:(t,n)=>P(this.getDocumentState(e(n)),t),getHighlightRectsForPage:(t,n)=>M(this.getDocumentState(e(n)),t),getHighlightRects:t=>this.getDocumentState(e(t)).rects,getBoundingRectForPage:(t,n)=>I(this.getDocumentState(e(n)),t),getBoundingRects:t=>E(this.getDocumentState(e(t))),getSelectedText:t=>this.getSelectedText(e(t)),clear:t=>this.clearSelection(e(t)),copyToClipboard:t=>this.copyToClipboard(e(t)),getState:t=>this.getDocumentState(e(t)),enableForMode:(t,n,i)=>{var o;return null==(o=this.enabledModesPerDoc.get(e(i)))?void 0:o.set(t,{...n})},isEnabledForMode:(t,n)=>{var i;return(null==(i=this.enabledModesPerDoc.get(e(n)))?void 0:i.has(t))??!1},setMarqueeEnabled:(t,n)=>this.setMarqueeEnabled(e(n),t),isMarqueeEnabled:t=>this.isMarqueeEnabled(e(t)),forDocument:this.createSelectionScope.bind(this),onCopyToClipboard:this.copyToClipboard$.onGlobal,onSelectionChange:this.selChange$.onGlobal,onTextRetrieved:this.textRetrieved$.onGlobal,onBeginSelection:this.beginSelection$.onGlobal,onEndSelection:this.endSelection$.onGlobal,onMarqueeChange:this.marqueeChange$.onGlobal,onMarqueeEnd:this.marqueeEnd$.onGlobal,onEmptySpaceClick:this.emptySpaceClick$.onGlobal}}createSelectionScope(e){return{getFormattedSelection:()=>v(this.getDocumentState(e)),getFormattedSelectionForPage:t=>P(this.getDocumentState(e),t),getHighlightRectsForPage:t=>M(this.getDocumentState(e),t),getHighlightRects:()=>this.getDocumentState(e).rects,getBoundingRectForPage:t=>I(this.getDocumentState(e),t),getBoundingRects:()=>E(this.getDocumentState(e)),getSelectedText:()=>this.getSelectedText(e),clear:()=>this.clearSelection(e),copyToClipboard:()=>this.copyToClipboard(e),getState:()=>this.getDocumentState(e),setMarqueeEnabled:t=>this.setMarqueeEnabled(e,t),isMarqueeEnabled:()=>this.isMarqueeEnabled(e),onSelectionChange:this.selChange$.forScope(e),onTextRetrieved:this.textRetrieved$.forScope(e),onCopyToClipboard:this.copyToClipboard$.forScope(e),onBeginSelection:this.beginSelection$.forScope(e),onEndSelection:this.endSelection$.forScope(e),onMarqueeChange:this.marqueeChange$.forScope(e),onMarqueeEnd:this.marqueeEnd$.forScope(e),onEmptySpaceClick:this.emptySpaceClick$.forScope(e)}}getDocumentState(e){const t=this.state.documents[e];if(!t)throw new Error(`Selection state not found for document: ${e}`);return t}onMenuPlacement(e,t){return this.menuPlacement$.forScope(e)(t)}registerSelectionOnPage(e){var n;const{documentId:i,pageIndex:o,onRectsChange:s}=e,r=this.state.documents[i];if(!r)return this.logger.warn("SelectionPlugin","RegisterFailed",`Cannot register selection on page ${o} for document ${i}: document state not initialized.`),()=>{};null==(n=this.pageCallbacks.get(i))||n.set(o,s);const c=this.getOrLoadGeometry(i,o),a=this.interactionManagerCapability.forDocument(i),l=this.enabledModesPerDoc.get(i);s({rects:M(r,o),boundingRect:I(r,o)}),c.wait(e=>{const t=this.getDocumentState(i),n=t.selection;if(!n||o<n.start.page||o>n.end.page)return;const s=w(n,e,o);if(!s)return;const r=$(e,s.from,s.to);this.dispatch(S(i,{...t.rects,[o]:r})),this.dispatch(y(i,{...t.slices,[o]:{start:s.from,count:s.to-s.from+1}})),this.notifyPage(i,o)},t.ignore);const d=function(e){const t=e.minDragDistance??3,n=e.toleranceFactor??1.5;let i=null,o=null,s=!1,r=0;function c(){var t;i=null,o=null,s=!1,null==(t=e.setHasTextAnchor)||t.call(e,!1)}return{onPointerDown:(t,c,a)=>{var l,d;if(c.target===c.currentTarget&&(null==(l=e.onEmptySpaceClick)||l.call(e,a)),!e.isEnabled(a))return;const h=Date.now();(0===r||h-r>=500)&&e.onClear(a);const u=e.getGeometry();if(!u)return;const g=T(u,t,n);-1!==g&&(i=g,o=t,s=!1,null==(d=e.setHasTextAnchor)||d.call(e,!0))},onPointerMove:(r,c,a)=>{if(!e.isEnabled(a))return;const l=e.getGeometry();if(!l)return;const d=T(l,r,n);if(e.setCursor(-1!==d?"text":null),null!==i&&o&&!s){const n=r.x-o.x,c=r.y-o.y;return void(Math.sqrt(n*n+c*c)>=t&&(s=!0,e.onBegin(i,a),-1!==d&&e.onUpdate(d,a)))}e.isSelecting()&&-1!==d&&e.onUpdate(d,a)},onPointerUp:(t,n,i)=>{e.isEnabled(i)?(s&&e.onEnd(i),c()):c()},onDoubleClick:(t,i,o)=>{var s;if(!e.isEnabled(o))return;const c=e.getGeometry();if(!c)return;const a=T(c,t,n);-1!==a&&(r=Date.now(),null==(s=e.onWordSelect)||s.call(e,a,o))},onClick:(t,i,o)=>{var s;if(!e.isEnabled(o))return;if(0===r)return;if(Date.now()-r>500)return void(r=0);r=0;const c=e.getGeometry();if(!c)return;const a=T(c,t,n);-1!==a&&(null==(s=e.onLineSelect)||s.call(e,a,o))},onHandlerActiveEnd:t=>{c(),e.isEnabled(t)&&e.onClear(t)}}}({getGeometry:()=>this.getDocumentState(i).geometry[o],isEnabled:e=>{const t=null==l?void 0:l.get(e);return!!t&&!1!==t.enableSelection},onBegin:(e,t)=>this.beginSelection(i,o,e,t),onUpdate:(e,t)=>this.updateSelection(i,o,e,t),onEnd:e=>this.endSelection(i,e),onClear:e=>this.clearSelection(i,e),isSelecting:()=>this.selecting.get(i)??!1,setCursor:e=>e?a.setCursor("selection-text",e,10):a.removeCursor("selection-text"),onEmptySpaceClick:e=>this.emptySpaceClick$.emit(i,{pageIndex:o,modeId:e}),onWordSelect:(e,t)=>this.selectWord(i,o,e,t),onLineSelect:(e,t)=>this.selectLine(i,o,e,t),setHasTextAnchor:e=>this.hasTextAnchor.set(i,e),minDragDistance:this.config.minSelectionDragDistance,toleranceFactor:this.config.toleranceFactor}),h=this.interactionManagerCapability.registerAlways({scope:{type:"page",documentId:i,pageIndex:o},handlers:d});return()=>{var e;h(),null==(e=this.pageCallbacks.get(i))||e.delete(o),c.abort({code:t.PdfErrorCode.Cancelled,message:"Cleanup"})}}registerMarqueeOnPage(t){var n;const{documentId:i,pageIndex:o,scale:s,onRectChange:r}=t;if(!this.state.documents[i])return this.logger.warn("SelectionPlugin","RegisterMarqueeFailed",`Cannot register marquee on page ${o} for document ${i}: document state not initialized.`),()=>{};const c=this.coreState.core.documents[i];if(!c||!c.document)return this.logger.warn("SelectionPlugin","DocumentNotFound",`Cannot register marquee on page ${o}: document not found`),()=>{};const a=c.document.pages[o];if(!a)return this.logger.warn("SelectionPlugin","PageNotFound",`Cannot register marquee on page ${o}: page not found`),()=>{};const l=a.size,d=(null==(n=this.config.marquee)?void 0:n.minDragPx)??5,h=()=>{var e;const t=this.interactionManagerCapability.forDocument(i).getActiveMode(),n=null==(e=this.enabledModesPerDoc.get(i))?void 0:e.get(t);return!1!==(null==n?void 0:n.showMarqueeRects)},u=function(t){const{pageSize:n,scale:i,minDragPx:o=5}=t;let s=null,r=null;return{onPointerDown:(e,n,i)=>{var o,c;t.isEnabled(i)&&((null==(o=t.isTextSelecting)?void 0:o.call(t))||(s=e,r={origin:{x:e.x,y:e.y},size:{width:0,height:0}},t.onBegin(e,i),null==(c=n.setPointerCapture)||c.call(n)))},onPointerMove:(i,o,c)=>{if(!s||!t.isEnabled(c))return;const a=e.clamp(i.x,0,n.width),l=e.clamp(i.y,0,n.height);r={origin:{x:Math.min(s.x,a),y:Math.min(s.y,l)},size:{width:Math.abs(a-s.x),height:Math.abs(l-s.y)}},t.onChange(r,c)},onPointerUp:(e,n,c)=>{var a;t.isEnabled(c)&&(r&&s&&(Math.max(r.size.width,r.size.height)*i>o?t.onEnd(r,c):t.onCancel(c)),s=null,r=null,null==(a=n.releasePointerCapture)||a.call(n))},onPointerCancel:(e,n,i)=>{var o;t.isEnabled(i)&&(s=null,r=null,t.onCancel(i),null==(o=n.releasePointerCapture)||o.call(n))}}}({pageSize:l,scale:s,minDragPx:d,isEnabled:e=>{var t;const n=null==(t=this.enabledModesPerDoc.get(i))?void 0:t.get(e);return!0===(null==n?void 0:n.enableMarquee)},isTextSelecting:()=>(this.selecting.get(i)??!1)||(this.hasTextAnchor.get(i)??!1),onBegin:(e,t)=>this.beginMarquee(i,o,e,t),onChange:(e,t)=>{this.updateMarquee(i,o,e,t),r(h()?e:null)},onEnd:(e,t)=>{this.endMarquee(i,o,e,t),r(null)},onCancel:e=>{this.cancelMarquee(i,e),r(null)}});return this.interactionManagerCapability.registerAlways({scope:{type:"page",documentId:i,pageIndex:o},handlers:u})}getPlacementMetrics(e,t,n,i){var o;const s=null==(o=this.scrollCapability)?void 0:o.forDocument(e),r=null==s?void 0:s.getRectPositionForPage(t,n);if(!r)return null;const c=r.origin.y-i.scrollTop,a=r.origin.y+r.size.height-i.scrollTop;return{pageIndex:t,rect:n,spaceAbove:c,spaceBelow:i.clientHeight-a,isBottomVisible:a>0&&a<=i.clientHeight,isTopVisible:c>=0&&c<i.clientHeight}}emitMenuPlacement(e,t){this.menuPlacement$.emit(e,t),t?this.interactionManagerCapability.claimPageActivity(e,"selection-menu",t.pageIndex):this.interactionManagerCapability.releasePageActivity(e,"selection-menu")}recalculateMenuPlacement(e){const t=this.state.documents[e];if(!t)return;if(t.selecting||null===t.selection)return void this.emitMenuPlacement(e,null);const n=E(t);if(0===n.length)return void this.emitMenuPlacement(e,null);const i=n[n.length-1];if(!this.viewportCapability||!this.scrollCapability)return void this.emitMenuPlacement(e,{pageIndex:i.page,rect:i.rect,spaceAbove:0,spaceBelow:1/0,suggestTop:!1,isVisible:!0});const o=this.viewportCapability.forDocument(e).getMetrics(),s=n[0],r=this.getPlacementMetrics(e,i.page,i.rect,o),c=this.getPlacementMetrics(e,s.page,s.rect,o);r&&r.isBottomVisible&&r.spaceBelow>this.menuHeight?this.emitMenuPlacement(e,{...r,suggestTop:!1,isVisible:!0}):c&&c.isTopVisible?this.emitMenuPlacement(e,{...c,suggestTop:!0,isVisible:!0}):r&&r.isBottomVisible?this.emitMenuPlacement(e,{...r,suggestTop:!1,isVisible:!0}):this.emitMenuPlacement(e,null)}notifyPage(e,t){var n,i;const o=null==(n=this.pageCallbacks.get(e))?void 0:n.get(t);if(o){const n=this.getDocumentState(e),s=this.interactionManagerCapability.forDocument(e).getActiveMode(),r=null==(i=this.enabledModesPerDoc.get(e))?void 0:i.get(s);o(r&&!1!==(r.showSelectionRects??r.showRects)?{rects:M(n,t),boundingRect:I(n,t)}:{rects:[],boundingRect:null})}}notifyAllPages(e){var t;null==(t=this.pageCallbacks.get(e))||t.forEach((t,n)=>{this.notifyPage(e,n)})}getNewPageGeometryAndCache(e,n){const i=this.getCoreDocument(e);if(!i||!i.document)return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Doc Not Found"});const o=i.document.pages.find(e=>e.index===n),s=this.engine.getPageGeometry(i.document,o);return s.wait(t=>{this.dispatch(((e,t,n)=>({type:r,payload:{documentId:e,page:t,geo:n}}))(e,n,t)),this.touchGeometry(e,n)},t.ignore),s}getOrLoadGeometry(e,n){const i=this.getDocumentState(e).geometry[n];return i?(this.touchGeometry(e,n),t.PdfTaskHelper.resolve(i)):this.getNewPageGeometryAndCache(e,n)}touchGeometry(e,t){const n=this.geoAccessOrder.get(e);if(!n)return;const i=n.indexOf(t);i>-1&&n.splice(i,1),n.push(t),this.evictGeometryIfNeeded(e)}evictGeometryIfNeeded(e){const t=this.config.maxCachedGeometries??50,n=this.geoAccessOrder.get(e);if(!n||n.length<=t)return;const i=this.pageCallbacks.get(e),o=[];for(;n.length-o.length>t;){const e=n.find(e=>!o.includes(e)&&!(null==i?void 0:i.has(e)));if(void 0===e)break;o.push(e)}if(0!==o.length){for(const e of o){const t=n.indexOf(e);t>-1&&n.splice(t,1)}this.dispatch(((e,t)=>({type:g,payload:{documentId:e,pages:t}}))(e,o))}}beginSelection(e,t,n,i){this.selecting.set(e,!0),this.anchor.set(e,{page:t,index:n}),this.dispatch(m(e)),this.beginSelection$.emit(e,{page:t,index:n,modeId:i}),this.recalculateMenuPlacement(e)}endSelection(e,t){this.selecting.set(e,!1),this.anchor.set(e,void 0),this.dispatch(f(e)),this.endSelection$.emit(e,{modeId:t}),this.recalculateMenuPlacement(e)}clearSelection(e,t){this.selecting.set(e,!1),this.anchor.set(e,void 0),this.dispatch((e=>({type:d,payload:{documentId:e}}))(e)),this.selChange$.emit(e,null),this.emitMenuPlacement(e,null),this.notifyAllPages(e)}selectWord(e,t,n,i){const o=this.getDocumentState(e).geometry[t];if(!o)return;const s=O(o,n);s&&this.applyInstantSelection(e,t,s.from,s.to,i)}selectLine(e,t,n,i){const o=this.getDocumentState(e).geometry[t];if(!o)return;const s=L(o,n);s&&this.applyInstantSelection(e,t,s.from,s.to,i)}applyInstantSelection(e,t,n,i,o){const s={start:{page:t,index:n},end:{page:t,index:i}};this.selecting.set(e,!1),this.anchor.set(e,void 0),this.dispatch(m(e)),this.dispatch(p(e,s)),this.updateRectsAndSlices(e,s),this.dispatch(f(e)),this.selChange$.emit(e,s),this.beginSelection$.emit(e,{page:t,index:n,modeId:o}),this.endSelection$.emit(e,{modeId:o});for(let r=s.start.page;r<=s.end.page;r++)this.notifyPage(e,r);this.recalculateMenuPlacement(e)}updateSelection(e,t,n,i){if(!this.selecting.get(e)||!this.anchor.get(e))return;const o=this.anchor.get(e),s=t>o.page||t===o.page&&n>=o.index,r={start:s?o:{page:t,index:n},end:s?{page:t,index:n}:o};this.dispatch(p(e,r)),this.updateRectsAndSlices(e,r),this.selChange$.emit(e,r);for(let c=r.start.page;c<=r.end.page;c++)this.notifyPage(e,c)}updateRectsAndSlices(e,t){const n=this.getDocumentState(e),i={},o={};for(let s=t.start.page;s<=t.end.page;s++){const e=n.geometry[s],r=w(t,e,s);r&&(i[s]=$(e,r.from,r.to),o[s]={start:r.from,count:r.to-r.from+1})}this.dispatch(S(e,i)),this.dispatch(y(e,o))}getSelectedText(e){if(!this.checkPermission(e,t.PdfPermissionFlag.CopyContents))return this.logger.debug("SelectionPlugin","GetSelectedText",`Cannot get selected text: document ${e} lacks CopyContents permission`),t.PdfTaskHelper.reject({code:t.PdfErrorCode.Security,message:"Document lacks CopyContents permission"});const n=this.getCoreDocument(e),i=this.getDocumentState(e);if(!(null==n?void 0:n.document)||!i.selection)return t.PdfTaskHelper.reject({code:t.PdfErrorCode.NotFound,message:"Doc Not Found or No Selection"});const o=i.selection,s=[];for(let t=o.start.page;t<=o.end.page;t++){const e=i.slices[t];e&&s.push({pageIndex:t,charIndex:e.start,charCount:e.count})}if(0===s.length)return t.PdfTaskHelper.resolve([]);const r=this.engine.getTextSlices(n.document,s);return r.wait(t=>{this.textRetrieved$.emit(e,t)},t.ignore),r}copyToClipboard(e){if(!this.checkPermission(e,t.PdfPermissionFlag.CopyContents))return void this.logger.debug("SelectionPlugin","CopyToClipboard",`Cannot copy to clipboard: document ${e} lacks CopyContents permission`);this.getSelectedText(e).wait(t=>{this.copyToClipboard$.emit(e,t.join("\n"))},t.ignore)}beginMarquee(e,t,n,i){this.marqueePage.set(e,t)}updateMarquee(e,t,n,i){this.marqueeChange$.emit(e,{pageIndex:t,rect:n,modeId:i})}endMarquee(e,t,n,i){this.marqueeEnd$.emit(e,{pageIndex:t,rect:n,modeId:i}),this.marqueeChange$.emit(e,{pageIndex:t,rect:null,modeId:i}),this.marqueePage.delete(e)}cancelMarquee(e,t){const n=this.marqueePage.get(e);void 0!==n&&(this.marqueeChange$.emit(e,{pageIndex:n,rect:null,modeId:t}),this.marqueePage.delete(e))}setMarqueeEnabled(e,t){const n=this.enabledModesPerDoc.get(e);if(!n)return;const i=n.get("pointerMode");i?i.enableMarquee=t:t&&n.set("pointerMode",{enableMarquee:!0})}isMarqueeEnabled(e){var t;const n=null==(t=this.enabledModesPerDoc.get(e))?void 0:t.get("pointerMode");return!1!==(null==n?void 0:n.enableMarquee)}};B.id="selection";let _=B;const V={manifest:i,create:(e,t)=>new _(n,e,t),reducer:(e=C,t)=>{switch(t.type){case o:{const{documentId:n,state:i}=t.payload;return b(e,n,i)}case s:{const n=t.payload,{[n]:i,...o}=e.documents;return{...e,documents:o}}case r:{const{documentId:n,page:i,geo:o}=t.payload,s=e.documents[n];return s?b(e,n,{...s,geometry:{...s.geometry,[i]:o}}):e}case c:{const{documentId:n,selection:i}=t.payload,o=e.documents[n];return o?b(e,n,{...o,selection:i,active:!0}):e}case a:{const{documentId:n}=t.payload,i=e.documents[n];return i?b(e,n,{...i,selecting:!0,selection:null,rects:{}}):e}case l:{const{documentId:n}=t.payload,i=e.documents[n];return i?b(e,n,{...i,selecting:!1}):e}case d:{const{documentId:n}=t.payload,i=e.documents[n];return i?b(e,n,{...i,selecting:!1,selection:null,rects:{},active:!1}):e}case h:{const{documentId:n,rects:i}=t.payload,o=e.documents[n];return o?b(e,n,{...o,rects:i}):e}case u:{const{documentId:n,slices:i}=t.payload,o=e.documents[n];return o?b(e,n,{...o,slices:i}):e}case g:{const{documentId:n,pages:i}=t.payload,o=e.documents[n];if(!o)return e;const s={...o.geometry},r={...o.rects},c={...o.slices};for(const e of i)delete s[e],delete r[e],delete c[e];return b(e,n,{...o,geometry:s,rects:r,slices:c})}case"SELECTION/RESET":{const{documentId:n}=t.payload;return e.documents[n]?b(e,n,x):e}default:return e}},initialState:C};exports.SELECTION_PLUGIN_ID=n,exports.SelectionPlugin=_,exports.SelectionPluginPackage=V,exports.expandToLineBoundary=L,exports.expandToWordBoundary=O,exports.getVerticalOverlap=z,exports.glyphAt=T,exports.manifest=i,exports.mergeAdjacentRects=k,exports.rectIntersect=q,exports.rectIsEmpty=R,exports.rectUnion=D,exports.rectsWithinSlice=$,exports.shouldMergeHorizontalRects=A,exports.sliceBounds=w;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|