@univerjs/thread-comment 1.5.2 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/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
- // \0@oxc-project+runtime@0.157.0/helpers/esm/typeof.js
5
+ // \0@oxc-project+runtime@0.158.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) {
@@ -12,7 +12,7 @@ function _typeof(o) {
12
12
  }, _typeof(o);
13
13
  }
14
14
 
15
- // \0@oxc-project+runtime@0.157.0/helpers/esm/toPrimitive.js
15
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/toPrimitive.js
16
16
  function toPrimitive(t, e) {
17
17
  if ("object" != _typeof(t) || !t) return t;
18
18
  var r;
@@ -24,13 +24,13 @@ function toPrimitive(t, e) {
24
24
  return ("string" === e ? String : Number)(t);
25
25
  }
26
26
 
27
- // \0@oxc-project+runtime@0.157.0/helpers/esm/toPropertyKey.js
27
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/toPropertyKey.js
28
28
  function toPropertyKey(t) {
29
29
  var i = toPrimitive(t, "string");
30
30
  return "symbol" == _typeof(i) ? i : i + "";
31
31
  }
32
32
 
33
- // \0@oxc-project+runtime@0.157.0/helpers/esm/defineProperty.js
33
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/defineProperty.js
34
34
  function _defineProperty(e, r, t) {
35
35
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
36
36
  value: t,
@@ -192,14 +192,14 @@ function deserializeThreadCommentAnchor(ref) {
192
192
  }
193
193
  }
194
194
 
195
- // \0@oxc-project+runtime@0.157.0/helpers/esm/decorateParam.js
195
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/decorateParam.js
196
196
  function __decorateParam(paramIndex, decorator) {
197
197
  return function(target, key) {
198
198
  decorator(target, key, paramIndex);
199
199
  };
200
200
  }
201
201
 
202
- // \0@oxc-project+runtime@0.157.0/helpers/esm/decorate.js
202
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/decorate.js
203
203
  function __decorate(decorators, target, key, desc) {
204
204
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
205
205
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -1062,13 +1062,48 @@ function createThreadCommentResourceHook(threadCommentModel, threadCommentDataSo
1062
1062
 
1063
1063
  // package.json
1064
1064
  var name = "@univerjs/thread-comment";
1065
- var version = "1.5.2";
1065
+ var version = "1.6.0";
1066
1066
 
1067
1067
  // src/config/config.ts
1068
1068
  const THREAD_COMMENT_PLUGIN_CONFIG_KEY = "thread-comment.config";
1069
1069
  const configSymbol = Symbol(THREAD_COMMENT_PLUGIN_CONFIG_KEY);
1070
1070
  const defaultPluginConfig = {};
1071
1071
 
1072
+ // src/services/tc-authz.service.ts
1073
+ let ThreadCommentAuthzService = class ThreadCommentAuthzService extends _univerjs_core.Disposable {
1074
+ constructor(_instanceService, _userManagerService) {
1075
+ super();
1076
+ this._instanceService = _instanceService;
1077
+ this._userManagerService = _userManagerService;
1078
+ _defineProperty(this, "_authzByType", /* @__PURE__ */ new Map());
1079
+ }
1080
+ registerAuthz(type, authz) {
1081
+ this._authzByType.set(type, authz);
1082
+ return { dispose: () => {
1083
+ if (this._authzByType.get(type) === authz) this._authzByType.delete(type);
1084
+ } };
1085
+ }
1086
+ canRead(target) {
1087
+ var _this$_resolve;
1088
+ return ((_this$_resolve = this._resolve(target)) === null || _this$_resolve === void 0 ? void 0 : _this$_resolve.canRead(target)) ?? true;
1089
+ }
1090
+ canWrite(action, target) {
1091
+ const authz = this._resolve(target);
1092
+ if (authz && !authz.canWrite(action, target)) return false;
1093
+ if (action === "update" || action === "delete") return this._isAuthor(target);
1094
+ return true;
1095
+ }
1096
+ _resolve(target) {
1097
+ return this._authzByType.get(this._instanceService.getUnitType(target.unitId));
1098
+ }
1099
+ _isAuthor(target) {
1100
+ var _this$_userManagerSer;
1101
+ return ((_this$_userManagerSer = this._userManagerService.getCurrentUser()) === null || _this$_userManagerSer === void 0 ? void 0 : _this$_userManagerSer.userID) === target.personId;
1102
+ }
1103
+ };
1104
+ ThreadCommentAuthzService = __decorate([__decorateParam(0, _univerjs_core.IUniverInstanceService), __decorateParam(1, (0, _univerjs_core.Inject)(_univerjs_core.UserManagerService))], ThreadCommentAuthzService);
1105
+ const IThreadCommentAuthzService = (0, _univerjs_core.createIdentifier)("univer.thread-comment.authz-service");
1106
+
1072
1107
  // src/types/const/index.ts
1073
1108
  /**
1074
1109
  * Copyright 2023-present DreamNum Co., Ltd.
@@ -1102,6 +1137,7 @@ let UniverThreadCommentPlugin = class UniverThreadCommentPlugin extends _univerj
1102
1137
  var _this$_config;
1103
1138
  (0, _univerjs_core.mergeOverrideWithDependencies)([
1104
1139
  [IThreadCommentDataSourceService, { useClass: ThreadCommentDataSourceService }],
1140
+ [IThreadCommentAuthzService, { useClass: ThreadCommentAuthzService }],
1105
1141
  [ThreadCommentModel],
1106
1142
  [ThreadCommentFacadeService]
1107
1143
  ], (_this$_config = this._config) === null || _this$_config === void 0 ? void 0 : _this$_config.overrides).forEach((d) => {
@@ -1138,11 +1174,18 @@ exports.AddCommentMutation = AddCommentMutation;
1138
1174
  exports.DeleteCommentCommand = DeleteCommentCommand;
1139
1175
  exports.DeleteCommentMutation = DeleteCommentMutation;
1140
1176
  exports.DeleteCommentTreeCommand = DeleteCommentTreeCommand;
1177
+ exports.IThreadCommentAuthzService = IThreadCommentAuthzService;
1141
1178
  exports.IThreadCommentDataSourceService = IThreadCommentDataSourceService;
1142
1179
  exports.ResolveCommentCommand = ResolveCommentCommand;
1143
1180
  exports.ResolveCommentMutation = ResolveCommentMutation;
1144
1181
  exports.TC_PLUGIN_NAME = TC_PLUGIN_NAME;
1145
1182
  exports.ThreadCommentAnchorKind = ThreadCommentAnchorKind;
1183
+ Object.defineProperty(exports, "ThreadCommentAuthzService", {
1184
+ enumerable: true,
1185
+ get: function () {
1186
+ return ThreadCommentAuthzService;
1187
+ }
1188
+ });
1146
1189
  exports.ThreadCommentDataSourceService = ThreadCommentDataSourceService;
1147
1190
  Object.defineProperty(exports, "ThreadCommentFacadeService", {
1148
1191
  enumerable: true,
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
- // \0@oxc-project+runtime@0.157.0/helpers/esm/typeof.js
4
+ // \0@oxc-project+runtime@0.158.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) {
@@ -11,7 +11,7 @@ function _typeof(o) {
11
11
  }, _typeof(o);
12
12
  }
13
13
 
14
- // \0@oxc-project+runtime@0.157.0/helpers/esm/toPrimitive.js
14
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/toPrimitive.js
15
15
  function toPrimitive(t, e) {
16
16
  if ("object" != _typeof(t) || !t) return t;
17
17
  var r;
@@ -23,13 +23,13 @@ function toPrimitive(t, e) {
23
23
  return ("string" === e ? String : Number)(t);
24
24
  }
25
25
 
26
- // \0@oxc-project+runtime@0.157.0/helpers/esm/toPropertyKey.js
26
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/toPropertyKey.js
27
27
  function toPropertyKey(t) {
28
28
  var i = toPrimitive(t, "string");
29
29
  return "symbol" == _typeof(i) ? i : i + "";
30
30
  }
31
31
 
32
- // \0@oxc-project+runtime@0.157.0/helpers/esm/defineProperty.js
32
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/defineProperty.js
33
33
  function _defineProperty(e, r, t) {
34
34
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
35
35
  value: t,
@@ -191,14 +191,14 @@ function deserializeThreadCommentAnchor(ref) {
191
191
  }
192
192
  }
193
193
 
194
- // \0@oxc-project+runtime@0.157.0/helpers/esm/decorateParam.js
194
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/decorateParam.js
195
195
  function __decorateParam(paramIndex, decorator) {
196
196
  return function(target, key) {
197
197
  decorator(target, key, paramIndex);
198
198
  };
199
199
  }
200
200
 
201
- // \0@oxc-project+runtime@0.157.0/helpers/esm/decorate.js
201
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/decorate.js
202
202
  function __decorate(decorators, target, key, desc) {
203
203
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
204
204
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -1061,13 +1061,48 @@ function createThreadCommentResourceHook(threadCommentModel, threadCommentDataSo
1061
1061
 
1062
1062
  // package.json
1063
1063
  var name = "@univerjs/thread-comment";
1064
- var version = "1.5.2";
1064
+ var version = "1.6.0";
1065
1065
 
1066
1066
  // src/config/config.ts
1067
1067
  const THREAD_COMMENT_PLUGIN_CONFIG_KEY = "thread-comment.config";
1068
1068
  const configSymbol = Symbol(THREAD_COMMENT_PLUGIN_CONFIG_KEY);
1069
1069
  const defaultPluginConfig = {};
1070
1070
 
1071
+ // src/services/tc-authz.service.ts
1072
+ let ThreadCommentAuthzService = class ThreadCommentAuthzService extends Disposable {
1073
+ constructor(_instanceService, _userManagerService) {
1074
+ super();
1075
+ this._instanceService = _instanceService;
1076
+ this._userManagerService = _userManagerService;
1077
+ _defineProperty(this, "_authzByType", /* @__PURE__ */ new Map());
1078
+ }
1079
+ registerAuthz(type, authz) {
1080
+ this._authzByType.set(type, authz);
1081
+ return { dispose: () => {
1082
+ if (this._authzByType.get(type) === authz) this._authzByType.delete(type);
1083
+ } };
1084
+ }
1085
+ canRead(target) {
1086
+ var _this$_resolve;
1087
+ return ((_this$_resolve = this._resolve(target)) === null || _this$_resolve === void 0 ? void 0 : _this$_resolve.canRead(target)) ?? true;
1088
+ }
1089
+ canWrite(action, target) {
1090
+ const authz = this._resolve(target);
1091
+ if (authz && !authz.canWrite(action, target)) return false;
1092
+ if (action === "update" || action === "delete") return this._isAuthor(target);
1093
+ return true;
1094
+ }
1095
+ _resolve(target) {
1096
+ return this._authzByType.get(this._instanceService.getUnitType(target.unitId));
1097
+ }
1098
+ _isAuthor(target) {
1099
+ var _this$_userManagerSer;
1100
+ return ((_this$_userManagerSer = this._userManagerService.getCurrentUser()) === null || _this$_userManagerSer === void 0 ? void 0 : _this$_userManagerSer.userID) === target.personId;
1101
+ }
1102
+ };
1103
+ ThreadCommentAuthzService = __decorate([__decorateParam(0, IUniverInstanceService), __decorateParam(1, Inject(UserManagerService))], ThreadCommentAuthzService);
1104
+ const IThreadCommentAuthzService = createIdentifier("univer.thread-comment.authz-service");
1105
+
1071
1106
  // src/types/const/index.ts
1072
1107
  /**
1073
1108
  * Copyright 2023-present DreamNum Co., Ltd.
@@ -1101,6 +1136,7 @@ let UniverThreadCommentPlugin = class UniverThreadCommentPlugin extends Plugin {
1101
1136
  var _this$_config;
1102
1137
  mergeOverrideWithDependencies([
1103
1138
  [IThreadCommentDataSourceService, { useClass: ThreadCommentDataSourceService }],
1139
+ [IThreadCommentAuthzService, { useClass: ThreadCommentAuthzService }],
1104
1140
  [ThreadCommentModel],
1105
1141
  [ThreadCommentFacadeService]
1106
1142
  ], (_this$_config = this._config) === null || _this$_config === void 0 ? void 0 : _this$_config.overrides).forEach((d) => {
@@ -1132,4 +1168,4 @@ UniverThreadCommentPlugin = __decorate([
1132
1168
  __decorateParam(3, IConfigService)
1133
1169
  ], UniverThreadCommentPlugin);
1134
1170
 
1135
- export { AddCommentCommand, AddCommentMutation, DeleteCommentCommand, DeleteCommentMutation, DeleteCommentTreeCommand, IThreadCommentDataSourceService, ResolveCommentCommand, ResolveCommentMutation, TC_PLUGIN_NAME, ThreadCommentAnchorKind, ThreadCommentDataSourceService, ThreadCommentFacadeService, ThreadCommentModel, UniverThreadCommentPlugin, UpdateCommentCommand, UpdateCommentMutation, UpdateCommentRefMutation, createThreadCommentResourceHook, deserializeThreadCommentAnchor, getDT, isThreadCommentAnchor, isThreadCommentDocumentBody, normalizeThreadCommentContent, serializeThreadCommentAnchor };
1171
+ export { AddCommentCommand, AddCommentMutation, DeleteCommentCommand, DeleteCommentMutation, DeleteCommentTreeCommand, IThreadCommentAuthzService, IThreadCommentDataSourceService, ResolveCommentCommand, ResolveCommentMutation, TC_PLUGIN_NAME, ThreadCommentAnchorKind, ThreadCommentAuthzService, ThreadCommentDataSourceService, ThreadCommentFacadeService, ThreadCommentModel, UniverThreadCommentPlugin, UpdateCommentCommand, UpdateCommentMutation, UpdateCommentRefMutation, createThreadCommentResourceHook, deserializeThreadCommentAnchor, getDT, isThreadCommentAnchor, isThreadCommentDocumentBody, normalizeThreadCommentContent, serializeThreadCommentAnchor };
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
- // \0@oxc-project+runtime@0.157.0/helpers/esm/typeof.js
4
+ // \0@oxc-project+runtime@0.158.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) {
@@ -11,7 +11,7 @@ function _typeof(o) {
11
11
  }, _typeof(o);
12
12
  }
13
13
 
14
- // \0@oxc-project+runtime@0.157.0/helpers/esm/toPrimitive.js
14
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/toPrimitive.js
15
15
  function toPrimitive(t, e) {
16
16
  if ("object" != _typeof(t) || !t) return t;
17
17
  var r;
@@ -23,13 +23,13 @@ function toPrimitive(t, e) {
23
23
  return ("string" === e ? String : Number)(t);
24
24
  }
25
25
 
26
- // \0@oxc-project+runtime@0.157.0/helpers/esm/toPropertyKey.js
26
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/toPropertyKey.js
27
27
  function toPropertyKey(t) {
28
28
  var i = toPrimitive(t, "string");
29
29
  return "symbol" == _typeof(i) ? i : i + "";
30
30
  }
31
31
 
32
- // \0@oxc-project+runtime@0.157.0/helpers/esm/defineProperty.js
32
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/defineProperty.js
33
33
  function _defineProperty(e, r, t) {
34
34
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
35
35
  value: t,
@@ -191,14 +191,14 @@ function deserializeThreadCommentAnchor(ref) {
191
191
  }
192
192
  }
193
193
 
194
- // \0@oxc-project+runtime@0.157.0/helpers/esm/decorateParam.js
194
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/decorateParam.js
195
195
  function __decorateParam(paramIndex, decorator) {
196
196
  return function(target, key) {
197
197
  decorator(target, key, paramIndex);
198
198
  };
199
199
  }
200
200
 
201
- // \0@oxc-project+runtime@0.157.0/helpers/esm/decorate.js
201
+ // \0@oxc-project+runtime@0.158.0/helpers/esm/decorate.js
202
202
  function __decorate(decorators, target, key, desc) {
203
203
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
204
204
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -1061,13 +1061,48 @@ function createThreadCommentResourceHook(threadCommentModel, threadCommentDataSo
1061
1061
 
1062
1062
  // package.json
1063
1063
  var name = "@univerjs/thread-comment";
1064
- var version = "1.5.2";
1064
+ var version = "1.6.0";
1065
1065
 
1066
1066
  // src/config/config.ts
1067
1067
  const THREAD_COMMENT_PLUGIN_CONFIG_KEY = "thread-comment.config";
1068
1068
  const configSymbol = Symbol(THREAD_COMMENT_PLUGIN_CONFIG_KEY);
1069
1069
  const defaultPluginConfig = {};
1070
1070
 
1071
+ // src/services/tc-authz.service.ts
1072
+ let ThreadCommentAuthzService = class ThreadCommentAuthzService extends Disposable {
1073
+ constructor(_instanceService, _userManagerService) {
1074
+ super();
1075
+ this._instanceService = _instanceService;
1076
+ this._userManagerService = _userManagerService;
1077
+ _defineProperty(this, "_authzByType", /* @__PURE__ */ new Map());
1078
+ }
1079
+ registerAuthz(type, authz) {
1080
+ this._authzByType.set(type, authz);
1081
+ return { dispose: () => {
1082
+ if (this._authzByType.get(type) === authz) this._authzByType.delete(type);
1083
+ } };
1084
+ }
1085
+ canRead(target) {
1086
+ var _this$_resolve;
1087
+ return ((_this$_resolve = this._resolve(target)) === null || _this$_resolve === void 0 ? void 0 : _this$_resolve.canRead(target)) ?? true;
1088
+ }
1089
+ canWrite(action, target) {
1090
+ const authz = this._resolve(target);
1091
+ if (authz && !authz.canWrite(action, target)) return false;
1092
+ if (action === "update" || action === "delete") return this._isAuthor(target);
1093
+ return true;
1094
+ }
1095
+ _resolve(target) {
1096
+ return this._authzByType.get(this._instanceService.getUnitType(target.unitId));
1097
+ }
1098
+ _isAuthor(target) {
1099
+ var _this$_userManagerSer;
1100
+ return ((_this$_userManagerSer = this._userManagerService.getCurrentUser()) === null || _this$_userManagerSer === void 0 ? void 0 : _this$_userManagerSer.userID) === target.personId;
1101
+ }
1102
+ };
1103
+ ThreadCommentAuthzService = __decorate([__decorateParam(0, IUniverInstanceService), __decorateParam(1, Inject(UserManagerService))], ThreadCommentAuthzService);
1104
+ const IThreadCommentAuthzService = createIdentifier("univer.thread-comment.authz-service");
1105
+
1071
1106
  // src/types/const/index.ts
1072
1107
  /**
1073
1108
  * Copyright 2023-present DreamNum Co., Ltd.
@@ -1101,6 +1136,7 @@ let UniverThreadCommentPlugin = class UniverThreadCommentPlugin extends Plugin {
1101
1136
  var _this$_config;
1102
1137
  mergeOverrideWithDependencies([
1103
1138
  [IThreadCommentDataSourceService, { useClass: ThreadCommentDataSourceService }],
1139
+ [IThreadCommentAuthzService, { useClass: ThreadCommentAuthzService }],
1104
1140
  [ThreadCommentModel],
1105
1141
  [ThreadCommentFacadeService]
1106
1142
  ], (_this$_config = this._config) === null || _this$_config === void 0 ? void 0 : _this$_config.overrides).forEach((d) => {
@@ -1132,4 +1168,4 @@ UniverThreadCommentPlugin = __decorate([
1132
1168
  __decorateParam(3, IConfigService)
1133
1169
  ], UniverThreadCommentPlugin);
1134
1170
 
1135
- export { AddCommentCommand, AddCommentMutation, DeleteCommentCommand, DeleteCommentMutation, DeleteCommentTreeCommand, IThreadCommentDataSourceService, ResolveCommentCommand, ResolveCommentMutation, TC_PLUGIN_NAME, ThreadCommentAnchorKind, ThreadCommentDataSourceService, ThreadCommentFacadeService, ThreadCommentModel, UniverThreadCommentPlugin, UpdateCommentCommand, UpdateCommentMutation, UpdateCommentRefMutation, createThreadCommentResourceHook, deserializeThreadCommentAnchor, getDT, isThreadCommentAnchor, isThreadCommentDocumentBody, normalizeThreadCommentContent, serializeThreadCommentAnchor };
1171
+ export { AddCommentCommand, AddCommentMutation, DeleteCommentCommand, DeleteCommentMutation, DeleteCommentTreeCommand, IThreadCommentAuthzService, IThreadCommentDataSourceService, ResolveCommentCommand, ResolveCommentMutation, TC_PLUGIN_NAME, ThreadCommentAnchorKind, ThreadCommentAuthzService, ThreadCommentDataSourceService, ThreadCommentFacadeService, ThreadCommentModel, UniverThreadCommentPlugin, UpdateCommentCommand, UpdateCommentMutation, UpdateCommentRefMutation, createThreadCommentResourceHook, deserializeThreadCommentAnchor, getDT, isThreadCommentAnchor, isThreadCommentDocumentBody, normalizeThreadCommentContent, serializeThreadCommentAnchor };
@@ -21,7 +21,7 @@ export interface IFUniverThreadCommentMixin {
21
21
  * Creates a root comment on a drawing, page element, page position, or Base record anchor.
22
22
  * For a sheet-cell comment, use `FRange.addCommentAsync`; for document text, use `FDocumentTextRange.createCommentAsync`.
23
23
  * @param options Comment content, owner IDs, stable anchor, and optional caller-controlled IDs.
24
- * @returns {Promise<boolean>} `true` when the command succeeds; otherwise, `false`.
24
+ * @returns {Promise<boolean>} `true` when the command succeeds; otherwise, `false`. Rejects when a host permission interceptor denies the write.
25
25
  * @throws {TypeError} If the content is empty or the anchor is invalid.
26
26
  * @example
27
27
  * ```ts
@@ -50,7 +50,7 @@ export interface IFUniverThreadCommentMixin {
50
50
  /**
51
51
  * Adds a reply to an existing root thread.
52
52
  * @param options Reply content and the owning unit, subunit, and thread IDs.
53
- * @returns {Promise<boolean>} `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. Rejects when a host permission interceptor denies the write.
54
54
  * @throws {TypeError} If the content is empty.
55
55
  * @example
56
56
  * ```ts
@@ -69,7 +69,7 @@ export interface IFUniverThreadCommentMixin {
69
69
  /**
70
70
  * Updates the content or attachments of an existing root comment or reply.
71
71
  * @param options Updated content and the owning unit, subunit, and comment IDs.
72
- * @returns {Promise<boolean>} `true` when the update command succeeds; otherwise, `false`.
72
+ * @returns {Promise<boolean>} `true` when the update command succeeds; otherwise, `false`. Rejects when a host permission interceptor denies the write.
73
73
  * @throws {TypeError} If the content is empty.
74
74
  * @example
75
75
  * ```ts
@@ -88,7 +88,7 @@ export interface IFUniverThreadCommentMixin {
88
88
  /**
89
89
  * Deletes one comment, or the complete root and reply tree when `deleteThread` is `true`.
90
90
  * @param options Owning IDs, target comment ID, and the optional whole-thread flag.
91
- * @returns {Promise<boolean>} `true` when the delete command succeeds; otherwise, `false`.
91
+ * @returns {Promise<boolean>} `true` when the delete command succeeds; otherwise, `false`. Rejects when a host permission interceptor denies the write.
92
92
  * @example
93
93
  * ```ts
94
94
  * const [comment] = univerAPI.getComments({ resolved: false });
@@ -106,7 +106,7 @@ export interface IFUniverThreadCommentMixin {
106
106
  /**
107
107
  * Resolves a thread. Pass `resolved: false` to reopen it.
108
108
  * @param options Owning IDs, a comment ID in the thread, and the desired resolution state.
109
- * @returns {Promise<boolean>} `true` when the resolve command succeeds; otherwise, `false`.
109
+ * @returns {Promise<boolean>} `true` when the resolve command succeeds; otherwise, `false`. Rejects when a host permission interceptor denies the write.
110
110
  * @example
111
111
  * ```ts
112
112
  * const [comment] = univerAPI.getComments({ resolved: false });
@@ -23,6 +23,8 @@ export type { IUniverThreadCommentConfig } from './config/config';
23
23
  export { ThreadCommentModel } from './models/thread-comment.model';
24
24
  export type { CommentUpdate, IThreadCommentQuery, IThreadInfo } from './models/thread-comment.model';
25
25
  export { UniverThreadCommentPlugin } from './plugin';
26
+ export { IThreadCommentAuthzService, ThreadCommentAuthzService } from './services/tc-authz.service';
27
+ export type { IThreadCommentAuthz, IThreadCommentAuthzTarget, ThreadCommentWriteAction } from './services/tc-authz.service';
26
28
  export { IThreadCommentDataSourceService, ThreadCommentDataSourceService } from './services/tc-datasource.service';
27
29
  export type { IThreadCommentDataSource, ThreadCommentJSON } from './services/tc-datasource.service';
28
30
  export { isThreadCommentDocumentBody, normalizeThreadCommentContent, ThreadCommentFacadeService } from './services/thread-comment-api.service';
@@ -13,8 +13,8 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import type { IUniverThreadCommentConfig } from './config/config';
17
16
  import { ICommandService, IConfigService, Injector, Plugin, UniverInstanceType } from '@univerjs/core';
17
+ import type { IUniverThreadCommentConfig } from './config/config';
18
18
  export declare class UniverThreadCommentPlugin extends Plugin {
19
19
  private readonly _config;
20
20
  protected _injector: Injector;
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Copyright 2023-present DreamNum Co., Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import type { IDisposable, UniverInstanceType } from '@univerjs/core';
17
+ import { Disposable, IUniverInstanceService, UserManagerService } from '@univerjs/core';
18
+ /**
19
+ * The subset of a comment that permission checks depend on. Callers may pass a
20
+ * full `IThreadComment`; a location without a comment can also be checked with
21
+ * only `unitId`, `subUnitId` and `ref`.
22
+ */
23
+ export interface IThreadCommentAuthzTarget {
24
+ unitId: string;
25
+ subUnitId: string;
26
+ ref: string;
27
+ personId?: string;
28
+ }
29
+ export type ThreadCommentWriteAction = 'add' | 'reply' | 'update' | 'resolve' | 'delete';
30
+ /**
31
+ * Platform-specific comment permission rules, such as worksheet permissions for
32
+ * sheets or document permissions for docs. Implementations are registered per
33
+ * unit type; unit types without an implementation are not restricted.
34
+ */
35
+ export interface IThreadCommentAuthz {
36
+ canRead(target: IThreadCommentAuthzTarget): boolean;
37
+ canWrite(action: ThreadCommentWriteAction, target: IThreadCommentAuthzTarget): boolean;
38
+ }
39
+ export interface IThreadCommentAuthzService {
40
+ registerAuthz(type: UniverInstanceType, authz: IThreadCommentAuthz): IDisposable;
41
+ canRead(target: IThreadCommentAuthzTarget): boolean;
42
+ canWrite(action: ThreadCommentWriteAction, target: IThreadCommentAuthzTarget): boolean;
43
+ }
44
+ export declare class ThreadCommentAuthzService extends Disposable implements IThreadCommentAuthzService {
45
+ private readonly _instanceService;
46
+ private readonly _userManagerService;
47
+ private readonly _authzByType;
48
+ constructor(_instanceService: IUniverInstanceService, _userManagerService: UserManagerService);
49
+ registerAuthz(type: UniverInstanceType, authz: IThreadCommentAuthz): IDisposable;
50
+ canRead(target: IThreadCommentAuthzTarget): boolean;
51
+ canWrite(action: ThreadCommentWriteAction, target: IThreadCommentAuthzTarget): boolean;
52
+ private _resolve;
53
+ private _isAuthor;
54
+ }
55
+ export declare const IThreadCommentAuthzService: import("@univerjs/core").IdentifierDecorator<IThreadCommentAuthzService>;
package/lib/umd/index.js CHANGED
@@ -1,2 +1,2 @@
1
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;if(typeof Symbol<`u`&&(n=e[Symbol.toPrimitive])!==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`,/* @__PURE__ */ 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=/* @__PURE__ */ 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`,/* @__PURE__ */ new Map),o(this,`_threadMap`,/* @__PURE__ */ new Map),o(this,`_syncVersions`,/* @__PURE__ */ 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=/* @__PURE__ */ new Map;e===t.LifecycleStages.Rendered&&(this._tasks.forEach(({unitId:e,subUnitId:t,threadIds:r})=>{let i=n.get(e);i||(i=/* @__PURE__ */ new Map,n.set(e,i));let a=i.get(t);a||(a=/* @__PURE__ */ 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=/* @__PURE__ */ new Map,this._commentsMap.set(e,n));let r=n.get(t);return r||(r=/* @__PURE__ */ 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=/* @__PURE__ */ new Map,this._threadMap.set(e,n));let r=n.get(t);return r||(r=/* @__PURE__ */ 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=/* @__PURE__ */ 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=/* @__PURE__ */ 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=/* @__PURE__ */ 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=/* @__PURE__ */ new Map;t.forEach(t=>{let n=i.get(t.threadId);n||(n={unitId:e,subUnitId:r,threadId:t.threadId,children:[],relativeUsers:/* @__PURE__ */ 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=/* @__PURE__ */ 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=/* @__PURE__ */ 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=/* @__PURE__ */ 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.5.2`;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});
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=/* @__PURE__ */ 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=/* @__PURE__ */ 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.6.0`;let W={},G=class extends t.Disposable{constructor(e,t){super(),this._instanceService=e,this._userManagerService=t,o(this,`_authzByType`,/* @__PURE__ */ new Map)}registerAuthz(e,t){return this._authzByType.set(e,t),{dispose:()=>{this._authzByType.get(e)===t&&this._authzByType.delete(e)}}}canRead(e){var t;return((t=this._resolve(e))==null?void 0:t.canRead(e))??!0}canWrite(e,t){let n=this._resolve(t);return n&&!n.canWrite(e,t)?!1:e===`update`||e===`delete`?this._isAuthor(t):!0}_resolve(e){return this._authzByType.get(this._instanceService.getUnitType(e.unitId))}_isAuthor(e){var t;return((t=this._userManagerService.getCurrentUser())==null?void 0:t.userID)===e.personId}};G=v([_(0,t.IUniverInstanceService),_(1,(0,t.Inject)(t.UserManagerService))],G);let K=(0,t.createIdentifier)(`univer.thread-comment.authz-service`),q=`UNIVER_THREAD_COMMENT_PLUGIN`,J=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}],[K,{useClass:G}],[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(J,`pluginName`,q),o(J,`packageName`,H),o(J,`version`,U),o(J,`type`,t.UniverInstanceType.UNIVER_UNKNOWN),J=v([_(1,(0,t.Inject)(t.Injector)),_(2,t.ICommandService),_(3,t.IConfigService)],J),e.AddCommentCommand=O,e.AddCommentMutation=b,e.DeleteCommentCommand=j,e.DeleteCommentMutation=w,e.DeleteCommentTreeCommand=M,e.IThreadCommentAuthzService=K,e.IThreadCommentDataSourceService=c,e.ResolveCommentCommand=A,e.ResolveCommentMutation=C,e.TC_PLUGIN_NAME=q,e.ThreadCommentAnchorKind=l,Object.defineProperty(e,"ThreadCommentAuthzService",{enumerable:!0,get:function(){return G}}),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 J}}),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.5.2",
3
+ "version": "1.6.0",
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.5.2"
65
+ "@univerjs/core": "1.6.0"
66
66
  },
67
67
  "devDependencies": {
68
- "@univerjs-infra/shared": "1.5.2",
68
+ "@univerjs-infra/shared": "1.6.0",
69
69
  "rxjs": "7.8.2",
70
- "tsrs": "0.23.0-ts7.1.0-beta",
71
- "vitest": "^5.1.0"
70
+ "tsrs": "7.1.0-beta.tsrs2",
71
+ "vitest": "^5.1.1"
72
72
  },
73
73
  "scripts": {
74
74
  "test": "vitest run",