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