@univerjs/docs 0.1.0-alpha.2 → 0.1.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @univerjs/docs
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/@univerjs/docs)](https://npmjs.org/packages/@univerjs/docs)
3
+ [![npm version](https://img.shields.io/npm/v/@univerjs/docs)](https://npmjs.org/package/@univerjs/docs)
4
4
  [![license](https://img.shields.io/npm/l/@univerjs/docs)](https://img.shields.io/npm/l/@univerjs/docs)
5
5
 
6
6
  ## Introduction
package/lib/cjs/index.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var ct=Object.defineProperty;var lt=(i,e,t)=>e in i?ct(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var S=(i,e,t)=>(lt(i,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@univerjs/core"),R=require("@univerjs/ui"),De=require("rxjs"),v=require("@univerjs/engine-render"),O=require("@wendellhu/redi"),Ke=require("@ctrl/tinycolor");function H(i,e){const n=i.getCurrentUniverDocInstance().getUnitId(),r=e.getRenderById(n);if(r==null)return;const{mainComponent:o,scene:s,engine:c}=r;return{document:o,scene:s,engine:c}}function dt(i,e){const t=e.getRenderById(i);if(t==null)return;const{components:n,mainComponent:r,scene:o,engine:s}=t;return{document:r,scene:o,engine:s}}var xe=(i=>(i.MAIN="__Document_Render_Main__",i))(xe||{}),G=(i=>(i.VIEW_MAIN="viewMain",i.VIEW_TOP="viewTop",i.VIEW_LEFT="viewLeft",i.VIEW_LEFT_TOP="viewLeftTop",i))(G||{});const Ae=0,et=2,tt=10,J="normalTextSelectionPluginName",de={id:R.CopyCommand.id,name:"doc.command.copy",type:a.CommandType.COMMAND,multi:!0,priority:999,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.FOCUSING_EDITOR),handler:async()=>!0},ue={id:R.CutCommand.id,name:"doc.command.cut",type:a.CommandType.COMMAND,multi:!0,priority:999,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.FOCUSING_EDITOR),handler:async()=>!0},me={id:R.PasteCommand.id,name:"doc.command.paste",type:a.CommandType.COMMAND,multi:!0,priority:999,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.FOCUSING_EDITOR),handler:async()=>!0};function oe(i,e="",t=0){const{startOffset:n,endOffset:r}=i,o=[],s=n-t,c=r-t;return s>0&&o.push({t:"r",len:s,segmentId:e}),o.push({t:"d",len:c-s,line:0,segmentId:e}),o}class x{constructor(){S(this,"_textSelectionInfo",new Map);S(this,"_currentSelection",null);S(this,"_textSelectionInfo$",new De.BehaviorSubject(null));S(this,"textSelectionInfo$",this._textSelectionInfo$.asObservable())}getCurrentSelection(){return this._currentSelection}dispose(){this._textSelectionInfo$.complete()}refreshSelection(){this._currentSelection!=null&&this._refresh(this._currentSelection)}setCurrentSelection(e){this._currentSelection=e,this._refresh(e)}setCurrentSelectionNotRefresh(e){this._currentSelection=e}getTextSelectionInfo(){return this._textSelectionInfo}getTextRangesByParam(e){return this._getTextRanges(e)}getSelections(){return this._getTextRanges(this._currentSelection)}getFirst(){return this._getFirstByParam(this._currentSelection)}getLast(){return this._getLastByParam(this._currentSelection)}add(e){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:e})}replaceTextRanges(e){this._currentSelection!=null&&this._textSelectionInfo$.next(e)}replaceTextRangesWithNoRefresh(e){this._currentSelection!=null&&this._replaceByParam({...this._currentSelection,textRanges:e})}_addByParam(e){const{pluginName:t,unitId:n,textRanges:r}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map);const o=this._textSelectionInfo.get(t);o.has(n)?o.get(n).push(...r):o.set(n,[...r]),this._refresh({pluginName:t,unitId:n})}_clear(){this._currentSelection!=null&&this._clearByParam(this._currentSelection)}_remove(e){this._currentSelection!=null&&this._removeByParam(e,this._currentSelection)}_reset(){var e;this._currentSelection!=null&&(this._currentSelection={pluginName:J,unitId:(e=this._currentSelection)==null?void 0:e.unitId},this._textSelectionInfo.clear(),this._refresh(this._currentSelection))}_resetPlugin(){this._currentSelection!=null&&(this._currentSelection.pluginName=J,this._refresh(this._currentSelection))}_getTextRanges(e){var r;if(e==null)return;const{pluginName:t,unitId:n}=e;return(r=this._textSelectionInfo.get(t))==null?void 0:r.get(n)}_refresh(e){const t=this._getTextRanges(e)??[];this._textSelectionInfo$.next(t.map(n=>{const r=n.startOffset,o=n.endOffset,s=n.collapsed,c=n.style;return{startOffset:r,endOffset:o,collapsed:s,style:c}}))}_getFirstByParam(e){const t=this._getTextRanges(e);return t==null?void 0:t[0]}_getLastByParam(e){const t=this._getTextRanges(e);return t==null?void 0:t[t.length-1]}_replaceByParam(e){const{pluginName:t,unitId:n,textRanges:r}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map),this._textSelectionInfo.get(t).set(n,r)}_clearByParam(e){const t=this._getTextRanges(e);t==null||t.splice(0),this._refresh(e)}_removeByParam(e,t){const n=this._getTextRanges(t);n==null||n.splice(e,1),this._refresh(t)}}var ut=Object.defineProperty,mt=Object.getOwnPropertyDescriptor,ht=(i,e,t,n)=>{for(var r=n>1?void 0:n?mt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&ut(e,t,r),r},gt=(i,e)=>(t,n)=>e(t,n,i);exports.DocViewModelManagerService=class{constructor(e){S(this,"_currentViewModelUnitId","");S(this,"_docViewModelMap",new Map);S(this,"_currentDocViewModel$",new De.BehaviorSubject(null));S(this,"currentDocViewModel$",this._currentDocViewModel$.asObservable());this._currentUniverService=e}dispose(){this._currentDocViewModel$.complete(),this._docViewModelMap=new Map}getCurrent(){return this._docViewModelMap.get(this._currentViewModelUnitId)}getViewModel(e){var t;return(t=this._docViewModelMap.get(e))==null?void 0:t.docViewModel}setCurrent(e){var n;const t=this._currentUniverService.getUniverDocInstance(e);if(t==null)throw new Error(`Document data model with id ${e} not found when build view model.`);if(t.getBody()!=null){if(!this._docViewModelMap.has(e)){const r=this._buildDocViewModel(t);this._docViewModelMap.set(e,{unitId:e,docViewModel:r})}if(e===a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY){const r=(n=this._docViewModelMap.get(e))==null?void 0:n.docViewModel;if(r==null)return;r.reset(t)}this._currentViewModelUnitId=e,this._currentDocViewModel$.next(this.getCurrent())}}_buildDocViewModel(e){return new v.DocumentViewModel(e)}};exports.DocViewModelManagerService=ht([gt(0,a.IUniverInstanceService)],exports.DocViewModelManagerService);const I={id:"doc.mutation.rich-text-editing",type:a.CommandType.MUTATION,handler:(i,e)=>{const{unitId:t,mutations:n}=e,o=i.get(a.IUniverInstanceService).getUniverDocInstance(t),c=i.get(exports.DocViewModelManagerService).getViewModel(t);if(!o)throw new Error(`DocumentDataModel not found for unitId: ${t}`);const l=new a.MemoryCursor;l.reset(),n.forEach(u=>{const{segmentId:p,len:h}=u,m=c==null?void 0:c.getSelfOrHeaderFooterViewModel(p);if(u.t==="r"){const{len:g}=u;l.moveCursor(g)}else if(u.t==="i"){const{body:g}=u;if(g.dataStream.length>1&&/\r/.test(g.dataStream)){const f=o.getSelfOrHeaderFooterModel(p);m.reset(f)}else m.insert(g,l.cursor);l.moveCursor(h)}else if(u.t==="d")m.delete(l.cursor,h);else throw new Error(`Unknown mutation type for mutation: ${u}.`)});const d=o.apply(n);return{unitId:t,mutations:d}}},Q={id:"doc.command.delete-left",type:a.CommandType.COMMAND,handler:async()=>!0},be={id:"doc.command.delete-right",type:a.CommandType.COMMAND,handler:async()=>!0},Ue={id:"doc.command.break-line",type:a.CommandType.COMMAND,handler:async()=>!0},Ne={id:"doc.command.insert-text",type:a.CommandType.COMMAND,handler:async(i,e)=>{const t=i.get(a.IUndoRedoService),n=i.get(a.ICommandService),r=i.get(x),{range:o,segmentId:s,body:c,unitId:l,textRanges:d}=e,{startOffset:u,collapsed:p}=o,h={id:I.id,params:{unitId:l,mutations:[]}};p?h.params.mutations.push({t:"r",len:u,segmentId:s}):h.params.mutations.push(...oe(o,s)),h.params.mutations.push({t:"i",body:c,len:c.dataStream.length,line:0,segmentId:s});const m=n.syncExecuteCommand(h.id,h.params);return r.replaceTextRanges(d),m?(t.pushUndoRedo({unitID:l,undoMutations:[{id:I.id,params:m}],redoMutations:[{id:I.id,params:h.params}],undo(){return n.syncExecuteCommand(I.id,m),r.replaceTextRanges([o]),!0},redo(){return n.syncExecuteCommand(I.id,h.params),r.replaceTextRanges(d),!0}}),!0):!1}};var $e=(i=>(i[i.LEFT=0]="LEFT",i[i.RIGHT=1]="RIGHT",i))($e||{});const Te={id:"doc.command.delete-text",type:a.CommandType.COMMAND,handler:async(i,e)=>{const t=i.get(a.ICommandService),n=i.get(a.IUndoRedoService),r=i.get(x),{range:o,segmentId:s,unitId:c,direction:l,textRanges:d,len:u=1}=e,{collapsed:p,startOffset:h}=o,m={id:I.id,params:{unitId:c,mutations:[]}};p?(h>0&&m.params.mutations.push({t:"r",len:l===0?h-u:h,segmentId:s}),m.params.mutations.push({t:"d",len:u,line:0,segmentId:s})):m.params.mutations.push(...oe(o,s));const g=t.syncExecuteCommand(m.id,m.params);return r.replaceTextRanges(d),g&&n.pushUndoRedo({unitID:c,undoMutations:[{id:I.id,params:g}],redoMutations:[{id:I.id,params:m.params}],undo(){return t.syncExecuteCommand(I.id,g),r.replaceTextRanges([o]),!0},redo(){return t.syncExecuteCommand(I.id,m.params),r.replaceTextRanges(d),!0}}),!1}},Fe={id:"doc.command.update-text",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{range:t,segmentId:n,updateBody:r,coverType:o,unitId:s,textRanges:c}=e,l=i.get(a.ICommandService),d=i.get(a.IUndoRedoService),u=i.get(x),p={id:I.id,params:{unitId:s,mutations:[]}},{startOffset:h,endOffset:m}=t;p.params.mutations.push({t:"r",len:h,segmentId:n}),p.params.mutations.push({t:"r",body:r,len:m-h,segmentId:n,coverType:o});const g=l.syncExecuteCommand(p.id,p.params);return u.replaceTextRanges(c),g?(d.pushUndoRedo({unitID:s,undoMutations:[{id:I.id,params:g}],redoMutations:[{id:I.id,params:p.params}],undo(){return l.syncExecuteCommand(I.id,g),u.replaceTextRanges(c),!0},redo(){return l.syncExecuteCommand(I.id,p.params),u.replaceTextRanges(c),!0}}),!0):!1}};class ke{constructor(){S(this,"_previousActiveRange",null);S(this,"_undoMutationParamsCache",[]);S(this,"_redoMutationParamsCache",[])}clearUndoRedoMutationParamsCache(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[]}setActiveRange(e){this._previousActiveRange=e}pushUndoRedoMutationParams(e,t){this._undoMutationParamsCache.push(e),this._redoMutationParamsCache.push(t)}fetchComposedUndoRedoMutationParams(e){if(this._undoMutationParamsCache.length===0||this._previousActiveRange==null)return null;const{unitId:t}=this._undoMutationParamsCache[0],{segmentId:n,startOffset:r,collapsed:o}=this._previousActiveRange,s={unitId:t,mutations:[]},c={unitId:t,mutations:[]};return o?(s.mutations.push({t:"r",len:r,segmentId:n}),c.mutations.push({t:"r",len:r,segmentId:n})):(r>0&&s.mutations.push({t:"r",len:r,segmentId:n}),s.mutations.push(this._undoMutationParamsCache[0].mutations.find(l=>l.t==="i")),c.mutations.push(...oe(this._previousActiveRange,n))),e.length&&(s.mutations.push({t:"d",len:e.length,line:0,segmentId:n}),c.mutations.push({t:"i",body:{dataStream:e},len:e.length,line:0,segmentId:n})),{redoMutationParams:c,undoMutationParams:s,previousActiveRange:this._previousActiveRange}}dispose(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[],this._previousActiveRange=null}}const Ve={id:"doc.command.ime-input",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{unitId:t,newText:n,oldTextLen:r,range:o,segmentId:s,textRanges:c,isCompositionEnd:l}=e,d=i.get(a.ICommandService),u=i.get(a.IUndoRedoService),p=i.get(x),h=i.get(ke),m={id:I.id,params:{unitId:t,mutations:[]}};o.collapsed?m.params.mutations.push({t:"r",len:o.startOffset,segmentId:s}):m.params.mutations.push(...oe(o,s)),r>0&&m.params.mutations.push({t:"d",len:r,line:0,segmentId:s}),m.params.mutations.push({t:"i",body:{dataStream:n},len:n.length,line:0,segmentId:s});const g=d.syncExecuteCommand(m.id,m.params);if(h.pushUndoRedoMutationParams(g,m.params),p.replaceTextRanges(c),l){if(g){const f=h.fetchComposedUndoRedoMutationParams(n);if(f==null)return!1;const{undoMutationParams:_,redoMutationParams:C,previousActiveRange:M}=f;return u.pushUndoRedo({unitID:t,undoMutations:[{id:I.id,params:_}],redoMutations:[{id:I.id,params:C}],undo(){return d.syncExecuteCommand(I.id,_),p.replaceTextRanges([M]),!0},redo(){return d.syncExecuteCommand(I.id,C),p.replaceTextRanges(c),!0}}),!0}}else return!!g;return!1}},V={id:"doc.command.set-inline-format-bold",type:a.CommandType.COMMAND,handler:async()=>!0},ee={id:"doc.command.set-inline-format-italic",type:a.CommandType.COMMAND,handler:async()=>!0},te={id:"doc.command.set-inline-format-underline",type:a.CommandType.COMMAND,handler:async()=>!0},ne={id:"doc.command.set-inline-format-strikethrough",type:a.CommandType.COMMAND,handler:async()=>!0},z={id:"doc.command.set-inline-format-fontsize",type:a.CommandType.COMMAND,handler:async()=>!0},W={id:"doc.command.set-inline-format-font-family",type:a.CommandType.COMMAND,handler:async()=>!0},re={id:"doc.command.set-inline-format-text-color",type:a.CommandType.COMMAND,handler:async()=>!0},ze={id:"doc.command.set-inline-format",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{segmentId:t,value:n,preCommandId:r}=e,o=i.get(a.IUndoRedoService),s=i.get(a.ICommandService),c=i.get(x),l=i.get(a.IUniverInstanceService),d=c.getSelections();if(!Array.isArray(d)||d.length===0)return!1;let u=l.getCurrentUniverDocInstance(),p=u.getUnitId();p===a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY&&(u=l.getUniverDocInstance(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),p=u.getUnitId());let h;const m={[V.id]:"bl",[ee.id]:"it",[te.id]:"ul",[ne.id]:"st",[z.id]:"fs",[W.id]:"ff",[re.id]:"cl"};switch(r){case V.id:case ee.id:case te.id:case ne.id:{h=ft(u.body.textRuns,m[r],d);break}case z.id:case W.id:{h=n;break}case re.id:{h={rgb:n};break}default:throw new Error(`Unknown command: ${r} in handleInlineFormat`)}const g={id:I.id,params:{unitId:p,mutations:[]}},f=new a.MemoryCursor;f.reset();for(const M of d){const{startOffset:D,endOffset:b}=M,w={dataStream:"",textRuns:[{st:0,ed:b-D,ts:{[m[r]]:h}}]},P=D-f.cursor;P!==0&&g.params.mutations.push({t:"r",len:P,segmentId:t}),g.params.mutations.push({t:"r",body:w,len:b-D,segmentId:t}),f.reset(),f.moveCursor(b)}const _=s.syncExecuteCommand(g.id,g.params),C=[V.id,z.id,W.id];return C.includes(r)&&c.refreshSelection(),_?(o.pushUndoRedo({unitID:p,undoMutations:[{id:I.id,params:_}],redoMutations:[{id:I.id,params:g.params}],undo(){return s.syncExecuteCommand(I.id,_),C.includes(r)&&c.refreshSelection(),!0},redo(){return s.syncExecuteCommand(I.id,g.params),C.includes(r)&&c.refreshSelection(),!0}}),!0):!1}};function pt(i){return i!==null&&typeof i=="object"}function ft(i,e,t){let n=0,r=0;for(;n!==i.length&&r!==t.length;){const{startOffset:o,endOffset:s}=t[r],{st:c,ed:l,ts:d}=i[n];if(s<=c)r++;else if(l<=o)n++;else{if((d==null?void 0:d[e])==null)return/bl|it/.test(e)?a.BooleanNumber.TRUE:{s:a.BooleanNumber.TRUE};if(d[e]===a.BooleanNumber.FALSE)return a.BooleanNumber.TRUE;if(pt(d[e])&&d[e].s===a.BooleanNumber.FALSE)return{s:a.BooleanNumber.TRUE};n++}}return/bl|it/.test(e)?a.BooleanNumber.FALSE:{s:a.BooleanNumber.FALSE}}const nt={id:"doc.command-replace-content",type:a.CommandType.COMMAND,handler:async(i,e)=>{var g;const{unitId:t,body:n,textRanges:r,segmentId:o=""}=e,s=i.get(a.IUniverInstanceService),c=i.get(a.ICommandService),l=i.get(x),d=i.get(a.IUndoRedoService),u=(g=s.getUniverDocInstance(t))==null?void 0:g.getSnapshot().body,p=l.getSelections();if(u==null||!Array.isArray(p)||p.length===0)return!1;const h=it(t,o,u,n),m=c.syncExecuteCommand(h.id,h.params);return l.replaceTextRanges(r),m?(d.pushUndoRedo({unitID:t,undoMutations:[{id:I.id,params:m}],redoMutations:[{id:I.id,params:h.params}],undo(){return c.syncExecuteCommand(I.id,m),l.replaceTextRanges(p),!0},redo(){return c.syncExecuteCommand(I.id,h.params),l.replaceTextRanges(r),!0}}),!0):!1}},rt={id:"doc.command-cover-content",type:a.CommandType.COMMAND,handler:async(i,e)=>{var u;const{unitId:t,body:n,segmentId:r=""}=e,o=i.get(a.IUniverInstanceService),s=i.get(a.ICommandService),c=i.get(a.IUndoRedoService),l=(u=o.getUniverDocInstance(t))==null?void 0:u.getSnapshot().body;if(l==null)return!1;const d=it(t,r,l,n);return s.syncExecuteCommand(d.id,d.params),c.clearUndoRedo(t),!0}};function it(i,e,t,n){const r={id:I.id,params:{unitId:i,mutations:[]}},o=(t==null?void 0:t.dataStream.length)-2;return o>0&&r.params.mutations.push({t:"d",len:o,line:0,segmentId:e}),n.dataStream.length>0&&r.params.mutations.push({t:"i",body:n,len:n.dataStream.length,line:0,segmentId:e}),r}const U={id:"doc.operation.move-cursor",type:a.CommandType.OPERATION,handler:(i,e)=>!!e},N={id:"doc.operation.move-selection",type:a.CommandType.OPERATION,handler:(i,e)=>!!e},We={id:"doc.operation.set-selections",type:a.CommandType.OPERATION,handler:(i,e)=>(i.get(x).replaceTextRangesWithNoRefresh(e.ranges),!0)},_t=(i,e)=>{const t=i.get(a.IUniverInstanceService).getUniverDocInstance(e.unitId),n=(t==null?void 0:t.zoomRatio)||1;return{...a.Tools.deepClone(e),zoomRatio:n}},A={id:"doc.operation.set-zoom-ratio",type:a.CommandType.OPERATION,handler:(i,e)=>{const t=i.get(a.IUniverInstanceService).getUniverDocInstance(e.unitId);if(!t)return!1;const n=t.getSnapshot();return n.settings==null?n.settings={zoomRatio:e.zoomRatio}:n.settings.zoomRatio=e.zoomRatio,!0}},ot={type:a.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(i,e)=>{const t=i.get(a.ICommandService),n=i.get(a.IUndoRedoService),r=i.get(a.IUniverInstanceService);let o=r.getCurrentUniverDocInstance().getUnitId(),s=1;if(e&&(o=e.documentId??o,s=e.zoomRatio??s),!r.getUniverDocInstance(o))return!1;const l={zoomRatio:s,unitId:o},d=_t(i,l);return t.syncExecuteCommand(A.id,l)?(n.pushUndoRedo({unitID:o,undoMutations:[{id:A.id,params:d}],redoMutations:[{id:A.id,params:l}]}),!0):!1}},st={id:"doc.operation.select-all",type:a.CommandType.COMMAND,handler:async i=>{const e=i.get(a.IUniverInstanceService),t=i.get(x),n=e.getCurrentUniverDocInstance().getSnapshot().body;if(n==null)return!1;const r=[{startOffset:0,endOffset:n.dataStream.length-2,collapsed:!1}];return t.replaceTextRanges(r),!0}},Ye={id:"doc.command.inner-paste",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{segmentId:t,body:n,textRanges:r}=e,o=i.get(a.IUndoRedoService),s=i.get(a.ICommandService),c=i.get(x),l=i.get(a.IUniverInstanceService),d=c.getSelections();if(!Array.isArray(d)||d.length===0)return!1;const p=l.getCurrentUniverDocInstance().getUnitId(),h={id:I.id,params:{unitId:p,mutations:[]}},m=new a.MemoryCursor;m.reset();for(const f of d){const{startOffset:_,endOffset:C,collapsed:M}=f,D=_-m.cursor;M?h.params.mutations.push({t:"r",len:D,segmentId:t}):h.params.mutations.push(...oe(f,t,m.cursor)),h.params.mutations.push({t:"i",body:n,len:n.dataStream.length,line:0,segmentId:t}),m.reset(),m.moveCursor(C)}const g=s.syncExecuteCommand(h.id,h.params);return c.replaceTextRanges(r),g?(o.pushUndoRedo({unitID:p,undoMutations:[{id:I.id,params:g}],redoMutations:[{id:I.id,params:h.params}],undo(){return s.syncExecuteCommand(I.id,g),c.replaceTextRanges(d),!0},redo(){return s.syncExecuteCommand(I.id,h.params),c.replaceTextRanges(r),!0}}),!0):!1}},Pe={id:"doc.command.inner-cut",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{segmentId:t,textRanges:n}=e,r=i.get(a.IUndoRedoService),o=i.get(a.ICommandService),s=i.get(x),c=i.get(a.IUniverInstanceService),l=s.getSelections();if(!Array.isArray(l)||l.length===0)return!1;const d=c.getCurrentUniverDocInstance().getUnitId(),u=c.getUniverDocInstance(d),p=a.getDocsUpdateBody(u.snapshot,t);if(p==null)return!1;const h={id:I.id,params:{unitId:d,mutations:[]}},m=new a.MemoryCursor;m.reset();for(const f of l){const{startOffset:_,endOffset:C,collapsed:M}=f,D=_-m.cursor;M?h.params.mutations.push({t:"r",len:D,segmentId:t}):h.params.mutations.push(...St(f,p,t,m.cursor)),m.reset(),m.moveCursor(C)}const g=o.syncExecuteCommand(h.id,h.params);return s.replaceTextRanges(n),g?(r.pushUndoRedo({unitID:d,undoMutations:[{id:I.id,params:g}],redoMutations:[{id:I.id,params:h.params}],undo(){return o.syncExecuteCommand(I.id,g),s.replaceTextRanges(l),!0},redo(){return o.syncExecuteCommand(I.id,h.params),s.replaceTextRanges(n),!0}}),!0):!1}};function St(i,e,t="",n=0){const{startOffset:r,endOffset:o}=i,s=[],{paragraphs:c=[]}=e,l=r-n,d=o-n,u=c==null?void 0:c.find(p=>p.startIndex-n>=l&&p.startIndex-n<=d);if(l>0&&s.push({t:"r",len:l,segmentId:t}),u&&u.startIndex-n>l){const p=u.startIndex-n;s.push({t:"d",len:p-l,line:0,segmentId:t}),s.push({t:"r",len:1,segmentId:t}),d>p+1&&s.push({t:"d",len:d-p-1,line:0,segmentId:t})}else s.push({t:"d",len:d-l,line:0,segmentId:t});return s}const vt=10,It=6;function Ct(){return a.Tools.generateRandomId(It)}function Mt(i){const e=i.match(/data-copy-id="([^\s]+)"/);return e&&e[1]?e[1]:null}class Ot{constructor(){S(this,"_cache",new a.LRUMap(vt))}set(e,t){this._cache.set(e,t)}get(e){return this._cache.get(e)}clear(){this._cache.clear()}}const Xe=new Ot;function Rt(i){const e=i.style,t={};for(let n=0;n<e.length;n++){const r=e[n],o=e.getPropertyValue(r);switch(r){case"margin-top":{const s=parseInt(o);t.spaceAbove=/pt/.test(o)?Be(s):s;break}case"margin-bottom":{const s=parseInt(o);t.spaceBelow=/pt/.test(o)?Be(s):s;break}}}return Object.getOwnPropertyNames(t).length?t:null}function Be(i){return i/.75}function Ge(i){const e=i.style,t={},n=i.tagName.toLowerCase();switch(n){case"b":case"em":case"strong":{t.bl=a.BooleanNumber.TRUE;break}case"s":{t.st={s:a.BooleanNumber.TRUE};break}case"u":{t.ul={s:a.BooleanNumber.TRUE};break}case"i":{t.it=a.BooleanNumber.TRUE;break}case"sub":case"sup":{t.va=n==="sup"?a.BaselineOffset.SUPERSCRIPT:a.BaselineOffset.SUBSCRIPT;break}}for(let r=0;r<e.length;r++){const o=e[r],s=e.getPropertyValue(o);switch(o){case"font-family":{t.ff=s;break}case"font-size":{const c=parseInt(s);Number.isNaN(c)||(t.fs=/pt$/.test(s)?Be(c):c);break}case"font-style":{s==="italic"&&(t.it=a.BooleanNumber.TRUE);break}case"font-weight":{Number(s)>400&&(t.bl=a.BooleanNumber.TRUE);break}case"text-decoration":{/underline/.test(s)?t.ul={s:a.BooleanNumber.TRUE}:/overline/.test(s)?t.ol={s:a.BooleanNumber.TRUE}:/line-through/.test(s)&&(t.st={s:a.BooleanNumber.TRUE});break}case"color":{const c=new Ke.TinyColor(s);c.isValid&&(t.cl={rgb:c.toRgbString()});break}case"background-color":{const c=new Ke.TinyColor(s);c.isValid&&(t.bg={rgb:c.toRgbString()});break}}}return t}function yt(i){const e=new DOMParser,t=`<x-univer id="univer-root">${i}</x-univer>`;return e.parseFromString(t,"text/html").querySelector("#univer-root")}function qe(i,e){const t=i.tagName.toLowerCase();return typeof e=="string"?t===e:Array.isArray(e)?e.some(n=>n===t):e(i)}const Ee=class Ee{constructor(){S(this,"styleCache",new Map);S(this,"styleRules",[]);S(this,"afterProcessRules",[])}static use(e){if(this.pluginList.includes(e))throw new Error(`Univer paste plugin ${e.name} already added`);this.pluginList.push(e)}convert(e){const t=Ee.pluginList.find(o=>o.checkPasteType(e)),n=yt(e),r={dataStream:"",textRuns:[]};return t&&(this.styleRules=[...t.stylesRules],this.afterProcessRules=[...t.afterProcessRules]),this.styleCache.clear(),this.process(null,n==null?void 0:n.childNodes,r),this.styleCache.clear(),this.styleRules=[],this.afterProcessRules=[],r}process(e,t,n){var r;for(const o of t)if(o.nodeType===Node.TEXT_NODE){const s=(r=o.nodeValue)==null?void 0:r.replace(/[\r\n]/g,"");let c;e&&this.styleCache.has(e)&&(c=this.styleCache.get(e)),n.dataStream+=s,c&&Object.getOwnPropertyNames(c).length&&n.textRuns.push({st:n.dataStream.length-s.length,ed:n.dataStream.length,ts:c})}else if(o.nodeType===Node.ELEMENT_NODE){const s=e?this.styleCache.get(e):{},c=this.styleRules.find(({filter:p})=>qe(o,p)),l=c?c.getStyle(o):Ge(o);this.styleCache.set(o,{...s,...l});const{childNodes:d}=o;this.process(o,d,n);const u=this.afterProcessRules.find(({filter:p})=>qe(o,p));u&&u.handler(n,o)}}};S(Ee,"pluginList",[]);let ie=Ee;const Dt={name:"univer-doc-paste-plugin-lark",checkPasteType(i){return/lark-record-clipboard/i.test(i)},stylesRules:[{filter:["s"],getStyle(i){const e=Ge(i);return{st:{s:a.BooleanNumber.TRUE},...e}}}],afterProcessRules:[{filter(i){return i.tagName==="DIV"&&/ace-line/i.test(i.className)},handler(i){i.paragraphs==null&&(i.paragraphs=[]),i.paragraphs.push({startIndex:i.dataStream.length}),i.dataStream+="\r"}}]},xt={name:"univer-doc-paste-plugin-word",checkPasteType(i){return/word|mso/i.test(i)},stylesRules:[{filter:["b"],getStyle(i){const e=Ge(i);return{bl:a.BooleanNumber.TRUE,...e}}}],afterProcessRules:[{filter(i){return i.tagName==="P"&&/mso/i.test(i.className)},handler(i,e){i.paragraphs==null&&(i.paragraphs=[]);const t={startIndex:i.dataStream.length},n=Rt(e);n&&(t.paragraphStyle=n),i.paragraphs.push(t),i.dataStream+="\r"}}]};function bt(i,e){const{st:t,ed:n,ts:r={}}=e,{ff:o,fs:s,it:c,bl:l,ul:d,st:u,ol:p,bg:h,cl:m,va:g}=r;let f=i.slice(t,n);const _=[];return c===a.BooleanNumber.TRUE&&(f=`<i>${f}</i>`),g===a.BaselineOffset.SUPERSCRIPT?f=`<sup>${f}</sup>`:g===a.BaselineOffset.SUBSCRIPT&&(f=`<sub>${f}</sub>`),(d==null?void 0:d.s)===a.BooleanNumber.TRUE&&(f=`<u>${f}</u>`),(u==null?void 0:u.s)===a.BooleanNumber.TRUE&&(f=`<s>${f}</s>`),l===a.BooleanNumber.TRUE&&(f=`<strong>${f}</strong>`),o&&_.push(`font-family: ${o}`),m&&_.push(`color: ${m.rgb}`),s&&_.push(`font-size: ${s}px`),p&&_.push("text-decoration: overline"),h&&_.push(`background: ${h.rgb}`),_.length?`<span style="${_.join(";")}">${f}</span>`:f}function Ze(i,e=!0){const{dataStream:t,textRuns:n=[],paragraphs:r=[]}=i;let o=0;const s=[],c=[];for(const l of n){const{st:d,ed:u}=l;if(d!==o&&s.push(t.slice(o,d)),s.push(bt(t,l)),o=u,e)for(const p of r){const{startIndex:h,paragraphStyle:m={}}=p;if(h>=d&&h<=u){const{spaceAbove:g,spaceBelow:f}=m,_=[];g!=null&&(typeof g=="number"?_.push(`margin-top: ${g}px`):_.push(`margin-top: ${g.v}px`)),f!=null&&(typeof f=="number"?_.push(`margin-bottom: ${f}px`):_.push(`margin-bottom: ${f.v}px`)),c.push(`<p className="UniverNormal" ${_.length?`style="${_.join(";")}"`:""}>${s.join("")}</p>`),s.length=0}}}return c.join("")+s.join("")}class Tt{convert(e){if(e.length===0)throw new Error("The bodyList length at least to be 1");if(e.length===1)return Ze(e[0]);let t="";for(const n of e)t+='<p className="UniverNormal">',t+=Ze(n,!1),t+="</p>";return t}}var Pt=Object.defineProperty,Et=Object.getOwnPropertyDescriptor,Ut=(i,e,t,n)=>{for(var r=n>1?void 0:n?Et(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Pt(e,t,r),r},Je=(i,e)=>(t,n)=>e(t,n,i);ie.use(xt);ie.use(Dt);const at=O.createIdentifier("doc.clipboard-service");let je=class extends a.Disposable{constructor(e,t){super();S(this,"_clipboardHooks",[]);S(this,"_htmlToUDM",new ie);S(this,"_umdToHtml",new Tt);this._currentUniverService=e,this._clipboardInterfaceService=t}async queryClipboardData(){const e=await this._clipboardInterfaceService.read();if(e.length===0)return Promise.reject();const t=e[0],n=await t.getType(R.PLAIN_TEXT_CLIPBOARD_MIME_TYPE).then(s=>s&&s.text()),r=await t.getType(R.HTML_CLIPBOARD_MIME_TYPE).then(s=>s&&s.text());if(!r)return{dataStream:n};const o=Mt(r);if(o){const s=Xe.get(o);if(s)return s}return this._htmlToUDM.convert(r)}async setClipboardData(e){const t=Ct(),n=e.length>1?e.map(o=>o.dataStream).join(`
2
- `):e[0].dataStream;let r=this._umdToHtml.convert(e);return e.length===1&&(r=r.replace(/(<[a-z]+)/,(o,s)=>`${s} data-copy-id="${t}"`),Xe.set(t,e[0])),this._clipboardInterfaceService.write(n,r)}addClipboardHook(e){return this._clipboardHooks.push(e),a.toDisposable(()=>{const t=this._clipboardHooks.indexOf(e);t>-1&&this._clipboardHooks.splice(t,1)})}};je=Ut([Je(0,a.IUniverInstanceService),Je(1,R.IClipboardInterfaceService)],je);var Nt=Object.defineProperty,wt=Object.getOwnPropertyDescriptor,Lt=(i,e,t,n)=>{for(var r=n>1?void 0:n?wt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Nt(e,t,r),r},$=(i,e)=>(t,n)=>e(t,n,i);let he=class extends a.Disposable{constructor(i,e,t,n,r,o){super(),this._logService=i,this._commandService=e,this._currentUniverService=t,this._docClipboardService=n,this._textSelectionRenderManager=r,this._contextService=o,this._commandExecutedListener(),this.initialize()}initialize(){[de,ue,me].forEach(i=>this.disposeWithMe(this._commandService.registerAsMultipleCommand(i))),[Ye,Pe].forEach(i=>this.disposeWithMe(this._commandService.registerCommand(i)))}_commandExecutedListener(){const i=[ue.id,de.id,me.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(i.includes(e.id)&&!(!this._contextService.getContextValue(a.FOCUSING_DOC)&&!this._contextService.getContextValue(a.FOCUSING_EDITOR)))switch(e.id){case me.id:{this._handlePaste();break}case de.id:{this._handleCopy();break}case ue.id:{this._handleCut();break}default:throw new Error(`Unhandled command ${e.id}`)}}))}async _handlePaste(){const{_docClipboardService:i}=this,{segmentId:e,endOffset:t,style:n}=this._textSelectionRenderManager.getActiveRange()??{},r=this._textSelectionRenderManager.getAllTextRanges();if(e==null&&this._logService.error("[DocClipboardController] segmentId is not existed"),t!=null)try{const o=await i.queryClipboardData();let s=t;for(const l of r){const{startOffset:d,endOffset:u}=l;d==null||u==null||u<=t&&(s+=o.dataStream.length-(u-d))}const c=[{startOffset:s,endOffset:s,collapsed:!0,style:n}];this._commandService.executeCommand(Ye.id,{body:o,segmentId:e,textRanges:c})}catch{this._logService.error("[DocClipboardController] clipboard is empty")}}_getDocumentBodyInRanges(){const i=this._textSelectionRenderManager.getAllTextRanges(),e=this._currentUniverService.getCurrentUniverDocInstance(),{dataStream:t,textRuns:n=[],paragraphs:r=[]}=e.getBody(),o=[];for(const s of i){const{startOffset:c,endOffset:l,collapsed:d}=s;if(d||c==null||l==null)continue;const u={dataStream:t.slice(c,l)},p=[];for(const m of n){const g=a.Tools.deepClone(m),{st:f,ed:_}=g;a.Tools.hasIntersectionBetweenTwoRanges(f,_,c,l)&&(c>=f&&c<=_?p.push({...g,st:c,ed:Math.min(l,_)}):l>=f&&l<=_?p.push({...g,st:Math.max(c,f),ed:l}):p.push(g))}p.length&&(u.textRuns=p.map(m=>{const{st:g,ed:f}=m;return{...m,st:g-c,ed:f-c}}));const h=[];for(const m of r){const{startIndex:g}=m;g>=c&&g<=l&&h.push(a.Tools.deepClone(m))}h.length&&(u.paragraphs=h.map(m=>({...m,startIndex:m.startIndex-c}))),o.push(u)}return o}async _handleCopy(){const{_docClipboardService:i}=this,e=this._getDocumentBodyInRanges();try{i.setClipboardData(e)}catch{this._logService.error("[DocClipboardController] set clipboard failed")}}async _handleCut(){const{segmentId:i,endOffset:e,style:t}=this._textSelectionRenderManager.getActiveRange()??{},n=this._textSelectionRenderManager.getAllTextRanges();if(i==null&&this._logService.error("[DocClipboardController] segmentId is not existed"),e!=null){this._handleCopy();try{let r=e;for(const s of n){const{startOffset:c,endOffset:l}=s;c==null||l==null||l<=e&&(r-=l-c)}const o=[{startOffset:r,endOffset:r,collapsed:!0,style:t}];this._commandService.executeCommand(Pe.id,{segmentId:i,textRanges:o})}catch{this._logService.error("[DocClipboardController] cut content failed")}}}};he=Lt([a.OnLifecycle(a.LifecycleStages.Rendered,he),$(0,a.ILogService),$(1,a.ICommandService),$(2,a.IUniverInstanceService),$(3,at),$(4,v.ITextSelectionRenderManager),$(5,a.IContextService)],he);var At=Object.defineProperty,$t=Object.getOwnPropertyDescriptor,Bt=(i,e,t,n)=>{for(var r=n>1?void 0:n?$t(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&At(e,t,r),r},Qe=(i,e)=>(t,n)=>e(t,n,i);exports.DocSkeletonManagerService=class{constructor(e,t){S(this,"_currentSkeletonUnitId","");S(this,"_docSkeletonMap",new Map);S(this,"_currentSkeleton$",new De.BehaviorSubject(null));S(this,"currentSkeleton$",this._currentSkeleton$.asObservable());S(this,"_currentSkeletonBefore$",new De.BehaviorSubject(null));S(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());this._localeService=e,this._docViewModelManagerService=t,this.initialize()}initialize(){this._docViewModelManagerService.currentDocViewModel$.subscribe(e=>{e!=null&&this._setCurrent(e)})}dispose(){this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete(),this._docSkeletonMap=new Map}getCurrent(){return this.getSkeletonByUnitId(this._currentSkeletonUnitId)}makeDirtyCurrent(e=!0){this.makeDirty(this._currentSkeletonUnitId,e)}makeDirty(e,t=!0){const n=this.getSkeletonByUnitId(e);n!=null&&(n.dirty=t)}_setCurrent(e){const{unitId:t}=e;if(this._docSkeletonMap.has(t)){const n=this.getSkeletonByUnitId(t);n.skeleton.calculate(),n.dirty=!0}else{const n=this._buildSkeleton(e.docViewModel);n.calculate(),this._docSkeletonMap.set(t,{unitId:t,skeleton:n,dirty:!1})}return this._currentSkeletonUnitId=t,this._currentSkeletonBefore$.next(this.getCurrent()),this._currentSkeleton$.next(this.getCurrent()),this.getCurrent()}getSkeletonByUnitId(e){return this._docSkeletonMap.get(e)}_buildSkeleton(e){return v.DocumentSkeleton.create(e,this._localeService)}};exports.DocSkeletonManagerService=Bt([Qe(0,O.Inject(a.LocaleService)),Qe(1,O.Inject(exports.DocViewModelManagerService))],exports.DocSkeletonManagerService);var jt=Object.defineProperty,Ft=Object.getOwnPropertyDescriptor,kt=(i,e,t,n)=>{for(var r=n>1?void 0:n?Ft(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&jt(e,t,r),r},se=(i,e)=>(t,n)=>e(t,n,i);let ge=class extends a.Disposable{constructor(i,e,t,n){super(),this._docSkeletonManagerService=i,this._currentUniverService=e,this._textSelectionRenderManager=t,this._commandService=n,this._initialize(),this._commandExecutedListener()}_initialize(){}_commandExecutedListener(){const i=[Q.id,be.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(i.includes(e.id))switch(e.id){case Q.id:this._handleDeleteLeft();break;case be.id:this._handleDeleteRight();break;default:throw new Error("Unknown command")}}))}_handleDeleteLeft(){var m;const i=this._textSelectionRenderManager.getActiveRange(),e=(m=this._docSkeletonManagerService.getCurrent())==null?void 0:m.skeleton;if(i==null||e==null)return;const t=this._currentUniverService.getCurrentUniverDocInstance(),{startOffset:n,collapsed:r,segmentId:o,style:s}=i;if(n===0&&r)return;const c=e.findNodeByCharIndex(n),l=v.hasListSpan(c),d=v.isIndentBySpan(c,t.body);let u=n;const p=e.findNodeByCharIndex(n-1);if(v.isFirstSpan(c)&&p!==c&&(l===!0||d===!0)){const g=v.getParagraphBySpan(c,t.body);if(g==null)return;const f=g==null?void 0:g.startIndex,_={startIndex:0},C=g.paragraphStyle;if(l===!0){const D=g.paragraphStyle;D&&(_.paragraphStyle=D)}else if(d===!0){const D=g.bullet;D&&(_.bullet=D),C!=null&&(_.paragraphStyle={...C},delete _.paragraphStyle.hanging,delete _.paragraphStyle.indentStart)}const M=[{startOffset:u,endOffset:u,collapsed:!0,style:s}];this._commandService.executeCommand(Fe.id,{unitId:t.getUnitId(),updateBody:{dataStream:"",paragraphs:[{..._}]},range:{startOffset:f,endOffset:f+1,collapsed:!1},textRanges:M,coverType:a.UpdateDocsAttributeType.REPLACE,segmentId:o})}else{const{endNodePosition:g}=i;if(g!=null){const f=e.findSpanByPosition(g);v.hasListSpan(f)&&!v.isSameLine(c,f)&&(i.endOffset-=1)}if(r===!0){u-=p.count;const f=[{startOffset:u,endOffset:u,collapsed:!0,style:s}];this._commandService.executeCommand(Te.id,{unitId:t.getUnitId(),range:i,segmentId:o,direction:$e.LEFT,len:p.count,textRanges:f})}else{const f=this._getTextRangesWhenDelete();this._commandService.executeCommand(Pe.id,{segmentId:o,textRanges:f})}}e==null||e.calculate()}_handleDeleteRight(){var c;const i=this._textSelectionRenderManager.getActiveRange(),e=(c=this._docSkeletonManagerService.getCurrent())==null?void 0:c.skeleton;if(i==null||e==null)return;const t=this._currentUniverService.getCurrentUniverDocInstance(),{startOffset:n,collapsed:r,segmentId:o,style:s}=i;if(!(n===t.getBody().dataStream.length-2&&r)){if(r===!0){const l=[{startOffset:n,endOffset:n,collapsed:!0,style:s}],d=e.findNodeByCharIndex(n);this._commandService.executeCommand(Te.id,{unitId:t.getUnitId(),range:i,segmentId:o,direction:$e.RIGHT,textRanges:l,len:d.count})}else{const l=this._getTextRangesWhenDelete();this._commandService.executeCommand(Pe.id,{segmentId:o,textRanges:l})}e==null||e.calculate()}}_getTextRangesWhenDelete(){const i=this._textSelectionRenderManager.getActiveRange(),e=this._textSelectionRenderManager.getAllTextRanges();let t=i.endOffset;for(const r of e){const{startOffset:o,endOffset:s}=r;o==null||s==null||s<=i.endOffset&&(t-=s-o)}return[{startOffset:t,endOffset:t,collapsed:!0,style:i.style}]}};ge=kt([a.OnLifecycle(a.LifecycleStages.Rendered,ge),se(0,O.Inject(exports.DocSkeletonManagerService)),se(1,a.IUniverInstanceService),se(2,v.ITextSelectionRenderManager),se(3,a.ICommandService)],ge);var Vt=Object.defineProperty,zt=Object.getOwnPropertyDescriptor,Wt=(i,e,t,n)=>{for(var r=n>1?void 0:n?zt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Vt(e,t,r),r},we=(i,e)=>(t,n)=>e(t,n,i);let pe=class extends a.Disposable{constructor(i,e,t){super(),this._docSkeletonManagerService=i,this._renderManagerService=e,this._commandService=t,this._initialRenderRefresh(),this._commandExecutedListener()}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeletonBefore$.subscribe(i=>{if(i==null)return;const{skeleton:e,unitId:t}=i,n=this._renderManagerService.getRenderById(t);if(n==null)return;const{mainComponent:r}=n;r.changeSkeleton(e),this._recalculateSizeBySkeleton(n,e)})}_recalculateSizeBySkeleton(i,e){var c;const{mainComponent:t}=i,n=t,r=(c=e.getSkeletonData())==null?void 0:c.pages;if(r==null)return;let o=0,s=0;for(let l=0,d=r.length;l<d;l++){const u=r[l],{pageWidth:p,pageHeight:h}=u;n.pageLayoutType===v.PageLayoutType.VERTICAL?(s+=h,l!==d-1&&(s+=n.pageMarginTop),o=Math.max(o,p)):n.pageLayoutType===v.PageLayoutType.HORIZONTAL&&(o+=p,l!==d-1&&(o+=n.pageMarginLeft),s=Math.max(s,h))}n.resize(o,s)}_commandExecutedListener(){const i=[I.id],e=[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{var n;if(i.includes(t.id)){const r=t.params,{unitId:o}=r,s=this._docSkeletonManagerService.getSkeletonByUnitId(o);if(s==null)return;const{skeleton:c}=s,l=this._renderManagerService.getRenderById(o);if(l==null)return;if(c.calculate(),e.includes(o)){(n=l.mainComponent)==null||n.makeDirty();return}this._recalculateSizeBySkeleton(l,c)}}))}};pe=Wt([a.OnLifecycle(a.LifecycleStages.Rendered,pe),we(0,O.Inject(exports.DocSkeletonManagerService)),we(1,v.IRenderManagerService),we(2,a.ICommandService)],pe);var Gt=Object.defineProperty,Ht=Object.getOwnPropertyDescriptor,Kt=(i,e,t,n)=>{for(var r=n>1?void 0:n?Ht(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Gt(e,t,r),r},q=(i,e)=>(t,n)=>e(t,n,i);let fe=class extends a.Disposable{constructor(e,t,n,r,o){super();S(this,"_liquid",new v.Liquid);S(this,"_pageMarginCache",new Map);this._docSkeletonManagerService=e,this._currentUniverService=t,this._renderManagerService=n,this._commandService=r,this._floatingObjectManagerService=o,this._initialize(),this._commandExecutedListener()}_initialize(){this._initialRenderRefresh(),this._updateOnPluginChange()}_updateOnPluginChange(){this._floatingObjectManagerService.pluginUpdate$.subscribe(e=>{const t=this._docSkeletonManagerService.getCurrent();if(t==null)return;const{unitId:n,skeleton:r}=t,o=this._renderManagerService.getRenderById(n);if(o==null)return;const{mainComponent:s,components:c,scene:l}=o,d=s,{left:u,top:p}=d;e.forEach(h=>{const{unitId:m,subComponentId:g,floatingObjectId:f,floatingObject:_}=h,{left:C=0,top:M=0,width:D=0,height:b=0,angle:w,flipX:P,flipY:K,skewX:Y,skewY:X}=_,y=this._pageMarginCache.get(f),E=(y==null?void 0:y.marginLeft)||0,T=(y==null?void 0:y.marginTop)||0;r==null||r.getViewModel().getDataModel().updateDrawing(f,{left:C-u-E,top:M-p-T,height:b,width:D})}),r==null||r.calculate(),s==null||s.makeDirty()})}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeleton$.subscribe(e=>{if(e==null)return;const{skeleton:t,unitId:n}=e,r=this._renderManagerService.getRenderById(n);if(r==null)return;const{mainComponent:o}=r;o.changeSkeleton(t),this._refreshFloatingObject(n,t,r)})}_commandExecutedListener(){const e=[I.id,A.id],t=[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY];this.disposeWithMe(this._commandService.onCommandExecuted(n=>{var r;if(e.includes(n.id)){const o=n.params,{unitId:s}=o,c=this._docSkeletonManagerService.getCurrent();if(c==null)return;const{unitId:l,skeleton:d}=c;if(s!==l)return;const u=this._renderManagerService.getRenderById(l);if(u==null)return;if(t.includes(l)){(r=u.mainComponent)==null||r.makeDirty();return}this._refreshFloatingObject(l,d,u)}}))}_refreshFloatingObject(e,t,n){const r=t==null?void 0:t.getSkeletonData(),{mainComponent:o,scene:s}=n,c=o;if(!r)return;const{left:l,top:d,pageLayoutType:u,pageMarginLeft:p,pageMarginTop:h}=c,{pages:m}=r,g=[];s.getAncestorScale(),this._liquid.reset(),this._pageMarginCache.clear();for(let f=0,_=m.length;f<_;f++){const C=m[f],{skeDrawings:M,marginLeft:D,marginTop:b}=C;this._liquid.translatePagePadding(C),M.forEach(w=>{const{aLeft:P,aTop:K,height:Y,width:X,objectId:y}=w;g.push({unitId:e,subComponentId:a.DEFAULT_DOCUMENT_SUB_COMPONENT_ID,floatingObjectId:y,floatingObject:{left:P+l+this._liquid.x,top:K+d+this._liquid.y,width:X,height:Y}}),this._pageMarginCache.set(y,{marginLeft:this._liquid.x,marginTop:this._liquid.y})}),this._liquid.translatePage(C,u,p,h)}this._floatingObjectManagerService.BatchAddOrUpdate(g)}};fe=Kt([a.OnLifecycle(a.LifecycleStages.Steady,fe),q(0,O.Inject(exports.DocSkeletonManagerService)),q(1,a.IUniverInstanceService),q(2,v.IRenderManagerService),q(3,a.ICommandService),q(4,a.IFloatingObjectManagerService)],fe);var Yt=Object.defineProperty,Xt=Object.getOwnPropertyDescriptor,qt=(i,e,t,n)=>{for(var r=n>1?void 0:n?Xt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Yt(e,t,r),r},L=(i,e)=>(t,n)=>e(t,n,i);let _e=class extends a.Disposable{constructor(e,t,n,r,o,s,c){super();S(this,"_previousIMEContent","");S(this,"_previousIMERange");S(this,"_onStartSubscription");S(this,"_onUpdateSubscription");S(this,"_onEndSubscription");this._docSkeletonManagerService=e,this._currentUniverService=t,this._renderManagerService=n,this._textSelectionRenderManager=r,this._textSelectionManagerService=o,this._imeInputManagerService=s,this._commandService=c,this._initialize()}dispose(){var e,t,n;(e=this._onStartSubscription)==null||e.unsubscribe(),(t=this._onUpdateSubscription)==null||t.unsubscribe(),(n=this._onEndSubscription)==null||n.unsubscribe()}_initialize(){this._initialOnCompositionstart(),this._initialOnCompositionUpdate(),this._initialOnCompositionend()}_initialOnCompositionstart(){this._onStartSubscription=this._textSelectionRenderManager.onCompositionstart$.subscribe(e=>{if(e==null)return;const{activeRange:t}=e;t!=null&&(this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(a.Tools.deepClone(t)),this._previousIMERange=t)})}_initialOnCompositionUpdate(){this._onUpdateSubscription=this._textSelectionRenderManager.onCompositionupdate$.subscribe(async e=>{this._updateContent(e,!0)})}_initialOnCompositionend(){this._onEndSubscription=this._textSelectionRenderManager.onCompositionend$.subscribe(e=>{this._updateContent(e,!1)})}async _updateContent(e,t){var g;const n=(g=this._docSkeletonManagerService.getCurrent())==null?void 0:g.skeleton;if(this._previousIMERange==null||e==null||n==null)return;const r=this._currentUniverService.getCurrentUniverDocInstance(),{event:o,activeRange:s}=e,{startOffset:c,segmentId:l,style:d}=this._previousIMERange;if(n==null||s==null)return;const p=o.data;if(p===this._previousIMEContent&&t)return;const h=p.length,m=[{startOffset:c+h,endOffset:c+h,collapsed:!0,style:d}];await this._commandService.executeCommand(Ve.id,{unitId:r.getUnitId(),newText:p,oldTextLen:this._previousIMEContent.length,range:this._previousIMERange,textRanges:m,isCompositionEnd:!t,segmentId:l}),n.calculate(),t?(this._previousIMERange.collapsed||(this._previousIMERange.collapsed=!0),this._previousIMEContent=p):this._resetIME()}_resetIME(){this._previousIMEContent="",this._previousIMERange=null,this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(null)}_getDocObject(){return H(this._currentUniverService,this._renderManagerService)}};_e=qt([a.OnLifecycle(a.LifecycleStages.Rendered,_e),L(0,O.Inject(exports.DocSkeletonManagerService)),L(1,a.IUniverInstanceService),L(2,v.IRenderManagerService),L(3,v.ITextSelectionRenderManager),L(4,O.Inject(x)),L(5,O.Inject(ke)),L(6,a.ICommandService)],_e);var Zt=Object.defineProperty,Jt=Object.getOwnPropertyDescriptor,Qt=(i,e,t,n)=>{for(var r=n>1?void 0:n?Jt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Zt(e,t,r),r},ae=(i,e)=>(t,n)=>e(t,n,i);let Se=class extends a.Disposable{constructor(i,e,t,n){super(),this._currentUniverService=i,this._textSelectionRenderManager=e,this._textSelectionManagerService=t,this._commandService=n,this._commandExecutedListener()}_commandExecutedListener(){const i=[V.id,ee.id,te.id,ne.id,z.id,W.id,re.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{i.includes(e.id)&&this._handleInlineFormat(e)}))}_handleInlineFormat(i){const{segmentId:e}=this._textSelectionRenderManager.getActiveRange()??{};e!=null&&this._commandService.executeCommand(ze.id,{segmentId:e,preCommandId:i.id,...i.params??{}})}};Se=Qt([a.OnLifecycle(a.LifecycleStages.Rendered,Se),ae(0,a.IUniverInstanceService),ae(1,v.ITextSelectionRenderManager),ae(2,O.Inject(x)),ae(3,a.ICommandService)],Se);var en=Object.defineProperty,tn=Object.getOwnPropertyDescriptor,nn=(i,e,t,n)=>{for(var r=n>1?void 0:n?tn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&en(e,t,r),r},B=(i,e)=>(t,n)=>e(t,n,i);function rn(i){const e=[];for(let t=0,n=i.length;t<n;t++)i[t]===a.DataStreamTreeTokenType.PARAGRAPH&&e.push({startIndex:t});return e}let ve=class extends a.Disposable{constructor(e,t,n,r,o,s){super();S(this,"_onInputSubscription");this._docSkeletonManagerService=e,this._currentUniverService=t,this._renderManagerService=n,this._textSelectionRenderManager=r,this._textSelectionManagerService=o,this._commandService=s,this._initialize(),this._commandExecutedListener()}dispose(){var e;(e=this._onInputSubscription)==null||e.unsubscribe()}_initialize(){}_commandExecutedListener(){const e=[Ue.id];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{e.includes(t.id)&&this._handleBreakLine()}))}_handleBreakLine(){var d;const e=(d=this._docSkeletonManagerService.getCurrent())==null?void 0:d.skeleton,t=this._textSelectionRenderManager.getActiveRange();if(t==null||e==null)return;const r=this._currentUniverService.getCurrentUniverDocInstance().getUnitId(),{startOffset:o,segmentId:s,style:c}=t,l=[{startOffset:o+1,endOffset:o+1,collapsed:!0,style:c}];this._commandService.executeCommand(Ne.id,{unitId:r,body:{dataStream:a.DataStreamTreeTokenType.PARAGRAPH,paragraphs:rn(a.DataStreamTreeTokenType.PARAGRAPH)},range:t,textRanges:l,segmentId:s}),e==null||e.calculate()}_getDocObject(){return H(this._currentUniverService,this._renderManagerService)}};ve=nn([a.OnLifecycle(a.LifecycleStages.Rendered,ve),B(0,O.Inject(exports.DocSkeletonManagerService)),B(1,a.IUniverInstanceService),B(2,v.IRenderManagerService),B(3,v.ITextSelectionRenderManager),B(4,O.Inject(x)),B(5,a.ICommandService)],ve);var on=Object.defineProperty,sn=Object.getOwnPropertyDescriptor,an=(i,e,t,n)=>{for(var r=n>1?void 0:n?sn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&on(e,t,r),r},j=(i,e)=>(t,n)=>e(t,n,i);let Ie=class extends a.Disposable{constructor(e,t,n,r,o,s){super();S(this,"_onInputSubscription");this._docSkeletonManagerService=e,this._currentUniverService=t,this._renderManagerService=n,this._textSelectionRenderManager=r,this._textSelectionManagerService=o,this._commandService=s,this._initialize(),this._commandExecutedListener()}dispose(){var e;(e=this._onInputSubscription)==null||e.unsubscribe()}_initialize(){}_commandExecutedListener(){const e=[U.id,N.id];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{if(!e.includes(t.id))return;const n=t.params;switch(t.id){case U.id:return this._handleMoveCursor(n.direction);case N.id:return this._handleShiftMoveSelection(n.direction);default:throw new Error("Unknown command")}}))}_handleShiftMoveSelection(e){var f;const t=this._textSelectionRenderManager.getActiveRange(),n=this._textSelectionRenderManager.getAllTextRanges(),r=this._currentUniverService.getCurrentUniverDocInstance(),o=(f=this._docSkeletonManagerService.getCurrent())==null?void 0:f.skeleton,s=this._getDocObject();if(t==null||o==null||s==null)return;const{startOffset:c,endOffset:l,style:d,collapsed:u,direction:p}=t;if(n.length>1){let _=1/0,C=-1/0;for(const M of n)_=Math.min(_,M.startOffset),C=Math.max(C,M.endOffset);this._textSelectionManagerService.replaceTextRanges([{startOffset:e===a.Direction.LEFT||e===a.Direction.UP?C:_,endOffset:e===a.Direction.LEFT||e===a.Direction.UP?_:C,collapsed:!1,style:d}]);return}const h=u||p===v.RANGE_DIRECTION.FORWARD?c:l;let m=u||p===v.RANGE_DIRECTION.FORWARD?l:c;const g=r.getBody().dataStream.length??1/0;if(e===a.Direction.LEFT||e===a.Direction.RIGHT){const _=o.findNodeByCharIndex(m-1),C=o.findNodeByCharIndex(m);m=e===a.Direction.RIGHT?m+C.count:m-_.count,m=Math.min(g-2,Math.max(0,m)),this._textSelectionManagerService.replaceTextRanges([{startOffset:h,endOffset:m,collapsed:h===m,style:d}])}else{const _=o.findNodeByCharIndex(m),C=s.document.getOffsetConfig(),M=this._getTopOrBottomPosition(o,_,e===a.Direction.DOWN);if(M==null){const b=e===a.Direction.UP?0:g-2;if(b===m)return;this._textSelectionManagerService.replaceTextRanges([{startOffset:h,endOffset:b,collapsed:h===b,style:d}]);return}const D=new v.NodePositionConvertToCursor(C,o).getRangePointData(M,M).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{startOffset:h,endOffset:D.endOffset,collapsed:h===D.endOffset,style:d}])}}_handleMoveCursor(e){var h;const t=this._textSelectionRenderManager.getActiveRange(),n=this._textSelectionRenderManager.getAllTextRanges(),r=this._currentUniverService.getCurrentUniverDocInstance(),o=(h=this._docSkeletonManagerService.getCurrent())==null?void 0:h.skeleton,s=this._getDocObject();if(t==null||o==null||s==null)return;const{startOffset:c,endOffset:l,style:d,collapsed:u}=t,p=r.getBody().dataStream.length??1/0;if(e===a.Direction.LEFT||e===a.Direction.RIGHT){let m;if(!t.collapsed||n.length>1){let g=1/0,f=-1/0;for(const _ of n)g=Math.min(g,_.startOffset),f=Math.max(f,_.endOffset);m=e===a.Direction.LEFT?g:f}else{const g=o.findNodeByCharIndex(c-1),f=o.findNodeByCharIndex(c);e===a.Direction.LEFT?m=Math.max(0,c-g.count):m=Math.min(p-2,l+f.count)}this._textSelectionManagerService.replaceTextRanges([{startOffset:m,endOffset:m,collapsed:!0,style:d}])}else{const m=o.findNodeByCharIndex(c),g=o.findNodeByCharIndex(l),f=s.document.getOffsetConfig(),_=this._getTopOrBottomPosition(o,e===a.Direction.UP?m:g,e===a.Direction.DOWN);if(_==null){let M;u?M=e===a.Direction.UP?0:p-2:M=e===a.Direction.UP?c:l,this._textSelectionManagerService.replaceTextRanges([{startOffset:M,endOffset:M,collapsed:!0,style:d}]);return}const C=new v.NodePositionConvertToCursor(f,o).getRangePointData(_,_).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{...C,style:d}])}}_getTopOrBottomPosition(e,t,n){if(t==null)return;const r=this._getSpanLeftOffsetInLine(t),o=this._getNextOrPrevLine(t,n);if(o==null)return;const s=this._matchPositionByLeftOffset(e,o,r);if(s!=null)return{...s,isBack:!0}}_getSpanLeftOffsetInLine(e){const t=e.parent;if(t==null)return-1/0;const n=t.left,{left:r}=e;return n+r}_matchPositionByLeftOffset(e,t,n){const r={distance:1/0};for(const o of t.divides){const s=o.left;for(const c of o.spanGroup){const{left:l}=c,d=s+l,u=Math.abs(n-d);u<r.distance&&(r.span=c,r.distance=u)}}if(r.span!=null)return e.findPositionBySpan(r.span)}_getNextOrPrevLine(e,t){var g,f,_,C,M,D,b,w,P,K,Y,X;const n=e.parent;if(n==null)return;const r=n.parent;if(r==null)return;const o=r.parent;if(o==null)return;const s=o.lines.indexOf(r);if(s===-1)return;let c;if(t===!0?c=o.lines[s+1]:c=o.lines[s-1],c!=null)return c;const l=o.parent;if(l==null)return;const d=l.columns.indexOf(o);if(d===-1)return;if(t===!0)c=(g=l.columns[d+1])==null?void 0:g.lines[0];else{const y=(_=(f=l.columns)==null?void 0:f[d-1])==null?void 0:_.lines;c=y==null?void 0:y[y.length-1]}if(c!=null)return c;const u=l.parent;if(u==null)return;const p=u.sections.indexOf(l);if(p===-1)return;if(t===!0)c=(M=(C=u.sections[p-1])==null?void 0:C.columns[0])==null?void 0:M.lines[0];else{const y=(b=(D=u.sections)==null?void 0:D[p-1])==null?void 0:b.columns,E=y==null?void 0:y[y.length-1],T=E==null?void 0:E.lines;c=T==null?void 0:T[T.length-1]}if(c!=null)return c;const h=u.parent;if(h==null)return;const m=h.pages.indexOf(u);if(m!==-1){if(t===!0)c=(K=(P=(w=h.pages[m+1])==null?void 0:w.sections[0])==null?void 0:P.columns[0])==null?void 0:K.lines[0];else{const y=(Y=h.pages[m-1])==null?void 0:Y.sections;if(y==null)return;const E=(X=y[y.length-1])==null?void 0:X.columns,T=E[E.length-1],He=T==null?void 0:T.lines;c=He[He.length-1]}if(c!=null)return c}}_getDocObject(){return H(this._currentUniverService,this._renderManagerService)}};Ie=an([a.OnLifecycle(a.LifecycleStages.Rendered,Ie),j(0,O.Inject(exports.DocSkeletonManagerService)),j(1,a.IUniverInstanceService),j(2,v.IRenderManagerService),j(3,v.ITextSelectionRenderManager),j(4,O.Inject(x)),j(5,a.ICommandService)],Ie);var cn=Object.defineProperty,ln=Object.getOwnPropertyDescriptor,dn=(i,e,t,n)=>{for(var r=n>1?void 0:n?ln(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&cn(e,t,r),r},F=(i,e)=>(t,n)=>e(t,n,i);let Ce=class extends a.Disposable{constructor(e,t,n,r,o,s){super();S(this,"_onInputSubscription");this._docSkeletonManagerService=e,this._currentUniverService=t,this._renderManagerService=n,this._textSelectionRenderManager=r,this._textSelectionManagerService=o,this._commandService=s,this._initialize(),this._commandExecutedListener()}dispose(){var e;(e=this._onInputSubscription)==null||e.unsubscribe()}_initialize(){this._initialNormalInput()}_initialNormalInput(){this._onInputSubscription=this._textSelectionRenderManager.onInput$.subscribe(async e=>{var m;if(e==null)return;const t=this._currentUniverService.getCurrentUniverDocInstance(),{event:n,content:r="",activeRange:o}=e,s=n,c=(m=this._docSkeletonManagerService.getCurrent())==null?void 0:m.skeleton;if(s.data==null||c==null||!c||!o)return;const{startOffset:l,segmentId:d,style:u}=o,p=r.length,h=[{startOffset:l+p,endOffset:l+p,collapsed:!0,style:u}];await this._commandService.executeCommand(Ne.id,{unitId:t.getUnitId(),body:{dataStream:r},range:o,textRanges:h,segmentId:d}),c.calculate()})}_commandExecutedListener(){}_getDocObject(){return H(this._currentUniverService,this._renderManagerService)}};Ce=dn([a.OnLifecycle(a.LifecycleStages.Rendered,Ce),F(0,O.Inject(exports.DocSkeletonManagerService)),F(1,a.IUniverInstanceService),F(2,v.IRenderManagerService),F(3,v.ITextSelectionRenderManager),F(4,O.Inject(x)),F(5,a.ICommandService)],Ce);var un=Object.defineProperty,mn=Object.getOwnPropertyDescriptor,hn=(i,e,t,n)=>{for(var r=n>1?void 0:n?mn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&un(e,t,r),r},gn=(i,e)=>(t,n)=>e(t,n,i);const pn="rgba(198,198,198, 1)",fn="rgba(255,255,255, 1)";let Me=class extends a.Disposable{constructor(i){super(),this._renderManagerService=i,this._initialize(),this._commandExecutedListener()}_initialize(){this._initialRenderRefresh()}_initialRenderRefresh(){this._renderManagerService.currentRender$.subscribe(i=>{var o;if(i==null)return;const e=this._renderManagerService.getRenderById(i);if(e==null)return;const{mainComponent:t}=e,n=t,r=(o=n.getSkeleton())==null?void 0:o.getPageSize();n.onPageRenderObservable.add(s=>{if([a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(i))return;const{page:c,pageLeft:l,pageTop:d,ctx:u}=s,{width:p,height:h}=c;u.save(),u.translate(l-.5,d-.5),v.Rect.drawWith(u,{width:(r==null?void 0:r.width)||p,height:(r==null?void 0:r.height)||h,strokeWidth:1,stroke:pn,fill:fn,zIndex:3}),u.restore()})})}_commandExecutedListener(){}};Me=hn([a.OnLifecycle(a.LifecycleStages.Rendered,Me),gn(0,v.IRenderManagerService)],Me);var _n=Object.defineProperty,Sn=Object.getOwnPropertyDescriptor,vn=(i,e,t,n)=>{for(var r=n>1?void 0:n?Sn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&_n(e,t,r),r},k=(i,e)=>(t,n)=>e(t,n,i);let Oe=class extends a.Disposable{constructor(e,t,n,r,o,s){super();S(this,"_moveInObserver");S(this,"_moveOutObserver");S(this,"_downObserver");S(this,"_dblClickObserver");S(this,"_tripleClickObserver");S(this,"_loadedMap",new Set);this._docSkeletonManagerService=e,this._currentUniverService=t,this._commandService=n,this._renderManagerService=r,this._textSelectionRenderManager=o,this._textSelectionManagerService=s,this._renderManagerService.currentRender$.subscribe(c=>{c!=null&&this._currentUniverService.getUniverDocInstance(c)!=null&&(this._loadedMap.has(c)||(this._initialMain(c),this._loadedMap.add(c)))}),this._initialize()}dispose(){this._renderManagerService.getRenderAll().forEach(e=>{const{mainComponent:t}=e;t!=null&&(t.onPointerEnterObserver.remove(this._moveInObserver),t.onPointerLeaveObserver.remove(this._moveOutObserver),t.onPointerDownObserver.remove(this._downObserver),t.onDblclickObserver.remove(this._dblClickObserver),t.onTripleClickObserver.remove(this._tripleClickObserver))})}_initialize(){this._onChangeListener(),this._skeletonListener(),this._userActionSyncListener(),this._commandExecutedListener()}_initialMain(e){const t=this._getDocObjectById(e);if(t==null)return;const{document:n,scene:r}=t,o=r.getViewport(G.VIEW_MAIN);this._moveInObserver=n.onPointerEnterObserver.add(()=>{n.cursor=v.CURSOR_TYPE.TEXT}),this._moveOutObserver=n.onPointerLeaveObserver.add(()=>{n.cursor=v.CURSOR_TYPE.DEFAULT,r.resetCursor()}),this._downObserver=n==null?void 0:n.onPointerDownObserver.add((s,c)=>{this._currentUniverService.getCurrentUniverDocInstance().getUnitId()!==e&&this._currentUniverService.setCurrentUniverDocInstance(e),this._textSelectionRenderManager.eventTrigger(s,n.getOffsetConfig(),o),s.button!==2&&c.stopPropagation()}),this._dblClickObserver=n==null?void 0:n.onDblclickObserver.add(s=>{this._textSelectionRenderManager.handleDblClick(s,n.getOffsetConfig(),o)}),this._tripleClickObserver=n==null?void 0:n.onTripleClickObserver.add(s=>{this._textSelectionRenderManager.handleTripleClick(s,n.getOffsetConfig(),o)})}_onChangeListener(){this._textSelectionManagerService.textSelectionInfo$.subscribe(e=>{var c,l;const t=(c=this._textSelectionManagerService.getCurrentSelection())==null?void 0:c.unitId;if(this._textSelectionRenderManager.removeAllTextRanges(),e==null||t==null)return;const n=(l=this._docSkeletonManagerService.getCurrent())==null?void 0:l.skeleton,r=this._getDocObjectById(t);if(r==null||n==null)return;const{scene:o,document:s}=r;this._textSelectionRenderManager.addTextRanges(e,{scene:o,skeleton:n,documentOffsetConfig:s.getOffsetConfig()})})}_userActionSyncListener(){this._textSelectionRenderManager.textSelection$.subscribe(e=>{const t=this._docSkeletonManagerService.getCurrent();if(t==null)return;const{unitId:n}=t;this._commandService.executeCommand(We.id,{unitId:n,pluginName:J,ranges:e})})}_getDocObjectById(e){return dt(e,this._renderManagerService)}_commandExecutedListener(){const e=[A.id];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{var n;if(e.includes(t.id)){const r=t.params,{unitId:o}=r,s=(n=this._textSelectionManagerService.getCurrentSelection())==null?void 0:n.unitId;if(o!==s)return;this._textSelectionManagerService.refreshSelection()}}))}_skeletonListener(){this._docSkeletonManagerService.currentSkeleton$.subscribe(e=>{if(e==null)return;const{unitId:t,skeleton:n}=e,r=this._renderManagerService.getRenderById(t);if(r==null)return;const{scene:o,mainComponent:s}=r,c=o.getViewport(G.VIEW_MAIN);this._textSelectionRenderManager.changeRuntime(n,o,c,s.getOffsetConfig()),this._textSelectionManagerService.setCurrentSelectionNotRefresh({pluginName:J,unitId:t})})}};Oe=vn([a.OnLifecycle(a.LifecycleStages.Rendered,Oe),k(0,O.Inject(exports.DocSkeletonManagerService)),k(1,a.IUniverInstanceService),k(2,a.ICommandService),k(3,v.IRenderManagerService),k(4,v.ITextSelectionRenderManager),k(5,O.Inject(x))],Oe);var In=Object.defineProperty,Cn=Object.getOwnPropertyDescriptor,Mn=(i,e,t,n)=>{for(var r=n>1?void 0:n?Cn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&In(e,t,r),r},Z=(i,e)=>(t,n)=>e(t,n,i);let Re=class extends a.Disposable{constructor(e,t,n,r,o){super();S(this,"_initializedRender",new Set);this._docSkeletonManagerService=e,this._currentUniverService=t,this._commandService=n,this._renderManagerService=r,this._textSelectionManagerService=o,this._initialize()}dispose(){super.dispose()}_initialize(){this._skeletonListener(),this._commandExecutedListener(),this._initialRenderRefresh()}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeleton$.subscribe(e=>{if(e==null)return;const{unitId:t}=e,n=this._renderManagerService.getRenderById(t);if(n==null||this._initializedRender.has(t)||[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(t))return;this._initializedRender.add(t);const{scene:r}=n;this.disposeWithMe(a.toDisposable(r.onMouseWheelObserver.add(o=>{if(!o.ctrlKey)return;const s=Math.abs(o.deltaX);let c=s<40?.2:s<80?.4:.2;c*=o.deltaY>0?-1:1,r.scaleX<1&&(c/=2);const l=this._currentUniverService.getCurrentUniverDocInstance(),d=l.zoomRatio;let u=+parseFloat(`${d+c}`).toFixed(1);u=u>=4?4:u<=.1?.1:u,this._commandService.executeCommand(ot.id,{zoomRatio:u,workbookId:l.getUnitId()}),o.preventDefault()})))})}_skeletonListener(){this.disposeWithMe(a.toDisposable(this._docSkeletonManagerService.currentSkeletonBefore$.subscribe(e=>{if(e==null)return;const n=this._currentUniverService.getCurrentUniverDocInstance().zoomRatio||1;this._updateViewZoom(n,!1)})))}_commandExecutedListener(){const e=[A.id];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{if(e.includes(t.id)){const n=this._currentUniverService.getCurrentUniverDocInstance(),r=t.params,{unitId:o}=r;if(o!==n.getUnitId())return;const s=n.zoomRatio||1;this._updateViewZoom(s)}}))}_updateViewZoom(e,t=!0){var r;const n=this._getDocObject();n!=null&&(n.scene.scale(e,e),this._calculatePagePosition(n,e),t&&this._textSelectionManagerService.refreshSelection(),(r=n.scene.getTransformer())==null||r.hideControl())}_calculatePagePosition(e,t){const{document:n,scene:r}=e,o=r==null?void 0:r.getParent(),{width:s,height:c,pageMarginLeft:l,pageMarginTop:d}=n;if(o==null||s===1/0||c===1/0)return;const{width:u,height:p}=o;let h=0,m=0,g=0,f=0,_=1/0;u>(s+l*2)*t?(h=u/2-s*t/2,h/=t,g=(u-l*2)/t,_=0):(h=l,g=s+l*2,_=(g-u/t)/2),p>c?(m=p/2-c/2,f=(p-d*2)/t):(m=d,f=c+d*2),r.resize(g,f+200),n.translate(h,m);const C=r.getViewport(G.VIEW_MAIN);if(_!==1/0&&C!=null){const M=C.getBarScroll(_,0).x;C.scrollTo({x:M})}return this}_getDocObject(){return H(this._currentUniverService,this._renderManagerService)}};Re=Mn([a.OnLifecycle(a.LifecycleStages.Rendered,Re),Z(0,O.Inject(exports.DocSkeletonManagerService)),Z(1,a.IUniverInstanceService),Z(2,a.ICommandService),Z(3,v.IRenderManagerService),Z(4,O.Inject(x))],Re);const On={id:Ue.id,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),binding:R.KeyCode.ENTER},Rn={id:Q.id,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),binding:R.KeyCode.BACKSPACE},yn={id:be.id,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),binding:R.KeyCode.DELETE},Dn={id:U.id,binding:R.KeyCode.ARROW_UP,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.UP}},xn={id:U.id,binding:R.KeyCode.ARROW_DOWN,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.DOWN}},bn={id:U.id,binding:R.KeyCode.ARROW_LEFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.LEFT}},Tn={id:U.id,binding:R.KeyCode.ARROW_RIGHT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.RIGHT}},Pn={id:N.id,binding:R.KeyCode.ARROW_UP|R.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.UP}},En={id:N.id,binding:R.KeyCode.ARROW_DOWN|R.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.DOWN}},Un={id:N.id,binding:R.KeyCode.ARROW_LEFT|R.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.LEFT}},Nn={id:N.id,binding:R.KeyCode.ARROW_RIGHT|R.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.RIGHT}},wn={id:st.id,binding:R.KeyCode.A|R.MetaKeys.CTRL_COMMAND,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.FOCUSING_EDITOR)};var Ln=Object.defineProperty,An=Object.getOwnPropertyDescriptor,$n=(i,e,t,n)=>{for(var r=n>1?void 0:n?An(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Ln(e,t,r),r},ce=(i,e)=>(t,n)=>e(t,n,i);let ye=class{constructor(i,e,t,n){S(this,"_scene");S(this,"_currentDocumentModel");S(this,"_loadedMap",new Set);this._renderManagerService=i,this._configService=e,this._currentUniverService=t,this._docViewModelManagerService=n,this._initialize()}_initialize(){this._currentUniverService.currentDoc$.subscribe(i=>{if(i==null)return;this._currentDocumentModel=i;const e=i.getUnitId();this._docViewModelManagerService.setCurrent(e),this._loadedMap.has(e)||(this._addNewRender(),this._loadedMap.add(e))})}_addNewRender(){const i=this._currentDocumentModel,e=i.getUnitId(),t=i.getContainer(),n=i.getParentRenderUnitId();if(t!=null&&n!=null)throw new Error("container or parentRenderUnitId can only exist one");t==null&&n!=null?this._renderManagerService.createRenderWithParent(e,n):this._renderManagerService.createRender(e);const r=this._renderManagerService.getRenderById(e);if(r==null)return;const{scene:o,engine:s}=r;o.openTransformer(),this._scene=o;const c=new v.Viewport(G.VIEW_MAIN,o,{left:0,top:0,bottom:0,right:0,isWheelPreventDefaultX:!0});o.addViewport(c).attachControl(),o.on(v.EVENT_TYPE.wheel,(u,p)=>{const h=u;if(h.ctrlKey){const m=Math.abs(h.deltaX);let g=m<40?.2:m<80?.4:.2;g*=h.deltaY>0?-1:1,o.scaleX<1&&(g/=2),o.scaleX+g>4?o.scale(4,4):o.scaleX+g<.1?o.scale(.1,.1):h.preventDefault()}else c.onMouseWheel(h,p)}),this._configService.getConfig("hasScroll")!==!1&&e!==a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY&&new v.ScrollBar(c),o.addLayer(v.Layer.create(o,[],Ae),v.Layer.create(o,[],et)),this._addComponent(r),this._currentDocumentModel.getShouldRenderLoopImmediately()&&s.runRenderLoop(()=>{o.render()}),this._renderManagerService.setCurrent(e)}_addComponent(i){const e=this._scene,t=this._currentDocumentModel,n=new v.Documents(xe.MAIN,void 0,{pageMarginLeft:t.documentStyle.marginLeft||0,pageMarginTop:t.documentStyle.marginTop||0});n.zIndex=tt,i.mainComponent=n,i.components.set(xe.MAIN,n),e.addObjects([n],Ae)}};ye=$n([a.OnLifecycle(a.LifecycleStages.Ready,ye),ce(0,v.IRenderManagerService),ce(1,a.IConfigService),ce(2,a.IUniverInstanceService),ce(3,O.Inject(exports.DocViewModelManagerService))],ye);var Bn=Object.defineProperty,jn=Object.getOwnPropertyDescriptor,Fn=(i,e,t,n)=>{for(var r=n>1?void 0:n?jn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Bn(e,t,r),r},le=(i,e)=>(t,n)=>e(t,n,i);const kn={hasScroll:!0},Vn="docs";var Le;exports.UniverDocsPlugin=(Le=class extends a.Plugin{constructor(t={},n,r,o,s){super(Vn);S(this,"_config");this._injector=n,this._localeService=r,this._configService=o,this._currentUniverService=s,this._config=Object.assign(kn,t),this._initializeDependencies(n),this._initializeCommands()}initialize(){}_initializeCommands(){[U,N,Q,be,V,ee,te,ne,z,W,re,ze,Ue,Ne,Te,Fe,Ve,I,nt,rt,ot,A,We,st].forEach(t=>{this._injector.get(a.ICommandService).registerCommand(t)}),[Dn,xn,Tn,bn,Pn,En,Un,Nn,wn,Rn,yn,On].forEach(t=>{this._injector.get(R.IShortcutService).registerShortcut(t)})}onReady(){this.initialize()}_initializeDependencies(t){[[ye],[exports.DocSkeletonManagerService],[exports.DocViewModelManagerService],[ke],[at,{useClass:je}],[v.ITextSelectionRenderManager,{useClass:v.TextSelectionRenderManager}],[x],[pe],[Me],[Oe],[Ce],[_e],[ge],[Se],[he],[ve],[Ie],[Re],[fe]].forEach(n=>t.add(n))}},S(Le,"type",a.PluginType.Doc),Le);exports.UniverDocsPlugin=Fn([le(1,O.Inject(O.Injector)),le(2,O.Inject(a.LocaleService)),le(3,a.IConfigService),le(4,a.IUniverInstanceService)],exports.UniverDocsPlugin);exports.BreakLineCommand=Ue;exports.CoverContentCommand=rt;exports.DOCS_COMPONENT_DEFAULT_Z_INDEX=tt;exports.DOCS_COMPONENT_HEADER_LAYER_INDEX=et;exports.DOCS_COMPONENT_MAIN_LAYER_INDEX=Ae;exports.DOCS_VIEW_KEY=xe;exports.DeleteCommand=Te;exports.DeleteLeftCommand=Q;exports.DocCopyCommand=de;exports.DocCutCommand=ue;exports.DocPasteCommand=me;exports.IMEInputCommand=Ve;exports.InsertCommand=Ne;exports.MoveCursorOperation=U;exports.MoveSelectionOperation=N;exports.NORMAL_TEXT_SELECTION_PLUGIN_NAME=J;exports.ReplaceContentCommand=nt;exports.RichTextEditingMutation=I;exports.SetInlineFormatBoldCommand=V;exports.SetInlineFormatCommand=ze;exports.SetInlineFormatFontFamilyCommand=W;exports.SetInlineFormatFontSizeCommand=z;exports.SetInlineFormatItalicCommand=ee;exports.SetInlineFormatStrikethroughCommand=ne;exports.SetInlineFormatTextColorCommand=re;exports.SetInlineFormatUnderlineCommand=te;exports.SetTextSelectionsOperation=We;exports.TextSelectionManagerService=x;exports.UpdateCommand=Fe;exports.VIEWPORT_KEY=G;exports.getDocObject=H;
1
+ "use strict";var ut=Object.defineProperty;var mt=(i,e,t)=>e in i?ut(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var v=(i,e,t)=>(mt(i,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@univerjs/core"),O=require("@univerjs/engine-render"),W=require("rxjs"),x=require("@univerjs/ui"),T=require("@wendellhu/redi");function ee(i,e){const n=i.getCurrentUniverDocInstance().getUnitId(),r=e.getRenderById(n);if(r==null)return;const{mainComponent:o,scene:s,engine:c}=r;return{document:o,scene:s,engine:c}}function gt(i,e){const t=e.getRenderById(i);if(t==null)return;const{components:n,mainComponent:r,scene:o,engine:s}=t;return{document:r,scene:o,engine:s}}var Ie=(i=>(i.MAIN="__Document_Render_Main__",i))(Ie||{}),G=(i=>(i.VIEW_MAIN="viewMain",i.VIEW_TOP="viewTop",i.VIEW_LEFT="viewLeft",i.VIEW_LEFT_TOP="viewLeftTop",i))(G||{});const ae=0,Qe=2,et=10,pt="normalTextSelectionPluginName",Ee={id:"doc.operation.set-selections",type:a.CommandType.OPERATION,handler:(i,e)=>!0};var ft=Object.defineProperty,ht=Object.getOwnPropertyDescriptor,St=(i,e,t,n)=>{for(var r=n>1?void 0:n?ht(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&ft(e,t,r),r},ze=(i,e)=>(t,n)=>e(t,n,i);function We(i){const{startOffset:e,endOffset:t,collapsed:n}=i;return{startOffset:e,endOffset:t,collapsed:n}}exports.TextSelectionManagerService=class extends a.RxDisposable{constructor(t,n){super();v(this,"_currentSelection",null);v(this,"_textSelectionInfo",new Map);v(this,"_textSelection$",new W.BehaviorSubject(null));v(this,"textSelection$",this._textSelection$.asObservable());this._textSelectionRenderManager=t,this._commandService=n,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(t){this._currentSelection=t,this._refresh(t)}setCurrentSelectionNotRefresh(t){this._currentSelection=t}getSelections(){var t;return(t=this._getTextRanges(this._currentSelection))==null?void 0:t.textRanges}getActiveRange(){const t=this._getTextRanges(this._currentSelection);if(t==null)return;const{textRanges:n,segmentId:r,style:o}=t,s=n.find(m=>m.isActive());if(s==null)return null;const{startOffset:c,endOffset:l,collapsed:u,startNodePosition:d,endNodePosition:g,direction:p}=s;return c==null||l==null?null:{startOffset:c,endOffset:l,collapsed:u,startNodePosition:d,endNodePosition:g,direction:p,segmentId:r,style:o}}add(t){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:t,segmentId:"",style:O.NORMAL_TEXT_SELECTION_PLUGIN_STYLE})}replaceTextRanges(t){this._currentSelection!=null&&(this._textSelectionRenderManager.removeAllTextRanges(),this._textSelectionRenderManager.addTextRanges(t))}_syncSelectionFromRenderService(){this._textSelectionRenderManager.textSelectionInner$.pipe(W.takeUntil(this.dispose$)).subscribe(t=>{t!=null&&this._replaceTextRangesWithNoRefresh(t)})}_replaceTextRangesWithNoRefresh(t){if(this._currentSelection==null)return;const n={...this._currentSelection,...t};this._replaceByParam(n),this._textSelection$.next(n);const{unitId:r,subUnitId:o,segmentId:s,style:c,textRanges:l}=n;this._commandService.executeCommand(Ee.id,{unitId:r,subUnitId:o,segmentId:s,style:c,ranges:l.map(We)})}_getTextRanges(t){var o;if(t==null)return;const{unitId:n,subUnitId:r=""}=t;return(o=this._textSelectionInfo.get(n))==null?void 0:o.get(r)}_refresh(t){const n=this._getTextRanges(t);this._textSelectionRenderManager.removeAllTextRanges(),n&&Array.isArray(n.textRanges)&&n.textRanges.length&&this._textSelectionRenderManager.addTextRanges(n.textRanges.map(We))}_replaceByParam(t){const{unitId:n,subUnitId:r,style:o,segmentId:s,textRanges:c}=t;this._textSelectionInfo.has(n)||this._textSelectionInfo.set(n,new Map),this._textSelectionInfo.get(n).set(r,{textRanges:c,style:o,segmentId:s})}_addByParam(t){const{unitId:n,subUnitId:r,textRanges:o,style:s,segmentId:c}=t;this._textSelectionInfo.has(n)||this._textSelectionInfo.set(n,new Map);const l=this._textSelectionInfo.get(n);l.has(r)?l.get(r).textRanges.push(...o):l.set(r,{textRanges:o,style:s,segmentId:c})}};exports.TextSelectionManagerService=St([ze(0,O.ITextSelectionRenderManager),ze(1,a.ICommandService)],exports.TextSelectionManagerService);function Re(i,e="",t=0){const{startOffset:n,endOffset:r}=i,o=[],s=n-t,c=r-t;return s>0&&o.push({t:"r",len:s,segmentId:e}),o.push({t:"d",len:c-s,line:0,segmentId:e}),o}var _t=Object.defineProperty,vt=Object.getOwnPropertyDescriptor,Ct=(i,e,t,n)=>{for(var r=n>1?void 0:n?vt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&_t(e,t,r),r},It=(i,e)=>(t,n)=>e(t,n,i);exports.DocViewModelManagerService=class{constructor(e){v(this,"_currentViewModelUnitId","");v(this,"_docViewModelMap",new Map);v(this,"_currentDocViewModel$",new W.BehaviorSubject(null));v(this,"currentDocViewModel$",this._currentDocViewModel$.asObservable());this._currentUniverService=e}dispose(){this._currentDocViewModel$.complete(),this._docViewModelMap=new Map}getCurrent(){return this._docViewModelMap.get(this._currentViewModelUnitId)}getViewModel(e){var t;return(t=this._docViewModelMap.get(e))==null?void 0:t.docViewModel}setCurrent(e){var n;const t=this._currentUniverService.getUniverDocInstance(e);if(t==null)throw new Error(`Document data model with id ${e} not found when build view model.`);if(t.getBody()!=null){if(!this._docViewModelMap.has(e)){const r=this._buildDocViewModel(t);this._docViewModelMap.set(e,{unitId:e,docViewModel:r})}if(e===a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY){const r=(n=this._docViewModelMap.get(e))==null?void 0:n.docViewModel;if(r==null)return;r.reset(t)}this._currentViewModelUnitId=e,this._currentDocViewModel$.next(this.getCurrent())}}_buildDocViewModel(e){return new O.DocumentViewModel(e)}};exports.DocViewModelManagerService=Ct([It(0,a.IUniverInstanceService)],exports.DocViewModelManagerService);const _={id:"doc.mutation.rich-text-editing",type:a.CommandType.MUTATION,handler:(i,e)=>{const{unitId:t,mutations:n}=e,o=i.get(a.IUniverInstanceService).getUniverDocInstance(t),c=i.get(exports.DocViewModelManagerService).getViewModel(t);if(o==null||c==null)throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${t}`);const l=o.apply(n),u=n[0].segmentId,d=o.getSelfOrHeaderFooterModel(u);return c.getSelfOrHeaderFooterViewModel(u).reset(d),{unitId:t,mutations:l}}},ye={id:"doc.command.insert-text",type:a.CommandType.COMMAND,handler:async(i,e)=>{const t=i.get(a.IUndoRedoService),n=i.get(a.ICommandService),r=i.get(exports.TextSelectionManagerService),{range:o,segmentId:s,body:c,unitId:l,textRanges:u}=e,{startOffset:d,collapsed:g}=o,p={id:_.id,params:{unitId:l,mutations:[]}};g?p.params.mutations.push({t:"r",len:d,segmentId:s}):p.params.mutations.push(...Re(o,s)),p.params.mutations.push({t:"i",body:c,len:c.dataStream.length,line:0,segmentId:s});const m=n.syncExecuteCommand(p.id,p.params);return r.replaceTextRanges(u),m?(t.pushUndoRedo({unitID:l,undoMutations:[{id:_.id,params:m}],redoMutations:[{id:_.id,params:p.params}],undo(){return n.syncExecuteCommand(_.id,m),r.replaceTextRanges([o]),!0},redo(){return n.syncExecuteCommand(_.id,p.params),r.replaceTextRanges(u),!0}}),!0):!1}};var L=(i=>(i[i.LEFT=0]="LEFT",i[i.RIGHT=1]="RIGHT",i))(L||{});const xe={id:"doc.command.delete-text",type:a.CommandType.COMMAND,handler:async(i,e)=>{const t=i.get(a.ICommandService),n=i.get(a.IUndoRedoService),r=i.get(exports.TextSelectionManagerService),{range:o,segmentId:s,unitId:c,direction:l,textRanges:u,len:d=1}=e,{startOffset:g}=o,p={id:_.id,params:{unitId:c,mutations:[]}};g>0&&p.params.mutations.push({t:"r",len:l===0?g-d:g,segmentId:s}),p.params.mutations.push({t:"d",len:d,line:0,segmentId:s});const m=t.syncExecuteCommand(p.id,p.params);return r.replaceTextRanges(u),m&&n.pushUndoRedo({unitID:c,undoMutations:[{id:_.id,params:m}],redoMutations:[{id:_.id,params:p.params}],undo(){return t.syncExecuteCommand(_.id,m),r.replaceTextRanges([o]),!0},redo(){return t.syncExecuteCommand(_.id,p.params),r.replaceTextRanges(u),!0}}),!1}},Pe={id:"doc.command.update-text",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{range:t,segmentId:n,updateBody:r,coverType:o,unitId:s,textRanges:c}=e,l=i.get(a.ICommandService),u=i.get(a.IUndoRedoService),d=i.get(exports.TextSelectionManagerService),g={id:_.id,params:{unitId:s,mutations:[]}},{startOffset:p,endOffset:m}=t;g.params.mutations.push({t:"r",len:p,segmentId:n}),g.params.mutations.push({t:"r",body:r,len:m-p,segmentId:n,coverType:o});const f=l.syncExecuteCommand(g.id,g.params);return d.replaceTextRanges(c),f?(u.pushUndoRedo({unitID:s,undoMutations:[{id:_.id,params:f}],redoMutations:[{id:_.id,params:g.params}],undo(){return l.syncExecuteCommand(_.id,f),d.replaceTextRanges(c),!0},redo(){return l.syncExecuteCommand(_.id,g.params),d.replaceTextRanges(c),!0}}),!0):!1}};function Mt(i,e){const t=[];for(let n=0,r=i.length;n<r;n++)i[n]===a.DataStreamTreeTokenType.PARAGRAPH&&t.push({startIndex:n});if(e)for(const n of t)e.bullet&&(n.bullet=a.Tools.deepClone(e.bullet)),e.paragraphStyle&&(n.paragraphStyle=a.Tools.deepClone(e.paragraphStyle));return t}const Ne={id:"doc.command.break-line",type:a.CommandType.COMMAND,handler:async i=>{var f;const e=i.get(exports.TextSelectionManagerService),t=i.get(a.IUniverInstanceService),n=i.get(a.ICommandService),r=e.getActiveRange();if(r==null)return!1;const o=t.getCurrentUniverDocInstance(),s=o.getUnitId(),{startOffset:c,segmentId:l,style:u}=r,d=[{startOffset:c+1,endOffset:c+1,style:u}],p=(((f=o.getBody())==null?void 0:f.paragraphs)??[]).find(h=>h.startIndex>=c);return await n.executeCommand(ye.id,{unitId:s,body:{dataStream:a.DataStreamTreeTokenType.PARAGRAPH,paragraphs:Mt(a.DataStreamTreeTokenType.PARAGRAPH,p)},range:r,textRanges:d,segmentId:l})}},ce={id:x.CopyCommand.id,name:"doc.command.copy",type:a.CommandType.COMMAND,multi:!0,priority:999,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.FOCUSING_EDITOR),handler:async()=>!0},le={id:x.CutCommand.id,name:"doc.command.cut",type:a.CommandType.COMMAND,multi:!0,priority:999,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.FOCUSING_EDITOR),handler:async()=>!0},de={id:x.PasteCommand.id,name:"doc.command.paste",type:a.CommandType.COMMAND,multi:!0,priority:999,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.FOCUSING_EDITOR),handler:async()=>!0};var Ot=Object.defineProperty,Rt=Object.getOwnPropertyDescriptor,yt=(i,e,t,n)=>{for(var r=n>1?void 0:n?Rt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Ot(e,t,r),r},Ge=(i,e)=>(t,n)=>e(t,n,i);exports.DocSkeletonManagerService=class{constructor(e,t){v(this,"_currentSkeletonUnitId","");v(this,"_docSkeletonMap",new Map);v(this,"_currentSkeleton$",new W.BehaviorSubject(null));v(this,"currentSkeleton$",this._currentSkeleton$.asObservable());v(this,"_currentSkeletonBefore$",new W.BehaviorSubject(null));v(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());this._localeService=e,this._docViewModelManagerService=t,this.initialize()}initialize(){this._docViewModelManagerService.currentDocViewModel$.subscribe(e=>{e!=null&&this._setCurrent(e)})}dispose(){this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete(),this._docSkeletonMap=new Map}getCurrent(){return this.getSkeletonByUnitId(this._currentSkeletonUnitId)}makeDirtyCurrent(e=!0){this.makeDirty(this._currentSkeletonUnitId,e)}makeDirty(e,t=!0){const n=this.getSkeletonByUnitId(e);n!=null&&(n.dirty=t)}_setCurrent(e){const{unitId:t}=e;if(this._docSkeletonMap.has(t)){const n=this.getSkeletonByUnitId(t);n.skeleton.calculate(),n.dirty=!0}else{const n=this._buildSkeleton(e.docViewModel);n.calculate(),this._docSkeletonMap.set(t,{unitId:t,skeleton:n,dirty:!1})}return this._currentSkeletonUnitId=t,this._currentSkeletonBefore$.next(this.getCurrent()),this._currentSkeleton$.next(this.getCurrent()),this.getCurrent()}getSkeletonByUnitId(e){return this._docSkeletonMap.get(e)}_buildSkeleton(e){return O.DocumentSkeleton.create(e,this._localeService)}};exports.DocSkeletonManagerService=yt([Ge(0,T.Inject(a.LocaleService)),Ge(1,T.Inject(exports.DocViewModelManagerService))],exports.DocSkeletonManagerService);const He={id:"doc.command.inner-paste",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{segmentId:t,body:n,textRanges:r}=e,o=i.get(a.IUndoRedoService),s=i.get(a.ICommandService),c=i.get(exports.TextSelectionManagerService),l=i.get(a.IUniverInstanceService),u=c.getSelections();if(!Array.isArray(u)||u.length===0)return!1;const g=l.getCurrentUniverDocInstance().getUnitId(),p={id:_.id,params:{unitId:g,mutations:[]}},m=new a.MemoryCursor;m.reset();for(const h of u){const{startOffset:S,endOffset:C,collapsed:I}=h,D=S-m.cursor;I?p.params.mutations.push({t:"r",len:D,segmentId:t}):p.params.mutations.push(...Re(h,t,m.cursor)),p.params.mutations.push({t:"i",body:n,len:n.dataStream.length,line:0,segmentId:t}),m.reset(),m.moveCursor(C)}const f=s.syncExecuteCommand(p.id,p.params);return c.replaceTextRanges(r),f?(o.pushUndoRedo({unitID:g,undoMutations:[{id:_.id,params:f}],redoMutations:[{id:_.id,params:p.params}],undo(){return s.syncExecuteCommand(_.id,f),c.replaceTextRanges(u),!0},redo(){return s.syncExecuteCommand(_.id,p.params),c.replaceTextRanges(r),!0}}),!0):!1}},Me={id:"doc.command.inner-cut",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{segmentId:t,textRanges:n}=e,r=i.get(a.IUndoRedoService),o=i.get(a.ICommandService),s=i.get(exports.TextSelectionManagerService),c=i.get(a.IUniverInstanceService),l=s.getSelections();if(!Array.isArray(l)||l.length===0)return!1;const u=c.getCurrentUniverDocInstance().getUnitId(),d=c.getUniverDocInstance(u),g=a.getDocsUpdateBody(d.snapshot,t);if(g==null)return!1;const p={id:_.id,params:{unitId:u,mutations:[]}},m=new a.MemoryCursor;m.reset();for(const h of l){const{startOffset:S,endOffset:C,collapsed:I}=h,D=S-m.cursor;I?p.params.mutations.push({t:"r",len:D,segmentId:t}):p.params.mutations.push(...xt(h,g,t,m.cursor)),m.reset(),m.moveCursor(C)}const f=o.syncExecuteCommand(p.id,p.params);return s.replaceTextRanges(n),f?(r.pushUndoRedo({unitID:u,undoMutations:[{id:_.id,params:f}],redoMutations:[{id:_.id,params:p.params}],undo(){return o.syncExecuteCommand(_.id,f),s.replaceTextRanges(l),!0},redo(){return o.syncExecuteCommand(_.id,p.params),s.replaceTextRanges(n),!0}}),!0):!1}};function xt(i,e,t="",n=0){const{startOffset:r,endOffset:o}=i,s=[],{paragraphs:c=[]}=e,l=r-n,u=o-n,d=c==null?void 0:c.find(g=>g.startIndex-n>=l&&g.startIndex-n<=u);if(l>0&&s.push({t:"r",len:l,segmentId:t}),d&&d.startIndex-n>l){const g=d.startIndex-n;s.push({t:"d",len:g-l,line:0,segmentId:t}),s.push({t:"r",len:1,segmentId:t}),u>g+1&&s.push({t:"d",len:u-g-1,line:0,segmentId:t})}else s.push({t:"d",len:u-l,line:0,segmentId:t});return s}const we={id:"doc.command.delete-left",type:a.CommandType.COMMAND,handler:async i=>{var y;const e=i.get(exports.TextSelectionManagerService),t=i.get(exports.DocSkeletonManagerService),n=i.get(a.IUniverInstanceService),r=i.get(a.ICommandService),o=e.getActiveRange(),s=e.getSelections(),c=(y=t.getCurrent())==null?void 0:y.skeleton;let l;if(o==null||c==null||s==null)return!1;const u=n.getCurrentUniverDocInstance(),{startOffset:d,collapsed:g,segmentId:p,style:m}=o;if(d===0&&g)return!0;const f=c.findNodeByCharIndex(d),h=O.hasListSpan(f),S=O.isIndentBySpan(f,u.body);let C=d;const I=c.findNodeByCharIndex(d-1);if(O.isFirstSpan(f)&&I!==f&&(h===!0||S===!0)){const M=O.getParagraphBySpan(f,u.body);if(M==null)return!1;const b=M==null?void 0:M.startIndex,U={startIndex:0},E=M.paragraphStyle;if(h===!0){const R=M.paragraphStyle;R&&(U.paragraphStyle=R)}else if(S===!0){const R=M.bullet;R&&(U.bullet=R),E!=null&&(U.paragraphStyle={...E},delete U.paragraphStyle.hanging,delete U.paragraphStyle.indentStart)}const P=[{startOffset:C,endOffset:C,style:m}];l=await r.executeCommand(Pe.id,{unitId:u.getUnitId(),updateBody:{dataStream:"",paragraphs:[{...U}]},range:{startOffset:b,endOffset:b+1},textRanges:P,coverType:a.UpdateDocsAttributeType.REPLACE,segmentId:p})}else if(g===!0)if(I.content==="\r")l=await r.executeCommand(Ae.id,{direction:L.LEFT,range:o});else{C-=I.count;const M=[{startOffset:C,endOffset:C,style:m}];l=await r.executeCommand(xe.id,{unitId:u.getUnitId(),range:o,segmentId:p,direction:L.LEFT,len:I.count,textRanges:M})}else{const M=tt(o,s);l=await r.executeCommand(Me.id,{segmentId:p,textRanges:M})}return l}},Le={id:"doc.command.delete-right",type:a.CommandType.COMMAND,handler:async i=>{var f;const e=i.get(exports.TextSelectionManagerService),t=i.get(exports.DocSkeletonManagerService),n=i.get(a.IUniverInstanceService),r=i.get(a.ICommandService),o=e.getActiveRange(),s=e.getSelections(),c=(f=t.getCurrent())==null?void 0:f.skeleton;let l;if(o==null||c==null||s==null)return!1;const u=n.getCurrentUniverDocInstance(),{startOffset:d,collapsed:g,segmentId:p,style:m}=o;if(d===u.getBody().dataStream.length-2&&g)return!0;if(g===!0){const h=c.findNodeByCharIndex(d);if(h.content==="\r")l=await r.executeCommand(Ae.id,{direction:L.RIGHT,range:o});else{const S=[{startOffset:d,endOffset:d,style:m}];l=await r.executeCommand(xe.id,{unitId:u.getUnitId(),range:o,segmentId:p,direction:L.RIGHT,textRanges:S,len:h.count})}}else{const h=tt(o,s);l=await r.executeCommand(Me.id,{segmentId:p,textRanges:h})}return l}},Ae={id:"doc.command.merge-two-paragraph",type:a.CommandType.COMMAND,handler:async(i,e)=>{var U,E,P;const t=i.get(exports.TextSelectionManagerService),n=i.get(a.IUniverInstanceService),r=i.get(a.ICommandService),o=i.get(a.IUndoRedoService),{direction:s,range:c}=e,l=t.getActiveRange(),u=t.getSelections();if(l==null||u==null)return!1;const d=n.getCurrentUniverDocInstance(),{startOffset:g,collapsed:p,segmentId:m,style:f}=l;if(!p)return!1;const h=s===L.LEFT?g:g+1,S=(P=(E=(U=d.getBody())==null?void 0:U.paragraphs)==null?void 0:E.find(R=>R.startIndex>=h))==null?void 0:P.startIndex,C=Dt(d.getBody(),h,S),I=s===L.LEFT?g-1:g,D=d.getUnitId(),y=[{startOffset:I,endOffset:I,style:f}],M={id:_.id,params:{unitId:D,mutations:[]}};M.params.mutations.push({t:"r",len:s===L.LEFT?g-1:g,segmentId:m}),C.dataStream.length&&M.params.mutations.push({t:"i",body:C,len:C.dataStream.length,line:0,segmentId:m}),M.params.mutations.push({t:"r",len:1,segmentId:m}),M.params.mutations.push({t:"d",len:S+1-h,line:0,segmentId:m});const b=r.syncExecuteCommand(M.id,M.params);return t.replaceTextRanges(y),b?(o.pushUndoRedo({unitID:D,undoMutations:[{id:_.id,params:b}],redoMutations:[{id:_.id,params:M.params}],undo(){return r.syncExecuteCommand(_.id,b),t.replaceTextRanges([c]),!0},redo(){return r.syncExecuteCommand(_.id,M.params),t.replaceTextRanges(y),!0}}),!0):!1}};function Dt(i,e,t){const{textRuns:n}=i,r=i.dataStream.substring(e,t);if(n==null)return{dataStream:r};const o=[];for(const s of n){const{st:c,ed:l}=s;l<=e||c>=t||(c<e?o.push({...s,st:0,ed:l-e}):l>t?o.push({...s,st:c-e,ed:t-e}):o.push({...s,st:c-e,ed:l-e}))}return{dataStream:r,textRuns:o}}function tt(i,e){let t=i.endOffset;for(const r of e){const{startOffset:o,endOffset:s}=r;o==null||s==null||s<=i.endOffset&&(t-=s-o)}return[{startOffset:t,endOffset:t,style:i.style}]}class Be{constructor(){v(this,"_previousActiveRange",null);v(this,"_undoMutationParamsCache",[]);v(this,"_redoMutationParamsCache",[])}clearUndoRedoMutationParamsCache(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[]}setActiveRange(e){this._previousActiveRange=e}pushUndoRedoMutationParams(e,t){this._undoMutationParamsCache.push(e),this._redoMutationParamsCache.push(t)}fetchComposedUndoRedoMutationParams(e){if(this._undoMutationParamsCache.length===0||this._previousActiveRange==null)return null;const{unitId:t}=this._undoMutationParamsCache[0],{segmentId:n,startOffset:r,collapsed:o}=this._previousActiveRange,s={unitId:t,mutations:[]},c={unitId:t,mutations:[]};return o?(s.mutations.push({t:"r",len:r,segmentId:n}),c.mutations.push({t:"r",len:r,segmentId:n})):(r>0&&s.mutations.push({t:"r",len:r,segmentId:n}),s.mutations.push(this._undoMutationParamsCache[0].mutations.find(l=>l.t==="i")),c.mutations.push(...Re(this._previousActiveRange,n))),e.length&&(s.mutations.push({t:"d",len:e.length,line:0,segmentId:n}),c.mutations.push({t:"i",body:{dataStream:e},len:e.length,line:0,segmentId:n})),{redoMutationParams:c,undoMutationParams:s,previousActiveRange:this._previousActiveRange}}dispose(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[],this._previousActiveRange=null}}const $e={id:"doc.command.ime-input",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{unitId:t,newText:n,oldTextLen:r,range:o,segmentId:s,textRanges:c,isCompositionEnd:l}=e,u=i.get(a.ICommandService),d=i.get(a.IUndoRedoService),g=i.get(exports.TextSelectionManagerService),p=i.get(Be),m={id:_.id,params:{unitId:t,mutations:[]}};o.collapsed?m.params.mutations.push({t:"r",len:o.startOffset,segmentId:s}):m.params.mutations.push(...Re(o,s)),r>0&&m.params.mutations.push({t:"d",len:r,line:0,segmentId:s}),m.params.mutations.push({t:"i",body:{dataStream:n},len:n.length,line:0,segmentId:s});const f=u.syncExecuteCommand(m.id,m.params);if(p.pushUndoRedoMutationParams(f,m.params),g.replaceTextRanges(c),l){if(f){const h=p.fetchComposedUndoRedoMutationParams(n);if(h==null)return!1;const{undoMutationParams:S,redoMutationParams:C,previousActiveRange:I}=h;return d.pushUndoRedo({unitID:t,undoMutations:[{id:_.id,params:S}],redoMutations:[{id:_.id,params:C}],undo(){return u.syncExecuteCommand(_.id,S),g.replaceTextRanges([I]),!0},redo(){return u.syncExecuteCommand(_.id,C),g.replaceTextRanges(c),!0}}),!0}}else return!!f;return!1}},H={id:"doc.command.set-inline-format-bold",type:a.CommandType.COMMAND,handler:async()=>!0},te={id:"doc.command.set-inline-format-italic",type:a.CommandType.COMMAND,handler:async()=>!0},ne={id:"doc.command.set-inline-format-underline",type:a.CommandType.COMMAND,handler:async()=>!0},re={id:"doc.command.set-inline-format-strikethrough",type:a.CommandType.COMMAND,handler:async()=>!0},A={id:"doc.command.set-inline-format-subscript",type:a.CommandType.COMMAND,handler:async()=>!0},V={id:"doc.command.set-inline-format-superscript",type:a.CommandType.COMMAND,handler:async()=>!0},K={id:"doc.command.set-inline-format-fontsize",type:a.CommandType.COMMAND,handler:async()=>!0},Y={id:"doc.command.set-inline-format-font-family",type:a.CommandType.COMMAND,handler:async()=>!0},ie={id:"doc.command.set-inline-format-text-color",type:a.CommandType.COMMAND,handler:async()=>!0},nt={[H.id]:"bl",[te.id]:"it",[ne.id]:"ul",[re.id]:"st",[K.id]:"fs",[Y.id]:"ff",[ie.id]:"cl",[A.id]:"va",[V.id]:"va"},Fe={id:"doc.command.set-inline-format",type:a.CommandType.COMMAND,handler:async(i,e)=>{const{segmentId:t,value:n,preCommandId:r}=e,o=i.get(a.IUndoRedoService),s=i.get(a.ICommandService),c=i.get(exports.TextSelectionManagerService),l=i.get(a.IUniverInstanceService),u=c.getSelections();if(!Array.isArray(u)||u.length===0)return!1;let d=l.getCurrentUniverDocInstance(),g=d.getUnitId();g===a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY&&(d=l.getUniverDocInstance(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),g=d.getUnitId());let p;switch(r){case H.id:case te.id:case ne.id:case re.id:case A.id:case V.id:{p=bt(d.getBody().textRuns,r,u);break}case K.id:case Y.id:{p=n;break}case ie.id:{p={rgb:n};break}default:throw new Error(`Unknown command: ${r} in handleInlineFormat`)}const m={id:_.id,params:{unitId:g,mutations:[]}},f=new a.MemoryCursor;f.reset();for(const C of u){const{startOffset:I,endOffset:D}=C,y={dataStream:"",textRuns:[{st:0,ed:D-I,ts:{[nt[r]]:p}}]},M=I-f.cursor;M!==0&&m.params.mutations.push({t:"r",len:M,segmentId:t}),m.params.mutations.push({t:"r",body:y,len:D-I,segmentId:t}),f.reset(),f.moveCursor(D)}const h=s.syncExecuteCommand(m.id,m.params),S=[H.id,K.id,Y.id,A.id,V.id];return S.includes(r)&&c.refreshSelection(),h?(o.pushUndoRedo({unitID:g,undoMutations:[{id:_.id,params:h}],redoMutations:[{id:_.id,params:m.params}],undo(){return s.syncExecuteCommand(_.id,h),S.includes(r)&&c.refreshSelection(),!0},redo(){return s.syncExecuteCommand(_.id,m.params),S.includes(r)&&c.refreshSelection(),!0}}),!0):!1}};function Tt(i){return i!==null&&typeof i=="object"}function bt(i,e,t){let n=0,r=0;const o=nt[e];for(;n!==i.length&&r!==t.length;){const{startOffset:s,endOffset:c}=t[r],{st:l,ed:u,ts:d}=i[n];if(c<=l)r++;else if(u<=s)n++;else{if((d==null?void 0:d[o])==null){if(/bl|it/.test(o))return a.BooleanNumber.TRUE;if(/ul|st/.test(o))return{s:a.BooleanNumber.TRUE};if(/va/.test(o))return e===A.id?a.BaselineOffset.SUBSCRIPT:a.BaselineOffset.SUPERSCRIPT}if(Tt(d==null?void 0:d[o])&&d[o].s===a.BooleanNumber.FALSE)return{s:a.BooleanNumber.TRUE};if(e===A.id&&(d==null?void 0:d[o])!==a.BaselineOffset.SUBSCRIPT)return a.BaselineOffset.SUBSCRIPT;if(e===V.id&&(d==null?void 0:d[o])!==a.BaselineOffset.SUPERSCRIPT)return a.BaselineOffset.SUPERSCRIPT;if((d==null?void 0:d[o])===a.BooleanNumber.FALSE)return a.BooleanNumber.TRUE;n++}}return/bl|it/.test(o)?a.BooleanNumber.FALSE:/ul|st/.test(o)?{s:a.BooleanNumber.FALSE}:a.BaselineOffset.NORMAL}const rt={id:"doc.command.bullet-list",type:a.CommandType.COMMAND,handler:i=>i.get(a.ICommandService).syncExecuteCommand(je.id,{listType:a.PresetListType.BULLET_LIST})},it={id:"doc.command.order-list",type:a.CommandType.COMMAND,handler:i=>i.get(a.ICommandService).syncExecuteCommand(je.id,{listType:a.PresetListType.ORDER_LIST})},je={id:"doc.command.list-operation",type:a.CommandType.COMMAND,handler:(i,e)=>{var D;const t=i.get(exports.TextSelectionManagerService),n=i.get(a.IUniverInstanceService),r=i.get(a.ICommandService),o=i.get(a.IUndoRedoService),{listType:s}=e,c=n.getCurrentUniverDocInstance(),l=t.getActiveRange(),u=(D=c.getBody())==null?void 0:D.paragraphs;if(l==null||u==null)return!1;const d=Ut(l,u),{segmentId:g}=l,p=c.getUnitId(),m=d.every(y=>{var M;return((M=y.bullet)==null?void 0:M.listType)===s}),f=6;let h=a.Tools.generateRandomId(f);if(d.length===1){const y=u.indexOf(d[0]),M=u[y-1],b=u[y+1];M&&M.bullet&&M.bullet.listType===s?h=M.bullet.listId:b&&b.bullet&&b.bullet.listType===s&&(h=b.bullet.listId)}const S={id:_.id,params:{unitId:p,mutations:[]}},C=new a.MemoryCursor;C.reset();for(const y of d){const{startIndex:M}=y;S.params.mutations.push({t:"r",len:M-C.cursor,segmentId:g});const b={...y.paragraphStyle,hanging:void 0,indentStart:void 0};S.params.mutations.push({t:"r",len:1,body:{dataStream:"",paragraphs:[m?{paragraphStyle:b,startIndex:0}:{...y,startIndex:0,bullet:{...y.bullet??{nestingLevel:0,textStyle:{fs:20}},listType:s,listId:h}}]},segmentId:g,coverType:a.UpdateDocsAttributeType.REPLACE}),C.moveCursorTo(M+1)}const I=r.syncExecuteCommand(S.id,S.params);return t.refreshSelection(),I&&o.pushUndoRedo({unitID:p,undoMutations:[{id:_.id,params:I}],redoMutations:[{id:_.id,params:S.params}],undo(){return r.syncExecuteCommand(_.id,I),t.refreshSelection(),!0},redo(){return r.syncExecuteCommand(_.id,S.params),t.refreshSelection(),!0}}),!0}};function Ut(i,e){const{startOffset:t,endOffset:n}=i,r=[];let o=0;for(const s of e){const{startIndex:c}=s;(t>o&&t<=c||n>o&&n<=c||c>=t&&c<=n)&&r.push(s),o=c}return r}const ot={id:"doc.command-replace-content",type:a.CommandType.COMMAND,handler:async(i,e)=>{var f;const{unitId:t,body:n,textRanges:r,segmentId:o=""}=e,s=i.get(a.IUniverInstanceService),c=i.get(a.ICommandService),l=i.get(exports.TextSelectionManagerService),u=i.get(a.IUndoRedoService),d=(f=s.getUniverDocInstance(t))==null?void 0:f.getSnapshot().body,g=l.getSelections();if(d==null||!Array.isArray(g)||g.length===0)return!1;const p=at(t,o,d,n),m=c.syncExecuteCommand(p.id,p.params);return l.replaceTextRanges(r),m?(u.pushUndoRedo({unitID:t,undoMutations:[{id:_.id,params:m}],redoMutations:[{id:_.id,params:p.params}],undo(){return c.syncExecuteCommand(_.id,m),l.replaceTextRanges(g),!0},redo(){return c.syncExecuteCommand(_.id,p.params),l.replaceTextRanges(r),!0}}),!0):!1}},st={id:"doc.command-cover-content",type:a.CommandType.COMMAND,handler:async(i,e)=>{var d;const{unitId:t,body:n,segmentId:r=""}=e,o=i.get(a.IUniverInstanceService),s=i.get(a.ICommandService),c=i.get(a.IUndoRedoService),l=(d=o.getUniverDocInstance(t))==null?void 0:d.getSnapshot().body;if(l==null)return!1;const u=at(t,r,l,n);return s.syncExecuteCommand(u.id,u.params),c.clearUndoRedo(t),!0}};function at(i,e,t,n){const r={id:_.id,params:{unitId:i,mutations:[]}},o=(t==null?void 0:t.dataStream.length)-2;return o>0&&r.params.mutations.push({t:"d",len:o,line:0,segmentId:e}),n.dataStream.length>0&&r.params.mutations.push({t:"i",body:n,len:n.dataStream.length,line:0,segmentId:e}),r}const B={id:"doc.operation.move-cursor",type:a.CommandType.OPERATION,handler:(i,e)=>!!e},$={id:"doc.operation.move-selection",type:a.CommandType.OPERATION,handler:(i,e)=>!!e},Et=(i,e)=>{const t=i.get(a.IUniverInstanceService).getUniverDocInstance(e.unitId),n=(t==null?void 0:t.zoomRatio)||1;return{...a.Tools.deepClone(e),zoomRatio:n}},j={id:"doc.operation.set-zoom-ratio",type:a.CommandType.OPERATION,handler:(i,e)=>{const t=i.get(a.IUniverInstanceService).getUniverDocInstance(e.unitId);if(!t)return!1;const n=t.getSnapshot();return n.settings==null?n.settings={zoomRatio:e.zoomRatio}:n.settings.zoomRatio=e.zoomRatio,!0}},ct={type:a.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(i,e)=>{const t=i.get(a.ICommandService),n=i.get(a.IUndoRedoService),r=i.get(a.IUniverInstanceService);let o=r.getCurrentUniverDocInstance().getUnitId(),s=1;if(e&&(o=e.documentId??o,s=e.zoomRatio??s),!r.getUniverDocInstance(o))return!1;const l={zoomRatio:s,unitId:o},u=Et(i,l);return t.syncExecuteCommand(j.id,l)?(n.pushUndoRedo({unitID:o,undoMutations:[{id:j.id,params:u}],redoMutations:[{id:j.id,params:l}]}),!0):!1}},lt={id:"doc.operation.select-all",type:a.CommandType.COMMAND,handler:async i=>{const e=i.get(a.IUniverInstanceService),t=i.get(exports.TextSelectionManagerService),n=e.getCurrentUniverDocInstance().getSnapshot().body;if(n==null)return!1;const r=[{startOffset:0,endOffset:n.dataStream.length-2}];return t.replaceTextRanges(r),!0}},Pt=10,Nt=6;function wt(){return a.Tools.generateRandomId(Nt)}function Lt(i){const e=i.match(/data-copy-id="([^\s]+)"/);return e&&e[1]?e[1]:null}class At{constructor(){v(this,"_cache",new a.LRUMap(Pt))}set(e,t){this._cache.set(e,t)}get(e){return this._cache.get(e)}clear(){this._cache.clear()}}const Ke=new At;function Bt(i){const e=i.style,t={};for(let n=0;n<e.length;n++){const r=e[n],o=e.getPropertyValue(r);switch(r){case"margin-top":{const s=parseInt(o);t.spaceAbove=/pt/.test(o)?be(s):s;break}case"margin-bottom":{const s=parseInt(o);t.spaceBelow=/pt/.test(o)?be(s):s;break}}}return Object.getOwnPropertyNames(t).length?t:null}function be(i){return i/.75}function Ve(i){const e=i.style,t={},n=i.tagName.toLowerCase();switch(n){case"b":case"em":case"strong":{t.bl=a.BooleanNumber.TRUE;break}case"s":{t.st={s:a.BooleanNumber.TRUE};break}case"u":{t.ul={s:a.BooleanNumber.TRUE};break}case"i":{t.it=a.BooleanNumber.TRUE;break}case"sub":case"sup":{t.va=n==="sup"?a.BaselineOffset.SUPERSCRIPT:a.BaselineOffset.SUBSCRIPT;break}}for(let r=0;r<e.length;r++){const o=e[r],s=e.getPropertyValue(o);switch(o){case"font-family":{t.ff=s;break}case"font-size":{const c=parseInt(s);Number.isNaN(c)||(t.fs=/pt$/.test(s)?be(c):c);break}case"font-style":{s==="italic"&&(t.it=a.BooleanNumber.TRUE);break}case"font-weight":{Number(s)>400&&(t.bl=a.BooleanNumber.TRUE);break}case"text-decoration":{/underline/.test(s)?t.ul={s:a.BooleanNumber.TRUE}:/overline/.test(s)?t.ol={s:a.BooleanNumber.TRUE}:/line-through/.test(s)&&(t.st={s:a.BooleanNumber.TRUE});break}case"color":{const c=new a.ColorKit(s);c.isValid&&(t.cl={rgb:c.toRgbString()});break}case"background-color":{const c=new a.ColorKit(s);c.isValid&&(t.bg={rgb:c.toRgbString()});break}}}return t}function $t(i){const e=new DOMParser,t=`<x-univer id="univer-root">${i}</x-univer>`;return e.parseFromString(t,"text/html").querySelector("#univer-root")}function Ye(i,e){const t=i.tagName.toLowerCase();return typeof e=="string"?t===e:Array.isArray(e)?e.some(n=>n===t):e(i)}const Oe=class Oe{constructor(){v(this,"styleCache",new Map);v(this,"styleRules",[]);v(this,"afterProcessRules",[])}static use(e){if(this.pluginList.includes(e))throw new Error(`Univer paste plugin ${e.name} already added`);this.pluginList.push(e)}convert(e){const t=Oe.pluginList.find(o=>o.checkPasteType(e)),n=$t(e),r={dataStream:"",textRuns:[]};return t&&(this.styleRules=[...t.stylesRules],this.afterProcessRules=[...t.afterProcessRules]),this.styleCache.clear(),this.process(null,n==null?void 0:n.childNodes,r),this.styleCache.clear(),this.styleRules=[],this.afterProcessRules=[],r}process(e,t,n){var r;for(const o of t)if(o.nodeType===Node.TEXT_NODE){const s=(r=o.nodeValue)==null?void 0:r.replace(/[\r\n]/g,"");let c;e&&this.styleCache.has(e)&&(c=this.styleCache.get(e)),n.dataStream+=s,c&&Object.getOwnPropertyNames(c).length&&n.textRuns.push({st:n.dataStream.length-s.length,ed:n.dataStream.length,ts:c})}else if(o.nodeType===Node.ELEMENT_NODE){const s=e?this.styleCache.get(e):{},c=this.styleRules.find(({filter:g})=>Ye(o,g)),l=c?c.getStyle(o):Ve(o);this.styleCache.set(o,{...s,...l});const{childNodes:u}=o;this.process(o,u,n);const d=this.afterProcessRules.find(({filter:g})=>Ye(o,g));d&&d.handler(n,o)}}};v(Oe,"pluginList",[]);let Q=Oe;const Ft={name:"univer-doc-paste-plugin-lark",checkPasteType(i){return/lark-record-clipboard/i.test(i)},stylesRules:[{filter:["s"],getStyle(i){const e=Ve(i);return{st:{s:a.BooleanNumber.TRUE},...e}}}],afterProcessRules:[{filter(i){return i.tagName==="DIV"&&/ace-line/i.test(i.className)},handler(i){i.paragraphs==null&&(i.paragraphs=[]),i.paragraphs.push({startIndex:i.dataStream.length}),i.dataStream+="\r"}}]},jt={name:"univer-doc-paste-plugin-word",checkPasteType(i){return/word|mso/i.test(i)},stylesRules:[{filter:["b"],getStyle(i){const e=Ve(i);return{bl:a.BooleanNumber.TRUE,...e}}}],afterProcessRules:[{filter(i){return i.tagName==="P"&&/mso/i.test(i.className)},handler(i,e){i.paragraphs==null&&(i.paragraphs=[]);const t={startIndex:i.dataStream.length},n=Bt(e);n&&(t.paragraphStyle=n),i.paragraphs.push(t),i.dataStream+="\r"}}]};function Vt(i,e){const{st:t,ed:n,ts:r={}}=e,{ff:o,fs:s,it:c,bl:l,ul:u,st:d,ol:g,bg:p,cl:m,va:f}=r;let h=i.slice(t,n);const S=[];return c===a.BooleanNumber.TRUE&&(h=`<i>${h}</i>`),f===a.BaselineOffset.SUPERSCRIPT?h=`<sup>${h}</sup>`:f===a.BaselineOffset.SUBSCRIPT&&(h=`<sub>${h}</sub>`),(u==null?void 0:u.s)===a.BooleanNumber.TRUE&&(h=`<u>${h}</u>`),(d==null?void 0:d.s)===a.BooleanNumber.TRUE&&(h=`<s>${h}</s>`),l===a.BooleanNumber.TRUE&&(h=`<strong>${h}</strong>`),o&&S.push(`font-family: ${o}`),m&&S.push(`color: ${m.rgb}`),s&&S.push(`font-size: ${s}px`),g&&S.push("text-decoration: overline"),p&&S.push(`background: ${p.rgb}`),S.length?`<span style="${S.join(";")}">${h}</span>`:h}function Xe(i,e=!0){const{dataStream:t,textRuns:n=[],paragraphs:r=[]}=i;let o=0;const s=[],c=[];for(const l of n){const{st:u,ed:d}=l;if(u!==o&&s.push(t.slice(o,u)),s.push(Vt(t,l)),o=d,e)for(const g of r){const{startIndex:p,paragraphStyle:m={}}=g;if(p>=u&&p<=d){const{spaceAbove:f,spaceBelow:h}=m,S=[];f!=null&&(typeof f=="number"?S.push(`margin-top: ${f}px`):S.push(`margin-top: ${f.v}px`)),h!=null&&(typeof h=="number"?S.push(`margin-bottom: ${h}px`):S.push(`margin-bottom: ${h.v}px`)),c.push(`<p className="UniverNormal" ${S.length?`style="${S.join(";")}"`:""}>${s.join("")}</p>`),s.length=0}}}return c.join("")+s.join("")}class kt{convert(e){if(e.length===0)throw new Error("The bodyList length at least to be 1");if(e.length===1)return Xe(e[0]);let t="";for(const n of e)t+='<p className="UniverNormal">',t+=Xe(n,!1),t+="</p>";return t}}var zt=Object.defineProperty,Wt=Object.getOwnPropertyDescriptor,Gt=(i,e,t,n)=>{for(var r=n>1?void 0:n?Wt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&zt(e,t,r),r},qe=(i,e)=>(t,n)=>e(t,n,i);Q.use(jt);Q.use(Ft);const dt=T.createIdentifier("doc.clipboard-service");let Ue=class extends a.Disposable{constructor(e,t){super();v(this,"_clipboardHooks",[]);v(this,"_htmlToUDM",new Q);v(this,"_umdToHtml",new kt);this._currentUniverService=e,this._clipboardInterfaceService=t}async queryClipboardData(){const e=await this._clipboardInterfaceService.read();if(e.length===0)return Promise.reject();try{let t="",n="";for(const o of e)for(const s of o.types)s===x.PLAIN_TEXT_CLIPBOARD_MIME_TYPE?n=await o.getType(s).then(c=>c&&c.text()):s===x.HTML_CLIPBOARD_MIME_TYPE&&(t=await o.getType(s).then(c=>c&&c.text()));if(!t)return{dataStream:n};const r=Lt(t);if(r){const o=Ke.get(r);if(o)return o}return this._htmlToUDM.convert(t)}catch(t){return Promise.reject(t)}}async setClipboardData(e){const t=wt(),n=e.length>1?e.map(o=>o.dataStream).join(`
2
+ `):e[0].dataStream;let r=this._umdToHtml.convert(e);return e.length===1&&(r=r.replace(/(<[a-z]+)/,(o,s)=>`${s} data-copy-id="${t}"`),Ke.set(t,e[0])),this._clipboardInterfaceService.write(n,r)}addClipboardHook(e){return this._clipboardHooks.push(e),a.toDisposable(()=>{const t=this._clipboardHooks.indexOf(e);t>-1&&this._clipboardHooks.splice(t,1)})}};Ue=Gt([qe(0,a.IUniverInstanceService),qe(1,x.IClipboardInterfaceService)],Ue);var Ht=Object.defineProperty,Kt=Object.getOwnPropertyDescriptor,Yt=(i,e,t,n)=>{for(var r=n>1?void 0:n?Kt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Ht(e,t,r),r},k=(i,e)=>(t,n)=>e(t,n,i);let ue=class extends a.Disposable{constructor(i,e,t,n,r,o){super(),this._logService=i,this._commandService=e,this._currentUniverService=t,this._docClipboardService=n,this._textSelectionManagerService=r,this._contextService=o,this._commandExecutedListener(),this.initialize()}initialize(){[ce,le,de].forEach(i=>this.disposeWithMe(this._commandService.registerAsMultipleCommand(i))),[He,Me].forEach(i=>this.disposeWithMe(this._commandService.registerCommand(i)))}_commandExecutedListener(){const i=[le.id,ce.id,de.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(i.includes(e.id)&&!(!this._contextService.getContextValue(a.FOCUSING_DOC)&&!this._contextService.getContextValue(a.FOCUSING_EDITOR)))switch(e.id){case de.id:{this._handlePaste();break}case ce.id:{this._handleCopy();break}case le.id:{this._handleCut();break}default:throw new Error(`Unhandled command ${e.id}`)}}))}async _handlePaste(){const{_docClipboardService:i}=this,{segmentId:e,endOffset:t,style:n}=this._textSelectionManagerService.getActiveRange()??{},r=this._textSelectionManagerService.getSelections();if(e==null&&this._logService.error("[DocClipboardController] segmentId is not existed"),!(t==null||r==null))try{const o=await i.queryClipboardData();let s=t;for(const l of r){const{startOffset:u,endOffset:d}=l;u==null||d==null||d<=t&&(s+=o.dataStream.length-(d-u))}const c=[{startOffset:s,endOffset:s,style:n}];this._commandService.executeCommand(He.id,{body:o,segmentId:e,textRanges:c})}catch{this._logService.error("[DocClipboardController] clipboard is empty")}}_getDocumentBodyInRanges(){const i=this._textSelectionManagerService.getSelections(),e=this._currentUniverService.getCurrentUniverDocInstance(),{dataStream:t,textRuns:n=[],paragraphs:r=[]}=e.getBody(),o=[];if(i==null)return o;for(const s of i){const{startOffset:c,endOffset:l,collapsed:u}=s;if(u||c==null||l==null)continue;const d={dataStream:t.slice(c,l)},g=[];for(const m of n){const f=a.Tools.deepClone(m),{st:h,ed:S}=f;a.Tools.hasIntersectionBetweenTwoRanges(h,S,c,l)&&(c>=h&&c<=S?g.push({...f,st:c,ed:Math.min(l,S)}):l>=h&&l<=S?g.push({...f,st:Math.max(c,h),ed:l}):g.push(f))}g.length&&(d.textRuns=g.map(m=>{const{st:f,ed:h}=m;return{...m,st:f-c,ed:h-c}}));const p=[];for(const m of r){const{startIndex:f}=m;f>=c&&f<=l&&p.push(a.Tools.deepClone(m))}p.length&&(d.paragraphs=p.map(m=>({...m,startIndex:m.startIndex-c}))),o.push(d)}return o}async _handleCopy(){const{_docClipboardService:i}=this,e=this._getDocumentBodyInRanges();try{i.setClipboardData(e)}catch{this._logService.error("[DocClipboardController] set clipboard failed")}}async _handleCut(){const{segmentId:i,endOffset:e,style:t}=this._textSelectionManagerService.getActiveRange()??{},n=this._textSelectionManagerService.getSelections();if(i==null&&this._logService.error("[DocClipboardController] segmentId is not existed"),!(e==null||n==null)){this._handleCopy();try{let r=e;for(const s of n){const{startOffset:c,endOffset:l}=s;c==null||l==null||l<=e&&(r-=l-c)}const o=[{startOffset:r,endOffset:r,style:t}];this._commandService.executeCommand(Me.id,{segmentId:i,textRanges:o})}catch{this._logService.error("[DocClipboardController] cut content failed")}}}};ue=Yt([a.OnLifecycle(a.LifecycleStages.Rendered,ue),k(0,a.ILogService),k(1,a.ICommandService),k(2,a.IUniverInstanceService),k(3,dt),k(4,T.Inject(exports.TextSelectionManagerService)),k(5,a.IContextService)],ue);var Xt=Object.defineProperty,qt=Object.getOwnPropertyDescriptor,Zt=(i,e,t,n)=>{for(var r=n>1?void 0:n?qt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Xt(e,t,r),r},De=(i,e)=>(t,n)=>e(t,n,i);let me=class extends a.Disposable{constructor(i,e,t){super(),this._docSkeletonManagerService=i,this._renderManagerService=e,this._commandService=t,this._initialRenderRefresh(),this._commandExecutedListener()}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeletonBefore$.subscribe(i=>{if(i==null)return;const{skeleton:e,unitId:t}=i,n=this._renderManagerService.getRenderById(t);if(n==null)return;const{mainComponent:r}=n;r.changeSkeleton(e),this._recalculateSizeBySkeleton(n,e)})}_recalculateSizeBySkeleton(i,e){var u;const{mainComponent:t,scene:n}=i,r=t,o=(u=e.getSkeletonData())==null?void 0:u.pages;if(o==null)return;let s=0,c=0;for(let d=0,g=o.length;d<g;d++){const p=o[d],{pageWidth:m,pageHeight:f}=p;r.pageLayoutType===O.PageLayoutType.VERTICAL?(c+=f,c+=r.pageMarginTop,d===g-1&&(c+=r.pageMarginTop),s=Math.max(s,m)):r.pageLayoutType===O.PageLayoutType.HORIZONTAL&&(s+=m,d!==g-1&&(s+=r.pageMarginLeft),c=Math.max(c,f))}r.resize(s,c),[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(i.unitId)||n.resize(s,c)}_commandExecutedListener(){const i=[_.id],e=[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{var n;if(i.includes(t.id)){const r=t.params,{unitId:o}=r,s=this._docSkeletonManagerService.getSkeletonByUnitId(o);if(s==null)return;const{skeleton:c}=s,l=this._renderManagerService.getRenderById(o);if(l==null)return;if(c.calculate(),e.includes(o)){(n=l.mainComponent)==null||n.makeDirty();return}this._recalculateSizeBySkeleton(l,c)}}))}};me=Zt([a.OnLifecycle(a.LifecycleStages.Rendered,me),De(0,T.Inject(exports.DocSkeletonManagerService)),De(1,O.IRenderManagerService),De(2,a.ICommandService)],me);var Jt=Object.defineProperty,Qt=Object.getOwnPropertyDescriptor,en=(i,e,t,n)=>{for(var r=n>1?void 0:n?Qt(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Jt(e,t,r),r},X=(i,e)=>(t,n)=>e(t,n,i);let ge=class extends a.Disposable{constructor(e,t,n,r,o){super();v(this,"_liquid",new O.Liquid);v(this,"_pageMarginCache",new Map);this._docSkeletonManagerService=e,this._currentUniverService=t,this._renderManagerService=n,this._commandService=r,this._floatingObjectManagerService=o,this._initialize(),this._commandExecutedListener()}_initialize(){this._initialRenderRefresh(),this._updateOnPluginChange()}_updateOnPluginChange(){this._floatingObjectManagerService.pluginUpdate$.subscribe(e=>{const t=this._docSkeletonManagerService.getCurrent();if(t==null)return;const{unitId:n,skeleton:r}=t,o=this._renderManagerService.getRenderById(n);if(o==null)return;const{mainComponent:s,components:c,scene:l}=o,u=s,{left:d,top:g}=u;e.forEach(p=>{const{unitId:m,subComponentId:f,floatingObjectId:h,floatingObject:S}=p,{left:C=0,top:I=0,width:D=0,height:y=0,angle:M,flipX:b,flipY:U,skewX:E,skewY:P}=S,R=this._pageMarginCache.get(h),w=(R==null?void 0:R.marginLeft)||0,N=(R==null?void 0:R.marginTop)||0;r==null||r.getViewModel().getDataModel().updateDrawing(h,{left:C-d-w,top:I-g-N,height:y,width:D})}),r==null||r.calculate(),s==null||s.makeDirty()})}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeleton$.subscribe(e=>{if(e==null)return;const{skeleton:t,unitId:n}=e,r=this._renderManagerService.getRenderById(n);if(r==null)return;const{mainComponent:o}=r;o.changeSkeleton(t),this._refreshFloatingObject(n,t,r)})}_commandExecutedListener(){const e=[_.id,j.id],t=[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY];this.disposeWithMe(this._commandService.onCommandExecuted(n=>{var r;if(e.includes(n.id)){const o=n.params,{unitId:s}=o,c=this._docSkeletonManagerService.getCurrent();if(c==null)return;const{unitId:l,skeleton:u}=c;if(s!==l)return;const d=this._renderManagerService.getRenderById(l);if(d==null)return;if(t.includes(l)){(r=d.mainComponent)==null||r.makeDirty();return}this._refreshFloatingObject(l,u,d)}}))}_refreshFloatingObject(e,t,n){const r=t==null?void 0:t.getSkeletonData(),{mainComponent:o,scene:s}=n,c=o;if(!r)return;const{left:l,top:u,pageLayoutType:d,pageMarginLeft:g,pageMarginTop:p}=c,{pages:m}=r,f=[];s.getAncestorScale(),this._liquid.reset(),this._pageMarginCache.clear();for(let h=0,S=m.length;h<S;h++){const C=m[h],{skeDrawings:I,marginLeft:D,marginTop:y}=C;this._liquid.translatePagePadding(C),I.forEach(M=>{const{aLeft:b,aTop:U,height:E,width:P,objectId:R}=M;f.push({unitId:e,subComponentId:a.DEFAULT_DOCUMENT_SUB_COMPONENT_ID,floatingObjectId:R,floatingObject:{left:b+l+this._liquid.x,top:U+u+this._liquid.y,width:P,height:E}}),this._pageMarginCache.set(R,{marginLeft:this._liquid.x,marginTop:this._liquid.y})}),this._liquid.translatePage(C,d,g,p)}this._floatingObjectManagerService.BatchAddOrUpdate(f)}};ge=en([a.OnLifecycle(a.LifecycleStages.Steady,ge),X(0,T.Inject(exports.DocSkeletonManagerService)),X(1,a.IUniverInstanceService),X(2,O.IRenderManagerService),X(3,a.ICommandService),X(4,a.IFloatingObjectManagerService)],ge);var tn=Object.defineProperty,nn=Object.getOwnPropertyDescriptor,rn=(i,e,t,n)=>{for(var r=n>1?void 0:n?nn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&tn(e,t,r),r},z=(i,e)=>(t,n)=>e(t,n,i);let pe=class extends a.Disposable{constructor(e,t,n,r,o,s){super();v(this,"_previousIMEContent","");v(this,"_previousIMERange");v(this,"_onStartSubscription");v(this,"_onUpdateSubscription");v(this,"_onEndSubscription");this._docSkeletonManagerService=e,this._currentUniverService=t,this._renderManagerService=n,this._textSelectionRenderManager=r,this._imeInputManagerService=o,this._commandService=s,this._initialize()}dispose(){var e,t,n;(e=this._onStartSubscription)==null||e.unsubscribe(),(t=this._onUpdateSubscription)==null||t.unsubscribe(),(n=this._onEndSubscription)==null||n.unsubscribe()}_initialize(){this._initialOnCompositionstart(),this._initialOnCompositionUpdate(),this._initialOnCompositionend()}_initialOnCompositionstart(){this._onStartSubscription=this._textSelectionRenderManager.onCompositionstart$.subscribe(e=>{if(e==null)return;const{activeRange:t}=e;t!=null&&(this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(a.Tools.deepClone(t)),this._previousIMERange=t)})}_initialOnCompositionUpdate(){this._onUpdateSubscription=this._textSelectionRenderManager.onCompositionupdate$.subscribe(async e=>{this._updateContent(e,!0)})}_initialOnCompositionend(){this._onEndSubscription=this._textSelectionRenderManager.onCompositionend$.subscribe(e=>{this._updateContent(e,!1)})}async _updateContent(e,t){var f;const n=(f=this._docSkeletonManagerService.getCurrent())==null?void 0:f.skeleton;if(this._previousIMERange==null||e==null||n==null)return;const r=this._currentUniverService.getCurrentUniverDocInstance(),{event:o,activeRange:s}=e,{startOffset:c,segmentId:l,style:u}=this._previousIMERange;if(n==null||s==null)return;const g=o.data;if(g===this._previousIMEContent&&t)return;const p=g.length,m=[{startOffset:c+p,endOffset:c+p,style:u}];await this._commandService.executeCommand($e.id,{unitId:r.getUnitId(),newText:g,oldTextLen:this._previousIMEContent.length,range:this._previousIMERange,textRanges:m,isCompositionEnd:!t,segmentId:l}),n.calculate(),t?(this._previousIMERange.collapsed||(this._previousIMERange.collapsed=!0),this._previousIMEContent=g):this._resetIME()}_resetIME(){this._previousIMEContent="",this._previousIMERange=null,this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(null)}_getDocObject(){return ee(this._currentUniverService,this._renderManagerService)}};pe=rn([a.OnLifecycle(a.LifecycleStages.Rendered,pe),z(0,T.Inject(exports.DocSkeletonManagerService)),z(1,a.IUniverInstanceService),z(2,O.IRenderManagerService),z(3,O.ITextSelectionRenderManager),z(4,T.Inject(Be)),z(5,a.ICommandService)],pe);var on=Object.defineProperty,sn=Object.getOwnPropertyDescriptor,an=(i,e,t,n)=>{for(var r=n>1?void 0:n?sn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&on(e,t,r),r},Ze=(i,e)=>(t,n)=>e(t,n,i);let fe=class extends a.Disposable{constructor(i,e){super(),this._textSelectionManagerService=i,this._commandService=e,this._commandExecutedListener()}_commandExecutedListener(){const i=[H.id,te.id,ne.id,re.id,A.id,V.id,K.id,Y.id,ie.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{i.includes(e.id)&&this._handleInlineFormat(e)}))}_handleInlineFormat(i){const{segmentId:e}=this._textSelectionManagerService.getActiveRange()??{};e!=null&&this._commandService.executeCommand(Fe.id,{segmentId:e,preCommandId:i.id,...i.params??{}})}};fe=an([a.OnLifecycle(a.LifecycleStages.Rendered,fe),Ze(0,T.Inject(exports.TextSelectionManagerService)),Ze(1,a.ICommandService)],fe);var cn=Object.defineProperty,ln=Object.getOwnPropertyDescriptor,dn=(i,e,t,n)=>{for(var r=n>1?void 0:n?ln(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&cn(e,t,r),r},q=(i,e)=>(t,n)=>e(t,n,i);let he=class extends a.Disposable{constructor(e,t,n,r,o){super();v(this,"_onInputSubscription");this._docSkeletonManagerService=e,this._currentUniverService=t,this._renderManagerService=n,this._textSelectionManagerService=r,this._commandService=o,this._initialize(),this._commandExecutedListener()}dispose(){var e;(e=this._onInputSubscription)==null||e.unsubscribe()}_initialize(){}_commandExecutedListener(){const e=[B.id,$.id];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{if(!e.includes(t.id))return;const n=t.params;switch(t.id){case B.id:return this._handleMoveCursor(n.direction);case $.id:return this._handleShiftMoveSelection(n.direction);default:throw new Error("Unknown command")}}))}_handleShiftMoveSelection(e){var h;const t=this._textSelectionManagerService.getActiveRange(),n=this._textSelectionManagerService.getSelections(),r=this._currentUniverService.getCurrentUniverDocInstance(),o=(h=this._docSkeletonManagerService.getCurrent())==null?void 0:h.skeleton,s=this._getDocObject();if(t==null||o==null||s==null)return;const{startOffset:c,endOffset:l,style:u,collapsed:d,direction:g}=t;if(n.length>1){let S=1/0,C=-1/0;for(const I of n)S=Math.min(S,I.startOffset),C=Math.max(C,I.endOffset);this._textSelectionManagerService.replaceTextRanges([{startOffset:e===a.Direction.LEFT||e===a.Direction.UP?C:S,endOffset:e===a.Direction.LEFT||e===a.Direction.UP?S:C,style:u}]);return}const p=d||g===O.RANGE_DIRECTION.FORWARD?c:l;let m=d||g===O.RANGE_DIRECTION.FORWARD?l:c;const f=r.getBody().dataStream.length??1/0;if(e===a.Direction.LEFT||e===a.Direction.RIGHT){const S=o.findNodeByCharIndex(m-1),C=o.findNodeByCharIndex(m);m=e===a.Direction.RIGHT?m+C.count:m-((S==null?void 0:S.count)??0),m=Math.min(f-2,Math.max(0,m)),this._textSelectionManagerService.replaceTextRanges([{startOffset:p,endOffset:m,style:u}])}else{const S=o.findNodeByCharIndex(m),C=s.document.getOffsetConfig(),I=this._getTopOrBottomPosition(o,S,e===a.Direction.DOWN);if(I==null){const y=e===a.Direction.UP?0:f-2;if(y===m)return;this._textSelectionManagerService.replaceTextRanges([{startOffset:p,endOffset:y,style:u}]);return}const D=new O.NodePositionConvertToCursor(C,o).getRangePointData(I,I).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{startOffset:p,endOffset:D.endOffset,style:u}])}}_handleMoveCursor(e){var p;const t=this._textSelectionManagerService.getActiveRange(),n=this._textSelectionManagerService.getSelections(),r=this._currentUniverService.getCurrentUniverDocInstance(),o=(p=this._docSkeletonManagerService.getCurrent())==null?void 0:p.skeleton,s=this._getDocObject();if(t==null||o==null||s==null||n==null)return;const{startOffset:c,endOffset:l,style:u,collapsed:d}=t,g=r.getBody().dataStream.length??1/0;if(e===a.Direction.LEFT||e===a.Direction.RIGHT){let m;if(!t.collapsed||n.length>1){let f=1/0,h=-1/0;for(const S of n)f=Math.min(f,S.startOffset),h=Math.max(h,S.endOffset);m=e===a.Direction.LEFT?f:h}else{const f=o.findNodeByCharIndex(c-1),h=o.findNodeByCharIndex(c);e===a.Direction.LEFT?m=Math.max(0,c-((f==null?void 0:f.count)??0)):m=Math.min(g-2,l+h.count)}this._textSelectionManagerService.replaceTextRanges([{startOffset:m,endOffset:m,style:u}])}else{const m=o.findNodeByCharIndex(c),f=o.findNodeByCharIndex(l),h=s.document.getOffsetConfig(),S=this._getTopOrBottomPosition(o,e===a.Direction.UP?m:f,e===a.Direction.DOWN);if(S==null){let I;d?I=e===a.Direction.UP?0:g-2:I=e===a.Direction.UP?c:l,this._textSelectionManagerService.replaceTextRanges([{startOffset:I,endOffset:I,style:u}]);return}const C=new O.NodePositionConvertToCursor(h,o).getRangePointData(S,S).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{...C,style:u}])}}_getTopOrBottomPosition(e,t,n){if(t==null)return;const r=this._getSpanLeftOffsetInLine(t),o=this._getNextOrPrevLine(t,n);if(o==null)return;const s=this._matchPositionByLeftOffset(e,o,r);if(s!=null)return{...s,isBack:!0}}_getSpanLeftOffsetInLine(e){const t=e.parent;if(t==null)return-1/0;const n=t.left,{left:r}=e;return n+r}_matchPositionByLeftOffset(e,t,n){const r={distance:1/0};for(const o of t.divides){const s=o.left;for(const c of o.spanGroup){const{left:l}=c,u=s+l,d=Math.abs(n-u);d<r.distance&&(r.span=c,r.distance=d)}}if(r.span!=null)return e.findPositionBySpan(r.span)}_getNextOrPrevLine(e,t){var f,h,S,C,I,D,y,M,b,U,E,P;const n=e.parent;if(n==null)return;const r=n.parent;if(r==null)return;const o=r.parent;if(o==null)return;const s=o.lines.indexOf(r);if(s===-1)return;let c;if(t===!0?c=o.lines[s+1]:c=o.lines[s-1],c!=null)return c;const l=o.parent;if(l==null)return;const u=l.columns.indexOf(o);if(u===-1)return;if(t===!0)c=(f=l.columns[u+1])==null?void 0:f.lines[0];else{const R=(S=(h=l.columns)==null?void 0:h[u-1])==null?void 0:S.lines;c=R==null?void 0:R[R.length-1]}if(c!=null)return c;const d=l.parent;if(d==null)return;const g=d.sections.indexOf(l);if(g===-1)return;if(t===!0)c=(I=(C=d.sections[g-1])==null?void 0:C.columns[0])==null?void 0:I.lines[0];else{const R=(y=(D=d.sections)==null?void 0:D[g-1])==null?void 0:y.columns,w=R==null?void 0:R[R.length-1],N=w==null?void 0:w.lines;c=N==null?void 0:N[N.length-1]}if(c!=null)return c;const p=d.parent;if(p==null)return;const m=p.pages.indexOf(d);if(m!==-1){if(t===!0)c=(U=(b=(M=p.pages[m+1])==null?void 0:M.sections[0])==null?void 0:b.columns[0])==null?void 0:U.lines[0];else{const R=(E=p.pages[m-1])==null?void 0:E.sections;if(R==null)return;const w=(P=R[R.length-1])==null?void 0:P.columns,N=w[w.length-1],ke=N==null?void 0:N.lines;c=ke[ke.length-1]}if(c!=null)return c}}_getDocObject(){return ee(this._currentUniverService,this._renderManagerService)}};he=dn([a.OnLifecycle(a.LifecycleStages.Rendered,he),q(0,T.Inject(exports.DocSkeletonManagerService)),q(1,a.IUniverInstanceService),q(2,O.IRenderManagerService),q(3,T.Inject(exports.TextSelectionManagerService)),q(4,a.ICommandService)],he);var un=Object.defineProperty,mn=Object.getOwnPropertyDescriptor,gn=(i,e,t,n)=>{for(var r=n>1?void 0:n?mn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&un(e,t,r),r},Z=(i,e)=>(t,n)=>e(t,n,i);let Se=class extends a.Disposable{constructor(e,t,n,r,o){super();v(this,"_onInputSubscription");this._docSkeletonManagerService=e,this._currentUniverService=t,this._renderManagerService=n,this._textSelectionRenderManager=r,this._commandService=o,this._initialize(),this._commandExecutedListener()}dispose(){var e;(e=this._onInputSubscription)==null||e.unsubscribe()}_initialize(){this._initialNormalInput()}_initialNormalInput(){this._onInputSubscription=this._textSelectionRenderManager.onInput$.subscribe(async e=>{var f;if(e==null)return;const n=this._currentUniverService.getCurrentUniverDocInstance().getUnitId(),{event:r,content:o="",activeRange:s}=e,c=r,l=(f=this._docSkeletonManagerService.getCurrent())==null?void 0:f.skeleton;if(c.data==null||l==null||!l||!s)return;const{startOffset:u,segmentId:d,style:g}=s,p=o.length,m=[{startOffset:u+p,endOffset:u+p,style:g}];await this._commandService.executeCommand(ye.id,{unitId:n,body:{dataStream:o},range:s,textRanges:m,segmentId:d})})}_commandExecutedListener(){}_getDocObject(){return ee(this._currentUniverService,this._renderManagerService)}};Se=gn([a.OnLifecycle(a.LifecycleStages.Rendered,Se),Z(0,T.Inject(exports.DocSkeletonManagerService)),Z(1,a.IUniverInstanceService),Z(2,O.IRenderManagerService),Z(3,O.ITextSelectionRenderManager),Z(4,a.ICommandService)],Se);var pn=Object.defineProperty,fn=Object.getOwnPropertyDescriptor,hn=(i,e,t,n)=>{for(var r=n>1?void 0:n?fn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&pn(e,t,r),r},Je=(i,e)=>(t,n)=>e(t,n,i);const Sn="rgba(198, 198, 198, 1)",_n="rgba(255, 255, 255, 1)";let _e=class extends a.Disposable{constructor(i,e){super(),this._renderManagerService=i,this._currentUniverService=e,this._initialize(),this._commandExecutedListener()}_initialize(){this._initialRenderRefresh()}_initialRenderRefresh(){this._renderManagerService.currentRender$.subscribe(i=>{var o;if(i==null||this._currentUniverService.getUniverDocInstance(i)==null)return;const e=this._renderManagerService.getRenderById(i);if(e==null)return;const{mainComponent:t}=e,n=t,r=(o=n.getSkeleton())==null?void 0:o.getPageSize();n.onPageRenderObservable.add(s=>{if([a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(i))return;const{page:c,pageLeft:l,pageTop:u,ctx:d}=s,{width:g,height:p}=c;d.save(),d.translate(l-.5,u-.5),O.Rect.drawWith(d,{width:(r==null?void 0:r.width)||g,height:(r==null?void 0:r.height)||p,strokeWidth:1,stroke:Sn,fill:_n,zIndex:3}),d.restore()})})}_commandExecutedListener(){}};_e=hn([a.OnLifecycle(a.LifecycleStages.Rendered,_e),Je(0,O.IRenderManagerService),Je(1,T.Inject(a.IUniverInstanceService))],_e);var vn=Object.defineProperty,Cn=Object.getOwnPropertyDescriptor,In=(i,e,t,n)=>{for(var r=n>1?void 0:n?Cn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&vn(e,t,r),r},F=(i,e)=>(t,n)=>e(t,n,i);let ve=class extends a.Disposable{constructor(e,t,n,r,o,s,c){super();v(this,"_moveInObserver");v(this,"_moveOutObserver");v(this,"_downObserver");v(this,"_dblClickObserver");v(this,"_tripleClickObserver");v(this,"_loadedMap",new Set);this._docSkeletonManagerService=e,this._currentUniverService=t,this._commandService=n,this._renderManagerService=r,this._textSelectionRenderManager=o,this._textSelectionManagerService=s,this._layoutService=c,this._renderManagerService.currentRender$.subscribe(l=>{l!=null&&this._currentUniverService.getUniverDocInstance(l)!=null&&(this._loadedMap.has(l)||(this._initialMain(l),this._loadedMap.add(l)))}),this._initialize()}_initialize(){this._skeletonListener(),this._commandExecutedListener(),this._layoutService&&this.disposeWithMe(this._layoutService.registerContainer(this._textSelectionRenderManager.__getEditorContainer()))}dispose(){this._renderManagerService.getRenderAll().forEach(e=>{const{mainComponent:t}=e;t!=null&&(t.onPointerEnterObserver.remove(this._moveInObserver),t.onPointerLeaveObserver.remove(this._moveOutObserver),t.onPointerDownObserver.remove(this._downObserver),t.onDblclickObserver.remove(this._dblClickObserver),t.onTripleClickObserver.remove(this._tripleClickObserver))})}_initialMain(e){const t=this._getDocObjectById(e);if(t==null)return;const{document:n,scene:r}=t,o=r.getViewport(G.VIEW_MAIN);this._moveInObserver=n.onPointerEnterObserver.add(()=>{n.cursor=O.CURSOR_TYPE.TEXT}),this._moveOutObserver=n.onPointerLeaveObserver.add(()=>{n.cursor=O.CURSOR_TYPE.DEFAULT,r.resetCursor()}),this._downObserver=n==null?void 0:n.onPointerDownObserver.add((s,c)=>{this._currentUniverService.getCurrentUniverDocInstance().getUnitId()!==e&&this._currentUniverService.setCurrentUniverDocInstance(e),this._textSelectionRenderManager.eventTrigger(s,n.getOffsetConfig(),o),s.button!==2&&c.stopPropagation()}),this._dblClickObserver=n==null?void 0:n.onDblclickObserver.add(s=>{this._textSelectionRenderManager.handleDblClick(s,n.getOffsetConfig(),o)}),this._tripleClickObserver=n==null?void 0:n.onTripleClickObserver.add(s=>{this._textSelectionRenderManager.handleTripleClick(s,n.getOffsetConfig(),o)})}_commandExecutedListener(){const e=[j.id];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{var n;if(e.includes(t.id)){const r=t.params,{unitId:o}=r,s=(n=this._textSelectionManagerService.getCurrentSelection())==null?void 0:n.unitId;if(o!==s)return;this._textSelectionManagerService.refreshSelection()}}))}_skeletonListener(){this._docSkeletonManagerService.currentSkeleton$.subscribe(e=>{if(e==null)return;const{unitId:t,skeleton:n}=e,r=this._renderManagerService.getRenderById(t);if(r==null)return;const{scene:o,mainComponent:s}=r,c=o.getViewport(G.VIEW_MAIN);this._textSelectionRenderManager.changeRuntime(n,o,c,s.getOffsetConfig()),this._textSelectionManagerService.setCurrentSelectionNotRefresh({unitId:t,subUnitId:""})})}_getDocObjectById(e){return gt(e,this._renderManagerService)}};ve=In([a.OnLifecycle(a.LifecycleStages.Rendered,ve),F(0,T.Inject(exports.DocSkeletonManagerService)),F(1,a.IUniverInstanceService),F(2,a.ICommandService),F(3,O.IRenderManagerService),F(4,O.ITextSelectionRenderManager),F(5,T.Inject(exports.TextSelectionManagerService)),F(6,T.Optional(x.LayoutService))],ve);var Mn=Object.defineProperty,On=Object.getOwnPropertyDescriptor,Rn=(i,e,t,n)=>{for(var r=n>1?void 0:n?On(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Mn(e,t,r),r},J=(i,e)=>(t,n)=>e(t,n,i);let Ce=class extends a.Disposable{constructor(e,t,n,r,o){super();v(this,"_initializedRender",new Set);this._docSkeletonManagerService=e,this._currentUniverService=t,this._commandService=n,this._renderManagerService=r,this._textSelectionManagerService=o,this._initialize()}dispose(){super.dispose()}_initialize(){this._skeletonListener(),this._commandExecutedListener(),this._initialRenderRefresh()}_initialRenderRefresh(){this._docSkeletonManagerService.currentSkeleton$.subscribe(e=>{if(e==null)return;const{unitId:t}=e,n=this._renderManagerService.getRenderById(t);if(n==null||this._initializedRender.has(t)||[a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(t))return;this._initializedRender.add(t);const{scene:r}=n;this.disposeWithMe(a.toDisposable(r.onMouseWheelObserver.add(o=>{if(!o.ctrlKey)return;const s=Math.abs(o.deltaX);let c=s<40?.2:s<80?.4:.2;c*=o.deltaY>0?-1:1,r.scaleX<1&&(c/=2);const l=this._currentUniverService.getCurrentUniverDocInstance(),u=l.zoomRatio;let d=+parseFloat(`${u+c}`).toFixed(1);d=d>=4?4:d<=.1?.1:d,this._commandService.executeCommand(ct.id,{zoomRatio:d,unitId:l.getUnitId()}),o.preventDefault()})))})}_skeletonListener(){this.disposeWithMe(a.toDisposable(this._docSkeletonManagerService.currentSkeletonBefore$.subscribe(e=>{if(e==null)return;const n=this._currentUniverService.getCurrentUniverDocInstance().zoomRatio||1;this._updateViewZoom(n,!1)})))}_commandExecutedListener(){const e=[j.id];this.disposeWithMe(this._commandService.onCommandExecuted(t=>{if(e.includes(t.id)){const n=this._currentUniverService.getCurrentUniverDocInstance(),r=t.params,{unitId:o}=r;if(o!==n.getUnitId())return;const s=n.zoomRatio||1;this._updateViewZoom(s)}}))}_updateViewZoom(e,t=!0){var r;const n=this._getDocObject();n!=null&&(n.scene.scale(e,e),this._calculatePagePosition(n,e),t&&this._textSelectionManagerService.refreshSelection(),(r=n.scene.getTransformer())==null||r.hideControl())}_calculatePagePosition(e,t){const{document:n,scene:r}=e,o=r==null?void 0:r.getParent(),{width:s,height:c,pageMarginLeft:l,pageMarginTop:u}=n;if(o==null||s===1/0||c===1/0)return;const{width:d,height:g}=o;let p=0,m=0,f=0,h=0,S=1/0;d>(s+l*2)*t?(p=d/2-s*t/2,p/=t,f=(d-l*2)/t,S=0):(p=l,f=s+l*2,S=(f-d/t)/2),g>c?(m=g/2-c/2,h=(g-u*2)/t):(m=u,h=c+u*2),r.resize(f,h+200),n.translate(p,m);const C=r.getViewport(G.VIEW_MAIN);if(S!==1/0&&C!=null){const I=C.getBarScroll(S,0).x;C.scrollTo({x:I})}return this}_getDocObject(){return ee(this._currentUniverService,this._renderManagerService)}};Ce=Rn([a.OnLifecycle(a.LifecycleStages.Rendered,Ce),J(0,T.Inject(exports.DocSkeletonManagerService)),J(1,a.IUniverInstanceService),J(2,a.ICommandService),J(3,O.IRenderManagerService),J(4,T.Inject(exports.TextSelectionManagerService))],Ce);const yn={id:Ne.id,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),binding:x.KeyCode.ENTER},xn={id:we.id,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),binding:x.KeyCode.BACKSPACE},Dn={id:Le.id,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),binding:x.KeyCode.DELETE},Tn={id:B.id,binding:x.KeyCode.ARROW_UP,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.UP}},bn={id:B.id,binding:x.KeyCode.ARROW_DOWN,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.DOWN}},Un={id:B.id,binding:x.KeyCode.ARROW_LEFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.LEFT}},En={id:B.id,binding:x.KeyCode.ARROW_RIGHT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.RIGHT}},Pn={id:$.id,binding:x.KeyCode.ARROW_UP|x.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.UP}},Nn={id:$.id,binding:x.KeyCode.ARROW_DOWN|x.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.DOWN}},wn={id:$.id,binding:x.KeyCode.ARROW_LEFT|x.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.LEFT}},Ln={id:$.id,binding:x.KeyCode.ARROW_RIGHT|x.MetaKeys.SHIFT,preconditions:i=>i.getContextValue(a.FOCUSING_DOC),staticParameters:{direction:a.Direction.RIGHT}},An={id:lt.id,binding:x.KeyCode.A|x.MetaKeys.CTRL_COMMAND,preconditions:i=>i.getContextValue(a.FOCUSING_DOC)||i.getContextValue(a.FOCUSING_EDITOR)};var Bn=Object.defineProperty,$n=Object.getOwnPropertyDescriptor,Fn=(i,e,t,n)=>{for(var r=n>1?void 0:n?$n(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&Bn(e,t,r),r},oe=(i,e)=>(t,n)=>e(t,n,i);exports.DocCanvasView=class extends a.RxDisposable{constructor(t,n,r,o){super();v(this,"_scene");v(this,"_currentDocumentModel");v(this,"_loadedMap",new Set);v(this,"_fps$",new W.BehaviorSubject(""));v(this,"fps$",this._fps$.asObservable());this._renderManagerService=t,this._configService=n,this._currentUniverService=r,this._docViewModelManagerService=o,this._initialize()}_initialize(){this._currentUniverService.currentDoc$.subscribe(t=>{if(t==null)return;this._currentDocumentModel=t;const n=t.getUnitId();this._docViewModelManagerService.setCurrent(n),this._loadedMap.has(n)||(this._addNewRender(),this._loadedMap.add(n))})}dispose(){this._fps$.complete()}_addNewRender(){const t=this._currentDocumentModel,n=t.getUnitId(),r=t.getContainer(),o=t.getParentRenderUnitId();if(r!=null&&o!=null)throw new Error("container or parentRenderUnitId can only exist one");r==null&&o!=null?this._renderManagerService.createRenderWithParent(n,o):this._renderManagerService.createRender(n);const s=this._renderManagerService.getRenderById(n);if(s==null)return;const{scene:c,engine:l}=s;c.openTransformer(),this._scene=c;const u=new O.Viewport(G.VIEW_MAIN,c,{left:0,top:0,bottom:0,right:0,isWheelPreventDefaultX:!0});c.addViewport(u).attachControl(),c.on(O.EVENT_TYPE.wheel,(p,m)=>{const f=p;if(f.ctrlKey){const h=Math.abs(f.deltaX);let S=h<40?.2:h<80?.4:.2;S*=f.deltaY>0?-1:1,c.scaleX<1&&(S/=2),c.scaleX+S>4?c.scale(4,4):c.scaleX+S<.1?c.scale(.1,.1):f.preventDefault()}else u.onMouseWheel(f,m)}),this._configService.getConfig("hasScroll")!==!1&&n!==a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY&&new O.ScrollBar(u),c.addLayer(new O.Layer(c,[],ae),new O.Layer(c,[],Qe)),this._addComponent(s),this._currentDocumentModel.getShouldRenderLoopImmediately()&&l.runRenderLoop(()=>{c.render(),this._fps$.next(Math.round(l.getFps()).toString())}),this._renderManagerService.setCurrent(n)}_addComponent(t){const n=this._scene,r=this._currentDocumentModel,o=new O.Documents(Ie.MAIN,void 0,{pageMarginLeft:r.documentStyle.marginLeft||0,pageMarginTop:r.documentStyle.marginTop||0});o.zIndex=et,t.mainComponent=o,t.components.set(Ie.MAIN,o),n.addObjects([o],ae),n.enableLayerCache(ae)}};exports.DocCanvasView=Fn([a.OnLifecycle(a.LifecycleStages.Ready,exports.DocCanvasView),oe(0,O.IRenderManagerService),oe(1,a.IConfigService),oe(2,a.IUniverInstanceService),oe(3,T.Inject(exports.DocViewModelManagerService))],exports.DocCanvasView);var jn=Object.defineProperty,Vn=Object.getOwnPropertyDescriptor,kn=(i,e,t,n)=>{for(var r=n>1?void 0:n?Vn(e,t):e,o=i.length-1,s;o>=0;o--)(s=i[o])&&(r=(n?s(e,t,r):s(r))||r);return n&&r&&jn(e,t,r),r},se=(i,e)=>(t,n)=>e(t,n,i);const zn={hasScroll:!0},Wn="docs";var Te;exports.UniverDocsPlugin=(Te=class extends a.Plugin{constructor(t={},n,r,o,s){super(Wn);v(this,"_config");this._injector=n,this._localeService=r,this._configService=o,this._currentUniverService=s,this._config=Object.assign(zn,t),this._initializeDependencies(n),this._initializeCommands()}initialize(){}_initializeCommands(){[B,$,we,Le,H,te,ne,re,A,V,K,Y,ie,Fe,Ne,ye,xe,Pe,$e,Ae,_,ot,st,ct,j,Ee,lt,it,rt,je].forEach(t=>{this._injector.get(a.ICommandService).registerCommand(t)}),[Tn,bn,En,Un,Pn,Nn,wn,Ln,An,xn,Dn,yn].forEach(t=>{this._injector.get(x.IShortcutService).registerShortcut(t)})}onReady(){this.initialize()}_initializeDependencies(t){[[exports.DocCanvasView],[exports.DocSkeletonManagerService],[exports.DocViewModelManagerService],[Be],[dt,{useClass:Ue}],[O.ITextSelectionRenderManager,{useClass:O.TextSelectionRenderManager}],[exports.TextSelectionManagerService],[me],[_e],[ve],[Se],[pe],[fe],[ue],[he],[Ce],[ge]].forEach(n=>t.add(n))}},v(Te,"type",a.PluginType.Doc),Te);exports.UniverDocsPlugin=kn([se(1,T.Inject(T.Injector)),se(2,T.Inject(a.LocaleService)),se(3,a.IConfigService),se(4,a.IUniverInstanceService)],exports.UniverDocsPlugin);exports.BreakLineCommand=Ne;exports.BulletListCommand=rt;exports.CoverContentCommand=st;exports.DOCS_COMPONENT_DEFAULT_Z_INDEX=et;exports.DOCS_COMPONENT_HEADER_LAYER_INDEX=Qe;exports.DOCS_COMPONENT_MAIN_LAYER_INDEX=ae;exports.DOCS_VIEW_KEY=Ie;exports.DeleteCommand=xe;exports.DeleteLeftCommand=we;exports.DeleteRightCommand=Le;exports.DocCopyCommand=ce;exports.DocCutCommand=le;exports.DocPasteCommand=de;exports.IMEInputCommand=$e;exports.InsertCommand=ye;exports.MoveCursorOperation=B;exports.MoveSelectionOperation=$;exports.NORMAL_TEXT_SELECTION_PLUGIN_NAME=pt;exports.OrderListCommand=it;exports.ReplaceContentCommand=ot;exports.RichTextEditingMutation=_;exports.SetInlineFormatBoldCommand=H;exports.SetInlineFormatCommand=Fe;exports.SetInlineFormatFontFamilyCommand=Y;exports.SetInlineFormatFontSizeCommand=K;exports.SetInlineFormatItalicCommand=te;exports.SetInlineFormatStrikethroughCommand=re;exports.SetInlineFormatSubscriptCommand=A;exports.SetInlineFormatSuperscriptCommand=V;exports.SetInlineFormatTextColorCommand=ie;exports.SetInlineFormatUnderlineCommand=ne;exports.SetTextSelectionsOperation=Ee;exports.UpdateCommand=Pe;exports.VIEWPORT_KEY=G;exports.getDocObject=ee;