@univerjs/thread-comment 1.0.0-rc.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cjs/facade.js CHANGED
@@ -20,7 +20,7 @@ var __copyProps = (to, from, except, desc) => {
20
20
  }
21
21
  return to;
22
22
  };
23
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
24
24
  value: mod,
25
25
  enumerable: true
26
26
  }) : target, mod));
package/lib/cjs/index.js CHANGED
@@ -2,7 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  let _univerjs_core = require("@univerjs/core");
3
3
  let rxjs = require("rxjs");
4
4
 
5
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/typeof.js
5
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/typeof.js
6
6
  function _typeof(o) {
7
7
  "@babel/helpers - typeof";
8
8
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -13,7 +13,7 @@ function _typeof(o) {
13
13
  }
14
14
 
15
15
  //#endregion
16
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/toPrimitive.js
16
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPrimitive.js
17
17
  function toPrimitive(t, r) {
18
18
  if ("object" != _typeof(t) || !t) return t;
19
19
  var e = t[Symbol.toPrimitive];
@@ -26,14 +26,14 @@ function toPrimitive(t, r) {
26
26
  }
27
27
 
28
28
  //#endregion
29
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/toPropertyKey.js
29
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPropertyKey.js
30
30
  function toPropertyKey(t) {
31
31
  var i = toPrimitive(t, "string");
32
32
  return "symbol" == _typeof(i) ? i : i + "";
33
33
  }
34
34
 
35
35
  //#endregion
36
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/defineProperty.js
36
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/defineProperty.js
37
37
  function _defineProperty(e, r, t) {
38
38
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
39
39
  value: t,
@@ -198,7 +198,7 @@ function deserializeThreadCommentAnchor(ref) {
198
198
  }
199
199
 
200
200
  //#endregion
201
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorateParam.js
201
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorateParam.js
202
202
  function __decorateParam(paramIndex, decorator) {
203
203
  return function(target, key) {
204
204
  decorator(target, key, paramIndex);
@@ -206,7 +206,7 @@ function __decorateParam(paramIndex, decorator) {
206
206
  }
207
207
 
208
208
  //#endregion
209
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorate.js
209
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorate.js
210
210
  function __decorate(decorators, target, key, desc) {
211
211
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
212
212
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -312,6 +312,7 @@ let ThreadCommentModel = class ThreadCommentModel extends _univerjs_core.Disposa
312
312
  });
313
313
  const newComment = {
314
314
  ...rest,
315
+ authorName: rest.authorName ?? (rest.personId === currentComment.personId ? currentComment.authorName : void 0),
315
316
  ref: currentComment.ref
316
317
  };
317
318
  commentMap.set(comment.id, newComment);
@@ -321,8 +322,10 @@ let ThreadCommentModel = class ThreadCommentModel extends _univerjs_core.Disposa
321
322
  if (localComment.threadId === comment.threadId && !remoteCommentIds.has(commentId)) this._deleteComment(unitId, subUnitId, commentId);
322
323
  });
323
324
  safeChildren.forEach((child) => {
325
+ var _commentMap$get, _commentMap$get2;
324
326
  commentMap.set(child.id, {
325
327
  ...child,
328
+ authorName: child.authorName ?? (child.personId === ((_commentMap$get = commentMap.get(child.id)) === null || _commentMap$get === void 0 ? void 0 : _commentMap$get.personId) ? (_commentMap$get2 = commentMap.get(child.id)) === null || _commentMap$get2 === void 0 ? void 0 : _commentMap$get2.authorName : void 0),
326
329
  parentId: comment.id,
327
330
  ref: ""
328
331
  });
@@ -920,6 +923,7 @@ let ThreadCommentFacadeService = class ThreadCommentFacadeService {
920
923
  text: normalizeThreadCommentContent(options.content),
921
924
  attachments: options.attachments ?? [],
922
925
  dT: getDT(options.dateTime),
926
+ authorName: options.authorName,
923
927
  personId
924
928
  }
925
929
  });
@@ -940,6 +944,7 @@ let ThreadCommentFacadeService = class ThreadCommentFacadeService {
940
944
  text: normalizeThreadCommentContent(options.content),
941
945
  attachments: options.attachments ?? [],
942
946
  dT: getDT(options.dateTime),
947
+ authorName: options.authorName,
943
948
  personId: options.personId ?? this._userManagerService.getCurrentUser().userID
944
949
  }
945
950
  });
@@ -1008,7 +1013,7 @@ function isStringArray(value) {
1008
1013
  function isSnapshotComment(value, allowChildren = true) {
1009
1014
  if (!value || typeof value !== "object") return false;
1010
1015
  const comment = value;
1011
- return typeof comment.id === "string" && comment.id.length > 0 && typeof comment.threadId === "string" && comment.threadId.length > 0 && typeof comment.ref === "string" && (comment.text === void 0 || isThreadCommentDocumentBody(comment.text)) && (comment.attachments === void 0 || isStringArray(comment.attachments)) && (comment.mentions === void 0 || isStringArray(comment.mentions)) && (!allowChildren || comment.children === void 0 || Array.isArray(comment.children) && comment.children.every((child) => isSnapshotComment(child, false)));
1016
+ return typeof comment.id === "string" && comment.id.length > 0 && typeof comment.threadId === "string" && comment.threadId.length > 0 && (typeof comment.ref === "string" || !allowChildren && comment.ref === void 0) && (comment.authorName === void 0 || typeof comment.authorName === "string") && (comment.text === void 0 || isThreadCommentDocumentBody(comment.text)) && (comment.attachments === void 0 || isStringArray(comment.attachments)) && (comment.mentions === void 0 || isStringArray(comment.mentions)) && (!allowChildren || comment.children === void 0 || Array.isArray(comment.children) && comment.children.every((child) => isSnapshotComment(child, false)));
1012
1017
  }
1013
1018
  function createThreadCommentResourceHook(threadCommentModel, threadCommentDataSourceService, pluginName, businesses) {
1014
1019
  return {
@@ -1071,7 +1076,7 @@ function createThreadCommentResourceHook(threadCommentModel, threadCommentDataSo
1071
1076
  //#endregion
1072
1077
  //#region package.json
1073
1078
  var name = "@univerjs/thread-comment";
1074
- var version = "1.0.0-rc.0";
1079
+ var version = "1.0.1";
1075
1080
 
1076
1081
  //#endregion
1077
1082
  //#region src/config/config.ts
package/lib/es/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { CommandType, Disposable, ICommandService, IConfigService, IUniverInstanceService, Inject, Injector, LifecycleService, LifecycleStages, Plugin, UniverInstanceType, UserManagerService, createIdentifier, dateKit, generateRandomId, merge, mergeOverrideWithDependencies } from "@univerjs/core";
2
2
  import { Subject } from "rxjs";
3
3
 
4
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/typeof.js
4
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/typeof.js
5
5
  function _typeof(o) {
6
6
  "@babel/helpers - typeof";
7
7
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -12,7 +12,7 @@ function _typeof(o) {
12
12
  }
13
13
 
14
14
  //#endregion
15
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/toPrimitive.js
15
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPrimitive.js
16
16
  function toPrimitive(t, r) {
17
17
  if ("object" != _typeof(t) || !t) return t;
18
18
  var e = t[Symbol.toPrimitive];
@@ -25,14 +25,14 @@ function toPrimitive(t, r) {
25
25
  }
26
26
 
27
27
  //#endregion
28
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/toPropertyKey.js
28
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPropertyKey.js
29
29
  function toPropertyKey(t) {
30
30
  var i = toPrimitive(t, "string");
31
31
  return "symbol" == _typeof(i) ? i : i + "";
32
32
  }
33
33
 
34
34
  //#endregion
35
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/defineProperty.js
35
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/defineProperty.js
36
36
  function _defineProperty(e, r, t) {
37
37
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
38
38
  value: t,
@@ -197,7 +197,7 @@ function deserializeThreadCommentAnchor(ref) {
197
197
  }
198
198
 
199
199
  //#endregion
200
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorateParam.js
200
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorateParam.js
201
201
  function __decorateParam(paramIndex, decorator) {
202
202
  return function(target, key) {
203
203
  decorator(target, key, paramIndex);
@@ -205,7 +205,7 @@ function __decorateParam(paramIndex, decorator) {
205
205
  }
206
206
 
207
207
  //#endregion
208
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorate.js
208
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorate.js
209
209
  function __decorate(decorators, target, key, desc) {
210
210
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
211
211
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -311,6 +311,7 @@ let ThreadCommentModel = class ThreadCommentModel extends Disposable {
311
311
  });
312
312
  const newComment = {
313
313
  ...rest,
314
+ authorName: rest.authorName ?? (rest.personId === currentComment.personId ? currentComment.authorName : void 0),
314
315
  ref: currentComment.ref
315
316
  };
316
317
  commentMap.set(comment.id, newComment);
@@ -320,8 +321,10 @@ let ThreadCommentModel = class ThreadCommentModel extends Disposable {
320
321
  if (localComment.threadId === comment.threadId && !remoteCommentIds.has(commentId)) this._deleteComment(unitId, subUnitId, commentId);
321
322
  });
322
323
  safeChildren.forEach((child) => {
324
+ var _commentMap$get, _commentMap$get2;
323
325
  commentMap.set(child.id, {
324
326
  ...child,
327
+ authorName: child.authorName ?? (child.personId === ((_commentMap$get = commentMap.get(child.id)) === null || _commentMap$get === void 0 ? void 0 : _commentMap$get.personId) ? (_commentMap$get2 = commentMap.get(child.id)) === null || _commentMap$get2 === void 0 ? void 0 : _commentMap$get2.authorName : void 0),
325
328
  parentId: comment.id,
326
329
  ref: ""
327
330
  });
@@ -919,6 +922,7 @@ let ThreadCommentFacadeService = class ThreadCommentFacadeService {
919
922
  text: normalizeThreadCommentContent(options.content),
920
923
  attachments: options.attachments ?? [],
921
924
  dT: getDT(options.dateTime),
925
+ authorName: options.authorName,
922
926
  personId
923
927
  }
924
928
  });
@@ -939,6 +943,7 @@ let ThreadCommentFacadeService = class ThreadCommentFacadeService {
939
943
  text: normalizeThreadCommentContent(options.content),
940
944
  attachments: options.attachments ?? [],
941
945
  dT: getDT(options.dateTime),
946
+ authorName: options.authorName,
942
947
  personId: options.personId ?? this._userManagerService.getCurrentUser().userID
943
948
  }
944
949
  });
@@ -1007,7 +1012,7 @@ function isStringArray(value) {
1007
1012
  function isSnapshotComment(value, allowChildren = true) {
1008
1013
  if (!value || typeof value !== "object") return false;
1009
1014
  const comment = value;
1010
- return typeof comment.id === "string" && comment.id.length > 0 && typeof comment.threadId === "string" && comment.threadId.length > 0 && typeof comment.ref === "string" && (comment.text === void 0 || isThreadCommentDocumentBody(comment.text)) && (comment.attachments === void 0 || isStringArray(comment.attachments)) && (comment.mentions === void 0 || isStringArray(comment.mentions)) && (!allowChildren || comment.children === void 0 || Array.isArray(comment.children) && comment.children.every((child) => isSnapshotComment(child, false)));
1015
+ return typeof comment.id === "string" && comment.id.length > 0 && typeof comment.threadId === "string" && comment.threadId.length > 0 && (typeof comment.ref === "string" || !allowChildren && comment.ref === void 0) && (comment.authorName === void 0 || typeof comment.authorName === "string") && (comment.text === void 0 || isThreadCommentDocumentBody(comment.text)) && (comment.attachments === void 0 || isStringArray(comment.attachments)) && (comment.mentions === void 0 || isStringArray(comment.mentions)) && (!allowChildren || comment.children === void 0 || Array.isArray(comment.children) && comment.children.every((child) => isSnapshotComment(child, false)));
1011
1016
  }
1012
1017
  function createThreadCommentResourceHook(threadCommentModel, threadCommentDataSourceService, pluginName, businesses) {
1013
1018
  return {
@@ -1070,7 +1075,7 @@ function createThreadCommentResourceHook(threadCommentModel, threadCommentDataSo
1070
1075
  //#endregion
1071
1076
  //#region package.json
1072
1077
  var name = "@univerjs/thread-comment";
1073
- var version = "1.0.0-rc.0";
1078
+ var version = "1.0.1";
1074
1079
 
1075
1080
  //#endregion
1076
1081
  //#region src/config/config.ts
package/lib/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { CommandType, Disposable, ICommandService, IConfigService, IUniverInstanceService, Inject, Injector, LifecycleService, LifecycleStages, Plugin, UniverInstanceType, UserManagerService, createIdentifier, dateKit, generateRandomId, merge, mergeOverrideWithDependencies } from "@univerjs/core";
2
2
  import { Subject } from "rxjs";
3
3
 
4
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/typeof.js
4
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/typeof.js
5
5
  function _typeof(o) {
6
6
  "@babel/helpers - typeof";
7
7
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -12,7 +12,7 @@ function _typeof(o) {
12
12
  }
13
13
 
14
14
  //#endregion
15
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/toPrimitive.js
15
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPrimitive.js
16
16
  function toPrimitive(t, r) {
17
17
  if ("object" != _typeof(t) || !t) return t;
18
18
  var e = t[Symbol.toPrimitive];
@@ -25,14 +25,14 @@ function toPrimitive(t, r) {
25
25
  }
26
26
 
27
27
  //#endregion
28
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/toPropertyKey.js
28
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPropertyKey.js
29
29
  function toPropertyKey(t) {
30
30
  var i = toPrimitive(t, "string");
31
31
  return "symbol" == _typeof(i) ? i : i + "";
32
32
  }
33
33
 
34
34
  //#endregion
35
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/defineProperty.js
35
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/defineProperty.js
36
36
  function _defineProperty(e, r, t) {
37
37
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
38
38
  value: t,
@@ -197,7 +197,7 @@ function deserializeThreadCommentAnchor(ref) {
197
197
  }
198
198
 
199
199
  //#endregion
200
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorateParam.js
200
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorateParam.js
201
201
  function __decorateParam(paramIndex, decorator) {
202
202
  return function(target, key) {
203
203
  decorator(target, key, paramIndex);
@@ -205,7 +205,7 @@ function __decorateParam(paramIndex, decorator) {
205
205
  }
206
206
 
207
207
  //#endregion
208
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorate.js
208
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorate.js
209
209
  function __decorate(decorators, target, key, desc) {
210
210
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
211
211
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -311,6 +311,7 @@ let ThreadCommentModel = class ThreadCommentModel extends Disposable {
311
311
  });
312
312
  const newComment = {
313
313
  ...rest,
314
+ authorName: rest.authorName ?? (rest.personId === currentComment.personId ? currentComment.authorName : void 0),
314
315
  ref: currentComment.ref
315
316
  };
316
317
  commentMap.set(comment.id, newComment);
@@ -320,8 +321,10 @@ let ThreadCommentModel = class ThreadCommentModel extends Disposable {
320
321
  if (localComment.threadId === comment.threadId && !remoteCommentIds.has(commentId)) this._deleteComment(unitId, subUnitId, commentId);
321
322
  });
322
323
  safeChildren.forEach((child) => {
324
+ var _commentMap$get, _commentMap$get2;
323
325
  commentMap.set(child.id, {
324
326
  ...child,
327
+ authorName: child.authorName ?? (child.personId === ((_commentMap$get = commentMap.get(child.id)) === null || _commentMap$get === void 0 ? void 0 : _commentMap$get.personId) ? (_commentMap$get2 = commentMap.get(child.id)) === null || _commentMap$get2 === void 0 ? void 0 : _commentMap$get2.authorName : void 0),
325
328
  parentId: comment.id,
326
329
  ref: ""
327
330
  });
@@ -919,6 +922,7 @@ let ThreadCommentFacadeService = class ThreadCommentFacadeService {
919
922
  text: normalizeThreadCommentContent(options.content),
920
923
  attachments: options.attachments ?? [],
921
924
  dT: getDT(options.dateTime),
925
+ authorName: options.authorName,
922
926
  personId
923
927
  }
924
928
  });
@@ -939,6 +943,7 @@ let ThreadCommentFacadeService = class ThreadCommentFacadeService {
939
943
  text: normalizeThreadCommentContent(options.content),
940
944
  attachments: options.attachments ?? [],
941
945
  dT: getDT(options.dateTime),
946
+ authorName: options.authorName,
942
947
  personId: options.personId ?? this._userManagerService.getCurrentUser().userID
943
948
  }
944
949
  });
@@ -1007,7 +1012,7 @@ function isStringArray(value) {
1007
1012
  function isSnapshotComment(value, allowChildren = true) {
1008
1013
  if (!value || typeof value !== "object") return false;
1009
1014
  const comment = value;
1010
- return typeof comment.id === "string" && comment.id.length > 0 && typeof comment.threadId === "string" && comment.threadId.length > 0 && typeof comment.ref === "string" && (comment.text === void 0 || isThreadCommentDocumentBody(comment.text)) && (comment.attachments === void 0 || isStringArray(comment.attachments)) && (comment.mentions === void 0 || isStringArray(comment.mentions)) && (!allowChildren || comment.children === void 0 || Array.isArray(comment.children) && comment.children.every((child) => isSnapshotComment(child, false)));
1015
+ return typeof comment.id === "string" && comment.id.length > 0 && typeof comment.threadId === "string" && comment.threadId.length > 0 && (typeof comment.ref === "string" || !allowChildren && comment.ref === void 0) && (comment.authorName === void 0 || typeof comment.authorName === "string") && (comment.text === void 0 || isThreadCommentDocumentBody(comment.text)) && (comment.attachments === void 0 || isStringArray(comment.attachments)) && (comment.mentions === void 0 || isStringArray(comment.mentions)) && (!allowChildren || comment.children === void 0 || Array.isArray(comment.children) && comment.children.every((child) => isSnapshotComment(child, false)));
1011
1016
  }
1012
1017
  function createThreadCommentResourceHook(threadCommentModel, threadCommentDataSourceService, pluginName, businesses) {
1013
1018
  return {
@@ -1070,7 +1075,7 @@ function createThreadCommentResourceHook(threadCommentModel, threadCommentDataSo
1070
1075
  //#endregion
1071
1076
  //#region package.json
1072
1077
  var name = "@univerjs/thread-comment";
1073
- var version = "1.0.0-rc.0";
1078
+ var version = "1.0.1";
1074
1079
 
1075
1080
  //#endregion
1076
1081
  //#region src/config/config.ts
@@ -18,25 +18,29 @@ import { FUniver } from '@univerjs/core/facade';
18
18
  import * as ThreadComment from '@univerjs/thread-comment';
19
19
  export interface IFUniverThreadCommentMixin {
20
20
  /**
21
- * Creates a root comment on a serialized product anchor.
21
+ * Creates a root comment on a drawing, page element, page position, or Base record anchor.
22
+ * For a sheet-cell comment, use `FRange.addCommentAsync`; for document text, use `FDocumentTextRange.createCommentAsync`.
22
23
  * @param options Comment content, owner IDs, stable anchor, and optional caller-controlled IDs.
23
- * @returns `true` when the command succeeds; otherwise, `false`.
24
+ * @returns {Promise<boolean>} `true` when the command succeeds; otherwise, `false`.
24
25
  * @throws {TypeError} If the content is empty or the anchor is invalid.
25
26
  * @example
26
27
  * ```ts
27
- * const presentation = univerAPI.getActivePresentation();
28
- * if (!presentation) throw new Error('No active presentation');
29
- * const slide = presentation.getSlideByIndex(0);
30
- * const element = slide?.getElements()[0];
31
- * if (!slide || !element) throw new Error('No commentable slide element');
28
+ * const workbook = univerAPI.getActiveWorkbook();
29
+ * if (!workbook) {
30
+ * throw new Error('No active workbook');
31
+ * }
32
+ * const sheet = workbook.getActiveSheet();
33
+ * const image = sheet.getImages()[0];
34
+ * if (!image) {
35
+ * throw new Error('No image to comment on');
36
+ * }
32
37
  *
33
38
  * await univerAPI.createCommentAsync({
34
- * unitId: presentation.getId(),
35
- * subUnitId: slide.getId(),
39
+ * unitId: workbook.getId(),
40
+ * subUnitId: sheet.getSheetId(),
36
41
  * anchor: {
37
- * kind: univerAPI.Enum.ThreadCommentAnchorKind.SLIDE_ELEMENT,
38
- * pageId: slide.getId(),
39
- * elementId: element.getId(),
42
+ * kind: univerAPI.Enum.ThreadCommentAnchorKind.SHEET_DRAWING,
43
+ * elementId: image.getId(),
40
44
  * },
41
45
  * content: 'Verify this value.',
42
46
  * });
@@ -46,7 +50,7 @@ export interface IFUniverThreadCommentMixin {
46
50
  /**
47
51
  * Adds a reply to an existing root thread.
48
52
  * @param options Reply content and the owning unit, subunit, and thread IDs.
49
- * @returns `true` when the reply is created. Returns `false` when the root thread is not loaded or the command fails.
53
+ * @returns {Promise<boolean>} `true` when the reply is created. Returns `false` when the root thread is not loaded or the command fails.
50
54
  * @throws {TypeError} If the content is empty.
51
55
  * @example
52
56
  * ```ts
@@ -65,7 +69,7 @@ export interface IFUniverThreadCommentMixin {
65
69
  /**
66
70
  * Updates the content or attachments of an existing root comment or reply.
67
71
  * @param options Updated content and the owning unit, subunit, and comment IDs.
68
- * @returns `true` when the update command succeeds; otherwise, `false`.
72
+ * @returns {Promise<boolean>} `true` when the update command succeeds; otherwise, `false`.
69
73
  * @throws {TypeError} If the content is empty.
70
74
  * @example
71
75
  * ```ts
@@ -84,7 +88,7 @@ export interface IFUniverThreadCommentMixin {
84
88
  /**
85
89
  * Deletes one comment, or the complete root and reply tree when `deleteThread` is `true`.
86
90
  * @param options Owning IDs, target comment ID, and the optional whole-thread flag.
87
- * @returns `true` when the delete command succeeds; otherwise, `false`.
91
+ * @returns {Promise<boolean>} `true` when the delete command succeeds; otherwise, `false`.
88
92
  * @example
89
93
  * ```ts
90
94
  * const [comment] = univerAPI.getComments({ resolved: false });
@@ -102,7 +106,7 @@ export interface IFUniverThreadCommentMixin {
102
106
  /**
103
107
  * Resolves a thread. Pass `resolved: false` to reopen it.
104
108
  * @param options Owning IDs, a comment ID in the thread, and the desired resolution state.
105
- * @returns `true` when the resolve command succeeds; otherwise, `false`.
109
+ * @returns {Promise<boolean>} `true` when the resolve command succeeds; otherwise, `false`.
106
110
  * @example
107
111
  * ```ts
108
112
  * const [comment] = univerAPI.getComments({ resolved: false });
@@ -118,15 +122,18 @@ export interface IFUniverThreadCommentMixin {
118
122
  resolveCommentAsync(options: ThreadComment.IResolveThreadCommentOptions): Promise<boolean>;
119
123
  /**
120
124
  * Queries locally loaded threads by unit, subunit, anchor kind, author, or resolution state.
121
- * @param query Optional filters. Omit the argument to return every locally loaded thread.
125
+ * @param [query] Optional filters. Omit the argument to return every locally loaded thread.
122
126
  * @returns Matching root threads, their replies, anchor kinds, parsed anchors, and related user IDs.
123
127
  * @example
124
128
  * ```ts
125
- * const presentation = univerAPI.getActivePresentation();
129
+ * const workbook = univerAPI.getActiveWorkbook();
130
+ * if (!workbook) {
131
+ * throw new Error('No active workbook');
132
+ * }
126
133
  * const openAgentReviews = univerAPI.getComments({
127
- * unitIds: presentation ? [presentation.getId()] : [],
134
+ * unitIds: [workbook.getId()],
128
135
  * authorIds: ['agent-reviewer'],
129
- * anchorKinds: [univerAPI.Enum.ThreadCommentAnchorKind.SLIDE_ELEMENT],
136
+ * anchorKinds: [univerAPI.Enum.ThreadCommentAnchorKind.SHEET_DRAWING],
130
137
  * resolved: false,
131
138
  * });
132
139
  * const sheetCellReviews = univerAPI.getComments({
@@ -138,14 +145,17 @@ export interface IFUniverThreadCommentMixin {
138
145
  /**
139
146
  * Synchronizes locally known threads from the configured datasource, then applies the same filters as `getComments`.
140
147
  * This method does not discover thread IDs that have never been loaded into the model.
141
- * @param query Optional filters. Omit the argument to synchronize and return every known thread.
142
- * @returns A promise resolving to the synchronized matching threads.
148
+ * @param [query] Optional filters. Omit the argument to synchronize and return every known thread.
149
+ * @returns {Promise<ThreadComment.IFacadeThreadCommentInfo[]>} A promise resolving to the synchronized matching threads.
143
150
  * @example
144
151
  * ```ts
145
- * const presentation = univerAPI.getActivePresentation();
152
+ * const workbook = univerAPI.getActiveWorkbook();
153
+ * if (!workbook) {
154
+ * throw new Error('No active workbook');
155
+ * }
146
156
  * const comments = await univerAPI.listCommentsAsync({
147
- * unitIds: presentation ? [presentation.getId()] : [],
148
- * anchorKinds: [univerAPI.Enum.ThreadCommentAnchorKind.SLIDE_ELEMENT],
157
+ * unitIds: [workbook.getId()],
158
+ * anchorKinds: [univerAPI.Enum.ThreadCommentAnchorKind.SHEET_DRAWING],
149
159
  * resolved: false,
150
160
  * });
151
161
  * comments.forEach(({ root, children, anchorKind, anchor }) => {
@@ -20,7 +20,10 @@ export type ThreadCommentJSON = {
20
20
  id: string;
21
21
  threadId: string;
22
22
  ref: string;
23
- } & Partial<Omit<IThreadComment, 'id' | 'threadId' | 'ref'>>;
23
+ children?: Array<Omit<ThreadCommentJSON, 'ref'> & {
24
+ ref?: string;
25
+ }>;
26
+ } & Partial<Omit<IThreadComment, 'id' | 'threadId' | 'ref' | 'children'>>;
24
27
  type Success = boolean;
25
28
  export interface IThreadCommentDataSource {
26
29
  /**
@@ -91,7 +94,7 @@ export declare class ThreadCommentDataSourceService extends Disposable implement
91
94
  deleteComment(unitId: string, subUnitId: string, threadId: string, commentId: string): Promise<boolean>;
92
95
  deleteThread(unitId: string, subUnitId: string, threadId: string): Promise<boolean>;
93
96
  listThreadComments(unitId: string, subUnitId: string, threadIds: string[]): Promise<false | IBaseComment[]>;
94
- saveToSnapshot(unitComments: Record<string, IThreadComment[]>, _unitId: string): Record<string, ThreadCommentJSON[]>;
97
+ saveToSnapshot(unitComments: Record<string, IThreadComment[]>, _unitId: string): Record<string, IThreadComment[]> | Record<string, ThreadCommentJSON[]>;
95
98
  }
96
99
  export declare const IThreadCommentDataSourceService: import("@wendellhu/redi").IdentifierDecorator<IThreadCommentDataSourceService>;
97
100
  export {};
@@ -35,6 +35,8 @@ export interface ICreateThreadCommentOptions {
35
35
  threadId?: string;
36
36
  /** Author user ID. Defaults to the current user managed by `UserManagerService`. */
37
37
  personId?: string;
38
+ /** Display-only author name used when the user profile is unavailable. */
39
+ authorName?: string;
38
40
  /** Creation time. Defaults to the current time. */
39
41
  dateTime?: Date;
40
42
  }
@@ -53,6 +55,8 @@ export interface IReplyThreadCommentOptions {
53
55
  id?: string;
54
56
  /** Author user ID. Defaults to the current user managed by `UserManagerService`. */
55
57
  personId?: string;
58
+ /** Display-only author name used when the user profile is unavailable. */
59
+ authorName?: string;
56
60
  /** Creation time. Defaults to the current time. */
57
61
  dateTime?: Date;
58
62
  }
@@ -25,6 +25,8 @@ export interface IBaseComment {
25
25
  dT: string;
26
26
  updateT?: string;
27
27
  personId: string;
28
+ /** Display-only fallback when the author profile cannot be resolved. Never used for permissions. */
29
+ authorName?: string;
28
30
  text: IDocumentBody;
29
31
  attachments?: string[];
30
32
  updated?: boolean;
package/lib/umd/facade.js CHANGED
@@ -1 +1 @@
1
- (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core/facade"),require("@univerjs/thread-comment")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core/facade`,`@univerjs/thread-comment`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverThreadCommentFacade={},e.UniverCoreFacade,e.UniverThreadComment))})(this,function(e,t,n){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var r=Object.create,i=Object.defineProperty,a=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.getPrototypeOf,c=Object.prototype.hasOwnProperty,l=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var s=o(t),l=0,u=s.length,d;l<u;l++)d=s[l],!c.call(e,d)&&d!==n&&i(e,d,{get:(e=>t[e]).bind(null,d),enumerable:!(r=a(t,d))||r.enumerable});return e};n=((e,t,n)=>(n=e==null?{}:r(s(e)),l(t||!e||!e.__esModule?i(n,`default`,{value:e,enumerable:!0}):n,e)))(n);var u=class extends t.FEnum{get ThreadCommentAnchorKind(){return n.ThreadCommentAnchorKind}};t.FEnum.extend(u);var d=class extends t.FUniver{_initialize(e){let t;Object.defineProperty(this,"_threadCommentService",{get:()=>t??=e.get(n.ThreadCommentFacadeService)})}createCommentAsync(e){return this._threadCommentService.createCommentAsync(e)}replyCommentAsync(e){return this._threadCommentService.replyCommentAsync(e)}updateCommentAsync(e){return this._threadCommentService.updateCommentAsync(e)}deleteCommentAsync(e){return this._threadCommentService.deleteCommentAsync(e)}resolveCommentAsync(e){return this._threadCommentService.resolveCommentAsync(e)}getComments(e={}){return this._threadCommentService.getComments(e)}async listCommentsAsync(e={}){return this._threadCommentService.listCommentsAsync(e)}};t.FUniver.extend(d),e.FThreadCommentEnumMixin=u,e.FUniverThreadCommentMixin=d});
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core/facade"),require("@univerjs/thread-comment")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core/facade`,`@univerjs/thread-comment`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverThreadCommentFacade={},e.UniverCoreFacade,e.UniverThreadComment))})(this,function(e,t,n){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var r=Object.create,i=Object.defineProperty,a=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.getPrototypeOf,c=Object.prototype.hasOwnProperty,l=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var s=o(t),l=0,u=s.length,d;l<u;l++)d=s[l],!c.call(e,d)&&d!==n&&i(e,d,{get:(e=>t[e]).bind(null,d),enumerable:!(r=a(t,d))||r.enumerable});return e};n=((e,t,n)=>(n=e==null?{}:r(s(e)),l(t||!e||!e.__esModule||!c.call(e,`default`)?i(n,`default`,{value:e,enumerable:!0}):n,e)))(n);var u=class extends t.FEnum{get ThreadCommentAnchorKind(){return n.ThreadCommentAnchorKind}};t.FEnum.extend(u);var d=class extends t.FUniver{_initialize(e){let t;Object.defineProperty(this,"_threadCommentService",{get:()=>t??=e.get(n.ThreadCommentFacadeService)})}createCommentAsync(e){return this._threadCommentService.createCommentAsync(e)}replyCommentAsync(e){return this._threadCommentService.replyCommentAsync(e)}updateCommentAsync(e){return this._threadCommentService.updateCommentAsync(e)}deleteCommentAsync(e){return this._threadCommentService.deleteCommentAsync(e)}resolveCommentAsync(e){return this._threadCommentService.resolveCommentAsync(e)}getComments(e={}){return this._threadCommentService.getComments(e)}async listCommentsAsync(e={}){return this._threadCommentService.listCommentsAsync(e)}};t.FUniver.extend(d),e.FThreadCommentEnumMixin=u,e.FUniverThreadCommentMixin=d});
package/lib/umd/index.js CHANGED
@@ -1,2 +1,2 @@
1
- (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverThreadComment={},e.UniverCore,e.rxjs))})(this,function(e,t,n){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function r(e){"@babel/helpers - typeof";return r=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},r(e)}function i(e,t){if(r(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var i=n.call(e,t||`default`);if(r(i)!=`object`)return i;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function a(e){var t=i(e,`string`);return r(t)==`symbol`?t:t+``}function o(e,t,n){return(t=a(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var s=class extends t.Disposable{set dataSource(e){this._dataSource=e}get dataSource(){return this._dataSource}constructor(){super(),o(this,`_dataSource`,null),o(this,`_pendingAdds`,new Map),o(this,`syncUpdateMutationToColla`,!0)}async getThreadComment(e,t,n){return this._dataSource?(await this._dataSource.listComments(e,t,[n]))[0]:null}addComment(e){let t=`${e.unitId}\0${e.subUnitId}\0${e.id}`,n=this._pendingAdds.get(t);if(n)return n;let r=(async()=>{let t=this._dataSource?await this._dataSource.addComment(e):e;return{...t,threadId:t.threadId||t.id}})();return this._pendingAdds.set(t,r),r.finally(()=>{this._pendingAdds.get(t)===r&&this._pendingAdds.delete(t)}).catch(()=>void 0),r}async updateComment(e){return!this._dataSource||this._dataSource.updateComment(e)}async resolveComment(e){return!this._dataSource||this._dataSource.resolveComment(e)}async deleteComment(e,t,n,r){return!this._dataSource||this._dataSource.deleteComment(e,t,n,r)}async deleteThread(e,t,n){var r;return(r=this._dataSource)!=null&&r.deleteThread?this._dataSource.deleteThread(e,t,n):this.deleteComment(e,t,n,n)}async listThreadComments(e,t,n){return this.dataSource?this.dataSource.listComments(e,t,n):!1}saveToSnapshot(e,t){if(this._dataSource){let t={};return Object.keys(e).forEach(n=>{let r=e[n];t[n]=r.map(this.dataSource.saveCommentToSnapshot)}),t}return e}};let c=(0,t.createIdentifier)(`univer.thread-comment.data-source-service`),l=function(e){return e.SHEET_CELL=`sheet-cell`,e.SHEET_DRAWING=`sheet-drawing`,e.DOC_TEXT_RANGE=`doc-text-range`,e.DOC_DRAWING=`doc-drawing`,e.SLIDE_ELEMENT=`slide-element`,e.SLIDE_POSITION=`slide-position`,e.BOARD_ELEMENT=`board-element`,e.BOARD_POSITION=`board-position`,e.BASE_RECORD=`base-record`,e}({}),u=`univer-comment-anchor:`;function d(e){return typeof e==`string`&&e.length>0}function f(e){return e===void 0||d(e)}function p(e){return typeof e==`number`&&Number.isFinite(e)}function m(e){if(!e||typeof e!=`object`)return!1;switch(Reflect.get(e,`kind`)){case`sheet-drawing`:case`doc-drawing`:case`slide-element`:case`board-element`:return d(Reflect.get(e,`elementId`))&&f(Reflect.get(e,`pageId`));case`slide-position`:case`board-position`:return p(Reflect.get(e,`x`))&&p(Reflect.get(e,`y`))&&f(Reflect.get(e,`pageId`));case`base-record`:return d(Reflect.get(e,`tableId`))&&d(Reflect.get(e,`recordId`));default:return!1}}function h(e){if(!m(e))throw TypeError(`Invalid thread comment anchor`);return`${u}${JSON.stringify({v:1,anchor:e})}`}function g(e){if(!e.startsWith(u))return null;try{let t=JSON.parse(e.slice(22));if(!t||typeof t!=`object`||Reflect.get(t,`v`)!==1)return null;let n=Reflect.get(t,`anchor`);return m(n)?n:null}catch{return null}}function _(e,t){return function(n,r){t(n,r,e)}}function v(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 y=class extends t.Disposable{constructor(e,r,i){super(),this._dataSourceService=e,this._lifecycleService=r,this._univerInstanceService=i,o(this,`_commentsMap`,new Map),o(this,`_threadMap`,new Map),o(this,`_syncVersions`,new Map),o(this,`_nextSyncVersion`,0),o(this,`_commentUpdate$`,new n.Subject),o(this,`commentUpdate$`,this._commentUpdate$.asObservable()),o(this,`_tasks`,[]),this.disposeWithMe(()=>{this._commentUpdate$.complete()}),this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe(e=>{let n=new Map;e===t.LifecycleStages.Rendered&&(this._tasks.forEach(({unitId:e,subUnitId:t,threadIds:r})=>{let i=n.get(e);i||(i=new Map,n.set(e,i));let a=i.get(t);a||(a=new Set,i.set(t,a));for(let e of r)a.add(e)}),this._tasks=[],n.forEach((e,t)=>{e.forEach((e,n)=>{this.syncThreadComments(t,n,Array.from(e))})}))}))}_ensureCommentMap(e,t){let n=this._commentsMap.get(e);n||(n=new Map,this._commentsMap.set(e,n));let r=n.get(t);return r||(r=new Map,n.set(t,r)),r}ensureMap(e,t){return this._ensureCommentMap(e,t)}_ensureThreadMap(e,t){let n=this._threadMap.get(e);n||(n=new Map,this._threadMap.set(e,n));let r=n.get(t);return r||(r=new Map,n.set(t,r)),r}_syncKey(e,t,n){return`${e}\0${t}\0${n}`}_invalidateSync(e,t,n){this._syncVersions.delete(this._syncKey(e,t,n))}_replaceComment(e,t,n){let r=this._ensureCommentMap(e,t),i=r.get(n.id);if(!i){`ref`in n&&typeof n.ref==`string`&&this.addComment(e,t,{...n,ref:n.ref});return}let{children:a=[],...o}=n,s=new Set([n.id]),c=a.filter(e=>{let t=r.get(e.id);return e.threadId!==n.threadId||s.has(e.id)||t&&t.threadId!==n.threadId?!1:(s.add(e.id),!0)}),l={...o,ref:i.ref};r.set(n.id,l),l.parentId||this._ensureThreadMap(e,t).set(l.threadId,l);let u=new Set([n.id,...c.map(e=>e.id)]);r.forEach((r,i)=>{r.threadId===n.threadId&&!u.has(i)&&this._deleteComment(e,t,i)}),c.forEach(e=>{r.set(e.id,{...e,parentId:n.id,ref:``})}),this._commentUpdate$.next({unitId:e,subUnitId:t,type:`syncUpdate`,payload:l}),!!n.resolved!=!!i.resolved&&this._commentUpdate$.next({unitId:e,subUnitId:t,type:`resolve`,payload:{commentId:n.id,resolved:!!n.resolved}})}async syncThreadComments(e,n,r){if(this._lifecycleService.stage<t.LifecycleStages.Rendered){this._tasks.push({unitId:e,subUnitId:n,threadIds:r});return}let i=new Map(r.map(t=>{let r=this._syncKey(e,n,t),i=++this._nextSyncVersion;return this._syncVersions.set(r,i),[t,{key:r,version:i}]})),a=()=>i.forEach(({key:e,version:t})=>{this._syncVersions.get(e)===t&&this._syncVersions.delete(e)}),o;try{o=await this._dataSourceService.listThreadComments(e,n,r)}catch(e){throw a(),e}if(!o){a();return}let s=new Set(r.filter(e=>{let t=i.get(e);return this._syncVersions.get(t.key)===t.version}));if(!s.size){a();return}let c=this._ensureThreadMap(e,n),l=this._ensureCommentMap(e,n);o.forEach(t=>{s.has(t.threadId)&&(this._replaceComment(e,n,t),s.delete(t.threadId))}),s.forEach(t=>{c.delete(t),l.forEach((r,i)=>{r.threadId===t&&this._deleteComment(e,n,i)})}),a()}addComment(e,t,n,r){let i=this._ensureCommentMap(e,t);if(i.has(n.id))return!0;let{parentId:a,children:o=[],...s}=n,c={...s,parentId:a===n.id?void 0:a};c.threadId||=c.parentId||c.id,this._invalidateSync(e,t,c.threadId);let l=n=>{i.set(n.id,n),this._commentUpdate$.next({unitId:e,subUnitId:t,type:`add`,payload:n,isRoot:!n.parentId})};l(c);let u=this._ensureThreadMap(e,t);if(!c.parentId){u.set(c.threadId,c);for(let e of o)e.id===c.id||e.threadId!==c.threadId||i.has(e.id)||l({...e,parentId:c.id,ref:``})}return r&&this.syncThreadComments(e,t,[c.threadId]),!0}updateComment(e,t,n,r){let i=this._ensureCommentMap(e,t).get(n.commentId);return i?(this._invalidateSync(e,t,i.threadId),i.updated=!0,i.text=n.text,i.attachments=n.attachments,i.updateT=n.updateT,this._commentUpdate$.next({unitId:e,subUnitId:t,type:`update`,payload:n,silent:r}),!0):!1}updateCommentRef(e,t,n,r){let i=this._ensureCommentMap(e,t).get(n.commentId);return i?(this._invalidateSync(e,t,i.threadId),i.ref=n.ref,this._commentUpdate$.next({unitId:e,subUnitId:t,type:`updateRef`,payload:n,silent:r,threadId:i.threadId}),!0):!1}resolveComment(e,t,n,r){let i=this._ensureCommentMap(e,t).get(n);return i?(this._invalidateSync(e,t,i.threadId),i.resolved=r,this._commentUpdate$.next({unitId:e,subUnitId:t,type:`resolve`,payload:{commentId:n,resolved:r}}),!0):!1}getComment(e,t,n){return this._ensureCommentMap(e,t).get(n)}getRootComment(e,t,n){return this._ensureThreadMap(e,t).get(n)}getThread(e,t,n){let r=this._ensureCommentMap(e,t),i=Array.from(r.values()).filter(e=>e.threadId===n),a,o=[],s=new Set;for(let e of i)e.parentId?o.push(e):a=e,s.add(e.personId);if(a)return{root:a,children:o,relativeUsers:s,unitId:e,subUnitId:t,threadId:n}}getCommentWithChildren(e,t,n){let r=this.getComment(e,t,n);if(r)return this.getThread(e,t,r.threadId)}_deleteComment(e,t,n){let r=this._ensureCommentMap(e,t),i=r.get(n);i&&(this._invalidateSync(e,t,i.threadId),r.delete(n),this._commentUpdate$.next({unitId:e,subUnitId:t,type:`delete`,payload:{commentId:n,isRoot:!i.parentId,comment:i}}))}deleteThread(e,t,n){this._invalidateSync(e,t,n),this._ensureThreadMap(e,t).delete(n),this._ensureCommentMap(e,t).forEach(r=>{r.threadId===n&&this._deleteComment(e,t,r.id)})}deleteComment(e,t,n){let r=this._ensureCommentMap(e,t).get(n);return r&&(r.parentId?this._deleteComment(e,t,n):this.deleteThread(e,t,r.threadId)),!0}deleteUnit(e){let t=this._commentsMap.get(e);t==null||t.forEach((t,n)=>{t.forEach(t=>{this.deleteComment(e,n,t.id)})}),this._commentsMap.delete(e),this._threadMap.delete(e),this._tasks=this._tasks.filter(t=>t.unitId!==e);let n=`${e}\0`;this._syncVersions.forEach((e,t)=>{t.startsWith(n)&&this._syncVersions.delete(t)})}getUnit(e){let t=this._commentsMap.get(e);if(!t)return[];let n=[];return t.forEach((t,r)=>{let i=new Map;t.forEach(t=>{let n=i.get(t.threadId);n||(n={unitId:e,subUnitId:r,threadId:t.threadId,children:[],relativeUsers:new Set},i.set(t.threadId,n)),t.parentId?n.children.push(t):n.root=t,n.relativeUsers.add(t.personId)}),i.forEach(e=>{e.root&&n.push({...e,root:e.root})})}),n}getAll(){let e=[];return this._commentsMap.forEach((t,n)=>{e.push({unitId:n,threads:this.getUnit(n)})}),e}getAnchorKind(e,n){let r=g(n);if(r)return r.kind;switch(this._univerInstanceService.getUnitType(e)){case t.UniverInstanceType.UNIVER_SHEET:return`sheet-cell`;case t.UniverInstanceType.UNIVER_DOC:return`doc-text-range`;default:return null}}query(e={}){let t=e.unitIds?new Set(e.unitIds):null,n=e.subUnitIds?new Set(e.subUnitIds):null,r=e.anchorKinds?new Set(e.anchorKinds):null,i=e.authorIds?new Set(e.authorIds):null;return this.getAll().flatMap(({unitId:a,threads:o})=>t&&!t.has(a)?[]:o.filter(t=>{if(n&&!n.has(t.subUnitId)||e.resolved!==void 0&&!!t.root.resolved!==e.resolved)return!1;if(r){let e=this.getAnchorKind(a,t.root.ref);if(!e||!r.has(e))return!1}return!(i&&![t.root,...t.children].some(e=>i.has(e.personId)))}))}};y=v([_(0,(0,t.Inject)(c)),_(1,(0,t.Inject)(t.LifecycleService)),_(2,t.IUniverInstanceService)],y);let b={id:`thread-comment.mutation.add-comment`,type:t.CommandType.MUTATION,handler(e,t,n){if(!t)return!1;let r=e.get(y),{unitId:i,subUnitId:a,comment:o,sync:s}=t,c=s||(n==null?void 0:n.fromChangeset)&&!o.parentId;return r.addComment(i,a,o,c)}},x={id:`thread-comment.mutation.update-comment`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(y),{unitId:r,subUnitId:i,payload:a,silent:o}=t;return n.updateComment(r,i,a,o)}},S={id:`thread-comment.mutation.update-comment-ref`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(y),{unitId:r,subUnitId:i,payload:a,silent:o}=t;return n.updateCommentRef(r,i,a,o)}},C={id:`thread-comment.mutation.resolve-comment`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(y),{unitId:r,subUnitId:i,resolved:a,commentId:o}=t;return n.resolveComment(r,i,o,a)}},w={id:`thread-comment.mutation.delete-comment`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(y),{unitId:r,subUnitId:i,commentId:a}=t;return n.deleteComment(r,i,a)}},T=new WeakMap;function E(e,t){let n=T.get(e)??{resolve:0,update:0},r=n[t]+1;return n[t]=r,T.set(e,n),r}function D(e,t,n){var r;return((r=T.get(e))==null?void 0:r[t])===n}let O={id:`thread-comment.command.add-comment`,type:t.CommandType.COMMAND,async handler(e,n){var r,i;if(!n)return!1;let a=e.get(t.ICommandService),o=e.get(c),s=e.get(y),{comment:l}=n,u=!l.parentId;if(!u&&((r=s.getRootComment(n.unitId,n.subUnitId,l.threadId))==null?void 0:r.id)!==l.parentId)return!1;let d={...await o.addComment(l),unitId:n.unitId,subUnitId:n.subUnitId,ref:l.ref,parentId:l.parentId,...u?{}:{threadId:l.threadId}},f=o.syncUpdateMutationToColla;if(!u&&((i=s.getRootComment(n.unitId,n.subUnitId,l.threadId))==null?void 0:i.id)!==l.parentId){try{await o.deleteComment(n.unitId,n.subUnitId,d.threadId,d.id)}catch{}return!1}let p={id:b.id,params:{...n,comment:d}};return u?await a.executeCommand(p.id,p.params):a.executeCommand(p.id,p.params,{onlyLocal:!f})}},k={id:`thread-comment.command.update-comment`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let{unitId:r,subUnitId:i,payload:a}=n,o=e.get(t.ICommandService),s=e.get(y),l=e.get(c),u=l.syncUpdateMutationToColla,d=s.getComment(r,i,a.commentId);if(!d)return!1;let{children:f,...p}=d,m=E(d,`update`);if(!await l.updateComment({...p,...a})||s.getComment(r,i,a.commentId)!==d||!D(d,`update`,m))return!1;let h={id:x.id,params:n};return o.executeCommand(h.id,h.params,{onlyLocal:!u})}},A={id:`thread-comment.command.resolve-comment`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let{unitId:r,subUnitId:i,resolved:a,commentId:o}=n,s=e.get(c),l=e.get(y),u=l.getComment(r,i,o),d=s.syncUpdateMutationToColla;if(!u)return!1;let f=E(u,`resolve`);return!await s.resolveComment({...u,resolved:a})||l.getComment(r,i,o)!==u||!D(u,`resolve`,f)?!1:e.get(t.ICommandService).executeCommand(C.id,n,{onlyLocal:!d})}},j={id:`thread-comment.command.delete-comment`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let r=e.get(y),i=e.get(c),a=e.get(t.ICommandService),{unitId:o,subUnitId:s,commentId:l}=n,u=i.syncUpdateMutationToColla,d=r.getComment(o,s,l);if(!d||!await i.deleteComment(o,s,d.threadId,l))return!1;let f={id:w.id,params:n};return a.executeCommand(f.id,f.params,{onlyLocal:!u})}},M={id:`thread-comment.command.delete-comment-tree`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let r=e.get(y),i=e.get(t.ICommandService),a=e.get(c),{unitId:o,subUnitId:s,commentId:l}=n,u=r.getCommentWithChildren(o,s,l);return!u||!(a.deleteThread?await a.deleteThread(o,s,u.root.threadId):await a.deleteComment(o,s,u.root.threadId,u.root.id))?!1:await i.executeCommand(w.id,{unitId:o,subUnitId:s,commentId:u.root.id})}};function N(e){return(0,t.dateKit)(e).format(`YYYY/MM/DD HH:mm`)}let P=[`renderedPageBreaks`,`textRuns`,`paragraphs`,`sectionBreaks`,`customBlocks`,`tables`,`columnGroups`,`blockRanges`,`customRanges`,`customDecorations`];function F(e){if(!e||typeof e!=`object`||typeof Reflect.get(e,`dataStream`)!=`string`||P.some(t=>{let n=Reflect.get(e,t);return n!==void 0&&!Array.isArray(n)}))return!1;let t=Reflect.get(e,`payloads`);return t===void 0||typeof t==`object`&&!!t&&!Array.isArray(t)&&Object.values(t).every(e=>typeof e==`string`)}function I(e){if(typeof e!=`string`){if(!F(e))throw TypeError(`Invalid thread comment content`);if(!e.dataStream.replace(/[\r\n]/g,``).trim())throw TypeError(`Thread comment content cannot be empty`);return e}if(!e.trim())throw TypeError(`Thread comment content cannot be empty`);return{dataStream:e.replace(/\r\n?|\n/g,`\r`).replace(/\r*$/,`\r
2
- `)}}function L(e,t){return{unitId:e.unitId,subUnitId:e.subUnitId,threadId:e.threadId,root:e.root,children:e.children,anchorKind:t,anchor:g(e.root.ref),relativeUserIds:Array.from(e.relativeUsers)}}let R=class{constructor(e,t,n){this._commandService=e,this._model=t,this._userManagerService=n}createCommentAsync(e){let n=e.id??(0,t.generateRandomId)(),r=e.threadId??n,i=e.personId??this._userManagerService.getCurrentUser().userID;return this._commandService.executeCommand(O.id,{unitId:e.unitId,subUnitId:e.subUnitId,comment:{id:n,threadId:r,unitId:e.unitId,subUnitId:e.subUnitId,ref:h(e.anchor),text:I(e.content),attachments:e.attachments??[],dT:N(e.dateTime),personId:i}})}replyCommentAsync(e){let n=this._model.getRootComment(e.unitId,e.subUnitId,e.threadId);return n?this._commandService.executeCommand(O.id,{unitId:e.unitId,subUnitId:e.subUnitId,comment:{id:e.id??(0,t.generateRandomId)(),threadId:e.threadId,parentId:n.id,unitId:e.unitId,subUnitId:e.subUnitId,ref:n.ref,text:I(e.content),attachments:e.attachments??[],dT:N(e.dateTime),personId:e.personId??this._userManagerService.getCurrentUser().userID}}):Promise.resolve(!1)}updateCommentAsync(e){return this._commandService.executeCommand(k.id,{unitId:e.unitId,subUnitId:e.subUnitId,payload:{commentId:e.commentId,text:I(e.content),attachments:e.attachments,updateT:N(e.dateTime)}})}deleteCommentAsync(e){return this._commandService.executeCommand(e.deleteThread?M.id:j.id,{unitId:e.unitId,subUnitId:e.subUnitId,commentId:e.commentId})}resolveCommentAsync(e){return this._commandService.executeCommand(A.id,{unitId:e.unitId,subUnitId:e.subUnitId,commentId:e.commentId,resolved:e.resolved??!0})}getComments(e={}){return this._model.query(e).map(e=>L(e,this._model.getAnchorKind(e.unitId,e.root.ref)))}async listCommentsAsync(e={}){let t=this._model.query({unitIds:e.unitIds,subUnitIds:e.subUnitIds}),n=new Map;return t.forEach(e=>{let t=`${e.unitId}\0${e.subUnitId}`,r=n.get(t)??{unitId:e.unitId,subUnitId:e.subUnitId,threadIds:[]};r.threadIds.push(e.threadId),n.set(t,r)}),await Promise.all(Array.from(n.values()).map(e=>this._model.syncThreadComments(e.unitId,e.subUnitId,e.threadIds))),this._model.query(e).map(e=>L(e,this._model.getAnchorKind(e.unitId,e.root.ref)))}};R=v([_(0,(0,t.Inject)(t.ICommandService)),_(1,(0,t.Inject)(y)),_(2,(0,t.Inject)(t.UserManagerService))],R);function z(e){return Array.isArray(e)&&e.every(e=>typeof e==`string`)}function B(e,t=!0){if(!e||typeof e!=`object`)return!1;let n=e;return typeof n.id==`string`&&n.id.length>0&&typeof n.threadId==`string`&&n.threadId.length>0&&typeof n.ref==`string`&&(n.text===void 0||F(n.text))&&(n.attachments===void 0||z(n.attachments))&&(n.mentions===void 0||z(n.mentions))&&(!t||n.children===void 0||Array.isArray(n.children)&&n.children.every(e=>B(e,!1)))}function V(e,t,n,r){return{pluginName:n,businesses:r,toJson:n=>{let r=e.getUnit(n),i={};return r.forEach(e=>{let t=i[e.subUnitId]??[];t.push({...e.root,children:e.children}),i[e.subUnitId]=t}),JSON.stringify(t.saveToSnapshot(i,n))},parseJson:e=>{if(!e)return{};try{let t=JSON.parse(e);return!t||typeof t!=`object`||Array.isArray(t)?{}:Object.fromEntries(Object.entries(t).flatMap(([e,t])=>Array.isArray(t)?[[e,t.filter(e=>B(e))]]:[]))}catch{return{}}},onUnLoad:t=>{e.deleteUnit(t)},onLoad:(t,n)=>{Object.keys(n).forEach(r=>{let i=n[r];i.forEach(n=>{var i;let a=new Set([n.id]),o=(i=n.children)==null?void 0:i.filter(e=>e.threadId!==n.threadId||a.has(e.id)?!1:(a.add(e.id),!0)).map(e=>({...e,unitId:t,subUnitId:r}));e.addComment(t,r,{...n,unitId:t,subUnitId:r,children:o})}),e.syncThreadComments(t,r,i.map(e=>e.threadId))})}}}var H=`@univerjs/thread-comment`,U=`1.0.0-rc.0`;let W={},G=`UNIVER_THREAD_COMMENT_PLUGIN`,K=class extends t.Plugin{constructor(e=W,n,r,i){super(),this._config=e,this._injector=n,this._commandService=r,this._configService=i;let{...a}=(0,t.merge)({},W,this._config);this._configService.setConfig(`thread-comment.config`,a)}onStarting(){var e;(0,t.mergeOverrideWithDependencies)([[c,{useClass:s}],[y],[R]],(e=this._config)==null?void 0:e.overrides).forEach(e=>{this._injector.add(e)}),[O,k,j,A,M,b,x,S,w,C].forEach(e=>{this._commandService.registerCommand(e)})}};o(K,`pluginName`,G),o(K,`packageName`,H),o(K,`version`,U),o(K,`type`,t.UniverInstanceType.UNIVER_UNKNOWN),K=v([_(1,(0,t.Inject)(t.Injector)),_(2,t.ICommandService),_(3,t.IConfigService)],K),e.AddCommentCommand=O,e.AddCommentMutation=b,e.DeleteCommentCommand=j,e.DeleteCommentMutation=w,e.DeleteCommentTreeCommand=M,e.IThreadCommentDataSourceService=c,e.ResolveCommentCommand=A,e.ResolveCommentMutation=C,e.TC_PLUGIN_NAME=G,e.ThreadCommentAnchorKind=l,e.ThreadCommentDataSourceService=s,Object.defineProperty(e,"ThreadCommentFacadeService",{enumerable:!0,get:function(){return R}}),Object.defineProperty(e,"ThreadCommentModel",{enumerable:!0,get:function(){return y}}),Object.defineProperty(e,"UniverThreadCommentPlugin",{enumerable:!0,get:function(){return K}}),e.UpdateCommentCommand=k,e.UpdateCommentMutation=x,e.UpdateCommentRefMutation=S,e.createThreadCommentResourceHook=V,e.deserializeThreadCommentAnchor=g,e.getDT=N,e.isThreadCommentAnchor=m,e.isThreadCommentDocumentBody=F,e.normalizeThreadCommentContent=I,e.serializeThreadCommentAnchor=h});
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverThreadComment={},e.UniverCore,e.rxjs))})(this,function(e,t,n){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function r(e){"@babel/helpers - typeof";return r=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},r(e)}function i(e,t){if(r(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var i=n.call(e,t||`default`);if(r(i)!=`object`)return i;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function a(e){var t=i(e,`string`);return r(t)==`symbol`?t:t+``}function o(e,t,n){return(t=a(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var s=class extends t.Disposable{set dataSource(e){this._dataSource=e}get dataSource(){return this._dataSource}constructor(){super(),o(this,`_dataSource`,null),o(this,`_pendingAdds`,new Map),o(this,`syncUpdateMutationToColla`,!0)}async getThreadComment(e,t,n){return this._dataSource?(await this._dataSource.listComments(e,t,[n]))[0]:null}addComment(e){let t=`${e.unitId}\0${e.subUnitId}\0${e.id}`,n=this._pendingAdds.get(t);if(n)return n;let r=(async()=>{let t=this._dataSource?await this._dataSource.addComment(e):e;return{...t,threadId:t.threadId||t.id}})();return this._pendingAdds.set(t,r),r.finally(()=>{this._pendingAdds.get(t)===r&&this._pendingAdds.delete(t)}).catch(()=>void 0),r}async updateComment(e){return!this._dataSource||this._dataSource.updateComment(e)}async resolveComment(e){return!this._dataSource||this._dataSource.resolveComment(e)}async deleteComment(e,t,n,r){return!this._dataSource||this._dataSource.deleteComment(e,t,n,r)}async deleteThread(e,t,n){var r;return(r=this._dataSource)!=null&&r.deleteThread?this._dataSource.deleteThread(e,t,n):this.deleteComment(e,t,n,n)}async listThreadComments(e,t,n){return this.dataSource?this.dataSource.listComments(e,t,n):!1}saveToSnapshot(e,t){if(this._dataSource){let t={};return Object.keys(e).forEach(n=>{let r=e[n];t[n]=r.map(this.dataSource.saveCommentToSnapshot)}),t}return e}};let c=(0,t.createIdentifier)(`univer.thread-comment.data-source-service`),l=function(e){return e.SHEET_CELL=`sheet-cell`,e.SHEET_DRAWING=`sheet-drawing`,e.DOC_TEXT_RANGE=`doc-text-range`,e.DOC_DRAWING=`doc-drawing`,e.SLIDE_ELEMENT=`slide-element`,e.SLIDE_POSITION=`slide-position`,e.BOARD_ELEMENT=`board-element`,e.BOARD_POSITION=`board-position`,e.BASE_RECORD=`base-record`,e}({}),u=`univer-comment-anchor:`;function d(e){return typeof e==`string`&&e.length>0}function f(e){return e===void 0||d(e)}function p(e){return typeof e==`number`&&Number.isFinite(e)}function m(e){if(!e||typeof e!=`object`)return!1;switch(Reflect.get(e,`kind`)){case`sheet-drawing`:case`doc-drawing`:case`slide-element`:case`board-element`:return d(Reflect.get(e,`elementId`))&&f(Reflect.get(e,`pageId`));case`slide-position`:case`board-position`:return p(Reflect.get(e,`x`))&&p(Reflect.get(e,`y`))&&f(Reflect.get(e,`pageId`));case`base-record`:return d(Reflect.get(e,`tableId`))&&d(Reflect.get(e,`recordId`));default:return!1}}function h(e){if(!m(e))throw TypeError(`Invalid thread comment anchor`);return`${u}${JSON.stringify({v:1,anchor:e})}`}function g(e){if(!e.startsWith(u))return null;try{let t=JSON.parse(e.slice(22));if(!t||typeof t!=`object`||Reflect.get(t,`v`)!==1)return null;let n=Reflect.get(t,`anchor`);return m(n)?n:null}catch{return null}}function _(e,t){return function(n,r){t(n,r,e)}}function v(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 y=class extends t.Disposable{constructor(e,r,i){super(),this._dataSourceService=e,this._lifecycleService=r,this._univerInstanceService=i,o(this,`_commentsMap`,new Map),o(this,`_threadMap`,new Map),o(this,`_syncVersions`,new Map),o(this,`_nextSyncVersion`,0),o(this,`_commentUpdate$`,new n.Subject),o(this,`commentUpdate$`,this._commentUpdate$.asObservable()),o(this,`_tasks`,[]),this.disposeWithMe(()=>{this._commentUpdate$.complete()}),this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe(e=>{let n=new Map;e===t.LifecycleStages.Rendered&&(this._tasks.forEach(({unitId:e,subUnitId:t,threadIds:r})=>{let i=n.get(e);i||(i=new Map,n.set(e,i));let a=i.get(t);a||(a=new Set,i.set(t,a));for(let e of r)a.add(e)}),this._tasks=[],n.forEach((e,t)=>{e.forEach((e,n)=>{this.syncThreadComments(t,n,Array.from(e))})}))}))}_ensureCommentMap(e,t){let n=this._commentsMap.get(e);n||(n=new Map,this._commentsMap.set(e,n));let r=n.get(t);return r||(r=new Map,n.set(t,r)),r}ensureMap(e,t){return this._ensureCommentMap(e,t)}_ensureThreadMap(e,t){let n=this._threadMap.get(e);n||(n=new Map,this._threadMap.set(e,n));let r=n.get(t);return r||(r=new Map,n.set(t,r)),r}_syncKey(e,t,n){return`${e}\0${t}\0${n}`}_invalidateSync(e,t,n){this._syncVersions.delete(this._syncKey(e,t,n))}_replaceComment(e,t,n){let r=this._ensureCommentMap(e,t),i=r.get(n.id);if(!i){`ref`in n&&typeof n.ref==`string`&&this.addComment(e,t,{...n,ref:n.ref});return}let{children:a=[],...o}=n,s=new Set([n.id]),c=a.filter(e=>{let t=r.get(e.id);return e.threadId!==n.threadId||s.has(e.id)||t&&t.threadId!==n.threadId?!1:(s.add(e.id),!0)}),l={...o,authorName:o.authorName??(o.personId===i.personId?i.authorName:void 0),ref:i.ref};r.set(n.id,l),l.parentId||this._ensureThreadMap(e,t).set(l.threadId,l);let u=new Set([n.id,...c.map(e=>e.id)]);r.forEach((r,i)=>{r.threadId===n.threadId&&!u.has(i)&&this._deleteComment(e,t,i)}),c.forEach(e=>{var t,i;r.set(e.id,{...e,authorName:e.authorName??(e.personId===((t=r.get(e.id))==null?void 0:t.personId)?(i=r.get(e.id))==null?void 0:i.authorName:void 0),parentId:n.id,ref:``})}),this._commentUpdate$.next({unitId:e,subUnitId:t,type:`syncUpdate`,payload:l}),!!n.resolved!=!!i.resolved&&this._commentUpdate$.next({unitId:e,subUnitId:t,type:`resolve`,payload:{commentId:n.id,resolved:!!n.resolved}})}async syncThreadComments(e,n,r){if(this._lifecycleService.stage<t.LifecycleStages.Rendered){this._tasks.push({unitId:e,subUnitId:n,threadIds:r});return}let i=new Map(r.map(t=>{let r=this._syncKey(e,n,t),i=++this._nextSyncVersion;return this._syncVersions.set(r,i),[t,{key:r,version:i}]})),a=()=>i.forEach(({key:e,version:t})=>{this._syncVersions.get(e)===t&&this._syncVersions.delete(e)}),o;try{o=await this._dataSourceService.listThreadComments(e,n,r)}catch(e){throw a(),e}if(!o){a();return}let s=new Set(r.filter(e=>{let t=i.get(e);return this._syncVersions.get(t.key)===t.version}));if(!s.size){a();return}let c=this._ensureThreadMap(e,n),l=this._ensureCommentMap(e,n);o.forEach(t=>{s.has(t.threadId)&&(this._replaceComment(e,n,t),s.delete(t.threadId))}),s.forEach(t=>{c.delete(t),l.forEach((r,i)=>{r.threadId===t&&this._deleteComment(e,n,i)})}),a()}addComment(e,t,n,r){let i=this._ensureCommentMap(e,t);if(i.has(n.id))return!0;let{parentId:a,children:o=[],...s}=n,c={...s,parentId:a===n.id?void 0:a};c.threadId||=c.parentId||c.id,this._invalidateSync(e,t,c.threadId);let l=n=>{i.set(n.id,n),this._commentUpdate$.next({unitId:e,subUnitId:t,type:`add`,payload:n,isRoot:!n.parentId})};l(c);let u=this._ensureThreadMap(e,t);if(!c.parentId){u.set(c.threadId,c);for(let e of o)e.id===c.id||e.threadId!==c.threadId||i.has(e.id)||l({...e,parentId:c.id,ref:``})}return r&&this.syncThreadComments(e,t,[c.threadId]),!0}updateComment(e,t,n,r){let i=this._ensureCommentMap(e,t).get(n.commentId);return i?(this._invalidateSync(e,t,i.threadId),i.updated=!0,i.text=n.text,i.attachments=n.attachments,i.updateT=n.updateT,this._commentUpdate$.next({unitId:e,subUnitId:t,type:`update`,payload:n,silent:r}),!0):!1}updateCommentRef(e,t,n,r){let i=this._ensureCommentMap(e,t).get(n.commentId);return i?(this._invalidateSync(e,t,i.threadId),i.ref=n.ref,this._commentUpdate$.next({unitId:e,subUnitId:t,type:`updateRef`,payload:n,silent:r,threadId:i.threadId}),!0):!1}resolveComment(e,t,n,r){let i=this._ensureCommentMap(e,t).get(n);return i?(this._invalidateSync(e,t,i.threadId),i.resolved=r,this._commentUpdate$.next({unitId:e,subUnitId:t,type:`resolve`,payload:{commentId:n,resolved:r}}),!0):!1}getComment(e,t,n){return this._ensureCommentMap(e,t).get(n)}getRootComment(e,t,n){return this._ensureThreadMap(e,t).get(n)}getThread(e,t,n){let r=this._ensureCommentMap(e,t),i=Array.from(r.values()).filter(e=>e.threadId===n),a,o=[],s=new Set;for(let e of i)e.parentId?o.push(e):a=e,s.add(e.personId);if(a)return{root:a,children:o,relativeUsers:s,unitId:e,subUnitId:t,threadId:n}}getCommentWithChildren(e,t,n){let r=this.getComment(e,t,n);if(r)return this.getThread(e,t,r.threadId)}_deleteComment(e,t,n){let r=this._ensureCommentMap(e,t),i=r.get(n);i&&(this._invalidateSync(e,t,i.threadId),r.delete(n),this._commentUpdate$.next({unitId:e,subUnitId:t,type:`delete`,payload:{commentId:n,isRoot:!i.parentId,comment:i}}))}deleteThread(e,t,n){this._invalidateSync(e,t,n),this._ensureThreadMap(e,t).delete(n),this._ensureCommentMap(e,t).forEach(r=>{r.threadId===n&&this._deleteComment(e,t,r.id)})}deleteComment(e,t,n){let r=this._ensureCommentMap(e,t).get(n);return r&&(r.parentId?this._deleteComment(e,t,n):this.deleteThread(e,t,r.threadId)),!0}deleteUnit(e){let t=this._commentsMap.get(e);t==null||t.forEach((t,n)=>{t.forEach(t=>{this.deleteComment(e,n,t.id)})}),this._commentsMap.delete(e),this._threadMap.delete(e),this._tasks=this._tasks.filter(t=>t.unitId!==e);let n=`${e}\0`;this._syncVersions.forEach((e,t)=>{t.startsWith(n)&&this._syncVersions.delete(t)})}getUnit(e){let t=this._commentsMap.get(e);if(!t)return[];let n=[];return t.forEach((t,r)=>{let i=new Map;t.forEach(t=>{let n=i.get(t.threadId);n||(n={unitId:e,subUnitId:r,threadId:t.threadId,children:[],relativeUsers:new Set},i.set(t.threadId,n)),t.parentId?n.children.push(t):n.root=t,n.relativeUsers.add(t.personId)}),i.forEach(e=>{e.root&&n.push({...e,root:e.root})})}),n}getAll(){let e=[];return this._commentsMap.forEach((t,n)=>{e.push({unitId:n,threads:this.getUnit(n)})}),e}getAnchorKind(e,n){let r=g(n);if(r)return r.kind;switch(this._univerInstanceService.getUnitType(e)){case t.UniverInstanceType.UNIVER_SHEET:return`sheet-cell`;case t.UniverInstanceType.UNIVER_DOC:return`doc-text-range`;default:return null}}query(e={}){let t=e.unitIds?new Set(e.unitIds):null,n=e.subUnitIds?new Set(e.subUnitIds):null,r=e.anchorKinds?new Set(e.anchorKinds):null,i=e.authorIds?new Set(e.authorIds):null;return this.getAll().flatMap(({unitId:a,threads:o})=>t&&!t.has(a)?[]:o.filter(t=>{if(n&&!n.has(t.subUnitId)||e.resolved!==void 0&&!!t.root.resolved!==e.resolved)return!1;if(r){let e=this.getAnchorKind(a,t.root.ref);if(!e||!r.has(e))return!1}return!(i&&![t.root,...t.children].some(e=>i.has(e.personId)))}))}};y=v([_(0,(0,t.Inject)(c)),_(1,(0,t.Inject)(t.LifecycleService)),_(2,t.IUniverInstanceService)],y);let b={id:`thread-comment.mutation.add-comment`,type:t.CommandType.MUTATION,handler(e,t,n){if(!t)return!1;let r=e.get(y),{unitId:i,subUnitId:a,comment:o,sync:s}=t,c=s||(n==null?void 0:n.fromChangeset)&&!o.parentId;return r.addComment(i,a,o,c)}},x={id:`thread-comment.mutation.update-comment`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(y),{unitId:r,subUnitId:i,payload:a,silent:o}=t;return n.updateComment(r,i,a,o)}},S={id:`thread-comment.mutation.update-comment-ref`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(y),{unitId:r,subUnitId:i,payload:a,silent:o}=t;return n.updateCommentRef(r,i,a,o)}},C={id:`thread-comment.mutation.resolve-comment`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(y),{unitId:r,subUnitId:i,resolved:a,commentId:o}=t;return n.resolveComment(r,i,o,a)}},w={id:`thread-comment.mutation.delete-comment`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(y),{unitId:r,subUnitId:i,commentId:a}=t;return n.deleteComment(r,i,a)}},T=new WeakMap;function E(e,t){let n=T.get(e)??{resolve:0,update:0},r=n[t]+1;return n[t]=r,T.set(e,n),r}function D(e,t,n){var r;return((r=T.get(e))==null?void 0:r[t])===n}let O={id:`thread-comment.command.add-comment`,type:t.CommandType.COMMAND,async handler(e,n){var r,i;if(!n)return!1;let a=e.get(t.ICommandService),o=e.get(c),s=e.get(y),{comment:l}=n,u=!l.parentId;if(!u&&((r=s.getRootComment(n.unitId,n.subUnitId,l.threadId))==null?void 0:r.id)!==l.parentId)return!1;let d={...await o.addComment(l),unitId:n.unitId,subUnitId:n.subUnitId,ref:l.ref,parentId:l.parentId,...u?{}:{threadId:l.threadId}},f=o.syncUpdateMutationToColla;if(!u&&((i=s.getRootComment(n.unitId,n.subUnitId,l.threadId))==null?void 0:i.id)!==l.parentId){try{await o.deleteComment(n.unitId,n.subUnitId,d.threadId,d.id)}catch{}return!1}let p={id:b.id,params:{...n,comment:d}};return u?await a.executeCommand(p.id,p.params):a.executeCommand(p.id,p.params,{onlyLocal:!f})}},k={id:`thread-comment.command.update-comment`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let{unitId:r,subUnitId:i,payload:a}=n,o=e.get(t.ICommandService),s=e.get(y),l=e.get(c),u=l.syncUpdateMutationToColla,d=s.getComment(r,i,a.commentId);if(!d)return!1;let{children:f,...p}=d,m=E(d,`update`);if(!await l.updateComment({...p,...a})||s.getComment(r,i,a.commentId)!==d||!D(d,`update`,m))return!1;let h={id:x.id,params:n};return o.executeCommand(h.id,h.params,{onlyLocal:!u})}},A={id:`thread-comment.command.resolve-comment`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let{unitId:r,subUnitId:i,resolved:a,commentId:o}=n,s=e.get(c),l=e.get(y),u=l.getComment(r,i,o),d=s.syncUpdateMutationToColla;if(!u)return!1;let f=E(u,`resolve`);return!await s.resolveComment({...u,resolved:a})||l.getComment(r,i,o)!==u||!D(u,`resolve`,f)?!1:e.get(t.ICommandService).executeCommand(C.id,n,{onlyLocal:!d})}},j={id:`thread-comment.command.delete-comment`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let r=e.get(y),i=e.get(c),a=e.get(t.ICommandService),{unitId:o,subUnitId:s,commentId:l}=n,u=i.syncUpdateMutationToColla,d=r.getComment(o,s,l);if(!d||!await i.deleteComment(o,s,d.threadId,l))return!1;let f={id:w.id,params:n};return a.executeCommand(f.id,f.params,{onlyLocal:!u})}},M={id:`thread-comment.command.delete-comment-tree`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let r=e.get(y),i=e.get(t.ICommandService),a=e.get(c),{unitId:o,subUnitId:s,commentId:l}=n,u=r.getCommentWithChildren(o,s,l);return!u||!(a.deleteThread?await a.deleteThread(o,s,u.root.threadId):await a.deleteComment(o,s,u.root.threadId,u.root.id))?!1:await i.executeCommand(w.id,{unitId:o,subUnitId:s,commentId:u.root.id})}};function N(e){return(0,t.dateKit)(e).format(`YYYY/MM/DD HH:mm`)}let P=[`renderedPageBreaks`,`textRuns`,`paragraphs`,`sectionBreaks`,`customBlocks`,`tables`,`columnGroups`,`blockRanges`,`customRanges`,`customDecorations`];function F(e){if(!e||typeof e!=`object`||typeof Reflect.get(e,`dataStream`)!=`string`||P.some(t=>{let n=Reflect.get(e,t);return n!==void 0&&!Array.isArray(n)}))return!1;let t=Reflect.get(e,`payloads`);return t===void 0||typeof t==`object`&&!!t&&!Array.isArray(t)&&Object.values(t).every(e=>typeof e==`string`)}function I(e){if(typeof e!=`string`){if(!F(e))throw TypeError(`Invalid thread comment content`);if(!e.dataStream.replace(/[\r\n]/g,``).trim())throw TypeError(`Thread comment content cannot be empty`);return e}if(!e.trim())throw TypeError(`Thread comment content cannot be empty`);return{dataStream:e.replace(/\r\n?|\n/g,`\r`).replace(/\r*$/,`\r
2
+ `)}}function L(e,t){return{unitId:e.unitId,subUnitId:e.subUnitId,threadId:e.threadId,root:e.root,children:e.children,anchorKind:t,anchor:g(e.root.ref),relativeUserIds:Array.from(e.relativeUsers)}}let R=class{constructor(e,t,n){this._commandService=e,this._model=t,this._userManagerService=n}createCommentAsync(e){let n=e.id??(0,t.generateRandomId)(),r=e.threadId??n,i=e.personId??this._userManagerService.getCurrentUser().userID;return this._commandService.executeCommand(O.id,{unitId:e.unitId,subUnitId:e.subUnitId,comment:{id:n,threadId:r,unitId:e.unitId,subUnitId:e.subUnitId,ref:h(e.anchor),text:I(e.content),attachments:e.attachments??[],dT:N(e.dateTime),authorName:e.authorName,personId:i}})}replyCommentAsync(e){let n=this._model.getRootComment(e.unitId,e.subUnitId,e.threadId);return n?this._commandService.executeCommand(O.id,{unitId:e.unitId,subUnitId:e.subUnitId,comment:{id:e.id??(0,t.generateRandomId)(),threadId:e.threadId,parentId:n.id,unitId:e.unitId,subUnitId:e.subUnitId,ref:n.ref,text:I(e.content),attachments:e.attachments??[],dT:N(e.dateTime),authorName:e.authorName,personId:e.personId??this._userManagerService.getCurrentUser().userID}}):Promise.resolve(!1)}updateCommentAsync(e){return this._commandService.executeCommand(k.id,{unitId:e.unitId,subUnitId:e.subUnitId,payload:{commentId:e.commentId,text:I(e.content),attachments:e.attachments,updateT:N(e.dateTime)}})}deleteCommentAsync(e){return this._commandService.executeCommand(e.deleteThread?M.id:j.id,{unitId:e.unitId,subUnitId:e.subUnitId,commentId:e.commentId})}resolveCommentAsync(e){return this._commandService.executeCommand(A.id,{unitId:e.unitId,subUnitId:e.subUnitId,commentId:e.commentId,resolved:e.resolved??!0})}getComments(e={}){return this._model.query(e).map(e=>L(e,this._model.getAnchorKind(e.unitId,e.root.ref)))}async listCommentsAsync(e={}){let t=this._model.query({unitIds:e.unitIds,subUnitIds:e.subUnitIds}),n=new Map;return t.forEach(e=>{let t=`${e.unitId}\0${e.subUnitId}`,r=n.get(t)??{unitId:e.unitId,subUnitId:e.subUnitId,threadIds:[]};r.threadIds.push(e.threadId),n.set(t,r)}),await Promise.all(Array.from(n.values()).map(e=>this._model.syncThreadComments(e.unitId,e.subUnitId,e.threadIds))),this._model.query(e).map(e=>L(e,this._model.getAnchorKind(e.unitId,e.root.ref)))}};R=v([_(0,(0,t.Inject)(t.ICommandService)),_(1,(0,t.Inject)(y)),_(2,(0,t.Inject)(t.UserManagerService))],R);function z(e){return Array.isArray(e)&&e.every(e=>typeof e==`string`)}function B(e,t=!0){if(!e||typeof e!=`object`)return!1;let n=e;return typeof n.id==`string`&&n.id.length>0&&typeof n.threadId==`string`&&n.threadId.length>0&&(typeof n.ref==`string`||!t&&n.ref===void 0)&&(n.authorName===void 0||typeof n.authorName==`string`)&&(n.text===void 0||F(n.text))&&(n.attachments===void 0||z(n.attachments))&&(n.mentions===void 0||z(n.mentions))&&(!t||n.children===void 0||Array.isArray(n.children)&&n.children.every(e=>B(e,!1)))}function V(e,t,n,r){return{pluginName:n,businesses:r,toJson:n=>{let r=e.getUnit(n),i={};return r.forEach(e=>{let t=i[e.subUnitId]??[];t.push({...e.root,children:e.children}),i[e.subUnitId]=t}),JSON.stringify(t.saveToSnapshot(i,n))},parseJson:e=>{if(!e)return{};try{let t=JSON.parse(e);return!t||typeof t!=`object`||Array.isArray(t)?{}:Object.fromEntries(Object.entries(t).flatMap(([e,t])=>Array.isArray(t)?[[e,t.filter(e=>B(e))]]:[]))}catch{return{}}},onUnLoad:t=>{e.deleteUnit(t)},onLoad:(t,n)=>{Object.keys(n).forEach(r=>{let i=n[r];i.forEach(n=>{var i;let a=new Set([n.id]),o=(i=n.children)==null?void 0:i.filter(e=>e.threadId!==n.threadId||a.has(e.id)?!1:(a.add(e.id),!0)).map(e=>({...e,unitId:t,subUnitId:r}));e.addComment(t,r,{...n,unitId:t,subUnitId:r,children:o})}),e.syncThreadComments(t,r,i.map(e=>e.threadId))})}}}var H=`@univerjs/thread-comment`,U=`1.0.1`;let W={},G=`UNIVER_THREAD_COMMENT_PLUGIN`,K=class extends t.Plugin{constructor(e=W,n,r,i){super(),this._config=e,this._injector=n,this._commandService=r,this._configService=i;let{...a}=(0,t.merge)({},W,this._config);this._configService.setConfig(`thread-comment.config`,a)}onStarting(){var e;(0,t.mergeOverrideWithDependencies)([[c,{useClass:s}],[y],[R]],(e=this._config)==null?void 0:e.overrides).forEach(e=>{this._injector.add(e)}),[O,k,j,A,M,b,x,S,w,C].forEach(e=>{this._commandService.registerCommand(e)})}};o(K,`pluginName`,G),o(K,`packageName`,H),o(K,`version`,U),o(K,`type`,t.UniverInstanceType.UNIVER_UNKNOWN),K=v([_(1,(0,t.Inject)(t.Injector)),_(2,t.ICommandService),_(3,t.IConfigService)],K),e.AddCommentCommand=O,e.AddCommentMutation=b,e.DeleteCommentCommand=j,e.DeleteCommentMutation=w,e.DeleteCommentTreeCommand=M,e.IThreadCommentDataSourceService=c,e.ResolveCommentCommand=A,e.ResolveCommentMutation=C,e.TC_PLUGIN_NAME=G,e.ThreadCommentAnchorKind=l,e.ThreadCommentDataSourceService=s,Object.defineProperty(e,"ThreadCommentFacadeService",{enumerable:!0,get:function(){return R}}),Object.defineProperty(e,"ThreadCommentModel",{enumerable:!0,get:function(){return y}}),Object.defineProperty(e,"UniverThreadCommentPlugin",{enumerable:!0,get:function(){return K}}),e.UpdateCommentCommand=k,e.UpdateCommentMutation=x,e.UpdateCommentRefMutation=S,e.createThreadCommentResourceHook=V,e.deserializeThreadCommentAnchor=g,e.getDT=N,e.isThreadCommentAnchor=m,e.isThreadCommentDocumentBody=F,e.normalizeThreadCommentContent=I,e.serializeThreadCommentAnchor=h});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/thread-comment",
3
- "version": "1.0.0-rc.0",
3
+ "version": "1.0.1",
4
4
  "private": false,
5
5
  "description": "Shared thread comment models, commands, and services for Univer.",
6
6
  "author": "DreamNum Co., Ltd. <developer@univer.ai>",
@@ -62,13 +62,13 @@
62
62
  "rxjs": ">=7.0.0"
63
63
  },
64
64
  "dependencies": {
65
- "@univerjs/core": "1.0.0-rc.0"
65
+ "@univerjs/core": "1.0.1"
66
66
  },
67
67
  "devDependencies": {
68
+ "@univerjs-infra/shared": "1.0.1",
68
69
  "rxjs": "^7.8.2",
69
70
  "typescript": "^6.0.3",
70
- "vitest": "^5.0.0",
71
- "@univerjs-infra/shared": "1.0.0-rc.0"
71
+ "vitest": "^5.0.0"
72
72
  },
73
73
  "scripts": {
74
74
  "test": "vitest run",