@univerjs/docs 0.1.0-beta.5 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,12 +5,22 @@
5
5
 
6
6
  ## Introduction
7
7
 
8
- > Core library for Univer Docs.
8
+ `@univerjs/docs` package provides the fundamental operations for rich text models, including the following capabilities:
9
+
10
+ * Logic for selection areas
11
+ * Commands/mutations for altering rich text data
12
+ * Canvas rendering for document presentation
13
+
14
+ Additionally, `@univerjs/docs` offers support for editors in other domains, such as cell editors for spreadsheets and formula editors, among others.
9
15
 
10
16
  ## Usage
11
17
 
12
18
  ### Installation
13
19
 
14
20
  ```shell
15
- npm i @univerjs/docs
21
+ # Using npm
22
+ npm install @univerjs/docs
23
+
24
+ # Using pnpm
25
+ pnpm add @univerjs/docs
16
26
  ```
package/lib/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var qe=Object.defineProperty;var Ze=(s,r,e)=>r in s?qe(s,r,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[r]=e;var I=(s,r,e)=>(Ze(s,typeof r!="symbol"?r+"":r,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("@univerjs/core"),C=require("@univerjs/engine-render"),N=require("rxjs"),b=require("@wendellhu/redi");function pe(s,r){const t=s.getCurrentUniverDocInstance().getUnitId(),n=r.getRenderById(t);if(n==null)return;const{mainComponent:i,scene:a,engine:c}=n;return{document:i,scene:a,engine:c}}function Je(s,r){const e=r.getRenderById(s);if(e==null)return;const{mainComponent:t,scene:n,engine:i}=e;return{document:t,scene:n,engine:i}}var ge=(s=>(s.MAIN="__Document_Render_Main__",s))(ge||{}),_e=(s=>(s.VIEW_MAIN="viewMain",s.VIEW_TOP="viewTop",s.VIEW_LEFT="viewLeft",s.VIEW_LEFT_TOP="viewLeftTop",s))(_e||{});const Me=0,Le=2,Be=10,Qe="normalTextSelectionPluginName",Ce={id:"doc.operation.set-selections",type:o.CommandType.OPERATION,handler:(s,r)=>!0};var et=Object.defineProperty,tt=Object.getOwnPropertyDescriptor,nt=(s,r,e,t)=>{for(var n=t>1?void 0:t?tt(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&et(r,e,n),n},Ue=(s,r)=>(e,t)=>r(e,t,s);function z(s){const{startOffset:r,endOffset:e,collapsed:t}=s;return{startOffset:r,endOffset:e,collapsed:t}}exports.TextSelectionManagerService=class extends o.RxDisposable{constructor(e,t){super();I(this,"_currentSelection",null);I(this,"_textSelectionInfo",new Map);I(this,"_textSelection$",new N.BehaviorSubject(null));I(this,"textSelection$",this._textSelection$.asObservable());this._textSelectionRenderManager=e,this._commandService=t,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:t,segmentId:n,style:i}=e,a=t.find(h=>h.isActive());if(a==null)return null;const{startOffset:c,endOffset:l,collapsed:u,startNodePosition:d,endNodePosition:m,direction:g}=a;return c==null||l==null?null:{startOffset:c,endOffset:l,collapsed:u,startNodePosition:d,endNodePosition:m,direction:g,segmentId:n,style:i}}add(e){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:e,segmentId:"",style:C.NORMAL_TEXT_SELECTION_PLUGIN_STYLE})}replaceTextRanges(e){this._currentSelection!=null&&(this._textSelectionRenderManager.removeAllTextRanges(),this._textSelectionRenderManager.addTextRanges(e))}_syncSelectionFromRenderService(){this._textSelectionRenderManager.textSelectionInner$.pipe(N.takeUntil(this.dispose$)).subscribe(e=>{e!=null&&this._replaceTextRangesWithNoRefresh(e)})}_replaceTextRangesWithNoRefresh(e){if(this._currentSelection==null)return;const t={...this._currentSelection,...e};this._replaceByParam(t),this._textSelection$.next(t);const{unitId:n,subUnitId:i,segmentId:a,style:c,textRanges:l}=t;this._commandService.executeCommand(Ce.id,{unitId:n,subUnitId:i,segmentId:a,style:c,ranges:l.map(z)})}_getTextRanges(e){var i;if(e==null)return;const{unitId:t,subUnitId:n=""}=e;return(i=this._textSelectionInfo.get(t))==null?void 0:i.get(n)}_refresh(e){const t=this._getTextRanges(e);this._textSelectionRenderManager.removeAllTextRanges(),t&&Array.isArray(t.textRanges)&&t.textRanges.length&&this._textSelectionRenderManager.addTextRanges(t.textRanges.map(z))}_replaceByParam(e){const{unitId:t,subUnitId:n,style:i,segmentId:a,textRanges:c}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map),this._textSelectionInfo.get(t).set(n,{textRanges:c,style:i,segmentId:a})}_addByParam(e){const{unitId:t,subUnitId:n,textRanges:i,style:a,segmentId:c}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map);const l=this._textSelectionInfo.get(t);l.has(n)?l.get(n).textRanges.push(...i):l.set(n,{textRanges:i,style:a,segmentId:c})}};exports.TextSelectionManagerService=nt([Ue(0,C.ITextSelectionRenderManager),Ue(1,o.ICommandService)],exports.TextSelectionManagerService);function Te(s,r="",e=0){const{startOffset:t,endOffset:n}=s,i=[],a=t-e,c=n-e;return a>0&&i.push({t:o.TextXActionType.RETAIN,len:a,segmentId:r}),i.push({t:o.TextXActionType.DELETE,len:c-a,line:0,segmentId:r}),i}var rt=Object.defineProperty,it=Object.getOwnPropertyDescriptor,ot=(s,r,e,t)=>{for(var n=t>1?void 0:t?it(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&rt(r,e,n),n},st=(s,r)=>(e,t)=>r(e,t,s);exports.DocViewModelManagerService=class extends o.RxDisposable{constructor(e){super();I(this,"_currentViewModelUnitId","");I(this,"_docViewModelMap",new Map);I(this,"_currentDocViewModel$",new N.BehaviorSubject(null));I(this,"currentDocViewModel$",this._currentDocViewModel$.asObservable());this._currentUniverService=e,this._initialize()}_initialize(){this._currentUniverService.currentDoc$.pipe(N.takeUntil(this.dispose$)).subscribe(e=>{if(e==null)return;const t=e.getUnitId();this._setCurrent(t)})}dispose(){this._currentDocViewModel$.complete(),this._docViewModelMap.clear()}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 i=this._buildDocViewModel(t);this._docViewModelMap.set(e,{unitId:e,docViewModel:i})}if(e===o.DOCS_NORMAL_EDITOR_UNIT_ID_KEY){const i=(n=this._docViewModelMap.get(e))==null?void 0:n.docViewModel;if(i==null)return;i.reset(t)}this._currentViewModelUnitId=e,this._currentDocViewModel$.next(this.getCurrent())}}_buildDocViewModel(e){return new C.DocumentViewModel(e)}};exports.DocViewModelManagerService=ot([st(0,o.IUniverInstanceService)],exports.DocViewModelManagerService);var at=Object.defineProperty,ct=Object.getOwnPropertyDescriptor,lt=(s,r,e,t)=>{for(var n=t>1?void 0:t?ct(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&at(r,e,n),n},fe=(s,r)=>(e,t)=>r(e,t,s);const dt=300;exports.DocStateChangeManagerService=class extends o.RxDisposable{constructor(e,t,n){super();I(this,"_docStateChange$",new N.BehaviorSubject(null));I(this,"docStateChange$",this._docStateChange$.asObservable());I(this,"_stateCache",new Map);I(this,"_timer",null);this._undoRedoService=e,this._commandService=t,this._univerInstanceService=n,this._initialize()}setChangeState(e){const{trigger:t,noHistory:n}=e;t!=null&&(this._cacheChangeState(e),n||this._docStateChange$.next(e))}_initialize(){this.disposeWithMe(this._commandService.beforeCommandExecuted(e=>{if(e.id===o.UndoCommandId||e.id===o.RedoCommandId){const t=this._univerInstanceService.getCurrentUniverDocInstance();if(t==null)return;const n=t.getUnitId();this._pushHistory(n)}}))}_cacheChangeState(e){const{trigger:t,unitId:n,noHistory:i}=e;if(!(t===o.RedoCommandId||t===o.UndoCommandId||i)){if(this._stateCache.has(n)){const a=this._stateCache.get(n);a==null||a.push(e)}else this._stateCache.set(n,[e]);t===H.id||t===W.id?(this._timer&&clearTimeout(this._timer),this._timer=setTimeout(()=>{this._pushHistory(n)},dt)):this._pushHistory(n)}}_pushHistory(e){const t=this._undoRedoService,n=this._stateCache.get(e);if(!Array.isArray(n)||n.length===0)return;const i=n.length,a=n[0].commandId,c=n[0],l=n[i-1],u={unitId:e,actions:n.reduce((m,g)=>o.TextX.compose(m,g.redoState.actions),[]),textRanges:l.redoState.textRanges},d={unitId:e,actions:n.reverse().reduce((m,g)=>o.TextX.compose(m,g.undoState.actions),[]),textRanges:c.undoState.textRanges};t.pushUndoRedo({unitID:e,undoMutations:[{id:a,params:d}],redoMutations:[{id:a,params:u}]}),n.length=0}};exports.DocStateChangeManagerService=lt([fe(0,b.Inject(o.IUndoRedoService)),fe(1,o.ICommandService),fe(2,o.IUniverInstanceService)],exports.DocStateChangeManagerService);class Y{constructor(){I(this,"_previousActiveRange",null);I(this,"_undoMutationParamsCache",[]);I(this,"_redoMutationParamsCache",[])}clearUndoRedoMutationParamsCache(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[]}getUndoRedoMutationParamsCache(){return{undoCache:this._undoMutationParamsCache,redoCache:this._redoMutationParamsCache}}setUndoRedoMutationParamsCache({undoCache:r=[],redoCache:e=[]}){this._undoMutationParamsCache=r,this._redoMutationParamsCache=e}getActiveRange(){return this._previousActiveRange}setActiveRange(r){this._previousActiveRange=r}pushUndoRedoMutationParams(r,e){this._undoMutationParamsCache.push(r),this._redoMutationParamsCache.push(e)}fetchComposedUndoRedoMutationParams(){if(this._undoMutationParamsCache.length===0||this._previousActiveRange==null||this._redoMutationParamsCache.length===0)return null;const{unitId:r}=this._undoMutationParamsCache[0],e={unitId:r,actions:this._undoMutationParamsCache.reverse().reduce((n,i)=>o.TextX.compose(n,i.actions),[]),textRanges:[]};return{redoMutationParams:{unitId:r,actions:this._redoMutationParamsCache.reduce((n,i)=>o.TextX.compose(n,i.actions),[]),textRanges:[]},undoMutationParams:e,previousActiveRange:this._previousActiveRange}}dispose(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[],this._previousActiveRange=null}}const Pe="doc.mutation.rich-text-editing",U={id:Pe,type:o.CommandType.MUTATION,handler:(s,r)=>{var x;const{unitId:e,actions:t,textRanges:n,prevTextRanges:i,trigger:a,noHistory:c,isCompositionEnd:l,noNeedSetTextRange:u}=r,m=s.get(o.IUniverInstanceService).getUniverDocInstance(e),h=s.get(exports.DocViewModelManagerService).getViewModel(e),p=s.get(exports.TextSelectionManagerService),S=((x=p.getSelections())!=null?x:[]).map(z),f=s.get(exports.DocStateChangeManagerService),v=s.get(Y);if(m==null||h==null)throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${e}`);if(t.length===0)throw new Error("Mutation's length should great than 0 when call RichTextEditingMutation");const T=m.apply(t),{segmentId:O}=t[0],R=m.getSelfOrHeaderFooterModel(O);h.getSelfOrHeaderFooterViewModel(O).reset(R),!u&&n&&queueMicrotask(()=>{p.replaceTextRanges(n)});const D={commandId:Pe,unitId:e,trigger:a,noHistory:c,redoState:{actions:t,textRanges:n},undoState:{actions:T,textRanges:i!=null?i:S}};if(l){const y=v.fetchComposedUndoRedoMutationParams();if(y==null)throw new Error("historyParams is null in RichTextEditingMutation");const{undoMutationParams:M,redoMutationParams:A,previousActiveRange:P}=y;D.redoState.actions=A.actions,D.undoState.actions=M.actions,D.undoState.textRanges=[P]}return f.setChangeState(D),{unitId:e,actions:T,textRanges:S}}},H={id:"doc.command.insert-text",type:o.CommandType.COMMAND,handler:async(s,r)=>{const e=s.get(o.ICommandService),{range:t,segmentId:n,body:i,unitId:a,textRanges:c}=r,{startOffset:l,collapsed:u}=t,d={id:U.id,params:{unitId:a,actions:[],textRanges:c}},m=new o.TextX;return u?l>0&&m.push({t:o.TextXActionType.RETAIN,len:l,segmentId:n}):m.push(...Te(t,n)),m.push({t:o.TextXActionType.INSERT,body:i,len:i.dataStream.length,line:0,segmentId:n}),d.params.actions=m.serialize(),!!e.syncExecuteCommand(d.id,d.params)}};var w=(s=>(s[s.LEFT=0]="LEFT",s[s.RIGHT=1]="RIGHT",s))(w||{});const W={id:"doc.command.delete-text",type:o.CommandType.COMMAND,handler:async(s,r)=>{const e=s.get(o.ICommandService),{range:t,segmentId:n,unitId:i,direction:a,textRanges:c,len:l=1}=r,{startOffset:u}=t,d={id:U.id,params:{unitId:i,actions:[],textRanges:c}},m=new o.TextX;return u>0&&m.push({t:o.TextXActionType.RETAIN,len:a===0?u-l:u,segmentId:n}),m.push({t:o.TextXActionType.DELETE,len:l,line:0,segmentId:n}),d.params.actions=m.serialize(),!!e.syncExecuteCommand(d.id,d.params)}},Oe={id:"doc.command.update-text",type:o.CommandType.COMMAND,handler:async(s,r)=>{const{range:e,segmentId:t,updateBody:n,coverType:i,unitId:a,textRanges:c}=r,l=s.get(o.ICommandService),u={id:U.id,params:{unitId:a,actions:[],textRanges:c}},d=new o.TextX,{startOffset:m,endOffset:g}=e;return d.push({t:o.TextXActionType.RETAIN,len:m,segmentId:t}),d.push({t:o.TextXActionType.RETAIN,body:n,len:g-m,segmentId:t,coverType:i}),u.params.actions=d.serialize(),!!l.syncExecuteCommand(u.id,u.params)}};function ut(s,r){const e=[];for(let t=0,n=s.length;t<n;t++)s[t]===o.DataStreamTreeTokenType.PARAGRAPH&&e.push({startIndex:t});if(r)for(const t of e)r.bullet&&(t.bullet=o.Tools.deepClone(r.bullet)),r.paragraphStyle&&(t.paragraphStyle=o.Tools.deepClone(r.paragraphStyle));return e}const $e={id:"doc.command.break-line",type:o.CommandType.COMMAND,handler:async s=>{var p,_;const r=s.get(exports.TextSelectionManagerService),e=s.get(o.IUniverInstanceService),t=s.get(o.ICommandService),n=r.getActiveRange();if(n==null)return!1;const i=e.getCurrentUniverDocInstance(),a=i.getUnitId(),{startOffset:c,segmentId:l,style:u}=n,d=[{startOffset:c+1,endOffset:c+1,style:u}],g=((_=(p=i.getBody())==null?void 0:p.paragraphs)!=null?_:[]).find(S=>S.startIndex>=c);return await t.executeCommand(H.id,{unitId:a,body:{dataStream:o.DataStreamTreeTokenType.PARAGRAPH,paragraphs:ut(o.DataStreamTreeTokenType.PARAGRAPH,g)},range:n,textRanges:d,segmentId:l})}};var mt=Object.defineProperty,gt=Object.getOwnPropertyDescriptor,ht=(s,r,e,t)=>{for(var n=t>1?void 0:t?gt(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&mt(r,e,n),n},Ne=(s,r)=>(e,t)=>r(e,t,s);exports.DocSkeletonManagerService=class extends o.RxDisposable{constructor(e,t){super();I(this,"_currentSkeletonUnitId","");I(this,"_docSkeletonMap",new Map);I(this,"_currentSkeleton$",new N.BehaviorSubject(null));I(this,"currentSkeleton$",this._currentSkeleton$.asObservable());I(this,"_currentSkeletonBefore$",new N.BehaviorSubject(null));I(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());this._localeService=e,this._docViewModelManagerService=t,this._initialize()}_initialize(){this._docViewModelManagerService.currentDocViewModel$.pipe(N.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,t=!0){const n=this.getSkeletonByUnitId(e);n!=null&&(n.dirty=t)}getSkeletonByUnitId(e){return this._docSkeletonMap.get(e)}_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()}_buildSkeleton(e){return C.DocumentSkeleton.create(e,this._localeService)}};exports.DocSkeletonManagerService=ht([Ne(0,b.Inject(o.LocaleService)),Ne(1,b.Inject(exports.DocViewModelManagerService))],exports.DocSkeletonManagerService);const St={id:"doc.command.inner-paste",type:o.CommandType.COMMAND,handler:async(s,r)=>{const{segmentId:e,body:t,textRanges:n}=r,i=s.get(o.ICommandService),a=s.get(exports.TextSelectionManagerService),c=s.get(o.IUniverInstanceService),l=a.getSelections();if(!Array.isArray(l)||l.length===0)return!1;const d=c.getCurrentUniverDocInstance().getUnitId(),m={id:U.id,params:{unitId:d,actions:[],textRanges:n}},g=new o.MemoryCursor;g.reset();const h=new o.TextX;for(const _ of l){const{startOffset:S,endOffset:f,collapsed:v}=_,T=S-g.cursor;v?h.push({t:o.TextXActionType.RETAIN,len:T,segmentId:e}):h.push(...Te(_,e,g.cursor)),h.push({t:o.TextXActionType.INSERT,body:t,len:t.dataStream.length,line:0,segmentId:e}),g.reset(),g.moveCursor(f)}return m.params.actions=h.serialize(),!!i.syncExecuteCommand(m.id,m.params)}},Re={id:"doc.command.inner-cut",type:o.CommandType.COMMAND,handler:async(s,r)=>{const{segmentId:e,textRanges:t}=r,n=s.get(o.ICommandService),i=s.get(exports.TextSelectionManagerService),a=s.get(o.IUniverInstanceService),c=i.getSelections();if(!Array.isArray(c)||c.length===0)return!1;const l=a.getCurrentUniverDocInstance().getUnitId(),u=a.getUniverDocInstance(l),d=o.getDocsUpdateBody(u.snapshot,e);if(d==null)return!1;const m={id:U.id,params:{unitId:l,actions:[],textRanges:t}},g=new o.MemoryCursor;g.reset();const h=new o.TextX;for(const _ of c){const{startOffset:S,endOffset:f,collapsed:v}=_,T=S-g.cursor;v?h.push({t:o.TextXActionType.RETAIN,len:T,segmentId:e}):h.push(...pt(_,d,e,g.cursor)),g.reset(),g.moveCursor(f)}return m.params.actions=h.serialize(),!!n.syncExecuteCommand(m.id,m.params)}};function pt(s,r,e="",t=0){const{startOffset:n,endOffset:i}=s,a=[],{paragraphs:c=[]}=r,l=n-t,u=i-t,d=c==null?void 0:c.find(m=>m.startIndex-t>=l&&m.startIndex-t<=u);if(l>0&&a.push({t:o.TextXActionType.RETAIN,len:l,segmentId:e}),d&&d.startIndex-t>l){const m=d.startIndex-t;a.push({t:o.TextXActionType.DELETE,len:m-l,line:0,segmentId:e}),a.push({t:o.TextXActionType.RETAIN,len:1,segmentId:e}),u>m+1&&a.push({t:o.TextXActionType.DELETE,len:u-m-1,line:0,segmentId:e})}else a.push({t:o.TextXActionType.DELETE,len:u-l,line:0,segmentId:e});return a}const je={id:"doc.command.delete-left",type:o.CommandType.COMMAND,handler:async s=>{var O;const r=s.get(exports.TextSelectionManagerService),e=s.get(exports.DocSkeletonManagerService),t=s.get(o.IUniverInstanceService),n=s.get(o.ICommandService),i=r.getActiveRange(),a=r.getSelections(),c=(O=e.getCurrent())==null?void 0:O.skeleton;let l=!0;if(i==null||c==null||a==null)return!1;const u=t.getCurrentUniverDocInstance(),{startOffset:d,collapsed:m,segmentId:g,style:h}=i,p=c.findNodeByCharIndex(d),_=C.hasListSpan(p),S=C.isIndentBySpan(p,u.getBody());let f=d;const v=c.findNodeByCharIndex(d-1);if(C.isFirstSpan(p)&&v!==p&&(_===!0||S===!0)){const R=C.getParagraphBySpan(p,u.getBody());if(R==null)return!1;const E=R==null?void 0:R.startIndex,D={startIndex:0},x=R.paragraphStyle;if(_===!0){const M=R.paragraphStyle;M&&(D.paragraphStyle=M)}else if(S===!0){const M=R.bullet;M&&(D.bullet=M),x!=null&&(D.paragraphStyle={...x},delete D.paragraphStyle.hanging,delete D.paragraphStyle.indentStart)}const y=[{startOffset:f,endOffset:f,style:h}];l=await n.executeCommand(Oe.id,{unitId:u.getUnitId(),updateBody:{dataStream:"",paragraphs:[{...D}]},range:{startOffset:E,endOffset:E+1},textRanges:y,coverType:o.UpdateDocsAttributeType.REPLACE,segmentId:g})}else if(m===!0){if(v==null)return!0;if(v.content==="\r")l=await n.executeCommand(xe.id,{direction:w.LEFT,range:i});else{f-=v.count;const R=[{startOffset:f,endOffset:f,style:h}];l=await n.executeCommand(W.id,{unitId:u.getUnitId(),range:i,segmentId:g,direction:w.LEFT,len:v.count,textRanges:R})}}else{const R=Ve(i,a);l=await n.executeCommand(Re.id,{segmentId:g,textRanges:R})}return l}},Fe={id:"doc.command.delete-right",type:o.CommandType.COMMAND,handler:async s=>{var p;const r=s.get(exports.TextSelectionManagerService),e=s.get(exports.DocSkeletonManagerService),t=s.get(o.IUniverInstanceService),n=s.get(o.ICommandService),i=r.getActiveRange(),a=r.getSelections(),c=(p=e.getCurrent())==null?void 0:p.skeleton;let l;if(i==null||c==null||a==null)return!1;const u=t.getCurrentUniverDocInstance(),{startOffset:d,collapsed:m,segmentId:g,style:h}=i;if(d===u.getBody().dataStream.length-2&&m)return!0;if(m===!0){const _=c.findNodeByCharIndex(d);if(_.content==="\r")l=await n.executeCommand(xe.id,{direction:w.RIGHT,range:i});else{const S=[{startOffset:d,endOffset:d,style:h}];l=await n.executeCommand(W.id,{unitId:u.getUnitId(),range:i,segmentId:g,direction:w.RIGHT,textRanges:S,len:_.count})}}else{const _=Ve(i,a);l=await n.executeCommand(Re.id,{segmentId:g,textRanges:_})}return l}},xe={id:"doc.command.merge-two-paragraph",type:o.CommandType.COMMAND,handler:async(s,r)=>{var D,x,y;const e=s.get(exports.TextSelectionManagerService),t=s.get(o.IUniverInstanceService),n=s.get(o.ICommandService);s.get(o.IUndoRedoService);const{direction:i,range:a}=r,c=e.getActiveRange(),l=e.getSelections();if(c==null||l==null)return!1;const u=t.getCurrentUniverDocInstance(),{startOffset:d,collapsed:m,segmentId:g,style:h}=c;if(!m)return!1;const p=i===w.LEFT?d:d+1,_=(y=(x=(D=u.getBody())==null?void 0:D.paragraphs)==null?void 0:x.find(M=>M.startIndex>=p))==null?void 0:y.startIndex,S=_t(u.getBody(),p,_),f=i===w.LEFT?d-1:d,v=u.getUnitId(),T=[{startOffset:f,endOffset:f,style:h}],O={id:U.id,params:{unitId:v,actions:[],textRanges:T,prevTextRanges:[a]}},R=new o.TextX;return R.push({t:o.TextXActionType.RETAIN,len:i===w.LEFT?d-1:d,segmentId:g}),S.dataStream.length&&R.push({t:o.TextXActionType.INSERT,body:S,len:S.dataStream.length,line:0,segmentId:g}),R.push({t:o.TextXActionType.RETAIN,len:1,segmentId:g}),R.push({t:o.TextXActionType.DELETE,len:_+1-p,line:0,segmentId:g}),O.params.actions=R.serialize(),!!n.syncExecuteCommand(O.id,O.params)}};function _t(s,r,e){const{textRuns:t}=s,n=s.dataStream.substring(r,e);if(t==null)return{dataStream:n};const i=[];for(const a of t){const{st:c,ed:l}=a;l<=r||c>=e||(c<r?i.push({...a,st:0,ed:l-r}):l>e?i.push({...a,st:c-r,ed:e-r}):i.push({...a,st:c-r,ed:l-r}))}return{dataStream:n,textRuns:i}}function Ve(s,r){let e=s.endOffset;for(const n of r){const{startOffset:i,endOffset:a}=n;i==null||a==null||a<=s.endOffset&&(e-=a-i)}return[{startOffset:e,endOffset:e,style:s.style}]}const ye={id:"doc.command.ime-input",type:o.CommandType.COMMAND,handler:async(s,r)=>{const{unitId:e,newText:t,oldTextLen:n,isCompositionEnd:i,isCompositionStart:a}=r,c=s.get(o.ICommandService),l=s.get(Y),u=l.getActiveRange();if(u==null)return!1;const{startOffset:d,style:m,segmentId:g}=u,h=t.length,p=[{startOffset:d+h,endOffset:d+h,collapsed:!0,style:m}],_={id:U.id,params:{unitId:e,actions:[],textRanges:p}},S=new o.TextX;!u.collapsed&&a?S.push(...Te(u,g)):S.push({t:o.TextXActionType.RETAIN,len:d,segmentId:g}),n>0&&S.push({t:o.TextXActionType.DELETE,len:n,line:0,segmentId:g}),S.push({t:o.TextXActionType.INSERT,body:{dataStream:t},len:t.length,line:0,segmentId:g}),_.params.actions=S.serialize(),_.params.noHistory=!i,_.params.isCompositionEnd=i;const f=c.syncExecuteCommand(_.id,_.params);return l.pushUndoRedoMutationParams(f,_.params),!!f}},G={id:"doc.command.set-inline-format-bold",type:o.CommandType.COMMAND,handler:async()=>!0},K={id:"doc.command.set-inline-format-italic",type:o.CommandType.COMMAND,handler:async()=>!0},q={id:"doc.command.set-inline-format-underline",type:o.CommandType.COMMAND,handler:async()=>!0},Z={id:"doc.command.set-inline-format-strikethrough",type:o.CommandType.COMMAND,handler:async()=>!0},B={id:"doc.command.set-inline-format-subscript",type:o.CommandType.COMMAND,handler:async()=>!0},J={id:"doc.command.set-inline-format-superscript",type:o.CommandType.COMMAND,handler:async()=>!0},Q={id:"doc.command.set-inline-format-fontsize",type:o.CommandType.COMMAND,handler:async()=>!0},ee={id:"doc.command.set-inline-format-font-family",type:o.CommandType.COMMAND,handler:async()=>!0},te={id:"doc.command.set-inline-format-text-color",type:o.CommandType.COMMAND,handler:async()=>!0},ke={[G.id]:"bl",[K.id]:"it",[q.id]:"ul",[Z.id]:"st",[Q.id]:"fs",[ee.id]:"ff",[te.id]:"cl",[B.id]:"va",[J.id]:"va"},De={id:"doc.command.set-inline-format",type:o.CommandType.COMMAND,handler:async(s,r)=>{const{segmentId:e,value:t,preCommandId:n}=r,i=s.get(o.ICommandService),a=s.get(exports.TextSelectionManagerService),c=s.get(o.IUniverInstanceService),l=a.getSelections();if(!Array.isArray(l)||l.length===0)return!1;let u=c.getCurrentUniverDocInstance(),d=u.getUnitId();d===o.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY&&(u=c.getUniverDocInstance(o.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),d=u.getUnitId());let m;switch(n){case G.id:case K.id:case q.id:case Z.id:case B.id:case J.id:{m=vt(u.getBody().textRuns,n,l);break}case Q.id:case ee.id:{m=t;break}case te.id:{m={rgb:t};break}default:throw new Error(`Unknown command: ${n} in handleInlineFormat`)}const g={id:U.id,params:{unitId:d,actions:[],textRanges:l}},h=new o.TextX,p=new o.MemoryCursor;p.reset();for(const S of l){const{startOffset:f,endOffset:v}=S,T={dataStream:"",textRuns:[{st:0,ed:v-f,ts:{[ke[n]]:m}}]},O=f-p.cursor;O!==0&&h.push({t:o.TextXActionType.RETAIN,len:O,segmentId:e}),h.push({t:o.TextXActionType.RETAIN,body:T,len:v-f,segmentId:e}),p.reset(),p.moveCursor(v)}return g.params.actions=h.serialize(),!!i.syncExecuteCommand(g.id,g.params)}};function ft(s){return s!==null&&typeof s=="object"}function vt(s,r,e){let t=0,n=0;const i=ke[r];for(;t!==s.length&&n!==e.length;){const{startOffset:a,endOffset:c}=e[n],{st:l,ed:u,ts:d}=s[t];if(c<=l)n++;else if(u<=a)t++;else{if(/bl|it/.test(i))return(d==null?void 0:d[i])===o.BooleanNumber.TRUE?o.BooleanNumber.FALSE:o.BooleanNumber.TRUE;if(/ul|st/.test(i))return ft(d==null?void 0:d[i])&&(d==null?void 0:d[i]).s===o.BooleanNumber.TRUE?{s:o.BooleanNumber.FALSE}:{s:o.BooleanNumber.TRUE};if(/va/.test(i))return r===B.id?(d==null?void 0:d[i])===o.BaselineOffset.SUBSCRIPT?o.BaselineOffset.NORMAL:o.BaselineOffset.SUBSCRIPT:(d==null?void 0:d[i])===o.BaselineOffset.SUPERSCRIPT?o.BaselineOffset.NORMAL:o.BaselineOffset.SUPERSCRIPT;t++}}return/bl|it/.test(i)?o.BooleanNumber.TRUE:/ul|st/.test(i)?{s:o.BooleanNumber.TRUE}:r===B.id?o.BaselineOffset.SUBSCRIPT:o.BaselineOffset.SUPERSCRIPT}const be={id:"doc.command.list-operation",type:o.CommandType.COMMAND,handler:(s,r)=>{var R,E,D;const e=s.get(exports.TextSelectionManagerService),t=s.get(o.IUniverInstanceService),n=s.get(o.ICommandService),{listType:i}=r,a=t.getCurrentUniverDocInstance(),c=e.getActiveRange(),l=(R=e.getSelections())!=null?R:[],u=(E=a.getBody())==null?void 0:E.paragraphs,d=l.map(z);if(c==null||u==null)return!1;const m=It(c,u),{segmentId:g}=c,h=a.getUnitId(),p=m.every(x=>{var y;return((y=x.bullet)==null?void 0:y.listType)===i});let S=o.Tools.generateRandomId(6);if(m.length===1){const x=u.indexOf(m[0]),y=u[x-1],M=u[x+1];y&&y.bullet&&y.bullet.listType===i?S=y.bullet.listId:M&&M.bullet&&M.bullet.listType===i&&(S=M.bullet.listId)}const f={id:U.id,params:{unitId:h,actions:[],textRanges:d}},v=new o.MemoryCursor;v.reset();const T=new o.TextX;for(const x of m){const{startIndex:y}=x;T.push({t:o.TextXActionType.RETAIN,len:y-v.cursor,segmentId:g});const M={...x.paragraphStyle,hanging:void 0,indentStart:void 0};T.push({t:o.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[p?{paragraphStyle:M,startIndex:0}:{...x,startIndex:0,bullet:{...(D=x.bullet)!=null?D:{nestingLevel:0,textStyle:{fs:20}},listType:i,listId:S}}]},segmentId:g,coverType:o.UpdateDocsAttributeType.REPLACE}),v.moveCursorTo(y+1)}return f.params.actions=T.serialize(),!!n.syncExecuteCommand(f.id,f.params)}},Xe={id:"doc.command.bullet-list",type:o.CommandType.COMMAND,handler:s=>s.get(o.ICommandService).syncExecuteCommand(be.id,{listType:o.PresetListType.BULLET_LIST})},ze={id:"doc.command.order-list",type:o.CommandType.COMMAND,handler:s=>s.get(o.ICommandService).syncExecuteCommand(be.id,{listType:o.PresetListType.ORDER_LIST})};function It(s,r){const{startOffset:e,endOffset:t}=s,n=[];let i=-1;for(const a of r){const{startIndex:c}=a;(e>i&&e<=c||t>i&&t<=c||c>=e&&c<=t)&&n.push(a),i=c}return n}const Ye={id:"doc.command-replace-content",type:o.CommandType.COMMAND,handler:async(s,r)=>{var h;const{unitId:e,body:t,textRanges:n,segmentId:i=""}=r,a=s.get(o.IUniverInstanceService),c=s.get(o.ICommandService),l=s.get(exports.TextSelectionManagerService),u=(h=a.getUniverDocInstance(e))==null?void 0:h.getSnapshot().body,d=l.getSelections();if(u==null||!Array.isArray(d)||d.length===0)return!1;const m=We(e,i,u,t);return m.params.textRanges=n,!!c.syncExecuteCommand(m.id,m.params)}},He={id:"doc.command-cover-content",type:o.CommandType.COMMAND,handler:async(s,r)=>{var d;const{unitId:e,body:t,segmentId:n=""}=r,i=s.get(o.IUniverInstanceService),a=s.get(o.ICommandService),c=s.get(o.IUndoRedoService),l=(d=i.getUniverDocInstance(e))==null?void 0:d.getSnapshot().body;if(l==null)return!1;const u=We(e,n,l,t);return u.params.noNeedSetTextRange=!0,u.params.noHistory=!0,a.syncExecuteCommand(u.id,u.params),c.clearUndoRedo(e),!0}};function We(s,r,e,t){const n={id:U.id,params:{unitId:s,actions:[],textRanges:[]}},i=new o.TextX,a=(e==null?void 0:e.dataStream.length)-2;return a>0&&i.push({t:o.TextXActionType.DELETE,len:a,line:0,segmentId:r}),t.dataStream.length>0&&i.push({t:o.TextXActionType.INSERT,body:t,len:t.dataStream.length,line:0,segmentId:r}),n.params.actions=i.serialize(),n}const he={id:"doc.operation.move-cursor",type:o.CommandType.OPERATION,handler:(s,r)=>!!r},Se={id:"doc.operation.move-selection",type:o.CommandType.OPERATION,handler:(s,r)=>!!r},Mt=(s,r)=>{const e=s.get(o.IUniverInstanceService).getUniverDocInstance(r.unitId),t=(e==null?void 0:e.zoomRatio)||1;return{...o.Tools.deepClone(r),zoomRatio:t}},L={id:"doc.operation.set-zoom-ratio",type:o.CommandType.OPERATION,handler:(s,r)=>{const e=s.get(o.IUniverInstanceService).getUniverDocInstance(r.unitId);if(!e)return!1;const t=e.getSnapshot();return t.settings==null?t.settings={zoomRatio:r.zoomRatio}:t.settings.zoomRatio=r.zoomRatio,!0}},Ge={type:o.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(s,r)=>{var m,g;const e=s.get(o.ICommandService),t=s.get(o.IUndoRedoService),n=s.get(o.IUniverInstanceService);let i=n.getCurrentUniverDocInstance().getUnitId(),a=1;if(r&&(i=(m=r.documentId)!=null?m:i,a=(g=r.zoomRatio)!=null?g:a),!n.getUniverDocInstance(i))return!1;const l={zoomRatio:a,unitId:i},u=Mt(s,l);return e.syncExecuteCommand(L.id,l)?(t.pushUndoRedo({unitID:i,undoMutations:[{id:L.id,params:u}],redoMutations:[{id:L.id,params:l}]}),!0):!1}},Ke={id:"doc.operation.select-all",type:o.CommandType.COMMAND,handler:async s=>{const r=s.get(o.IUniverInstanceService),e=s.get(exports.TextSelectionManagerService),t=r.getCurrentUniverDocInstance().getSnapshot().body;if(t==null)return!1;const n=[{startOffset:0,endOffset:t.dataStream.length-2}];return e.replaceTextRanges(n),!0}};var Ct=Object.defineProperty,Tt=Object.getOwnPropertyDescriptor,Ot=(s,r,e,t)=>{for(var n=t>1?void 0:t?Tt(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&Ct(r,e,n),n},ve=(s,r)=>(e,t)=>r(e,t,s);let ie=class extends o.RxDisposable{constructor(s,r,e){super(),this._docSkeletonManagerService=s,this._renderManagerService=r,this._commandService=e,this._initialRenderRefresh(),this._commandExecutedListener()}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeletonBefore$.pipe(N.takeUntil(this.dispose$)).subscribe(s=>{if(s==null)return;const{skeleton:r,unitId:e}=s,t=this._renderManagerService.getRenderById(e);if(t==null)return;const{mainComponent:n}=t;n.changeSkeleton(r),this._recalculateSizeBySkeleton(t,r)})}_recalculateSizeBySkeleton(s,r){var u;const{mainComponent:e,scene:t}=s,n=e,i=(u=r.getSkeletonData())==null?void 0:u.pages;if(i==null)return;let a=0,c=0;for(let d=0,m=i.length;d<m;d++){const g=i[d],{pageWidth:h,pageHeight:p}=g;n.pageLayoutType===C.PageLayoutType.VERTICAL?(c+=p,c+=n.pageMarginTop,d===m-1&&(c+=n.pageMarginTop),a=Math.max(a,h)):n.pageLayoutType===C.PageLayoutType.HORIZONTAL&&(a+=h,d!==m-1&&(a+=n.pageMarginLeft),c=Math.max(c,p))}n.resize(a,c),[o.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,o.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(s.unitId)||t.resize(a,c)}_commandExecutedListener(){const s=[U.id],r=[o.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,o.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{var t;if(s.includes(e.id)){const n=e.params,{unitId:i}=n,a=this._docSkeletonManagerService.getSkeletonByUnitId(i);if(a==null)return;const{skeleton:c}=a,l=this._renderManagerService.getRenderById(i);if(l==null)return;if(c.calculate(),r.includes(i)){(t=l.mainComponent)==null||t.makeDirty();return}this._recalculateSizeBySkeleton(l,c)}}))}};ie=Ot([o.OnLifecycle(o.LifecycleStages.Rendered,ie),ve(0,b.Inject(exports.DocSkeletonManagerService)),ve(1,C.IRenderManagerService),ve(2,o.ICommandService)],ie);var Rt=Object.defineProperty,xt=Object.getOwnPropertyDescriptor,yt=(s,r,e,t)=>{for(var n=t>1?void 0:t?xt(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&Rt(r,e,n),n},j=(s,r)=>(e,t)=>r(e,t,s);let oe=class extends o.Disposable{constructor(r,e,t,n,i){super();I(this,"_liquid",new C.Liquid);I(this,"_pageMarginCache",new Map);this._docSkeletonManagerService=r,this._currentUniverService=e,this._renderManagerService=t,this._commandService=n,this._floatingObjectManagerService=i,this._initialize(),this._commandExecutedListener()}_initialize(){this._initialRenderRefresh(),this._updateOnPluginChange()}_updateOnPluginChange(){this._floatingObjectManagerService.pluginUpdate$.subscribe(r=>{const e=this._docSkeletonManagerService.getCurrent();if(e==null)return;const{unitId:t,skeleton:n}=e,i=this._renderManagerService.getRenderById(t);if(i==null)return;const{mainComponent:a,components:c,scene:l}=i,u=a,{left:d,top:m}=u;r.forEach(g=>{const{unitId:h,subUnitId:p,floatingObjectId:_,floatingObject:S}=g,{left:f=0,top:v=0,width:T=0,height:O=0,angle:R,flipX:E,flipY:D,skewX:x,skewY:y}=S,M=this._pageMarginCache.get(_),A=(M==null?void 0:M.marginLeft)||0,P=(M==null?void 0:M.marginTop)||0;n==null||n.getViewModel().getDataModel().updateDrawing(_,{left:f-d-A,top:v-m-P,height:O,width:T})}),n==null||n.calculate(),a==null||a.makeDirty()})}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeleton$.subscribe(r=>{if(r==null)return;const{skeleton:e,unitId:t}=r,n=this._renderManagerService.getRenderById(t);if(n==null)return;const{mainComponent:i}=n;i.changeSkeleton(e),this._refreshFloatingObject(t,e,n)})}_commandExecutedListener(){const r=[U.id,L.id],e=[o.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,o.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{var n;if(r.includes(t.id)){const i=t.params,{unitId:a}=i,c=this._docSkeletonManagerService.getCurrent();if(c==null)return;const{unitId:l,skeleton:u}=c;if(a!==l)return;const d=this._renderManagerService.getRenderById(l);if(d==null)return;if(e.includes(l)){(n=d.mainComponent)==null||n.makeDirty();return}this._refreshFloatingObject(l,u,d)}}))}_refreshFloatingObject(r,e,t){const n=e==null?void 0:e.getSkeletonData(),{mainComponent:i,scene:a}=t,c=i;if(!n)return;const{left:l,top:u,pageLayoutType:d,pageMarginLeft:m,pageMarginTop:g}=c,{pages:h}=n,p=[];a.getAncestorScale(),this._liquid.reset(),this._pageMarginCache.clear();for(let _=0,S=h.length;_<S;_++){const f=h[_],{skeDrawings:v,marginLeft:T,marginTop:O}=f;this._liquid.translatePagePadding(f),v.forEach(R=>{const{aLeft:E,aTop:D,height:x,width:y,objectId:M}=R;p.push({unitId:r,subUnitId:o.DEFAULT_DOCUMENT_SUB_COMPONENT_ID,floatingObjectId:M,floatingObject:{left:E+l+this._liquid.x,top:D+u+this._liquid.y,width:y,height:x}}),this._pageMarginCache.set(M,{marginLeft:this._liquid.x,marginTop:this._liquid.y})}),this._liquid.translatePage(f,d,m,g)}this._floatingObjectManagerService.BatchAddOrUpdate(p)}};oe=yt([o.OnLifecycle(o.LifecycleStages.Steady,oe),j(0,b.Inject(exports.DocSkeletonManagerService)),j(1,o.IUniverInstanceService),j(2,C.IRenderManagerService),j(3,o.ICommandService),j(4,o.IFloatingObjectManagerService)],oe);var Dt=Object.defineProperty,bt=Object.getOwnPropertyDescriptor,Et=(s,r,e,t)=>{for(var n=t>1?void 0:t?bt(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&Dt(r,e,n),n},F=(s,r)=>(e,t)=>r(e,t,s);let se=class extends o.Disposable{constructor(r,e,t,n,i){super();I(this,"_previousIMEContent","");I(this,"_isCompositionStart",!0);I(this,"_onStartSubscription");I(this,"_onUpdateSubscription");I(this,"_onEndSubscription");this._docSkeletonManagerService=r,this._currentUniverService=e,this._textSelectionRenderManager=t,this._imeInputManagerService=n,this._commandService=i,this._initialize()}dispose(){var r,e,t;(r=this._onStartSubscription)==null||r.unsubscribe(),(e=this._onUpdateSubscription)==null||e.unsubscribe(),(t=this._onEndSubscription)==null||t.unsubscribe()}_initialize(){this._initialOnCompositionstart(),this._initialOnCompositionUpdate(),this._initialOnCompositionend()}_initialOnCompositionstart(){this._onStartSubscription=this._textSelectionRenderManager.onCompositionstart$.subscribe(r=>{if(r==null)return;const{activeRange:e}=r;e!=null&&(this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(o.Tools.deepClone(e)),this._isCompositionStart=!0)})}_initialOnCompositionUpdate(){this._onUpdateSubscription=this._textSelectionRenderManager.onCompositionupdate$.subscribe(async r=>{this._updateContent(r,!0)})}_initialOnCompositionend(){this._onEndSubscription=this._textSelectionRenderManager.onCompositionend$.subscribe(r=>{this._updateContent(r,!1)})}async _updateContent(r,e){var u;const t=(u=this._docSkeletonManagerService.getCurrent())==null?void 0:u.skeleton;if(r==null||t==null)return;const n=this._currentUniverService.getCurrentUniverDocInstance(),{event:i,activeRange:a}=r;if(t==null||a==null)return;const l=i.data;l===this._previousIMEContent&&e||(await this._commandService.executeCommand(ye.id,{unitId:n.getUnitId(),newText:l,oldTextLen:this._previousIMEContent.length,isCompositionStart:this._isCompositionStart,isCompositionEnd:!e}),e?(this._isCompositionStart&&(this._isCompositionStart=!1),this._previousIMEContent=l):this._resetIME())}_resetIME(){this._previousIMEContent="",this._isCompositionStart=!0,this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(null)}};se=Et([o.OnLifecycle(o.LifecycleStages.Rendered,se),F(0,b.Inject(exports.DocSkeletonManagerService)),F(1,o.IUniverInstanceService),F(2,C.ITextSelectionRenderManager),F(3,b.Inject(Y)),F(4,o.ICommandService)],se);var Ut=Object.defineProperty,Pt=Object.getOwnPropertyDescriptor,Nt=(s,r,e,t)=>{for(var n=t>1?void 0:t?Pt(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&Ut(r,e,n),n},Ae=(s,r)=>(e,t)=>r(e,t,s);let ae=class extends o.Disposable{constructor(s,r){super(),this._textSelectionManagerService=s,this._commandService=r,this._commandExecutedListener()}_commandExecutedListener(){const s=[G.id,K.id,q.id,Z.id,B.id,J.id,Q.id,ee.id,te.id];this.disposeWithMe(this._commandService.onCommandExecuted(r=>{s.includes(r.id)&&this._handleInlineFormat(r)}))}_handleInlineFormat(s){var e,t;const{segmentId:r}=(e=this._textSelectionManagerService.getActiveRange())!=null?e:{};r!=null&&this._commandService.executeCommand(De.id,{segmentId:r,preCommandId:s.id,...(t=s.params)!=null?t:{}})}};ae=Nt([o.OnLifecycle(o.LifecycleStages.Rendered,ae),Ae(0,b.Inject(exports.TextSelectionManagerService)),Ae(1,o.ICommandService)],ae);var At=Object.defineProperty,wt=Object.getOwnPropertyDescriptor,Lt=(s,r,e,t)=>{for(var n=t>1?void 0:t?wt(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&At(r,e,n),n},V=(s,r)=>(e,t)=>r(e,t,s);let ce=class extends o.Disposable{constructor(r,e,t,n,i){super();I(this,"_onInputSubscription");this._docSkeletonManagerService=r,this._currentUniverService=e,this._renderManagerService=t,this._textSelectionManagerService=n,this._commandService=i,this._initialize(),this._commandExecutedListener()}dispose(){var r;(r=this._onInputSubscription)==null||r.unsubscribe()}_initialize(){}_commandExecutedListener(){const r=[he.id,Se.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(!r.includes(e.id))return;const t=e.params;switch(e.id){case he.id:return this._handleMoveCursor(t.direction);case Se.id:return this._handleShiftMoveSelection(t.direction);default:throw new Error("Unknown command")}}))}_handleShiftMoveSelection(r){var _,S,f;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getSelections(),n=this._currentUniverService.getCurrentUniverDocInstance(),i=(_=this._docSkeletonManagerService.getCurrent())==null?void 0:_.skeleton,a=this._getDocObject();if(e==null||i==null||a==null)return;const{startOffset:c,endOffset:l,style:u,collapsed:d,direction:m}=e;if(t.length>1){let v=Number.POSITIVE_INFINITY,T=Number.NEGATIVE_INFINITY;for(const O of t)v=Math.min(v,O.startOffset),T=Math.max(T,O.endOffset);this._textSelectionManagerService.replaceTextRanges([{startOffset:r===o.Direction.LEFT||r===o.Direction.UP?T:v,endOffset:r===o.Direction.LEFT||r===o.Direction.UP?v:T,style:u}]);return}const g=d||m===C.RANGE_DIRECTION.FORWARD?c:l;let h=d||m===C.RANGE_DIRECTION.FORWARD?l:c;const p=(S=n.getBody().dataStream.length)!=null?S:Number.POSITIVE_INFINITY;if(r===o.Direction.LEFT||r===o.Direction.RIGHT){const v=i.findNodeByCharIndex(h-1),T=i.findNodeByCharIndex(h);h=r===o.Direction.RIGHT?h+T.count:h-((f=v==null?void 0:v.count)!=null?f:0),h=Math.min(p-2,Math.max(0,h)),this._textSelectionManagerService.replaceTextRanges([{startOffset:g,endOffset:h,style:u}])}else{const v=i.findNodeByCharIndex(h),T=a.document.getOffsetConfig(),O=this._getTopOrBottomPosition(i,v,r===o.Direction.DOWN);if(O==null){const E=r===o.Direction.UP?0:p-2;if(E===h)return;this._textSelectionManagerService.replaceTextRanges([{startOffset:g,endOffset:E,style:u}]);return}const R=new C.NodePositionConvertToCursor(T,i).getRangePointData(O,O).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{startOffset:g,endOffset:R.endOffset,style:u}])}}_handleMoveCursor(r){var g,h,p;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getSelections(),n=this._currentUniverService.getCurrentUniverDocInstance(),i=(g=this._docSkeletonManagerService.getCurrent())==null?void 0:g.skeleton,a=this._getDocObject();if(e==null||i==null||a==null||t==null)return;const{startOffset:c,endOffset:l,style:u,collapsed:d}=e,m=(h=n.getBody().dataStream.length)!=null?h:Number.POSITIVE_INFINITY;if(r===o.Direction.LEFT||r===o.Direction.RIGHT){let _;if(!e.collapsed||t.length>1){let S=Number.POSITIVE_INFINITY,f=Number.NEGATIVE_INFINITY;for(const v of t)S=Math.min(S,v.startOffset),f=Math.max(f,v.endOffset);_=r===o.Direction.LEFT?S:f}else{const S=i.findNodeByCharIndex(c-1),f=i.findNodeByCharIndex(c);r===o.Direction.LEFT?_=Math.max(0,c-((p=S==null?void 0:S.count)!=null?p:0)):_=Math.min(m-2,l+f.count)}this._textSelectionManagerService.replaceTextRanges([{startOffset:_,endOffset:_,style:u}])}else{const _=i.findNodeByCharIndex(c),S=i.findNodeByCharIndex(l),f=a.document.getOffsetConfig(),v=this._getTopOrBottomPosition(i,r===o.Direction.UP?_:S,r===o.Direction.DOWN);if(v==null){let O;d?O=r===o.Direction.UP?0:m-2:O=r===o.Direction.UP?c:l,this._textSelectionManagerService.replaceTextRanges([{startOffset:O,endOffset:O,style:u}]);return}const T=new C.NodePositionConvertToCursor(f,i).getRangePointData(v,v).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{...T,style:u}])}}_getTopOrBottomPosition(r,e,t){if(e==null)return;const n=this._getSpanLeftOffsetInLine(e),i=this._getNextOrPrevLine(e,t);if(i==null)return;const a=this._matchPositionByLeftOffset(r,i,n);if(a!=null)return{...a,isBack:!0}}_getSpanLeftOffsetInLine(r){const e=r.parent;if(e==null)return Number.NEGATIVE_INFINITY;const t=e.left,{left:n}=r;return t+n}_matchPositionByLeftOffset(r,e,t){const n={distance:Number.POSITIVE_INFINITY};for(const i of e.divides){const a=i.left;for(const c of i.spanGroup){const{left:l}=c,u=a+l,d=Math.abs(t-u);d<n.distance&&(n.span=c,n.distance=d)}}if(n.span!=null)return r.findPositionBySpan(n.span)}_getNextOrPrevLine(r,e){var p,_,S,f,v,T,O,R,E,D,x,y;const t=r.parent;if(t==null)return;const n=t.parent;if(n==null)return;const i=n.parent;if(i==null)return;const a=i.lines.indexOf(n);if(a===-1)return;let c;if(e===!0?c=i.lines[a+1]:c=i.lines[a-1],c!=null)return c;const l=i.parent;if(l==null)return;const u=l.columns.indexOf(i);if(u===-1)return;if(e===!0)c=(p=l.columns[u+1])==null?void 0:p.lines[0];else{const M=(S=(_=l.columns)==null?void 0:_[u-1])==null?void 0:S.lines;c=M==null?void 0:M[M.length-1]}if(c!=null)return c;const d=l.parent;if(d==null)return;const m=d.sections.indexOf(l);if(m===-1)return;if(e===!0)c=(v=(f=d.sections[m-1])==null?void 0:f.columns[0])==null?void 0:v.lines[0];else{const M=(O=(T=d.sections)==null?void 0:T[m-1])==null?void 0:O.columns,A=M==null?void 0:M[M.length-1],P=A==null?void 0:A.lines;c=P==null?void 0:P[P.length-1]}if(c!=null)return c;const g=d.parent;if(g==null)return;const h=g.pages.indexOf(d);if(h!==-1){if(e===!0)c=(D=(E=(R=g.pages[h+1])==null?void 0:R.sections[0])==null?void 0:E.columns[0])==null?void 0:D.lines[0];else{const M=(x=g.pages[h-1])==null?void 0:x.sections;if(M==null)return;const A=(y=M[M.length-1])==null?void 0:y.columns,P=A[A.length-1],Ee=P==null?void 0:P.lines;c=Ee[Ee.length-1]}if(c!=null)return c}}_getDocObject(){return pe(this._currentUniverService,this._renderManagerService)}};ce=Lt([o.OnLifecycle(o.LifecycleStages.Rendered,ce),V(0,b.Inject(exports.DocSkeletonManagerService)),V(1,o.IUniverInstanceService),V(2,C.IRenderManagerService),V(3,b.Inject(exports.TextSelectionManagerService)),V(4,o.ICommandService)],ce);var Bt=Object.defineProperty,$t=Object.getOwnPropertyDescriptor,jt=(s,r,e,t)=>{for(var n=t>1?void 0:t?$t(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&Bt(r,e,n),n},k=(s,r)=>(e,t)=>r(e,t,s);let le=class extends o.Disposable{constructor(r,e,t,n,i){super();I(this,"_onInputSubscription");this._docSkeletonManagerService=r,this._currentUniverService=e,this._renderManagerService=t,this._textSelectionRenderManager=n,this._commandService=i,this._initialize(),this._commandExecutedListener()}dispose(){var r;(r=this._onInputSubscription)==null||r.unsubscribe()}_initialize(){this._initialNormalInput()}_initialNormalInput(){this._onInputSubscription=this._textSelectionRenderManager.onInput$.subscribe(async r=>{var p;if(r==null)return;const t=this._currentUniverService.getCurrentUniverDocInstance().getUnitId(),{event:n,content:i="",activeRange:a}=r,c=n,l=(p=this._docSkeletonManagerService.getCurrent())==null?void 0:p.skeleton;if(c.data==null||l==null||!l||!a)return;const{startOffset:u,segmentId:d,style:m}=a,g=i.length,h=[{startOffset:u+g,endOffset:u+g,style:m}];await this._commandService.executeCommand(H.id,{unitId:t,body:{dataStream:i},range:a,textRanges:h,segmentId:d})})}_commandExecutedListener(){}_getDocObject(){return pe(this._currentUniverService,this._renderManagerService)}};le=jt([o.OnLifecycle(o.LifecycleStages.Rendered,le),k(0,b.Inject(exports.DocSkeletonManagerService)),k(1,o.IUniverInstanceService),k(2,C.IRenderManagerService),k(3,C.ITextSelectionRenderManager),k(4,o.ICommandService)],le);var Ft=Object.defineProperty,Vt=Object.getOwnPropertyDescriptor,kt=(s,r,e,t)=>{for(var n=t>1?void 0:t?Vt(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&Ft(r,e,n),n},we=(s,r)=>(e,t)=>r(e,t,s);const Xt="rgba(198, 198, 198, 1)",zt="rgba(255, 255, 255, 1)";let de=class extends o.Disposable{constructor(s,r){super(),this._renderManagerService=s,this._currentUniverService=r,this._initialize(),this._commandExecutedListener()}_initialize(){this._initialRenderRefresh()}_initialRenderRefresh(){this._renderManagerService.currentRender$.subscribe(s=>{var i;if(s==null||this._currentUniverService.getUniverDocInstance(s)==null)return;const r=this._renderManagerService.getRenderById(s);if(r==null)return;const{mainComponent:e}=r,t=e,n=(i=t.getSkeleton())==null?void 0:i.getPageSize();t.onPageRenderObservable.add(a=>{var _,S,f,v;if([o.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,o.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(s))return;const{page:c,pageLeft:l,pageTop:u,ctx:d}=a,{width:m,pageWidth:g,height:h,pageHeight:p}=c;d.save(),d.translate(l-.5,u-.5),C.Rect.drawWith(d,{width:(S=(_=n==null?void 0:n.width)!=null?_:g)!=null?S:m,height:(v=(f=n==null?void 0:n.height)!=null?f:p)!=null?v:h,strokeWidth:1,stroke:Xt,fill:zt,zIndex:3}),d.restore()})})}_commandExecutedListener(){}};de=kt([o.OnLifecycle(o.LifecycleStages.Rendered,de),we(0,C.IRenderManagerService),we(1,b.Inject(o.IUniverInstanceService))],de);var Yt=Object.defineProperty,Ht=Object.getOwnPropertyDescriptor,Wt=(s,r,e,t)=>{for(var n=t>1?void 0:t?Ht(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&Yt(r,e,n),n},$=(s,r)=>(e,t)=>r(e,t,s);let ue=class extends o.Disposable{constructor(r,e,t,n,i,a){super();I(this,"_moveInObserver");I(this,"_moveOutObserver");I(this,"_downObserver");I(this,"_dblClickObserver");I(this,"_tripleClickObserver");I(this,"_loadedMap",new Set);this._docSkeletonManagerService=r,this._currentUniverService=e,this._commandService=t,this._renderManagerService=n,this._textSelectionRenderManager=i,this._textSelectionManagerService=a,this._renderManagerService.currentRender$.subscribe(c=>{c!=null&&this._currentUniverService.getUniverDocInstance(c)!=null&&(this._loadedMap.has(c)||(this._initialMain(c),this._loadedMap.add(c)))}),this._initialize()}_initialize(){this._skeletonListener(),this._commandExecutedListener()}dispose(){this._renderManagerService.getRenderAll().forEach(r=>{const{mainComponent:e}=r;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(r){const e=this._getDocObjectById(r);if(e==null)return;const{document:t,scene:n}=e;this._moveInObserver=t.onPointerEnterObserver.add(()=>{t.cursor=C.CURSOR_TYPE.TEXT}),this._moveOutObserver=t.onPointerLeaveObserver.add(()=>{t.cursor=C.CURSOR_TYPE.DEFAULT,n.resetCursor()}),this._downObserver=t==null?void 0:t.onPointerDownObserver.add((i,a)=>{this._currentUniverService.getCurrentUniverDocInstance().getUnitId()!==r&&this._currentUniverService.setCurrentUniverDocInstance(r),this._textSelectionRenderManager.eventTrigger(i),i.button!==2&&a.stopPropagation()}),this._dblClickObserver=t==null?void 0:t.onDblclickObserver.add(i=>{this._textSelectionRenderManager.handleDblClick(i)}),this._tripleClickObserver=t==null?void 0:t.onTripleClickObserver.add(i=>{this._textSelectionRenderManager.handleTripleClick(i)})}_commandExecutedListener(){const r=[L.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{var t;if(r.includes(e.id)){const n=e.params,{unitId:i}=n,a=(t=this._textSelectionManagerService.getCurrentSelection())==null?void 0:t.unitId;if(i!==a)return;this._textSelectionManagerService.refreshSelection()}}))}_skeletonListener(){this._docSkeletonManagerService.currentSkeleton$.subscribe(r=>{if(r==null)return;const{unitId:e,skeleton:t}=r,n=this._renderManagerService.getRenderById(e);if(n==null)return;const{scene:i,mainComponent:a}=n;this._textSelectionRenderManager.changeRuntime(t,i,a),this._textSelectionManagerService.setCurrentSelectionNotRefresh({unitId:e,subUnitId:""})})}_getDocObjectById(r){return Je(r,this._renderManagerService)}};ue=Wt([o.OnLifecycle(o.LifecycleStages.Rendered,ue),$(0,b.Inject(exports.DocSkeletonManagerService)),$(1,o.IUniverInstanceService),$(2,o.ICommandService),$(3,C.IRenderManagerService),$(4,C.ITextSelectionRenderManager),$(5,b.Inject(exports.TextSelectionManagerService))],ue);var Gt=Object.defineProperty,Kt=Object.getOwnPropertyDescriptor,qt=(s,r,e,t)=>{for(var n=t>1?void 0:t?Kt(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&Gt(r,e,n),n},X=(s,r)=>(e,t)=>r(e,t,s);let me=class extends o.Disposable{constructor(r,e,t,n,i){super();I(this,"_initializedRender",new Set);this._docSkeletonManagerService=r,this._currentUniverService=e,this._commandService=t,this._renderManagerService=n,this._textSelectionManagerService=i,this._initialize()}dispose(){super.dispose()}_initialize(){this._skeletonListener(),this._commandExecutedListener(),this._initialRenderRefresh()}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeleton$.subscribe(r=>{if(r==null)return;const{unitId:e}=r,t=this._renderManagerService.getRenderById(e);if(t==null||this._initializedRender.has(e)||[o.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,o.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(e))return;this._initializedRender.add(e);const{scene:n}=t;this.disposeWithMe(o.toDisposable(n.onMouseWheelObserver.add(i=>{if(!i.ctrlKey)return;const a=Math.abs(i.deltaX);let c=a<40?.2:a<80?.4:.2;c*=i.deltaY>0?-1:1,n.scaleX<1&&(c/=2);const l=this._currentUniverService.getCurrentUniverDocInstance(),u=l.zoomRatio;let d=+Number.parseFloat(`${u+c}`).toFixed(1);d=d>=4?4:d<=.1?.1:d,this._commandService.executeCommand(Ge.id,{zoomRatio:d,unitId:l.getUnitId()}),i.preventDefault()})))})}_skeletonListener(){this.disposeWithMe(o.toDisposable(this._docSkeletonManagerService.currentSkeletonBefore$.subscribe(r=>{if(r==null)return;const t=this._currentUniverService.getCurrentUniverDocInstance().zoomRatio||1;this._updateViewZoom(t,!1)})))}_commandExecutedListener(){const r=[L.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(r.includes(e.id)){const t=this._currentUniverService.getCurrentUniverDocInstance(),n=e.params,{unitId:i}=n;if(i!==t.getUnitId())return;const a=t.zoomRatio||1;this._updateViewZoom(a)}}))}_updateViewZoom(r,e=!0){var n;const t=this._getDocObject();t!=null&&(t.scene.scale(r,r),this._calculatePagePosition(t,r),e&&this._textSelectionManagerService.refreshSelection(),(n=t.scene.getTransformer())==null||n.hideControl())}_calculatePagePosition(r,e){const{document:t,scene:n}=r,i=n==null?void 0:n.getParent(),{width:a,height:c,pageMarginLeft:l,pageMarginTop:u}=t;if(i==null||a===Number.POSITIVE_INFINITY||c===Number.POSITIVE_INFINITY)return;const{width:d,height:m}=i;let g=0,h=0,p=0,_=0,S=Number.POSITIVE_INFINITY;d>(a+l*2)*e?(g=d/2-a*e/2,g/=e,p=(d-l*2)/e,S=0):(g=l,p=a+l*2,S=(p-d/e)/2),m>c?(h=m/2-c/2,_=(m-u*2)/e):(h=u,_=c+u*2),n.resize(p,_+200),t.translate(g,h);const f=n.getViewport(_e.VIEW_MAIN);if(S!==Number.POSITIVE_INFINITY&&f!=null){const v=f.getBarScroll(S,0).x;f.scrollTo({x:v})}return this}_getDocObject(){return pe(this._currentUniverService,this._renderManagerService)}};me=qt([o.OnLifecycle(o.LifecycleStages.Rendered,me),X(0,b.Inject(exports.DocSkeletonManagerService)),X(1,o.IUniverInstanceService),X(2,o.ICommandService),X(3,C.IRenderManagerService),X(4,b.Inject(exports.TextSelectionManagerService))],me);var Zt=Object.defineProperty,Jt=Object.getOwnPropertyDescriptor,Qt=(s,r,e,t)=>{for(var n=t>1?void 0:t?Jt(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&Zt(r,e,n),n},ne=(s,r)=>(e,t)=>r(e,t,s);exports.DocCanvasView=class extends o.RxDisposable{constructor(e,t,n,i){super();I(this,"_scene");I(this,"_currentDocumentModel");I(this,"_loadedMap",new Set);I(this,"_fps$",new N.BehaviorSubject(""));I(this,"fps$",this._fps$.asObservable());this._renderManagerService=e,this._configService=t,this._currentUniverService=n,this._docViewModelManagerService=i,this._initialize()}_initialize(){this._currentUniverService.currentDoc$.pipe(N.takeUntil(this.dispose$)).subscribe(e=>{if(e==null)return;this._currentDocumentModel=e;const t=e.getUnitId();this._loadedMap.has(t)||(this._addNewRender(),this._loadedMap.add(t))})}dispose(){this._fps$.complete()}_addNewRender(){const e=this._currentDocumentModel,t=e.getUnitId(),n=e.getContainer(),i=e.getParentRenderUnitId();if(n!=null&&i!=null)throw new Error("container or parentRenderUnitId can only exist one");n==null&&i!=null?this._renderManagerService.createRenderWithParent(t,i):this._renderManagerService.createRender(t);const a=this._renderManagerService.getRenderById(t);if(a==null)return;const{scene:c,engine:l}=a;c.openTransformer(),this._scene=c;const u=new C.Viewport(_e.VIEW_MAIN,c,{left:0,top:0,bottom:0,right:0,isWheelPreventDefaultX:!0});c.attachControl(),c.on(C.EVENT_TYPE.wheel,(g,h)=>{const p=g;if(p.ctrlKey){const _=Math.abs(p.deltaX);let S=_<40?.2:_<80?.4:.2;S*=p.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):p.preventDefault()}else u.onMouseWheel(p,h)}),this._configService.getConfig("hasScroll")!==!1&&t!==o.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY&&new C.ScrollBar(u),c.addLayer(new C.Layer(c,[],Me),new C.Layer(c,[],Le)),this._addComponent(a),this._currentDocumentModel.getShouldRenderLoopImmediately()&&l.runRenderLoop(()=>{c.render(),this._fps$.next(Math.round(l.getFps()).toString())}),this._renderManagerService.setCurrent(t)}_addComponent(e){const t=this._scene,n=this._currentDocumentModel,i=new C.Documents(ge.MAIN,void 0,{pageMarginLeft:n.documentStyle.marginLeft||0,pageMarginTop:n.documentStyle.marginTop||0});i.zIndex=Be,e.mainComponent=i,e.components.set(ge.MAIN,i),t.addObjects([i],Me)}};exports.DocCanvasView=Qt([o.OnLifecycle(o.LifecycleStages.Ready,exports.DocCanvasView),ne(0,C.IRenderManagerService),ne(1,o.IConfigService),ne(2,o.IUniverInstanceService),ne(3,b.Inject(exports.DocViewModelManagerService))],exports.DocCanvasView);var en=Object.defineProperty,tn=Object.getOwnPropertyDescriptor,nn=(s,r,e,t)=>{for(var n=t>1?void 0:t?tn(r,e):r,i=s.length-1,a;i>=0;i--)(a=s[i])&&(n=(t?a(r,e,n):a(n))||n);return t&&n&&en(r,e,n),n},re=(s,r)=>(e,t)=>r(e,t,s);const rn={hasScroll:!0},on="docs";var Ie;exports.UniverDocsPlugin=(Ie=class extends o.Plugin{constructor(e={},t,n,i,a){super(on);I(this,"_config");this._injector=t,this._localeService=n,this._configService=i,this._currentUniverService=a,this._config=Object.assign(rn,e),this._initializeDependencies(t),this._initializeCommands()}initialize(){}_initializeCommands(){[he,Se,je,Fe,G,K,q,Z,B,J,Q,ee,te,De,$e,H,W,Oe,ye,xe,U,Ye,He,Ge,L,Ce,Ke,ze,Xe,be].forEach(e=>{this._injector.get(o.ICommandService).registerCommand(e)})}onReady(){this.initialize()}_initializeDependencies(e){[[exports.DocCanvasView],[exports.DocSkeletonManagerService],[exports.DocViewModelManagerService],[exports.DocStateChangeManagerService],[Y],[C.ITextSelectionRenderManager,{useClass:C.TextSelectionRenderManager}],[exports.TextSelectionManagerService],[ie],[de],[ue],[le],[se],[ae],[ce],[me],[oe]].forEach(t=>e.add(t))}},I(Ie,"type",o.PluginType.Doc),Ie);exports.UniverDocsPlugin=nn([re(1,b.Inject(b.Injector)),re(2,b.Inject(o.LocaleService)),re(3,o.IConfigService),re(4,o.IUniverInstanceService)],exports.UniverDocsPlugin);exports.BreakLineCommand=$e;exports.BulletListCommand=Xe;exports.CoverContentCommand=He;exports.CutContentCommand=Re;exports.DOCS_COMPONENT_DEFAULT_Z_INDEX=Be;exports.DOCS_COMPONENT_HEADER_LAYER_INDEX=Le;exports.DOCS_COMPONENT_MAIN_LAYER_INDEX=Me;exports.DOCS_VIEW_KEY=ge;exports.DeleteCommand=W;exports.DeleteLeftCommand=je;exports.DeleteRightCommand=Fe;exports.IMEInputCommand=ye;exports.IMEInputManagerService=Y;exports.InnerPasteCommand=St;exports.InsertCommand=H;exports.MoveCursorOperation=he;exports.MoveSelectionOperation=Se;exports.NORMAL_TEXT_SELECTION_PLUGIN_NAME=Qe;exports.OrderListCommand=ze;exports.ReplaceContentCommand=Ye;exports.RichTextEditingMutation=U;exports.SelectAllOperation=Ke;exports.SetInlineFormatBoldCommand=G;exports.SetInlineFormatCommand=De;exports.SetInlineFormatFontFamilyCommand=ee;exports.SetInlineFormatFontSizeCommand=Q;exports.SetInlineFormatItalicCommand=K;exports.SetInlineFormatStrikethroughCommand=Z;exports.SetInlineFormatSubscriptCommand=B;exports.SetInlineFormatSuperscriptCommand=J;exports.SetInlineFormatTextColorCommand=te;exports.SetInlineFormatUnderlineCommand=q;exports.SetTextSelectionsOperation=Ce;exports.UpdateCommand=Oe;exports.VIEWPORT_KEY=_e;exports.getDocObject=pe;exports.serializeTextRange=z;
1
+ "use strict";var Le=Object.defineProperty;var Be=(s,n,e)=>n in s?Le(s,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[n]=e;var C=(s,n,e)=>(Be(s,typeof n!="symbol"?n+"":n,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("@univerjs/core"),D=require("@univerjs/engine-render"),b=require("rxjs"),A=require("@wendellhu/redi");function le(s,n){const t=s.getCurrentUniverDocInstance().getUnitId(),o=n.getRenderById(t);if(o==null)return;const{mainComponent:i,scene:a,engine:c}=o;return{document:i,scene:a,engine:c}}function $e(s,n){const e=n.getRenderById(s);if(e==null)return;const{mainComponent:t,scene:o,engine:i}=e;return{document:t,scene:o,engine:i}}var Me=(s=>(s.MAIN="__Document_Render_Main__",s))(Me||{}),Te=(s=>(s.VIEW_MAIN="viewMain",s.VIEW_TOP="viewTop",s.VIEW_LEFT="viewLeft",s.VIEW_LEFT_TOP="viewLeftTop",s))(Te||{});const Fe=0,Xe=2,Ve=10,je="normalTextSelectionPluginName",de={id:"doc.operation.set-selections",type:r.CommandType.OPERATION,handler:(s,n)=>!0};var ke=Object.defineProperty,ze=Object.getOwnPropertyDescriptor,He=(s,n,e,t)=>{for(var o=t>1?void 0:t?ze(n,e):n,i=s.length-1,a;i>=0;i--)(a=s[i])&&(o=(t?a(n,e,o):a(o))||o);return t&&o&&ke(n,e,o),o},_e=(s,n)=>(e,t)=>n(e,t,s);function B(s){const{startOffset:n,endOffset:e,collapsed:t}=s,o={startOffset:n,endOffset:e,collapsed:t};return typeof s.isActive=="function"&&(o.isActive=s.isActive()),o}exports.TextSelectionManagerService=class extends r.RxDisposable{constructor(e,t){super();C(this,"_currentSelection",null);C(this,"_textSelectionInfo",new Map);C(this,"_textSelection$",new b.BehaviorSubject(null));C(this,"textSelection$",this._textSelection$.asObservable());this._textSelectionRenderManager=e,this._commandService=t,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:t,segmentId:o,style:i}=e,a=t.find(S=>S.isActive());if(a==null)return null;const{startOffset:c,endOffset:l,collapsed:u,startNodePosition:d,endNodePosition:m,direction:g}=a;return c==null||l==null?null:{startOffset:c,endOffset:l,collapsed:u,startNodePosition:d,endNodePosition:m,direction:g,segmentId:o,style:i}}add(e,t=!0){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:e,segmentId:"",isEditing:t,style:D.NORMAL_TEXT_SELECTION_PLUGIN_STYLE})}replaceTextRanges(e,t=!0){this._currentSelection!=null&&(this._textSelectionRenderManager.removeAllTextRanges(),this._textSelectionRenderManager.addTextRanges(e,t))}_syncSelectionFromRenderService(){this._textSelectionRenderManager.textSelectionInner$.pipe(b.takeUntil(this.dispose$)).subscribe(e=>{e!=null&&this._replaceTextRangesWithNoRefresh(e)})}_replaceTextRangesWithNoRefresh(e){if(this._currentSelection==null)return;const t={...this._currentSelection,...e};this._replaceByParam(t),this._textSelection$.next(t);const{unitId:o,subUnitId:i,segmentId:a,style:c,textRanges:l,isEditing:u}=t;this._commandService.executeCommand(de.id,{unitId:o,subUnitId:i,segmentId:a,style:c,isEditing:u,ranges:l.map(B)})}_getTextRanges(e){var i;if(e==null)return;const{unitId:t,subUnitId:o=""}=e;return(i=this._textSelectionInfo.get(t))==null?void 0:i.get(o)}_refresh(e){const t=this._getTextRanges(e);this._textSelectionRenderManager.removeAllTextRanges(),t&&Array.isArray(t.textRanges)&&t.textRanges.length&&this._textSelectionRenderManager.addTextRanges(t.textRanges.map(B))}_replaceByParam(e){const{unitId:t,subUnitId:o,style:i,segmentId:a,textRanges:c,isEditing:l}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map),this._textSelectionInfo.get(t).set(o,{textRanges:c,style:i,segmentId:a,isEditing:l})}_addByParam(e){const{unitId:t,subUnitId:o,textRanges:i,style:a,segmentId:c,isEditing:l}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map);const u=this._textSelectionInfo.get(t);u.has(o)?u.get(o).textRanges.push(...i):u.set(o,{textRanges:i,style:a,segmentId:c,isEditing:l})}};exports.TextSelectionManagerService=He([_e(0,D.ITextSelectionRenderManager),_e(1,r.ICommandService)],exports.TextSelectionManagerService);function ue(s,n="",e=0){const{startOffset:t,endOffset:o}=s,i=[],a=t-e,c=o-e;return a>0&&i.push({t:r.TextXActionType.RETAIN,len:a,segmentId:n}),i.push({t:r.TextXActionType.DELETE,len:c-a,line:0,segmentId:n}),i}var Ge=Object.defineProperty,Ye=Object.getOwnPropertyDescriptor,We=(s,n,e,t)=>{for(var o=t>1?void 0:t?Ye(n,e):n,i=s.length-1,a;i>=0;i--)(a=s[i])&&(o=(t?a(n,e,o):a(o))||o);return t&&o&&Ge(n,e,o),o},Ze=(s,n)=>(e,t)=>n(e,t,s);exports.DocViewModelManagerService=class extends r.RxDisposable{constructor(e){super();C(this,"_docViewModelMap",new Map);C(this,"_currentDocViewModel$",new b.BehaviorSubject(null));C(this,"currentDocViewModel$",this._currentDocViewModel$.asObservable());this._currentUniverService=e,this._initialize()}_initialize(){this._init()}dispose(){this._currentDocViewModel$.complete(),this._docViewModelMap.clear()}_init(){this._currentUniverService.currentDoc$.pipe(b.takeUntil(this.dispose$)).subscribe(e=>{this._create(e)}),this._currentUniverService.getAllUniverDocsInstance().forEach(e=>{this._create(e)}),this._currentUniverService.docDisposed$.pipe(b.takeUntil(this.dispose$)).subscribe(e=>{this._docViewModelMap.delete(e.getUnitId())})}_create(e){if(e==null)return;const t=e.getUnitId();this._setCurrent(t)}getAllModel(){return this._docViewModelMap}getViewModel(e){var t;return(t=this._docViewModelMap.get(e))==null?void 0:t.docViewModel}_setCurrent(e){var o;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 i=this._buildDocViewModel(t);this._docViewModelMap.set(e,{unitId:e,docViewModel:i})}if(e===r.DOCS_NORMAL_EDITOR_UNIT_ID_KEY){const i=(o=this._docViewModelMap.get(e))==null?void 0:o.docViewModel;if(i==null)return;i.reset(t)}this._currentDocViewModel$.next(this._docViewModelMap.get(e))}}_buildDocViewModel(e){return new D.DocumentViewModel(e)}};exports.DocViewModelManagerService=We([Ze(0,r.IUniverInstanceService)],exports.DocViewModelManagerService);var qe=Object.defineProperty,Ke=Object.getOwnPropertyDescriptor,Je=(s,n,e,t)=>{for(var o=t>1?void 0:t?Ke(n,e):n,i=s.length-1,a;i>=0;i--)(a=s[i])&&(o=(t?a(n,e,o):a(o))||o);return t&&o&&qe(n,e,o),o},se=(s,n)=>(e,t)=>n(e,t,s);const Qe=300;exports.DocStateChangeManagerService=class extends r.RxDisposable{constructor(e,t,o){super();C(this,"_docStateChange$",new b.BehaviorSubject(null));C(this,"docStateChange$",this._docStateChange$.asObservable());C(this,"_stateCache",new Map);C(this,"_timer",null);this._undoRedoService=e,this._commandService=t,this._univerInstanceService=o,this._initialize()}setChangeState(e){const{trigger:t,noHistory:o}=e;t!=null&&(this._cacheChangeState(e),o||this._docStateChange$.next(e))}_initialize(){this.disposeWithMe(this._commandService.beforeCommandExecuted(e=>{if(e.id===r.UndoCommandId||e.id===r.RedoCommandId){const t=this._univerInstanceService.getCurrentUniverDocInstance();if(t==null)return;const o=t.getUnitId();this._pushHistory(o)}}))}_cacheChangeState(e){const{trigger:t,unitId:o,noHistory:i}=e;if(!(t===r.RedoCommandId||t===r.UndoCommandId||i)){if(this._stateCache.has(o)){const a=this._stateCache.get(o);a==null||a.push(e)}else this._stateCache.set(o,[e]);t===k.id||t===z.id?(this._timer&&clearTimeout(this._timer),this._timer=setTimeout(()=>{this._pushHistory(o)},Qe)):this._pushHistory(o)}}_pushHistory(e){const t=this._undoRedoService,o=this._stateCache.get(e);if(!Array.isArray(o)||o.length===0)return;const i=o.length,a=o[0].commandId,c=o[0],l=o[i-1],u={unitId:e,actions:o.reduce((m,g)=>r.TextX.compose(m,g.redoState.actions),[]),textRanges:l.redoState.textRanges},d={unitId:e,actions:o.reverse().reduce((m,g)=>r.TextX.compose(m,g.undoState.actions),[]),textRanges:c.undoState.textRanges};t.pushUndoRedo({unitID:e,undoMutations:[{id:a,params:d}],redoMutations:[{id:a,params:u}]}),o.length=0}};exports.DocStateChangeManagerService=Je([se(0,A.Inject(r.IUndoRedoService)),se(1,r.ICommandService),se(2,r.IUniverInstanceService)],exports.DocStateChangeManagerService);class j{constructor(){C(this,"_previousActiveRange",null);C(this,"_undoMutationParamsCache",[]);C(this,"_redoMutationParamsCache",[])}clearUndoRedoMutationParamsCache(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[]}getUndoRedoMutationParamsCache(){return{undoCache:this._undoMutationParamsCache,redoCache:this._redoMutationParamsCache}}setUndoRedoMutationParamsCache({undoCache:n=[],redoCache:e=[]}){this._undoMutationParamsCache=n,this._redoMutationParamsCache=e}getActiveRange(){return this._previousActiveRange}setActiveRange(n){this._previousActiveRange=n}pushUndoRedoMutationParams(n,e){this._undoMutationParamsCache.push(n),this._redoMutationParamsCache.push(e)}fetchComposedUndoRedoMutationParams(){if(this._undoMutationParamsCache.length===0||this._previousActiveRange==null||this._redoMutationParamsCache.length===0)return null;const{unitId:n}=this._undoMutationParamsCache[0],e={unitId:n,actions:this._undoMutationParamsCache.reverse().reduce((o,i)=>r.TextX.compose(o,i.actions),[]),textRanges:[]};return{redoMutationParams:{unitId:n,actions:this._redoMutationParamsCache.reduce((o,i)=>r.TextX.compose(o,i.actions),[]),textRanges:[]},undoMutationParams:e,previousActiveRange:this._previousActiveRange}}dispose(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[],this._previousActiveRange=null}}const Ie="doc.mutation.rich-text-editing",U={id:Ie,type:r.CommandType.MUTATION,handler:(s,n)=>{var R;const{unitId:e,actions:t,textRanges:o,prevTextRanges:i,trigger:a,noHistory:c,isCompositionEnd:l,noNeedSetTextRange:u}=n,m=s.get(r.IUniverInstanceService).getUniverDocInstance(e),S=s.get(exports.DocViewModelManagerService).getViewModel(e),v=s.get(exports.TextSelectionManagerService),p=((R=v.getSelections())!=null?R:[]).map(B),_=s.get(exports.DocStateChangeManagerService),h=s.get(j);if(m==null||S==null)throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${e}`);if(t.length===0)return{unitId:e,actions:[],textRanges:p};const M=m.apply(t),{segmentId:T}=t[0],x=m.getSelfOrHeaderFooterModel(T);S.getSelfOrHeaderFooterViewModel(T).reset(x),!u&&o&&a!=null&&queueMicrotask(()=>{v.replaceTextRanges(o)});const O={commandId:Ie,unitId:e,trigger:a,noHistory:c,redoState:{actions:t,textRanges:o},undoState:{actions:M,textRanges:i!=null?i:p}};if(l){const y=h.fetchComposedUndoRedoMutationParams();if(y==null)throw new Error("historyParams is null in RichTextEditingMutation");const{undoMutationParams:I,redoMutationParams:P,previousActiveRange:N}=y;O.redoState.actions=P.actions,O.undoState.actions=I.actions,O.undoState.textRanges=[N]}return _.setChangeState(O),{unitId:e,actions:M,textRanges:p}}},k={id:"doc.command.insert-text",type:r.CommandType.COMMAND,handler:async(s,n)=>{const e=s.get(r.ICommandService),{range:t,segmentId:o,body:i,unitId:a,textRanges:c}=n,{startOffset:l,collapsed:u}=t,d={id:U.id,params:{unitId:a,actions:[],textRanges:c}},m=new r.TextX;return u?l>0&&m.push({t:r.TextXActionType.RETAIN,len:l,segmentId:o}):m.push(...ue(t,o)),m.push({t:r.TextXActionType.INSERT,body:i,len:i.dataStream.length,line:0,segmentId:o}),d.params.actions=m.serialize(),!!e.syncExecuteCommand(d.id,d.params)}};var w=(s=>(s[s.LEFT=0]="LEFT",s[s.RIGHT=1]="RIGHT",s))(w||{});const z={id:"doc.command.delete-text",type:r.CommandType.COMMAND,handler:async(s,n)=>{const e=s.get(r.ICommandService),{range:t,segmentId:o,unitId:i,direction:a,textRanges:c,len:l=1}=n,{startOffset:u}=t,d={id:U.id,params:{unitId:i,actions:[],textRanges:c}},m=new r.TextX;return u>0&&m.push({t:r.TextXActionType.RETAIN,len:a===0?u-l:u,segmentId:o}),m.push({t:r.TextXActionType.DELETE,len:l,line:0,segmentId:o}),d.params.actions=m.serialize(),!!e.syncExecuteCommand(d.id,d.params)}},me={id:"doc.command.update-text",type:r.CommandType.COMMAND,handler:async(s,n)=>{const{range:e,segmentId:t,updateBody:o,coverType:i,unitId:a,textRanges:c}=n,l=s.get(r.ICommandService),u={id:U.id,params:{unitId:a,actions:[],textRanges:c}},d=new r.TextX,{startOffset:m,endOffset:g}=e;return d.push({t:r.TextXActionType.RETAIN,len:m,segmentId:t}),d.push({t:r.TextXActionType.RETAIN,body:o,len:g-m,segmentId:t,coverType:i}),u.params.actions=d.serialize(),!!l.syncExecuteCommand(u.id,u.params)}};function et(s,n){const e=[];for(let t=0,o=s.length;t<o;t++)s[t]===r.DataStreamTreeTokenType.PARAGRAPH&&e.push({startIndex:t});if(n)for(const t of e)n.bullet&&(t.bullet=r.Tools.deepClone(n.bullet)),n.paragraphStyle&&(t.paragraphStyle=r.Tools.deepClone(n.paragraphStyle));return e}const xe={id:"doc.command.break-line",type:r.CommandType.COMMAND,handler:async s=>{var v,f;const n=s.get(exports.TextSelectionManagerService),e=s.get(r.IUniverInstanceService),t=s.get(r.ICommandService),o=n.getActiveRange();if(o==null)return!1;const i=e.getCurrentUniverDocInstance(),a=i.getUnitId(),{startOffset:c,segmentId:l,style:u}=o,d=[{startOffset:c+1,endOffset:c+1,style:u}],g=((f=(v=i.getBody())==null?void 0:v.paragraphs)!=null?f:[]).find(p=>p.startIndex>=c);return await t.executeCommand(k.id,{unitId:a,body:{dataStream:r.DataStreamTreeTokenType.PARAGRAPH,paragraphs:et(r.DataStreamTreeTokenType.PARAGRAPH,g)},range:o,textRanges:d,segmentId:l})}};var tt=Object.defineProperty,nt=Object.getOwnPropertyDescriptor,ot=(s,n,e,t)=>{for(var o=t>1?void 0:t?nt(n,e):n,i=s.length-1,a;i>=0;i--)(a=s[i])&&(o=(t?a(n,e,o):a(o))||o);return t&&o&&tt(n,e,o),o},ae=(s,n)=>(e,t)=>n(e,t,s);exports.DocSkeletonManagerService=class extends r.RxDisposable{constructor(e,t,o){super();C(this,"_currentSkeletonUnitId","");C(this,"_docSkeletonMap",new Map);C(this,"_currentSkeleton$",new b.BehaviorSubject(null));C(this,"currentSkeleton$",this._currentSkeleton$.asObservable());C(this,"_currentSkeletonBefore$",new b.BehaviorSubject(null));C(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());this._localeService=e,this._docViewModelManagerService=t,this._currentUniverService=o,this._initialize()}_initialize(){this._init()}dispose(){this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete(),this._docSkeletonMap.clear()}_init(){this._docViewModelManagerService.currentDocViewModel$.pipe(b.takeUntil(this.dispose$)).subscribe(e=>{e!=null&&this._setCurrent(e)}),this._docViewModelManagerService.getAllModel().forEach(e=>{e!=null&&this._setCurrent(e)}),this._currentUniverService.docDisposed$.pipe(b.takeUntil(this.dispose$)).subscribe(e=>{this._docSkeletonMap.delete(e.getUnitId()),this._currentSkeletonUnitId=this._currentUniverService.getCurrentUniverDocInstance().getUnitId()})}getCurrent(){return this.getSkeletonByUnitId(this._currentSkeletonUnitId)}getAllSkeleton(){return this._docSkeletonMap}makeDirtyCurrent(e=!0){this.makeDirty(this._currentSkeletonUnitId,e)}makeDirty(e,t=!0){const o=this.getSkeletonByUnitId(e);o!=null&&(o.dirty=t)}getSkeletonByUnitId(e){return this._docSkeletonMap.get(e)}_setCurrent(e){const{unitId:t}=e;if(this._docSkeletonMap.has(t)){const o=this.getSkeletonByUnitId(t);o.skeleton.calculate(),o.dirty=!0}else{const o=this._buildSkeleton(e.docViewModel);o.calculate(),this._docSkeletonMap.set(t,{unitId:t,skeleton:o,dirty:!1})}return this._currentSkeletonUnitId=t,this._currentSkeletonBefore$.next(this.getCurrent()),this._currentSkeleton$.next(this.getCurrent()),this.getCurrent()}_buildSkeleton(e){return D.DocumentSkeleton.create(e,this._localeService)}};exports.DocSkeletonManagerService=ot([ae(0,A.Inject(r.LocaleService)),ae(1,A.Inject(exports.DocViewModelManagerService)),ae(2,r.IUniverInstanceService)],exports.DocSkeletonManagerService);const rt={id:"doc.command.inner-paste",type:r.CommandType.COMMAND,handler:async(s,n)=>{const{segmentId:e,body:t,textRanges:o}=n,i=s.get(r.ICommandService),a=s.get(exports.TextSelectionManagerService),c=s.get(r.IUniverInstanceService),l=a.getSelections();if(!Array.isArray(l)||l.length===0)return!1;const d=c.getCurrentUniverDocInstance().getUnitId(),m={id:U.id,params:{unitId:d,actions:[],textRanges:o}},g=new r.MemoryCursor;g.reset();const S=new r.TextX;for(const f of l){const{startOffset:p,endOffset:_,collapsed:h}=f,M=p-g.cursor;h?S.push({t:r.TextXActionType.RETAIN,len:M,segmentId:e}):S.push(...ue(f,e,g.cursor)),S.push({t:r.TextXActionType.INSERT,body:t,len:t.dataStream.length,line:0,segmentId:e}),g.reset(),g.moveCursor(_)}return m.params.actions=S.serialize(),!!i.syncExecuteCommand(m.id,m.params)}},ge={id:"doc.command.inner-cut",type:r.CommandType.COMMAND,handler:async(s,n)=>{const{segmentId:e,textRanges:t}=n,o=s.get(r.ICommandService),i=s.get(exports.TextSelectionManagerService),a=s.get(r.IUniverInstanceService),c=i.getSelections();if(!Array.isArray(c)||c.length===0)return!1;const l=a.getCurrentUniverDocInstance().getUnitId(),u=a.getUniverDocInstance(l),d=r.getDocsUpdateBody(u.snapshot,e);if(d==null)return!1;const m={id:U.id,params:{unitId:l,actions:[],textRanges:t}},g=new r.MemoryCursor;g.reset();const S=new r.TextX;for(const f of c){const{startOffset:p,endOffset:_,collapsed:h}=f,M=p-g.cursor;h?S.push({t:r.TextXActionType.RETAIN,len:M,segmentId:e}):S.push(...it(f,d,e,g.cursor)),g.reset(),g.moveCursor(_)}return m.params.actions=S.serialize(),!!o.syncExecuteCommand(m.id,m.params)}};function it(s,n,e="",t=0){const{startOffset:o,endOffset:i}=s,a=[],{paragraphs:c=[]}=n,l=o-t,u=i-t,d=c==null?void 0:c.find(m=>m.startIndex-t>=l&&m.startIndex-t<=u);if(l>0&&a.push({t:r.TextXActionType.RETAIN,len:l,segmentId:e}),d&&d.startIndex-t>l){const m=d.startIndex-t;a.push({t:r.TextXActionType.DELETE,len:m-l,line:0,segmentId:e}),a.push({t:r.TextXActionType.RETAIN,len:1,segmentId:e}),u>m+1&&a.push({t:r.TextXActionType.DELETE,len:u-m-1,line:0,segmentId:e})}else a.push({t:r.TextXActionType.DELETE,len:u-l,line:0,segmentId:e});return a}const Re={id:"doc.command.delete-left",type:r.CommandType.COMMAND,handler:async s=>{var T;const n=s.get(exports.TextSelectionManagerService),e=s.get(exports.DocSkeletonManagerService),t=s.get(r.IUniverInstanceService),o=s.get(r.ICommandService),i=n.getActiveRange(),a=n.getSelections(),c=(T=e.getCurrent())==null?void 0:T.skeleton;let l=!0;if(i==null||c==null||a==null)return!1;const u=t.getCurrentUniverDocInstance(),{startOffset:d,collapsed:m,segmentId:g,style:S}=i,v=c.findNodeByCharIndex(d),f=D.hasListSpan(v),p=D.isIndentBySpan(v,u.getBody());let _=d;const h=c.findNodeByCharIndex(d-1);if(D.isFirstSpan(v)&&h!==v&&(f===!0||p===!0)){const x=D.getParagraphBySpan(v,u.getBody());if(x==null)return!1;const E=x==null?void 0:x.startIndex,O={startIndex:0},R=x.paragraphStyle;if(f===!0){const I=x.paragraphStyle;I&&(O.paragraphStyle=I)}else if(p===!0){const I=x.bullet;I&&(O.bullet=I),R!=null&&(O.paragraphStyle={...R},delete O.paragraphStyle.hanging,delete O.paragraphStyle.indentStart)}const y=[{startOffset:_,endOffset:_,style:S}];l=await o.executeCommand(me.id,{unitId:u.getUnitId(),updateBody:{dataStream:"",paragraphs:[{...O}]},range:{startOffset:E,endOffset:E+1},textRanges:y,coverType:r.UpdateDocsAttributeType.REPLACE,segmentId:g})}else if(m===!0){if(h==null)return!0;if(h.content==="\r")l=await o.executeCommand(Se.id,{direction:w.LEFT,range:i});else{_-=h.count;const x=[{startOffset:_,endOffset:_,style:S}];l=await o.executeCommand(z.id,{unitId:u.getUnitId(),range:i,segmentId:g,direction:w.LEFT,len:h.count,textRanges:x})}}else{const x=Oe(i,a);l=await o.executeCommand(ge.id,{segmentId:g,textRanges:x})}return l}},ye={id:"doc.command.delete-right",type:r.CommandType.COMMAND,handler:async s=>{var v;const n=s.get(exports.TextSelectionManagerService),e=s.get(exports.DocSkeletonManagerService),t=s.get(r.IUniverInstanceService),o=s.get(r.ICommandService),i=n.getActiveRange(),a=n.getSelections(),c=(v=e.getCurrent())==null?void 0:v.skeleton;let l;if(i==null||c==null||a==null)return!1;const u=t.getCurrentUniverDocInstance(),{startOffset:d,collapsed:m,segmentId:g,style:S}=i;if(d===u.getBody().dataStream.length-2&&m)return!0;if(m===!0){const f=c.findNodeByCharIndex(d);if(f.content==="\r")l=await o.executeCommand(Se.id,{direction:w.RIGHT,range:i});else{const p=[{startOffset:d,endOffset:d,style:S}];l=await o.executeCommand(z.id,{unitId:u.getUnitId(),range:i,segmentId:g,direction:w.RIGHT,textRanges:p,len:f.count})}}else{const f=Oe(i,a);l=await o.executeCommand(ge.id,{segmentId:g,textRanges:f})}return l}},Se={id:"doc.command.merge-two-paragraph",type:r.CommandType.COMMAND,handler:async(s,n)=>{var O,R,y;const e=s.get(exports.TextSelectionManagerService),t=s.get(r.IUniverInstanceService),o=s.get(r.ICommandService);s.get(r.IUndoRedoService);const{direction:i,range:a}=n,c=e.getActiveRange(),l=e.getSelections();if(c==null||l==null)return!1;const u=t.getCurrentUniverDocInstance(),{startOffset:d,collapsed:m,segmentId:g,style:S}=c;if(!m)return!1;const v=i===w.LEFT?d:d+1,f=(y=(R=(O=u.getBody())==null?void 0:O.paragraphs)==null?void 0:R.find(I=>I.startIndex>=v))==null?void 0:y.startIndex,p=st(u.getBody(),v,f),_=i===w.LEFT?d-1:d,h=u.getUnitId(),M=[{startOffset:_,endOffset:_,style:S}],T={id:U.id,params:{unitId:h,actions:[],textRanges:M,prevTextRanges:[a]}},x=new r.TextX;return x.push({t:r.TextXActionType.RETAIN,len:i===w.LEFT?d-1:d,segmentId:g}),p.dataStream.length&&x.push({t:r.TextXActionType.INSERT,body:p,len:p.dataStream.length,line:0,segmentId:g}),x.push({t:r.TextXActionType.RETAIN,len:1,segmentId:g}),x.push({t:r.TextXActionType.DELETE,len:f+1-v,line:0,segmentId:g}),T.params.actions=x.serialize(),!!o.syncExecuteCommand(T.id,T.params)}};function st(s,n,e){const{textRuns:t}=s,o=s.dataStream.substring(n,e);if(t==null)return{dataStream:o};const i=[];for(const a of t){const{st:c,ed:l}=a;l<=n||c>=e||(c<n?i.push({...a,st:0,ed:l-n}):l>e?i.push({...a,st:c-n,ed:e-n}):i.push({...a,st:c-n,ed:l-n}))}return{dataStream:o,textRuns:i}}function Oe(s,n){let e=s.endOffset;for(const o of n){const{startOffset:i,endOffset:a}=o;i==null||a==null||a<=s.endOffset&&(e-=a-i)}return[{startOffset:e,endOffset:e,style:s.style}]}const pe={id:"doc.command.ime-input",type:r.CommandType.COMMAND,handler:async(s,n)=>{const{unitId:e,newText:t,oldTextLen:o,isCompositionEnd:i,isCompositionStart:a}=n,c=s.get(r.ICommandService),l=s.get(j),u=l.getActiveRange();if(u==null)return!1;const{startOffset:d,style:m,segmentId:g}=u,S=t.length,v=[{startOffset:d+S,endOffset:d+S,collapsed:!0,style:m}],f={id:U.id,params:{unitId:e,actions:[],textRanges:v}},p=new r.TextX;!u.collapsed&&a?p.push(...ue(u,g)):p.push({t:r.TextXActionType.RETAIN,len:d,segmentId:g}),o>0&&p.push({t:r.TextXActionType.DELETE,len:o,line:0,segmentId:g}),p.push({t:r.TextXActionType.INSERT,body:{dataStream:t},len:t.length,line:0,segmentId:g}),f.params.actions=p.serialize(),f.params.noHistory=!i,f.params.isCompositionEnd=i;const _=c.syncExecuteCommand(f.id,f.params);return l.pushUndoRedoMutationParams(_,f.params),!!_}},H={id:"doc.command.set-inline-format-bold",type:r.CommandType.COMMAND,handler:async()=>!0},G={id:"doc.command.set-inline-format-italic",type:r.CommandType.COMMAND,handler:async()=>!0},Y={id:"doc.command.set-inline-format-underline",type:r.CommandType.COMMAND,handler:async()=>!0},W={id:"doc.command.set-inline-format-strikethrough",type:r.CommandType.COMMAND,handler:async()=>!0},L={id:"doc.command.set-inline-format-subscript",type:r.CommandType.COMMAND,handler:async()=>!0},Z={id:"doc.command.set-inline-format-superscript",type:r.CommandType.COMMAND,handler:async()=>!0},q={id:"doc.command.set-inline-format-fontsize",type:r.CommandType.COMMAND,handler:async()=>!0},K={id:"doc.command.set-inline-format-font-family",type:r.CommandType.COMMAND,handler:async()=>!0},J={id:"doc.command.set-inline-format-text-color",type:r.CommandType.COMMAND,handler:async()=>!0},De={[H.id]:"bl",[G.id]:"it",[Y.id]:"ul",[W.id]:"st",[q.id]:"fs",[K.id]:"ff",[J.id]:"cl",[L.id]:"va",[Z.id]:"va"},fe={id:"doc.command.set-inline-format",type:r.CommandType.COMMAND,handler:async(s,n)=>{const{segmentId:e,value:t,preCommandId:o}=n,i=s.get(r.ICommandService),a=s.get(exports.TextSelectionManagerService),c=s.get(r.IUniverInstanceService),l=a.getSelections();if(!Array.isArray(l)||l.length===0)return!1;let u=c.getCurrentUniverDocInstance(),d=u.getUnitId();d===r.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY&&(u=c.getUniverDocInstance(r.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),d=u.getUnitId());let m;switch(o){case H.id:case G.id:case Y.id:case W.id:case L.id:case Z.id:{m=ct(u.getBody().textRuns,o,l);break}case q.id:case K.id:{m=t;break}case J.id:{m={rgb:t};break}default:throw new Error(`Unknown command: ${o} in handleInlineFormat`)}const g={id:U.id,params:{unitId:d,actions:[],textRanges:l.map(B)}},S=new r.TextX,v=new r.MemoryCursor;v.reset();for(const p of l){const{startOffset:_,endOffset:h}=p,M={dataStream:"",textRuns:[{st:0,ed:h-_,ts:{[De[o]]:m}}]},T=_-v.cursor;T!==0&&S.push({t:r.TextXActionType.RETAIN,len:T,segmentId:e}),S.push({t:r.TextXActionType.RETAIN,body:M,len:h-_,segmentId:e}),v.reset(),v.moveCursor(h)}return g.params.actions=S.serialize(),!!i.syncExecuteCommand(g.id,g.params)}};function at(s){return s!==null&&typeof s=="object"}function ct(s,n,e){let t=0,o=0;const i=De[n];for(;t!==s.length&&o!==e.length;){const{startOffset:a,endOffset:c}=e[o],{st:l,ed:u,ts:d}=s[t];if(c<=l)o++;else if(u<=a)t++;else{if(/bl|it/.test(i))return(d==null?void 0:d[i])===r.BooleanNumber.TRUE?r.BooleanNumber.FALSE:r.BooleanNumber.TRUE;if(/ul|st/.test(i))return at(d==null?void 0:d[i])&&(d==null?void 0:d[i]).s===r.BooleanNumber.TRUE?{s:r.BooleanNumber.FALSE}:{s:r.BooleanNumber.TRUE};if(/va/.test(i))return n===L.id?(d==null?void 0:d[i])===r.BaselineOffset.SUBSCRIPT?r.BaselineOffset.NORMAL:r.BaselineOffset.SUBSCRIPT:(d==null?void 0:d[i])===r.BaselineOffset.SUPERSCRIPT?r.BaselineOffset.NORMAL:r.BaselineOffset.SUPERSCRIPT;t++}}return/bl|it/.test(i)?r.BooleanNumber.TRUE:/ul|st/.test(i)?{s:r.BooleanNumber.TRUE}:n===L.id?r.BaselineOffset.SUBSCRIPT:r.BaselineOffset.SUPERSCRIPT}const he={id:"doc.command.list-operation",type:r.CommandType.COMMAND,handler:(s,n)=>{var x,E,O;const e=s.get(exports.TextSelectionManagerService),t=s.get(r.IUniverInstanceService),o=s.get(r.ICommandService),{listType:i}=n,a=t.getCurrentUniverDocInstance(),c=e.getActiveRange(),l=(x=e.getSelections())!=null?x:[],u=(E=a.getBody())==null?void 0:E.paragraphs,d=l.map(B);if(c==null||u==null)return!1;const m=lt(c,u),{segmentId:g}=c,S=a.getUnitId(),v=m.every(R=>{var y;return((y=R.bullet)==null?void 0:y.listType)===i});let p=r.Tools.generateRandomId(6);if(m.length===1){const R=u.indexOf(m[0]),y=u[R-1],I=u[R+1];y&&y.bullet&&y.bullet.listType===i?p=y.bullet.listId:I&&I.bullet&&I.bullet.listType===i&&(p=I.bullet.listId)}const _={id:U.id,params:{unitId:S,actions:[],textRanges:d}},h=new r.MemoryCursor;h.reset();const M=new r.TextX;for(const R of m){const{startIndex:y}=R;M.push({t:r.TextXActionType.RETAIN,len:y-h.cursor,segmentId:g});const I={...R.paragraphStyle,hanging:void 0,indentStart:void 0};M.push({t:r.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[v?{paragraphStyle:I,startIndex:0}:{...R,startIndex:0,bullet:{...(O=R.bullet)!=null?O:{nestingLevel:0,textStyle:{fs:20}},listType:i,listId:p}}]},segmentId:g,coverType:r.UpdateDocsAttributeType.REPLACE}),h.moveCursorTo(y+1)}return _.params.actions=M.serialize(),!!o.syncExecuteCommand(_.id,_.params)}},Ee={id:"doc.command.bullet-list",type:r.CommandType.COMMAND,handler:s=>s.get(r.ICommandService).syncExecuteCommand(he.id,{listType:r.PresetListType.BULLET_LIST})},Ae={id:"doc.command.order-list",type:r.CommandType.COMMAND,handler:s=>s.get(r.ICommandService).syncExecuteCommand(he.id,{listType:r.PresetListType.ORDER_LIST})};function lt(s,n){const{startOffset:e,endOffset:t}=s,o=[];let i=-1;for(const a of n){const{startIndex:c}=a;(e>i&&e<=c||t>i&&t<=c||c>=e&&c<=t)&&o.push(a),i=c}return o}const Ue={id:"doc.command-replace-content",type:r.CommandType.COMMAND,handler:async(s,n)=>{var S;const{unitId:e,body:t,textRanges:o,segmentId:i=""}=n,a=s.get(r.IUniverInstanceService),c=s.get(r.ICommandService),l=s.get(exports.TextSelectionManagerService),u=(S=a.getUniverDocInstance(e))==null?void 0:S.getSnapshot().body,d=l.getSelections();if(u==null||!Array.isArray(d)||d.length===0)return!1;const m=be(e,i,u,t);return m.params.textRanges=o,!!c.syncExecuteCommand(m.id,m.params)}},Ne={id:"doc.command-cover-content",type:r.CommandType.COMMAND,handler:async(s,n)=>{var d;const{unitId:e,body:t,segmentId:o=""}=n,i=s.get(r.IUniverInstanceService),a=s.get(r.ICommandService),c=s.get(r.IUndoRedoService),l=(d=i.getUniverDocInstance(e))==null?void 0:d.getSnapshot().body;if(l==null)return!1;const u=be(e,o,l,t);return u.params.noNeedSetTextRange=!0,u.params.noHistory=!0,a.syncExecuteCommand(u.id,u.params),c.clearUndoRedo(e),!0}};function be(s,n,e,t){const o={id:U.id,params:{unitId:s,actions:[],textRanges:[]}},i=new r.TextX,a=(e==null?void 0:e.dataStream.length)-2;return a>0&&i.push({t:r.TextXActionType.DELETE,len:a,line:0,segmentId:n}),t.dataStream.length>0&&i.push({t:r.TextXActionType.INSERT,body:t,len:t.dataStream.length,line:0,segmentId:n}),o.params.actions=i.serialize(),o}const re={id:"doc.operation.move-cursor",type:r.CommandType.OPERATION,handler:(s,n)=>!!n},ie={id:"doc.operation.move-selection",type:r.CommandType.OPERATION,handler:(s,n)=>!!n},dt=(s,n)=>{const e=s.get(r.IUniverInstanceService).getUniverDocInstance(n.unitId),t=(e==null?void 0:e.zoomRatio)||1;return{...r.Tools.deepClone(n),zoomRatio:t}},V={id:"doc.operation.set-zoom-ratio",type:r.CommandType.OPERATION,handler:(s,n)=>{const e=s.get(r.IUniverInstanceService).getUniverDocInstance(n.unitId);if(!e)return!1;const t=e.getSnapshot();return t.settings==null?t.settings={zoomRatio:n.zoomRatio}:t.settings.zoomRatio=n.zoomRatio,!0}},Pe={type:r.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(s,n)=>{var m,g;const e=s.get(r.ICommandService),t=s.get(r.IUndoRedoService),o=s.get(r.IUniverInstanceService);let i=o.getCurrentUniverDocInstance().getUnitId(),a=1;if(n&&(i=(m=n.documentId)!=null?m:i,a=(g=n.zoomRatio)!=null?g:a),!o.getUniverDocInstance(i))return!1;const l={zoomRatio:a,unitId:i},u=dt(s,l);return e.syncExecuteCommand(V.id,l)?(t.pushUndoRedo({unitID:i,undoMutations:[{id:V.id,params:u}],redoMutations:[{id:V.id,params:l}]}),!0):!1}},we={id:"doc.operation.select-all",type:r.CommandType.COMMAND,handler:async s=>{const n=s.get(r.IUniverInstanceService),e=s.get(exports.TextSelectionManagerService),t=n.getCurrentUniverDocInstance().getSnapshot().body;if(t==null)return!1;const o=[{startOffset:0,endOffset:t.dataStream.length-2}];return e.replaceTextRanges(o,!1),!0}};var ut=Object.defineProperty,mt=Object.getOwnPropertyDescriptor,gt=(s,n,e,t)=>{for(var o=t>1?void 0:t?mt(n,e):n,i=s.length-1,a;i>=0;i--)(a=s[i])&&(o=(t?a(n,e,o):a(o))||o);return t&&o&&ut(n,e,o),o},$=(s,n)=>(e,t)=>n(e,t,s);let ee=class extends r.Disposable{constructor(n,e,t,o,i){super();C(this,"_previousIMEContent","");C(this,"_isCompositionStart",!0);C(this,"_onStartSubscription");C(this,"_onUpdateSubscription");C(this,"_onEndSubscription");this._docSkeletonManagerService=n,this._currentUniverService=e,this._textSelectionRenderManager=t,this._imeInputManagerService=o,this._commandService=i,this._initialize()}dispose(){var n,e,t;(n=this._onStartSubscription)==null||n.unsubscribe(),(e=this._onUpdateSubscription)==null||e.unsubscribe(),(t=this._onEndSubscription)==null||t.unsubscribe()}_initialize(){this._initialOnCompositionstart(),this._initialOnCompositionUpdate(),this._initialOnCompositionend()}_initialOnCompositionstart(){this._onStartSubscription=this._textSelectionRenderManager.onCompositionstart$.subscribe(n=>{if(n==null)return;const{activeRange:e}=n;e!=null&&(this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(r.Tools.deepClone(e)),this._isCompositionStart=!0)})}_initialOnCompositionUpdate(){this._onUpdateSubscription=this._textSelectionRenderManager.onCompositionupdate$.subscribe(async n=>{this._updateContent(n,!0)})}_initialOnCompositionend(){this._onEndSubscription=this._textSelectionRenderManager.onCompositionend$.subscribe(n=>{this._updateContent(n,!1)})}async _updateContent(n,e){var u;const t=(u=this._docSkeletonManagerService.getCurrent())==null?void 0:u.skeleton;if(n==null||t==null)return;const o=this._currentUniverService.getCurrentUniverDocInstance(),{event:i,activeRange:a}=n;if(t==null||a==null)return;const l=i.data;l===this._previousIMEContent&&e||(await this._commandService.executeCommand(pe.id,{unitId:o.getUnitId(),newText:l,oldTextLen:this._previousIMEContent.length,isCompositionStart:this._isCompositionStart,isCompositionEnd:!e}),e?(this._isCompositionStart&&(this._isCompositionStart=!1),this._previousIMEContent=l):this._resetIME())}_resetIME(){this._previousIMEContent="",this._isCompositionStart=!0,this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(null)}};ee=gt([r.OnLifecycle(r.LifecycleStages.Rendered,ee),$(0,A.Inject(exports.DocSkeletonManagerService)),$(1,r.IUniverInstanceService),$(2,D.ITextSelectionRenderManager),$(3,A.Inject(j)),$(4,r.ICommandService)],ee);var St=Object.defineProperty,pt=Object.getOwnPropertyDescriptor,ft=(s,n,e,t)=>{for(var o=t>1?void 0:t?pt(n,e):n,i=s.length-1,a;i>=0;i--)(a=s[i])&&(o=(t?a(n,e,o):a(o))||o);return t&&o&&St(n,e,o),o},Ce=(s,n)=>(e,t)=>n(e,t,s);let te=class extends r.Disposable{constructor(s,n){super(),this._textSelectionManagerService=s,this._commandService=n,this._commandExecutedListener()}_commandExecutedListener(){const s=[H.id,G.id,Y.id,W.id,L.id,Z.id,q.id,K.id,J.id];this.disposeWithMe(this._commandService.onCommandExecuted(n=>{s.includes(n.id)&&this._handleInlineFormat(n)}))}_handleInlineFormat(s){var e,t;const{segmentId:n}=(e=this._textSelectionManagerService.getActiveRange())!=null?e:{};n!=null&&this._commandService.executeCommand(fe.id,{segmentId:n,preCommandId:s.id,...(t=s.params)!=null?t:{}})}};te=ft([r.OnLifecycle(r.LifecycleStages.Rendered,te),Ce(0,A.Inject(exports.TextSelectionManagerService)),Ce(1,r.ICommandService)],te);var ht=Object.defineProperty,vt=Object.getOwnPropertyDescriptor,_t=(s,n,e,t)=>{for(var o=t>1?void 0:t?vt(n,e):n,i=s.length-1,a;i>=0;i--)(a=s[i])&&(o=(t?a(n,e,o):a(o))||o);return t&&o&&ht(n,e,o),o},F=(s,n)=>(e,t)=>n(e,t,s);let ne=class extends r.Disposable{constructor(n,e,t,o,i){super();C(this,"_onInputSubscription");this._docSkeletonManagerService=n,this._currentUniverService=e,this._renderManagerService=t,this._textSelectionManagerService=o,this._commandService=i,this._initialize(),this._commandExecutedListener()}dispose(){var n;(n=this._onInputSubscription)==null||n.unsubscribe()}_initialize(){}_commandExecutedListener(){const n=[re.id,ie.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(!n.includes(e.id))return;const t=e.params;switch(e.id){case re.id:return this._handleMoveCursor(t.direction);case ie.id:return this._handleShiftMoveSelection(t.direction);default:throw new Error("Unknown command")}}))}_handleShiftMoveSelection(n){var f,p,_;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getSelections(),o=this._currentUniverService.getCurrentUniverDocInstance(),i=(f=this._docSkeletonManagerService.getCurrent())==null?void 0:f.skeleton,a=this._getDocObject();if(e==null||i==null||a==null)return;const{startOffset:c,endOffset:l,style:u,collapsed:d,direction:m}=e;if(t.length>1){let h=Number.POSITIVE_INFINITY,M=Number.NEGATIVE_INFINITY;for(const T of t)h=Math.min(h,T.startOffset),M=Math.max(M,T.endOffset);this._textSelectionManagerService.replaceTextRanges([{startOffset:n===r.Direction.LEFT||n===r.Direction.UP?M:h,endOffset:n===r.Direction.LEFT||n===r.Direction.UP?h:M,style:u}],!1);return}const g=d||m===D.RANGE_DIRECTION.FORWARD?c:l;let S=d||m===D.RANGE_DIRECTION.FORWARD?l:c;const v=(p=o.getBody().dataStream.length)!=null?p:Number.POSITIVE_INFINITY;if(n===r.Direction.LEFT||n===r.Direction.RIGHT){const h=i.findNodeByCharIndex(S-1),M=i.findNodeByCharIndex(S);S=n===r.Direction.RIGHT?S+M.count:S-((_=h==null?void 0:h.count)!=null?_:0),S=Math.min(v-2,Math.max(0,S)),this._textSelectionManagerService.replaceTextRanges([{startOffset:g,endOffset:S,style:u}],!1)}else{const h=i.findNodeByCharIndex(S),M=a.document.getOffsetConfig(),T=this._getTopOrBottomPosition(i,h,n===r.Direction.DOWN);if(T==null){const E=n===r.Direction.UP?0:v-2;if(E===S)return;this._textSelectionManagerService.replaceTextRanges([{startOffset:g,endOffset:E,style:u}],!1);return}const x=new D.NodePositionConvertToCursor(M,i).getRangePointData(T,T).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{startOffset:g,endOffset:x.endOffset,style:u}],!1)}}_handleMoveCursor(n){var g,S,v;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getSelections(),o=this._currentUniverService.getCurrentUniverDocInstance(),i=(g=this._docSkeletonManagerService.getCurrent())==null?void 0:g.skeleton,a=this._getDocObject();if(e==null||i==null||a==null||t==null)return;const{startOffset:c,endOffset:l,style:u,collapsed:d}=e,m=(S=o.getBody().dataStream.length)!=null?S:Number.POSITIVE_INFINITY;if(n===r.Direction.LEFT||n===r.Direction.RIGHT){let f;if(!e.collapsed||t.length>1){let p=Number.POSITIVE_INFINITY,_=Number.NEGATIVE_INFINITY;for(const h of t)p=Math.min(p,h.startOffset),_=Math.max(_,h.endOffset);f=n===r.Direction.LEFT?p:_}else{const p=i.findNodeByCharIndex(c-1),_=i.findNodeByCharIndex(c);n===r.Direction.LEFT?f=Math.max(0,c-((v=p==null?void 0:p.count)!=null?v:0)):f=Math.min(m-2,l+_.count)}this._textSelectionManagerService.replaceTextRanges([{startOffset:f,endOffset:f,style:u}],!1)}else{const f=i.findNodeByCharIndex(c),p=i.findNodeByCharIndex(l),_=a.document.getOffsetConfig(),h=this._getTopOrBottomPosition(i,n===r.Direction.UP?f:p,n===r.Direction.DOWN);if(h==null){let T;d?T=n===r.Direction.UP?0:m-2:T=n===r.Direction.UP?c:l,this._textSelectionManagerService.replaceTextRanges([{startOffset:T,endOffset:T,style:u}],!1);return}const M=new D.NodePositionConvertToCursor(_,i).getRangePointData(h,h).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{...M,style:u}],!1)}}_getTopOrBottomPosition(n,e,t){if(e==null)return;const o=this._getSpanLeftOffsetInLine(e),i=this._getNextOrPrevLine(e,t);if(i==null)return;const a=this._matchPositionByLeftOffset(n,i,o);if(a!=null)return{...a,isBack:!0}}_getSpanLeftOffsetInLine(n){const e=n.parent;if(e==null)return Number.NEGATIVE_INFINITY;const t=e.left,{left:o}=n;return t+o}_matchPositionByLeftOffset(n,e,t){const o={distance:Number.POSITIVE_INFINITY};for(const i of e.divides){const a=i.left;for(const c of i.spanGroup){const{left:l}=c,u=a+l,d=Math.abs(t-u);d<o.distance&&(o.span=c,o.distance=d)}}if(o.span!=null)return n.findPositionBySpan(o.span)}_getNextOrPrevLine(n,e){var v,f,p,_,h,M,T,x,E,O,R,y;const t=n.parent;if(t==null)return;const o=t.parent;if(o==null)return;const i=o.parent;if(i==null)return;const a=i.lines.indexOf(o);if(a===-1)return;let c;if(e===!0?c=i.lines[a+1]:c=i.lines[a-1],c!=null)return c;const l=i.parent;if(l==null)return;const u=l.columns.indexOf(i);if(u===-1)return;if(e===!0)c=(v=l.columns[u+1])==null?void 0:v.lines[0];else{const I=(p=(f=l.columns)==null?void 0:f[u-1])==null?void 0:p.lines;c=I==null?void 0:I[I.length-1]}if(c!=null)return c;const d=l.parent;if(d==null)return;const m=d.sections.indexOf(l);if(m===-1)return;if(e===!0)c=(h=(_=d.sections[m-1])==null?void 0:_.columns[0])==null?void 0:h.lines[0];else{const I=(T=(M=d.sections)==null?void 0:M[m-1])==null?void 0:T.columns,P=I==null?void 0:I[I.length-1],N=P==null?void 0:P.lines;c=N==null?void 0:N[N.length-1]}if(c!=null)return c;const g=d.parent;if(g==null)return;const S=g.pages.indexOf(d);if(S!==-1){if(e===!0)c=(O=(E=(x=g.pages[S+1])==null?void 0:x.sections[0])==null?void 0:E.columns[0])==null?void 0:O.lines[0];else{const I=(R=g.pages[S-1])==null?void 0:R.sections;if(I==null)return;const P=(y=I[I.length-1])==null?void 0:y.columns,N=P[P.length-1],ve=N==null?void 0:N.lines;c=ve[ve.length-1]}if(c!=null)return c}}_getDocObject(){return le(this._currentUniverService,this._renderManagerService)}};ne=_t([r.OnLifecycle(r.LifecycleStages.Rendered,ne),F(0,A.Inject(exports.DocSkeletonManagerService)),F(1,r.IUniverInstanceService),F(2,D.IRenderManagerService),F(3,A.Inject(exports.TextSelectionManagerService)),F(4,r.ICommandService)],ne);var It=Object.defineProperty,Ct=Object.getOwnPropertyDescriptor,Mt=(s,n,e,t)=>{for(var o=t>1?void 0:t?Ct(n,e):n,i=s.length-1,a;i>=0;i--)(a=s[i])&&(o=(t?a(n,e,o):a(o))||o);return t&&o&&It(n,e,o),o},X=(s,n)=>(e,t)=>n(e,t,s);let oe=class extends r.Disposable{constructor(n,e,t,o,i){super();C(this,"_onInputSubscription");this._docSkeletonManagerService=n,this._currentUniverService=e,this._renderManagerService=t,this._textSelectionRenderManager=o,this._commandService=i,this._initialize(),this._commandExecutedListener()}dispose(){var n;(n=this._onInputSubscription)==null||n.unsubscribe()}_initialize(){this._initialNormalInput()}_initialNormalInput(){this._onInputSubscription=this._textSelectionRenderManager.onInput$.subscribe(async n=>{var v;if(n==null)return;const t=this._currentUniverService.getCurrentUniverDocInstance().getUnitId(),{event:o,content:i="",activeRange:a}=n,c=o,l=(v=this._docSkeletonManagerService.getCurrent())==null?void 0:v.skeleton;if(c.data==null||l==null||!l||!a)return;const{startOffset:u,segmentId:d,style:m}=a,g=i.length,S=[{startOffset:u+g,endOffset:u+g,style:m}];await this._commandService.executeCommand(k.id,{unitId:t,body:{dataStream:i},range:a,textRanges:S,segmentId:d})})}_commandExecutedListener(){}_getDocObject(){return le(this._currentUniverService,this._renderManagerService)}};oe=Mt([r.OnLifecycle(r.LifecycleStages.Rendered,oe),X(0,A.Inject(exports.DocSkeletonManagerService)),X(1,r.IUniverInstanceService),X(2,D.IRenderManagerService),X(3,D.ITextSelectionRenderManager),X(4,r.ICommandService)],oe);var Tt=Object.defineProperty,xt=Object.getOwnPropertyDescriptor,Rt=(s,n,e,t)=>{for(var o=t>1?void 0:t?xt(n,e):n,i=s.length-1,a;i>=0;i--)(a=s[i])&&(o=(t?a(n,e,o):a(o))||o);return t&&o&&Tt(n,e,o),o},Q=(s,n)=>(e,t)=>n(e,t,s);const yt={hasScroll:!0},Ot="docs";var ce;exports.UniverDocsPlugin=(ce=class extends r.Plugin{constructor(e={},t,o,i,a){super(Ot);C(this,"_config");this._injector=t,this._localeService=o,this._configService=i,this._currentUniverService=a,this._config=Object.assign(yt,e),this._initializeDependencies(t),this._initializeCommands()}initialize(){}_initializeCommands(){[re,ie,Re,ye,H,G,Y,W,L,Z,q,K,J,fe,xe,k,z,me,pe,Se,U,Ue,Ne,Pe,V,de,we,Ae,Ee,he].forEach(e=>{this._injector.get(r.ICommandService).registerCommand(e)})}onReady(){this.initialize()}_initializeDependencies(e){[[exports.DocSkeletonManagerService],[exports.DocViewModelManagerService],[exports.DocStateChangeManagerService],[j],[D.ITextSelectionRenderManager,{useClass:D.TextSelectionRenderManager}],[exports.TextSelectionManagerService],[oe],[ee],[te],[ne]].forEach(t=>e.add(t))}},C(ce,"type",r.PluginType.Doc),ce);exports.UniverDocsPlugin=Rt([Q(1,A.Inject(A.Injector)),Q(2,A.Inject(r.LocaleService)),Q(3,r.IConfigService),Q(4,r.IUniverInstanceService)],exports.UniverDocsPlugin);exports.BreakLineCommand=xe;exports.BulletListCommand=Ee;exports.CoverContentCommand=Ne;exports.CutContentCommand=ge;exports.DOCS_COMPONENT_DEFAULT_Z_INDEX=Ve;exports.DOCS_COMPONENT_HEADER_LAYER_INDEX=Xe;exports.DOCS_COMPONENT_MAIN_LAYER_INDEX=Fe;exports.DOCS_VIEW_KEY=Me;exports.DeleteCommand=z;exports.DeleteLeftCommand=Re;exports.DeleteRightCommand=ye;exports.IMEInputCommand=pe;exports.IMEInputManagerService=j;exports.InnerPasteCommand=rt;exports.InsertCommand=k;exports.MoveCursorOperation=re;exports.MoveSelectionOperation=ie;exports.NORMAL_TEXT_SELECTION_PLUGIN_NAME=je;exports.OrderListCommand=Ae;exports.ReplaceContentCommand=Ue;exports.RichTextEditingMutation=U;exports.SelectAllOperation=we;exports.SetDocZoomRatioCommand=Pe;exports.SetDocZoomRatioOperation=V;exports.SetInlineFormatBoldCommand=H;exports.SetInlineFormatCommand=fe;exports.SetInlineFormatFontFamilyCommand=K;exports.SetInlineFormatFontSizeCommand=q;exports.SetInlineFormatItalicCommand=G;exports.SetInlineFormatStrikethroughCommand=W;exports.SetInlineFormatSubscriptCommand=L;exports.SetInlineFormatSuperscriptCommand=Z;exports.SetInlineFormatTextColorCommand=J;exports.SetInlineFormatUnderlineCommand=Y;exports.SetTextSelectionsOperation=de;exports.UpdateCommand=me;exports.VIEWPORT_KEY=Te;exports.getDocObject=le;exports.getDocObjectById=$e;exports.serializeTextRange=B;