@univerjs/docs 0.17.0 → 0.18.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,610 +1 @@
1
- var re = Object.defineProperty;
2
- var oe = (i, e, t) => e in i ? re(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
3
- var l = (i, e, t) => oe(i, typeof e != "symbol" ? e + "" : e, t);
4
- import { CommandType as P, RxDisposable as V, UniverInstanceType as w, ICommandService as B, IUniverInstanceService as I, Inject as b, LocaleService as ce, isInternalEditorID as ae, JSONX as x, Disposable as Z, BuildTextUtils as D, Injector as le, IConfigService as ue, Plugin as de, merge as ge, createInterceptorKey as ee, DOCS_NORMAL_EDITOR_UNIT_ID_KEY as he, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY as _e, DisposableCollection as $, toDisposable as fe, remove as Se, composeInterceptors as Ie } from "@univerjs/core";
5
- import { NORMAL_TEXT_SELECTION_PLUGIN_STYLE as pe, DocumentSkeleton as me, DocumentViewModel as ve, IRenderManagerService as Re } from "@univerjs/engine-render";
6
- import { Subject as xe, BehaviorSubject as C, takeUntil as te } from "rxjs";
7
- const k = {
8
- id: "doc.operation.set-selections",
9
- type: P.OPERATION,
10
- handler: () => !0
11
- };
12
- var Oe = Object.getOwnPropertyDescriptor, Ce = (i, e, t, n) => {
13
- for (var s = n > 1 ? void 0 : n ? Oe(e, t) : e, r = i.length - 1, o; r >= 0; r--)
14
- (o = i[r]) && (s = o(s) || s);
15
- return s;
16
- }, G = (i, e) => (t, n) => e(t, n, i);
17
- let m = class extends V {
18
- constructor(e, t) {
19
- super();
20
- l(this, "_currentSelection", null);
21
- l(this, "_textSelectionInfo", /* @__PURE__ */ new Map());
22
- l(this, "_textSelection$", new xe());
23
- l(this, "textSelection$", this._textSelection$.asObservable());
24
- l(this, "_refreshSelection$", new C(null));
25
- l(this, "refreshSelection$", this._refreshSelection$.asObservable());
26
- this._commandService = e, this._univerInstanceService = t, this._listenCurrentUnit();
27
- }
28
- _listenCurrentUnit() {
29
- this._univerInstanceService.getCurrentTypeOfUnit$(w.UNIVER_DOC).pipe(te(this.dispose$)).subscribe((e) => {
30
- if (e == null)
31
- return;
32
- const t = e.getUnitId();
33
- this._setCurrentSelectionNotRefresh({
34
- unitId: t,
35
- subUnitId: t
36
- });
37
- });
38
- }
39
- __getCurrentSelection() {
40
- return this._currentSelection;
41
- }
42
- getSelectionInfo(e = this._currentSelection) {
43
- return this._getTextRanges(e);
44
- }
45
- refreshSelection(e = this._currentSelection) {
46
- e != null && this._refresh(e);
47
- }
48
- // **Only used in test case** because this does not go through the render layer.
49
- __TEST_ONLY_setCurrentSelection(e) {
50
- this._currentSelection = e, this._refresh(e);
51
- }
52
- getTextRanges(e = this._currentSelection) {
53
- var t;
54
- return (t = this._getTextRanges(e)) == null ? void 0 : t.textRanges;
55
- }
56
- getRectRanges(e = this._currentSelection) {
57
- var t;
58
- return (t = this._getTextRanges(e)) == null ? void 0 : t.rectRanges;
59
- }
60
- getDocRanges(e = this._currentSelection) {
61
- var r, o;
62
- const t = (r = this.getTextRanges(e)) != null ? r : [], n = (o = this.getRectRanges(e)) != null ? o : [];
63
- return [...t, ...n].filter((c) => c.startOffset != null && c.endOffset != null).sort((c, a) => c.startOffset > a.startOffset ? 1 : c.startOffset < a.startOffset ? -1 : 0);
64
- }
65
- getActiveTextRange() {
66
- const e = this._getTextRanges(this._currentSelection);
67
- if (e == null)
68
- return;
69
- const { textRanges: t } = e;
70
- return t.find((n) => n.isActive);
71
- }
72
- /**
73
- *
74
- * @deprecated
75
- */
76
- getActiveRectRange() {
77
- const e = this._getTextRanges(this._currentSelection);
78
- if (e == null)
79
- return;
80
- const { rectRanges: t } = e;
81
- return t.find((n) => n.isActive);
82
- }
83
- // **Only used in test case** because this does not go through the render layer.
84
- __TEST_ONLY_add(e, t = !0) {
85
- this._currentSelection != null && this._addByParam({
86
- ...this._currentSelection,
87
- textRanges: e,
88
- rectRanges: [],
89
- segmentId: "",
90
- segmentPage: -1,
91
- isEditing: t,
92
- style: pe
93
- // mock style.
94
- });
95
- }
96
- // Use to replace the current editor selection.
97
- /**
98
- * @deprecated pls use replaceDocRanges.
99
- */
100
- replaceTextRanges(e, t = !0, n) {
101
- return this.replaceDocRanges(
102
- e,
103
- this._currentSelection,
104
- t,
105
- n
106
- );
107
- }
108
- replaceDocRanges(e, t = this._currentSelection, n = !0, s) {
109
- if (t == null)
110
- return;
111
- const { unitId: r, subUnitId: o } = t;
112
- this._refreshSelection$.next({
113
- unitId: r,
114
- subUnitId: o,
115
- docRanges: e,
116
- isEditing: n,
117
- options: s
118
- });
119
- }
120
- // Only use in doc-selection-render.controller.ts
121
- __replaceTextRangesWithNoRefresh(e, t) {
122
- if (this._currentSelection == null)
123
- return;
124
- const n = {
125
- ...e,
126
- ...t
127
- };
128
- this._replaceByParam(n), this._textSelection$.next(n);
129
- const { unitId: s, subUnitId: r, segmentId: o, style: c, textRanges: a, rectRanges: u, isEditing: d } = n, h = [...a, ...u].filter((g) => g.startOffset != null && g.endOffset != null).sort((g, _) => g.startOffset > _.startOffset ? 1 : g.startOffset < _.startOffset ? -1 : 0);
130
- this._commandService.executeCommand(k.id, {
131
- unitId: s,
132
- subUnitId: r,
133
- segmentId: o,
134
- style: c,
135
- isEditing: d,
136
- ranges: h
137
- });
138
- }
139
- dispose() {
140
- this._textSelection$.complete(), this._refreshSelection$.complete();
141
- }
142
- _setCurrentSelectionNotRefresh(e) {
143
- this._currentSelection = e;
144
- }
145
- _getTextRanges(e) {
146
- var s;
147
- if (e == null)
148
- return;
149
- const { unitId: t, subUnitId: n = "" } = e;
150
- return (s = this._textSelectionInfo.get(t)) == null ? void 0 : s.get(n);
151
- }
152
- _refresh(e) {
153
- const t = this._getTextRanges(e);
154
- if (t == null)
155
- return;
156
- const { textRanges: n, rectRanges: s } = t, r = [...n, ...s], { unitId: o, subUnitId: c } = e;
157
- this._refreshSelection$.next({
158
- unitId: o,
159
- subUnitId: c,
160
- docRanges: r,
161
- isEditing: !1
162
- });
163
- }
164
- _replaceByParam(e) {
165
- const { unitId: t, subUnitId: n, ...s } = e;
166
- this._textSelectionInfo.has(t) || this._textSelectionInfo.set(t, /* @__PURE__ */ new Map()), this._textSelectionInfo.get(t).set(n, { ...s });
167
- }
168
- _addByParam(e) {
169
- const { unitId: t, subUnitId: n, ...s } = e;
170
- this._textSelectionInfo.has(t) || this._textSelectionInfo.set(t, /* @__PURE__ */ new Map());
171
- const r = this._textSelectionInfo.get(t);
172
- r.has(n) ? r.get(n).textRanges.push(...e.textRanges) : r.set(n, { ...s });
173
- }
174
- };
175
- m = Ce([
176
- G(0, B),
177
- G(1, I)
178
- ], m);
179
- var De = Object.getOwnPropertyDescriptor, Me = (i, e, t, n) => {
180
- for (var s = n > 1 ? void 0 : n ? De(e, t) : e, r = i.length - 1, o; r >= 0; r--)
181
- (o = i[r]) && (s = o(s) || s);
182
- return s;
183
- }, W = (i, e) => (t, n) => e(t, n, i);
184
- let T = class extends V {
185
- constructor(e, t, n) {
186
- super();
187
- l(this, "_skeleton");
188
- l(this, "_docViewModel");
189
- l(this, "_currentSkeleton$", new C(null));
190
- l(this, "currentSkeleton$", this._currentSkeleton$.asObservable());
191
- // CurrentSkeletonBefore for pre-triggered logic during registration
192
- l(this, "_currentSkeletonBefore$", new C(null));
193
- l(this, "currentSkeletonBefore$", this._currentSkeletonBefore$.asObservable());
194
- l(this, "_currentViewModel$", new C(null));
195
- l(this, "currentViewModel$", this._currentViewModel$.asObservable());
196
- this._context = e, this._localeService = t, this._univerInstanceService = n, this._init(), this._univerInstanceService.getCurrentTypeOfUnit$(w.UNIVER_DOC).pipe(te(this.dispose$)).subscribe((s) => {
197
- s && s.getUnitId() === this._context.unitId && this._update(s);
198
- });
199
- }
200
- dispose() {
201
- super.dispose(), this._currentSkeletonBefore$.complete(), this._currentSkeleton$.complete();
202
- }
203
- getSkeleton() {
204
- return this._skeleton;
205
- }
206
- getViewModel() {
207
- return this._docViewModel;
208
- }
209
- _init() {
210
- const e = this._context.unit;
211
- this._update(e);
212
- }
213
- _update(e) {
214
- const t = this._context.unitId;
215
- if (e.getBody() == null)
216
- return;
217
- this._docViewModel && ae(t) ? (this._docViewModel.reset(e), this._context.unit = e) : this._docViewModel || (this._docViewModel = this._buildDocViewModel(e)), this._skeleton || (this._skeleton = this._buildSkeleton(this._docViewModel));
218
- const n = this._skeleton;
219
- n.calculate(), this._currentSkeletonBefore$.next(n), this._currentSkeleton$.next(n), this._currentViewModel$.next(this._docViewModel);
220
- }
221
- _buildSkeleton(e) {
222
- return me.create(e, this._localeService);
223
- }
224
- _buildDocViewModel(e) {
225
- return new ve(e);
226
- }
227
- };
228
- T = Me([
229
- W(1, b(ce)),
230
- W(2, I)
231
- ], T);
232
- class ne extends V {
233
- constructor() {
234
- super();
235
- l(this, "_docStateChangeParams$", new C(null));
236
- l(this, "docStateChangeParams$", this._docStateChangeParams$.asObservable());
237
- }
238
- emitStateChangeInfo(t) {
239
- this._docStateChangeParams$.next(t);
240
- }
241
- dispose() {
242
- super.dispose(), this._docStateChangeParams$.complete();
243
- }
244
- }
245
- const Y = "doc.mutation.rich-text-editing", M = {
246
- id: Y,
247
- type: P.MUTATION,
248
- // eslint-disable-next-line max-lines-per-function
249
- handler: (i, e) => {
250
- var z, F;
251
- const {
252
- unitId: t,
253
- segmentId: n = "",
254
- actions: s,
255
- textRanges: r,
256
- prevTextRanges: o,
257
- trigger: c,
258
- noHistory: a,
259
- isCompositionEnd: u,
260
- noNeedSetTextRange: d,
261
- debounce: h,
262
- isEditing: g = !0,
263
- isSync: _,
264
- syncer: f
265
- } = e, v = i.get(I), p = i.get(Re), R = i.get(ne), S = v.getUniverDocInstance(t), j = (z = p.getRenderById(t)) == null ? void 0 : z.with(T).getViewModel();
266
- if (S == null || j == null)
267
- throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${t}`);
268
- const L = i.get(m), U = (F = L.getDocRanges()) != null ? F : [], se = !!S.getSnapshot().disabled;
269
- if (x.isNoop(s) || s && s.length === 0 || se)
270
- return {
271
- unitId: t,
272
- actions: [],
273
- textRanges: U
274
- };
275
- const X = x.invertWithDoc(s, S.getSnapshot());
276
- S.apply(s), j.reset(S), !d && r && c != null && !_ && queueMicrotask(() => {
277
- L.replaceDocRanges(r, { unitId: t, subUnitId: t }, g, e.options);
278
- });
279
- const ie = {
280
- commandId: Y,
281
- unitId: t,
282
- segmentId: n,
283
- trigger: c,
284
- noHistory: a,
285
- debounce: h,
286
- redoState: {
287
- actions: s,
288
- textRanges: r
289
- },
290
- undoState: {
291
- actions: X,
292
- textRanges: o != null ? o : U
293
- },
294
- isCompositionEnd: u,
295
- isSync: _,
296
- syncer: f
297
- };
298
- return R.emitStateChangeInfo(ie), {
299
- unitId: t,
300
- actions: X,
301
- textRanges: U
302
- };
303
- }
304
- }, Te = "@univerjs/docs", ye = "0.17.0", K = {
305
- name: Te,
306
- version: ye
307
- }, we = {
308
- id: "doc.mutation.rename-doc",
309
- type: P.MUTATION,
310
- handler: (i, e) => {
311
- const n = i.get(I).getUnit(e.unitId, w.UNIVER_DOC);
312
- return n ? (n.setName(e.name), !0) : !1;
313
- }
314
- }, be = "docs.config", H = {};
315
- var Ue = Object.getOwnPropertyDescriptor, $e = (i, e, t, n) => {
316
- for (var s = n > 1 ? void 0 : n ? Ue(e, t) : e, r = i.length - 1, o; r >= 0; r--)
317
- (o = i[r]) && (s = o(s) || s);
318
- return s;
319
- }, E = (i, e) => (t, n) => e(t, n, i);
320
- let y = class extends Z {
321
- constructor(i, e, t) {
322
- super(), this._commandService = i, this._textSelectionManagerService = e, this._univerInstanceService = t, this._initSelectionChange();
323
- }
324
- _transformCustomRange(i, e) {
325
- var o;
326
- const { startOffset: t, endOffset: n, collapsed: s } = e, r = (o = i.getCustomRanges()) == null ? void 0 : o.filter((c) => !c.wholeEntity || t <= c.startIndex && n > c.endIndex ? !1 : s ? c.startIndex < t && c.endIndex >= n : D.range.isIntersects(t, n - 1, c.startIndex, c.endIndex));
327
- if (r != null && r.length) {
328
- let c = t, a = n;
329
- return r.forEach((u) => {
330
- c = Math.min(u.startIndex, c), a = Math.max(u.endIndex + 1, a);
331
- }), {
332
- ...e,
333
- startOffset: c,
334
- endOffset: a,
335
- collapsed: c === a
336
- };
337
- }
338
- return e;
339
- }
340
- _initSelectionChange() {
341
- this.disposeWithMe(this._commandService.onCommandExecuted((i) => {
342
- if (i.id === k.id) {
343
- const e = i.params, { unitId: t, ranges: n, isEditing: s } = e, r = this._univerInstanceService.getUnit(t);
344
- if (!r)
345
- return;
346
- const o = n.map((c) => this._transformCustomRange(r, c));
347
- o.some((c, a) => n[a] !== c) && this._textSelectionManagerService.replaceTextRanges(o, s);
348
- }
349
- }));
350
- }
351
- };
352
- y = $e([
353
- E(0, B),
354
- E(1, b(m)),
355
- E(2, I)
356
- ], y);
357
- var Ee = Object.getOwnPropertyDescriptor, Ne = (i, e, t, n) => {
358
- for (var s = n > 1 ? void 0 : n ? Ee(e, t) : e, r = i.length - 1, o; r >= 0; r--)
359
- (o = i[r]) && (s = o(s) || s);
360
- return s;
361
- }, q = (i, e) => (t, n) => e(t, n, i), O;
362
- let J = (O = class extends de {
363
- // static override type = UniverInstanceType.UNIVER_DOC;
364
- constructor(i = H, e, t) {
365
- super(), this._config = i, this._injector = e, this._configService = t;
366
- const { ...n } = ge(
367
- {},
368
- H,
369
- this._config
370
- );
371
- this._configService.setConfig(be, n);
372
- }
373
- onStarting() {
374
- this._initializeDependencies(), this._initializeCommands();
375
- }
376
- _initializeCommands() {
377
- [
378
- M,
379
- we,
380
- k
381
- ].forEach((i) => {
382
- this._injector.get(B).registerCommand(i);
383
- });
384
- }
385
- _initializeDependencies() {
386
- [
387
- [m],
388
- [ne],
389
- [y]
390
- ].forEach((i) => this._injector.add(i));
391
- }
392
- onReady() {
393
- this._injector.get(y);
394
- }
395
- }, l(O, "pluginName", "DOCS_PLUGIN"), l(O, "packageName", K.name), l(O, "version", K.version), O);
396
- J = Ne([
397
- q(1, b(le)),
398
- q(2, ue)
399
- ], J);
400
- const Pe = ee("CUSTOM_RANGE"), Ve = ee("CUSTOM_DECORATION"), N = {
401
- CUSTOM_RANGE: Pe,
402
- CUSTOM_DECORATION: Ve
403
- };
404
- var Be = Object.getOwnPropertyDescriptor, ke = (i, e, t, n) => {
405
- for (var s = n > 1 ? void 0 : n ? Be(e, t) : e, r = i.length - 1, o; r >= 0; r--)
406
- (o = i[r]) && (s = o(s) || s);
407
- return s;
408
- }, Ae = (i, e) => (t, n) => e(t, n, i);
409
- let Q = class extends Z {
410
- constructor(e, t) {
411
- super();
412
- l(this, "_interceptorsByName", /* @__PURE__ */ new Map());
413
- this._context = e, this._docSkeletonManagerService = t;
414
- const n = this._docSkeletonManagerService.getViewModel(), s = n.getDataModel().getUnitId();
415
- if (s === he || s === _e)
416
- return;
417
- this.disposeWithMe(this.interceptDocumentViewModel(n)), this.disposeWithMe(this.intercept(N.CUSTOM_RANGE, {
418
- priority: -1,
419
- handler: (o, c, a) => a(o)
420
- }));
421
- let r = new $();
422
- n.segmentViewModels$.subscribe((o) => {
423
- r.dispose(), r = new $(), o.forEach((c) => {
424
- r.add(this.interceptDocumentViewModel(c));
425
- });
426
- }), this.disposeWithMe(r);
427
- }
428
- intercept(e, t) {
429
- const n = e;
430
- this._interceptorsByName.has(n) || this._interceptorsByName.set(n, []);
431
- const s = this._interceptorsByName.get(n);
432
- return s.push(t), this._interceptorsByName.set(
433
- n,
434
- s.sort((r, o) => {
435
- var c, a;
436
- return ((c = o.priority) != null ? c : 0) - ((a = r.priority) != null ? a : 0);
437
- })
438
- ), this.disposeWithMe(fe(() => Se(this._interceptorsByName.get(n), t)));
439
- }
440
- fetchThroughInterceptors(e) {
441
- const t = e, n = this._interceptorsByName.get(t);
442
- return Ie(n || []);
443
- }
444
- interceptDocumentViewModel(e) {
445
- const t = new $();
446
- return t.add(e.registerCustomRangeInterceptor({
447
- getCustomRange: (n) => {
448
- var s;
449
- return this.fetchThroughInterceptors(N.CUSTOM_RANGE)(
450
- e.getCustomRangeRaw(n),
451
- {
452
- index: n,
453
- unitId: e.getDataModel().getUnitId(),
454
- customRanges: (s = e.getDataModel().getCustomRanges()) != null ? s : []
455
- }
456
- );
457
- },
458
- getCustomDecoration: (n) => {
459
- var s;
460
- return this.fetchThroughInterceptors(N.CUSTOM_DECORATION)(
461
- e.getCustomDecorationRaw(n),
462
- {
463
- index: n,
464
- unitId: e.getDataModel().getUnitId(),
465
- customDecorations: (s = e.getDataModel().getCustomDecorations()) != null ? s : []
466
- }
467
- );
468
- }
469
- })), t;
470
- }
471
- };
472
- Q = ke([
473
- Ae(1, b(T))
474
- ], Q);
475
- function A(i, e = "") {
476
- if (!e)
477
- return ["body"];
478
- const { headers: t, footers: n } = i.getSnapshot();
479
- if (t == null && n == null)
480
- throw new Error("Document data model must have headers or footers when update by segment id");
481
- if ((t == null ? void 0 : t[e]) != null)
482
- return ["headers", e, "body"];
483
- if ((n == null ? void 0 : n[e]) != null)
484
- return ["footers", e, "body"];
485
- throw new Error("Segment id not found in headers or footers");
486
- }
487
- function Fe(i, e, t) {
488
- const { unitId: n, segmentId: s } = e, o = i.get(I).getUnit(n);
489
- if (!o)
490
- return !1;
491
- const c = {
492
- id: M.id,
493
- params: {
494
- unitId: e.unitId,
495
- actions: [],
496
- textRanges: void 0
497
- }
498
- }, a = x.getInstance(), u = D.customRange.add({ ...e, body: t });
499
- if (!u)
500
- return !1;
501
- const d = A(o, s);
502
- return c.params.actions = a.editOp(u.serialize(), d), c;
503
- }
504
- function Ge(i, e) {
505
- var S;
506
- const { rangeId: t, rangeType: n, wholeEntity: s, properties: r, unitId: o, selections: c } = e, a = i.get(m), u = i.get(I), d = c != null ? c : a.getTextRanges({ unitId: o, subUnitId: o }), h = (S = d == null ? void 0 : d[0]) == null ? void 0 : S.segmentId;
507
- if (!(d != null && d.length))
508
- return !1;
509
- const g = u.getUnit(o, w.UNIVER_DOC);
510
- if (!g)
511
- return !1;
512
- const _ = g.getSelfOrHeaderFooterModel(h).getBody();
513
- if (!_)
514
- return !1;
515
- const f = D.customRange.add({
516
- ranges: d,
517
- rangeId: t,
518
- rangeType: n,
519
- segmentId: h,
520
- wholeEntity: s,
521
- properties: r,
522
- body: _
523
- });
524
- if (!f)
525
- return !1;
526
- const v = x.getInstance(), p = {
527
- id: M.id,
528
- params: {
529
- unitId: o,
530
- actions: [],
531
- textRanges: f.selections,
532
- segmentId: h
533
- },
534
- textX: f
535
- }, R = A(g, h);
536
- return p.params.actions = v.editOp(f.serialize(), R), p;
537
- }
538
- function We(i, e) {
539
- const { unitId: t, segmentId: n, insert: s } = e, o = i.get(I).getUnit(t);
540
- if (!o)
541
- return !1;
542
- const c = {
543
- id: M.id,
544
- params: {
545
- unitId: e.unitId,
546
- actions: [],
547
- textRanges: void 0,
548
- segmentId: n
549
- }
550
- }, a = x.getInstance(), u = D.customRange.delete({
551
- documentDataModel: o,
552
- rangeId: e.rangeId,
553
- insert: s,
554
- segmentId: n
555
- });
556
- if (!u)
557
- return !1;
558
- const d = A(o, n);
559
- return c.params.actions = a.editOp(u.serialize(), d), c.params.textRanges = u.selections, c;
560
- }
561
- function Ye(i, e) {
562
- var f, v, p, R;
563
- const { unitId: t, body: n, doc: s } = e;
564
- let r = s;
565
- if (r || (r = i.get(I).getUnit(t)), !r)
566
- return !1;
567
- const o = (f = e.selection) == null ? void 0 : f.segmentId, c = (v = r.getSelfOrHeaderFooterModel(o)) == null ? void 0 : v.getBody();
568
- if (!c) return !1;
569
- const a = i.get(m), u = (p = e.selection) != null ? p : a.getActiveTextRange();
570
- if (!u || !c)
571
- return !1;
572
- const d = (R = e.textRanges) != null ? R : [{
573
- startOffset: u.startOffset + n.dataStream.length,
574
- endOffset: u.startOffset + n.dataStream.length,
575
- collapsed: !0,
576
- segmentId: o
577
- }], h = D.selection.replace({
578
- selection: u,
579
- body: n,
580
- doc: r
581
- });
582
- if (!h)
583
- return !1;
584
- const g = {
585
- id: M.id,
586
- params: {
587
- unitId: t,
588
- actions: [],
589
- textRanges: d,
590
- debounce: !0,
591
- segmentId: o
592
- },
593
- textX: h
594
- }, _ = x.getInstance();
595
- return g.params.actions = _.editOp(h.serialize()), g;
596
- }
597
- export {
598
- N as DOC_INTERCEPTOR_POINT,
599
- Q as DocInterceptorService,
600
- m as DocSelectionManagerService,
601
- T as DocSkeletonManagerService,
602
- ne as DocStateEmitService,
603
- M as RichTextEditingMutation,
604
- k as SetTextSelectionsOperation,
605
- J as UniverDocsPlugin,
606
- Ge as addCustomRangeBySelectionFactory,
607
- Fe as addCustomRangeFactory,
608
- We as deleteCustomRangeFactory,
609
- Ye as replaceSelectionFactory
610
- };
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.18.0`;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,5 +1,20 @@
1
- import { IMutation, IMutationCommonParams, JSONXActions, Nullable } from '@univerjs/core';
2
- import { ITextRangeWithStyle } from '@univerjs/engine-render';
1
+ /**
2
+ * Copyright 2023-present DreamNum Co., Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import type { IMutation, IMutationCommonParams, JSONXActions, Nullable } from '@univerjs/core';
17
+ import type { ITextRangeWithStyle } from '@univerjs/engine-render';
3
18
  export interface IRichTextEditingMutationParams extends IMutationCommonParams {
4
19
  unitId: string;
5
20
  actions: JSONXActions;
@@ -1,4 +1,19 @@
1
- import { ICommand } from '@univerjs/core';
1
+ /**
2
+ * Copyright 2023-present DreamNum Co., Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import type { ICommand } from '@univerjs/core';
2
17
  export interface IDocsRenameMutationParams {
3
18
  name: string;
4
19
  unitId: string;