@univerjs/docs 0.2.2 → 0.2.4-alpha.0

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 (29) hide show
  1. package/README.md +11 -2
  2. package/lib/cjs/index.js +1 -1
  3. package/lib/es/index.js +1944 -1700
  4. package/lib/types/basics/__tests__/plain-text.spec.d.ts +16 -0
  5. package/lib/types/basics/custom-decoration-factory.d.ts +1 -2
  6. package/lib/types/basics/custom-range-factory.d.ts +2 -2
  7. package/lib/types/basics/plain-text.d.ts +6 -0
  8. package/lib/types/basics/replace.d.ts +12 -0
  9. package/lib/types/basics/selection.d.ts +3 -5
  10. package/lib/types/commands/commands/__tests__/create-command-test-bed.d.ts +7 -8
  11. package/lib/types/commands/commands/__tests__/mock-text-selection-render-manager.d.ts +1 -1
  12. package/lib/types/commands/commands/clipboard.inner.command.d.ts +2 -1
  13. package/lib/types/commands/commands/core-editing.command.d.ts +1 -4
  14. package/lib/types/commands/commands/delete.command.d.ts +8 -1
  15. package/lib/types/commands/commands/list.command.d.ts +2 -1
  16. package/lib/types/commands/commands/replace-content.command.d.ts +11 -1
  17. package/lib/types/commands/mutations/core-editing.mutation.d.ts +3 -0
  18. package/lib/types/commands/operations/set-doc-zoom-ratio.operation.d.ts +1 -2
  19. package/lib/types/commands/util.d.ts +1 -2
  20. package/lib/types/controllers/custom-range.controller.d.ts +11 -0
  21. package/lib/types/doc-plugin.d.ts +1 -2
  22. package/lib/types/index.d.ts +6 -2
  23. package/lib/types/services/doc-custom-range.service.d.ts +10 -8
  24. package/lib/types/services/doc-interceptor/doc-interceptor.service.d.ts +1 -1
  25. package/lib/types/services/ime-input-manager.service.d.ts +1 -2
  26. package/lib/types/services/text-selection-manager.service.d.ts +4 -2
  27. package/lib/types/types/enums/delete-direction.d.ts +19 -0
  28. package/lib/umd/index.js +1 -1
  29. package/package.json +8 -10
package/README.md CHANGED
@@ -1,7 +1,10 @@
1
1
  # @univerjs/docs
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/@univerjs/docs)](https://npmjs.org/package/@univerjs/docs)
4
- [![license](https://img.shields.io/npm/l/@univerjs/docs)](https://img.shields.io/npm/l/@univerjs/docs)
3
+ ## Package Overview
4
+
5
+ | Package Name | UMD Namespace | Version | License | Downloads | Contains CSS | Contains i18n locales |
6
+ | --- | --- | --- | --- | --- | :---: | :---: |
7
+ | `@univerjs/docs` | `UniverDocs` | [![][npm-version-shield]][npm-version-link] | ![][npm-license-shield] | ![][npm-downloads-shield] | ❌ | ❌ |
5
8
 
6
9
  ## Introduction
7
10
 
@@ -24,3 +27,9 @@ npm install @univerjs/docs
24
27
  # Using pnpm
25
28
  pnpm add @univerjs/docs
26
29
  ```
30
+
31
+ <!-- Links -->
32
+ [npm-version-shield]: https://img.shields.io/npm/v/@univerjs/docs?style=flat-square
33
+ [npm-version-link]: https://npmjs.com/package/@univerjs/docs
34
+ [npm-license-shield]: https://img.shields.io/npm/l/@univerjs/docs?style=flat-square
35
+ [npm-downloads-shield]: https://img.shields.io/npm/dm/@univerjs/docs?style=flat-square
package/lib/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var Rt=Object.defineProperty;var Ot=(a,o,e)=>o in a?Rt(a,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[o]=e;var M=(a,o,e)=>Ot(a,typeof o!="symbol"?o+"":o,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("@univerjs/core"),E=require("@univerjs/engine-render"),G=require("@wendellhu/redi"),k=require("rxjs");function se(a){return a===n.DataStreamTreeTokenType.CUSTOM_RANGE_END||a===n.DataStreamTreeTokenType.CUSTOM_RANGE_START}function Me(a,o,e,t){return a<=e&&o>=e||a>=e&&a<=t}function De(a,o,e,t){const r=t.slice(e.startIndex+1,e.endIndex),s=Math.max(a-(e.startIndex+1),0),i=a+o-1-(e.startIndex+1);if(i<0)return!1;if(s===0&&i>=r.length)return!0;const d=r.slice(0,s)+r.slice(s+o);for(let c=0,l=d.length;c<l;c++){const u=d[c];if(!se(u))return!1}return!0}function Y(a){const{startOffset:o,endOffset:e,collapsed:t}=a,r=Math.min(o,e),s=Math.max(o,e);return{startOffset:r,endOffset:s,collapsed:t}}function Ee(a,o){let{startOffset:e,endOffset:t}=Y(a);for(;o.dataStream[e-1]===n.DataStreamTreeTokenType.CUSTOM_RANGE_START;)e-=1;for(;o.dataStream[t]===n.DataStreamTreeTokenType.CUSTOM_RANGE_END;)t+=1;return{startOffset:e,endOffset:t}}function de(a,o){let{startOffset:e,endOffset:t,collapsed:r}=Y(a);if(r){for(;se(o.dataStream[e-1]);)t-=1,e-=1;return{startOffset:e,endOffset:t,collapsed:r}}else return{...Ee(a,o),collapsed:!1}}function K(a,o){let{startOffset:e,endOffset:t,collapsed:r}=Y(a);if(r){for(;o.dataStream[t]===n.DataStreamTreeTokenType.CUSTOM_RANGE_END;)t+=1,e+=1;for(;o.dataStream[t-1]===n.DataStreamTreeTokenType.CUSTOM_RANGE_START;)t-=1,e-=1;return{startOffset:e,endOffset:t,collapsed:r}}else return{...Ee(a,o),collapsed:!1}}function Mt(a,o){if(a.startOffset===a.endOffset)return null;const e=o.customRanges;if(!e)return Y(a);let{startOffset:t,endOffset:r}=Ee(a,o);for(;se(o.dataStream[t]);){if(o.dataStream[t]===n.DataStreamTreeTokenType.CUSTOM_RANGE_START){const s=e.find(i=>i.startIndex===t);if(!s)throw new Error("No custom-range matched");if(s.endIndex===r-1)return{startOffset:t,endOffset:r,collapsed:!1};if(s.endIndex<r-1)break}t+=1}for(;se(o.dataStream[r-1]);){if(o.dataStream[t]===n.DataStreamTreeTokenType.CUSTOM_RANGE_END){const s=e.find(i=>i.endIndex===r-1);if(!s)throw new Error("No custom-range matched");if(s.startIndex===t)return{startOffset:t,endOffset:r,collapsed:!1};if(s.startIndex>t)break}r-=1}return r<=t?null:{startOffset:t,endOffset:r,collapsed:!1}}const Dt=[n.DataStreamTreeTokenType.PARAGRAPH,n.DataStreamTreeTokenType.SECTION_BREAK,n.DataStreamTreeTokenType.TABLE_START,n.DataStreamTreeTokenType.TABLE_ROW_START,n.DataStreamTreeTokenType.TABLE_CELL_START,n.DataStreamTreeTokenType.TABLE_CELL_END,n.DataStreamTreeTokenType.TABLE_ROW_END,n.DataStreamTreeTokenType.TABLE_END,n.DataStreamTreeTokenType.CUSTOM_RANGE_START,n.DataStreamTreeTokenType.CUSTOM_RANGE_END,n.DataStreamTreeTokenType.COLUMN_BREAK,n.DataStreamTreeTokenType.PAGE_BREAK,n.DataStreamTreeTokenType.DOCS_END,n.DataStreamTreeTokenType.TAB,n.DataStreamTreeTokenType.CUSTOM_BLOCK];function Et(a,o,e){const t=a.slice(o,e);return Dt.reduce((r,s)=>r.replaceAll(s,""),t)}var Z=(a=>(a.MAIN="__Document_Render_Main__",a.BACKGROUND="__Document_Render_Background__",a))(Z||{}),et=(a=>(a.VIEW_MAIN="viewMain",a.VIEW_TOP="viewTop",a.VIEW_LEFT="viewLeft",a.VIEW_LEFT_TOP="viewLeftTop",a))(et||{});const At=0,Nt=2,Ut=4,bt=10,Pt="normalTextSelectionPluginName";function Bt(a){const{mainComponent:o,scene:e,engine:t,components:r}=a,s=o,i=r.get(Z.BACKGROUND);return{document:s,docBackground:i,scene:e,engine:t}}function tt(a,o){const e=a.getCurrentUnitForType(n.UniverInstanceType.UNIVER_DOC);if(!e)return null;const t=e.getUnitId(),r=o.getRenderById(t);if(r==null)return;const{mainComponent:s,scene:i,engine:d,components:c}=r,l=s,u=c.get(Z.BACKGROUND);return{document:l,docBackground:u,scene:i,engine:d}}function Xt(a,o){const e=o.getRenderById(a);if(e==null)return;const{mainComponent:t,scene:r,engine:s,components:i}=e,d=t,c=i.get(Z.BACKGROUND);return{document:d,docBackground:c,scene:r,engine:s}}const Ae={id:"doc.operation.set-selections",type:n.CommandType.OPERATION,handler:(a,o)=>!0};var wt=Object.defineProperty,Lt=Object.getOwnPropertyDescriptor,Ft=(a,o,e,t)=>{for(var r=t>1?void 0:t?Lt(o,e):o,s=a.length-1,i;s>=0;s--)(i=a[s])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&wt(o,e,r),r},Le=(a,o)=>(e,t)=>o(e,t,a);function $(a){const{startOffset:o,endOffset:e,collapsed:t}=a,r={startOffset:o,endOffset:e,collapsed:t};return typeof a.isActive=="function"&&(r.isActive=a.isActive()),r}exports.TextSelectionManagerService=class extends n.RxDisposable{constructor(e,t){super();M(this,"_currentSelection",null);M(this,"_textSelectionInfo",new Map);M(this,"_textSelection$",new k.BehaviorSubject(null));M(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}getActiveTextRange(){const e=this._getTextRanges(this._currentSelection);if(e==null)return;const{textRanges:t}=e;return t.find(r=>r.isActive())}getActiveRange(){const e=this._getTextRanges(this._currentSelection);if(e==null)return;const{textRanges:t,segmentId:r,style:s,segmentPage:i}=e,d=t.find(f=>f.isActive());if(d==null)return null;const{startOffset:c,endOffset:l,collapsed:u,startNodePosition:g,endNodePosition:m,direction:T}=d;return c==null||l==null?null:{startOffset:c,endOffset:l,collapsed:u,startNodePosition:g,endNodePosition:m,direction:T,segmentId:r,segmentPage:i,style:s}}add(e,t=!0){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:e,segmentId:"",segmentPage:-1,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(k.takeUntil(this.dispose$)).subscribe(e=>{e!=null&&this._replaceTextRangesWithNoRefresh(e)})}_replaceTextRangesWithNoRefresh(e){if(this._currentSelection==null)return;const t={...this._currentSelection,...e};this._replaceByParam(t),this._textSelection$.next(t);const{unitId:r,subUnitId:s,segmentId:i,style:d,textRanges:c,isEditing:l}=t;this._commandService.executeCommand(Ae.id,{unitId:r,subUnitId:s,segmentId:i,style:d,isEditing:l,ranges:c.map($)})}_getTextRanges(e){var s;if(e==null)return;const{unitId:t,subUnitId:r=""}=e;return(s=this._textSelectionInfo.get(t))==null?void 0:s.get(r)}_refresh(e){const t=this._getTextRanges(e);this._textSelectionRenderManager.removeAllTextRanges(),t&&Array.isArray(t.textRanges)&&t.textRanges.length&&this._textSelectionRenderManager.addTextRanges(t.textRanges.map($))}_replaceByParam(e){const{unitId:t,subUnitId:r,style:s,segmentId:i,textRanges:d,isEditing:c,segmentPage:l}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map),this._textSelectionInfo.get(t).set(r,{textRanges:d,style:s,segmentId:i,isEditing:c,segmentPage:l})}_addByParam(e){const{unitId:t,subUnitId:r,textRanges:s,style:i,segmentId:d,isEditing:c,segmentPage:l}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map);const u=this._textSelectionInfo.get(t);u.has(r)?u.get(r).textRanges.push(...s):u.set(r,{textRanges:s,style:i,segmentId:d,isEditing:c,segmentPage:l})}};exports.TextSelectionManagerService=Ft([Le(0,E.ITextSelectionRenderManager),Le(1,n.ICommandService)],exports.TextSelectionManagerService);function Ne(a,o="",e,t){var I;const{startOffset:r,endOffset:s}=a,i=[],d=r-e,c=s-e,l=t.dataStream,u=(I=t.customRanges)==null?void 0:I.filter(S=>Me(S.startIndex,S.endIndex,r,s)),g=new Set(u==null?void 0:u.filter(S=>De(r,s-r,S,l))),m=new Set;u==null||u.forEach(S=>{g.has(S)||(S.startIndex-e>=d&&S.startIndex-e<=c&&S.endIndex-e>c&&m.add(S.startIndex),S.endIndex-e>=d&&S.endIndex-e<=c&&S.startIndex<d&&m.add(S.endIndex))}),d>0&&i.push({t:n.TextXActionType.RETAIN,len:d,segmentId:o});const T=[...m].sort((S,p)=>S-p);let f=d;return T.forEach(S=>{const p=S-f;p>0&&i.push({t:n.TextXActionType.DELETE,len:p,line:0,segmentId:o}),i.push({t:n.TextXActionType.RETAIN,len:1,segmentId:o}),f=S+1}),f<c&&(i.push({t:n.TextXActionType.DELETE,len:c-f,line:0,segmentId:o}),f=c+1),{dos:i,cursor:f,retain:m.size}}var kt=Object.defineProperty,Gt=Object.getOwnPropertyDescriptor,$t=(a,o,e,t)=>{for(var r=t>1?void 0:t?Gt(o,e):o,s=a.length-1,i;s>=0;s--)(i=a[s])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&kt(o,e,r),r},Oe=(a,o)=>(e,t)=>o(e,t,a);const Fe=300;exports.DocStateChangeManagerService=class extends n.RxDisposable{constructor(e,t,r){super();M(this,"_docStateChange$",new k.BehaviorSubject(null));M(this,"docStateChange$",this._docStateChange$.asObservable());M(this,"_historyStateCache",new Map);M(this,"_changeStateCache",new Map);M(this,"_historyTimer",null);M(this,"_changeStateCacheTimer",null);this._undoRedoService=e,this._commandService=t,this._univerInstanceService=r,this._initialize()}setChangeState(e){this._cacheChangeState(e,"history"),this._cacheChangeState(e,"collaboration")}_initialize(){this.disposeWithMe(this._commandService.beforeCommandExecuted(e=>{if(e.id===n.UndoCommandId||e.id===n.RedoCommandId){const t=this._univerInstanceService.getCurrentUniverDocInstance();if(t==null)return;const r=t.getUnitId();this._pushHistory(r),this._emitChangeState(r)}}))}_cacheChangeState(e,t="history"){const{trigger:r,unitId:s,noHistory:i,debounce:d=!1}=e;if(i||t==="history"&&(r===n.RedoCommandId||r===n.UndoCommandId))return;const c=t==="history"?this._historyStateCache:this._changeStateCache,l=t==="history"?this._pushHistory.bind(this):this._emitChangeState.bind(this);if(c.has(s)){const u=c.get(s);u==null||u.push(e)}else c.set(s,[e]);d?t==="history"?(this._historyTimer&&clearTimeout(this._historyTimer),this._historyTimer=setTimeout(()=>{l(s)},Fe)):(this._changeStateCacheTimer&&clearTimeout(this._changeStateCacheTimer),this._changeStateCacheTimer=setTimeout(()=>{l(s)},Fe)):l(s)}_pushHistory(e){const t=this._undoRedoService,r=this._historyStateCache.get(e);if(!Array.isArray(r)||r.length===0)return;const s=r.length,i=r[0].commandId,d=r[0],c=r[s-1],l={unitId:e,actions:r.reduce((g,m)=>n.JSONX.compose(g,m.redoState.actions),null),textRanges:c.redoState.textRanges},u={unitId:e,actions:r.reverse().reduce((g,m)=>n.JSONX.compose(g,m.undoState.actions),null),textRanges:d.undoState.textRanges};t.pushUndoRedo({unitID:e,undoMutations:[{id:i,params:u}],redoMutations:[{id:i,params:l}]}),r.length=0}_emitChangeState(e){const t=this._changeStateCache.get(e);if(!Array.isArray(t)||t.length===0)return;const r=t.length,{commandId:s,trigger:i,segmentId:d,noHistory:c,debounce:l}=t[0],u=t[0],g=t[r-1],m={unitId:e,actions:t.reduce((I,S)=>n.JSONX.compose(I,S.redoState.actions),null),textRanges:g.redoState.textRanges},T={unitId:e,actions:t.reverse().reduce((I,S)=>n.JSONX.compose(I,S.undoState.actions),null),textRanges:u.undoState.textRanges},f={commandId:s,unitId:e,trigger:i,redoState:m,undoState:T,segmentId:d,noHistory:c,debounce:l};t.length=0,this._docStateChange$.next(f)}};exports.DocStateChangeManagerService=$t([Oe(0,G.Inject(n.IUndoRedoService)),Oe(1,n.ICommandService),Oe(2,n.IUniverInstanceService)],exports.DocStateChangeManagerService);class q{constructor(){M(this,"_previousActiveRange",null);M(this,"_undoMutationParamsCache",[]);M(this,"_redoMutationParamsCache",[])}clearUndoRedoMutationParamsCache(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[]}getUndoRedoMutationParamsCache(){return{undoCache:this._undoMutationParamsCache,redoCache:this._redoMutationParamsCache}}setUndoRedoMutationParamsCache({undoCache:o=[],redoCache:e=[]}){this._undoMutationParamsCache=o,this._redoMutationParamsCache=e}getActiveRange(){return this._previousActiveRange}setActiveRange(o){this._previousActiveRange=o}pushUndoRedoMutationParams(o,e){this._undoMutationParamsCache.push(o),this._redoMutationParamsCache.push(e)}fetchComposedUndoRedoMutationParams(){if(this._undoMutationParamsCache.length===0||this._previousActiveRange==null||this._redoMutationParamsCache.length===0)return null;const{unitId:o}=this._undoMutationParamsCache[0],e={unitId:o,actions:this._undoMutationParamsCache.reverse().reduce((r,s)=>n.JSONX.compose(r,s.actions),null),textRanges:[]};return{redoMutationParams:{unitId:o,actions:this._redoMutationParamsCache.reduce((r,s)=>n.JSONX.compose(r,s.actions),null),textRanges:[]},undoMutationParams:e,previousActiveRange:this._previousActiveRange}}dispose(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[],this._previousActiveRange=null}}var jt=Object.defineProperty,zt=Object.getOwnPropertyDescriptor,Ht=(a,o,e,t)=>{for(var r=t>1?void 0:t?zt(o,e):o,s=a.length-1,i;s>=0;s--)(i=a[s])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&jt(o,e,r),r},ke=(a,o)=>(e,t)=>o(e,t,a);exports.DocSkeletonManagerService=class extends n.RxDisposable{constructor(e,t,r){super();M(this,"_skeleton");M(this,"_docViewModel");M(this,"_currentSkeleton$",new k.BehaviorSubject(null));M(this,"currentSkeleton$",this._currentSkeleton$.asObservable());M(this,"_currentSkeletonBefore$",new k.BehaviorSubject(null));M(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());M(this,"_currentViewModel$",new k.BehaviorSubject(null));M(this,"currentViewModel$",this._currentViewModel$.asObservable());this._context=e,this._localeService=t,this._univerInstanceService=r,this._init(),this._univerInstanceService.getCurrentTypeOfUnit$(n.UniverInstanceType.UNIVER_DOC).pipe(k.takeUntil(this.dispose$)).subscribe(s=>{(s==null?void 0:s.getUnitId())===this._context.unitId&&this._update(s)})}dispose(){super.dispose(),this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete()}getSkeleton(){return this._skeleton}getViewModel(){return this._docViewModel}_init(){const e=this._context.unit;this._update(e)}_update(e){const t=this._context.unitId;if(e.getBody()==null)return;this._docViewModel&&t===n.DOCS_NORMAL_EDITOR_UNIT_ID_KEY?(this._docViewModel.reset(e),this._context.unit=e):this._docViewModel||(this._docViewModel=this._buildDocViewModel(e)),this._skeleton||(this._skeleton=this._buildSkeleton(this._docViewModel));const r=this._skeleton;r.calculate(),this._currentSkeletonBefore$.next(r),this._currentSkeleton$.next(r),this._currentViewModel$.next(this._docViewModel)}_buildSkeleton(e){return E.DocumentSkeleton.create(e,this._localeService)}_buildDocViewModel(e){return new E.DocumentViewModel(e)}};exports.DocSkeletonManagerService=Ht([ke(1,G.Inject(n.LocaleService)),ke(2,n.IUniverInstanceService)],exports.DocSkeletonManagerService);const Ge="doc.mutation.rich-text-editing",U={id:Ge,type:n.CommandType.MUTATION,handler:(a,o)=>{var x,D;const{unitId:e,segmentId:t="",actions:r,textRanges:s,prevTextRanges:i,trigger:d,noHistory:c,isCompositionEnd:l,noNeedSetTextRange:u,debounce:g}=o,m=a.get(n.IUniverInstanceService),T=a.get(E.IRenderManagerService),f=m.getUniverDocInstance(e),I=(x=T.getRenderById(e))==null?void 0:x.with(exports.DocSkeletonManagerService).getViewModel();if(f==null||I==null)throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${e}`);const S=a.get(exports.TextSelectionManagerService),h=((D=S.getSelections())!=null?D:[]).map($),C=a.get(exports.DocStateChangeManagerService),_=a.get(q),O=!!f.getSnapshot().disabled;if(n.JSONX.isNoop(r)||r&&r.length===0||O)return{unitId:e,actions:[],textRanges:h};const v=n.JSONX.invertWithDoc(r,f.getSnapshot());f.apply(r),I.reset(f),!u&&s&&d!=null&&queueMicrotask(()=>{S.replaceTextRanges(s)});const R={commandId:Ge,unitId:e,segmentId:t,trigger:d,noHistory:c,debounce:g,redoState:{actions:r,textRanges:s},undoState:{actions:v,textRanges:i!=null?i:h}};if(l){const y=_.fetchComposedUndoRedoMutationParams();if(y==null)throw new Error("historyParams is null in RichTextEditingMutation");const{undoMutationParams:A,redoMutationParams:N,previousActiveRange:b}=y;R.redoState.actions=N.actions,R.undoState.actions=A.actions,R.undoState.textRanges=[b]}return C.setChangeState(R),{unitId:e,actions:v,textRanges:h}}};function nt(a,o){var t;return(t=a.get(E.IRenderManagerService).getRenderById(o))==null?void 0:t.with(exports.DocSkeletonManagerService)}function P(a,o=""){if(!o)return["body"];const{headers:e,footers:t}=a.getSnapshot();if(e==null&&t==null)throw new Error("Document data model must have headers or footers when update by segment id");if((e==null?void 0:e[o])!=null)return["headers",o,"body"];if((t==null?void 0:t[o])!=null)return["footers",o,"body"];throw new Error("Segment id not found in headers or footers")}const ot="doc.command.insert-text",W={id:ot,type:n.CommandType.COMMAND,handler:async(a,o)=>{var x;const e=a.get(n.ICommandService),{range:t,segmentId:r,body:s,unitId:i,textRanges:d,cursorOffset:c}=o,l=a.get(exports.TextSelectionManagerService),g=a.get(n.IUniverInstanceService).getUnit(i,n.UniverInstanceType.UNIVER_DOC);if(g==null)return!1;const m=l.getActiveRange(),T=g.getSelfOrHeaderFooterModel((x=m==null?void 0:m.segmentId)!=null?x:"").getBody();if(!T)return!1;const f=K(t,T),{startOffset:I,collapsed:S}=f,p=c!=null?c:s.dataStream.length,h=[{startOffset:I+p,endOffset:I+p,style:m==null?void 0:m.style,collapsed:S}],C={id:U.id,params:{unitId:i,actions:[],textRanges:d!=null?d:h,debounce:!0}},_=new n.TextX,O=n.JSONX.getInstance();if(S)I>0&&_.push({t:n.TextXActionType.RETAIN,len:I,segmentId:r});else{const{dos:D,retain:y}=Ne(f,r,0,T);_.push(...D),d||(C.params.textRanges=[{startOffset:I+p+y,endOffset:I+p+y,collapsed:S}])}_.push({t:n.TextXActionType.INSERT,body:s,len:s.dataStream.length,line:0,segmentId:r});const v=P(g,r);return C.params.actions=O.editOp(_.serialize(),v),!!e.syncExecuteCommand(C.id,C.params)}};var L=(a=>(a[a.LEFT=0]="LEFT",a[a.RIGHT=1]="RIGHT",a))(L||{});const le={id:"doc.command.delete-text",type:n.CommandType.COMMAND,handler:async(a,o)=>{var D;const e=a.get(n.ICommandService),t=a.get(n.IUniverInstanceService),{range:r,segmentId:s,unitId:i,direction:d,len:c=1}=o,l=t.getUnit(i,n.UniverInstanceType.UNIVER_DOC),u=l==null?void 0:l.getSelfOrHeaderFooterModel(s).getBody();if(l==null||u==null)return!1;const{startOffset:g}=r,m=u.dataStream,T=d===0?g-c:g,f=T+c-1,I=(D=u.customRanges)==null?void 0:D.filter(y=>Me(y.startIndex,y.endIndex,T,f)),S=I==null?void 0:I.filter(y=>De(T,c,y,m)),p=[];for(let y=0;y<c;y++)p.push(T+y);S==null||S.forEach(y=>{p.push(y.startIndex,y.endIndex)}),p.sort((y,A)=>y-A);const h=p[0],C={id:U.id,params:{unitId:i,actions:[],textRanges:[{startOffset:h,endOffset:h,collapsed:!0}],debounce:!0}},_=new n.TextX,O=n.JSONX.getInstance();let v=0;for(let y=0;y<p.length;y++){const A=p[y];A-v>0&&_.push({t:n.TextXActionType.RETAIN,len:A-v,segmentId:s}),_.push({t:n.TextXActionType.DELETE,len:1,segmentId:s,line:0}),v=A+1}const R=P(l,s);return C.params.actions=O.editOp(_.serialize(),R),!!e.syncExecuteCommand(C.id,C.params)}},Ue={id:"doc.command.update-text",type:n.CommandType.COMMAND,handler:async(a,o)=>{const{range:e,segmentId:t,updateBody:r,coverType:s,unitId:i,textRanges:d}=o,c=a.get(n.ICommandService),u=a.get(n.IUniverInstanceService).getCurrentUniverDocInstance();if(u==null)return!1;const g={id:U.id,params:{unitId:i,actions:[],textRanges:d}},m=new n.TextX,T=n.JSONX.getInstance(),{startOffset:f,endOffset:I}=e;m.push({t:n.TextXActionType.RETAIN,len:f,segmentId:t}),m.push({t:n.TextXActionType.RETAIN,body:r,len:I-f,segmentId:t,coverType:s});const S=P(u,t);return g.params.actions=T.editOp(m.serialize(),S),!!c.syncExecuteCommand(g.id,g.params)}};function $e(a,o){const e=[];for(let t=0,r=a.length;t<r;t++)a[t]===n.DataStreamTreeTokenType.PARAGRAPH&&e.push({startIndex:t});if(o)for(const t of e)o.bullet&&(t.bullet=n.Tools.deepClone(o.bullet)),o.paragraphStyle&&(t.paragraphStyle=n.Tools.deepClone(o.paragraphStyle));return e}const rt={id:"doc.command.break-line",type:n.CommandType.COMMAND,handler:async a=>{var T,f;const o=a.get(exports.TextSelectionManagerService),e=a.get(n.IUniverInstanceService),t=a.get(n.ICommandService),r=o.getActiveRange();if(r==null)return!1;const s=e.getCurrentUniverDocInstance(),i=s==null?void 0:s.getBody();if(!s||!i)return!1;const d=s.getUnitId(),{startOffset:c,endOffset:l}=K(r,i),{segmentId:u}=r,m=((f=(T=s.getSelfOrHeaderFooterModel(u).getBody())==null?void 0:T.paragraphs)!=null?f:[]).find(I=>I.startIndex>=c);if(m&&m.startIndex>l){const I=n.normalizeBody(n.getBodySlice(i,l,m.startIndex)),S={startOffset:c,endOffset:m.startIndex,collapsed:!1};return n.updateAttributeByInsert(I,{dataStream:n.DataStreamTreeTokenType.PARAGRAPH,paragraphs:$e(n.DataStreamTreeTokenType.PARAGRAPH,m)},1,0),await t.executeCommand(W.id,{unitId:d,body:I,range:S,segmentId:u,cursorOffset:1})}else return await t.executeCommand(W.id,{unitId:d,body:{dataStream:n.DataStreamTreeTokenType.PARAGRAPH,paragraphs:$e(n.DataStreamTreeTokenType.PARAGRAPH,m)},range:r,segmentId:u})}},Vt={id:"doc.command.inner-paste",type:n.CommandType.COMMAND,handler:async(a,o)=>{const{segmentId:e,textRanges:t}=o,r=o.body,s=a.get(n.ICommandService),i=a.get(exports.TextSelectionManagerService),d=a.get(n.IUniverInstanceService),c=i.getSelections();if(!Array.isArray(c)||c.length===0)return!1;const l=d.getCurrentUniverDocInstance(),u=l==null?void 0:l.getSelfOrHeaderFooterModel(e).getBody();if(l==null||u==null)return!1;const g=l.getUnitId(),m={id:U.id,params:{unitId:g,actions:[],textRanges:t}},T=new n.MemoryCursor;T.reset();const f=new n.TextX,I=n.JSONX.getInstance();for(const h of c){const{startOffset:C,endOffset:_,collapsed:O}=h,v=C-T.cursor;if(O)f.push({t:n.TextXActionType.RETAIN,len:v,segmentId:e});else{const{dos:R}=Ne(h,e,T.cursor,u);f.push(...R)}f.push({t:n.TextXActionType.INSERT,body:r,len:r.dataStream.length,line:0,segmentId:e}),T.reset(),T.moveCursor(_)}const S=P(l,e);return m.params.actions=I.editOp(f.serialize(),S),!!s.syncExecuteCommand(m.id,m.params)}},be={id:"doc.command.inner-cut",type:n.CommandType.COMMAND,handler:async(a,o)=>{var p;const{segmentId:e,textRanges:t}=o,r=a.get(n.ICommandService),s=a.get(exports.TextSelectionManagerService),i=a.get(n.IUniverInstanceService),d=s.getSelections();if(!Array.isArray(d)||d.length===0)return!1;const c=(p=i.getCurrentUniverDocInstance())==null?void 0:p.getUnitId();if(!c)return!1;const l=i.getUniverDocInstance(c),u=n.getDocsUpdateBody(l.getSnapshot(),e);if(l==null||u==null)return!1;const g={id:U.id,params:{unitId:c,actions:[],textRanges:t}},m=new n.MemoryCursor;m.reset();const T=new n.TextX,f=n.JSONX.getInstance();for(const h of d){const{startOffset:C,endOffset:_,collapsed:O}=h,v=C-m.cursor;O?T.push({t:n.TextXActionType.RETAIN,len:v,segmentId:e}):T.push(...Jt(h,u,e,m.cursor)),m.reset(),m.moveCursor(_)}const I=P(l,e);return g.params.actions=f.editOp(T.serialize(),I),!!r.syncExecuteCommand(g.id,g.params)}};function Jt(a,o,e="",t=0){var p;const{startOffset:r,endOffset:s}=de(a,o),i=[],{paragraphs:d=[],dataStream:c}=o,l=r-t,u=s-t,g=d==null?void 0:d.find(h=>h.startIndex-t>=l&&h.startIndex-t<=u),m=(p=o.customRanges)==null?void 0:p.filter(h=>Me(h.startIndex,h.endIndex,r,s)),T=new Set(m==null?void 0:m.filter(h=>De(r,s-r,h,c))),f=new Set;if(m==null||m.forEach(h=>{T.has(h)||(h.startIndex-t>=l&&h.startIndex-t<=u&&h.endIndex-t>u&&f.add(h.startIndex),h.endIndex-t>=l&&h.endIndex-t<=u&&h.startIndex<l&&f.add(h.endIndex))}),l>0&&i.push({t:n.TextXActionType.RETAIN,len:l,segmentId:e}),g&&g.startIndex-t>l){const h=g.startIndex-t;f.add(h)}const I=[...f].sort((h,C)=>h-C);let S=l;return I.forEach(h=>{const C=h-S;C>0&&i.push({t:n.TextXActionType.DELETE,len:C,line:0,segmentId:e}),i.push({t:n.TextXActionType.RETAIN,len:1,segmentId:e}),S=h+1}),S<u&&i.push({t:n.TextXActionType.DELETE,len:u-S,line:0,segmentId:e}),i}const ue={id:"doc.command.merge-two-paragraph",type:n.CommandType.COMMAND,handler:async(a,o)=>{var A,N;const e=a.get(exports.TextSelectionManagerService),t=a.get(n.IUniverInstanceService),r=a.get(n.ICommandService),{direction:s,range:i}=o,d=e.getActiveRange(),c=e.getSelections();if(d==null||c==null)return!1;const{segmentId:l,style:u}=d,g=t.getCurrentUniverDocInstance(),m=g==null?void 0:g.getSelfOrHeaderFooterModel(l).getBody();if(!g||!m)return!1;const T=de(d,m),{startOffset:f,collapsed:I}=T;if(!I)return!1;const S=s===L.LEFT?f:f+1,p=(N=(A=m.paragraphs)==null?void 0:A.find(b=>b.startIndex>=S))==null?void 0:N.startIndex,h=Wt(m,S,p),C=s===L.LEFT?f-1:f,_=g.getUnitId(),O=[{startOffset:C,endOffset:C,style:u}],v={id:U.id,params:{unitId:_,actions:[],textRanges:O,prevTextRanges:[i]}},R=new n.TextX,x=n.JSONX.getInstance();R.push({t:n.TextXActionType.RETAIN,len:s===L.LEFT?f-1:f,segmentId:l}),h.dataStream.length&&R.push({t:n.TextXActionType.INSERT,body:h,len:h.dataStream.length,line:0,segmentId:l}),R.push({t:n.TextXActionType.RETAIN,len:1,segmentId:l}),R.push({t:n.TextXActionType.DELETE,len:p+1-S,line:0,segmentId:l});const D=P(g,l);return v.params.actions=x.editOp(R.serialize(),D),!!r.syncExecuteCommand(v.id,v.params)}},at={id:"doc.command.delete-left",type:n.CommandType.COMMAND,handler:async a=>{const o=a.get(exports.TextSelectionManagerService),e=a.get(n.IUniverInstanceService),t=a.get(n.ICommandService);let r=!0;const s=e.getCurrentUniverDocInstance();if(!s)return!1;const i=s.getUnitId(),d=nt(a,i),c=o.getActiveRange(),l=o.getSelections(),u=d==null?void 0:d.getSkeleton();if(c==null||u==null||l==null)return!1;const{segmentId:g,style:m,segmentPage:T}=c,f=s.getSelfOrHeaderFooterModel(g).getBody();if(f==null)return!1;const I=de(c,f),{startOffset:S,collapsed:p}=I,h=u.findNodeByCharIndex(S,g,T),C=E.hasListGlyph(h),_=E.isIndentByGlyph(h,f);let O=S;const v=u.findNodeByCharIndex(S-1,g,T);if(E.isFirstGlyph(h)&&v!==h&&(C===!0||_===!0)&&p){const x=E.getParagraphByGlyph(h,f);if(x==null)return!1;const D=x==null?void 0:x.startIndex,y={startIndex:0},A=x.paragraphStyle;if(C===!0){const b=x.paragraphStyle;if(b){y.paragraphStyle=b;const{hanging:H}=b;H&&(y.paragraphStyle.indentStart=H,y.paragraphStyle.hanging=void 0)}}else if(_===!0){const b=x.bullet;b&&(y.bullet=b),A!=null&&(y.paragraphStyle={...A},delete y.paragraphStyle.hanging,delete y.paragraphStyle.indentStart)}const N=[{startOffset:O,endOffset:O,style:m}];r=await t.executeCommand(Ue.id,{unitId:s.getUnitId(),updateBody:{dataStream:"",paragraphs:[{...y}]},range:{startOffset:D,endOffset:D+1},textRanges:N,coverType:n.UpdateDocsAttributeType.REPLACE,segmentId:g})}else if(p===!0){if(v==null)return!0;v.content==="\r"?r=await t.executeCommand(ue.id,{direction:L.LEFT,range:I}):(O-=v.count,r=await t.executeCommand(le.id,{unitId:s.getUnitId(),range:I,segmentId:g,direction:L.LEFT,len:v.count}))}else{const x=it({...c,...I},l);r=await t.executeCommand(be.id,{segmentId:g,textRanges:x})}return r}},st={id:"doc.command.delete-right",type:n.CommandType.COMMAND,handler:async a=>{const o=a.get(exports.TextSelectionManagerService),t=a.get(n.IUniverInstanceService).getCurrentUniverDocInstance();if(!t)return!1;const r=nt(a,t.getUnitId()),s=a.get(n.ICommandService),i=o.getActiveRange(),d=o.getSelections(),c=r==null?void 0:r.getSkeleton();if(i==null||c==null||d==null)return!1;const{segmentId:l,style:u,segmentPage:g}=i,m=t==null?void 0:t.getSelfOrHeaderFooterModel(l).getBody();if(!t||!m)return!1;const T=K(i,m),{startOffset:f,collapsed:I}=T;if(f===m.dataStream.length-2&&I)return!0;let S=!1;if(I===!0){const p=c.findNodeByCharIndex(f,l,g);if(p.content==="\r")S=await s.executeCommand(ue.id,{direction:L.RIGHT,range:i});else{const h=[{startOffset:f,endOffset:f,style:u}];S=await s.executeCommand(le.id,{unitId:t.getUnitId(),range:{startOffset:f,endOffset:f,collapsed:I},segmentId:l,direction:L.RIGHT,textRanges:h,len:p.count})}}else{const p=it(i,d);S=await s.executeCommand(be.id,{segmentId:l,textRanges:p})}return S}};function Wt(a,o,e){const{textRuns:t}=a,r=a.dataStream.substring(o,e);if(t==null)return{dataStream:r,customRanges:n.getCustomRangeSlice(a,o,e).customRanges,customDecorations:n.getCustomDecorationSlice(a,o,e)};const s=[];for(const i of t){const{st:d,ed:c}=i;c<=o||d>=e||(d<o?s.push({...i,st:0,ed:c-o}):c>e?s.push({...i,st:d-o,ed:e-o}):s.push({...i,st:d-o,ed:c-o}))}return{dataStream:r,textRuns:s,customRanges:n.getCustomRangeSlice(a,o,e).customRanges,customDecorations:n.getCustomDecorationSlice(a,o,e)}}function it(a,o){let e=a.endOffset;for(const r of o){const{startOffset:s,endOffset:i}=r;s==null||i==null||i<=a.endOffset&&(e-=i-s)}return[{startOffset:e,endOffset:e,style:a.style}]}const Pe={id:"doc.command.ime-input",type:n.CommandType.COMMAND,handler:async(a,o)=>{const{unitId:e,newText:t,oldTextLen:r,isCompositionEnd:s,isCompositionStart:i}=o,d=a.get(n.ICommandService),c=a.get(q),u=a.get(n.IUniverInstanceService).getCurrentUniverDocInstance();if(u==null)return!1;const g=c.getActiveRange();if(!g)return!1;const{startOffset:m,style:T,segmentId:f}=g,I=u.getSelfOrHeaderFooterModel(f).getBody();if(I==null)return!1;const S=K(g,I);Object.assign(g,S);const p=t.length,h=[{startOffset:m+p,endOffset:m+p,collapsed:!0,style:T}],C={id:U.id,params:{unitId:e,actions:[],textRanges:h}},_=new n.TextX,O=n.JSONX.getInstance();if(!g.collapsed&&i){const{dos:x,retain:D,cursor:y}=Ne(g,f,0,I);_.push(...x),C.params.textRanges=[{startOffset:m+p+D,endOffset:m+p+D,collapsed:!0}]}else _.push({t:n.TextXActionType.RETAIN,len:m,segmentId:f});r>0&&_.push({t:n.TextXActionType.DELETE,len:r,line:0,segmentId:f}),_.push({t:n.TextXActionType.INSERT,body:{dataStream:t},len:t.length,line:0,segmentId:f});const v=P(u,f);C.params.actions=O.editOp(_.serialize(),v),C.params.noHistory=!s,C.params.isCompositionEnd=s;const R=d.syncExecuteCommand(C.id,C.params);return c.pushUndoRedoMutationParams(R,C.params),!!R}};function B(a,o,e,t){var s;const{segmentId:r}=(s=e.getActiveRange())!=null?s:{};return r==null?!1:t.executeCommand(Be.id,{segmentId:r,preCommandId:a,...o!=null?o:{}})}const je="doc.command.set-inline-format-bold",me={id:je,type:n.CommandType.COMMAND,handler:async(a,o)=>{const e=a.get(n.ICommandService),t=a.get(exports.TextSelectionManagerService);return B(je,o,t,e)}},ze="doc.command.set-inline-format-italic",ge={id:ze,type:n.CommandType.COMMAND,handler:async(a,o)=>{const e=a.get(n.ICommandService),t=a.get(exports.TextSelectionManagerService);return B(ze,o,t,e)}},He="doc.command.set-inline-format-underline",Se={id:He,type:n.CommandType.COMMAND,handler:async(a,o)=>{const e=a.get(n.ICommandService),t=a.get(exports.TextSelectionManagerService);return B(He,o,t,e)}},Ve="doc.command.set-inline-format-strikethrough",fe={id:Ve,type:n.CommandType.COMMAND,handler:async(a,o)=>{const e=a.get(n.ICommandService),t=a.get(exports.TextSelectionManagerService);return B(Ve,o,t,e)}},Je="doc.command.set-inline-format-subscript",j={id:Je,type:n.CommandType.COMMAND,handler:async(a,o)=>{const e=a.get(n.ICommandService),t=a.get(exports.TextSelectionManagerService);return B(Je,o,t,e)}},We="doc.command.set-inline-format-superscript",pe={id:We,type:n.CommandType.COMMAND,handler:async(a,o)=>{const e=a.get(n.ICommandService),t=a.get(exports.TextSelectionManagerService);return B(We,o,t,e)}},Ye="doc.command.set-inline-format-fontsize",he={id:Ye,type:n.CommandType.COMMAND,handler:async(a,o)=>{const e=a.get(n.ICommandService),t=a.get(exports.TextSelectionManagerService);return B(Ye,o,t,e)}},Ke="doc.command.set-inline-format-font-family",Ie={id:Ke,type:n.CommandType.COMMAND,handler:async(a,o)=>{const e=a.get(n.ICommandService),t=a.get(exports.TextSelectionManagerService);return B(Ke,o,t,e)}},Ze="doc.command.set-inline-format-text-color",Te={id:Ze,type:n.CommandType.COMMAND,handler:async(a,o)=>{const e=a.get(n.ICommandService),t=a.get(exports.TextSelectionManagerService);return B(Ze,o,t,e)}},qe="doc.command.set-inline-format-text-background-color",ve={id:qe,type:n.CommandType.COMMAND,handler:async(a,o)=>{const e=a.get(n.ICommandService),t=a.get(exports.TextSelectionManagerService);return B(qe,o,t,e)}},Qe="doc.command.reset-inline-format-text-background-color",Ce={id:Qe,type:n.CommandType.COMMAND,handler:async(a,o)=>{const e=a.get(n.ICommandService),t=a.get(exports.TextSelectionManagerService);return B(Qe,o,t,e)}},ct={[me.id]:"bl",[ge.id]:"it",[Se.id]:"ul",[fe.id]:"st",[he.id]:"fs",[Ie.id]:"ff",[Te.id]:"cl",[ve.id]:"bg",[Ce.id]:"bg",[j.id]:"va",[pe.id]:"va"},Be={id:"doc.command.set-inline-format",type:n.CommandType.COMMAND,handler:async(a,o)=>{const{segmentId:e,value:t,preCommandId:r}=o,s=a.get(n.ICommandService),i=a.get(exports.TextSelectionManagerService),d=a.get(n.IUniverInstanceService),c=i.getSelections();if(!Array.isArray(c)||c.length===0)return!1;const l=d.getCurrentUniverDocInstance();if(l==null)return!1;const u=l.getUnitId();let g;switch(r){case me.id:case ge.id:case Se.id:case fe.id:case j.id:case pe.id:{g=Kt(l.getSelfOrHeaderFooterModel(e).getBody().textRuns,r,c);break}case he.id:case Ie.id:{g=t;break}case Te.id:case ve.id:{g={rgb:t};break}case Ce.id:{g={rgb:null};break}default:throw new Error(`Unknown command: ${r} in handleInlineFormat`)}const m={id:U.id,params:{unitId:u,actions:[],textRanges:c.map($)}},T=new n.TextX,f=n.JSONX.getInstance(),I=new n.MemoryCursor;I.reset();for(const h of c){const{startOffset:C,endOffset:_}=h,O={dataStream:"",textRuns:[{st:0,ed:_-C,ts:{[ct[r]]:g}}]},v=C-I.cursor;v!==0&&T.push({t:n.TextXActionType.RETAIN,len:v,segmentId:e}),T.push({t:n.TextXActionType.RETAIN,body:O,len:_-C,segmentId:e}),I.reset(),I.moveCursor(_)}const S=P(l,e);return m.params.actions=f.editOp(T.serialize(),S),!!s.syncExecuteCommand(m.id,m.params)}};function Yt(a){return a!==null&&typeof a=="object"}function Kt(a,o,e){let t=0,r=0;const s=ct[o];for(;t!==a.length&&r!==e.length;){const{startOffset:i,endOffset:d}=e[r],{st:c,ed:l,ts:u}=a[t];if(d<=c)r++;else if(l<=i)t++;else{if(/bl|it/.test(s))return(u==null?void 0:u[s])===n.BooleanNumber.TRUE?n.BooleanNumber.FALSE:n.BooleanNumber.TRUE;if(/ul|st/.test(s))return Yt(u==null?void 0:u[s])&&(u==null?void 0:u[s]).s===n.BooleanNumber.TRUE?{s:n.BooleanNumber.FALSE}:{s:n.BooleanNumber.TRUE};if(/va/.test(s))return o===j.id?(u==null?void 0:u[s])===n.BaselineOffset.SUBSCRIPT?n.BaselineOffset.NORMAL:n.BaselineOffset.SUBSCRIPT:(u==null?void 0:u[s])===n.BaselineOffset.SUPERSCRIPT?n.BaselineOffset.NORMAL:n.BaselineOffset.SUPERSCRIPT;t++}}return/bl|it/.test(s)?n.BooleanNumber.TRUE:/ul|st/.test(s)?{s:n.BooleanNumber.TRUE}:o===j.id?n.BaselineOffset.SUBSCRIPT:n.BaselineOffset.SUPERSCRIPT}const _e={id:"doc.command.list-operation",type:n.CommandType.COMMAND,handler:(a,o)=>{var N,b,H,Xe;const e=a.get(exports.TextSelectionManagerService),t=a.get(n.IUniverInstanceService),r=a.get(n.ICommandService),{listType:s}=o,i=t.getCurrentUniverDocInstance(),d=e.getActiveRange();if(i==null||d==null)return!1;const{segmentId:c}=d,l=(N=e.getSelections())!=null?N:[],u=(b=i.getSelfOrHeaderFooterModel(c).getBody())==null?void 0:b.paragraphs,g=l.map($);if(u==null)return!1;const m=ut(d,u),T=i.getUnitId(),f=m.every(F=>{var X;return((X=F.bullet)==null?void 0:X.listType)===s});let S=n.Tools.generateRandomId(6);if(m.length===1){const F=u.indexOf(m[0]),X=u[F-1],w=u[F+1];X&&X.bullet&&X.bullet.listType===s?S=X.bullet.listId:w&&w.bullet&&w.bullet.listType===s&&(S=w.bullet.listId)}const p={id:U.id,params:{unitId:T,actions:[],textRanges:g}},h=new n.MemoryCursor;h.reset();const C=new n.TextX,_=n.JSONX.getInstance(),O=(H=i.getSnapshot().lists)!=null?H:{},v={...n.PRESET_LIST_TYPE,...O},{charSpace:R,defaultTabStop:x=36,gridType:D}=i.getSnapshot().documentStyle;for(const F of m){const{startIndex:X,paragraphStyle:w={}}=F,{indentFirstLine:yt=0,snapToGrid:xt,indentStart:ye=0}=w,{hanging:xe,indentStart:we}=v[s].nestingLevel[0],Re=E.getCharSpaceApply(R,x,D,xt);C.push({t:n.TextXActionType.RETAIN,len:X-h.cursor,segmentId:c}),C.push({t:n.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[f?{paragraphStyle:{...w,hanging:void 0,indentStart:ye?Math.max(0,E.getNumberUnitValue(ye,Re)+xe-we):void 0},startIndex:0}:{startIndex:0,paragraphStyle:{...w,indentFirstLine:void 0,hanging:xe,indentStart:we-xe+E.getNumberUnitValue(yt,Re)+E.getNumberUnitValue(ye,Re)},bullet:{...(Xe=F.bullet)!=null?Xe:{nestingLevel:0,textStyle:{fs:20}},listType:s,listId:S}}]},segmentId:c,coverType:n.UpdateDocsAttributeType.REPLACE}),h.moveCursorTo(X+1)}const y=P(i,c);return p.params.actions=_.editOp(C.serialize(),y),!!r.syncExecuteCommand(p.id,p.params)}},dt={id:"doc.command.bullet-list",type:n.CommandType.COMMAND,handler:a=>a.get(n.ICommandService).syncExecuteCommand(_e.id,{listType:n.PresetListType.BULLET_LIST})},lt={id:"doc.command.order-list",type:n.CommandType.COMMAND,handler:a=>a.get(n.ICommandService).syncExecuteCommand(_e.id,{listType:n.PresetListType.ORDER_LIST})};function ut(a,o){const{startOffset:e,endOffset:t}=a,r=[];let s=-1;for(const i of o){const{startIndex:d}=i;(e>s&&e<=d||t>s&&t<=d||d>=e&&d<=t)&&r.push(i),s=d}return r}const mt={id:"doc.command-replace-content",type:n.CommandType.COMMAND,handler:async(a,o)=>{const{unitId:e,body:t,textRanges:r,segmentId:s=""}=o,i=a.get(n.IUniverInstanceService),d=a.get(n.ICommandService),c=a.get(exports.TextSelectionManagerService),l=i.getUniverDocInstance(e),u=l==null?void 0:l.getSnapshot().body,g=c.getSelections();if(l==null||u==null||!Array.isArray(g)||g.length===0)return!1;const m=St(e,s,l,u,t);return m.params.textRanges=r,!!d.syncExecuteCommand(m.id,m.params)}},gt={id:"doc.command-cover-content",type:n.CommandType.COMMAND,handler:async(a,o)=>{const{unitId:e,body:t,segmentId:r=""}=o,s=a.get(n.IUniverInstanceService),i=a.get(n.ICommandService),d=a.get(n.IUndoRedoService),c=s.getUniverDocInstance(e),l=c==null?void 0:c.getSnapshot().body;if(c==null||l==null)return!1;const u=St(e,r,c,l,t);return u.params.noNeedSetTextRange=!0,u.params.noHistory=!0,i.syncExecuteCommand(u.id,u.params),d.clearUndoRedo(e),!0}};function St(a,o,e,t,r){const s={id:U.id,params:{unitId:a,actions:[],textRanges:[]}},i=new n.TextX,d=n.JSONX.getInstance(),c=(t==null?void 0:t.dataStream.length)-2;c>0&&i.push({t:n.TextXActionType.DELETE,len:c,line:0,segmentId:o}),r.dataStream.length>0&&i.push({t:n.TextXActionType.INSERT,body:r,len:r.dataStream.length,line:0,segmentId:o});const l=P(e,o);return s.params.actions=d.editOp(i.serialize(),l),s}const Zt=(a,o)=>{const e=a.get(n.IUniverInstanceService).getUniverDocInstance(o.unitId),t=(e==null?void 0:e.zoomRatio)||1;return{...n.Tools.deepClone(o),zoomRatio:t}},J={id:"doc.operation.set-zoom-ratio",type:n.CommandType.OPERATION,handler:(a,o)=>{const e=a.get(n.IUniverInstanceService).getUniverDocInstance(o.unitId);if(!e)return!1;const t=e.getSnapshot();return t.settings==null?t.settings={zoomRatio:o.zoomRatio}:t.settings.zoomRatio=o.zoomRatio,!0}},ft={type:n.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(a,o)=>{var g,m,T;const e=a.get(n.ICommandService),t=a.get(n.IUndoRedoService),r=a.get(n.IUniverInstanceService);let s=(g=r.getCurrentUniverDocInstance())==null?void 0:g.getUnitId();if(!s)return!1;let i=1;if(o&&(s=(m=o.documentId)!=null?m:s,i=(T=o.zoomRatio)!=null?T:i),!r.getUniverDocInstance(s))return!1;const c={zoomRatio:i,unitId:s},l=Zt(a,c);return e.syncExecuteCommand(J.id,c)?(t.pushUndoRedo({unitID:s,undoMutations:[{id:J.id,params:l}],redoMutations:[{id:J.id,params:c}]}),!0):!1}},ie={id:"doc.operation.move-cursor",type:n.CommandType.OPERATION,handler:(a,o)=>!!o},ce={id:"doc.operation.move-selection",type:n.CommandType.OPERATION,handler:(a,o)=>!!o},pt={id:"doc.operation.select-all",type:n.CommandType.COMMAND,handler:async a=>{const o=a.get(n.IUniverInstanceService),e=a.get(exports.TextSelectionManagerService),t=o.getCurrentUniverDocInstance(),r=e.getActiveRange();if(t==null||r==null)return!1;const{segmentId:s}=r,i=t.getSelfOrHeaderFooterModel(s).getSnapshot().body;if(i==null)return!1;const d=[{startOffset:0,endOffset:i.dataStream.length-2}];return e.replaceTextRanges(d,!1),!0}};var qt=Object.defineProperty,Qt=Object.getOwnPropertyDescriptor,en=(a,o,e,t)=>{for(var r=t>1?void 0:t?Qt(o,e):o,s=a.length-1,i;s>=0;s--)(i=a[s])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&qt(o,e,r),r},V=(a,o)=>(e,t)=>o(e,t,a);let ne=class extends n.Disposable{constructor(o,e,t,r,s){super();M(this,"_previousIMEContent","");M(this,"_isCompositionStart",!0);M(this,"_onStartSubscription");M(this,"_onUpdateSubscription");M(this,"_onEndSubscription");this._univerInstanceService=o,this._renderManagerSrv=e,this._textSelectionRenderManager=t,this._imeInputManagerService=r,this._commandService=s,this._initialize()}dispose(){var o,e,t;(o=this._onStartSubscription)==null||o.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(o=>{if(o==null)return;this._resetIME();const{activeRange:e}=o;e!=null&&this._imeInputManagerService.setActiveRange(n.Tools.deepClone(e))})}_initialOnCompositionUpdate(){this._onUpdateSubscription=this._textSelectionRenderManager.onCompositionupdate$.subscribe(async o=>{this._updateContent(o,!0)})}_initialOnCompositionend(){this._onEndSubscription=this._textSelectionRenderManager.onCompositionend$.subscribe(o=>{this._updateContent(o,!1)})}async _updateContent(o,e){var l;if(o==null)return;const t=this._univerInstanceService.getCurrentUniverDocInstance();if(!t)return;const r=(l=this._renderManagerSrv.getRenderById(t.getUnitId()))==null?void 0:l.with(exports.DocSkeletonManagerService).getSkeleton(),{event:s,activeRange:i}=o;if(r==null||i==null)return;const c=s.data;c===this._previousIMEContent&&e||(await this._commandService.executeCommand(Pe.id,{unitId:t.getUnitId(),newText:c,oldTextLen:this._previousIMEContent.length,isCompositionStart:this._isCompositionStart,isCompositionEnd:!e}),e?(this._isCompositionStart&&(this._isCompositionStart=!1),this._previousIMEContent=c):this._resetIME())}_resetIME(){this._previousIMEContent="",this._isCompositionStart=!0,this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(null)}};ne=en([n.OnLifecycle(n.LifecycleStages.Rendered,ne),V(0,n.IUniverInstanceService),V(1,E.IRenderManagerService),V(2,E.ITextSelectionRenderManager),V(3,G.Inject(q)),V(4,n.ICommandService)],ne);var tn=Object.defineProperty,nn=Object.getOwnPropertyDescriptor,on=(a,o,e,t)=>{for(var r=t>1?void 0:t?nn(o,e):o,s=a.length-1,i;s>=0;s--)(i=a[s])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&tn(o,e,r),r},Q=(a,o)=>(e,t)=>o(e,t,a);let oe=class extends n.Disposable{constructor(o,e,t,r){super();M(this,"_onInputSubscription");this._univerInstanceService=o,this._renderManagerService=e,this._textSelectionManagerService=t,this._commandService=r,this._commandExecutedListener()}dispose(){var o;super.dispose(),(o=this._onInputSubscription)==null||o.unsubscribe()}_commandExecutedListener(){const o=[ie.id,ce.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(!o.includes(e.id))return;const t=e.params;switch(e.id){case ie.id:return this._handleMoveCursor(t.direction);case ce.id:return this._handleShiftMoveSelection(t.direction);default:throw new Error("Unknown command")}}))}_handleShiftMoveSelection(o){var C,_,O;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getSelections(),r=this._univerInstanceService.getCurrentUniverDocInstance();if(!r)return;const s=(C=this._renderManagerService.getRenderById(r.getUnitId()))==null?void 0:C.with(exports.DocSkeletonManagerService).getSkeleton(),i=this._getDocObject();if(e==null||s==null||i==null)return;const{startOffset:d,endOffset:c,style:l,collapsed:u,direction:g,segmentId:m,startNodePosition:T,endNodePosition:f,segmentPage:I}=e;if(t.length>1){let v=Number.POSITIVE_INFINITY,R=Number.NEGATIVE_INFINITY;for(const x of t)v=Math.min(v,x.startOffset),R=Math.max(R,x.endOffset);this._textSelectionManagerService.replaceTextRanges([{startOffset:o===n.Direction.LEFT||o===n.Direction.UP?R:v,endOffset:o===n.Direction.LEFT||o===n.Direction.UP?v:R,style:l}],!1);return}const S=u||g===E.RANGE_DIRECTION.FORWARD?d:c;let p=u||g===E.RANGE_DIRECTION.FORWARD?c:d;const h=(_=r.getSelfOrHeaderFooterModel(m).getBody().dataStream.length)!=null?_:Number.POSITIVE_INFINITY;if(o===n.Direction.LEFT||o===n.Direction.RIGHT){const v=s.findNodeByCharIndex(p-1,m,I),R=s.findNodeByCharIndex(p,m,I);p=o===n.Direction.RIGHT?p+R.count:p-((O=v==null?void 0:v.count)!=null?O:0),p=Math.min(h-2,Math.max(0,p)),this._textSelectionManagerService.replaceTextRanges([{startOffset:S,endOffset:p,style:l}],!1)}else{const v=s.findNodeByCharIndex(p,m,I),R=i.document.getOffsetConfig(),x=u?T:g===E.RANGE_DIRECTION.FORWARD?f:T,D=this._getTopOrBottomPosition(s,v,x,o===n.Direction.DOWN);if(D==null){const A=o===n.Direction.UP?0:h-2;if(A===p)return;this._textSelectionManagerService.replaceTextRanges([{startOffset:S,endOffset:A,style:l}],!1);return}const y=new E.NodePositionConvertToCursor(R,s).getRangePointData(D,D).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{startOffset:S,endOffset:y.endOffset,style:l}],!1)}}_handleMoveCursor(o){var S,p,h;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getSelections(),r=this._univerInstanceService.getCurrentUniverDocInstance();if(!r)return!1;const s=(S=this._renderManagerService.getRenderById(r.getUnitId()))==null?void 0:S.with(exports.DocSkeletonManagerService).getSkeleton(),i=this._getDocObject();if(e==null||s==null||i==null||t==null)return;const{startOffset:d,endOffset:c,style:l,collapsed:u,segmentId:g,startNodePosition:m,endNodePosition:T,segmentPage:f}=e,I=(p=r.getSelfOrHeaderFooterModel(g).getBody().dataStream.length)!=null?p:Number.POSITIVE_INFINITY;if(o===n.Direction.LEFT||o===n.Direction.RIGHT){let C;if(!e.collapsed||t.length>1){let _=Number.POSITIVE_INFINITY,O=Number.NEGATIVE_INFINITY;for(const v of t)_=Math.min(_,v.startOffset),O=Math.max(O,v.endOffset);C=o===n.Direction.LEFT?_:O}else{const _=s.findNodeByCharIndex(d-1,g,f),O=s.findNodeByCharIndex(d,g,f);o===n.Direction.LEFT?C=Math.max(0,d-((h=_==null?void 0:_.count)!=null?h:0)):C=Math.min(I-2,c+O.count)}this._textSelectionManagerService.replaceTextRanges([{startOffset:C,endOffset:C,style:l}],!1)}else{const C=s.findNodeByCharIndex(d,g,f),_=s.findNodeByCharIndex(c,g,f),O=i.document.getOffsetConfig(),v=this._getTopOrBottomPosition(s,o===n.Direction.UP?C:_,o===n.Direction.UP?m:T,o===n.Direction.DOWN);if(v==null){let x;u?x=o===n.Direction.UP?0:I-2:x=o===n.Direction.UP?d:c,this._textSelectionManagerService.replaceTextRanges([{startOffset:x,endOffset:x,style:l}],!1);return}const R=new E.NodePositionConvertToCursor(O,s).getRangePointData(v,v).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{...R,style:l}],!1)}}_getTopOrBottomPosition(o,e,t,r){if(e==null||t==null)return;const s=this._getGlyphLeftOffsetInLine(e),i=this._getNextOrPrevLine(e,r);if(i==null)return;const d=this._matchPositionByLeftOffset(o,i,s,t);if(d!=null)return{...d,isBack:!0}}_getGlyphLeftOffsetInLine(o){const e=o.parent;if(e==null)return Number.NEGATIVE_INFINITY;const t=e.left,{left:r}=o;return t+r}_matchPositionByLeftOffset(o,e,t,r){const s={distance:Number.POSITIVE_INFINITY};for(const d of e.divides){const c=d.left;for(const l of d.glyphGroup){const{left:u}=l,g=c+u,m=Math.abs(t-g);m<s.distance&&(s.glyph=l,s.distance=m)}}if(s.glyph==null)return;const{segmentPage:i}=r;return o.findPositionByGlyph(s.glyph,i)}_getNextOrPrevLine(o,e){var f,I,S,p,h,C,_,O,v,R,x,D;const t=o.parent;if(t==null)return;const r=t.parent;if(r==null)return;const s=r.parent;if(s==null)return;const i=s.lines.indexOf(r);if(i===-1)return;let d;if(e===!0?d=s.lines[i+1]:d=s.lines[i-1],d!=null)return d;const c=s.parent;if(c==null)return;const l=c.columns.indexOf(s);if(l===-1)return;if(e===!0)d=(f=c.columns[l+1])==null?void 0:f.lines[0];else{const y=(S=(I=c.columns)==null?void 0:I[l-1])==null?void 0:S.lines;d=y==null?void 0:y[y.length-1]}if(d!=null)return d;const u=c.parent;if(u==null)return;const g=u.sections.indexOf(c);if(g===-1)return;if(e===!0)d=(h=(p=u.sections[g-1])==null?void 0:p.columns[0])==null?void 0:h.lines[0];else{const y=(_=(C=u.sections)==null?void 0:C[g-1])==null?void 0:_.columns,A=y==null?void 0:y[y.length-1],N=A==null?void 0:A.lines;d=N==null?void 0:N[N.length-1]}if(d!=null)return d;const m=u.parent;if(m==null)return;const T=m.pages.indexOf(u);if(T!==-1){if(e===!0)d=(R=(v=(O=m.pages[T+1])==null?void 0:O.sections[0])==null?void 0:v.columns[0])==null?void 0:R.lines[0];else{const y=(x=m.pages[T-1])==null?void 0:x.sections;if(y==null)return;const A=(D=y[y.length-1])==null?void 0:D.columns,N=A[A.length-1],b=N==null?void 0:N.lines;d=b[b.length-1]}if(d!=null)return d}}_getDocObject(){return tt(this._univerInstanceService,this._renderManagerService)}};oe=on([n.OnLifecycle(n.LifecycleStages.Rendered,oe),Q(0,n.IUniverInstanceService),Q(1,E.IRenderManagerService),Q(2,G.Inject(exports.TextSelectionManagerService)),Q(3,n.ICommandService)],oe);var rn=Object.defineProperty,an=Object.getOwnPropertyDescriptor,sn=(a,o,e,t)=>{for(var r=t>1?void 0:t?an(o,e):o,s=a.length-1,i;s>=0;s--)(i=a[s])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&rn(o,e,r),r},ee=(a,o)=>(e,t)=>o(e,t,a);let re=class extends n.Disposable{constructor(o,e,t,r){super();M(this,"_onInputSubscription");this._univerInstanceService=o,this._renderManagerService=e,this._textSelectionRenderManager=t,this._commandService=r,this._init()}dispose(){var o;super.dispose(),(o=this._onInputSubscription)==null||o.unsubscribe()}_init(){this._initialNormalInput()}_initialNormalInput(){this._onInputSubscription=this._textSelectionRenderManager.onInput$.subscribe(async o=>{var I;if(o==null)return;const e=this._univerInstanceService.getCurrentUniverDocInstance();if(!e)return;const t=e.getUnitId(),{event:r,content:s="",activeRange:i}=o,d=r,c=(I=this._renderManagerService.getRenderById(e.getUnitId()))==null?void 0:I.with(exports.DocSkeletonManagerService).getSkeleton();if(d.data==null||c==null||!c||!i)return;const{startOffset:l,segmentId:u,style:g,segmentPage:m}=i,T=s.length,f=[{startOffset:l+T,endOffset:l+T,segmentId:u,segmentPage:m,style:g}];await this._commandService.executeCommand(W.id,{unitId:t,body:{dataStream:s},textRanges:f,range:i,segmentId:u})})}};re=sn([n.OnLifecycle(n.LifecycleStages.Rendered,re),ee(0,n.IUniverInstanceService),ee(1,E.IRenderManagerService),ee(2,E.ITextSelectionRenderManager),ee(3,n.ICommandService)],re);const z={id:"doc.command.align-operation",type:n.CommandType.COMMAND,handler:(a,o)=>{var O,v;const e=a.get(exports.TextSelectionManagerService),t=a.get(n.IUniverInstanceService),r=a.get(n.ICommandService),{alignType:s}=o,i=t.getCurrentUniverDocInstance(),d=e.getActiveRange();if(i==null||d==null)return!1;const{segmentId:c}=d,l=(O=e.getSelections())!=null?O:[],u=(v=i.getSelfOrHeaderFooterModel(c).getBody())==null?void 0:v.paragraphs,g=l.map($);if(u==null)return!1;const m=ut(d,u),T=i.getUnitId(),f=m.every(R=>{var x;return((x=R.paragraphStyle)==null?void 0:x.horizontalAlign)===s}),I={id:U.id,params:{unitId:T,actions:[],textRanges:g}},S=new n.MemoryCursor;S.reset();const p=new n.TextX,h=n.JSONX.getInstance();for(const R of m){const{startIndex:x}=R;p.push({t:n.TextXActionType.RETAIN,len:x-S.cursor,segmentId:c});const D={...R.paragraphStyle,horizontalAlign:f?n.HorizontalAlign.UNSPECIFIED:s};p.push({t:n.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[{...R,paragraphStyle:D,startIndex:0}]},segmentId:c,coverType:n.UpdateDocsAttributeType.REPLACE}),S.moveCursorTo(x+1)}const C=P(i,c);return I.params.actions=h.editOp(p.serialize(),C),!!r.syncExecuteCommand(I.id,I.params)}},ht={id:"doc.command.align-left",type:n.CommandType.COMMAND,handler:a=>a.get(n.ICommandService).syncExecuteCommand(z.id,{alignType:n.HorizontalAlign.LEFT})},It={id:"doc.command.align-center",type:n.CommandType.COMMAND,handler:a=>a.get(n.ICommandService).syncExecuteCommand(z.id,{alignType:n.HorizontalAlign.CENTER})},Tt={id:"doc.command.align-right",type:n.CommandType.COMMAND,handler:a=>a.get(n.ICommandService).syncExecuteCommand(z.id,{alignType:n.HorizontalAlign.RIGHT})},vt={id:"doc.command.align-justify",type:n.CommandType.COMMAND,handler:a=>a.get(n.ICommandService).syncExecuteCommand(z.id,{alignType:n.HorizontalAlign.JUSTIFIED})};class Ct{constructor(){M(this,"_customRangeHooks",[])}addClipboardHook(o){return this._customRangeHooks.push(o),n.toDisposable(()=>{const e=this._customRangeHooks.indexOf(o);e>-1&&this._customRangeHooks.splice(e,1)})}copyCustomRange(o){let e={...o};return this._customRangeHooks.forEach(t=>{t.onCopyCustomRange&&(e=t.onCopyCustomRange(e))}),e}}var cn=Object.defineProperty,dn=Object.getOwnPropertyDescriptor,ln=(a,o,e,t)=>{for(var r=t>1?void 0:t?dn(o,e):o,s=a.length-1,i;s>=0;s--)(i=a[s])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&cn(o,e,r),r},un=(a,o)=>(e,t)=>o(e,t,a);const mn="docs";var te;exports.UniverDocsPlugin=(te=class extends n.Plugin{constructor(o={},e){super(),this._injector=e,this._initializeDependencies(e),this._initializeCommands()}_initializeCommands(){[ie,ce,at,st,me,ge,Se,fe,j,pe,he,Ie,Te,Ce,ve,Be,rt,W,le,Ue,Pe,ue,U,mt,gt,ft,J,Ae,pt,lt,dt,_e,ht,It,Tt,z,vt].forEach(o=>{this._injector.get(n.ICommandService).registerCommand(o)})}_initializeDependencies(o){[[exports.DocStateChangeManagerService],[q],[E.ITextSelectionRenderManager,{useClass:E.TextSelectionRenderManager}],[exports.TextSelectionManagerService],[Ct],[re],[ne],[oe]].forEach(e=>o.add(e))}},M(te,"pluginName",mn),M(te,"type",n.UniverInstanceType.UNIVER_DOC),te);exports.UniverDocsPlugin=ln([un(1,G.Inject(G.Injector))],exports.UniverDocsPlugin);function gn(a,o){const{range:e,rangeId:t,rangeType:r,segmentId:s}=a,i=Mt(e,o);if(!i)return null;const{startOffset:d,endOffset:c}=i,l=new n.TextX;return d>0&&l.push({t:n.TextXActionType.RETAIN,len:d,segmentId:s}),l.push({t:n.TextXActionType.INSERT,body:{dataStream:n.DataStreamTreeTokenType.CUSTOM_RANGE_START},len:1,line:0}),l.push({t:n.TextXActionType.RETAIN,body:{dataStream:""},len:c-d,segmentId:s}),l.push({t:n.TextXActionType.INSERT,body:{dataStream:n.DataStreamTreeTokenType.CUSTOM_RANGE_END,customRanges:[{rangeId:t,rangeType:r,startIndex:-(c-d)-1,endIndex:0}]},len:1,line:0}),l}function Sn(a,o){const e={id:U.id,params:{unitId:a.unitId,actions:[],textRanges:void 0}},t=n.JSONX.getInstance(),r=gn(a,o);return r?(e.params.actions=t.editOp(r.serialize()),e):!1}function fn(a,o){var O;const{segmentId:e,rangeId:t,rangeType:r}=o,s=a.get(exports.TextSelectionManagerService),i=a.get(n.IUniverInstanceService),d=s.getActiveRange();if(!d)return!1;const c=i.getCurrentUnitForType(n.UniverInstanceType.UNIVER_DOC);if(!c)return!1;const l=c.getBody(),u=c.getUnitId();if(!l)return!1;const{startOffset:g,endOffset:m}=Y(d),T=(O=l.customRanges)!=null?O:[],f=[];for(let v=0,R=T.length;v<R;v++){const x=T[v];if(x.rangeType===r&&Math.max(x.startIndex,g)<=Math.min(x.endIndex,m-1)&&f.push({...x}),x.startIndex>=m)break}const I=f.map(v=>[v.startIndex,v.endIndex]).flat().sort((v,R)=>v-R);let S=0;const p=new n.TextX,h=I.length?{startOffset:Math.min(I[0],g),endOffset:Math.max(I[I.length-1]+1,m)}:d;h.startOffset!==S&&(p.push({t:n.TextXActionType.RETAIN,len:h.startOffset-S,segmentId:e}),S=h.startOffset),p.push({t:n.TextXActionType.INSERT,body:{dataStream:n.DataStreamTreeTokenType.CUSTOM_RANGE_START},len:1,line:0,segmentId:e}),I.forEach((v,R)=>{v!==S&&(p.push({t:n.TextXActionType.RETAIN,len:v-S,segmentId:e}),S=v),p.push({t:n.TextXActionType.DELETE,len:1,line:0,segmentId:e}),S++}),S!==h.endOffset&&(p.push({t:n.TextXActionType.RETAIN,len:h.endOffset-S,segmentId:e}),S=h.endOffset),p.push({t:n.TextXActionType.INSERT,body:{dataStream:n.DataStreamTreeTokenType.CUSTOM_RANGE_END,customRanges:[{rangeId:t,rangeType:r,startIndex:-(h.endOffset-h.startOffset-I.length+1),endIndex:0}]},len:1,line:0,segmentId:e});const C=n.JSONX.getInstance(),_={id:U.id,params:{unitId:u,actions:[],textRanges:void 0}};return _.params.actions=C.editOp(p.serialize()),_}function pn(a,o){var m,T;const{unitId:e,rangeId:t,segmentId:r}=o,i=a.get(n.IUniverInstanceService).getUnit(e);if(!i)return!1;const d=(T=(m=i.getBody())==null?void 0:m.customRanges)==null?void 0:T.find(f=>f.rangeId===t);if(!d)return!1;const{startIndex:c,endIndex:l}=d,u=new n.TextX,g=l-c+1;return c>0&&u.push({t:n.TextXActionType.RETAIN,len:c,segmentId:r}),u.push({t:n.TextXActionType.DELETE,len:1,segmentId:r,line:0}),g-2>0&&u.push({t:n.TextXActionType.RETAIN,len:g-2,segmentId:r}),u.push({t:n.TextXActionType.DELETE,len:1,segmentId:r,line:0}),u}function hn(a,o){const e={id:U.id,params:{unitId:o.unitId,actions:[],textRanges:void 0}},t=n.JSONX.getInstance(),r=pn(a,o);return r?(e.params.actions=t.editOp(r.serialize()),e):!1}function _t(a){const{unitId:o,range:e,id:t,type:r,segmentId:s}=a,{startOffset:i,endOffset:d}=e,c={id:U.id,params:{unitId:o,actions:[],textRanges:void 0}},l=new n.TextX,u=n.JSONX.getInstance();return i>0&&l.push({t:n.TextXActionType.RETAIN,len:i,segmentId:s}),l.push({t:n.TextXActionType.RETAIN,body:{dataStream:"",customDecorations:[{id:t,type:r,startIndex:0,endIndex:d-i-1}]},len:d-i,segmentId:s}),c.params.actions=u.editOp(l.serialize()),c}function In(a,o){const{segmentId:e,id:t,type:r}=o,s=a.get(exports.TextSelectionManagerService),i=a.get(n.IUniverInstanceService),d=s.getActiveRange();if(!d)return!1;const c=i.getCurrentUnitForType(n.UniverInstanceType.UNIVER_DOC);if(!c)return!1;const l=c.getBody(),u=c.getUnitId();return l?_t({unitId:u,range:{startOffset:d.startOffset,endOffset:d.endOffset,collapsed:!0},id:t,type:r,segmentId:e}):!1}function Tn(a,o){var I,S;const{unitId:e,id:t,segmentId:r}=o,i=a.get(n.IUniverInstanceService).getUnit(e),d=i==null?void 0:i.getBody();if(!i||!d)return!1;const c=(S=(I=i.getBody())==null?void 0:I.customDecorations)==null?void 0:S.filter(p=>p.id===t);if(!(c!=null&&c.length))return!1;const l=c.map(p=>n.getBodySlice(d,p.startIndex,p.endIndex+1)),u=l.map(p=>{var C;const h=n.Tools.deepClone(p);return h.customDecorations=(C=h.customDecorations)==null?void 0:C.filter(_=>_.id!==t),h}),g={id:U.id,params:{unitId:e,actions:[],textRanges:void 0}},m=new n.TextX,T=n.JSONX.getInstance();let f=0;return c.forEach((p,h)=>{const C=u[h],_=l[h];p.startIndex!==f&&m.push({t:n.TextXActionType.RETAIN,len:p.startIndex-f,segmentId:r}),f=p.startIndex,m.push({t:n.TextXActionType.RETAIN,len:p.endIndex-p.startIndex+1,segmentId:r,body:C,oldBody:_,coverType:n.UpdateDocsAttributeType.REPLACE}),f=f+(p.endIndex-p.startIndex+1)}),g.params.actions=T.editOp(m.serialize()),g}const vn=n.createInterceptorKey("CUSTOM_RANGE"),Cn=n.createInterceptorKey("CUSTOM_DECORATION"),ae={CUSTOM_RANGE:vn,CUSTOM_DECORATION:Cn};var _n=Object.defineProperty,yn=Object.getOwnPropertyDescriptor,xn=(a,o,e,t)=>{for(var r=t>1?void 0:t?yn(o,e):o,s=a.length-1,i;s>=0;s--)(i=a[s])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&_n(o,e,r),r},Rn=(a,o)=>(e,t)=>o(e,t,a);exports.DocInterceptorService=class extends n.Disposable{constructor(e,t){super();M(this,"_interceptorsByName",new Map);this._context=e,this._docSkeletonManagerService=t,this.disposeWithMe(this._docSkeletonManagerService.currentViewModel$.subscribe(r=>{if(r){const s=r.getDataModel().getUnitId();if(s===n.DOCS_NORMAL_EDITOR_UNIT_ID_KEY||s===n.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY)return;this.interceptDocumentViewModel(r)}})),this.disposeWithMe(this.intercept(ae.CUSTOM_RANGE,{priority:-1,handler:(r,s,i)=>i(r)}))}intercept(e,t){const r=e;this._interceptorsByName.has(r)||this._interceptorsByName.set(r,[]);const s=this._interceptorsByName.get(r);return s.push(t),this._interceptorsByName.set(r,s.sort((i,d)=>{var c,l;return((c=d.priority)!=null?c:0)-((l=i.priority)!=null?l:0)})),this.disposeWithMe(n.toDisposable(()=>n.remove(this._interceptorsByName.get(r),t)))}fetchThroughInterceptors(e){const t=e,r=this._interceptorsByName.get(t);return n.composeInterceptors(r||[])}interceptDocumentViewModel(e){const t=new n.DisposableCollection;return t.add(e.registerCustomRangeInterceptor({getCustomRange:r=>{var s;return this.fetchThroughInterceptors(ae.CUSTOM_RANGE)(e.getCustomRangeRaw(r),{index:r,unitId:e.getDataModel().getUnitId(),customRanges:(s=e.getDataModel().getCustomRanges())!=null?s:[]})},getCustomDecoration:r=>{var s;return this.fetchThroughInterceptors(ae.CUSTOM_DECORATION)(e.getCustomDecorationRaw(r),{index:r,unitId:e.getDataModel().getUnitId(),customDecorations:(s=e.getDataModel().getCustomDecorations())!=null?s:[]})}})),t}};exports.DocInterceptorService=xn([n.OnLifecycle(n.LifecycleStages.Starting,exports.DocInterceptorService),Rn(1,G.Inject(exports.DocSkeletonManagerService))],exports.DocInterceptorService);exports.AlignCenterCommand=It;exports.AlignJustifyCommand=vt;exports.AlignLeftCommand=ht;exports.AlignOperationCommand=z;exports.AlignRightCommand=Tt;exports.BreakLineCommand=rt;exports.BulletListCommand=dt;exports.CoverContentCommand=gt;exports.CutContentCommand=be;exports.DOCS_COMPONENT_BACKGROUND_LAYER_INDEX=At;exports.DOCS_COMPONENT_DEFAULT_Z_INDEX=bt;exports.DOCS_COMPONENT_HEADER_LAYER_INDEX=Ut;exports.DOCS_COMPONENT_MAIN_LAYER_INDEX=Nt;exports.DOCS_VIEW_KEY=Z;exports.DOC_INTERCEPTOR_POINT=ae;exports.DeleteCommand=le;exports.DeleteLeftCommand=at;exports.DeleteRightCommand=st;exports.DocCustomRangeService=Ct;exports.EditorInsertTextCommandId=ot;exports.IMEInputCommand=Pe;exports.IMEInputManagerService=q;exports.InnerPasteCommand=Vt;exports.InsertCommand=W;exports.ListOperationCommand=_e;exports.MergeTwoParagraphCommand=ue;exports.MoveCursorOperation=ie;exports.MoveSelectionOperation=ce;exports.NORMAL_TEXT_SELECTION_PLUGIN_NAME=Pt;exports.OrderListCommand=lt;exports.ReplaceContentCommand=mt;exports.ResetInlineFormatTextBackgroundColorCommand=Ce;exports.RichTextEditingMutation=U;exports.SelectAllOperation=pt;exports.SetDocZoomRatioCommand=ft;exports.SetDocZoomRatioOperation=J;exports.SetInlineFormatBoldCommand=me;exports.SetInlineFormatCommand=Be;exports.SetInlineFormatFontFamilyCommand=Ie;exports.SetInlineFormatFontSizeCommand=he;exports.SetInlineFormatItalicCommand=ge;exports.SetInlineFormatStrikethroughCommand=fe;exports.SetInlineFormatSubscriptCommand=j;exports.SetInlineFormatSuperscriptCommand=pe;exports.SetInlineFormatTextBackgroundColorCommand=ve;exports.SetInlineFormatTextColorCommand=Te;exports.SetInlineFormatUnderlineCommand=Se;exports.SetTextSelectionsOperation=Ae;exports.UpdateCommand=Ue;exports.VIEWPORT_KEY=et;exports.addCustomDecorationBySelectionFactory=In;exports.addCustomDecorationFactory=_t;exports.addCustomRangeBySelectionFactory=fn;exports.addCustomRangeFactory=Sn;exports.deleteCustomDecorationFactory=Tn;exports.deleteCustomRangeFactory=hn;exports.getDeleteSelection=de;exports.getDocObject=tt;exports.getDocObjectById=Xt;exports.getInsertSelection=K;exports.getSelectionText=Et;exports.neoGetDocObject=Bt;exports.serializeTextRange=$;
1
+ "use strict";var Ut=Object.defineProperty;var bt=(s,o,e)=>o in s?Ut(s,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[o]=e;var A=(s,o,e)=>bt(s,typeof o!="symbol"?o+"":o,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("@univerjs/core"),N=require("@univerjs/engine-render"),$=require("rxjs"),me={id:"doc.operation.set-selections",type:n.CommandType.OPERATION,handler:(s,o)=>!0};var Pt=Object.defineProperty,wt=Object.getOwnPropertyDescriptor,Bt=(s,o,e,t)=>{for(var r=t>1?void 0:t?wt(o,e):o,a=s.length-1,i;a>=0;a--)(i=s[a])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&Pt(o,e,r),r},ze=(s,o)=>(e,t)=>o(e,t,s);function z(s){const{startOffset:o,endOffset:e,collapsed:t}=s,r={startOffset:o,endOffset:e,collapsed:t};return typeof s.isActive=="function"&&(r.isActive=s.isActive()),r}exports.TextSelectionManagerService=class extends n.RxDisposable{constructor(e,t){super();A(this,"_currentSelection",null);A(this,"_textSelectionInfo",new Map);A(this,"_textSelection$",new $.BehaviorSubject(null));A(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}getCurrentSelections(){var e;return(e=this._getTextRanges(this._currentSelection))==null?void 0:e.textRanges}getActiveTextRange(){const e=this._getTextRanges(this._currentSelection);if(e==null)return;const{textRanges:t}=e;return t.find(r=>r.isActive())}getActiveRange(){const e=this._getTextRanges(this._currentSelection);if(e==null)return;const{textRanges:t,segmentId:r,style:a,segmentPage:i}=e,d=t.find(S=>S.isActive());if(d==null)return null;const{startOffset:c,endOffset:u,collapsed:l,startNodePosition:g,endNodePosition:m,direction:f}=d;return c==null||u==null?null:{startOffset:c,endOffset:u,collapsed:l,startNodePosition:g,endNodePosition:m,direction:f,segmentId:r,segmentPage:i,style:a}}add(e,t=!0){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:e,segmentId:"",segmentPage:-1,isEditing:t,style:N.NORMAL_TEXT_SELECTION_PLUGIN_STYLE})}replaceTextRanges(e,t=!0,r){this._currentSelection!=null&&(this._textSelectionRenderManager.removeAllTextRanges(),this._textSelectionRenderManager.addTextRanges(e,t,r))}_syncSelectionFromRenderService(){this._textSelectionRenderManager.textSelectionInner$.pipe($.takeUntil(this.dispose$)).subscribe(e=>{e!=null&&this._replaceTextRangesWithNoRefresh(e)})}_replaceTextRangesWithNoRefresh(e){if(this._currentSelection==null)return;const t={...this._currentSelection,...e};this._replaceByParam(t),this._textSelection$.next(t);const{unitId:r,subUnitId:a,segmentId:i,style:d,textRanges:c,isEditing:u}=t;this._commandService.executeCommand(me.id,{unitId:r,subUnitId:a,segmentId:i,style:d,isEditing:u,ranges:c.map(z)})}_getTextRanges(e){var a;if(e==null)return;const{unitId:t,subUnitId:r=""}=e;return(a=this._textSelectionInfo.get(t))==null?void 0:a.get(r)}_refresh(e){const t=this._getTextRanges(e);this._textSelectionRenderManager.removeAllTextRanges(),t&&Array.isArray(t.textRanges)&&t.textRanges.length&&this._textSelectionRenderManager.addTextRanges(t.textRanges.map(z))}_replaceByParam(e){const{unitId:t,subUnitId:r,style:a,segmentId:i,textRanges:d,isEditing:c,segmentPage:u}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map),this._textSelectionInfo.get(t).set(r,{textRanges:d,style:a,segmentId:i,isEditing:c,segmentPage:u})}_addByParam(e){const{unitId:t,subUnitId:r,textRanges:a,style:i,segmentId:d,isEditing:c,segmentPage:u}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map);const l=this._textSelectionInfo.get(t);l.has(r)?l.get(r).textRanges.push(...a):l.set(r,{textRanges:a,style:i,segmentId:d,isEditing:c,segmentPage:u})}};exports.TextSelectionManagerService=Bt([ze(0,N.ITextSelectionRenderManager),ze(1,n.ICommandService)],exports.TextSelectionManagerService);var Xt=Object.defineProperty,Lt=Object.getOwnPropertyDescriptor,Ft=(s,o,e,t)=>{for(var r=t>1?void 0:t?Lt(o,e):o,a=s.length-1,i;a>=0;a--)(i=s[a])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&Xt(o,e,r),r},Ne=(s,o)=>(e,t)=>o(e,t,s);const He=300;exports.DocStateChangeManagerService=class extends n.RxDisposable{constructor(e,t,r){super();A(this,"_docStateChange$",new $.BehaviorSubject(null));A(this,"docStateChange$",this._docStateChange$.asObservable());A(this,"_historyStateCache",new Map);A(this,"_changeStateCache",new Map);A(this,"_historyTimer",null);A(this,"_changeStateCacheTimer",null);this._undoRedoService=e,this._commandService=t,this._univerInstanceService=r,this._initialize()}setChangeState(e){this._cacheChangeState(e,"history"),this._cacheChangeState(e,"collaboration")}_initialize(){this.disposeWithMe(this._commandService.beforeCommandExecuted(e=>{if(e.id===n.UndoCommandId||e.id===n.RedoCommandId){const t=this._univerInstanceService.getCurrentUniverDocInstance();if(t==null)return;const r=t.getUnitId();this._pushHistory(r),this._emitChangeState(r)}}))}_cacheChangeState(e,t="history"){const{trigger:r,unitId:a,noHistory:i,debounce:d=!1}=e;if(i||r==null||t==="history"&&(r===n.RedoCommandId||r===n.UndoCommandId))return;const c=t==="history"?this._historyStateCache:this._changeStateCache,u=t==="history"?this._pushHistory.bind(this):this._emitChangeState.bind(this);if(c.has(a)){const l=c.get(a);l==null||l.push(e)}else c.set(a,[e]);d?t==="history"?(this._historyTimer&&clearTimeout(this._historyTimer),this._historyTimer=setTimeout(()=>{u(a)},He)):(this._changeStateCacheTimer&&clearTimeout(this._changeStateCacheTimer),this._changeStateCacheTimer=setTimeout(()=>{u(a)},He)):u(a)}_pushHistory(e){const t=this._undoRedoService,r=this._historyStateCache.get(e);if(!Array.isArray(r)||r.length===0)return;const a=r.length,i=r[0].commandId,d=r[0],c=r[a-1],u={unitId:e,actions:r.reduce((g,m)=>n.JSONX.compose(g,m.redoState.actions),null),textRanges:c.redoState.textRanges},l={unitId:e,actions:r.reverse().reduce((g,m)=>n.JSONX.compose(g,m.undoState.actions),null),textRanges:d.undoState.textRanges};t.pushUndoRedo({unitID:e,undoMutations:[{id:i,params:l}],redoMutations:[{id:i,params:u}]}),r.length=0}_emitChangeState(e){const t=this._changeStateCache.get(e);if(!Array.isArray(t)||t.length===0)return;const r=t.length,{commandId:a,trigger:i,segmentId:d,noHistory:c,debounce:u}=t[0],l=t[0],g=t[r-1],m={unitId:e,actions:t.reduce((T,I)=>n.JSONX.compose(T,I.redoState.actions),null),textRanges:g.redoState.textRanges},f={unitId:e,actions:t.reverse().reduce((T,I)=>n.JSONX.compose(T,I.undoState.actions),null),textRanges:l.undoState.textRanges},S={commandId:a,unitId:e,trigger:i,redoState:m,undoState:f,segmentId:d,noHistory:c,debounce:u};t.length=0,this._docStateChange$.next(S)}};exports.DocStateChangeManagerService=Ft([Ne(0,n.Inject(n.IUndoRedoService)),Ne(1,n.ICommandService),Ne(2,n.IUniverInstanceService)],exports.DocStateChangeManagerService);class q{constructor(){A(this,"_previousActiveRange",null);A(this,"_undoMutationParamsCache",[]);A(this,"_redoMutationParamsCache",[])}clearUndoRedoMutationParamsCache(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[]}getUndoRedoMutationParamsCache(){return{undoCache:this._undoMutationParamsCache,redoCache:this._redoMutationParamsCache}}setUndoRedoMutationParamsCache({undoCache:o=[],redoCache:e=[]}){this._undoMutationParamsCache=o,this._redoMutationParamsCache=e}getActiveRange(){return this._previousActiveRange}setActiveRange(o){this._previousActiveRange=o}pushUndoRedoMutationParams(o,e){this._undoMutationParamsCache.push(o),this._redoMutationParamsCache.push(e)}fetchComposedUndoRedoMutationParams(){if(this._undoMutationParamsCache.length===0||this._previousActiveRange==null||this._redoMutationParamsCache.length===0)return null;const{unitId:o}=this._undoMutationParamsCache[0],e={unitId:o,actions:this._undoMutationParamsCache.reverse().reduce((r,a)=>n.JSONX.compose(r,a.actions),null),textRanges:[]};return{redoMutationParams:{unitId:o,actions:this._redoMutationParamsCache.reduce((r,a)=>n.JSONX.compose(r,a.actions),null),textRanges:[]},undoMutationParams:e,previousActiveRange:this._previousActiveRange}}dispose(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[],this._previousActiveRange=null}}var kt=Object.defineProperty,Gt=Object.getOwnPropertyDescriptor,jt=(s,o,e,t)=>{for(var r=t>1?void 0:t?Gt(o,e):o,a=s.length-1,i;a>=0;a--)(i=s[a])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&kt(o,e,r),r},Ve=(s,o)=>(e,t)=>o(e,t,s);exports.DocSkeletonManagerService=class extends n.RxDisposable{constructor(e,t,r){super();A(this,"_skeleton");A(this,"_docViewModel");A(this,"_currentSkeleton$",new $.BehaviorSubject(null));A(this,"currentSkeleton$",this._currentSkeleton$.asObservable());A(this,"_currentSkeletonBefore$",new $.BehaviorSubject(null));A(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());A(this,"_currentViewModel$",new $.BehaviorSubject(null));A(this,"currentViewModel$",this._currentViewModel$.asObservable());this._context=e,this._localeService=t,this._univerInstanceService=r,this._init(),this._univerInstanceService.getCurrentTypeOfUnit$(n.UniverInstanceType.UNIVER_DOC).pipe($.takeUntil(this.dispose$)).subscribe(a=>{a&&a.getUnitId()===this._context.unitId&&this._update(a)})}dispose(){super.dispose(),this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete()}getSkeleton(){return this._skeleton}getViewModel(){return this._docViewModel}_init(){const e=this._context.unit;this._update(e)}_update(e){const t=this._context.unitId;if(e.getBody()==null)return;this._docViewModel&&t===n.DOCS_NORMAL_EDITOR_UNIT_ID_KEY?(this._docViewModel.reset(e),this._context.unit=e):this._docViewModel||(this._docViewModel=this._buildDocViewModel(e)),this._skeleton||(this._skeleton=this._buildSkeleton(this._docViewModel));const r=this._skeleton;r.calculate(),this._currentSkeletonBefore$.next(r),this._currentSkeleton$.next(r),this._currentViewModel$.next(this._docViewModel)}_buildSkeleton(e){return N.DocumentSkeleton.create(e,this._localeService)}_buildDocViewModel(e){return new N.DocumentViewModel(e)}};exports.DocSkeletonManagerService=jt([Ve(1,n.Inject(n.LocaleService)),Ve(2,n.IUniverInstanceService)],exports.DocSkeletonManagerService);const Je="doc.mutation.rich-text-editing",U={id:Je,type:n.CommandType.MUTATION,handler:(s,o)=>{var M,O;const{unitId:e,segmentId:t="",actions:r,textRanges:a,prevTextRanges:i,trigger:d,noHistory:c,isCompositionEnd:u,noNeedSetTextRange:l,debounce:g}=o,m=s.get(n.IUniverInstanceService),f=s.get(N.IRenderManagerService),S=m.getUniverDocInstance(e),T=(M=f.getRenderById(e))==null?void 0:M.with(exports.DocSkeletonManagerService).getViewModel();if(S==null||T==null)throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${e}`);const I=s.get(exports.TextSelectionManagerService),h=((O=I.getCurrentSelections())!=null?O:[]).map(z),v=s.get(exports.DocStateChangeManagerService),_=s.get(q),D=!!S.getSnapshot().disabled;if(n.JSONX.isNoop(r)||r&&r.length===0||D)return{unitId:e,actions:[],textRanges:h};const C=n.JSONX.invertWithDoc(r,S.getSnapshot());S.apply(r),T.reset(S),!l&&a&&d!=null&&queueMicrotask(()=>{I.replaceTextRanges(a,!0,o.options)});const y={commandId:Je,unitId:e,segmentId:t,trigger:d,noHistory:c,debounce:g,redoState:{actions:r,textRanges:a},undoState:{actions:C,textRanges:i!=null?i:h}};if(u){const x=_.fetchComposedUndoRedoMutationParams();if(x==null)throw new Error("historyParams is null in RichTextEditingMutation");const{undoMutationParams:R,redoMutationParams:E,previousActiveRange:b}=x;y.redoState.actions=E.actions,y.undoState.actions=R.actions,y.undoState.textRanges=[b]}return v.setChangeState(y),{unitId:e,actions:C,textRanges:h}}};function be(s){return s===n.DataStreamTreeTokenType.CUSTOM_RANGE_END||s===n.DataStreamTreeTokenType.CUSTOM_RANGE_START}function Pe(s,o,e,t){return s<=e&&o>=e||s>=e&&s<=t}function we(s,o,e,t){const r=t.slice(e.startIndex+1,e.endIndex),a=Math.max(s-(e.startIndex+1),0),i=s+o-1-(e.startIndex+1);if(i<0)return!1;if(a===0&&i>=r.length)return!0;const d=r.slice(0,a)+r.slice(a+o);for(let c=0,u=d.length;c<u;c++){const l=d[c];if(!be(l))return!1}return!0}var P=(s=>(s[s.LEFT=0]="LEFT",s[s.RIGHT=1]="RIGHT",s))(P||{});function Q(s){const{startOffset:o,endOffset:e,collapsed:t}=s,r=Math.min(o,e),a=Math.max(o,e);return{startOffset:r,endOffset:a,collapsed:t}}function Be(s,o){let{startOffset:e,endOffset:t}=Q(s);for(;o.dataStream[e-1]===n.DataStreamTreeTokenType.CUSTOM_RANGE_START;)e-=1;for(;o.dataStream[t]===n.DataStreamTreeTokenType.CUSTOM_RANGE_END;)t+=1;return{startOffset:e,endOffset:t}}function ee(s,o,e=P.LEFT){var d;let{startOffset:t,endOffset:r,collapsed:a}=Q(s);if(a)if(e===P.LEFT)for(;o.dataStream[t-1]===n.DataStreamTreeTokenType.CUSTOM_RANGE_END;)r-=1,t-=1;else for(;o.dataStream[t]===n.DataStreamTreeTokenType.CUSTOM_RANGE_START;)r+=1,t+=1;else{const c=Be(s,o);t=c.startOffset,r=c.endOffset}a=t===r;const i=(d=o.customRanges)==null?void 0:d.filter(c=>!c.wholeEntity||t<=c.startIndex&&r>c.endIndex?!1:Xe(t,a?r:r-1,c.startIndex,c.endIndex));return i!=null&&i.length&&i.forEach(c=>{t=Math.min(c.startIndex,t),r=Math.max(c.endIndex+1,r)}),{...s,startOffset:t,endOffset:r,collapsed:t===r}}function ge(s,o){let{startOffset:e,endOffset:t,collapsed:r}=Q(s);if(r){for(;o.dataStream[t]===n.DataStreamTreeTokenType.CUSTOM_RANGE_END;)t+=1,e+=1;for(;o.dataStream[t-1]===n.DataStreamTreeTokenType.CUSTOM_RANGE_START;)t-=1,e-=1;return{startOffset:e,endOffset:t,collapsed:r}}else return{...Be(s,o),collapsed:!1}}function $t(s,o){if(s.startOffset===s.endOffset)return null;const e=o.customRanges;if(!e)return Q(s);let{startOffset:t,endOffset:r}=Be(s,o);for(;be(o.dataStream[t]);){if(o.dataStream[t]===n.DataStreamTreeTokenType.CUSTOM_RANGE_START){const a=e.find(i=>i.startIndex===t);if(!a)throw new Error("No custom-range matched");if(a.endIndex===r-1)return{startOffset:t,endOffset:r,collapsed:!1};if(a.endIndex<r-1)break}t+=1}for(;be(o.dataStream[r-1]);){if(o.dataStream[t]===n.DataStreamTreeTokenType.CUSTOM_RANGE_END){const a=e.find(i=>i.endIndex===r-1);if(!a)throw new Error("No custom-range matched");if(a.startIndex===t)return{startOffset:t,endOffset:r,collapsed:!1};if(a.startIndex>t)break}r-=1}return r<=t?null:{startOffset:t,endOffset:r,collapsed:!1}}const zt=[n.DataStreamTreeTokenType.PARAGRAPH,n.DataStreamTreeTokenType.SECTION_BREAK,n.DataStreamTreeTokenType.TABLE_START,n.DataStreamTreeTokenType.TABLE_ROW_START,n.DataStreamTreeTokenType.TABLE_CELL_START,n.DataStreamTreeTokenType.TABLE_CELL_END,n.DataStreamTreeTokenType.TABLE_ROW_END,n.DataStreamTreeTokenType.TABLE_END,n.DataStreamTreeTokenType.CUSTOM_RANGE_START,n.DataStreamTreeTokenType.CUSTOM_RANGE_END,n.DataStreamTreeTokenType.COLUMN_BREAK,n.DataStreamTreeTokenType.PAGE_BREAK,n.DataStreamTreeTokenType.DOCS_END,n.DataStreamTreeTokenType.TAB,n.DataStreamTreeTokenType.CUSTOM_BLOCK];function Ht(s,o,e){const t=s.slice(o,e);return zt.reduce((r,a)=>r.replaceAll(a,""),t)}function Xe(s,o,e,t){return Math.max(s,e)<=Math.min(o,t)}function Le(s,o,e="",t=0){var p;const{startOffset:r,endOffset:a}=ee(s,o),i=[],{paragraphs:d=[],dataStream:c}=o,u=r-t,l=a-t,g=d==null?void 0:d.find(h=>h.startIndex-t>=u&&h.startIndex-t<l),m=(p=o.customRanges)==null?void 0:p.filter(h=>Pe(h.startIndex,h.endIndex,r,a)),f=new Set(m==null?void 0:m.filter(h=>we(r,a-r,h,c))),S=new Set;if(m==null||m.forEach(h=>{f.has(h)||(h.startIndex-t>=u&&h.startIndex-t<=l&&h.endIndex-t>l&&S.add(h.startIndex),h.endIndex-t>=u&&h.endIndex-t<=l&&h.startIndex<u&&S.add(h.endIndex))}),u>0&&i.push({t:n.TextXActionType.RETAIN,len:u,segmentId:e}),g&&g.startIndex-t>u){const h=g.startIndex-t;S.add(h)}const T=[...S].sort((h,v)=>h-v);let I=u;return T.forEach(h=>{const v=h-I;v>0&&i.push({t:n.TextXActionType.DELETE,len:v,line:0,segmentId:e}),i.push({t:n.TextXActionType.RETAIN,len:1,segmentId:e}),I=h+1}),I<l&&i.push({t:n.TextXActionType.DELETE,len:l-I,line:0,segmentId:e}),i}function Vt(s,o){var f,S;const{unitId:e,body:t}=o,a=s.get(n.IUniverInstanceService).getUnit(e),i=s.get(exports.TextSelectionManagerService);if(!a)return!1;const d=a.getBody(),c=(f=o.selection)!=null?f:i.getActiveRange();if(!c||!d)return!1;const u=(S=o.textRanges)!=null?S:[{startOffset:c.startOffset+t.dataStream.length,endOffset:c.startOffset+t.dataStream.length,collapsed:!0}],l={id:U.id,params:{unitId:e,actions:[],textRanges:u,debounce:!0}},g=new n.TextX,m=n.JSONX.getInstance();return g.push(...Le(c,d)),g.push({t:n.TextXActionType.INSERT,body:t,len:t.dataStream.length,line:0}),l.params.actions=m.editOp(g.serialize()),l}var te=(s=>(s.MAIN="__Document_Render_Main__",s.BACKGROUND="__Document_Render_Background__",s))(te||{}),at=(s=>(s.VIEW_MAIN="viewMain",s.VIEW_TOP="viewTop",s.VIEW_LEFT="viewLeft",s.VIEW_LEFT_TOP="viewLeftTop",s))(at||{});const Jt=0,Wt=2,Yt=4,Kt=10,Zt="normalTextSelectionPluginName";function qt(s){const{mainComponent:o,scene:e,engine:t,components:r}=s,a=o,i=r.get(te.BACKGROUND);return{document:a,docBackground:i,scene:e,engine:t}}function it(s,o){const e=s.getCurrentUnitForType(n.UniverInstanceType.UNIVER_DOC);if(!e)return null;const t=e.getUnitId(),r=o.getRenderById(t);if(r==null)return;const{mainComponent:a,scene:i,engine:d,components:c}=r,u=a,l=c.get(te.BACKGROUND);return{document:u,docBackground:l,scene:i,engine:d}}function Qt(s,o){const e=o.getRenderById(s);if(e==null)return;const{mainComponent:t,scene:r,engine:a,components:i}=e,d=t,c=i.get(te.BACKGROUND);return{document:d,docBackground:c,scene:r,engine:a}}class fe{constructor(){A(this,"_customRangeHooks",[])}addClipboardHook(o){return this._customRangeHooks.push(o),n.toDisposable(()=>{const e=this._customRangeHooks.indexOf(o);e>-1&&this._customRangeHooks.splice(e,1)})}copyCustomRange(o,e){let t={...e};return this._customRangeHooks.forEach(r=>{r.onCopyCustomRange&&(t=r.onCopyCustomRange(o,t))}),t}}function Se(s,o="",e,t){var T;const{startOffset:r,endOffset:a}=s,i=[],d=r-e,c=a-e,u=t.dataStream,l=(T=t.customRanges)==null?void 0:T.filter(I=>Pe(I.startIndex,I.endIndex,r,a)),g=new Set(l==null?void 0:l.filter(I=>we(r,a-r,I,u))),m=new Set;l==null||l.forEach(I=>{g.has(I)||(I.startIndex-e>=d&&I.startIndex-e<=c&&I.endIndex-e>c&&m.add(I.startIndex),I.endIndex-e>=d&&I.endIndex-e<=c&&I.startIndex<d&&m.add(I.endIndex))}),d>0&&i.push({t:n.TextXActionType.RETAIN,len:d,segmentId:o});const f=[...m].sort((I,p)=>I-p);let S=d;return f.forEach(I=>{const p=I-S;p>0&&i.push({t:n.TextXActionType.DELETE,len:p,line:0,segmentId:o}),i.push({t:n.TextXActionType.RETAIN,len:1,segmentId:o}),S=I+1}),S<c&&(i.push({t:n.TextXActionType.DELETE,len:c-S,line:0,segmentId:o}),S=c+1),{dos:i,cursor:S,retain:m.size}}function ct(s,o){var t;return(t=s.get(N.IRenderManagerService).getRenderById(o))==null?void 0:t.with(exports.DocSkeletonManagerService)}function w(s,o=""){if(!o)return["body"];const{headers:e,footers:t}=s.getSnapshot();if(e==null&&t==null)throw new Error("Document data model must have headers or footers when update by segment id");if((e==null?void 0:e[o])!=null)return["headers",o,"body"];if((t==null?void 0:t[o])!=null)return["footers",o,"body"];throw new Error("Segment id not found in headers or footers")}const dt="doc.command.insert-text",Z={id:dt,type:n.CommandType.COMMAND,handler:async(s,o)=>{var M;const e=s.get(n.ICommandService),{range:t,segmentId:r,body:a,unitId:i,textRanges:d,cursorOffset:c}=o,u=s.get(exports.TextSelectionManagerService),g=s.get(n.IUniverInstanceService).getUnit(i,n.UniverInstanceType.UNIVER_DOC);if(g==null)return!1;const m=u.getActiveRange(),f=g.getSelfOrHeaderFooterModel((M=m==null?void 0:m.segmentId)!=null?M:"").getBody();if(!f)return!1;const S=ge(t,f),{startOffset:T,collapsed:I}=S,p=c!=null?c:a.dataStream.length,h=[{startOffset:T+p,endOffset:T+p,style:m==null?void 0:m.style,collapsed:I}],v={id:U.id,params:{unitId:i,actions:[],textRanges:d!=null?d:h,debounce:!0}},_=new n.TextX,D=n.JSONX.getInstance();if(I)T>0&&_.push({t:n.TextXActionType.RETAIN,len:T,segmentId:r});else{const{dos:O,retain:x}=Se(S,r,0,f);_.push(...O),d||(v.params.textRanges=[{startOffset:T+p+x,endOffset:T+p+x,collapsed:I}])}_.push({t:n.TextXActionType.INSERT,body:a,len:a.dataStream.length,line:0,segmentId:r});const C=w(g,r);return v.params.actions=D.editOp(_.serialize(),C),!!e.syncExecuteCommand(v.id,v.params)}},H={id:"doc.command.delete-text",type:n.CommandType.COMMAND,handler:async(s,o)=>{var O;const e=s.get(n.ICommandService),t=s.get(n.IUniverInstanceService),{range:r,segmentId:a,unitId:i,direction:d,len:c=1}=o,u=t.getUnit(i,n.UniverInstanceType.UNIVER_DOC),l=u==null?void 0:u.getSelfOrHeaderFooterModel(a).getBody();if(u==null||l==null)return!1;const{startOffset:g}=r,m=l.dataStream,f=d===P.LEFT?g-c:g,S=f+c-1,T=(O=l.customRanges)==null?void 0:O.filter(x=>Pe(x.startIndex,x.endIndex,f,S)),I=T==null?void 0:T.filter(x=>we(f,c,x,m)),p=[];for(let x=0;x<c;x++)p.push(f+x);I==null||I.forEach(x=>{p.push(x.startIndex,x.endIndex)}),p.sort((x,R)=>x-R);const h=p[0],v={id:U.id,params:{unitId:i,actions:[],textRanges:[{startOffset:h,endOffset:h,collapsed:!0}],debounce:!0}},_=new n.TextX,D=n.JSONX.getInstance();let C=0;for(let x=0;x<p.length;x++){const R=p[x];R-C>0&&_.push({t:n.TextXActionType.RETAIN,len:R-C,segmentId:a}),_.push({t:n.TextXActionType.DELETE,len:1,segmentId:a,line:0}),C=R+1}const y=w(u,a);return v.params.actions=D.editOp(_.serialize(),y),!!e.syncExecuteCommand(v.id,v.params)}},Fe={id:"doc.command.update-text",type:n.CommandType.COMMAND,handler:async(s,o)=>{const{range:e,segmentId:t,updateBody:r,coverType:a,unitId:i,textRanges:d}=o,c=s.get(n.ICommandService),l=s.get(n.IUniverInstanceService).getCurrentUniverDocInstance();if(l==null)return!1;const g={id:U.id,params:{unitId:i,actions:[],textRanges:d}},m=new n.TextX,f=n.JSONX.getInstance(),{startOffset:S,endOffset:T}=e;m.push({t:n.TextXActionType.RETAIN,len:S,segmentId:t}),m.push({t:n.TextXActionType.RETAIN,body:r,len:T-S,segmentId:t,coverType:a});const I=w(l,t);return g.params.actions=f.editOp(m.serialize(),I),!!c.syncExecuteCommand(g.id,g.params)}};function We(s,o){const e=[];for(let t=0,r=s.length;t<r;t++)s[t]===n.DataStreamTreeTokenType.PARAGRAPH&&e.push({startIndex:t});if(o)for(const t of e)o.bullet&&(t.bullet=n.Tools.deepClone(o.bullet)),o.paragraphStyle&&(t.paragraphStyle=n.Tools.deepClone(o.paragraphStyle));return e}const lt={id:"doc.command.break-line",type:n.CommandType.COMMAND,handler:async s=>{var S,T;const o=s.get(exports.TextSelectionManagerService),e=s.get(n.IUniverInstanceService),t=s.get(n.ICommandService),r=s.get(fe),a=o.getActiveRange();if(a==null)return!1;const{segmentId:i}=a,d=e.getCurrentUniverDocInstance(),c=d==null?void 0:d.getSelfOrHeaderFooterModel(i).getBody();if(!d||!c)return!1;const u=d.getUnitId(),{startOffset:l,endOffset:g}=ge(a,c),f=((S=c.paragraphs)!=null?S:[]).find(I=>I.startIndex>=l);if(f&&f.startIndex>g){const I=n.normalizeBody(n.getBodySlice(c,g,f.startIndex));I.customRanges=(T=I.customRanges)==null?void 0:T.map(v=>r.copyCustomRange(u,v));const p={startOffset:l,endOffset:f.startIndex,collapsed:!1};return n.updateAttributeByInsert(I,{dataStream:n.DataStreamTreeTokenType.PARAGRAPH,paragraphs:We(n.DataStreamTreeTokenType.PARAGRAPH,f)},1,0),await t.executeCommand(Z.id,{unitId:u,body:I,range:p,segmentId:i,cursorOffset:1})}else return await t.executeCommand(Z.id,{unitId:u,body:{dataStream:n.DataStreamTreeTokenType.PARAGRAPH,paragraphs:We(n.DataStreamTreeTokenType.PARAGRAPH,f)},range:a,segmentId:i})}},en={id:"doc.command.inner-paste",type:n.CommandType.COMMAND,handler:async(s,o)=>{const{segmentId:e,textRanges:t}=o,r=o.body,a=s.get(n.ICommandService),i=s.get(exports.TextSelectionManagerService),d=s.get(n.IUniverInstanceService),c=i.getCurrentSelections();if(!Array.isArray(c)||c.length===0)return!1;const u=d.getCurrentUniverDocInstance(),l=u==null?void 0:u.getSelfOrHeaderFooterModel(e).getBody();if(u==null||l==null)return!1;const g=u.getUnitId(),m={id:U.id,params:{unitId:g,actions:[],textRanges:t}},f=new n.MemoryCursor;f.reset();const S=new n.TextX,T=n.JSONX.getInstance();for(const h of c){const{startOffset:v,endOffset:_,collapsed:D}=h,C=v-f.cursor;if(D)S.push({t:n.TextXActionType.RETAIN,len:C,segmentId:e});else{const{dos:y}=Se(h,e,f.cursor,l);S.push(...y)}S.push({t:n.TextXActionType.INSERT,body:r,len:r.dataStream.length,line:0,segmentId:e}),f.reset(),f.moveCursor(_)}const I=w(u,e);return m.params.actions=T.editOp(S.serialize(),I),!!a.syncExecuteCommand(m.id,m.params)}},ke={id:"doc.command.inner-cut",type:n.CommandType.COMMAND,handler:async(s,o)=>{var C,y,M,O;const{segmentId:e,textRanges:t}=o,r=s.get(n.ICommandService),a=s.get(exports.TextSelectionManagerService),i=s.get(n.IUniverInstanceService),d=(C=o.selections)!=null?C:a.getCurrentSelections();if(!Array.isArray(d)||d.length===0)return!1;const c=(y=i.getCurrentUniverDocInstance())==null?void 0:y.getUnitId();if(!c)return!1;const u=i.getUniverDocInstance(c),l=n.getDocsUpdateBody(u.getSnapshot(),e);if(u==null||l==null)return!1;const g={id:U.id,params:{unitId:c,actions:[],textRanges:t}},m=new n.MemoryCursor;m.reset();const f=new n.TextX,S=n.JSONX.getInstance(),T=[];for(const x of d){const{startOffset:R,endOffset:E,collapsed:b}=x;if(R==null||E==null)continue;const F=R-m.cursor;b?f.push({t:n.TextXActionType.RETAIN,len:F,segmentId:e}):f.push(...Le(x,l,e,m.cursor)),m.reset(),m.moveCursor(E)}const I=w(u,e);T.push(S.editOp(f.serialize(),I));const p=tn(l,d),h=(M=u.getDrawings())!=null?M:{},v=(O=u.getDrawingsOrder())!=null?O:[],_=p.sort((x,R)=>v.indexOf(x)>v.indexOf(R)?-1:v.indexOf(x)<v.indexOf(R)?1:0);if(_.length>0)for(const x of _){const R=h[x],E=v.indexOf(x);if(R==null||E<0)continue;const b=S.removeOp(["drawings",x],R),F=S.removeOp(["drawingsOrder",E],x);T.push(b),T.push(F)}return g.params.actions=T.reduce((x,R)=>n.JSONX.compose(x,R),null),!!r.syncExecuteCommand(g.id,g.params)}};function tn(s,o){const e=[],{customBlocks:t=[]}=s;for(const r of o){const{startOffset:a,endOffset:i}=r;if(!(a==null||i==null))for(const d of t){const{startIndex:c}=d;c>=a&&c<i&&e.push(d.blockId)}}return e}const pe={id:"doc.command.delete-custom-block",type:n.CommandType.COMMAND,handler:async(s,o)=>{var x;const e=s.get(exports.TextSelectionManagerService),t=s.get(n.IUniverInstanceService),r=s.get(n.ICommandService),a=e.getActiveRange(),i=t.getCurrentUniverDocInstance();if(a==null||i==null)return!1;const{direction:d,range:c,unitId:u,drawingId:l}=o,{startOffset:g,segmentId:m,style:f}=a,S=d===P.LEFT?g-1:g,T=[{startOffset:S,endOffset:S,style:f}],I={id:U.id,params:{unitId:u,actions:[],textRanges:T,prevTextRanges:[c]}},p=new n.TextX,h=n.JSONX.getInstance(),v=[];g>0&&p.push({t:n.TextXActionType.RETAIN,len:d===P.LEFT?g-1:g,segmentId:m}),p.push({t:n.TextXActionType.DELETE,len:1,line:0,segmentId:m}),v.push(h.editOp(p.serialize()));const _=((x=i.getDrawings())!=null?x:{})[l],C=i.getDrawingsOrder().indexOf(l),y=h.removeOp(["drawings",l],_),M=h.removeOp(["drawingsOrder",C],l);return v.push(y),v.push(M),I.params.actions=v.reduce((R,E)=>n.JSONX.compose(R,E),null),!!r.syncExecuteCommand(I.id,I.params)}},Ie={id:"doc.command.merge-two-paragraph",type:n.CommandType.COMMAND,handler:async(s,o)=>{var R,E;const e=s.get(exports.TextSelectionManagerService),t=s.get(n.IUniverInstanceService),r=s.get(n.ICommandService),{direction:a,range:i}=o,d=e.getActiveRange(),c=e.getCurrentSelections();if(d==null||c==null)return!1;const{segmentId:u,style:l}=d,g=t.getCurrentUniverDocInstance(),m=g==null?void 0:g.getSelfOrHeaderFooterModel(u).getBody();if(!g||!m)return!1;const f=ee(d,m),S=g.getUnitId(),{startOffset:T,collapsed:I}=f;if(!I)return!1;const p=a===P.LEFT?T:T+1,h=(E=(R=m.paragraphs)==null?void 0:R.find(b=>b.startIndex>=p))==null?void 0:E.startIndex,v=nn(s,S,m,p,h),_=a===P.LEFT?T-1:T,D=[{startOffset:_,endOffset:_,style:l}],C={id:U.id,params:{unitId:S,actions:[],textRanges:D,prevTextRanges:[i]}},y=new n.TextX,M=n.JSONX.getInstance();y.push({t:n.TextXActionType.RETAIN,len:a===P.LEFT?T-1:T,segmentId:u}),v.dataStream.length&&y.push({t:n.TextXActionType.INSERT,body:v,len:v.dataStream.length,line:0,segmentId:u}),y.push({t:n.TextXActionType.RETAIN,len:1,segmentId:u}),y.push({t:n.TextXActionType.DELETE,len:h+1-p,line:0,segmentId:u});const O=w(g,u);return C.params.actions=M.editOp(y.serialize(),O),!!r.syncExecuteCommand(C.id,C.params)}},ut={id:"doc.command.delete-left",type:n.CommandType.COMMAND,handler:async s=>{var M,O;const o=s.get(exports.TextSelectionManagerService),e=s.get(n.IUniverInstanceService),t=s.get(n.ICommandService);let r=!0;const a=e.getCurrentUniverDocInstance();if(!a)return!1;const i=a.getUnitId(),d=ct(s,i),c=o.getActiveRange(),u=o.getCurrentSelections(),l=d==null?void 0:d.getSkeleton();if(c==null||l==null||u==null)return!1;const{segmentId:g,style:m,segmentPage:f}=c,S=a.getSelfOrHeaderFooterModel(g).getBody();if(S==null)return!1;const T=ee(c,S),{startOffset:I,collapsed:p}=T,h=l.findNodeByCharIndex(I,g,f),v=N.hasListGlyph(h),_=N.isIndentByGlyph(h,S);let D=I;const C=l.findNodeByCharIndex(I-1,g,f);if(N.isFirstGlyph(h)&&C!==h&&(v===!0||_===!0)&&p){const x=N.getParagraphByGlyph(h,S);if(x==null)return!1;const R=x==null?void 0:x.startIndex,E={startIndex:0},b=x.paragraphStyle;if(v===!0){const L=x.paragraphStyle;if(L){E.paragraphStyle=L;const{hanging:W}=L;W&&(E.paragraphStyle.indentStart=W,E.paragraphStyle.hanging=void 0)}}else if(_===!0){const L=x.bullet;L&&(E.bullet=L),b!=null&&(E.paragraphStyle={...b},delete E.paragraphStyle.hanging,delete E.paragraphStyle.indentStart)}const F=[{startOffset:D,endOffset:D,style:m}];r=await t.executeCommand(Fe.id,{unitId:a.getUnitId(),updateBody:{dataStream:"",paragraphs:[{...E}]},range:{startOffset:R,endOffset:R+1},textRanges:F,coverType:n.UpdateDocsAttributeType.REPLACE,segmentId:g})}else if(p===!0){if(C==null)return!0;if(C.content==="\r")r=await t.executeCommand(Ie.id,{direction:P.LEFT,range:T});else if(C.streamType==="\b"){const x=(O=a.getSnapshot().drawings)==null?void 0:O[(M=C.drawingId)!=null?M:""];if(x==null)return!0;if(x.layoutType===n.PositionedObjectLayoutType.INLINE){const E=a.getUnitId();r=await t.executeCommand(pe.id,{direction:P.LEFT,range:c,unitId:E,drawingId:C.drawingId})}else{const E=l.findNodeByCharIndex(I-2);if(E==null)return!0;D-=C.count,D-=E.count;const b=[{startOffset:D,endOffset:D,style:m}];r=await t.executeCommand(H.id,{unitId:a.getUnitId(),range:{...c,startOffset:c.startOffset-1,endOffset:c.endOffset-1},segmentId:g,direction:P.LEFT,len:E.count,textRanges:b})}}else D-=C.count,r=await t.executeCommand(H.id,{unitId:a.getUnitId(),range:T,segmentId:g,direction:P.LEFT,len:C.count})}else{const x=gt(T,[T]);r=await t.executeCommand(ke.id,{segmentId:g,textRanges:x,selections:[T]})}return r}},mt={id:"doc.command.delete-right",type:n.CommandType.COMMAND,handler:async s=>{var h,v;const o=s.get(exports.TextSelectionManagerService),t=s.get(n.IUniverInstanceService).getCurrentUniverDocInstance();if(!t)return!1;const r=ct(s,t.getUnitId()),a=s.get(n.ICommandService),i=o.getActiveRange(),d=o.getCurrentSelections(),c=r==null?void 0:r.getSkeleton();if(i==null||c==null||d==null)return!1;const{segmentId:u,style:l,segmentPage:g}=i,m=t==null?void 0:t.getSelfOrHeaderFooterModel(u).getBody();if(!t||!m)return!1;const f=ee(i,m,P.RIGHT),{startOffset:S,endOffset:T,collapsed:I}=f;if(S===m.dataStream.length-2&&I)return!0;let p=!1;if(I===!0){const _=c.findNodeByCharIndex(S,u,g),D=c.findNodeByCharIndex(S+1);if(_.content==="\r")p=await a.executeCommand(Ie.id,{direction:P.RIGHT,range:i});else if(_.streamType==="\b"){const C=(v=t.getSnapshot().drawings)==null?void 0:v[(h=_.drawingId)!=null?h:""];if(C==null)return!0;if(C.layoutType===n.PositionedObjectLayoutType.INLINE){const M=t.getUnitId();p=await a.executeCommand(pe.id,{direction:P.RIGHT,range:i,unitId:M,drawingId:_.drawingId})}else{if(D==null)return!0;const M=[{startOffset:S+1,endOffset:S+1,style:l}];p=await a.executeCommand(H.id,{unitId:t.getUnitId(),range:{...i,startOffset:S+1,endOffset:T+1},segmentId:u,direction:P.RIGHT,textRanges:M,len:D.count})}}else{const C=[{startOffset:S,endOffset:S,style:l}];p=await a.executeCommand(H.id,{unitId:t.getUnitId(),range:f,segmentId:u,direction:P.RIGHT,textRanges:C,len:_.count})}}else{const _=gt(f,[f]);p=await a.executeCommand(ke.id,{segmentId:u,textRanges:_,selections:[f]})}return p}};function nn(s,o,e,t,r){const{textRuns:a=[],customBlocks:i=[]}=e,d=e.dataStream.substring(t,r),c=s.get(fe),u={dataStream:d,customRanges:n.getCustomRangeSlice(e,t,r).customRanges.map(m=>c.copyCustomRange(o,m)),customDecorations:n.getCustomDecorationSlice(e,t,r)},l=[];for(const m of a){const{st:f,ed:S}=m;S<=t||f>=r||(f<t?l.push({...m,st:0,ed:S-t}):S>r?l.push({...m,st:f-t,ed:r-t}):l.push({...m,st:f-t,ed:S-t}))}l.length>0&&(u.textRuns=l);const g=[];for(const m of i){const{startIndex:f}=m;f>=t&&f<=r&&g.push({...m,startIndex:f-t})}return g.length>0&&(u.customBlocks=g),u}function gt(s,o){let e=s.endOffset;for(const r of o){const{startOffset:a,endOffset:i}=r;a==null||i==null||i<=s.endOffset&&(e-=i-a)}return[{startOffset:e,endOffset:e,style:s.style}]}const Ge={id:"doc.command.ime-input",type:n.CommandType.COMMAND,handler:async(s,o)=>{const{unitId:e,newText:t,oldTextLen:r,isCompositionEnd:a,isCompositionStart:i}=o,d=s.get(n.ICommandService),c=s.get(q),l=s.get(n.IUniverInstanceService).getCurrentUniverDocInstance();if(l==null)return!1;const g=c.getActiveRange();if(!g)return!1;const{startOffset:m,style:f,segmentId:S}=g,T=l.getSelfOrHeaderFooterModel(S).getBody();if(T==null)return!1;const I=ge(g,T);Object.assign(g,I);const p=t.length,h=[{startOffset:m+p,endOffset:m+p,collapsed:!0,style:f}],v={id:U.id,params:{unitId:e,actions:[],textRanges:h}},_=new n.TextX,D=n.JSONX.getInstance();if(!g.collapsed&&i){const{dos:M,retain:O,cursor:x}=Se(g,S,0,T);_.push(...M),v.params.textRanges=[{startOffset:m+p+O,endOffset:m+p+O,collapsed:!0}]}else _.push({t:n.TextXActionType.RETAIN,len:m,segmentId:S});r>0&&_.push({t:n.TextXActionType.DELETE,len:r,line:0,segmentId:S}),_.push({t:n.TextXActionType.INSERT,body:{dataStream:t},len:t.length,line:0,segmentId:S});const C=w(l,S);v.params.actions=D.editOp(_.serialize(),C),v.params.noHistory=!a,v.params.isCompositionEnd=a;const y=d.syncExecuteCommand(v.id,v.params);return c.pushUndoRedoMutationParams(y,v.params),!!y}};function X(s,o,e,t){var a;const{segmentId:r}=(a=e.getActiveRange())!=null?a:{};return r==null?!1:t.executeCommand(je.id,{segmentId:r,preCommandId:s,...o!=null?o:{}})}const Ye="doc.command.set-inline-format-bold",he={id:Ye,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return X(Ye,o,t,e)}},Ke="doc.command.set-inline-format-italic",Te={id:Ke,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return X(Ke,o,t,e)}},Ze="doc.command.set-inline-format-underline",ve={id:Ze,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return X(Ze,o,t,e)}},qe="doc.command.set-inline-format-strikethrough",Ce={id:qe,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return X(qe,o,t,e)}},Qe="doc.command.set-inline-format-subscript",V={id:Qe,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return X(Qe,o,t,e)}},et="doc.command.set-inline-format-superscript",xe={id:et,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return X(et,o,t,e)}},tt="doc.command.set-inline-format-fontsize",ye={id:tt,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return X(tt,o,t,e)}},nt="doc.command.set-inline-format-font-family",_e={id:nt,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return X(nt,o,t,e)}},ot="doc.command.set-inline-format-text-color",Oe={id:ot,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return X(ot,o,t,e)}},rt="doc.command.set-inline-format-text-background-color",Re={id:rt,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return X(rt,o,t,e)}},st="doc.command.reset-inline-format-text-background-color",Me={id:st,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return X(st,o,t,e)}},ft={[he.id]:"bl",[Te.id]:"it",[ve.id]:"ul",[Ce.id]:"st",[ye.id]:"fs",[_e.id]:"ff",[Oe.id]:"cl",[Re.id]:"bg",[Me.id]:"bg",[V.id]:"va",[xe.id]:"va"},je={id:"doc.command.set-inline-format",type:n.CommandType.COMMAND,handler:async(s,o)=>{const{segmentId:e,value:t,preCommandId:r}=o,a=s.get(n.ICommandService),i=s.get(exports.TextSelectionManagerService),d=s.get(n.IUniverInstanceService),c=i.getCurrentSelections();if(!Array.isArray(c)||c.length===0)return!1;const u=d.getCurrentUniverDocInstance();if(u==null)return!1;const l=u.getUnitId();let g;switch(r){case he.id:case Te.id:case ve.id:case Ce.id:case V.id:case xe.id:{g=rn(u.getSelfOrHeaderFooterModel(e).getBody().textRuns,r,c);break}case ye.id:case _e.id:{g=t;break}case Oe.id:case Re.id:{g={rgb:t};break}case Me.id:{g={rgb:null};break}default:throw new Error(`Unknown command: ${r} in handleInlineFormat`)}const m={id:U.id,params:{unitId:l,actions:[],textRanges:c.map(z)}},f=new n.TextX,S=n.JSONX.getInstance(),T=new n.MemoryCursor;T.reset();for(const h of c){const{startOffset:v,endOffset:_}=h,D={dataStream:"",textRuns:[{st:0,ed:_-v,ts:{[ft[r]]:g}}]},C=v-T.cursor;C!==0&&f.push({t:n.TextXActionType.RETAIN,len:C,segmentId:e}),f.push({t:n.TextXActionType.RETAIN,body:D,len:_-v,segmentId:e}),T.reset(),T.moveCursor(_)}const I=w(u,e);return m.params.actions=S.editOp(f.serialize(),I),!!a.syncExecuteCommand(m.id,m.params)}};function on(s){return s!==null&&typeof s=="object"}function rn(s,o,e){let t=0,r=0;const a=ft[o];for(;t!==s.length&&r!==e.length;){const{startOffset:i,endOffset:d}=e[r],{st:c,ed:u,ts:l}=s[t];if(d<=c)r++;else if(u<=i)t++;else{if(/bl|it/.test(a))return(l==null?void 0:l[a])===n.BooleanNumber.TRUE?n.BooleanNumber.FALSE:n.BooleanNumber.TRUE;if(/ul|st/.test(a))return on(l==null?void 0:l[a])&&(l==null?void 0:l[a]).s===n.BooleanNumber.TRUE?{s:n.BooleanNumber.FALSE}:{s:n.BooleanNumber.TRUE};if(/va/.test(a))return o===V.id?(l==null?void 0:l[a])===n.BaselineOffset.SUBSCRIPT?n.BaselineOffset.NORMAL:n.BaselineOffset.SUBSCRIPT:(l==null?void 0:l[a])===n.BaselineOffset.SUPERSCRIPT?n.BaselineOffset.NORMAL:n.BaselineOffset.SUPERSCRIPT;t++}}return/bl|it/.test(a)?n.BooleanNumber.TRUE:/ul|st/.test(a)?{s:n.BooleanNumber.TRUE}:o===V.id?n.BaselineOffset.SUBSCRIPT:n.BaselineOffset.SUPERSCRIPT}const De={id:"doc.command.list-operation",type:n.CommandType.COMMAND,handler:(s,o)=>{var R,E,b,F,L,W;const e=s.get(exports.TextSelectionManagerService),t=s.get(n.IUniverInstanceService),r=s.get(n.ICommandService),{listType:a}=o,i=t.getCurrentUniverDocInstance(),d=e.getActiveRange();if(i==null||d==null)return!1;const{segmentId:c}=d,u=(R=e.getCurrentSelections())!=null?R:[],l=(E=i.getSelfOrHeaderFooterModel(c).getBody())==null?void 0:E.paragraphs,g=u.map(z);if(l==null)return!1;const m=(F=(b=i.getSelfOrHeaderFooterModel(c).getBody())==null?void 0:b.sectionBreaks)!=null?F:[],f=It(d,l),S=i.getUnitId(),T=f.every(G=>{var B;return((B=G.bullet)==null?void 0:B.listType)===a});let p=n.Tools.generateRandomId(6);if(f.length===1){const G=l.indexOf(f[0]),B=l[G-1],k=l[G+1];B&&B.bullet&&B.bullet.listType===a?p=B.bullet.listId:k&&k.bullet&&k.bullet.listType===a&&(p=k.bullet.listId)}const h={id:U.id,params:{unitId:S,actions:[],textRanges:g}},v=new n.MemoryCursor;v.reset();const _=new n.TextX,D=n.JSONX.getInstance(),C=(L=i.getSnapshot().lists)!=null?L:{},y={...n.PRESET_LIST_TYPE,...C},{defaultTabStop:M=36}=i.getSnapshot().documentStyle;for(const G of f){const{startIndex:B,paragraphStyle:k={}}=G,{indentFirstLine:Dt,snapToGrid:Et,indentStart:Ee}=k,{hanging:Ae,indentStart:$e}=y[a].nestingLevel[0],{charSpace:At,gridType:Nt}=sn(B,m)||{charSpace:0,gridType:n.GridType.LINES},j=N.getCharSpaceApply(At,M,Nt,Et);_.push({t:n.TextXActionType.RETAIN,len:B-v.cursor,segmentId:c}),_.push({t:n.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[T?{paragraphStyle:{...k,hanging:void 0,indentStart:Ee?{v:Math.max(0,N.getNumberUnitValue(Ee,j)+N.getNumberUnitValue(Ae,j)-N.getNumberUnitValue($e,j))}:void 0},startIndex:0}:{startIndex:0,paragraphStyle:{...k,indentFirstLine:void 0,hanging:Ae,indentStart:{v:N.getNumberUnitValue($e,j)-N.getNumberUnitValue(Ae,j)+N.getNumberUnitValue(Dt,j)+N.getNumberUnitValue(Ee,j)}},bullet:{...(W=G.bullet)!=null?W:{nestingLevel:0,textStyle:{fs:20}},listType:a,listId:p}}]},segmentId:c,coverType:n.UpdateDocsAttributeType.REPLACE}),v.moveCursorTo(B+1)}const O=w(i,c);return h.params.actions=D.editOp(_.serialize(),O),!!r.syncExecuteCommand(h.id,h.params)}},St={id:"doc.command.bullet-list",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(De.id,{listType:n.PresetListType.BULLET_LIST})},pt={id:"doc.command.order-list",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(De.id,{listType:n.PresetListType.ORDER_LIST})};function It(s,o){const{startOffset:e,endOffset:t}=s,r=[];let a=-1;for(const i of o){const{startIndex:d}=i;(e>a&&e<=d||t>a&&t<=d||d>=e&&d<=t)&&r.push(i),a=d}return r}function sn(s,o){const e=o.sort(n.sortRulesFactory("startIndex"));for(let t=0;t<e.length;t++){const r=o[t];if(r.startIndex>=s)return r}}const ht={id:"doc.command-replace-content",type:n.CommandType.COMMAND,handler:async(s,o)=>{const{unitId:e,body:t,textRanges:r,segmentId:a="",options:i}=o,d=s.get(n.IUniverInstanceService),c=s.get(n.ICommandService),u=s.get(exports.TextSelectionManagerService),l=d.getUniverDocInstance(e),g=l==null?void 0:l.getSnapshot().body,m=u.getCurrentSelections();if(l==null||g==null||!Array.isArray(m)||m.length===0)return!1;const f=vt(e,a,l,g,t);return f.params.textRanges=r,i&&(f.params.options=i),!!c.syncExecuteCommand(f.id,f.params)}},Tt={id:"doc.command-cover-content",type:n.CommandType.COMMAND,handler:async(s,o)=>{const{unitId:e,body:t,segmentId:r=""}=o,a=s.get(n.IUniverInstanceService),i=s.get(n.ICommandService),d=s.get(n.IUndoRedoService),c=a.getUniverDocInstance(e),u=c==null?void 0:c.getSnapshot().body;if(c==null||u==null)return!1;const l=vt(e,r,c,u,t);return l.params.noNeedSetTextRange=!0,l.params.noHistory=!0,i.syncExecuteCommand(l.id,l.params),d.clearUndoRedo(e),!0}};function vt(s,o,e,t,r){const a={id:U.id,params:{unitId:s,actions:[],textRanges:[]}},i=new n.TextX,d=n.JSONX.getInstance(),c=(t==null?void 0:t.dataStream.length)-2;c>0&&i.push({t:n.TextXActionType.DELETE,len:c,line:0,segmentId:o}),r.dataStream.length>0&&i.push({t:n.TextXActionType.INSERT,body:r,len:r.dataStream.length,line:0,segmentId:o});const u=w(e,o);return a.params.actions=d.editOp(i.serialize(),u),a}n.CommandType.COMMAND;const an=(s,o)=>{const e=s.get(n.IUniverInstanceService).getUniverDocInstance(o.unitId),t=(e==null?void 0:e.zoomRatio)||1;return{...n.Tools.deepClone(o),zoomRatio:t}},K={id:"doc.operation.set-zoom-ratio",type:n.CommandType.OPERATION,handler:(s,o)=>{const e=s.get(n.IUniverInstanceService).getUniverDocInstance(o.unitId);if(!e)return!1;const t=e.getSnapshot();return t.settings==null?t.settings={zoomRatio:o.zoomRatio}:t.settings.zoomRatio=o.zoomRatio,!0}},Ct={type:n.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(s,o)=>{var g,m,f;const e=s.get(n.ICommandService),t=s.get(n.IUndoRedoService),r=s.get(n.IUniverInstanceService);let a=(g=r.getCurrentUniverDocInstance())==null?void 0:g.getUnitId();if(!a)return!1;let i=1;if(o&&(a=(m=o.documentId)!=null?m:a,i=(f=o.zoomRatio)!=null?f:i),!r.getUniverDocInstance(a))return!1;const c={zoomRatio:i,unitId:a},u=an(s,c);return e.syncExecuteCommand(K.id,c)?(t.pushUndoRedo({unitID:a,undoMutations:[{id:K.id,params:u}],redoMutations:[{id:K.id,params:c}]}),!0):!1}},le={id:"doc.operation.move-cursor",type:n.CommandType.OPERATION,handler:(s,o)=>!!o},ue={id:"doc.operation.move-selection",type:n.CommandType.OPERATION,handler:(s,o)=>!!o},xt={id:"doc.operation.select-all",type:n.CommandType.COMMAND,handler:async s=>{const o=s.get(n.IUniverInstanceService),e=s.get(exports.TextSelectionManagerService),t=o.getCurrentUniverDocInstance(),r=e.getActiveRange();if(t==null||r==null)return!1;const{segmentId:a}=r,i=t.getSelfOrHeaderFooterModel(a).getSnapshot().body;if(i==null)return!1;const d=[{startOffset:0,endOffset:i.dataStream.length-2}];return e.replaceTextRanges(d,!1),!0}};var cn=Object.defineProperty,dn=Object.getOwnPropertyDescriptor,ln=(s,o,e,t)=>{for(var r=t>1?void 0:t?dn(o,e):o,a=s.length-1,i;a>=0;a--)(i=s[a])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&cn(o,e,r),r},Y=(s,o)=>(e,t)=>o(e,t,s);let se=class extends n.Disposable{constructor(o,e,t,r,a){super();A(this,"_previousIMEContent","");A(this,"_isCompositionStart",!0);A(this,"_onStartSubscription");A(this,"_onUpdateSubscription");A(this,"_onEndSubscription");this._univerInstanceService=o,this._renderManagerSrv=e,this._textSelectionRenderManager=t,this._imeInputManagerService=r,this._commandService=a,this._initialize()}dispose(){var o,e,t;(o=this._onStartSubscription)==null||o.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(o=>{if(o==null)return;this._resetIME();const{activeRange:e}=o;e!=null&&this._imeInputManagerService.setActiveRange(n.Tools.deepClone(e))})}_initialOnCompositionUpdate(){this._onUpdateSubscription=this._textSelectionRenderManager.onCompositionupdate$.subscribe(async o=>{this._updateContent(o,!0)})}_initialOnCompositionend(){this._onEndSubscription=this._textSelectionRenderManager.onCompositionend$.subscribe(o=>{this._updateContent(o,!1)})}async _updateContent(o,e){var u;if(o==null)return;const t=this._univerInstanceService.getCurrentUniverDocInstance();if(!t)return;const r=(u=this._renderManagerSrv.getRenderById(t.getUnitId()))==null?void 0:u.with(exports.DocSkeletonManagerService).getSkeleton(),{event:a,activeRange:i}=o;if(r==null||i==null)return;const c=a.data;c===this._previousIMEContent&&e||(await this._commandService.executeCommand(Ge.id,{unitId:t.getUnitId(),newText:c,oldTextLen:this._previousIMEContent.length,isCompositionStart:this._isCompositionStart,isCompositionEnd:!e}),e?(this._isCompositionStart&&(this._isCompositionStart=!1),this._previousIMEContent=c):this._resetIME())}_resetIME(){this._previousIMEContent="",this._isCompositionStart=!0,this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(null)}};se=ln([n.OnLifecycle(n.LifecycleStages.Rendered,se),Y(0,n.IUniverInstanceService),Y(1,N.IRenderManagerService),Y(2,N.ITextSelectionRenderManager),Y(3,n.Inject(q)),Y(4,n.ICommandService)],se);var un=Object.defineProperty,mn=Object.getOwnPropertyDescriptor,gn=(s,o,e,t)=>{for(var r=t>1?void 0:t?mn(o,e):o,a=s.length-1,i;a>=0;a--)(i=s[a])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&un(o,e,r),r},ne=(s,o)=>(e,t)=>o(e,t,s);let ae=class extends n.Disposable{constructor(o,e,t,r){super();A(this,"_onInputSubscription");this._univerInstanceService=o,this._renderManagerService=e,this._textSelectionManagerService=t,this._commandService=r,this._commandExecutedListener()}dispose(){var o;super.dispose(),(o=this._onInputSubscription)==null||o.unsubscribe()}_commandExecutedListener(){const o=[le.id,ue.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(!o.includes(e.id))return;const t=e.params;switch(e.id){case le.id:return this._handleMoveCursor(t.direction);case ue.id:return this._handleShiftMoveSelection(t.direction);default:throw new Error("Unknown command")}}))}_handleShiftMoveSelection(o){var v,_,D;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getCurrentSelections(),r=this._univerInstanceService.getCurrentUniverDocInstance();if(!r)return;const a=(v=this._renderManagerService.getRenderById(r.getUnitId()))==null?void 0:v.with(exports.DocSkeletonManagerService).getSkeleton(),i=this._getDocObject();if(e==null||a==null||i==null)return;const{startOffset:d,endOffset:c,style:u,collapsed:l,direction:g,segmentId:m,startNodePosition:f,endNodePosition:S,segmentPage:T}=e;if(t.length>1){let C=Number.POSITIVE_INFINITY,y=Number.NEGATIVE_INFINITY;for(const M of t)C=Math.min(C,M.startOffset),y=Math.max(y,M.endOffset);this._textSelectionManagerService.replaceTextRanges([{startOffset:o===n.Direction.LEFT||o===n.Direction.UP?y:C,endOffset:o===n.Direction.LEFT||o===n.Direction.UP?C:y,style:u}],!1);return}const I=l||g===N.RANGE_DIRECTION.FORWARD?d:c;let p=l||g===N.RANGE_DIRECTION.FORWARD?c:d;const h=(_=r.getSelfOrHeaderFooterModel(m).getBody().dataStream.length)!=null?_:Number.POSITIVE_INFINITY;if(o===n.Direction.LEFT||o===n.Direction.RIGHT){const C=a.findNodeByCharIndex(p-1,m,T),y=a.findNodeByCharIndex(p,m,T);p=o===n.Direction.RIGHT?p+y.count:p-((D=C==null?void 0:C.count)!=null?D:0),p=Math.min(h-2,Math.max(0,p)),this._textSelectionManagerService.replaceTextRanges([{startOffset:I,endOffset:p,style:u}],!1)}else{const C=a.findNodeByCharIndex(p,m,T),y=i.document.getOffsetConfig(),M=l?f:g===N.RANGE_DIRECTION.FORWARD?S:f,O=this._getTopOrBottomPosition(a,C,M,o===n.Direction.DOWN);if(O==null){const R=o===n.Direction.UP?0:h-2;if(R===p)return;this._textSelectionManagerService.replaceTextRanges([{startOffset:I,endOffset:R,style:u}],!1);return}const x=new N.NodePositionConvertToCursor(y,a).getRangePointData(O,O).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{startOffset:I,endOffset:x.endOffset,style:u}],!1)}}_handleMoveCursor(o){var h,v,_,D;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getCurrentSelections(),r=this._univerInstanceService.getCurrentUniverDocInstance();if(!r)return!1;const a=(h=this._renderManagerService.getRenderById(r.getUnitId()))==null?void 0:h.with(exports.DocSkeletonManagerService).getSkeleton(),i=this._getDocObject(),d=r.getBody();if(e==null||a==null||i==null||t==null||d==null)return;const{startOffset:c,endOffset:u,style:l,collapsed:g,segmentId:m,startNodePosition:f,endNodePosition:S,segmentPage:T}=e,I=(v=r.getSelfOrHeaderFooterModel(m).getBody().dataStream.length)!=null?v:Number.POSITIVE_INFINITY,p=(_=r.getCustomRanges())!=null?_:[];if(o===n.Direction.LEFT||o===n.Direction.RIGHT){let C;if(!e.collapsed||t.length>1){let O=Number.POSITIVE_INFINITY,x=Number.NEGATIVE_INFINITY;for(const R of t)O=Math.min(O,R.startOffset),x=Math.max(x,R.endOffset);C=o===n.Direction.LEFT?O:x}else{const O=a.findNodeByCharIndex(c-1,m,T),x=a.findNodeByCharIndex(c,m,T);o===n.Direction.LEFT?C=Math.max(0,c-((D=O==null?void 0:O.count)!=null?D:0)):C=Math.min(I-2,u+x.count)}const y=[n.DataStreamTreeTokenType.CUSTOM_RANGE_START,n.DataStreamTreeTokenType.CUSTOM_RANGE_END];if(o===n.Direction.LEFT)for(;y.includes(d.dataStream[C]);)C--;else for(;y.includes(d.dataStream[C-1]);)C++;p.filter(O=>O.wholeEntity&&O.startIndex<C&&O.endIndex>=C).forEach(O=>{o===n.Direction.LEFT?C=Math.min(O.startIndex,C):C=Math.max(O.endIndex+1,C)}),this._textSelectionManagerService.replaceTextRanges([{startOffset:C,endOffset:C,style:l}],!1)}else{const C=a.findNodeByCharIndex(c,m,T),y=a.findNodeByCharIndex(u,m,T),M=i.document.getOffsetConfig(),O=this._getTopOrBottomPosition(a,o===n.Direction.UP||g?C:y,o===n.Direction.UP||g?f:S,o===n.Direction.DOWN);if(O==null){let R;g?R=o===n.Direction.UP?0:I-2:R=o===n.Direction.UP?c:u,this._textSelectionManagerService.replaceTextRanges([{startOffset:R,endOffset:R,style:l}],!1);return}const x=new N.NodePositionConvertToCursor(M,a).getRangePointData(O,O).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{...x,style:l}],!1)}}_getTopOrBottomPosition(o,e,t,r){if(e==null||t==null)return;const a=this._getGlyphLeftOffsetInLine(e),i=this._getNextOrPrevLine(e,r);if(i==null)return;const d=this._matchPositionByLeftOffset(o,i,a,t);if(d!=null)return{...d,isBack:!0}}_getGlyphLeftOffsetInLine(o){const e=o.parent;if(e==null)return Number.NEGATIVE_INFINITY;const t=e.left,{left:r}=o;return t+r}_matchPositionByLeftOffset(o,e,t,r){const a={distance:Number.POSITIVE_INFINITY};for(const d of e.divides){const c=d.left;for(const u of d.glyphGroup){const{left:l}=u,g=c+l,m=Math.abs(t-g);m<a.distance&&(a.glyph=u,a.distance=m)}}if(a.glyph==null)return;const{segmentPage:i}=r;return o.findPositionByGlyph(a.glyph,i)}_getNextOrPrevLine(o,e){var S,T,I,p,h,v,_,D,C,y,M,O;const t=o.parent;if(t==null)return;const r=t.parent;if(r==null)return;const a=r.parent;if(a==null)return;const i=a.lines.indexOf(r);if(i===-1)return;let d;if(e===!0?d=a.lines[i+1]:d=a.lines[i-1],d!=null)return d;const c=a.parent;if(c==null)return;const u=c.columns.indexOf(a);if(u===-1)return;if(e===!0)d=(S=c.columns[u+1])==null?void 0:S.lines[0];else{const x=(I=(T=c.columns)==null?void 0:T[u-1])==null?void 0:I.lines;d=x==null?void 0:x[x.length-1]}if(d!=null)return d;const l=c.parent;if(l==null)return;const g=l.sections.indexOf(c);if(g===-1)return;if(e===!0)d=(h=(p=l.sections[g-1])==null?void 0:p.columns[0])==null?void 0:h.lines[0];else{const x=(_=(v=l.sections)==null?void 0:v[g-1])==null?void 0:_.columns,R=x==null?void 0:x[x.length-1],E=R==null?void 0:R.lines;d=E==null?void 0:E[E.length-1]}if(d!=null)return d;const m=l.parent;if(m==null)return;const f=m.pages.indexOf(l);if(f!==-1){if(e===!0)d=(y=(C=(D=m.pages[f+1])==null?void 0:D.sections[0])==null?void 0:C.columns[0])==null?void 0:y.lines[0];else{const x=(M=m.pages[f-1])==null?void 0:M.sections;if(x==null)return;const R=(O=x[x.length-1])==null?void 0:O.columns,E=R[R.length-1],b=E==null?void 0:E.lines;d=b[b.length-1]}if(d!=null)return d}}_getDocObject(){return it(this._univerInstanceService,this._renderManagerService)}};ae=gn([n.OnLifecycle(n.LifecycleStages.Rendered,ae),ne(0,n.IUniverInstanceService),ne(1,N.IRenderManagerService),ne(2,n.Inject(exports.TextSelectionManagerService)),ne(3,n.ICommandService)],ae);var fn=Object.defineProperty,Sn=Object.getOwnPropertyDescriptor,pn=(s,o,e,t)=>{for(var r=t>1?void 0:t?Sn(o,e):o,a=s.length-1,i;a>=0;a--)(i=s[a])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&fn(o,e,r),r},oe=(s,o)=>(e,t)=>o(e,t,s);let ie=class extends n.Disposable{constructor(o,e,t,r){super();A(this,"_onInputSubscription");this._univerInstanceService=o,this._renderManagerService=e,this._textSelectionRenderManager=t,this._commandService=r,this._init()}dispose(){var o;super.dispose(),(o=this._onInputSubscription)==null||o.unsubscribe()}_init(){this._initialNormalInput()}_initialNormalInput(){this._onInputSubscription=this._textSelectionRenderManager.onInput$.subscribe(async o=>{var T;if(o==null)return;const e=this._univerInstanceService.getCurrentUniverDocInstance();if(!e)return;const t=e.getUnitId(),{event:r,content:a="",activeRange:i}=o,d=r,c=(T=this._renderManagerService.getRenderById(e.getUnitId()))==null?void 0:T.with(exports.DocSkeletonManagerService).getSkeleton();if(d.data==null||c==null||!c||!i)return;const{startOffset:u,segmentId:l,style:g,segmentPage:m}=i,f=a.length,S=[{startOffset:u+f,endOffset:u+f,segmentId:l,segmentPage:m,style:g}];await this._commandService.executeCommand(Z.id,{unitId:t,body:{dataStream:a},textRanges:S,range:i,segmentId:l})})}};ie=pn([n.OnLifecycle(n.LifecycleStages.Rendered,ie),oe(0,n.IUniverInstanceService),oe(1,N.IRenderManagerService),oe(2,N.ITextSelectionRenderManager),oe(3,n.ICommandService)],ie);const J={id:"doc.command.align-operation",type:n.CommandType.COMMAND,handler:(s,o)=>{var D,C;const e=s.get(exports.TextSelectionManagerService),t=s.get(n.IUniverInstanceService),r=s.get(n.ICommandService),{alignType:a}=o,i=t.getCurrentUniverDocInstance(),d=e.getActiveRange();if(i==null||d==null)return!1;const{segmentId:c}=d,u=(D=e.getCurrentSelections())!=null?D:[],l=(C=i.getSelfOrHeaderFooterModel(c).getBody())==null?void 0:C.paragraphs,g=u.map(z);if(l==null)return!1;const m=It(d,l),f=i.getUnitId(),S=m.every(y=>{var M;return((M=y.paragraphStyle)==null?void 0:M.horizontalAlign)===a}),T={id:U.id,params:{unitId:f,actions:[],textRanges:g}},I=new n.MemoryCursor;I.reset();const p=new n.TextX,h=n.JSONX.getInstance();for(const y of m){const{startIndex:M}=y;p.push({t:n.TextXActionType.RETAIN,len:M-I.cursor,segmentId:c});const O={...y.paragraphStyle,horizontalAlign:S?n.HorizontalAlign.UNSPECIFIED:a};p.push({t:n.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[{...y,paragraphStyle:O,startIndex:0}]},segmentId:c,coverType:n.UpdateDocsAttributeType.REPLACE}),I.moveCursorTo(M+1)}const v=w(i,c);return T.params.actions=h.editOp(p.serialize(),v),!!r.syncExecuteCommand(T.id,T.params)}},yt={id:"doc.command.align-left",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(J.id,{alignType:n.HorizontalAlign.LEFT})},_t={id:"doc.command.align-center",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(J.id,{alignType:n.HorizontalAlign.CENTER})},Ot={id:"doc.command.align-right",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(J.id,{alignType:n.HorizontalAlign.RIGHT})},Rt={id:"doc.command.align-justify",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(J.id,{alignType:n.HorizontalAlign.JUSTIFIED})};var In=Object.defineProperty,hn=Object.getOwnPropertyDescriptor,Tn=(s,o,e,t)=>{for(var r=t>1?void 0:t?hn(o,e):o,a=s.length-1,i;a>=0;a--)(i=s[a])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&In(o,e,r),r},Ue=(s,o)=>(e,t)=>o(e,t,s);let ce=class extends n.Disposable{constructor(s,o,e){super(),this._commandService=s,this._textSelectionManagerService=o,this._univerInstanceService=e,this._initSelectionChange()}_transformCustomRange(s,o){var i;const{startOffset:e,endOffset:t,collapsed:r}=o,a=(i=s.getCustomRanges())==null?void 0:i.filter(d=>!d.wholeEntity||e<=d.startIndex&&t>d.endIndex?!1:r?d.startIndex<e&&d.endIndex>=t:Xe(e,t-1,d.startIndex,d.endIndex));if(a!=null&&a.length){let d=e,c=t;return a.forEach(u=>{d=Math.min(u.startIndex,d),c=Math.max(u.endIndex+1,c)}),{...o,startOffset:d,endOffset:c,collapsed:d===c}}return o}_initSelectionChange(){this.disposeWithMe(this._commandService.onCommandExecuted(s=>{if(s.id===me.id){const o=s.params,{unitId:e,ranges:t,isEditing:r}=o,a=this._univerInstanceService.getUnit(e);if(!a)return;const i=t.map(d=>this._transformCustomRange(a,d));i.some((d,c)=>t[c]!==d)&&this._textSelectionManagerService.replaceTextRanges(i,r)}}))}};ce=Tn([n.OnLifecycle(n.LifecycleStages.Ready,ce),Ue(0,n.ICommandService),Ue(1,n.Inject(exports.TextSelectionManagerService)),Ue(2,n.IUniverInstanceService)],ce);var vn=Object.defineProperty,Cn=Object.getOwnPropertyDescriptor,xn=(s,o,e,t)=>{for(var r=t>1?void 0:t?Cn(o,e):o,a=s.length-1,i;a>=0;a--)(i=s[a])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&vn(o,e,r),r},yn=(s,o)=>(e,t)=>o(e,t,s);const _n="docs";var re;exports.UniverDocsPlugin=(re=class extends n.Plugin{constructor(o={},e){super(),this._injector=e,this._initializeDependencies(e),this._initializeCommands()}_initializeCommands(){[le,ue,ut,mt,he,Te,ve,Ce,V,xe,ye,_e,Oe,Me,Re,je,lt,Z,H,pe,Fe,Ge,Ie,U,ht,Tt,Ct,K,me,xt,pt,St,De,yt,_t,Ot,J,Rt].forEach(o=>{this._injector.get(n.ICommandService).registerCommand(o)})}_initializeDependencies(o){[[exports.DocStateChangeManagerService],[q],[N.ITextSelectionRenderManager,{useClass:N.TextSelectionRenderManager}],[exports.TextSelectionManagerService],[fe],[ie],[se],[ae],[ce]].forEach(e=>o.add(e))}},A(re,"pluginName",_n),A(re,"type",n.UniverInstanceType.UNIVER_DOC),re);exports.UniverDocsPlugin=xn([yn(1,n.Inject(n.Injector))],exports.UniverDocsPlugin);function On(s,o){const{range:e,rangeId:t,rangeType:r,segmentId:a}=s,i=$t(e,o);if(!i)return null;const{startOffset:d,endOffset:c}=i,u=new n.TextX;return d>0&&u.push({t:n.TextXActionType.RETAIN,len:d,segmentId:a}),u.push({t:n.TextXActionType.INSERT,body:{dataStream:n.DataStreamTreeTokenType.CUSTOM_RANGE_START},len:1,line:0}),u.push({t:n.TextXActionType.RETAIN,body:{dataStream:""},len:c-d,segmentId:a}),u.push({t:n.TextXActionType.INSERT,body:{dataStream:n.DataStreamTreeTokenType.CUSTOM_RANGE_END,customRanges:[{rangeId:t,rangeType:r,startIndex:-(c-d)-1,endIndex:0}]},len:1,line:0}),u}function Rn(s,o){const e={id:U.id,params:{unitId:s.unitId,actions:[],textRanges:void 0}},t=n.JSONX.getInstance(),r=On(s,o);return r?(e.params.actions=t.editOp(r.serialize()),e):!1}function Mn(s,o){var C;const{segmentId:e,rangeId:t,rangeType:r,wholeEntity:a}=o,i=s.get(exports.TextSelectionManagerService),d=s.get(n.IUniverInstanceService),c=i.getActiveRange();if(!c)return!1;const u=d.getCurrentUnitForType(n.UniverInstanceType.UNIVER_DOC);if(!u)return!1;const l=u.getBody(),g=u.getUnitId();if(!l)return!1;const{startOffset:m,endOffset:f}=Q(c),S=(C=l.customRanges)!=null?C:[],T=[];for(let y=0,M=S.length;y<M;y++){const O=S[y];if(O.rangeType===r&&Math.max(O.startIndex,m)<=Math.min(O.endIndex,f-1)&&T.push({...O}),O.startIndex>=f)break}const I=T.map(y=>[y.startIndex,y.endIndex]).flat().sort((y,M)=>y-M);let p=0;const h=new n.TextX,v=I.length?{startOffset:Math.min(I[0],m),endOffset:Math.max(I[I.length-1]+1,f)}:c;v.startOffset!==p&&(h.push({t:n.TextXActionType.RETAIN,len:v.startOffset-p,segmentId:e}),p=v.startOffset),h.push({t:n.TextXActionType.INSERT,body:{dataStream:n.DataStreamTreeTokenType.CUSTOM_RANGE_START},len:1,line:0,segmentId:e}),I.forEach((y,M)=>{y!==p&&(h.push({t:n.TextXActionType.RETAIN,len:y-p,segmentId:e}),p=y),h.push({t:n.TextXActionType.DELETE,len:1,line:0,segmentId:e}),p++}),p!==v.endOffset&&(h.push({t:n.TextXActionType.RETAIN,len:v.endOffset-p,segmentId:e}),p=v.endOffset),h.push({t:n.TextXActionType.INSERT,body:{dataStream:n.DataStreamTreeTokenType.CUSTOM_RANGE_END,customRanges:[{rangeId:t,rangeType:r,startIndex:-(v.endOffset-v.startOffset-I.length+1),endIndex:0,wholeEntity:a}]},len:1,line:0,segmentId:e});const _=n.JSONX.getInstance(),D={id:U.id,params:{unitId:g,actions:[],textRanges:void 0}};return D.params.actions=_.editOp(h.serialize()),D}function Dn(s,o){var m,f;const{unitId:e,rangeId:t,segmentId:r}=o,i=s.get(n.IUniverInstanceService).getUnit(e);if(!i)return!1;const d=(f=(m=i.getBody())==null?void 0:m.customRanges)==null?void 0:f.find(S=>S.rangeId===t);if(!d)return!1;const{startIndex:c,endIndex:u}=d,l=new n.TextX,g=u-c+1;return c>0&&l.push({t:n.TextXActionType.RETAIN,len:c,segmentId:r}),l.push({t:n.TextXActionType.DELETE,len:1,segmentId:r,line:0}),g-2>0&&l.push({t:n.TextXActionType.RETAIN,len:g-2,segmentId:r}),l.push({t:n.TextXActionType.DELETE,len:1,segmentId:r,line:0}),l}function En(s,o){const e={id:U.id,params:{unitId:o.unitId,actions:[],textRanges:void 0}},t=n.JSONX.getInstance(),r=Dn(s,o);return r?(e.params.actions=t.editOp(r.serialize()),e):!1}function Mt(s){const{unitId:o,range:e,id:t,type:r,segmentId:a}=s,{startOffset:i,endOffset:d}=e,c={id:U.id,params:{unitId:o,actions:[],textRanges:void 0}},u=new n.TextX,l=n.JSONX.getInstance();return i>0&&u.push({t:n.TextXActionType.RETAIN,len:i,segmentId:a}),u.push({t:n.TextXActionType.RETAIN,body:{dataStream:"",customDecorations:[{id:t,type:r,startIndex:0,endIndex:d-i-1}]},len:d-i,segmentId:a}),c.params.actions=l.editOp(u.serialize()),c}function An(s,o){const{segmentId:e,id:t,type:r}=o,a=s.get(exports.TextSelectionManagerService),i=s.get(n.IUniverInstanceService),d=a.getActiveRange();if(!d)return!1;const c=i.getCurrentUnitForType(n.UniverInstanceType.UNIVER_DOC);if(!c)return!1;const u=c.getBody(),l=c.getUnitId();return u?Mt({unitId:l,range:{startOffset:d.startOffset,endOffset:d.endOffset,collapsed:!0},id:t,type:r,segmentId:e}):!1}function Nn(s,o){var T,I;const{unitId:e,id:t,segmentId:r}=o,i=s.get(n.IUniverInstanceService).getUnit(e),d=i==null?void 0:i.getBody();if(!i||!d)return!1;const c=(I=(T=i.getBody())==null?void 0:T.customDecorations)==null?void 0:I.filter(p=>p.id===t);if(!(c!=null&&c.length))return!1;const u=c.map(p=>n.getBodySlice(d,p.startIndex,p.endIndex+1)),l=u.map(p=>{var v;const h=n.Tools.deepClone(p);return h.customDecorations=(v=h.customDecorations)==null?void 0:v.filter(_=>_.id!==t),h}),g={id:U.id,params:{unitId:e,actions:[],textRanges:void 0}},m=new n.TextX,f=n.JSONX.getInstance();let S=0;return c.forEach((p,h)=>{const v=l[h],_=u[h];p.startIndex!==S&&m.push({t:n.TextXActionType.RETAIN,len:p.startIndex-S,segmentId:r}),S=p.startIndex,m.push({t:n.TextXActionType.RETAIN,len:p.endIndex-p.startIndex+1,segmentId:r,body:v,oldBody:_,coverType:n.UpdateDocsAttributeType.REPLACE}),S=S+(p.endIndex-p.startIndex+1)}),g.params.actions=f.editOp(m.serialize()),g}const Un=n.createInterceptorKey("CUSTOM_RANGE"),bn=n.createInterceptorKey("CUSTOM_DECORATION"),de={CUSTOM_RANGE:Un,CUSTOM_DECORATION:bn};var Pn=Object.defineProperty,wn=Object.getOwnPropertyDescriptor,Bn=(s,o,e,t)=>{for(var r=t>1?void 0:t?wn(o,e):o,a=s.length-1,i;a>=0;a--)(i=s[a])&&(r=(t?i(o,e,r):i(r))||r);return t&&r&&Pn(o,e,r),r},Xn=(s,o)=>(e,t)=>o(e,t,s);exports.DocInterceptorService=class extends n.Disposable{constructor(e,t){super();A(this,"_interceptorsByName",new Map);this._context=e,this._docSkeletonManagerService=t,this.disposeWithMe(this._docSkeletonManagerService.currentViewModel$.subscribe(r=>{if(r){const a=r.getDataModel().getUnitId();if(a===n.DOCS_NORMAL_EDITOR_UNIT_ID_KEY||a===n.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY)return;this.interceptDocumentViewModel(r)}})),this.disposeWithMe(this.intercept(de.CUSTOM_RANGE,{priority:-1,handler:(r,a,i)=>i(r)}))}intercept(e,t){const r=e;this._interceptorsByName.has(r)||this._interceptorsByName.set(r,[]);const a=this._interceptorsByName.get(r);return a.push(t),this._interceptorsByName.set(r,a.sort((i,d)=>{var c,u;return((c=d.priority)!=null?c:0)-((u=i.priority)!=null?u:0)})),this.disposeWithMe(n.toDisposable(()=>n.remove(this._interceptorsByName.get(r),t)))}fetchThroughInterceptors(e){const t=e,r=this._interceptorsByName.get(t);return n.composeInterceptors(r||[])}interceptDocumentViewModel(e){const t=new n.DisposableCollection;return t.add(e.registerCustomRangeInterceptor({getCustomRange:r=>{var a;return this.fetchThroughInterceptors(de.CUSTOM_RANGE)(e.getCustomRangeRaw(r),{index:r,unitId:e.getDataModel().getUnitId(),customRanges:(a=e.getDataModel().getCustomRanges())!=null?a:[]})},getCustomDecoration:r=>{var a;return this.fetchThroughInterceptors(de.CUSTOM_DECORATION)(e.getCustomDecorationRaw(r),{index:r,unitId:e.getDataModel().getUnitId(),customDecorations:(a=e.getDataModel().getCustomDecorations())!=null?a:[]})}})),t}};exports.DocInterceptorService=Bn([n.OnLifecycle(n.LifecycleStages.Starting,exports.DocInterceptorService),Xn(1,n.Inject(exports.DocSkeletonManagerService))],exports.DocInterceptorService);const Ln=s=>s.body?s.body.dataStream.slice(0,-2).replaceAll(n.DataStreamTreeTokenType.CUSTOM_RANGE_START,"").replaceAll(n.DataStreamTreeTokenType.CUSTOM_RANGE_END,""):"";exports.AlignCenterCommand=_t;exports.AlignJustifyCommand=Rt;exports.AlignLeftCommand=yt;exports.AlignOperationCommand=J;exports.AlignRightCommand=Ot;exports.BreakLineCommand=lt;exports.BulletListCommand=St;exports.CoverContentCommand=Tt;exports.CutContentCommand=ke;exports.DOCS_COMPONENT_BACKGROUND_LAYER_INDEX=Jt;exports.DOCS_COMPONENT_DEFAULT_Z_INDEX=Kt;exports.DOCS_COMPONENT_HEADER_LAYER_INDEX=Yt;exports.DOCS_COMPONENT_MAIN_LAYER_INDEX=Wt;exports.DOCS_VIEW_KEY=te;exports.DOC_INTERCEPTOR_POINT=de;exports.DeleteCommand=H;exports.DeleteCustomBlockCommand=pe;exports.DeleteLeftCommand=ut;exports.DeleteRightCommand=mt;exports.DocCustomRangeService=fe;exports.EditorInsertTextCommandId=dt;exports.IMEInputCommand=Ge;exports.IMEInputManagerService=q;exports.InnerPasteCommand=en;exports.InsertCommand=Z;exports.ListOperationCommand=De;exports.MergeTwoParagraphCommand=Ie;exports.MoveCursorOperation=le;exports.MoveSelectionOperation=ue;exports.NORMAL_TEXT_SELECTION_PLUGIN_NAME=Zt;exports.OrderListCommand=pt;exports.ReplaceContentCommand=ht;exports.ResetInlineFormatTextBackgroundColorCommand=Me;exports.RichTextEditingMutation=U;exports.SelectAllOperation=xt;exports.SetDocZoomRatioCommand=Ct;exports.SetDocZoomRatioOperation=K;exports.SetInlineFormatBoldCommand=he;exports.SetInlineFormatCommand=je;exports.SetInlineFormatFontFamilyCommand=_e;exports.SetInlineFormatFontSizeCommand=ye;exports.SetInlineFormatItalicCommand=Te;exports.SetInlineFormatStrikethroughCommand=Ce;exports.SetInlineFormatSubscriptCommand=V;exports.SetInlineFormatSuperscriptCommand=xe;exports.SetInlineFormatTextBackgroundColorCommand=Re;exports.SetInlineFormatTextColorCommand=Oe;exports.SetInlineFormatUnderlineCommand=ve;exports.SetTextSelectionsOperation=me;exports.UpdateCommand=Fe;exports.VIEWPORT_KEY=at;exports.addCustomDecorationBySelectionFactory=An;exports.addCustomDecorationFactory=Mt;exports.addCustomRangeBySelectionFactory=Mn;exports.addCustomRangeFactory=Rn;exports.deleteCustomDecorationFactory=Nn;exports.deleteCustomRangeFactory=En;exports.getDeleteSelection=ee;exports.getDocObject=it;exports.getDocObjectById=Qt;exports.getInsertSelection=ge;exports.getPlainTextFormDocument=Ln;exports.getRetainAndDeleteFromReplace=Se;exports.getRichTextEditPath=w;exports.getSelectionText=Ht;exports.isSegmentIntersects=Xe;exports.neoGetDocObject=qt;exports.replaceSelectionFactory=Vt;exports.serializeTextRange=z;