@univerjs/docs 0.1.3 → 0.1.5

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.
Files changed (40) hide show
  1. package/lib/cjs/index.js +1 -1
  2. package/lib/es/index.js +888 -768
  3. package/lib/types/basics/component-tools.d.ts +3 -17
  4. package/lib/types/basics/retain-delete-params.d.ts +2 -16
  5. package/lib/types/commands/commands/__tests__/create-command-test-bed.d.ts +10 -25
  6. package/lib/types/commands/commands/__tests__/mock-text-selection-render-manager.d.ts +5 -19
  7. package/lib/types/commands/commands/break-line.command.d.ts +2 -16
  8. package/lib/types/commands/commands/clipboard.inner.command.d.ts +3 -17
  9. package/lib/types/commands/commands/core-editing.command.d.ts +3 -17
  10. package/lib/types/commands/commands/delete.command.d.ts +3 -17
  11. package/lib/types/commands/commands/ime-input.command.d.ts +2 -16
  12. package/lib/types/commands/commands/inline-format.command.d.ts +2 -16
  13. package/lib/types/commands/commands/insert-floating-object.command.d.ts +2 -16
  14. package/lib/types/commands/commands/list.command.d.ts +4 -17
  15. package/lib/types/commands/commands/paragraph-align.command.d.ts +19 -0
  16. package/lib/types/commands/commands/remove-floating-object.command.d.ts +2 -16
  17. package/lib/types/commands/commands/replace-content.command.d.ts +3 -17
  18. package/lib/types/commands/commands/set-doc-zoom-ratio.command.d.ts +2 -16
  19. package/lib/types/commands/commands/set-floating-object-transform.command.d.ts +2 -16
  20. package/lib/types/commands/mutations/core-editing.mutation.d.ts +3 -17
  21. package/lib/types/commands/mutations/insert-floating-object.mutation.d.ts +3 -17
  22. package/lib/types/commands/mutations/remove-floating-object.mutation.d.ts +3 -17
  23. package/lib/types/commands/mutations/set-floating-object.mutation.d.ts +3 -17
  24. package/lib/types/commands/operations/cursor.operation.d.ts +2 -16
  25. package/lib/types/commands/operations/select-all.operation.d.ts +2 -16
  26. package/lib/types/commands/operations/set-doc-zoom-ratio.operation.d.ts +3 -17
  27. package/lib/types/commands/operations/text-selection.operation.d.ts +3 -17
  28. package/lib/types/controllers/ime-input.controller.d.ts +4 -18
  29. package/lib/types/controllers/move-cursor.controller.d.ts +4 -18
  30. package/lib/types/controllers/normal-input.controller.d.ts +3 -17
  31. package/lib/types/doc-plugin.d.ts +3 -20
  32. package/lib/types/index.d.ts +1 -0
  33. package/lib/types/services/doc-skeleton-manager.service.d.ts +5 -20
  34. package/lib/types/services/doc-state-change-manager.service.d.ts +4 -19
  35. package/lib/types/services/doc-view-model-manager.service.d.ts +3 -18
  36. package/lib/types/services/ime-input-manager.service.d.ts +5 -19
  37. package/lib/types/services/text-selection-manager.service.d.ts +4 -20
  38. package/lib/umd/index.js +1 -1
  39. package/package.json +12 -11
  40. package/lib/types/controllers/inline-format.controller.d.ts +0 -29
package/lib/cjs/index.js CHANGED
@@ -1 +1 @@
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 y;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=((y=v.getSelections())!=null?y:[]).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 R=h.fetchComposedUndoRedoMutationParams();if(R==null)throw new Error("historyParams is null in RichTextEditingMutation");const{undoMutationParams:I,redoMutationParams:P,previousActiveRange:N}=R;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 ye={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},y=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),y!=null&&(O.paragraphStyle={...y},delete O.paragraphStyle.hanging,delete O.paragraphStyle.indentStart)}const R=[{startOffset:_,endOffset:_,style:S}];l=await o.executeCommand(me.id,{unitId:u.getUnitId(),updateBody:{dataStream:"",paragraphs:[{...O}]},range:{startOffset:E,endOffset:E+1},textRanges:R,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}},Re={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,y,R;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=(R=(y=(O=u.getBody())==null?void 0:O.paragraphs)==null?void 0:y.find(I=>I.startIndex>=v))==null?void 0:R.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(y=>{var R;return((R=y.bullet)==null?void 0:R.listType)===i});let p=r.Tools.generateRandomId(6);if(m.length===1){const y=u.indexOf(m[0]),R=u[y-1],I=u[y+1];R&&R.bullet&&R.bullet.listType===i?p=R.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 y of m){const{startIndex:R}=y;M.push({t:r.TextXActionType.RETAIN,len:R-h.cursor,segmentId:g});const I={...y.paragraphStyle,hanging:void 0,indentStart:void 0};M.push({t:r.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[v?{paragraphStyle:I,startIndex:0}:{...y,startIndex:0,bullet:{...(O=y.bullet)!=null?O:{nestingLevel:0,textStyle:{fs:20}},listType:i,listId:p}}]},segmentId:g,coverType:r.UpdateDocsAttributeType.REPLACE}),h.moveCursorTo(R+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;this._resetIME();const{activeRange:e}=n;e!=null&&this._imeInputManagerService.setActiveRange(r.Tools.deepClone(e))})}_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,y,R;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=(y=g.pages[S-1])==null?void 0:y.sections;if(I==null)return;const P=(R=I[I.length-1])==null?void 0:R.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,yt=(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 Rt={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(Rt,e),this._initializeDependencies(t),this._initializeCommands()}initialize(){}_initializeCommands(){[re,ie,ye,Re,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=yt([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=ye;exports.DeleteRightCommand=Re;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;
1
+ "use strict";var Ze=Object.defineProperty;var qe=(i,n,e)=>n in i?Ze(i,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[n]=e;var x=(i,n,e)=>(qe(i,typeof n!="symbol"?n+"":n,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("@univerjs/core"),E=require("@univerjs/engine-render"),N=require("rxjs"),b=require("@wendellhu/redi");function de(i,n){const t=i.getCurrentUniverDocInstance().getUnitId(),o=n.getRenderById(t);if(o==null)return;const{mainComponent:a,scene:s,engine:c}=o;return{document:a,scene:s,engine:c}}function Je(i,n){const e=n.getRenderById(i);if(e==null)return;const{mainComponent:t,scene:o,engine:a}=e;return{document:t,scene:o,engine:a}}var Ae=(i=>(i.MAIN="__Document_Render_Main__",i))(Ae||{}),Ue=(i=>(i.VIEW_MAIN="viewMain",i.VIEW_TOP="viewTop",i.VIEW_LEFT="viewLeft",i.VIEW_LEFT_TOP="viewLeftTop",i))(Ue||{});const Ke=0,Qe=2,et=10,tt="normalTextSelectionPluginName",ue={id:"doc.operation.set-selections",type:r.CommandType.OPERATION,handler:(i,n)=>!0};var nt=Object.defineProperty,ot=Object.getOwnPropertyDescriptor,rt=(i,n,e,t)=>{for(var o=t>1?void 0:t?ot(n,e):n,a=i.length-1,s;a>=0;a--)(s=i[a])&&(o=(t?s(n,e,o):s(o))||o);return t&&o&&nt(n,e,o),o},Ie=(i,n)=>(e,t)=>n(e,t,i);function B(i){const{startOffset:n,endOffset:e,collapsed:t}=i,o={startOffset:n,endOffset:e,collapsed:t};return typeof i.isActive=="function"&&(o.isActive=i.isActive()),o}exports.TextSelectionManagerService=class extends r.RxDisposable{constructor(e,t){super();x(this,"_currentSelection",null);x(this,"_textSelectionInfo",new Map);x(this,"_textSelection$",new N.BehaviorSubject(null));x(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:a}=e,s=t.find(S=>S.isActive());if(s==null)return null;const{startOffset:c,endOffset:l,collapsed:d,startNodePosition:u,endNodePosition:m,direction:g}=s;return c==null||l==null?null:{startOffset:c,endOffset:l,collapsed:d,startNodePosition:u,endNodePosition:m,direction:g,segmentId:o,style:a}}add(e,t=!0){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:e,segmentId:"",isEditing:t,style:E.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(N.takeUntil(this.dispose$)).subscribe(e=>{e!=null&&this._replaceTextRangesWithNoRefresh(e)})}_replaceTextRangesWithNoRefresh(e){if(this._currentSelection==null)return;const t={...this._currentSelection,...e};this._replaceByParam(t),this._textSelection$.next(t);const{unitId:o,subUnitId:a,segmentId:s,style:c,textRanges:l,isEditing:d}=t;this._commandService.executeCommand(ue.id,{unitId:o,subUnitId:a,segmentId:s,style:c,isEditing:d,ranges:l.map(B)})}_getTextRanges(e){var a;if(e==null)return;const{unitId:t,subUnitId:o=""}=e;return(a=this._textSelectionInfo.get(t))==null?void 0:a.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:a,segmentId:s,textRanges:c,isEditing:l}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map),this._textSelectionInfo.get(t).set(o,{textRanges:c,style:a,segmentId:s,isEditing:l})}_addByParam(e){const{unitId:t,subUnitId:o,textRanges:a,style:s,segmentId:c,isEditing:l}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map);const d=this._textSelectionInfo.get(t);d.has(o)?d.get(o).textRanges.push(...a):d.set(o,{textRanges:a,style:s,segmentId:c,isEditing:l})}};exports.TextSelectionManagerService=rt([Ie(0,E.ITextSelectionRenderManager),Ie(1,r.ICommandService)],exports.TextSelectionManagerService);function me(i,n="",e=0){const{startOffset:t,endOffset:o}=i,a=[],s=t-e,c=o-e;return s>0&&a.push({t:r.TextXActionType.RETAIN,len:s,segmentId:n}),a.push({t:r.TextXActionType.DELETE,len:c-s,line:0,segmentId:n}),a}var it=Object.defineProperty,at=Object.getOwnPropertyDescriptor,st=(i,n,e,t)=>{for(var o=t>1?void 0:t?at(n,e):n,a=i.length-1,s;a>=0;a--)(s=i[a])&&(o=(t?s(n,e,o):s(o))||o);return t&&o&&it(n,e,o),o},ct=(i,n)=>(e,t)=>n(e,t,i);exports.DocViewModelManagerService=class extends r.RxDisposable{constructor(e){super();x(this,"_docViewModelMap",new Map);x(this,"_currentDocViewModel$",new N.BehaviorSubject(null));x(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(N.takeUntil(this.dispose$)).subscribe(e=>{this._create(e)}),this._currentUniverService.getAllUniverDocsInstance().forEach(e=>{this._create(e)}),this._currentUniverService.docDisposed$.pipe(N.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 a=this._buildDocViewModel(t);this._docViewModelMap.set(e,{unitId:e,docViewModel:a})}if(e===r.DOCS_NORMAL_EDITOR_UNIT_ID_KEY){const a=(o=this._docViewModelMap.get(e))==null?void 0:o.docViewModel;if(a==null)return;a.reset(t)}this._currentDocViewModel$.next(this._docViewModelMap.get(e))}}_buildDocViewModel(e){return new E.DocumentViewModel(e)}};exports.DocViewModelManagerService=st([ct(0,r.IUniverInstanceService)],exports.DocViewModelManagerService);var lt=Object.defineProperty,dt=Object.getOwnPropertyDescriptor,ut=(i,n,e,t)=>{for(var o=t>1?void 0:t?dt(n,e):n,a=i.length-1,s;a>=0;a--)(s=i[a])&&(o=(t?s(n,e,o):s(o))||o);return t&&o&&lt(n,e,o),o},se=(i,n)=>(e,t)=>n(e,t,i);const mt=300;exports.DocStateChangeManagerService=class extends r.RxDisposable{constructor(e,t,o){super();x(this,"_docStateChange$",new N.BehaviorSubject(null));x(this,"docStateChange$",this._docStateChange$.asObservable());x(this,"_stateCache",new Map);x(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:a}=e;if(!(t===r.RedoCommandId||t===r.UndoCommandId||a)){if(this._stateCache.has(o)){const s=this._stateCache.get(o);s==null||s.push(e)}else this._stateCache.set(o,[e]);t===H.id||t===G.id?(this._timer&&clearTimeout(this._timer),this._timer=setTimeout(()=>{this._pushHistory(o)},mt)):this._pushHistory(o)}}_pushHistory(e){const t=this._undoRedoService,o=this._stateCache.get(e);if(!Array.isArray(o)||o.length===0)return;const a=o.length,s=o[0].commandId,c=o[0],l=o[a-1],d={unitId:e,actions:o.reduce((m,g)=>r.TextX.compose(m,g.redoState.actions),[]),textRanges:l.redoState.textRanges},u={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:s,params:u}],redoMutations:[{id:s,params:d}]}),o.length=0}};exports.DocStateChangeManagerService=ut([se(0,b.Inject(r.IUndoRedoService)),se(1,r.ICommandService),se(2,r.IUniverInstanceService)],exports.DocStateChangeManagerService);class j{constructor(){x(this,"_previousActiveRange",null);x(this,"_undoMutationParamsCache",[]);x(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,a)=>r.TextX.compose(o,a.actions),[]),textRanges:[]};return{redoMutationParams:{unitId:n,actions:this._redoMutationParamsCache.reduce((o,a)=>r.TextX.compose(o,a.actions),[]),textRanges:[]},undoMutationParams:e,previousActiveRange:this._previousActiveRange}}dispose(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[],this._previousActiveRange=null}}const _e="doc.mutation.rich-text-editing",A={id:_e,type:r.CommandType.MUTATION,handler:(i,n)=>{var R;const{unitId:e,actions:t,textRanges:o,prevTextRanges:a,trigger:s,noHistory:c,isCompositionEnd:l,noNeedSetTextRange:d}=n,m=i.get(r.IUniverInstanceService).getUniverDocInstance(e),S=i.get(exports.DocViewModelManagerService).getViewModel(e),h=i.get(exports.TextSelectionManagerService),p=((R=h.getSelections())!=null?R:[]).map(B),v=i.get(exports.DocStateChangeManagerService),I=i.get(j);if(m==null||S==null)throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${e}`);const C=!!m.getSnapshot().disabled;if(t.length===0||C)return{unitId:e,actions:[],textRanges:p};const M=m.apply(t),{segmentId:_}=t[0],O=m.getSelfOrHeaderFooterModel(_);S.getSelfOrHeaderFooterViewModel(_).reset(O),!d&&o&&s!=null&&queueMicrotask(()=>{h.replaceTextRanges(o)});const y={commandId:_e,unitId:e,trigger:s,noHistory:c,redoState:{actions:t,textRanges:o},undoState:{actions:M,textRanges:a!=null?a:p}};if(l){const T=I.fetchComposedUndoRedoMutationParams();if(T==null)throw new Error("historyParams is null in RichTextEditingMutation");const{undoMutationParams:w,redoMutationParams:U,previousActiveRange:Y}=T;y.redoState.actions=U.actions,y.undoState.actions=w.actions,y.undoState.textRanges=[Y]}return v.setChangeState(y),{unitId:e,actions:M,textRanges:p}}},H={id:"doc.command.insert-text",type:r.CommandType.COMMAND,handler:async(i,n)=>{const e=i.get(r.ICommandService),{range:t,segmentId:o,body:a,unitId:s,textRanges:c}=n,{startOffset:l,collapsed:d}=t,u={id:A.id,params:{unitId:s,actions:[],textRanges:c}},m=new r.TextX;return d?l>0&&m.push({t:r.TextXActionType.RETAIN,len:l,segmentId:o}):m.push(...me(t,o)),m.push({t:r.TextXActionType.INSERT,body:a,len:a.dataStream.length,line:0,segmentId:o}),u.params.actions=m.serialize(),!!e.syncExecuteCommand(u.id,u.params)}};var L=(i=>(i[i.LEFT=0]="LEFT",i[i.RIGHT=1]="RIGHT",i))(L||{});const G={id:"doc.command.delete-text",type:r.CommandType.COMMAND,handler:async(i,n)=>{const e=i.get(r.ICommandService),{range:t,segmentId:o,unitId:a,direction:s,textRanges:c,len:l=1}=n,{startOffset:d}=t,u={id:A.id,params:{unitId:a,actions:[],textRanges:c}},m=new r.TextX;return d>0&&m.push({t:r.TextXActionType.RETAIN,len:s===0?d-l:d,segmentId:o}),m.push({t:r.TextXActionType.DELETE,len:l,line:0,segmentId:o}),u.params.actions=m.serialize(),!!e.syncExecuteCommand(u.id,u.params)}},ge={id:"doc.command.update-text",type:r.CommandType.COMMAND,handler:async(i,n)=>{const{range:e,segmentId:t,updateBody:o,coverType:a,unitId:s,textRanges:c}=n,l=i.get(r.ICommandService),d={id:A.id,params:{unitId:s,actions:[],textRanges:c}},u=new r.TextX,{startOffset:m,endOffset:g}=e;return u.push({t:r.TextXActionType.RETAIN,len:m,segmentId:t}),u.push({t:r.TextXActionType.RETAIN,body:o,len:g-m,segmentId:t,coverType:a}),d.params.actions=u.serialize(),!!l.syncExecuteCommand(d.id,d.params)}};function gt(i,n){const e=[];for(let t=0,o=i.length;t<o;t++)i[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 Ne={id:"doc.command.break-line",type:r.CommandType.COMMAND,handler:async i=>{var h,f;const n=i.get(exports.TextSelectionManagerService),e=i.get(r.IUniverInstanceService),t=i.get(r.ICommandService),o=n.getActiveRange();if(o==null)return!1;const a=e.getCurrentUniverDocInstance(),s=a.getUnitId(),{startOffset:c,segmentId:l,style:d}=o,u=[{startOffset:c+1,endOffset:c+1,style:d}],g=((f=(h=a.getBody())==null?void 0:h.paragraphs)!=null?f:[]).find(p=>p.startIndex>=c);return await t.executeCommand(H.id,{unitId:s,body:{dataStream:r.DataStreamTreeTokenType.PARAGRAPH,paragraphs:gt(r.DataStreamTreeTokenType.PARAGRAPH,g)},range:o,textRanges:u,segmentId:l})}};var St=Object.defineProperty,pt=Object.getOwnPropertyDescriptor,ft=(i,n,e,t)=>{for(var o=t>1?void 0:t?pt(n,e):n,a=i.length-1,s;a>=0;a--)(s=i[a])&&(o=(t?s(n,e,o):s(o))||o);return t&&o&&St(n,e,o),o},ce=(i,n)=>(e,t)=>n(e,t,i);exports.DocSkeletonManagerService=class extends r.RxDisposable{constructor(e,t,o){super();x(this,"_currentSkeletonUnitId","");x(this,"_docSkeletonMap",new Map);x(this,"_currentSkeleton$",new N.BehaviorSubject(null));x(this,"currentSkeleton$",this._currentSkeleton$.asObservable());x(this,"_currentSkeletonBefore$",new N.BehaviorSubject(null));x(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(N.takeUntil(this.dispose$)).subscribe(e=>{e!=null&&this._setCurrent(e)}),this._docViewModelManagerService.getAllModel().forEach(e=>{e!=null&&this._setCurrent(e)}),this._currentUniverService.docDisposed$.pipe(N.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 E.DocumentSkeleton.create(e,this._localeService)}};exports.DocSkeletonManagerService=ft([ce(0,b.Inject(r.LocaleService)),ce(1,b.Inject(exports.DocViewModelManagerService)),ce(2,r.IUniverInstanceService)],exports.DocSkeletonManagerService);const ht={id:"doc.command.inner-paste",type:r.CommandType.COMMAND,handler:async(i,n)=>{const{segmentId:e,body:t,textRanges:o}=n,a=i.get(r.ICommandService),s=i.get(exports.TextSelectionManagerService),c=i.get(r.IUniverInstanceService),l=s.getSelections();if(!Array.isArray(l)||l.length===0)return!1;const u=c.getCurrentUniverDocInstance().getUnitId(),m={id:A.id,params:{unitId:u,actions:[],textRanges:o}},g=new r.MemoryCursor;g.reset();const S=new r.TextX;for(const f of l){const{startOffset:p,endOffset:v,collapsed:I}=f,C=p-g.cursor;I?S.push({t:r.TextXActionType.RETAIN,len:C,segmentId:e}):S.push(...me(f,e,g.cursor)),S.push({t:r.TextXActionType.INSERT,body:t,len:t.dataStream.length,line:0,segmentId:e}),g.reset(),g.moveCursor(v)}return m.params.actions=S.serialize(),!!a.syncExecuteCommand(m.id,m.params)}},Se={id:"doc.command.inner-cut",type:r.CommandType.COMMAND,handler:async(i,n)=>{const{segmentId:e,textRanges:t}=n,o=i.get(r.ICommandService),a=i.get(exports.TextSelectionManagerService),s=i.get(r.IUniverInstanceService),c=a.getSelections();if(!Array.isArray(c)||c.length===0)return!1;const l=s.getCurrentUniverDocInstance().getUnitId(),d=s.getUniverDocInstance(l),u=r.getDocsUpdateBody(d.snapshot,e);if(u==null)return!1;const m={id:A.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:v,collapsed:I}=f,C=p-g.cursor;I?S.push({t:r.TextXActionType.RETAIN,len:C,segmentId:e}):S.push(...vt(f,u,e,g.cursor)),g.reset(),g.moveCursor(v)}return m.params.actions=S.serialize(),!!o.syncExecuteCommand(m.id,m.params)}};function vt(i,n,e="",t=0){const{startOffset:o,endOffset:a}=i,s=[],{paragraphs:c=[]}=n,l=o-t,d=a-t,u=c==null?void 0:c.find(m=>m.startIndex-t>=l&&m.startIndex-t<=d);if(l>0&&s.push({t:r.TextXActionType.RETAIN,len:l,segmentId:e}),u&&u.startIndex-t>l){const m=u.startIndex-t;s.push({t:r.TextXActionType.DELETE,len:m-l,line:0,segmentId:e}),s.push({t:r.TextXActionType.RETAIN,len:1,segmentId:e}),d>m+1&&s.push({t:r.TextXActionType.DELETE,len:d-m-1,line:0,segmentId:e})}else s.push({t:r.TextXActionType.DELETE,len:d-l,line:0,segmentId:e});return s}const be={id:"doc.command.delete-left",type:r.CommandType.COMMAND,handler:async i=>{var M;const n=i.get(exports.TextSelectionManagerService),e=i.get(exports.DocSkeletonManagerService),t=i.get(r.IUniverInstanceService),o=i.get(r.ICommandService),a=n.getActiveRange(),s=n.getSelections(),c=(M=e.getCurrent())==null?void 0:M.skeleton;let l=!0;if(a==null||c==null||s==null)return!1;const d=t.getCurrentUniverDocInstance(),{startOffset:u,collapsed:m,segmentId:g,style:S}=a,h=c.findNodeByCharIndex(u),f=E.hasListGlyph(h),p=E.isIndentByGlyph(h,d.getBody());let v=u;const I=c.findNodeByCharIndex(u-1);if(E.isFirstGlyph(h)&&I!==h&&(f===!0||p===!0)){const _=E.getParagraphByGlyph(h,d.getBody());if(_==null)return!1;const O=_==null?void 0:_.startIndex,D={startIndex:0},y=_.paragraphStyle;if(f===!0){const T=_.paragraphStyle;T&&(D.paragraphStyle=T)}else if(p===!0){const T=_.bullet;T&&(D.bullet=T),y!=null&&(D.paragraphStyle={...y},delete D.paragraphStyle.hanging,delete D.paragraphStyle.indentStart)}const R=[{startOffset:v,endOffset:v,style:S}];l=await o.executeCommand(ge.id,{unitId:d.getUnitId(),updateBody:{dataStream:"",paragraphs:[{...D}]},range:{startOffset:O,endOffset:O+1},textRanges:R,coverType:r.UpdateDocsAttributeType.REPLACE,segmentId:g})}else if(m===!0){if(I==null)return!0;if(I.content==="\r")l=await o.executeCommand(pe.id,{direction:L.LEFT,range:a});else{v-=I.count;const _=[{startOffset:v,endOffset:v,style:S}];l=await o.executeCommand(G.id,{unitId:d.getUnitId(),range:a,segmentId:g,direction:L.LEFT,len:I.count,textRanges:_})}}else{const _=we(a,s);l=await o.executeCommand(Se.id,{segmentId:g,textRanges:_})}return l}},Pe={id:"doc.command.delete-right",type:r.CommandType.COMMAND,handler:async i=>{var h;const n=i.get(exports.TextSelectionManagerService),e=i.get(exports.DocSkeletonManagerService),t=i.get(r.IUniverInstanceService),o=i.get(r.ICommandService),a=n.getActiveRange(),s=n.getSelections(),c=(h=e.getCurrent())==null?void 0:h.skeleton;let l;if(a==null||c==null||s==null)return!1;const d=t.getCurrentUniverDocInstance(),{startOffset:u,collapsed:m,segmentId:g,style:S}=a;if(u===d.getBody().dataStream.length-2&&m)return!0;if(m===!0){const f=c.findNodeByCharIndex(u);if(f.content==="\r")l=await o.executeCommand(pe.id,{direction:L.RIGHT,range:a});else{const p=[{startOffset:u,endOffset:u,style:S}];l=await o.executeCommand(G.id,{unitId:d.getUnitId(),range:a,segmentId:g,direction:L.RIGHT,textRanges:p,len:f.count})}}else{const f=we(a,s);l=await o.executeCommand(Se.id,{segmentId:g,textRanges:f})}return l}},pe={id:"doc.command.merge-two-paragraph",type:r.CommandType.COMMAND,handler:async(i,n)=>{var D,y,R;const e=i.get(exports.TextSelectionManagerService),t=i.get(r.IUniverInstanceService),o=i.get(r.ICommandService),{direction:a,range:s}=n,c=e.getActiveRange(),l=e.getSelections();if(c==null||l==null)return!1;const d=t.getCurrentUniverDocInstance(),{startOffset:u,collapsed:m,segmentId:g,style:S}=c;if(!m)return!1;const h=a===L.LEFT?u:u+1,f=(R=(y=(D=d.getBody())==null?void 0:D.paragraphs)==null?void 0:y.find(T=>T.startIndex>=h))==null?void 0:R.startIndex,p=It(d.getBody(),h,f),v=a===L.LEFT?u-1:u,I=d.getUnitId(),C=[{startOffset:v,endOffset:v,style:S}],M={id:A.id,params:{unitId:I,actions:[],textRanges:C,prevTextRanges:[s]}},_=new r.TextX;return _.push({t:r.TextXActionType.RETAIN,len:a===L.LEFT?u-1:u,segmentId:g}),p.dataStream.length&&_.push({t:r.TextXActionType.INSERT,body:p,len:p.dataStream.length,line:0,segmentId:g}),_.push({t:r.TextXActionType.RETAIN,len:1,segmentId:g}),_.push({t:r.TextXActionType.DELETE,len:f+1-h,line:0,segmentId:g}),M.params.actions=_.serialize(),!!o.syncExecuteCommand(M.id,M.params)}};function It(i,n,e){const{textRuns:t}=i,o=i.dataStream.substring(n,e);if(t==null)return{dataStream:o};const a=[];for(const s of t){const{st:c,ed:l}=s;l<=n||c>=e||(c<n?a.push({...s,st:0,ed:l-n}):l>e?a.push({...s,st:c-n,ed:e-n}):a.push({...s,st:c-n,ed:l-n}))}return{dataStream:o,textRuns:a}}function we(i,n){let e=i.endOffset;for(const o of n){const{startOffset:a,endOffset:s}=o;a==null||s==null||s<=i.endOffset&&(e-=s-a)}return[{startOffset:e,endOffset:e,style:i.style}]}const fe={id:"doc.command.ime-input",type:r.CommandType.COMMAND,handler:async(i,n)=>{const{unitId:e,newText:t,oldTextLen:o,isCompositionEnd:a,isCompositionStart:s}=n,c=i.get(r.ICommandService),l=i.get(j),d=l.getActiveRange();if(d==null)return!1;const{startOffset:u,style:m,segmentId:g}=d,S=t.length,h=[{startOffset:u+S,endOffset:u+S,collapsed:!0,style:m}],f={id:A.id,params:{unitId:e,actions:[],textRanges:h}},p=new r.TextX;!d.collapsed&&s?p.push(...me(d,g)):p.push({t:r.TextXActionType.RETAIN,len:u,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=!a,f.params.isCompositionEnd=a;const v=c.syncExecuteCommand(f.id,f.params);return l.pushUndoRedoMutationParams(v,f.params),!!v}};function P(i,n,e,t){var a;const{segmentId:o}=(a=e.getActiveRange())!=null?a:{};return o==null?!1:t.executeCommand(he.id,{segmentId:o,preCommandId:i,...n!=null?n:{}})}const Ce="doc.command.set-inline-format-bold",Q={id:Ce,type:r.CommandType.COMMAND,handler:async(i,n)=>{const e=i.get(r.ICommandService),t=i.get(exports.TextSelectionManagerService);return P(Ce,n,t,e)}},Me="doc.command.set-inline-format-italic",ee={id:Me,type:r.CommandType.COMMAND,handler:async(i,n)=>{const e=i.get(r.ICommandService),t=i.get(exports.TextSelectionManagerService);return P(Me,n,t,e)}},Te="doc.command.set-inline-format-underline",te={id:Te,type:r.CommandType.COMMAND,handler:async(i,n)=>{const e=i.get(r.ICommandService),t=i.get(exports.TextSelectionManagerService);return P(Te,n,t,e)}},xe="doc.command.set-inline-format-strikethrough",ne={id:xe,type:r.CommandType.COMMAND,handler:async(i,n)=>{const e=i.get(r.ICommandService),t=i.get(exports.TextSelectionManagerService);return P(xe,n,t,e)}},ye="doc.command.set-inline-format-subscript",F={id:ye,type:r.CommandType.COMMAND,handler:async(i,n)=>{const e=i.get(r.ICommandService),t=i.get(exports.TextSelectionManagerService);return P(ye,n,t,e)}},Re="doc.command.set-inline-format-superscript",oe={id:Re,type:r.CommandType.COMMAND,handler:async(i,n)=>{const e=i.get(r.ICommandService),t=i.get(exports.TextSelectionManagerService);return P(Re,n,t,e)}},Oe="doc.command.set-inline-format-fontsize",re={id:Oe,type:r.CommandType.COMMAND,handler:async(i,n)=>{const e=i.get(r.ICommandService),t=i.get(exports.TextSelectionManagerService);return P(Oe,n,t,e)}},De="doc.command.set-inline-format-font-family",ie={id:De,type:r.CommandType.COMMAND,handler:async(i,n)=>{const e=i.get(r.ICommandService),t=i.get(exports.TextSelectionManagerService);return P(De,n,t,e)}},Ee="doc.command.set-inline-format-text-color",ae={id:Ee,type:r.CommandType.COMMAND,handler:async(i,n)=>{const e=i.get(r.ICommandService),t=i.get(exports.TextSelectionManagerService);return P(Ee,n,t,e)}},Le={[Q.id]:"bl",[ee.id]:"it",[te.id]:"ul",[ne.id]:"st",[re.id]:"fs",[ie.id]:"ff",[ae.id]:"cl",[F.id]:"va",[oe.id]:"va"},he={id:"doc.command.set-inline-format",type:r.CommandType.COMMAND,handler:async(i,n)=>{const{segmentId:e,value:t,preCommandId:o}=n,a=i.get(r.ICommandService),s=i.get(exports.TextSelectionManagerService),c=i.get(r.IUniverInstanceService),l=s.getSelections();if(!Array.isArray(l)||l.length===0)return!1;let d=c.getCurrentUniverDocInstance(),u=d.getUnitId();u===r.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY&&(d=c.getUniverDocInstance(r.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),u=d.getUnitId());let m;switch(o){case Q.id:case ee.id:case te.id:case ne.id:case F.id:case oe.id:{m=Ct(d.getBody().textRuns,o,l);break}case re.id:case ie.id:{m=t;break}case ae.id:{m={rgb:t};break}default:throw new Error(`Unknown command: ${o} in handleInlineFormat`)}const g={id:A.id,params:{unitId:u,actions:[],textRanges:l.map(B)}},S=new r.TextX,h=new r.MemoryCursor;h.reset();for(const p of l){const{startOffset:v,endOffset:I}=p,C={dataStream:"",textRuns:[{st:0,ed:I-v,ts:{[Le[o]]:m}}]},M=v-h.cursor;M!==0&&S.push({t:r.TextXActionType.RETAIN,len:M,segmentId:e}),S.push({t:r.TextXActionType.RETAIN,body:C,len:I-v,segmentId:e}),h.reset(),h.moveCursor(I)}return g.params.actions=S.serialize(),!!a.syncExecuteCommand(g.id,g.params)}};function _t(i){return i!==null&&typeof i=="object"}function Ct(i,n,e){let t=0,o=0;const a=Le[n];for(;t!==i.length&&o!==e.length;){const{startOffset:s,endOffset:c}=e[o],{st:l,ed:d,ts:u}=i[t];if(c<=l)o++;else if(d<=s)t++;else{if(/bl|it/.test(a))return(u==null?void 0:u[a])===r.BooleanNumber.TRUE?r.BooleanNumber.FALSE:r.BooleanNumber.TRUE;if(/ul|st/.test(a))return _t(u==null?void 0:u[a])&&(u==null?void 0:u[a]).s===r.BooleanNumber.TRUE?{s:r.BooleanNumber.FALSE}:{s:r.BooleanNumber.TRUE};if(/va/.test(a))return n===F.id?(u==null?void 0:u[a])===r.BaselineOffset.SUBSCRIPT?r.BaselineOffset.NORMAL:r.BaselineOffset.SUBSCRIPT:(u==null?void 0:u[a])===r.BaselineOffset.SUPERSCRIPT?r.BaselineOffset.NORMAL:r.BaselineOffset.SUPERSCRIPT;t++}}return/bl|it/.test(a)?r.BooleanNumber.TRUE:/ul|st/.test(a)?{s:r.BooleanNumber.TRUE}:n===F.id?r.BaselineOffset.SUBSCRIPT:r.BaselineOffset.SUPERSCRIPT}const ve={id:"doc.command.list-operation",type:r.CommandType.COMMAND,handler:(i,n)=>{var _,O,D;const e=i.get(exports.TextSelectionManagerService),t=i.get(r.IUniverInstanceService),o=i.get(r.ICommandService),{listType:a}=n,s=t.getCurrentUniverDocInstance(),c=e.getActiveRange(),l=(_=e.getSelections())!=null?_:[],d=(O=s.getBody())==null?void 0:O.paragraphs,u=l.map(B);if(c==null||d==null)return!1;const m=$e(c,d),{segmentId:g}=c,S=s.getUnitId(),h=m.every(y=>{var R;return((R=y.bullet)==null?void 0:R.listType)===a});let p=r.Tools.generateRandomId(6);if(m.length===1){const y=d.indexOf(m[0]),R=d[y-1],T=d[y+1];R&&R.bullet&&R.bullet.listType===a?p=R.bullet.listId:T&&T.bullet&&T.bullet.listType===a&&(p=T.bullet.listId)}const v={id:A.id,params:{unitId:S,actions:[],textRanges:u}},I=new r.MemoryCursor;I.reset();const C=new r.TextX;for(const y of m){const{startIndex:R}=y;C.push({t:r.TextXActionType.RETAIN,len:R-I.cursor,segmentId:g});const T={...y.paragraphStyle,hanging:void 0,indentStart:void 0};C.push({t:r.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[h?{paragraphStyle:T,startIndex:0}:{...y,startIndex:0,bullet:{...(D=y.bullet)!=null?D:{nestingLevel:0,textStyle:{fs:20}},listType:a,listId:p}}]},segmentId:g,coverType:r.UpdateDocsAttributeType.REPLACE}),I.moveCursorTo(R+1)}return v.params.actions=C.serialize(),!!o.syncExecuteCommand(v.id,v.params)}},Be={id:"doc.command.bullet-list",type:r.CommandType.COMMAND,handler:i=>i.get(r.ICommandService).syncExecuteCommand(ve.id,{listType:r.PresetListType.BULLET_LIST})},Fe={id:"doc.command.order-list",type:r.CommandType.COMMAND,handler:i=>i.get(r.ICommandService).syncExecuteCommand(ve.id,{listType:r.PresetListType.ORDER_LIST})};function $e(i,n){const{startOffset:e,endOffset:t}=i,o=[];let a=-1;for(const s of n){const{startIndex:c}=s;(e>a&&e<=c||t>a&&t<=c||c>=e&&c<=t)&&o.push(s),a=c}return o}const $={id:"doc.command.align-operation",type:r.CommandType.COMMAND,handler:(i,n)=>{var C,M;const e=i.get(exports.TextSelectionManagerService),t=i.get(r.IUniverInstanceService),o=i.get(r.ICommandService),{alignType:a}=n,s=t.getCurrentUniverDocInstance(),c=e.getActiveRange(),l=(C=e.getSelections())!=null?C:[],d=(M=s.getBody())==null?void 0:M.paragraphs,u=l.map(B);if(c==null||d==null)return!1;const m=$e(c,d),{segmentId:g}=c,S=s.getUnitId(),h=m.every(_=>{var O;return((O=_.paragraphStyle)==null?void 0:O.horizontalAlign)===a}),f={id:A.id,params:{unitId:S,actions:[],textRanges:u}},p=new r.MemoryCursor;p.reset();const v=new r.TextX;for(const _ of m){const{startIndex:O}=_;v.push({t:r.TextXActionType.RETAIN,len:O-p.cursor,segmentId:g});const D={..._.paragraphStyle,horizontalAlign:h?r.HorizontalAlign.UNSPECIFIED:a};v.push({t:r.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[{..._,paragraphStyle:D,startIndex:0}]},segmentId:g,coverType:r.UpdateDocsAttributeType.REPLACE}),p.moveCursorTo(O+1)}return f.params.actions=v.serialize(),!!o.syncExecuteCommand(f.id,f.params)}},Xe={id:"doc.command.align-left",type:r.CommandType.COMMAND,handler:i=>i.get(r.ICommandService).syncExecuteCommand($.id,{alignType:r.HorizontalAlign.LEFT})},ke={id:"doc.command.align-center",type:r.CommandType.COMMAND,handler:i=>i.get(r.ICommandService).syncExecuteCommand($.id,{alignType:r.HorizontalAlign.CENTER})},ze={id:"doc.command.align-right",type:r.CommandType.COMMAND,handler:i=>i.get(r.ICommandService).syncExecuteCommand($.id,{alignType:r.HorizontalAlign.RIGHT})},Ve={id:"doc.command.align-justify",type:r.CommandType.COMMAND,handler:i=>i.get(r.ICommandService).syncExecuteCommand($.id,{alignType:r.HorizontalAlign.JUSTIFIED})},je={id:"doc.command-replace-content",type:r.CommandType.COMMAND,handler:async(i,n)=>{var S;const{unitId:e,body:t,textRanges:o,segmentId:a=""}=n,s=i.get(r.IUniverInstanceService),c=i.get(r.ICommandService),l=i.get(exports.TextSelectionManagerService),d=(S=s.getUniverDocInstance(e))==null?void 0:S.getSnapshot().body,u=l.getSelections();if(d==null||!Array.isArray(u)||u.length===0)return!1;const m=Ge(e,a,d,t);return m.params.textRanges=o,!!c.syncExecuteCommand(m.id,m.params)}},He={id:"doc.command-cover-content",type:r.CommandType.COMMAND,handler:async(i,n)=>{var u;const{unitId:e,body:t,segmentId:o=""}=n,a=i.get(r.IUniverInstanceService),s=i.get(r.ICommandService),c=i.get(r.IUndoRedoService),l=(u=a.getUniverDocInstance(e))==null?void 0:u.getSnapshot().body;if(l==null)return!1;const d=Ge(e,o,l,t);return d.params.noNeedSetTextRange=!0,d.params.noHistory=!0,s.syncExecuteCommand(d.id,d.params),c.clearUndoRedo(e),!0}};function Ge(i,n,e,t){const o={id:A.id,params:{unitId:i,actions:[],textRanges:[]}},a=new r.TextX,s=(e==null?void 0:e.dataStream.length)-2;return s>0&&a.push({t:r.TextXActionType.DELETE,len:s,line:0,segmentId:n}),t.dataStream.length>0&&a.push({t:r.TextXActionType.INSERT,body:t,len:t.dataStream.length,line:0,segmentId:n}),o.params.actions=a.serialize(),o}const J={id:"doc.operation.move-cursor",type:r.CommandType.OPERATION,handler:(i,n)=>!!n},K={id:"doc.operation.move-selection",type:r.CommandType.OPERATION,handler:(i,n)=>!!n},Mt=(i,n)=>{const e=i.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:(i,n)=>{const e=i.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}},Ye={type:r.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(i,n)=>{var m,g;const e=i.get(r.ICommandService),t=i.get(r.IUndoRedoService),o=i.get(r.IUniverInstanceService);let a=o.getCurrentUniverDocInstance().getUnitId(),s=1;if(n&&(a=(m=n.documentId)!=null?m:a,s=(g=n.zoomRatio)!=null?g:s),!o.getUniverDocInstance(a))return!1;const l={zoomRatio:s,unitId:a},d=Mt(i,l);return e.syncExecuteCommand(V.id,l)?(t.pushUndoRedo({unitID:a,undoMutations:[{id:V.id,params:d}],redoMutations:[{id:V.id,params:l}]}),!0):!1}},We={id:"doc.operation.select-all",type:r.CommandType.COMMAND,handler:async i=>{const n=i.get(r.IUniverInstanceService),e=i.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 Tt=Object.defineProperty,xt=Object.getOwnPropertyDescriptor,yt=(i,n,e,t)=>{for(var o=t>1?void 0:t?xt(n,e):n,a=i.length-1,s;a>=0;a--)(s=i[a])&&(o=(t?s(n,e,o):s(o))||o);return t&&o&&Tt(n,e,o),o},X=(i,n)=>(e,t)=>n(e,t,i);let W=class extends r.Disposable{constructor(n,e,t,o,a){super();x(this,"_previousIMEContent","");x(this,"_isCompositionStart",!0);x(this,"_onStartSubscription");x(this,"_onUpdateSubscription");x(this,"_onEndSubscription");this._docSkeletonManagerService=n,this._currentUniverService=e,this._textSelectionRenderManager=t,this._imeInputManagerService=o,this._commandService=a,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;this._resetIME();const{activeRange:e}=n;e!=null&&this._imeInputManagerService.setActiveRange(r.Tools.deepClone(e))})}_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 d;const t=(d=this._docSkeletonManagerService.getCurrent())==null?void 0:d.skeleton;if(n==null||t==null)return;const o=this._currentUniverService.getCurrentUniverDocInstance(),{event:a,activeRange:s}=n;if(t==null||s==null)return;const l=a.data;l===this._previousIMEContent&&e||(await this._commandService.executeCommand(fe.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)}};W=yt([r.OnLifecycle(r.LifecycleStages.Rendered,W),X(0,b.Inject(exports.DocSkeletonManagerService)),X(1,r.IUniverInstanceService),X(2,E.ITextSelectionRenderManager),X(3,b.Inject(j)),X(4,r.ICommandService)],W);var Rt=Object.defineProperty,Ot=Object.getOwnPropertyDescriptor,Dt=(i,n,e,t)=>{for(var o=t>1?void 0:t?Ot(n,e):n,a=i.length-1,s;a>=0;a--)(s=i[a])&&(o=(t?s(n,e,o):s(o))||o);return t&&o&&Rt(n,e,o),o},k=(i,n)=>(e,t)=>n(e,t,i);let Z=class extends r.Disposable{constructor(n,e,t,o,a){super();x(this,"_onInputSubscription");this._docSkeletonManagerService=n,this._currentUniverService=e,this._renderManagerService=t,this._textSelectionManagerService=o,this._commandService=a,this._initialize(),this._commandExecutedListener()}dispose(){var n;(n=this._onInputSubscription)==null||n.unsubscribe()}_initialize(){}_commandExecutedListener(){const n=[J.id,K.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(!n.includes(e.id))return;const t=e.params;switch(e.id){case J.id:return this._handleMoveCursor(t.direction);case K.id:return this._handleShiftMoveSelection(t.direction);default:throw new Error("Unknown command")}}))}_handleShiftMoveSelection(n){var f,p,v;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getSelections(),o=this._currentUniverService.getCurrentUniverDocInstance(),a=(f=this._docSkeletonManagerService.getCurrent())==null?void 0:f.skeleton,s=this._getDocObject();if(e==null||a==null||s==null)return;const{startOffset:c,endOffset:l,style:d,collapsed:u,direction:m}=e;if(t.length>1){let I=Number.POSITIVE_INFINITY,C=Number.NEGATIVE_INFINITY;for(const M of t)I=Math.min(I,M.startOffset),C=Math.max(C,M.endOffset);this._textSelectionManagerService.replaceTextRanges([{startOffset:n===r.Direction.LEFT||n===r.Direction.UP?C:I,endOffset:n===r.Direction.LEFT||n===r.Direction.UP?I:C,style:d}],!1);return}const g=u||m===E.RANGE_DIRECTION.FORWARD?c:l;let S=u||m===E.RANGE_DIRECTION.FORWARD?l:c;const h=(p=o.getBody().dataStream.length)!=null?p:Number.POSITIVE_INFINITY;if(n===r.Direction.LEFT||n===r.Direction.RIGHT){const I=a.findNodeByCharIndex(S-1),C=a.findNodeByCharIndex(S);S=n===r.Direction.RIGHT?S+C.count:S-((v=I==null?void 0:I.count)!=null?v:0),S=Math.min(h-2,Math.max(0,S)),this._textSelectionManagerService.replaceTextRanges([{startOffset:g,endOffset:S,style:d}],!1)}else{const I=a.findNodeByCharIndex(S),C=s.document.getOffsetConfig(),M=this._getTopOrBottomPosition(a,I,n===r.Direction.DOWN);if(M==null){const O=n===r.Direction.UP?0:h-2;if(O===S)return;this._textSelectionManagerService.replaceTextRanges([{startOffset:g,endOffset:O,style:d}],!1);return}const _=new E.NodePositionConvertToCursor(C,a).getRangePointData(M,M).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{startOffset:g,endOffset:_.endOffset,style:d}],!1)}}_handleMoveCursor(n){var g,S,h;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getSelections(),o=this._currentUniverService.getCurrentUniverDocInstance(),a=(g=this._docSkeletonManagerService.getCurrent())==null?void 0:g.skeleton,s=this._getDocObject();if(e==null||a==null||s==null||t==null)return;const{startOffset:c,endOffset:l,style:d,collapsed:u}=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,v=Number.NEGATIVE_INFINITY;for(const I of t)p=Math.min(p,I.startOffset),v=Math.max(v,I.endOffset);f=n===r.Direction.LEFT?p:v}else{const p=a.findNodeByCharIndex(c-1),v=a.findNodeByCharIndex(c);n===r.Direction.LEFT?f=Math.max(0,c-((h=p==null?void 0:p.count)!=null?h:0)):f=Math.min(m-2,l+v.count)}this._textSelectionManagerService.replaceTextRanges([{startOffset:f,endOffset:f,style:d}],!1)}else{const f=a.findNodeByCharIndex(c),p=a.findNodeByCharIndex(l),v=s.document.getOffsetConfig(),I=this._getTopOrBottomPosition(a,n===r.Direction.UP?f:p,n===r.Direction.DOWN);if(I==null){let M;u?M=n===r.Direction.UP?0:m-2:M=n===r.Direction.UP?c:l,this._textSelectionManagerService.replaceTextRanges([{startOffset:M,endOffset:M,style:d}],!1);return}const C=new E.NodePositionConvertToCursor(v,a).getRangePointData(I,I).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{...C,style:d}],!1)}}_getTopOrBottomPosition(n,e,t){if(e==null)return;const o=this._getSpanLeftOffsetInLine(e),a=this._getNextOrPrevLine(e,t);if(a==null)return;const s=this._matchPositionByLeftOffset(n,a,o);if(s!=null)return{...s,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 a of e.divides){const s=a.left;for(const c of a.glyphGroup){const{left:l}=c,d=s+l,u=Math.abs(t-d);u<o.distance&&(o.glyph=c,o.distance=u)}}if(o.glyph!=null)return n.findPositionByGlyph(o.glyph)}_getNextOrPrevLine(n,e){var h,f,p,v,I,C,M,_,O,D,y,R;const t=n.parent;if(t==null)return;const o=t.parent;if(o==null)return;const a=o.parent;if(a==null)return;const s=a.lines.indexOf(o);if(s===-1)return;let c;if(e===!0?c=a.lines[s+1]:c=a.lines[s-1],c!=null)return c;const l=a.parent;if(l==null)return;const d=l.columns.indexOf(a);if(d===-1)return;if(e===!0)c=(h=l.columns[d+1])==null?void 0:h.lines[0];else{const T=(p=(f=l.columns)==null?void 0:f[d-1])==null?void 0:p.lines;c=T==null?void 0:T[T.length-1]}if(c!=null)return c;const u=l.parent;if(u==null)return;const m=u.sections.indexOf(l);if(m===-1)return;if(e===!0)c=(I=(v=u.sections[m-1])==null?void 0:v.columns[0])==null?void 0:I.lines[0];else{const T=(M=(C=u.sections)==null?void 0:C[m-1])==null?void 0:M.columns,w=T==null?void 0:T[T.length-1],U=w==null?void 0:w.lines;c=U==null?void 0:U[U.length-1]}if(c!=null)return c;const g=u.parent;if(g==null)return;const S=g.pages.indexOf(u);if(S!==-1){if(e===!0)c=(D=(O=(_=g.pages[S+1])==null?void 0:_.sections[0])==null?void 0:O.columns[0])==null?void 0:D.lines[0];else{const T=(y=g.pages[S-1])==null?void 0:y.sections;if(T==null)return;const w=(R=T[T.length-1])==null?void 0:R.columns,U=w[w.length-1],Y=U==null?void 0:U.lines;c=Y[Y.length-1]}if(c!=null)return c}}_getDocObject(){return de(this._currentUniverService,this._renderManagerService)}};Z=Dt([r.OnLifecycle(r.LifecycleStages.Rendered,Z),k(0,b.Inject(exports.DocSkeletonManagerService)),k(1,r.IUniverInstanceService),k(2,E.IRenderManagerService),k(3,b.Inject(exports.TextSelectionManagerService)),k(4,r.ICommandService)],Z);var Et=Object.defineProperty,At=Object.getOwnPropertyDescriptor,Ut=(i,n,e,t)=>{for(var o=t>1?void 0:t?At(n,e):n,a=i.length-1,s;a>=0;a--)(s=i[a])&&(o=(t?s(n,e,o):s(o))||o);return t&&o&&Et(n,e,o),o},z=(i,n)=>(e,t)=>n(e,t,i);let q=class extends r.Disposable{constructor(n,e,t,o,a){super();x(this,"_onInputSubscription");this._docSkeletonManagerService=n,this._currentUniverService=e,this._renderManagerService=t,this._textSelectionRenderManager=o,this._commandService=a,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 h;if(n==null)return;const t=this._currentUniverService.getCurrentUniverDocInstance().getUnitId(),{event:o,content:a="",activeRange:s}=n,c=o,l=(h=this._docSkeletonManagerService.getCurrent())==null?void 0:h.skeleton;if(c.data==null||l==null||!l||!s)return;const{startOffset:d,segmentId:u,style:m}=s,g=a.length,S=[{startOffset:d+g,endOffset:d+g,style:m}];await this._commandService.executeCommand(H.id,{unitId:t,body:{dataStream:a},range:s,textRanges:S,segmentId:u})})}_commandExecutedListener(){}_getDocObject(){return de(this._currentUniverService,this._renderManagerService)}};q=Ut([r.OnLifecycle(r.LifecycleStages.Rendered,q),z(0,b.Inject(exports.DocSkeletonManagerService)),z(1,r.IUniverInstanceService),z(2,E.IRenderManagerService),z(3,E.ITextSelectionRenderManager),z(4,r.ICommandService)],q);var Nt=Object.defineProperty,bt=Object.getOwnPropertyDescriptor,Pt=(i,n,e,t)=>{for(var o=t>1?void 0:t?bt(n,e):n,a=i.length-1,s;a>=0;a--)(s=i[a])&&(o=(t?s(n,e,o):s(o))||o);return t&&o&&Nt(n,e,o),o},wt=(i,n)=>(e,t)=>n(e,t,i);const Lt={hasScroll:!0},Bt="docs";var le;exports.UniverDocsPlugin=(le=class extends r.Plugin{constructor(e={},t){super(Bt);x(this,"_config");this._injector=t,this._config=Object.assign(Lt,e),this._initializeDependencies(t),this._initializeCommands()}initialize(){}_initializeCommands(){[J,K,be,Pe,Q,ee,te,ne,F,oe,re,ie,ae,he,Ne,H,G,ge,fe,pe,A,je,He,Ye,V,ue,We,Fe,Be,ve,Xe,ke,ze,$,Ve].forEach(e=>{this._injector.get(r.ICommandService).registerCommand(e)})}onReady(){this.initialize()}_initializeDependencies(e){[[exports.DocSkeletonManagerService],[exports.DocViewModelManagerService],[exports.DocStateChangeManagerService],[j],[E.ITextSelectionRenderManager,{useClass:E.TextSelectionRenderManager}],[exports.TextSelectionManagerService],[q],[W],[Z]].forEach(t=>e.add(t))}},x(le,"type",r.PluginType.Doc),le);exports.UniverDocsPlugin=Pt([wt(1,b.Inject(b.Injector))],exports.UniverDocsPlugin);exports.AlignCenterCommand=ke;exports.AlignJustifyCommand=Ve;exports.AlignLeftCommand=Xe;exports.AlignOperationCommand=$;exports.AlignRightCommand=ze;exports.BreakLineCommand=Ne;exports.BulletListCommand=Be;exports.CoverContentCommand=He;exports.CutContentCommand=Se;exports.DOCS_COMPONENT_DEFAULT_Z_INDEX=et;exports.DOCS_COMPONENT_HEADER_LAYER_INDEX=Qe;exports.DOCS_COMPONENT_MAIN_LAYER_INDEX=Ke;exports.DOCS_VIEW_KEY=Ae;exports.DeleteCommand=G;exports.DeleteLeftCommand=be;exports.DeleteRightCommand=Pe;exports.IMEInputCommand=fe;exports.IMEInputManagerService=j;exports.InnerPasteCommand=ht;exports.InsertCommand=H;exports.MoveCursorOperation=J;exports.MoveSelectionOperation=K;exports.NORMAL_TEXT_SELECTION_PLUGIN_NAME=tt;exports.OrderListCommand=Fe;exports.ReplaceContentCommand=je;exports.RichTextEditingMutation=A;exports.SelectAllOperation=We;exports.SetDocZoomRatioCommand=Ye;exports.SetDocZoomRatioOperation=V;exports.SetInlineFormatBoldCommand=Q;exports.SetInlineFormatCommand=he;exports.SetInlineFormatFontFamilyCommand=ie;exports.SetInlineFormatFontSizeCommand=re;exports.SetInlineFormatItalicCommand=ee;exports.SetInlineFormatStrikethroughCommand=ne;exports.SetInlineFormatSubscriptCommand=F;exports.SetInlineFormatSuperscriptCommand=oe;exports.SetInlineFormatTextColorCommand=ae;exports.SetInlineFormatUnderlineCommand=te;exports.SetTextSelectionsOperation=ue;exports.UpdateCommand=ge;exports.VIEWPORT_KEY=Ue;exports.getDocObject=de;exports.getDocObjectById=Je;exports.serializeTextRange=B;