@univerjs/docs 0.4.2 → 0.5.0-nightly.202411091605

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var __defProp=Object.defineProperty;var __defNormalProp=(obj,key,value)=>key in obj?__defProp(obj,key,{enumerable:!0,configurable:!0,writable:!0,value}):obj[key]=value;var __name=(target,value)=>__defProp(target,"name",{value,configurable:!0});var __publicField=(obj,key,value)=>__defNormalProp(obj,typeof key!="symbol"?key+"":key,value);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const core=require("@univerjs/core"),engineRender=require("@univerjs/engine-render"),rxjs=require("rxjs"),SetTextSelectionsOperation={id:"doc.operation.set-selections",type:core.CommandType.OPERATION,handler:__name(()=>!0,"handler")};var __defProp$4=Object.defineProperty,__getOwnPropDesc$4=Object.getOwnPropertyDescriptor,__decorateClass$4=__name((decorators,target,key,kind)=>{for(var result=kind>1?void 0:kind?__getOwnPropDesc$4(target,key):target,i=decorators.length-1,decorator;i>=0;i--)(decorator=decorators[i])&&(result=(kind?decorator(target,key,result):decorator(result))||result);return kind&&result&&__defProp$4(target,key,result),result},"__decorateClass$4"),__decorateParam$4=__name((index,decorator)=>(target,key)=>decorator(target,key,index),"__decorateParam$4"),_a;exports.DocSelectionManagerService=(_a=class extends core.RxDisposable{constructor(_commandService,_univerInstanceService){super();__publicField(this,"_currentSelection",null);__publicField(this,"_textSelectionInfo",new Map);__publicField(this,"_textSelection$",new rxjs.Subject);__publicField(this,"textSelection$",this._textSelection$.asObservable());__publicField(this,"_refreshSelection$",new rxjs.BehaviorSubject(null));__publicField(this,"refreshSelection$",this._refreshSelection$.asObservable());this._commandService=_commandService,this._univerInstanceService=_univerInstanceService,this._listenCurrentUnit()}_listenCurrentUnit(){this._univerInstanceService.getCurrentTypeOfUnit$(core.UniverInstanceType.UNIVER_DOC).pipe(rxjs.takeUntil(this.dispose$)).subscribe(documentModel=>{if(documentModel==null)return;const unitId=documentModel.getUnitId();this._setCurrentSelectionNotRefresh({unitId,subUnitId:unitId})})}__getCurrentSelection(){return this._currentSelection}getSelectionInfo(params=this._currentSelection){return this._getTextRanges(params)}refreshSelection(params=this._currentSelection){params!=null&&this._refresh(params)}__TEST_ONLY_setCurrentSelection(param){this._currentSelection=param,this._refresh(param)}getTextRanges(params=this._currentSelection){var _a6;return(_a6=this._getTextRanges(params))==null?void 0:_a6.textRanges}getRectRanges(params=this._currentSelection){var _a6;return(_a6=this._getTextRanges(params))==null?void 0:_a6.rectRanges}getDocRanges(params=this._currentSelection){var _a6,_b;const textRanges=(_a6=this.getTextRanges(params))!=null?_a6:[],rectRanges=(_b=this.getRectRanges(params))!=null?_b:[];return[...textRanges,...rectRanges].filter(range=>range.startOffset!=null&&range.endOffset!=null).sort((a,b)=>a.startOffset>b.startOffset?1:a.startOffset<b.startOffset?-1:0)}getActiveTextRange(){const selectionInfo=this._getTextRanges(this._currentSelection);if(selectionInfo==null)return;const{textRanges}=selectionInfo;return textRanges.find(textRange=>textRange.isActive)}getActiveRectRange(){const selectionInfo=this._getTextRanges(this._currentSelection);if(selectionInfo==null)return;const{rectRanges}=selectionInfo;return rectRanges.find(rectRange=>rectRange.isActive)}__TEST_ONLY_add(textRanges,isEditing=!0){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges,rectRanges:[],segmentId:"",segmentPage:-1,isEditing,style:engineRender.NORMAL_TEXT_SELECTION_PLUGIN_STYLE})}replaceTextRanges(docRanges,isEditing=!0,options){return this.replaceDocRanges(docRanges,this._currentSelection,isEditing,options)}replaceDocRanges(docRanges,params=this._currentSelection,isEditing=!0,options){if(params==null)return;const{unitId,subUnitId}=params;this._refreshSelection$.next({unitId,subUnitId,docRanges,isEditing,options})}__replaceTextRangesWithNoRefresh(textSelectionInfo){if(this._currentSelection==null)return;const params={...this._currentSelection,...textSelectionInfo};this._replaceByParam(params),this._textSelection$.next(params);const{unitId,subUnitId,segmentId,style,textRanges,rectRanges,isEditing}=params,ranges=[...textRanges,...rectRanges].filter(range=>range.startOffset!=null&&range.endOffset!=null).sort((a,b)=>a.startOffset>b.startOffset?1:a.startOffset<b.startOffset?-1:0);this._commandService.executeCommand(SetTextSelectionsOperation.id,{unitId,subUnitId,segmentId,style,isEditing,ranges})}dispose(){this._textSelection$.complete()}_setCurrentSelectionNotRefresh(param){this._currentSelection=param}_getTextRanges(param){var _a6;if(param==null)return;const{unitId,subUnitId=""}=param;return(_a6=this._textSelectionInfo.get(unitId))==null?void 0:_a6.get(subUnitId)}_refresh(param){const allTextSelectionInfo=this._getTextRanges(param);if(allTextSelectionInfo==null)return;const{textRanges,rectRanges}=allTextSelectionInfo,docRanges=[...textRanges,...rectRanges],{unitId,subUnitId}=param;this._refreshSelection$.next({unitId,subUnitId,docRanges,isEditing:!1})}_replaceByParam(insertParam){const{unitId,subUnitId,...selectionInsertParam}=insertParam;this._textSelectionInfo.has(unitId)||this._textSelectionInfo.set(unitId,new Map),this._textSelectionInfo.get(unitId).set(subUnitId,{...selectionInsertParam})}_addByParam(insertParam){const{unitId,subUnitId,...selectionInsertParam}=insertParam;this._textSelectionInfo.has(unitId)||this._textSelectionInfo.set(unitId,new Map);const unitTextRange=this._textSelectionInfo.get(unitId);unitTextRange.has(subUnitId)?unitTextRange.get(subUnitId).textRanges.push(...insertParam.textRanges):unitTextRange.set(subUnitId,{...selectionInsertParam})}},__name(_a,"DocSelectionManagerService"),_a);exports.DocSelectionManagerService=__decorateClass$4([__decorateParam$4(0,core.ICommandService),__decorateParam$4(1,core.IUniverInstanceService)],exports.DocSelectionManagerService);var __defProp$3=Object.defineProperty,__getOwnPropDesc$3=Object.getOwnPropertyDescriptor,__decorateClass$3=__name((decorators,target,key,kind)=>{for(var result=kind>1?void 0:kind?__getOwnPropDesc$3(target,key):target,i=decorators.length-1,decorator;i>=0;i--)(decorator=decorators[i])&&(result=(kind?decorator(target,key,result):decorator(result))||result);return kind&&result&&__defProp$3(target,key,result),result},"__decorateClass$3"),__decorateParam$3=__name((index,decorator)=>(target,key)=>decorator(target,key,index),"__decorateParam$3"),_a2;exports.DocSkeletonManagerService=(_a2=class extends core.RxDisposable{constructor(_context,_localeService,_univerInstanceService){super();__publicField(this,"_skeleton");__publicField(this,"_docViewModel");__publicField(this,"_currentSkeleton$",new rxjs.BehaviorSubject(null));__publicField(this,"currentSkeleton$",this._currentSkeleton$.asObservable());__publicField(this,"_currentSkeletonBefore$",new rxjs.BehaviorSubject(null));__publicField(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());__publicField(this,"_currentViewModel$",new rxjs.BehaviorSubject(null));__publicField(this,"currentViewModel$",this._currentViewModel$.asObservable());this._context=_context,this._localeService=_localeService,this._univerInstanceService=_univerInstanceService,this._init(),this._univerInstanceService.getCurrentTypeOfUnit$(core.UniverInstanceType.UNIVER_DOC).pipe(rxjs.takeUntil(this.dispose$)).subscribe(documentModel=>{documentModel&&documentModel.getUnitId()===this._context.unitId&&this._update(documentModel)})}dispose(){super.dispose(),this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete()}getSkeleton(){return this._skeleton}getViewModel(){return this._docViewModel}_init(){const documentDataModel=this._context.unit;this._update(documentDataModel)}_update(documentDataModel){const unitId=this._context.unitId;if(documentDataModel.getBody()==null)return;this._docViewModel&&core.isInternalEditorID(unitId)?(this._docViewModel.reset(documentDataModel),this._context.unit=documentDataModel):this._docViewModel||(this._docViewModel=this._buildDocViewModel(documentDataModel)),this._skeleton||(this._skeleton=this._buildSkeleton(this._docViewModel));const skeleton=this._skeleton;skeleton.calculate(),this._currentSkeletonBefore$.next(skeleton),this._currentSkeleton$.next(skeleton),this._currentViewModel$.next(this._docViewModel)}_buildSkeleton(documentViewModel){return engineRender.DocumentSkeleton.create(documentViewModel,this._localeService)}_buildDocViewModel(documentDataModel){return new engineRender.DocumentViewModel(documentDataModel)}},__name(_a2,"DocSkeletonManagerService"),_a2);exports.DocSkeletonManagerService=__decorateClass$3([__decorateParam$3(1,core.Inject(core.LocaleService)),__decorateParam$3(2,core.IUniverInstanceService)],exports.DocSkeletonManagerService);const _DocStateEmitService=class _DocStateEmitService extends core.RxDisposable{constructor(){super();__publicField(this,"_docStateChangeParams$",new rxjs.BehaviorSubject(null));__publicField(this,"docStateChangeParams$",this._docStateChangeParams$.asObservable())}emitStateChangeInfo(params){this._docStateChangeParams$.next(params)}dispose(){super.dispose(),this._docStateChangeParams$.complete()}};__name(_DocStateEmitService,"DocStateEmitService");let DocStateEmitService=_DocStateEmitService;const RichTextEditingMutationId="doc.mutation.rich-text-editing",RichTextEditingMutation={id:RichTextEditingMutationId,type:core.CommandType.MUTATION,handler:__name((accessor,params)=>{var _a6,_b;const{unitId,segmentId="",actions,textRanges,prevTextRanges,trigger,noHistory,isCompositionEnd,noNeedSetTextRange,debounce,isEditing=!0,isSync,syncer}=params,univerInstanceService=accessor.get(core.IUniverInstanceService),renderManagerService=accessor.get(engineRender.IRenderManagerService),docStateEmitService=accessor.get(DocStateEmitService),documentDataModel=univerInstanceService.getUniverDocInstance(unitId),documentViewModel=(_a6=renderManagerService.getRenderById(unitId))==null?void 0:_a6.with(exports.DocSkeletonManagerService).getViewModel();if(documentDataModel==null||documentViewModel==null)throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${unitId}`);const docSelectionManagerService=accessor.get(exports.DocSelectionManagerService),docRanges=(_b=docSelectionManagerService.getDocRanges())!=null?_b:[],disabled=!!documentDataModel.getSnapshot().disabled;if(core.JSONX.isNoop(actions)||actions&&actions.length===0||disabled)return{unitId,actions:[],textRanges:docRanges};const undoActions=core.JSONX.invertWithDoc(actions,documentDataModel.getSnapshot());documentDataModel.apply(actions),documentViewModel.reset(documentDataModel),!noNeedSetTextRange&&textRanges&&trigger!=null&&queueMicrotask(()=>{docSelectionManagerService.replaceDocRanges(textRanges,{unitId,subUnitId:unitId},isEditing,params.options)});const changeState={commandId:RichTextEditingMutationId,unitId,segmentId,trigger,noHistory,debounce,redoState:{actions,textRanges},undoState:{actions:undoActions,textRanges:prevTextRanges!=null?prevTextRanges:docRanges},isCompositionEnd,isSync,syncer};return docStateEmitService.emitStateChangeInfo(changeState),{unitId,actions:undoActions,textRanges:docRanges}},"handler")},DocsRenameMutation={id:"doc.mutation.rename-doc",type:core.CommandType.MUTATION,handler:__name((accessor,params)=>{const doc=accessor.get(core.IUniverInstanceService).getUnit(params.unitId,core.UniverInstanceType.UNIVER_DOC);return doc?(doc.setName(params.name),!0):!1},"handler")},PLUGIN_CONFIG_KEY="docs.config",defaultPluginConfig={};var __defProp$2=Object.defineProperty,__getOwnPropDesc$2=Object.getOwnPropertyDescriptor,__decorateClass$2=__name((decorators,target,key,kind)=>{for(var result=kind>1?void 0:kind?__getOwnPropDesc$2(target,key):target,i=decorators.length-1,decorator;i>=0;i--)(decorator=decorators[i])&&(result=(kind?decorator(target,key,result):decorator(result))||result);return kind&&result&&__defProp$2(target,key,result),result},"__decorateClass$2"),__decorateParam$2=__name((index,decorator)=>(target,key)=>decorator(target,key,index),"__decorateParam$2"),_a3;let DocCustomRangeController=(_a3=class extends core.Disposable{constructor(_commandService,_textSelectionManagerService,_univerInstanceService){super(),this._commandService=_commandService,this._textSelectionManagerService=_textSelectionManagerService,this._univerInstanceService=_univerInstanceService,this._initSelectionChange()}_transformCustomRange(doc,selection){var _a6;const{startOffset,endOffset,collapsed}=selection,customRanges=(_a6=doc.getCustomRanges())==null?void 0:_a6.filter(range=>!range.wholeEntity||startOffset<=range.startIndex&&endOffset>range.endIndex?!1:collapsed?range.startIndex<startOffset&&range.endIndex>=endOffset:core.BuildTextUtils.range.isIntersects(startOffset,endOffset-1,range.startIndex,range.endIndex));if(customRanges!=null&&customRanges.length){let start=startOffset,end=endOffset;return customRanges.forEach(range=>{start=Math.min(range.startIndex,start),end=Math.max(range.endIndex+1,end)}),{...selection,startOffset:start,endOffset:end,collapsed:start===end}}return selection}_initSelectionChange(){this.disposeWithMe(this._commandService.onCommandExecuted(commandInfo=>{if(commandInfo.id===SetTextSelectionsOperation.id){const params=commandInfo.params,{unitId,ranges,isEditing}=params,doc=this._univerInstanceService.getUnit(unitId);if(!doc)return;const transformedRanges=ranges.map(range=>this._transformCustomRange(doc,range));transformedRanges.some((range,i)=>ranges[i]!==range)&&this._textSelectionManagerService.replaceTextRanges(transformedRanges,isEditing)}}))}},__name(_a3,"DocCustomRangeController"),_a3);DocCustomRangeController=__decorateClass$2([__decorateParam$2(0,core.ICommandService),__decorateParam$2(1,core.Inject(exports.DocSelectionManagerService)),__decorateParam$2(2,core.IUniverInstanceService)],DocCustomRangeController);var __defProp$1=Object.defineProperty,__getOwnPropDesc$1=Object.getOwnPropertyDescriptor,__decorateClass$1=__name((decorators,target,key,kind)=>{for(var result=kind>1?void 0:kind?__getOwnPropDesc$1(target,key):target,i=decorators.length-1,decorator;i>=0;i--)(decorator=decorators[i])&&(result=(kind?decorator(target,key,result):decorator(result))||result);return kind&&result&&__defProp$1(target,key,result),result},"__decorateClass$1"),__decorateParam$1=__name((index,decorator)=>(target,key)=>decorator(target,key,index),"__decorateParam$1");const PLUGIN_NAME="DOCS_PLUGIN";var _a4;exports.UniverDocsPlugin=(_a4=class extends core.Plugin{constructor(_config=defaultPluginConfig,_injector,_configService){super(),this._config=_config,this._injector=_injector,this._configService=_configService;const{...rest}=this._config;this._configService.setConfig(PLUGIN_CONFIG_KEY,rest)}onStarting(){this._initializeDependencies(),this._initializeCommands()}_initializeCommands(){[RichTextEditingMutation,DocsRenameMutation,SetTextSelectionsOperation].forEach(command=>{this._injector.get(core.ICommandService).registerCommand(command)})}_initializeDependencies(){[[exports.DocSelectionManagerService],[DocStateEmitService],[DocCustomRangeController]].forEach(d=>this._injector.add(d))}onReady(){this._injector.get(DocCustomRangeController)}},__name(_a4,"UniverDocsPlugin"),__publicField(_a4,"pluginName",PLUGIN_NAME),_a4);exports.UniverDocsPlugin=__decorateClass$1([__decorateParam$1(1,core.Inject(core.Injector)),__decorateParam$1(2,core.IConfigService)],exports.UniverDocsPlugin);const CUSTOM_RANGE=core.createInterceptorKey("CUSTOM_RANGE"),CUSTOM_DECORATION=core.createInterceptorKey("CUSTOM_DECORATION"),DOC_INTERCEPTOR_POINT={CUSTOM_RANGE,CUSTOM_DECORATION};var __defProp2=Object.defineProperty,__getOwnPropDesc=Object.getOwnPropertyDescriptor,__decorateClass=__name((decorators,target,key,kind)=>{for(var result=kind>1?void 0:kind?__getOwnPropDesc(target,key):target,i=decorators.length-1,decorator;i>=0;i--)(decorator=decorators[i])&&(result=(kind?decorator(target,key,result):decorator(result))||result);return kind&&result&&__defProp2(target,key,result),result},"__decorateClass"),__decorateParam=__name((index,decorator)=>(target,key)=>decorator(target,key,index),"__decorateParam"),_a5;exports.DocInterceptorService=(_a5=class extends core.Disposable{constructor(_context,_docSkeletonManagerService){super();__publicField(this,"_interceptorsByName",new Map);this._context=_context,this._docSkeletonManagerService=_docSkeletonManagerService;const viewModel=this._docSkeletonManagerService.getViewModel(),unitId=viewModel.getDataModel().getUnitId();if(unitId===core.DOCS_NORMAL_EDITOR_UNIT_ID_KEY||unitId===core.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY)return;this.disposeWithMe(this.interceptDocumentViewModel(viewModel)),this.disposeWithMe(this.intercept(DOC_INTERCEPTOR_POINT.CUSTOM_RANGE,{priority:-1,handler:__name((data,pos,next)=>next(data),"handler")}));let disposableCollection=new core.DisposableCollection;viewModel.segmentViewModels$.subscribe(segmentViewModels=>{disposableCollection.dispose(),disposableCollection=new core.DisposableCollection,segmentViewModels.forEach(segmentViewModel=>{disposableCollection.add(this.interceptDocumentViewModel(segmentViewModel))})}),this.disposeWithMe(disposableCollection)}intercept(name,interceptor){const key=name;this._interceptorsByName.has(key)||this._interceptorsByName.set(key,[]);const interceptors=this._interceptorsByName.get(key);return interceptors.push(interceptor),this._interceptorsByName.set(key,interceptors.sort((a,b)=>{var _a6,_b;return((_a6=b.priority)!=null?_a6:0)-((_b=a.priority)!=null?_b:0)})),this.disposeWithMe(core.toDisposable(()=>core.remove(this._interceptorsByName.get(key),interceptor)))}fetchThroughInterceptors(name){const key=name,interceptors=this._interceptorsByName.get(key);return core.composeInterceptors(interceptors||[])}interceptDocumentViewModel(viewModel){const disposableCollection=new core.DisposableCollection;return disposableCollection.add(viewModel.registerCustomRangeInterceptor({getCustomRange:__name(index=>{var _a6;return this.fetchThroughInterceptors(DOC_INTERCEPTOR_POINT.CUSTOM_RANGE)(viewModel.getCustomRangeRaw(index),{index,unitId:viewModel.getDataModel().getUnitId(),customRanges:(_a6=viewModel.getDataModel().getCustomRanges())!=null?_a6:[]})},"getCustomRange"),getCustomDecoration:__name(index=>{var _a6;return this.fetchThroughInterceptors(DOC_INTERCEPTOR_POINT.CUSTOM_DECORATION)(viewModel.getCustomDecorationRaw(index),{index,unitId:viewModel.getDataModel().getUnitId(),customDecorations:(_a6=viewModel.getDataModel().getCustomDecorations())!=null?_a6:[]})},"getCustomDecoration")})),disposableCollection}},__name(_a5,"DocInterceptorService"),_a5);exports.DocInterceptorService=__decorateClass([__decorateParam(1,core.Inject(exports.DocSkeletonManagerService))],exports.DocInterceptorService);exports.DOC_INTERCEPTOR_POINT=DOC_INTERCEPTOR_POINT;exports.DocStateEmitService=DocStateEmitService;exports.RichTextEditingMutation=RichTextEditingMutation;exports.SetTextSelectionsOperation=SetTextSelectionsOperation;
1
+ "use strict";var z=Object.defineProperty;var G=(o,s,e)=>s in o?z(o,s,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[s]=e;var d=(o,s,e)=>G(o,typeof s!="symbol"?s+"":s,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("@univerjs/core"),C=require("@univerjs/engine-render"),p=require("rxjs"),b={id:"doc.operation.set-selections",type:c.CommandType.OPERATION,handler:()=>!0};var k=Object.defineProperty,W=Object.getOwnPropertyDescriptor,J=(o,s,e,t)=>{for(var n=t>1?void 0:t?W(s,e):s,i=o.length-1,r;i>=0;i--)(r=o[i])&&(n=(t?r(s,e,n):r(n))||n);return t&&n&&k(s,e,n),n},j=(o,s)=>(e,t)=>s(e,t,o);exports.DocSelectionManagerService=class extends c.RxDisposable{constructor(e,t){super();d(this,"_currentSelection",null);d(this,"_textSelectionInfo",new Map);d(this,"_textSelection$",new p.Subject);d(this,"textSelection$",this._textSelection$.asObservable());d(this,"_refreshSelection$",new p.BehaviorSubject(null));d(this,"refreshSelection$",this._refreshSelection$.asObservable());this._commandService=e,this._univerInstanceService=t,this._listenCurrentUnit()}_listenCurrentUnit(){this._univerInstanceService.getCurrentTypeOfUnit$(c.UniverInstanceType.UNIVER_DOC).pipe(p.takeUntil(this.dispose$)).subscribe(e=>{if(e==null)return;const t=e.getUnitId();this._setCurrentSelectionNotRefresh({unitId:t,subUnitId:t})})}__getCurrentSelection(){return this._currentSelection}getSelectionInfo(e=this._currentSelection){return this._getTextRanges(e)}refreshSelection(e=this._currentSelection){e!=null&&this._refresh(e)}__TEST_ONLY_setCurrentSelection(e){this._currentSelection=e,this._refresh(e)}getTextRanges(e=this._currentSelection){var t;return(t=this._getTextRanges(e))==null?void 0:t.textRanges}getRectRanges(e=this._currentSelection){var t;return(t=this._getTextRanges(e))==null?void 0:t.rectRanges}getDocRanges(e=this._currentSelection){var r,a;const t=(r=this.getTextRanges(e))!=null?r:[],n=(a=this.getRectRanges(e))!=null?a:[];return[...t,...n].filter(l=>l.startOffset!=null&&l.endOffset!=null).sort((l,u)=>l.startOffset>u.startOffset?1:l.startOffset<u.startOffset?-1:0)}getActiveTextRange(){const e=this._getTextRanges(this._currentSelection);if(e==null)return;const{textRanges:t}=e;return t.find(n=>n.isActive)}getActiveRectRange(){const e=this._getTextRanges(this._currentSelection);if(e==null)return;const{rectRanges:t}=e;return t.find(n=>n.isActive)}__TEST_ONLY_add(e,t=!0){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:e,rectRanges:[],segmentId:"",segmentPage:-1,isEditing:t,style:C.NORMAL_TEXT_SELECTION_PLUGIN_STYLE})}replaceTextRanges(e,t=!0,n){return this.replaceDocRanges(e,this._currentSelection,t,n)}replaceDocRanges(e,t=this._currentSelection,n=!0,i){if(t==null)return;const{unitId:r,subUnitId:a}=t;this._refreshSelection$.next({unitId:r,subUnitId:a,docRanges:e,isEditing:n,options:i})}__replaceTextRangesWithNoRefresh(e,t){if(this._currentSelection==null)return;const n={...e,...t};this._replaceByParam(n),this._textSelection$.next(n);const{unitId:i,subUnitId:r,segmentId:a,style:l,textRanges:u,rectRanges:g,isEditing:S}=n,f=[...u,...g].filter(_=>_.startOffset!=null&&_.endOffset!=null).sort((_,I)=>_.startOffset>I.startOffset?1:_.startOffset<I.startOffset?-1:0);this._commandService.executeCommand(b.id,{unitId:i,subUnitId:r,segmentId:a,style:l,isEditing:S,ranges:f})}dispose(){this._textSelection$.complete()}_setCurrentSelectionNotRefresh(e){this._currentSelection=e}_getTextRanges(e){var i;if(e==null)return;const{unitId:t,subUnitId:n=""}=e;return(i=this._textSelectionInfo.get(t))==null?void 0:i.get(n)}_refresh(e){const t=this._getTextRanges(e);if(t==null)return;const{textRanges:n,rectRanges:i}=t,r=[...n,...i],{unitId:a,subUnitId:l}=e;this._refreshSelection$.next({unitId:a,subUnitId:l,docRanges:r,isEditing:!1})}_replaceByParam(e){const{unitId:t,subUnitId:n,...i}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map),this._textSelectionInfo.get(t).set(n,{...i})}_addByParam(e){const{unitId:t,subUnitId:n,...i}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map);const r=this._textSelectionInfo.get(t);r.has(n)?r.get(n).textRanges.push(...e.textRanges):r.set(n,{...i})}};exports.DocSelectionManagerService=J([j(0,c.ICommandService),j(1,c.IUniverInstanceService)],exports.DocSelectionManagerService);var Y=Object.defineProperty,K=Object.getOwnPropertyDescriptor,q=(o,s,e,t)=>{for(var n=t>1?void 0:t?K(s,e):s,i=o.length-1,r;i>=0;i--)(r=o[i])&&(n=(t?r(s,e,n):r(n))||n);return t&&n&&Y(s,e,n),n},V=(o,s)=>(e,t)=>s(e,t,o);exports.DocSkeletonManagerService=class extends c.RxDisposable{constructor(e,t,n){super();d(this,"_skeleton");d(this,"_docViewModel");d(this,"_currentSkeleton$",new p.BehaviorSubject(null));d(this,"currentSkeleton$",this._currentSkeleton$.asObservable());d(this,"_currentSkeletonBefore$",new p.BehaviorSubject(null));d(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());d(this,"_currentViewModel$",new p.BehaviorSubject(null));d(this,"currentViewModel$",this._currentViewModel$.asObservable());this._context=e,this._localeService=t,this._univerInstanceService=n,this._init(),this._univerInstanceService.getCurrentTypeOfUnit$(c.UniverInstanceType.UNIVER_DOC).pipe(p.takeUntil(this.dispose$)).subscribe(i=>{i&&i.getUnitId()===this._context.unitId&&this._update(i)})}dispose(){super.dispose(),this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete()}getSkeleton(){return this._skeleton}getViewModel(){return this._docViewModel}_init(){const e=this._context.unit;this._update(e)}_update(e){const t=this._context.unitId;if(e.getBody()==null)return;this._docViewModel&&c.isInternalEditorID(t)?(this._docViewModel.reset(e),this._context.unit=e):this._docViewModel||(this._docViewModel=this._buildDocViewModel(e)),this._skeleton||(this._skeleton=this._buildSkeleton(this._docViewModel));const n=this._skeleton;n.calculate(),this._currentSkeletonBefore$.next(n),this._currentSkeleton$.next(n),this._currentViewModel$.next(this._docViewModel)}_buildSkeleton(e){return C.DocumentSkeleton.create(e,this._localeService)}_buildDocViewModel(e){return new C.DocumentViewModel(e)}};exports.DocSkeletonManagerService=q([V(1,c.Inject(c.LocaleService)),V(2,c.IUniverInstanceService)],exports.DocSkeletonManagerService);class E extends c.RxDisposable{constructor(){super();d(this,"_docStateChangeParams$",new p.BehaviorSubject(null));d(this,"docStateChangeParams$",this._docStateChangeParams$.asObservable())}emitStateChangeInfo(e){this._docStateChangeParams$.next(e)}dispose(){super.dispose(),this._docStateChangeParams$.complete()}}const A="doc.mutation.rich-text-editing",O={id:A,type:c.CommandType.MUTATION,handler:(o,s)=>{var $,B;const{unitId:e,segmentId:t="",actions:n,textRanges:i,prevTextRanges:r,trigger:a,noHistory:l,isCompositionEnd:u,noNeedSetTextRange:g,debounce:S,isEditing:f=!0,isSync:_,syncer:I}=s,m=o.get(c.IUniverInstanceService),v=o.get(C.IRenderManagerService),R=o.get(E),h=m.getUniverDocInstance(e),x=($=v.getRenderById(e))==null?void 0:$.with(exports.DocSkeletonManagerService).getViewModel();if(h==null||x==null)throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${e}`);const D=o.get(exports.DocSelectionManagerService),y=(B=D.getDocRanges())!=null?B:[],F=!!h.getSnapshot().disabled;if(c.JSONX.isNoop(n)||n&&n.length===0||F)return{unitId:e,actions:[],textRanges:y};const N=c.JSONX.invertWithDoc(n,h.getSnapshot());h.apply(n),x.reset(h),!g&&i&&a!=null&&queueMicrotask(()=>{D.replaceDocRanges(i,{unitId:e,subUnitId:e},f,s.options)});const L={commandId:A,unitId:e,segmentId:t,trigger:a,noHistory:l,debounce:S,redoState:{actions:n,textRanges:i},undoState:{actions:N,textRanges:r!=null?r:y},isCompositionEnd:u,isSync:_,syncer:I};return R.emitStateChangeInfo(L),{unitId:e,actions:N,textRanges:y}}},H={id:"doc.mutation.rename-doc",type:c.CommandType.MUTATION,handler:(o,s)=>{const t=o.get(c.IUniverInstanceService).getUnit(s.unitId,c.UniverInstanceType.UNIVER_DOC);return t?(t.setName(s.name),!0):!1}},Q="docs.config",Z={};var ee=Object.defineProperty,te=Object.getOwnPropertyDescriptor,ne=(o,s,e,t)=>{for(var n=t>1?void 0:t?te(s,e):s,i=o.length-1,r;i>=0;i--)(r=o[i])&&(n=(t?r(s,e,n):r(n))||n);return t&&n&&ee(s,e,n),n},w=(o,s)=>(e,t)=>s(e,t,o);let T=class extends c.Disposable{constructor(o,s,e){super(),this._commandService=o,this._textSelectionManagerService=s,this._univerInstanceService=e,this._initSelectionChange()}_transformCustomRange(o,s){var r;const{startOffset:e,endOffset:t,collapsed:n}=s,i=(r=o.getCustomRanges())==null?void 0:r.filter(a=>!a.wholeEntity||e<=a.startIndex&&t>a.endIndex?!1:n?a.startIndex<e&&a.endIndex>=t:c.BuildTextUtils.range.isIntersects(e,t-1,a.startIndex,a.endIndex));if(i!=null&&i.length){let a=e,l=t;return i.forEach(u=>{a=Math.min(u.startIndex,a),l=Math.max(u.endIndex+1,l)}),{...s,startOffset:a,endOffset:l,collapsed:a===l}}return s}_initSelectionChange(){this.disposeWithMe(this._commandService.onCommandExecuted(o=>{if(o.id===b.id){const s=o.params,{unitId:e,ranges:t,isEditing:n}=s,i=this._univerInstanceService.getUnit(e);if(!i)return;const r=t.map(a=>this._transformCustomRange(i,a));r.some((a,l)=>t[l]!==a)&&this._textSelectionManagerService.replaceTextRanges(r,n)}}))}};T=ne([w(0,c.ICommandService),w(1,c.Inject(exports.DocSelectionManagerService)),w(2,c.IUniverInstanceService)],T);var se=Object.defineProperty,ie=Object.getOwnPropertyDescriptor,re=(o,s,e,t)=>{for(var n=t>1?void 0:t?ie(s,e):s,i=o.length-1,r;i>=0;i--)(r=o[i])&&(n=(t?r(s,e,n):r(n))||n);return t&&n&&se(s,e,n),n},X=(o,s)=>(e,t)=>s(e,t,o);const oe="DOCS_PLUGIN";var P;exports.UniverDocsPlugin=(P=class extends c.Plugin{constructor(s=Z,e,t){super(),this._config=s,this._injector=e,this._configService=t;const{...n}=this._config;this._configService.setConfig(Q,n)}onStarting(){this._initializeDependencies(),this._initializeCommands()}_initializeCommands(){[O,H,b].forEach(s=>{this._injector.get(c.ICommandService).registerCommand(s)})}_initializeDependencies(){[[exports.DocSelectionManagerService],[E],[T]].forEach(s=>this._injector.add(s))}onReady(){this._injector.get(T)}},d(P,"pluginName",oe),P);exports.UniverDocsPlugin=re([X(1,c.Inject(c.Injector)),X(2,c.IConfigService)],exports.UniverDocsPlugin);const ce=c.createInterceptorKey("CUSTOM_RANGE"),ae=c.createInterceptorKey("CUSTOM_DECORATION"),M={CUSTOM_RANGE:ce,CUSTOM_DECORATION:ae};var le=Object.defineProperty,ue=Object.getOwnPropertyDescriptor,de=(o,s,e,t)=>{for(var n=t>1?void 0:t?ue(s,e):s,i=o.length-1,r;i>=0;i--)(r=o[i])&&(n=(t?r(s,e,n):r(n))||n);return t&&n&&le(s,e,n),n},ge=(o,s)=>(e,t)=>s(e,t,o);exports.DocInterceptorService=class extends c.Disposable{constructor(e,t){super();d(this,"_interceptorsByName",new Map);this._context=e,this._docSkeletonManagerService=t;const n=this._docSkeletonManagerService.getViewModel(),i=n.getDataModel().getUnitId();if(i===c.DOCS_NORMAL_EDITOR_UNIT_ID_KEY||i===c.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY)return;this.disposeWithMe(this.interceptDocumentViewModel(n)),this.disposeWithMe(this.intercept(M.CUSTOM_RANGE,{priority:-1,handler:(a,l,u)=>u(a)}));let r=new c.DisposableCollection;n.segmentViewModels$.subscribe(a=>{r.dispose(),r=new c.DisposableCollection,a.forEach(l=>{r.add(this.interceptDocumentViewModel(l))})}),this.disposeWithMe(r)}intercept(e,t){const n=e;this._interceptorsByName.has(n)||this._interceptorsByName.set(n,[]);const i=this._interceptorsByName.get(n);return i.push(t),this._interceptorsByName.set(n,i.sort((r,a)=>{var l,u;return((l=a.priority)!=null?l:0)-((u=r.priority)!=null?u:0)})),this.disposeWithMe(c.toDisposable(()=>c.remove(this._interceptorsByName.get(n),t)))}fetchThroughInterceptors(e){const t=e,n=this._interceptorsByName.get(t);return c.composeInterceptors(n||[])}interceptDocumentViewModel(e){const t=new c.DisposableCollection;return t.add(e.registerCustomRangeInterceptor({getCustomRange:n=>{var i;return this.fetchThroughInterceptors(M.CUSTOM_RANGE)(e.getCustomRangeRaw(n),{index:n,unitId:e.getDataModel().getUnitId(),customRanges:(i=e.getDataModel().getCustomRanges())!=null?i:[]})},getCustomDecoration:n=>{var i;return this.fetchThroughInterceptors(M.CUSTOM_DECORATION)(e.getCustomDecorationRaw(n),{index:n,unitId:e.getDataModel().getUnitId(),customDecorations:(i=e.getDataModel().getCustomDecorations())!=null?i:[]})}})),t}};exports.DocInterceptorService=de([ge(1,c.Inject(exports.DocSkeletonManagerService))],exports.DocInterceptorService);function U(o,s=""){if(!s)return["body"];const{headers:e,footers:t}=o.getSnapshot();if(e==null&&t==null)throw new Error("Document data model must have headers or footers when update by segment id");if((e==null?void 0:e[s])!=null)return["headers",s,"body"];if((t==null?void 0:t[s])!=null)return["footers",s,"body"];throw new Error("Segment id not found in headers or footers")}function _e(o,s,e){const{unitId:t,segmentId:n}=s,r=o.get(c.IUniverInstanceService).getUnit(t);if(!r)return!1;const a={id:O.id,params:{unitId:s.unitId,actions:[],textRanges:void 0}},l=c.JSONX.getInstance(),u=c.BuildTextUtils.customRange.add({...s,body:e});if(!u)return!1;const g=U(r,n);return a.params.actions=l.editOp(u.serialize(),g),a}function he(o,s){var D;const{rangeId:e,rangeType:t,wholeEntity:n,properties:i,unitId:r,selection:a}=s,l=o.get(exports.DocSelectionManagerService),u=o.get(c.IUniverInstanceService),g=a!=null?a:(D=l.getTextRanges({unitId:r,subUnitId:r}))==null?void 0:D[0],S=g==null?void 0:g.segmentId;if(!g)return!1;const f=u.getUnit(r,c.UniverInstanceType.UNIVER_DOC);if(!f)return!1;const _=f.getSelfOrHeaderFooterModel(g.segmentId).getBody();if(!_)return!1;const{startOffset:I,endOffset:m}=c.BuildTextUtils.selection.normalizeSelection(g),v=c.BuildTextUtils.customRange.add({range:{startOffset:I,endOffset:m,collapsed:I===m},rangeId:e,rangeType:t,segmentId:S,wholeEntity:n,properties:i,body:_});if(!v)return!1;const R=c.JSONX.getInstance(),h={id:O.id,params:{unitId:r,actions:[],textRanges:v.selections},textX:v},x=U(f,S);return h.params.actions=R.editOp(v.serialize(),x),h}function Se(o,s){const{unitId:e,segmentId:t,insert:n}=s,r=o.get(c.IUniverInstanceService).getUnit(e);if(!r)return!1;const a={id:O.id,params:{unitId:s.unitId,actions:[],textRanges:void 0,segmentId:t}},l=c.JSONX.getInstance(),u=c.BuildTextUtils.customRange.delete(o,{documentDataModel:r,rangeId:s.rangeId,insert:n,segmentId:t});if(!u)return!1;const g=U(r,t);return a.params.actions=l.editOp(u.serialize(),g),a.params.textRanges=u.selections,a}function fe(o,s){var m,v,R,h;const{unitId:e,body:t,doc:n}=s;let i=n;if(i||(i=o.get(c.IUniverInstanceService).getUnit(e)),!i)return!1;const r=(m=s.selection)==null?void 0:m.segmentId,a=(v=i.getSelfOrHeaderFooterModel(r))==null?void 0:v.getBody();if(!a)return!1;const l=o.get(exports.DocSelectionManagerService),u=(R=s.selection)!=null?R:l.getActiveTextRange();if(!u||!a)return!1;const g=(h=s.textRanges)!=null?h:[{startOffset:u.startOffset+t.dataStream.length,endOffset:u.startOffset+t.dataStream.length,collapsed:!0,segmentId:r}],S=c.BuildTextUtils.selection.replace({selection:u,body:t,doc:i});if(!S)return!1;const f={id:O.id,params:{unitId:e,actions:[],textRanges:g,debounce:!0,segmentId:r},textX:S},_=c.JSONX.getInstance(),I=U(i,r);return f.params.actions=_.editOp(S.serialize(),I),f}exports.DOC_INTERCEPTOR_POINT=M;exports.DocStateEmitService=E;exports.RichTextEditingMutation=O;exports.SetTextSelectionsOperation=b;exports.addCustomRangeBySelectionFactory=he;exports.addCustomRangeFactory=_e;exports.deleteCustomRangeFactory=Se;exports.replaceSelectionFactory=fe;