@univerjs/thread-comment 0.20.1 → 0.21.0-insiders.20260422-d7fcb4d

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/es/index.js CHANGED
@@ -1 +1,740 @@
1
- import{CommandType as e,Disposable as t,ICommandService as n,IConfigService as r,IResourceManagerService as i,Inject as a,Injector as o,LifecycleService as s,LifecycleStages as c,Plugin as l,UniverInstanceType as u,createIdentifier as d,dateKit as f,merge as p,mergeOverrideWithDependencies as m}from"@univerjs/core";import{Subject as h}from"rxjs";function g(e){"@babel/helpers - typeof";return g=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},g(e)}function _(e,t){if(g(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(g(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function v(e){var t=_(e,`string`);return g(t)==`symbol`?t:t+``}function y(e,t,n){return(t=v(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var b=class extends t{set dataSource(e){this._dataSource=e}get dataSource(){return this._dataSource}constructor(){super(),y(this,`_dataSource`,null),y(this,`syncUpdateMutationToColla`,!0)}async getThreadComment(e,t,n){return this._dataSource?(await this._dataSource.listComments(e,t,[n]))[0]:null}async addComment(e){var t;return this._dataSource?this._dataSource.addComment(e):{...e,threadId:(t=e.threadId)==null?e.id:t}}async updateComment(e){return this._dataSource?this._dataSource.updateComment(e):!0}async resolveComment(e){return this._dataSource?this._dataSource.resolveComment(e):!0}async deleteComment(e,t,n,r){return this._dataSource?this._dataSource.deleteComment(e,t,n,r):!0}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=>{t[n]=e[n].map(this.dataSource.saveCommentToSnapshot)}),t}return e}};const x=d(`univer.thread-comment.data-source-service`);function S(e,t){return function(n,r){t(n,r,e)}}function C(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 w=class extends t{constructor(e,t){super(),this._dataSourceService=e,this._lifecycleService=t,y(this,`_commentsMap`,new Map),y(this,`_threadMap`,new Map),y(this,`_commentUpdate$`,new h),y(this,`commentUpdate$`,this._commentUpdate$.asObservable()),y(this,`_tasks`,[]),this.disposeWithMe(()=>{this._commentUpdate$.complete()}),this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe(e=>{let t=new Map;e===c.Rendered&&(this._tasks.forEach(({unitId:e,subUnitId:n,threadIds:r})=>{let i=t.get(e);i||(i=new Map,t.set(e,i));let a=i.get(n);a||(a=new Set,i.set(n,a));for(let e of r)a.add(e)}),this._tasks=[],t.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}_replaceComment(e,t,n){let r=this._ensureCommentMap(e,t),i=r.get(n.id);if(i){let{children:a,...o}=n,s={...o,ref:i.ref};r.set(n.id,s),a==null||a.forEach(e=>{r.set(e.id,{...e,ref:``})}),this._commentUpdate$.next({unitId:e,subUnitId:t,type:`syncUpdate`,payload:s}),!!n.resolved!=!!i.resolved&&this._commentUpdate$.next({unitId:e,subUnitId:t,type:`resolve`,payload:{commentId:n.id,resolved:!!n.resolved}})}}async syncThreadComments(e,t,n){if(this._lifecycleService.stage<c.Rendered){this._tasks.push({unitId:e,subUnitId:t,threadIds:n});return}let r=this._ensureThreadMap(e,t),i=this._ensureCommentMap(e,t),a=await this._dataSourceService.listThreadComments(e,t,n);if(!a)return;let o=new Set(n);a.forEach(n=>{this._replaceComment(e,t,n),o.delete(n.threadId)}),o.forEach(e=>{r.delete(e),i.forEach((t,n)=>{t.threadId===e&&i.delete(n)})})}addComment(e,t,n,r){let i=this._ensureCommentMap(e,t),{parentId:a,children:o=[],...s}=n,c={...s,parentId:a===n.id?void 0:a};c.threadId||(c.threadId=c.parentId||c.id);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)l(e)}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?(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):!0}updateCommentRef(e,t,n,r){let i=this._ensureCommentMap(e,t).get(n.commentId);return i?(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?(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&&(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._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&&t.forEach((t,n)=>{t.forEach(t=>{this.deleteComment(e,n,t.id)})})}getUnit(e){let t=this._threadMap.get(e);if(!t)return[];let n=[];return t.forEach((t,r)=>{t.forEach((t,i)=>{let a=this.getThread(e,r,i);a&&n.push(a)})}),n}getAll(){let e=[];return this._commentsMap.forEach((t,n)=>{e.push({unitId:n,threads:this.getUnit(n)})}),e}};w=C([S(0,a(x)),S(1,a(s))],w);const T={id:`thread-comment.mutation.add-comment`,type:e.MUTATION,handler(e,t,n){if(!t)return!1;let r=e.get(w),{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)}},E={id:`thread-comment.mutation.update-comment`,type:e.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(w),{unitId:r,subUnitId:i,payload:a,silent:o}=t;return n.updateComment(r,i,a,o)}},D={id:`thread-comment.mutation.update-comment-ref`,type:e.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(w),{unitId:r,subUnitId:i,payload:a,silent:o}=t;return n.updateCommentRef(r,i,a,o)}},O={id:`thread-comment.mutation.resolve-comment`,type:e.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(w),{unitId:r,subUnitId:i,resolved:a,commentId:o}=t;return n.resolveComment(r,i,o,a)}},k={id:`thread-comment.mutation.delete-comment`,type:e.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(w),{unitId:r,subUnitId:i,commentId:a}=t;return n.deleteComment(r,i,a)}},A={id:`thread-comment.command.add-comment`,type:e.COMMAND,async handler(e,t){if(!t)return!1;let r=e.get(n),i=e.get(x),{comment:a}=t,o=await i.addComment(a),s=i.syncUpdateMutationToColla,c=!a.parentId,l={id:T.id,params:{...t,comment:o}};return c?await r.executeCommand(l.id,l.params):r.executeCommand(l.id,l.params,{onlyLocal:!s})}},j={id:`thread-comment.command.update-comment`,type:e.COMMAND,async handler(e,t){if(!t)return!1;let{unitId:r,subUnitId:i,payload:a}=t,o=e.get(n),s=e.get(w),c=e.get(x),l=c.syncUpdateMutationToColla,u=s.getComment(r,i,a.commentId);if(!u)return!1;let{children:d,...f}=u;if(!await c.updateComment({...f,...a}))return!1;let p={id:E.id,params:t};return o.executeCommand(p.id,p.params,{onlyLocal:!l}),!0}},M={id:`thread-comment.command.resolve-comment`,type:e.COMMAND,async handler(e,t){if(!t)return!1;let{unitId:r,subUnitId:i,resolved:a,commentId:o}=t,s=e.get(x),c=e.get(w).getComment(r,i,o),l=s.syncUpdateMutationToColla;return!c||!await s.resolveComment({...c,resolved:a})?!1:e.get(n).executeCommand(O.id,t,{onlyLocal:!l})}},N={id:`thread-comment.command.delete-comment`,type:e.COMMAND,async handler(e,t){if(!t)return!1;let r=e.get(w),i=e.get(x),a=e.get(n),{unitId:o,subUnitId:s,commentId:c}=t,l=i.syncUpdateMutationToColla,u=r.getComment(o,s,c);if(!u||!await i.deleteComment(o,s,u.threadId,c))return!1;let d={id:k.id,params:t};return a.executeCommand(d.id,d.params,{onlyLocal:!l})}},P={id:`thread-comment.command.delete-comment-tree`,type:e.COMMAND,async handler(e,t){if(!t)return!1;let r=e.get(w),i=e.get(n),a=e.get(x),{unitId:o,subUnitId:s,commentId:c}=t,l=r.getCommentWithChildren(o,s,c);return!l||!await a.deleteComment(o,s,l.root.threadId,c)?!1:await i.executeCommand(k.id,{unitId:o,subUnitId:s,commentId:l.root.id})}};function F(e){return f(e).format(`YYYY/MM/DD HH:mm`)}const I=`UNIVER_THREAD_COMMENT_PLUGIN`,L=`SHEET_${I}`;let R=class extends t{constructor(e,t,n){super(),this._resourceManagerService=e,this._threadCommentModel=t,this._threadCommentDataSourceService=n,this._initSnapshot()}_initSnapshot(){let e=e=>{let t=this._threadCommentModel.getUnit(e),n={};return t?(t.forEach(e=>{var t;let r=(t=n[e.subUnitId])==null?[]:t;r.push({...e.root,children:e.children}),n[e.subUnitId]=r}),JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(n,e))):``},t=e=>{if(!e)return{};try{return JSON.parse(e)}catch{return{}}};this.disposeWithMe(this._resourceManagerService.registerPluginResource({pluginName:L,businesses:[u.UNIVER_SHEET,u.UNIVER_DOC],toJson:t=>e(t),parseJson:e=>t(e),onUnLoad:e=>{this._threadCommentModel.deleteUnit(e)},onLoad:async(e,t)=>{Object.keys(t).forEach(n=>{let r=t[n];r.forEach(t=>{this._threadCommentModel.addComment(e,n,t)}),this._threadCommentModel.syncThreadComments(e,n,r.map(e=>e.threadId))})}}))}};R=C([S(0,i),S(1,a(w)),S(2,x)],R);var z=`@univerjs/thread-comment`,B=`0.20.1`;const V=`thread-comment.config`;Symbol(V);const H={};let U=class extends l{constructor(e=H,t,n,r){super(),this._config=e,this._injector=t,this._commandService=n,this._configService=r;let{...i}=p({},H,this._config);this._configService.setConfig(V,i)}onStarting(){var e;m([[x,{useClass:b}],[w],[R]],(e=this._config)==null?void 0:e.overrides).forEach(e=>{this._injector.add(e)}),[A,j,N,M,P,T,E,D,k,O].forEach(e=>{this._commandService.registerCommand(e)}),this._injector.get(R)}};y(U,`pluginName`,I),y(U,`packageName`,z),y(U,`version`,B),y(U,`type`,u.UNIVER_UNKNOWN),U=C([S(1,a(o)),S(2,n),S(3,r)],U);export{A as AddCommentCommand,T as AddCommentMutation,N as DeleteCommentCommand,k as DeleteCommentMutation,P as DeleteCommentTreeCommand,x as IThreadCommentDataSourceService,M as ResolveCommentCommand,O as ResolveCommentMutation,L as SHEET_UNIVER_THREAD_COMMENT_PLUGIN,I as TC_PLUGIN_NAME,b as ThreadCommentDataSourceService,w as ThreadCommentModel,R as ThreadCommentResourceController,U as UniverThreadCommentPlugin,j as UpdateCommentCommand,E as UpdateCommentMutation,D as UpdateCommentRefMutation,F as getDT};
1
+ import { CommandType, Disposable, ICommandService, IConfigService, IResourceManagerService, Inject, Injector, LifecycleService, LifecycleStages, Plugin, UniverInstanceType, createIdentifier, dateKit, merge, mergeOverrideWithDependencies } from "@univerjs/core";
2
+ import { Subject } from "rxjs";
3
+
4
+ //#region \0@oxc-project+runtime@0.124.0/helpers/typeof.js
5
+ function _typeof(o) {
6
+ "@babel/helpers - typeof";
7
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
8
+ return typeof o;
9
+ } : function(o) {
10
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
11
+ }, _typeof(o);
12
+ }
13
+
14
+ //#endregion
15
+ //#region \0@oxc-project+runtime@0.124.0/helpers/toPrimitive.js
16
+ function toPrimitive(t, r) {
17
+ if ("object" != _typeof(t) || !t) return t;
18
+ var e = t[Symbol.toPrimitive];
19
+ if (void 0 !== e) {
20
+ var i = e.call(t, r || "default");
21
+ if ("object" != _typeof(i)) return i;
22
+ throw new TypeError("@@toPrimitive must return a primitive value.");
23
+ }
24
+ return ("string" === r ? String : Number)(t);
25
+ }
26
+
27
+ //#endregion
28
+ //#region \0@oxc-project+runtime@0.124.0/helpers/toPropertyKey.js
29
+ function toPropertyKey(t) {
30
+ var i = toPrimitive(t, "string");
31
+ return "symbol" == _typeof(i) ? i : i + "";
32
+ }
33
+
34
+ //#endregion
35
+ //#region \0@oxc-project+runtime@0.124.0/helpers/defineProperty.js
36
+ function _defineProperty(e, r, t) {
37
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
38
+ value: t,
39
+ enumerable: !0,
40
+ configurable: !0,
41
+ writable: !0
42
+ }) : e[r] = t, e;
43
+ }
44
+
45
+ //#endregion
46
+ //#region src/services/tc-datasource.service.ts
47
+ /**
48
+ * Preserve for import async comment system
49
+ */
50
+ var ThreadCommentDataSourceService = class extends Disposable {
51
+ set dataSource(dataSource) {
52
+ this._dataSource = dataSource;
53
+ }
54
+ get dataSource() {
55
+ return this._dataSource;
56
+ }
57
+ constructor() {
58
+ super();
59
+ _defineProperty(this, "_dataSource", null);
60
+ _defineProperty(this, "syncUpdateMutationToColla", true);
61
+ }
62
+ async getThreadComment(unitId, subUnitId, threadId) {
63
+ if (this._dataSource) return (await this._dataSource.listComments(unitId, subUnitId, [threadId]))[0];
64
+ return null;
65
+ }
66
+ async addComment(comment) {
67
+ var _comment$threadId;
68
+ if (this._dataSource) return this._dataSource.addComment(comment);
69
+ return {
70
+ ...comment,
71
+ threadId: (_comment$threadId = comment.threadId) !== null && _comment$threadId !== void 0 ? _comment$threadId : comment.id
72
+ };
73
+ }
74
+ async updateComment(comment) {
75
+ if (this._dataSource) return this._dataSource.updateComment(comment);
76
+ return true;
77
+ }
78
+ async resolveComment(comment) {
79
+ if (this._dataSource) return this._dataSource.resolveComment(comment);
80
+ return true;
81
+ }
82
+ async deleteComment(unitId, subUnitId, threadId, commentId) {
83
+ if (this._dataSource) return this._dataSource.deleteComment(unitId, subUnitId, threadId, commentId);
84
+ return true;
85
+ }
86
+ async listThreadComments(unitId, subUnitId, threadIds) {
87
+ if (this.dataSource) return this.dataSource.listComments(unitId, subUnitId, threadIds);
88
+ return false;
89
+ }
90
+ saveToSnapshot(unitComments, unitId) {
91
+ if (this._dataSource) {
92
+ const map = {};
93
+ Object.keys(unitComments).forEach((subUnitId) => {
94
+ map[subUnitId] = unitComments[subUnitId].map(this.dataSource.saveCommentToSnapshot);
95
+ });
96
+ return map;
97
+ }
98
+ return unitComments;
99
+ }
100
+ };
101
+ const IThreadCommentDataSourceService = createIdentifier("univer.thread-comment.data-source-service");
102
+
103
+ //#endregion
104
+ //#region \0@oxc-project+runtime@0.124.0/helpers/decorateParam.js
105
+ function __decorateParam(paramIndex, decorator) {
106
+ return function(target, key) {
107
+ decorator(target, key, paramIndex);
108
+ };
109
+ }
110
+
111
+ //#endregion
112
+ //#region \0@oxc-project+runtime@0.124.0/helpers/decorate.js
113
+ function __decorate(decorators, target, key, desc) {
114
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
115
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
116
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
117
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
118
+ }
119
+
120
+ //#endregion
121
+ //#region src/models/thread-comment.model.ts
122
+ let ThreadCommentModel = class ThreadCommentModel extends Disposable {
123
+ constructor(_dataSourceService, _lifecycleService) {
124
+ super();
125
+ this._dataSourceService = _dataSourceService;
126
+ this._lifecycleService = _lifecycleService;
127
+ _defineProperty(this, "_commentsMap", /* @__PURE__ */ new Map());
128
+ _defineProperty(this, "_threadMap", /* @__PURE__ */ new Map());
129
+ _defineProperty(this, "_commentUpdate$", new Subject());
130
+ _defineProperty(this, "commentUpdate$", this._commentUpdate$.asObservable());
131
+ _defineProperty(this, "_tasks", []);
132
+ this.disposeWithMe(() => {
133
+ this._commentUpdate$.complete();
134
+ });
135
+ this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe((stage) => {
136
+ const taskMap = /* @__PURE__ */ new Map();
137
+ if (stage === LifecycleStages.Rendered) {
138
+ this._tasks.forEach(({ unitId, subUnitId, threadIds }) => {
139
+ let unitMap = taskMap.get(unitId);
140
+ if (!unitMap) {
141
+ unitMap = /* @__PURE__ */ new Map();
142
+ taskMap.set(unitId, unitMap);
143
+ }
144
+ let subUnitMap = unitMap.get(subUnitId);
145
+ if (!subUnitMap) {
146
+ subUnitMap = /* @__PURE__ */ new Set();
147
+ unitMap.set(subUnitId, subUnitMap);
148
+ }
149
+ for (const threadId of threadIds) subUnitMap.add(threadId);
150
+ });
151
+ this._tasks = [];
152
+ taskMap.forEach((subUnitMap, unitId) => {
153
+ subUnitMap.forEach((threadIds, subUnitId) => {
154
+ this.syncThreadComments(unitId, subUnitId, Array.from(threadIds));
155
+ });
156
+ });
157
+ }
158
+ }));
159
+ }
160
+ _ensureCommentMap(unitId, subUnitId) {
161
+ let unitMap = this._commentsMap.get(unitId);
162
+ if (!unitMap) {
163
+ unitMap = /* @__PURE__ */ new Map();
164
+ this._commentsMap.set(unitId, unitMap);
165
+ }
166
+ let subUnitMap = unitMap.get(subUnitId);
167
+ if (!subUnitMap) {
168
+ subUnitMap = /* @__PURE__ */ new Map();
169
+ unitMap.set(subUnitId, subUnitMap);
170
+ }
171
+ return subUnitMap;
172
+ }
173
+ ensureMap(unitId, subUnitId) {
174
+ return this._ensureCommentMap(unitId, subUnitId);
175
+ }
176
+ _ensureThreadMap(unitId, subUnitId) {
177
+ let unitMap = this._threadMap.get(unitId);
178
+ if (!unitMap) {
179
+ unitMap = /* @__PURE__ */ new Map();
180
+ this._threadMap.set(unitId, unitMap);
181
+ }
182
+ let subUnitMap = unitMap.get(subUnitId);
183
+ if (!subUnitMap) {
184
+ subUnitMap = /* @__PURE__ */ new Map();
185
+ unitMap.set(subUnitId, subUnitMap);
186
+ }
187
+ return subUnitMap;
188
+ }
189
+ _replaceComment(unitId, subUnitId, comment) {
190
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
191
+ const currentComment = commentMap.get(comment.id);
192
+ if (currentComment) {
193
+ const { children, ...rest } = comment;
194
+ const newComment = {
195
+ ...rest,
196
+ ref: currentComment.ref
197
+ };
198
+ commentMap.set(comment.id, newComment);
199
+ children === null || children === void 0 || children.forEach((child) => {
200
+ commentMap.set(child.id, {
201
+ ...child,
202
+ ref: ""
203
+ });
204
+ });
205
+ this._commentUpdate$.next({
206
+ unitId,
207
+ subUnitId,
208
+ type: "syncUpdate",
209
+ payload: newComment
210
+ });
211
+ if (Boolean(comment.resolved) !== Boolean(currentComment.resolved)) this._commentUpdate$.next({
212
+ unitId,
213
+ subUnitId,
214
+ type: "resolve",
215
+ payload: {
216
+ commentId: comment.id,
217
+ resolved: Boolean(comment.resolved)
218
+ }
219
+ });
220
+ }
221
+ }
222
+ async syncThreadComments(unitId, subUnitId, threadIds) {
223
+ if (this._lifecycleService.stage < LifecycleStages.Rendered) {
224
+ this._tasks.push({
225
+ unitId,
226
+ subUnitId,
227
+ threadIds
228
+ });
229
+ return;
230
+ }
231
+ const threadMap = this._ensureThreadMap(unitId, subUnitId);
232
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
233
+ const comments = await this._dataSourceService.listThreadComments(unitId, subUnitId, threadIds);
234
+ if (!comments) return;
235
+ const deleteThreads = new Set(threadIds);
236
+ comments.forEach((comment) => {
237
+ this._replaceComment(unitId, subUnitId, comment);
238
+ deleteThreads.delete(comment.threadId);
239
+ });
240
+ deleteThreads.forEach((id) => {
241
+ threadMap.delete(id);
242
+ commentMap.forEach((comment, commentId) => {
243
+ if (comment.threadId === id) commentMap.delete(commentId);
244
+ });
245
+ });
246
+ }
247
+ addComment(unitId, subUnitId, origin, shouldSync) {
248
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
249
+ const { parentId, children = [], ...rest } = origin;
250
+ const comment = {
251
+ ...rest,
252
+ parentId: parentId === origin.id ? void 0 : parentId
253
+ };
254
+ if (!comment.threadId) comment.threadId = comment.parentId || comment.id;
255
+ const addCommentItem = (item) => {
256
+ commentMap.set(item.id, item);
257
+ this._commentUpdate$.next({
258
+ unitId,
259
+ subUnitId,
260
+ type: "add",
261
+ payload: item,
262
+ isRoot: !item.parentId
263
+ });
264
+ };
265
+ addCommentItem(comment);
266
+ const threadMap = this._ensureThreadMap(unitId, subUnitId);
267
+ if (!comment.parentId) {
268
+ threadMap.set(comment.threadId, comment);
269
+ for (const child of children) addCommentItem(child);
270
+ }
271
+ if (shouldSync) this.syncThreadComments(unitId, subUnitId, [comment.threadId]);
272
+ return true;
273
+ }
274
+ updateComment(unitId, subUnitId, payload, silent) {
275
+ const oldComment = this._ensureCommentMap(unitId, subUnitId).get(payload.commentId);
276
+ if (!oldComment) return true;
277
+ oldComment.updated = true;
278
+ oldComment.text = payload.text;
279
+ oldComment.attachments = payload.attachments;
280
+ oldComment.updateT = payload.updateT;
281
+ this._commentUpdate$.next({
282
+ unitId,
283
+ subUnitId,
284
+ type: "update",
285
+ payload,
286
+ silent
287
+ });
288
+ return true;
289
+ }
290
+ updateCommentRef(unitId, subUnitId, payload, silent) {
291
+ const oldComment = this._ensureCommentMap(unitId, subUnitId).get(payload.commentId);
292
+ if (!oldComment) return false;
293
+ oldComment.ref = payload.ref;
294
+ this._commentUpdate$.next({
295
+ unitId,
296
+ subUnitId,
297
+ type: "updateRef",
298
+ payload,
299
+ silent,
300
+ threadId: oldComment.threadId
301
+ });
302
+ return true;
303
+ }
304
+ resolveComment(unitId, subUnitId, commentId, resolved) {
305
+ const oldComment = this._ensureCommentMap(unitId, subUnitId).get(commentId);
306
+ if (!oldComment) return false;
307
+ oldComment.resolved = resolved;
308
+ this._commentUpdate$.next({
309
+ unitId,
310
+ subUnitId,
311
+ type: "resolve",
312
+ payload: {
313
+ commentId,
314
+ resolved
315
+ }
316
+ });
317
+ return true;
318
+ }
319
+ getComment(unitId, subUnitId, commentId) {
320
+ return this._ensureCommentMap(unitId, subUnitId).get(commentId);
321
+ }
322
+ getRootComment(unitId, subUnitId, threadId) {
323
+ return this._ensureThreadMap(unitId, subUnitId).get(threadId);
324
+ }
325
+ getThread(unitId, subUnitId, threadId) {
326
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
327
+ const comments = Array.from(commentMap.values()).filter((comment) => comment.threadId === threadId);
328
+ let root;
329
+ const children = [];
330
+ const relativeUsers = /* @__PURE__ */ new Set();
331
+ for (const comment of comments) {
332
+ if (!comment.parentId) root = comment;
333
+ else children.push(comment);
334
+ relativeUsers.add(comment.personId);
335
+ }
336
+ if (!root) return;
337
+ return {
338
+ root,
339
+ children,
340
+ relativeUsers,
341
+ unitId,
342
+ subUnitId,
343
+ threadId
344
+ };
345
+ }
346
+ getCommentWithChildren(unitId, subUnitId, commentId) {
347
+ const comment = this.getComment(unitId, subUnitId, commentId);
348
+ if (!comment) return;
349
+ return this.getThread(unitId, subUnitId, comment.threadId);
350
+ }
351
+ _deleteComment(unitId, subUnitId, commentId) {
352
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
353
+ const current = commentMap.get(commentId);
354
+ if (!current) return;
355
+ commentMap.delete(commentId);
356
+ this._commentUpdate$.next({
357
+ unitId,
358
+ subUnitId,
359
+ type: "delete",
360
+ payload: {
361
+ commentId,
362
+ isRoot: !current.parentId,
363
+ comment: current
364
+ }
365
+ });
366
+ }
367
+ deleteThread(unitId, subUnitId, threadId) {
368
+ this._ensureThreadMap(unitId, subUnitId).delete(threadId);
369
+ this._ensureCommentMap(unitId, subUnitId).forEach((comment) => {
370
+ if (comment.threadId === threadId) this._deleteComment(unitId, subUnitId, comment.id);
371
+ });
372
+ }
373
+ deleteComment(unitId, subUnitId, commentId) {
374
+ const current = this._ensureCommentMap(unitId, subUnitId).get(commentId);
375
+ if (!current) return true;
376
+ if (current.parentId) this._deleteComment(unitId, subUnitId, commentId);
377
+ else this.deleteThread(unitId, subUnitId, current.threadId);
378
+ return true;
379
+ }
380
+ deleteUnit(unitId) {
381
+ const unitMap = this._commentsMap.get(unitId);
382
+ if (!unitMap) return;
383
+ unitMap.forEach((subUnitMap, subUnitId) => {
384
+ subUnitMap.forEach((comment) => {
385
+ this.deleteComment(unitId, subUnitId, comment.id);
386
+ });
387
+ });
388
+ }
389
+ getUnit(unitId) {
390
+ const unitMap = this._threadMap.get(unitId);
391
+ if (!unitMap) return [];
392
+ const threads = [];
393
+ unitMap.forEach((subUnitSet, subUnitId) => {
394
+ subUnitSet.forEach((threadComment, threadId) => {
395
+ const thread = this.getThread(unitId, subUnitId, threadId);
396
+ if (thread) threads.push(thread);
397
+ });
398
+ });
399
+ return threads;
400
+ }
401
+ getAll() {
402
+ const all = [];
403
+ this._commentsMap.forEach((unitMap, unitId) => {
404
+ all.push({
405
+ unitId,
406
+ threads: this.getUnit(unitId)
407
+ });
408
+ });
409
+ return all;
410
+ }
411
+ };
412
+ ThreadCommentModel = __decorate([__decorateParam(0, Inject(IThreadCommentDataSourceService)), __decorateParam(1, Inject(LifecycleService))], ThreadCommentModel);
413
+
414
+ //#endregion
415
+ //#region src/commands/mutations/comment.mutation.ts
416
+ const AddCommentMutation = {
417
+ id: "thread-comment.mutation.add-comment",
418
+ type: CommandType.MUTATION,
419
+ handler(accessor, params, options) {
420
+ if (!params) return false;
421
+ const threadCommentModel = accessor.get(ThreadCommentModel);
422
+ const { unitId, subUnitId, comment, sync } = params;
423
+ const shouldSync = sync || (options === null || options === void 0 ? void 0 : options.fromChangeset) && !comment.parentId;
424
+ return threadCommentModel.addComment(unitId, subUnitId, comment, shouldSync);
425
+ }
426
+ };
427
+ const UpdateCommentMutation = {
428
+ id: "thread-comment.mutation.update-comment",
429
+ type: CommandType.MUTATION,
430
+ handler(accessor, params) {
431
+ if (!params) return false;
432
+ const threadCommentModel = accessor.get(ThreadCommentModel);
433
+ const { unitId, subUnitId, payload, silent } = params;
434
+ return threadCommentModel.updateComment(unitId, subUnitId, payload, silent);
435
+ }
436
+ };
437
+ const UpdateCommentRefMutation = {
438
+ id: "thread-comment.mutation.update-comment-ref",
439
+ type: CommandType.MUTATION,
440
+ handler(accessor, params) {
441
+ if (!params) return false;
442
+ const threadCommentModel = accessor.get(ThreadCommentModel);
443
+ const { unitId, subUnitId, payload, silent } = params;
444
+ return threadCommentModel.updateCommentRef(unitId, subUnitId, payload, silent);
445
+ }
446
+ };
447
+ const ResolveCommentMutation = {
448
+ id: "thread-comment.mutation.resolve-comment",
449
+ type: CommandType.MUTATION,
450
+ handler(accessor, params) {
451
+ if (!params) return false;
452
+ const threadCommentModel = accessor.get(ThreadCommentModel);
453
+ const { unitId, subUnitId, resolved, commentId } = params;
454
+ return threadCommentModel.resolveComment(unitId, subUnitId, commentId, resolved);
455
+ }
456
+ };
457
+ const DeleteCommentMutation = {
458
+ id: "thread-comment.mutation.delete-comment",
459
+ type: CommandType.MUTATION,
460
+ handler(accessor, params) {
461
+ if (!params) return false;
462
+ const threadCommentModel = accessor.get(ThreadCommentModel);
463
+ const { unitId, subUnitId, commentId } = params;
464
+ return threadCommentModel.deleteComment(unitId, subUnitId, commentId);
465
+ }
466
+ };
467
+
468
+ //#endregion
469
+ //#region src/commands/commands/comment.command.ts
470
+ const AddCommentCommand = {
471
+ id: "thread-comment.command.add-comment",
472
+ type: CommandType.COMMAND,
473
+ async handler(accessor, params) {
474
+ if (!params) return false;
475
+ const commandService = accessor.get(ICommandService);
476
+ const dataSourceService = accessor.get(IThreadCommentDataSourceService);
477
+ const { comment: originComment } = params;
478
+ const comment = await dataSourceService.addComment(originComment);
479
+ const syncUpdateMutationToColla = dataSourceService.syncUpdateMutationToColla;
480
+ const isRoot = !originComment.parentId;
481
+ const redo = {
482
+ id: AddCommentMutation.id,
483
+ params: {
484
+ ...params,
485
+ comment
486
+ }
487
+ };
488
+ if (isRoot) return await commandService.executeCommand(redo.id, redo.params);
489
+ return commandService.executeCommand(redo.id, redo.params, { onlyLocal: !syncUpdateMutationToColla });
490
+ }
491
+ };
492
+ const UpdateCommentCommand = {
493
+ id: "thread-comment.command.update-comment",
494
+ type: CommandType.COMMAND,
495
+ async handler(accessor, params) {
496
+ if (!params) return false;
497
+ const { unitId, subUnitId, payload } = params;
498
+ const commandService = accessor.get(ICommandService);
499
+ const threadCommentModel = accessor.get(ThreadCommentModel);
500
+ const dataSourceService = accessor.get(IThreadCommentDataSourceService);
501
+ const syncUpdateMutationToColla = dataSourceService.syncUpdateMutationToColla;
502
+ const current = threadCommentModel.getComment(unitId, subUnitId, payload.commentId);
503
+ if (!current) return false;
504
+ const { children, ...currentComment } = current;
505
+ if (!await dataSourceService.updateComment({
506
+ ...currentComment,
507
+ ...payload
508
+ })) return false;
509
+ const redo = {
510
+ id: UpdateCommentMutation.id,
511
+ params
512
+ };
513
+ commandService.executeCommand(redo.id, redo.params, { onlyLocal: !syncUpdateMutationToColla });
514
+ return true;
515
+ }
516
+ };
517
+ const ResolveCommentCommand = {
518
+ id: "thread-comment.command.resolve-comment",
519
+ type: CommandType.COMMAND,
520
+ async handler(accessor, params) {
521
+ if (!params) return false;
522
+ const { unitId, subUnitId, resolved, commentId } = params;
523
+ const dataSourceService = accessor.get(IThreadCommentDataSourceService);
524
+ const currentComment = accessor.get(ThreadCommentModel).getComment(unitId, subUnitId, commentId);
525
+ const syncUpdateMutationToColla = dataSourceService.syncUpdateMutationToColla;
526
+ if (!currentComment) return false;
527
+ if (!await dataSourceService.resolveComment({
528
+ ...currentComment,
529
+ resolved
530
+ })) return false;
531
+ return accessor.get(ICommandService).executeCommand(ResolveCommentMutation.id, params, { onlyLocal: !syncUpdateMutationToColla });
532
+ }
533
+ };
534
+ /**
535
+ * Delete Reply
536
+ */
537
+ const DeleteCommentCommand = {
538
+ id: "thread-comment.command.delete-comment",
539
+ type: CommandType.COMMAND,
540
+ async handler(accessor, params) {
541
+ if (!params) return false;
542
+ const threadCommentModel = accessor.get(ThreadCommentModel);
543
+ const dataSourceService = accessor.get(IThreadCommentDataSourceService);
544
+ const commandService = accessor.get(ICommandService);
545
+ const { unitId, subUnitId, commentId } = params;
546
+ const syncUpdateMutationToColla = dataSourceService.syncUpdateMutationToColla;
547
+ const comment = threadCommentModel.getComment(unitId, subUnitId, commentId);
548
+ if (!comment) return false;
549
+ if (!await dataSourceService.deleteComment(unitId, subUnitId, comment.threadId, commentId)) return false;
550
+ const redo = {
551
+ id: DeleteCommentMutation.id,
552
+ params
553
+ };
554
+ return commandService.executeCommand(redo.id, redo.params, { onlyLocal: !syncUpdateMutationToColla });
555
+ }
556
+ };
557
+ const DeleteCommentTreeCommand = {
558
+ id: "thread-comment.command.delete-comment-tree",
559
+ type: CommandType.COMMAND,
560
+ async handler(accessor, params) {
561
+ if (!params) return false;
562
+ const threadCommentModel = accessor.get(ThreadCommentModel);
563
+ const commandService = accessor.get(ICommandService);
564
+ const dataSourceService = accessor.get(IThreadCommentDataSourceService);
565
+ const { unitId, subUnitId, commentId } = params;
566
+ const commentWithChildren = threadCommentModel.getCommentWithChildren(unitId, subUnitId, commentId);
567
+ if (!commentWithChildren) return false;
568
+ if (!await dataSourceService.deleteComment(unitId, subUnitId, commentWithChildren.root.threadId, commentId)) return false;
569
+ return await commandService.executeCommand(DeleteCommentMutation.id, {
570
+ unitId,
571
+ subUnitId,
572
+ commentId: commentWithChildren.root.id
573
+ });
574
+ }
575
+ };
576
+
577
+ //#endregion
578
+ //#region src/common/utils.ts
579
+ /**
580
+ * Copyright 2023-present DreamNum Co., Ltd.
581
+ *
582
+ * Licensed under the Apache License, Version 2.0 (the "License");
583
+ * you may not use this file except in compliance with the License.
584
+ * You may obtain a copy of the License at
585
+ *
586
+ * http://www.apache.org/licenses/LICENSE-2.0
587
+ *
588
+ * Unless required by applicable law or agreed to in writing, software
589
+ * distributed under the License is distributed on an "AS IS" BASIS,
590
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
591
+ * See the License for the specific language governing permissions and
592
+ * limitations under the License.
593
+ */
594
+ function getDT(date) {
595
+ return dateKit(date).format("YYYY/MM/DD HH:mm");
596
+ }
597
+
598
+ //#endregion
599
+ //#region src/types/const/index.ts
600
+ /**
601
+ * Copyright 2023-present DreamNum Co., Ltd.
602
+ *
603
+ * Licensed under the Apache License, Version 2.0 (the "License");
604
+ * you may not use this file except in compliance with the License.
605
+ * You may obtain a copy of the License at
606
+ *
607
+ * http://www.apache.org/licenses/LICENSE-2.0
608
+ *
609
+ * Unless required by applicable law or agreed to in writing, software
610
+ * distributed under the License is distributed on an "AS IS" BASIS,
611
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
612
+ * See the License for the specific language governing permissions and
613
+ * limitations under the License.
614
+ */
615
+ const TC_PLUGIN_NAME = "UNIVER_THREAD_COMMENT_PLUGIN";
616
+
617
+ //#endregion
618
+ //#region src/controllers/tc-resource.controller.ts
619
+ const SHEET_UNIVER_THREAD_COMMENT_PLUGIN = `SHEET_${TC_PLUGIN_NAME}`;
620
+ let ThreadCommentResourceController = class ThreadCommentResourceController extends Disposable {
621
+ constructor(_resourceManagerService, _threadCommentModel, _threadCommentDataSourceService) {
622
+ super();
623
+ this._resourceManagerService = _resourceManagerService;
624
+ this._threadCommentModel = _threadCommentModel;
625
+ this._threadCommentDataSourceService = _threadCommentDataSourceService;
626
+ this._initSnapshot();
627
+ }
628
+ _initSnapshot() {
629
+ const toJson = (unitID) => {
630
+ const map = this._threadCommentModel.getUnit(unitID);
631
+ const resultMap = {};
632
+ if (map) {
633
+ map.forEach((info) => {
634
+ var _resultMap$info$subUn;
635
+ const subUnitComments = (_resultMap$info$subUn = resultMap[info.subUnitId]) !== null && _resultMap$info$subUn !== void 0 ? _resultMap$info$subUn : [];
636
+ subUnitComments.push({
637
+ ...info.root,
638
+ children: info.children
639
+ });
640
+ resultMap[info.subUnitId] = subUnitComments;
641
+ });
642
+ return JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(resultMap, unitID));
643
+ }
644
+ return "";
645
+ };
646
+ const parseJson = (json) => {
647
+ if (!json) return {};
648
+ try {
649
+ return JSON.parse(json);
650
+ } catch (err) {
651
+ return {};
652
+ }
653
+ };
654
+ this.disposeWithMe(this._resourceManagerService.registerPluginResource({
655
+ pluginName: SHEET_UNIVER_THREAD_COMMENT_PLUGIN,
656
+ businesses: [UniverInstanceType.UNIVER_SHEET, UniverInstanceType.UNIVER_DOC],
657
+ toJson: (unitID) => toJson(unitID),
658
+ parseJson: (json) => parseJson(json),
659
+ onUnLoad: (unitID) => {
660
+ this._threadCommentModel.deleteUnit(unitID);
661
+ },
662
+ onLoad: async (unitID, value) => {
663
+ Object.keys(value).forEach((subunitId) => {
664
+ const commentList = value[subunitId];
665
+ commentList.forEach((comment) => {
666
+ this._threadCommentModel.addComment(unitID, subunitId, comment);
667
+ });
668
+ this._threadCommentModel.syncThreadComments(unitID, subunitId, commentList.map((i) => i.threadId));
669
+ });
670
+ }
671
+ }));
672
+ }
673
+ };
674
+ ThreadCommentResourceController = __decorate([
675
+ __decorateParam(0, IResourceManagerService),
676
+ __decorateParam(1, Inject(ThreadCommentModel)),
677
+ __decorateParam(2, IThreadCommentDataSourceService)
678
+ ], ThreadCommentResourceController);
679
+
680
+ //#endregion
681
+ //#region package.json
682
+ var name = "@univerjs/thread-comment";
683
+ var version = "0.21.0-insiders.20260422-d7fcb4d";
684
+
685
+ //#endregion
686
+ //#region src/config/config.ts
687
+ const THREAD_COMMENT_PLUGIN_CONFIG_KEY = "thread-comment.config";
688
+ const configSymbol = Symbol(THREAD_COMMENT_PLUGIN_CONFIG_KEY);
689
+ const defaultPluginConfig = {};
690
+
691
+ //#endregion
692
+ //#region src/plugin.ts
693
+ let UniverThreadCommentPlugin = class UniverThreadCommentPlugin extends Plugin {
694
+ constructor(_config = defaultPluginConfig, _injector, _commandService, _configService) {
695
+ super();
696
+ this._config = _config;
697
+ this._injector = _injector;
698
+ this._commandService = _commandService;
699
+ this._configService = _configService;
700
+ const { ...rest } = merge({}, defaultPluginConfig, this._config);
701
+ this._configService.setConfig(THREAD_COMMENT_PLUGIN_CONFIG_KEY, rest);
702
+ }
703
+ onStarting() {
704
+ var _this$_config;
705
+ mergeOverrideWithDependencies([
706
+ [IThreadCommentDataSourceService, { useClass: ThreadCommentDataSourceService }],
707
+ [ThreadCommentModel],
708
+ [ThreadCommentResourceController]
709
+ ], (_this$_config = this._config) === null || _this$_config === void 0 ? void 0 : _this$_config.overrides).forEach((d) => {
710
+ this._injector.add(d);
711
+ });
712
+ [
713
+ AddCommentCommand,
714
+ UpdateCommentCommand,
715
+ DeleteCommentCommand,
716
+ ResolveCommentCommand,
717
+ DeleteCommentTreeCommand,
718
+ AddCommentMutation,
719
+ UpdateCommentMutation,
720
+ UpdateCommentRefMutation,
721
+ DeleteCommentMutation,
722
+ ResolveCommentMutation
723
+ ].forEach((command) => {
724
+ this._commandService.registerCommand(command);
725
+ });
726
+ this._injector.get(ThreadCommentResourceController);
727
+ }
728
+ };
729
+ _defineProperty(UniverThreadCommentPlugin, "pluginName", TC_PLUGIN_NAME);
730
+ _defineProperty(UniverThreadCommentPlugin, "packageName", name);
731
+ _defineProperty(UniverThreadCommentPlugin, "version", version);
732
+ _defineProperty(UniverThreadCommentPlugin, "type", UniverInstanceType.UNIVER_UNKNOWN);
733
+ UniverThreadCommentPlugin = __decorate([
734
+ __decorateParam(1, Inject(Injector)),
735
+ __decorateParam(2, ICommandService),
736
+ __decorateParam(3, IConfigService)
737
+ ], UniverThreadCommentPlugin);
738
+
739
+ //#endregion
740
+ export { AddCommentCommand, AddCommentMutation, DeleteCommentCommand, DeleteCommentMutation, DeleteCommentTreeCommand, IThreadCommentDataSourceService, ResolveCommentCommand, ResolveCommentMutation, SHEET_UNIVER_THREAD_COMMENT_PLUGIN, TC_PLUGIN_NAME, ThreadCommentDataSourceService, ThreadCommentModel, ThreadCommentResourceController, UniverThreadCommentPlugin, UpdateCommentCommand, UpdateCommentMutation, UpdateCommentRefMutation, getDT };