@univerjs/docs 0.1.0-beta.1 → 0.1.0-beta.3

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.
@@ -1,5 +1,3 @@
1
-
2
-
3
1
  Apache License
4
2
  Version 2.0, January 2004
5
3
  http://www.apache.org/licenses/
package/lib/cjs/index.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var ut=Object.defineProperty;var mt=(i,e,t)=>e in i?ut(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var v=(i,e,t)=>(mt(i,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@univerjs/core"),R=require("@univerjs/engine-render"),V=require("rxjs"),x=require("@univerjs/ui"),T=require("@wendellhu/redi");function Q(i,e){const n=i.getCurrentUniverDocInstance().getUnitId(),r=e.getRenderById(n);if(r==null)return;const{mainComponent:o,scene:s,engine:c}=r;return{document:o,scene:s,engine:c}}function gt(i,e){const t=e.getRenderById(i);if(t==null)return;const{mainComponent:n,scene:r,engine:o}=t;return{document:n,scene:r,engine:o}}var ve=(i=>(i.MAIN="__Document_Render_Main__",i))(ve||{}),Me=(i=>(i.VIEW_MAIN="viewMain",i.VIEW_TOP="viewTop",i.VIEW_LEFT="viewLeft",i.VIEW_LEFT_TOP="viewLeftTop",i))(Me||{});const Te=0,Qe=2,et=10,pt="normalTextSelectionPluginName",Ee={id:"doc.operation.set-selections",type:a.CommandType.OPERATION,handler:(i,e)=>!0};var ht=Object.defineProperty,ft=Object.getOwnPropertyDescriptor,St=(i,e,t,n)=>{for(var r=n>1?void 0:n?ft(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&ht(e,t,r),r},ze=(i,e)=>(t,n)=>e(t,n,i);function Ge(i){const{startOffset:e,endOffset:t,collapsed:n}=i;return{startOffset:e,endOffset:t,collapsed:n}}exports.TextSelectionManagerService=class extends a.RxDisposable{constructor(t,n){super();v(this,"_currentSelection",null);v(this,"_textSelectionInfo",new Map);v(this,"_textSelection$",new V.BehaviorSubject(null));v(this,"textSelection$",this._textSelection$.asObservable());this._textSelectionRenderManager=t,this._commandService=n,this._syncSelectionFromRenderService()}getCurrentSelection(){return this._currentSelection}getCurrentSelectionInfo(){return this._getTextRanges(this._currentSelection)}dispose(){this._textSelection$.complete()}refreshSelection(){this._currentSelection!=null&&this._refresh(this._currentSelection)}setCurrentSelection(t){this._currentSelection=t,this._refresh(t)}setCurrentSelectionNotRefresh(t){this._currentSelection=t}getSelections(){var t;return(t=this._getTextRanges(this._currentSelection))==null?void 0:t.textRanges}getActiveRange(){const t=this._getTextRanges(this._currentSelection);if(t==null)return;const{textRanges:n,segmentId:r,style:o}=t,s=n.find(m=>m.isActive());if(s==null)return null;const{startOffset:c,endOffset:l,collapsed:u,startNodePosition:d,endNodePosition:g,direction:p}=s;return c==null||l==null?null:{startOffset:c,endOffset:l,collapsed:u,startNodePosition:d,endNodePosition:g,direction:p,segmentId:r,style:o}}add(t){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:t,segmentId:"",style:R.NORMAL_TEXT_SELECTION_PLUGIN_STYLE})}replaceTextRanges(t){this._currentSelection!=null&&(this._textSelectionRenderManager.removeAllTextRanges(),this._textSelectionRenderManager.addTextRanges(t))}_syncSelectionFromRenderService(){this._textSelectionRenderManager.textSelectionInner$.pipe(V.takeUntil(this.dispose$)).subscribe(t=>{t!=null&&this._replaceTextRangesWithNoRefresh(t)})}_replaceTextRangesWithNoRefresh(t){if(this._currentSelection==null)return;const n={...this._currentSelection,...t};this._replaceByParam(n),this._textSelection$.next(n);const{unitId:r,subUnitId:o,segmentId:s,style:c,textRanges:l}=n;this._commandService.executeCommand(Ee.id,{unitId:r,subUnitId:o,segmentId:s,style:c,ranges:l.map(Ge)})}_getTextRanges(t){var o;if(t==null)return;const{unitId:n,subUnitId:r=""}=t;return(o=this._textSelectionInfo.get(n))==null?void 0:o.get(r)}_refresh(t){const n=this._getTextRanges(t);this._textSelectionRenderManager.removeAllTextRanges(),n&&Array.isArray(n.textRanges)&&n.textRanges.length&&this._textSelectionRenderManager.addTextRanges(n.textRanges.map(Ge))}_replaceByParam(t){const{unitId:n,subUnitId:r,style:o,segmentId:s,textRanges:c}=t;this._textSelectionInfo.has(n)||this._textSelectionInfo.set(n,new Map),this._textSelectionInfo.get(n).set(r,{textRanges:c,style:o,segmentId:s})}_addByParam(t){const{unitId:n,subUnitId:r,textRanges:o,style:s,segmentId:c}=t;this._textSelectionInfo.has(n)||this._textSelectionInfo.set(n,new Map);const l=this._textSelectionInfo.get(n);l.has(r)?l.get(r).textRanges.push(...o):l.set(r,{textRanges:o,style:s,segmentId:c})}};exports.TextSelectionManagerService=St([ze(0,R.ITextSelectionRenderManager),ze(1,a.ICommandService)],exports.TextSelectionManagerService);function Re(i,e="",t=0){const{startOffset:n,endOffset:r}=i,o=[],s=n-t,c=r-t;return s>0&&o.push({t:"r",len:s,segmentId:e}),o.push({t:"d",len:c-s,line:0,segmentId:e}),o}var _t=Object.defineProperty,vt=Object.getOwnPropertyDescriptor,Ct=(i,e,t,n)=>{for(var r=n>1?void 0:n?vt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&_t(e,t,r),r},It=(i,e)=>(t,n)=>e(t,n,i);exports.DocViewModelManagerService=class{constructor(e){v(this,"_currentViewModelUnitId","");v(this,"_docViewModelMap",new Map);v(this,"_currentDocViewModel$",new V.BehaviorSubject(null));v(this,"currentDocViewModel$",this._currentDocViewModel$.asObservable());this._currentUniverService=e}dispose(){this._currentDocViewModel$.complete(),this._docViewModelMap=new Map}getCurrent(){return this._docViewModelMap.get(this._currentViewModelUnitId)}getViewModel(e){var t;return(t=this._docViewModelMap.get(e))==null?void 0:t.docViewModel}setCurrent(e){var n;const t=this._currentUniverService.getUniverDocInstance(e);if(t==null)throw new Error(`Document data model with id ${e} not found when build view model.`);if(t.getBody()!=null){if(!this._docViewModelMap.has(e)){const r=this._buildDocViewModel(t);this._docViewModelMap.set(e,{unitId:e,docViewModel:r})}if(e===a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY){const r=(n=this._docViewModelMap.get(e))==null?void 0:n.docViewModel;if(r==null)return;r.reset(t)}this._currentViewModelUnitId=e,this._currentDocViewModel$.next(this.getCurrent())}}_buildDocViewModel(e){return new R.DocumentViewModel(e)}};exports.DocViewModelManagerService=Ct([It(0,a.IUniverInstanceService)],exports.DocViewModelManagerService);const _={id:"doc.mutation.rich-text-editing",type:a.CommandType.MUTATION,handler:(i,e)=>{const{unitId:t,mutations:n}=e,o=i.get(a.IUniverInstanceService).getUniverDocInstance(t),c=i.get(exports.DocViewModelManagerService).getViewModel(t);if(o==null||c==null)throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${t}`);const l=o.apply(n),u=n[0].segmentId,d=o.getSelfOrHeaderFooterModel(u);return c.getSelfOrHeaderFooterViewModel(u).reset(d),{unitId:t,mutations:l}}},Oe={id:"doc.command.insert-text",type:a.CommandType.COMMAND,handler:async(i,e)=>{const t=i.get(a.IUndoRedoService),n=i.get(a.ICommandService),r=i.get(exports.TextSelectionManagerService),{range:o,segmentId:s,body:c,unitId:l,textRanges:u}=e,{startOffset:d,collapsed:g}=o,p={id:_.id,params:{unitId:l,mutations:[]}};g?d>0&&p.params.mutations.push({t:"r",len:d,segmentId:s}):p.params.mutations.push(...Re(o,s)),p.params.mutations.push({t:"i",body:c,len:c.dataStream.length,line:0,segmentId:s});const m=n.syncExecuteCommand(p.id,p.params);return r.replaceTextRanges(u),m?(t.pushUndoRedo({unitID:l,undoMutations:[{id:_.id,params:m}],redoMutations:[{id:_.id,params:p.params}],undo(){return n.syncExecuteCommand(_.id,m),r.replaceTextRanges([o]),!0},redo(){return n.syncExecuteCommand(_.id,p.params),r.replaceTextRanges(u),!0}}),!0):!1}};var L=(i=>(i[i.LEFT=0]="LEFT",i[i.RIGHT=1]="RIGHT",i))(L||{});const ye={id:"doc.command.delete-text",type:a.CommandType.COMMAND,handler:async(i,e)=>{const t=i.get(a.ICommandService),n=i.get(a.IUndoRedoService),r=i.get(exports.TextSelectionManagerService),{range:o,segmentId:s,unitId:c,direction:l,textRanges:u,len:d=1}=e,{startOffset:g}=o,p={id:_.id,params:{unitId:c,mutations:[]}};g>0&&p.params.mutations.push({t:"r",len:l===0?g-d:g,segmentId:s}),p.params.mutations.push({t:"d",len:d,line:0,segmentId:s});const m=t.syncExecuteCommand(p.id,p.params);return r.replaceTextRanges(u),m&&n.pushUndoRedo({unitID:c,undoMutations:[{id:_.id,params:m}],redoMutations:[{id:_.id,params:p.params}],undo(){return t.syncExecuteCommand(_.id,m),r.replaceTextRanges([o]),!0},redo(){return t.syncExecuteCommand(_.id,p.params),r.replaceTextRanges(u),!0}}),!1}},Pe={id:"doc.command.update-text",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{range:t,segmentId:n,updateBody:r,coverType:o,unitId:s,textRanges:c}=e,l=i.get(a.ICommandService),u=i.get(a.IUndoRedoService),d=i.get(exports.TextSelectionManagerService),g={id:_.id,params:{unitId:s,mutations:[]}},{startOffset:p,endOffset:m}=t;g.params.mutations.push({t:"r",len:p,segmentId:n}),g.params.mutations.push({t:"r",body:r,len:m-p,segmentId:n,coverType:o});const h=l.syncExecuteCommand(g.id,g.params);return d.replaceTextRanges(c),h?(u.pushUndoRedo({unitID:s,undoMutations:[{id:_.id,params:h}],redoMutations:[{id:_.id,params:g.params}],undo(){return l.syncExecuteCommand(_.id,h),d.replaceTextRanges(c),!0},redo(){return l.syncExecuteCommand(_.id,g.params),d.replaceTextRanges(c),!0}}),!0):!1}};function Mt(i,e){const t=[];for(let n=0,r=i.length;n<r;n++)i[n]===a.DataStreamTreeTokenType.PARAGRAPH&&t.push({startIndex:n});if(e)for(const n of t)e.bullet&&(n.bullet=a.Tools.deepClone(e.bullet)),e.paragraphStyle&&(n.paragraphStyle=a.Tools.deepClone(e.paragraphStyle));return t}const Ne={id:"doc.command.break-line",type:a.CommandType.COMMAND,handler:async i=>{var h;const e=i.get(exports.TextSelectionManagerService),t=i.get(a.IUniverInstanceService),n=i.get(a.ICommandService),r=e.getActiveRange();if(r==null)return!1;const o=t.getCurrentUniverDocInstance(),s=o.getUnitId(),{startOffset:c,segmentId:l,style:u}=r,d=[{startOffset:c+1,endOffset:c+1,style:u}],p=(((h=o.getBody())==null?void 0:h.paragraphs)??[]).find(f=>f.startIndex>=c);return await n.executeCommand(Oe.id,{unitId:s,body:{dataStream:a.DataStreamTreeTokenType.PARAGRAPH,paragraphs:Mt(a.DataStreamTreeTokenType.PARAGRAPH,p)},range:r,textRanges:d,segmentId:l})}},se={id:x.CopyCommand.id,name:"doc.command.copy",type:a.CommandType.COMMAND,multi:!0,priority:999,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.FOCUSING_EDITOR),handler:async()=>!0},ae={id:x.CutCommand.id,name:"doc.command.cut",type:a.CommandType.COMMAND,multi:!0,priority:999,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.FOCUSING_EDITOR),handler:async()=>!0},ce={id:x.PasteCommand.id,name:"doc.command.paste",type:a.CommandType.COMMAND,multi:!0,priority:999,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.FOCUSING_EDITOR),handler:async()=>!0};var Rt=Object.defineProperty,Ot=Object.getOwnPropertyDescriptor,yt=(i,e,t,n)=>{for(var r=n>1?void 0:n?Ot(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Rt(e,t,r),r},We=(i,e)=>(t,n)=>e(t,n,i);exports.DocSkeletonManagerService=class{constructor(e,t){v(this,"_currentSkeletonUnitId","");v(this,"_docSkeletonMap",new Map);v(this,"_currentSkeleton$",new V.BehaviorSubject(null));v(this,"currentSkeleton$",this._currentSkeleton$.asObservable());v(this,"_currentSkeletonBefore$",new V.BehaviorSubject(null));v(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());this._localeService=e,this._docViewModelManagerService=t,this.initialize()}initialize(){this._docViewModelManagerService.currentDocViewModel$.subscribe(e=>{e!=null&&this._setCurrent(e)})}dispose(){this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete(),this._docSkeletonMap=new Map}getCurrent(){return this.getSkeletonByUnitId(this._currentSkeletonUnitId)}makeDirtyCurrent(e=!0){this.makeDirty(this._currentSkeletonUnitId,e)}makeDirty(e,t=!0){const n=this.getSkeletonByUnitId(e);n!=null&&(n.dirty=t)}_setCurrent(e){const{unitId:t}=e;if(this._docSkeletonMap.has(t)){const n=this.getSkeletonByUnitId(t);n.skeleton.calculate(),n.dirty=!0}else{const n=this._buildSkeleton(e.docViewModel);n.calculate(),this._docSkeletonMap.set(t,{unitId:t,skeleton:n,dirty:!1})}return this._currentSkeletonUnitId=t,this._currentSkeletonBefore$.next(this.getCurrent()),this._currentSkeleton$.next(this.getCurrent()),this.getCurrent()}getSkeletonByUnitId(e){return this._docSkeletonMap.get(e)}_buildSkeleton(e){return R.DocumentSkeleton.create(e,this._localeService)}};exports.DocSkeletonManagerService=yt([We(0,T.Inject(a.LocaleService)),We(1,T.Inject(exports.DocViewModelManagerService))],exports.DocSkeletonManagerService);const He={id:"doc.command.inner-paste",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{segmentId:t,body:n,textRanges:r}=e,o=i.get(a.IUndoRedoService),s=i.get(a.ICommandService),c=i.get(exports.TextSelectionManagerService),l=i.get(a.IUniverInstanceService),u=c.getSelections();if(!Array.isArray(u)||u.length===0)return!1;const g=l.getCurrentUniverDocInstance().getUnitId(),p={id:_.id,params:{unitId:g,mutations:[]}},m=new a.MemoryCursor;m.reset();for(const f of u){const{startOffset:S,endOffset:C,collapsed:I}=f,D=S-m.cursor;I?p.params.mutations.push({t:"r",len:D,segmentId:t}):p.params.mutations.push(...Re(f,t,m.cursor)),p.params.mutations.push({t:"i",body:n,len:n.dataStream.length,line:0,segmentId:t}),m.reset(),m.moveCursor(C)}const h=s.syncExecuteCommand(p.id,p.params);return c.replaceTextRanges(r),h?(o.pushUndoRedo({unitID:g,undoMutations:[{id:_.id,params:h}],redoMutations:[{id:_.id,params:p.params}],undo(){return s.syncExecuteCommand(_.id,h),c.replaceTextRanges(u),!0},redo(){return s.syncExecuteCommand(_.id,p.params),c.replaceTextRanges(r),!0}}),!0):!1}},Ce={id:"doc.command.inner-cut",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{segmentId:t,textRanges:n}=e,r=i.get(a.IUndoRedoService),o=i.get(a.ICommandService),s=i.get(exports.TextSelectionManagerService),c=i.get(a.IUniverInstanceService),l=s.getSelections();if(!Array.isArray(l)||l.length===0)return!1;const u=c.getCurrentUniverDocInstance().getUnitId(),d=c.getUniverDocInstance(u),g=a.getDocsUpdateBody(d.snapshot,t);if(g==null)return!1;const p={id:_.id,params:{unitId:u,mutations:[]}},m=new a.MemoryCursor;m.reset();for(const f of l){const{startOffset:S,endOffset:C,collapsed:I}=f,D=S-m.cursor;I?p.params.mutations.push({t:"r",len:D,segmentId:t}):p.params.mutations.push(...xt(f,g,t,m.cursor)),m.reset(),m.moveCursor(C)}const h=o.syncExecuteCommand(p.id,p.params);return s.replaceTextRanges(n),h?(r.pushUndoRedo({unitID:u,undoMutations:[{id:_.id,params:h}],redoMutations:[{id:_.id,params:p.params}],undo(){return o.syncExecuteCommand(_.id,h),s.replaceTextRanges(l),!0},redo(){return o.syncExecuteCommand(_.id,p.params),s.replaceTextRanges(n),!0}}),!0):!1}};function xt(i,e,t="",n=0){const{startOffset:r,endOffset:o}=i,s=[],{paragraphs:c=[]}=e,l=r-n,u=o-n,d=c==null?void 0:c.find(g=>g.startIndex-n>=l&&g.startIndex-n<=u);if(l>0&&s.push({t:"r",len:l,segmentId:t}),d&&d.startIndex-n>l){const g=d.startIndex-n;s.push({t:"d",len:g-l,line:0,segmentId:t}),s.push({t:"r",len:1,segmentId:t}),u>g+1&&s.push({t:"d",len:u-g-1,line:0,segmentId:t})}else s.push({t:"d",len:u-l,line:0,segmentId:t});return s}const we={id:"doc.command.delete-left",type:a.CommandType.COMMAND,handler:async i=>{var y;const e=i.get(exports.TextSelectionManagerService),t=i.get(exports.DocSkeletonManagerService),n=i.get(a.IUniverInstanceService),r=i.get(a.ICommandService),o=e.getActiveRange(),s=e.getSelections(),c=(y=t.getCurrent())==null?void 0:y.skeleton;let l;if(o==null||c==null||s==null)return!1;const u=n.getCurrentUniverDocInstance(),{startOffset:d,collapsed:g,segmentId:p,style:m}=o;if(d===0&&g)return!0;const h=c.findNodeByCharIndex(d),f=R.hasListSpan(h),S=R.isIndentBySpan(h,u.body);let C=d;const I=c.findNodeByCharIndex(d-1);if(R.isFirstSpan(h)&&I!==h&&(f===!0||S===!0)){const M=R.getParagraphBySpan(h,u.body);if(M==null)return!1;const b=M==null?void 0:M.startIndex,U={startIndex:0},E=M.paragraphStyle;if(f===!0){const O=M.paragraphStyle;O&&(U.paragraphStyle=O)}else if(S===!0){const O=M.bullet;O&&(U.bullet=O),E!=null&&(U.paragraphStyle={...E},delete U.paragraphStyle.hanging,delete U.paragraphStyle.indentStart)}const P=[{startOffset:C,endOffset:C,style:m}];l=await r.executeCommand(Pe.id,{unitId:u.getUnitId(),updateBody:{dataStream:"",paragraphs:[{...U}]},range:{startOffset:b,endOffset:b+1},textRanges:P,coverType:a.UpdateDocsAttributeType.REPLACE,segmentId:p})}else if(g===!0)if(I.content==="\r")l=await r.executeCommand(Ae.id,{direction:L.LEFT,range:o});else{C-=I.count;const M=[{startOffset:C,endOffset:C,style:m}];l=await r.executeCommand(ye.id,{unitId:u.getUnitId(),range:o,segmentId:p,direction:L.LEFT,len:I.count,textRanges:M})}else{const M=tt(o,s);l=await r.executeCommand(Ce.id,{segmentId:p,textRanges:M})}return l}},Le={id:"doc.command.delete-right",type:a.CommandType.COMMAND,handler:async i=>{var h;const e=i.get(exports.TextSelectionManagerService),t=i.get(exports.DocSkeletonManagerService),n=i.get(a.IUniverInstanceService),r=i.get(a.ICommandService),o=e.getActiveRange(),s=e.getSelections(),c=(h=t.getCurrent())==null?void 0:h.skeleton;let l;if(o==null||c==null||s==null)return!1;const u=n.getCurrentUniverDocInstance(),{startOffset:d,collapsed:g,segmentId:p,style:m}=o;if(d===u.getBody().dataStream.length-2&&g)return!0;if(g===!0){const f=c.findNodeByCharIndex(d);if(f.content==="\r")l=await r.executeCommand(Ae.id,{direction:L.RIGHT,range:o});else{const S=[{startOffset:d,endOffset:d,style:m}];l=await r.executeCommand(ye.id,{unitId:u.getUnitId(),range:o,segmentId:p,direction:L.RIGHT,textRanges:S,len:f.count})}}else{const f=tt(o,s);l=await r.executeCommand(Ce.id,{segmentId:p,textRanges:f})}return l}},Ae={id:"doc.command.merge-two-paragraph",type:a.CommandType.COMMAND,handler:async(i,e)=>{var U,E,P;const t=i.get(exports.TextSelectionManagerService),n=i.get(a.IUniverInstanceService),r=i.get(a.ICommandService),o=i.get(a.IUndoRedoService),{direction:s,range:c}=e,l=t.getActiveRange(),u=t.getSelections();if(l==null||u==null)return!1;const d=n.getCurrentUniverDocInstance(),{startOffset:g,collapsed:p,segmentId:m,style:h}=l;if(!p)return!1;const f=s===L.LEFT?g:g+1,S=(P=(E=(U=d.getBody())==null?void 0:U.paragraphs)==null?void 0:E.find(O=>O.startIndex>=f))==null?void 0:P.startIndex,C=Dt(d.getBody(),f,S),I=s===L.LEFT?g-1:g,D=d.getUnitId(),y=[{startOffset:I,endOffset:I,style:h}],M={id:_.id,params:{unitId:D,mutations:[]}};M.params.mutations.push({t:"r",len:s===L.LEFT?g-1:g,segmentId:m}),C.dataStream.length&&M.params.mutations.push({t:"i",body:C,len:C.dataStream.length,line:0,segmentId:m}),M.params.mutations.push({t:"r",len:1,segmentId:m}),M.params.mutations.push({t:"d",len:S+1-f,line:0,segmentId:m});const b=r.syncExecuteCommand(M.id,M.params);return t.replaceTextRanges(y),b?(o.pushUndoRedo({unitID:D,undoMutations:[{id:_.id,params:b}],redoMutations:[{id:_.id,params:M.params}],undo(){return r.syncExecuteCommand(_.id,b),t.replaceTextRanges([c]),!0},redo(){return r.syncExecuteCommand(_.id,M.params),t.replaceTextRanges(y),!0}}),!0):!1}};function Dt(i,e,t){const{textRuns:n}=i,r=i.dataStream.substring(e,t);if(n==null)return{dataStream:r};const o=[];for(const s of n){const{st:c,ed:l}=s;l<=e||c>=t||(c<e?o.push({...s,st:0,ed:l-e}):l>t?o.push({...s,st:c-e,ed:t-e}):o.push({...s,st:c-e,ed:l-e}))}return{dataStream:r,textRuns:o}}function tt(i,e){let t=i.endOffset;for(const r of e){const{startOffset:o,endOffset:s}=r;o==null||s==null||s<=i.endOffset&&(t-=s-o)}return[{startOffset:t,endOffset:t,style:i.style}]}class Be{constructor(){v(this,"_previousActiveRange",null);v(this,"_undoMutationParamsCache",[]);v(this,"_redoMutationParamsCache",[])}clearUndoRedoMutationParamsCache(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[]}setActiveRange(e){this._previousActiveRange=e}pushUndoRedoMutationParams(e,t){this._undoMutationParamsCache.push(e),this._redoMutationParamsCache.push(t)}fetchComposedUndoRedoMutationParams(e){if(this._undoMutationParamsCache.length===0||this._previousActiveRange==null)return null;const{unitId:t}=this._undoMutationParamsCache[0],{segmentId:n,startOffset:r,collapsed:o}=this._previousActiveRange,s={unitId:t,mutations:[]},c={unitId:t,mutations:[]};return o?(s.mutations.push({t:"r",len:r,segmentId:n}),c.mutations.push({t:"r",len:r,segmentId:n})):(r>0&&s.mutations.push({t:"r",len:r,segmentId:n}),s.mutations.push(this._undoMutationParamsCache[0].mutations.find(l=>l.t==="i")),c.mutations.push(...Re(this._previousActiveRange,n))),e.length&&(s.mutations.push({t:"d",len:e.length,line:0,segmentId:n}),c.mutations.push({t:"i",body:{dataStream:e},len:e.length,line:0,segmentId:n})),{redoMutationParams:c,undoMutationParams:s,previousActiveRange:this._previousActiveRange}}dispose(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[],this._previousActiveRange=null}}const $e={id:"doc.command.ime-input",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{unitId:t,newText:n,oldTextLen:r,range:o,segmentId:s,textRanges:c,isCompositionEnd:l}=e,u=i.get(a.ICommandService),d=i.get(a.IUndoRedoService),g=i.get(exports.TextSelectionManagerService),p=i.get(Be),m={id:_.id,params:{unitId:t,mutations:[]}};o.collapsed?m.params.mutations.push({t:"r",len:o.startOffset,segmentId:s}):m.params.mutations.push(...Re(o,s)),r>0&&m.params.mutations.push({t:"d",len:r,line:0,segmentId:s}),m.params.mutations.push({t:"i",body:{dataStream:n},len:n.length,line:0,segmentId:s});const h=u.syncExecuteCommand(m.id,m.params);if(p.pushUndoRedoMutationParams(h,m.params),g.replaceTextRanges(c),l){if(h){const f=p.fetchComposedUndoRedoMutationParams(n);if(f==null)return!1;const{undoMutationParams:S,redoMutationParams:C,previousActiveRange:I}=f;return d.pushUndoRedo({unitID:t,undoMutations:[{id:_.id,params:S}],redoMutations:[{id:_.id,params:C}],undo(){return u.syncExecuteCommand(_.id,S),g.replaceTextRanges([I]),!0},redo(){return u.syncExecuteCommand(_.id,C),g.replaceTextRanges(c),!0}}),!0}}else return!!h;return!1}},W={id:"doc.command.set-inline-format-bold",type:a.CommandType.COMMAND,handler:async()=>!0},ee={id:"doc.command.set-inline-format-italic",type:a.CommandType.COMMAND,handler:async()=>!0},te={id:"doc.command.set-inline-format-underline",type:a.CommandType.COMMAND,handler:async()=>!0},ne={id:"doc.command.set-inline-format-strikethrough",type:a.CommandType.COMMAND,handler:async()=>!0},A={id:"doc.command.set-inline-format-subscript",type:a.CommandType.COMMAND,handler:async()=>!0},k={id:"doc.command.set-inline-format-superscript",type:a.CommandType.COMMAND,handler:async()=>!0},H={id:"doc.command.set-inline-format-fontsize",type:a.CommandType.COMMAND,handler:async()=>!0},K={id:"doc.command.set-inline-format-font-family",type:a.CommandType.COMMAND,handler:async()=>!0},re={id:"doc.command.set-inline-format-text-color",type:a.CommandType.COMMAND,handler:async()=>!0},nt={[W.id]:"bl",[ee.id]:"it",[te.id]:"ul",[ne.id]:"st",[H.id]:"fs",[K.id]:"ff",[re.id]:"cl",[A.id]:"va",[k.id]:"va"},Fe={id:"doc.command.set-inline-format",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{segmentId:t,value:n,preCommandId:r}=e,o=i.get(a.IUndoRedoService),s=i.get(a.ICommandService),c=i.get(exports.TextSelectionManagerService),l=i.get(a.IUniverInstanceService),u=c.getSelections();if(!Array.isArray(u)||u.length===0)return!1;let d=l.getCurrentUniverDocInstance(),g=d.getUnitId();g===a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY&&(d=l.getUniverDocInstance(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),g=d.getUnitId());let p;switch(r){case W.id:case ee.id:case te.id:case ne.id:case A.id:case k.id:{p=bt(d.getBody().textRuns,r,u);break}case H.id:case K.id:{p=n;break}case re.id:{p={rgb:n};break}default:throw new Error(`Unknown command: ${r} in handleInlineFormat`)}const m={id:_.id,params:{unitId:g,mutations:[]}},h=new a.MemoryCursor;h.reset();for(const C of u){const{startOffset:I,endOffset:D}=C,y={dataStream:"",textRuns:[{st:0,ed:D-I,ts:{[nt[r]]:p}}]},M=I-h.cursor;M!==0&&m.params.mutations.push({t:"r",len:M,segmentId:t}),m.params.mutations.push({t:"r",body:y,len:D-I,segmentId:t}),h.reset(),h.moveCursor(D)}const f=s.syncExecuteCommand(m.id,m.params),S=[W.id,H.id,K.id,A.id,k.id];return S.includes(r)&&c.refreshSelection(),f?(o.pushUndoRedo({unitID:g,undoMutations:[{id:_.id,params:f}],redoMutations:[{id:_.id,params:m.params}],undo(){return s.syncExecuteCommand(_.id,f),S.includes(r)&&c.refreshSelection(),!0},redo(){return s.syncExecuteCommand(_.id,m.params),S.includes(r)&&c.refreshSelection(),!0}}),!0):!1}};function Tt(i){return i!==null&&typeof i=="object"}function bt(i,e,t){let n=0,r=0;const o=nt[e];for(;n!==i.length&&r!==t.length;){const{startOffset:s,endOffset:c}=t[r],{st:l,ed:u,ts:d}=i[n];if(c<=l)r++;else if(u<=s)n++;else{if((d==null?void 0:d[o])==null){if(/bl|it/.test(o))return a.BooleanNumber.TRUE;if(/ul|st/.test(o))return{s:a.BooleanNumber.TRUE};if(/va/.test(o))return e===A.id?a.BaselineOffset.SUBSCRIPT:a.BaselineOffset.SUPERSCRIPT}if(Tt(d==null?void 0:d[o])&&d[o].s===a.BooleanNumber.FALSE)return{s:a.BooleanNumber.TRUE};if(e===A.id&&(d==null?void 0:d[o])!==a.BaselineOffset.SUBSCRIPT)return a.BaselineOffset.SUBSCRIPT;if(e===k.id&&(d==null?void 0:d[o])!==a.BaselineOffset.SUPERSCRIPT)return a.BaselineOffset.SUPERSCRIPT;if((d==null?void 0:d[o])===a.BooleanNumber.FALSE)return a.BooleanNumber.TRUE;n++}}return/bl|it/.test(o)?a.BooleanNumber.FALSE:/ul|st/.test(o)?{s:a.BooleanNumber.FALSE}:a.BaselineOffset.NORMAL}const rt={id:"doc.command.bullet-list",type:a.CommandType.COMMAND,handler:i=>i.get(a.ICommandService).syncExecuteCommand(je.id,{listType:a.PresetListType.BULLET_LIST})},it={id:"doc.command.order-list",type:a.CommandType.COMMAND,handler:i=>i.get(a.ICommandService).syncExecuteCommand(je.id,{listType:a.PresetListType.ORDER_LIST})},je={id:"doc.command.list-operation",type:a.CommandType.COMMAND,handler:(i,e)=>{var D;const t=i.get(exports.TextSelectionManagerService),n=i.get(a.IUniverInstanceService),r=i.get(a.ICommandService),o=i.get(a.IUndoRedoService),{listType:s}=e,c=n.getCurrentUniverDocInstance(),l=t.getActiveRange(),u=(D=c.getBody())==null?void 0:D.paragraphs;if(l==null||u==null)return!1;const d=Ut(l,u),{segmentId:g}=l,p=c.getUnitId(),m=d.every(y=>{var M;return((M=y.bullet)==null?void 0:M.listType)===s}),h=6;let f=a.Tools.generateRandomId(h);if(d.length===1){const y=u.indexOf(d[0]),M=u[y-1],b=u[y+1];M&&M.bullet&&M.bullet.listType===s?f=M.bullet.listId:b&&b.bullet&&b.bullet.listType===s&&(f=b.bullet.listId)}const S={id:_.id,params:{unitId:p,mutations:[]}},C=new a.MemoryCursor;C.reset();for(const y of d){const{startIndex:M}=y;S.params.mutations.push({t:"r",len:M-C.cursor,segmentId:g});const b={...y.paragraphStyle,hanging:void 0,indentStart:void 0};S.params.mutations.push({t:"r",len:1,body:{dataStream:"",paragraphs:[m?{paragraphStyle:b,startIndex:0}:{...y,startIndex:0,bullet:{...y.bullet??{nestingLevel:0,textStyle:{fs:20}},listType:s,listId:f}}]},segmentId:g,coverType:a.UpdateDocsAttributeType.REPLACE}),C.moveCursorTo(M+1)}const I=r.syncExecuteCommand(S.id,S.params);return t.refreshSelection(),I&&o.pushUndoRedo({unitID:p,undoMutations:[{id:_.id,params:I}],redoMutations:[{id:_.id,params:S.params}],undo(){return r.syncExecuteCommand(_.id,I),t.refreshSelection(),!0},redo(){return r.syncExecuteCommand(_.id,S.params),t.refreshSelection(),!0}}),!0}};function Ut(i,e){const{startOffset:t,endOffset:n}=i,r=[];let o=0;for(const s of e){const{startIndex:c}=s;(t>o&&t<=c||n>o&&n<=c||c>=t&&c<=n)&&r.push(s),o=c}return r}const ot={id:"doc.command-replace-content",type:a.CommandType.COMMAND,handler:async(i,e)=>{var h;const{unitId:t,body:n,textRanges:r,segmentId:o=""}=e,s=i.get(a.IUniverInstanceService),c=i.get(a.ICommandService),l=i.get(exports.TextSelectionManagerService),u=i.get(a.IUndoRedoService),d=(h=s.getUniverDocInstance(t))==null?void 0:h.getSnapshot().body,g=l.getSelections();if(d==null||!Array.isArray(g)||g.length===0)return!1;const p=at(t,o,d,n),m=c.syncExecuteCommand(p.id,p.params);return l.replaceTextRanges(r),m?(u.pushUndoRedo({unitID:t,undoMutations:[{id:_.id,params:m}],redoMutations:[{id:_.id,params:p.params}],undo(){return c.syncExecuteCommand(_.id,m),l.replaceTextRanges(g),!0},redo(){return c.syncExecuteCommand(_.id,p.params),l.replaceTextRanges(r),!0}}),!0):!1}},st={id:"doc.command-cover-content",type:a.CommandType.COMMAND,handler:async(i,e)=>{var d;const{unitId:t,body:n,segmentId:r=""}=e,o=i.get(a.IUniverInstanceService),s=i.get(a.ICommandService),c=i.get(a.IUndoRedoService),l=(d=o.getUniverDocInstance(t))==null?void 0:d.getSnapshot().body;if(l==null)return!1;const u=at(t,r,l,n);return s.syncExecuteCommand(u.id,u.params),c.clearUndoRedo(t),!0}};function at(i,e,t,n){const r={id:_.id,params:{unitId:i,mutations:[]}},o=(t==null?void 0:t.dataStream.length)-2;return o>0&&r.params.mutations.push({t:"d",len:o,line:0,segmentId:e}),n.dataStream.length>0&&r.params.mutations.push({t:"i",body:n,len:n.dataStream.length,line:0,segmentId:e}),r}const B={id:"doc.operation.move-cursor",type:a.CommandType.OPERATION,handler:(i,e)=>!!e},$={id:"doc.operation.move-selection",type:a.CommandType.OPERATION,handler:(i,e)=>!!e},Et=(i,e)=>{const t=i.get(a.IUniverInstanceService).getUniverDocInstance(e.unitId),n=(t==null?void 0:t.zoomRatio)||1;return{...a.Tools.deepClone(e),zoomRatio:n}},j={id:"doc.operation.set-zoom-ratio",type:a.CommandType.OPERATION,handler:(i,e)=>{const t=i.get(a.IUniverInstanceService).getUniverDocInstance(e.unitId);if(!t)return!1;const n=t.getSnapshot();return n.settings==null?n.settings={zoomRatio:e.zoomRatio}:n.settings.zoomRatio=e.zoomRatio,!0}},ct={type:a.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(i,e)=>{const t=i.get(a.ICommandService),n=i.get(a.IUndoRedoService),r=i.get(a.IUniverInstanceService);let o=r.getCurrentUniverDocInstance().getUnitId(),s=1;if(e&&(o=e.documentId??o,s=e.zoomRatio??s),!r.getUniverDocInstance(o))return!1;const l={zoomRatio:s,unitId:o},u=Et(i,l);return t.syncExecuteCommand(j.id,l)?(n.pushUndoRedo({unitID:o,undoMutations:[{id:j.id,params:u}],redoMutations:[{id:j.id,params:l}]}),!0):!1}},lt={id:"doc.operation.select-all",type:a.CommandType.COMMAND,handler:async i=>{const e=i.get(a.IUniverInstanceService),t=i.get(exports.TextSelectionManagerService),n=e.getCurrentUniverDocInstance().getSnapshot().body;if(n==null)return!1;const r=[{startOffset:0,endOffset:n.dataStream.length-2}];return t.replaceTextRanges(r),!0}},Pt=10,Nt=6;function wt(){return a.Tools.generateRandomId(Nt)}function Lt(i){const e=i.match(/data-copy-id="([^\s]+)"/);return e&&e[1]?e[1]:null}class At{constructor(){v(this,"_cache",new a.LRUMap(Pt))}set(e,t){this._cache.set(e,t)}get(e){return this._cache.get(e)}clear(){this._cache.clear()}}const Ke=new At;function Bt(i){const e=i.style,t={};for(let n=0;n<e.length;n++){const r=e[n],o=e.getPropertyValue(r);switch(r){case"margin-top":{const s=parseInt(o);t.spaceAbove=/pt/.test(o)?be(s):s;break}case"margin-bottom":{const s=parseInt(o);t.spaceBelow=/pt/.test(o)?be(s):s;break}}}return Object.getOwnPropertyNames(t).length?t:null}function be(i){return i/.75}function Ve(i){const e=i.style,t={},n=i.tagName.toLowerCase();switch(n){case"b":case"em":case"strong":{t.bl=a.BooleanNumber.TRUE;break}case"s":{t.st={s:a.BooleanNumber.TRUE};break}case"u":{t.ul={s:a.BooleanNumber.TRUE};break}case"i":{t.it=a.BooleanNumber.TRUE;break}case"sub":case"sup":{t.va=n==="sup"?a.BaselineOffset.SUPERSCRIPT:a.BaselineOffset.SUBSCRIPT;break}}for(let r=0;r<e.length;r++){const o=e[r],s=e.getPropertyValue(o);switch(o){case"font-family":{t.ff=s;break}case"font-size":{const c=parseInt(s);Number.isNaN(c)||(t.fs=/pt$/.test(s)?be(c):c);break}case"font-style":{s==="italic"&&(t.it=a.BooleanNumber.TRUE);break}case"font-weight":{Number(s)>400&&(t.bl=a.BooleanNumber.TRUE);break}case"text-decoration":{/underline/.test(s)?t.ul={s:a.BooleanNumber.TRUE}:/overline/.test(s)?t.ol={s:a.BooleanNumber.TRUE}:/line-through/.test(s)&&(t.st={s:a.BooleanNumber.TRUE});break}case"color":{const c=new a.ColorKit(s);c.isValid&&(t.cl={rgb:c.toRgbString()});break}case"background-color":{const c=new a.ColorKit(s);c.isValid&&(t.bg={rgb:c.toRgbString()});break}}}return t}function $t(i){const e=new DOMParser,t=`<x-univer id="univer-root">${i}</x-univer>`;return e.parseFromString(t,"text/html").querySelector("#univer-root")}function Ye(i,e){const t=i.tagName.toLowerCase();return typeof e=="string"?t===e:Array.isArray(e)?e.some(n=>n===t):e(i)}const Ie=class Ie{constructor(){v(this,"_styleCache",new Map);v(this,"_styleRules",[]);v(this,"_afterProcessRules",[])}static use(e){if(this._pluginList.includes(e))throw new Error(`Univer paste plugin ${e.name} already added`);this._pluginList.push(e)}convert(e){const t=Ie._pluginList.find(o=>o.checkPasteType(e)),n=$t(e),r={dataStream:"",textRuns:[]};return t&&(this._styleRules=[...t.stylesRules],this._afterProcessRules=[...t.afterProcessRules]),this._styleCache.clear(),this._process(null,n==null?void 0:n.childNodes,r),this._styleCache.clear(),this._styleRules=[],this._afterProcessRules=[],r}_process(e,t,n){var r;for(const o of t)if(o.nodeType===Node.TEXT_NODE){const s=(r=o.nodeValue)==null?void 0:r.replace(/[\r\n]/g,"");let c;e&&this._styleCache.has(e)&&(c=this._styleCache.get(e)),n.dataStream+=s,c&&Object.getOwnPropertyNames(c).length&&n.textRuns.push({st:n.dataStream.length-s.length,ed:n.dataStream.length,ts:c})}else if(o.nodeType===Node.ELEMENT_NODE){const s=e?this._styleCache.get(e):{},c=this._styleRules.find(({filter:g})=>Ye(o,g)),l=c?c.getStyle(o):Ve(o);this._styleCache.set(o,{...s,...l});const{childNodes:u}=o;this._process(o,u,n);const d=this._afterProcessRules.find(({filter:g})=>Ye(o,g));d&&d.handler(n,o)}}};v(Ie,"_pluginList",[]);let J=Ie;const Ft={name:"univer-doc-paste-plugin-lark",checkPasteType(i){return/lark-record-clipboard/i.test(i)},stylesRules:[{filter:["s"],getStyle(i){const e=Ve(i);return{st:{s:a.BooleanNumber.TRUE},...e}}}],afterProcessRules:[{filter(i){return i.tagName==="DIV"&&/ace-line/i.test(i.className)},handler(i){i.paragraphs==null&&(i.paragraphs=[]),i.paragraphs.push({startIndex:i.dataStream.length}),i.dataStream+="\r"}}]},jt={name:"univer-doc-paste-plugin-word",checkPasteType(i){return/word|mso/i.test(i)},stylesRules:[{filter:["b"],getStyle(i){const e=Ve(i);return{bl:a.BooleanNumber.TRUE,...e}}}],afterProcessRules:[{filter(i){return i.tagName==="P"&&/mso/i.test(i.className)},handler(i,e){i.paragraphs==null&&(i.paragraphs=[]);const t={startIndex:i.dataStream.length},n=Bt(e);n&&(t.paragraphStyle=n),i.paragraphs.push(t),i.dataStream+="\r"}}]};function Vt(i,e){const{st:t,ed:n,ts:r={}}=e,{ff:o,fs:s,it:c,bl:l,ul:u,st:d,ol:g,bg:p,cl:m,va:h}=r;let f=i.slice(t,n);const S=[];return c===a.BooleanNumber.TRUE&&(f=`<i>${f}</i>`),h===a.BaselineOffset.SUPERSCRIPT?f=`<sup>${f}</sup>`:h===a.BaselineOffset.SUBSCRIPT&&(f=`<sub>${f}</sub>`),(u==null?void 0:u.s)===a.BooleanNumber.TRUE&&(f=`<u>${f}</u>`),(d==null?void 0:d.s)===a.BooleanNumber.TRUE&&(f=`<s>${f}</s>`),l===a.BooleanNumber.TRUE&&(f=`<strong>${f}</strong>`),o&&S.push(`font-family: ${o}`),m&&S.push(`color: ${m.rgb}`),s&&S.push(`font-size: ${s}px`),g&&S.push("text-decoration: overline"),p&&S.push(`background: ${p.rgb}`),S.length?`<span style="${S.join(";")}">${f}</span>`:f}function Xe(i,e=!0){const{dataStream:t,textRuns:n=[],paragraphs:r=[]}=i;let o=0;const s=[],c=[];for(const l of n){const{st:u,ed:d}=l;if(u!==o&&s.push(t.slice(o,u)),s.push(Vt(t,l)),o=d,e)for(const g of r){const{startIndex:p,paragraphStyle:m={}}=g;if(p>=u&&p<=d){const{spaceAbove:h,spaceBelow:f}=m,S=[];h!=null&&(typeof h=="number"?S.push(`margin-top: ${h}px`):S.push(`margin-top: ${h.v}px`)),f!=null&&(typeof f=="number"?S.push(`margin-bottom: ${f}px`):S.push(`margin-bottom: ${f.v}px`)),c.push(`<p className="UniverNormal" ${S.length?`style="${S.join(";")}"`:""}>${s.join("")}</p>`),s.length=0}}}return c.join("")+s.join("")}class kt{convert(e){if(e.length===0)throw new Error("The bodyList length at least to be 1");if(e.length===1)return Xe(e[0]);let t="";for(const n of e)t+='<p className="UniverNormal">',t+=Xe(n,!1),t+="</p>";return t}}var zt=Object.defineProperty,Gt=Object.getOwnPropertyDescriptor,Wt=(i,e,t,n)=>{for(var r=n>1?void 0:n?Gt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&zt(e,t,r),r},qe=(i,e)=>(t,n)=>e(t,n,i);J.use(jt);J.use(Ft);function Ht(i){const e=i.replace(/\n/g,"\r"),t=[];for(let n=0;n<e.length;n++)e[n]==="\r"&&t.push({startIndex:n});return{dataStream:e,paragraphs:t}}const dt=T.createIdentifier("doc.clipboard-service");let Ue=class extends a.Disposable{constructor(e,t){super();v(this,"_clipboardHooks",[]);v(this,"_htmlToUDM",new J);v(this,"_umdToHtml",new kt);this._currentUniverService=e,this._clipboardInterfaceService=t}async queryClipboardData(){const e=await this._clipboardInterfaceService.read();if(e.length===0)return Promise.reject();try{let t="",n="";for(const o of e)for(const s of o.types)s===x.PLAIN_TEXT_CLIPBOARD_MIME_TYPE?n=await o.getType(s).then(c=>c&&c.text()):s===x.HTML_CLIPBOARD_MIME_TYPE&&(t=await o.getType(s).then(c=>c&&c.text()));if(!t)return Ht(n);const r=Lt(t);if(r){const o=Ke.get(r);if(o)return o}return this._htmlToUDM.convert(t)}catch(t){return Promise.reject(t)}}async setClipboardData(e){const t=wt(),n=e.length>1?e.map(o=>o.dataStream).join(`
2
- `):e[0].dataStream;let r=this._umdToHtml.convert(e);return e.length===1&&(r=r.replace(/(<[a-z]+)/,(o,s)=>`${s} data-copy-id="${t}"`),Ke.set(t,e[0])),this._clipboardInterfaceService.write(n,r)}addClipboardHook(e){return this._clipboardHooks.push(e),a.toDisposable(()=>{const t=this._clipboardHooks.indexOf(e);t>-1&&this._clipboardHooks.splice(t,1)})}};Ue=Wt([qe(0,a.IUniverInstanceService),qe(1,x.IClipboardInterfaceService)],Ue);var Kt=Object.defineProperty,Yt=Object.getOwnPropertyDescriptor,Xt=(i,e,t,n)=>{for(var r=n>1?void 0:n?Yt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Kt(e,t,r),r},z=(i,e)=>(t,n)=>e(t,n,i);let le=class extends a.Disposable{constructor(i,e,t,n,r,o){super(),this._logService=i,this._commandService=e,this._currentUniverService=t,this._docClipboardService=n,this._textSelectionManagerService=r,this._contextService=o,this._commandExecutedListener(),this.initialize()}initialize(){[se,ae,ce].forEach(i=>this.disposeWithMe(this._commandService.registerAsMultipleCommand(i))),[He,Ce].forEach(i=>this.disposeWithMe(this._commandService.registerCommand(i)))}_commandExecutedListener(){const i=[ae.id,se.id,ce.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(i.includes(e.id)&&!(!this._contextService.getContextValue(a.FOCUSING_DOC)&&!this._contextService.getContextValue(a.FOCUSING_EDITOR)))switch(e.id){case ce.id:{this._handlePaste();break}case se.id:{this._handleCopy();break}case ae.id:{this._handleCut();break}default:throw new Error(`Unhandled command ${e.id}`)}}))}async _handlePaste(){const{_docClipboardService:i}=this,{segmentId:e,endOffset:t,style:n}=this._textSelectionManagerService.getActiveRange()??{},r=this._textSelectionManagerService.getSelections();if(e==null&&this._logService.error("[DocClipboardController] segmentId is not existed"),!(t==null||r==null))try{const o=await i.queryClipboardData();let s=t;for(const l of r){const{startOffset:u,endOffset:d}=l;u==null||d==null||d<=t&&(s+=o.dataStream.length-(d-u))}const c=[{startOffset:s,endOffset:s,style:n}];this._commandService.executeCommand(He.id,{body:o,segmentId:e,textRanges:c})}catch{this._logService.error("[DocClipboardController] clipboard is empty")}}_getDocumentBodyInRanges(){const i=this._textSelectionManagerService.getSelections(),e=this._currentUniverService.getCurrentUniverDocInstance(),{dataStream:t,textRuns:n=[],paragraphs:r=[]}=e.getBody(),o=[];if(i==null)return o;for(const s of i){const{startOffset:c,endOffset:l,collapsed:u}=s;if(u||c==null||l==null)continue;const d={dataStream:t.slice(c,l)},g=[];for(const m of n){const h=a.Tools.deepClone(m),{st:f,ed:S}=h;a.Tools.hasIntersectionBetweenTwoRanges(f,S,c,l)&&(c>=f&&c<=S?g.push({...h,st:c,ed:Math.min(l,S)}):l>=f&&l<=S?g.push({...h,st:Math.max(c,f),ed:l}):g.push(h))}g.length&&(d.textRuns=g.map(m=>{const{st:h,ed:f}=m;return{...m,st:h-c,ed:f-c}}));const p=[];for(const m of r){const{startIndex:h}=m;h>=c&&h<=l&&p.push(a.Tools.deepClone(m))}p.length&&(d.paragraphs=p.map(m=>({...m,startIndex:m.startIndex-c}))),o.push(d)}return o}async _handleCopy(){const{_docClipboardService:i}=this,e=this._getDocumentBodyInRanges();try{i.setClipboardData(e)}catch{this._logService.error("[DocClipboardController] set clipboard failed")}}async _handleCut(){const{segmentId:i,endOffset:e,style:t}=this._textSelectionManagerService.getActiveRange()??{},n=this._textSelectionManagerService.getSelections();if(i==null&&this._logService.error("[DocClipboardController] segmentId is not existed"),!(e==null||n==null)){this._handleCopy();try{let r=e;for(const s of n){const{startOffset:c,endOffset:l}=s;c==null||l==null||l<=e&&(r-=l-c)}const o=[{startOffset:r,endOffset:r,style:t}];this._commandService.executeCommand(Ce.id,{segmentId:i,textRanges:o})}catch{this._logService.error("[DocClipboardController] cut content failed")}}}};le=Xt([a.OnLifecycle(a.LifecycleStages.Rendered,le),z(0,a.ILogService),z(1,a.ICommandService),z(2,a.IUniverInstanceService),z(3,dt),z(4,T.Inject(exports.TextSelectionManagerService)),z(5,a.IContextService)],le);var qt=Object.defineProperty,Zt=Object.getOwnPropertyDescriptor,Jt=(i,e,t,n)=>{for(var r=n>1?void 0:n?Zt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&qt(e,t,r),r},xe=(i,e)=>(t,n)=>e(t,n,i);let de=class extends a.RxDisposable{constructor(i,e,t){super(),this._docSkeletonManagerService=i,this._renderManagerService=e,this._commandService=t,this._initialRenderRefresh(),this._commandExecutedListener()}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeletonBefore$.pipe(V.takeUntil(this.dispose$)).subscribe(i=>{if(i==null)return;const{skeleton:e,unitId:t}=i,n=this._renderManagerService.getRenderById(t);if(n==null)return;const{mainComponent:r}=n;r.changeSkeleton(e),this._recalculateSizeBySkeleton(n,e)})}_recalculateSizeBySkeleton(i,e){var u;const{mainComponent:t,scene:n}=i,r=t,o=(u=e.getSkeletonData())==null?void 0:u.pages;if(o==null)return;let s=0,c=0;for(let d=0,g=o.length;d<g;d++){const p=o[d],{pageWidth:m,pageHeight:h}=p;r.pageLayoutType===R.PageLayoutType.VERTICAL?(c+=h,c+=r.pageMarginTop,d===g-1&&(c+=r.pageMarginTop),s=Math.max(s,m)):r.pageLayoutType===R.PageLayoutType.HORIZONTAL&&(s+=m,d!==g-1&&(s+=r.pageMarginLeft),c=Math.max(c,h))}r.resize(s,c),[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(i.unitId)||n.resize(s,c)}_commandExecutedListener(){const i=[_.id],e=[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{var n;if(i.includes(t.id)){const r=t.params,{unitId:o}=r,s=this._docSkeletonManagerService.getSkeletonByUnitId(o);if(s==null)return;const{skeleton:c}=s,l=this._renderManagerService.getRenderById(o);if(l==null)return;if(c.calculate(),e.includes(o)){(n=l.mainComponent)==null||n.makeDirty();return}this._recalculateSizeBySkeleton(l,c)}}))}};de=Jt([a.OnLifecycle(a.LifecycleStages.Rendered,de),xe(0,T.Inject(exports.DocSkeletonManagerService)),xe(1,R.IRenderManagerService),xe(2,a.ICommandService)],de);var Qt=Object.defineProperty,en=Object.getOwnPropertyDescriptor,tn=(i,e,t,n)=>{for(var r=n>1?void 0:n?en(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Qt(e,t,r),r},Y=(i,e)=>(t,n)=>e(t,n,i);let ue=class extends a.Disposable{constructor(e,t,n,r,o){super();v(this,"_liquid",new R.Liquid);v(this,"_pageMarginCache",new Map);this._docSkeletonManagerService=e,this._currentUniverService=t,this._renderManagerService=n,this._commandService=r,this._floatingObjectManagerService=o,this._initialize(),this._commandExecutedListener()}_initialize(){this._initialRenderRefresh(),this._updateOnPluginChange()}_updateOnPluginChange(){this._floatingObjectManagerService.pluginUpdate$.subscribe(e=>{const t=this._docSkeletonManagerService.getCurrent();if(t==null)return;const{unitId:n,skeleton:r}=t,o=this._renderManagerService.getRenderById(n);if(o==null)return;const{mainComponent:s,components:c,scene:l}=o,u=s,{left:d,top:g}=u;e.forEach(p=>{const{unitId:m,subComponentId:h,floatingObjectId:f,floatingObject:S}=p,{left:C=0,top:I=0,width:D=0,height:y=0,angle:M,flipX:b,flipY:U,skewX:E,skewY:P}=S,O=this._pageMarginCache.get(f),w=(O==null?void 0:O.marginLeft)||0,N=(O==null?void 0:O.marginTop)||0;r==null||r.getViewModel().getDataModel().updateDrawing(f,{left:C-d-w,top:I-g-N,height:y,width:D})}),r==null||r.calculate(),s==null||s.makeDirty()})}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeleton$.subscribe(e=>{if(e==null)return;const{skeleton:t,unitId:n}=e,r=this._renderManagerService.getRenderById(n);if(r==null)return;const{mainComponent:o}=r;o.changeSkeleton(t),this._refreshFloatingObject(n,t,r)})}_commandExecutedListener(){const e=[_.id,j.id],t=[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY];this.disposeWithMe(this._commandService.onCommandExecuted(n=>{var r;if(e.includes(n.id)){const o=n.params,{unitId:s}=o,c=this._docSkeletonManagerService.getCurrent();if(c==null)return;const{unitId:l,skeleton:u}=c;if(s!==l)return;const d=this._renderManagerService.getRenderById(l);if(d==null)return;if(t.includes(l)){(r=d.mainComponent)==null||r.makeDirty();return}this._refreshFloatingObject(l,u,d)}}))}_refreshFloatingObject(e,t,n){const r=t==null?void 0:t.getSkeletonData(),{mainComponent:o,scene:s}=n,c=o;if(!r)return;const{left:l,top:u,pageLayoutType:d,pageMarginLeft:g,pageMarginTop:p}=c,{pages:m}=r,h=[];s.getAncestorScale(),this._liquid.reset(),this._pageMarginCache.clear();for(let f=0,S=m.length;f<S;f++){const C=m[f],{skeDrawings:I,marginLeft:D,marginTop:y}=C;this._liquid.translatePagePadding(C),I.forEach(M=>{const{aLeft:b,aTop:U,height:E,width:P,objectId:O}=M;h.push({unitId:e,subComponentId:a.DEFAULT_DOCUMENT_SUB_COMPONENT_ID,floatingObjectId:O,floatingObject:{left:b+l+this._liquid.x,top:U+u+this._liquid.y,width:P,height:E}}),this._pageMarginCache.set(O,{marginLeft:this._liquid.x,marginTop:this._liquid.y})}),this._liquid.translatePage(C,d,g,p)}this._floatingObjectManagerService.BatchAddOrUpdate(h)}};ue=tn([a.OnLifecycle(a.LifecycleStages.Steady,ue),Y(0,T.Inject(exports.DocSkeletonManagerService)),Y(1,a.IUniverInstanceService),Y(2,R.IRenderManagerService),Y(3,a.ICommandService),Y(4,a.IFloatingObjectManagerService)],ue);var nn=Object.defineProperty,rn=Object.getOwnPropertyDescriptor,on=(i,e,t,n)=>{for(var r=n>1?void 0:n?rn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&nn(e,t,r),r},G=(i,e)=>(t,n)=>e(t,n,i);let me=class extends a.Disposable{constructor(e,t,n,r,o,s){super();v(this,"_previousIMEContent","");v(this,"_previousIMERange");v(this,"_onStartSubscription");v(this,"_onUpdateSubscription");v(this,"_onEndSubscription");this._docSkeletonManagerService=e,this._currentUniverService=t,this._renderManagerService=n,this._textSelectionRenderManager=r,this._imeInputManagerService=o,this._commandService=s,this._initialize()}dispose(){var e,t,n;(e=this._onStartSubscription)==null||e.unsubscribe(),(t=this._onUpdateSubscription)==null||t.unsubscribe(),(n=this._onEndSubscription)==null||n.unsubscribe()}_initialize(){this._initialOnCompositionstart(),this._initialOnCompositionUpdate(),this._initialOnCompositionend()}_initialOnCompositionstart(){this._onStartSubscription=this._textSelectionRenderManager.onCompositionstart$.subscribe(e=>{if(e==null)return;const{activeRange:t}=e;t!=null&&(this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(a.Tools.deepClone(t)),this._previousIMERange=t)})}_initialOnCompositionUpdate(){this._onUpdateSubscription=this._textSelectionRenderManager.onCompositionupdate$.subscribe(async e=>{this._updateContent(e,!0)})}_initialOnCompositionend(){this._onEndSubscription=this._textSelectionRenderManager.onCompositionend$.subscribe(e=>{this._updateContent(e,!1)})}async _updateContent(e,t){var h;const n=(h=this._docSkeletonManagerService.getCurrent())==null?void 0:h.skeleton;if(this._previousIMERange==null||e==null||n==null)return;const r=this._currentUniverService.getCurrentUniverDocInstance(),{event:o,activeRange:s}=e,{startOffset:c,segmentId:l,style:u}=this._previousIMERange;if(n==null||s==null)return;const g=o.data;if(g===this._previousIMEContent&&t)return;const p=g.length,m=[{startOffset:c+p,endOffset:c+p,style:u}];await this._commandService.executeCommand($e.id,{unitId:r.getUnitId(),newText:g,oldTextLen:this._previousIMEContent.length,range:this._previousIMERange,textRanges:m,isCompositionEnd:!t,segmentId:l}),n.calculate(),t?(this._previousIMERange.collapsed||(this._previousIMERange.collapsed=!0),this._previousIMEContent=g):this._resetIME()}_resetIME(){this._previousIMEContent="",this._previousIMERange=null,this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(null)}_getDocObject(){return Q(this._currentUniverService,this._renderManagerService)}};me=on([a.OnLifecycle(a.LifecycleStages.Rendered,me),G(0,T.Inject(exports.DocSkeletonManagerService)),G(1,a.IUniverInstanceService),G(2,R.IRenderManagerService),G(3,R.ITextSelectionRenderManager),G(4,T.Inject(Be)),G(5,a.ICommandService)],me);var sn=Object.defineProperty,an=Object.getOwnPropertyDescriptor,cn=(i,e,t,n)=>{for(var r=n>1?void 0:n?an(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&sn(e,t,r),r},Ze=(i,e)=>(t,n)=>e(t,n,i);let ge=class extends a.Disposable{constructor(i,e){super(),this._textSelectionManagerService=i,this._commandService=e,this._commandExecutedListener()}_commandExecutedListener(){const i=[W.id,ee.id,te.id,ne.id,A.id,k.id,H.id,K.id,re.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{i.includes(e.id)&&this._handleInlineFormat(e)}))}_handleInlineFormat(i){const{segmentId:e}=this._textSelectionManagerService.getActiveRange()??{};e!=null&&this._commandService.executeCommand(Fe.id,{segmentId:e,preCommandId:i.id,...i.params??{}})}};ge=cn([a.OnLifecycle(a.LifecycleStages.Rendered,ge),Ze(0,T.Inject(exports.TextSelectionManagerService)),Ze(1,a.ICommandService)],ge);var ln=Object.defineProperty,dn=Object.getOwnPropertyDescriptor,un=(i,e,t,n)=>{for(var r=n>1?void 0:n?dn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&ln(e,t,r),r},X=(i,e)=>(t,n)=>e(t,n,i);let pe=class extends a.Disposable{constructor(e,t,n,r,o){super();v(this,"_onInputSubscription");this._docSkeletonManagerService=e,this._currentUniverService=t,this._renderManagerService=n,this._textSelectionManagerService=r,this._commandService=o,this._initialize(),this._commandExecutedListener()}dispose(){var e;(e=this._onInputSubscription)==null||e.unsubscribe()}_initialize(){}_commandExecutedListener(){const e=[B.id,$.id];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{if(!e.includes(t.id))return;const n=t.params;switch(t.id){case B.id:return this._handleMoveCursor(n.direction);case $.id:return this._handleShiftMoveSelection(n.direction);default:throw new Error("Unknown command")}}))}_handleShiftMoveSelection(e){var f;const t=this._textSelectionManagerService.getActiveRange(),n=this._textSelectionManagerService.getSelections(),r=this._currentUniverService.getCurrentUniverDocInstance(),o=(f=this._docSkeletonManagerService.getCurrent())==null?void 0:f.skeleton,s=this._getDocObject();if(t==null||o==null||s==null)return;const{startOffset:c,endOffset:l,style:u,collapsed:d,direction:g}=t;if(n.length>1){let S=1/0,C=-1/0;for(const I of n)S=Math.min(S,I.startOffset),C=Math.max(C,I.endOffset);this._textSelectionManagerService.replaceTextRanges([{startOffset:e===a.Direction.LEFT||e===a.Direction.UP?C:S,endOffset:e===a.Direction.LEFT||e===a.Direction.UP?S:C,style:u}]);return}const p=d||g===R.RANGE_DIRECTION.FORWARD?c:l;let m=d||g===R.RANGE_DIRECTION.FORWARD?l:c;const h=r.getBody().dataStream.length??1/0;if(e===a.Direction.LEFT||e===a.Direction.RIGHT){const S=o.findNodeByCharIndex(m-1),C=o.findNodeByCharIndex(m);m=e===a.Direction.RIGHT?m+C.count:m-((S==null?void 0:S.count)??0),m=Math.min(h-2,Math.max(0,m)),this._textSelectionManagerService.replaceTextRanges([{startOffset:p,endOffset:m,style:u}])}else{const S=o.findNodeByCharIndex(m),C=s.document.getOffsetConfig(),I=this._getTopOrBottomPosition(o,S,e===a.Direction.DOWN);if(I==null){const y=e===a.Direction.UP?0:h-2;if(y===m)return;this._textSelectionManagerService.replaceTextRanges([{startOffset:p,endOffset:y,style:u}]);return}const D=new R.NodePositionConvertToCursor(C,o).getRangePointData(I,I).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{startOffset:p,endOffset:D.endOffset,style:u}])}}_handleMoveCursor(e){var p;const t=this._textSelectionManagerService.getActiveRange(),n=this._textSelectionManagerService.getSelections(),r=this._currentUniverService.getCurrentUniverDocInstance(),o=(p=this._docSkeletonManagerService.getCurrent())==null?void 0:p.skeleton,s=this._getDocObject();if(t==null||o==null||s==null||n==null)return;const{startOffset:c,endOffset:l,style:u,collapsed:d}=t,g=r.getBody().dataStream.length??1/0;if(e===a.Direction.LEFT||e===a.Direction.RIGHT){let m;if(!t.collapsed||n.length>1){let h=1/0,f=-1/0;for(const S of n)h=Math.min(h,S.startOffset),f=Math.max(f,S.endOffset);m=e===a.Direction.LEFT?h:f}else{const h=o.findNodeByCharIndex(c-1),f=o.findNodeByCharIndex(c);e===a.Direction.LEFT?m=Math.max(0,c-((h==null?void 0:h.count)??0)):m=Math.min(g-2,l+f.count)}this._textSelectionManagerService.replaceTextRanges([{startOffset:m,endOffset:m,style:u}])}else{const m=o.findNodeByCharIndex(c),h=o.findNodeByCharIndex(l),f=s.document.getOffsetConfig(),S=this._getTopOrBottomPosition(o,e===a.Direction.UP?m:h,e===a.Direction.DOWN);if(S==null){let I;d?I=e===a.Direction.UP?0:g-2:I=e===a.Direction.UP?c:l,this._textSelectionManagerService.replaceTextRanges([{startOffset:I,endOffset:I,style:u}]);return}const C=new R.NodePositionConvertToCursor(f,o).getRangePointData(S,S).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{...C,style:u}])}}_getTopOrBottomPosition(e,t,n){if(t==null)return;const r=this._getSpanLeftOffsetInLine(t),o=this._getNextOrPrevLine(t,n);if(o==null)return;const s=this._matchPositionByLeftOffset(e,o,r);if(s!=null)return{...s,isBack:!0}}_getSpanLeftOffsetInLine(e){const t=e.parent;if(t==null)return-1/0;const n=t.left,{left:r}=e;return n+r}_matchPositionByLeftOffset(e,t,n){const r={distance:1/0};for(const o of t.divides){const s=o.left;for(const c of o.spanGroup){const{left:l}=c,u=s+l,d=Math.abs(n-u);d<r.distance&&(r.span=c,r.distance=d)}}if(r.span!=null)return e.findPositionBySpan(r.span)}_getNextOrPrevLine(e,t){var h,f,S,C,I,D,y,M,b,U,E,P;const n=e.parent;if(n==null)return;const r=n.parent;if(r==null)return;const o=r.parent;if(o==null)return;const s=o.lines.indexOf(r);if(s===-1)return;let c;if(t===!0?c=o.lines[s+1]:c=o.lines[s-1],c!=null)return c;const l=o.parent;if(l==null)return;const u=l.columns.indexOf(o);if(u===-1)return;if(t===!0)c=(h=l.columns[u+1])==null?void 0:h.lines[0];else{const O=(S=(f=l.columns)==null?void 0:f[u-1])==null?void 0:S.lines;c=O==null?void 0:O[O.length-1]}if(c!=null)return c;const d=l.parent;if(d==null)return;const g=d.sections.indexOf(l);if(g===-1)return;if(t===!0)c=(I=(C=d.sections[g-1])==null?void 0:C.columns[0])==null?void 0:I.lines[0];else{const O=(y=(D=d.sections)==null?void 0:D[g-1])==null?void 0:y.columns,w=O==null?void 0:O[O.length-1],N=w==null?void 0:w.lines;c=N==null?void 0:N[N.length-1]}if(c!=null)return c;const p=d.parent;if(p==null)return;const m=p.pages.indexOf(d);if(m!==-1){if(t===!0)c=(U=(b=(M=p.pages[m+1])==null?void 0:M.sections[0])==null?void 0:b.columns[0])==null?void 0:U.lines[0];else{const O=(E=p.pages[m-1])==null?void 0:E.sections;if(O==null)return;const w=(P=O[O.length-1])==null?void 0:P.columns,N=w[w.length-1],ke=N==null?void 0:N.lines;c=ke[ke.length-1]}if(c!=null)return c}}_getDocObject(){return Q(this._currentUniverService,this._renderManagerService)}};pe=un([a.OnLifecycle(a.LifecycleStages.Rendered,pe),X(0,T.Inject(exports.DocSkeletonManagerService)),X(1,a.IUniverInstanceService),X(2,R.IRenderManagerService),X(3,T.Inject(exports.TextSelectionManagerService)),X(4,a.ICommandService)],pe);var mn=Object.defineProperty,gn=Object.getOwnPropertyDescriptor,pn=(i,e,t,n)=>{for(var r=n>1?void 0:n?gn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&mn(e,t,r),r},q=(i,e)=>(t,n)=>e(t,n,i);let he=class extends a.Disposable{constructor(e,t,n,r,o){super();v(this,"_onInputSubscription");this._docSkeletonManagerService=e,this._currentUniverService=t,this._renderManagerService=n,this._textSelectionRenderManager=r,this._commandService=o,this._initialize(),this._commandExecutedListener()}dispose(){var e;(e=this._onInputSubscription)==null||e.unsubscribe()}_initialize(){this._initialNormalInput()}_initialNormalInput(){this._onInputSubscription=this._textSelectionRenderManager.onInput$.subscribe(async e=>{var h;if(e==null)return;const n=this._currentUniverService.getCurrentUniverDocInstance().getUnitId(),{event:r,content:o="",activeRange:s}=e,c=r,l=(h=this._docSkeletonManagerService.getCurrent())==null?void 0:h.skeleton;if(c.data==null||l==null||!l||!s)return;const{startOffset:u,segmentId:d,style:g}=s,p=o.length,m=[{startOffset:u+p,endOffset:u+p,style:g}];await this._commandService.executeCommand(Oe.id,{unitId:n,body:{dataStream:o},range:s,textRanges:m,segmentId:d})})}_commandExecutedListener(){}_getDocObject(){return Q(this._currentUniverService,this._renderManagerService)}};he=pn([a.OnLifecycle(a.LifecycleStages.Rendered,he),q(0,T.Inject(exports.DocSkeletonManagerService)),q(1,a.IUniverInstanceService),q(2,R.IRenderManagerService),q(3,R.ITextSelectionRenderManager),q(4,a.ICommandService)],he);var hn=Object.defineProperty,fn=Object.getOwnPropertyDescriptor,Sn=(i,e,t,n)=>{for(var r=n>1?void 0:n?fn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&hn(e,t,r),r},Je=(i,e)=>(t,n)=>e(t,n,i);const _n="rgba(198, 198, 198, 1)",vn="rgba(255, 255, 255, 1)";let fe=class extends a.Disposable{constructor(i,e){super(),this._renderManagerService=i,this._currentUniverService=e,this._initialize(),this._commandExecutedListener()}_initialize(){this._initialRenderRefresh()}_initialRenderRefresh(){this._renderManagerService.currentRender$.subscribe(i=>{var o;if(i==null||this._currentUniverService.getUniverDocInstance(i)==null)return;const e=this._renderManagerService.getRenderById(i);if(e==null)return;const{mainComponent:t}=e,n=t,r=(o=n.getSkeleton())==null?void 0:o.getPageSize();n.onPageRenderObservable.add(s=>{if([a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(i))return;const{page:c,pageLeft:l,pageTop:u,ctx:d}=s,{width:g,pageWidth:p,height:m,pageHeight:h}=c;d.save(),d.translate(l-.5,u-.5),R.Rect.drawWith(d,{width:(r==null?void 0:r.width)??p??g,height:(r==null?void 0:r.height)??h??m,strokeWidth:1,stroke:_n,fill:vn,zIndex:3}),d.restore()})})}_commandExecutedListener(){}};fe=Sn([a.OnLifecycle(a.LifecycleStages.Rendered,fe),Je(0,R.IRenderManagerService),Je(1,T.Inject(a.IUniverInstanceService))],fe);var Cn=Object.defineProperty,In=Object.getOwnPropertyDescriptor,Mn=(i,e,t,n)=>{for(var r=n>1?void 0:n?In(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Cn(e,t,r),r},F=(i,e)=>(t,n)=>e(t,n,i);let Se=class extends a.Disposable{constructor(e,t,n,r,o,s,c){super();v(this,"_moveInObserver");v(this,"_moveOutObserver");v(this,"_downObserver");v(this,"_dblClickObserver");v(this,"_tripleClickObserver");v(this,"_loadedMap",new Set);this._docSkeletonManagerService=e,this._currentUniverService=t,this._commandService=n,this._renderManagerService=r,this._textSelectionRenderManager=o,this._textSelectionManagerService=s,this._layoutService=c,this._renderManagerService.currentRender$.subscribe(l=>{l!=null&&this._currentUniverService.getUniverDocInstance(l)!=null&&(this._loadedMap.has(l)||(this._initialMain(l),this._loadedMap.add(l)))}),this._initialize()}_initialize(){this._skeletonListener(),this._commandExecutedListener(),this._layoutService&&this.disposeWithMe(this._layoutService.registerContainer(this._textSelectionRenderManager.__getEditorContainer()))}dispose(){this._renderManagerService.getRenderAll().forEach(e=>{const{mainComponent:t}=e;t!=null&&(t.onPointerEnterObserver.remove(this._moveInObserver),t.onPointerLeaveObserver.remove(this._moveOutObserver),t.onPointerDownObserver.remove(this._downObserver),t.onDblclickObserver.remove(this._dblClickObserver),t.onTripleClickObserver.remove(this._tripleClickObserver))})}_initialMain(e){const t=this._getDocObjectById(e);if(t==null)return;const{document:n,scene:r}=t;this._moveInObserver=n.onPointerEnterObserver.add(()=>{n.cursor=R.CURSOR_TYPE.TEXT}),this._moveOutObserver=n.onPointerLeaveObserver.add(()=>{n.cursor=R.CURSOR_TYPE.DEFAULT,r.resetCursor()}),this._downObserver=n==null?void 0:n.onPointerDownObserver.add((o,s)=>{this._currentUniverService.getCurrentUniverDocInstance().getUnitId()!==e&&this._currentUniverService.setCurrentUniverDocInstance(e),this._textSelectionRenderManager.eventTrigger(o),o.button!==2&&s.stopPropagation()}),this._dblClickObserver=n==null?void 0:n.onDblclickObserver.add(o=>{this._textSelectionRenderManager.handleDblClick(o)}),this._tripleClickObserver=n==null?void 0:n.onTripleClickObserver.add(o=>{this._textSelectionRenderManager.handleTripleClick(o)})}_commandExecutedListener(){const e=[j.id];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{var n;if(e.includes(t.id)){const r=t.params,{unitId:o}=r,s=(n=this._textSelectionManagerService.getCurrentSelection())==null?void 0:n.unitId;if(o!==s)return;this._textSelectionManagerService.refreshSelection()}}))}_skeletonListener(){this._docSkeletonManagerService.currentSkeleton$.subscribe(e=>{if(e==null)return;const{unitId:t,skeleton:n}=e,r=this._renderManagerService.getRenderById(t);if(r==null)return;const{scene:o,mainComponent:s}=r;this._textSelectionRenderManager.changeRuntime(n,o,s),this._textSelectionManagerService.setCurrentSelectionNotRefresh({unitId:t,subUnitId:""})})}_getDocObjectById(e){return gt(e,this._renderManagerService)}};Se=Mn([a.OnLifecycle(a.LifecycleStages.Rendered,Se),F(0,T.Inject(exports.DocSkeletonManagerService)),F(1,a.IUniverInstanceService),F(2,a.ICommandService),F(3,R.IRenderManagerService),F(4,R.ITextSelectionRenderManager),F(5,T.Inject(exports.TextSelectionManagerService)),F(6,T.Optional(x.LayoutService))],Se);var Rn=Object.defineProperty,On=Object.getOwnPropertyDescriptor,yn=(i,e,t,n)=>{for(var r=n>1?void 0:n?On(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Rn(e,t,r),r},Z=(i,e)=>(t,n)=>e(t,n,i);let _e=class extends a.Disposable{constructor(e,t,n,r,o){super();v(this,"_initializedRender",new Set);this._docSkeletonManagerService=e,this._currentUniverService=t,this._commandService=n,this._renderManagerService=r,this._textSelectionManagerService=o,this._initialize()}dispose(){super.dispose()}_initialize(){this._skeletonListener(),this._commandExecutedListener(),this._initialRenderRefresh()}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeleton$.subscribe(e=>{if(e==null)return;const{unitId:t}=e,n=this._renderManagerService.getRenderById(t);if(n==null||this._initializedRender.has(t)||[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(t))return;this._initializedRender.add(t);const{scene:r}=n;this.disposeWithMe(a.toDisposable(r.onMouseWheelObserver.add(o=>{if(!o.ctrlKey)return;const s=Math.abs(o.deltaX);let c=s<40?.2:s<80?.4:.2;c*=o.deltaY>0?-1:1,r.scaleX<1&&(c/=2);const l=this._currentUniverService.getCurrentUniverDocInstance(),u=l.zoomRatio;let d=+parseFloat(`${u+c}`).toFixed(1);d=d>=4?4:d<=.1?.1:d,this._commandService.executeCommand(ct.id,{zoomRatio:d,unitId:l.getUnitId()}),o.preventDefault()})))})}_skeletonListener(){this.disposeWithMe(a.toDisposable(this._docSkeletonManagerService.currentSkeletonBefore$.subscribe(e=>{if(e==null)return;const n=this._currentUniverService.getCurrentUniverDocInstance().zoomRatio||1;this._updateViewZoom(n,!1)})))}_commandExecutedListener(){const e=[j.id];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{if(e.includes(t.id)){const n=this._currentUniverService.getCurrentUniverDocInstance(),r=t.params,{unitId:o}=r;if(o!==n.getUnitId())return;const s=n.zoomRatio||1;this._updateViewZoom(s)}}))}_updateViewZoom(e,t=!0){var r;const n=this._getDocObject();n!=null&&(n.scene.scale(e,e),this._calculatePagePosition(n,e),t&&this._textSelectionManagerService.refreshSelection(),(r=n.scene.getTransformer())==null||r.hideControl())}_calculatePagePosition(e,t){const{document:n,scene:r}=e,o=r==null?void 0:r.getParent(),{width:s,height:c,pageMarginLeft:l,pageMarginTop:u}=n;if(o==null||s===1/0||c===1/0)return;const{width:d,height:g}=o;let p=0,m=0,h=0,f=0,S=1/0;d>(s+l*2)*t?(p=d/2-s*t/2,p/=t,h=(d-l*2)/t,S=0):(p=l,h=s+l*2,S=(h-d/t)/2),g>c?(m=g/2-c/2,f=(g-u*2)/t):(m=u,f=c+u*2),r.resize(h,f+200),n.translate(p,m);const C=r.getViewport(Me.VIEW_MAIN);if(S!==1/0&&C!=null){const I=C.getBarScroll(S,0).x;C.scrollTo({x:I})}return this}_getDocObject(){return Q(this._currentUniverService,this._renderManagerService)}};_e=yn([a.OnLifecycle(a.LifecycleStages.Rendered,_e),Z(0,T.Inject(exports.DocSkeletonManagerService)),Z(1,a.IUniverInstanceService),Z(2,a.ICommandService),Z(3,R.IRenderManagerService),Z(4,T.Inject(exports.TextSelectionManagerService))],_e);const xn={id:Ne.id,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),binding:x.KeyCode.ENTER},Dn={id:we.id,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),binding:x.KeyCode.BACKSPACE},Tn={id:Le.id,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),binding:x.KeyCode.DELETE},bn={id:B.id,binding:x.KeyCode.ARROW_UP,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.UP}},Un={id:B.id,binding:x.KeyCode.ARROW_DOWN,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.DOWN}},En={id:B.id,binding:x.KeyCode.ARROW_LEFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.LEFT}},Pn={id:B.id,binding:x.KeyCode.ARROW_RIGHT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.RIGHT}},Nn={id:$.id,binding:x.KeyCode.ARROW_UP|x.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.UP}},wn={id:$.id,binding:x.KeyCode.ARROW_DOWN|x.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.DOWN}},Ln={id:$.id,binding:x.KeyCode.ARROW_LEFT|x.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.LEFT}},An={id:$.id,binding:x.KeyCode.ARROW_RIGHT|x.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.RIGHT}},Bn={id:lt.id,binding:x.KeyCode.A|x.MetaKeys.CTRL_COMMAND,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.FOCUSING_EDITOR)};var $n=Object.defineProperty,Fn=Object.getOwnPropertyDescriptor,jn=(i,e,t,n)=>{for(var r=n>1?void 0:n?Fn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&$n(e,t,r),r},ie=(i,e)=>(t,n)=>e(t,n,i);exports.DocCanvasView=class extends a.RxDisposable{constructor(t,n,r,o){super();v(this,"_scene");v(this,"_currentDocumentModel");v(this,"_loadedMap",new Set);v(this,"_fps$",new V.BehaviorSubject(""));v(this,"fps$",this._fps$.asObservable());this._renderManagerService=t,this._configService=n,this._currentUniverService=r,this._docViewModelManagerService=o,this._initialize()}_initialize(){this._currentUniverService.currentDoc$.subscribe(t=>{if(t==null)return;this._currentDocumentModel=t;const n=t.getUnitId();this._docViewModelManagerService.setCurrent(n),this._loadedMap.has(n)||(this._addNewRender(),this._loadedMap.add(n))})}dispose(){this._fps$.complete()}_addNewRender(){const t=this._currentDocumentModel,n=t.getUnitId(),r=t.getContainer(),o=t.getParentRenderUnitId();if(r!=null&&o!=null)throw new Error("container or parentRenderUnitId can only exist one");r==null&&o!=null?this._renderManagerService.createRenderWithParent(n,o):this._renderManagerService.createRender(n);const s=this._renderManagerService.getRenderById(n);if(s==null)return;const{scene:c,engine:l}=s;c.openTransformer(),this._scene=c;const u=new R.Viewport(Me.VIEW_MAIN,c,{left:0,top:0,bottom:0,right:0,isWheelPreventDefaultX:!0});c.addViewport(u).attachControl(),c.on(R.EVENT_TYPE.wheel,(p,m)=>{const h=p;if(h.ctrlKey){const f=Math.abs(h.deltaX);let S=f<40?.2:f<80?.4:.2;S*=h.deltaY>0?-1:1,c.scaleX<1&&(S/=2),c.scaleX+S>4?c.scale(4,4):c.scaleX+S<.1?c.scale(.1,.1):h.preventDefault()}else u.onMouseWheel(h,m)}),this._configService.getConfig("hasScroll")!==!1&&n!==a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY&&new R.ScrollBar(u),c.addLayer(new R.Layer(c,[],Te),new R.Layer(c,[],Qe)),this._addComponent(s),this._currentDocumentModel.getShouldRenderLoopImmediately()&&l.runRenderLoop(()=>{c.render(),this._fps$.next(Math.round(l.getFps()).toString())}),this._renderManagerService.setCurrent(n)}_addComponent(t){const n=this._scene,r=this._currentDocumentModel,o=new R.Documents(ve.MAIN,void 0,{pageMarginLeft:r.documentStyle.marginLeft||0,pageMarginTop:r.documentStyle.marginTop||0});o.zIndex=et,t.mainComponent=o,t.components.set(ve.MAIN,o),n.addObjects([o],Te)}};exports.DocCanvasView=jn([a.OnLifecycle(a.LifecycleStages.Ready,exports.DocCanvasView),ie(0,R.IRenderManagerService),ie(1,a.IConfigService),ie(2,a.IUniverInstanceService),ie(3,T.Inject(exports.DocViewModelManagerService))],exports.DocCanvasView);var Vn=Object.defineProperty,kn=Object.getOwnPropertyDescriptor,zn=(i,e,t,n)=>{for(var r=n>1?void 0:n?kn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Vn(e,t,r),r},oe=(i,e)=>(t,n)=>e(t,n,i);const Gn={hasScroll:!0},Wn="docs";var De;exports.UniverDocsPlugin=(De=class extends a.Plugin{constructor(t={},n,r,o,s){super(Wn);v(this,"_config");this._injector=n,this._localeService=r,this._configService=o,this._currentUniverService=s,this._config=Object.assign(Gn,t),this._initializeDependencies(n),this._initializeCommands()}initialize(){}_initializeCommands(){[B,$,we,Le,W,ee,te,ne,A,k,H,K,re,Fe,Ne,Oe,ye,Pe,$e,Ae,_,ot,st,ct,j,Ee,lt,it,rt,je].forEach(t=>{this._injector.get(a.ICommandService).registerCommand(t)}),[bn,Un,Pn,En,Nn,wn,Ln,An,Bn,Dn,Tn,xn].forEach(t=>{this._injector.get(x.IShortcutService).registerShortcut(t)})}onReady(){this.initialize()}_initializeDependencies(t){[[exports.DocCanvasView],[exports.DocSkeletonManagerService],[exports.DocViewModelManagerService],[Be],[dt,{useClass:Ue}],[R.ITextSelectionRenderManager,{useClass:R.TextSelectionRenderManager}],[exports.TextSelectionManagerService],[de],[fe],[Se],[he],[me],[ge],[le],[pe],[_e],[ue]].forEach(n=>t.add(n))}},v(De,"type",a.PluginType.Doc),De);exports.UniverDocsPlugin=zn([oe(1,T.Inject(T.Injector)),oe(2,T.Inject(a.LocaleService)),oe(3,a.IConfigService),oe(4,a.IUniverInstanceService)],exports.UniverDocsPlugin);exports.BreakLineCommand=Ne;exports.BulletListCommand=rt;exports.CoverContentCommand=st;exports.DOCS_COMPONENT_DEFAULT_Z_INDEX=et;exports.DOCS_COMPONENT_HEADER_LAYER_INDEX=Qe;exports.DOCS_COMPONENT_MAIN_LAYER_INDEX=Te;exports.DOCS_VIEW_KEY=ve;exports.DeleteCommand=ye;exports.DeleteLeftCommand=we;exports.DeleteRightCommand=Le;exports.DocCopyCommand=se;exports.DocCutCommand=ae;exports.DocPasteCommand=ce;exports.IMEInputCommand=$e;exports.InsertCommand=Oe;exports.MoveCursorOperation=B;exports.MoveSelectionOperation=$;exports.NORMAL_TEXT_SELECTION_PLUGIN_NAME=pt;exports.OrderListCommand=it;exports.ReplaceContentCommand=ot;exports.RichTextEditingMutation=_;exports.SetInlineFormatBoldCommand=W;exports.SetInlineFormatCommand=Fe;exports.SetInlineFormatFontFamilyCommand=K;exports.SetInlineFormatFontSizeCommand=H;exports.SetInlineFormatItalicCommand=ee;exports.SetInlineFormatStrikethroughCommand=ne;exports.SetInlineFormatSubscriptCommand=A;exports.SetInlineFormatSuperscriptCommand=k;exports.SetInlineFormatTextColorCommand=re;exports.SetInlineFormatUnderlineCommand=te;exports.SetTextSelectionsOperation=Ee;exports.UpdateCommand=Pe;exports.VIEWPORT_KEY=Me;exports.getDocObject=Q;
1
+ "use strict";var ut=Object.defineProperty;var mt=(i,t,e)=>t in i?ut(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e;var v=(i,t,e)=>(mt(i,typeof t!="symbol"?t+"":t,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@univerjs/core"),M=require("@univerjs/engine-render"),L=require("rxjs"),D=require("@univerjs/ui"),E=require("@wendellhu/redi");function Q(i,t){const n=i.getCurrentUniverDocInstance().getUnitId(),r=t.getRenderById(n);if(r==null)return;const{mainComponent:o,scene:s,engine:c}=r;return{document:o,scene:s,engine:c}}function gt(i,t){const e=t.getRenderById(i);if(e==null)return;const{mainComponent:n,scene:r,engine:o}=e;return{document:n,scene:r,engine:o}}var ve=(i=>(i.MAIN="__Document_Render_Main__",i))(ve||{}),Me=(i=>(i.VIEW_MAIN="viewMain",i.VIEW_TOP="viewTop",i.VIEW_LEFT="viewLeft",i.VIEW_LEFT_TOP="viewLeftTop",i))(Me||{});const De=0,Qe=2,et=10,pt="normalTextSelectionPluginName",Ue={id:"doc.operation.set-selections",type:a.CommandType.OPERATION,handler:(i,t)=>!0};var ht=Object.defineProperty,ft=Object.getOwnPropertyDescriptor,St=(i,t,e,n)=>{for(var r=n>1?void 0:n?ft(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&ht(t,e,r),r},ze=(i,t)=>(e,n)=>t(e,n,i);function We(i){const{startOffset:t,endOffset:e,collapsed:n}=i;return{startOffset:t,endOffset:e,collapsed:n}}exports.TextSelectionManagerService=class extends a.RxDisposable{constructor(e,n){super();v(this,"_currentSelection",null);v(this,"_textSelectionInfo",new Map);v(this,"_textSelection$",new L.BehaviorSubject(null));v(this,"textSelection$",this._textSelection$.asObservable());this._textSelectionRenderManager=e,this._commandService=n,this._syncSelectionFromRenderService()}getCurrentSelection(){return this._currentSelection}getCurrentSelectionInfo(){return this._getTextRanges(this._currentSelection)}dispose(){this._textSelection$.complete()}refreshSelection(){this._currentSelection!=null&&this._refresh(this._currentSelection)}setCurrentSelection(e){this._currentSelection=e,this._refresh(e)}setCurrentSelectionNotRefresh(e){this._currentSelection=e}getSelections(){var e;return(e=this._getTextRanges(this._currentSelection))==null?void 0:e.textRanges}getActiveRange(){const e=this._getTextRanges(this._currentSelection);if(e==null)return;const{textRanges:n,segmentId:r,style:o}=e,s=n.find(m=>m.isActive());if(s==null)return null;const{startOffset:c,endOffset:l,collapsed:u,startNodePosition:d,endNodePosition:p,direction:h}=s;return c==null||l==null?null:{startOffset:c,endOffset:l,collapsed:u,startNodePosition:d,endNodePosition:p,direction:h,segmentId:r,style:o}}add(e){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:e,segmentId:"",style:M.NORMAL_TEXT_SELECTION_PLUGIN_STYLE})}replaceTextRanges(e){this._currentSelection!=null&&(this._textSelectionRenderManager.removeAllTextRanges(),this._textSelectionRenderManager.addTextRanges(e))}_syncSelectionFromRenderService(){this._textSelectionRenderManager.textSelectionInner$.pipe(L.takeUntil(this.dispose$)).subscribe(e=>{e!=null&&this._replaceTextRangesWithNoRefresh(e)})}_replaceTextRangesWithNoRefresh(e){if(this._currentSelection==null)return;const n={...this._currentSelection,...e};this._replaceByParam(n),this._textSelection$.next(n);const{unitId:r,subUnitId:o,segmentId:s,style:c,textRanges:l}=n;this._commandService.executeCommand(Ue.id,{unitId:r,subUnitId:o,segmentId:s,style:c,ranges:l.map(We)})}_getTextRanges(e){var o;if(e==null)return;const{unitId:n,subUnitId:r=""}=e;return(o=this._textSelectionInfo.get(n))==null?void 0:o.get(r)}_refresh(e){const n=this._getTextRanges(e);this._textSelectionRenderManager.removeAllTextRanges(),n&&Array.isArray(n.textRanges)&&n.textRanges.length&&this._textSelectionRenderManager.addTextRanges(n.textRanges.map(We))}_replaceByParam(e){const{unitId:n,subUnitId:r,style:o,segmentId:s,textRanges:c}=e;this._textSelectionInfo.has(n)||this._textSelectionInfo.set(n,new Map),this._textSelectionInfo.get(n).set(r,{textRanges:c,style:o,segmentId:s})}_addByParam(e){const{unitId:n,subUnitId:r,textRanges:o,style:s,segmentId:c}=e;this._textSelectionInfo.has(n)||this._textSelectionInfo.set(n,new Map);const l=this._textSelectionInfo.get(n);l.has(r)?l.get(r).textRanges.push(...o):l.set(r,{textRanges:o,style:s,segmentId:c})}};exports.TextSelectionManagerService=St([ze(0,M.ITextSelectionRenderManager),ze(1,a.ICommandService)],exports.TextSelectionManagerService);function Oe(i,t="",e=0){const{startOffset:n,endOffset:r}=i,o=[],s=n-e,c=r-e;return s>0&&o.push({t:"r",len:s,segmentId:t}),o.push({t:"d",len:c-s,line:0,segmentId:t}),o}var _t=Object.defineProperty,vt=Object.getOwnPropertyDescriptor,It=(i,t,e,n)=>{for(var r=n>1?void 0:n?vt(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&_t(t,e,r),r},Ct=(i,t)=>(e,n)=>t(e,n,i);exports.DocViewModelManagerService=class extends a.RxDisposable{constructor(e){super();v(this,"_currentViewModelUnitId","");v(this,"_docViewModelMap",new Map);v(this,"_currentDocViewModel$",new L.BehaviorSubject(null));v(this,"currentDocViewModel$",this._currentDocViewModel$.asObservable());this._currentUniverService=e,this._initialize()}_initialize(){this._currentUniverService.currentDoc$.pipe(L.takeUntil(this.dispose$)).subscribe(e=>{if(e==null)return;const n=e.getUnitId();this._setCurrent(n)})}dispose(){this._currentDocViewModel$.complete(),this._docViewModelMap.clear()}getCurrent(){return this._docViewModelMap.get(this._currentViewModelUnitId)}getViewModel(e){var n;return(n=this._docViewModelMap.get(e))==null?void 0:n.docViewModel}_setCurrent(e){var r;const n=this._currentUniverService.getUniverDocInstance(e);if(n==null)throw new Error(`Document data model with id ${e} not found when build view model.`);if(n.getBody()!=null){if(!this._docViewModelMap.has(e)){const o=this._buildDocViewModel(n);this._docViewModelMap.set(e,{unitId:e,docViewModel:o})}if(e===a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY){const o=(r=this._docViewModelMap.get(e))==null?void 0:r.docViewModel;if(o==null)return;o.reset(n)}this._currentViewModelUnitId=e,this._currentDocViewModel$.next(this.getCurrent())}}_buildDocViewModel(e){return new M.DocumentViewModel(e)}};exports.DocViewModelManagerService=It([Ct(0,a.IUniverInstanceService)],exports.DocViewModelManagerService);const _={id:"doc.mutation.rich-text-editing",type:a.CommandType.MUTATION,handler:(i,t)=>{const{unitId:e,mutations:n}=t,o=i.get(a.IUniverInstanceService).getUniverDocInstance(e),c=i.get(exports.DocViewModelManagerService).getViewModel(e);if(o==null||c==null)throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${e}`);if(n.length===0)throw new Error("Mutation's length should great than 0 when call RichTextEditingMutation");const l=o.apply(n),{segmentId:u}=n[0],d=o.getSelfOrHeaderFooterModel(u);return c.getSelfOrHeaderFooterViewModel(u).reset(d),{unitId:e,mutations:l}}},Re={id:"doc.command.insert-text",type:a.CommandType.COMMAND,handler:async(i,t)=>{const e=i.get(a.IUndoRedoService),n=i.get(a.ICommandService),r=i.get(exports.TextSelectionManagerService),{range:o,segmentId:s,body:c,unitId:l,textRanges:u}=t,{startOffset:d,collapsed:p}=o,h={id:_.id,params:{unitId:l,mutations:[]}},m=new a.TextX;p?d>0&&m.push({t:"r",len:d,segmentId:s}):m.push(...Oe(o,s)),m.push({t:"i",body:c,len:c.dataStream.length,line:0,segmentId:s}),h.params.mutations=m.serialize();const g=n.syncExecuteCommand(h.id,h.params);return r.replaceTextRanges(u),g?(e.pushUndoRedo({unitID:l,undoMutations:[{id:_.id,params:g}],redoMutations:[{id:_.id,params:h.params}],undo(){return n.syncExecuteCommand(_.id,g),r.replaceTextRanges([o]),!0},redo(){return n.syncExecuteCommand(_.id,h.params),r.replaceTextRanges(u),!0}}),!0):!1}};var A=(i=>(i[i.LEFT=0]="LEFT",i[i.RIGHT=1]="RIGHT",i))(A||{});const ye={id:"doc.command.delete-text",type:a.CommandType.COMMAND,handler:async(i,t)=>{const e=i.get(a.ICommandService),n=i.get(a.IUndoRedoService),r=i.get(exports.TextSelectionManagerService),{range:o,segmentId:s,unitId:c,direction:l,textRanges:u,len:d=1}=t,{startOffset:p}=o,h={id:_.id,params:{unitId:c,mutations:[]}},m=new a.TextX;p>0&&m.push({t:"r",len:l===0?p-d:p,segmentId:s}),m.push({t:"d",len:d,line:0,segmentId:s}),h.params.mutations=m.serialize();const g=e.syncExecuteCommand(h.id,h.params);return r.replaceTextRanges(u),g&&n.pushUndoRedo({unitID:c,undoMutations:[{id:_.id,params:g}],redoMutations:[{id:_.id,params:h.params}],undo(){return e.syncExecuteCommand(_.id,g),r.replaceTextRanges([o]),!0},redo(){return e.syncExecuteCommand(_.id,h.params),r.replaceTextRanges(u),!0}}),!1}},Pe={id:"doc.command.update-text",type:a.CommandType.COMMAND,handler:async(i,t)=>{const{range:e,segmentId:n,updateBody:r,coverType:o,unitId:s,textRanges:c}=t,l=i.get(a.ICommandService),u=i.get(a.IUndoRedoService),d=i.get(exports.TextSelectionManagerService),p={id:_.id,params:{unitId:s,mutations:[]}},h=new a.TextX,{startOffset:m,endOffset:g}=e;h.push({t:"r",len:m,segmentId:n}),h.push({t:"r",body:r,len:g-m,segmentId:n,coverType:o}),p.params.mutations=h.serialize();const f=l.syncExecuteCommand(p.id,p.params);return d.replaceTextRanges(c),f?(u.pushUndoRedo({unitID:s,undoMutations:[{id:_.id,params:f}],redoMutations:[{id:_.id,params:p.params}],undo(){return l.syncExecuteCommand(_.id,f),d.replaceTextRanges(c),!0},redo(){return l.syncExecuteCommand(_.id,p.params),d.replaceTextRanges(c),!0}}),!0):!1}};function Mt(i,t){const e=[];for(let n=0,r=i.length;n<r;n++)i[n]===a.DataStreamTreeTokenType.PARAGRAPH&&e.push({startIndex:n});if(t)for(const n of e)t.bullet&&(n.bullet=a.Tools.deepClone(t.bullet)),t.paragraphStyle&&(n.paragraphStyle=a.Tools.deepClone(t.paragraphStyle));return e}const Ne={id:"doc.command.break-line",type:a.CommandType.COMMAND,handler:async i=>{var g;const t=i.get(exports.TextSelectionManagerService),e=i.get(a.IUniverInstanceService),n=i.get(a.ICommandService),r=t.getActiveRange();if(r==null)return!1;const o=e.getCurrentUniverDocInstance(),s=o.getUnitId(),{startOffset:c,segmentId:l,style:u}=r,d=[{startOffset:c+1,endOffset:c+1,style:u}],h=(((g=o.getBody())==null?void 0:g.paragraphs)??[]).find(f=>f.startIndex>=c);return await n.executeCommand(Re.id,{unitId:s,body:{dataStream:a.DataStreamTreeTokenType.PARAGRAPH,paragraphs:Mt(a.DataStreamTreeTokenType.PARAGRAPH,h)},range:r,textRanges:d,segmentId:l})}},se={id:D.CopyCommand.id,name:"doc.command.copy",type:a.CommandType.COMMAND,multi:!0,priority:999,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.EDITOR_ACTIVATED),handler:async()=>!0},ae={id:D.CutCommand.id,name:"doc.command.cut",type:a.CommandType.COMMAND,multi:!0,priority:999,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.EDITOR_ACTIVATED),handler:async()=>!0},ce={id:D.PasteCommand.id,name:"doc.command.paste",type:a.CommandType.COMMAND,multi:!0,priority:999,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.EDITOR_ACTIVATED),handler:async()=>!0};var Ot=Object.defineProperty,Rt=Object.getOwnPropertyDescriptor,yt=(i,t,e,n)=>{for(var r=n>1?void 0:n?Rt(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&Ot(t,e,r),r},Ge=(i,t)=>(e,n)=>t(e,n,i);exports.DocSkeletonManagerService=class extends a.RxDisposable{constructor(e,n){super();v(this,"_currentSkeletonUnitId","");v(this,"_docSkeletonMap",new Map);v(this,"_currentSkeleton$",new L.BehaviorSubject(null));v(this,"currentSkeleton$",this._currentSkeleton$.asObservable());v(this,"_currentSkeletonBefore$",new L.BehaviorSubject(null));v(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());this._localeService=e,this._docViewModelManagerService=n,this._initialize()}_initialize(){this._docViewModelManagerService.currentDocViewModel$.pipe(L.takeUntil(this.dispose$)).subscribe(e=>{e!=null&&this._setCurrent(e)})}dispose(){this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete(),this._docSkeletonMap.clear()}getCurrent(){return this.getSkeletonByUnitId(this._currentSkeletonUnitId)}makeDirtyCurrent(e=!0){this.makeDirty(this._currentSkeletonUnitId,e)}makeDirty(e,n=!0){const r=this.getSkeletonByUnitId(e);r!=null&&(r.dirty=n)}getSkeletonByUnitId(e){return this._docSkeletonMap.get(e)}_setCurrent(e){const{unitId:n}=e;if(this._docSkeletonMap.has(n)){const r=this.getSkeletonByUnitId(n);r.skeleton.calculate(),r.dirty=!0}else{const r=this._buildSkeleton(e.docViewModel);r.calculate(),this._docSkeletonMap.set(n,{unitId:n,skeleton:r,dirty:!1})}return this._currentSkeletonUnitId=n,this._currentSkeletonBefore$.next(this.getCurrent()),this._currentSkeleton$.next(this.getCurrent()),this.getCurrent()}_buildSkeleton(e){return M.DocumentSkeleton.create(e,this._localeService)}};exports.DocSkeletonManagerService=yt([Ge(0,E.Inject(a.LocaleService)),Ge(1,E.Inject(exports.DocViewModelManagerService))],exports.DocSkeletonManagerService);const Xe={id:"doc.command.inner-paste",type:a.CommandType.COMMAND,handler:async(i,t)=>{const{segmentId:e,body:n,textRanges:r}=t,o=i.get(a.IUndoRedoService),s=i.get(a.ICommandService),c=i.get(exports.TextSelectionManagerService),l=i.get(a.IUniverInstanceService),u=c.getSelections();if(!Array.isArray(u)||u.length===0)return!1;const p=l.getCurrentUniverDocInstance().getUnitId(),h={id:_.id,params:{unitId:p,mutations:[]}},m=new a.MemoryCursor;m.reset();const g=new a.TextX;for(const S of u){const{startOffset:I,endOffset:C,collapsed:x}=S,T=I-m.cursor;x?g.push({t:"r",len:T,segmentId:e}):g.push(...Oe(S,e,m.cursor)),g.push({t:"i",body:n,len:n.dataStream.length,line:0,segmentId:e}),m.reset(),m.moveCursor(C)}h.params.mutations=g.serialize();const f=s.syncExecuteCommand(h.id,h.params);return c.replaceTextRanges(r),f?(o.pushUndoRedo({unitID:p,undoMutations:[{id:_.id,params:f}],redoMutations:[{id:_.id,params:h.params}],undo(){return s.syncExecuteCommand(_.id,f),c.replaceTextRanges(u),!0},redo(){return s.syncExecuteCommand(_.id,h.params),c.replaceTextRanges(r),!0}}),!0):!1}},Ie={id:"doc.command.inner-cut",type:a.CommandType.COMMAND,handler:async(i,t)=>{const{segmentId:e,textRanges:n}=t,r=i.get(a.IUndoRedoService),o=i.get(a.ICommandService),s=i.get(exports.TextSelectionManagerService),c=i.get(a.IUniverInstanceService),l=s.getSelections();if(!Array.isArray(l)||l.length===0)return!1;const u=c.getCurrentUniverDocInstance().getUnitId(),d=c.getUniverDocInstance(u),p=a.getDocsUpdateBody(d.snapshot,e);if(p==null)return!1;const h={id:_.id,params:{unitId:u,mutations:[]}},m=new a.MemoryCursor;m.reset();const g=new a.TextX;for(const S of l){const{startOffset:I,endOffset:C,collapsed:x}=S,T=I-m.cursor;x?g.push({t:"r",len:T,segmentId:e}):g.push(...xt(S,p,e,m.cursor)),m.reset(),m.moveCursor(C)}h.params.mutations=g.serialize();const f=o.syncExecuteCommand(h.id,h.params);return s.replaceTextRanges(n),f?(r.pushUndoRedo({unitID:u,undoMutations:[{id:_.id,params:f}],redoMutations:[{id:_.id,params:h.params}],undo(){return o.syncExecuteCommand(_.id,f),s.replaceTextRanges(l),!0},redo(){return o.syncExecuteCommand(_.id,h.params),s.replaceTextRanges(n),!0}}),!0):!1}};function xt(i,t,e="",n=0){const{startOffset:r,endOffset:o}=i,s=[],{paragraphs:c=[]}=t,l=r-n,u=o-n,d=c==null?void 0:c.find(p=>p.startIndex-n>=l&&p.startIndex-n<=u);if(l>0&&s.push({t:"r",len:l,segmentId:e}),d&&d.startIndex-n>l){const p=d.startIndex-n;s.push({t:"d",len:p-l,line:0,segmentId:e}),s.push({t:"r",len:1,segmentId:e}),u>p+1&&s.push({t:"d",len:u-p-1,line:0,segmentId:e})}else s.push({t:"d",len:u-l,line:0,segmentId:e});return s}const we={id:"doc.command.delete-left",type:a.CommandType.COMMAND,handler:async i=>{var T;const t=i.get(exports.TextSelectionManagerService),e=i.get(exports.DocSkeletonManagerService),n=i.get(a.IUniverInstanceService),r=i.get(a.ICommandService),o=t.getActiveRange(),s=t.getSelections(),c=(T=e.getCurrent())==null?void 0:T.skeleton;let l=!0;if(o==null||c==null||s==null)return!1;const u=n.getCurrentUniverDocInstance(),{startOffset:d,collapsed:p,segmentId:h,style:m}=o,g=c.findNodeByCharIndex(d),f=M.hasListSpan(g),S=M.isIndentBySpan(g,u.getBody());let I=d;const C=c.findNodeByCharIndex(d-1);if(M.isFirstSpan(g)&&C!==g&&(f===!0||S===!0)){const O=M.getParagraphBySpan(g,u.getBody());if(O==null)return!1;const y=O==null?void 0:O.startIndex,b={startIndex:0},U=O.paragraphStyle;if(f===!0){const R=O.paragraphStyle;R&&(b.paragraphStyle=R)}else if(S===!0){const R=O.bullet;R&&(b.bullet=R),U!=null&&(b.paragraphStyle={...U},delete b.paragraphStyle.hanging,delete b.paragraphStyle.indentStart)}const P=[{startOffset:I,endOffset:I,style:m}];l=await r.executeCommand(Pe.id,{unitId:u.getUnitId(),updateBody:{dataStream:"",paragraphs:[{...b}]},range:{startOffset:y,endOffset:y+1},textRanges:P,coverType:a.UpdateDocsAttributeType.REPLACE,segmentId:h})}else if(p===!0){if(C==null)return!0;if(C.content==="\r")l=await r.executeCommand(Ae.id,{direction:A.LEFT,range:o});else{I-=C.count;const O=[{startOffset:I,endOffset:I,style:m}];l=await r.executeCommand(ye.id,{unitId:u.getUnitId(),range:o,segmentId:h,direction:A.LEFT,len:C.count,textRanges:O})}}else{const O=tt(o,s);l=await r.executeCommand(Ie.id,{segmentId:h,textRanges:O})}return l}},Le={id:"doc.command.delete-right",type:a.CommandType.COMMAND,handler:async i=>{var g;const t=i.get(exports.TextSelectionManagerService),e=i.get(exports.DocSkeletonManagerService),n=i.get(a.IUniverInstanceService),r=i.get(a.ICommandService),o=t.getActiveRange(),s=t.getSelections(),c=(g=e.getCurrent())==null?void 0:g.skeleton;let l;if(o==null||c==null||s==null)return!1;const u=n.getCurrentUniverDocInstance(),{startOffset:d,collapsed:p,segmentId:h,style:m}=o;if(d===u.getBody().dataStream.length-2&&p)return!0;if(p===!0){const f=c.findNodeByCharIndex(d);if(f.content==="\r")l=await r.executeCommand(Ae.id,{direction:A.RIGHT,range:o});else{const S=[{startOffset:d,endOffset:d,style:m}];l=await r.executeCommand(ye.id,{unitId:u.getUnitId(),range:o,segmentId:h,direction:A.RIGHT,textRanges:S,len:f.count})}}else{const f=tt(o,s);l=await r.executeCommand(Ie.id,{segmentId:h,textRanges:f})}return l}},Ae={id:"doc.command.merge-two-paragraph",type:a.CommandType.COMMAND,handler:async(i,t)=>{var U,P,R;const e=i.get(exports.TextSelectionManagerService),n=i.get(a.IUniverInstanceService),r=i.get(a.ICommandService),o=i.get(a.IUndoRedoService),{direction:s,range:c}=t,l=e.getActiveRange(),u=e.getSelections();if(l==null||u==null)return!1;const d=n.getCurrentUniverDocInstance(),{startOffset:p,collapsed:h,segmentId:m,style:g}=l;if(!h)return!1;const f=s===A.LEFT?p:p+1,S=(R=(P=(U=d.getBody())==null?void 0:U.paragraphs)==null?void 0:P.find(N=>N.startIndex>=f))==null?void 0:R.startIndex,I=Tt(d.getBody(),f,S),C=s===A.LEFT?p-1:p,x=d.getUnitId(),T=[{startOffset:C,endOffset:C,style:g}],O={id:_.id,params:{unitId:x,mutations:[]}},y=new a.TextX;y.push({t:"r",len:s===A.LEFT?p-1:p,segmentId:m}),I.dataStream.length&&y.push({t:"i",body:I,len:I.dataStream.length,line:0,segmentId:m}),y.push({t:"r",len:1,segmentId:m}),y.push({t:"d",len:S+1-f,line:0,segmentId:m}),O.params.mutations=y.serialize();const b=r.syncExecuteCommand(O.id,O.params);return e.replaceTextRanges(T),b?(o.pushUndoRedo({unitID:x,undoMutations:[{id:_.id,params:b}],redoMutations:[{id:_.id,params:O.params}],undo(){return r.syncExecuteCommand(_.id,b),e.replaceTextRanges([c]),!0},redo(){return r.syncExecuteCommand(_.id,O.params),e.replaceTextRanges(T),!0}}),!0):!1}};function Tt(i,t,e){const{textRuns:n}=i,r=i.dataStream.substring(t,e);if(n==null)return{dataStream:r};const o=[];for(const s of n){const{st:c,ed:l}=s;l<=t||c>=e||(c<t?o.push({...s,st:0,ed:l-t}):l>e?o.push({...s,st:c-t,ed:e-t}):o.push({...s,st:c-t,ed:l-t}))}return{dataStream:r,textRuns:o}}function tt(i,t){let e=i.endOffset;for(const r of t){const{startOffset:o,endOffset:s}=r;o==null||s==null||s<=i.endOffset&&(e-=s-o)}return[{startOffset:e,endOffset:e,style:i.style}]}class Be{constructor(){v(this,"_previousActiveRange",null);v(this,"_undoMutationParamsCache",[]);v(this,"_redoMutationParamsCache",[])}clearUndoRedoMutationParamsCache(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[]}setActiveRange(t){this._previousActiveRange=t}pushUndoRedoMutationParams(t,e){this._undoMutationParamsCache.push(t),this._redoMutationParamsCache.push(e)}fetchComposedUndoRedoMutationParams(t){if(this._undoMutationParamsCache.length===0||this._previousActiveRange==null)return null;const{unitId:e}=this._undoMutationParamsCache[0],{segmentId:n,startOffset:r,collapsed:o}=this._previousActiveRange,s={unitId:e,mutations:[]},c={unitId:e,mutations:[]};return o?(s.mutations.push({t:"r",len:r,segmentId:n}),c.mutations.push({t:"r",len:r,segmentId:n})):(r>0&&s.mutations.push({t:"r",len:r,segmentId:n}),s.mutations.push(this._undoMutationParamsCache[0].mutations.find(l=>l.t==="i")),c.mutations.push(...Oe(this._previousActiveRange,n))),t.length&&(s.mutations.push({t:"d",len:t.length,line:0,segmentId:n}),c.mutations.push({t:"i",body:{dataStream:t},len:t.length,line:0,segmentId:n})),{redoMutationParams:c,undoMutationParams:s,previousActiveRange:this._previousActiveRange}}dispose(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[],this._previousActiveRange=null}}const $e={id:"doc.command.ime-input",type:a.CommandType.COMMAND,handler:async(i,t)=>{const{unitId:e,newText:n,oldTextLen:r,range:o,segmentId:s,textRanges:c,isCompositionEnd:l}=t,u=i.get(a.ICommandService),d=i.get(a.IUndoRedoService),p=i.get(exports.TextSelectionManagerService),h=i.get(Be),m={id:_.id,params:{unitId:e,mutations:[]}},g=new a.TextX;o.collapsed?g.push({t:"r",len:o.startOffset,segmentId:s}):g.push(...Oe(o,s)),r>0&&g.push({t:"d",len:r,line:0,segmentId:s}),g.push({t:"i",body:{dataStream:n},len:n.length,line:0,segmentId:s}),m.params.mutations=g.serialize();const f=u.syncExecuteCommand(m.id,m.params);if(h.pushUndoRedoMutationParams(f,m.params),p.replaceTextRanges(c),l){if(f){const S=h.fetchComposedUndoRedoMutationParams(n);if(S==null)return!1;const{undoMutationParams:I,redoMutationParams:C,previousActiveRange:x}=S;return d.pushUndoRedo({unitID:e,undoMutations:[{id:_.id,params:I}],redoMutations:[{id:_.id,params:C}],undo(){return u.syncExecuteCommand(_.id,I),p.replaceTextRanges([x]),!0},redo(){return u.syncExecuteCommand(_.id,C),p.replaceTextRanges(c),!0}}),!0}}else return!!f;return!1}},G={id:"doc.command.set-inline-format-bold",type:a.CommandType.COMMAND,handler:async()=>!0},ee={id:"doc.command.set-inline-format-italic",type:a.CommandType.COMMAND,handler:async()=>!0},te={id:"doc.command.set-inline-format-underline",type:a.CommandType.COMMAND,handler:async()=>!0},ne={id:"doc.command.set-inline-format-strikethrough",type:a.CommandType.COMMAND,handler:async()=>!0},B={id:"doc.command.set-inline-format-subscript",type:a.CommandType.COMMAND,handler:async()=>!0},k={id:"doc.command.set-inline-format-superscript",type:a.CommandType.COMMAND,handler:async()=>!0},X={id:"doc.command.set-inline-format-fontsize",type:a.CommandType.COMMAND,handler:async()=>!0},H={id:"doc.command.set-inline-format-font-family",type:a.CommandType.COMMAND,handler:async()=>!0},re={id:"doc.command.set-inline-format-text-color",type:a.CommandType.COMMAND,handler:async()=>!0},nt={[G.id]:"bl",[ee.id]:"it",[te.id]:"ul",[ne.id]:"st",[X.id]:"fs",[H.id]:"ff",[re.id]:"cl",[B.id]:"va",[k.id]:"va"},Fe={id:"doc.command.set-inline-format",type:a.CommandType.COMMAND,handler:async(i,t)=>{const{segmentId:e,value:n,preCommandId:r}=t,o=i.get(a.IUndoRedoService),s=i.get(a.ICommandService),c=i.get(exports.TextSelectionManagerService),l=i.get(a.IUniverInstanceService),u=c.getSelections();if(!Array.isArray(u)||u.length===0)return!1;let d=l.getCurrentUniverDocInstance(),p=d.getUnitId();p===a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY&&(d=l.getUniverDocInstance(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),p=d.getUnitId());let h;switch(r){case G.id:case ee.id:case te.id:case ne.id:case B.id:case k.id:{h=bt(d.getBody().textRuns,r,u);break}case X.id:case H.id:{h=n;break}case re.id:{h={rgb:n};break}default:throw new Error(`Unknown command: ${r} in handleInlineFormat`)}const m={id:_.id,params:{unitId:p,mutations:[]}},g=new a.TextX,f=new a.MemoryCursor;f.reset();for(const C of u){const{startOffset:x,endOffset:T}=C,O={dataStream:"",textRuns:[{st:0,ed:T-x,ts:{[nt[r]]:h}}]},y=x-f.cursor;y!==0&&g.push({t:"r",len:y,segmentId:e}),g.push({t:"r",body:O,len:T-x,segmentId:e}),f.reset(),f.moveCursor(T)}m.params.mutations=g.serialize();const S=s.syncExecuteCommand(m.id,m.params),I=[G.id,X.id,H.id,B.id,k.id];return I.includes(r)&&c.refreshSelection(),S?(o.pushUndoRedo({unitID:p,undoMutations:[{id:_.id,params:S}],redoMutations:[{id:_.id,params:m.params}],undo(){return s.syncExecuteCommand(_.id,S),I.includes(r)&&c.refreshSelection(),!0},redo(){return s.syncExecuteCommand(_.id,m.params),I.includes(r)&&c.refreshSelection(),!0}}),!0):!1}};function Dt(i){return i!==null&&typeof i=="object"}function bt(i,t,e){let n=0,r=0;const o=nt[t];for(;n!==i.length&&r!==e.length;){const{startOffset:s,endOffset:c}=e[r],{st:l,ed:u,ts:d}=i[n];if(c<=l)r++;else if(u<=s)n++;else{if((d==null?void 0:d[o])==null){if(/bl|it/.test(o))return a.BooleanNumber.TRUE;if(/ul|st/.test(o))return{s:a.BooleanNumber.TRUE};if(/va/.test(o))return t===B.id?a.BaselineOffset.SUBSCRIPT:a.BaselineOffset.SUPERSCRIPT}if(Dt(d==null?void 0:d[o])&&d[o].s===a.BooleanNumber.FALSE)return{s:a.BooleanNumber.TRUE};if(t===B.id&&(d==null?void 0:d[o])!==a.BaselineOffset.SUBSCRIPT)return a.BaselineOffset.SUBSCRIPT;if(t===k.id&&(d==null?void 0:d[o])!==a.BaselineOffset.SUPERSCRIPT)return a.BaselineOffset.SUPERSCRIPT;if((d==null?void 0:d[o])===a.BooleanNumber.FALSE)return a.BooleanNumber.TRUE;n++}}return/bl|it/.test(o)?a.BooleanNumber.FALSE:/ul|st/.test(o)?{s:a.BooleanNumber.FALSE}:a.BaselineOffset.NORMAL}const rt={id:"doc.command.bullet-list",type:a.CommandType.COMMAND,handler:i=>i.get(a.ICommandService).syncExecuteCommand(je.id,{listType:a.PresetListType.BULLET_LIST})},it={id:"doc.command.order-list",type:a.CommandType.COMMAND,handler:i=>i.get(a.ICommandService).syncExecuteCommand(je.id,{listType:a.PresetListType.ORDER_LIST})},je={id:"doc.command.list-operation",type:a.CommandType.COMMAND,handler:(i,t)=>{var T;const e=i.get(exports.TextSelectionManagerService),n=i.get(a.IUniverInstanceService),r=i.get(a.ICommandService),o=i.get(a.IUndoRedoService),{listType:s}=t,c=n.getCurrentUniverDocInstance(),l=e.getActiveRange(),u=(T=c.getBody())==null?void 0:T.paragraphs;if(l==null||u==null)return!1;const d=Et(l,u),{segmentId:p}=l,h=c.getUnitId(),m=d.every(O=>{var y;return((y=O.bullet)==null?void 0:y.listType)===s}),g=6;let f=a.Tools.generateRandomId(g);if(d.length===1){const O=u.indexOf(d[0]),y=u[O-1],b=u[O+1];y&&y.bullet&&y.bullet.listType===s?f=y.bullet.listId:b&&b.bullet&&b.bullet.listType===s&&(f=b.bullet.listId)}const S={id:_.id,params:{unitId:h,mutations:[]}},I=new a.MemoryCursor;I.reset();const C=new a.TextX;for(const O of d){const{startIndex:y}=O;C.push({t:"r",len:y-I.cursor,segmentId:p});const b={...O.paragraphStyle,hanging:void 0,indentStart:void 0};C.push({t:"r",len:1,body:{dataStream:"",paragraphs:[m?{paragraphStyle:b,startIndex:0}:{...O,startIndex:0,bullet:{...O.bullet??{nestingLevel:0,textStyle:{fs:20}},listType:s,listId:f}}]},segmentId:p,coverType:a.UpdateDocsAttributeType.REPLACE}),I.moveCursorTo(y+1)}S.params.mutations=C.serialize();const x=r.syncExecuteCommand(S.id,S.params);return e.refreshSelection(),x&&o.pushUndoRedo({unitID:h,undoMutations:[{id:_.id,params:x}],redoMutations:[{id:_.id,params:S.params}],undo(){return r.syncExecuteCommand(_.id,x),e.refreshSelection(),!0},redo(){return r.syncExecuteCommand(_.id,S.params),e.refreshSelection(),!0}}),!0}};function Et(i,t){const{startOffset:e,endOffset:n}=i,r=[];let o=-1;for(const s of t){const{startIndex:c}=s;(e>o&&e<=c||n>o&&n<=c||c>=e&&c<=n)&&r.push(s),o=c}return r}const ot={id:"doc.command-replace-content",type:a.CommandType.COMMAND,handler:async(i,t)=>{var g;const{unitId:e,body:n,textRanges:r,segmentId:o=""}=t,s=i.get(a.IUniverInstanceService),c=i.get(a.ICommandService),l=i.get(exports.TextSelectionManagerService),u=i.get(a.IUndoRedoService),d=(g=s.getUniverDocInstance(e))==null?void 0:g.getSnapshot().body,p=l.getSelections();if(d==null||!Array.isArray(p)||p.length===0)return!1;const h=at(e,o,d,n),m=c.syncExecuteCommand(h.id,h.params);return l.replaceTextRanges(r),m?(u.pushUndoRedo({unitID:e,undoMutations:[{id:_.id,params:m}],redoMutations:[{id:_.id,params:h.params}],undo(){return c.syncExecuteCommand(_.id,m),l.replaceTextRanges(p),!0},redo(){return c.syncExecuteCommand(_.id,h.params),l.replaceTextRanges(r),!0}}),!0):!1}},st={id:"doc.command-cover-content",type:a.CommandType.COMMAND,handler:async(i,t)=>{var d;const{unitId:e,body:n,segmentId:r=""}=t,o=i.get(a.IUniverInstanceService),s=i.get(a.ICommandService),c=i.get(a.IUndoRedoService),l=(d=o.getUniverDocInstance(e))==null?void 0:d.getSnapshot().body;if(l==null)return!1;const u=at(e,r,l,n);return s.syncExecuteCommand(u.id,u.params),c.clearUndoRedo(e),!0}};function at(i,t,e,n){const r={id:_.id,params:{unitId:i,mutations:[]}},o=new a.TextX,s=(e==null?void 0:e.dataStream.length)-2;return s>0&&o.push({t:"d",len:s,line:0,segmentId:t}),n.dataStream.length>0&&o.push({t:"i",body:n,len:n.dataStream.length,line:0,segmentId:t}),r.params.mutations=o.serialize(),r}const $={id:"doc.operation.move-cursor",type:a.CommandType.OPERATION,handler:(i,t)=>!!t},F={id:"doc.operation.move-selection",type:a.CommandType.OPERATION,handler:(i,t)=>!!t},Ut=(i,t)=>{const e=i.get(a.IUniverInstanceService).getUniverDocInstance(t.unitId),n=(e==null?void 0:e.zoomRatio)||1;return{...a.Tools.deepClone(t),zoomRatio:n}},V={id:"doc.operation.set-zoom-ratio",type:a.CommandType.OPERATION,handler:(i,t)=>{const e=i.get(a.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}},ct={type:a.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(i,t)=>{const e=i.get(a.ICommandService),n=i.get(a.IUndoRedoService),r=i.get(a.IUniverInstanceService);let o=r.getCurrentUniverDocInstance().getUnitId(),s=1;if(t&&(o=t.documentId??o,s=t.zoomRatio??s),!r.getUniverDocInstance(o))return!1;const l={zoomRatio:s,unitId:o},u=Ut(i,l);return e.syncExecuteCommand(V.id,l)?(n.pushUndoRedo({unitID:o,undoMutations:[{id:V.id,params:u}],redoMutations:[{id:V.id,params:l}]}),!0):!1}},lt={id:"doc.operation.select-all",type:a.CommandType.COMMAND,handler:async i=>{const t=i.get(a.IUniverInstanceService),e=i.get(exports.TextSelectionManagerService),n=t.getCurrentUniverDocInstance().getSnapshot().body;if(n==null)return!1;const r=[{startOffset:0,endOffset:n.dataStream.length-2}];return e.replaceTextRanges(r),!0}},Pt=10,Nt=6;function wt(){return a.Tools.generateRandomId(Nt)}function Lt(i){const t=i.match(/data-copy-id="([^\s]+)"/);return t&&t[1]?t[1]:null}class At{constructor(){v(this,"_cache",new a.LRUMap(Pt))}set(t,e){this._cache.set(t,e)}get(t){return this._cache.get(t)}clear(){this._cache.clear()}}const He=new At;function Bt(i){const t=i.style,e={};for(let n=0;n<t.length;n++){const r=t[n],o=t.getPropertyValue(r);switch(r){case"margin-top":{const s=parseInt(o);e.spaceAbove=/pt/.test(o)?be(s):s;break}case"margin-bottom":{const s=parseInt(o);e.spaceBelow=/pt/.test(o)?be(s):s;break}}}return Object.getOwnPropertyNames(e).length?e:null}function be(i){return i/.75}function Ve(i){const t=i.style,e={},n=i.tagName.toLowerCase();switch(n){case"b":case"em":case"strong":{e.bl=a.BooleanNumber.TRUE;break}case"s":{e.st={s:a.BooleanNumber.TRUE};break}case"u":{e.ul={s:a.BooleanNumber.TRUE};break}case"i":{e.it=a.BooleanNumber.TRUE;break}case"sub":case"sup":{e.va=n==="sup"?a.BaselineOffset.SUPERSCRIPT:a.BaselineOffset.SUBSCRIPT;break}}for(let r=0;r<t.length;r++){const o=t[r],s=t.getPropertyValue(o);switch(o){case"font-family":{e.ff=s;break}case"font-size":{const c=parseInt(s);Number.isNaN(c)||(e.fs=/pt$/.test(s)?be(c):c);break}case"font-style":{s==="italic"&&(e.it=a.BooleanNumber.TRUE);break}case"font-weight":{Number(s)>400&&(e.bl=a.BooleanNumber.TRUE);break}case"text-decoration":{/underline/.test(s)?e.ul={s:a.BooleanNumber.TRUE}:/overline/.test(s)?e.ol={s:a.BooleanNumber.TRUE}:/line-through/.test(s)&&(e.st={s:a.BooleanNumber.TRUE});break}case"color":{const c=new a.ColorKit(s);c.isValid&&(e.cl={rgb:c.toRgbString()});break}case"background-color":{const c=new a.ColorKit(s);c.isValid&&(e.bg={rgb:c.toRgbString()});break}}}return e}function $t(i){const t=new DOMParser,e=`<x-univer id="univer-root">${i}</x-univer>`;return t.parseFromString(e,"text/html").querySelector("#univer-root")}function Ke(i,t){const e=i.tagName.toLowerCase();return typeof t=="string"?e===t:Array.isArray(t)?t.some(n=>n===e):t(i)}const Ce=class Ce{constructor(){v(this,"_styleCache",new Map);v(this,"_styleRules",[]);v(this,"_afterProcessRules",[])}static use(t){if(this._pluginList.includes(t))throw new Error(`Univer paste plugin ${t.name} already added`);this._pluginList.push(t)}convert(t){const e=Ce._pluginList.find(o=>o.checkPasteType(t)),n=$t(t),r={dataStream:"",textRuns:[]};return e&&(this._styleRules=[...e.stylesRules],this._afterProcessRules=[...e.afterProcessRules]),this._styleCache.clear(),this._process(null,n==null?void 0:n.childNodes,r),this._styleCache.clear(),this._styleRules=[],this._afterProcessRules=[],r}_process(t,e,n){var r;for(const o of e)if(o.nodeType===Node.TEXT_NODE){const s=(r=o.nodeValue)==null?void 0:r.replace(/[\r\n]/g,"");let c;t&&this._styleCache.has(t)&&(c=this._styleCache.get(t)),n.dataStream+=s,c&&Object.getOwnPropertyNames(c).length&&n.textRuns.push({st:n.dataStream.length-s.length,ed:n.dataStream.length,ts:c})}else if(o.nodeType===Node.ELEMENT_NODE){const s=t?this._styleCache.get(t):{},c=this._styleRules.find(({filter:p})=>Ke(o,p)),l=c?c.getStyle(o):Ve(o);this._styleCache.set(o,{...s,...l});const{childNodes:u}=o;this._process(o,u,n);const d=this._afterProcessRules.find(({filter:p})=>Ke(o,p));d&&d.handler(n,o)}}};v(Ce,"_pluginList",[]);let J=Ce;const Ft={name:"univer-doc-paste-plugin-lark",checkPasteType(i){return/lark-record-clipboard/i.test(i)},stylesRules:[{filter:["s"],getStyle(i){const t=Ve(i);return{st:{s:a.BooleanNumber.TRUE},...t}}}],afterProcessRules:[{filter(i){return i.tagName==="DIV"&&/ace-line/i.test(i.className)},handler(i){i.paragraphs==null&&(i.paragraphs=[]),i.paragraphs.push({startIndex:i.dataStream.length}),i.dataStream+="\r"}}]},jt={name:"univer-doc-paste-plugin-word",checkPasteType(i){return/word|mso/i.test(i)},stylesRules:[{filter:["b"],getStyle(i){const t=Ve(i);return{bl:a.BooleanNumber.TRUE,...t}}}],afterProcessRules:[{filter(i){return i.tagName==="P"&&/mso/i.test(i.className)},handler(i,t){i.paragraphs==null&&(i.paragraphs=[]);const e={startIndex:i.dataStream.length},n=Bt(t);n&&(e.paragraphStyle=n),i.paragraphs.push(e),i.dataStream+="\r"}}]};function Vt(i,t){const{st:e,ed:n,ts:r={}}=t,{ff:o,fs:s,it:c,bl:l,ul:u,st:d,ol:p,bg:h,cl:m,va:g}=r;let f=i.slice(e,n);const S=[];return c===a.BooleanNumber.TRUE&&(f=`<i>${f}</i>`),g===a.BaselineOffset.SUPERSCRIPT?f=`<sup>${f}</sup>`:g===a.BaselineOffset.SUBSCRIPT&&(f=`<sub>${f}</sub>`),(u==null?void 0:u.s)===a.BooleanNumber.TRUE&&(f=`<u>${f}</u>`),(d==null?void 0:d.s)===a.BooleanNumber.TRUE&&(f=`<s>${f}</s>`),l===a.BooleanNumber.TRUE&&(f=`<strong>${f}</strong>`),o&&S.push(`font-family: ${o}`),m&&S.push(`color: ${m.rgb}`),s&&S.push(`font-size: ${s}px`),p&&S.push("text-decoration: overline"),h&&S.push(`background: ${h.rgb}`),S.length?`<span style="${S.join(";")}">${f}</span>`:f}function Ye(i,t=!0){const{dataStream:e,textRuns:n=[],paragraphs:r=[]}=i;let o=0;const s=[],c=[];for(const l of n){const{st:u,ed:d}=l;if(u!==o&&s.push(e.slice(o,u)),s.push(Vt(e,l)),o=d,t)for(const p of r){const{startIndex:h,paragraphStyle:m={}}=p;if(h>=u&&h<=d){const{spaceAbove:g,spaceBelow:f}=m,S=[];g!=null&&(typeof g=="number"?S.push(`margin-top: ${g}px`):S.push(`margin-top: ${g.v}px`)),f!=null&&(typeof f=="number"?S.push(`margin-bottom: ${f}px`):S.push(`margin-bottom: ${f.v}px`)),c.push(`<p className="UniverNormal" ${S.length?`style="${S.join(";")}"`:""}>${s.join("")}</p>`),s.length=0}}}return c.join("")+s.join("")}class kt{convert(t){if(t.length===0)throw new Error("The bodyList length at least to be 1");if(t.length===1)return Ye(t[0]);let e="";for(const n of t)e+='<p className="UniverNormal">',e+=Ye(n,!1),e+="</p>";return e}}var zt=Object.defineProperty,Wt=Object.getOwnPropertyDescriptor,Gt=(i,t,e,n)=>{for(var r=n>1?void 0:n?Wt(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&zt(t,e,r),r},qe=(i,t)=>(e,n)=>t(e,n,i);J.use(jt);J.use(Ft);function Xt(i){const t=i.replace(/\n/g,"\r"),e=[];for(let n=0;n<t.length;n++)t[n]==="\r"&&e.push({startIndex:n});return{dataStream:t,paragraphs:e}}const dt=E.createIdentifier("doc.clipboard-service");let Ee=class extends a.Disposable{constructor(t,e){super();v(this,"_clipboardHooks",[]);v(this,"_htmlToUDM",new J);v(this,"_umdToHtml",new kt);this._currentUniverService=t,this._clipboardInterfaceService=e}async queryClipboardData(){const t=await this._clipboardInterfaceService.read();if(t.length===0)return Promise.reject();try{let e="",n="";for(const o of t)for(const s of o.types)s===D.PLAIN_TEXT_CLIPBOARD_MIME_TYPE?n=await o.getType(s).then(c=>c&&c.text()):s===D.HTML_CLIPBOARD_MIME_TYPE&&(e=await o.getType(s).then(c=>c&&c.text()));if(!e)return Xt(n);const r=Lt(e);if(r){const o=He.get(r);if(o)return o}return this._htmlToUDM.convert(e)}catch(e){return Promise.reject(e)}}async setClipboardData(t){const e=wt(),n=t.length>1?t.map(o=>o.dataStream).join(`
2
+ `):t[0].dataStream;let r=this._umdToHtml.convert(t);return t.length===1&&(r=r.replace(/(<[a-z]+)/,(o,s)=>`${s} data-copy-id="${e}"`),He.set(e,t[0])),this._clipboardInterfaceService.write(n,r)}addClipboardHook(t){return this._clipboardHooks.push(t),a.toDisposable(()=>{const e=this._clipboardHooks.indexOf(t);e>-1&&this._clipboardHooks.splice(e,1)})}};Ee=Gt([qe(0,a.IUniverInstanceService),qe(1,D.IClipboardInterfaceService)],Ee);var Ht=Object.defineProperty,Kt=Object.getOwnPropertyDescriptor,Yt=(i,t,e,n)=>{for(var r=n>1?void 0:n?Kt(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&Ht(t,e,r),r},z=(i,t)=>(e,n)=>t(e,n,i);let le=class extends a.Disposable{constructor(i,t,e,n,r,o){super(),this._logService=i,this._commandService=t,this._currentUniverService=e,this._docClipboardService=n,this._textSelectionManagerService=r,this._contextService=o,this._commandExecutedListener(),this.initialize()}initialize(){[se,ae,ce].forEach(i=>this.disposeWithMe(this._commandService.registerMultipleCommand(i))),[Xe,Ie].forEach(i=>this.disposeWithMe(this._commandService.registerCommand(i)))}_commandExecutedListener(){const i=[ae.id,se.id,ce.id];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{if(i.includes(t.id)&&!(!this._contextService.getContextValue(a.FOCUSING_DOC)&&!this._contextService.getContextValue(a.EDITOR_ACTIVATED)))switch(t.id){case ce.id:{this._handlePaste();break}case se.id:{this._handleCopy();break}case ae.id:{this._handleCut();break}default:throw new Error(`Unhandled command ${t.id}`)}}))}async _handlePaste(){const{_docClipboardService:i}=this,{segmentId:t,endOffset:e,style:n}=this._textSelectionManagerService.getActiveRange()??{},r=this._textSelectionManagerService.getSelections();if(t==null&&this._logService.error("[DocClipboardController] segmentId is not existed"),!(e==null||r==null))try{const o=await i.queryClipboardData();let s=e;for(const l of r){const{startOffset:u,endOffset:d}=l;u==null||d==null||d<=e&&(s+=o.dataStream.length-(d-u))}const c=[{startOffset:s,endOffset:s,style:n}];this._commandService.executeCommand(Xe.id,{body:o,segmentId:t,textRanges:c})}catch{this._logService.error("[DocClipboardController] clipboard is empty")}}_getDocumentBodyInRanges(){const i=this._textSelectionManagerService.getSelections(),t=this._currentUniverService.getCurrentUniverDocInstance(),e=[];if(i==null)return e;for(const n of i){const{startOffset:r,endOffset:o,collapsed:s}=n;if(s||r==null||o==null)continue;const c=t.sliceBody(r,o);c!=null&&e.push(c)}return e}async _handleCopy(){const{_docClipboardService:i}=this,t=this._getDocumentBodyInRanges();try{i.setClipboardData(t)}catch{this._logService.error("[DocClipboardController] set clipboard failed")}}async _handleCut(){const{segmentId:i,endOffset:t,style:e}=this._textSelectionManagerService.getActiveRange()??{},n=this._textSelectionManagerService.getSelections();if(i==null&&this._logService.error("[DocClipboardController] segmentId is not existed"),!(t==null||n==null)){this._handleCopy();try{let r=t;for(const s of n){const{startOffset:c,endOffset:l}=s;c==null||l==null||l<=t&&(r-=l-c)}const o=[{startOffset:r,endOffset:r,style:e}];this._commandService.executeCommand(Ie.id,{segmentId:i,textRanges:o})}catch{this._logService.error("[DocClipboardController] cut content failed")}}}};le=Yt([a.OnLifecycle(a.LifecycleStages.Rendered,le),z(0,a.ILogService),z(1,a.ICommandService),z(2,a.IUniverInstanceService),z(3,dt),z(4,E.Inject(exports.TextSelectionManagerService)),z(5,a.IContextService)],le);var qt=Object.defineProperty,Zt=Object.getOwnPropertyDescriptor,Jt=(i,t,e,n)=>{for(var r=n>1?void 0:n?Zt(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&qt(t,e,r),r},xe=(i,t)=>(e,n)=>t(e,n,i);let de=class extends a.RxDisposable{constructor(i,t,e){super(),this._docSkeletonManagerService=i,this._renderManagerService=t,this._commandService=e,this._initialRenderRefresh(),this._commandExecutedListener()}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeletonBefore$.pipe(L.takeUntil(this.dispose$)).subscribe(i=>{if(i==null)return;const{skeleton:t,unitId:e}=i,n=this._renderManagerService.getRenderById(e);if(n==null)return;const{mainComponent:r}=n;r.changeSkeleton(t),this._recalculateSizeBySkeleton(n,t)})}_recalculateSizeBySkeleton(i,t){var u;const{mainComponent:e,scene:n}=i,r=e,o=(u=t.getSkeletonData())==null?void 0:u.pages;if(o==null)return;let s=0,c=0;for(let d=0,p=o.length;d<p;d++){const h=o[d],{pageWidth:m,pageHeight:g}=h;r.pageLayoutType===M.PageLayoutType.VERTICAL?(c+=g,c+=r.pageMarginTop,d===p-1&&(c+=r.pageMarginTop),s=Math.max(s,m)):r.pageLayoutType===M.PageLayoutType.HORIZONTAL&&(s+=m,d!==p-1&&(s+=r.pageMarginLeft),c=Math.max(c,g))}r.resize(s,c),[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(i.unitId)||n.resize(s,c)}_commandExecutedListener(){const i=[_.id],t=[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{var n;if(i.includes(e.id)){const r=e.params,{unitId:o}=r,s=this._docSkeletonManagerService.getSkeletonByUnitId(o);if(s==null)return;const{skeleton:c}=s,l=this._renderManagerService.getRenderById(o);if(l==null)return;if(c.calculate(),t.includes(o)){(n=l.mainComponent)==null||n.makeDirty();return}this._recalculateSizeBySkeleton(l,c)}}))}};de=Jt([a.OnLifecycle(a.LifecycleStages.Rendered,de),xe(0,E.Inject(exports.DocSkeletonManagerService)),xe(1,M.IRenderManagerService),xe(2,a.ICommandService)],de);var Qt=Object.defineProperty,en=Object.getOwnPropertyDescriptor,tn=(i,t,e,n)=>{for(var r=n>1?void 0:n?en(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&Qt(t,e,r),r},K=(i,t)=>(e,n)=>t(e,n,i);let ue=class extends a.Disposable{constructor(t,e,n,r,o){super();v(this,"_liquid",new M.Liquid);v(this,"_pageMarginCache",new Map);this._docSkeletonManagerService=t,this._currentUniverService=e,this._renderManagerService=n,this._commandService=r,this._floatingObjectManagerService=o,this._initialize(),this._commandExecutedListener()}_initialize(){this._initialRenderRefresh(),this._updateOnPluginChange()}_updateOnPluginChange(){this._floatingObjectManagerService.pluginUpdate$.subscribe(t=>{const e=this._docSkeletonManagerService.getCurrent();if(e==null)return;const{unitId:n,skeleton:r}=e,o=this._renderManagerService.getRenderById(n);if(o==null)return;const{mainComponent:s,components:c,scene:l}=o,u=s,{left:d,top:p}=u;t.forEach(h=>{const{unitId:m,subUnitId:g,floatingObjectId:f,floatingObject:S}=h,{left:I=0,top:C=0,width:x=0,height:T=0,angle:O,flipX:y,flipY:b,skewX:U,skewY:P}=S,R=this._pageMarginCache.get(f),N=(R==null?void 0:R.marginLeft)||0,w=(R==null?void 0:R.marginTop)||0;r==null||r.getViewModel().getDataModel().updateDrawing(f,{left:I-d-N,top:C-p-w,height:T,width:x})}),r==null||r.calculate(),s==null||s.makeDirty()})}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeleton$.subscribe(t=>{if(t==null)return;const{skeleton:e,unitId:n}=t,r=this._renderManagerService.getRenderById(n);if(r==null)return;const{mainComponent:o}=r;o.changeSkeleton(e),this._refreshFloatingObject(n,e,r)})}_commandExecutedListener(){const t=[_.id,V.id],e=[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY];this.disposeWithMe(this._commandService.onCommandExecuted(n=>{var r;if(t.includes(n.id)){const o=n.params,{unitId:s}=o,c=this._docSkeletonManagerService.getCurrent();if(c==null)return;const{unitId:l,skeleton:u}=c;if(s!==l)return;const d=this._renderManagerService.getRenderById(l);if(d==null)return;if(e.includes(l)){(r=d.mainComponent)==null||r.makeDirty();return}this._refreshFloatingObject(l,u,d)}}))}_refreshFloatingObject(t,e,n){const r=e==null?void 0:e.getSkeletonData(),{mainComponent:o,scene:s}=n,c=o;if(!r)return;const{left:l,top:u,pageLayoutType:d,pageMarginLeft:p,pageMarginTop:h}=c,{pages:m}=r,g=[];s.getAncestorScale(),this._liquid.reset(),this._pageMarginCache.clear();for(let f=0,S=m.length;f<S;f++){const I=m[f],{skeDrawings:C,marginLeft:x,marginTop:T}=I;this._liquid.translatePagePadding(I),C.forEach(O=>{const{aLeft:y,aTop:b,height:U,width:P,objectId:R}=O;g.push({unitId:t,subUnitId:a.DEFAULT_DOCUMENT_SUB_COMPONENT_ID,floatingObjectId:R,floatingObject:{left:y+l+this._liquid.x,top:b+u+this._liquid.y,width:P,height:U}}),this._pageMarginCache.set(R,{marginLeft:this._liquid.x,marginTop:this._liquid.y})}),this._liquid.translatePage(I,d,p,h)}this._floatingObjectManagerService.BatchAddOrUpdate(g)}};ue=tn([a.OnLifecycle(a.LifecycleStages.Steady,ue),K(0,E.Inject(exports.DocSkeletonManagerService)),K(1,a.IUniverInstanceService),K(2,M.IRenderManagerService),K(3,a.ICommandService),K(4,a.IFloatingObjectManagerService)],ue);var nn=Object.defineProperty,rn=Object.getOwnPropertyDescriptor,on=(i,t,e,n)=>{for(var r=n>1?void 0:n?rn(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&nn(t,e,r),r},W=(i,t)=>(e,n)=>t(e,n,i);let me=class extends a.Disposable{constructor(t,e,n,r,o,s){super();v(this,"_previousIMEContent","");v(this,"_previousIMERange");v(this,"_onStartSubscription");v(this,"_onUpdateSubscription");v(this,"_onEndSubscription");this._docSkeletonManagerService=t,this._currentUniverService=e,this._renderManagerService=n,this._textSelectionRenderManager=r,this._imeInputManagerService=o,this._commandService=s,this._initialize()}dispose(){var t,e,n;(t=this._onStartSubscription)==null||t.unsubscribe(),(e=this._onUpdateSubscription)==null||e.unsubscribe(),(n=this._onEndSubscription)==null||n.unsubscribe()}_initialize(){this._initialOnCompositionstart(),this._initialOnCompositionUpdate(),this._initialOnCompositionend()}_initialOnCompositionstart(){this._onStartSubscription=this._textSelectionRenderManager.onCompositionstart$.subscribe(t=>{if(t==null)return;const{activeRange:e}=t;e!=null&&(this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(a.Tools.deepClone(e)),this._previousIMERange=e)})}_initialOnCompositionUpdate(){this._onUpdateSubscription=this._textSelectionRenderManager.onCompositionupdate$.subscribe(async t=>{this._updateContent(t,!0)})}_initialOnCompositionend(){this._onEndSubscription=this._textSelectionRenderManager.onCompositionend$.subscribe(t=>{this._updateContent(t,!1)})}async _updateContent(t,e){var g;const n=(g=this._docSkeletonManagerService.getCurrent())==null?void 0:g.skeleton;if(this._previousIMERange==null||t==null||n==null)return;const r=this._currentUniverService.getCurrentUniverDocInstance(),{event:o,activeRange:s}=t,{startOffset:c,segmentId:l,style:u}=this._previousIMERange;if(n==null||s==null)return;const p=o.data;if(p===this._previousIMEContent&&e)return;const h=p.length,m=[{startOffset:c+h,endOffset:c+h,style:u}];await this._commandService.executeCommand($e.id,{unitId:r.getUnitId(),newText:p,oldTextLen:this._previousIMEContent.length,range:this._previousIMERange,textRanges:m,isCompositionEnd:!e,segmentId:l}),e?(this._previousIMERange.collapsed||(this._previousIMERange.collapsed=!0),this._previousIMEContent=p):this._resetIME()}_resetIME(){this._previousIMEContent="",this._previousIMERange=null,this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(null)}_getDocObject(){return Q(this._currentUniverService,this._renderManagerService)}};me=on([a.OnLifecycle(a.LifecycleStages.Rendered,me),W(0,E.Inject(exports.DocSkeletonManagerService)),W(1,a.IUniverInstanceService),W(2,M.IRenderManagerService),W(3,M.ITextSelectionRenderManager),W(4,E.Inject(Be)),W(5,a.ICommandService)],me);var sn=Object.defineProperty,an=Object.getOwnPropertyDescriptor,cn=(i,t,e,n)=>{for(var r=n>1?void 0:n?an(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&sn(t,e,r),r},Ze=(i,t)=>(e,n)=>t(e,n,i);let ge=class extends a.Disposable{constructor(i,t){super(),this._textSelectionManagerService=i,this._commandService=t,this._commandExecutedListener()}_commandExecutedListener(){const i=[G.id,ee.id,te.id,ne.id,B.id,k.id,X.id,H.id,re.id];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{i.includes(t.id)&&this._handleInlineFormat(t)}))}_handleInlineFormat(i){const{segmentId:t}=this._textSelectionManagerService.getActiveRange()??{};t!=null&&this._commandService.executeCommand(Fe.id,{segmentId:t,preCommandId:i.id,...i.params??{}})}};ge=cn([a.OnLifecycle(a.LifecycleStages.Rendered,ge),Ze(0,E.Inject(exports.TextSelectionManagerService)),Ze(1,a.ICommandService)],ge);var ln=Object.defineProperty,dn=Object.getOwnPropertyDescriptor,un=(i,t,e,n)=>{for(var r=n>1?void 0:n?dn(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&ln(t,e,r),r},Y=(i,t)=>(e,n)=>t(e,n,i);let pe=class extends a.Disposable{constructor(t,e,n,r,o){super();v(this,"_onInputSubscription");this._docSkeletonManagerService=t,this._currentUniverService=e,this._renderManagerService=n,this._textSelectionManagerService=r,this._commandService=o,this._initialize(),this._commandExecutedListener()}dispose(){var t;(t=this._onInputSubscription)==null||t.unsubscribe()}_initialize(){}_commandExecutedListener(){const t=[$.id,F.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(!t.includes(e.id))return;const n=e.params;switch(e.id){case $.id:return this._handleMoveCursor(n.direction);case F.id:return this._handleShiftMoveSelection(n.direction);default:throw new Error("Unknown command")}}))}_handleShiftMoveSelection(t){var f;const e=this._textSelectionManagerService.getActiveRange(),n=this._textSelectionManagerService.getSelections(),r=this._currentUniverService.getCurrentUniverDocInstance(),o=(f=this._docSkeletonManagerService.getCurrent())==null?void 0:f.skeleton,s=this._getDocObject();if(e==null||o==null||s==null)return;const{startOffset:c,endOffset:l,style:u,collapsed:d,direction:p}=e;if(n.length>1){let S=Number.POSITIVE_INFINITY,I=Number.NEGATIVE_INFINITY;for(const C of n)S=Math.min(S,C.startOffset),I=Math.max(I,C.endOffset);this._textSelectionManagerService.replaceTextRanges([{startOffset:t===a.Direction.LEFT||t===a.Direction.UP?I:S,endOffset:t===a.Direction.LEFT||t===a.Direction.UP?S:I,style:u}]);return}const h=d||p===M.RANGE_DIRECTION.FORWARD?c:l;let m=d||p===M.RANGE_DIRECTION.FORWARD?l:c;const g=r.getBody().dataStream.length??Number.POSITIVE_INFINITY;if(t===a.Direction.LEFT||t===a.Direction.RIGHT){const S=o.findNodeByCharIndex(m-1),I=o.findNodeByCharIndex(m);m=t===a.Direction.RIGHT?m+I.count:m-((S==null?void 0:S.count)??0),m=Math.min(g-2,Math.max(0,m)),this._textSelectionManagerService.replaceTextRanges([{startOffset:h,endOffset:m,style:u}])}else{const S=o.findNodeByCharIndex(m),I=s.document.getOffsetConfig(),C=this._getTopOrBottomPosition(o,S,t===a.Direction.DOWN);if(C==null){const T=t===a.Direction.UP?0:g-2;if(T===m)return;this._textSelectionManagerService.replaceTextRanges([{startOffset:h,endOffset:T,style:u}]);return}const x=new M.NodePositionConvertToCursor(I,o).getRangePointData(C,C).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{startOffset:h,endOffset:x.endOffset,style:u}])}}_handleMoveCursor(t){var h;const e=this._textSelectionManagerService.getActiveRange(),n=this._textSelectionManagerService.getSelections(),r=this._currentUniverService.getCurrentUniverDocInstance(),o=(h=this._docSkeletonManagerService.getCurrent())==null?void 0:h.skeleton,s=this._getDocObject();if(e==null||o==null||s==null||n==null)return;const{startOffset:c,endOffset:l,style:u,collapsed:d}=e,p=r.getBody().dataStream.length??Number.POSITIVE_INFINITY;if(t===a.Direction.LEFT||t===a.Direction.RIGHT){let m;if(!e.collapsed||n.length>1){let g=Number.POSITIVE_INFINITY,f=Number.NEGATIVE_INFINITY;for(const S of n)g=Math.min(g,S.startOffset),f=Math.max(f,S.endOffset);m=t===a.Direction.LEFT?g:f}else{const g=o.findNodeByCharIndex(c-1),f=o.findNodeByCharIndex(c);t===a.Direction.LEFT?m=Math.max(0,c-((g==null?void 0:g.count)??0)):m=Math.min(p-2,l+f.count)}this._textSelectionManagerService.replaceTextRanges([{startOffset:m,endOffset:m,style:u}])}else{const m=o.findNodeByCharIndex(c),g=o.findNodeByCharIndex(l),f=s.document.getOffsetConfig(),S=this._getTopOrBottomPosition(o,t===a.Direction.UP?m:g,t===a.Direction.DOWN);if(S==null){let C;d?C=t===a.Direction.UP?0:p-2:C=t===a.Direction.UP?c:l,this._textSelectionManagerService.replaceTextRanges([{startOffset:C,endOffset:C,style:u}]);return}const I=new M.NodePositionConvertToCursor(f,o).getRangePointData(S,S).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{...I,style:u}])}}_getTopOrBottomPosition(t,e,n){if(e==null)return;const r=this._getSpanLeftOffsetInLine(e),o=this._getNextOrPrevLine(e,n);if(o==null)return;const s=this._matchPositionByLeftOffset(t,o,r);if(s!=null)return{...s,isBack:!0}}_getSpanLeftOffsetInLine(t){const e=t.parent;if(e==null)return Number.NEGATIVE_INFINITY;const n=e.left,{left:r}=t;return n+r}_matchPositionByLeftOffset(t,e,n){const r={distance:Number.POSITIVE_INFINITY};for(const o of e.divides){const s=o.left;for(const c of o.spanGroup){const{left:l}=c,u=s+l,d=Math.abs(n-u);d<r.distance&&(r.span=c,r.distance=d)}}if(r.span!=null)return t.findPositionBySpan(r.span)}_getNextOrPrevLine(t,e){var g,f,S,I,C,x,T,O,y,b,U,P;const n=t.parent;if(n==null)return;const r=n.parent;if(r==null)return;const o=r.parent;if(o==null)return;const s=o.lines.indexOf(r);if(s===-1)return;let c;if(e===!0?c=o.lines[s+1]:c=o.lines[s-1],c!=null)return c;const l=o.parent;if(l==null)return;const u=l.columns.indexOf(o);if(u===-1)return;if(e===!0)c=(g=l.columns[u+1])==null?void 0:g.lines[0];else{const R=(S=(f=l.columns)==null?void 0:f[u-1])==null?void 0:S.lines;c=R==null?void 0:R[R.length-1]}if(c!=null)return c;const d=l.parent;if(d==null)return;const p=d.sections.indexOf(l);if(p===-1)return;if(e===!0)c=(C=(I=d.sections[p-1])==null?void 0:I.columns[0])==null?void 0:C.lines[0];else{const R=(T=(x=d.sections)==null?void 0:x[p-1])==null?void 0:T.columns,N=R==null?void 0:R[R.length-1],w=N==null?void 0:N.lines;c=w==null?void 0:w[w.length-1]}if(c!=null)return c;const h=d.parent;if(h==null)return;const m=h.pages.indexOf(d);if(m!==-1){if(e===!0)c=(b=(y=(O=h.pages[m+1])==null?void 0:O.sections[0])==null?void 0:y.columns[0])==null?void 0:b.lines[0];else{const R=(U=h.pages[m-1])==null?void 0:U.sections;if(R==null)return;const N=(P=R[R.length-1])==null?void 0:P.columns,w=N[N.length-1],ke=w==null?void 0:w.lines;c=ke[ke.length-1]}if(c!=null)return c}}_getDocObject(){return Q(this._currentUniverService,this._renderManagerService)}};pe=un([a.OnLifecycle(a.LifecycleStages.Rendered,pe),Y(0,E.Inject(exports.DocSkeletonManagerService)),Y(1,a.IUniverInstanceService),Y(2,M.IRenderManagerService),Y(3,E.Inject(exports.TextSelectionManagerService)),Y(4,a.ICommandService)],pe);var mn=Object.defineProperty,gn=Object.getOwnPropertyDescriptor,pn=(i,t,e,n)=>{for(var r=n>1?void 0:n?gn(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&mn(t,e,r),r},q=(i,t)=>(e,n)=>t(e,n,i);let he=class extends a.Disposable{constructor(t,e,n,r,o){super();v(this,"_onInputSubscription");this._docSkeletonManagerService=t,this._currentUniverService=e,this._renderManagerService=n,this._textSelectionRenderManager=r,this._commandService=o,this._initialize(),this._commandExecutedListener()}dispose(){var t;(t=this._onInputSubscription)==null||t.unsubscribe()}_initialize(){this._initialNormalInput()}_initialNormalInput(){this._onInputSubscription=this._textSelectionRenderManager.onInput$.subscribe(async t=>{var g;if(t==null)return;const n=this._currentUniverService.getCurrentUniverDocInstance().getUnitId(),{event:r,content:o="",activeRange:s}=t,c=r,l=(g=this._docSkeletonManagerService.getCurrent())==null?void 0:g.skeleton;if(c.data==null||l==null||!l||!s)return;const{startOffset:u,segmentId:d,style:p}=s,h=o.length,m=[{startOffset:u+h,endOffset:u+h,style:p}];await this._commandService.executeCommand(Re.id,{unitId:n,body:{dataStream:o},range:s,textRanges:m,segmentId:d})})}_commandExecutedListener(){}_getDocObject(){return Q(this._currentUniverService,this._renderManagerService)}};he=pn([a.OnLifecycle(a.LifecycleStages.Rendered,he),q(0,E.Inject(exports.DocSkeletonManagerService)),q(1,a.IUniverInstanceService),q(2,M.IRenderManagerService),q(3,M.ITextSelectionRenderManager),q(4,a.ICommandService)],he);var hn=Object.defineProperty,fn=Object.getOwnPropertyDescriptor,Sn=(i,t,e,n)=>{for(var r=n>1?void 0:n?fn(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&hn(t,e,r),r},Je=(i,t)=>(e,n)=>t(e,n,i);const _n="rgba(198, 198, 198, 1)",vn="rgba(255, 255, 255, 1)";let fe=class extends a.Disposable{constructor(i,t){super(),this._renderManagerService=i,this._currentUniverService=t,this._initialize(),this._commandExecutedListener()}_initialize(){this._initialRenderRefresh()}_initialRenderRefresh(){this._renderManagerService.currentRender$.subscribe(i=>{var o;if(i==null||this._currentUniverService.getUniverDocInstance(i)==null)return;const t=this._renderManagerService.getRenderById(i);if(t==null)return;const{mainComponent:e}=t,n=e,r=(o=n.getSkeleton())==null?void 0:o.getPageSize();n.onPageRenderObservable.add(s=>{if([a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(i))return;const{page:c,pageLeft:l,pageTop:u,ctx:d}=s,{width:p,pageWidth:h,height:m,pageHeight:g}=c;d.save(),d.translate(l-.5,u-.5),M.Rect.drawWith(d,{width:(r==null?void 0:r.width)??h??p,height:(r==null?void 0:r.height)??g??m,strokeWidth:1,stroke:_n,fill:vn,zIndex:3}),d.restore()})})}_commandExecutedListener(){}};fe=Sn([a.OnLifecycle(a.LifecycleStages.Rendered,fe),Je(0,M.IRenderManagerService),Je(1,E.Inject(a.IUniverInstanceService))],fe);var In=Object.defineProperty,Cn=Object.getOwnPropertyDescriptor,Mn=(i,t,e,n)=>{for(var r=n>1?void 0:n?Cn(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&In(t,e,r),r},j=(i,t)=>(e,n)=>t(e,n,i);let Se=class extends a.Disposable{constructor(t,e,n,r,o,s,c){super();v(this,"_moveInObserver");v(this,"_moveOutObserver");v(this,"_downObserver");v(this,"_dblClickObserver");v(this,"_tripleClickObserver");v(this,"_loadedMap",new Set);this._docSkeletonManagerService=t,this._currentUniverService=e,this._commandService=n,this._renderManagerService=r,this._textSelectionRenderManager=o,this._textSelectionManagerService=s,this._layoutService=c,this._renderManagerService.currentRender$.subscribe(l=>{l!=null&&this._currentUniverService.getUniverDocInstance(l)!=null&&(this._loadedMap.has(l)||(this._initialMain(l),this._loadedMap.add(l)))}),this._initialize()}_initialize(){this._skeletonListener(),this._commandExecutedListener(),this._layoutService&&this.disposeWithMe(this._layoutService.registerContainer(this._textSelectionRenderManager.__getEditorContainer()))}dispose(){this._renderManagerService.getRenderAll().forEach(t=>{const{mainComponent:e}=t;e!=null&&(e.onPointerEnterObserver.remove(this._moveInObserver),e.onPointerLeaveObserver.remove(this._moveOutObserver),e.onPointerDownObserver.remove(this._downObserver),e.onDblclickObserver.remove(this._dblClickObserver),e.onTripleClickObserver.remove(this._tripleClickObserver))})}_initialMain(t){const e=this._getDocObjectById(t);if(e==null)return;const{document:n,scene:r}=e;this._moveInObserver=n.onPointerEnterObserver.add(()=>{n.cursor=M.CURSOR_TYPE.TEXT}),this._moveOutObserver=n.onPointerLeaveObserver.add(()=>{n.cursor=M.CURSOR_TYPE.DEFAULT,r.resetCursor()}),this._downObserver=n==null?void 0:n.onPointerDownObserver.add((o,s)=>{this._currentUniverService.getCurrentUniverDocInstance().getUnitId()!==t&&this._currentUniverService.setCurrentUniverDocInstance(t),this._textSelectionRenderManager.eventTrigger(o),o.button!==2&&s.stopPropagation()}),this._dblClickObserver=n==null?void 0:n.onDblclickObserver.add(o=>{this._textSelectionRenderManager.handleDblClick(o)}),this._tripleClickObserver=n==null?void 0:n.onTripleClickObserver.add(o=>{this._textSelectionRenderManager.handleTripleClick(o)})}_commandExecutedListener(){const t=[V.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{var n;if(t.includes(e.id)){const r=e.params,{unitId:o}=r,s=(n=this._textSelectionManagerService.getCurrentSelection())==null?void 0:n.unitId;if(o!==s)return;this._textSelectionManagerService.refreshSelection()}}))}_skeletonListener(){this._docSkeletonManagerService.currentSkeleton$.subscribe(t=>{if(t==null)return;const{unitId:e,skeleton:n}=t,r=this._renderManagerService.getRenderById(e);if(r==null)return;const{scene:o,mainComponent:s}=r;this._textSelectionRenderManager.changeRuntime(n,o,s),this._textSelectionManagerService.setCurrentSelectionNotRefresh({unitId:e,subUnitId:""})})}_getDocObjectById(t){return gt(t,this._renderManagerService)}};Se=Mn([a.OnLifecycle(a.LifecycleStages.Rendered,Se),j(0,E.Inject(exports.DocSkeletonManagerService)),j(1,a.IUniverInstanceService),j(2,a.ICommandService),j(3,M.IRenderManagerService),j(4,M.ITextSelectionRenderManager),j(5,E.Inject(exports.TextSelectionManagerService)),j(6,E.Optional(D.LayoutService))],Se);var On=Object.defineProperty,Rn=Object.getOwnPropertyDescriptor,yn=(i,t,e,n)=>{for(var r=n>1?void 0:n?Rn(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&On(t,e,r),r},Z=(i,t)=>(e,n)=>t(e,n,i);let _e=class extends a.Disposable{constructor(t,e,n,r,o){super();v(this,"_initializedRender",new Set);this._docSkeletonManagerService=t,this._currentUniverService=e,this._commandService=n,this._renderManagerService=r,this._textSelectionManagerService=o,this._initialize()}dispose(){super.dispose()}_initialize(){this._skeletonListener(),this._commandExecutedListener(),this._initialRenderRefresh()}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeleton$.subscribe(t=>{if(t==null)return;const{unitId:e}=t,n=this._renderManagerService.getRenderById(e);if(n==null||this._initializedRender.has(e)||[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(e))return;this._initializedRender.add(e);const{scene:r}=n;this.disposeWithMe(a.toDisposable(r.onMouseWheelObserver.add(o=>{if(!o.ctrlKey)return;const s=Math.abs(o.deltaX);let c=s<40?.2:s<80?.4:.2;c*=o.deltaY>0?-1:1,r.scaleX<1&&(c/=2);const l=this._currentUniverService.getCurrentUniverDocInstance(),u=l.zoomRatio;let d=+parseFloat(`${u+c}`).toFixed(1);d=d>=4?4:d<=.1?.1:d,this._commandService.executeCommand(ct.id,{zoomRatio:d,unitId:l.getUnitId()}),o.preventDefault()})))})}_skeletonListener(){this.disposeWithMe(a.toDisposable(this._docSkeletonManagerService.currentSkeletonBefore$.subscribe(t=>{if(t==null)return;const n=this._currentUniverService.getCurrentUniverDocInstance().zoomRatio||1;this._updateViewZoom(n,!1)})))}_commandExecutedListener(){const t=[V.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(t.includes(e.id)){const n=this._currentUniverService.getCurrentUniverDocInstance(),r=e.params,{unitId:o}=r;if(o!==n.getUnitId())return;const s=n.zoomRatio||1;this._updateViewZoom(s)}}))}_updateViewZoom(t,e=!0){var r;const n=this._getDocObject();n!=null&&(n.scene.scale(t,t),this._calculatePagePosition(n,t),e&&this._textSelectionManagerService.refreshSelection(),(r=n.scene.getTransformer())==null||r.hideControl())}_calculatePagePosition(t,e){const{document:n,scene:r}=t,o=r==null?void 0:r.getParent(),{width:s,height:c,pageMarginLeft:l,pageMarginTop:u}=n;if(o==null||s===1/0||c===1/0)return;const{width:d,height:p}=o;let h=0,m=0,g=0,f=0,S=1/0;d>(s+l*2)*e?(h=d/2-s*e/2,h/=e,g=(d-l*2)/e,S=0):(h=l,g=s+l*2,S=(g-d/e)/2),p>c?(m=p/2-c/2,f=(p-u*2)/e):(m=u,f=c+u*2),r.resize(g,f+200),n.translate(h,m);const I=r.getViewport(Me.VIEW_MAIN);if(S!==1/0&&I!=null){const C=I.getBarScroll(S,0).x;I.scrollTo({x:C})}return this}_getDocObject(){return Q(this._currentUniverService,this._renderManagerService)}};_e=yn([a.OnLifecycle(a.LifecycleStages.Rendered,_e),Z(0,E.Inject(exports.DocSkeletonManagerService)),Z(1,a.IUniverInstanceService),Z(2,a.ICommandService),Z(3,M.IRenderManagerService),Z(4,E.Inject(exports.TextSelectionManagerService))],_e);const xn={id:Ne.id,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),binding:D.KeyCode.ENTER},Tn={id:we.id,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),binding:D.KeyCode.BACKSPACE},Dn={id:Le.id,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),binding:D.KeyCode.DELETE},bn={id:$.id,binding:D.KeyCode.ARROW_UP,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.UP}},En={id:$.id,binding:D.KeyCode.ARROW_DOWN,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.DOWN}},Un={id:$.id,binding:D.KeyCode.ARROW_LEFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.LEFT}},Pn={id:$.id,binding:D.KeyCode.ARROW_RIGHT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.RIGHT}},Nn={id:F.id,binding:D.KeyCode.ARROW_UP|D.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.UP}},wn={id:F.id,binding:D.KeyCode.ARROW_DOWN|D.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.DOWN}},Ln={id:F.id,binding:D.KeyCode.ARROW_LEFT|D.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.LEFT}},An={id:F.id,binding:D.KeyCode.ARROW_RIGHT|D.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.RIGHT}},Bn={id:lt.id,binding:D.KeyCode.A|D.MetaKeys.CTRL_COMMAND,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.EDITOR_ACTIVATED)};var $n=Object.defineProperty,Fn=Object.getOwnPropertyDescriptor,jn=(i,t,e,n)=>{for(var r=n>1?void 0:n?Fn(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&$n(t,e,r),r},ie=(i,t)=>(e,n)=>t(e,n,i);exports.DocCanvasView=class extends a.RxDisposable{constructor(e,n,r,o){super();v(this,"_scene");v(this,"_currentDocumentModel");v(this,"_loadedMap",new Set);v(this,"_fps$",new L.BehaviorSubject(""));v(this,"fps$",this._fps$.asObservable());this._renderManagerService=e,this._configService=n,this._currentUniverService=r,this._docViewModelManagerService=o,this._initialize()}_initialize(){this._currentUniverService.currentDoc$.pipe(L.takeUntil(this.dispose$)).subscribe(e=>{if(e==null)return;this._currentDocumentModel=e;const n=e.getUnitId();this._loadedMap.has(n)||(this._addNewRender(),this._loadedMap.add(n))})}dispose(){this._fps$.complete()}_addNewRender(){const e=this._currentDocumentModel,n=e.getUnitId(),r=e.getContainer(),o=e.getParentRenderUnitId();if(r!=null&&o!=null)throw new Error("container or parentRenderUnitId can only exist one");r==null&&o!=null?this._renderManagerService.createRenderWithParent(n,o):this._renderManagerService.createRender(n);const s=this._renderManagerService.getRenderById(n);if(s==null)return;const{scene:c,engine:l}=s;c.openTransformer(),this._scene=c;const u=new M.Viewport(Me.VIEW_MAIN,c,{left:0,top:0,bottom:0,right:0,isWheelPreventDefaultX:!0});c.attachControl(),c.on(M.EVENT_TYPE.wheel,(h,m)=>{const g=h;if(g.ctrlKey){const f=Math.abs(g.deltaX);let S=f<40?.2:f<80?.4:.2;S*=g.deltaY>0?-1:1,c.scaleX<1&&(S/=2),c.scaleX+S>4?c.scale(4,4):c.scaleX+S<.1?c.scale(.1,.1):g.preventDefault()}else u.onMouseWheel(g,m)}),this._configService.getConfig("hasScroll")!==!1&&n!==a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY&&new M.ScrollBar(u),c.addLayer(new M.Layer(c,[],De),new M.Layer(c,[],Qe)),this._addComponent(s),this._currentDocumentModel.getShouldRenderLoopImmediately()&&l.runRenderLoop(()=>{c.render(),this._fps$.next(Math.round(l.getFps()).toString())}),this._renderManagerService.setCurrent(n)}_addComponent(e){const n=this._scene,r=this._currentDocumentModel,o=new M.Documents(ve.MAIN,void 0,{pageMarginLeft:r.documentStyle.marginLeft||0,pageMarginTop:r.documentStyle.marginTop||0});o.zIndex=et,e.mainComponent=o,e.components.set(ve.MAIN,o),n.addObjects([o],De)}};exports.DocCanvasView=jn([a.OnLifecycle(a.LifecycleStages.Ready,exports.DocCanvasView),ie(0,M.IRenderManagerService),ie(1,a.IConfigService),ie(2,a.IUniverInstanceService),ie(3,E.Inject(exports.DocViewModelManagerService))],exports.DocCanvasView);var Vn=Object.defineProperty,kn=Object.getOwnPropertyDescriptor,zn=(i,t,e,n)=>{for(var r=n>1?void 0:n?kn(t,e):t,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(t,e,r):s(r))||r);return n&&r&&Vn(t,e,r),r},oe=(i,t)=>(e,n)=>t(e,n,i);const Wn={hasScroll:!0},Gn="docs";var Te;exports.UniverDocsPlugin=(Te=class extends a.Plugin{constructor(e={},n,r,o,s){super(Gn);v(this,"_config");this._injector=n,this._localeService=r,this._configService=o,this._currentUniverService=s,this._config=Object.assign(Wn,e),this._initializeDependencies(n),this._initializeCommands()}initialize(){}_initializeCommands(){[$,F,we,Le,G,ee,te,ne,B,k,X,H,re,Fe,Ne,Re,ye,Pe,$e,Ae,_,ot,st,ct,V,Ue,lt,it,rt,je].forEach(e=>{this._injector.get(a.ICommandService).registerCommand(e)}),[bn,En,Pn,Un,Nn,wn,Ln,An,Bn,Tn,Dn,xn].forEach(e=>{this._injector.get(D.IShortcutService).registerShortcut(e)})}onReady(){this.initialize()}_initializeDependencies(e){[[exports.DocCanvasView],[exports.DocSkeletonManagerService],[exports.DocViewModelManagerService],[Be],[dt,{useClass:Ee}],[M.ITextSelectionRenderManager,{useClass:M.TextSelectionRenderManager}],[exports.TextSelectionManagerService],[de],[fe],[Se],[he],[me],[ge],[le],[pe],[_e],[ue]].forEach(n=>e.add(n))}},v(Te,"type",a.PluginType.Doc),Te);exports.UniverDocsPlugin=zn([oe(1,E.Inject(E.Injector)),oe(2,E.Inject(a.LocaleService)),oe(3,a.IConfigService),oe(4,a.IUniverInstanceService)],exports.UniverDocsPlugin);exports.BreakLineCommand=Ne;exports.BulletListCommand=rt;exports.CoverContentCommand=st;exports.DOCS_COMPONENT_DEFAULT_Z_INDEX=et;exports.DOCS_COMPONENT_HEADER_LAYER_INDEX=Qe;exports.DOCS_COMPONENT_MAIN_LAYER_INDEX=De;exports.DOCS_VIEW_KEY=ve;exports.DeleteCommand=ye;exports.DeleteLeftCommand=we;exports.DeleteRightCommand=Le;exports.DocCopyCommand=se;exports.DocCutCommand=ae;exports.DocPasteCommand=ce;exports.IMEInputCommand=$e;exports.InsertCommand=Re;exports.MoveCursorOperation=$;exports.MoveSelectionOperation=F;exports.NORMAL_TEXT_SELECTION_PLUGIN_NAME=pt;exports.OrderListCommand=it;exports.ReplaceContentCommand=ot;exports.RichTextEditingMutation=_;exports.SetInlineFormatBoldCommand=G;exports.SetInlineFormatCommand=Fe;exports.SetInlineFormatFontFamilyCommand=H;exports.SetInlineFormatFontSizeCommand=X;exports.SetInlineFormatItalicCommand=ee;exports.SetInlineFormatStrikethroughCommand=ne;exports.SetInlineFormatSubscriptCommand=B;exports.SetInlineFormatSuperscriptCommand=k;exports.SetInlineFormatTextColorCommand=re;exports.SetInlineFormatUnderlineCommand=te;exports.SetTextSelectionsOperation=Ue;exports.UpdateCommand=Pe;exports.VIEWPORT_KEY=Me;exports.getDocObject=Q;