@univerjs/docs 0.1.1 → 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 $e=Object.defineProperty;var Fe=(s,n,e)=>n in s?$e(s,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[n]=e;var I=(s,n,e)=>(Fe(s,typeof n!="symbol"?n+"":n,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("@univerjs/core"),R=require("@univerjs/engine-render"),P=require("rxjs"),E=require("@wendellhu/redi");function ue(s,n){const t=s.getCurrentUniverDocInstance().getUnitId(),r=n.getRenderById(t);if(r==null)return;const{mainComponent:o,scene:a,engine:c}=r;return{document:o,scene:a,engine:c}}function Xe(s,n){const e=n.getRenderById(s);if(e==null)return;const{mainComponent:t,scene:r,engine:o}=e;return{document:t,scene:r,engine:o}}var xe=(s=>(s.MAIN="__Document_Render_Main__",s))(xe||{}),Re=(s=>(s.VIEW_MAIN="viewMain",s.VIEW_TOP="viewTop",s.VIEW_LEFT="viewLeft",s.VIEW_LEFT_TOP="viewLeftTop",s))(Re||{});const je=0,ke=2,Ve=10,ze="normalTextSelectionPluginName",me={id:"doc.operation.set-selections",type:i.CommandType.OPERATION,handler:(s,n)=>!0};var He=Object.defineProperty,Ge=Object.getOwnPropertyDescriptor,We=(s,n,e,t)=>{for(var r=t>1?void 0:t?Ge(n,e):n,o=s.length-1,a;o>=0;o--)(a=s[o])&&(r=(t?a(n,e,r):a(r))||r);return t&&r&&He(n,e,r),r},Me=(s,n)=>(e,t)=>n(e,t,s);function k(s){const{startOffset:n,endOffset:e,collapsed:t}=s,r={startOffset:n,endOffset:e,collapsed:t};return typeof s.isActive=="function"&&(r.isActive=s.isActive()),r}exports.TextSelectionManagerService=class extends i.RxDisposable{constructor(e,t){super();I(this,"_currentSelection",null);I(this,"_textSelectionInfo",new Map);I(this,"_textSelection$",new P.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:r,style:o}=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:r,style:o}}add(e,t=!0){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:e,segmentId:"",isEditing:t,style:R.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(P.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:r,subUnitId:o,segmentId:a,style:c,textRanges:l,isEditing:u}=t;this._commandService.executeCommand(me.id,{unitId:r,subUnitId:o,segmentId:a,style:c,isEditing:u,ranges:l.map(k)})}_getTextRanges(e){var o;if(e==null)return;const{unitId:t,subUnitId:r=""}=e;return(o=this._textSelectionInfo.get(t))==null?void 0:o.get(r)}_refresh(e){const t=this._getTextRanges(e);this._textSelectionRenderManager.removeAllTextRanges(),t&&Array.isArray(t.textRanges)&&t.textRanges.length&&this._textSelectionRenderManager.addTextRanges(t.textRanges.map(k))}_replaceByParam(e){const{unitId:t,subUnitId:r,style:o,segmentId:a,textRanges:c,isEditing:l}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map),this._textSelectionInfo.get(t).set(r,{textRanges:c,style:o,segmentId:a,isEditing:l})}_addByParam(e){const{unitId:t,subUnitId:r,textRanges:o,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(r)?u.get(r).textRanges.push(...o):u.set(r,{textRanges:o,style:a,segmentId:c,isEditing:l})}};exports.TextSelectionManagerService=We([Me(0,R.ITextSelectionRenderManager),Me(1,i.ICommandService)],exports.TextSelectionManagerService);function ge(s,n="",e=0){const{startOffset:t,endOffset:r}=s,o=[],a=t-e,c=r-e;return a>0&&o.push({t:i.TextXActionType.RETAIN,len:a,segmentId:n}),o.push({t:i.TextXActionType.DELETE,len:c-a,line:0,segmentId:n}),o}var Ye=Object.defineProperty,Ze=Object.getOwnPropertyDescriptor,qe=(s,n,e,t)=>{for(var r=t>1?void 0:t?Ze(n,e):n,o=s.length-1,a;o>=0;o--)(a=s[o])&&(r=(t?a(n,e,r):a(r))||r);return t&&r&&Ye(n,e,r),r},Ke=(s,n)=>(e,t)=>n(e,t,s);exports.DocViewModelManagerService=class extends i.RxDisposable{constructor(e){super();I(this,"_docViewModelMap",new Map);I(this,"_currentDocViewModel$",new P.BehaviorSubject(null));I(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(P.takeUntil(this.dispose$)).subscribe(e=>{this._create(e)}),this._currentUniverService.getAllUniverDocsInstance().forEach(e=>{this._create(e)}),this._currentUniverService.docDisposed$.pipe(P.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 r;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 o=this._buildDocViewModel(t);this._docViewModelMap.set(e,{unitId:e,docViewModel:o})}if(e===i.DOCS_NORMAL_EDITOR_UNIT_ID_KEY){const o=(r=this._docViewModelMap.get(e))==null?void 0:r.docViewModel;if(o==null)return;o.reset(t)}this._currentDocViewModel$.next(this._docViewModelMap.get(e))}}_buildDocViewModel(e){return new R.DocumentViewModel(e)}};exports.DocViewModelManagerService=qe([Ke(0,i.IUniverInstanceService)],exports.DocViewModelManagerService);var Je=Object.defineProperty,Qe=Object.getOwnPropertyDescriptor,et=(s,n,e,t)=>{for(var r=t>1?void 0:t?Qe(n,e):n,o=s.length-1,a;o>=0;o--)(a=s[o])&&(r=(t?a(n,e,r):a(r))||r);return t&&r&&Je(n,e,r),r},ce=(s,n)=>(e,t)=>n(e,t,s);const tt=300;exports.DocStateChangeManagerService=class extends i.RxDisposable{constructor(e,t,r){super();I(this,"_docStateChange$",new P.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=r,this._initialize()}setChangeState(e){const{trigger:t,noHistory:r}=e;t!=null&&(this._cacheChangeState(e),r||this._docStateChange$.next(e))}_initialize(){this.disposeWithMe(this._commandService.beforeCommandExecuted(e=>{if(e.id===i.UndoCommandId||e.id===i.RedoCommandId){const t=this._univerInstanceService.getCurrentUniverDocInstance();if(t==null)return;const r=t.getUnitId();this._pushHistory(r)}}))}_cacheChangeState(e){const{trigger:t,unitId:r,noHistory:o}=e;if(!(t===i.RedoCommandId||t===i.UndoCommandId||o)){if(this._stateCache.has(r)){const a=this._stateCache.get(r);a==null||a.push(e)}else this._stateCache.set(r,[e]);t===z.id||t===H.id?(this._timer&&clearTimeout(this._timer),this._timer=setTimeout(()=>{this._pushHistory(r)},tt)):this._pushHistory(r)}}_pushHistory(e){const t=this._undoRedoService,r=this._stateCache.get(e);if(!Array.isArray(r)||r.length===0)return;const o=r.length,a=r[0].commandId,c=r[0],l=r[o-1],u={unitId:e,actions:r.reduce((m,g)=>i.TextX.compose(m,g.redoState.actions),[]),textRanges:l.redoState.textRanges},d={unitId:e,actions:r.reverse().reduce((m,g)=>i.TextX.compose(m,g.undoState.actions),[]),textRanges:c.undoState.textRanges};t.pushUndoRedo({unitID:e,undoMutations:[{id:a,params:d}],redoMutations:[{id:a,params:u}]}),r.length=0}};exports.DocStateChangeManagerService=et([ce(0,E.Inject(i.IUndoRedoService)),ce(1,i.ICommandService),ce(2,i.IUniverInstanceService)],exports.DocStateChangeManagerService);class V{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: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((r,o)=>i.TextX.compose(r,o.actions),[]),textRanges:[]};return{redoMutationParams:{unitId:n,actions:this._redoMutationParamsCache.reduce((r,o)=>i.TextX.compose(r,o.actions),[]),textRanges:[]},undoMutationParams:e,previousActiveRange:this._previousActiveRange}}dispose(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[],this._previousActiveRange=null}}const Ce="doc.mutation.rich-text-editing",b={id:Ce,type:i.CommandType.MUTATION,handler:(s,n)=>{var y;const{unitId:e,actions:t,textRanges:r,prevTextRanges:o,trigger:a,noHistory:c,isCompositionEnd:l,noNeedSetTextRange:u}=n,m=s.get(i.IUniverInstanceService).getUniverDocInstance(e),S=s.get(exports.DocViewModelManagerService).getViewModel(e),v=s.get(exports.TextSelectionManagerService),h=((y=v.getSelections())!=null?y:[]).map(k),_=s.get(exports.DocStateChangeManagerService),f=s.get(V);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:h};const C=m.apply(t),{segmentId:T}=t[0],x=m.getSelfOrHeaderFooterModel(T);S.getSelfOrHeaderFooterViewModel(T).reset(x),!u&&r&&a!=null&&queueMicrotask(()=>{v.replaceTextRanges(r)});const D={commandId:Ce,unitId:e,trigger:a,noHistory:c,redoState:{actions:t,textRanges:r},undoState:{actions:C,textRanges:o!=null?o:h}};if(l){const O=f.fetchComposedUndoRedoMutationParams();if(O==null)throw new Error("historyParams is null in RichTextEditingMutation");const{undoMutationParams:M,redoMutationParams:N,previousActiveRange:A}=O;D.redoState.actions=N.actions,D.undoState.actions=M.actions,D.undoState.textRanges=[A]}return _.setChangeState(D),{unitId:e,actions:C,textRanges:h}}},z={id:"doc.command.insert-text",type:i.CommandType.COMMAND,handler:async(s,n)=>{const e=s.get(i.ICommandService),{range:t,segmentId:r,body:o,unitId:a,textRanges:c}=n,{startOffset:l,collapsed:u}=t,d={id:b.id,params:{unitId:a,actions:[],textRanges:c}},m=new i.TextX;return u?l>0&&m.push({t:i.TextXActionType.RETAIN,len:l,segmentId:r}):m.push(...ge(t,r)),m.push({t:i.TextXActionType.INSERT,body:o,len:o.dataStream.length,line:0,segmentId:r}),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 H={id:"doc.command.delete-text",type:i.CommandType.COMMAND,handler:async(s,n)=>{const e=s.get(i.ICommandService),{range:t,segmentId:r,unitId:o,direction:a,textRanges:c,len:l=1}=n,{startOffset:u}=t,d={id:b.id,params:{unitId:o,actions:[],textRanges:c}},m=new i.TextX;return u>0&&m.push({t:i.TextXActionType.RETAIN,len:a===0?u-l:u,segmentId:r}),m.push({t:i.TextXActionType.DELETE,len:l,line:0,segmentId:r}),d.params.actions=m.serialize(),!!e.syncExecuteCommand(d.id,d.params)}},Se={id:"doc.command.update-text",type:i.CommandType.COMMAND,handler:async(s,n)=>{const{range:e,segmentId:t,updateBody:r,coverType:o,unitId:a,textRanges:c}=n,l=s.get(i.ICommandService),u={id:b.id,params:{unitId:a,actions:[],textRanges:c}},d=new i.TextX,{startOffset:m,endOffset:g}=e;return d.push({t:i.TextXActionType.RETAIN,len:m,segmentId:t}),d.push({t:i.TextXActionType.RETAIN,body:r,len:g-m,segmentId:t,coverType:o}),u.params.actions=d.serialize(),!!l.syncExecuteCommand(u.id,u.params)}};function nt(s,n){const e=[];for(let t=0,r=s.length;t<r;t++)s[t]===i.DataStreamTreeTokenType.PARAGRAPH&&e.push({startIndex:t});if(n)for(const t of e)n.bullet&&(t.bullet=i.Tools.deepClone(n.bullet)),n.paragraphStyle&&(t.paragraphStyle=i.Tools.deepClone(n.paragraphStyle));return e}const ye={id:"doc.command.break-line",type:i.CommandType.COMMAND,handler:async s=>{var v,p;const n=s.get(exports.TextSelectionManagerService),e=s.get(i.IUniverInstanceService),t=s.get(i.ICommandService),r=n.getActiveRange();if(r==null)return!1;const o=e.getCurrentUniverDocInstance(),a=o.getUnitId(),{startOffset:c,segmentId:l,style:u}=r,d=[{startOffset:c+1,endOffset:c+1,style:u}],g=((p=(v=o.getBody())==null?void 0:v.paragraphs)!=null?p:[]).find(h=>h.startIndex>=c);return await t.executeCommand(z.id,{unitId:a,body:{dataStream:i.DataStreamTreeTokenType.PARAGRAPH,paragraphs:nt(i.DataStreamTreeTokenType.PARAGRAPH,g)},range:r,textRanges:d,segmentId:l})}};var rt=Object.defineProperty,it=Object.getOwnPropertyDescriptor,ot=(s,n,e,t)=>{for(var r=t>1?void 0:t?it(n,e):n,o=s.length-1,a;o>=0;o--)(a=s[o])&&(r=(t?a(n,e,r):a(r))||r);return t&&r&&rt(n,e,r),r},le=(s,n)=>(e,t)=>n(e,t,s);exports.DocSkeletonManagerService=class extends i.RxDisposable{constructor(e,t,r){super();I(this,"_currentSkeletonUnitId","");I(this,"_docSkeletonMap",new Map);I(this,"_currentSkeleton$",new P.BehaviorSubject(null));I(this,"currentSkeleton$",this._currentSkeleton$.asObservable());I(this,"_currentSkeletonBefore$",new P.BehaviorSubject(null));I(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());this._localeService=e,this._docViewModelManagerService=t,this._currentUniverService=r,this._initialize()}_initialize(){this._init()}dispose(){this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete(),this._docSkeletonMap.clear()}_init(){this._docViewModelManagerService.currentDocViewModel$.pipe(P.takeUntil(this.dispose$)).subscribe(e=>{e!=null&&this._setCurrent(e)}),this._docViewModelManagerService.getAllModel().forEach(e=>{e!=null&&this._setCurrent(e)}),this._currentUniverService.docDisposed$.pipe(P.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 r=this.getSkeletonByUnitId(e);r!=null&&(r.dirty=t)}getSkeletonByUnitId(e){return this._docSkeletonMap.get(e)}_setCurrent(e){const{unitId:t}=e;if(this._docSkeletonMap.has(t)){const r=this.getSkeletonByUnitId(t);r.skeleton.calculate(),r.dirty=!0}else{const r=this._buildSkeleton(e.docViewModel);r.calculate(),this._docSkeletonMap.set(t,{unitId:t,skeleton:r,dirty:!1})}return this._currentSkeletonUnitId=t,this._currentSkeletonBefore$.next(this.getCurrent()),this._currentSkeleton$.next(this.getCurrent()),this.getCurrent()}_buildSkeleton(e){return R.DocumentSkeleton.create(e,this._localeService)}};exports.DocSkeletonManagerService=ot([le(0,E.Inject(i.LocaleService)),le(1,E.Inject(exports.DocViewModelManagerService)),le(2,i.IUniverInstanceService)],exports.DocSkeletonManagerService);const st={id:"doc.command.inner-paste",type:i.CommandType.COMMAND,handler:async(s,n)=>{const{segmentId:e,body:t,textRanges:r}=n,o=s.get(i.ICommandService),a=s.get(exports.TextSelectionManagerService),c=s.get(i.IUniverInstanceService),l=a.getSelections();if(!Array.isArray(l)||l.length===0)return!1;const d=c.getCurrentUniverDocInstance().getUnitId(),m={id:b.id,params:{unitId:d,actions:[],textRanges:r}},g=new i.MemoryCursor;g.reset();const S=new i.TextX;for(const p of l){const{startOffset:h,endOffset:_,collapsed:f}=p,C=h-g.cursor;f?S.push({t:i.TextXActionType.RETAIN,len:C,segmentId:e}):S.push(...ge(p,e,g.cursor)),S.push({t:i.TextXActionType.INSERT,body:t,len:t.dataStream.length,line:0,segmentId:e}),g.reset(),g.moveCursor(_)}return m.params.actions=S.serialize(),!!o.syncExecuteCommand(m.id,m.params)}},he={id:"doc.command.inner-cut",type:i.CommandType.COMMAND,handler:async(s,n)=>{const{segmentId:e,textRanges:t}=n,r=s.get(i.ICommandService),o=s.get(exports.TextSelectionManagerService),a=s.get(i.IUniverInstanceService),c=o.getSelections();if(!Array.isArray(c)||c.length===0)return!1;const l=a.getCurrentUniverDocInstance().getUnitId(),u=a.getUniverDocInstance(l),d=i.getDocsUpdateBody(u.snapshot,e);if(d==null)return!1;const m={id:b.id,params:{unitId:l,actions:[],textRanges:t}},g=new i.MemoryCursor;g.reset();const S=new i.TextX;for(const p of c){const{startOffset:h,endOffset:_,collapsed:f}=p,C=h-g.cursor;f?S.push({t:i.TextXActionType.RETAIN,len:C,segmentId:e}):S.push(...at(p,d,e,g.cursor)),g.reset(),g.moveCursor(_)}return m.params.actions=S.serialize(),!!r.syncExecuteCommand(m.id,m.params)}};function at(s,n,e="",t=0){const{startOffset:r,endOffset:o}=s,a=[],{paragraphs:c=[]}=n,l=r-t,u=o-t,d=c==null?void 0:c.find(m=>m.startIndex-t>=l&&m.startIndex-t<=u);if(l>0&&a.push({t:i.TextXActionType.RETAIN,len:l,segmentId:e}),d&&d.startIndex-t>l){const m=d.startIndex-t;a.push({t:i.TextXActionType.DELETE,len:m-l,line:0,segmentId:e}),a.push({t:i.TextXActionType.RETAIN,len:1,segmentId:e}),u>m+1&&a.push({t:i.TextXActionType.DELETE,len:u-m-1,line:0,segmentId:e})}else a.push({t:i.TextXActionType.DELETE,len:u-l,line:0,segmentId:e});return a}const Oe={id:"doc.command.delete-left",type:i.CommandType.COMMAND,handler:async s=>{var T;const n=s.get(exports.TextSelectionManagerService),e=s.get(exports.DocSkeletonManagerService),t=s.get(i.IUniverInstanceService),r=s.get(i.ICommandService),o=n.getActiveRange(),a=n.getSelections(),c=(T=e.getCurrent())==null?void 0:T.skeleton;let l=!0;if(o==null||c==null||a==null)return!1;const u=t.getCurrentUniverDocInstance(),{startOffset:d,collapsed:m,segmentId:g,style:S}=o,v=c.findNodeByCharIndex(d),p=R.hasListSpan(v),h=R.isIndentBySpan(v,u.getBody());let _=d;const f=c.findNodeByCharIndex(d-1);if(R.isFirstSpan(v)&&f!==v&&(p===!0||h===!0)){const x=R.getParagraphBySpan(v,u.getBody());if(x==null)return!1;const U=x==null?void 0:x.startIndex,D={startIndex:0},y=x.paragraphStyle;if(p===!0){const M=x.paragraphStyle;M&&(D.paragraphStyle=M)}else if(h===!0){const M=x.bullet;M&&(D.bullet=M),y!=null&&(D.paragraphStyle={...y},delete D.paragraphStyle.hanging,delete D.paragraphStyle.indentStart)}const O=[{startOffset:_,endOffset:_,style:S}];l=await r.executeCommand(Se.id,{unitId:u.getUnitId(),updateBody:{dataStream:"",paragraphs:[{...D}]},range:{startOffset:U,endOffset:U+1},textRanges:O,coverType:i.UpdateDocsAttributeType.REPLACE,segmentId:g})}else if(m===!0){if(f==null)return!0;if(f.content==="\r")l=await r.executeCommand(pe.id,{direction:w.LEFT,range:o});else{_-=f.count;const x=[{startOffset:_,endOffset:_,style:S}];l=await r.executeCommand(H.id,{unitId:u.getUnitId(),range:o,segmentId:g,direction:w.LEFT,len:f.count,textRanges:x})}}else{const x=Ee(o,a);l=await r.executeCommand(he.id,{segmentId:g,textRanges:x})}return l}},De={id:"doc.command.delete-right",type:i.CommandType.COMMAND,handler:async s=>{var v;const n=s.get(exports.TextSelectionManagerService),e=s.get(exports.DocSkeletonManagerService),t=s.get(i.IUniverInstanceService),r=s.get(i.ICommandService),o=n.getActiveRange(),a=n.getSelections(),c=(v=e.getCurrent())==null?void 0:v.skeleton;let l;if(o==null||c==null||a==null)return!1;const u=t.getCurrentUniverDocInstance(),{startOffset:d,collapsed:m,segmentId:g,style:S}=o;if(d===u.getBody().dataStream.length-2&&m)return!0;if(m===!0){const p=c.findNodeByCharIndex(d);if(p.content==="\r")l=await r.executeCommand(pe.id,{direction:w.RIGHT,range:o});else{const h=[{startOffset:d,endOffset:d,style:S}];l=await r.executeCommand(H.id,{unitId:u.getUnitId(),range:o,segmentId:g,direction:w.RIGHT,textRanges:h,len:p.count})}}else{const p=Ee(o,a);l=await r.executeCommand(he.id,{segmentId:g,textRanges:p})}return l}},pe={id:"doc.command.merge-two-paragraph",type:i.CommandType.COMMAND,handler:async(s,n)=>{var D,y,O;const e=s.get(exports.TextSelectionManagerService),t=s.get(i.IUniverInstanceService),r=s.get(i.ICommandService);s.get(i.IUndoRedoService);const{direction:o,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=o===w.LEFT?d:d+1,p=(O=(y=(D=u.getBody())==null?void 0:D.paragraphs)==null?void 0:y.find(M=>M.startIndex>=v))==null?void 0:O.startIndex,h=ct(u.getBody(),v,p),_=o===w.LEFT?d-1:d,f=u.getUnitId(),C=[{startOffset:_,endOffset:_,style:S}],T={id:b.id,params:{unitId:f,actions:[],textRanges:C,prevTextRanges:[a]}},x=new i.TextX;return x.push({t:i.TextXActionType.RETAIN,len:o===w.LEFT?d-1:d,segmentId:g}),h.dataStream.length&&x.push({t:i.TextXActionType.INSERT,body:h,len:h.dataStream.length,line:0,segmentId:g}),x.push({t:i.TextXActionType.RETAIN,len:1,segmentId:g}),x.push({t:i.TextXActionType.DELETE,len:p+1-v,line:0,segmentId:g}),T.params.actions=x.serialize(),!!r.syncExecuteCommand(T.id,T.params)}};function ct(s,n,e){const{textRuns:t}=s,r=s.dataStream.substring(n,e);if(t==null)return{dataStream:r};const o=[];for(const a of t){const{st:c,ed:l}=a;l<=n||c>=e||(c<n?o.push({...a,st:0,ed:l-n}):l>e?o.push({...a,st:c-n,ed:e-n}):o.push({...a,st:c-n,ed:l-n}))}return{dataStream:r,textRuns:o}}function Ee(s,n){let e=s.endOffset;for(const r of n){const{startOffset:o,endOffset:a}=r;o==null||a==null||a<=s.endOffset&&(e-=a-o)}return[{startOffset:e,endOffset:e,style:s.style}]}const fe={id:"doc.command.ime-input",type:i.CommandType.COMMAND,handler:async(s,n)=>{const{unitId:e,newText:t,oldTextLen:r,isCompositionEnd:o,isCompositionStart:a}=n,c=s.get(i.ICommandService),l=s.get(V),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}],p={id:b.id,params:{unitId:e,actions:[],textRanges:v}},h=new i.TextX;!u.collapsed&&a?h.push(...ge(u,g)):h.push({t:i.TextXActionType.RETAIN,len:d,segmentId:g}),r>0&&h.push({t:i.TextXActionType.DELETE,len:r,line:0,segmentId:g}),h.push({t:i.TextXActionType.INSERT,body:{dataStream:t},len:t.length,line:0,segmentId:g}),p.params.actions=h.serialize(),p.params.noHistory=!o,p.params.isCompositionEnd=o;const _=c.syncExecuteCommand(p.id,p.params);return l.pushUndoRedoMutationParams(_,p.params),!!_}},G={id:"doc.command.set-inline-format-bold",type:i.CommandType.COMMAND,handler:async()=>!0},W={id:"doc.command.set-inline-format-italic",type:i.CommandType.COMMAND,handler:async()=>!0},Y={id:"doc.command.set-inline-format-underline",type:i.CommandType.COMMAND,handler:async()=>!0},Z={id:"doc.command.set-inline-format-strikethrough",type:i.CommandType.COMMAND,handler:async()=>!0},L={id:"doc.command.set-inline-format-subscript",type:i.CommandType.COMMAND,handler:async()=>!0},q={id:"doc.command.set-inline-format-superscript",type:i.CommandType.COMMAND,handler:async()=>!0},K={id:"doc.command.set-inline-format-fontsize",type:i.CommandType.COMMAND,handler:async()=>!0},J={id:"doc.command.set-inline-format-font-family",type:i.CommandType.COMMAND,handler:async()=>!0},Q={id:"doc.command.set-inline-format-text-color",type:i.CommandType.COMMAND,handler:async()=>!0},Ue={[G.id]:"bl",[W.id]:"it",[Y.id]:"ul",[Z.id]:"st",[K.id]:"fs",[J.id]:"ff",[Q.id]:"cl",[L.id]:"va",[q.id]:"va"},ve={id:"doc.command.set-inline-format",type:i.CommandType.COMMAND,handler:async(s,n)=>{const{segmentId:e,value:t,preCommandId:r}=n,o=s.get(i.ICommandService),a=s.get(exports.TextSelectionManagerService),c=s.get(i.IUniverInstanceService),l=a.getSelections();if(!Array.isArray(l)||l.length===0)return!1;let u=c.getCurrentUniverDocInstance(),d=u.getUnitId();d===i.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY&&(u=c.getUniverDocInstance(i.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),d=u.getUnitId());let m;switch(r){case G.id:case W.id:case Y.id:case Z.id:case L.id:case q.id:{m=dt(u.getBody().textRuns,r,l);break}case K.id:case J.id:{m=t;break}case Q.id:{m={rgb:t};break}default:throw new Error(`Unknown command: ${r} in handleInlineFormat`)}const g={id:b.id,params:{unitId:d,actions:[],textRanges:l}},S=new i.TextX,v=new i.MemoryCursor;v.reset();for(const h of l){const{startOffset:_,endOffset:f}=h,C={dataStream:"",textRuns:[{st:0,ed:f-_,ts:{[Ue[r]]:m}}]},T=_-v.cursor;T!==0&&S.push({t:i.TextXActionType.RETAIN,len:T,segmentId:e}),S.push({t:i.TextXActionType.RETAIN,body:C,len:f-_,segmentId:e}),v.reset(),v.moveCursor(f)}return g.params.actions=S.serialize(),!!o.syncExecuteCommand(g.id,g.params)}};function lt(s){return s!==null&&typeof s=="object"}function dt(s,n,e){let t=0,r=0;const o=Ue[n];for(;t!==s.length&&r!==e.length;){const{startOffset:a,endOffset:c}=e[r],{st:l,ed:u,ts:d}=s[t];if(c<=l)r++;else if(u<=a)t++;else{if(/bl|it/.test(o))return(d==null?void 0:d[o])===i.BooleanNumber.TRUE?i.BooleanNumber.FALSE:i.BooleanNumber.TRUE;if(/ul|st/.test(o))return lt(d==null?void 0:d[o])&&(d==null?void 0:d[o]).s===i.BooleanNumber.TRUE?{s:i.BooleanNumber.FALSE}:{s:i.BooleanNumber.TRUE};if(/va/.test(o))return n===L.id?(d==null?void 0:d[o])===i.BaselineOffset.SUBSCRIPT?i.BaselineOffset.NORMAL:i.BaselineOffset.SUBSCRIPT:(d==null?void 0:d[o])===i.BaselineOffset.SUPERSCRIPT?i.BaselineOffset.NORMAL:i.BaselineOffset.SUPERSCRIPT;t++}}return/bl|it/.test(o)?i.BooleanNumber.TRUE:/ul|st/.test(o)?{s:i.BooleanNumber.TRUE}:n===L.id?i.BaselineOffset.SUBSCRIPT:i.BaselineOffset.SUPERSCRIPT}const _e={id:"doc.command.list-operation",type:i.CommandType.COMMAND,handler:(s,n)=>{var x,U,D;const e=s.get(exports.TextSelectionManagerService),t=s.get(i.IUniverInstanceService),r=s.get(i.ICommandService),{listType:o}=n,a=t.getCurrentUniverDocInstance(),c=e.getActiveRange(),l=(x=e.getSelections())!=null?x:[],u=(U=a.getBody())==null?void 0:U.paragraphs,d=l.map(k);if(c==null||u==null)return!1;const m=ut(c,u),{segmentId:g}=c,S=a.getUnitId(),v=m.every(y=>{var O;return((O=y.bullet)==null?void 0:O.listType)===o});let h=i.Tools.generateRandomId(6);if(m.length===1){const y=u.indexOf(m[0]),O=u[y-1],M=u[y+1];O&&O.bullet&&O.bullet.listType===o?h=O.bullet.listId:M&&M.bullet&&M.bullet.listType===o&&(h=M.bullet.listId)}const _={id:b.id,params:{unitId:S,actions:[],textRanges:d}},f=new i.MemoryCursor;f.reset();const C=new i.TextX;for(const y of m){const{startIndex:O}=y;C.push({t:i.TextXActionType.RETAIN,len:O-f.cursor,segmentId:g});const M={...y.paragraphStyle,hanging:void 0,indentStart:void 0};C.push({t:i.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[v?{paragraphStyle:M,startIndex:0}:{...y,startIndex:0,bullet:{...(D=y.bullet)!=null?D:{nestingLevel:0,textStyle:{fs:20}},listType:o,listId:h}}]},segmentId:g,coverType:i.UpdateDocsAttributeType.REPLACE}),f.moveCursorTo(O+1)}return _.params.actions=C.serialize(),!!r.syncExecuteCommand(_.id,_.params)}},be={id:"doc.command.bullet-list",type:i.CommandType.COMMAND,handler:s=>s.get(i.ICommandService).syncExecuteCommand(_e.id,{listType:i.PresetListType.BULLET_LIST})},Ae={id:"doc.command.order-list",type:i.CommandType.COMMAND,handler:s=>s.get(i.ICommandService).syncExecuteCommand(_e.id,{listType:i.PresetListType.ORDER_LIST})};function ut(s,n){const{startOffset:e,endOffset:t}=s,r=[];let o=-1;for(const a of n){const{startIndex:c}=a;(e>o&&e<=c||t>o&&t<=c||c>=e&&c<=t)&&r.push(a),o=c}return r}const Pe={id:"doc.command-replace-content",type:i.CommandType.COMMAND,handler:async(s,n)=>{var S;const{unitId:e,body:t,textRanges:r,segmentId:o=""}=n,a=s.get(i.IUniverInstanceService),c=s.get(i.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=we(e,o,u,t);return m.params.textRanges=r,!!c.syncExecuteCommand(m.id,m.params)}},Ne={id:"doc.command-cover-content",type:i.CommandType.COMMAND,handler:async(s,n)=>{var d;const{unitId:e,body:t,segmentId:r=""}=n,o=s.get(i.IUniverInstanceService),a=s.get(i.ICommandService),c=s.get(i.IUndoRedoService),l=(d=o.getUniverDocInstance(e))==null?void 0:d.getSnapshot().body;if(l==null)return!1;const u=we(e,r,l,t);return u.params.noNeedSetTextRange=!0,u.params.noHistory=!0,a.syncExecuteCommand(u.id,u.params),c.clearUndoRedo(e),!0}};function we(s,n,e,t){const r={id:b.id,params:{unitId:s,actions:[],textRanges:[]}},o=new i.TextX,a=(e==null?void 0:e.dataStream.length)-2;return a>0&&o.push({t:i.TextXActionType.DELETE,len:a,line:0,segmentId:n}),t.dataStream.length>0&&o.push({t:i.TextXActionType.INSERT,body:t,len:t.dataStream.length,line:0,segmentId:n}),r.params.actions=o.serialize(),r}const se={id:"doc.operation.move-cursor",type:i.CommandType.OPERATION,handler:(s,n)=>!!n},ae={id:"doc.operation.move-selection",type:i.CommandType.OPERATION,handler:(s,n)=>!!n},mt=(s,n)=>{const e=s.get(i.IUniverInstanceService).getUniverDocInstance(n.unitId),t=(e==null?void 0:e.zoomRatio)||1;return{...i.Tools.deepClone(n),zoomRatio:t}},$={id:"doc.operation.set-zoom-ratio",type:i.CommandType.OPERATION,handler:(s,n)=>{const e=s.get(i.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}},Le={type:i.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(s,n)=>{var m,g;const e=s.get(i.ICommandService),t=s.get(i.IUndoRedoService),r=s.get(i.IUniverInstanceService);let o=r.getCurrentUniverDocInstance().getUnitId(),a=1;if(n&&(o=(m=n.documentId)!=null?m:o,a=(g=n.zoomRatio)!=null?g:a),!r.getUniverDocInstance(o))return!1;const l={zoomRatio:a,unitId:o},u=mt(s,l);return e.syncExecuteCommand($.id,l)?(t.pushUndoRedo({unitID:o,undoMutations:[{id:$.id,params:u}],redoMutations:[{id:$.id,params:l}]}),!0):!1}},Be={id:"doc.operation.select-all",type:i.CommandType.COMMAND,handler:async s=>{const n=s.get(i.IUniverInstanceService),e=s.get(exports.TextSelectionManagerService),t=n.getCurrentUniverDocInstance().getSnapshot().body;if(t==null)return!1;const r=[{startOffset:0,endOffset:t.dataStream.length-2}];return e.replaceTextRanges(r,!1),!0}};var gt=Object.defineProperty,St=Object.getOwnPropertyDescriptor,ht=(s,n,e,t)=>{for(var r=t>1?void 0:t?St(n,e):n,o=s.length-1,a;o>=0;o--)(a=s[o])&&(r=(t?a(n,e,r):a(r))||r);return t&&r&&gt(n,e,r),r},F=(s,n)=>(e,t)=>n(e,t,s);let te=class extends i.Disposable{constructor(n,e,t,r,o){super();I(this,"_previousIMEContent","");I(this,"_isCompositionStart",!0);I(this,"_onStartSubscription");I(this,"_onUpdateSubscription");I(this,"_onEndSubscription");this._docSkeletonManagerService=n,this._currentUniverService=e,this._textSelectionRenderManager=t,this._imeInputManagerService=r,this._commandService=o,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(i.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 r=this._currentUniverService.getCurrentUniverDocInstance(),{event:o,activeRange:a}=n;if(t==null||a==null)return;const l=o.data;l===this._previousIMEContent&&e||(await this._commandService.executeCommand(fe.id,{unitId:r.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)}};te=ht([i.OnLifecycle(i.LifecycleStages.Rendered,te),F(0,E.Inject(exports.DocSkeletonManagerService)),F(1,i.IUniverInstanceService),F(2,R.ITextSelectionRenderManager),F(3,E.Inject(V)),F(4,i.ICommandService)],te);var pt=Object.defineProperty,ft=Object.getOwnPropertyDescriptor,vt=(s,n,e,t)=>{for(var r=t>1?void 0:t?ft(n,e):n,o=s.length-1,a;o>=0;o--)(a=s[o])&&(r=(t?a(n,e,r):a(r))||r);return t&&r&&pt(n,e,r),r},Te=(s,n)=>(e,t)=>n(e,t,s);let ne=class extends i.Disposable{constructor(s,n){super(),this._textSelectionManagerService=s,this._commandService=n,this._commandExecutedListener()}_commandExecutedListener(){const s=[G.id,W.id,Y.id,Z.id,L.id,q.id,K.id,J.id,Q.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(ve.id,{segmentId:n,preCommandId:s.id,...(t=s.params)!=null?t:{}})}};ne=vt([i.OnLifecycle(i.LifecycleStages.Rendered,ne),Te(0,E.Inject(exports.TextSelectionManagerService)),Te(1,i.ICommandService)],ne);var _t=Object.defineProperty,It=Object.getOwnPropertyDescriptor,Mt=(s,n,e,t)=>{for(var r=t>1?void 0:t?It(n,e):n,o=s.length-1,a;o>=0;o--)(a=s[o])&&(r=(t?a(n,e,r):a(r))||r);return t&&r&&_t(n,e,r),r},X=(s,n)=>(e,t)=>n(e,t,s);let re=class extends i.Disposable{constructor(n,e,t,r,o){super();I(this,"_onInputSubscription");this._docSkeletonManagerService=n,this._currentUniverService=e,this._renderManagerService=t,this._textSelectionManagerService=r,this._commandService=o,this._initialize(),this._commandExecutedListener()}dispose(){var n;(n=this._onInputSubscription)==null||n.unsubscribe()}_initialize(){}_commandExecutedListener(){const n=[se.id,ae.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(!n.includes(e.id))return;const t=e.params;switch(e.id){case se.id:return this._handleMoveCursor(t.direction);case ae.id:return this._handleShiftMoveSelection(t.direction);default:throw new Error("Unknown command")}}))}_handleShiftMoveSelection(n){var p,h,_;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getSelections(),r=this._currentUniverService.getCurrentUniverDocInstance(),o=(p=this._docSkeletonManagerService.getCurrent())==null?void 0:p.skeleton,a=this._getDocObject();if(e==null||o==null||a==null)return;const{startOffset:c,endOffset:l,style:u,collapsed:d,direction:m}=e;if(t.length>1){let f=Number.POSITIVE_INFINITY,C=Number.NEGATIVE_INFINITY;for(const T of t)f=Math.min(f,T.startOffset),C=Math.max(C,T.endOffset);this._textSelectionManagerService.replaceTextRanges([{startOffset:n===i.Direction.LEFT||n===i.Direction.UP?C:f,endOffset:n===i.Direction.LEFT||n===i.Direction.UP?f:C,style:u}],!1);return}const g=d||m===R.RANGE_DIRECTION.FORWARD?c:l;let S=d||m===R.RANGE_DIRECTION.FORWARD?l:c;const v=(h=r.getBody().dataStream.length)!=null?h:Number.POSITIVE_INFINITY;if(n===i.Direction.LEFT||n===i.Direction.RIGHT){const f=o.findNodeByCharIndex(S-1),C=o.findNodeByCharIndex(S);S=n===i.Direction.RIGHT?S+C.count:S-((_=f==null?void 0:f.count)!=null?_:0),S=Math.min(v-2,Math.max(0,S)),this._textSelectionManagerService.replaceTextRanges([{startOffset:g,endOffset:S,style:u}],!1)}else{const f=o.findNodeByCharIndex(S),C=a.document.getOffsetConfig(),T=this._getTopOrBottomPosition(o,f,n===i.Direction.DOWN);if(T==null){const U=n===i.Direction.UP?0:v-2;if(U===S)return;this._textSelectionManagerService.replaceTextRanges([{startOffset:g,endOffset:U,style:u}],!1);return}const x=new R.NodePositionConvertToCursor(C,o).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(),r=this._currentUniverService.getCurrentUniverDocInstance(),o=(g=this._docSkeletonManagerService.getCurrent())==null?void 0:g.skeleton,a=this._getDocObject();if(e==null||o==null||a==null||t==null)return;const{startOffset:c,endOffset:l,style:u,collapsed:d}=e,m=(S=r.getBody().dataStream.length)!=null?S:Number.POSITIVE_INFINITY;if(n===i.Direction.LEFT||n===i.Direction.RIGHT){let p;if(!e.collapsed||t.length>1){let h=Number.POSITIVE_INFINITY,_=Number.NEGATIVE_INFINITY;for(const f of t)h=Math.min(h,f.startOffset),_=Math.max(_,f.endOffset);p=n===i.Direction.LEFT?h:_}else{const h=o.findNodeByCharIndex(c-1),_=o.findNodeByCharIndex(c);n===i.Direction.LEFT?p=Math.max(0,c-((v=h==null?void 0:h.count)!=null?v:0)):p=Math.min(m-2,l+_.count)}this._textSelectionManagerService.replaceTextRanges([{startOffset:p,endOffset:p,style:u}],!1)}else{const p=o.findNodeByCharIndex(c),h=o.findNodeByCharIndex(l),_=a.document.getOffsetConfig(),f=this._getTopOrBottomPosition(o,n===i.Direction.UP?p:h,n===i.Direction.DOWN);if(f==null){let T;d?T=n===i.Direction.UP?0:m-2:T=n===i.Direction.UP?c:l,this._textSelectionManagerService.replaceTextRanges([{startOffset:T,endOffset:T,style:u}],!1);return}const C=new R.NodePositionConvertToCursor(_,o).getRangePointData(f,f).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{...C,style:u}],!1)}}_getTopOrBottomPosition(n,e,t){if(e==null)return;const r=this._getSpanLeftOffsetInLine(e),o=this._getNextOrPrevLine(e,t);if(o==null)return;const a=this._matchPositionByLeftOffset(n,o,r);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:r}=n;return t+r}_matchPositionByLeftOffset(n,e,t){const r={distance:Number.POSITIVE_INFINITY};for(const o of e.divides){const a=o.left;for(const c of o.spanGroup){const{left:l}=c,u=a+l,d=Math.abs(t-u);d<r.distance&&(r.span=c,r.distance=d)}}if(r.span!=null)return n.findPositionBySpan(r.span)}_getNextOrPrevLine(n,e){var v,p,h,_,f,C,T,x,U,D,y,O;const t=n.parent;if(t==null)return;const r=t.parent;if(r==null)return;const o=r.parent;if(o==null)return;const a=o.lines.indexOf(r);if(a===-1)return;let c;if(e===!0?c=o.lines[a+1]:c=o.lines[a-1],c!=null)return c;const l=o.parent;if(l==null)return;const u=l.columns.indexOf(o);if(u===-1)return;if(e===!0)c=(v=l.columns[u+1])==null?void 0:v.lines[0];else{const M=(h=(p=l.columns)==null?void 0:p[u-1])==null?void 0:h.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=(f=(_=d.sections[m-1])==null?void 0:_.columns[0])==null?void 0:f.lines[0];else{const M=(T=(C=d.sections)==null?void 0:C[m-1])==null?void 0:T.columns,N=M==null?void 0:M[M.length-1],A=N==null?void 0:N.lines;c=A==null?void 0:A[A.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=(D=(U=(x=g.pages[S+1])==null?void 0:x.sections[0])==null?void 0:U.columns[0])==null?void 0:D.lines[0];else{const M=(y=g.pages[S-1])==null?void 0:y.sections;if(M==null)return;const N=(O=M[M.length-1])==null?void 0:O.columns,A=N[N.length-1],Ie=A==null?void 0:A.lines;c=Ie[Ie.length-1]}if(c!=null)return c}}_getDocObject(){return ue(this._currentUniverService,this._renderManagerService)}};re=Mt([i.OnLifecycle(i.LifecycleStages.Rendered,re),X(0,E.Inject(exports.DocSkeletonManagerService)),X(1,i.IUniverInstanceService),X(2,R.IRenderManagerService),X(3,E.Inject(exports.TextSelectionManagerService)),X(4,i.ICommandService)],re);var Ct=Object.defineProperty,Tt=Object.getOwnPropertyDescriptor,xt=(s,n,e,t)=>{for(var r=t>1?void 0:t?Tt(n,e):n,o=s.length-1,a;o>=0;o--)(a=s[o])&&(r=(t?a(n,e,r):a(r))||r);return t&&r&&Ct(n,e,r),r},j=(s,n)=>(e,t)=>n(e,t,s);let ie=class extends i.Disposable{constructor(n,e,t,r,o){super();I(this,"_onInputSubscription");this._docSkeletonManagerService=n,this._currentUniverService=e,this._renderManagerService=t,this._textSelectionRenderManager=r,this._commandService=o,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:r,content:o="",activeRange:a}=n,c=r,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=o.length,S=[{startOffset:u+g,endOffset:u+g,style:m}];await this._commandService.executeCommand(z.id,{unitId:t,body:{dataStream:o},range:a,textRanges:S,segmentId:d})})}_commandExecutedListener(){}_getDocObject(){return ue(this._currentUniverService,this._renderManagerService)}};ie=xt([i.OnLifecycle(i.LifecycleStages.Rendered,ie),j(0,E.Inject(exports.DocSkeletonManagerService)),j(1,i.IUniverInstanceService),j(2,R.IRenderManagerService),j(3,R.ITextSelectionRenderManager),j(4,i.ICommandService)],ie);var Rt=Object.defineProperty,yt=Object.getOwnPropertyDescriptor,Ot=(s,n,e,t)=>{for(var r=t>1?void 0:t?yt(n,e):n,o=s.length-1,a;o>=0;o--)(a=s[o])&&(r=(t?a(n,e,r):a(r))||r);return t&&r&&Rt(n,e,r),r},B=(s,n)=>(e,t)=>n(e,t,s);let oe=class extends i.Disposable{constructor(n,e,t,r,o,a){super();I(this,"_loadedMap",new WeakSet);this._docSkeletonManagerService=n,this._currentUniverService=e,this._commandService=t,this._renderManagerService=r,this._textSelectionRenderManager=o,this._textSelectionManagerService=a,this._initialize()}_initialize(){this._init(),this._skeletonListener(),this._commandExecutedListener()}_init(){this.disposeWithMe(this._renderManagerService.currentRender$.subscribe(n=>{this._create(n)})),this._renderManagerService.getRenderAll().forEach((n,e)=>{this._create(e)})}_create(n){if(n==null||this._currentUniverService.getUniverDocInstance(n)==null)return;const e=this._getDocObjectById(n);e==null||e.document==null||this._loadedMap.has(e.document)||(this._initialMain(n),this._loadedMap.add(e.document))}_initialMain(n){const e=this._getDocObjectById(n);if(e==null)return;const{document:t,scene:r}=e;this.disposeWithMe(i.toDisposable(t.onPointerEnterObserver.add(()=>{t.cursor=R.CURSOR_TYPE.TEXT}))),this.disposeWithMe(i.toDisposable(t.onPointerLeaveObserver.add(()=>{t.cursor=R.CURSOR_TYPE.DEFAULT,r.resetCursor()}))),this.disposeWithMe(i.toDisposable(t==null?void 0:t.onPointerDownObserver.add((o,a)=>{this._currentUniverService.getCurrentUniverDocInstance().getUnitId()!==n&&this._currentUniverService.setCurrentUniverDocInstance(n),this._textSelectionRenderManager.eventTrigger(o),o.button!==2&&a.stopPropagation()}))),this.disposeWithMe(i.toDisposable(t==null?void 0:t.onDblclickObserver.add(o=>{this._textSelectionRenderManager.handleDblClick(o)}))),this.disposeWithMe(i.toDisposable(t==null?void 0:t.onTripleClickObserver.add(o=>{this._textSelectionRenderManager.handleTripleClick(o)})))}_commandExecutedListener(){const n=[$.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{var t;if(n.includes(e.id)){const r=e.params,{unitId:o}=r,a=(t=this._textSelectionManagerService.getCurrentSelection())==null?void 0:t.unitId;if(o!==a)return;this._textSelectionManagerService.refreshSelection()}}))}_skeletonListener(){this.disposeWithMe(this._docSkeletonManagerService.currentSkeleton$.subscribe(n=>{if(n==null)return;const{unitId:e,skeleton:t}=n,r=this._renderManagerService.getRenderById(e);if(r==null)return;const{scene:o,mainComponent:a}=r;this._textSelectionRenderManager.changeRuntime(t,o,a),this._textSelectionManagerService.setCurrentSelectionNotRefresh({unitId:e,subUnitId:""})}))}_getDocObjectById(n){return Xe(n,this._renderManagerService)}};oe=Ot([i.OnLifecycle(i.LifecycleStages.Rendered,oe),B(0,E.Inject(exports.DocSkeletonManagerService)),B(1,i.IUniverInstanceService),B(2,i.ICommandService),B(3,R.IRenderManagerService),B(4,R.ITextSelectionRenderManager),B(5,E.Inject(exports.TextSelectionManagerService))],oe);var Dt=Object.defineProperty,Et=Object.getOwnPropertyDescriptor,Ut=(s,n,e,t)=>{for(var r=t>1?void 0:t?Et(n,e):n,o=s.length-1,a;o>=0;o--)(a=s[o])&&(r=(t?a(n,e,r):a(r))||r);return t&&r&&Dt(n,e,r),r},ee=(s,n)=>(e,t)=>n(e,t,s);const bt={hasScroll:!0},At="docs";var de;exports.UniverDocsPlugin=(de=class extends i.Plugin{constructor(e={},t,r,o,a){super(At);I(this,"_config");this._injector=t,this._localeService=r,this._configService=o,this._currentUniverService=a,this._config=Object.assign(bt,e),this._initializeDependencies(t),this._initializeCommands()}initialize(){}_initializeCommands(){[se,ae,Oe,De,G,W,Y,Z,L,q,K,J,Q,ve,ye,z,H,Se,fe,pe,b,Pe,Ne,Le,$,me,Be,Ae,be,_e].forEach(e=>{this._injector.get(i.ICommandService).registerCommand(e)})}onReady(){this.initialize()}_initializeDependencies(e){[[exports.DocSkeletonManagerService],[exports.DocViewModelManagerService],[exports.DocStateChangeManagerService],[V],[R.ITextSelectionRenderManager,{useClass:R.TextSelectionRenderManager}],[exports.TextSelectionManagerService],[oe],[ie],[te],[ne],[re]].forEach(t=>e.add(t))}},I(de,"type",i.PluginType.Doc),de);exports.UniverDocsPlugin=Ut([ee(1,E.Inject(E.Injector)),ee(2,E.Inject(i.LocaleService)),ee(3,i.IConfigService),ee(4,i.IUniverInstanceService)],exports.UniverDocsPlugin);exports.BreakLineCommand=ye;exports.BulletListCommand=be;exports.CoverContentCommand=Ne;exports.CutContentCommand=he;exports.DOCS_COMPONENT_DEFAULT_Z_INDEX=Ve;exports.DOCS_COMPONENT_HEADER_LAYER_INDEX=ke;exports.DOCS_COMPONENT_MAIN_LAYER_INDEX=je;exports.DOCS_VIEW_KEY=xe;exports.DeleteCommand=H;exports.DeleteLeftCommand=Oe;exports.DeleteRightCommand=De;exports.IMEInputCommand=fe;exports.IMEInputManagerService=V;exports.InnerPasteCommand=st;exports.InsertCommand=z;exports.MoveCursorOperation=se;exports.MoveSelectionOperation=ae;exports.NORMAL_TEXT_SELECTION_PLUGIN_NAME=ze;exports.OrderListCommand=Ae;exports.ReplaceContentCommand=Pe;exports.RichTextEditingMutation=b;exports.SelectAllOperation=Be;exports.SetDocZoomRatioCommand=Le;exports.SetDocZoomRatioOperation=$;exports.SetInlineFormatBoldCommand=G;exports.SetInlineFormatCommand=ve;exports.SetInlineFormatFontFamilyCommand=J;exports.SetInlineFormatFontSizeCommand=K;exports.SetInlineFormatItalicCommand=W;exports.SetInlineFormatStrikethroughCommand=Z;exports.SetInlineFormatSubscriptCommand=L;exports.SetInlineFormatSuperscriptCommand=q;exports.SetInlineFormatTextColorCommand=Q;exports.SetInlineFormatUnderlineCommand=Y;exports.SetTextSelectionsOperation=me;exports.UpdateCommand=Se;exports.VIEWPORT_KEY=Re;exports.getDocObject=ue;exports.serializeTextRange=k;
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;