@univerjs/thread-comment 1.0.4 → 1.2.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
- //#region \0@oxc-project+runtime@0.149.0/helpers/esm/typeof.js
5
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/toPrimitive.js
16
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/toPropertyKey.js
29
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/defineProperty.js
36
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/decorateParam.js
201
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/decorate.js
209
+ //#region \0@oxc-project+runtime@0.153.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);
@@ -1076,7 +1076,7 @@ function createThreadCommentResourceHook(threadCommentModel, threadCommentDataSo
1076
1076
  //#endregion
1077
1077
  //#region package.json
1078
1078
  var name = "@univerjs/thread-comment";
1079
- var version = "1.0.4";
1079
+ var version = "1.2.0";
1080
1080
 
1081
1081
  //#endregion
1082
1082
  //#region src/config/config.ts
@@ -1162,19 +1162,19 @@ exports.ResolveCommentMutation = ResolveCommentMutation;
1162
1162
  exports.TC_PLUGIN_NAME = TC_PLUGIN_NAME;
1163
1163
  exports.ThreadCommentAnchorKind = ThreadCommentAnchorKind;
1164
1164
  exports.ThreadCommentDataSourceService = ThreadCommentDataSourceService;
1165
- Object.defineProperty(exports, 'ThreadCommentFacadeService', {
1165
+ Object.defineProperty(exports, "ThreadCommentFacadeService", {
1166
1166
  enumerable: true,
1167
1167
  get: function () {
1168
1168
  return ThreadCommentFacadeService;
1169
1169
  }
1170
1170
  });
1171
- Object.defineProperty(exports, 'ThreadCommentModel', {
1171
+ Object.defineProperty(exports, "ThreadCommentModel", {
1172
1172
  enumerable: true,
1173
1173
  get: function () {
1174
1174
  return ThreadCommentModel;
1175
1175
  }
1176
1176
  });
1177
- Object.defineProperty(exports, 'UniverThreadCommentPlugin', {
1177
+ Object.defineProperty(exports, "UniverThreadCommentPlugin", {
1178
1178
  enumerable: true,
1179
1179
  get: function () {
1180
1180
  return UniverThreadCommentPlugin;
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.149.0/helpers/esm/typeof.js
4
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/toPrimitive.js
15
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/toPropertyKey.js
28
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/defineProperty.js
35
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/decorateParam.js
200
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/decorate.js
208
+ //#region \0@oxc-project+runtime@0.153.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);
@@ -1075,7 +1075,7 @@ function createThreadCommentResourceHook(threadCommentModel, threadCommentDataSo
1075
1075
  //#endregion
1076
1076
  //#region package.json
1077
1077
  var name = "@univerjs/thread-comment";
1078
- var version = "1.0.4";
1078
+ var version = "1.2.0";
1079
1079
 
1080
1080
  //#endregion
1081
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.149.0/helpers/esm/typeof.js
4
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/toPrimitive.js
15
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/toPropertyKey.js
28
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/defineProperty.js
35
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/decorateParam.js
200
+ //#region \0@oxc-project+runtime@0.153.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.149.0/helpers/esm/decorate.js
208
+ //#region \0@oxc-project+runtime@0.153.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);
@@ -1075,7 +1075,7 @@ function createThreadCommentResourceHook(threadCommentModel, threadCommentDataSo
1075
1075
  //#endregion
1076
1076
  //#region package.json
1077
1077
  var name = "@univerjs/thread-comment";
1078
- var version = "1.0.4";
1078
+ var version = "1.2.0";
1079
1079
 
1080
1080
  //#endregion
1081
1081
  //#region src/config/config.ts
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,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.4`;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`,/* @__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.2.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});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/thread-comment",
3
- "version": "1.0.4",
3
+ "version": "1.2.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.0.4"
65
+ "@univerjs/core": "1.2.0"
66
66
  },
67
67
  "devDependencies": {
68
- "@univerjs-infra/shared": "1.0.4",
69
- "rxjs": "^7.8.2",
68
+ "@univerjs-infra/shared": "1.2.0",
69
+ "rxjs": "7.8.2",
70
70
  "typescript": "^6.0.3",
71
- "vitest": "^5.0.0"
71
+ "vitest": "^5.0.4"
72
72
  },
73
73
  "scripts": {
74
74
  "test": "vitest run",