@univerjs/docs-ui 0.9.2-nightly.202507181608 → 0.9.3-nightly.202507250750

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/lib/cjs/index.js CHANGED
@@ -1,4 +1,4 @@
1
- "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"),co=require("@univerjs/drawing"),U=require("rxjs"),mt=require("rxjs/operators"),N=require("react/jsx-runtime"),P=require("react"),q=require("@univerjs/design");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}}const Os="doc.command.tab",We={id:Os,type:i.CommandType.COMMAND,async handler(o,t){const n=o.get(exports.DocAutoFormatService).onAutoFormat(We.id,t);return(await i.sequenceExecuteAsync(n,o.get(i.ICommandService))).result}},Ms="doc.command.after-space",Tt={id:Ms,type:i.CommandType.COMMAND,async handler(o){const e=o.get(exports.DocAutoFormatService).onAutoFormat(Tt.id);return(await i.sequenceExecuteAsync(e,o.get(i.ICommandService))).result}},et={id:"doc.command.enter",type:i.CommandType.COMMAND,async handler(o){const e=o.get(exports.DocAutoFormatService).onAutoFormat(et.id);return(await i.sequenceExecuteAsync(e,o.get(i.ICommandService))).result}};var Ds=Object.getOwnPropertyDescriptor,Bs=(o,t,e,n)=>{for(var r=n>1?void 0:n?Ds(t,e):t,s=o.length-1,a;s>=0;s--)(a=o[s])&&(r=a(r)||r);return r},Mn=(o,t)=>(e,n)=>t(e,n,o);const _s=11,Ns=9,wt={ff:"Arial",fs:_s};let ve=class extends i.Disposable{constructor(t,e,n){super();M(this,"_cacheStyle",null);this._textSelectionManagerService=t,this._univerInstanceService=e,this._renderManagerService=n,this._init()}_init(){this._listenDocRangeChange()}_listenDocRangeChange(){this.disposeWithMe(this._textSelectionManagerService.textSelection$.subscribe(()=>{this._clearStyleCache()}))}getStyleCache(){return this._cacheStyle}getDefaultStyle(){var a;const t=this._univerInstanceService.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC);if(t==null)return{...wt};const e=t==null?void 0:t.getUnitId(),n=(a=this._renderManagerService.getRenderById(e))==null?void 0:a.with(D.DocSkeletonManagerService),r=n==null?void 0:n.getViewModel();return r==null?{...wt}:r.getEditArea()===B.DocumentEditArea.BODY?{...wt}:{...wt,fs:Ns}}setStyleCache(t){this._cacheStyle={...this._cacheStyle,...t}}_clearStyleCache(){this._cacheStyle=null}};ve=Bs([Mn(0,i.Inject(D.DocSelectionManagerService)),Mn(1,i.IUniverInstanceService),Mn(2,B.IRenderManagerService)],ve);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")}function uo(o,t,e){const n=[];for(let r=0,s=o.length;r<s;r++)o[r]===i.DataStreamTreeTokenType.PARAGRAPH&&n.push({startIndex:r});if(t)for(const r of n)t.bullet&&(r.bullet=i.Tools.deepClone(t.bullet)),t.paragraphStyle&&(r.paragraphStyle=i.Tools.deepClone(t.paragraphStyle),delete r.paragraphStyle.borderBottom,t.paragraphStyle.headingId&&(r.paragraphStyle.headingId=i.generateRandomId(6)));if(e)for(const r of n)r.paragraphStyle||(r.paragraphStyle={}),r.paragraphStyle.borderBottom=e;return n}const cn={id:"doc.command.break-line",type:i.CommandType.COMMAND,handler:(o,t)=>{var L,G,W,H,V;const e=o.get(D.DocSelectionManagerService),n=o.get(i.IUniverInstanceService),r=o.get(i.ICommandService),s=o.get(ve),a=(L=t==null?void 0:t.textRange)!=null?L:e.getActiveTextRange(),c=e.getRectRanges();if(a==null)return!1;if(c&&c.length){const{startOffset:j}=a;return e.replaceDocRanges([{startOffset:j,endOffset:j}]),!0}const{horizontalLine:l}=t!=null?t:{},{segmentId:A}=a,d=n.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),u=d==null?void 0:d.getSelfOrHeaderFooterModel(A!=null?A:"").getBody();if(d==null||u==null)return!1;const m=d.getUnitId(),{startOffset:g,endOffset:h}=a,f=((G=u.paragraphs)!=null?G:[]).find(j=>j.startIndex>=g);if(!f)return!1;const S=g===f.startIndex,E=f.startIndex,C=s.getDefaultStyle(),T=s.getStyleCache(),R=an(u,h,C,T),v={dataStream:i.DataStreamTreeTokenType.PARAGRAPH,paragraphs:uo(i.DataStreamTreeTokenType.PARAGRAPH,f,l),textRuns:[{st:0,ed:1,ts:{...R.ts}}]};if(d==null)return!1;const b=e.getActiveTextRange();if(u==null)return!1;const{collapsed:x}=a,y=v.dataStream.length,_=[{startOffset:g+y,endOffset:g+y,style:b==null?void 0:b.style,collapsed:x}],O={id:D.RichTextEditingMutation.id,params:{unitId:m,actions:[],textRanges:_,debounce:!0}},w=new i.TextX,Q=i.JSONX.getInstance();if(x)g>0&&w.push({t:i.TextXActionType.RETAIN,len:g}),w.push({t:i.TextXActionType.INSERT,body:v,len:v.dataStream.length});else{const j=i.BuildTextUtils.selection.delete([a],u,0,v);w.push(...j)}(((W=f.bullet)==null?void 0:W.listType)===i.PresetListType.CHECK_LIST_CHECKED||(H=f.paragraphStyle)!=null&&H.headingId)&&(a.endOffset<E&&w.push({t:i.TextXActionType.RETAIN,len:E-a.endOffset}),w.push({t:i.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[{...f,paragraphStyle:{...f.paragraphStyle,...S?{headingId:void 0,namedStyleType:void 0}:null},startIndex:0,bullet:(V=f.paragraphStyle)!=null&&V.headingId?void 0:{...f.bullet,listType:i.PresetListType.CHECK_LIST}}]},coverType:i.UpdateDocsAttributeType.REPLACE})),O.params.textRanges=[{startOffset:g+y,endOffset:g+y,collapsed:x}];const k=J(d,A);return O.params.actions=Q.editOp(w.serialize(),k),!!r.syncExecuteCommand(O.id,O.params)}};var ln=(o=>(o[o.ABOVE=0]="ABOVE",o[o.BELLOW=1]="BELLOW",o))(ln||{}),go=(o=>(o[o.LEFT=0]="LEFT",o[o.RIGHT=1]="RIGHT",o))(go||{});function Ps(o,t){let e=i.DataStreamTreeTokenType.TABLE_START;const n=[],r=[];for(let s=0;s<o;s++){e+=i.DataStreamTreeTokenType.TABLE_ROW_START;for(let a=0;a<t;a++)e+=`${i.DataStreamTreeTokenType.TABLE_CELL_START}\r
1
+ "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"),co=require("@univerjs/drawing"),U=require("rxjs"),mt=require("rxjs/operators"),N=require("react/jsx-runtime"),P=require("react"),q=require("@univerjs/design");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.getCurrentUnitOfType(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}}const Os="doc.command.tab",We={id:Os,type:i.CommandType.COMMAND,async handler(o,t){const n=o.get(exports.DocAutoFormatService).onAutoFormat(We.id,t);return(await i.sequenceExecuteAsync(n,o.get(i.ICommandService))).result}},Ms="doc.command.after-space",Tt={id:Ms,type:i.CommandType.COMMAND,async handler(o){const e=o.get(exports.DocAutoFormatService).onAutoFormat(Tt.id);return(await i.sequenceExecuteAsync(e,o.get(i.ICommandService))).result}},et={id:"doc.command.enter",type:i.CommandType.COMMAND,async handler(o){const e=o.get(exports.DocAutoFormatService).onAutoFormat(et.id);return(await i.sequenceExecuteAsync(e,o.get(i.ICommandService))).result}};var Ds=Object.getOwnPropertyDescriptor,Bs=(o,t,e,n)=>{for(var r=n>1?void 0:n?Ds(t,e):t,s=o.length-1,a;s>=0;s--)(a=o[s])&&(r=a(r)||r);return r},Mn=(o,t)=>(e,n)=>t(e,n,o);const _s=11,Ns=9,wt={ff:"Arial",fs:_s};let ve=class extends i.Disposable{constructor(t,e,n){super();M(this,"_cacheStyle",null);this._textSelectionManagerService=t,this._univerInstanceService=e,this._renderManagerService=n,this._init()}_init(){this._listenDocRangeChange()}_listenDocRangeChange(){this.disposeWithMe(this._textSelectionManagerService.textSelection$.subscribe(()=>{this._clearStyleCache()}))}getStyleCache(){return this._cacheStyle}getDefaultStyle(){var a;const t=this._univerInstanceService.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC);if(t==null)return{...wt};const e=t==null?void 0:t.getUnitId(),n=(a=this._renderManagerService.getRenderById(e))==null?void 0:a.with(D.DocSkeletonManagerService),r=n==null?void 0:n.getViewModel();return r==null?{...wt}:r.getEditArea()===B.DocumentEditArea.BODY?{...wt}:{...wt,fs:Ns}}setStyleCache(t){this._cacheStyle={...this._cacheStyle,...t}}_clearStyleCache(){this._cacheStyle=null}};ve=Bs([Mn(0,i.Inject(D.DocSelectionManagerService)),Mn(1,i.IUniverInstanceService),Mn(2,B.IRenderManagerService)],ve);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")}function uo(o,t,e){const n=[];for(let r=0,s=o.length;r<s;r++)o[r]===i.DataStreamTreeTokenType.PARAGRAPH&&n.push({startIndex:r});if(t)for(const r of n)t.bullet&&(r.bullet=i.Tools.deepClone(t.bullet)),t.paragraphStyle&&(r.paragraphStyle=i.Tools.deepClone(t.paragraphStyle),delete r.paragraphStyle.borderBottom,t.paragraphStyle.headingId&&(r.paragraphStyle.headingId=i.generateRandomId(6)));if(e)for(const r of n)r.paragraphStyle||(r.paragraphStyle={}),r.paragraphStyle.borderBottom=e;return n}const cn={id:"doc.command.break-line",type:i.CommandType.COMMAND,handler:(o,t)=>{var L,G,W,H,V;const e=o.get(D.DocSelectionManagerService),n=o.get(i.IUniverInstanceService),r=o.get(i.ICommandService),s=o.get(ve),a=(L=t==null?void 0:t.textRange)!=null?L:e.getActiveTextRange(),c=e.getRectRanges();if(a==null)return!1;if(c&&c.length){const{startOffset:j}=a;return e.replaceDocRanges([{startOffset:j,endOffset:j}]),!0}const{horizontalLine:l}=t!=null?t:{},{segmentId:A}=a,d=n.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),u=d==null?void 0:d.getSelfOrHeaderFooterModel(A!=null?A:"").getBody();if(d==null||u==null)return!1;const m=d.getUnitId(),{startOffset:g,endOffset:h}=a,f=((G=u.paragraphs)!=null?G:[]).find(j=>j.startIndex>=g);if(!f)return!1;const S=g===f.startIndex,E=f.startIndex,C=s.getDefaultStyle(),T=s.getStyleCache(),R=an(u,h,C,T),v={dataStream:i.DataStreamTreeTokenType.PARAGRAPH,paragraphs:uo(i.DataStreamTreeTokenType.PARAGRAPH,f,l),textRuns:[{st:0,ed:1,ts:{...R.ts}}]};if(d==null)return!1;const b=e.getActiveTextRange();if(u==null)return!1;const{collapsed:x}=a,y=v.dataStream.length,_=[{startOffset:g+y,endOffset:g+y,style:b==null?void 0:b.style,collapsed:x}],O={id:D.RichTextEditingMutation.id,params:{unitId:m,actions:[],textRanges:_,debounce:!0}},w=new i.TextX,Q=i.JSONX.getInstance();if(x)g>0&&w.push({t:i.TextXActionType.RETAIN,len:g}),w.push({t:i.TextXActionType.INSERT,body:v,len:v.dataStream.length});else{const j=i.BuildTextUtils.selection.delete([a],u,0,v);w.push(...j)}(((W=f.bullet)==null?void 0:W.listType)===i.PresetListType.CHECK_LIST_CHECKED||(H=f.paragraphStyle)!=null&&H.headingId)&&(a.endOffset<E&&w.push({t:i.TextXActionType.RETAIN,len:E-a.endOffset}),w.push({t:i.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[{...f,paragraphStyle:{...f.paragraphStyle,...S?{headingId:void 0,namedStyleType:void 0}:null},startIndex:0,bullet:(V=f.paragraphStyle)!=null&&V.headingId?void 0:{...f.bullet,listType:i.PresetListType.CHECK_LIST}}]},coverType:i.UpdateDocsAttributeType.REPLACE})),O.params.textRanges=[{startOffset:g+y,endOffset:g+y,collapsed:x}];const k=J(d,A);return O.params.actions=Q.editOp(w.serialize(),k),!!r.syncExecuteCommand(O.id,O.params)}};var ln=(o=>(o[o.ABOVE=0]="ABOVE",o[o.BELLOW=1]="BELLOW",o))(ln||{}),go=(o=>(o[o.LEFT=0]="LEFT",o[o.RIGHT=1]="RIGHT",o))(go||{});function Ps(o,t){let e=i.DataStreamTreeTokenType.TABLE_START;const n=[],r=[];for(let s=0;s<o;s++){e+=i.DataStreamTreeTokenType.TABLE_ROW_START;for(let a=0;a<t;a++)e+=`${i.DataStreamTreeTokenType.TABLE_CELL_START}\r
2
2
  ${i.DataStreamTreeTokenType.TABLE_CELL_END}`,n.push({startIndex:e.length-3,paragraphStyle:{spaceAbove:{v:3},lineSpacing:2,spaceBelow:{v:0}}}),r.push({startIndex:e.length-2});e+=i.DataStreamTreeTokenType.TABLE_ROW_END}return e+=i.DataStreamTreeTokenType.TABLE_END,{dataStream:e,paragraphs:n,sectionBreaks:r}}function dn(){return{margin:{start:{v:10},end:{v:10},top:{v:5},bottom:{v:5}}}}function An(o){const t=dn();return{tableCells:[...new Array(o).fill(null).map(()=>i.Tools.deepClone(t))],trHeight:{val:{v:30},hRule:i.TableRowHeightRule.AUTO}}}function un(o){return{size:{type:i.TableSizeType.SPECIFIED,width:{v:o}}}}function ho(o,t,e){const n=un(e/t),r=An(t),s=[...new Array(o).fill(null).map(()=>i.Tools.deepClone(r))],a=[...new Array(t).fill(null).map(()=>i.Tools.deepClone(n))],c=i.generateRandomId(6);return{tableRows:s,tableColumns:a,tableId:c,align:i.TableAlignmentType.START,indent:{v:0},textWrap:i.TableTextWrapType.NONE,position:{positionH:{relativeFrom:i.ObjectRelativeFromH.PAGE,posOffset:0},positionV:{relativeFrom:i.ObjectRelativeFromV.PAGE,posOffset:0}},dist:{distB:0,distL:0,distR:0,distT:0},cellMargin:{start:{v:10},end:{v:10},top:{v:5},bottom:{v:5}},size:{type:i.TableSizeType.UNSPECIFIED,width:{v:e}}}}function yt(o,t){if(!o&&!t)return null;if(t&&t.length>0){let e=Number.POSITIVE_INFINITY,n=Number.NEGATIVE_INFINITY;const r="";for(const s of t){const{startOffset:a,endOffset:c,segmentId:l}=s;a==null||c==null||l==null||(e=Math.min(e,a),n=Math.max(n,c))}if(Number.isFinite(e)&&Number.isFinite(n))return{startOffset:e,endOffset:n,segmentId:r}}else if(o){const{startOffset:e,endOffset:n,segmentId:r}=o;return e==null||n==null||r==null?null:{startOffset:e,endOffset:n,segmentId:r}}}function ws(o){let t=i.DataStreamTreeTokenType.TABLE_ROW_START;const e=[],n=[];for(let r=0;r<o;r++)t+=`${i.DataStreamTreeTokenType.TABLE_CELL_START}\r
3
3
  ${i.DataStreamTreeTokenType.TABLE_CELL_END}`,e.push({startIndex:t.length-3,paragraphStyle:{spaceAbove:{v:3},lineSpacing:2,spaceBelow:{v:0}}}),n.push({startIndex:t.length-2});return t+=i.DataStreamTreeTokenType.TABLE_ROW_END,{dataStream:t,paragraphs:e,sectionBreaks:n}}function Us(){const o=`${i.DataStreamTreeTokenType.TABLE_CELL_START}\r
4
4
  ${i.DataStreamTreeTokenType.TABLE_CELL_END}`,t=[],e=[];return t.push({startIndex:1,paragraphStyle:{spaceAbove:{v:3},lineSpacing:2,spaceBelow:{v:0}}}),e.push({startIndex:2}),{dataStream:o,paragraphs:t,sectionBreaks:e}}function Ls(o,t,e){var u,m,g;const{startOffset:n,endOffset:r,segmentId:s}=o,a=e.getSelfOrHeaderFooterViewModel(s),c=t===0?n:r;let l=null;const A=(g=(m=(u=e.getBody())==null?void 0:u.tables)==null?void 0:m.find(h=>c>=h.startIndex&&c<=h.endIndex))==null?void 0:g.tableId;let d=0;for(const h of a.getChildren()){for(const p of h.children){const{children:f}=p,S=f[0];if(S){for(const E of S.children)if(E.startIndex<=c&&c<=E.endIndex){d=S.children.indexOf(E),l=E;break}}if(l)break}if(l)break}return l==null||A==null?null:{offset:t===0?l.startIndex:l.endIndex+1,colCount:l.children.length,tableId:A,insertRowIndex:t===0?d:d+1}}function Qs(o,t,e){var g,h,p;const{startOffset:n,endOffset:r,segmentId:s}=o,a=e.getSelfOrHeaderFooterViewModel(s),c=t===0?n:r,l=(p=(h=(g=e.getBody())==null?void 0:g.tables)==null?void 0:h.find(f=>c>=f.startIndex&&c<=f.endIndex))==null?void 0:p.tableId,A=[];let d=null,u=-1;for(const f of a.getChildren()){for(const S of f.children){const{children:E}=S,C=E[0];if(C){if(c<C.startIndex||c>C.endIndex)continue;d=C;for(const T of C.children){for(const R of T.children){const v=T.children.indexOf(R);if(c>=R.startIndex&&c<=R.endIndex){u=v;break}}if(u!==-1)break}}if(d)break}if(d)break}if(d==null||l==null||u===-1)return null;let m=0;for(const f of d.children){const S=f.children[u],E=t===0?S.startIndex:S.endIndex+1;A.push(E-m),m=E}return{offsets:A,tableId:l,columnIndex:u,rowCount:d.children.length}}function Fs(o,t,e){const n=[];let r=t[e].size.width.v,s=0;for(let a=0;a<t.length;a++)s+=t[a].size.width.v;s+=r;for(let a=0;a<t.length;a++)n.push(t[a].size.width.v/s*o);return r=r/s*o,{widths:n,newColWidth:r}}function oi(o,t){var m,g,h;const{startOffset:e,endOffset:n,segmentId:r}=o,s=t.getSelfOrHeaderFooterViewModel(r),a=(h=(g=(m=t.getBody())==null?void 0:m.tables)==null?void 0:g.find(p=>e>=p.startIndex&&n<=p.endIndex))==null?void 0:h.tableId,c=[];let l=-1,A=0,d=-1,u=!1;for(const p of s.getChildren()){for(const f of p.children){const{children:S}=f,E=S[0];if(E){if(e<E.startIndex||n>E.endIndex)continue;d=E.startIndex+3;for(const C of E.children){const T=E.children.indexOf(C),{startIndex:R,endIndex:v}=C;e>=R&&e<=v?(l=R,c.push(T),A+=v-R+1):(R>e&&v<n||n>=R&&n<=v)&&(c.push(T),A+=v-R+1),c.length===E.children.length&&(u=!0)}}if(c.length)break}if(c.length)break}return a==null||c.length===0?null:{tableId:a,rowIndexes:c,offset:l,len:A,cursor:d,selectWholeTable:u}}function Hs(o,t){var h,p,f;const{startOffset:e,endOffset:n,segmentId:r}=o,s=t.getSelfOrHeaderFooterViewModel(r),a=(f=(p=(h=t.getBody())==null?void 0:h.tables)==null?void 0:p.find(S=>e>=S.startIndex&&n<=S.endIndex))==null?void 0:f.tableId,c=[];let l=null;const A=[];let d=-1,u=-1,m=-1;for(const S of s.getChildren()){for(const E of S.children){const{children:C}=E,T=C[0];if(T){if(e<T.startIndex||n>T.endIndex)continue;l=T;for(const R of T.children)for(const v of R.children){const b=R.children.indexOf(v);e>=v.startIndex&&e<=v.endIndex&&(u=b),n>=v.startIndex&&n<=v.endIndex&&(m=b)}}if(l)break}if(l)break}if(l==null||a==null)return null;for(let S=u;S<=m;S++)A.push(S);let g=0;for(const S of l.children){const E=S.children[u],C=S.children[m];c.push({retain:E.startIndex-g,delete:C.endIndex-E.startIndex+1}),g=C.endIndex+1}return d=l.startIndex+3,{offsets:c,tableId:a,columnIndexes:A,cursor:d,selectWholeTable:A.length===l.children[0].children.length,rowCount:l.children.length}}function ri(o,t){var d,u,m;const{startOffset:e,endOffset:n,segmentId:r}=o,s=t.getSelfOrHeaderFooterViewModel(r),a=(m=(u=(d=t.getBody())==null?void 0:d.tables)==null?void 0:u.find(g=>e>=g.startIndex&&n<=g.endIndex))==null?void 0:m.tableId;let c=-1,l=0,A=-1;for(const g of s.getChildren()){for(const h of g.children){const{children:p}=h,f=p[0];if(f){if(e<f.startIndex||n>f.endIndex)continue;c=f.startIndex,l=f.endIndex-f.startIndex+1,A=f.startIndex}if(f)break}if(l>0)break}return a==null?null:{tableId:a,offset:c,len:l,cursor:A}}function ks(o,t){var h,p,f;const{startOffset:e,endOffset:n,segmentId:r}=o,s=t.getSelfOrHeaderFooterViewModel(r),a=(f=(p=(h=t.getBody())==null?void 0:h.tables)==null?void 0:p.find(S=>e>=S.startIndex&&n<=S.endIndex))==null?void 0:f.tableId,c=[];let l=null,A=-1,d=-1,u=-1,m=-1;for(const S of s.getChildren()){for(const E of S.children){const{children:C}=E,T=C[0];if(T){if(e<T.startIndex||n>T.endIndex)continue;l=T;for(const R of T.children){const v=T.children.indexOf(R);for(const b of R.children){const x=R.children.indexOf(b);e>=b.startIndex&&e<=b.endIndex&&(d=v,u=x),n>=b.startIndex&&n<=b.endIndex&&(m=x)}}}if(l)break}if(l)break}if(l==null||a==null||d===-1)return null;const g=l.children[d];for(let S=u;S<=m;S++){const E=g.children[S];c.push({retain:E.startIndex+1,delete:E.endIndex-E.startIndex-3})}return A=l.startIndex+3,{offsets:c,tableId:a,cursor:A,rowCount:l.children.length}}var Vn=(o=>(o[o.NEXT=0]="NEXT",o[o.PREV=1]="PREV",o))(Vn||{});function ir(o,t,e){const{startOffset:n}=t;let r=null;for(const A of o.getChildren()){for(const d of A.children){const u=d.children[0];if(u&&n>u.startIndex&&n<u.endIndex){r=u;break}}if(r)break}if(r==null)return null;let s=-1,a=-1,c=null;for(const A of r.children){for(const d of A.children)if(n>d.startIndex&&n<d.endIndex){s=A.children.indexOf(d),a=r.children.indexOf(A),c=A;break}if(s>-1)break}if(s===-1||a===-1||c==null)return null;let l=null;if(e===0){if(l=c.children[s+1],!l){const A=r.children[a+1];A&&(l=A.children[0])}}else if(l=c.children[s-1],!l){const A=r.children[a-1];A&&(l=A.children[A.children.length-1])}if(l){const{startIndex:A,endIndex:d}=l;return{startOffset:A+1,endOffset:d-2}}}function ii(o,t){const e=[],{customBlocks:n=[]}=o;for(const r of t){const{startOffset:s,endOffset:a}=r;if(!(s==null||a==null))for(const c of n){const{startIndex:l}=c;l>=s&&l<a&&e.push(c.blockId)}}return e}function Vs(o){return o.some(t=>{const{startNodePosition:e}=t;return e?(e==null?void 0:e.path.indexOf("cells"))>-1:!1})}const js=i.SHEET_EDITOR_UNITS,mo={id:"doc.command.inner-paste",type:i.CommandType.COMMAND,handler:async(o,t)=>{var y,_,O,w,Q,k;const{segmentId:e,textRanges:n,doc:r}=t,s=o.get(i.ICommandService),a=o.get(D.DocSelectionManagerService),c=o.get(i.IUniverInstanceService),l=a.getTextRanges(),A=a.getRectRanges(),{body:d,tableSource:u,drawings:m}=r;if(!Array.isArray(l)||l.length===0||d==null)return!1;const g=c.getCurrentUniverDocInstance(),h=g==null?void 0:g.getSelfOrHeaderFooterModel(e).getBody();if(g==null||h==null)return!1;const p=g.getUnitId(),f={id:D.RichTextEditingMutation.id,params:{unitId:p,actions:[],textRanges:n,segmentId:e}},S=new i.MemoryCursor;S.reset();const E=new i.TextX,C=i.JSONX.getInstance(),T=[],R=!!((y=d.tables)!=null&&y.length),v=!!((_=d.customBlocks)!=null&&_.length);if(R&&e||R&&Vs(l)||l.length&&(A!=null&&A.length))return!1;for(let F=0;F<l.length;F++){const L=l[F],{startOffset:G,endOffset:W,collapsed:H}=L,V=G-S.cursor,j=i.Tools.deepClone(d);if(R)for(const Y of j.tables){const{tableId:K}=Y,Z=i.generateRandomId(6);Y.tableId=Z;const ne=i.Tools.deepClone(u[K]);ne.tableId=Z;const ge=C.insertOp(["tableSource",Z],ne);T.push(ge)}if(v&&m){const Y=(w=(O=g.getSnapshot().drawingsOrder)==null?void 0:O.length)!=null?w:0;for(const K of j.customBlocks){const{blockId:Z}=K,ne=i.generateRandomId(6);K.blockId=ne;const ge=i.Tools.deepClone(m[Z]);ge.drawingId=ne;const xe=C.insertOp(["drawings",ne],ge),he=C.insertOp(["drawingsOrder",Y],ne);T.push(xe),T.push(he)}}const X=lo((Q=h.customRanges)!=null?Q:[],W,js.includes(p)),z=Ao((k=h.customDecorations)!=null?k:[],W);if(X&&(j.customRanges=[{...X,startIndex:0,endIndex:d.dataStream.length-1}]),z.length&&(j.customDecorations=z.map(Y=>({...Y,startIndex:0,endIndex:d.dataStream.length-1}))),H)E.push({t:i.TextXActionType.RETAIN,len:V}),E.push({t:i.TextXActionType.INSERT,body:j,len:d.dataStream.length});else{const Y=i.BuildTextUtils.selection.delete([L],d,S.cursor,j,l.length===1);E.push(...Y)}S.reset(),S.moveCursor(W)}const b=J(g,e);return T.push(C.editOp(E.serialize(),b)),f.params.actions=T.reduce((F,L)=>i.JSONX.compose(F,L),null),!!s.syncExecuteCommand(f.id,f.params)}};function Ws(o,t){const{startOffset:e,endOffset:n}=o,r=t.some(a=>a.startIndex===n),s=Math.max(e,r?n-1:n);return{...o,endOffset:s,collapsed:e===s}}function Gs(o,t,e){var h,p;const n=t.getSelfOrHeaderFooterModel(e).getBody(),r=new i.TextX,s=i.JSONX.getInstance(),a=[];if(n==null)return a;const{tables:c=[]}=n,l=new i.MemoryCursor;l.reset();for(let f=0;f<o.length;f++){const S=Ws(o[f],c),{startOffset:E,endOffset:C,collapsed:T}=S,R=E-l.cursor;T?r.push({t:i.TextXActionType.RETAIN,len:R}):r.push(...i.BuildTextUtils.selection.delete([S],n,l.cursor,null,!1)),l.reset(),l.moveCursor(C)}const A=J(t,e);a.push(s.editOp(r.serialize(),A));const d=ii(n,o),u=(h=t.getDrawings())!=null?h:{},m=(p=t.getDrawingsOrder())!=null?p:[],g=d.sort((f,S)=>m.indexOf(f)>m.indexOf(S)?-1:m.indexOf(f)<m.indexOf(S)?1:0);if(g.length>0)for(const f of g){const S=u[f],E=m.indexOf(f);if(S==null||E<0)continue;const C=s.removeOp(["drawings",f],S),T=s.removeOp(["drawingsOrder",E],f);a.push(C),a.push(T)}return a.reduce((f,S)=>i.JSONX.compose(f,S),null)}function Xs(o,t,e,n){const r=[];if(t.getSelfOrHeaderFooterModel(n).getBody()==null)return r;const a=new i.TextX,c=i.JSONX.getInstance(),l=new i.MemoryCursor;l.reset();for(const d of o){const{startOffset:u,endOffset:m,spanEntireRow:g,spanEntireTable:h}=d;if(!(u==null||m==null))if(h){const p=ri({startOffset:u,endOffset:m,segmentId:n},e);if(p==null)continue;const{offset:f,len:S,tableId:E}=p;f-l.cursor>0&&a.push({t:i.TextXActionType.RETAIN,len:f-l.cursor}),a.push({t:i.TextXActionType.DELETE,len:S});const C=c.removeOp(["tableSource",E]);r.push(C),l.moveCursorTo(f+S)}else if(g){const p=oi({startOffset:u,endOffset:m,segmentId:n},e);if(p==null)continue;const{offset:f,rowIndexes:S,len:E,tableId:C}=p;f-l.cursor>0&&a.push({t:i.TextXActionType.RETAIN,len:f-l.cursor}),a.push({t:i.TextXActionType.DELETE,len:E});for(const T of S.reverse()){const R=c.removeOp(["tableSource",C,"tableRows",T]);r.push(R)}l.moveCursorTo(f+E)}else{const p=ks({startOffset:u,endOffset:m,segmentId:n},e);if(p==null)continue;const{offsets:f}=p;for(const S of f){const{retain:E,delete:C}=S;E-l.cursor>0&&a.push({t:i.TextXActionType.RETAIN,len:E-l.cursor}),a.push({t:i.TextXActionType.DELETE,len:C}),l.moveCursorTo(E+C)}}}const A=J(t,n);return r.push(c.editOp(a.serialize(),A)),r.reduce((d,u)=>i.JSONX.compose(d,u),null)}function si(o,t,e,n,r){let s=[];if(Array.isArray(o)&&(o==null?void 0:o.length)!==0&&(s=Gs(o,e,r)),Array.isArray(t)&&(t==null?void 0:t.length)!==0){const a=Xs(t,e,n,r);s==null||s.length===0?s=a:s=i.JSONX.compose(s,i.JSONX.transform(a,s,"right"))}return s}const Ge={id:"doc.command.inner-cut",type:i.CommandType.COMMAND,handler:(o,t)=>{var p;const e=o.get(D.DocSelectionManagerService),n=o.get(i.ICommandService),r=o.get(i.IUniverInstanceService),{segmentId:s,textRanges:a,selections:c=e.getTextRanges(),rectRanges:l=e.getRectRanges()}=t;if((!Array.isArray(c)||c.length===0)&&(!Array.isArray(l)||l.length===0))return!1;const A=(p=r.getCurrentUniverDocInstance())==null?void 0:p.getUnitId();if(!A)return!1;const d=r.getUniverDocInstance(A);if(d==null)return!1;const u=ce(o,A);if(u==null)return!1;const m=u.getViewModel(),g={id:D.RichTextEditingMutation.id,params:{unitId:A,actions:[],textRanges:a}};return g.params.actions=si(c,l,d,m,s),!!n.syncExecuteCommand(g.id,g.params)}};var oe=(o=>(o[o.LEFT=0]="LEFT",o[o.RIGHT=1]="RIGHT",o))(oe||{});const ai="doc.command.insert-text",po={id:ai,type:i.CommandType.COMMAND,handler:async(o,t)=>{var v;const e=o.get(i.ICommandService),{range:n,segmentId:r,body:s,unitId:a,cursorOffset:c}=t,l=o.get(D.DocSelectionManagerService),d=o.get(i.IUniverInstanceService).getUnit(a,i.UniverInstanceType.UNIVER_DOC);if(d==null)return!1;const u=l.getActiveTextRange(),m=d.getSelfOrHeaderFooterModel((v=u==null?void 0:u.segmentId)!=null?v:"").getBody();if(m==null)return!1;const{startOffset:g,collapsed:h}=n,p=c!=null?c:s.dataStream.length,f=[{startOffset:g+p,endOffset:g+p,style:u==null?void 0:u.style,collapsed:h}],S={id:D.RichTextEditingMutation.id,params:{unitId:a,actions:[],textRanges:f,debounce:!0}},E=new i.TextX,C=i.JSONX.getInstance();if(h)g>0&&E.push({t:i.TextXActionType.RETAIN,len:g}),E.push({t:i.TextXActionType.INSERT,body:s,len:s.dataStream.length});else{const b=i.BuildTextUtils.selection.delete([n],m,0,s);E.push(...b)}S.params.textRanges=[{startOffset:g+p,endOffset:g+p,collapsed:h}];const T=J(d,r);return S.params.actions=C.editOp(E.serialize(),T),!!e.syncExecuteCommand(S.id,S.params)}},tt={id:"doc.command.delete-text",type:i.CommandType.COMMAND,handler:async(o,t)=>{var R;const e=o.get(i.ICommandService),n=o.get(i.IUniverInstanceService),{range:r,segmentId:s,unitId:a,direction:c,len:l=1}=t,A=n.getUnit(a,i.UniverInstanceType.UNIVER_DOC),d=A==null?void 0:A.getSelfOrHeaderFooterModel(s).getBody();if(A==null||d==null)return!1;const{startOffset:u}=r;let m=c===oe.LEFT?u-l:u,g=c===oe.LEFT?u-1:u+l-1;const h=(R=d.customRanges)==null?void 0:R.find(v=>v.startIndex<=m&&v.endIndex>=g);h!=null&&h.wholeEntity&&(m=h.startIndex,g=Math.max(g,h.endIndex));const p={id:D.RichTextEditingMutation.id,params:{unitId:a,actions:[],textRanges:[{startOffset:m,endOffset:m,collapsed:!0}],debounce:!0}},f=new i.TextX,S=i.JSONX.getInstance();f.push({t:i.TextXActionType.RETAIN,len:m-0}),f.push({t:i.TextXActionType.DELETE,len:g-m+1});const C=J(A,s);return p.params.actions=S.editOp(f.serialize(),C),!!e.syncExecuteCommand(p.id,p.params)}},fo={id:"doc.command.update-text",type:i.CommandType.COMMAND,handler:async(o,t)=>{const{range:e,segmentId:n,updateBody:r,coverType:s,unitId:a,textRanges:c}=t,l=o.get(i.ICommandService),d=o.get(i.IUniverInstanceService).getCurrentUniverDocInstance();if(d==null)return!1;const u={id:D.RichTextEditingMutation.id,params:{unitId:a,actions:[],textRanges:c}},m=new i.TextX,g=i.JSONX.getInstance(),{startOffset:h,endOffset:p}=e;m.push({t:i.TextXActionType.RETAIN,len:h}),m.push({t:i.TextXActionType.RETAIN,body:r,len:p-h,coverType:s});const f=J(d,n);return u.params.actions=g.editOp(m.serialize(),f),!!l.syncExecuteCommand(u.id,u.params)}};function bt(o){var a,c,l,A;const t=o.get(i.IUniverInstanceService),n=o.get(D.DocSelectionManagerService).getActiveTextRange(),r=t.getCurrentUnitOfType(i.UniverInstanceType.UNIVER_DOC);return!n||!n.collapsed||!r||n.segmentId?!1:i.BuildTextUtils.range.getParagraphsInRange(n,(c=(a=r.getBody())==null?void 0:a.paragraphs)!=null?c:[],(A=(l=r.getBody())==null?void 0:l.dataStream)!=null?A:"")[0]}const gn={id:"doc.command.delete-custom-block",type:i.CommandType.COMMAND,handler:async(o,t)=>{var O;const e=o.get(D.DocSelectionManagerService),n=o.get(i.IUniverInstanceService),r=o.get(i.ICommandService),s=e.getActiveTextRange(),a=n.getCurrentUniverDocInstance();if(s==null||a==null)return!1;const{direction:c,range:l,unitId:A,drawingId:d}=t,{startOffset:u,segmentId:m,style:g}=s,h=c===oe.LEFT?u-1:u,p=[{startOffset:h,endOffset:h,style:g}],f={id:D.RichTextEditingMutation.id,params:{unitId:A,actions:[],textRanges:p,prevTextRanges:[l]}},S=new i.TextX,E=i.JSONX.getInstance(),C=[];u>0&&S.push({t:i.TextXActionType.RETAIN,len:c===oe.LEFT?u-1:u}),S.push({t:i.TextXActionType.DELETE,len:1});const T=J(a,m);C.push(E.editOp(S.serialize(),T));const R=((O=a.getDrawings())!=null?O:{})[d],b=a.getDrawingsOrder().indexOf(d),x=E.removeOp(["drawings",d],R),y=E.removeOp(["drawingsOrder",b],d);return C.push(x),C.push(y),f.params.actions=C.reduce((w,Q)=>i.JSONX.compose(w,Q),null),!!r.syncExecuteCommand(f.id,f.params)}},hn={id:"doc.command.merge-two-paragraph",type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(D.DocSelectionManagerService),n=o.get(i.IUniverInstanceService),r=o.get(i.ICommandService),{direction:s,range:a}=t,c=e.getActiveTextRange(),l=e.getTextRanges();if(c==null||l==null)return!1;const{segmentId:A,style:d}=c,u=n.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),m=u==null?void 0:u.getSelfOrHeaderFooterModel(A).getBody();if(u==null||m==null)return!1;const g=m.dataStream,h=c,p=u.getUnitId(),{startOffset:f,collapsed:S}=h;if(!S)return!1;const E=s===oe.LEFT?f:f+1;let C,T;for(const k of m.paragraphs){if(k.startIndex>=E){T=k;break}C=k}if(C==null||T==null)return!1;const R=s===oe.LEFT?f-1:f,v=[{startOffset:R,endOffset:R,style:d}],b={id:D.RichTextEditingMutation.id,params:{unitId:p,actions:[],textRanges:v,prevTextRanges:[a]}},x=new i.TextX,y=i.JSONX.getInstance();C.startIndex>0&&x.push({t:i.TextXActionType.RETAIN,len:C.startIndex}),x.push({t:i.TextXActionType.DELETE,len:1}),T.startIndex>C.startIndex+1&&x.push({t:i.TextXActionType.RETAIN,len:T.startIndex-C.startIndex-1});const _=Object.values(i.DataStreamTreeTokenType),O=g[C.startIndex-1];(O&&!_.includes(O)||O===" ")&&x.push({t:i.TextXActionType.RETAIN,len:1,coverType:i.UpdateDocsAttributeType.REPLACE,body:{dataStream:"",paragraphs:[{...i.Tools.deepClone(C),startIndex:0}]}});const w=J(u,A);return b.params.actions=y.editOp(x.serialize(),w),!!r.syncExecuteCommand(b.id,b.params)}},ci={id:"doc.command.remove-horizontal-line",type:i.CommandType.COMMAND,handler:async o=>{var R;const t=o.get(D.DocSelectionManagerService),e=o.get(i.IUniverInstanceService),n=o.get(i.ICommandService),r=t.getActiveTextRange(),s=t.getTextRanges();if(r==null||s==null)return!1;const{segmentId:a,style:c}=r,l=e.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),A=l==null?void 0:l.getSelfOrHeaderFooterModel(a).getBody();if(l==null||A==null)return!1;const d=r,u=l.getUnitId(),{startOffset:m,collapsed:g}=d;if(!g)return!1;const h=(R=A.paragraphs)==null?void 0:R.find(v=>v.startIndex===m-1);if(h==null)return!1;const p=[{startOffset:m,endOffset:m,style:c}],f={id:D.RichTextEditingMutation.id,params:{unitId:u,actions:[],textRanges:p}},S=new i.TextX,E=i.JSONX.getInstance();h.startIndex>0&&S.push({t:i.TextXActionType.RETAIN,len:h.startIndex}),S.push({t:i.TextXActionType.RETAIN,len:1,coverType:i.UpdateDocsAttributeType.REPLACE,body:{dataStream:"",paragraphs:[{...i.Tools.deepClone({...h,paragraphStyle:{...h.paragraphStyle,borderBottom:void 0}}),startIndex:0}]}});const C=J(l,a);return f.params.actions=E.editOp(S.serialize(),C),!!n.syncExecuteCommand(f.id,f.params)}};function mn(o,t){let e=0;if(o==null||o.length===0){if(typeof t[0].startOffset=="number"){const n=t[0],{spanEntireRow:r,spanEntireTable:s}=n;s?e=n.startOffset-3:r?n.startRow>0?e=n.startOffset-6:e=n.startOffset:e=t[0].startOffset}}else if(o.length>0&&t.length>0){const n=o[0],r=t[0];if(n.startOffset!=null&&r.startOffset!=null){if(n.startOffset<r.startOffset)e=n.startOffset;else if(n.startOffset>=r.startOffset){const{spanEntireRow:s,spanEntireTable:a}=r;a?e=r.startOffset-3:s&&(e=r.startOffset-6)}}}return e}const Rt={id:"doc.command.delete-left",type:i.CommandType.COMMAND,handler:async o=>{var y,_,O,w,Q,k;const t=o.get(D.DocSelectionManagerService),e=o.get(i.IUniverInstanceService),n=o.get(i.ICommandService);let r=!0;const s=e.getCurrentUniverDocInstance();if(s==null)return!1;const a=s.getUnitId(),c=ce(o,a),l=t.getActiveTextRange(),A=t.getRectRanges(),d=t.getTextRanges(),u=c==null?void 0:c.getSkeleton();if(u==null)return!1;if(A!=null&&A.length){const F=mn(d,A),L=A[0].segmentId,G=[{startOffset:F,endOffset:F}];return n.executeCommand(Ge.id,{segmentId:L,textRanges:G})}if(l==null||d==null)return!1;const{segmentId:m,style:g,segmentPage:h}=l,p=s.getSelfOrHeaderFooterModel(m).getBody();if(p==null)return!1;const f=l,{startOffset:S,collapsed:E}=f,C=u.findNodeByCharIndex(S,m,h),T=B.hasListGlyph(C),R=B.isIndentByGlyph(C,p);let v=S;const b=u.findNodeByCharIndex(S-1,m,h);if(B.isFirstGlyph(C)&&b!==C&&(T===!0||R===!0)&&E){const F=B.getParagraphByGlyph(C,p);if(F==null)return!1;const L=F==null?void 0:F.startIndex,G={startIndex:0},W=F.paragraphStyle;if(T===!0){const V=F.paragraphStyle;if(V){G.paragraphStyle=V;const{hanging:j}=V;j&&(G.paragraphStyle.indentStart=j,G.paragraphStyle.hanging=void 0)}}else if(R===!0){const V=F.bullet;V&&(G.bullet=V),W!=null&&(G.paragraphStyle={...W},delete G.paragraphStyle.hanging,delete G.paragraphStyle.indentStart)}const H=[{startOffset:v,endOffset:v,style:g}];r=await n.executeCommand(fo.id,{unitId:s.getUnitId(),updateBody:{dataStream:"",paragraphs:[{...G}]},range:{startOffset:L,endOffset:L+1},textRanges:H,coverType:i.UpdateDocsAttributeType.REPLACE,segmentId:m})}else if(E===!0){if(b==null)return!0;if(b.content==="\r"){const F=(y=p.paragraphs)==null?void 0:y.find(L=>L.startIndex===S-1);(_=F==null?void 0:F.paragraphStyle)!=null&&_.borderBottom?r=await n.executeCommand(ci.id):r=await n.executeCommand(hn.id,{direction:oe.LEFT,range:f})}else if(b.streamType==="\b"){const F=(w=s.getSnapshot().drawings)==null?void 0:w[(O=b.drawingId)!=null?O:""];if(F==null)return!0;const L=(k=(Q=s.getBody())==null?void 0:Q.customBlocks)==null?void 0:k.find(W=>W.blockId===b.drawingId);if(F.layoutType===i.PositionedObjectLayoutType.INLINE||(L==null?void 0:L.blockType)===i.BlockType.CUSTOM){const W=s.getUnitId();r=await n.executeCommand(gn.id,{direction:oe.LEFT,range:l,unitId:W,drawingId:b.drawingId})}else{const W=u.findNodeByCharIndex(S-2);if(W==null)return!0;v-=b.count,v-=W.count;const H=[{startOffset:v,endOffset:v,style:g}];r=await n.executeCommand(tt.id,{unitId:s.getUnitId(),range:{...l,startOffset:l.startOffset-1,endOffset:l.endOffset-1},segmentId:m,direction:oe.LEFT,len:W.count,textRanges:H})}}else v-=b.count,r=await n.executeCommand(tt.id,{unitId:s.getUnitId(),range:f,segmentId:m,direction:oe.LEFT,len:b.count})}else{const F=li(f,[f]);r=await n.executeCommand(Ge.id,{segmentId:m,textRanges:F,selections:[f]})}return r}},So={id:"doc.command.delete-right",type:i.CommandType.COMMAND,handler:async o=>{var C,T;const t=o.get(D.DocSelectionManagerService),n=o.get(i.IUniverInstanceService).getCurrentUniverDocInstance();if(!n)return!1;const r=ce(o,n.getUnitId()),s=o.get(i.ICommandService),a=t.getActiveTextRange(),c=t.getRectRanges(),l=t.getTextRanges(),A=r==null?void 0:r.getSkeleton();if(c!=null&&c.length){const R=mn(l,c),v=c[0].segmentId,b=[{startOffset:R,endOffset:R}];return s.executeCommand(Ge.id,{segmentId:v,textRanges:b})}if(a==null||A==null||l==null)return!1;const{segmentId:d,style:u,segmentPage:m}=a,g=n==null?void 0:n.getSelfOrHeaderFooterModel(d).getBody();if(!n||!g)return!1;const h=a,{startOffset:p,endOffset:f,collapsed:S}=h;if(p===g.dataStream.length-2&&S)return!0;let E=!1;if(S===!0){const R=A.findNodeByCharIndex(p,d,m),v=A.findNodeByCharIndex(p+1);if(R.streamType===i.DataStreamTreeTokenType.PARAGRAPH&&(v==null?void 0:v.streamType)===i.DataStreamTreeTokenType.SECTION_BREAK)return!1;if(R.content==="\r")E=await s.executeCommand(hn.id,{direction:oe.RIGHT,range:a});else if(R.streamType==="\b"){const b=(T=n.getSnapshot().drawings)==null?void 0:T[(C=R.drawingId)!=null?C:""];if(b==null)return!0;if(b.layoutType===i.PositionedObjectLayoutType.INLINE){const y=n.getUnitId();E=await s.executeCommand(gn.id,{direction:oe.RIGHT,range:a,unitId:y,drawingId:R.drawingId})}else{if(v==null)return!0;const y=[{startOffset:p+1,endOffset:p+1,style:u}];E=await s.executeCommand(tt.id,{unitId:n.getUnitId(),range:{...a,startOffset:p+1,endOffset:f+1},segmentId:d,direction:oe.RIGHT,textRanges:y,len:v.count})}}else{const b=[{startOffset:p,endOffset:p,style:u}];E=await s.executeCommand(tt.id,{unitId:n.getUnitId(),range:h,segmentId:d,direction:oe.RIGHT,textRanges:b,len:R.count})}}else{const R=li(h,[h]);E=await s.executeCommand(Ge.id,{segmentId:d,textRanges:R,selections:[h]})}return E}};function li(o,t){let e=o.endOffset;for(const r of t){const{startOffset:s,endOffset:a}=r;s==null||a==null||a<=o.endOffset&&(e-=a-s)}return[{startOffset:e,endOffset:e,style:o.style}]}const Io={id:"doc.command.delete-current-paragraph",type:i.CommandType.COMMAND,handler:async o=>{var A,d;const t=o.get(i.IUniverInstanceService),e=o.get(i.ICommandService),n=t.getCurrentUnitOfType(i.UniverInstanceType.UNIVER_DOC);if(!n)return!1;const r=(d=(A=n.getBody())==null?void 0:A.dataStream)!=null?d:"",s=bt(o);if(!s)return!1;const a=i.BuildTextUtils.selection.delete([{startOffset:s.paragraphStart,endOffset:r[s.paragraphEnd+1]===`
@@ -8,7 +8,7 @@ ${i.DataStreamTreeTokenType.TABLE_CELL_END}`;break}}}_processBeforeLink(t,e){ret
8
8
  `,e.push({startIndex:r.length-2}),n.push({startIndex:r.length-1}),t.dataStream=r,t.paragraphs=e,t.sectionBreaks=n);const s={html:""},a=B.parseDataStreamToTree(r).sectionList;for(const c of a)Ze(c,o,s);return s.html}function Ze(o,t,e){var n,r;switch(o.nodeType){case i.DataStreamTreeNodeType.SECTION_BREAK:{for(const s of o.children)Ze(s,t,e);break}case i.DataStreamTreeNodeType.PARAGRAPH:{const{children:s,startIndex:a,endIndex:c}=o,l=(r=(n=t.body)==null?void 0:n.paragraphs.find(h=>h.startIndex===c))!=null?r:{},{paragraphStyle:A={}}=l,{spaceAbove:d,spaceBelow:u,lineSpacing:m}=A,g=[];if(d!=null&&(typeof d=="number"?g.push(`margin-top: ${d}px`):g.push(`margin-top: ${d.v}px`)),u!=null&&(typeof u=="number"?g.push(`margin-bottom: ${u}px`):g.push(`margin-bottom: ${u.v}px`)),m!=null&&g.push(`line-height: ${m}`),e.html+=`<p class="UniverNormal" ${g.length?`style="${g.join("; ")};"`:""}>`,s.length)for(const h of s)Ze(h,t,e);e.html+=`${ea(t,a,c)}</p>`;break}case i.DataStreamTreeNodeType.TABLE:{const{children:s}=o;e.html+='<table class="UniverTable" style="width: 100%; border-collapse: collapse;"><tbody>';for(const a of s)Ze(a,t,e);e.html+="</tbody></table>";break}case i.DataStreamTreeNodeType.TABLE_ROW:{const{children:s}=o;e.html+='<tr class="UniverTableRow">';for(const a of s)Ze(a,t,e);e.html+="</tr>";break}case i.DataStreamTreeNodeType.TABLE_CELL:{const{children:s}=o;e.html+='<td class="UniverTableCell">';for(const a of s)Ze(a,t,e);e.html+="</td>";break}default:throw new Error(`Unknown node type: ${o.nodeType}`)}}class ta{convert(t){if(t.length===0)throw new Error("The bodyList length at least to be 1");let e="";for(const n of i.Tools.deepClone(t))e+=hi(n);return e}}var na=Object.getOwnPropertyDescriptor,oa=(o,t,e,n)=>{for(var r=n>1?void 0:n?na(t,e):t,s=o.length-1,a;s>=0;s--)(a=o[s])&&(r=a(r)||r);return r},lt=(o,t)=>(e,n)=>t(e,n,o);nt.use(gi);nt.use(di);nt.use(ui);function lr(o,t,e){var s,a,c;const n=i.getBodySlice(o,t,e+2),r=i.DataStreamTreeTokenType.TABLE_START+i.DataStreamTreeTokenType.TABLE_ROW_START+i.DataStreamTreeTokenType.TABLE_CELL_START+n.dataStream+i.DataStreamTreeTokenType.TABLE_CELL_END+i.DataStreamTreeTokenType.TABLE_ROW_END+i.DataStreamTreeTokenType.TABLE_END;return n.dataStream=r,(s=n.textRuns)==null||s.forEach(l=>{const{st:A,ed:d}=l;l.st=A+3,l.ed=d+3}),(a=n.tables)==null||a.forEach(l=>{const{startIndex:A,endIndex:d}=l;l.startIndex=A+3,l.endIndex=d+3}),(c=n.paragraphs)==null||c.forEach(l=>{const{startIndex:A}=l;l.startIndex=A+3}),n}const Be=i.createIdentifier("doc.clipboard-service");let jn=class extends i.Disposable{constructor(t,e,n,r,s){super();M(this,"_clipboardHooks",[]);M(this,"_htmlToUDM",new nt);M(this,"_umdToHtml",new ta);this._univerInstanceService=t,this._logService=e,this._commandService=n,this._clipboardInterfaceService=r,this._docSelectionManagerService=s}async copy(t=i.SliceBodyType.copy,e){var c,l;const{newSnapshotList:n=[],needCache:r=!1,snapshot:s,ranges:a}=(c=this._getDocumentBodyInRanges(t,e))!=null?c:{};if(n.length===0||s==null)return!1;try{const A=!!((l=a==null?void 0:a[0])!=null&&l.segmentId);this._setClipboardData(n,!A&&r)}catch(A){return this._logService.error("[DocClipboardService] copy failed",A),!1}return!0}async cut(t){return this._cut(t)}async paste(t){const e=await this._genDocDataFromClipboardItems(t);return this._paste(e)}async legacyPaste(t){let{html:e,text:n,files:r}=t;const s=this._univerInstanceService.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),a=(s==null?void 0:s.getUnitId())||"";if(!e&&!n&&r.length&&(e=await this._createImagePasteHtml(r)),!e&&!n)return this._logService.warn("[DocClipboardController] html and text cannot be both empty!"),!1;const c=this._genDocDataFromHtmlAndText(e,n,a);if(a===i.DOCS_NORMAL_EDITOR_UNIT_ID_KEY)if(n){const l=i.BuildTextUtils.transform.fromPlainText(n);return this._paste({body:l})}else c.body.textRuns=[];return this._paste(c)}async _cut(t){var c,l,A,d,u;const e=(l=(c=t==null?void 0:t.filter(m=>m.rangeType===i.DOC_RANGE_TYPE.TEXT))!=null?c:this._docSelectionManagerService.getTextRanges())!=null?l:[],n=(d=(A=t==null?void 0:t.filter(m=>m.rangeType===i.DOC_RANGE_TYPE.RECT))!=null?A:this._docSelectionManagerService.getRectRanges())!=null?d:[],{segmentId:r,endOffset:s,style:a}=(u=e[0])!=null?u:{};if(r==null&&this._logService.error("[DocClipboardController] segmentId is not existed"),e.length===0&&n.length===0)return!1;this.copy(i.SliceBodyType.cut,t);try{let m=0;if(n.length>0)m=mn(e,n);else if(s!=null){m=s;for(const h of e){const{startOffset:p,endOffset:f}=h;p==null||f==null||f<=s&&(m-=f-p)}}const g=[{startOffset:m,endOffset:m,style:a}];return this._commandService.executeCommand(Ge.id,{segmentId:r,textRanges:g,rectRanges:n,selections:e})}catch{return this._logService.error("[DocClipboardController] cut content failed"),!1}}async _paste(t){var d,u,m;const{body:e}=t;if(e==null)return!1;let n=i.normalizeBody(e);if(!((d=this._univerInstanceService.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC))==null?void 0:d.getUnitId()))return!1;this._clipboardHooks.forEach(g=>{g.onBeforePaste&&(n=g.onBeforePaste(n))}),n.customRanges=(u=n.customRanges)==null?void 0:u.map(i.BuildTextUtils.customRange.copyCustomRange),(m=n.paragraphs)==null||m.forEach(g=>{var h;(h=g.paragraphStyle)!=null&&h.headingId&&(g.paragraphStyle.headingId=i.generateRandomId(6))});const s=this._docSelectionManagerService.getActiveTextRange(),{segmentId:a,endOffset:c,style:l}=s||{},A=this._docSelectionManagerService.getTextRanges();if(a==null&&this._logService.error("[DocClipboardController] segmentId does not exist!"),c==null||A==null)return!1;try{let g=c;for(const p of A){const{startOffset:f,endOffset:S}=p;f==null||S==null||S<=c&&(g+=n.dataStream.length-(S-f))}const h=[{startOffset:g,endOffset:g,style:l}];return this._commandService.executeCommand(mo.id,{doc:{...t,body:n},segmentId:a,textRanges:h})}catch{return this._logService.error("[DocClipboardController]","clipboard is empty."),!1}}async _setClipboardData(t,e=!0){var a,c,l,A;const n=Ys(),r=(t.length>1?t.map(d=>{var u;return((u=d.body)==null?void 0:u.dataStream)||""}).join(`
9
9
  `):((a=t[0].body)==null?void 0:a.dataStream)||"").replaceAll(i.DataStreamTreeTokenType.TABLE_START,"").replaceAll(i.DataStreamTreeTokenType.TABLE_END,"").replaceAll(i.DataStreamTreeTokenType.TABLE_ROW_START,"").replaceAll(i.DataStreamTreeTokenType.TABLE_ROW_END,"").replaceAll(i.DataStreamTreeTokenType.TABLE_CELL_START,"").replaceAll(i.DataStreamTreeTokenType.TABLE_CELL_END,"").replaceAll(`\r
10
10
  `," ");let s=this._umdToHtml.convert(t);if(t.length===1&&e){s=s.replace(/(<[a-z]+)/,(m,g)=>`${g} data-copy-id="${n}"`);const d=t[0],u={body:d.body};if((l=(c=d.body)==null?void 0:c.customBlocks)!=null&&l.length){u.drawings={};for(const m of d.body.customBlocks){const{blockId:g}=m,h=(A=d.drawings)==null?void 0:A[g];if(h){const p=i.generateRandomId(6);m.blockId=p,u.drawings[p]={...i.Tools.deepClone(h),drawingId:p}}}}Dn.set(n,u)}return this._clipboardInterfaceService.write(r,s)}addClipboardHook(t){return this._clipboardHooks.push(t),i.toDisposable(()=>{const e=this._clipboardHooks.indexOf(t);e>-1&&this._clipboardHooks.splice(e,1)})}_getDocumentBodyInRanges(t,e){var d;const n=this._univerInstanceService.getCurrentUniverDocInstance(),r=e!=null?e:this._docSelectionManagerService.getDocRanges(),s=[];let a=!0;if(n==null||r.length===0)return;const c=r[0].segmentId,l=(d=n==null?void 0:n.getSelfOrHeaderFooterModel(c))==null?void 0:d.getBody(),A=n.getSnapshot();if(l!=null){for(const u of r){const{startOffset:m,endOffset:g,collapsed:h,rangeType:p}=u;if(h||m==null||g==null)continue;if(p===i.DOC_RANGE_TYPE.RECT){a=!1;const{spanEntireRow:E}=u;let C;C=lr(l,m,g),s.push(C);continue}const f={startOffset:m,endOffset:g},S=n.getSelfOrHeaderFooterModel(c).sliceBody(f.startOffset,f.endOffset,t);S!=null&&s.push(S)}return{newSnapshotList:s.map(u=>({...A,body:u})),needCache:a,snapshot:A,ranges:r}}}async _genDocDataFromClipboardItems(t){try{let e="",n="";const r=[];for(const s of t)for(const a of s.types)switch(a){case I.PLAIN_TEXT_CLIPBOARD_MIME_TYPE:{n=await s.getType(a).then(c=>c&&c.text());break}case I.HTML_CLIPBOARD_MIME_TYPE:{e=await s.getType(a).then(c=>c&&c.text());break}case I.FILE__BMP_CLIPBOARD_MIME_TYPE:case I.FILE__JPEG_CLIPBOARD_MIME_TYPE:case I.FILE__WEBP_CLIPBOARD_MIME_TYPE:case I.FILE_PNG_CLIPBOARD_MIME_TYPE:{const c=await s.getType(a),l=new File([c],`pasted_image.${a.split("/")[1]}`,{type:a});r.push(l);break}}return!e&&!n&&r.length&&(e=await this._createImagePasteHtml(r)),this._genDocDataFromHtmlAndText(e,n)}catch(e){return Promise.reject(e)}}_genDocDataFromHtmlAndText(t,e,n){if(!t){if(e)return{body:i.BuildTextUtils.transform.fromPlainText(e)};throw new Error("[DocClipboardService] html and text cannot be both empty!")}const r=qs(t);if(r){const a=Dn.get(r);if(a)return a}if(!n){const a=this._univerInstanceService.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC);n=(a==null?void 0:a.getUnitId())||""}const s=this._htmlToUDM.convert(t,{unitId:n});return r&&Dn.set(r,s),s}async _createImagePasteHtml(t){var c,l;const e={id:"",documentStyle:{},body:{dataStream:"",customBlocks:[]},drawings:{}},n=async A=>{const d=new FileReader;return new Promise(u=>{d.onloadend=function(){u({source:d.result,imageSourceType:co.ImageSourceType.BASE64})},d.readAsDataURL(A)})},r=A=>{const d=new Image,u=500;return new Promise(m=>{d.src=typeof A=="string"?A:URL.createObjectURL(A),d.onload=()=>{const g=Math.min(u,d.naturalWidth),h=d.naturalHeight/d.naturalWidth;m({width:g,height:g*h})}})},s=(l=(c=this._clipboardHooks.find(A=>A.onBeforePasteImage))==null?void 0:c.onBeforePasteImage)!=null?l:n;return await Promise.all(t.map(async(A,d)=>{var S;const u=await s(A);if(!u)return Promise.resolve();const{width:m=100,height:g=100}=await r(A),h=`paste_image_id_${d}`,p=e.body,f=e.drawings;p.dataStream+="\b",(S=p.customBlocks)==null||S.push({startIndex:d,blockId:h}),f[h]={drawingId:h,unitId:"",subUnitId:"",imageSourceType:u.imageSourceType,title:"",source:u.source,description:"",layoutType:i.PositionedObjectLayoutType.INLINE,drawingType:i.DrawingTypeEnum.DRAWING_IMAGE,transform:{width:m,height:g,angle:0},docTransform:{angle:0,size:{width:m,height:g},positionH:{relativeFrom:i.ObjectRelativeFromH.CHARACTER,posOffset:0},positionV:{relativeFrom:i.ObjectRelativeFromV.LINE,posOffset:0}}}})),this._umdToHtml.convert([e])}};jn=oa([lt(0,i.IUniverInstanceService),lt(1,i.ILogService),lt(2,i.ICommandService),lt(3,I.IClipboardInterfaceService),lt(4,i.Inject(D.DocSelectionManagerService))],jn);function pn(o){return o.getContextValue(i.FOCUSING_DOC)||o.getContextValue(i.EDITOR_ACTIVATED)}const Eo=999,vt={id:I.CopyCommand.id,name:"doc.command.copy",type:i.CommandType.COMMAND,multi:!0,priority:Eo,preconditions:pn,handler:async o=>o.get(Be).copy()},Co={id:"doc.command.copy-current-paragraph",type:i.CommandType.COMMAND,handler:async o=>{const t=o.get(Be),e=bt(o);return e?t.copy(i.SliceBodyType.copy,[{startOffset:e.paragraphStart,endOffset:e.paragraphEnd+1,collapsed:!1}]):!1}},fn={id:I.CutCommand.id,name:"doc.command.cut",type:i.CommandType.COMMAND,multi:!0,priority:Eo,preconditions:pn,handler:async o=>o.get(Be).cut()},To={id:"doc.command.cut-current-paragraph",type:i.CommandType.COMMAND,handler:async o=>{const t=o.get(Be),e=bt(o);return e?t.cut([{startOffset:e.paragraphStart,endOffset:e.paragraphEnd+1,collapsed:!1,rangeType:i.DOC_RANGE_TYPE.TEXT}]):!1}},Sn={id:I.PasteCommand.id,name:"doc.command.paste",type:i.CommandType.COMMAND,multi:!0,priority:Eo,preconditions:pn,handler:async o=>{const t=o.get(Be),n=await o.get(I.IClipboardInterfaceService).read();return n.length===0?!1:t.paste(n)}},xt={id:"doc.command.horizontal-line",type:i.CommandType.COMMAND,handler:(o,t)=>o.get(i.ICommandService).syncExecuteCommand(cn.id,{horizontalLine:{padding:5,color:{rgb:"#CDD0D8"},width:1,dashStyle:i.DashStyleType.SOLID},textRange:t==null?void 0:t.insertRange})},yo={id:"doc.command.insert-horizontal-line-bellow",type:i.CommandType.COMMAND,handler:o=>{const t=o.get(i.ICommandService),e=bt(o);return e?t.syncExecuteCommand(xt.id,{insertRange:{startOffset:e.startIndex+1,endOffset:e.startIndex+1}}):!1}},bo={id:"doc.command.select-all",type:i.CommandType.COMMAND,handler:async o=>{var g;const t=o.get(i.IUniverInstanceService),e=o.get(D.DocSelectionManagerService),n=t.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),r=e.getDocRanges(),s=(g=r.find(h=>h.isActive))!=null?g:r[0];if(n==null||s==null)return!1;const{segmentId:a}=s,c=n.getUnitId(),l=n.getSelfOrHeaderFooterModel(a).getSnapshot().body;if(l==null)return!1;const{tables:A=[],dataStream:d}=l;if(d===`\r
11
- `)return!0;const u=[];let m=0;for(const h of A){const{startIndex:p,endIndex:f}=h;m!==p&&u.push({startOffset:m,endOffset:p-1,rangeType:i.DOC_RANGE_TYPE.TEXT}),u.push({startOffset:p+3,endOffset:f-5,rangeType:i.DOC_RANGE_TYPE.RECT}),m=f}return m!==l.dataStream.length-2&&u.push({startOffset:m,endOffset:l.dataStream.length-2,rangeType:i.DOC_RANGE_TYPE.TEXT}),e.replaceDocRanges(u,{unitId:c,subUnitId:c},!1),!0}};class Ot extends i.RxDisposable{constructor(e){super();M(this,"_previousActiveRange",null);M(this,"_undoMutationParamsCache",[]);M(this,"_redoMutationParamsCache",[]);this._context=e}clearUndoRedoMutationParamsCache(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[]}getUndoRedoMutationParamsCache(){return{undoCache:this._undoMutationParamsCache,redoCache:this._redoMutationParamsCache}}setUndoRedoMutationParamsCache({undoCache:e=[],redoCache:n=[]}){this._undoMutationParamsCache=e,this._redoMutationParamsCache=n}getActiveRange(){return this._previousActiveRange}setActiveRange(e){this._previousActiveRange=e}pushUndoRedoMutationParams(e,n){this._undoMutationParamsCache.push(e),this._redoMutationParamsCache.push(n)}fetchComposedUndoRedoMutationParams(){if(this._undoMutationParamsCache.length===0||this._previousActiveRange==null||this._redoMutationParamsCache.length===0)return null;const{unitId:e}=this._undoMutationParamsCache[0],n={unitId:e,actions:this._undoMutationParamsCache.reverse().reduce((s,a)=>i.JSONX.compose(s,a.actions),null),textRanges:[]};return{redoMutationParams:{unitId:e,actions:this._redoMutationParamsCache.reduce((s,a)=>i.JSONX.compose(s,a.actions),null),textRanges:[]},undoMutationParams:n,previousActiveRange:this._previousActiveRange}}dispose(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[],this._previousActiveRange=null}}const Ro={id:"doc.command.ime-input",type:i.CommandType.COMMAND,handler:async(o,t)=>{var L,G,W;const{unitId:e,newText:n,oldTextLen:r,isCompositionEnd:s,isCompositionStart:a}=t,c=o.get(i.ICommandService),l=o.get(B.IRenderManagerService),A=o.get(i.IUniverInstanceService),d=o.get(ve),u=(L=l.getRenderById(e))==null?void 0:L.with(Ot),m=A.getUnit(e,i.UniverInstanceType.UNIVER_DOC);if(m==null||u==null)return!1;const g=u.getActiveRange();if(g==null)return!1;const{style:h,segmentId:p}=g,f=m.getSelfOrHeaderFooterModel(p).getBody();if(f==null)return!1;Object.assign(g,g);const{startOffset:E,endOffset:C}=g,T=n.length,R=[{startOffset:E+T,endOffset:E+T,collapsed:!0,style:h}],v={id:D.RichTextEditingMutation.id,params:{unitId:e,actions:[],textRanges:R}},b=d.getDefaultStyle(),x=d.getStyleCache(),y=lo((G=f.customRanges)!=null?G:[],E+r,i.SHEET_EDITOR_UNITS.includes(e)),_=an(f,a?C:E+r,b,x,i.SHEET_EDITOR_UNITS.includes(e)),O=Ao((W=f.customDecorations)!=null?W:[],E+r),w=new i.TextX,Q=i.JSONX.getInstance();if(!g.collapsed&&a){const H=i.BuildTextUtils.selection.delete([g],f,0,null,!1);w.push(...H),v.params.textRanges=[{startOffset:E+T,endOffset:E+T,collapsed:!0}]}else w.push({t:i.TextXActionType.RETAIN,len:E});r>0&&w.push({t:i.TextXActionType.DELETE,len:r}),w.push({t:i.TextXActionType.INSERT,body:{dataStream:n,textRuns:_?[{..._,st:0,ed:n.length}]:[],customRanges:y?[{...y,startIndex:0,endIndex:n.length-1}]:[],customDecorations:O.map(H=>({...H,startIndex:0,endIndex:n.length-1}))},len:n.length});const k=J(m,p);v.params.actions=Q.editOp(w.serialize(),k),v.params.noHistory=!s,v.params.isCompositionEnd=s;const F=c.syncExecuteCommand(v.id,v.params);return u.pushUndoRedoMutationParams(F,v.params),!!F}};function ue(o,t,e){return e.executeCommand(be.id,{preCommandId:o,...t!=null?t:{}})}const dr="doc.command.set-inline-format-bold",_e={id:dr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(dr,t,e)}},Ar="doc.command.set-inline-format-italic",Ne={id:Ar,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(Ar,t,e)}},ur="doc.command.set-inline-format-underline",Pe={id:ur,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(ur,t,e)}},gr="doc.command.set-inline-format-strikethrough",we={id:gr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(gr,t,e)}},hr="doc.command.set-inline-format-subscript",ye={id:hr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(hr,t,e)}},mr="doc.command.set-inline-format-superscript",Ue={id:mr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(mr,t,e)}},pr="doc.command.set-inline-format-fontsize",me={id:pr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(pr,t,e)}},fr="doc.command.set-inline-format-font-family",Xe={id:fr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(fr,t,e)}},Sr="doc.command.set-inline-format-text-color",Me={id:Sr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(Sr,t,e)}},Ir="doc.command.set-inline-format-text-background-color",De={id:Ir,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(Ir,t,e)}},vr="doc.command.reset-inline-format-text-background-color",st={id:vr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(vr,t,e)}},Wn={[_e.id]:"bl",[Ne.id]:"it",[Pe.id]:"ul",[we.id]:"st",[me.id]:"fs",[Xe.id]:"ff",[Me.id]:"cl",[De.id]:"bg",[st.id]:"bg",[ye.id]:"va",[Ue.id]:"va"},be={id:"doc.command.set-inline-format",type:i.CommandType.COMMAND,handler:async(o,t)=>{var R;const{value:e,preCommandId:n}=t,r=o.get(i.ICommandService),s=o.get(D.DocSelectionManagerService),a=o.get(i.IUniverInstanceService),c=o.get(ve),l=s.getDocRanges(),A=(R=l.find(v=>v.isActive))!=null?R:l[0];if(l.length===0)return!1;const{segmentId:d}=l[0],u=a.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC);if(u==null)return!1;const m=u.getSelfOrHeaderFooterModel(d).getBody();if(m==null)return!1;const g=u.getUnitId();let h;switch(n){case _e.id:case Ne.id:case Pe.id:case we.id:case ye.id:case Ue.id:{const v=c.getDefaultStyle(),b=pi(m,A,v);h=ia(b,n);break}case me.id:case Xe.id:{h=e;break}case Me.id:case De.id:{h={rgb:e};break}case st.id:{h={rgb:null};break}default:throw new Error(`Unknown command: ${n} in handleInlineFormat`)}const p={id:D.RichTextEditingMutation.id,params:{unitId:g,actions:[],textRanges:l}},f=new i.TextX,S=i.JSONX.getInstance(),E=new i.MemoryCursor;E.reset();for(const v of l){let{startOffset:b,endOffset:x,rangeType:y}=v;if(b==null||x==null)continue;if(y===i.DOC_RANGE_TYPE.RECT&&(b=b-1),b===x){const w=c.getStyleCache(),Q=Wn[n];c.setStyleCache({[Q]:(w==null?void 0:w[Q])!==void 0?mi(w,Q,n):h});continue}const _={dataStream:"",textRuns:[{st:0,ed:x-b,ts:{[Wn[n]]:h}}]},O=b-E.cursor;O!==0&&f.push({t:i.TextXActionType.RETAIN,len:O}),f.push({t:i.TextXActionType.RETAIN,body:_,len:x-b}),E.reset(),E.moveCursor(x)}const C=J(u,d);return p.params.actions=S.editOp(f.serialize(),C),!!r.syncExecuteCommand(p.id,p.params)}};function ra(o){return o!==null&&typeof o=="object"}function mi(o,t,e){if(/bl|it/.test(t))return(o==null?void 0:o[t])===i.BooleanNumber.TRUE?i.BooleanNumber.FALSE:i.BooleanNumber.TRUE;if(/ul|st/.test(t))return ra(o==null?void 0:o[t])&&(o==null?void 0:o[t]).s===i.BooleanNumber.TRUE?{s:i.BooleanNumber.FALSE}:{s:i.BooleanNumber.TRUE};if(/va/.test(t))return e===ye.id?(o==null?void 0:o[t])===i.BaselineOffset.SUBSCRIPT?i.BaselineOffset.NORMAL:i.BaselineOffset.SUBSCRIPT:(o==null?void 0:o[t])===i.BaselineOffset.SUPERSCRIPT?i.BaselineOffset.NORMAL:i.BaselineOffset.SUPERSCRIPT}function pi(o,t,e){var d,u,m,g,h,p,f,S,E,C,T,R,v,b;const{startOffset:n,endOffset:r,collapsed:s}=t;if(s){const x=(d=o.textRuns)!=null?d:[];let y=null;for(let _=x.length-1;_>=0;_--){const O=x[_];if(O.st<n&&n<=O.ed){y=O;break}}return y!=null&&y.ts?{...e,...y.ts}:e}const{textRuns:a=[]}=i.getBodySlice(o,n,r),c=i.Tools.deepClone(e),l=a.map(x=>{var y;return(y=x==null?void 0:x.ts)==null?void 0:y.fs}).filter(Boolean);c.fs=c.fs?Math.max(c.fs,...l):l.length?Math.max(...l):void 0,c.fs=!c.fs||Number.isNaN(c.fs)?void 0:c.fs,c.ff=(g=(m=(u=a.find(x=>{var y;return((y=x.ts)==null?void 0:y.ff)!=null}))==null?void 0:u.ts)==null?void 0:m.ff)!=null?g:c.ff,c.it=a.length&&a.every(x=>{var y;return((y=x.ts)==null?void 0:y.it)===i.BooleanNumber.TRUE})?i.BooleanNumber.TRUE:i.BooleanNumber.FALSE,c.bl=a.length&&a.every(x=>{var y;return((y=x.ts)==null?void 0:y.bl)===i.BooleanNumber.TRUE})?i.BooleanNumber.TRUE:i.BooleanNumber.FALSE,c.ul=a.length&&a.every(x=>{var y,_;return((_=(y=x.ts)==null?void 0:y.ul)==null?void 0:_.s)===i.BooleanNumber.TRUE})?(h=a[0].ts)==null?void 0:h.ul:c.ul,c.st=a.length&&a.every(x=>{var y,_;return((_=(y=x.ts)==null?void 0:y.st)==null?void 0:_.s)===i.BooleanNumber.TRUE})?(p=a[0].ts)==null?void 0:p.st:c.st,c.bg=(E=(S=(f=a.find(x=>{var y;return((y=x.ts)==null?void 0:y.bg)!=null}))==null?void 0:f.ts)==null?void 0:S.bg)!=null?E:c.bg,c.cl=(R=(T=(C=a.find(x=>{var y;return((y=x.ts)==null?void 0:y.cl)!=null}))==null?void 0:C.ts)==null?void 0:T.cl)!=null?R:c.cl;const A=a.filter(x=>{var y;return((y=x==null?void 0:x.ts)==null?void 0:y.va)!=null});if(A.length>0&&A.length===a.length){const x=(v=A[0].ts)==null?void 0:v.va;let y=!0;for(let _=1;_<A.length;_++)if(((b=A[_].ts)==null?void 0:b.va)!==x){y=!1;break}y&&(c.va=x)}return c}function ia(o,t){const e=Wn[t];return mi(o,e,t)}const xo={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)}},fi={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=Ii(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=Ii(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 sa(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 Ii(o,t,e,n,r){const s={id:D.RichTextEditingMutation.id,params:{unitId:o,actions:[],textRanges:[]}},a=sa(t,e,n,r);return s.params.actions=a,s}const Oo={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)}},vi={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)}},Ei={id:"doc.command.insert-custom-range",type:i.CommandType.COMMAND,handler:(o,t)=>{if(!t)return!1;const e=o.get(i.ICommandService),{unitId:n,rangeId:r=i.generateRandomId(),textRanges:s,properties:a,text:c,wholeEntity:l}=t,A={unitId:n,textRanges:s,body:{dataStream:c,customRanges:[{startIndex:0,endIndex:c.length-1,rangeId:r,rangeType:i.CustomRangeType.CUSTOM,properties:a,wholeEntity:l}]}};return e.syncExecuteCommand(Oo.id,A)}},at={id:"doc.command.list-operation",type:i.CommandType.COMMAND,handler:(o,t)=>{var T,R,v,b,x,y;const e=o.get(D.DocSelectionManagerService),n=o.get(i.IUniverInstanceService),r=o.get(i.ICommandService),s=t.listType,a=n.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),c=(R=(T=t.docRange)!=null?T:e.getDocRanges())!=null?R:[];if(a==null||c.length===0)return!1;const l=c[0].segmentId,A=a.getSelfOrHeaderFooterModel(l),d=(b=(v=A.getBody())==null?void 0:v.paragraphs)!=null?b:[],u=(y=(x=A.getBody())==null?void 0:x.dataStream)!=null?y:"";if(!d.length)return!1;const m=i.BuildTextUtils.range.getParagraphsInRanges(c,d,u),g=a.getUnitId(),h={id:D.RichTextEditingMutation.id,params:{unitId:g,actions:[],textRanges:c,isEditing:!1}};new i.MemoryCursor().reset();const f=i.BuildTextUtils.paragraph.bullet.switch({paragraphs:m,listType:s,document:a,segmentId:l}),S=i.JSONX.getInstance(),E=J(a,l);return h.params.actions=S.editOp(f.serialize(),E),!!r.syncExecuteCommand(h.id,h.params)}},Mt={id:"doc.command.change-list-type",type:i.CommandType.COMMAND,handler:(o,t)=>{var C,T,R,v,b;const e=o.get(D.DocSelectionManagerService),n=o.get(i.IUniverInstanceService),r=o.get(i.ICommandService),{listType:s}=t,a=n.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),c=(C=t.docRange)!=null?C:e.getDocRanges();if(a==null||c==null||!c.length)return!1;const{segmentId:l}=c[0],A=a.getSelfOrHeaderFooterModel(l),d=(R=(T=A.getBody())==null?void 0:T.paragraphs)!=null?R:[],u=(b=(v=A.getBody())==null?void 0:v.dataStream)!=null?b:"";if(!d.length)return!1;const m=i.BuildTextUtils.range.getParagraphsInRanges(c,d,u),g=a.getUnitId(),h=i.BuildTextUtils.paragraph.bullet.set({paragraphs:m,listType:s,segmentId:l,document:a});if(!h)return!1;const p=i.JSONX.getInstance(),f={id:D.RichTextEditingMutation.id,params:{unitId:g,actions:[],textRanges:c,isEditing:!1}},S=J(a,l);return f.params.actions=p.editOp(h.serialize(),S),!!r.syncExecuteCommand(f.id,f.params)}};var ft=(o=>(o[o.increase=1]="increase",o[o.decrease=-1]="decrease",o))(ft||{});const Kt={id:"doc.command.change-list-nesting-level",type:i.CommandType.COMMAND,handler:(o,t)=>{var T,R,v,b,x;if(!t)return!1;const{type:e}=t,n=o.get(D.DocSelectionManagerService),r=o.get(i.IUniverInstanceService),s=o.get(i.ICommandService),a=r.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),c=n.getActiveTextRange();if(a==null||c==null)return!1;const{segmentId:l}=c,A=(T=n.getDocRanges())!=null?T:[],d=a.getSelfOrHeaderFooterModel(l),u=(v=(R=d.getBody())==null?void 0:R.paragraphs)!=null?v:[],m=(x=(b=d.getBody())==null?void 0:b.dataStream)!=null?x:"";if(!u.length)return!1;const g=i.BuildTextUtils.range.getParagraphsInRange(c,u,m),h=a.getUnitId(),p=i.JSONX.getInstance(),f=i.BuildTextUtils.paragraph.bullet.changeNestLevel({paragraphs:g,type:e,segmentId:l,document:a}),S={id:D.RichTextEditingMutation.id,params:{unitId:h,actions:[],textRanges:A,isEditing:!1}},E=J(a,l);return S.params.actions=p.editOp(f.serialize(),E),!!s.syncExecuteCommand(S.id,S.params)}},ot={id:"doc.command.bullet-list",type:i.CommandType.COMMAND,handler:(o,t)=>{const e=o.get(i.ICommandService);return t!=null&&t.value?e.syncExecuteCommand(Mt.id,{listType:t.value,docRange:t.docRange}):e.syncExecuteCommand(at.id,{listType:i.PresetListType.BULLET_LIST,docRange:t==null?void 0:t.docRange})}},Et={id:"doc.command.check-list",type:i.CommandType.COMMAND,handler:(o,t)=>{const e=o.get(i.ICommandService);return t!=null&&t.value?e.syncExecuteCommand(Mt.id,{listType:t.value,docRange:t.docRange}):e.syncExecuteCommand(at.id,{listType:i.PresetListType.CHECK_LIST,docRange:t==null?void 0:t.docRange})}},Mo={id:"doc.command.toggle-check-list",type:i.CommandType.COMMAND,handler:(o,t)=>{var p;if(!t)return!1;const e=o.get(i.IUniverInstanceService),n=o.get(i.ICommandService),{index:r,segmentId:s,textRanges:a}=t,c=e.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC);if(c==null||((p=c.getSelfOrHeaderFooterModel(s).getBody())==null?void 0:p.paragraphs)==null)return!1;const A=c.getUnitId(),d={id:D.RichTextEditingMutation.id,params:{unitId:A,actions:[],textRanges:a!=null?a:[],segmentId:s,isEditing:!1}},u=i.BuildTextUtils.paragraph.bullet.toggleChecklist({document:c,paragraphIndex:r,segmentId:s});if(!u)return!1;const m=i.JSONX.getInstance(),g=J(c,s);return d.params.actions=m.editOp(u.serialize(),g),!!n.syncExecuteCommand(d.id,d.params)}},rt={id:"doc.command.order-list",type:i.CommandType.COMMAND,handler:(o,t)=>{const e=o.get(i.ICommandService);return t!=null&&t.value?e.syncExecuteCommand(Mt.id,{listType:t.value}):e.syncExecuteCommand(at.id,{listType:i.PresetListType.ORDER_LIST})}},Do={id:"doc.command.quick-list",type:i.CommandType.COMMAND,handler(o,t){var O,w,Q;if(!t)return!1;const e=o.get(D.DocSelectionManagerService),n=o.get(i.IUniverInstanceService),r=o.get(i.ICommandService),s=n.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),a=e.getActiveTextRange();if(s==null||a==null)return!1;const{segmentId:c,startOffset:l}=a,{listType:A,paragraph:d}=t,{paragraphStart:u,paragraphEnd:m}=d,g=new i.TextX,h=i.JSONX.getInstance(),{paragraphStyle:p={}}=d,S=(i.PRESET_LIST_TYPE[A].nestingLevel[0].paragraphProperties||{}).textStyle;let C=i.generateRandomId(6);const T=(w=(O=s.getBody())==null?void 0:O.paragraphs)!=null?w:[],R=T.findIndex(k=>k.startIndex===d.startIndex),v=T[R-1],b=T[R+1];v&&v.bullet&&v.bullet.listType.indexOf(A)===0?C=v.bullet.listId:b&&b.bullet&&b.bullet.listType.indexOf(A)===0&&(C=b.bullet.listId);const x={id:D.RichTextEditingMutation.id,params:{unitId:s.getUnitId(),actions:[],textRanges:[{startOffset:u,endOffset:u,collapsed:!0}],isEditing:!1}};g.push({t:i.TextXActionType.RETAIN,len:u}),g.push({t:i.TextXActionType.DELETE,len:l-u}),m>l&&g.push({t:i.TextXActionType.RETAIN,len:m-l}),g.push({t:i.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[{startIndex:0,paragraphStyle:{...p,textStyle:{...p.textStyle,...S},indentFirstLine:void 0},bullet:{...(Q=d.bullet)!=null?Q:{nestingLevel:0,textStyle:{fs:20}},listType:A,listId:C}}]}});const y=J(s,c);return x.params.actions=h.editOp(g.serialize(),y),!!r.syncExecuteCommand(x.id,x.params)}};function Bo(o,t){var d,u;const e=o.get(i.ICommandService),n=bt(o);if(!n)return!1;const r=o.get(i.IUniverInstanceService).getCurrentUnitOfType(i.UniverInstanceType.UNIVER_DOC);if(!r)return!1;const s=i.BuildTextUtils.selection.replace({doc:r,selection:{startOffset:n.startIndex+1,endOffset:n.startIndex+1,collapsed:!0},body:{dataStream:"\r",paragraphs:[{startIndex:0,paragraphStyle:{...n.paragraphStyle},bullet:{listType:t,listId:((d=n.bullet)==null?void 0:d.listType)===t?n.bullet.listId:i.generateRandomId(6),nestingLevel:((u=n.bullet)==null?void 0:u.listType)===t?n.bullet.nestingLevel:0}}]}});if(!s)return!1;const a={id:D.RichTextEditingMutation.id,params:{unitId:r.getUnitId(),actions:[],textRanges:[{startOffset:n.startIndex+1,endOffset:n.startIndex+1,collapsed:!0}],isEditing:!1}},c=i.JSONX.getInstance(),l=J(r);return a.params.actions=c.editOp(s.serialize(),l),!!e.syncExecuteCommand(a.id,a.params)}const _o={id:"doc.command.insert-bullet-list-bellow",type:i.CommandType.COMMAND,handler:o=>Bo(o,i.PresetListType.BULLET_LIST)},No={id:"doc.command.insert-order-list-bellow",type:i.CommandType.COMMAND,handler:o=>Bo(o,i.PresetListType.ORDER_LIST)},Po={id:"doc.command.insert-check-list-bellow",type:i.CommandType.COMMAND,handler:o=>Bo(o,i.PresetListType.CHECK_LIST)},pe={id:"doc.command.align-action",type:i.CommandType.COMMAND,handler:(o,t)=>{var R,v,b,x;const e=o.get(D.DocSelectionManagerService),n=o.get(i.IUniverInstanceService),r=o.get(i.ICommandService),{alignType:s}=t,a=n.getCurrentUniverDocInstance();if(a==null)return!1;const c=e.getDocRanges();if(c.length===0)return!1;const l=c[0].segmentId,A=a.getSelfOrHeaderFooterModel(l),d=(v=(R=A.getBody())==null?void 0:R.paragraphs)!=null?v:[],u=(x=(b=A.getBody())==null?void 0:b.dataStream)!=null?x:"";if(d==null)return!1;const m=i.BuildTextUtils.range.getParagraphsInRanges(c,d,u),g=a.getUnitId(),h=m.every(y=>{var _;return((_=y.paragraphStyle)==null?void 0:_.horizontalAlign)===s}),p={id:D.RichTextEditingMutation.id,params:{unitId:g,actions:[],textRanges:c}},f=new i.MemoryCursor;f.reset();const S=new i.TextX,E=i.JSONX.getInstance();for(const y of m){const{startIndex:_}=y;S.push({t:i.TextXActionType.RETAIN,len:_-f.cursor});const O={...y.paragraphStyle,horizontalAlign:h?i.HorizontalAlign.UNSPECIFIED:s};S.push({t:i.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[{...y,paragraphStyle:O,startIndex:0}]},coverType:i.UpdateDocsAttributeType.REPLACE}),f.moveCursorTo(_+1)}const C=J(a,l);return p.params.actions=E.editOp(S.serialize(),C),!!r.syncExecuteCommand(p.id,p.params)}},Dt={id:"doc.command.align-left",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(pe.id,{alignType:i.HorizontalAlign.LEFT})},Bt={id:"doc.command.align-center",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(pe.id,{alignType:i.HorizontalAlign.CENTER})},_t={id:"doc.command.align-right",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(pe.id,{alignType:i.HorizontalAlign.RIGHT})},Nt={id:"doc.command.align-justify",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(pe.id,{alignType:i.HorizontalAlign.JUSTIFIED})},aa=(o,t)=>{const e=o.get(i.IUniverInstanceService).getUniverDocInstance(t.unitId),n=(e==null?void 0:e.zoomRatio)||1;return{...i.Tools.deepClone(t),zoomRatio:n}},Ae={id:"doc.operation.set-zoom-ratio",type:i.CommandType.OPERATION,handler:(o,t)=>{const e=o.get(i.IUniverInstanceService).getUniverDocInstance(t.unitId);if(!e)return!1;const n=e.getSnapshot();return n.settings==null?n.settings={zoomRatio:t.zoomRatio}:n.settings.zoomRatio=t.zoomRatio,!0}},St={type:i.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(o,t)=>{var u,m,g;const e=o.get(i.ICommandService),n=o.get(i.IUndoRedoService),r=o.get(i.IUniverInstanceService);let s=(u=r.getCurrentUniverDocInstance())==null?void 0:u.getUnitId();if(!s)return!1;let a=1;if(t&&(s=(m=t.documentId)!=null?m:s,a=(g=t.zoomRatio)!=null?g:a),!r.getUniverDocInstance(s))return!1;const l={zoomRatio:a,unitId:s},A=aa(o,l);return e.syncExecuteCommand(Ae.id,l)?(n.pushUndoRedo({unitID:s,undoMutations:[{id:Ae.id,params:A}],redoMutations:[{id:Ae.id,params:l}]}),!0):!1}},le={id:"doc.command.set-paragraph-named-style",type:i.CommandType.COMMAND,handler(o,t){var h;if(!t)return!1;const n=o.get(i.IUniverInstanceService).getCurrentUnitOfType(i.UniverInstanceType.UNIVER_DOC);if(!n)return!1;const r=n.getUnitId(),s=o.get(D.DocSelectionManagerService),a=(h=t.textRanges)!=null?h:s.getTextRanges({unitId:r,subUnitId:r});if(!(a!=null&&a.length))return!1;const c=a[0].segmentId,l=i.BuildTextUtils.paragraph.style.set({document:n.getSelfOrHeaderFooterModel(c),textRanges:a,style:{namedStyleType:t.value,headingId:!t.value||t.value===i.NamedStyleType.NORMAL_TEXT?void 0:i.generateRandomId(6),spaceAbove:void 0,spaceBelow:void 0,lineSpacing:void 0},paragraphTextRun:{}}),A={id:D.RichTextEditingMutation.id,params:{actions:[],textRanges:a,unitId:r,segmentId:c}},d=i.JSONX.getInstance(),u=J(n,c);return A.params.actions=d.editOp(l.serialize(),u),!!o.get(i.ICommandService).syncExecuteCommand(A.id,A.params)}},Ci={id:"doc.command.quick-heading",type:i.CommandType.COMMAND,handler(o,t){var R,v,b,x;if(!t)return!1;const{value:e}=t,n=o.get(D.DocSelectionManagerService),r=o.get(i.IUniverInstanceService),s=o.get(i.ICommandService),a=r.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),c=n.getActiveTextRange();if(a==null||c==null||!c.collapsed)return!1;const{segmentId:l,startOffset:A}=c,d=a.getSelfOrHeaderFooterModel(l),u=(v=(R=d.getBody())==null?void 0:R.paragraphs)!=null?v:[],m=(x=(b=d.getBody())==null?void 0:b.dataStream)!=null?x:"",g=i.BuildTextUtils.paragraph.util.getParagraphsInRange(c,u,m)[0];if(!g)return!1;const{paragraphStart:h}=g,p=new i.TextX,f=i.JSONX.getInstance(),{paragraphStyle:S={}}=g,E={id:D.RichTextEditingMutation.id,params:{unitId:a.getUnitId(),actions:[],textRanges:[{startOffset:h,endOffset:h,collapsed:!0}],isEditing:!1}};p.push({t:i.TextXActionType.RETAIN,len:h}),i.BuildTextUtils.paragraph.style.set({textRanges:[c],document:a,style:{...S,headingId:i.generateRandomId(6),namedStyleType:e},cursor:A,deleteLen:A-h,textX:p,paragraphTextRun:{}});const C=J(a,l);return E.params.actions=f.editOp(p.serialize(),C),!!s.syncExecuteCommand(E.id,E.params)}},_n={"#":i.NamedStyleType.HEADING_1,"##":i.NamedStyleType.HEADING_2,"###":i.NamedStyleType.HEADING_3,"####":i.NamedStyleType.HEADING_4,"#####":i.NamedStyleType.HEADING_5},wo={id:"doc.command.h1-heading",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.HEADING_1})},Uo={id:"doc.command.h2-heading",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.HEADING_2})},Lo={id:"doc.command.h3-heading",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.HEADING_3})},Qo={id:"doc.command.h4-heading",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.HEADING_4})},Fo={id:"doc.command.h5-heading",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.HEADING_5})},zt={id:"doc.command.normal-text-heading",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.NORMAL_TEXT})},Ti={id:"doc.command.title",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.TITLE})},yi={id:"doc.command.subtitle-heading",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.SUBTITLE})};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 $e={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 Gn(o,t){return Ho(o,t)?{start:o,end:t}:{start:t,end:o}}function Xn(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 ca(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 Kn(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 Ee{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(`
11
+ `)return!0;const u=[];let m=0;for(const h of A){const{startIndex:p,endIndex:f}=h;m!==p&&u.push({startOffset:m,endOffset:p-1,rangeType:i.DOC_RANGE_TYPE.TEXT}),u.push({startOffset:p+3,endOffset:f-5,rangeType:i.DOC_RANGE_TYPE.RECT}),m=f}return m!==l.dataStream.length-2&&u.push({startOffset:m,endOffset:l.dataStream.length-2,rangeType:i.DOC_RANGE_TYPE.TEXT}),e.replaceDocRanges(u,{unitId:c,subUnitId:c},!1),!0}};class Ot extends i.RxDisposable{constructor(e){super();M(this,"_previousActiveRange",null);M(this,"_undoMutationParamsCache",[]);M(this,"_redoMutationParamsCache",[]);this._context=e}clearUndoRedoMutationParamsCache(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[]}getUndoRedoMutationParamsCache(){return{undoCache:this._undoMutationParamsCache,redoCache:this._redoMutationParamsCache}}setUndoRedoMutationParamsCache({undoCache:e=[],redoCache:n=[]}){this._undoMutationParamsCache=e,this._redoMutationParamsCache=n}getActiveRange(){return this._previousActiveRange}setActiveRange(e){this._previousActiveRange=e}pushUndoRedoMutationParams(e,n){this._undoMutationParamsCache.push(e),this._redoMutationParamsCache.push(n)}fetchComposedUndoRedoMutationParams(){if(this._undoMutationParamsCache.length===0||this._previousActiveRange==null||this._redoMutationParamsCache.length===0)return null;const{unitId:e}=this._undoMutationParamsCache[0],n={unitId:e,actions:this._undoMutationParamsCache.reverse().reduce((s,a)=>i.JSONX.compose(s,a.actions),null),textRanges:[]};return{redoMutationParams:{unitId:e,actions:this._redoMutationParamsCache.reduce((s,a)=>i.JSONX.compose(s,a.actions),null),textRanges:[]},undoMutationParams:n,previousActiveRange:this._previousActiveRange}}dispose(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[],this._previousActiveRange=null}}const Ro={id:"doc.command.ime-input",type:i.CommandType.COMMAND,handler:async(o,t)=>{var L,G,W;const{unitId:e,newText:n,oldTextLen:r,isCompositionEnd:s,isCompositionStart:a}=t,c=o.get(i.ICommandService),l=o.get(B.IRenderManagerService),A=o.get(i.IUniverInstanceService),d=o.get(ve),u=(L=l.getRenderById(e))==null?void 0:L.with(Ot),m=A.getUnit(e,i.UniverInstanceType.UNIVER_DOC);if(m==null||u==null)return!1;const g=u.getActiveRange();if(g==null)return!1;const{style:h,segmentId:p}=g,f=m.getSelfOrHeaderFooterModel(p).getBody();if(f==null)return!1;Object.assign(g,g);const{startOffset:E,endOffset:C}=g,T=n.length,R=[{startOffset:E+T,endOffset:E+T,collapsed:!0,style:h}],v={id:D.RichTextEditingMutation.id,params:{unitId:e,actions:[],textRanges:R}},b=d.getDefaultStyle(),x=d.getStyleCache(),y=lo((G=f.customRanges)!=null?G:[],E+r,i.SHEET_EDITOR_UNITS.includes(e)),_=an(f,a?C:E+r,b,x,i.SHEET_EDITOR_UNITS.includes(e)),O=Ao((W=f.customDecorations)!=null?W:[],E+r),w=new i.TextX,Q=i.JSONX.getInstance();if(!g.collapsed&&a){const H=i.BuildTextUtils.selection.delete([g],f,0,null,!1);w.push(...H),v.params.textRanges=[{startOffset:E+T,endOffset:E+T,collapsed:!0}]}else w.push({t:i.TextXActionType.RETAIN,len:E});r>0&&w.push({t:i.TextXActionType.DELETE,len:r}),w.push({t:i.TextXActionType.INSERT,body:{dataStream:n,textRuns:_?[{..._,st:0,ed:n.length}]:[],customRanges:y?[{...y,startIndex:0,endIndex:n.length-1}]:[],customDecorations:O.map(H=>({...H,startIndex:0,endIndex:n.length-1}))},len:n.length});const k=J(m,p);v.params.actions=Q.editOp(w.serialize(),k),v.params.noHistory=!s,v.params.isCompositionEnd=s;const F=c.syncExecuteCommand(v.id,v.params);return u.pushUndoRedoMutationParams(F,v.params),!!F}};function ue(o,t,e){return e.executeCommand(be.id,{preCommandId:o,...t!=null?t:{}})}const dr="doc.command.set-inline-format-bold",_e={id:dr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(dr,t,e)}},Ar="doc.command.set-inline-format-italic",Ne={id:Ar,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(Ar,t,e)}},ur="doc.command.set-inline-format-underline",Pe={id:ur,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(ur,t,e)}},gr="doc.command.set-inline-format-strikethrough",we={id:gr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(gr,t,e)}},hr="doc.command.set-inline-format-subscript",ye={id:hr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(hr,t,e)}},mr="doc.command.set-inline-format-superscript",Ue={id:mr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(mr,t,e)}},pr="doc.command.set-inline-format-fontsize",me={id:pr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(pr,t,e)}},fr="doc.command.set-inline-format-font-family",Xe={id:fr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(fr,t,e)}},Sr="doc.command.set-inline-format-text-color",Me={id:Sr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(Sr,t,e)}},Ir="doc.command.set-inline-format-text-background-color",De={id:Ir,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(Ir,t,e)}},vr="doc.command.reset-inline-format-text-background-color",st={id:vr,type:i.CommandType.COMMAND,handler:async(o,t)=>{const e=o.get(i.ICommandService);return ue(vr,t,e)}},Wn={[_e.id]:"bl",[Ne.id]:"it",[Pe.id]:"ul",[we.id]:"st",[me.id]:"fs",[Xe.id]:"ff",[Me.id]:"cl",[De.id]:"bg",[st.id]:"bg",[ye.id]:"va",[Ue.id]:"va"},be={id:"doc.command.set-inline-format",type:i.CommandType.COMMAND,handler:async(o,t)=>{var R;const{value:e,preCommandId:n}=t,r=o.get(i.ICommandService),s=o.get(D.DocSelectionManagerService),a=o.get(i.IUniverInstanceService),c=o.get(ve),l=s.getDocRanges(),A=(R=l.find(v=>v.isActive))!=null?R:l[0];if(l.length===0)return!1;const{segmentId:d}=l[0],u=a.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC);if(u==null)return!1;const m=u.getSelfOrHeaderFooterModel(d).getBody();if(m==null)return!1;const g=u.getUnitId();let h;switch(n){case _e.id:case Ne.id:case Pe.id:case we.id:case ye.id:case Ue.id:{const v=c.getDefaultStyle(),b=pi(m,A,v);h=ia(b,n);break}case me.id:case Xe.id:{h=e;break}case Me.id:case De.id:{h={rgb:e};break}case st.id:{h={rgb:null};break}default:throw new Error(`Unknown command: ${n} in handleInlineFormat`)}const p={id:D.RichTextEditingMutation.id,params:{unitId:g,actions:[],textRanges:l}},f=new i.TextX,S=i.JSONX.getInstance(),E=new i.MemoryCursor;E.reset();for(const v of l){let{startOffset:b,endOffset:x,rangeType:y}=v;if(b==null||x==null)continue;if(y===i.DOC_RANGE_TYPE.RECT&&(b=b-1),b===x){const w=c.getStyleCache(),Q=Wn[n];c.setStyleCache({[Q]:(w==null?void 0:w[Q])!==void 0?mi(w,Q,n):h});continue}const _={dataStream:"",textRuns:[{st:0,ed:x-b,ts:{[Wn[n]]:h}}]},O=b-E.cursor;O!==0&&f.push({t:i.TextXActionType.RETAIN,len:O}),f.push({t:i.TextXActionType.RETAIN,body:_,len:x-b}),E.reset(),E.moveCursor(x)}const C=J(u,d);return p.params.actions=S.editOp(f.serialize(),C),!!r.syncExecuteCommand(p.id,p.params)}};function ra(o){return o!==null&&typeof o=="object"}function mi(o,t,e){if(/bl|it/.test(t))return(o==null?void 0:o[t])===i.BooleanNumber.TRUE?i.BooleanNumber.FALSE:i.BooleanNumber.TRUE;if(/ul|st/.test(t))return ra(o==null?void 0:o[t])&&(o==null?void 0:o[t]).s===i.BooleanNumber.TRUE?{s:i.BooleanNumber.FALSE}:{s:i.BooleanNumber.TRUE};if(/va/.test(t))return e===ye.id?(o==null?void 0:o[t])===i.BaselineOffset.SUBSCRIPT?i.BaselineOffset.NORMAL:i.BaselineOffset.SUBSCRIPT:(o==null?void 0:o[t])===i.BaselineOffset.SUPERSCRIPT?i.BaselineOffset.NORMAL:i.BaselineOffset.SUPERSCRIPT}function pi(o,t,e){var d,u,m,g,h,p,f,S,E,C,T,R,v,b;const{startOffset:n,endOffset:r,collapsed:s}=t;if(s){const x=(d=o.textRuns)!=null?d:[];let y=null;for(let _=x.length-1;_>=0;_--){const O=x[_];if(O.st<n&&n<=O.ed){y=O;break}}return y!=null&&y.ts?{...e,...y.ts}:e}const{textRuns:a=[]}=i.getBodySlice(o,n,r),c=i.Tools.deepClone(e),l=a.map(x=>{var y;return(y=x==null?void 0:x.ts)==null?void 0:y.fs}).filter(Boolean);c.fs=c.fs?Math.max(c.fs,...l):l.length?Math.max(...l):void 0,c.fs=!c.fs||Number.isNaN(c.fs)?void 0:c.fs,c.ff=(g=(m=(u=a.find(x=>{var y;return((y=x.ts)==null?void 0:y.ff)!=null}))==null?void 0:u.ts)==null?void 0:m.ff)!=null?g:c.ff,c.it=a.length&&a.every(x=>{var y;return((y=x.ts)==null?void 0:y.it)===i.BooleanNumber.TRUE})?i.BooleanNumber.TRUE:i.BooleanNumber.FALSE,c.bl=a.length&&a.every(x=>{var y;return((y=x.ts)==null?void 0:y.bl)===i.BooleanNumber.TRUE})?i.BooleanNumber.TRUE:i.BooleanNumber.FALSE,c.ul=a.length&&a.every(x=>{var y,_;return((_=(y=x.ts)==null?void 0:y.ul)==null?void 0:_.s)===i.BooleanNumber.TRUE})?(h=a[0].ts)==null?void 0:h.ul:c.ul,c.st=a.length&&a.every(x=>{var y,_;return((_=(y=x.ts)==null?void 0:y.st)==null?void 0:_.s)===i.BooleanNumber.TRUE})?(p=a[0].ts)==null?void 0:p.st:c.st,c.bg=(E=(S=(f=a.find(x=>{var y;return((y=x.ts)==null?void 0:y.bg)!=null}))==null?void 0:f.ts)==null?void 0:S.bg)!=null?E:c.bg,c.cl=(R=(T=(C=a.find(x=>{var y;return((y=x.ts)==null?void 0:y.cl)!=null}))==null?void 0:C.ts)==null?void 0:T.cl)!=null?R:c.cl;const A=a.filter(x=>{var y;return((y=x==null?void 0:x.ts)==null?void 0:y.va)!=null});if(A.length>0&&A.length===a.length){const x=(v=A[0].ts)==null?void 0:v.va;let y=!0;for(let _=1;_<A.length;_++)if(((b=A[_].ts)==null?void 0:b.va)!==x){y=!1;break}y&&(c.va=x)}return c}function ia(o,t){const e=Wn[t];return mi(o,e,t)}const xo={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)}},fi={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=Ii(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=Ii(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 sa(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 Ii(o,t,e,n,r){const s={id:D.RichTextEditingMutation.id,params:{unitId:o,actions:[],textRanges:[]}},a=sa(t,e,n,r);return s.params.actions=a,s}const Oo={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)}},vi={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)}},Ei={id:"doc.command.insert-custom-range",type:i.CommandType.COMMAND,handler:(o,t)=>{if(!t)return!1;const e=o.get(i.ICommandService),{unitId:n,rangeId:r=i.generateRandomId(),textRanges:s,properties:a,text:c,wholeEntity:l}=t,A={unitId:n,textRanges:s,body:{dataStream:c,customRanges:[{startIndex:0,endIndex:c.length-1,rangeId:r,rangeType:i.CustomRangeType.CUSTOM,properties:a,wholeEntity:l}]}};return e.syncExecuteCommand(Oo.id,A)}},at={id:"doc.command.list-operation",type:i.CommandType.COMMAND,handler:(o,t)=>{var T,R,v,b,x,y;const e=o.get(D.DocSelectionManagerService),n=o.get(i.IUniverInstanceService),r=o.get(i.ICommandService),s=t.listType,a=n.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),c=(R=(T=t.docRange)!=null?T:e.getDocRanges())!=null?R:[];if(a==null||c.length===0)return!1;const l=c[0].segmentId,A=a.getSelfOrHeaderFooterModel(l),d=(b=(v=A.getBody())==null?void 0:v.paragraphs)!=null?b:[],u=(y=(x=A.getBody())==null?void 0:x.dataStream)!=null?y:"";if(!d.length)return!1;const m=i.BuildTextUtils.range.getParagraphsInRanges(c,d,u),g=a.getUnitId(),h={id:D.RichTextEditingMutation.id,params:{unitId:g,actions:[],textRanges:c,isEditing:!1}};new i.MemoryCursor().reset();const f=i.BuildTextUtils.paragraph.bullet.switch({paragraphs:m,listType:s,document:a,segmentId:l}),S=i.JSONX.getInstance(),E=J(a,l);return h.params.actions=S.editOp(f.serialize(),E),!!r.syncExecuteCommand(h.id,h.params)}},Mt={id:"doc.command.change-list-type",type:i.CommandType.COMMAND,handler:(o,t)=>{var C,T,R,v,b;const e=o.get(D.DocSelectionManagerService),n=o.get(i.IUniverInstanceService),r=o.get(i.ICommandService),{listType:s}=t,a=n.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),c=(C=t.docRange)!=null?C:e.getDocRanges();if(a==null||c==null||!c.length)return!1;const{segmentId:l}=c[0],A=a.getSelfOrHeaderFooterModel(l),d=(R=(T=A.getBody())==null?void 0:T.paragraphs)!=null?R:[],u=(b=(v=A.getBody())==null?void 0:v.dataStream)!=null?b:"";if(!d.length)return!1;const m=i.BuildTextUtils.range.getParagraphsInRanges(c,d,u),g=a.getUnitId(),h=i.BuildTextUtils.paragraph.bullet.set({paragraphs:m,listType:s,segmentId:l,document:a});if(!h)return!1;const p=i.JSONX.getInstance(),f={id:D.RichTextEditingMutation.id,params:{unitId:g,actions:[],textRanges:c,isEditing:!1}},S=J(a,l);return f.params.actions=p.editOp(h.serialize(),S),!!r.syncExecuteCommand(f.id,f.params)}};var ft=(o=>(o[o.increase=1]="increase",o[o.decrease=-1]="decrease",o))(ft||{});const Kt={id:"doc.command.change-list-nesting-level",type:i.CommandType.COMMAND,handler:(o,t)=>{var T,R,v,b,x;if(!t)return!1;const{type:e}=t,n=o.get(D.DocSelectionManagerService),r=o.get(i.IUniverInstanceService),s=o.get(i.ICommandService),a=r.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),c=n.getActiveTextRange();if(a==null||c==null)return!1;const{segmentId:l}=c,A=(T=n.getDocRanges())!=null?T:[],d=a.getSelfOrHeaderFooterModel(l),u=(v=(R=d.getBody())==null?void 0:R.paragraphs)!=null?v:[],m=(x=(b=d.getBody())==null?void 0:b.dataStream)!=null?x:"";if(!u.length)return!1;const g=i.BuildTextUtils.range.getParagraphsInRange(c,u,m),h=a.getUnitId(),p=i.JSONX.getInstance(),f=i.BuildTextUtils.paragraph.bullet.changeNestLevel({paragraphs:g,type:e,segmentId:l,document:a}),S={id:D.RichTextEditingMutation.id,params:{unitId:h,actions:[],textRanges:A,isEditing:!1}},E=J(a,l);return S.params.actions=p.editOp(f.serialize(),E),!!s.syncExecuteCommand(S.id,S.params)}},ot={id:"doc.command.bullet-list",type:i.CommandType.COMMAND,handler:(o,t)=>{const e=o.get(i.ICommandService);return t!=null&&t.value?e.syncExecuteCommand(Mt.id,{listType:t.value,docRange:t.docRange}):e.syncExecuteCommand(at.id,{listType:i.PresetListType.BULLET_LIST,docRange:t==null?void 0:t.docRange})}},Et={id:"doc.command.check-list",type:i.CommandType.COMMAND,handler:(o,t)=>{const e=o.get(i.ICommandService);return t!=null&&t.value?e.syncExecuteCommand(Mt.id,{listType:t.value,docRange:t.docRange}):e.syncExecuteCommand(at.id,{listType:i.PresetListType.CHECK_LIST,docRange:t==null?void 0:t.docRange})}},Mo={id:"doc.command.toggle-check-list",type:i.CommandType.COMMAND,handler:(o,t)=>{var p;if(!t)return!1;const e=o.get(i.IUniverInstanceService),n=o.get(i.ICommandService),{index:r,segmentId:s,textRanges:a}=t,c=e.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC);if(c==null||((p=c.getSelfOrHeaderFooterModel(s).getBody())==null?void 0:p.paragraphs)==null)return!1;const A=c.getUnitId(),d={id:D.RichTextEditingMutation.id,params:{unitId:A,actions:[],textRanges:a!=null?a:[],segmentId:s,isEditing:!1}},u=i.BuildTextUtils.paragraph.bullet.toggleChecklist({document:c,paragraphIndex:r,segmentId:s});if(!u)return!1;const m=i.JSONX.getInstance(),g=J(c,s);return d.params.actions=m.editOp(u.serialize(),g),!!n.syncExecuteCommand(d.id,d.params)}},rt={id:"doc.command.order-list",type:i.CommandType.COMMAND,handler:(o,t)=>{const e=o.get(i.ICommandService);return t!=null&&t.value?e.syncExecuteCommand(Mt.id,{listType:t.value}):e.syncExecuteCommand(at.id,{listType:i.PresetListType.ORDER_LIST})}},Do={id:"doc.command.quick-list",type:i.CommandType.COMMAND,handler(o,t){var O,w,Q;if(!t)return!1;const e=o.get(D.DocSelectionManagerService),n=o.get(i.IUniverInstanceService),r=o.get(i.ICommandService),s=n.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),a=e.getActiveTextRange();if(s==null||a==null)return!1;const{segmentId:c,startOffset:l}=a,{listType:A,paragraph:d}=t,{paragraphStart:u,paragraphEnd:m}=d,g=new i.TextX,h=i.JSONX.getInstance(),{paragraphStyle:p={}}=d,S=(i.PRESET_LIST_TYPE[A].nestingLevel[0].paragraphProperties||{}).textStyle;let C=i.generateRandomId(6);const T=(w=(O=s.getBody())==null?void 0:O.paragraphs)!=null?w:[],R=T.findIndex(k=>k.startIndex===d.startIndex),v=T[R-1],b=T[R+1];v&&v.bullet&&v.bullet.listType.indexOf(A)===0?C=v.bullet.listId:b&&b.bullet&&b.bullet.listType.indexOf(A)===0&&(C=b.bullet.listId);const x={id:D.RichTextEditingMutation.id,params:{unitId:s.getUnitId(),actions:[],textRanges:[{startOffset:u,endOffset:u,collapsed:!0}],isEditing:!1}};g.push({t:i.TextXActionType.RETAIN,len:u}),g.push({t:i.TextXActionType.DELETE,len:l-u}),m>l&&g.push({t:i.TextXActionType.RETAIN,len:m-l}),g.push({t:i.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[{startIndex:0,paragraphStyle:{...p,textStyle:{...p.textStyle,...S},indentFirstLine:void 0},bullet:{...(Q=d.bullet)!=null?Q:{nestingLevel:0,textStyle:{fs:20}},listType:A,listId:C}}]}});const y=J(s,c);return x.params.actions=h.editOp(g.serialize(),y),!!r.syncExecuteCommand(x.id,x.params)}};function Bo(o,t){var d,u;const e=o.get(i.ICommandService),n=bt(o);if(!n)return!1;const r=o.get(i.IUniverInstanceService).getCurrentUnitOfType(i.UniverInstanceType.UNIVER_DOC);if(!r)return!1;const s=i.BuildTextUtils.selection.replace({doc:r,selection:{startOffset:n.startIndex+1,endOffset:n.startIndex+1,collapsed:!0},body:{dataStream:"\r",paragraphs:[{startIndex:0,paragraphStyle:{...n.paragraphStyle},bullet:{listType:t,listId:((d=n.bullet)==null?void 0:d.listType)===t?n.bullet.listId:i.generateRandomId(6),nestingLevel:((u=n.bullet)==null?void 0:u.listType)===t?n.bullet.nestingLevel:0}}]}});if(!s)return!1;const a={id:D.RichTextEditingMutation.id,params:{unitId:r.getUnitId(),actions:[],textRanges:[{startOffset:n.startIndex+1,endOffset:n.startIndex+1,collapsed:!0}],isEditing:!1}},c=i.JSONX.getInstance(),l=J(r);return a.params.actions=c.editOp(s.serialize(),l),!!e.syncExecuteCommand(a.id,a.params)}const _o={id:"doc.command.insert-bullet-list-bellow",type:i.CommandType.COMMAND,handler:o=>Bo(o,i.PresetListType.BULLET_LIST)},No={id:"doc.command.insert-order-list-bellow",type:i.CommandType.COMMAND,handler:o=>Bo(o,i.PresetListType.ORDER_LIST)},Po={id:"doc.command.insert-check-list-bellow",type:i.CommandType.COMMAND,handler:o=>Bo(o,i.PresetListType.CHECK_LIST)},pe={id:"doc.command.align-action",type:i.CommandType.COMMAND,handler:(o,t)=>{var R,v,b,x;const e=o.get(D.DocSelectionManagerService),n=o.get(i.IUniverInstanceService),r=o.get(i.ICommandService),{alignType:s}=t,a=n.getCurrentUniverDocInstance();if(a==null)return!1;const c=e.getDocRanges();if(c.length===0)return!1;const l=c[0].segmentId,A=a.getSelfOrHeaderFooterModel(l),d=(v=(R=A.getBody())==null?void 0:R.paragraphs)!=null?v:[],u=(x=(b=A.getBody())==null?void 0:b.dataStream)!=null?x:"";if(d==null)return!1;const m=i.BuildTextUtils.range.getParagraphsInRanges(c,d,u),g=a.getUnitId(),h=m.every(y=>{var _;return((_=y.paragraphStyle)==null?void 0:_.horizontalAlign)===s}),p={id:D.RichTextEditingMutation.id,params:{unitId:g,actions:[],textRanges:c}},f=new i.MemoryCursor;f.reset();const S=new i.TextX,E=i.JSONX.getInstance();for(const y of m){const{startIndex:_}=y;S.push({t:i.TextXActionType.RETAIN,len:_-f.cursor});const O={...y.paragraphStyle,horizontalAlign:h?i.HorizontalAlign.UNSPECIFIED:s};S.push({t:i.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[{...y,paragraphStyle:O,startIndex:0}]},coverType:i.UpdateDocsAttributeType.REPLACE}),f.moveCursorTo(_+1)}const C=J(a,l);return p.params.actions=E.editOp(S.serialize(),C),!!r.syncExecuteCommand(p.id,p.params)}},Dt={id:"doc.command.align-left",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(pe.id,{alignType:i.HorizontalAlign.LEFT})},Bt={id:"doc.command.align-center",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(pe.id,{alignType:i.HorizontalAlign.CENTER})},_t={id:"doc.command.align-right",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(pe.id,{alignType:i.HorizontalAlign.RIGHT})},Nt={id:"doc.command.align-justify",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(pe.id,{alignType:i.HorizontalAlign.JUSTIFIED})},aa=(o,t)=>{const n=o.get(i.IUniverInstanceService).getUnit(t.unitId,i.UniverInstanceType.UNIVER_DOC);return n==null||n.setZoomRatio(t.zoomRatio),{...t}},Ae={id:"doc.operation.set-zoom-ratio",type:i.CommandType.OPERATION,handler:(o,t)=>{const n=o.get(i.IUniverInstanceService).getUnit(t.unitId,i.UniverInstanceType.UNIVER_DOC);return n?(n.setZoomRatio(t.zoomRatio),!0):!1}},St={type:i.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(o,t)=>{var u,m,g;const e=o.get(i.ICommandService),n=o.get(i.IUndoRedoService),r=o.get(i.IUniverInstanceService);let s=(u=r.getCurrentUniverDocInstance())==null?void 0:u.getUnitId();if(!s)return!1;let a=1;if(t&&(s=(m=t.documentId)!=null?m:s,a=(g=t.zoomRatio)!=null?g:a),!r.getUniverDocInstance(s))return!1;const l={zoomRatio:a,unitId:s},A=aa(o,l);return e.syncExecuteCommand(Ae.id,l)?(n.pushUndoRedo({unitID:s,undoMutations:[{id:Ae.id,params:A}],redoMutations:[{id:Ae.id,params:l}]}),!0):!1}},le={id:"doc.command.set-paragraph-named-style",type:i.CommandType.COMMAND,handler(o,t){var h;if(!t)return!1;const n=o.get(i.IUniverInstanceService).getCurrentUnitOfType(i.UniverInstanceType.UNIVER_DOC);if(!n)return!1;const r=n.getUnitId(),s=o.get(D.DocSelectionManagerService),a=(h=t.textRanges)!=null?h:s.getTextRanges({unitId:r,subUnitId:r});if(!(a!=null&&a.length))return!1;const c=a[0].segmentId,l=i.BuildTextUtils.paragraph.style.set({document:n.getSelfOrHeaderFooterModel(c),textRanges:a,style:{namedStyleType:t.value,headingId:!t.value||t.value===i.NamedStyleType.NORMAL_TEXT?void 0:i.generateRandomId(6),spaceAbove:void 0,spaceBelow:void 0,lineSpacing:void 0},paragraphTextRun:{}}),A={id:D.RichTextEditingMutation.id,params:{actions:[],textRanges:a,unitId:r,segmentId:c}},d=i.JSONX.getInstance(),u=J(n,c);return A.params.actions=d.editOp(l.serialize(),u),!!o.get(i.ICommandService).syncExecuteCommand(A.id,A.params)}},Ci={id:"doc.command.quick-heading",type:i.CommandType.COMMAND,handler(o,t){var R,v,b,x;if(!t)return!1;const{value:e}=t,n=o.get(D.DocSelectionManagerService),r=o.get(i.IUniverInstanceService),s=o.get(i.ICommandService),a=r.getCurrentUnitForType(i.UniverInstanceType.UNIVER_DOC),c=n.getActiveTextRange();if(a==null||c==null||!c.collapsed)return!1;const{segmentId:l,startOffset:A}=c,d=a.getSelfOrHeaderFooterModel(l),u=(v=(R=d.getBody())==null?void 0:R.paragraphs)!=null?v:[],m=(x=(b=d.getBody())==null?void 0:b.dataStream)!=null?x:"",g=i.BuildTextUtils.paragraph.util.getParagraphsInRange(c,u,m)[0];if(!g)return!1;const{paragraphStart:h}=g,p=new i.TextX,f=i.JSONX.getInstance(),{paragraphStyle:S={}}=g,E={id:D.RichTextEditingMutation.id,params:{unitId:a.getUnitId(),actions:[],textRanges:[{startOffset:h,endOffset:h,collapsed:!0}],isEditing:!1}};p.push({t:i.TextXActionType.RETAIN,len:h}),i.BuildTextUtils.paragraph.style.set({textRanges:[c],document:a,style:{...S,headingId:i.generateRandomId(6),namedStyleType:e},cursor:A,deleteLen:A-h,textX:p,paragraphTextRun:{}});const C=J(a,l);return E.params.actions=f.editOp(p.serialize(),C),!!s.syncExecuteCommand(E.id,E.params)}},_n={"#":i.NamedStyleType.HEADING_1,"##":i.NamedStyleType.HEADING_2,"###":i.NamedStyleType.HEADING_3,"####":i.NamedStyleType.HEADING_4,"#####":i.NamedStyleType.HEADING_5},wo={id:"doc.command.h1-heading",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.HEADING_1})},Uo={id:"doc.command.h2-heading",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.HEADING_2})},Lo={id:"doc.command.h3-heading",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.HEADING_3})},Qo={id:"doc.command.h4-heading",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.HEADING_4})},Fo={id:"doc.command.h5-heading",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.HEADING_5})},zt={id:"doc.command.normal-text-heading",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.NORMAL_TEXT})},Ti={id:"doc.command.title",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.TITLE})},yi={id:"doc.command.subtitle-heading",type:i.CommandType.COMMAND,handler:o=>o.get(i.ICommandService).syncExecuteCommand(le.id,{value:i.NamedStyleType.SUBTITLE})};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 $e={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 Gn(o,t){return Ho(o,t)?{start:o,end:t}:{start:t,end:o}}function Xn(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 ca(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 Kn(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 Ee{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(`
12
12
  Invalid positions in NodePositionConvertToCursor,
13
13
  they are not in the same segment page when in header or footer.`);const{start:c,end:l}=Gn(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}=ca(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(Kn(x)),r.push(Kn(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($e.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($e.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($e.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($e.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($e.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 bi(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 ko(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 Ri(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 Nn(o,t){if(ko(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 la(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 Vo{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=la(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(Kn(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 da="__TestSelectionRange__",Aa="__TestSelectionAnchor__",Er=6,Pn=500,ua=500,Yt=3;function It(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 In(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()},Pn),this._anchorBlinkTimer=setInterval(()=>{this._anchorShape&&this._anchorShape.visible&&(this.activeStatic(),setTimeout(()=>{this.deactivateStatic()},Pn))},ua+Pn)}get startOffset(){var r;const{startOffset:t}=(r=Xn(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=Xn(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}=Gn(this.anchorNodePosition,this.focusNodePosition);return t}get endNodePosition(){if(this.anchorNodePosition==null)return this.focusNodePosition;if(this.focusNodePosition==null)return null;const{end:t}=Gn(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 Ee(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=It(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=It(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 Ee(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($e);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(da+i.generateRandomId(Er),{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,Yt)}_createOrUpdateAnchor(t,e,n,r){var p,f,S;const s=It(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(Aa+i.generateRandomId(Er),{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,Yt),this.activeStatic()}_setCursorList(t){t.length!==0&&(this._cursorList=t)}}const ga="__DocTableRectRange__",ha=6;function Wt(o,t,e,n,r,s=B.NORMAL_TEXT_SELECTION_PLUGIN_STYLE,a="",c=-1){const l=t.getOffsetConfig(),d=new Vo(l,e).getNodePositionGroup(n,r);return(d!=null?d:[]).map(u=>new jo(o,t,e,u.anchor,u.focus,s,a,c))}class jo{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 Nn(t,e)?t:e}get endNodePosition(){const{anchorNodePosition:t,focusNodePosition:e}=this;return Nn(t,e)?e:t}get direction(){const{anchorNodePosition:t,focusNodePosition:e}=this;return Nn(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 Vo(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(ga+i.generateRandomId(ha),{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,Yt)}}function Cr(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 ma(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 jo(e,n,r,l,A,s,a,c)}function pa(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 zn(l,A,e,n,r,s,a,c)}function zn(o,t,e,n,r,s,a,c){const l=[],A=[],d=[e,n,r,o,t,s,a,c];if(Ri(r,o,t)){if(ko(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(bi(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 xi(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 Tr(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 Yn(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 fa(o){const t=Yn(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 Sa=Object.getOwnPropertyDescriptor,Ia=(o,t,e,n)=>{for(var r=n>1?void 0:n?Sa(t,e):t,s=o.length-1,a;s>=0;s--)(a=o[s])&&(r=a(r)||r);return r},Ut=(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=pa(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=ma(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=Cr(S.isBack?h:h-1,E.isBack?p:p-1,l,d,u,c,s,a,S.isBack,E.isBack):C=Cr(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=Tr(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=Tr(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(Yn)}_getAllRectRanges(){return this._rectRangeList.map(fa)}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:Yn(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=`
14
14
  position:absolute;