@univerjs/docs 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 Rt=Object.defineProperty;var Mt=(s,o,e)=>o in s?Rt(s,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[o]=e;var A=(s,o,e)=>Mt(s,typeof o!="symbol"?o+"":o,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("@univerjs/core"),N=require("@univerjs/engine-render"),j=require("@wendellhu/redi"),G=require("rxjs");function ce(s){return s===n.DataStreamTreeTokenType.CUSTOM_RANGE_END||s===n.DataStreamTreeTokenType.CUSTOM_RANGE_START}function Ae(s,o,e,t){return s<=e&&o>=e||s>=e&&s<=t}function Ne(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 c=r.slice(0,a)+r.slice(a+o);for(let d=0,u=c.length;d<u;d++){const l=c[d];if(!ce(l))return!1}return!0}function K(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 Ue(s,o){let{startOffset:e,endOffset:t}=K(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 ue(s,o){let{startOffset:e,endOffset:t,collapsed:r}=K(s);if(r){for(;ce(o.dataStream[e-1]);)t-=1,e-=1;return{startOffset:e,endOffset:t,collapsed:r}}else return{...Ue(s,o),collapsed:!1}}function Z(s,o){let{startOffset:e,endOffset:t,collapsed:r}=K(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{...Ue(s,o),collapsed:!1}}function Dt(s,o){if(s.startOffset===s.endOffset)return null;const e=o.customRanges;if(!e)return K(s);let{startOffset:t,endOffset:r}=Ue(s,o);for(;ce(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(;ce(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 Et=[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 At(s,o,e){const t=s.slice(o,e);return Et.reduce((r,a)=>r.replaceAll(a,""),t)}var q=(s=>(s.MAIN="__Document_Render_Main__",s.BACKGROUND="__Document_Render_Background__",s))(q||{}),tt=(s=>(s.VIEW_MAIN="viewMain",s.VIEW_TOP="viewTop",s.VIEW_LEFT="viewLeft",s.VIEW_LEFT_TOP="viewLeftTop",s))(tt||{});const Nt=0,Ut=2,bt=4,Pt=10,wt="normalTextSelectionPluginName";function Bt(s){const{mainComponent:o,scene:e,engine:t,components:r}=s,a=o,i=r.get(q.BACKGROUND);return{document:a,docBackground:i,scene:e,engine:t}}function nt(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:c,components:d}=r,u=a,l=d.get(q.BACKGROUND);return{document:u,docBackground:l,scene:i,engine:c}}function Xt(s,o){const e=o.getRenderById(s);if(e==null)return;const{mainComponent:t,scene:r,engine:a,components:i}=e,c=t,d=i.get(q.BACKGROUND);return{document:c,docBackground:d,scene:r,engine:a}}const be={id:"doc.operation.set-selections",type:n.CommandType.OPERATION,handler:(s,o)=>!0};var Lt=Object.defineProperty,Ft=Object.getOwnPropertyDescriptor,kt=(s,o,e,t)=>{for(var r=t>1?void 0:t?Ft(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&&Lt(o,e,r),r},Fe=(s,o)=>(e,t)=>o(e,t,s);function $(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 G.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,c=t.find(f=>f.isActive());if(c==null)return null;const{startOffset:d,endOffset:u,collapsed:l,startNodePosition:g,endNodePosition:m,direction:h}=c;return d==null||u==null?null:{startOffset:d,endOffset:u,collapsed:l,startNodePosition:g,endNodePosition:m,direction:h,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(G.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:c,textRanges:d,isEditing:u}=t;this._commandService.executeCommand(be.id,{unitId:r,subUnitId:a,segmentId:i,style:c,isEditing:u,ranges:d.map($)})}_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($))}_replaceByParam(e){const{unitId:t,subUnitId:r,style:a,segmentId:i,textRanges:c,isEditing:d,segmentPage:u}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map),this._textSelectionInfo.get(t).set(r,{textRanges:c,style:a,segmentId:i,isEditing:d,segmentPage:u})}_addByParam(e){const{unitId:t,subUnitId:r,textRanges:a,style:i,segmentId:c,isEditing:d,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:c,isEditing:d,segmentPage:u})}};exports.TextSelectionManagerService=kt([Fe(0,N.ITextSelectionRenderManager),Fe(1,n.ICommandService)],exports.TextSelectionManagerService);function me(s,o="",e,t){var T;const{startOffset:r,endOffset:a}=s,i=[],c=r-e,d=a-e,u=t.dataStream,l=(T=t.customRanges)==null?void 0:T.filter(S=>Ae(S.startIndex,S.endIndex,r,a)),g=new Set(l==null?void 0:l.filter(S=>Ne(r,a-r,S,u))),m=new Set;l==null||l.forEach(S=>{g.has(S)||(S.startIndex-e>=c&&S.startIndex-e<=d&&S.endIndex-e>d&&m.add(S.startIndex),S.endIndex-e>=c&&S.endIndex-e<=d&&S.startIndex<c&&m.add(S.endIndex))}),c>0&&i.push({t:n.TextXActionType.RETAIN,len:c,segmentId:o});const h=[...m].sort((S,p)=>S-p);let f=c;return h.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<d&&(i.push({t:n.TextXActionType.DELETE,len:d-f,line:0,segmentId:o}),f=d+1),{dos:i,cursor:f,retain:m.size}}var Gt=Object.defineProperty,jt=Object.getOwnPropertyDescriptor,$t=(s,o,e,t)=>{for(var r=t>1?void 0:t?jt(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&&Gt(o,e,r),r},Ee=(s,o)=>(e,t)=>o(e,t,s);const ke=300;exports.DocStateChangeManagerService=class extends n.RxDisposable{constructor(e,t,r){super();A(this,"_docStateChange$",new G.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:c=!1}=e;if(i||r==null||t==="history"&&(r===n.RedoCommandId||r===n.UndoCommandId))return;const d=t==="history"?this._historyStateCache:this._changeStateCache,u=t==="history"?this._pushHistory.bind(this):this._emitChangeState.bind(this);if(d.has(a)){const l=d.get(a);l==null||l.push(e)}else d.set(a,[e]);c?t==="history"?(this._historyTimer&&clearTimeout(this._historyTimer),this._historyTimer=setTimeout(()=>{u(a)},ke)):(this._changeStateCacheTimer&&clearTimeout(this._changeStateCacheTimer),this._changeStateCacheTimer=setTimeout(()=>{u(a)},ke)):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,c=r[0],d=r[a-1],u={unitId:e,actions:r.reduce((g,m)=>n.JSONX.compose(g,m.redoState.actions),null),textRanges:d.redoState.textRanges},l={unitId:e,actions:r.reverse().reduce((g,m)=>n.JSONX.compose(g,m.undoState.actions),null),textRanges:c.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:c,noHistory:d,debounce:u}=t[0],l=t[0],g=t[r-1],m={unitId:e,actions:t.reduce((T,S)=>n.JSONX.compose(T,S.redoState.actions),null),textRanges:g.redoState.textRanges},h={unitId:e,actions:t.reverse().reduce((T,S)=>n.JSONX.compose(T,S.undoState.actions),null),textRanges:l.undoState.textRanges},f={commandId:a,unitId:e,trigger:i,redoState:m,undoState:h,segmentId:c,noHistory:d,debounce:u};t.length=0,this._docStateChange$.next(f)}};exports.DocStateChangeManagerService=$t([Ee(0,j.Inject(n.IUndoRedoService)),Ee(1,n.ICommandService),Ee(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 zt=Object.defineProperty,Ht=Object.getOwnPropertyDescriptor,Vt=(s,o,e,t)=>{for(var r=t>1?void 0:t?Ht(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&&zt(o,e,r),r},Ge=(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 G.BehaviorSubject(null));A(this,"currentSkeleton$",this._currentSkeleton$.asObservable());A(this,"_currentSkeletonBefore$",new G.BehaviorSubject(null));A(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());A(this,"_currentViewModel$",new G.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(G.takeUntil(this.dispose$)).subscribe(a=>{(a==null?void 0: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=Vt([Ge(1,j.Inject(n.LocaleService)),Ge(2,n.IUniverInstanceService)],exports.DocSkeletonManagerService);const je="doc.mutation.rich-text-editing",U={id:je,type:n.CommandType.MUTATION,handler:(s,o)=>{var O,M;const{unitId:e,segmentId:t="",actions:r,textRanges:a,prevTextRanges:i,trigger:c,noHistory:d,isCompositionEnd:u,noNeedSetTextRange:l,debounce:g}=o,m=s.get(n.IUniverInstanceService),h=s.get(N.IRenderManagerService),f=m.getUniverDocInstance(e),T=(O=h.getRenderById(e))==null?void 0:O.with(exports.DocSkeletonManagerService).getViewModel();if(f==null||T==null)throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${e}`);const S=s.get(exports.TextSelectionManagerService),I=((M=S.getCurrentSelections())!=null?M:[]).map($),C=s.get(exports.DocStateChangeManagerService),_=s.get(Q),R=!!f.getSnapshot().disabled;if(n.JSONX.isNoop(r)||r&&r.length===0||R)return{unitId:e,actions:[],textRanges:I};const v=n.JSONX.invertWithDoc(r,f.getSnapshot());f.apply(r),T.reset(f),!l&&a&&c!=null&&queueMicrotask(()=>{S.replaceTextRanges(a,!0,o.options)});const y={commandId:je,unitId:e,segmentId:t,trigger:c,noHistory:d,debounce:g,redoState:{actions:r,textRanges:a},undoState:{actions:v,textRanges:i!=null?i:I}};if(u){const x=_.fetchComposedUndoRedoMutationParams();if(x==null)throw new Error("historyParams is null in RichTextEditingMutation");const{undoMutationParams:E,redoMutationParams:D,previousActiveRange:b}=x;y.redoState.actions=D.actions,y.undoState.actions=E.actions,y.undoState.textRanges=[b]}return C.setChangeState(y),{unitId:e,actions:v,textRanges:I}}};function ot(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 rt="doc.command.insert-text",Y={id:rt,type:n.CommandType.COMMAND,handler:async(s,o)=>{var O;const e=s.get(n.ICommandService),{range:t,segmentId:r,body:a,unitId:i,textRanges:c,cursorOffset:d}=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(),h=g.getSelfOrHeaderFooterModel((O=m==null?void 0:m.segmentId)!=null?O:"").getBody();if(!h)return!1;const f=Z(t,h),{startOffset:T,collapsed:S}=f,p=d!=null?d:a.dataStream.length,I=[{startOffset:T+p,endOffset:T+p,style:m==null?void 0:m.style,collapsed:S}],C={id:U.id,params:{unitId:i,actions:[],textRanges:c!=null?c:I,debounce:!0}},_=new n.TextX,R=n.JSONX.getInstance();if(S)T>0&&_.push({t:n.TextXActionType.RETAIN,len:T,segmentId:r});else{const{dos:M,retain:x}=me(f,r,0,h);_.push(...M),c||(C.params.textRanges=[{startOffset:T+p+x,endOffset:T+p+x,collapsed:S}])}_.push({t:n.TextXActionType.INSERT,body:a,len:a.dataStream.length,line:0,segmentId:r});const v=w(g,r);return C.params.actions=R.editOp(_.serialize(),v),!!e.syncExecuteCommand(C.id,C.params)}};var P=(s=>(s[s.LEFT=0]="LEFT",s[s.RIGHT=1]="RIGHT",s))(P||{});const z={id:"doc.command.delete-text",type:n.CommandType.COMMAND,handler:async(s,o)=>{var M;const e=s.get(n.ICommandService),t=s.get(n.IUniverInstanceService),{range:r,segmentId:a,unitId:i,direction:c,len:d=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,h=c===0?g-d:g,f=h+d-1,T=(M=l.customRanges)==null?void 0:M.filter(x=>Ae(x.startIndex,x.endIndex,h,f)),S=T==null?void 0:T.filter(x=>Ne(h,d,x,m)),p=[];for(let x=0;x<d;x++)p.push(h+x);S==null||S.forEach(x=>{p.push(x.startIndex,x.endIndex)}),p.sort((x,E)=>x-E);const I=p[0],C={id:U.id,params:{unitId:i,actions:[],textRanges:[{startOffset:I,endOffset:I,collapsed:!0}],debounce:!0}},_=new n.TextX,R=n.JSONX.getInstance();let v=0;for(let x=0;x<p.length;x++){const E=p[x];E-v>0&&_.push({t:n.TextXActionType.RETAIN,len:E-v,segmentId:a}),_.push({t:n.TextXActionType.DELETE,len:1,segmentId:a,line:0}),v=E+1}const y=w(u,a);return C.params.actions=R.editOp(_.serialize(),y),!!e.syncExecuteCommand(C.id,C.params)}},Pe={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:c}=o,d=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:c}},m=new n.TextX,h=n.JSONX.getInstance(),{startOffset:f,endOffset:T}=e;m.push({t:n.TextXActionType.RETAIN,len:f,segmentId:t}),m.push({t:n.TextXActionType.RETAIN,body:r,len:T-f,segmentId:t,coverType:a});const S=w(l,t);return g.params.actions=h.editOp(m.serialize(),S),!!d.syncExecuteCommand(g.id,g.params)}};function $e(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 st={id:"doc.command.break-line",type:n.CommandType.COMMAND,handler:async s=>{var h;const o=s.get(exports.TextSelectionManagerService),e=s.get(n.IUniverInstanceService),t=s.get(n.ICommandService),r=o.getActiveRange();if(r==null)return!1;const{segmentId:a}=r,i=e.getCurrentUniverDocInstance(),c=i==null?void 0:i.getSelfOrHeaderFooterModel(a).getBody();if(!i||!c)return!1;const d=i.getUnitId(),{startOffset:u,endOffset:l}=Z(r,c),m=((h=c.paragraphs)!=null?h:[]).find(f=>f.startIndex>=u);if(m&&m.startIndex>l){const f=n.normalizeBody(n.getBodySlice(c,l,m.startIndex)),T={startOffset:u,endOffset:m.startIndex,collapsed:!1};return n.updateAttributeByInsert(f,{dataStream:n.DataStreamTreeTokenType.PARAGRAPH,paragraphs:$e(n.DataStreamTreeTokenType.PARAGRAPH,m)},1,0),await t.executeCommand(Y.id,{unitId:d,body:f,range:T,segmentId:a,cursorOffset:1})}else return await t.executeCommand(Y.id,{unitId:d,body:{dataStream:n.DataStreamTreeTokenType.PARAGRAPH,paragraphs:$e(n.DataStreamTreeTokenType.PARAGRAPH,m)},range:r,segmentId:a})}},Jt={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),c=s.get(n.IUniverInstanceService),d=i.getCurrentSelections();if(!Array.isArray(d)||d.length===0)return!1;const u=c.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}},h=new n.MemoryCursor;h.reset();const f=new n.TextX,T=n.JSONX.getInstance();for(const I of d){const{startOffset:C,endOffset:_,collapsed:R}=I,v=C-h.cursor;if(R)f.push({t:n.TextXActionType.RETAIN,len:v,segmentId:e});else{const{dos:y}=me(I,e,h.cursor,l);f.push(...y)}f.push({t:n.TextXActionType.INSERT,body:r,len:r.dataStream.length,line:0,segmentId:e}),h.reset(),h.moveCursor(_)}const S=w(u,e);return m.params.actions=T.editOp(f.serialize(),S),!!a.syncExecuteCommand(m.id,m.params)}},we={id:"doc.command.inner-cut",type:n.CommandType.COMMAND,handler:async(s,o)=>{var v,y,O;const{segmentId:e,textRanges:t}=o,r=s.get(n.ICommandService),a=s.get(exports.TextSelectionManagerService),i=s.get(n.IUniverInstanceService),c=a.getCurrentSelections();if(!Array.isArray(c)||c.length===0)return!1;const d=(v=i.getCurrentUniverDocInstance())==null?void 0:v.getUnitId();if(!d)return!1;const u=i.getUniverDocInstance(d),l=n.getDocsUpdateBody(u.getSnapshot(),e);if(u==null||l==null)return!1;const g={id:U.id,params:{unitId:d,actions:[],textRanges:t}},m=new n.MemoryCursor;m.reset();const h=new n.TextX,f=n.JSONX.getInstance(),T=[];for(const M of c){const{startOffset:x,endOffset:E,collapsed:D}=M;if(x==null||E==null)continue;const b=x-m.cursor;D?h.push({t:n.TextXActionType.RETAIN,len:b,segmentId:e}):h.push(...Yt(M,l,e,m.cursor)),m.reset(),m.moveCursor(E)}const S=w(u,e);T.push(f.editOp(h.serialize(),S));const p=Wt(l,c),I=(y=u.getDrawings())!=null?y:{},C=(O=u.getDrawingsOrder())!=null?O:[],_=p.sort((M,x)=>C.indexOf(M)>C.indexOf(x)?-1:C.indexOf(M)<C.indexOf(x)?1:0);if(_.length>0)for(const M of _){const x=I[M],E=C.indexOf(M);if(x==null||E<0)continue;const D=f.removeOp(["drawings",M],x),b=f.removeOp(["drawingsOrder",E],M);T.push(D),T.push(b)}return g.params.actions=T.reduce((M,x)=>n.JSONX.compose(M,x),null),!!r.syncExecuteCommand(g.id,g.params)}};function Wt(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 c of t){const{startIndex:d}=c;d>=a&&d<i&&e.push(c.blockId)}}return e}function Yt(s,o,e="",t=0){var p;const{startOffset:r,endOffset:a}=ue(s,o),i=[],{paragraphs:c=[],dataStream:d}=o,u=r-t,l=a-t,g=c==null?void 0:c.find(I=>I.startIndex-t>=u&&I.startIndex-t<=l),m=(p=o.customRanges)==null?void 0:p.filter(I=>Ae(I.startIndex,I.endIndex,r,a)),h=new Set(m==null?void 0:m.filter(I=>Ne(r,a-r,I,d))),f=new Set;if(m==null||m.forEach(I=>{h.has(I)||(I.startIndex-t>=u&&I.startIndex-t<=l&&I.endIndex-t>l&&f.add(I.startIndex),I.endIndex-t>=u&&I.endIndex-t<=l&&I.startIndex<u&&f.add(I.endIndex))}),u>0&&i.push({t:n.TextXActionType.RETAIN,len:u,segmentId:e}),g&&g.startIndex-t>u){const I=g.startIndex-t;f.add(I)}const T=[...f].sort((I,C)=>I-C);let S=u;return T.forEach(I=>{const C=I-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=I+1}),S<l&&i.push({t:n.TextXActionType.DELETE,len:l-S,line:0,segmentId:e}),i}const ge={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:c,range:d,unitId:u,drawingId:l}=o,{startOffset:g,segmentId:m,style:h}=a,f=c===P.LEFT?g-1:g,T=[{startOffset:f,endOffset:f,style:h}],S={id:U.id,params:{unitId:u,actions:[],textRanges:T,prevTextRanges:[d]}},p=new n.TextX,I=n.JSONX.getInstance(),C=[];g>0&&p.push({t:n.TextXActionType.RETAIN,len:c===P.LEFT?g-1:g,segmentId:m}),p.push({t:n.TextXActionType.DELETE,len:1,line:0,segmentId:m}),C.push(I.editOp(p.serialize()));const _=((x=i.getDrawings())!=null?x:{})[l],v=i.getDrawingsOrder().indexOf(l),y=I.removeOp(["drawings",l],_),O=I.removeOp(["drawingsOrder",v],l);return C.push(y),C.push(O),S.params.actions=C.reduce((E,D)=>n.JSONX.compose(E,D),null),!!r.syncExecuteCommand(S.id,S.params)}},fe={id:"doc.command.merge-two-paragraph",type:n.CommandType.COMMAND,handler:async(s,o)=>{var E,D;const e=s.get(exports.TextSelectionManagerService),t=s.get(n.IUniverInstanceService),r=s.get(n.ICommandService),{direction:a,range:i}=o,c=e.getActiveRange(),d=e.getCurrentSelections();if(c==null||d==null)return!1;const{segmentId:u,style:l}=c,g=t.getCurrentUniverDocInstance(),m=g==null?void 0:g.getSelfOrHeaderFooterModel(u).getBody();if(!g||!m)return!1;const h=ue(c,m),{startOffset:f,collapsed:T}=h;if(!T)return!1;const S=a===P.LEFT?f:f+1,p=(D=(E=m.paragraphs)==null?void 0:E.find(b=>b.startIndex>=S))==null?void 0:D.startIndex,I=Kt(m,S,p),C=a===P.LEFT?f-1:f,_=g.getUnitId(),R=[{startOffset:C,endOffset:C,style:l}],v={id:U.id,params:{unitId:_,actions:[],textRanges:R,prevTextRanges:[i]}},y=new n.TextX,O=n.JSONX.getInstance();y.push({t:n.TextXActionType.RETAIN,len:a===P.LEFT?f-1:f,segmentId:u}),I.dataStream.length&&y.push({t:n.TextXActionType.INSERT,body:I,len:I.dataStream.length,line:0,segmentId:u}),y.push({t:n.TextXActionType.RETAIN,len:1,segmentId:u}),y.push({t:n.TextXActionType.DELETE,len:p+1-S,line:0,segmentId:u});const M=w(g,u);return v.params.actions=O.editOp(y.serialize(),M),!!r.syncExecuteCommand(v.id,v.params)}},at={id:"doc.command.delete-left",type:n.CommandType.COMMAND,handler:async s=>{var O,M;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(),c=ot(s,i),d=o.getActiveRange(),u=o.getCurrentSelections(),l=c==null?void 0:c.getSkeleton();if(d==null||l==null||u==null)return!1;const{segmentId:g,style:m,segmentPage:h}=d,f=a.getSelfOrHeaderFooterModel(g).getBody();if(f==null)return!1;const T=ue(d,f),{startOffset:S,collapsed:p}=T,I=l.findNodeByCharIndex(S,g,h),C=N.hasListGlyph(I),_=N.isIndentByGlyph(I,f);let R=S;const v=l.findNodeByCharIndex(S-1,g,h);if(N.isFirstGlyph(I)&&v!==I&&(C===!0||_===!0)&&p){const x=N.getParagraphByGlyph(I,f);if(x==null)return!1;const E=x==null?void 0:x.startIndex,D={startIndex:0},b=x.paragraphStyle;if(C===!0){const F=x.paragraphStyle;if(F){D.paragraphStyle=F;const{hanging:X}=F;X&&(D.paragraphStyle.indentStart=X,D.paragraphStyle.hanging=void 0)}}else if(_===!0){const F=x.bullet;F&&(D.bullet=F),b!=null&&(D.paragraphStyle={...b},delete D.paragraphStyle.hanging,delete D.paragraphStyle.indentStart)}const ee=[{startOffset:R,endOffset:R,style:m}];r=await t.executeCommand(Pe.id,{unitId:a.getUnitId(),updateBody:{dataStream:"",paragraphs:[{...D}]},range:{startOffset:E,endOffset:E+1},textRanges:ee,coverType:n.UpdateDocsAttributeType.REPLACE,segmentId:g})}else if(p===!0){if(v==null)return!0;if(v.content==="\r")r=await t.executeCommand(fe.id,{direction:P.LEFT,range:T});else if(v.streamType==="\b"){const x=(M=a.getSnapshot().drawings)==null?void 0:M[(O=v.drawingId)!=null?O:""];if(x==null)return!0;if(x.layoutType===n.PositionedObjectLayoutType.INLINE){const D=a.getUnitId();r=await t.executeCommand(ge.id,{direction:P.LEFT,range:d,unitId:D,drawingId:v.drawingId})}else{const D=l.findNodeByCharIndex(S-2);if(D==null)return!0;R-=v.count,R-=D.count;const b=[{startOffset:R,endOffset:R,style:m}];r=await t.executeCommand(z.id,{unitId:a.getUnitId(),range:{...d,startOffset:d.startOffset-1,endOffset:d.endOffset-1},segmentId:g,direction:P.LEFT,len:D.count,textRanges:b})}}else R-=v.count,r=await t.executeCommand(z.id,{unitId:a.getUnitId(),range:T,segmentId:g,direction:P.LEFT,len:v.count})}else{const x=ct({...d,...T},u);r=await t.executeCommand(we.id,{segmentId:g,textRanges:x})}return r}},it={id:"doc.command.delete-right",type:n.CommandType.COMMAND,handler:async s=>{var I,C;const o=s.get(exports.TextSelectionManagerService),t=s.get(n.IUniverInstanceService).getCurrentUniverDocInstance();if(!t)return!1;const r=ot(s,t.getUnitId()),a=s.get(n.ICommandService),i=o.getActiveRange(),c=o.getCurrentSelections(),d=r==null?void 0:r.getSkeleton();if(i==null||d==null||c==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 h=Z(i,m),{startOffset:f,endOffset:T,collapsed:S}=h;if(f===m.dataStream.length-2&&S)return!0;let p=!1;if(S===!0){const _=d.findNodeByCharIndex(f,u,g),R=d.findNodeByCharIndex(f+1);if(_.content==="\r")p=await a.executeCommand(fe.id,{direction:P.RIGHT,range:i});else if(_.streamType==="\b"){const v=(C=t.getSnapshot().drawings)==null?void 0:C[(I=_.drawingId)!=null?I:""];if(v==null)return!0;if(v.layoutType===n.PositionedObjectLayoutType.INLINE){const O=t.getUnitId();p=await a.executeCommand(ge.id,{direction:P.RIGHT,range:i,unitId:O,drawingId:_.drawingId})}else{if(R==null)return!0;const O=[{startOffset:f+1,endOffset:f+1,style:l}];p=await a.executeCommand(z.id,{unitId:t.getUnitId(),range:{...i,startOffset:f+1,endOffset:T+1},segmentId:u,direction:P.RIGHT,textRanges:O,len:R.count})}}else{const v=[{startOffset:f,endOffset:f,style:l}];p=await a.executeCommand(z.id,{unitId:t.getUnitId(),range:h,segmentId:u,direction:P.RIGHT,textRanges:v,len:_.count})}}else{const _=ct(i,c);p=await a.executeCommand(we.id,{segmentId:u,textRanges:_})}return p}};function Kt(s,o,e){const{textRuns:t=[],customBlocks:r=[]}=s,i={dataStream:s.dataStream.substring(o,e),customRanges:n.getCustomRangeSlice(s,o,e).customRanges,customDecorations:n.getCustomDecorationSlice(s,o,e)},c=[];for(const u of t){const{st:l,ed:g}=u;g<=o||l>=e||(l<o?c.push({...u,st:0,ed:g-o}):g>e?c.push({...u,st:l-o,ed:e-o}):c.push({...u,st:l-o,ed:g-o}))}c.length>0&&(i.textRuns=c);const d=[];for(const u of r){const{startIndex:l}=u;l>=o&&l<=e&&d.push({...u,startIndex:l-o})}return d.length>0&&(i.customBlocks=d),i}function ct(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 Be={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,c=s.get(n.ICommandService),d=s.get(Q),l=s.get(n.IUniverInstanceService).getCurrentUniverDocInstance();if(l==null)return!1;const g=d.getActiveRange();if(!g)return!1;const{startOffset:m,style:h,segmentId:f}=g,T=l.getSelfOrHeaderFooterModel(f).getBody();if(T==null)return!1;const S=Z(g,T);Object.assign(g,S);const p=t.length,I=[{startOffset:m+p,endOffset:m+p,collapsed:!0,style:h}],C={id:U.id,params:{unitId:e,actions:[],textRanges:I}},_=new n.TextX,R=n.JSONX.getInstance();if(!g.collapsed&&i){const{dos:O,retain:M,cursor:x}=me(g,f,0,T);_.push(...O),C.params.textRanges=[{startOffset:m+p+M,endOffset:m+p+M,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=w(l,f);C.params.actions=R.editOp(_.serialize(),v),C.params.noHistory=!a,C.params.isCompositionEnd=a;const y=c.syncExecuteCommand(C.id,C.params);return d.pushUndoRedoMutationParams(y,C.params),!!y}};function B(s,o,e,t){var a;const{segmentId:r}=(a=e.getActiveRange())!=null?a:{};return r==null?!1:t.executeCommand(Xe.id,{segmentId:r,preCommandId:s,...o!=null?o:{}})}const ze="doc.command.set-inline-format-bold",Se={id:ze,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return B(ze,o,t,e)}},He="doc.command.set-inline-format-italic",pe={id:He,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return B(He,o,t,e)}},Ve="doc.command.set-inline-format-underline",Ie={id:Ve,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return B(Ve,o,t,e)}},Je="doc.command.set-inline-format-strikethrough",he={id:Je,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return B(Je,o,t,e)}},We="doc.command.set-inline-format-subscript",H={id:We,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return B(We,o,t,e)}},Ye="doc.command.set-inline-format-superscript",Te={id:Ye,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return B(Ye,o,t,e)}},Ke="doc.command.set-inline-format-fontsize",ve={id:Ke,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return B(Ke,o,t,e)}},Ze="doc.command.set-inline-format-font-family",Ce={id:Ze,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return B(Ze,o,t,e)}},qe="doc.command.set-inline-format-text-color",xe={id:qe,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return B(qe,o,t,e)}},Qe="doc.command.set-inline-format-text-background-color",_e={id:Qe,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return B(Qe,o,t,e)}},et="doc.command.reset-inline-format-text-background-color",ye={id:et,type:n.CommandType.COMMAND,handler:async(s,o)=>{const e=s.get(n.ICommandService),t=s.get(exports.TextSelectionManagerService);return B(et,o,t,e)}},dt={[Se.id]:"bl",[pe.id]:"it",[Ie.id]:"ul",[he.id]:"st",[ve.id]:"fs",[Ce.id]:"ff",[xe.id]:"cl",[_e.id]:"bg",[ye.id]:"bg",[H.id]:"va",[Te.id]:"va"},Xe={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),c=s.get(n.IUniverInstanceService),d=i.getCurrentSelections();if(!Array.isArray(d)||d.length===0)return!1;const u=c.getCurrentUniverDocInstance();if(u==null)return!1;const l=u.getUnitId();let g;switch(r){case Se.id:case pe.id:case Ie.id:case he.id:case H.id:case Te.id:{g=qt(u.getSelfOrHeaderFooterModel(e).getBody().textRuns,r,d);break}case ve.id:case Ce.id:{g=t;break}case xe.id:case _e.id:{g={rgb:t};break}case ye.id:{g={rgb:null};break}default:throw new Error(`Unknown command: ${r} in handleInlineFormat`)}const m={id:U.id,params:{unitId:l,actions:[],textRanges:d.map($)}},h=new n.TextX,f=n.JSONX.getInstance(),T=new n.MemoryCursor;T.reset();for(const I of d){const{startOffset:C,endOffset:_}=I,R={dataStream:"",textRuns:[{st:0,ed:_-C,ts:{[dt[r]]:g}}]},v=C-T.cursor;v!==0&&h.push({t:n.TextXActionType.RETAIN,len:v,segmentId:e}),h.push({t:n.TextXActionType.RETAIN,body:R,len:_-C,segmentId:e}),T.reset(),T.moveCursor(_)}const S=w(u,e);return m.params.actions=f.editOp(h.serialize(),S),!!a.syncExecuteCommand(m.id,m.params)}};function Zt(s){return s!==null&&typeof s=="object"}function qt(s,o,e){let t=0,r=0;const a=dt[o];for(;t!==s.length&&r!==e.length;){const{startOffset:i,endOffset:c}=e[r],{st:d,ed:u,ts:l}=s[t];if(c<=d)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 Zt(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===H.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===H.id?n.BaselineOffset.SUBSCRIPT:n.BaselineOffset.SUPERSCRIPT}const Oe={id:"doc.command.list-operation",type:n.CommandType.COMMAND,handler:(s,o)=>{var D,b,ee,F;const e=s.get(exports.TextSelectionManagerService),t=s.get(n.IUniverInstanceService),r=s.get(n.ICommandService),{listType:a}=o,i=t.getCurrentUniverDocInstance(),c=e.getActiveRange();if(i==null||c==null)return!1;const{segmentId:d}=c,u=(D=e.getCurrentSelections())!=null?D:[],l=(b=i.getSelfOrHeaderFooterModel(d).getBody())==null?void 0:b.paragraphs,g=u.map($);if(l==null)return!1;const m=mt(c,l),h=i.getUnitId(),f=m.every(X=>{var L;return((L=X.bullet)==null?void 0:L.listType)===a});let S=n.Tools.generateRandomId(6);if(m.length===1){const X=l.indexOf(m[0]),L=l[X-1],k=l[X+1];L&&L.bullet&&L.bullet.listType===a?S=L.bullet.listId:k&&k.bullet&&k.bullet.listType===a&&(S=k.bullet.listId)}const p={id:U.id,params:{unitId:h,actions:[],textRanges:g}},I=new n.MemoryCursor;I.reset();const C=new n.TextX,_=n.JSONX.getInstance(),R=(ee=i.getSnapshot().lists)!=null?ee:{},v={...n.PRESET_LIST_TYPE,...R},{charSpace:y,defaultTabStop:O=36,gridType:M}=i.getSnapshot().documentStyle;for(const X of m){const{startIndex:L,paragraphStyle:k={}}=X,{indentFirstLine:yt=0,snapToGrid:Ot,indentStart:Re=0}=k,{hanging:Me,indentStart:Le}=v[a].nestingLevel[0],De=N.getCharSpaceApply(y,O,M,Ot);C.push({t:n.TextXActionType.RETAIN,len:L-I.cursor,segmentId:d}),C.push({t:n.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[f?{paragraphStyle:{...k,hanging:void 0,indentStart:Re?Math.max(0,N.getNumberUnitValue(Re,De)+Me-Le):void 0},startIndex:0}:{startIndex:0,paragraphStyle:{...k,indentFirstLine:void 0,hanging:Me,indentStart:Le-Me+N.getNumberUnitValue(yt,De)+N.getNumberUnitValue(Re,De)},bullet:{...(F=X.bullet)!=null?F:{nestingLevel:0,textStyle:{fs:20}},listType:a,listId:S}}]},segmentId:d,coverType:n.UpdateDocsAttributeType.REPLACE}),I.moveCursorTo(L+1)}const x=w(i,d);return p.params.actions=_.editOp(C.serialize(),x),!!r.syncExecuteCommand(p.id,p.params)}},lt={id:"doc.command.bullet-list",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(Oe.id,{listType:n.PresetListType.BULLET_LIST})},ut={id:"doc.command.order-list",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(Oe.id,{listType:n.PresetListType.ORDER_LIST})};function mt(s,o){const{startOffset:e,endOffset:t}=s,r=[];let a=-1;for(const i of o){const{startIndex:c}=i;(e>a&&e<=c||t>a&&t<=c||c>=e&&c<=t)&&r.push(i),a=c}return r}const gt={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,c=s.get(n.IUniverInstanceService),d=s.get(n.ICommandService),u=s.get(exports.TextSelectionManagerService),l=c.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 h=St(e,a,l,g,t);return h.params.textRanges=r,i&&(h.params.options=i),!!d.syncExecuteCommand(h.id,h.params)}},ft={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),c=s.get(n.IUndoRedoService),d=a.getUniverDocInstance(e),u=d==null?void 0:d.getSnapshot().body;if(d==null||u==null)return!1;const l=St(e,r,d,u,t);return l.params.noNeedSetTextRange=!0,l.params.noHistory=!0,i.syncExecuteCommand(l.id,l.params),c.clearUndoRedo(e),!0}};function St(s,o,e,t,r){const a={id:U.id,params:{unitId:s,actions:[],textRanges:[]}},i=new n.TextX,c=n.JSONX.getInstance(),d=(t==null?void 0:t.dataStream.length)-2;d>0&&i.push({t:n.TextXActionType.DELETE,len:d,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=c.editOp(i.serialize(),u),a}const Qt=(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}},W={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}},pt={type:n.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(s,o)=>{var g,m,h;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=(h=o.zoomRatio)!=null?h:i),!r.getUniverDocInstance(a))return!1;const d={zoomRatio:i,unitId:a},u=Qt(s,d);return e.syncExecuteCommand(W.id,d)?(t.pushUndoRedo({unitID:a,undoMutations:[{id:W.id,params:u}],redoMutations:[{id:W.id,params:d}]}),!0):!1}},de={id:"doc.operation.move-cursor",type:n.CommandType.OPERATION,handler:(s,o)=>!!o},le={id:"doc.operation.move-selection",type:n.CommandType.OPERATION,handler:(s,o)=>!!o},It={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 c=[{startOffset:0,endOffset:i.dataStream.length-2}];return e.replaceTextRanges(c,!1),!0}};var en=Object.defineProperty,tn=Object.getOwnPropertyDescriptor,nn=(s,o,e,t)=>{for(var r=t>1?void 0:t?tn(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&&en(o,e,r),r},J=(s,o)=>(e,t)=>o(e,t,s);let re=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 d=a.data;d===this._previousIMEContent&&e||(await this._commandService.executeCommand(Be.id,{unitId:t.getUnitId(),newText:d,oldTextLen:this._previousIMEContent.length,isCompositionStart:this._isCompositionStart,isCompositionEnd:!e}),e?(this._isCompositionStart&&(this._isCompositionStart=!1),this._previousIMEContent=d):this._resetIME())}_resetIME(){this._previousIMEContent="",this._isCompositionStart=!0,this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(null)}};re=nn([n.OnLifecycle(n.LifecycleStages.Rendered,re),J(0,n.IUniverInstanceService),J(1,N.IRenderManagerService),J(2,N.ITextSelectionRenderManager),J(3,j.Inject(Q)),J(4,n.ICommandService)],re);var on=Object.defineProperty,rn=Object.getOwnPropertyDescriptor,sn=(s,o,e,t)=>{for(var r=t>1?void 0:t?rn(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&&on(o,e,r),r},te=(s,o)=>(e,t)=>o(e,t,s);let se=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=[de.id,le.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(!o.includes(e.id))return;const t=e.params;switch(e.id){case de.id:return this._handleMoveCursor(t.direction);case le.id:return this._handleShiftMoveSelection(t.direction);default:throw new Error("Unknown command")}}))}_handleShiftMoveSelection(o){var C,_,R;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getCurrentSelections(),r=this._univerInstanceService.getCurrentUniverDocInstance();if(!r)return;const a=(C=this._renderManagerService.getRenderById(r.getUnitId()))==null?void 0:C.with(exports.DocSkeletonManagerService).getSkeleton(),i=this._getDocObject();if(e==null||a==null||i==null)return;const{startOffset:c,endOffset:d,style:u,collapsed:l,direction:g,segmentId:m,startNodePosition:h,endNodePosition:f,segmentPage:T}=e;if(t.length>1){let v=Number.POSITIVE_INFINITY,y=Number.NEGATIVE_INFINITY;for(const O of t)v=Math.min(v,O.startOffset),y=Math.max(y,O.endOffset);this._textSelectionManagerService.replaceTextRanges([{startOffset:o===n.Direction.LEFT||o===n.Direction.UP?y:v,endOffset:o===n.Direction.LEFT||o===n.Direction.UP?v:y,style:u}],!1);return}const S=l||g===N.RANGE_DIRECTION.FORWARD?c:d;let p=l||g===N.RANGE_DIRECTION.FORWARD?d:c;const I=(_=r.getSelfOrHeaderFooterModel(m).getBody().dataStream.length)!=null?_:Number.POSITIVE_INFINITY;if(o===n.Direction.LEFT||o===n.Direction.RIGHT){const v=a.findNodeByCharIndex(p-1,m,T),y=a.findNodeByCharIndex(p,m,T);p=o===n.Direction.RIGHT?p+y.count:p-((R=v==null?void 0:v.count)!=null?R:0),p=Math.min(I-2,Math.max(0,p)),this._textSelectionManagerService.replaceTextRanges([{startOffset:S,endOffset:p,style:u}],!1)}else{const v=a.findNodeByCharIndex(p,m,T),y=i.document.getOffsetConfig(),O=l?h:g===N.RANGE_DIRECTION.FORWARD?f:h,M=this._getTopOrBottomPosition(a,v,O,o===n.Direction.DOWN);if(M==null){const E=o===n.Direction.UP?0:I-2;if(E===p)return;this._textSelectionManagerService.replaceTextRanges([{startOffset:S,endOffset:E,style:u}],!1);return}const x=new N.NodePositionConvertToCursor(y,a).getRangePointData(M,M).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{startOffset:S,endOffset:x.endOffset,style:u}],!1)}}_handleMoveCursor(o){var S,p,I;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getCurrentSelections(),r=this._univerInstanceService.getCurrentUniverDocInstance();if(!r)return!1;const a=(S=this._renderManagerService.getRenderById(r.getUnitId()))==null?void 0:S.with(exports.DocSkeletonManagerService).getSkeleton(),i=this._getDocObject();if(e==null||a==null||i==null||t==null)return;const{startOffset:c,endOffset:d,style:u,collapsed:l,segmentId:g,startNodePosition:m,endNodePosition:h,segmentPage:f}=e,T=(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,R=Number.NEGATIVE_INFINITY;for(const v of t)_=Math.min(_,v.startOffset),R=Math.max(R,v.endOffset);C=o===n.Direction.LEFT?_:R}else{const _=a.findNodeByCharIndex(c-1,g,f),R=a.findNodeByCharIndex(c,g,f);o===n.Direction.LEFT?C=Math.max(0,c-((I=_==null?void 0:_.count)!=null?I:0)):C=Math.min(T-2,d+R.count)}this._textSelectionManagerService.replaceTextRanges([{startOffset:C,endOffset:C,style:u}],!1)}else{const C=a.findNodeByCharIndex(c,g,f),_=a.findNodeByCharIndex(d,g,f),R=i.document.getOffsetConfig(),v=this._getTopOrBottomPosition(a,o===n.Direction.UP?C:_,o===n.Direction.UP?m:h,o===n.Direction.DOWN);if(v==null){let O;l?O=o===n.Direction.UP?0:T-2:O=o===n.Direction.UP?c:d,this._textSelectionManagerService.replaceTextRanges([{startOffset:O,endOffset:O,style:u}],!1);return}const y=new N.NodePositionConvertToCursor(R,a).getRangePointData(v,v).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{...y,style:u}],!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 c=this._matchPositionByLeftOffset(o,i,a,t);if(c!=null)return{...c,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 c of e.divides){const d=c.left;for(const u of c.glyphGroup){const{left:l}=u,g=d+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 f,T,S,p,I,C,_,R,v,y,O,M;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 c;if(e===!0?c=a.lines[i+1]:c=a.lines[i-1],c!=null)return c;const d=a.parent;if(d==null)return;const u=d.columns.indexOf(a);if(u===-1)return;if(e===!0)c=(f=d.columns[u+1])==null?void 0:f.lines[0];else{const x=(S=(T=d.columns)==null?void 0:T[u-1])==null?void 0:S.lines;c=x==null?void 0:x[x.length-1]}if(c!=null)return c;const l=d.parent;if(l==null)return;const g=l.sections.indexOf(d);if(g===-1)return;if(e===!0)c=(I=(p=l.sections[g-1])==null?void 0:p.columns[0])==null?void 0:I.lines[0];else{const x=(_=(C=l.sections)==null?void 0:C[g-1])==null?void 0:_.columns,E=x==null?void 0:x[x.length-1],D=E==null?void 0:E.lines;c=D==null?void 0:D[D.length-1]}if(c!=null)return c;const m=l.parent;if(m==null)return;const h=m.pages.indexOf(l);if(h!==-1){if(e===!0)c=(y=(v=(R=m.pages[h+1])==null?void 0:R.sections[0])==null?void 0:v.columns[0])==null?void 0:y.lines[0];else{const x=(O=m.pages[h-1])==null?void 0:O.sections;if(x==null)return;const E=(M=x[x.length-1])==null?void 0:M.columns,D=E[E.length-1],b=D==null?void 0:D.lines;c=b[b.length-1]}if(c!=null)return c}}_getDocObject(){return nt(this._univerInstanceService,this._renderManagerService)}};se=sn([n.OnLifecycle(n.LifecycleStages.Rendered,se),te(0,n.IUniverInstanceService),te(1,N.IRenderManagerService),te(2,j.Inject(exports.TextSelectionManagerService)),te(3,n.ICommandService)],se);var an=Object.defineProperty,cn=Object.getOwnPropertyDescriptor,dn=(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&&an(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._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,c=r,d=(T=this._renderManagerService.getRenderById(e.getUnitId()))==null?void 0:T.with(exports.DocSkeletonManagerService).getSkeleton();if(c.data==null||d==null||!d||!i)return;const{startOffset:u,segmentId:l,style:g,segmentPage:m}=i,h=a.length,f=[{startOffset:u+h,endOffset:u+h,segmentId:l,segmentPage:m,style:g}];await this._commandService.executeCommand(Y.id,{unitId:t,body:{dataStream:a},textRanges:f,range:i,segmentId:l})})}};ae=dn([n.OnLifecycle(n.LifecycleStages.Rendered,ae),ne(0,n.IUniverInstanceService),ne(1,N.IRenderManagerService),ne(2,N.ITextSelectionRenderManager),ne(3,n.ICommandService)],ae);const V={id:"doc.command.align-operation",type:n.CommandType.COMMAND,handler:(s,o)=>{var R,v;const e=s.get(exports.TextSelectionManagerService),t=s.get(n.IUniverInstanceService),r=s.get(n.ICommandService),{alignType:a}=o,i=t.getCurrentUniverDocInstance(),c=e.getActiveRange();if(i==null||c==null)return!1;const{segmentId:d}=c,u=(R=e.getCurrentSelections())!=null?R:[],l=(v=i.getSelfOrHeaderFooterModel(d).getBody())==null?void 0:v.paragraphs,g=u.map($);if(l==null)return!1;const m=mt(c,l),h=i.getUnitId(),f=m.every(y=>{var O;return((O=y.paragraphStyle)==null?void 0:O.horizontalAlign)===a}),T={id:U.id,params:{unitId:h,actions:[],textRanges:g}},S=new n.MemoryCursor;S.reset();const p=new n.TextX,I=n.JSONX.getInstance();for(const y of m){const{startIndex:O}=y;p.push({t:n.TextXActionType.RETAIN,len:O-S.cursor,segmentId:d});const M={...y.paragraphStyle,horizontalAlign:f?n.HorizontalAlign.UNSPECIFIED:a};p.push({t:n.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[{...y,paragraphStyle:M,startIndex:0}]},segmentId:d,coverType:n.UpdateDocsAttributeType.REPLACE}),S.moveCursorTo(O+1)}const C=w(i,d);return T.params.actions=I.editOp(p.serialize(),C),!!r.syncExecuteCommand(T.id,T.params)}},ht={id:"doc.command.align-left",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(V.id,{alignType:n.HorizontalAlign.LEFT})},Tt={id:"doc.command.align-center",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(V.id,{alignType:n.HorizontalAlign.CENTER})},vt={id:"doc.command.align-right",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(V.id,{alignType:n.HorizontalAlign.RIGHT})},Ct={id:"doc.command.align-justify",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(V.id,{alignType:n.HorizontalAlign.JUSTIFIED})};class xt{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){let e={...o};return this._customRangeHooks.forEach(t=>{t.onCopyCustomRange&&(e=t.onCopyCustomRange(e))}),e}}var ln=Object.defineProperty,un=Object.getOwnPropertyDescriptor,mn=(s,o,e,t)=>{for(var r=t>1?void 0:t?un(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&&ln(o,e,r),r},gn=(s,o)=>(e,t)=>o(e,t,s);const fn="docs";var oe;exports.UniverDocsPlugin=(oe=class extends n.Plugin{constructor(o={},e){super(),this._injector=e,this._initializeDependencies(e),this._initializeCommands()}_initializeCommands(){[de,le,at,it,Se,pe,Ie,he,H,Te,ve,Ce,xe,ye,_e,Xe,st,Y,z,ge,Pe,Be,fe,U,gt,ft,pt,W,be,It,ut,lt,Oe,ht,Tt,vt,V,Ct].forEach(o=>{this._injector.get(n.ICommandService).registerCommand(o)})}_initializeDependencies(o){[[exports.DocStateChangeManagerService],[Q],[N.ITextSelectionRenderManager,{useClass:N.TextSelectionRenderManager}],[exports.TextSelectionManagerService],[xt],[ae],[re],[se]].forEach(e=>o.add(e))}},A(oe,"pluginName",fn),A(oe,"type",n.UniverInstanceType.UNIVER_DOC),oe);exports.UniverDocsPlugin=mn([gn(1,j.Inject(j.Injector))],exports.UniverDocsPlugin);function Sn(s,o){const{range:e,rangeId:t,rangeType:r,segmentId:a}=s,i=Dt(e,o);if(!i)return null;const{startOffset:c,endOffset:d}=i,u=new n.TextX;return c>0&&u.push({t:n.TextXActionType.RETAIN,len:c,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:d-c,segmentId:a}),u.push({t:n.TextXActionType.INSERT,body:{dataStream:n.DataStreamTreeTokenType.CUSTOM_RANGE_END,customRanges:[{rangeId:t,rangeType:r,startIndex:-(d-c)-1,endIndex:0}]},len:1,line:0}),u}function pn(s,o){const e={id:U.id,params:{unitId:s.unitId,actions:[],textRanges:void 0}},t=n.JSONX.getInstance(),r=Sn(s,o);return r?(e.params.actions=t.editOp(r.serialize()),e):!1}function In(s,o){var R;const{segmentId:e,rangeId:t,rangeType:r}=o,a=s.get(exports.TextSelectionManagerService),i=s.get(n.IUniverInstanceService),c=a.getActiveRange();if(!c)return!1;const d=i.getCurrentUnitForType(n.UniverInstanceType.UNIVER_DOC);if(!d)return!1;const u=d.getBody(),l=d.getUnitId();if(!u)return!1;const{startOffset:g,endOffset:m}=K(c),h=(R=u.customRanges)!=null?R:[],f=[];for(let v=0,y=h.length;v<y;v++){const O=h[v];if(O.rangeType===r&&Math.max(O.startIndex,g)<=Math.min(O.endIndex,m-1)&&f.push({...O}),O.startIndex>=m)break}const T=f.map(v=>[v.startIndex,v.endIndex]).flat().sort((v,y)=>v-y);let S=0;const p=new n.TextX,I=T.length?{startOffset:Math.min(T[0],g),endOffset:Math.max(T[T.length-1]+1,m)}:c;I.startOffset!==S&&(p.push({t:n.TextXActionType.RETAIN,len:I.startOffset-S,segmentId:e}),S=I.startOffset),p.push({t:n.TextXActionType.INSERT,body:{dataStream:n.DataStreamTreeTokenType.CUSTOM_RANGE_START},len:1,line:0,segmentId:e}),T.forEach((v,y)=>{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!==I.endOffset&&(p.push({t:n.TextXActionType.RETAIN,len:I.endOffset-S,segmentId:e}),S=I.endOffset),p.push({t:n.TextXActionType.INSERT,body:{dataStream:n.DataStreamTreeTokenType.CUSTOM_RANGE_END,customRanges:[{rangeId:t,rangeType:r,startIndex:-(I.endOffset-I.startOffset-T.length+1),endIndex:0}]},len:1,line:0,segmentId:e});const C=n.JSONX.getInstance(),_={id:U.id,params:{unitId:l,actions:[],textRanges:void 0}};return _.params.actions=C.editOp(p.serialize()),_}function hn(s,o){var m,h;const{unitId:e,rangeId:t,segmentId:r}=o,i=s.get(n.IUniverInstanceService).getUnit(e);if(!i)return!1;const c=(h=(m=i.getBody())==null?void 0:m.customRanges)==null?void 0:h.find(f=>f.rangeId===t);if(!c)return!1;const{startIndex:d,endIndex:u}=c,l=new n.TextX,g=u-d+1;return d>0&&l.push({t:n.TextXActionType.RETAIN,len:d,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 Tn(s,o){const e={id:U.id,params:{unitId:o.unitId,actions:[],textRanges:void 0}},t=n.JSONX.getInstance(),r=hn(s,o);return r?(e.params.actions=t.editOp(r.serialize()),e):!1}function _t(s){const{unitId:o,range:e,id:t,type:r,segmentId:a}=s,{startOffset:i,endOffset:c}=e,d={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:c-i-1}]},len:c-i,segmentId:a}),d.params.actions=l.editOp(u.serialize()),d}function vn(s,o){const{segmentId:e,id:t,type:r}=o,a=s.get(exports.TextSelectionManagerService),i=s.get(n.IUniverInstanceService),c=a.getActiveRange();if(!c)return!1;const d=i.getCurrentUnitForType(n.UniverInstanceType.UNIVER_DOC);if(!d)return!1;const u=d.getBody(),l=d.getUnitId();return u?_t({unitId:l,range:{startOffset:c.startOffset,endOffset:c.endOffset,collapsed:!0},id:t,type:r,segmentId:e}):!1}function Cn(s,o){var T,S;const{unitId:e,id:t,segmentId:r}=o,i=s.get(n.IUniverInstanceService).getUnit(e),c=i==null?void 0:i.getBody();if(!i||!c)return!1;const d=(S=(T=i.getBody())==null?void 0:T.customDecorations)==null?void 0:S.filter(p=>p.id===t);if(!(d!=null&&d.length))return!1;const u=d.map(p=>n.getBodySlice(c,p.startIndex,p.endIndex+1)),l=u.map(p=>{var C;const I=n.Tools.deepClone(p);return I.customDecorations=(C=I.customDecorations)==null?void 0:C.filter(_=>_.id!==t),I}),g={id:U.id,params:{unitId:e,actions:[],textRanges:void 0}},m=new n.TextX,h=n.JSONX.getInstance();let f=0;return d.forEach((p,I)=>{const C=l[I],_=u[I];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=h.editOp(m.serialize()),g}const xn=n.createInterceptorKey("CUSTOM_RANGE"),_n=n.createInterceptorKey("CUSTOM_DECORATION"),ie={CUSTOM_RANGE:xn,CUSTOM_DECORATION:_n};var yn=Object.defineProperty,On=Object.getOwnPropertyDescriptor,Rn=(s,o,e,t)=>{for(var r=t>1?void 0:t?On(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&&yn(o,e,r),r},Mn=(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(ie.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,c)=>{var d,u;return((d=c.priority)!=null?d: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(ie.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(ie.CUSTOM_DECORATION)(e.getCustomDecorationRaw(r),{index:r,unitId:e.getDataModel().getUnitId(),customDecorations:(a=e.getDataModel().getCustomDecorations())!=null?a:[]})}})),t}};exports.DocInterceptorService=Rn([n.OnLifecycle(n.LifecycleStages.Starting,exports.DocInterceptorService),Mn(1,j.Inject(exports.DocSkeletonManagerService))],exports.DocInterceptorService);exports.AlignCenterCommand=Tt;exports.AlignJustifyCommand=Ct;exports.AlignLeftCommand=ht;exports.AlignOperationCommand=V;exports.AlignRightCommand=vt;exports.BreakLineCommand=st;exports.BulletListCommand=lt;exports.CoverContentCommand=ft;exports.CutContentCommand=we;exports.DOCS_COMPONENT_BACKGROUND_LAYER_INDEX=Nt;exports.DOCS_COMPONENT_DEFAULT_Z_INDEX=Pt;exports.DOCS_COMPONENT_HEADER_LAYER_INDEX=bt;exports.DOCS_COMPONENT_MAIN_LAYER_INDEX=Ut;exports.DOCS_VIEW_KEY=q;exports.DOC_INTERCEPTOR_POINT=ie;exports.DeleteCommand=z;exports.DeleteCustomBlockCommand=ge;exports.DeleteLeftCommand=at;exports.DeleteRightCommand=it;exports.DocCustomRangeService=xt;exports.EditorInsertTextCommandId=rt;exports.IMEInputCommand=Be;exports.IMEInputManagerService=Q;exports.InnerPasteCommand=Jt;exports.InsertCommand=Y;exports.ListOperationCommand=Oe;exports.MergeTwoParagraphCommand=fe;exports.MoveCursorOperation=de;exports.MoveSelectionOperation=le;exports.NORMAL_TEXT_SELECTION_PLUGIN_NAME=wt;exports.OrderListCommand=ut;exports.ReplaceContentCommand=gt;exports.ResetInlineFormatTextBackgroundColorCommand=ye;exports.RichTextEditingMutation=U;exports.SelectAllOperation=It;exports.SetDocZoomRatioCommand=pt;exports.SetDocZoomRatioOperation=W;exports.SetInlineFormatBoldCommand=Se;exports.SetInlineFormatCommand=Xe;exports.SetInlineFormatFontFamilyCommand=Ce;exports.SetInlineFormatFontSizeCommand=ve;exports.SetInlineFormatItalicCommand=pe;exports.SetInlineFormatStrikethroughCommand=he;exports.SetInlineFormatSubscriptCommand=H;exports.SetInlineFormatSuperscriptCommand=Te;exports.SetInlineFormatTextBackgroundColorCommand=_e;exports.SetInlineFormatTextColorCommand=xe;exports.SetInlineFormatUnderlineCommand=Ie;exports.SetTextSelectionsOperation=be;exports.UpdateCommand=Pe;exports.VIEWPORT_KEY=tt;exports.addCustomDecorationBySelectionFactory=vn;exports.addCustomDecorationFactory=_t;exports.addCustomRangeBySelectionFactory=In;exports.addCustomRangeFactory=pn;exports.deleteCustomDecorationFactory=Cn;exports.deleteCustomRangeFactory=Tn;exports.getDeleteSelection=ue;exports.getDocObject=nt;exports.getDocObjectById=Xt;exports.getInsertSelection=Z;exports.getRetainAndDeleteFromReplace=me;exports.getRichTextEditPath=w;exports.getSelectionText=At;exports.neoGetDocObject=Bt;exports.serializeTextRange=$;