@univerjs/thread-comment 0.6.6 → 0.6.7

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
@@ -1 +1 @@
1
- "use strict";var L=Object.defineProperty;var j=(a,m,e)=>m in a?L(a,m,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[m]=e;var l=(a,m,e)=>j(a,typeof m!="symbol"?m+"":m,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@univerjs/core"),V=require("rxjs");class N extends d.Disposable{constructor(){super();l(this,"_dataSource",null);l(this,"syncUpdateMutationToColla",!0)}set dataSource(e){this._dataSource=e}get dataSource(){return this._dataSource}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?t:e.id}}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,o){return this._dataSource?this._dataSource.deleteComment(e,t,n,o):!0}async listThreadComments(e,t,n){return this.dataSource?this.dataSource.listComments(e,t,n):!1}saveToSnapshot(e,t){if(this._dataSource){const n={};return Object.keys(e).forEach(o=>{const r=e[o];n[o]=r.map(this.dataSource.saveCommentToSnapshot)}),n}return e}}const p=d.createIdentifier("univer.thread-comment.data-source-service");var H=Object.getOwnPropertyDescriptor,W=(a,m,e,t)=>{for(var n=t>1?void 0:t?H(m,e):m,o=a.length-1,r;o>=0;o--)(r=a[o])&&(n=r(n)||n);return n},I=(a,m)=>(e,t)=>m(e,t,a);exports.ThreadCommentModel=class extends d.Disposable{constructor(e,t){super();l(this,"_commentsMap",new Map);l(this,"_threadMap",new Map);l(this,"_commentUpdate$",new V.Subject);l(this,"commentUpdate$",this._commentUpdate$.asObservable());l(this,"_tasks",[]);this._dataSourceService=e,this._lifecycleService=t,this.disposeWithMe(()=>{this._commentUpdate$.complete()}),this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe(n=>{const o=new Map;n===d.LifecycleStages.Rendered&&(this._tasks.forEach(({unitId:r,subUnitId:s,threadIds:c})=>{let i=o.get(r);i||(i=new Map,o.set(r,i));let h=i.get(s);h||(h=new Set,i.set(s,h));for(const u of c)h.add(u)}),this._tasks=[],o.forEach((r,s)=>{r.forEach((c,i)=>{this.syncThreadComments(s,i,Array.from(c))})}))}))}_ensureCommentMap(e,t){let n=this._commentsMap.get(e);n||(n=new Map,this._commentsMap.set(e,n));let o=n.get(t);return o||(o=new Map,n.set(t,o)),o}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 o=n.get(t);return o||(o=new Map,n.set(t,o)),o}_replaceComment(e,t,n){const o=this._ensureCommentMap(e,t),r=o.get(n.id);if(r){const{children:s,...c}=n,i={...c,ref:r.ref};o.set(n.id,i),s==null||s.forEach(h=>{o.set(h.id,{...h,ref:""})}),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"syncUpdate",payload:i}),!!n.resolved!=!!r.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<d.LifecycleStages.Rendered){this._tasks.push({unitId:e,subUnitId:t,threadIds:n});return}const o=this._ensureThreadMap(e,t),r=this._ensureCommentMap(e,t),s=await this._dataSourceService.listThreadComments(e,t,n);if(!s)return;const c=new Set(n);s.forEach(i=>{this._replaceComment(e,t,i),c.delete(i.threadId)}),c.forEach(i=>{o.delete(i),r.forEach((h,u)=>{h.threadId===i&&r.delete(u)})})}addComment(e,t,n,o){const r=this._ensureCommentMap(e,t),{parentId:s,children:c=[],...i}=n,h={...i,parentId:s===n.id?void 0:s};h.threadId||(h.threadId=h.parentId||h.id);const u=C=>{r.set(C.id,C),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"add",payload:C,isRoot:!C.parentId})};u(h);const U=this._ensureThreadMap(e,t);if(!h.parentId){U.set(h.threadId,h);for(const C of c)u(C)}return o&&this.syncThreadComments(e,t,[h.threadId]),!0}updateComment(e,t,n,o){const s=this._ensureCommentMap(e,t).get(n.commentId);return s&&(s.updated=!0,s.text=n.text,s.attachments=n.attachments,s.updateT=n.updateT,this._commentUpdate$.next({unitId:e,subUnitId:t,type:"update",payload:n,silent:o})),!0}updateCommentRef(e,t,n,o){const s=this._ensureCommentMap(e,t).get(n.commentId);return s?(s.ref=n.ref,this._commentUpdate$.next({unitId:e,subUnitId:t,type:"updateRef",payload:n,silent:o,threadId:s.threadId}),!0):!1}resolveComment(e,t,n,o){const s=this._ensureCommentMap(e,t).get(n);return s?(s.resolved=o,this._commentUpdate$.next({unitId:e,subUnitId:t,type:"resolve",payload:{commentId:n,resolved:o}}),!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){const o=this._ensureCommentMap(e,t),r=Array.from(o.values()).filter(h=>h.threadId===n);let s;const c=[],i=new Set;for(const h of r)h.parentId?c.push(h):s=h,i.add(h.personId);if(s)return{root:s,children:c,relativeUsers:i,unitId:e,subUnitId:t,threadId:n}}getCommentWithChildren(e,t,n){const o=this.getComment(e,t,n);if(o)return this.getThread(e,t,o.threadId)}_deleteComment(e,t,n){const o=this._ensureCommentMap(e,t),r=o.get(n);r&&(o.delete(n),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"delete",payload:{commentId:n,isRoot:!r.parentId,comment:r}}))}deleteThread(e,t,n){this._ensureThreadMap(e,t).delete(n),this._ensureCommentMap(e,t).forEach(s=>{s.threadId===n&&this._deleteComment(e,t,s.id)})}deleteComment(e,t,n){const 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){const t=this._commentsMap.get(e);t&&t.forEach((n,o)=>{n.forEach(r=>{this.deleteComment(e,o,r.id)})})}getUnit(e){const t=this._threadMap.get(e);if(!t)return[];const n=[];return t.forEach((o,r)=>{o.forEach((s,c)=>{const i=this.getThread(e,r,c);i&&n.push(i)})}),n}getAll(){const e=[];return this._commentsMap.forEach((t,n)=>{e.push({unitId:n,threads:this.getUnit(n)})}),e}};exports.ThreadCommentModel=W([I(0,d.Inject(p)),I(1,d.Inject(d.LifecycleService))],exports.ThreadCommentModel);var g=(a=>(a[a.UNIVER_UNKNOWN=0]="UNIVER_UNKNOWN",a[a.UNIVER_DOC=1]="UNIVER_DOC",a[a.UNIVER_SHEET=2]="UNIVER_SHEET",a[a.UNIVER_SLIDE=3]="UNIVER_SLIDE",a[a.UNIVER_PROJECT=4]="UNIVER_PROJECT",a[a.UNRECOGNIZED=-1]="UNRECOGNIZED",a))(g||{});const S="UNIVER_THREAD_COMMENT_PLUGIN";var G=Object.getOwnPropertyDescriptor,J=(a,m,e,t)=>{for(var n=t>1?void 0:t?G(m,e):m,o=a.length-1,r;o>=0;o--)(r=a[o])&&(n=r(n)||n);return n},_=(a,m)=>(e,t)=>m(e,t,a);const O=`SHEET_${S}`;exports.ThreadCommentResourceController=class extends d.Disposable{constructor(m,e,t){super(),this._resourceManagerService=m,this._threadCommentModel=e,this._threadCommentDataSourceService=t,this._initSnapshot()}_initSnapshot(){const m=t=>{const n=this._threadCommentModel.getUnit(t),o={};return n?(n.forEach(r=>{var c;const s=(c=o[r.subUnitId])!=null?c:[];s.push({...r.root,children:r.children}),o[r.subUnitId]=s}),JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(o,t))):""},e=t=>{if(!t)return{};try{return JSON.parse(t)}catch{return{}}};this.disposeWithMe(this._resourceManagerService.registerPluginResource({pluginName:O,businesses:[g.UNIVER_SHEET,g.UNIVER_DOC],toJson:t=>m(t),parseJson:t=>e(t),onUnLoad:t=>{this._threadCommentModel.deleteUnit(t)},onLoad:async(t,n)=>{Object.keys(n).forEach(o=>{const r=n[o];r.forEach(s=>{this._threadCommentModel.addComment(t,o,s)}),this._threadCommentModel.syncThreadComments(t,o,r.map(s=>s.threadId))})}}))}};exports.ThreadCommentResourceController=J([_(0,d.IResourceManagerService),_(1,d.Inject(exports.ThreadCommentModel)),_(2,p)],exports.ThreadCommentResourceController);const v={id:"thread-comment.mutation.add-comment",type:d.CommandType.MUTATION,handler(a,m,e){if(!m)return!1;const t=a.get(exports.ThreadCommentModel),{unitId:n,subUnitId:o,comment:r,sync:s}=m,c=s||(e==null?void 0:e.fromChangeset)&&!r.parentId;return t.addComment(n,o,r,c)}},y={id:"thread-comment.mutation.update-comment",type:d.CommandType.MUTATION,handler(a,m){if(!m)return!1;const e=a.get(exports.ThreadCommentModel),{unitId:t,subUnitId:n,payload:o,silent:r}=m;return e.updateComment(t,n,o,r)}},D={id:"thread-comment.mutation.update-comment-ref",type:d.CommandType.MUTATION,handler(a,m){if(!m)return!1;const e=a.get(exports.ThreadCommentModel),{unitId:t,subUnitId:n,payload:o,silent:r}=m;return e.updateCommentRef(t,n,o,r)}},E={id:"thread-comment.mutation.resolve-comment",type:d.CommandType.MUTATION,handler(a,m){if(!m)return!1;const e=a.get(exports.ThreadCommentModel),{unitId:t,subUnitId:n,resolved:o,commentId:r}=m;return e.resolveComment(t,n,r,o)}},f={id:"thread-comment.mutation.delete-comment",type:d.CommandType.MUTATION,handler(a,m){if(!m)return!1;const e=a.get(exports.ThreadCommentModel),{unitId:t,subUnitId:n,commentId:o}=m;return e.deleteComment(t,n,o)}},w={id:"thread-comment.command.add-comment",type:d.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const e=a.get(d.ICommandService),t=a.get(p),{comment:n}=m,o=await t.addComment(n),r=t.syncUpdateMutationToColla,s=!n.parentId,c={id:v.id,params:{...m,comment:o}};return s?await e.executeCommand(c.id,c.params):e.executeCommand(c.id,c.params,{onlyLocal:!r})}},P={id:"thread-comment.command.update-comment",type:d.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const{unitId:e,subUnitId:t,payload:n}=m,o=a.get(d.ICommandService),r=a.get(exports.ThreadCommentModel),s=a.get(p),c=s.syncUpdateMutationToColla,i=r.getComment(e,t,n.commentId);if(!i)return!1;const{children:h,...u}=i;if(!await s.updateComment({...u,...n}))return!1;const C={id:y.id,params:m};return o.executeCommand(C.id,C.params,{onlyLocal:!c}),!0}},A={id:"thread-comment.command.resolve-comment",type:d.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const{unitId:e,subUnitId:t,resolved:n,commentId:o}=m,r=a.get(p),c=a.get(exports.ThreadCommentModel).getComment(e,t,o),i=r.syncUpdateMutationToColla;return!c||!await r.resolveComment({...c,resolved:n})?!1:a.get(d.ICommandService).executeCommand(E.id,m,{onlyLocal:!i})}},x={id:"thread-comment.command.delete-comment",type:d.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const e=a.get(exports.ThreadCommentModel),t=a.get(p),n=a.get(d.ICommandService),{unitId:o,subUnitId:r,commentId:s}=m,c=t.syncUpdateMutationToColla,i=e.getComment(o,r,s);if(!i||!await t.deleteComment(o,r,i.threadId,s))return!1;const h={id:f.id,params:m};return n.executeCommand(h.id,h.params,{onlyLocal:!c})}},$={id:"thread-comment.command.delete-comment-tree",type:d.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const e=a.get(exports.ThreadCommentModel),t=a.get(d.ICommandService),n=a.get(p),{unitId:o,subUnitId:r,commentId:s}=m,c=e.getCommentWithChildren(o,r,s);return!c||!await n.deleteComment(o,r,c.root.threadId,s)?!1:await t.executeCommand(f.id,{unitId:o,subUnitId:r,commentId:c.root.id})}},b="thread-comment.config",R={};var Y=Object.getOwnPropertyDescriptor,k=(a,m,e,t)=>{for(var n=t>1?void 0:t?Y(m,e):m,o=a.length-1,r;o>=0;o--)(r=a[o])&&(n=r(n)||n);return n},T=(a,m)=>(e,t)=>m(e,t,a),M;exports.UniverThreadCommentPlugin=(M=class extends d.Plugin{constructor(m=R,e,t,n){super(),this._config=m,this._injector=e,this._commandService=t,this._configService=n;const{...o}=d.merge({},R,this._config);this._configService.setConfig(b,o)}onStarting(){var m;d.mergeOverrideWithDependencies([[p,{useClass:N}],[exports.ThreadCommentModel],[exports.ThreadCommentResourceController]],(m=this._config)==null?void 0:m.overrides).forEach(e=>{this._injector.add(e)}),[w,P,x,A,$,v,y,D,f,E].forEach(e=>{this._commandService.registerCommand(e)}),this._injector.get(exports.ThreadCommentResourceController)}},l(M,"pluginName",S),l(M,"type",d.UniverInstanceType.UNIVER_UNKNOWN),M);exports.UniverThreadCommentPlugin=k([T(1,d.Inject(d.Injector)),T(2,d.ICommandService),T(3,d.IConfigService)],exports.UniverThreadCommentPlugin);function K(a){return d.dayjs(a).format("YYYY/MM/DD HH:mm")}exports.AddCommentCommand=w;exports.AddCommentMutation=v;exports.DeleteCommentCommand=x;exports.DeleteCommentMutation=f;exports.DeleteCommentTreeCommand=$;exports.IThreadCommentDataSourceService=p;exports.ResolveCommentCommand=A;exports.ResolveCommentMutation=E;exports.SHEET_UNIVER_THREAD_COMMENT_PLUGIN=O;exports.TC_PLUGIN_NAME=S;exports.ThreadCommentDataSourceService=N;exports.UpdateCommentCommand=P;exports.UpdateCommentMutation=y;exports.UpdateCommentRefMutation=D;exports.getDT=K;
1
+ "use strict";var L=Object.defineProperty;var j=(a,m,e)=>m in a?L(a,m,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[m]=e;var l=(a,m,e)=>j(a,typeof m!="symbol"?m+"":m,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@univerjs/core"),V=require("rxjs");class R extends d.Disposable{constructor(){super();l(this,"_dataSource",null);l(this,"syncUpdateMutationToColla",!0)}set dataSource(e){this._dataSource=e}get dataSource(){return this._dataSource}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?t:e.id}}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,o){return this._dataSource?this._dataSource.deleteComment(e,t,n,o):!0}async listThreadComments(e,t,n){return this.dataSource?this.dataSource.listComments(e,t,n):!1}saveToSnapshot(e,t){if(this._dataSource){const n={};return Object.keys(e).forEach(o=>{const r=e[o];n[o]=r.map(this.dataSource.saveCommentToSnapshot)}),n}return e}}const p=d.createIdentifier("univer.thread-comment.data-source-service");var H=Object.getOwnPropertyDescriptor,W=(a,m,e,t)=>{for(var n=t>1?void 0:t?H(m,e):m,o=a.length-1,r;o>=0;o--)(r=a[o])&&(n=r(n)||n);return n},E=(a,m)=>(e,t)=>m(e,t,a);exports.ThreadCommentModel=class extends d.Disposable{constructor(e,t){super();l(this,"_commentsMap",new Map);l(this,"_threadMap",new Map);l(this,"_commentUpdate$",new V.Subject);l(this,"commentUpdate$",this._commentUpdate$.asObservable());l(this,"_tasks",[]);this._dataSourceService=e,this._lifecycleService=t,this.disposeWithMe(()=>{this._commentUpdate$.complete()}),this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe(n=>{const o=new Map;n===d.LifecycleStages.Rendered&&(this._tasks.forEach(({unitId:r,subUnitId:s,threadIds:c})=>{let i=o.get(r);i||(i=new Map,o.set(r,i));let h=i.get(s);h||(h=new Set,i.set(s,h));for(const u of c)h.add(u)}),this._tasks=[],o.forEach((r,s)=>{r.forEach((c,i)=>{this.syncThreadComments(s,i,Array.from(c))})}))}))}_ensureCommentMap(e,t){let n=this._commentsMap.get(e);n||(n=new Map,this._commentsMap.set(e,n));let o=n.get(t);return o||(o=new Map,n.set(t,o)),o}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 o=n.get(t);return o||(o=new Map,n.set(t,o)),o}_replaceComment(e,t,n){const o=this._ensureCommentMap(e,t),r=o.get(n.id);if(r){const{children:s,...c}=n,i={...c,ref:r.ref};o.set(n.id,i),s==null||s.forEach(h=>{o.set(h.id,{...h,ref:""})}),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"syncUpdate",payload:i}),!!n.resolved!=!!r.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<d.LifecycleStages.Rendered){this._tasks.push({unitId:e,subUnitId:t,threadIds:n});return}const o=this._ensureThreadMap(e,t),r=this._ensureCommentMap(e,t),s=await this._dataSourceService.listThreadComments(e,t,n);if(!s)return;const c=new Set(n);s.forEach(i=>{this._replaceComment(e,t,i),c.delete(i.threadId)}),c.forEach(i=>{o.delete(i),r.forEach((h,u)=>{h.threadId===i&&r.delete(u)})})}addComment(e,t,n,o){const r=this._ensureCommentMap(e,t),{parentId:s,children:c=[],...i}=n,h={...i,parentId:s===n.id?void 0:s};h.threadId||(h.threadId=h.parentId||h.id);const u=C=>{r.set(C.id,C),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"add",payload:C,isRoot:!C.parentId})};u(h);const N=this._ensureThreadMap(e,t);if(!h.parentId){N.set(h.threadId,h);for(const C of c)u(C)}return o&&this.syncThreadComments(e,t,[h.threadId]),!0}updateComment(e,t,n,o){const s=this._ensureCommentMap(e,t).get(n.commentId);return s&&(s.updated=!0,s.text=n.text,s.attachments=n.attachments,s.updateT=n.updateT,this._commentUpdate$.next({unitId:e,subUnitId:t,type:"update",payload:n,silent:o})),!0}updateCommentRef(e,t,n,o){const s=this._ensureCommentMap(e,t).get(n.commentId);return s?(s.ref=n.ref,this._commentUpdate$.next({unitId:e,subUnitId:t,type:"updateRef",payload:n,silent:o,threadId:s.threadId}),!0):!1}resolveComment(e,t,n,o){const s=this._ensureCommentMap(e,t).get(n);return s?(s.resolved=o,this._commentUpdate$.next({unitId:e,subUnitId:t,type:"resolve",payload:{commentId:n,resolved:o}}),!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){const o=this._ensureCommentMap(e,t),r=Array.from(o.values()).filter(h=>h.threadId===n);let s;const c=[],i=new Set;for(const h of r)h.parentId?c.push(h):s=h,i.add(h.personId);if(s)return{root:s,children:c,relativeUsers:i,unitId:e,subUnitId:t,threadId:n}}getCommentWithChildren(e,t,n){const o=this.getComment(e,t,n);if(o)return this.getThread(e,t,o.threadId)}_deleteComment(e,t,n){const o=this._ensureCommentMap(e,t),r=o.get(n);r&&(o.delete(n),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"delete",payload:{commentId:n,isRoot:!r.parentId,comment:r}}))}deleteThread(e,t,n){this._ensureThreadMap(e,t).delete(n),this._ensureCommentMap(e,t).forEach(s=>{s.threadId===n&&this._deleteComment(e,t,s.id)})}deleteComment(e,t,n){const 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){const t=this._commentsMap.get(e);t&&t.forEach((n,o)=>{n.forEach(r=>{this.deleteComment(e,o,r.id)})})}getUnit(e){const t=this._threadMap.get(e);if(!t)return[];const n=[];return t.forEach((o,r)=>{o.forEach((s,c)=>{const i=this.getThread(e,r,c);i&&n.push(i)})}),n}getAll(){const e=[];return this._commentsMap.forEach((t,n)=>{e.push({unitId:n,threads:this.getUnit(n)})}),e}};exports.ThreadCommentModel=W([E(0,d.Inject(p)),E(1,d.Inject(d.LifecycleService))],exports.ThreadCommentModel);var g=(a=>(a[a.UNIVER_UNKNOWN=0]="UNIVER_UNKNOWN",a[a.UNIVER_DOC=1]="UNIVER_DOC",a[a.UNIVER_SHEET=2]="UNIVER_SHEET",a[a.UNIVER_SLIDE=3]="UNIVER_SLIDE",a[a.UNIVER_PROJECT=4]="UNIVER_PROJECT",a[a.UNRECOGNIZED=-1]="UNRECOGNIZED",a))(g||{});const S="UNIVER_THREAD_COMMENT_PLUGIN";var G=Object.getOwnPropertyDescriptor,J=(a,m,e,t)=>{for(var n=t>1?void 0:t?G(m,e):m,o=a.length-1,r;o>=0;o--)(r=a[o])&&(n=r(n)||n);return n},_=(a,m)=>(e,t)=>m(e,t,a);const O=`SHEET_${S}`;exports.ThreadCommentResourceController=class extends d.Disposable{constructor(m,e,t){super(),this._resourceManagerService=m,this._threadCommentModel=e,this._threadCommentDataSourceService=t,this._initSnapshot()}_initSnapshot(){const m=t=>{const n=this._threadCommentModel.getUnit(t),o={};return n?(n.forEach(r=>{var c;const s=(c=o[r.subUnitId])!=null?c:[];s.push({...r.root,children:r.children}),o[r.subUnitId]=s}),JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(o,t))):""},e=t=>{if(!t)return{};try{return JSON.parse(t)}catch{return{}}};this.disposeWithMe(this._resourceManagerService.registerPluginResource({pluginName:O,businesses:[g.UNIVER_SHEET,g.UNIVER_DOC],toJson:t=>m(t),parseJson:t=>e(t),onUnLoad:t=>{this._threadCommentModel.deleteUnit(t)},onLoad:async(t,n)=>{Object.keys(n).forEach(o=>{const r=n[o];r.forEach(s=>{this._threadCommentModel.addComment(t,o,s)}),this._threadCommentModel.syncThreadComments(t,o,r.map(s=>s.threadId))})}}))}};exports.ThreadCommentResourceController=J([_(0,d.IResourceManagerService),_(1,d.Inject(exports.ThreadCommentModel)),_(2,p)],exports.ThreadCommentResourceController);const v={id:"thread-comment.mutation.add-comment",type:d.CommandType.MUTATION,handler(a,m,e){if(!m)return!1;const t=a.get(exports.ThreadCommentModel),{unitId:n,subUnitId:o,comment:r,sync:s}=m,c=s||(e==null?void 0:e.fromChangeset)&&!r.parentId;return t.addComment(n,o,r,c)}},y={id:"thread-comment.mutation.update-comment",type:d.CommandType.MUTATION,handler(a,m){if(!m)return!1;const e=a.get(exports.ThreadCommentModel),{unitId:t,subUnitId:n,payload:o,silent:r}=m;return e.updateComment(t,n,o,r)}},D={id:"thread-comment.mutation.update-comment-ref",type:d.CommandType.MUTATION,handler(a,m){if(!m)return!1;const e=a.get(exports.ThreadCommentModel),{unitId:t,subUnitId:n,payload:o,silent:r}=m;return e.updateCommentRef(t,n,o,r)}},U={id:"thread-comment.mutation.resolve-comment",type:d.CommandType.MUTATION,handler(a,m){if(!m)return!1;const e=a.get(exports.ThreadCommentModel),{unitId:t,subUnitId:n,resolved:o,commentId:r}=m;return e.resolveComment(t,n,r,o)}},f={id:"thread-comment.mutation.delete-comment",type:d.CommandType.MUTATION,handler(a,m){if(!m)return!1;const e=a.get(exports.ThreadCommentModel),{unitId:t,subUnitId:n,commentId:o}=m;return e.deleteComment(t,n,o)}},w={id:"thread-comment.command.add-comment",type:d.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const e=a.get(d.ICommandService),t=a.get(p),{comment:n}=m,o=await t.addComment(n),r=t.syncUpdateMutationToColla,s=!n.parentId,c={id:v.id,params:{...m,comment:o}};return s?await e.executeCommand(c.id,c.params):e.executeCommand(c.id,c.params,{onlyLocal:!r})}},P={id:"thread-comment.command.update-comment",type:d.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const{unitId:e,subUnitId:t,payload:n}=m,o=a.get(d.ICommandService),r=a.get(exports.ThreadCommentModel),s=a.get(p),c=s.syncUpdateMutationToColla,i=r.getComment(e,t,n.commentId);if(!i)return!1;const{children:h,...u}=i;if(!await s.updateComment({...u,...n}))return!1;const C={id:y.id,params:m};return o.executeCommand(C.id,C.params,{onlyLocal:!c}),!0}},A={id:"thread-comment.command.resolve-comment",type:d.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const{unitId:e,subUnitId:t,resolved:n,commentId:o}=m,r=a.get(p),c=a.get(exports.ThreadCommentModel).getComment(e,t,o),i=r.syncUpdateMutationToColla;return!c||!await r.resolveComment({...c,resolved:n})?!1:a.get(d.ICommandService).executeCommand(U.id,m,{onlyLocal:!i})}},x={id:"thread-comment.command.delete-comment",type:d.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const e=a.get(exports.ThreadCommentModel),t=a.get(p),n=a.get(d.ICommandService),{unitId:o,subUnitId:r,commentId:s}=m,c=t.syncUpdateMutationToColla,i=e.getComment(o,r,s);if(!i||!await t.deleteComment(o,r,i.threadId,s))return!1;const h={id:f.id,params:m};return n.executeCommand(h.id,h.params,{onlyLocal:!c})}},$={id:"thread-comment.command.delete-comment-tree",type:d.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const e=a.get(exports.ThreadCommentModel),t=a.get(d.ICommandService),n=a.get(p),{unitId:o,subUnitId:r,commentId:s}=m,c=e.getCommentWithChildren(o,r,s);return!c||!await n.deleteComment(o,r,c.root.threadId,s)?!1:await t.executeCommand(f.id,{unitId:o,subUnitId:r,commentId:c.root.id})}},b="thread-comment.config",I={};var Y=Object.getOwnPropertyDescriptor,k=(a,m,e,t)=>{for(var n=t>1?void 0:t?Y(m,e):m,o=a.length-1,r;o>=0;o--)(r=a[o])&&(n=r(n)||n);return n},T=(a,m)=>(e,t)=>m(e,t,a),M;exports.UniverThreadCommentPlugin=(M=class extends d.Plugin{constructor(m=I,e,t,n){super(),this._config=m,this._injector=e,this._commandService=t,this._configService=n;const{...o}=d.merge({},I,this._config);this._configService.setConfig(b,o)}onStarting(){var m;d.mergeOverrideWithDependencies([[p,{useClass:R}],[exports.ThreadCommentModel],[exports.ThreadCommentResourceController]],(m=this._config)==null?void 0:m.overrides).forEach(e=>{this._injector.add(e)}),[w,P,x,A,$,v,y,D,f,U].forEach(e=>{this._commandService.registerCommand(e)}),this._injector.get(exports.ThreadCommentResourceController)}},l(M,"pluginName",S),l(M,"type",d.UniverInstanceType.UNIVER_UNKNOWN),M);exports.UniverThreadCommentPlugin=k([T(1,d.Inject(d.Injector)),T(2,d.ICommandService),T(3,d.IConfigService)],exports.UniverThreadCommentPlugin);function K(a){return d.dayjs(a).format("YYYY/MM/DD HH:mm")}exports.AddCommentCommand=w;exports.AddCommentMutation=v;exports.DeleteCommentCommand=x;exports.DeleteCommentMutation=f;exports.DeleteCommentTreeCommand=$;exports.IThreadCommentDataSourceService=p;exports.ResolveCommentCommand=A;exports.ResolveCommentMutation=U;exports.SHEET_UNIVER_THREAD_COMMENT_PLUGIN=O;exports.TC_PLUGIN_NAME=S;exports.ThreadCommentDataSourceService=R;exports.UpdateCommentCommand=P;exports.UpdateCommentMutation=y;exports.UpdateCommentRefMutation=D;exports.getDT=K;
package/lib/es/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  var $ = Object.defineProperty;
2
2
  var L = (a, t, e) => t in a ? $(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e;
3
3
  var u = (a, t, e) => L(a, typeof t != "symbol" ? t + "" : t, e);
4
- import { createIdentifier as V, Disposable as E, Inject as g, LifecycleService as j, LifecycleStages as N, IResourceManagerService as H, CommandType as l, ICommandService as f, Plugin as W, merge as J, UniverInstanceType as G, mergeOverrideWithDependencies as Y, Injector as k, IConfigService as K, dayjs as B } from "@univerjs/core";
4
+ import { createIdentifier as V, Disposable as U, Inject as g, LifecycleService as j, LifecycleStages as O, IResourceManagerService as H, CommandType as l, ICommandService as f, Plugin as W, merge as J, UniverInstanceType as G, mergeOverrideWithDependencies as Y, Injector as k, IConfigService as K, dayjs as B } from "@univerjs/core";
5
5
  import { Subject as b } from "rxjs";
6
- class Z extends E {
6
+ class Z extends U {
7
7
  constructor() {
8
8
  super();
9
9
  u(this, "_dataSource", null);
@@ -51,7 +51,7 @@ var F = Object.getOwnPropertyDescriptor, q = (a, t, e, n) => {
51
51
  (s = a[r]) && (o = s(o) || o);
52
52
  return o;
53
53
  }, I = (a, t) => (e, n) => t(e, n, a);
54
- let i = class extends E {
54
+ let i = class extends U {
55
55
  constructor(t, e) {
56
56
  super();
57
57
  u(this, "_commentsMap", /* @__PURE__ */ new Map());
@@ -63,7 +63,7 @@ let i = class extends E {
63
63
  this._commentUpdate$.complete();
64
64
  }), this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe((n) => {
65
65
  const o = /* @__PURE__ */ new Map();
66
- n === N.Rendered && (this._tasks.forEach(({ unitId: r, subUnitId: s, threadIds: d }) => {
66
+ n === O.Rendered && (this._tasks.forEach(({ unitId: r, subUnitId: s, threadIds: d }) => {
67
67
  let m = o.get(r);
68
68
  m || (m = /* @__PURE__ */ new Map(), o.set(r, m));
69
69
  let c = m.get(s);
@@ -121,7 +121,7 @@ let i = class extends E {
121
121
  }
122
122
  }
123
123
  async syncThreadComments(t, e, n) {
124
- if (this._lifecycleService.stage < N.Rendered) {
124
+ if (this._lifecycleService.stage < O.Rendered) {
125
125
  this._tasks.push({ unitId: t, subUnitId: e, threadIds: n });
126
126
  return;
127
127
  }
@@ -285,7 +285,7 @@ var z = Object.getOwnPropertyDescriptor, Q = (a, t, e, n) => {
285
285
  return o;
286
286
  }, v = (a, t) => (e, n) => t(e, n, a);
287
287
  const X = `SHEET_${w}`;
288
- let S = class extends E {
288
+ let S = class extends U {
289
289
  constructor(a, t, e) {
290
290
  super(), this._resourceManagerService = a, this._threadCommentModel = t, this._threadCommentDataSourceService = e, this._initSnapshot();
291
291
  }
@@ -371,7 +371,7 @@ const x = {
371
371
  const e = a.get(i), { unitId: n, subUnitId: o, resolved: r, commentId: s } = t;
372
372
  return e.resolveComment(n, o, s, r);
373
373
  }
374
- }, U = {
374
+ }, N = {
375
375
  id: "thread-comment.mutation.delete-comment",
376
376
  type: l.MUTATION,
377
377
  handler(a, t) {
@@ -416,11 +416,11 @@ const x = {
416
416
  ...o
417
417
  }))
418
418
  return !1;
419
- const O = {
419
+ const E = {
420
420
  id: A.id,
421
421
  params: t
422
422
  };
423
- return r.executeCommand(O.id, O.params, { onlyLocal: !m }), !0;
423
+ return r.executeCommand(E.id, E.params, { onlyLocal: !m }), !0;
424
424
  }
425
425
  }, oe = {
426
426
  id: "thread-comment.command.resolve-comment",
@@ -448,7 +448,7 @@ const x = {
448
448
  if (!c || !await n.deleteComment(r, s, c.threadId, d))
449
449
  return !1;
450
450
  const h = {
451
- id: U.id,
451
+ id: N.id,
452
452
  params: t
453
453
  };
454
454
  return o.executeCommand(h.id, h.params, { onlyLocal: !m });
@@ -460,7 +460,7 @@ const x = {
460
460
  if (!t)
461
461
  return !1;
462
462
  const e = a.get(i), n = a.get(f), o = a.get(p), { unitId: r, subUnitId: s, commentId: d } = t, m = e.getCommentWithChildren(r, s, d);
463
- return !m || !await o.deleteComment(r, s, m.root.threadId, d) ? !1 : await n.executeCommand(U.id, {
463
+ return !m || !await o.deleteComment(r, s, m.root.threadId, d) ? !1 : await n.executeCommand(N.id, {
464
464
  unitId: r,
465
465
  subUnitId: s,
466
466
  commentId: m.root.id
@@ -501,7 +501,7 @@ let D = (M = class extends W {
501
501
  x,
502
502
  A,
503
503
  ee,
504
- U,
504
+ N,
505
505
  P
506
506
  ].forEach((t) => {
507
507
  this._commandService.registerCommand(t);
@@ -520,7 +520,7 @@ export {
520
520
  te as AddCommentCommand,
521
521
  x as AddCommentMutation,
522
522
  re as DeleteCommentCommand,
523
- U as DeleteCommentMutation,
523
+ N as DeleteCommentMutation,
524
524
  ae as DeleteCommentTreeCommand,
525
525
  p as IThreadCommentDataSourceService,
526
526
  oe as ResolveCommentCommand,
package/lib/umd/index.js CHANGED
@@ -1 +1 @@
1
- (function(s,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("@univerjs/core"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs"],c):(s=typeof globalThis<"u"?globalThis:s||self,c(s.UniverThreadComment={},s.UniverCore,s.rxjs))})(this,function(s,c,M){"use strict";var B=Object.defineProperty;var q=(s,c,M)=>c in s?B(s,c,{enumerable:!0,configurable:!0,writable:!0,value:M}):s[c]=M;var u=(s,c,M)=>q(s,typeof c!="symbol"?c+"":c,M);var T;class R extends c.Disposable{constructor(){super();u(this,"_dataSource",null);u(this,"syncUpdateMutationToColla",!0)}set dataSource(e){this._dataSource=e}get dataSource(){return this._dataSource}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?t:e.id}}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,o){return this._dataSource?this._dataSource.deleteComment(e,t,n,o):!0}async listThreadComments(e,t,n){return this.dataSource?this.dataSource.listComments(e,t,n):!1}saveToSnapshot(e,t){if(this._dataSource){const n={};return Object.keys(e).forEach(o=>{const a=e[o];n[o]=a.map(this.dataSource.saveCommentToSnapshot)}),n}return e}}const C=c.createIdentifier("univer.thread-comment.data-source-service");var H=Object.getOwnPropertyDescriptor,W=(m,r,e,t)=>{for(var n=t>1?void 0:t?H(r,e):r,o=m.length-1,a;o>=0;o--)(a=m[o])&&(n=a(n)||n);return n},N=(m,r)=>(e,t)=>r(e,t,m);s.ThreadCommentModel=class extends c.Disposable{constructor(e,t){super();u(this,"_commentsMap",new Map);u(this,"_threadMap",new Map);u(this,"_commentUpdate$",new M.Subject);u(this,"commentUpdate$",this._commentUpdate$.asObservable());u(this,"_tasks",[]);this._dataSourceService=e,this._lifecycleService=t,this.disposeWithMe(()=>{this._commentUpdate$.complete()}),this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe(n=>{const o=new Map;n===c.LifecycleStages.Rendered&&(this._tasks.forEach(({unitId:a,subUnitId:d,threadIds:i})=>{let h=o.get(a);h||(h=new Map,o.set(a,h));let l=h.get(d);l||(l=new Set,h.set(d,l));for(const p of i)l.add(p)}),this._tasks=[],o.forEach((a,d)=>{a.forEach((i,h)=>{this.syncThreadComments(d,h,Array.from(i))})}))}))}_ensureCommentMap(e,t){let n=this._commentsMap.get(e);n||(n=new Map,this._commentsMap.set(e,n));let o=n.get(t);return o||(o=new Map,n.set(t,o)),o}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 o=n.get(t);return o||(o=new Map,n.set(t,o)),o}_replaceComment(e,t,n){const o=this._ensureCommentMap(e,t),a=o.get(n.id);if(a){const{children:d,...i}=n,h={...i,ref:a.ref};o.set(n.id,h),d==null||d.forEach(l=>{o.set(l.id,{...l,ref:""})}),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"syncUpdate",payload:h}),!!n.resolved!=!!a.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.LifecycleStages.Rendered){this._tasks.push({unitId:e,subUnitId:t,threadIds:n});return}const o=this._ensureThreadMap(e,t),a=this._ensureCommentMap(e,t),d=await this._dataSourceService.listThreadComments(e,t,n);if(!d)return;const i=new Set(n);d.forEach(h=>{this._replaceComment(e,t,h),i.delete(h.threadId)}),i.forEach(h=>{o.delete(h),a.forEach((l,p)=>{l.threadId===h&&a.delete(p)})})}addComment(e,t,n,o){const a=this._ensureCommentMap(e,t),{parentId:d,children:i=[],...h}=n,l={...h,parentId:d===n.id?void 0:d};l.threadId||(l.threadId=l.parentId||l.id);const p=f=>{a.set(f.id,f),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"add",payload:f,isRoot:!f.parentId})};p(l);const V=this._ensureThreadMap(e,t);if(!l.parentId){V.set(l.threadId,l);for(const f of i)p(f)}return o&&this.syncThreadComments(e,t,[l.threadId]),!0}updateComment(e,t,n,o){const d=this._ensureCommentMap(e,t).get(n.commentId);return d&&(d.updated=!0,d.text=n.text,d.attachments=n.attachments,d.updateT=n.updateT,this._commentUpdate$.next({unitId:e,subUnitId:t,type:"update",payload:n,silent:o})),!0}updateCommentRef(e,t,n,o){const d=this._ensureCommentMap(e,t).get(n.commentId);return d?(d.ref=n.ref,this._commentUpdate$.next({unitId:e,subUnitId:t,type:"updateRef",payload:n,silent:o,threadId:d.threadId}),!0):!1}resolveComment(e,t,n,o){const d=this._ensureCommentMap(e,t).get(n);return d?(d.resolved=o,this._commentUpdate$.next({unitId:e,subUnitId:t,type:"resolve",payload:{commentId:n,resolved:o}}),!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){const o=this._ensureCommentMap(e,t),a=Array.from(o.values()).filter(l=>l.threadId===n);let d;const i=[],h=new Set;for(const l of a)l.parentId?i.push(l):d=l,h.add(l.personId);if(d)return{root:d,children:i,relativeUsers:h,unitId:e,subUnitId:t,threadId:n}}getCommentWithChildren(e,t,n){const o=this.getComment(e,t,n);if(o)return this.getThread(e,t,o.threadId)}_deleteComment(e,t,n){const o=this._ensureCommentMap(e,t),a=o.get(n);a&&(o.delete(n),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"delete",payload:{commentId:n,isRoot:!a.parentId,comment:a}}))}deleteThread(e,t,n){this._ensureThreadMap(e,t).delete(n),this._ensureCommentMap(e,t).forEach(d=>{d.threadId===n&&this._deleteComment(e,t,d.id)})}deleteComment(e,t,n){const a=this._ensureCommentMap(e,t).get(n);return a&&(a.parentId?this._deleteComment(e,t,n):this.deleteThread(e,t,a.threadId)),!0}deleteUnit(e){const t=this._commentsMap.get(e);t&&t.forEach((n,o)=>{n.forEach(a=>{this.deleteComment(e,o,a.id)})})}getUnit(e){const t=this._threadMap.get(e);if(!t)return[];const n=[];return t.forEach((o,a)=>{o.forEach((d,i)=>{const h=this.getThread(e,a,i);h&&n.push(h)})}),n}getAll(){const e=[];return this._commentsMap.forEach((t,n)=>{e.push({unitId:n,threads:this.getUnit(n)})}),e}},s.ThreadCommentModel=W([N(0,c.Inject(C)),N(1,c.Inject(c.LifecycleService))],s.ThreadCommentModel);var v=(m=>(m[m.UNIVER_UNKNOWN=0]="UNIVER_UNKNOWN",m[m.UNIVER_DOC=1]="UNIVER_DOC",m[m.UNIVER_SHEET=2]="UNIVER_SHEET",m[m.UNIVER_SLIDE=3]="UNIVER_SLIDE",m[m.UNIVER_PROJECT=4]="UNIVER_PROJECT",m[m.UNRECOGNIZED=-1]="UNRECOGNIZED",m))(v||{});const S="UNIVER_THREAD_COMMENT_PLUGIN";var G=Object.getOwnPropertyDescriptor,J=(m,r,e,t)=>{for(var n=t>1?void 0:t?G(r,e):r,o=m.length-1,a;o>=0;o--)(a=m[o])&&(n=a(n)||n);return n},g=(m,r)=>(e,t)=>r(e,t,m);const O=`SHEET_${S}`;s.ThreadCommentResourceController=class extends c.Disposable{constructor(r,e,t){super(),this._resourceManagerService=r,this._threadCommentModel=e,this._threadCommentDataSourceService=t,this._initSnapshot()}_initSnapshot(){const r=t=>{const n=this._threadCommentModel.getUnit(t),o={};return n?(n.forEach(a=>{var i;const d=(i=o[a.subUnitId])!=null?i:[];d.push({...a.root,children:a.children}),o[a.subUnitId]=d}),JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(o,t))):""},e=t=>{if(!t)return{};try{return JSON.parse(t)}catch{return{}}};this.disposeWithMe(this._resourceManagerService.registerPluginResource({pluginName:O,businesses:[v.UNIVER_SHEET,v.UNIVER_DOC],toJson:t=>r(t),parseJson:t=>e(t),onUnLoad:t=>{this._threadCommentModel.deleteUnit(t)},onLoad:async(t,n)=>{Object.keys(n).forEach(o=>{const a=n[o];a.forEach(d=>{this._threadCommentModel.addComment(t,o,d)}),this._threadCommentModel.syncThreadComments(t,o,a.map(d=>d.threadId))})}}))}},s.ThreadCommentResourceController=J([g(0,c.IResourceManagerService),g(1,c.Inject(s.ThreadCommentModel)),g(2,C)],s.ThreadCommentResourceController);const y={id:"thread-comment.mutation.add-comment",type:c.CommandType.MUTATION,handler(m,r,e){if(!r)return!1;const t=m.get(s.ThreadCommentModel),{unitId:n,subUnitId:o,comment:a,sync:d}=r,i=d||(e==null?void 0:e.fromChangeset)&&!a.parentId;return t.addComment(n,o,a,i)}},E={id:"thread-comment.mutation.update-comment",type:c.CommandType.MUTATION,handler(m,r){if(!r)return!1;const e=m.get(s.ThreadCommentModel),{unitId:t,subUnitId:n,payload:o,silent:a}=r;return e.updateComment(t,n,o,a)}},D={id:"thread-comment.mutation.update-comment-ref",type:c.CommandType.MUTATION,handler(m,r){if(!r)return!1;const e=m.get(s.ThreadCommentModel),{unitId:t,subUnitId:n,payload:o,silent:a}=r;return e.updateCommentRef(t,n,o,a)}},U={id:"thread-comment.mutation.resolve-comment",type:c.CommandType.MUTATION,handler(m,r){if(!r)return!1;const e=m.get(s.ThreadCommentModel),{unitId:t,subUnitId:n,resolved:o,commentId:a}=r;return e.resolveComment(t,n,a,o)}},_={id:"thread-comment.mutation.delete-comment",type:c.CommandType.MUTATION,handler(m,r){if(!r)return!1;const e=m.get(s.ThreadCommentModel),{unitId:t,subUnitId:n,commentId:o}=r;return e.deleteComment(t,n,o)}},w={id:"thread-comment.command.add-comment",type:c.CommandType.COMMAND,async handler(m,r){if(!r)return!1;const e=m.get(c.ICommandService),t=m.get(C),{comment:n}=r,o=await t.addComment(n),a=t.syncUpdateMutationToColla,d=!n.parentId,i={id:y.id,params:{...r,comment:o}};return d?await e.executeCommand(i.id,i.params):e.executeCommand(i.id,i.params,{onlyLocal:!a})}},P={id:"thread-comment.command.update-comment",type:c.CommandType.COMMAND,async handler(m,r){if(!r)return!1;const{unitId:e,subUnitId:t,payload:n}=r,o=m.get(c.ICommandService),a=m.get(s.ThreadCommentModel),d=m.get(C),i=d.syncUpdateMutationToColla,h=a.getComment(e,t,n.commentId);if(!h)return!1;const{children:l,...p}=h;if(!await d.updateComment({...p,...n}))return!1;const f={id:E.id,params:r};return o.executeCommand(f.id,f.params,{onlyLocal:!i}),!0}},A={id:"thread-comment.command.resolve-comment",type:c.CommandType.COMMAND,async handler(m,r){if(!r)return!1;const{unitId:e,subUnitId:t,resolved:n,commentId:o}=r,a=m.get(C),i=m.get(s.ThreadCommentModel).getComment(e,t,o),h=a.syncUpdateMutationToColla;return!i||!await a.resolveComment({...i,resolved:n})?!1:m.get(c.ICommandService).executeCommand(U.id,r,{onlyLocal:!h})}},j={id:"thread-comment.command.delete-comment",type:c.CommandType.COMMAND,async handler(m,r){if(!r)return!1;const e=m.get(s.ThreadCommentModel),t=m.get(C),n=m.get(c.ICommandService),{unitId:o,subUnitId:a,commentId:d}=r,i=t.syncUpdateMutationToColla,h=e.getComment(o,a,d);if(!h||!await t.deleteComment(o,a,h.threadId,d))return!1;const l={id:_.id,params:r};return n.executeCommand(l.id,l.params,{onlyLocal:!i})}},$={id:"thread-comment.command.delete-comment-tree",type:c.CommandType.COMMAND,async handler(m,r){if(!r)return!1;const e=m.get(s.ThreadCommentModel),t=m.get(c.ICommandService),n=m.get(C),{unitId:o,subUnitId:a,commentId:d}=r,i=e.getCommentWithChildren(o,a,d);return!i||!await n.deleteComment(o,a,i.root.threadId,d)?!1:await t.executeCommand(_.id,{unitId:o,subUnitId:a,commentId:i.root.id})}},b="thread-comment.config",L={};var Y=Object.getOwnPropertyDescriptor,k=(m,r,e,t)=>{for(var n=t>1?void 0:t?Y(r,e):r,o=m.length-1,a;o>=0;o--)(a=m[o])&&(n=a(n)||n);return n},I=(m,r)=>(e,t)=>r(e,t,m);s.UniverThreadCommentPlugin=(T=class extends c.Plugin{constructor(r=L,e,t,n){super(),this._config=r,this._injector=e,this._commandService=t,this._configService=n;const{...o}=c.merge({},L,this._config);this._configService.setConfig(b,o)}onStarting(){var r;c.mergeOverrideWithDependencies([[C,{useClass:R}],[s.ThreadCommentModel],[s.ThreadCommentResourceController]],(r=this._config)==null?void 0:r.overrides).forEach(e=>{this._injector.add(e)}),[w,P,j,A,$,y,E,D,_,U].forEach(e=>{this._commandService.registerCommand(e)}),this._injector.get(s.ThreadCommentResourceController)}},u(T,"pluginName",S),u(T,"type",c.UniverInstanceType.UNIVER_UNKNOWN),T),s.UniverThreadCommentPlugin=k([I(1,c.Inject(c.Injector)),I(2,c.ICommandService),I(3,c.IConfigService)],s.UniverThreadCommentPlugin);function K(m){return c.dayjs(m).format("YYYY/MM/DD HH:mm")}s.AddCommentCommand=w,s.AddCommentMutation=y,s.DeleteCommentCommand=j,s.DeleteCommentMutation=_,s.DeleteCommentTreeCommand=$,s.IThreadCommentDataSourceService=C,s.ResolveCommentCommand=A,s.ResolveCommentMutation=U,s.SHEET_UNIVER_THREAD_COMMENT_PLUGIN=O,s.TC_PLUGIN_NAME=S,s.ThreadCommentDataSourceService=R,s.UpdateCommentCommand=P,s.UpdateCommentMutation=E,s.UpdateCommentRefMutation=D,s.getDT=K,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
1
+ (function(s,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("@univerjs/core"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs"],c):(s=typeof globalThis<"u"?globalThis:s||self,c(s.UniverThreadComment={},s.UniverCore,s.rxjs))})(this,function(s,c,M){"use strict";var B=Object.defineProperty;var q=(s,c,M)=>c in s?B(s,c,{enumerable:!0,configurable:!0,writable:!0,value:M}):s[c]=M;var u=(s,c,M)=>q(s,typeof c!="symbol"?c+"":c,M);var T;class I extends c.Disposable{constructor(){super();u(this,"_dataSource",null);u(this,"syncUpdateMutationToColla",!0)}set dataSource(e){this._dataSource=e}get dataSource(){return this._dataSource}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?t:e.id}}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,o){return this._dataSource?this._dataSource.deleteComment(e,t,n,o):!0}async listThreadComments(e,t,n){return this.dataSource?this.dataSource.listComments(e,t,n):!1}saveToSnapshot(e,t){if(this._dataSource){const n={};return Object.keys(e).forEach(o=>{const a=e[o];n[o]=a.map(this.dataSource.saveCommentToSnapshot)}),n}return e}}const C=c.createIdentifier("univer.thread-comment.data-source-service");var H=Object.getOwnPropertyDescriptor,W=(m,r,e,t)=>{for(var n=t>1?void 0:t?H(r,e):r,o=m.length-1,a;o>=0;o--)(a=m[o])&&(n=a(n)||n);return n},R=(m,r)=>(e,t)=>r(e,t,m);s.ThreadCommentModel=class extends c.Disposable{constructor(e,t){super();u(this,"_commentsMap",new Map);u(this,"_threadMap",new Map);u(this,"_commentUpdate$",new M.Subject);u(this,"commentUpdate$",this._commentUpdate$.asObservable());u(this,"_tasks",[]);this._dataSourceService=e,this._lifecycleService=t,this.disposeWithMe(()=>{this._commentUpdate$.complete()}),this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe(n=>{const o=new Map;n===c.LifecycleStages.Rendered&&(this._tasks.forEach(({unitId:a,subUnitId:d,threadIds:i})=>{let h=o.get(a);h||(h=new Map,o.set(a,h));let l=h.get(d);l||(l=new Set,h.set(d,l));for(const p of i)l.add(p)}),this._tasks=[],o.forEach((a,d)=>{a.forEach((i,h)=>{this.syncThreadComments(d,h,Array.from(i))})}))}))}_ensureCommentMap(e,t){let n=this._commentsMap.get(e);n||(n=new Map,this._commentsMap.set(e,n));let o=n.get(t);return o||(o=new Map,n.set(t,o)),o}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 o=n.get(t);return o||(o=new Map,n.set(t,o)),o}_replaceComment(e,t,n){const o=this._ensureCommentMap(e,t),a=o.get(n.id);if(a){const{children:d,...i}=n,h={...i,ref:a.ref};o.set(n.id,h),d==null||d.forEach(l=>{o.set(l.id,{...l,ref:""})}),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"syncUpdate",payload:h}),!!n.resolved!=!!a.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.LifecycleStages.Rendered){this._tasks.push({unitId:e,subUnitId:t,threadIds:n});return}const o=this._ensureThreadMap(e,t),a=this._ensureCommentMap(e,t),d=await this._dataSourceService.listThreadComments(e,t,n);if(!d)return;const i=new Set(n);d.forEach(h=>{this._replaceComment(e,t,h),i.delete(h.threadId)}),i.forEach(h=>{o.delete(h),a.forEach((l,p)=>{l.threadId===h&&a.delete(p)})})}addComment(e,t,n,o){const a=this._ensureCommentMap(e,t),{parentId:d,children:i=[],...h}=n,l={...h,parentId:d===n.id?void 0:d};l.threadId||(l.threadId=l.parentId||l.id);const p=f=>{a.set(f.id,f),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"add",payload:f,isRoot:!f.parentId})};p(l);const V=this._ensureThreadMap(e,t);if(!l.parentId){V.set(l.threadId,l);for(const f of i)p(f)}return o&&this.syncThreadComments(e,t,[l.threadId]),!0}updateComment(e,t,n,o){const d=this._ensureCommentMap(e,t).get(n.commentId);return d&&(d.updated=!0,d.text=n.text,d.attachments=n.attachments,d.updateT=n.updateT,this._commentUpdate$.next({unitId:e,subUnitId:t,type:"update",payload:n,silent:o})),!0}updateCommentRef(e,t,n,o){const d=this._ensureCommentMap(e,t).get(n.commentId);return d?(d.ref=n.ref,this._commentUpdate$.next({unitId:e,subUnitId:t,type:"updateRef",payload:n,silent:o,threadId:d.threadId}),!0):!1}resolveComment(e,t,n,o){const d=this._ensureCommentMap(e,t).get(n);return d?(d.resolved=o,this._commentUpdate$.next({unitId:e,subUnitId:t,type:"resolve",payload:{commentId:n,resolved:o}}),!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){const o=this._ensureCommentMap(e,t),a=Array.from(o.values()).filter(l=>l.threadId===n);let d;const i=[],h=new Set;for(const l of a)l.parentId?i.push(l):d=l,h.add(l.personId);if(d)return{root:d,children:i,relativeUsers:h,unitId:e,subUnitId:t,threadId:n}}getCommentWithChildren(e,t,n){const o=this.getComment(e,t,n);if(o)return this.getThread(e,t,o.threadId)}_deleteComment(e,t,n){const o=this._ensureCommentMap(e,t),a=o.get(n);a&&(o.delete(n),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"delete",payload:{commentId:n,isRoot:!a.parentId,comment:a}}))}deleteThread(e,t,n){this._ensureThreadMap(e,t).delete(n),this._ensureCommentMap(e,t).forEach(d=>{d.threadId===n&&this._deleteComment(e,t,d.id)})}deleteComment(e,t,n){const a=this._ensureCommentMap(e,t).get(n);return a&&(a.parentId?this._deleteComment(e,t,n):this.deleteThread(e,t,a.threadId)),!0}deleteUnit(e){const t=this._commentsMap.get(e);t&&t.forEach((n,o)=>{n.forEach(a=>{this.deleteComment(e,o,a.id)})})}getUnit(e){const t=this._threadMap.get(e);if(!t)return[];const n=[];return t.forEach((o,a)=>{o.forEach((d,i)=>{const h=this.getThread(e,a,i);h&&n.push(h)})}),n}getAll(){const e=[];return this._commentsMap.forEach((t,n)=>{e.push({unitId:n,threads:this.getUnit(n)})}),e}},s.ThreadCommentModel=W([R(0,c.Inject(C)),R(1,c.Inject(c.LifecycleService))],s.ThreadCommentModel);var v=(m=>(m[m.UNIVER_UNKNOWN=0]="UNIVER_UNKNOWN",m[m.UNIVER_DOC=1]="UNIVER_DOC",m[m.UNIVER_SHEET=2]="UNIVER_SHEET",m[m.UNIVER_SLIDE=3]="UNIVER_SLIDE",m[m.UNIVER_PROJECT=4]="UNIVER_PROJECT",m[m.UNRECOGNIZED=-1]="UNRECOGNIZED",m))(v||{});const S="UNIVER_THREAD_COMMENT_PLUGIN";var G=Object.getOwnPropertyDescriptor,J=(m,r,e,t)=>{for(var n=t>1?void 0:t?G(r,e):r,o=m.length-1,a;o>=0;o--)(a=m[o])&&(n=a(n)||n);return n},g=(m,r)=>(e,t)=>r(e,t,m);const O=`SHEET_${S}`;s.ThreadCommentResourceController=class extends c.Disposable{constructor(r,e,t){super(),this._resourceManagerService=r,this._threadCommentModel=e,this._threadCommentDataSourceService=t,this._initSnapshot()}_initSnapshot(){const r=t=>{const n=this._threadCommentModel.getUnit(t),o={};return n?(n.forEach(a=>{var i;const d=(i=o[a.subUnitId])!=null?i:[];d.push({...a.root,children:a.children}),o[a.subUnitId]=d}),JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(o,t))):""},e=t=>{if(!t)return{};try{return JSON.parse(t)}catch{return{}}};this.disposeWithMe(this._resourceManagerService.registerPluginResource({pluginName:O,businesses:[v.UNIVER_SHEET,v.UNIVER_DOC],toJson:t=>r(t),parseJson:t=>e(t),onUnLoad:t=>{this._threadCommentModel.deleteUnit(t)},onLoad:async(t,n)=>{Object.keys(n).forEach(o=>{const a=n[o];a.forEach(d=>{this._threadCommentModel.addComment(t,o,d)}),this._threadCommentModel.syncThreadComments(t,o,a.map(d=>d.threadId))})}}))}},s.ThreadCommentResourceController=J([g(0,c.IResourceManagerService),g(1,c.Inject(s.ThreadCommentModel)),g(2,C)],s.ThreadCommentResourceController);const y={id:"thread-comment.mutation.add-comment",type:c.CommandType.MUTATION,handler(m,r,e){if(!r)return!1;const t=m.get(s.ThreadCommentModel),{unitId:n,subUnitId:o,comment:a,sync:d}=r,i=d||(e==null?void 0:e.fromChangeset)&&!a.parentId;return t.addComment(n,o,a,i)}},U={id:"thread-comment.mutation.update-comment",type:c.CommandType.MUTATION,handler(m,r){if(!r)return!1;const e=m.get(s.ThreadCommentModel),{unitId:t,subUnitId:n,payload:o,silent:a}=r;return e.updateComment(t,n,o,a)}},D={id:"thread-comment.mutation.update-comment-ref",type:c.CommandType.MUTATION,handler(m,r){if(!r)return!1;const e=m.get(s.ThreadCommentModel),{unitId:t,subUnitId:n,payload:o,silent:a}=r;return e.updateCommentRef(t,n,o,a)}},N={id:"thread-comment.mutation.resolve-comment",type:c.CommandType.MUTATION,handler(m,r){if(!r)return!1;const e=m.get(s.ThreadCommentModel),{unitId:t,subUnitId:n,resolved:o,commentId:a}=r;return e.resolveComment(t,n,a,o)}},_={id:"thread-comment.mutation.delete-comment",type:c.CommandType.MUTATION,handler(m,r){if(!r)return!1;const e=m.get(s.ThreadCommentModel),{unitId:t,subUnitId:n,commentId:o}=r;return e.deleteComment(t,n,o)}},w={id:"thread-comment.command.add-comment",type:c.CommandType.COMMAND,async handler(m,r){if(!r)return!1;const e=m.get(c.ICommandService),t=m.get(C),{comment:n}=r,o=await t.addComment(n),a=t.syncUpdateMutationToColla,d=!n.parentId,i={id:y.id,params:{...r,comment:o}};return d?await e.executeCommand(i.id,i.params):e.executeCommand(i.id,i.params,{onlyLocal:!a})}},P={id:"thread-comment.command.update-comment",type:c.CommandType.COMMAND,async handler(m,r){if(!r)return!1;const{unitId:e,subUnitId:t,payload:n}=r,o=m.get(c.ICommandService),a=m.get(s.ThreadCommentModel),d=m.get(C),i=d.syncUpdateMutationToColla,h=a.getComment(e,t,n.commentId);if(!h)return!1;const{children:l,...p}=h;if(!await d.updateComment({...p,...n}))return!1;const f={id:U.id,params:r};return o.executeCommand(f.id,f.params,{onlyLocal:!i}),!0}},A={id:"thread-comment.command.resolve-comment",type:c.CommandType.COMMAND,async handler(m,r){if(!r)return!1;const{unitId:e,subUnitId:t,resolved:n,commentId:o}=r,a=m.get(C),i=m.get(s.ThreadCommentModel).getComment(e,t,o),h=a.syncUpdateMutationToColla;return!i||!await a.resolveComment({...i,resolved:n})?!1:m.get(c.ICommandService).executeCommand(N.id,r,{onlyLocal:!h})}},j={id:"thread-comment.command.delete-comment",type:c.CommandType.COMMAND,async handler(m,r){if(!r)return!1;const e=m.get(s.ThreadCommentModel),t=m.get(C),n=m.get(c.ICommandService),{unitId:o,subUnitId:a,commentId:d}=r,i=t.syncUpdateMutationToColla,h=e.getComment(o,a,d);if(!h||!await t.deleteComment(o,a,h.threadId,d))return!1;const l={id:_.id,params:r};return n.executeCommand(l.id,l.params,{onlyLocal:!i})}},$={id:"thread-comment.command.delete-comment-tree",type:c.CommandType.COMMAND,async handler(m,r){if(!r)return!1;const e=m.get(s.ThreadCommentModel),t=m.get(c.ICommandService),n=m.get(C),{unitId:o,subUnitId:a,commentId:d}=r,i=e.getCommentWithChildren(o,a,d);return!i||!await n.deleteComment(o,a,i.root.threadId,d)?!1:await t.executeCommand(_.id,{unitId:o,subUnitId:a,commentId:i.root.id})}},b="thread-comment.config",L={};var Y=Object.getOwnPropertyDescriptor,k=(m,r,e,t)=>{for(var n=t>1?void 0:t?Y(r,e):r,o=m.length-1,a;o>=0;o--)(a=m[o])&&(n=a(n)||n);return n},E=(m,r)=>(e,t)=>r(e,t,m);s.UniverThreadCommentPlugin=(T=class extends c.Plugin{constructor(r=L,e,t,n){super(),this._config=r,this._injector=e,this._commandService=t,this._configService=n;const{...o}=c.merge({},L,this._config);this._configService.setConfig(b,o)}onStarting(){var r;c.mergeOverrideWithDependencies([[C,{useClass:I}],[s.ThreadCommentModel],[s.ThreadCommentResourceController]],(r=this._config)==null?void 0:r.overrides).forEach(e=>{this._injector.add(e)}),[w,P,j,A,$,y,U,D,_,N].forEach(e=>{this._commandService.registerCommand(e)}),this._injector.get(s.ThreadCommentResourceController)}},u(T,"pluginName",S),u(T,"type",c.UniverInstanceType.UNIVER_UNKNOWN),T),s.UniverThreadCommentPlugin=k([E(1,c.Inject(c.Injector)),E(2,c.ICommandService),E(3,c.IConfigService)],s.UniverThreadCommentPlugin);function K(m){return c.dayjs(m).format("YYYY/MM/DD HH:mm")}s.AddCommentCommand=w,s.AddCommentMutation=y,s.DeleteCommentCommand=j,s.DeleteCommentMutation=_,s.DeleteCommentTreeCommand=$,s.IThreadCommentDataSourceService=C,s.ResolveCommentCommand=A,s.ResolveCommentMutation=N,s.SHEET_UNIVER_THREAD_COMMENT_PLUGIN=O,s.TC_PLUGIN_NAME=S,s.ThreadCommentDataSourceService=I,s.UpdateCommentCommand=P,s.UpdateCommentMutation=U,s.UpdateCommentRefMutation=D,s.getDT=K,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/thread-comment",
3
- "version": "0.6.6",
3
+ "version": "0.6.7",
4
4
  "private": false,
5
5
  "description": "Univer thread comment core plugin",
6
6
  "author": "DreamNum <developer@univer.ai>",
@@ -46,15 +46,15 @@
46
46
  "rxjs": ">=7.0.0"
47
47
  },
48
48
  "dependencies": {
49
- "@univerjs/protocol": "0.1.44",
50
- "@univerjs/core": "0.6.6"
49
+ "@univerjs/protocol": "0.1.45",
50
+ "@univerjs/core": "0.6.7"
51
51
  },
52
52
  "devDependencies": {
53
53
  "rxjs": "^7.8.1",
54
54
  "typescript": "^5.8.2",
55
- "vite": "^6.2.1",
56
- "vitest": "^3.0.8",
57
- "@univerjs-infra/shared": "0.6.6"
55
+ "vite": "^6.2.3",
56
+ "vitest": "^3.0.9",
57
+ "@univerjs-infra/shared": "0.6.7"
58
58
  },
59
59
  "scripts": {
60
60
  "test": "vitest run",