UNPKG

@univerjs/docs-ui

Version:

Univer normal ui-plugin-docs

19 lines • 440 kB
"use strict";var fs=Object.defineProperty;var Ss=(o,t,e)=>t in o?fs(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e;var M=(o,t,e)=>Ss(o,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("@univerjs/core"),D=require("@univerjs/docs"),B=require("@univerjs/engine-render"),I=require("@univerjs/ui"),P=require("react"),N=require("react/jsx-runtime"),q=require("@univerjs/design"),U=require("rxjs"),mt=require("rxjs/operators"),co=require("@univerjs/drawing");var Is=Object.getOwnPropertyDescriptor,vs=(o,t,e,n)=>{for(var r=n>1?void 0:n?Is(t,e):t,s=o.length-1,a;s>=0;s--)(a=o[s])&&(r=a(r)||r);return r},rr=(o,t)=>(e,n)=>t(e,n,o);exports.DocAutoFormatService=class extends i.Disposable{constructor(e,n){super();M(this,"_matches",new Map);this._univerInstanceService=e,this._textSelectionManagerService=n}registerAutoFormat(e){const n=this._matches.get(e.id);return n?(n.push(e),n.sort((r,s)=>{var a,c;return((a=s.priority)!=null?a:0)-((c=r.priority)!=null?c:0)})):this._matches.set(e.id,[e]),i.toDisposable(()=>{const r=this._matches.get(e.id);if(r){const s=r.findIndex(a=>a===e);s>=0&&r.splice(s,1)}})}onAutoFormat(e,n){var l,A,d,u,m,g,h,p,f;const r=(l=this._matches.get(e))!=null?l:[],s=this._univerInstanceService.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),a=this._textSelectionManagerService.getDocRanges(),c=(A=a.find(S=>S.isActive))!=null?A:a[0];if(s&&c){const S=s.getSelfOrHeaderFooterModel(c.segmentId),E={unit:S,selection:c,isBody:!c.segmentId,paragraphs:i.BuildTextUtils.range.getParagraphsInRange(c,(u=(d=S.getBody())==null?void 0:d.paragraphs)!=null?u:[],(g=(m=S.getBody())==null?void 0:m.dataStream)!=null?g:""),customRanges:i.BuildTextUtils.customRange.getCustomRangesInterestsWithSelection(c,(p=(h=S.getBody())==null?void 0:h.customRanges)!=null?p:[]),commandId:e,commandParams:n},C=r.find(T=>T.match(E));return(f=C==null?void 0:C.getMutations(E))!=null?f:[]}return[]}};exports.DocAutoFormatService=vs([rr(0,i.IUniverInstanceService),rr(1,i.Inject(D.DocSelectionManagerService))],exports.DocAutoFormatService);const qr={undo:!0,redo:!0,font:!0,fontSize:!0,bold:!0,italic:!0,strikethrough:!0,underline:!0,textColor:!0,fillColor:!0,horizontalAlignMode:!0,verticalAlignMode:!0,textWrapMode:!0,textRotateMode:!0},Jr={outerLeft:!1,outerRight:!1,header:!0,footer:!0,innerLeft:!1,innerRight:!1,frozenHeaderLT:!1,frozenHeaderRT:!1,frozenHeaderLM:!1,frozenContent:!1,infoBar:!0,toolbar:!0},Zr="DOC_UI_PLUGIN";var de=(o=>(o.MAIN="__Document_Render_Main__",o.BACKGROUND="__Document_Render_Background__",o))(de||{}),ie=(o=>(o.VIEW_MAIN="viewMain",o.VIEW_TOP="viewTop",o.VIEW_LEFT="viewLeft",o.VIEW_LEFT_TOP="viewLeftTop",o))(ie||{});const $r=0,Vt=2,ei=4,kn=10,Es="normalTextSelectionPluginName";function Ie(o){const{mainComponent:t,scene:e,engine:n,components:r}=o,s=t,a=r.get(de.BACKGROUND);return{document:s,docBackground:a,scene:e,engine:n}}function ti(o,t){const e=o.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC);if(!e)return null;const n=e.getUnitId(),r=t.getRenderById(n);if(r==null)return;const{mainComponent:s,scene:a,engine:c,components:l}=r,A=s,d=l.get(de.BACKGROUND);return{document:A,docBackground:d,scene:a,engine:c}}function Cs(o,t){const e=t.getRenderById(o);if(e==null)return;const{mainComponent:n,scene:r,engine:s,components:a}=e,c=n,l=a.get(de.BACKGROUND);return{document:c,docBackground:l,scene:r,engine:s}}function ni(o){const{unitId:t,ranges:e,id:n,type:r,segmentId:s}=o,a={id:D.RichTextEditingMutation.id,params:{unitId:t,actions:[],textRanges:void 0,segmentId:s}},c=i.JSONX.getInstance(),l=i.BuildTextUtils.customDecoration.add({ranges:e,id:n,type:r});return a.params.actions=c.editOp(l.serialize()),a}function Ts(o,t){const{segmentId:e,id:n,type:r,unitId:s}=t,a=o.get(D.DocSelectionManagerService),c=o.get(i.IUniverInstanceService),l=s?c.getUnit(s,i.UniverInstanceType.UNIVER_DOC):c.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC);if(!l)return!1;const A=l.getUnitId(),d=a.getTextRanges({unitId:A,subUnitId:A});return!d||!l.getBody()?!1:ni({unitId:A,ranges:d,id:n,type:r,segmentId:e})}function ys(o,t){const{unitId:e,id:n,segmentId:r}=t,a=o.get(i.IUniverInstanceService).getUnit(e);if(!a)return!1;const c={id:D.RichTextEditingMutation.id,params:{unitId:e,actions:[],textRanges:void 0,segmentId:r}},l=i.BuildTextUtils.customDecoration.delete({id:n,segmentId:r,documentDataModel:a});if(!l)return!1;const A=i.JSONX.getInstance();return c.params.actions=A.editOp(l.serialize()),c}function bs(o,t){return t.some(e=>o.startIndex>e.startIndex&&o.startIndex<e.endIndex)}function an(o,t,e,n,r){const{textRuns:s=[],dataStream:a}=o,c=r&&a.startsWith("="),l={st:0,ed:0,ts:{}};if(c)return l;for(let A=s.length-1;A>=0;A--){const d=s[A],{st:u,ed:m}=d;t>u&&t<=m&&(l.st=u,l.ed=m,l.ts={...l.ts,...d.ts})}if(t===0){const A=s==null?void 0:s[0];A&&A.st===0&&(l.ts={...l.ts,...A.ts})}return n&&(l.ts={...l.ts,...n}),l}function lo(o,t,e){if(e){const r=o.find(s=>t>=s.startIndex&&t<=s.endIndex+1);return r!=null&&r.wholeEntity?null:r}const n=o.find(r=>t>r.startIndex&&t<=r.endIndex);return n!=null&&n.wholeEntity?null:n}function Ao(o,t){return o.filter(e=>t>e.startIndex&&t<=e.endIndex)}function Rs(o){return{left:o.positionH.posOffset,top:o.positionV.posOffset,width:o.size.width,height:o.size.height}}function xs(o,t=0,e=0){return{size:{width:o.width,height:o.height},positionH:{relativeFrom:i.ObjectRelativeFromH.MARGIN,posOffset:(o.left||0)-t},positionV:{relativeFrom:i.ObjectRelativeFromV.PAGE,posOffset:(o.top||0)-e},angle:o.angle||0}}function oi(o,t,e){const n=I.useDependency(i.ICommandService),r=I.useDependency(I.IShortcutService),s=P.useMemo(()=>i.generateRandomId(4),[]);P.useEffect(()=>{if(!e||!o||!t)return;const c=`sheet.operation.editor-${e.getEditorId()}-keyboard-${s}`,l=new i.DisposableCollection;return l.add(n.registerCommand({id:c,type:i.CommandType.OPERATION,handler(A,d){const{keyCode:u,metaKey:m}=d;t.handler(u,m)}})),t.keyCodes.map(A=>({id:c,binding:A.metaKey?A.keyCode|A.metaKey:A.keyCode,preconditions:()=>!0,priority:901,staticParameters:{eventType:B.DeviceInputEventType.Keyboard,keyCode:A.keyCode,metaKey:A.metaKey}})).forEach(A=>{l.add(r.registerShortcut(A))}),()=>{l.dispose()}},[n,t,e,o,s,r])}const ri=(o,t=!0,e,n)=>{const r=P.useCallback(()=>{if(o){const{scene:a,mainComponent:c}=o.render,l=o.render.with(D.DocSkeletonManagerService),{width:A,height:d}=o.getBoundingClientRect();l.getViewModel().getDataModel().updateDocumentDataPageSize(t?1/0:A,1/0),a.transformByState({width:A,height:d}),c==null||c.resize(A,d)}},[o,t]),s=P.useMemo(()=>i.debounce(()=>{var E,C;if(!e||!o||!e)return;const c=o.render.with(D.DocSkeletonManagerService).getSkeleton(),{scene:l,mainComponent:A}=o.render,d=l.getViewport(ie.VIEW_MAIN),{actualWidth:u,actualHeight:m}=c.getActualSize(),{width:g,height:h}=o.getBoundingClientRect();let p=d==null?void 0:d.getScrollBar();const f=Math.max(u,g),S=Math.max(m,h);l.transformByState({width:f,height:S}),A==null||A.resize(f,S),t?u>g?(p==null?d&&new B.ScrollBar(d,{barSize:8,enableVertical:!1,enableHorizontal:!0,minThumbSizeV:8}):d==null||d.resetCanvasSizeAndUpdateScroll(),n&&(d==null||d.scrollToBarPos({x:1/0,y:0}))):(p=null,d==null||d.scrollToBarPos({x:0,y:0}),(C=d==null?void 0:d.getScrollBar())==null||C.dispose()):m>h?(p==null?d&&(p=new B.ScrollBar(d,{enableHorizontal:!1,enableVertical:!0,barSize:8,minThumbSizeV:8})):d==null||d.resetCanvasSizeAndUpdateScroll(),n&&(d==null||d.scrollToBarPos({x:0,y:1/0}))):(p=null,d==null||d.scrollToBarPos({x:0,y:0}),(E=d==null?void 0:d.getScrollBar())==null||E.dispose())},30),[o,e,t,n]);return P.useEffect(()=>{if(e&&o){const a=setTimeout(()=>{r(),s()},500);return()=>{clearTimeout(a)}}},[o,e,r,s]),P.useEffect(()=>{if(e&&o){const a=o.input$.subscribe(()=>{s()});return()=>{a.unsubscribe()}}},[o,e,s]),{resize:r,checkScrollBar:s}};function ce(o,t){var n;return(n=o.get(B.IRenderManagerService).getRenderById(t))==null?void 0:n.with(D.DocSkeletonManagerService)}function J(o,t=""){if(!t)return["body"];const{headers:e,footers:n}=o.getSnapshot();if(e==null&&n==null)throw new Error("Document data model must have headers or footers when update by segment id");if((e==null?void 0:e[t])!=null)return["headers",t,"body"];if((n==null?void 0:n[t])!=null)return["footers",t,"body"];throw new Error("Segment id not found in headers or footers")}const uo={id:"doc.command-replace-snapshot",type:i.CommandType.COMMAND,handler:(o,t)=>{const{unitId:e,snapshot:n,textRanges:r,segmentId:s="",options:a}=t,c=o.get(i.IUniverInstanceService),l=o.get(i.ICommandService),A=o.get(D.DocSelectionManagerService),d=c.getUnit(e,i.UniverInstanceType.UNIVER_DOC),u=d==null?void 0:d.getSelfOrHeaderFooterModel(s).getSnapshot();if(d==null||u==null)return!1;const{body:m,tableSource:g,footers:h,headers:p,lists:f,drawings:S,drawingsOrder:E,documentStyle:C}=i.Tools.deepClone(n),{body:T,tableSource:R,footers:v,headers:b,lists:x,drawings:y,drawingsOrder:_,documentStyle:O}=u;if(m==null||T==null)return!1;if(i.Tools.diffValue(m,T)&&r)return A.replaceDocRanges(r,{unitId:e,subUnitId:e},!1),!0;const w={id:D.RichTextEditingMutation.id,params:{unitId:e,actions:[],textRanges:r}};a&&(w.params.options=a);const Q=[],k=i.JSONX.getInstance();if(!i.Tools.diffValue(O,C)){const L=k.replaceOp(["documentStyle"],O,C);L!=null&&Q.push(L)}if(!i.Tools.diffValue(m,T)){const L=k.replaceOp(["body"],T,m);L!=null&&Q.push(L)}if(!i.Tools.diffValue(g,R)){const L=k.replaceOp(["tableSource"],R,g);L!=null&&Q.push(L)}if(!i.Tools.diffValue(h,v)){const L=k.replaceOp(["footers"],v,h);L!=null&&Q.push(L)}if(!i.Tools.diffValue(p,b)){const L=k.replaceOp(["headers"],b,p);L!=null&&Q.push(L)}if(!i.Tools.diffValue(f,x)){const L=k.replaceOp(["lists"],x,f);L!=null&&Q.push(L)}if(!i.Tools.diffValue(S,y)){const L=k.replaceOp(["drawings"],y,S);L!=null&&Q.push(L)}if(!i.Tools.diffValue(E,_)){const L=k.replaceOp(["drawingsOrder"],_,E);L!=null&&Q.push(L)}return w.params.actions=Q.reduce((L,G)=>i.JSONX.compose(L,G),null),!!l.syncExecuteCommand(w.id,w.params)}},ii={id:"doc.command-replace-content",type:i.CommandType.COMMAND,handler:async(o,t)=>{const{unitId:e,body:n,textRanges:r,segmentId:s="",options:a}=t,c=o.get(i.IUniverInstanceService),l=o.get(i.ICommandService),A=o.get(D.DocSelectionManagerService),d=c.getUnit(e,i.UniverInstanceType.UNIVER_DOC),u=d==null?void 0:d.getSelfOrHeaderFooterModel(s).getSnapshot().body;if(d==null||u==null)return!1;const m=ai(e,s,d,u,n);return m.params.textRanges=r,a&&(m.params.options=a),m.params.actions==null&&r?(A.replaceDocRanges(r,{unitId:e,subUnitId:e},!1),!0):!!l.syncExecuteCommand(m.id,m.params)}},si={id:"doc.command-cover-content",type:i.CommandType.COMMAND,handler:(o,t)=>{const{unitId:e,body:n,segmentId:r="",textRanges:s}=t,a=o.get(i.IUniverInstanceService),c=o.get(i.ICommandService),l=o.get(i.IUndoRedoService),A=a.getUniverDocInstance(e),d=A==null?void 0:A.getSnapshot().body;if(A==null||d==null)return!1;const u=ai(e,r,A,d,n);return u.params.noNeedSetTextRange=!0,u.params.noHistory=!0,u.params.textRanges=s,c.syncExecuteCommand(u.id,u.params),l.clearUndoRedo(e),!0}};function Os(o,t,e,n){const r=new i.TextX,s=i.JSONX.getInstance(),a=(e==null?void 0:e.dataStream.length)-2;a>0&&r.push({t:i.TextXActionType.DELETE,len:a}),n.dataStream.length>0&&r.push({t:i.TextXActionType.INSERT,body:n,len:n.dataStream.length});const c=J(t,o);return s.editOp(r.serialize(),c)}function ai(o,t,e,n,r){const s={id:D.RichTextEditingMutation.id,params:{unitId:o,actions:[],textRanges:[]}},a=Os(t,e,n,r);return s.params.actions=a,s}const go={id:"doc.command.replace-selection",type:i.CommandType.COMMAND,handler(o,t){var h;if(!t)return!1;const e=o.get(i.ICommandService),{unitId:n,body:r,textRanges:s}=t,c=o.get(i.IUniverInstanceService).getUnit(n),l=o.get(D.DocSelectionManagerService);if(!c)return!1;const A=c.getBody(),d=(h=t.selection)!=null?h:l.getActiveTextRange();if(!d||!A)return!1;const u={id:D.RichTextEditingMutation.id,params:{unitId:n,actions:[],textRanges:s,debounce:!0}},m=new i.TextX,g=i.JSONX.getInstance();return m.push(...i.BuildTextUtils.selection.delete([d],A,0,r)),u.params.actions=g.editOp(m.serialize()),e.syncExecuteCommand(u.id,u.params)}},ci={id:"doc.command.replace-text-runs",type:i.CommandType.COMMAND,handler:(o,t)=>{const{unitId:e,body:n,textRanges:r,segmentId:s="",options:a}=t,c=o.get(i.IUniverInstanceService),l=o.get(i.ICommandService),A=o.get(i.ThemeService),d=c.getUnit(e,i.UniverInstanceType.UNIVER_DOC),u=d==null?void 0:d.getSelfOrHeaderFooterModel(s).getSnapshot().body;if(d==null||u==null)return!1;const m=i.BuildTextUtils.selection.replaceTextRuns({doc:d,body:n,selection:{startOffset:0,endOffset:u.dataStream.length-2,collapsed:!1},themeService:A});if(!m)return!1;const g={id:D.RichTextEditingMutation.id,params:{unitId:e,actions:[],textRanges:r,noHistory:!0}},h=i.JSONX.getInstance(),p=J(d,s);return g.params.actions=h.editOp(m.serialize(),p),g.params.textRanges=r,a&&(g.params.options=a),!!l.syncExecuteCommand(g.id,g.params)}};var jt=(o=>(o[o.page=0]="page",o[o.section=1]="section",o[o.column=2]="column",o[o.line=3]="line",o[o.divide=4]="divide",o[o.glyph=5]="glyph",o))(jt||{});const Ze={page:0,section:1,column:2,line:3,divide:4,glyph:5};function ho(o,t){return o.page>t.page?!1:o.page<t.page?!0:o.section>t.section?!1:o.section<t.section?!0:o.column>t.column?!1:o.column<t.column?!0:o.line>t.line?!1:o.line<t.line?!0:o.divide>t.divide?!1:o.divide<t.divide?!0:o.glyph>t.glyph?!1:(o.glyph<t.glyph,!0)}function Vn(o,t){return ho(o,t)?{start:o,end:t}:{start:t,end:o}}function jn(o){const t=o.length;if(t===0)return;const e=o[0],n=o[t-1],r=o.length===1&&e.collapsed;return{startOffset:e.startOffset,endOffset:n.endOffset,collapsed:r}}function Ms(o,t,e,n){let r=n,s=n;for(let a=0;a<o.length;a++){const l=o[a].count;a<t&&(r+=l),a<e&&(s+=l)}return{startOffset:r,endOffset:s}}function Wn(o){const{startX:t,startY:e,endX:n,endY:r}=o,s=[];return s.push({x:t,y:e}),s.push({x:n,y:e}),s.push({x:n,y:r}),s.push({x:t,y:r}),s.push({x:t,y:e}),s}class ve{constructor(t,e){M(this,"_liquid",new B.Liquid);M(this,"_currentStartState",{page:0,section:0,column:0,line:0,divide:0,glyph:0});M(this,"_currentEndState",{page:0,section:0,column:0,line:0,divide:0,glyph:0});this._documentOffsetConfig=t,this._docSkeleton=e}getRangePointData(t,e){const n=[],r=[],s=[];if(t==null||e==null)return{borderBoxPointGroup:n,contentBoxPointGroup:r,cursorList:s};if(!this._isValidPosition(t,e))throw new Error(` Invalid positions in NodePositionConvertToCursor, they are not in the same segment page when in header or footer.`);const{start:c,end:l}=Vn(t,e);return this._selectionIterator(c,l,(A,d,u,m,g,h)=>{const{lineHeight:p,asc:f,paddingTop:S,marginTop:E,marginBottom:C}=h,{glyphGroup:T,st:R}=g;if(T.length===0)return;const{x:v,y:b}=this._liquid;let x,y;const _=T[A],O=T[d],w=T[A-1],Q=(_==null?void 0:_.left)||0,k=(_==null?void 0:_.width)||0,F=(O==null?void 0:O.left)||0,L=(O==null?void 0:O.width)||0,G=(_==null?void 0:_.glyphType)===B.GlyphType.LIST,{startOffset:W,endOffset:H}=Ms(T,A,d,R),V=c.glyph===A&&u?c.isBack:!0,j=l.glyph===d&&m?l.isBack:!1,X=c===l,z=V&&w!=null?w:_;if(A===0&&d===T.length-1)x={startX:v+Q+(G?k:0),startY:b,endX:v+F+(j?0:L),endY:b+p-E-C},y={startX:v+Q+(G?k:0),startY:b+S+f-z.bBox.ba,endX:v+F+(j?0:L),endY:b+S+f+z.bBox.bd};else{const Y=V&&!G;x={startX:v+Q+(Y?0:k),startY:b,endX:v+F+(j?0:L),endY:b+p-E-C},y={startX:v+Q+(Y?0:k),startY:b+S+f-z.bBox.ba,endX:v+F+(j?0:L),endY:b+S+f+z.bBox.bd}}n.push(Wn(x)),r.push(Wn(y)),s.push({startOffset:V?W:W+_.count,endOffset:j?H:H+O.count,collapsed:X})}),{borderBoxPointGroup:n,contentBoxPointGroup:r,cursorList:s}}_isValidPosition(t,e){const{segmentPage:n,pageType:r}=t,{segmentPage:s,pageType:a}=e;return r!==a?!1:r===B.DocumentSkeletonPageType.HEADER||r===B.DocumentSkeletonPageType.FOOTER?n===s:!0}_resetCurrentNodePositionState(){this._currentStartState={page:0,section:0,column:0,line:0,divide:0,glyph:0},this._currentEndState={page:0,section:0,column:0,line:0,divide:0,glyph:0}}_setNodePositionState(t=0,e,n,r){r===e?this._currentStartState[t]=1:this._currentStartState[t]=0,r===n?this._currentEndState[t]=2:this._currentEndState[t]=0}_checkPreviousNodePositionState(t,e=!0){let n=t,r;for(;n>=0;){const s=jt[n];let a;if(e?a=this._currentStartState[s]:a=this._currentEndState[s],a===void 0)return;if(r===void 0&&(r=a),a!==r)return 0;n--}return r}_getSelectionRuler(t,e,n,r,s){let a=0,c=r;const l=jt[t],A=jt[t+1];if(A===null||l===null)return{start_next:a,end_next:c};const d=e[l],u=n[l];this._setNodePositionState(l,d,u,s);const m=this._checkPreviousNodePositionState(t),g=this._checkPreviousNodePositionState(t,!1);return m===1&&(a=e[A]),g===2&&(c=n[A]),{start_next:a,end_next:c}}_selectionIterator(t,e,n){var T,R;const r=this._docSkeleton;if(!r)return[];const{pageType:s,path:a}=t;this._liquid.reset();const c=r.getSkeletonData();if(c==null)return[];const{pages:l,skeHeaders:A,skeFooters:d}=c,{page:u,segmentPage:m}=t,{page:g,segmentPage:h}=e;if(this._resetCurrentNodePositionState(),this._documentOffsetConfig==null)return[];const{pageLayoutType:p,pageMarginLeft:f,pageMarginTop:S}=this._documentOffsetConfig,E=s===B.DocumentSkeletonPageType.BODY||s===B.DocumentSkeletonPageType.CELL?u:m;for(let v=0;v<E;v++){const b=l[v];this._liquid.translatePage(b,p,f,S)}const C=s===B.DocumentSkeletonPageType.BODY||s===B.DocumentSkeletonPageType.CELL?g:h;for(let v=E;v<=C;v++){const b=l[v],{headerId:x,footerId:y,pageWidth:_}=b;let O=b;if(s===B.DocumentSkeletonPageType.HEADER?O=(T=A.get(x))==null?void 0:T.get(_):s===B.DocumentSkeletonPageType.FOOTER?O=(R=d.get(y))==null?void 0:R.get(_):s===B.DocumentSkeletonPageType.CELL&&(O=B.getPageFromPath(c,a)),O==null){this._liquid.translatePage(b,p,f,S);continue}const w=O.sections,{start_next:Q,end_next:k}=this._getSelectionRuler(Ze.page,t,e,w.length-1,s===B.DocumentSkeletonPageType.BODY||s===B.DocumentSkeletonPageType.CELL?v:0);switch(this._liquid.translateSave(),s){case B.DocumentSkeletonPageType.HEADER:this._liquid.translatePagePadding({...O,marginLeft:b.marginLeft});break;case B.DocumentSkeletonPageType.FOOTER:{const F=b.pageHeight-O.height-O.marginBottom;this._liquid.translate(b.marginLeft,F);break}case B.DocumentSkeletonPageType.CELL:{this._liquid.translatePagePadding(b);const F=O.parent,L=F.parent,{left:G}=O,{top:W,left:H}=L,{top:V}=F;this._liquid.translate(H+G,W+V),this._liquid.translatePagePadding(O);break}default:this._liquid.translatePagePadding(b);break}for(let F=Q;F<=k;F++){const L=w[F],G=L.columns,{start_next:W,end_next:H}=this._getSelectionRuler(Ze.section,t,e,G.length-1,F);this._liquid.translateSection(L);for(let V=W;V<=H;V++){const j=G[V],X=j.lines,{start_next:z,end_next:Y}=this._getSelectionRuler(Ze.column,t,e,X.length-1,V);this._liquid.translateColumn(j);for(let K=z;K<=Y;K++){const Z=X[K],{divides:ne}=Z,{start_next:ge,end_next:xe}=this._getSelectionRuler(Ze.line,t,e,ne.length-1,K);this._liquid.translateSave(),this._liquid.translateLine(Z,!0,!1);for(let he=ge;he<=xe;he++){const On=ne[he];this._liquid.translateSave(),this._liquid.translateDivide(On);const{glyphGroup:hs}=On,{start_next:ms,end_next:ps}=this._getSelectionRuler(Ze.divide,t,e,hs.length-1,he);let nr=!1,or=!1;v===E&&F===Q&&V===W&&K===z&&he===ge&&(nr=!0),v===C&&F===k&&V===H&&K===Y&&he===xe&&(or=!0),n&&n(ms,ps,nr,or,On,Z,j,L,O),this._liquid.translateRestore()}this._liquid.translateRestore()}}}this._liquid.translateRestore(),this._liquid.translatePage(b,p,f,S)}}}function li(o,t){const{path:e}=o,{path:n}=t;if(e.length!==n.length||e.indexOf("cells")===-1)return!1;const r=e.indexOf("skeTables")+1,s=e.indexOf("rows")+1,a=e.indexOf("cells")+1,{tableId:c,sliceIndex:l}=B.getTableIdAndSliceIndex(e[r]),{tableId:A,sliceIndex:d}=B.getTableIdAndSliceIndex(n[r]);if(c!==A)return!1;const u=e[s],m=n[s],g=e[a],h=n[a];return!(u===m&&g===h&&l===d)}function mo(o,t){const{path:e}=o,{path:n}=t;return e.indexOf("cells")===-1||n.indexOf("cells")===-1||e.length!==n.length?!1:i.Tools.diffValue(e,n)}function di(o,t,e){var g,h,p,f,S,E,C,T;const{path:n}=t,{path:r}=e;if(n.indexOf("cells")===-1||r.indexOf("cells")===-1)return!1;const s=o.findGlyphByPosition(t),a=o.findGlyphByPosition(e),c=(f=(p=(h=(g=s==null?void 0:s.parent)==null?void 0:g.parent)==null?void 0:h.parent)==null?void 0:p.parent)==null?void 0:f.parent,l=(T=(C=(E=(S=a==null?void 0:a.parent)==null?void 0:S.parent)==null?void 0:E.parent)==null?void 0:C.parent)==null?void 0:T.parent;if(c==null||l==null)return!1;const A=c.parent,d=l.parent,u=A.cells.indexOf(c),m=d.cells.indexOf(l);return u===m&&A.index===d.index}function Mn(o,t){if(mo(o,t))return ho(o,t);const{path:e}=o,{path:n}=t,r=e[e.length-5],s=n[n.length-5];if(r!==s&&typeof r=="string"&&typeof s=="string"){const d=r.split("#-#")[1],u=s.split("#-#")[1];return+d<+u}const a=e[e.length-3],c=n[n.length-3],l=e[e.length-1],A=n[n.length-1];return a<c?!0:a>c?!1:l<=A}function ze(o){return o.sections[0].columns[0].lines.length===0}function Ds(o,t,e){let n=t,r=e,s=o[n],a=o[r];for(;n<r&&(ze(s)||ze(a));)ze(s)?(n++,s=o[n]):ze(a)&&(r--,a=o[r]);if(!ze(s)&&!ze(a))return[s,a]}class po{constructor(t,e){M(this,"_liquid",new B.Liquid);this._documentOffsetConfig=t,this._docSkeleton=e}getRangePointData(t,e){const n=[],s=this._docSkeleton.getSkeletonData();if(s==null)return;const{pages:a}=s,{segmentPage:c,page:l,pageType:A}=t,d=this._getTableRectRangeInfo(t,e);if(d==null)return;const{tableId:u,startRowIndex:m,startColumnIndex:g,endRowIndex:h,endColumnIndex:p}=d;this._liquid.reset();const{pageLayoutType:f,pageMarginLeft:S,pageMarginTop:E}=this._documentOffsetConfig,C=A===B.DocumentSkeletonPageType.BODY||A===B.DocumentSkeletonPageType.CELL?l:c;for(let T=0;T<C;T++){const R=a[T];this._liquid.translatePage(R,f,S,E)}for(let T=C;T<a.length;T++){const R=a[T];this._liquid.translatePagePadding(R);const{skeTables:v}=R;let b=null;for(const[O,w]of v.entries())O.startsWith(u)&&(b=w);if(b==null){this._liquid.restorePagePadding(R),this._liquid.translatePage(R,f,S,E);continue}this._liquid.translateSave(),this._liquid.translate(0,b.top);const{x,y}=this._liquid,{left:_}=b;for(const O of b.rows)if(O.index>=m&&O.index<=h){const w=Ds(O.cells,g,p);if(w==null)continue;const[Q,k]=w,F={startX:x+Q.left+_,startY:y+O.top,endX:x+k.left+k.pageWidth+_,endY:y+O.top+O.height};n.push(Wn(F))}this._liquid.translateRestore(),this._liquid.restorePagePadding(R),this._liquid.translatePage(R,f,S,E)}return{pointGroup:n,startRow:m,startColumn:g,endRow:h,endColumn:p,tableId:u}}getNodePositionGroup(t,e){const n=[],r=this._docSkeleton.findCharIndexByPosition(t),s=this._docSkeleton.findCharIndexByPosition(e);if(r==null||s==null)return;const a=r<s,c=this._getTableRectRangeInfo(t,e);if(c==null)return;const{tableId:l,startRowIndex:A,startColumnIndex:d,endRowIndex:u,endColumnIndex:m}=c,g=this._docSkeleton.getViewModel().findTableNodeById(l);if(g==null)return;const h=g.children[0].children.length;if(d===0&&m===h-1){const f=g.children[A].children[d],S=this._docSkeleton.findNodePositionByCharIndex(f.startIndex+1),E=g.children[u].children[m],C=this._docSkeleton.findNodePositionByCharIndex(E.endIndex-2);if(S==null||C==null)return;n.push({anchor:a?S:C,focus:a?C:S})}else for(let f=A;f<=u;f++){const S=g.children[f],E=S.children[d],C=S.children[m],T=this._docSkeleton.findNodePositionByCharIndex(E.startIndex+1),R=this._docSkeleton.findNodePositionByCharIndex(C.endIndex-2);if(T==null||R==null)return;n.push({anchor:a?T:R,focus:a?R:T})}return n}_getTableRectRangeInfo(t,e){const r=this._docSkeleton.getSkeletonData();if(r==null)return;const{pages:s}=r,{path:a}=t,{path:c}=e,l=B.getPageFromPath(r,a),A=B.getPageFromPath(r,c);if(l==null||A==null)return;const d=l.segmentId,u=l.parent.index,m=l.parent.cells.indexOf(l),g=(A==null?void 0:A.parent).index,h=(A==null?void 0:A.parent).cells.indexOf(A),p=Math.min(u,g),f=Math.max(u,g),S=Math.min(m,h),E=Math.max(m,h);return{pages:s,tableId:d,startRowIndex:p,startColumnIndex:S,endRowIndex:f,endColumnIndex:E}}}const Bs="__TestSelectionRange__",_s="__TestSelectionAnchor__",ir=6,Dn=500,Ns=500,Kt=3;function ft(o){const t=o[0],e=t[0],n=t[2],{x:r,y:s}=e,{x:a,y:c}=n;return{left:r,top:s,width:a-r,height:c-s}}function cn(o){return o.map(t=>{let e=1/0,n=-1/0,r=1/0,s=-1/0;return t.forEach(a=>{e=Math.min(a.x,e),n=Math.max(a.x,n),s=Math.max(a.y,s),r=Math.min(a.y,r)}),{left:e,right:n,top:r,bottom:s}})}class je{constructor(t,e,n,r,s,a=B.NORMAL_TEXT_SELECTION_PLUGIN_STYLE,c="",l=-1){M(this,"rangeType",i.DOC_RANGE_TYPE.TEXT);M(this,"_current",!1);M(this,"_rangeShape");M(this,"_anchorShape");M(this,"_cursorList",[]);M(this,"_anchorBlinkTimer",null);this._scene=t,this._document=e,this._docSkeleton=n,this.anchorNodePosition=r,this.focusNodePosition=s,this.style=a,this._segmentId=c,this._segmentPage=l,this._anchorBlink(),this.refresh()}_anchorBlink(){setTimeout(()=>{this._anchorShape&&this._anchorShape.visible&&this.deactivateStatic()},Dn),this._anchorBlinkTimer=setInterval(()=>{this._anchorShape&&this._anchorShape.visible&&(this.activeStatic(),setTimeout(()=>{this.deactivateStatic()},Dn))},Ns+Dn)}get startOffset(){var r;const{startOffset:t}=(r=jn(this._cursorList))!=null?r:{},e=this._docSkeleton.getViewModel().getDataModel().getSelfOrHeaderFooterModel(this._segmentId).getBody();if(t==null||e==null)return t;const n=e.dataStream.length-2;return Math.min(n,t)}get endOffset(){var r;const{endOffset:t}=(r=jn(this._cursorList))!=null?r:{},e=this._docSkeleton.getViewModel().getDataModel().getSelfOrHeaderFooterModel(this._segmentId).getBody();if(t==null||e==null)return t;const n=e.dataStream.length-2;return Math.min(t,n)}get collapsed(){const{startOffset:t,endOffset:e}=this;return t!=null&&t===e}get startNodePosition(){if(this.anchorNodePosition==null)return null;if(this.focusNodePosition==null)return this.anchorNodePosition;const{start:t}=Vn(this.anchorNodePosition,this.focusNodePosition);return t}get endNodePosition(){if(this.anchorNodePosition==null)return this.focusNodePosition;if(this.focusNodePosition==null)return null;const{end:t}=Vn(this.anchorNodePosition,this.focusNodePosition);return t}get direction(){const{collapsed:t,anchorNodePosition:e,focusNodePosition:n}=this;return t||e==null||n==null?i.RANGE_DIRECTION.NONE:ho(e,n)?i.RANGE_DIRECTION.FORWARD:i.RANGE_DIRECTION.BACKWARD}get segmentId(){return this._segmentId}get segmentPage(){return this._segmentPage}getAbsolutePosition(){const t=this.anchorNodePosition,e=this.focusNodePosition;if(this._isEmpty())return;const n=this._document.getOffsetConfig(),{docsLeft:r,docsTop:s}=n,a=new ve(n,this._docSkeleton);if(this._isCollapsed()){const{contentBoxPointGroup:d,cursorList:u}=a.getRangePointData(t,t);if(this._setCursorList(u),d.length===0)return;const m=ft(d);return{...m,left:m.left+r,top:m.top+s}}const{borderBoxPointGroup:c,cursorList:l}=a.getRangePointData(t,e);if(this._setCursorList(l),c.length===0)return;const A=ft(c);return{...A,left:A.left+r,top:A.top+s}}getAnchor(){return this._anchorShape}activeStatic(){var t,e;(e=this._anchorShape)==null||e.setProps({stroke:((t=this.style)==null?void 0:t.strokeActive)||B.getColor(i.COLORS.black,1)})}deactivateStatic(){var t,e;(e=this._anchorShape)==null||e.setProps({stroke:((t=this.style)==null?void 0:t.stroke)||B.getColor(i.COLORS.black,0)})}isActive(){return this._current===!0}activate(){this._current=!0}deactivate(){this._current=!1}dispose(){var t,e;(t=this._rangeShape)==null||t.dispose(),this._rangeShape=null,(e=this._anchorShape)==null||e.dispose(),this._anchorShape=null,this._anchorBlinkTimer&&(clearInterval(this._anchorBlinkTimer),this._anchorBlinkTimer=null)}isIntersection(t){const{startOffset:e,endOffset:n}=this,{startOffset:r,endOffset:s}=t;return e==null||n==null||r==null||s==null?!1:e<=s&&n>=r}refresh(){var u,m;const{_document:t,_docSkeleton:e}=this,n=this.anchorNodePosition,r=this.focusNodePosition;if((u=this._anchorShape)==null||u.hide(),(m=this._rangeShape)==null||m.hide(),this._isEmpty())return;const s=t.getOffsetConfig(),{docsLeft:a,docsTop:c}=s,l=new ve(s,e);if(this._isCollapsed()){const{contentBoxPointGroup:g,cursorList:h}=l.getRangePointData(n,n);if(this._setCursorList(h),g.length>0){const p=e.findGlyphByPosition(n);this._createOrUpdateAnchor(g,a,c,p)}return}const{borderBoxPointGroup:A,cursorList:d}=l.getRangePointData(n,r);this._setCursorList(d),A.length>0&&this._createOrUpdateRange(A,a,c)}_isEmpty(){return this.anchorNodePosition==null&&this.focusNodePosition==null}_isCollapsed(){const t=this.anchorNodePosition,e=this.focusNodePosition;if(t!=null&&e==null)return!0;if(t==null||e==null)return!1;const n=Object.keys(Ze);for(const r of n){const s=t[r],a=e[r];if(s!==a)return!1}return t.isBack===e.isBack}_createOrUpdateRange(t,e,n){var a;if(this._rangeShape){this._rangeShape.translate(e,n),this._rangeShape.updatePointGroup(t),this._rangeShape.show();return}const r=.3,s=new B.RegularPolygon(Bs+i.Tools.generateRandomId(ir),{pointsGroup:t,fill:((a=this.style)==null?void 0:a.fill)||B.getColor(i.COLORS.black,r),left:e,top:n,evented:!1,debounceParentDirty:!1});this._rangeShape=s,this._scene.addObject(s,Kt)}_createOrUpdateAnchor(t,e,n,r){var p,f,S;const s=ft(t),{left:a}=s;let{top:c,height:l}=s;const A=14;l<A&&(c-=A-l,l=A);const d=12;let u=a+e;const m=c+n,g=((p=r==null?void 0:r.ts)==null?void 0:p.it)===i.BooleanNumber.TRUE;if(g&&(u+=l*Math.tan(d*Math.PI/180)/2),this._anchorShape){this._anchorShape.transformByState({left:u,top:m,height:l}),this._anchorShape.show(),g?this._anchorShape.skew(-12,0):this._anchorShape.skew(0,0);return}const h=new B.Rect(_s+i.generateRandomId(ir),{left:u,top:m,height:l,strokeWidth:((f=this.style)==null?void 0:f.strokeWidth)||1,stroke:((S=this.style)==null?void 0:S.strokeActive)||B.getColor(i.COLORS.black,1),evented:!1});g&&h.skew(-12,0),this._anchorShape=h,this._scene.addObject(h,Kt),this.activeStatic()}_setCursorList(t){t.length!==0&&(this._cursorList=t)}}const Ps="__DocTableRectRange__",ws=6;function Wt(o,t,e,n,r,s=B.NORMAL_TEXT_SELECTION_PLUGIN_STYLE,a="",c=-1){const l=t.getOffsetConfig(),d=new po(l,e).getNodePositionGroup(n,r);return(d!=null?d:[]).map(u=>new fo(o,t,e,u.anchor,u.focus,s,a,c))}class fo{constructor(t,e,n,r,s,a=B.NORMAL_TEXT_SELECTION_PLUGIN_STYLE,c="",l=-1){M(this,"rangeType",i.DOC_RANGE_TYPE.RECT);M(this,"_rangeShape");M(this,"_current",!1);M(this,"_startRow");M(this,"_startCol");M(this,"_endRow");M(this,"_endCol");M(this,"_tableId");this._scene=t,this._document=e,this._docSkeleton=n,this.anchorNodePosition=r,this.focusNodePosition=s,this.style=a,this._segmentId=c,this._segmentPage=l,this.refresh()}get startOffset(){const{startNodePosition:t}=this;return this._docSkeleton.findCharIndexByPosition(t)}get endOffset(){const{endNodePosition:t}=this;return this._docSkeleton.findCharIndexByPosition(t)}get collapsed(){return!1}get startRow(){return this._startRow}get startColumn(){return this._startCol}get endRow(){return this._endRow}get endColumn(){return this._endCol}get tableId(){return this._tableId}get segmentId(){return this._segmentId}get segmentPage(){return this._segmentPage}get spanEntireRow(){var a;const e=(a=this._docSkeleton.getViewModel().getSnapshot().tableSource)==null?void 0:a[this._tableId],{_startCol:n,_endCol:r}=this;if(e==null)throw new Error("Table is not found.");const{tableColumns:s}=e;return n===0&&r===s.length-1}get spanEntireColumn(){var a;const e=(a=this._docSkeleton.getViewModel().getSnapshot().tableSource)==null?void 0:a[this._tableId],{_startRow:n,_endRow:r}=this;if(e==null)throw new Error("Table is not found.");const{tableRows:s}=e;return n===0&&r===s.length-1}get spanEntireTable(){return this.spanEntireRow&&this.spanEntireColumn}get startNodePosition(){const{anchorNodePosition:t,focusNodePosition:e}=this;return Mn(t,e)?t:e}get endNodePosition(){const{anchorNodePosition:t,focusNodePosition:e}=this;return Mn(t,e)?e:t}get direction(){const{anchorNodePosition:t,focusNodePosition:e}=this;return Mn(t,e)?i.RANGE_DIRECTION.FORWARD:i.RANGE_DIRECTION.BACKWARD}isActive(){return this._current===!0}activate(){this._current=!0}deactivate(){this._current=!1}dispose(){var t;(t=this._rangeShape)==null||t.dispose(),this._rangeShape=null}isIntersection(t){const{startRow:e,startColumn:n,endRow:r,endColumn:s}=this,{startRow:a,startColumn:c,endRow:l,endColumn:A}=t,d={left:n,top:e,right:s,bottom:r},u={left:c,top:a,right:A,bottom:l};return i.Rectangle.hasIntersectionBetweenTwoRect(d,u)}refresh(){var f;(f=this._rangeShape)==null||f.hide();const{startNodePosition:t,endNodePosition:e,_document:n,_docSkeleton:r}=this,s=n.getOffsetConfig(),{docsLeft:a,docsTop:c}=s,A=new po(s,r).getRangePointData(t,e);if(A==null)return;const{pointGroup:d=[],startRow:u,endRow:m,startColumn:g,endColumn:h,tableId:p}=A;(d==null?void 0:d.length)>0&&this._createOrUpdateRange(d,a,c),this._updateTableInfo(u,m,g,h,p)}_updateTableInfo(t,e,n,r,s){this._startRow=t,this._endRow=e,this._startCol=n,this._endCol=r,this._tableId=s}_createOrUpdateRange(t,e,n){var a;if(this._rangeShape){this._rangeShape.translate(e,n),this._rangeShape.updatePointGroup(t),this._rangeShape.show();return}const r=.3,s=new B.RegularPolygon(Ps+i.Tools.generateRandomId(ws),{pointsGroup:t,fill:((a=this.style)==null?void 0:a.fill)||B.getColor(i.COLORS.black,r),left:e,top:n,evented:!1,debounceParentDirty:!1});this._rangeShape=s,this._scene.addObject(s,Kt)}}function sr(o,t,e,n,r,s,a,c,l=!0,A=!0){const d=r.findNodePositionByCharIndex(o,l,a,c),u=r.findNodePositionByCharIndex(t,A,a,c);if(!(d==null||u==null))return new je(e,n,r,d,u,s,a,c)}function Us(o,t,e,n,r,s,a,c){const l=r.findNodePositionByCharIndex(o,!0,a,c),A=r.findNodePositionByCharIndex(t,!0,a,c);if(!(l==null||A==null))return new fo(e,n,r,l,A,s,a,c)}function Ls(o,t,e,n,r,s,a,c){const l=r.findNodePositionByCharIndex(o,!0,a,c),A=r.findNodePositionByCharIndex(t,!0,a,c);if(!(l==null||A==null))return Gn(l,A,e,n,r,s,a,c)}function Gn(o,t,e,n,r,s,a,c){const l=[],A=[],d=[e,n,r,o,t,s,a,c];if(di(r,o,t)){if(mo(o,t))return l.push(new je(...d)),{textRanges:l,rectRanges:A};{const C=Wt(...d);return A.push(...C),{textRanges:l,rectRanges:A}}}if(li(o,t)){const C=Wt(...d);return A.push(...C),{textRanges:l,rectRanges:A}}const u=r.getViewModel().getSelfOrHeaderFooterViewModel(a),m=r.findCharIndexByPosition(o),g=r.findCharIndexByPosition(t);if(m==null||g==null)return;const h=m<=g?i.RANGE_DIRECTION.FORWARD:i.RANGE_DIRECTION.BACKWARD,p=Math.min(m,g),f=Math.max(m,g);let S=p,E=f;for(const C of u.getChildren())for(const T of C.children){const{startIndex:R,endIndex:v,children:b}=T,x=C.children.indexOf(T),y=C.children[x+1],_=b[0];let O=!1;if(_){const{startIndex:w,endIndex:Q,children:k}=_;let F=null,L=null;const G=k.find(H=>H.startIndex<=p&&H.endIndex>=p),W=k.find(H=>H.startIndex<=f&&H.endIndex>=f);if(p>w&&p<Q)F=r.findNodePositionByCharIndex(G.startIndex+2,!0,a,c),L=r.findNodePositionByCharIndex(Q-4,!0,a,c),S=Q+1;else if(f>w&&f<Q)F=r.findNodePositionByCharIndex(w+3,!0,a,c),L=r.findNodePositionByCharIndex(W.endIndex-3,!0,a,c),E=w-1,O=!0;else if(w>p&&Q<f){if(F=r.findNodePositionByCharIndex(w+3,!0,a,c),L=r.findNodePositionByCharIndex(Q-4,!0,a,c),S<=w-1){const H=r.findNodePositionByCharIndex(S,!0,a,c),V=r.findNodePositionByCharIndex(w-1,!1,a,c),j=h===i.RANGE_DIRECTION.FORWARD?H:V,X=h===i.RANGE_DIRECTION.FORWARD?V:H;l.push(new je(e,n,r,j,X,s,a,c))}S=Q+1}if(F&&L){const H=h===i.RANGE_DIRECTION.FORWARD?F:L,V=h===i.RANGE_DIRECTION.FORWARD?L:F;A.push(...Wt(e,n,r,H,V,s,a,c))}}if(E===v+1&&!O&&y&&y.children.length&&(E=v,O=!0),E>=R&&E<=v||O){const w=r.findNodePositionByCharIndex(S,!0,a,c),Q=r.findNodePositionByCharIndex(E,!O,a,c),k=h===i.RANGE_DIRECTION.FORWARD?w:Q,F=h===i.RANGE_DIRECTION.FORWARD?Q:w;if(A.length&&i.Tools.diffValue(k,F))continue;l.push(new je(e,n,r,k,F,s,a,c))}}return{textRanges:l,rectRanges:A}}function Ai(o){const t=o==null?void 0:o.getCanvasElement();if(!t)return{left:0,top:0};const{top:e,left:n}=B.getOffsetRectForDom(t);return{left:n,top:e}}function ar(o){var l;const t=(l=o.parent)==null?void 0:l.parent,e=t==null?void 0:t.parent;if(t==null||e==null)return;const{paragraphIndex:n}=t,r=e.lines.filter(A=>A.paragraphIndex===n);let s=-1,a="",c=!1;for(const A of r)for(const d of A.divides)for(const u of d.glyphGroup)c||(s+=u.count),u===o&&(c=!0),a+=u.count>0?u.content:"";return{st:r[0].st,ed:n,content:a,nodeIndex:s}}function Xn(o){const{startOffset:t,endOffset:e,collapsed:n,rangeType:r,startNodePosition:s,endNodePosition:a,direction:c,segmentId:l,segmentPage:A}=o;return{startOffset:t,endOffset:e,collapsed:n,rangeType:r,startNodePosition:s,endNodePosition:a,direction:c,segmentId:l,segmentPage:A,isActive:o.isActive()}}function Qs(o){const t=Xn(o),{startRow:e,startColumn:n,endRow:r,endColumn:s,tableId:a,spanEntireRow:c,spanEntireColumn:l,spanEntireTable:A}=o;return{...t,startRow:e,startColumn:n,endRow:r,endColumn:s,tableId:a,spanEntireRow:c,spanEntireColumn:l,spanEntireTable:A}}var Fs=Object.getOwnPropertyDescriptor,Hs=(o,t,e,n)=>{for(var r=n>1?void 0:n?Fs(t,e):t,s=o.length-1,a;s>=0;s--)(a=o[s])&&(r=a(r)||r);return r},wt=(o,t)=>(e,n)=>t(e,n,o);exports.DocSelectionRenderService=class extends i.RxDisposable{constructor(e,n,r,s,a){super();M(this,"_onInputBefore$",new U.Subject);M(this,"onInputBefore$",this._onInputBefore$.asObservable());M(this,"_onKeydown$",new U.Subject);M(this,"onKeydown$",this._onKeydown$.asObservable());M(this,"_onInput$",new U.Subject);M(this,"onInput$",this._onInput$.asObservable());M(this,"_onCompositionstart$",new U.BehaviorSubject(null));M(this,"onCompositionstart$",this._onCompositionstart$.asObservable());M(this,"_onCompositionupdate$",new U.BehaviorSubject(null));M(this,"onCompositionupdate$",this._onCompositionupdate$.asObservable());M(this,"_onCompositionend$",new U.BehaviorSubject(null));M(this,"onCompositionend$",this._onCompositionend$.asObservable());M(this,"_onSelectionStart$",new U.BehaviorSubject(null));M(this,"onSelectionStart$",this._onSelectionStart$.asObservable());M(this,"onChangeByEvent$",U.merge(this._onInput$,this._onKeydown$.pipe(U.filter(e=>e.event.keyCode===I.KeyCode.BACKSPACE)),this._onCompositionend$));M(this,"_onPaste$",new U.Subject);M(this,"onPaste$",this._onPaste$.asObservable());M(this,"_textSelectionInner$",new U.BehaviorSubject(null));M(this,"textSelectionInner$",this._textSelectionInner$.asObservable());M(this,"_onFocus$",new U.Subject);M(this,"onFocus$",this._onFocus$.asObservable());M(this,"_onBlur$",new U.Subject);M(this,"onBlur$",this._onBlur$.asObservable());M(this,"_onPointerDown$",new U.Subject);M(this,"onPointerDown$",this._onPointerDown$.asObservable());M(this,"_container");M(this,"_inputParent");M(this,"_input");M(this,"_scrollTimers",[]);M(this,"_rangeList",[]);M(this,"_rangeListCache",[]);M(this,"_rectRangeList",[]);M(this,"_rectRangeListCache",[]);M(this,"_anchorNodePosition",null);M(this,"_focusNodePosition",null);M(this,"_currentSegmentId","");M(this,"_currentSegmentPage",-1);M(this,"_selectionStyle",B.NORMAL_TEXT_SELECTION_PLUGIN_STYLE);M(this,"_onPointerEvent",!1);M(this,"_viewPortObserverMap",new Map);M(this,"_isIMEInputApply",!1);M(this,"_scenePointerMoveSubs",[]);M(this,"_scenePointerUpSubs",[]);M(this,"_reserveRanges",!1);this._context=e,this._layoutService=n,this._logService=r,this._univerInstanceService=s,this._docSkeletonManagerService=a,this._initDOM(),this._registerContainer(),this._setSystemHighlightColorToStyle(),this._listenCurrentUnitChange()}get isOnPointerEvent(){return this._onPointerEvent}get isFocusing(){return this._input===document.activeElement}get canFocusing(){return this.isFocusing||document.activeElement===document.body||document.activeElement===null}_listenCurrentUnitChange(){this._univerInstanceService.getCurrentTypeOfUnit$(i.UniverInstanceType.UNIVER_DOC).pipe(U.takeUntil(this.dispose$)).subscribe(e=>{if(e==null)return;e.getUnitId()!==this._context.unitId&&!this._reserveRanges&&this.removeAllRanges()})}get activeViewPort(){return this._context.scene.getViewports()[0]}setSegment(e){this._currentSegmentId=e}getSegment(){return this._currentSegmentId}setSegmentPage(e){this._currentSegmentPage=e}getSegmentPage(){return this._currentSegmentPage}setReserveRangesStatus(e){this._reserveRanges=e}_setRangeStyle(e=B.NORMAL_TEXT_SELECTION_PLUGIN_STYLE){this._selectionStyle=e}addDocRanges(e,n=!0,r){const{_currentSegmentId:s,_currentSegmentPage:a,_selectionStyle:c}=this,{scene:l,mainComponent:A}=this._context,d=A,u=this._docSkeletonManagerService.getSkeleton(),m=(g,h)=>{const p=Ls(g,h,l,d,u,c,s,a);if(p==null)return;const{textRanges:f,rectRanges:S}=p;for(const E of f)this._addTextRange(E);this._addRectRanges(S)};for(const g of e){const{startOffset:h,endOffset:p,rangeType:f,startNodePosition:S,endNodePosition:E}=g;if(f===i.DOC_RANGE_TYPE.RECT){const C=Us(h,p,l,d,u,c,s,a);C&&this._addRectRanges([C])}else if(f===i.DOC_RANGE_TYPE.TEXT)try{let C=null;S&&E?C=sr(S.isBack?h:h-1,E.isBack?p:p-1,l,d,u,c,s,a,S.isBack,E.isBack):C=sr(h,p,l,d,u,c,s,a),C&&this._addTextRange(C)}catch{m(h,p)}else m(h,p)}this._textSelectionInner$.next({textRanges:this._getAllTextRanges(),rectRanges:this._getAllRectRanges(),segmentId:s,segmentPage:a,style:c,isEditing:n,options:r}),!(!e.length||(r==null?void 0:r.shouldFocus)===!1)&&this._updateInputPosition(r==null?void 0:r.forceFocus)}setCursorManually(e,n){const r=this._findNodeByCoord(e,n,{strict:!0,segmentId:this._currentSegmentId,segmentPage:this._currentSegmentPage}),s=this._getNodePosition(r);if(s==null){this._removeAllRanges();return}(r==null?void 0:r.node.streamType)===i.DataStreamTreeTokenType.PARAGRAPH&&(s.isBack=!0),this._createTextRangeByAnchorPosition(s),this._textSelectionInner$.next({textRanges:this._getAllTextRanges(),rectRanges:this._getAllRectRanges(),segmentId:this._currentSegmentId,segmentPage:this._currentSegmentPage,style:this._selectionStyle,isEditing:!1})}sync(){this._updateInputPosition()}activate(e,n,r=!1){this._container.style.left=`${e}px`,this._container.style.top=`${n}px`,this._container.style.zIndex="1000",(this.canFocusing||r)&&this.focus()}hasFocus(){return document.activeElement===this._input}focus(){this._input.focus()}blur(){this._input.blur()}deactivate(){this._container.style.left="0px",this._container.style.top="0px"}__handleDblClick(e){const{offsetX:n,offsetY:r}=e,s=this._findNodeByCoord(n,r,{strict:!1,segmentId:this._currentSegmentId,segmentPage:this._currentSegmentPage});if(s==null||s.node==null)return;const a=ar(s.node);if(a==null)return;const{content:c,st:l,nodeIndex:A}=a;if(A===-1||Intl.Segmenter==null)return;const u=new Intl.Segmenter(void 0,{granularity:"word"}).segment(c);let m=Number.NEGATIVE_INFINITY,g=Number.NEGATIVE_INFINITY;for(const{segment:h,index:p,isWordLike:f}of u)if(p<=A&&A<p+h.length&&f){m=p+l,g=p+l+h.length;break}if(Number.isFinite(m)&&Number.isFinite(g)){this.removeAllRanges();const h=[{startOffset:m,endOffset:g}];this.addDocRanges(h,!1,{forceFocus:!0})}}__handleTripleClick(e){const{offsetX:n,offsetY:r}=e,s=this._findNodeByCoord(n,r,{strict:!1,segmentId:this._currentSegmentId,segmentPage:this._currentSegmentPage});if(s==null||s.node==null)return;const a=ar(s.node);if(a==null)return;this.removeAllRanges();const{st:c,ed:l}=a,A=[{startOffset:c,endOffset:l}];this.addDocRanges(A,!1,{forceFocus:!0})}__onPointDown(e){var f,S;const{scene:n,mainComponent:r}=this._context,s=this._docSkeletonManagerService.getSkeleton(),{offsetX:a,offsetY:c}=e,l=this._findNodeByCoord(a,c,{strict:!1,segmentId:this._currentSegmentId,segmentPage:this._currentSegmentPage}),A=this._getNodePosition(l);if(A==null||l==null){this._removeAllRanges();return}(l==null?void 0:l.node.streamType)===i.DataStreamTreeTokenType.PARAGRAPH&&(A.isBack=!0);const d=this._textSelectionInner$.value;if(l&&e.button===2&&d){const E=s.findCharIndexByPosition(A);if(typeof E=="number"&&d.textRanges.some(C=>C.startOffset<=E&&C.endOffset>E)||typeof E=="number"&&d.rectRanges.some(C=>C.startOffset<=E&&C.endOffset>=E))return}const{segmentId:u,segmentPage:m}=l;u&&this._currentSegmentId&&u!==this._currentSegmentId&&this.setSegment(u),u&&m!==this._currentSegmentPage&&this.setSegmentPage(m),this._anchorNodePosition=A,e.shiftKey&&this._getActiveRangeInstance()?this._updateActiveRangePosition(A):e.ctrlKey?this._removeAllCollapsedTextRanges():this._isEmpty()||this._removeAllRanges(),n.disableObjectsEvent();const g=B.ScrollTimer.create(n);this._scrollTimers.push(g),g.startScroll(a,c),this._onSelectionStart$.next((f=this._getActiveRangeInstance())==null?void 0:f.startNodePosition),(S=n.getTransformer())==null||S.clearSelectedObjects();let h=a,p=c;this._onPointerEvent=!0,this._scenePointerMoveSubs.push(n.onPointerMove$.subscribeEvent(E=>{const{offsetX:C,offsetY:T}=E;n.setCursor(B.CURSOR_TYPE.TEXT),!(Math.sqrt((C-h)**2+(T-p)**2)<3)&&(this._moving(C,T),g.scrolling(C,T,()=>{this._moving(C,T)}),h=C,p=T)})),this._scenePointerUpSubs.push(n.onPointerUp$.subscribeEvent(()=>{if([...this._scenePointerMoveSubs,...this._scenePointerUpSubs].forEach(C=>{C.unsubscribe()}),this._onPointerEvent=!1,n.enableObjectsEvent(),this._anchorNodePosition&&!this._focusNodePosition){if(e.ctrlKey){this._disposeScrollTimers();return}const C=new je(n,r,s,this._anchorNodePosition,void 0,this._selectionStyle,this._currentSegmentId,this._currentSegmentPage);this._addTextRange(C)}else if(this._anchorNodePosition&&this._focusNodePosition){for(const C of this._rangeListCache)e.ctrlKey?C.collapsed?C.dispose():this._addTextRange(C):this._addTextRange(C);this._addRectRanges(this._rectRangeListCache),this._rangeListCache=[],this._rectRangeListCache=[]}this._anchorNodePosition=null,this._focusNodePosition=null;const E={textRanges:this._getAllTextRanges(),rectRanges:this._getAllRectRanges(),segmentId:this._currentSegmentId,segmentPage:this._currentSegmentPage,style:this._selectionStyle,isEditing:!1};this._textSelectionInner$.next(E),this._disposeScrollTimers(),this._updateInputPosition(!0)}))}removeAllRanges(){this._removeAllRanges(),this.deactivate()}getActiveTextRange(){return this._getActiveRangeInstance()}_disposeScrollTimers(){this._scrollTimers.forEach(e=>{e==null||e.dispose()}),this._scrollTimers=[]}_setSystemHighlightColorToStyle(){const{r:e,g:n,b:r,a:s}=B.getSystemHighlightColor(),a={strokeWidth:1,stroke:"rgba(0, 0, 0, 0)",strokeActive:"rgba(0, 0, 0, 1)",fill:`rgba(${e}, ${n}, ${r}, ${s!=null?s:.3})`};this._setRangeStyle(a)}_getAllTextRanges(){return this._rangeList.map(Xn)}_getAllRectRanges(){return this._rectRangeList.map(Qs)}getAllTextRanges(){return this._getAllTextRanges()}getAllRectRanges(){return this._getAllRectRanges()}_getActiveRange(){const e=this._rangeList.find(s=>s.isActive());if(e==null)return null;const{startOffset:n,endOffset:r}=e;return n==null||r==null?null:Xn(e)}_getActiveRangeInstance(){return this._rangeList.find(e=>e.isActive())}dispose(){super.dispose(),this._detachEvent(),this._removeAllRanges(),this._container.remove()}_initDOM(){const{unitId:e}=this._context,n=document.createElement("div");n.style.position="fixed",n.style.left="0px",n.style.top="0px",n.id=`univer-doc-selection-container-${e}`;const r=document.createElement("div"),s=document.createElement("div");r.appendChild(s),n.appendChild(r),this._container=n,this._inputParent=r,this._input=s,this._initInput(),this._initInputEvents(),document.body.appendChild(n)}_registerContainer(){this.disposeWithMe(this._layoutService.registerContainerElement(this._container))}_initInput(){this._inputParent.style.cssText=` position:absolute; height:1px; width:1px; overflow: hidden; `,this._input.contentEditable="true",this._input.dataset.uComp="editor",this._input.id=`__editor_${this._context.unitId}`,this._input.style.cssText=` position: absolute; overflow: hidden; opacity: 1; background: #000; color: transparent; outline: none; z-index: -2; caret-color: transparent; white-space: pre-wrap; user-select: text; `}_getNodePosition(e){if(e==null)return;const{node:n,ratioX:r,segmentPage:s}=e,c=this._docSkeletonManagerService.getSkeleton().findPositionByGlyph(n,s);if(c==null)return;let A=r<.5;return n.glyphType===B.GlyphType.LIST&&(A=!0),{...c,isBack:A}}_interactTextRanges(e){const n=[];for(const r of this._rangeList){if(e.some(s=>s.isIntersection(r))){r.dispose();continue}n.push(r)}this._rangeList=n}_interactRectRanges(e){const n=[];for(const r of this._rectRangeList){if(e.some(s=>s.isIntersection(r))){r.dispose();continue}n.push(r)}this._rectRangeList=n}_removeAllRanges(){this._removeAllTextRanges(),this._removeAllRectRanges()}_removeAllCacheRanges(){this._rangeListCache.forEach(e=>{e.dispose()}),this._rectRangeListCache.forEach(e=>{e.dispose()}),this._rangeListCache=[],this._rectRangeListCache=[]}_removeAllTextRanges(){this._rangeList.forEach(e=>{e.dispose()}),this._rangeList=[]}_removeAllRectRanges(){this._rectRangeList.forEach(e=>{e.dispose()}),this._rectRangeList=[]}_removeAllCollapsedTextRanges(){for(const e of this._rangeList)e.collapsed&&e.dispose()}_deactivateAllTextRanges(){this._rangeList.forEach(e=>{e.deactivate()})}_deactivateAllRectRanges(){this._rectRangeList.forEach(e=>{e.deactivate()})}_addTextRangesToCache(e){this._rangeListCache.push(...e)}_addTextRange(e){this._deactivateAllTextRanges(),e.activate(),this._rangeList.push(e)}_addRectRangesToCache(e){this._rectRangeListCache.push(...e)}_addRectRanges(e){e.length!==0&&(this._deactivateAllRectRanges(),e[e.length-1].activate(),this._rectRangeList.push(...e))}_createTextRangeByAnchorPosition(e){this._removeAllRanges();const{scene:n,mainComponent:r}=this._context,s=this._docSkeletonManagerService.getSkeleton(),a=new je(n,r,s,e,void 0,this._selectionStyle,this._currentSegmentId,this._currentSegmentPage);this._addTextRange(a)}_updateActiveRangePosition(e){const n=this._getActiveRangeInstance();if(n==null||n.anchorNodePosition==null){this._logService.error("[DocSelectionRenderService] _updateActiveRangeFocusPosition: active range has no anchor");return}this._removeAllRanges(),this._anchorNodePosition=n.anchorNodePosition,this._focusNodePosition=e;const{scene:r,mainComponent:s}=this._context,a=this._docSkeletonManagerService.getSkeleton(),{_anchorNodePosition:c,_focusNodePosition:l,_selectionStyle:A,_currentSegmentId:d,_currentSegmen