@univerjs/docs 0.20.1 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -1 +1,741 @@
1
- import{BuildTextUtils as e,CommandType as t,DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY as n,DOCS_NORMAL_EDITOR_UNIT_ID_KEY as r,Disposable as i,DisposableCollection as a,ICommandService as o,IConfigService as s,IUniverInstanceService as c,Inject as l,Injector as u,JSONX as d,LocaleService as f,Plugin as p,RxDisposable as m,UniverInstanceType as h,composeInterceptors as g,createInterceptorKey as _,isInternalEditorID as v,merge as y,remove as b,toDisposable as x}from"@univerjs/core";import{DocumentSkeleton as S,DocumentViewModel as C,IRenderManagerService as w,NORMAL_TEXT_SELECTION_PLUGIN_STYLE as T}from"@univerjs/engine-render";import{BehaviorSubject as E,Subject as D,takeUntil as O}from"rxjs";const k={id:`doc.operation.set-selections`,type:t.OPERATION,handler:()=>!0};function A(e){"@babel/helpers - typeof";return A=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},A(e)}function j(e,t){if(A(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(A(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function M(e){var t=j(e,`string`);return A(t)==`symbol`?t:t+``}function N(e,t,n){return(t=M(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function P(e,t){return function(n,r){t(n,r,e)}}function F(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let I=class extends m{constructor(e,t){super(),this._commandService=e,this._univerInstanceService=t,N(this,`_currentSelection`,null),N(this,`_textSelectionInfo`,new Map),N(this,`_textSelection$`,new D),N(this,`textSelection$`,this._textSelection$.asObservable()),N(this,`_refreshSelection$`,new E(null)),N(this,`refreshSelection$`,this._refreshSelection$.asObservable()),this._listenCurrentUnit()}_listenCurrentUnit(){this._univerInstanceService.getCurrentTypeOfUnit$(h.UNIVER_DOC).pipe(O(this.dispose$)).subscribe(e=>{if(e==null)return;let 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 t,n;let r=(t=this.getTextRanges(e))==null?[]:t,i=(n=this.getRectRanges(e))==null?[]:n;return[...r,...i].filter(e=>e.startOffset!=null&&e.endOffset!=null).sort((e,t)=>e.startOffset>t.startOffset?1:e.startOffset<t.startOffset?-1:0)}getActiveTextRange(){let e=this._getTextRanges(this._currentSelection);if(e==null)return;let{textRanges:t}=e;return t.find(e=>e.isActive)}getActiveRectRange(){let e=this._getTextRanges(this._currentSelection);if(e==null)return;let{rectRanges:t}=e;return t.find(e=>e.isActive)}__TEST_ONLY_add(e,t=!0){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:e,rectRanges:[],segmentId:``,segmentPage:-1,isEditing:t,style:T})}replaceTextRanges(e,t=!0,n){return this.replaceDocRanges(e,this._currentSelection,t,n)}replaceDocRanges(e,t=this._currentSelection,n=!0,r){if(t==null)return;let{unitId:i,subUnitId:a}=t;this._refreshSelection$.next({unitId:i,subUnitId:a,docRanges:e,isEditing:n,options:r})}__replaceTextRangesWithNoRefresh(e,t){if(this._currentSelection==null)return;let n={...e,...t};this._replaceByParam(n),this._textSelection$.next(n);let{unitId:r,subUnitId:i,segmentId:a,style:o,textRanges:s,rectRanges:c,isEditing:l}=n,u=[...s,...c].filter(e=>e.startOffset!=null&&e.endOffset!=null).sort((e,t)=>e.startOffset>t.startOffset?1:e.startOffset<t.startOffset?-1:0);this._commandService.executeCommand(k.id,{unitId:r,subUnitId:i,segmentId:a,style:o,isEditing:l,ranges:u})}dispose(){this._textSelection$.complete(),this._refreshSelection$.complete()}_setCurrentSelectionNotRefresh(e){this._currentSelection=e}_getTextRanges(e){var t;if(e==null)return;let{unitId:n,subUnitId:r=``}=e;return(t=this._textSelectionInfo.get(n))==null?void 0:t.get(r)}_refresh(e){let t=this._getTextRanges(e);if(t==null)return;let{textRanges:n,rectRanges:r}=t,i=[...n,...r],{unitId:a,subUnitId:o}=e;this._refreshSelection$.next({unitId:a,subUnitId:o,docRanges:i,isEditing:!1})}_replaceByParam(e){let{unitId:t,subUnitId:n,...r}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map),this._textSelectionInfo.get(t).set(n,{...r})}_addByParam(e){let{unitId:t,subUnitId:n,...r}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map);let i=this._textSelectionInfo.get(t);i.has(n)?i.get(n).textRanges.push(...e.textRanges):i.set(n,{...r})}};I=F([P(0,o),P(1,c)],I);let L=class extends m{constructor(e,t,n){super(),this._context=e,this._localeService=t,this._univerInstanceService=n,N(this,`_skeleton`,void 0),N(this,`_docViewModel`,void 0),N(this,`_currentSkeleton$`,new E(null)),N(this,`currentSkeleton$`,this._currentSkeleton$.asObservable()),N(this,`_currentSkeletonBefore$`,new E(null)),N(this,`currentSkeletonBefore$`,this._currentSkeletonBefore$.asObservable()),N(this,`_currentViewModel$`,new E(null)),N(this,`currentViewModel$`,this._currentViewModel$.asObservable()),this._init(),this._univerInstanceService.getCurrentTypeOfUnit$(h.UNIVER_DOC).pipe(O(this.dispose$)).subscribe(e=>{e&&e.getUnitId()===this._context.unitId&&this._update(e)})}dispose(){super.dispose(),this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete()}getSkeleton(){return this._skeleton}getViewModel(){return this._docViewModel}_init(){let e=this._context.unit;this._update(e)}_update(e){let t=this._context.unitId;if(e.getBody()==null)return;this._docViewModel&&v(t)?(this._docViewModel.reset(e),this._context.unit=e):this._docViewModel||(this._docViewModel=this._buildDocViewModel(e)),this._skeleton||(this._skeleton=this._buildSkeleton(this._docViewModel));let n=this._skeleton;n.calculate(),this._currentSkeletonBefore$.next(n),this._currentSkeleton$.next(n),this._currentViewModel$.next(this._docViewModel)}_buildSkeleton(e){return S.create(e,this._localeService)}_buildDocViewModel(e){return new C(e)}};L=F([P(1,l(f)),P(2,c)],L);var R=class extends m{constructor(){super(),N(this,`_docStateChangeParams$`,new E(null)),N(this,`docStateChangeParams$`,this._docStateChangeParams$.asObservable())}emitStateChangeInfo(e){this._docStateChangeParams$.next(e)}dispose(){super.dispose(),this._docStateChangeParams$.complete()}};const z=`doc.mutation.rich-text-editing`,B={id:z,type:t.MUTATION,handler:(e,t)=>{var n,r;let{unitId:i,segmentId:a=``,actions:o,textRanges:s,prevTextRanges:l,trigger:u,noHistory:f,isCompositionEnd:p,noNeedSetTextRange:m,debounce:h,isEditing:g=!0,isSync:_,syncer:v}=t,y=e.get(c),b=e.get(w),x=e.get(R),S=y.getUniverDocInstance(i),C=(n=b.getRenderById(i))==null?void 0:n.with(L).getViewModel();if(S==null||C==null)throw Error(`DocumentDataModel or documentViewModel not found for unitId: ${i}`);let T=e.get(I),E=(r=T.getDocRanges())==null?[]:r,D=!!S.getSnapshot().disabled;if(d.isNoop(o)||o&&o.length===0||D)return{unitId:i,actions:[],textRanges:E};let O=d.invertWithDoc(o,S.getSnapshot());S.apply(o),C.reset(S),!m&&s&&u!=null&&!_&&queueMicrotask(()=>{T.replaceDocRanges(s,{unitId:i,subUnitId:i},g,t.options)});let k={commandId:z,unitId:i,segmentId:a,trigger:u,noHistory:f,debounce:h,redoState:{actions:o,textRanges:s},undoState:{actions:O,textRanges:l==null?E:l},isCompositionEnd:p,isSync:_,syncer:v};return x.emitStateChangeInfo(k),{unitId:i,actions:O,textRanges:E}}};var V=`@univerjs/docs`,H=`0.20.1`;const U={id:`doc.mutation.rename-doc`,type:t.MUTATION,handler:(e,t)=>{let n=e.get(c).getUnit(t.unitId,h.UNIVER_DOC);return n?(n.setName(t.name),!0):!1}},W=`docs.config`;Symbol(W);const G={};let K=class extends i{constructor(e,t,n){super(),this._commandService=e,this._textSelectionManagerService=t,this._univerInstanceService=n,this._initSelectionChange()}_transformCustomRange(t,n){var r;let{startOffset:i,endOffset:a,collapsed:o}=n,s=(r=t.getCustomRanges())==null?void 0:r.filter(t=>!t.wholeEntity||i<=t.startIndex&&a>t.endIndex?!1:o?t.startIndex<i&&t.endIndex>=a:e.range.isIntersects(i,a-1,t.startIndex,t.endIndex));if(s!=null&&s.length){let e=i,t=a;return s.forEach(n=>{e=Math.min(n.startIndex,e),t=Math.max(n.endIndex+1,t)}),{...n,startOffset:e,endOffset:t,collapsed:e===t}}return n}_initSelectionChange(){this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===k.id){let{unitId:t,ranges:n,isEditing:r}=e.params,i=this._univerInstanceService.getUnit(t);if(!i)return;let a=n.map(e=>this._transformCustomRange(i,e));a.some((e,t)=>n[t]!==e)&&this._textSelectionManagerService.replaceTextRanges(a,r)}}))}};K=F([P(0,o),P(1,l(I)),P(2,c)],K);let q=class extends p{constructor(e=G,t,n){super(),this._config=e,this._injector=t,this._configService=n;let{...r}=y({},G,this._config);this._configService.setConfig(W,r)}onStarting(){this._initializeDependencies(),this._initializeCommands()}_initializeCommands(){[B,U,k].forEach(e=>{this._injector.get(o).registerCommand(e)})}_initializeDependencies(){[[I],[R],[K]].forEach(e=>this._injector.add(e))}onReady(){this._injector.get(K)}};N(q,`pluginName`,`DOCS_PLUGIN`),N(q,`packageName`,V),N(q,`version`,H),q=F([P(1,l(u)),P(2,s)],q);const J={CUSTOM_RANGE:_(`CUSTOM_RANGE`),CUSTOM_DECORATION:_(`CUSTOM_DECORATION`)};let Y=class extends i{constructor(e,t){super(),this._context=e,this._docSkeletonManagerService=t,N(this,`_interceptorsByName`,new Map);let i=this._docSkeletonManagerService.getViewModel(),o=i.getDataModel().getUnitId();if(o===r||o===n)return;this.disposeWithMe(this.interceptDocumentViewModel(i)),this.disposeWithMe(this.intercept(J.CUSTOM_RANGE,{priority:-1,handler:(e,t,n)=>n(e)}));let s=new a;i.segmentViewModels$.subscribe(e=>{s.dispose(),s=new a,e.forEach(e=>{s.add(this.interceptDocumentViewModel(e))})}),this.disposeWithMe(s)}intercept(e,t){let n=e;this._interceptorsByName.has(n)||this._interceptorsByName.set(n,[]);let r=this._interceptorsByName.get(n);return r.push(t),this._interceptorsByName.set(n,r.sort((e,t)=>{var n,r;return((n=t.priority)==null?0:n)-((r=e.priority)==null?0:r)})),this.disposeWithMe(x(()=>b(this._interceptorsByName.get(n),t)))}fetchThroughInterceptors(e){let t=e;return g(this._interceptorsByName.get(t)||[])}interceptDocumentViewModel(e){let t=new a;return t.add(e.registerCustomRangeInterceptor({getCustomRange:t=>{var n;return this.fetchThroughInterceptors(J.CUSTOM_RANGE)(e.getCustomRangeRaw(t),{index:t,unitId:e.getDataModel().getUnitId(),customRanges:(n=e.getDataModel().getCustomRanges())==null?[]:n})},getCustomDecoration:t=>{var n;return this.fetchThroughInterceptors(J.CUSTOM_DECORATION)(e.getCustomDecorationRaw(t),{index:t,unitId:e.getDataModel().getUnitId(),customDecorations:(n=e.getDataModel().getCustomDecorations())==null?[]:n})}})),t}};Y=F([P(1,l(L))],Y);function X(e,t=``){if(!t)return[`body`];let{headers:n,footers:r}=e.getSnapshot();if(n==null&&r==null)throw Error(`Document data model must have headers or footers when update by segment id`);if((n==null?void 0:n[t])!=null)return[`headers`,t,`body`];if((r==null?void 0:r[t])!=null)return[`footers`,t,`body`];throw Error(`Segment id not found in headers or footers`)}function Z(t,n,r){let{unitId:i,segmentId:a}=n,o=t.get(c).getUnit(i);if(!o)return!1;let s={id:B.id,params:{unitId:n.unitId,actions:[],textRanges:void 0}},l=d.getInstance(),u=e.customRange.add({...n,body:r});if(!u)return!1;let f=X(o,a);return s.params.actions=l.editOp(u.serialize(),f),s}function Q(t,n){var r;let{rangeId:i,rangeType:a,wholeEntity:o,properties:s,unitId:l,selections:u}=n,f=t.get(I),p=t.get(c),m=u==null?f.getTextRanges({unitId:l,subUnitId:l}):u,g=m==null||(r=m[0])==null?void 0:r.segmentId;if(!(m!=null&&m.length))return!1;let _=p.getUnit(l,h.UNIVER_DOC);if(!_)return!1;let v=_.getSelfOrHeaderFooterModel(g).getBody();if(!v)return!1;let y=e.customRange.add({ranges:m,rangeId:i,rangeType:a,segmentId:g,wholeEntity:o,properties:s,body:v});if(!y)return!1;let b=d.getInstance(),x={id:B.id,params:{unitId:l,actions:[],textRanges:y.selections,segmentId:g},textX:y},S=X(_,g);return x.params.actions=b.editOp(y.serialize(),S),x}function $(t,n){let{unitId:r,segmentId:i,insert:a}=n,o=t.get(c).getUnit(r);if(!o)return!1;let s={id:B.id,params:{unitId:n.unitId,actions:[],textRanges:void 0,segmentId:i}},l=d.getInstance(),u=e.customRange.delete({documentDataModel:o,rangeId:n.rangeId,insert:a,segmentId:i});if(!u)return!1;let f=X(o,i);return s.params.actions=l.editOp(u.serialize(),f),s.params.textRanges=u.selections,s}function ee(t,n){var r,i,a,o;let{unitId:s,body:l,doc:u}=n,f=u;if(f||(f=t.get(c).getUnit(s)),!f)return!1;let p=(r=n.selection)==null?void 0:r.segmentId,m=(i=f.getSelfOrHeaderFooterModel(p))==null?void 0:i.getBody();if(!m)return!1;let h=t.get(I),g=(a=n.selection)==null?h.getActiveTextRange():a;if(!g||!m)return!1;let _=(o=n.textRanges)==null?[{startOffset:g.startOffset+l.dataStream.length,endOffset:g.startOffset+l.dataStream.length,collapsed:!0,segmentId:p}]:o,v=e.selection.replace({selection:g,body:l,doc:f});if(!v)return!1;let y={id:B.id,params:{unitId:s,actions:[],textRanges:_,debounce:!0,segmentId:p},textX:v},b=d.getInstance();return y.params.actions=b.editOp(v.serialize()),y}export{J as DOC_INTERCEPTOR_POINT,Y as DocInterceptorService,I as DocSelectionManagerService,L as DocSkeletonManagerService,R as DocStateEmitService,B as RichTextEditingMutation,k as SetTextSelectionsOperation,q as UniverDocsPlugin,Q as addCustomRangeBySelectionFactory,Z as addCustomRangeFactory,$ as deleteCustomRangeFactory,ee as replaceSelectionFactory};
1
+ import { BuildTextUtils, CommandType, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, Disposable, DisposableCollection, ICommandService, IConfigService, IUniverInstanceService, Inject, Injector, JSONX, LocaleService, Plugin, RxDisposable, UniverInstanceType, composeInterceptors, createInterceptorKey, isInternalEditorID, merge, remove, toDisposable } from "@univerjs/core";
2
+ import { DocumentSkeleton, DocumentViewModel, IRenderManagerService, NORMAL_TEXT_SELECTION_PLUGIN_STYLE } from "@univerjs/engine-render";
3
+ import { BehaviorSubject, Subject, takeUntil } from "rxjs";
4
+
5
+ //#region src/commands/operations/text-selection.operation.ts
6
+ const SetTextSelectionsOperation = {
7
+ id: "doc.operation.set-selections",
8
+ type: CommandType.OPERATION,
9
+ handler: () => {
10
+ return true;
11
+ }
12
+ };
13
+
14
+ //#endregion
15
+ //#region \0@oxc-project+runtime@0.124.0/helpers/typeof.js
16
+ function _typeof(o) {
17
+ "@babel/helpers - typeof";
18
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
19
+ return typeof o;
20
+ } : function(o) {
21
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
22
+ }, _typeof(o);
23
+ }
24
+
25
+ //#endregion
26
+ //#region \0@oxc-project+runtime@0.124.0/helpers/toPrimitive.js
27
+ function toPrimitive(t, r) {
28
+ if ("object" != _typeof(t) || !t) return t;
29
+ var e = t[Symbol.toPrimitive];
30
+ if (void 0 !== e) {
31
+ var i = e.call(t, r || "default");
32
+ if ("object" != _typeof(i)) return i;
33
+ throw new TypeError("@@toPrimitive must return a primitive value.");
34
+ }
35
+ return ("string" === r ? String : Number)(t);
36
+ }
37
+
38
+ //#endregion
39
+ //#region \0@oxc-project+runtime@0.124.0/helpers/toPropertyKey.js
40
+ function toPropertyKey(t) {
41
+ var i = toPrimitive(t, "string");
42
+ return "symbol" == _typeof(i) ? i : i + "";
43
+ }
44
+
45
+ //#endregion
46
+ //#region \0@oxc-project+runtime@0.124.0/helpers/defineProperty.js
47
+ function _defineProperty(e, r, t) {
48
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
49
+ value: t,
50
+ enumerable: !0,
51
+ configurable: !0,
52
+ writable: !0
53
+ }) : e[r] = t, e;
54
+ }
55
+
56
+ //#endregion
57
+ //#region \0@oxc-project+runtime@0.124.0/helpers/decorateParam.js
58
+ function __decorateParam(paramIndex, decorator) {
59
+ return function(target, key) {
60
+ decorator(target, key, paramIndex);
61
+ };
62
+ }
63
+
64
+ //#endregion
65
+ //#region \0@oxc-project+runtime@0.124.0/helpers/decorate.js
66
+ function __decorate(decorators, target, key, desc) {
67
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
68
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
69
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
70
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
71
+ }
72
+
73
+ //#endregion
74
+ //#region src/services/doc-selection-manager.service.ts
75
+ let DocSelectionManagerService = class DocSelectionManagerService extends RxDisposable {
76
+ constructor(_commandService, _univerInstanceService) {
77
+ super();
78
+ this._commandService = _commandService;
79
+ this._univerInstanceService = _univerInstanceService;
80
+ _defineProperty(this, "_currentSelection", null);
81
+ _defineProperty(this, "_textSelectionInfo", /* @__PURE__ */ new Map());
82
+ _defineProperty(this, "_textSelection$", new Subject());
83
+ _defineProperty(this, "textSelection$", this._textSelection$.asObservable());
84
+ _defineProperty(this, "_refreshSelection$", new BehaviorSubject(null));
85
+ _defineProperty(this, "refreshSelection$", this._refreshSelection$.asObservable());
86
+ this._listenCurrentUnit();
87
+ }
88
+ _listenCurrentUnit() {
89
+ this._univerInstanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_DOC).pipe(takeUntil(this.dispose$)).subscribe((documentModel) => {
90
+ if (documentModel == null) return;
91
+ const unitId = documentModel.getUnitId();
92
+ this._setCurrentSelectionNotRefresh({
93
+ unitId,
94
+ subUnitId: unitId
95
+ });
96
+ });
97
+ }
98
+ __getCurrentSelection() {
99
+ return this._currentSelection;
100
+ }
101
+ getSelectionInfo(params = this._currentSelection) {
102
+ return this._getTextRanges(params);
103
+ }
104
+ refreshSelection(params = this._currentSelection) {
105
+ if (params == null) return;
106
+ this._refresh(params);
107
+ }
108
+ __TEST_ONLY_setCurrentSelection(param) {
109
+ this._currentSelection = param;
110
+ this._refresh(param);
111
+ }
112
+ getTextRanges(params = this._currentSelection) {
113
+ var _this$_getTextRanges;
114
+ return (_this$_getTextRanges = this._getTextRanges(params)) === null || _this$_getTextRanges === void 0 ? void 0 : _this$_getTextRanges.textRanges;
115
+ }
116
+ getRectRanges(params = this._currentSelection) {
117
+ var _this$_getTextRanges2;
118
+ return (_this$_getTextRanges2 = this._getTextRanges(params)) === null || _this$_getTextRanges2 === void 0 ? void 0 : _this$_getTextRanges2.rectRanges;
119
+ }
120
+ getDocRanges(params = this._currentSelection) {
121
+ var _this$getTextRanges, _this$getRectRanges;
122
+ const textRanges = (_this$getTextRanges = this.getTextRanges(params)) !== null && _this$getTextRanges !== void 0 ? _this$getTextRanges : [];
123
+ const rectRanges = (_this$getRectRanges = this.getRectRanges(params)) !== null && _this$getRectRanges !== void 0 ? _this$getRectRanges : [];
124
+ return [...textRanges, ...rectRanges].filter((range) => range.startOffset != null && range.endOffset != null).sort((a, b) => {
125
+ if (a.startOffset > b.startOffset) return 1;
126
+ else if (a.startOffset < b.startOffset) return -1;
127
+ else return 0;
128
+ });
129
+ }
130
+ getActiveTextRange() {
131
+ const selectionInfo = this._getTextRanges(this._currentSelection);
132
+ if (selectionInfo == null) return;
133
+ const { textRanges } = selectionInfo;
134
+ return textRanges.find((textRange) => textRange.isActive);
135
+ }
136
+ /**
137
+ *
138
+ * @deprecated
139
+ */
140
+ getActiveRectRange() {
141
+ const selectionInfo = this._getTextRanges(this._currentSelection);
142
+ if (selectionInfo == null) return;
143
+ const { rectRanges } = selectionInfo;
144
+ return rectRanges.find((rectRange) => rectRange.isActive);
145
+ }
146
+ __TEST_ONLY_add(textRanges, isEditing = true) {
147
+ if (this._currentSelection == null) return;
148
+ this._addByParam({
149
+ ...this._currentSelection,
150
+ textRanges,
151
+ rectRanges: [],
152
+ segmentId: "",
153
+ segmentPage: -1,
154
+ isEditing,
155
+ style: NORMAL_TEXT_SELECTION_PLUGIN_STYLE
156
+ });
157
+ }
158
+ /**
159
+ * @deprecated pls use replaceDocRanges.
160
+ */
161
+ replaceTextRanges(docRanges, isEditing = true, options) {
162
+ return this.replaceDocRanges(docRanges, this._currentSelection, isEditing, options);
163
+ }
164
+ replaceDocRanges(docRanges, params = this._currentSelection, isEditing = true, options) {
165
+ if (params == null) return;
166
+ const { unitId, subUnitId } = params;
167
+ this._refreshSelection$.next({
168
+ unitId,
169
+ subUnitId,
170
+ docRanges,
171
+ isEditing,
172
+ options
173
+ });
174
+ }
175
+ __replaceTextRangesWithNoRefresh(textSelectionInfo, search) {
176
+ if (this._currentSelection == null) return;
177
+ const params = {
178
+ ...textSelectionInfo,
179
+ ...search
180
+ };
181
+ this._replaceByParam(params);
182
+ this._textSelection$.next(params);
183
+ const { unitId, subUnitId, segmentId, style, textRanges, rectRanges, isEditing } = params;
184
+ const ranges = [...textRanges, ...rectRanges].filter((range) => range.startOffset != null && range.endOffset != null).sort((a, b) => {
185
+ if (a.startOffset > b.startOffset) return 1;
186
+ else if (a.startOffset < b.startOffset) return -1;
187
+ else return 0;
188
+ });
189
+ this._commandService.executeCommand(SetTextSelectionsOperation.id, {
190
+ unitId,
191
+ subUnitId,
192
+ segmentId,
193
+ style,
194
+ isEditing,
195
+ ranges
196
+ });
197
+ }
198
+ dispose() {
199
+ this._textSelection$.complete();
200
+ this._refreshSelection$.complete();
201
+ }
202
+ _setCurrentSelectionNotRefresh(param) {
203
+ this._currentSelection = param;
204
+ }
205
+ _getTextRanges(param) {
206
+ var _this$_textSelectionI;
207
+ if (param == null) return;
208
+ const { unitId, subUnitId = "" } = param;
209
+ return (_this$_textSelectionI = this._textSelectionInfo.get(unitId)) === null || _this$_textSelectionI === void 0 ? void 0 : _this$_textSelectionI.get(subUnitId);
210
+ }
211
+ _refresh(param) {
212
+ const allTextSelectionInfo = this._getTextRanges(param);
213
+ if (allTextSelectionInfo == null) return;
214
+ const { textRanges, rectRanges } = allTextSelectionInfo;
215
+ const docRanges = [...textRanges, ...rectRanges];
216
+ const { unitId, subUnitId } = param;
217
+ this._refreshSelection$.next({
218
+ unitId,
219
+ subUnitId,
220
+ docRanges,
221
+ isEditing: false
222
+ });
223
+ }
224
+ _replaceByParam(insertParam) {
225
+ const { unitId, subUnitId, ...selectionInsertParam } = insertParam;
226
+ if (!this._textSelectionInfo.has(unitId)) this._textSelectionInfo.set(unitId, /* @__PURE__ */ new Map());
227
+ this._textSelectionInfo.get(unitId).set(subUnitId, { ...selectionInsertParam });
228
+ }
229
+ _addByParam(insertParam) {
230
+ const { unitId, subUnitId, ...selectionInsertParam } = insertParam;
231
+ if (!this._textSelectionInfo.has(unitId)) this._textSelectionInfo.set(unitId, /* @__PURE__ */ new Map());
232
+ const unitTextRange = this._textSelectionInfo.get(unitId);
233
+ if (!unitTextRange.has(subUnitId)) unitTextRange.set(subUnitId, { ...selectionInsertParam });
234
+ else unitTextRange.get(subUnitId).textRanges.push(...insertParam.textRanges);
235
+ }
236
+ };
237
+ DocSelectionManagerService = __decorate([__decorateParam(0, ICommandService), __decorateParam(1, IUniverInstanceService)], DocSelectionManagerService);
238
+
239
+ //#endregion
240
+ //#region src/services/doc-skeleton-manager.service.ts
241
+ let DocSkeletonManagerService = class DocSkeletonManagerService extends RxDisposable {
242
+ constructor(_context, _localeService, _univerInstanceService) {
243
+ super();
244
+ this._context = _context;
245
+ this._localeService = _localeService;
246
+ this._univerInstanceService = _univerInstanceService;
247
+ _defineProperty(this, "_skeleton", void 0);
248
+ _defineProperty(this, "_docViewModel", void 0);
249
+ _defineProperty(this, "_currentSkeleton$", new BehaviorSubject(null));
250
+ _defineProperty(this, "currentSkeleton$", this._currentSkeleton$.asObservable());
251
+ _defineProperty(this, "_currentSkeletonBefore$", new BehaviorSubject(null));
252
+ _defineProperty(this, "currentSkeletonBefore$", this._currentSkeletonBefore$.asObservable());
253
+ _defineProperty(this, "_currentViewModel$", new BehaviorSubject(null));
254
+ _defineProperty(this, "currentViewModel$", this._currentViewModel$.asObservable());
255
+ this._init();
256
+ this._univerInstanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_DOC).pipe(takeUntil(this.dispose$)).subscribe((documentModel) => {
257
+ if (documentModel && documentModel.getUnitId() === this._context.unitId) this._update(documentModel);
258
+ });
259
+ }
260
+ dispose() {
261
+ super.dispose();
262
+ this._currentSkeletonBefore$.complete();
263
+ this._currentSkeleton$.complete();
264
+ }
265
+ getSkeleton() {
266
+ return this._skeleton;
267
+ }
268
+ getViewModel() {
269
+ return this._docViewModel;
270
+ }
271
+ _init() {
272
+ const documentDataModel = this._context.unit;
273
+ this._update(documentDataModel);
274
+ }
275
+ _update(documentDataModel) {
276
+ const unitId = this._context.unitId;
277
+ if (documentDataModel.getBody() == null) return;
278
+ if (this._docViewModel && isInternalEditorID(unitId)) {
279
+ this._docViewModel.reset(documentDataModel);
280
+ this._context.unit = documentDataModel;
281
+ } else if (!this._docViewModel) this._docViewModel = this._buildDocViewModel(documentDataModel);
282
+ if (!this._skeleton) this._skeleton = this._buildSkeleton(this._docViewModel);
283
+ const skeleton = this._skeleton;
284
+ skeleton.calculate();
285
+ this._currentSkeletonBefore$.next(skeleton);
286
+ this._currentSkeleton$.next(skeleton);
287
+ this._currentViewModel$.next(this._docViewModel);
288
+ }
289
+ _buildSkeleton(documentViewModel) {
290
+ return DocumentSkeleton.create(documentViewModel, this._localeService);
291
+ }
292
+ _buildDocViewModel(documentDataModel) {
293
+ return new DocumentViewModel(documentDataModel);
294
+ }
295
+ };
296
+ DocSkeletonManagerService = __decorate([__decorateParam(1, Inject(LocaleService)), __decorateParam(2, IUniverInstanceService)], DocSkeletonManagerService);
297
+
298
+ //#endregion
299
+ //#region src/services/doc-state-emit.service.ts
300
+ var DocStateEmitService = class extends RxDisposable {
301
+ constructor() {
302
+ super();
303
+ _defineProperty(this, "_docStateChangeParams$", new BehaviorSubject(null));
304
+ _defineProperty(this, "docStateChangeParams$", this._docStateChangeParams$.asObservable());
305
+ }
306
+ emitStateChangeInfo(params) {
307
+ this._docStateChangeParams$.next(params);
308
+ }
309
+ dispose() {
310
+ super.dispose();
311
+ this._docStateChangeParams$.complete();
312
+ }
313
+ };
314
+
315
+ //#endregion
316
+ //#region src/commands/mutations/core-editing.mutation.ts
317
+ const RichTextEditingMutationId = "doc.mutation.rich-text-editing";
318
+ /**
319
+ * The core mutator to change rich text actions. The execution result would be undo mutation params. Could be directly
320
+ * send to undo redo service (will be used by the triggering command).
321
+ */
322
+ const RichTextEditingMutation = {
323
+ id: RichTextEditingMutationId,
324
+ type: CommandType.MUTATION,
325
+ handler: (accessor, params) => {
326
+ var _renderManagerService, _docSelectionManagerS;
327
+ const { unitId, segmentId = "", actions, textRanges, prevTextRanges, trigger, noHistory, isCompositionEnd, noNeedSetTextRange, debounce, isEditing = true, isSync, syncer } = params;
328
+ const univerInstanceService = accessor.get(IUniverInstanceService);
329
+ const renderManagerService = accessor.get(IRenderManagerService);
330
+ const docStateEmitService = accessor.get(DocStateEmitService);
331
+ const documentDataModel = univerInstanceService.getUniverDocInstance(unitId);
332
+ const documentViewModel = (_renderManagerService = renderManagerService.getRenderById(unitId)) === null || _renderManagerService === void 0 ? void 0 : _renderManagerService.with(DocSkeletonManagerService).getViewModel();
333
+ if (documentDataModel == null || documentViewModel == null) throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${unitId}`);
334
+ const docSelectionManagerService = accessor.get(DocSelectionManagerService);
335
+ const docRanges = (_docSelectionManagerS = docSelectionManagerService.getDocRanges()) !== null && _docSelectionManagerS !== void 0 ? _docSelectionManagerS : [];
336
+ const disabled = !!documentDataModel.getSnapshot().disabled;
337
+ if (JSONX.isNoop(actions) || actions && actions.length === 0 || disabled) return {
338
+ unitId,
339
+ actions: [],
340
+ textRanges: docRanges
341
+ };
342
+ const undoActions = JSONX.invertWithDoc(actions, documentDataModel.getSnapshot());
343
+ documentDataModel.apply(actions);
344
+ documentViewModel.reset(documentDataModel);
345
+ if (!noNeedSetTextRange && textRanges && trigger != null && !isSync) queueMicrotask(() => {
346
+ docSelectionManagerService.replaceDocRanges(textRanges, {
347
+ unitId,
348
+ subUnitId: unitId
349
+ }, isEditing, params.options);
350
+ });
351
+ const changeState = {
352
+ commandId: RichTextEditingMutationId,
353
+ unitId,
354
+ segmentId,
355
+ trigger,
356
+ noHistory,
357
+ debounce,
358
+ redoState: {
359
+ actions,
360
+ textRanges
361
+ },
362
+ undoState: {
363
+ actions: undoActions,
364
+ textRanges: prevTextRanges !== null && prevTextRanges !== void 0 ? prevTextRanges : docRanges
365
+ },
366
+ isCompositionEnd,
367
+ isSync,
368
+ syncer
369
+ };
370
+ docStateEmitService.emitStateChangeInfo(changeState);
371
+ return {
372
+ unitId,
373
+ actions: undoActions,
374
+ textRanges: docRanges
375
+ };
376
+ }
377
+ };
378
+
379
+ //#endregion
380
+ //#region package.json
381
+ var name = "@univerjs/docs";
382
+ var version = "0.21.0";
383
+
384
+ //#endregion
385
+ //#region src/commands/mutations/docs-rename.mutation.ts
386
+ const DocsRenameMutation = {
387
+ id: "doc.mutation.rename-doc",
388
+ type: CommandType.MUTATION,
389
+ handler: (accessor, params) => {
390
+ const doc = accessor.get(IUniverInstanceService).getUnit(params.unitId, UniverInstanceType.UNIVER_DOC);
391
+ if (!doc) return false;
392
+ doc.setName(params.name);
393
+ return true;
394
+ }
395
+ };
396
+
397
+ //#endregion
398
+ //#region src/config/config.ts
399
+ /**
400
+ * Copyright 2023-present DreamNum Co., Ltd.
401
+ *
402
+ * Licensed under the Apache License, Version 2.0 (the "License");
403
+ * you may not use this file except in compliance with the License.
404
+ * You may obtain a copy of the License at
405
+ *
406
+ * http://www.apache.org/licenses/LICENSE-2.0
407
+ *
408
+ * Unless required by applicable law or agreed to in writing, software
409
+ * distributed under the License is distributed on an "AS IS" BASIS,
410
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
411
+ * See the License for the specific language governing permissions and
412
+ * limitations under the License.
413
+ */
414
+ const DOCS_PLUGIN_CONFIG_KEY = "docs.config";
415
+ const configSymbol = Symbol(DOCS_PLUGIN_CONFIG_KEY);
416
+ const defaultPluginConfig = {};
417
+
418
+ //#endregion
419
+ //#region src/controllers/custom-range.controller.ts
420
+ let DocCustomRangeController = class DocCustomRangeController extends Disposable {
421
+ constructor(_commandService, _textSelectionManagerService, _univerInstanceService) {
422
+ super();
423
+ this._commandService = _commandService;
424
+ this._textSelectionManagerService = _textSelectionManagerService;
425
+ this._univerInstanceService = _univerInstanceService;
426
+ this._initSelectionChange();
427
+ }
428
+ _transformCustomRange(doc, selection) {
429
+ var _doc$getCustomRanges;
430
+ const { startOffset, endOffset, collapsed } = selection;
431
+ const customRanges = (_doc$getCustomRanges = doc.getCustomRanges()) === null || _doc$getCustomRanges === void 0 ? void 0 : _doc$getCustomRanges.filter((range) => {
432
+ if (!range.wholeEntity) return false;
433
+ if (startOffset <= range.startIndex && endOffset > range.endIndex) return false;
434
+ if (collapsed) return range.startIndex < startOffset && range.endIndex >= endOffset;
435
+ return BuildTextUtils.range.isIntersects(startOffset, endOffset - 1, range.startIndex, range.endIndex);
436
+ });
437
+ if (customRanges === null || customRanges === void 0 ? void 0 : customRanges.length) {
438
+ let start = startOffset;
439
+ let end = endOffset;
440
+ customRanges.forEach((range) => {
441
+ start = Math.min(range.startIndex, start);
442
+ end = Math.max(range.endIndex + 1, end);
443
+ });
444
+ return {
445
+ ...selection,
446
+ startOffset: start,
447
+ endOffset: end,
448
+ collapsed: start === end
449
+ };
450
+ }
451
+ return selection;
452
+ }
453
+ _initSelectionChange() {
454
+ this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo) => {
455
+ if (commandInfo.id === SetTextSelectionsOperation.id) {
456
+ const { unitId, ranges, isEditing } = commandInfo.params;
457
+ const doc = this._univerInstanceService.getUnit(unitId);
458
+ if (!doc) return;
459
+ const transformedRanges = ranges.map((range) => this._transformCustomRange(doc, range));
460
+ if (transformedRanges.some((range, i) => ranges[i] !== range)) this._textSelectionManagerService.replaceTextRanges(transformedRanges, isEditing);
461
+ }
462
+ }));
463
+ }
464
+ };
465
+ DocCustomRangeController = __decorate([
466
+ __decorateParam(0, ICommandService),
467
+ __decorateParam(1, Inject(DocSelectionManagerService)),
468
+ __decorateParam(2, IUniverInstanceService)
469
+ ], DocCustomRangeController);
470
+
471
+ //#endregion
472
+ //#region src/plugin.ts
473
+ let UniverDocsPlugin = class UniverDocsPlugin extends Plugin {
474
+ constructor(_config = defaultPluginConfig, _injector, _configService) {
475
+ super();
476
+ this._config = _config;
477
+ this._injector = _injector;
478
+ this._configService = _configService;
479
+ const { ...rest } = merge({}, defaultPluginConfig, this._config);
480
+ this._configService.setConfig(DOCS_PLUGIN_CONFIG_KEY, rest);
481
+ }
482
+ onStarting() {
483
+ this._initializeDependencies();
484
+ this._initializeCommands();
485
+ }
486
+ _initializeCommands() {
487
+ [
488
+ RichTextEditingMutation,
489
+ DocsRenameMutation,
490
+ SetTextSelectionsOperation
491
+ ].forEach((command) => {
492
+ this._injector.get(ICommandService).registerCommand(command);
493
+ });
494
+ }
495
+ _initializeDependencies() {
496
+ [
497
+ [DocSelectionManagerService],
498
+ [DocStateEmitService],
499
+ [DocCustomRangeController]
500
+ ].forEach((d) => this._injector.add(d));
501
+ }
502
+ onReady() {
503
+ this._injector.get(DocCustomRangeController);
504
+ }
505
+ };
506
+ _defineProperty(UniverDocsPlugin, "pluginName", "DOCS_PLUGIN");
507
+ _defineProperty(UniverDocsPlugin, "packageName", name);
508
+ _defineProperty(UniverDocsPlugin, "version", version);
509
+ UniverDocsPlugin = __decorate([__decorateParam(1, Inject(Injector)), __decorateParam(2, IConfigService)], UniverDocsPlugin);
510
+
511
+ //#endregion
512
+ //#region src/services/doc-interceptor/interceptor-const.ts
513
+ const CUSTOM_RANGE = createInterceptorKey("CUSTOM_RANGE");
514
+ const CUSTOM_DECORATION = createInterceptorKey("CUSTOM_DECORATION");
515
+ const DOC_INTERCEPTOR_POINT = {
516
+ CUSTOM_RANGE,
517
+ CUSTOM_DECORATION
518
+ };
519
+
520
+ //#endregion
521
+ //#region src/services/doc-interceptor/doc-interceptor.service.ts
522
+ let DocInterceptorService = class DocInterceptorService extends Disposable {
523
+ constructor(_context, _docSkeletonManagerService) {
524
+ super();
525
+ this._context = _context;
526
+ this._docSkeletonManagerService = _docSkeletonManagerService;
527
+ _defineProperty(this, "_interceptorsByName", /* @__PURE__ */ new Map());
528
+ const viewModel = this._docSkeletonManagerService.getViewModel();
529
+ const unitId = viewModel.getDataModel().getUnitId();
530
+ if (unitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY || unitId === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY) return;
531
+ this.disposeWithMe(this.interceptDocumentViewModel(viewModel));
532
+ this.disposeWithMe(this.intercept(DOC_INTERCEPTOR_POINT.CUSTOM_RANGE, {
533
+ priority: -1,
534
+ handler: (data, pos, next) => {
535
+ return next(data);
536
+ }
537
+ }));
538
+ let disposableCollection = new DisposableCollection();
539
+ viewModel.segmentViewModels$.subscribe((segmentViewModels) => {
540
+ disposableCollection.dispose();
541
+ disposableCollection = new DisposableCollection();
542
+ segmentViewModels.forEach((segmentViewModel) => {
543
+ disposableCollection.add(this.interceptDocumentViewModel(segmentViewModel));
544
+ });
545
+ });
546
+ this.disposeWithMe(disposableCollection);
547
+ }
548
+ intercept(name, interceptor) {
549
+ const key = name;
550
+ if (!this._interceptorsByName.has(key)) this._interceptorsByName.set(key, []);
551
+ const interceptors = this._interceptorsByName.get(key);
552
+ interceptors.push(interceptor);
553
+ this._interceptorsByName.set(key, interceptors.sort((a, b) => {
554
+ var _b$priority, _a$priority;
555
+ return ((_b$priority = b.priority) !== null && _b$priority !== void 0 ? _b$priority : 0) - ((_a$priority = a.priority) !== null && _a$priority !== void 0 ? _a$priority : 0);
556
+ }));
557
+ return this.disposeWithMe(toDisposable(() => remove(this._interceptorsByName.get(key), interceptor)));
558
+ }
559
+ fetchThroughInterceptors(name) {
560
+ const key = name;
561
+ return composeInterceptors(this._interceptorsByName.get(key) || []);
562
+ }
563
+ interceptDocumentViewModel(viewModel) {
564
+ const disposableCollection = new DisposableCollection();
565
+ disposableCollection.add(viewModel.registerCustomRangeInterceptor({
566
+ getCustomRange: (index) => {
567
+ var _viewModel$getDataMod;
568
+ return this.fetchThroughInterceptors(DOC_INTERCEPTOR_POINT.CUSTOM_RANGE)(viewModel.getCustomRangeRaw(index), {
569
+ index,
570
+ unitId: viewModel.getDataModel().getUnitId(),
571
+ customRanges: (_viewModel$getDataMod = viewModel.getDataModel().getCustomRanges()) !== null && _viewModel$getDataMod !== void 0 ? _viewModel$getDataMod : []
572
+ });
573
+ },
574
+ getCustomDecoration: (index) => {
575
+ var _viewModel$getDataMod2;
576
+ return this.fetchThroughInterceptors(DOC_INTERCEPTOR_POINT.CUSTOM_DECORATION)(viewModel.getCustomDecorationRaw(index), {
577
+ index,
578
+ unitId: viewModel.getDataModel().getUnitId(),
579
+ customDecorations: (_viewModel$getDataMod2 = viewModel.getDataModel().getCustomDecorations()) !== null && _viewModel$getDataMod2 !== void 0 ? _viewModel$getDataMod2 : []
580
+ });
581
+ }
582
+ }));
583
+ return disposableCollection;
584
+ }
585
+ };
586
+ DocInterceptorService = __decorate([__decorateParam(1, Inject(DocSkeletonManagerService))], DocInterceptorService);
587
+
588
+ //#endregion
589
+ //#region src/utils/custom-range-factory.ts
590
+ /**
591
+ * @deprecated This is a duplication from docs-ui to avoid making too much breaking changes.
592
+ */
593
+ function getRichTextEditPath(docDataModel, segmentId = "") {
594
+ if (!segmentId) return ["body"];
595
+ const { headers, footers } = docDataModel.getSnapshot();
596
+ if (headers == null && footers == null) throw new Error("Document data model must have headers or footers when update by segment id");
597
+ if ((headers === null || headers === void 0 ? void 0 : headers[segmentId]) != null) return [
598
+ "headers",
599
+ segmentId,
600
+ "body"
601
+ ];
602
+ else if ((footers === null || footers === void 0 ? void 0 : footers[segmentId]) != null) return [
603
+ "footers",
604
+ segmentId,
605
+ "body"
606
+ ];
607
+ else throw new Error("Segment id not found in headers or footers");
608
+ }
609
+ function addCustomRangeFactory(accessor, param, body) {
610
+ const { unitId, segmentId } = param;
611
+ const documentDataModel = accessor.get(IUniverInstanceService).getUnit(unitId);
612
+ if (!documentDataModel) return false;
613
+ const doMutation = {
614
+ id: RichTextEditingMutation.id,
615
+ params: {
616
+ unitId: param.unitId,
617
+ actions: [],
618
+ textRanges: void 0
619
+ }
620
+ };
621
+ const jsonX = JSONX.getInstance();
622
+ const textX = BuildTextUtils.customRange.add({
623
+ ...param,
624
+ body
625
+ });
626
+ if (!textX) return false;
627
+ const path = getRichTextEditPath(documentDataModel, segmentId);
628
+ doMutation.params.actions = jsonX.editOp(textX.serialize(), path);
629
+ return doMutation;
630
+ }
631
+ function addCustomRangeBySelectionFactory(accessor, param) {
632
+ var _selections$;
633
+ const { rangeId, rangeType, wholeEntity, properties, unitId, selections: propSelection } = param;
634
+ const docSelectionManagerService = accessor.get(DocSelectionManagerService);
635
+ const univerInstanceService = accessor.get(IUniverInstanceService);
636
+ const selections = propSelection !== null && propSelection !== void 0 ? propSelection : docSelectionManagerService.getTextRanges({
637
+ unitId,
638
+ subUnitId: unitId
639
+ });
640
+ const segmentId = selections === null || selections === void 0 || (_selections$ = selections[0]) === null || _selections$ === void 0 ? void 0 : _selections$.segmentId;
641
+ if (!(selections === null || selections === void 0 ? void 0 : selections.length)) return false;
642
+ const documentDataModel = univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_DOC);
643
+ if (!documentDataModel) return false;
644
+ const body = documentDataModel.getSelfOrHeaderFooterModel(segmentId).getBody();
645
+ if (!body) return false;
646
+ const textX = BuildTextUtils.customRange.add({
647
+ ranges: selections,
648
+ rangeId,
649
+ rangeType,
650
+ segmentId,
651
+ wholeEntity,
652
+ properties,
653
+ body
654
+ });
655
+ if (!textX) return false;
656
+ const jsonX = JSONX.getInstance();
657
+ const doMutation = {
658
+ id: RichTextEditingMutation.id,
659
+ params: {
660
+ unitId,
661
+ actions: [],
662
+ textRanges: textX.selections,
663
+ segmentId
664
+ },
665
+ textX
666
+ };
667
+ const path = getRichTextEditPath(documentDataModel, segmentId);
668
+ doMutation.params.actions = jsonX.editOp(textX.serialize(), path);
669
+ return doMutation;
670
+ }
671
+ function deleteCustomRangeFactory(accessor, params) {
672
+ const { unitId, segmentId, insert } = params;
673
+ const documentDataModel = accessor.get(IUniverInstanceService).getUnit(unitId);
674
+ if (!documentDataModel) return false;
675
+ const doMutation = {
676
+ id: RichTextEditingMutation.id,
677
+ params: {
678
+ unitId: params.unitId,
679
+ actions: [],
680
+ textRanges: void 0,
681
+ segmentId
682
+ }
683
+ };
684
+ const jsonX = JSONX.getInstance();
685
+ const textX = BuildTextUtils.customRange.delete({
686
+ documentDataModel,
687
+ rangeId: params.rangeId,
688
+ insert,
689
+ segmentId
690
+ });
691
+ if (!textX) return false;
692
+ const path = getRichTextEditPath(documentDataModel, segmentId);
693
+ doMutation.params.actions = jsonX.editOp(textX.serialize(), path);
694
+ doMutation.params.textRanges = textX.selections;
695
+ return doMutation;
696
+ }
697
+
698
+ //#endregion
699
+ //#region src/utils/replace-selection-factory.ts
700
+ function replaceSelectionFactory(accessor, params) {
701
+ var _params$selection, _docDataModel$getSelf, _params$selection2, _params$textRanges;
702
+ const { unitId, body: insertBody, doc } = params;
703
+ let docDataModel = doc;
704
+ if (!docDataModel) docDataModel = accessor.get(IUniverInstanceService).getUnit(unitId);
705
+ if (!docDataModel) return false;
706
+ const segmentId = (_params$selection = params.selection) === null || _params$selection === void 0 ? void 0 : _params$selection.segmentId;
707
+ const body = (_docDataModel$getSelf = docDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _docDataModel$getSelf === void 0 ? void 0 : _docDataModel$getSelf.getBody();
708
+ if (!body) return false;
709
+ const docSelectionManagerService = accessor.get(DocSelectionManagerService);
710
+ const selection = (_params$selection2 = params.selection) !== null && _params$selection2 !== void 0 ? _params$selection2 : docSelectionManagerService.getActiveTextRange();
711
+ if (!selection || !body) return false;
712
+ const textRanges = (_params$textRanges = params.textRanges) !== null && _params$textRanges !== void 0 ? _params$textRanges : [{
713
+ startOffset: selection.startOffset + insertBody.dataStream.length,
714
+ endOffset: selection.startOffset + insertBody.dataStream.length,
715
+ collapsed: true,
716
+ segmentId
717
+ }];
718
+ const textX = BuildTextUtils.selection.replace({
719
+ selection,
720
+ body: insertBody,
721
+ doc: docDataModel
722
+ });
723
+ if (!textX) return false;
724
+ const doMutation = {
725
+ id: RichTextEditingMutation.id,
726
+ params: {
727
+ unitId,
728
+ actions: [],
729
+ textRanges,
730
+ debounce: true,
731
+ segmentId
732
+ },
733
+ textX
734
+ };
735
+ const jsonX = JSONX.getInstance();
736
+ doMutation.params.actions = jsonX.editOp(textX.serialize());
737
+ return doMutation;
738
+ }
739
+
740
+ //#endregion
741
+ export { DOC_INTERCEPTOR_POINT, DocInterceptorService, DocSelectionManagerService, DocSkeletonManagerService, DocStateEmitService, RichTextEditingMutation, SetTextSelectionsOperation, UniverDocsPlugin, addCustomRangeBySelectionFactory, addCustomRangeFactory, deleteCustomRangeFactory, replaceSelectionFactory };