@univerjs/thread-comment 0.6.7 → 0.6.9

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.
Files changed (2) hide show
  1. package/lib/index.js +538 -0
  2. package/package.json +3 -3
package/lib/index.js ADDED
@@ -0,0 +1,538 @@
1
+ var $ = Object.defineProperty;
2
+ var L = (a, t, e) => t in a ? $(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e;
3
+ var u = (a, t, e) => L(a, typeof t != "symbol" ? t + "" : t, e);
4
+ import { createIdentifier as V, Disposable as U, Inject as g, LifecycleService as j, LifecycleStages as O, IResourceManagerService as H, CommandType as l, ICommandService as f, Plugin as W, merge as J, UniverInstanceType as G, mergeOverrideWithDependencies as Y, Injector as k, IConfigService as K, dayjs as B } from "@univerjs/core";
5
+ import { Subject as b } from "rxjs";
6
+ class Z extends U {
7
+ constructor() {
8
+ super();
9
+ u(this, "_dataSource", null);
10
+ u(this, "syncUpdateMutationToColla", !0);
11
+ }
12
+ set dataSource(e) {
13
+ this._dataSource = e;
14
+ }
15
+ get dataSource() {
16
+ return this._dataSource;
17
+ }
18
+ async getThreadComment(e, n, o) {
19
+ return this._dataSource ? (await this._dataSource.listComments(e, n, [o]))[0] : null;
20
+ }
21
+ async addComment(e) {
22
+ var n;
23
+ return this._dataSource ? this._dataSource.addComment(e) : { ...e, threadId: (n = e.threadId) != null ? n : e.id };
24
+ }
25
+ async updateComment(e) {
26
+ return this._dataSource ? this._dataSource.updateComment(e) : !0;
27
+ }
28
+ async resolveComment(e) {
29
+ return this._dataSource ? this._dataSource.resolveComment(e) : !0;
30
+ }
31
+ async deleteComment(e, n, o, r) {
32
+ return this._dataSource ? this._dataSource.deleteComment(e, n, o, r) : !0;
33
+ }
34
+ async listThreadComments(e, n, o) {
35
+ return this.dataSource ? this.dataSource.listComments(e, n, o) : !1;
36
+ }
37
+ saveToSnapshot(e, n) {
38
+ if (this._dataSource) {
39
+ const o = {};
40
+ return Object.keys(e).forEach((r) => {
41
+ const s = e[r];
42
+ o[r] = s.map(this.dataSource.saveCommentToSnapshot);
43
+ }), o;
44
+ }
45
+ return e;
46
+ }
47
+ }
48
+ const p = V("univer.thread-comment.data-source-service");
49
+ var F = Object.getOwnPropertyDescriptor, q = (a, t, e, n) => {
50
+ for (var o = n > 1 ? void 0 : n ? F(t, e) : t, r = a.length - 1, s; r >= 0; r--)
51
+ (s = a[r]) && (o = s(o) || o);
52
+ return o;
53
+ }, I = (a, t) => (e, n) => t(e, n, a);
54
+ let i = class extends U {
55
+ constructor(t, e) {
56
+ super();
57
+ u(this, "_commentsMap", /* @__PURE__ */ new Map());
58
+ u(this, "_threadMap", /* @__PURE__ */ new Map());
59
+ u(this, "_commentUpdate$", new b());
60
+ u(this, "commentUpdate$", this._commentUpdate$.asObservable());
61
+ u(this, "_tasks", []);
62
+ this._dataSourceService = t, this._lifecycleService = e, this.disposeWithMe(() => {
63
+ this._commentUpdate$.complete();
64
+ }), this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe((n) => {
65
+ const o = /* @__PURE__ */ new Map();
66
+ n === O.Rendered && (this._tasks.forEach(({ unitId: r, subUnitId: s, threadIds: d }) => {
67
+ let m = o.get(r);
68
+ m || (m = /* @__PURE__ */ new Map(), o.set(r, m));
69
+ let c = m.get(s);
70
+ c || (c = /* @__PURE__ */ new Set(), m.set(s, c));
71
+ for (const h of d)
72
+ c.add(h);
73
+ }), this._tasks = [], o.forEach((r, s) => {
74
+ r.forEach((d, m) => {
75
+ this.syncThreadComments(s, m, Array.from(d));
76
+ });
77
+ }));
78
+ }));
79
+ }
80
+ _ensureCommentMap(t, e) {
81
+ let n = this._commentsMap.get(t);
82
+ n || (n = /* @__PURE__ */ new Map(), this._commentsMap.set(t, n));
83
+ let o = n.get(e);
84
+ return o || (o = /* @__PURE__ */ new Map(), n.set(e, o)), o;
85
+ }
86
+ ensureMap(t, e) {
87
+ return this._ensureCommentMap(t, e);
88
+ }
89
+ _ensureThreadMap(t, e) {
90
+ let n = this._threadMap.get(t);
91
+ n || (n = /* @__PURE__ */ new Map(), this._threadMap.set(t, n));
92
+ let o = n.get(e);
93
+ return o || (o = /* @__PURE__ */ new Map(), n.set(e, o)), o;
94
+ }
95
+ _replaceComment(t, e, n) {
96
+ const o = this._ensureCommentMap(t, e), r = o.get(n.id);
97
+ if (r) {
98
+ const { children: s, ...d } = n, m = {
99
+ ...d,
100
+ ref: r.ref
101
+ };
102
+ o.set(n.id, m), s == null || s.forEach((c) => {
103
+ o.set(c.id, {
104
+ ...c,
105
+ ref: ""
106
+ });
107
+ }), this._commentUpdate$.next({
108
+ unitId: t,
109
+ subUnitId: e,
110
+ type: "syncUpdate",
111
+ payload: m
112
+ }), !!n.resolved != !!r.resolved && this._commentUpdate$.next({
113
+ unitId: t,
114
+ subUnitId: e,
115
+ type: "resolve",
116
+ payload: {
117
+ commentId: n.id,
118
+ resolved: !!n.resolved
119
+ }
120
+ });
121
+ }
122
+ }
123
+ async syncThreadComments(t, e, n) {
124
+ if (this._lifecycleService.stage < O.Rendered) {
125
+ this._tasks.push({ unitId: t, subUnitId: e, threadIds: n });
126
+ return;
127
+ }
128
+ const o = this._ensureThreadMap(t, e), r = this._ensureCommentMap(t, e), s = await this._dataSourceService.listThreadComments(t, e, n);
129
+ if (!s)
130
+ return;
131
+ const d = new Set(n);
132
+ s.forEach((m) => {
133
+ this._replaceComment(t, e, m), d.delete(m.threadId);
134
+ }), d.forEach((m) => {
135
+ o.delete(m), r.forEach((c, h) => {
136
+ c.threadId === m && r.delete(h);
137
+ });
138
+ });
139
+ }
140
+ addComment(t, e, n, o) {
141
+ const r = this._ensureCommentMap(t, e), { parentId: s, children: d = [], ...m } = n, c = {
142
+ ...m,
143
+ parentId: s === n.id ? void 0 : s
144
+ };
145
+ c.threadId || (c.threadId = c.parentId || c.id);
146
+ const h = (C) => {
147
+ r.set(C.id, C), this._commentUpdate$.next({
148
+ unitId: t,
149
+ subUnitId: e,
150
+ type: "add",
151
+ payload: C,
152
+ isRoot: !C.parentId
153
+ });
154
+ };
155
+ h(c);
156
+ const _ = this._ensureThreadMap(t, e);
157
+ if (!c.parentId) {
158
+ _.set(c.threadId, c);
159
+ for (const C of d)
160
+ h(C);
161
+ }
162
+ return o && this.syncThreadComments(t, e, [c.threadId]), !0;
163
+ }
164
+ updateComment(t, e, n, o) {
165
+ const s = this._ensureCommentMap(t, e).get(n.commentId);
166
+ return s && (s.updated = !0, s.text = n.text, s.attachments = n.attachments, s.updateT = n.updateT, this._commentUpdate$.next({
167
+ unitId: t,
168
+ subUnitId: e,
169
+ type: "update",
170
+ payload: n,
171
+ silent: o
172
+ })), !0;
173
+ }
174
+ updateCommentRef(t, e, n, o) {
175
+ const s = this._ensureCommentMap(t, e).get(n.commentId);
176
+ return s ? (s.ref = n.ref, this._commentUpdate$.next({
177
+ unitId: t,
178
+ subUnitId: e,
179
+ type: "updateRef",
180
+ payload: n,
181
+ silent: o,
182
+ threadId: s.threadId
183
+ }), !0) : !1;
184
+ }
185
+ resolveComment(t, e, n, o) {
186
+ const s = this._ensureCommentMap(t, e).get(n);
187
+ return s ? (s.resolved = o, this._commentUpdate$.next({
188
+ unitId: t,
189
+ subUnitId: e,
190
+ type: "resolve",
191
+ payload: {
192
+ commentId: n,
193
+ resolved: o
194
+ }
195
+ }), !0) : !1;
196
+ }
197
+ getComment(t, e, n) {
198
+ return this._ensureCommentMap(t, e).get(n);
199
+ }
200
+ getRootComment(t, e, n) {
201
+ return this._ensureThreadMap(t, e).get(n);
202
+ }
203
+ getThread(t, e, n) {
204
+ const o = this._ensureCommentMap(t, e), r = Array.from(o.values()).filter((c) => c.threadId === n);
205
+ let s;
206
+ const d = [], m = /* @__PURE__ */ new Set();
207
+ for (const c of r)
208
+ c.parentId ? d.push(c) : s = c, m.add(c.personId);
209
+ if (s)
210
+ return {
211
+ root: s,
212
+ children: d,
213
+ relativeUsers: m,
214
+ unitId: t,
215
+ subUnitId: e,
216
+ threadId: n
217
+ };
218
+ }
219
+ getCommentWithChildren(t, e, n) {
220
+ const o = this.getComment(t, e, n);
221
+ if (o)
222
+ return this.getThread(t, e, o.threadId);
223
+ }
224
+ _deleteComment(t, e, n) {
225
+ const o = this._ensureCommentMap(t, e), r = o.get(n);
226
+ r && (o.delete(n), this._commentUpdate$.next({
227
+ unitId: t,
228
+ subUnitId: e,
229
+ type: "delete",
230
+ payload: {
231
+ commentId: n,
232
+ isRoot: !r.parentId,
233
+ comment: r
234
+ }
235
+ }));
236
+ }
237
+ deleteThread(t, e, n) {
238
+ this._ensureThreadMap(t, e).delete(n), this._ensureCommentMap(t, e).forEach((s) => {
239
+ s.threadId === n && this._deleteComment(t, e, s.id);
240
+ });
241
+ }
242
+ deleteComment(t, e, n) {
243
+ const r = this._ensureCommentMap(t, e).get(n);
244
+ return r && (r.parentId ? this._deleteComment(t, e, n) : this.deleteThread(t, e, r.threadId)), !0;
245
+ }
246
+ deleteUnit(t) {
247
+ const e = this._commentsMap.get(t);
248
+ e && e.forEach((n, o) => {
249
+ n.forEach((r) => {
250
+ this.deleteComment(t, o, r.id);
251
+ });
252
+ });
253
+ }
254
+ getUnit(t) {
255
+ const e = this._threadMap.get(t);
256
+ if (!e)
257
+ return [];
258
+ const n = [];
259
+ return e.forEach((o, r) => {
260
+ o.forEach((s, d) => {
261
+ const m = this.getThread(t, r, d);
262
+ m && n.push(m);
263
+ });
264
+ }), n;
265
+ }
266
+ getAll() {
267
+ const t = [];
268
+ return this._commentsMap.forEach((e, n) => {
269
+ t.push({
270
+ unitId: n,
271
+ threads: this.getUnit(n)
272
+ });
273
+ }), t;
274
+ }
275
+ };
276
+ i = q([
277
+ I(0, g(p)),
278
+ I(1, g(j))
279
+ ], i);
280
+ var T = /* @__PURE__ */ ((a) => (a[a.UNIVER_UNKNOWN = 0] = "UNIVER_UNKNOWN", a[a.UNIVER_DOC = 1] = "UNIVER_DOC", a[a.UNIVER_SHEET = 2] = "UNIVER_SHEET", a[a.UNIVER_SLIDE = 3] = "UNIVER_SLIDE", a[a.UNIVER_PROJECT = 4] = "UNIVER_PROJECT", a[a.UNRECOGNIZED = -1] = "UNRECOGNIZED", a))(T || {});
281
+ const w = "UNIVER_THREAD_COMMENT_PLUGIN";
282
+ var z = Object.getOwnPropertyDescriptor, Q = (a, t, e, n) => {
283
+ for (var o = n > 1 ? void 0 : n ? z(t, e) : t, r = a.length - 1, s; r >= 0; r--)
284
+ (s = a[r]) && (o = s(o) || o);
285
+ return o;
286
+ }, v = (a, t) => (e, n) => t(e, n, a);
287
+ const X = `SHEET_${w}`;
288
+ let S = class extends U {
289
+ constructor(a, t, e) {
290
+ super(), this._resourceManagerService = a, this._threadCommentModel = t, this._threadCommentDataSourceService = e, this._initSnapshot();
291
+ }
292
+ _initSnapshot() {
293
+ const a = (e) => {
294
+ const n = this._threadCommentModel.getUnit(e), o = {};
295
+ return n ? (n.forEach((r) => {
296
+ var d;
297
+ const s = (d = o[r.subUnitId]) != null ? d : [];
298
+ s.push({
299
+ ...r.root,
300
+ children: r.children
301
+ }), o[r.subUnitId] = s;
302
+ }), JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(o, e))) : "";
303
+ }, t = (e) => {
304
+ if (!e)
305
+ return {};
306
+ try {
307
+ return JSON.parse(e);
308
+ } catch {
309
+ return {};
310
+ }
311
+ };
312
+ this.disposeWithMe(
313
+ this._resourceManagerService.registerPluginResource({
314
+ pluginName: X,
315
+ businesses: [T.UNIVER_SHEET, T.UNIVER_DOC],
316
+ toJson: (e) => a(e),
317
+ parseJson: (e) => t(e),
318
+ onUnLoad: (e) => {
319
+ this._threadCommentModel.deleteUnit(e);
320
+ },
321
+ onLoad: async (e, n) => {
322
+ Object.keys(n).forEach((o) => {
323
+ const r = n[o];
324
+ r.forEach((s) => {
325
+ this._threadCommentModel.addComment(e, o, s);
326
+ }), this._threadCommentModel.syncThreadComments(e, o, r.map((s) => s.threadId));
327
+ });
328
+ }
329
+ })
330
+ );
331
+ }
332
+ };
333
+ S = Q([
334
+ v(0, H),
335
+ v(1, g(i)),
336
+ v(2, p)
337
+ ], S);
338
+ const x = {
339
+ id: "thread-comment.mutation.add-comment",
340
+ type: l.MUTATION,
341
+ handler(a, t, e) {
342
+ if (!t)
343
+ return !1;
344
+ const n = a.get(i), { unitId: o, subUnitId: r, comment: s, sync: d } = t, m = d || (e == null ? void 0 : e.fromChangeset) && !s.parentId;
345
+ return n.addComment(o, r, s, m);
346
+ }
347
+ }, A = {
348
+ id: "thread-comment.mutation.update-comment",
349
+ type: l.MUTATION,
350
+ handler(a, t) {
351
+ if (!t)
352
+ return !1;
353
+ const e = a.get(i), { unitId: n, subUnitId: o, payload: r, silent: s } = t;
354
+ return e.updateComment(n, o, r, s);
355
+ }
356
+ }, ee = {
357
+ id: "thread-comment.mutation.update-comment-ref",
358
+ type: l.MUTATION,
359
+ handler(a, t) {
360
+ if (!t)
361
+ return !1;
362
+ const e = a.get(i), { unitId: n, subUnitId: o, payload: r, silent: s } = t;
363
+ return e.updateCommentRef(n, o, r, s);
364
+ }
365
+ }, P = {
366
+ id: "thread-comment.mutation.resolve-comment",
367
+ type: l.MUTATION,
368
+ handler(a, t) {
369
+ if (!t)
370
+ return !1;
371
+ const e = a.get(i), { unitId: n, subUnitId: o, resolved: r, commentId: s } = t;
372
+ return e.resolveComment(n, o, s, r);
373
+ }
374
+ }, N = {
375
+ id: "thread-comment.mutation.delete-comment",
376
+ type: l.MUTATION,
377
+ handler(a, t) {
378
+ if (!t)
379
+ return !1;
380
+ const e = a.get(i), { unitId: n, subUnitId: o, commentId: r } = t;
381
+ return e.deleteComment(n, o, r);
382
+ }
383
+ }, te = {
384
+ id: "thread-comment.command.add-comment",
385
+ type: l.COMMAND,
386
+ async handler(a, t) {
387
+ if (!t)
388
+ return !1;
389
+ const e = a.get(f), n = a.get(p), { comment: o } = t, r = await n.addComment(o), s = n.syncUpdateMutationToColla, d = !o.parentId, m = {
390
+ id: x.id,
391
+ params: {
392
+ ...t,
393
+ comment: r
394
+ }
395
+ };
396
+ return d ? await e.executeCommand(m.id, m.params) : e.executeCommand(m.id, m.params, {
397
+ onlyLocal: !s
398
+ });
399
+ }
400
+ }, ne = {
401
+ id: "thread-comment.command.update-comment",
402
+ type: l.COMMAND,
403
+ async handler(a, t) {
404
+ if (!t)
405
+ return !1;
406
+ const { unitId: e, subUnitId: n, payload: o } = t, r = a.get(f), s = a.get(i), d = a.get(p), m = d.syncUpdateMutationToColla, c = s.getComment(
407
+ e,
408
+ n,
409
+ o.commentId
410
+ );
411
+ if (!c)
412
+ return !1;
413
+ const { children: h, ..._ } = c;
414
+ if (!await d.updateComment({
415
+ ..._,
416
+ ...o
417
+ }))
418
+ return !1;
419
+ const E = {
420
+ id: A.id,
421
+ params: t
422
+ };
423
+ return r.executeCommand(E.id, E.params, { onlyLocal: !m }), !0;
424
+ }
425
+ }, oe = {
426
+ id: "thread-comment.command.resolve-comment",
427
+ type: l.COMMAND,
428
+ async handler(a, t) {
429
+ if (!t)
430
+ return !1;
431
+ const { unitId: e, subUnitId: n, resolved: o, commentId: r } = t, s = a.get(p), m = a.get(i).getComment(e, n, r), c = s.syncUpdateMutationToColla;
432
+ return !m || !await s.resolveComment({
433
+ ...m,
434
+ resolved: o
435
+ }) ? !1 : a.get(f).executeCommand(
436
+ P.id,
437
+ t,
438
+ { onlyLocal: !c }
439
+ );
440
+ }
441
+ }, re = {
442
+ id: "thread-comment.command.delete-comment",
443
+ type: l.COMMAND,
444
+ async handler(a, t) {
445
+ if (!t)
446
+ return !1;
447
+ const e = a.get(i), n = a.get(p), o = a.get(f), { unitId: r, subUnitId: s, commentId: d } = t, m = n.syncUpdateMutationToColla, c = e.getComment(r, s, d);
448
+ if (!c || !await n.deleteComment(r, s, c.threadId, d))
449
+ return !1;
450
+ const h = {
451
+ id: N.id,
452
+ params: t
453
+ };
454
+ return o.executeCommand(h.id, h.params, { onlyLocal: !m });
455
+ }
456
+ }, ae = {
457
+ id: "thread-comment.command.delete-comment-tree",
458
+ type: l.COMMAND,
459
+ async handler(a, t) {
460
+ if (!t)
461
+ return !1;
462
+ const e = a.get(i), n = a.get(f), o = a.get(p), { unitId: r, subUnitId: s, commentId: d } = t, m = e.getCommentWithChildren(r, s, d);
463
+ return !m || !await o.deleteComment(r, s, m.root.threadId, d) ? !1 : await n.executeCommand(N.id, {
464
+ unitId: r,
465
+ subUnitId: s,
466
+ commentId: m.root.id
467
+ });
468
+ }
469
+ }, se = "thread-comment.config", R = {};
470
+ var me = Object.getOwnPropertyDescriptor, ce = (a, t, e, n) => {
471
+ for (var o = n > 1 ? void 0 : n ? me(t, e) : t, r = a.length - 1, s; r >= 0; r--)
472
+ (s = a[r]) && (o = s(o) || o);
473
+ return o;
474
+ }, y = (a, t) => (e, n) => t(e, n, a), M;
475
+ let D = (M = class extends W {
476
+ constructor(a = R, t, e, n) {
477
+ super(), this._config = a, this._injector = t, this._commandService = e, this._configService = n;
478
+ const { ...o } = J(
479
+ {},
480
+ R,
481
+ this._config
482
+ );
483
+ this._configService.setConfig(se, o);
484
+ }
485
+ onStarting() {
486
+ var a;
487
+ Y([
488
+ [p, { useClass: Z }],
489
+ [i],
490
+ [S]
491
+ ], (a = this._config) == null ? void 0 : a.overrides).forEach(
492
+ (t) => {
493
+ this._injector.add(t);
494
+ }
495
+ ), [
496
+ te,
497
+ ne,
498
+ re,
499
+ oe,
500
+ ae,
501
+ x,
502
+ A,
503
+ ee,
504
+ N,
505
+ P
506
+ ].forEach((t) => {
507
+ this._commandService.registerCommand(t);
508
+ }), this._injector.get(S);
509
+ }
510
+ }, u(M, "pluginName", w), u(M, "type", G.UNIVER_UNKNOWN), M);
511
+ D = ce([
512
+ y(1, g(k)),
513
+ y(2, f),
514
+ y(3, K)
515
+ ], D);
516
+ function ue(a) {
517
+ return B(a).format("YYYY/MM/DD HH:mm");
518
+ }
519
+ export {
520
+ te as AddCommentCommand,
521
+ x as AddCommentMutation,
522
+ re as DeleteCommentCommand,
523
+ N as DeleteCommentMutation,
524
+ ae as DeleteCommentTreeCommand,
525
+ p as IThreadCommentDataSourceService,
526
+ oe as ResolveCommentCommand,
527
+ P as ResolveCommentMutation,
528
+ X as SHEET_UNIVER_THREAD_COMMENT_PLUGIN,
529
+ w as TC_PLUGIN_NAME,
530
+ Z as ThreadCommentDataSourceService,
531
+ i as ThreadCommentModel,
532
+ S as ThreadCommentResourceController,
533
+ D as UniverThreadCommentPlugin,
534
+ ne as UpdateCommentCommand,
535
+ A as UpdateCommentMutation,
536
+ ee as UpdateCommentRefMutation,
537
+ ue as getDT
538
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/thread-comment",
3
- "version": "0.6.7",
3
+ "version": "0.6.9",
4
4
  "private": false,
5
5
  "description": "Univer thread comment core plugin",
6
6
  "author": "DreamNum <developer@univer.ai>",
@@ -47,14 +47,14 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "@univerjs/protocol": "0.1.45",
50
- "@univerjs/core": "0.6.7"
50
+ "@univerjs/core": "0.6.9"
51
51
  },
52
52
  "devDependencies": {
53
53
  "rxjs": "^7.8.1",
54
54
  "typescript": "^5.8.2",
55
55
  "vite": "^6.2.3",
56
56
  "vitest": "^3.0.9",
57
- "@univerjs-infra/shared": "0.6.7"
57
+ "@univerjs-infra/shared": "0.6.9"
58
58
  },
59
59
  "scripts": {
60
60
  "test": "vitest run",