@univerjs/thread-comment 0.2.4-alpha.0 → 0.2.4
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 b=Object.defineProperty;var j=(a,r,t)=>r in a?b(a,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[r]=t;var C=(a,r,t)=>j(a,typeof r!="symbol"?r+"":r,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=require("rxjs"),c=require("@univerjs/core");class E extends c.Disposable{constructor(){super();C(this,"_dataSource",null);C(this,"syncUpdateMutationToColla",!0)}set dataSource(t){this._dataSource=t}get dataSource(){return this._dataSource}async getThreadComment(t,n,e){return this._dataSource?(await this._dataSource.listComments(t,n,[e]))[0]:null}async addComment(t){return this._dataSource?this._dataSource.addComment(t):t}async updateComment(t){return this._dataSource?this._dataSource.updateComment(t):!0}async resolveComment(t){return this._dataSource?this._dataSource.resolveComment(t):!0}async deleteComment(t,n,e,o){return this._dataSource?this._dataSource.deleteComment(t,n,e,o):!0}async listThreadComments(t,n,e){return this.dataSource?this.dataSource.listComments(t,n,e):[]}saveToSnapshot(t,n){if(this._dataSource){const e={};return Object.keys(t).forEach(o=>{const m=t[o];e[o]=m.map(this.dataSource.saveCommentToSnapshot)}),e}return t}}const p=c.createIdentifier("univer.thread-comment.data-source-service");var L=Object.defineProperty,V=Object.getOwnPropertyDescriptor,W=(a,r,t,n)=>{for(var e=n>1?void 0:n?V(r,t):r,o=a.length-1,m;o>=0;o--)(m=a[o])&&(e=(n?m(r,t,e):m(e))||e);return n&&e&&L(r,t,e),e},O=(a,r)=>(t,n)=>r(t,n,a);exports.ThreadCommentModel=class extends c.Disposable{constructor(t,n){super();C(this,"_commentsMap",{});C(this,"_commentsTreeMap",new Map);C(this,"_threadMap",new Map);C(this,"_commentUpdate$",new v.Subject);C(this,"_commentsMap$",new v.BehaviorSubject({}));C(this,"commentUpdate$",this._commentUpdate$.asObservable());C(this,"commentMap$",this._commentsMap$.asObservable());this._dataSourceService=t,this._commandService=n,this.disposeWithMe(()=>{this._commentUpdate$.complete(),this._commentsMap$.complete()})}_ensureCommentMap(t,n){let e=this._commentsMap[t];e||(e={},this._commentsMap[t]=e);let o=e[n];return o||(o={},e[n]=o),o}_ensureCommentChildrenMap(t,n){let e=this._commentsTreeMap.get(t);e||(e=new Map,this._commentsTreeMap.set(t,e));let o=e.get(n);return o||(o=new Map,e.set(n,o)),o}_ensureThreadMap(t){let n=this._threadMap.get(t);return n||(n=new Map,this._threadMap.set(t,n)),n}_refreshCommentsMap$(){this._commentsMap$.next({...this._commentsMap})}ensureMap(t,n){const e=this._ensureCommentMap(t,n),o=this._ensureCommentChildrenMap(t,n);return{commentMap:e,commentChildrenMap:o}}_replaceComment(t,n,e){var d;const{commentMap:o,commentChildrenMap:m}=this.ensureMap(t,n),s=o[e.id];if(s){const i={...e,ref:s.ref};o[e.id]=i,(d=e.children)==null||d.forEach(h=>{o[h.id]={...h,ref:""}}),m.set(e.id,i),this._commentUpdate$.next({unitId:t,subUnitId:n,type:"syncUpdate",payload:i}),!!e.resolved!=!!s.resolved&&this._commentUpdate$.next({unitId:t,subUnitId:n,type:"resolve",payload:{commentId:e.id,resolved:!!e.resolved}})}}async syncThreadComments(t,n,e){const o=await this._dataSourceService.listThreadComments(t,n,e);if(!o.length)return;const m=new Set(e);o.forEach(s=>{this._replaceComment(t,n,s),m.delete(s.threadId)}),m.forEach(s=>{const d=this.getThread(t,s);d&&this.deleteComment(d.unitId,d.subUnitId,d.id)}),this._refreshCommentsMap$()}addComment(t,n,e,o){var l,M;const{commentMap:m,commentChildrenMap:s}=this.ensureMap(t,n),d=e,i=u=>{m[u.id]=u,this._commentUpdate$.next({unitId:t,subUnitId:n,type:"add",payload:u,isRoot:!u.parentId})},h=d.parentId;if(h){const u=m[h];u.children=[...(l=u.children)!=null?l:[],d],i(d)}else s.set(d.id,d),this._ensureThreadMap(t).set(d.threadId,d),i(d),(M=d.children)==null||M.forEach(f=>i({...f,ref:""}));return this._refreshCommentsMap$(),o&&this.syncThreadComments(t,n,[d.threadId]),!0}updateComment(t,n,e,o){const{commentMap:m}=this.ensureMap(t,n),s=m[e.commentId];return s?(s.updated=!0,s.text=e.text,s.attachments=e.attachments,s.updateT=e.updateT,this._commentUpdate$.next({unitId:t,subUnitId:n,type:"update",payload:e,silent:o}),this._refreshCommentsMap$(),!0):!1}updateCommentRef(t,n,e,o){const{commentMap:m}=this.ensureMap(t,n),s=m[e.commentId];return s?(s.ref=e.ref,this._commentUpdate$.next({unitId:t,subUnitId:n,type:"updateRef",payload:e,silent:o}),this._refreshCommentsMap$(),!0):!1}resolveComment(t,n,e,o){const{commentMap:m}=this.ensureMap(t,n),s=m[e];return s?(s.resolved=o,this._commentUpdate$.next({unitId:t,subUnitId:n,type:"resolve",payload:{commentId:e,resolved:o}}),this._refreshCommentsMap$(),!0):!1}getComment(t,n,e){const{commentMap:o}=this.ensureMap(t,n);return o[e]}getComment$(t,n,e){return this._commentsMap$.pipe(v.map(o=>o[t][n][e]))}getCommentWithChildren(t,n,e){var h,l;const{commentMap:o,commentChildrenMap:m}=this.ensureMap(t,n),s=o[e];if(!s)return;const d=new Set,i=m.get(e);if(i)return[i,...(h=i.children)!=null?h:[]].forEach(M=>{var u;d.add(M.personId),(u=M.text.customRanges)==null||u.forEach(f=>{f.rangeType===c.CustomRangeType.MENTION&&d.add(f.rangeId)})}),{root:s,children:(l=i.children)!=null?l:[],relativeUsers:d}}deleteComment(t,n,e){var d;const{commentMap:o,commentChildrenMap:m}=this.ensureMap(t,n),s=o[e];if(!s)return!0;if(s.parentId){const i=m.get(s.parentId);if(i&&i.children){const h=i.children.findIndex(l=>l.id=e);i.children.splice(h,1)}delete o[e]}else{delete o[e];const i=m.get(e);m.delete(e),this._ensureThreadMap(t).delete(s.threadId),(d=i==null?void 0:i.children)==null||d.forEach(l=>{delete o[l.id],this._commentUpdate$.next({unitId:t,subUnitId:n,type:"delete",payload:{commentId:l.id,isRoot:!1,comment:l}})})}return this._commentUpdate$.next({unitId:t,subUnitId:n,type:"delete",payload:{commentId:e,isRoot:!s.parentId,comment:s}}),this._refreshCommentsMap$(),!0}getUnit(t){const n=this._commentsMap[t];return n?Array.from(Object.entries(n)).map(([e,o])=>[e,Array.from(Object.values(o))]):[]}deleteUnit(t){const n=this._commentsMap[t];n&&Object.entries(n).forEach(([e,o])=>{Object.values(o).forEach(m=>{this.deleteComment(t,e,m.id)})})}getRootCommentIds(t,n){const e=this._ensureCommentChildrenMap(t,n);return Array.from(e.keys())}getAll(){return this._commentsMap}getThread(t,n){return this._ensureThreadMap(t).get(n)}};exports.ThreadCommentModel=W([O(0,c.Inject(p)),O(1,c.ICommandService)],exports.ThreadCommentModel);var y=(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.UNRECOGNIZED=-1]="UNRECOGNIZED",a))(y||{});const g="THREAD_COMMENT_PLUGIN";var J=Object.defineProperty,G=Object.getOwnPropertyDescriptor,H=(a,r,t,n)=>{for(var e=n>1?void 0:n?G(r,t):r,o=a.length-1,m;o>=0;o--)(m=a[o])&&(e=(n?m(r,t,e):m(e))||e);return n&&e&&J(r,t,e),e},S=(a,r)=>(t,n)=>r(t,n,a);exports.ThreadCommentResourceController=class extends c.Disposable{constructor(r,t,n){super(),this._resourceManagerService=r,this._threadCommentModel=t,this._threadCommentDataSourceService=n,this._initSnapshot()}_initSnapshot(){const r=n=>{const e=this._threadCommentModel.getUnit(n),o={};return e?(e.forEach(([m,s])=>{o[m]=s}),JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(o,n))):""},t=n=>{if(!n)return{};try{return JSON.parse(n)}catch{return{}}};this.disposeWithMe(this._resourceManagerService.registerPluginResource({pluginName:`SHEET_${g}`,businesses:[y.UNIVER_SHEET,y.UNIVER_DOC],toJson:n=>r(n),parseJson:n=>t(n),onUnLoad:n=>{this._threadCommentModel.deleteUnit(n)},onLoad:async(n,e)=>{Object.keys(e).forEach(o=>{const m=e[o];m.forEach(s=>{this._threadCommentModel.addComment(n,o,s)}),this._threadCommentModel.syncThreadComments(n,o,m.map(s=>s.threadId))})}}))}};exports.ThreadCommentResourceController=H([c.OnLifecycle(c.LifecycleStages.Starting,exports.ThreadCommentResourceController),S(0,c.IResourceManagerService),S(1,c.Inject(exports.ThreadCommentModel)),S(2,p)],exports.ThreadCommentResourceController);const U={id:"thread-comment.mutation.add-comment",type:c.CommandType.MUTATION,handler(a,r,t){if(!r)return!1;const n=a.get(exports.ThreadCommentModel),{unitId:e,subUnitId:o,comment:m,sync:s}=r,d=s||(t==null?void 0:t.fromChangeset)&&!m.parentId;return n.addComment(e,o,m,d)}},I={id:"thread-comment.mutation.update-comment",type:c.CommandType.MUTATION,handler(a,r){if(!r)return!1;const t=a.get(exports.ThreadCommentModel),{unitId:n,subUnitId:e,payload:o,silent:m}=r;return t.updateComment(n,e,o,m)}},$={id:"thread-comment.mutation.update-comment-ref",type:c.CommandType.MUTATION,handler(a,r){if(!r)return!1;const t=a.get(exports.ThreadCommentModel),{unitId:n,subUnitId:e,payload:o,silent:m}=r;return t.updateCommentRef(n,e,o,m)}},N={id:"thread-comment.mutation.resolve-comment",type:c.CommandType.MUTATION,handler(a,r){if(!r)return!1;const t=a.get(exports.ThreadCommentModel),{unitId:n,subUnitId:e,resolved:o,commentId:m}=r;return t.resolveComment(n,e,m,o)}},T={id:"thread-comment.mutation.delete-comment",type:c.CommandType.MUTATION,handler(a,r){if(!r)return!1;const t=a.get(exports.ThreadCommentModel),{unitId:n,subUnitId:e,commentId:o}=r;return t.deleteComment(n,e,o)}},D={id:"thread-comment.command.add-comment",type:c.CommandType.COMMAND,async handler(a,r){if(!r)return!1;const t=a.get(c.ICommandService),n=a.get(p),{comment:e}=r,o=await n.addComment(e),m=n.syncUpdateMutationToColla,s=!e.parentId,d={id:U.id,params:{...r,comment:o}};return s?await t.executeCommand(d.id,d.params):t.executeCommand(d.id,d.params,{onlyLocal:!m})}},w={id:"thread-comment.command.update-comment",type:c.CommandType.COMMAND,async handler(a,r){if(!r)return!1;const{unitId:t,subUnitId:n,payload:e}=r,o=a.get(c.ICommandService),m=a.get(exports.ThreadCommentModel),s=a.get(p),d=s.syncUpdateMutationToColla,i=m.getComment(t,n,e.commentId);if(!i)return!1;const{children:h,...l}=i;if(!await s.updateComment({...l,...e}))return!1;const u={id:I.id,params:r};return o.executeCommand(u.id,u.params,{onlyLocal:!d}),!0}},x={id:"thread-comment.command.resolve-comment",type:c.CommandType.COMMAND,async handler(a,r){if(!r)return!1;const{unitId:t,subUnitId:n,resolved:e,commentId:o}=r,m=a.get(p),d=a.get(exports.ThreadCommentModel).getComment(t,n,o),i=m.syncUpdateMutationToColla;return!d||!await m.resolveComment({...d,resolved:e})?!1:a.get(c.ICommandService).executeCommand(N.id,r,{onlyLocal:!i})}},P={id:"thread-comment.command.delete-comment",type:c.CommandType.COMMAND,async handler(a,r){if(!r)return!1;const t=a.get(exports.ThreadCommentModel),n=a.get(p),e=a.get(c.ICommandService),{unitId:o,subUnitId:m,commentId:s}=r,d=n.syncUpdateMutationToColla,i=t.getComment(o,m,s);if(!i||!await n.deleteComment(o,m,i.threadId,s))return!1;const h={id:T.id,params:r};return e.executeCommand(h.id,h.params,{onlyLocal:!d})}},A={id:"thread-comment.command.delete-comment-tree",type:c.CommandType.COMMAND,async handler(a,r){if(!r)return!1;const t=a.get(exports.ThreadCommentModel),n=a.get(c.ICommandService),e=a.get(p),{unitId:o,subUnitId:m,commentId:s}=r,d=t.getCommentWithChildren(o,m,s);return!d||!await e.deleteComment(o,m,d.root.threadId,s)?!1:await n.executeCommand(T.id,{unitId:o,subUnitId:m,commentId:d.root.id})}};var B=Object.defineProperty,K=Object.getOwnPropertyDescriptor,q=(a,r,t,n)=>{for(var e=n>1?void 0:n?K(r,t):r,o=a.length-1,m;o>=0;o--)(m=a[o])&&(e=(n?m(r,t,e):m(e))||e);return n&&e&&B(r,t,e),e},R=(a,r)=>(t,n)=>r(t,n,a),_;exports.UniverThreadCommentPlugin=(_=class extends c.Plugin{constructor(t,n,e){super();C(this,"_config");this._injector=n,this._commandService=e,this._config=t}onStarting(t){var n;c.mergeOverrideWithDependencies([[p,{useClass:E}],[exports.ThreadCommentModel],[exports.ThreadCommentResourceController]],(n=this._config)==null?void 0:n.overrides).forEach(e=>{t.add(e)}),[D,w,P,x,A,U,I,$,T,N].forEach(e=>{this._commandService.registerCommand(e)})}},C(_,"pluginName",g),C(_,"type",c.UniverInstanceType.UNIVER_UNKNOWN),_);exports.UniverThreadCommentPlugin=q([R(1,c.Inject(c.Injector)),R(2,c.ICommandService)],exports.UniverThreadCommentPlugin);exports.AddCommentCommand=D;exports.AddCommentMutation=U;exports.DeleteCommentCommand=P;exports.DeleteCommentMutation=T;exports.DeleteCommentTreeCommand=A;exports.IThreadCommentDataSourceService=p;exports.ResolveCommentCommand=x;exports.ResolveCommentMutation=N;exports.TC_PLUGIN_NAME=g;exports.ThreadCommentDataSourceService=E;exports.UpdateCommentCommand=w;exports.UpdateCommentMutation=I;exports.UpdateCommentRefMutation=$;
|
|
1
|
+
"use strict";var j=Object.defineProperty;var L=(a,r,t)=>r in a?j(a,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[r]=t;var C=(a,r,t)=>L(a,typeof r!="symbol"?r+"":r,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("rxjs"),i=require("@univerjs/core"),f=require("@wendellhu/redi");class $ extends i.Disposable{constructor(){super();C(this,"_dataSource",null);C(this,"syncUpdateMutationToColla",!0)}set dataSource(t){this._dataSource=t}get dataSource(){return this._dataSource}async getThreadComment(t,n,e){return this._dataSource?(await this._dataSource.listComments(t,n,[e]))[0]:null}async addComment(t){return this._dataSource?this._dataSource.addComment(t):t}async updateComment(t){return this._dataSource?this._dataSource.updateComment(t):!0}async resolveComment(t){return this._dataSource?this._dataSource.resolveComment(t):!0}async deleteComment(t,n,e,o){return this._dataSource?this._dataSource.deleteComment(t,n,e,o):!0}async listThreadComments(t,n,e){return this.dataSource?this.dataSource.listComments(t,n,e):[]}saveToSnapshot(t,n){if(this._dataSource){const e={};return Object.keys(t).forEach(o=>{const m=t[o];e[o]=m.map(this.dataSource.saveCommentToSnapshot)}),e}return t}}const p=f.createIdentifier("univer.thread-comment.data-source-service");var V=Object.defineProperty,W=Object.getOwnPropertyDescriptor,J=(a,r,t,n)=>{for(var e=n>1?void 0:n?W(r,t):r,o=a.length-1,m;o>=0;o--)(m=a[o])&&(e=(n?m(r,t,e):m(e))||e);return n&&e&&V(r,t,e),e},R=(a,r)=>(t,n)=>r(t,n,a);exports.ThreadCommentModel=class extends i.Disposable{constructor(t,n){super();C(this,"_commentsMap",{});C(this,"_commentsTreeMap",new Map);C(this,"_threadMap",new Map);C(this,"_commentUpdate$",new S.Subject);C(this,"_commentsMap$",new S.BehaviorSubject({}));C(this,"commentUpdate$",this._commentUpdate$.asObservable());C(this,"commentMap$",this._commentsMap$.asObservable());this._dataSourceService=t,this._commandService=n,this.disposeWithMe(()=>{this._commentUpdate$.complete(),this._commentsMap$.complete()})}_ensureCommentMap(t,n){let e=this._commentsMap[t];e||(e={},this._commentsMap[t]=e);let o=e[n];return o||(o={},e[n]=o),o}_ensureCommentChildrenMap(t,n){let e=this._commentsTreeMap.get(t);e||(e=new Map,this._commentsTreeMap.set(t,e));let o=e.get(n);return o||(o=new Map,e.set(n,o)),o}_ensureThreadMap(t){let n=this._threadMap.get(t);return n||(n=new Map,this._threadMap.set(t,n)),n}_refreshCommentsMap$(){this._commentsMap$.next({...this._commentsMap})}ensureMap(t,n){const e=this._ensureCommentMap(t,n),o=this._ensureCommentChildrenMap(t,n);return{commentMap:e,commentChildrenMap:o}}_replaceComment(t,n,e){var d;const{commentMap:o,commentChildrenMap:m}=this.ensureMap(t,n),s=o[e.id];if(s){const c={...e,ref:s.ref};o[e.id]=c,(d=e.children)==null||d.forEach(h=>{o[h.id]={...h,ref:""}}),m.set(e.id,c),this._commentUpdate$.next({unitId:t,subUnitId:n,type:"syncUpdate",payload:c}),!!e.resolved!=!!s.resolved&&this._commentUpdate$.next({unitId:t,subUnitId:n,type:"resolve",payload:{commentId:e.id,resolved:!!e.resolved}})}}async syncThreadComments(t,n,e){const o=await this._dataSourceService.listThreadComments(t,n,e);if(!o.length)return;const m=new Set(e);o.forEach(s=>{this._replaceComment(t,n,s),m.delete(s.threadId)}),m.forEach(s=>{const d=this.getThread(t,s);d&&this.deleteComment(d.unitId,d.subUnitId,d.id)}),this._refreshCommentsMap$()}addComment(t,n,e,o){var l,M;const{commentMap:m,commentChildrenMap:s}=this.ensureMap(t,n),d=e,c=u=>{m[u.id]=u,this._commentUpdate$.next({unitId:t,subUnitId:n,type:"add",payload:u,isRoot:!u.parentId})},h=d.parentId;if(h){const u=m[h];u.children=[...(l=u.children)!=null?l:[],d],c(d)}else s.set(d.id,d),this._ensureThreadMap(t).set(d.threadId,d),c(d),(M=d.children)==null||M.forEach(_=>c({..._,ref:""}));return this._refreshCommentsMap$(),o&&this.syncThreadComments(t,n,[d.threadId]),!0}updateComment(t,n,e,o){const{commentMap:m}=this.ensureMap(t,n),s=m[e.commentId];return s?(s.updated=!0,s.text=e.text,s.attachments=e.attachments,s.updateT=e.updateT,this._commentUpdate$.next({unitId:t,subUnitId:n,type:"update",payload:e,silent:o}),this._refreshCommentsMap$(),!0):!1}updateCommentRef(t,n,e,o){const{commentMap:m}=this.ensureMap(t,n),s=m[e.commentId];return s?(s.ref=e.ref,this._commentUpdate$.next({unitId:t,subUnitId:n,type:"updateRef",payload:e,silent:o}),this._refreshCommentsMap$(),!0):!1}resolveComment(t,n,e,o){const{commentMap:m}=this.ensureMap(t,n),s=m[e];return s?(s.resolved=o,this._commentUpdate$.next({unitId:t,subUnitId:n,type:"resolve",payload:{commentId:e,resolved:o}}),this._refreshCommentsMap$(),!0):!1}getComment(t,n,e){const{commentMap:o}=this.ensureMap(t,n);return o[e]}getComment$(t,n,e){return this._commentsMap$.pipe(S.map(o=>o[t][n][e]))}getCommentWithChildren(t,n,e){var h,l;const{commentMap:o,commentChildrenMap:m}=this.ensureMap(t,n),s=o[e];if(!s)return;const d=new Set,c=m.get(e);if(c)return[c,...(h=c.children)!=null?h:[]].forEach(M=>{var u;d.add(M.personId),(u=M.text.customRanges)==null||u.forEach(_=>{_.rangeType===i.CustomRangeType.MENTION&&d.add(_.rangeId)})}),{root:s,children:(l=c.children)!=null?l:[],relativeUsers:d}}deleteComment(t,n,e){var d;const{commentMap:o,commentChildrenMap:m}=this.ensureMap(t,n),s=o[e];if(!s)return!0;if(s.parentId){const c=m.get(s.parentId);if(c&&c.children){const h=c.children.findIndex(l=>l.id=e);c.children.splice(h,1)}delete o[e]}else{delete o[e];const c=m.get(e);m.delete(e),this._ensureThreadMap(t).delete(s.threadId),(d=c==null?void 0:c.children)==null||d.forEach(l=>{delete o[l.id],this._commentUpdate$.next({unitId:t,subUnitId:n,type:"delete",payload:{commentId:l.id,isRoot:!1,comment:l}})})}return this._commentUpdate$.next({unitId:t,subUnitId:n,type:"delete",payload:{commentId:e,isRoot:!s.parentId,comment:s}}),this._refreshCommentsMap$(),!0}getUnit(t){const n=this._commentsMap[t];return n?Array.from(Object.entries(n)).map(([e,o])=>[e,Array.from(Object.values(o))]):[]}deleteUnit(t){const n=this._commentsMap[t];n&&Object.entries(n).forEach(([e,o])=>{Object.values(o).forEach(m=>{this.deleteComment(t,e,m.id)})})}getRootCommentIds(t,n){const e=this._ensureCommentChildrenMap(t,n);return Array.from(e.keys())}getAll(){return this._commentsMap}getThread(t,n){return this._ensureThreadMap(t).get(n)}};exports.ThreadCommentModel=J([R(0,f.Inject(p)),R(1,i.ICommandService)],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.UNRECOGNIZED=-1]="UNRECOGNIZED",a))(g||{});const U="THREAD_COMMENT_PLUGIN";var G=Object.defineProperty,H=Object.getOwnPropertyDescriptor,B=(a,r,t,n)=>{for(var e=n>1?void 0:n?H(r,t):r,o=a.length-1,m;o>=0;o--)(m=a[o])&&(e=(n?m(r,t,e):m(e))||e);return n&&e&&G(r,t,e),e},y=(a,r)=>(t,n)=>r(t,n,a);exports.ThreadCommentResourceController=class extends i.Disposable{constructor(r,t,n){super(),this._resourceManagerService=r,this._threadCommentModel=t,this._threadCommentDataSourceService=n,this._initSnapshot()}_initSnapshot(){const r=n=>{const e=this._threadCommentModel.getUnit(n),o={};return e?(e.forEach(([m,s])=>{o[m]=s}),JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(o,n))):""},t=n=>{if(!n)return{};try{return JSON.parse(n)}catch{return{}}};this.disposeWithMe(this._resourceManagerService.registerPluginResource({pluginName:`SHEET_${U}`,businesses:[g.UNIVER_SHEET,g.UNIVER_DOC],toJson:n=>r(n),parseJson:n=>t(n),onUnLoad:n=>{this._threadCommentModel.deleteUnit(n)},onLoad:async(n,e)=>{Object.keys(e).forEach(o=>{const m=e[o];m.forEach(s=>{this._threadCommentModel.addComment(n,o,s)}),this._threadCommentModel.syncThreadComments(n,o,m.map(s=>s.threadId))})}}))}};exports.ThreadCommentResourceController=B([i.OnLifecycle(i.LifecycleStages.Starting,exports.ThreadCommentResourceController),y(0,i.IResourceManagerService),y(1,f.Inject(exports.ThreadCommentModel)),y(2,p)],exports.ThreadCommentResourceController);const I={id:"thread-comment.mutation.add-comment",type:i.CommandType.MUTATION,handler(a,r,t){if(!r)return!1;const n=a.get(exports.ThreadCommentModel),{unitId:e,subUnitId:o,comment:m,sync:s}=r,d=s||(t==null?void 0:t.fromChangeset)&&!m.parentId;return n.addComment(e,o,m,d)}},N={id:"thread-comment.mutation.update-comment",type:i.CommandType.MUTATION,handler(a,r){if(!r)return!1;const t=a.get(exports.ThreadCommentModel),{unitId:n,subUnitId:e,payload:o,silent:m}=r;return t.updateComment(n,e,o,m)}},D={id:"thread-comment.mutation.update-comment-ref",type:i.CommandType.MUTATION,handler(a,r){if(!r)return!1;const t=a.get(exports.ThreadCommentModel),{unitId:n,subUnitId:e,payload:o,silent:m}=r;return t.updateCommentRef(n,e,o,m)}},O={id:"thread-comment.mutation.resolve-comment",type:i.CommandType.MUTATION,handler(a,r){if(!r)return!1;const t=a.get(exports.ThreadCommentModel),{unitId:n,subUnitId:e,resolved:o,commentId:m}=r;return t.resolveComment(n,e,m,o)}},v={id:"thread-comment.mutation.delete-comment",type:i.CommandType.MUTATION,handler(a,r){if(!r)return!1;const t=a.get(exports.ThreadCommentModel),{unitId:n,subUnitId:e,commentId:o}=r;return t.deleteComment(n,e,o)}},w={id:"thread-comment.command.add-comment",type:i.CommandType.COMMAND,async handler(a,r){if(!r)return!1;const t=a.get(i.ICommandService),n=a.get(p),{comment:e}=r,o=await n.addComment(e),m=n.syncUpdateMutationToColla,s=!e.parentId,d={id:I.id,params:{...r,comment:o}};return s?await t.executeCommand(d.id,d.params):t.executeCommand(d.id,d.params,{onlyLocal:!m})}},x={id:"thread-comment.command.update-comment",type:i.CommandType.COMMAND,async handler(a,r){if(!r)return!1;const{unitId:t,subUnitId:n,payload:e}=r,o=a.get(i.ICommandService),m=a.get(exports.ThreadCommentModel),s=a.get(p),d=s.syncUpdateMutationToColla,c=m.getComment(t,n,e.commentId);if(!c)return!1;const{children:h,...l}=c;if(!await s.updateComment({...l,...e}))return!1;const u={id:N.id,params:r};return o.executeCommand(u.id,u.params,{onlyLocal:!d}),!0}},P={id:"thread-comment.command.resolve-comment",type:i.CommandType.COMMAND,async handler(a,r){if(!r)return!1;const{unitId:t,subUnitId:n,resolved:e,commentId:o}=r,m=a.get(p),d=a.get(exports.ThreadCommentModel).getComment(t,n,o),c=m.syncUpdateMutationToColla;return!d||!await m.resolveComment({...d,resolved:e})?!1:a.get(i.ICommandService).executeCommand(O.id,r,{onlyLocal:!c})}},A={id:"thread-comment.command.delete-comment",type:i.CommandType.COMMAND,async handler(a,r){if(!r)return!1;const t=a.get(exports.ThreadCommentModel),n=a.get(p),e=a.get(i.ICommandService),{unitId:o,subUnitId:m,commentId:s}=r,d=n.syncUpdateMutationToColla,c=t.getComment(o,m,s);if(!c||!await n.deleteComment(o,m,c.threadId,s))return!1;const h={id:v.id,params:r};return e.executeCommand(h.id,h.params,{onlyLocal:!d})}},b={id:"thread-comment.command.delete-comment-tree",type:i.CommandType.COMMAND,async handler(a,r){if(!r)return!1;const t=a.get(exports.ThreadCommentModel),n=a.get(i.ICommandService),e=a.get(p),{unitId:o,subUnitId:m,commentId:s}=r,d=t.getCommentWithChildren(o,m,s);return!d||!await e.deleteComment(o,m,d.root.threadId,s)?!1:await n.executeCommand(v.id,{unitId:o,subUnitId:m,commentId:d.root.id})}};var q=Object.defineProperty,K=Object.getOwnPropertyDescriptor,Z=(a,r,t,n)=>{for(var e=n>1?void 0:n?K(r,t):r,o=a.length-1,m;o>=0;o--)(m=a[o])&&(e=(n?m(r,t,e):m(e))||e);return n&&e&&q(r,t,e),e},E=(a,r)=>(t,n)=>r(t,n,a),T;exports.UniverThreadCommentPlugin=(T=class extends i.Plugin{constructor(t,n,e){super();C(this,"_config");this._injector=n,this._commandService=e,this._config=t}onStarting(t){var n;i.mergeOverrideWithDependencies([[p,{useClass:$}],[exports.ThreadCommentModel],[exports.ThreadCommentResourceController]],(n=this._config)==null?void 0:n.overrides).forEach(e=>{t.add(e)}),[w,x,A,P,b,I,N,D,v,O].forEach(e=>{this._commandService.registerCommand(e)})}},C(T,"pluginName",U),C(T,"type",i.UniverInstanceType.UNIVER_UNKNOWN),T);exports.UniverThreadCommentPlugin=Z([E(1,f.Inject(f.Injector)),E(2,i.ICommandService)],exports.UniverThreadCommentPlugin);exports.AddCommentCommand=w;exports.AddCommentMutation=I;exports.DeleteCommentCommand=A;exports.DeleteCommentMutation=v;exports.DeleteCommentTreeCommand=b;exports.IThreadCommentDataSourceService=p;exports.ResolveCommentCommand=P;exports.ResolveCommentMutation=O;exports.TC_PLUGIN_NAME=U;exports.ThreadCommentDataSourceService=$;exports.UpdateCommentCommand=x;exports.UpdateCommentMutation=N;exports.UpdateCommentRefMutation=D;
|
package/lib/es/index.js
CHANGED
|
@@ -2,7 +2,8 @@ var x = Object.defineProperty;
|
|
|
2
2
|
var P = (a, t, e) => t in a ? x(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e;
|
|
3
3
|
var h = (a, t, e) => P(a, typeof t != "symbol" ? t + "" : t, e);
|
|
4
4
|
import { Subject as A, BehaviorSubject as b, map as j } from "rxjs";
|
|
5
|
-
import { Disposable as T,
|
|
5
|
+
import { Disposable as T, CustomRangeType as L, ICommandService as _, OnLifecycle as V, LifecycleStages as W, IResourceManagerService as J, CommandType as p, Plugin as H, UniverInstanceType as B, mergeOverrideWithDependencies as G } from "@univerjs/core";
|
|
6
|
+
import { createIdentifier as K, Inject as U, Injector as Z } from "@wendellhu/redi";
|
|
6
7
|
class q extends T {
|
|
7
8
|
constructor() {
|
|
8
9
|
super();
|
|
@@ -44,7 +45,7 @@ class q extends T {
|
|
|
44
45
|
return e;
|
|
45
46
|
}
|
|
46
47
|
}
|
|
47
|
-
const f =
|
|
48
|
+
const f = K("univer.thread-comment.data-source-service");
|
|
48
49
|
var z = Object.defineProperty, F = Object.getOwnPropertyDescriptor, Q = (a, t, e, n) => {
|
|
49
50
|
for (var r = n > 1 ? void 0 : n ? F(t, e) : t, m = a.length - 1, o; m >= 0; m--)
|
|
50
51
|
(o = a[m]) && (r = (n ? o(t, e, r) : o(r)) || r);
|
|
@@ -206,7 +207,7 @@ let u = class extends T {
|
|
|
206
207
|
return [c, ...(d = c.children) != null ? d : []].forEach((C) => {
|
|
207
208
|
var l;
|
|
208
209
|
s.add(C.personId), (l = C.text.customRanges) == null || l.forEach((M) => {
|
|
209
|
-
M.rangeType ===
|
|
210
|
+
M.rangeType === L.MENTION && s.add(M.rangeId);
|
|
210
211
|
});
|
|
211
212
|
}), {
|
|
212
213
|
root: o,
|
|
@@ -328,8 +329,8 @@ let S = class extends T {
|
|
|
328
329
|
}
|
|
329
330
|
};
|
|
330
331
|
S = k([
|
|
331
|
-
W
|
|
332
|
-
y(0,
|
|
332
|
+
V(W.Starting, S),
|
|
333
|
+
y(0, J),
|
|
333
334
|
y(1, U(u)),
|
|
334
335
|
y(2, f)
|
|
335
336
|
], S);
|
|
@@ -470,7 +471,7 @@ var me = Object.defineProperty, se = Object.getOwnPropertyDescriptor, ce = (a, t
|
|
|
470
471
|
(o = a[m]) && (r = (n ? o(t, e, r) : o(r)) || r);
|
|
471
472
|
return n && r && me(t, e, r), r;
|
|
472
473
|
}, I = (a, t) => (e, n) => t(e, n, a), v;
|
|
473
|
-
let $ = (v = class extends
|
|
474
|
+
let $ = (v = class extends H {
|
|
474
475
|
constructor(t, e, n) {
|
|
475
476
|
super();
|
|
476
477
|
h(this, "_config");
|
|
@@ -478,7 +479,7 @@ let $ = (v = class extends B {
|
|
|
478
479
|
}
|
|
479
480
|
onStarting(t) {
|
|
480
481
|
var e;
|
|
481
|
-
|
|
482
|
+
G([
|
|
482
483
|
[f, { useClass: q }],
|
|
483
484
|
[u],
|
|
484
485
|
[S]
|
|
@@ -501,7 +502,7 @@ let $ = (v = class extends B {
|
|
|
501
502
|
this._commandService.registerCommand(n);
|
|
502
503
|
});
|
|
503
504
|
}
|
|
504
|
-
}, h(v, "pluginName", E), h(v, "type",
|
|
505
|
+
}, h(v, "pluginName", E), h(v, "type", B.UNIVER_UNKNOWN), v);
|
|
505
506
|
$ = ce([
|
|
506
507
|
I(1, U(Z)),
|
|
507
508
|
I(2, _)
|
package/lib/types/plugin.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { DependencyOverride, ICommandService,
|
|
1
|
+
import { DependencyOverride, ICommandService, Plugin, UniverInstanceType } from '@univerjs/core';
|
|
2
|
+
import { Injector } from '@wendellhu/redi';
|
|
2
3
|
|
|
3
4
|
export interface IUniverThreadCommentConfig {
|
|
4
5
|
overrides?: DependencyOverride;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Nullable, Disposable } from '@univerjs/core';
|
|
2
2
|
import { IBaseComment, IThreadComment } from '../types/interfaces/i-thread-comment';
|
|
3
3
|
|
|
4
4
|
export type ThreadCommentJSON = {
|
|
@@ -70,5 +70,5 @@ export declare class ThreadCommentDataSourceService extends Disposable implement
|
|
|
70
70
|
listThreadComments(unitId: string, subUnitId: string, threadIds: string[]): Promise<IBaseComment[]>;
|
|
71
71
|
saveToSnapshot(unitComments: Record<string, IThreadComment[]>, unitId: string): Record<string, ThreadCommentJSON[]>;
|
|
72
72
|
}
|
|
73
|
-
export declare const IThreadCommentDataSourceService: import('@
|
|
73
|
+
export declare const IThreadCommentDataSourceService: import('@wendellhu/redi').IdentifierDecorator<IThreadCommentDataSourceService>;
|
|
74
74
|
export {};
|
package/lib/umd/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("rxjs"),require("@univerjs/core")):typeof define=="function"&&define.amd?define(["exports","rxjs","@univerjs/core"],l):(s=typeof globalThis<"u"?globalThis:s||self,l(s.UniverThreadComment={},s.rxjs,s.UniverCore))})(this,function(s,l,i){"use strict";var Z=Object.defineProperty;var z=(s,l,i)=>l in s?Z(s,l,{enumerable:!0,configurable:!0,writable:!0,value:i}):s[l]=i;var M=(s,l,i)=>z(s,typeof l!="symbol"?l+"":l,i);var T;class R extends i.Disposable{constructor(){super();M(this,"_dataSource",null);M(this,"syncUpdateMutationToColla",!0)}set dataSource(n){this._dataSource=n}get dataSource(){return this._dataSource}async getThreadComment(n,t,e){return this._dataSource?(await this._dataSource.listComments(n,t,[e]))[0]:null}async addComment(n){return this._dataSource?this._dataSource.addComment(n):n}async updateComment(n){return this._dataSource?this._dataSource.updateComment(n):!0}async resolveComment(n){return this._dataSource?this._dataSource.resolveComment(n):!0}async deleteComment(n,t,e,o){return this._dataSource?this._dataSource.deleteComment(n,t,e,o):!0}async listThreadComments(n,t,e){return this.dataSource?this.dataSource.listComments(n,t,e):[]}saveToSnapshot(n,t){if(this._dataSource){const e={};return Object.keys(n).forEach(o=>{const a=n[o];e[o]=a.map(this.dataSource.saveCommentToSnapshot)}),e}return n}}const f=i.createIdentifier("univer.thread-comment.data-source-service");var L=Object.defineProperty,V=Object.getOwnPropertyDescriptor,W=(r,m,n,t)=>{for(var e=t>1?void 0:t?V(m,n):m,o=r.length-1,a;o>=0;o--)(a=r[o])&&(e=(t?a(m,n,e):a(e))||e);return t&&e&&L(m,n,e),e},E=(r,m)=>(n,t)=>m(n,t,r);s.ThreadCommentModel=class extends i.Disposable{constructor(n,t){super();M(this,"_commentsMap",{});M(this,"_commentsTreeMap",new Map);M(this,"_threadMap",new Map);M(this,"_commentUpdate$",new l.Subject);M(this,"_commentsMap$",new l.BehaviorSubject({}));M(this,"commentUpdate$",this._commentUpdate$.asObservable());M(this,"commentMap$",this._commentsMap$.asObservable());this._dataSourceService=n,this._commandService=t,this.disposeWithMe(()=>{this._commentUpdate$.complete(),this._commentsMap$.complete()})}_ensureCommentMap(n,t){let e=this._commentsMap[n];e||(e={},this._commentsMap[n]=e);let o=e[t];return o||(o={},e[t]=o),o}_ensureCommentChildrenMap(n,t){let e=this._commentsTreeMap.get(n);e||(e=new Map,this._commentsTreeMap.set(n,e));let o=e.get(t);return o||(o=new Map,e.set(t,o)),o}_ensureThreadMap(n){let t=this._threadMap.get(n);return t||(t=new Map,this._threadMap.set(n,t)),t}_refreshCommentsMap$(){this._commentsMap$.next({...this._commentsMap})}ensureMap(n,t){const e=this._ensureCommentMap(n,t),o=this._ensureCommentChildrenMap(n,t);return{commentMap:e,commentChildrenMap:o}}_replaceComment(n,t,e){var c;const{commentMap:o,commentChildrenMap:a}=this.ensureMap(n,t),d=o[e.id];if(d){const h={...e,ref:d.ref};o[e.id]=h,(c=e.children)==null||c.forEach(u=>{o[u.id]={...u,ref:""}}),a.set(e.id,h),this._commentUpdate$.next({unitId:n,subUnitId:t,type:"syncUpdate",payload:h}),!!e.resolved!=!!d.resolved&&this._commentUpdate$.next({unitId:n,subUnitId:t,type:"resolve",payload:{commentId:e.id,resolved:!!e.resolved}})}}async syncThreadComments(n,t,e){const o=await this._dataSourceService.listThreadComments(n,t,e);if(!o.length)return;const a=new Set(e);o.forEach(d=>{this._replaceComment(n,t,d),a.delete(d.threadId)}),a.forEach(d=>{const c=this.getThread(n,d);c&&this.deleteComment(c.unitId,c.subUnitId,c.id)}),this._refreshCommentsMap$()}addComment(n,t,e,o){var C,_;const{commentMap:a,commentChildrenMap:d}=this.ensureMap(n,t),c=e,h=p=>{a[p.id]=p,this._commentUpdate$.next({unitId:n,subUnitId:t,type:"add",payload:p,isRoot:!p.parentId})},u=c.parentId;if(u){const p=a[u];p.children=[...(C=p.children)!=null?C:[],c],h(c)}else d.set(c.id,c),this._ensureThreadMap(n).set(c.threadId,c),h(c),(_=c.children)==null||_.forEach(S=>h({...S,ref:""}));return this._refreshCommentsMap$(),o&&this.syncThreadComments(n,t,[c.threadId]),!0}updateComment(n,t,e,o){const{commentMap:a}=this.ensureMap(n,t),d=a[e.commentId];return d?(d.updated=!0,d.text=e.text,d.attachments=e.attachments,d.updateT=e.updateT,this._commentUpdate$.next({unitId:n,subUnitId:t,type:"update",payload:e,silent:o}),this._refreshCommentsMap$(),!0):!1}updateCommentRef(n,t,e,o){const{commentMap:a}=this.ensureMap(n,t),d=a[e.commentId];return d?(d.ref=e.ref,this._commentUpdate$.next({unitId:n,subUnitId:t,type:"updateRef",payload:e,silent:o}),this._refreshCommentsMap$(),!0):!1}resolveComment(n,t,e,o){const{commentMap:a}=this.ensureMap(n,t),d=a[e];return d?(d.resolved=o,this._commentUpdate$.next({unitId:n,subUnitId:t,type:"resolve",payload:{commentId:e,resolved:o}}),this._refreshCommentsMap$(),!0):!1}getComment(n,t,e){const{commentMap:o}=this.ensureMap(n,t);return o[e]}getComment$(n,t,e){return this._commentsMap$.pipe(l.map(o=>o[n][t][e]))}getCommentWithChildren(n,t,e){var u,C;const{commentMap:o,commentChildrenMap:a}=this.ensureMap(n,t),d=o[e];if(!d)return;const c=new Set,h=a.get(e);if(h)return[h,...(u=h.children)!=null?u:[]].forEach(_=>{var p;c.add(_.personId),(p=_.text.customRanges)==null||p.forEach(S=>{S.rangeType===i.CustomRangeType.MENTION&&c.add(S.rangeId)})}),{root:d,children:(C=h.children)!=null?C:[],relativeUsers:c}}deleteComment(n,t,e){var c;const{commentMap:o,commentChildrenMap:a}=this.ensureMap(n,t),d=o[e];if(!d)return!0;if(d.parentId){const h=a.get(d.parentId);if(h&&h.children){const u=h.children.findIndex(C=>C.id=e);h.children.splice(u,1)}delete o[e]}else{delete o[e];const h=a.get(e);a.delete(e),this._ensureThreadMap(n).delete(d.threadId),(c=h==null?void 0:h.children)==null||c.forEach(C=>{delete o[C.id],this._commentUpdate$.next({unitId:n,subUnitId:t,type:"delete",payload:{commentId:C.id,isRoot:!1,comment:C}})})}return this._commentUpdate$.next({unitId:n,subUnitId:t,type:"delete",payload:{commentId:e,isRoot:!d.parentId,comment:d}}),this._refreshCommentsMap$(),!0}getUnit(n){const t=this._commentsMap[n];return t?Array.from(Object.entries(t)).map(([e,o])=>[e,Array.from(Object.values(o))]):[]}deleteUnit(n){const t=this._commentsMap[n];t&&Object.entries(t).forEach(([e,o])=>{Object.values(o).forEach(a=>{this.deleteComment(n,e,a.id)})})}getRootCommentIds(n,t){const e=this._ensureCommentChildrenMap(n,t);return Array.from(e.keys())}getAll(){return this._commentsMap}getThread(n,t){return this._ensureThreadMap(n).get(t)}},s.ThreadCommentModel=W([E(0,i.Inject(f)),E(1,i.ICommandService)],s.ThreadCommentModel);var y=(r=>(r[r.UNIVER_UNKNOWN=0]="UNIVER_UNKNOWN",r[r.UNIVER_DOC=1]="UNIVER_DOC",r[r.UNIVER_SHEET=2]="UNIVER_SHEET",r[r.UNIVER_SLIDE=3]="UNIVER_SLIDE",r[r.UNRECOGNIZED=-1]="UNRECOGNIZED",r))(y||{});const g="THREAD_COMMENT_PLUGIN";var J=Object.defineProperty,G=Object.getOwnPropertyDescriptor,H=(r,m,n,t)=>{for(var e=t>1?void 0:t?G(m,n):m,o=r.length-1,a;o>=0;o--)(a=r[o])&&(e=(t?a(m,n,e):a(e))||e);return t&&e&&J(m,n,e),e},U=(r,m)=>(n,t)=>m(n,t,r);s.ThreadCommentResourceController=class extends i.Disposable{constructor(m,n,t){super(),this._resourceManagerService=m,this._threadCommentModel=n,this._threadCommentDataSourceService=t,this._initSnapshot()}_initSnapshot(){const m=t=>{const e=this._threadCommentModel.getUnit(t),o={};return e?(e.forEach(([a,d])=>{o[a]=d}),JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(o,t))):""},n=t=>{if(!t)return{};try{return JSON.parse(t)}catch{return{}}};this.disposeWithMe(this._resourceManagerService.registerPluginResource({pluginName:`SHEET_${g}`,businesses:[y.UNIVER_SHEET,y.UNIVER_DOC],toJson:t=>m(t),parseJson:t=>n(t),onUnLoad:t=>{this._threadCommentModel.deleteUnit(t)},onLoad:async(t,e)=>{Object.keys(e).forEach(o=>{const a=e[o];a.forEach(d=>{this._threadCommentModel.addComment(t,o,d)}),this._threadCommentModel.syncThreadComments(t,o,a.map(d=>d.threadId))})}}))}},s.ThreadCommentResourceController=H([i.OnLifecycle(i.LifecycleStages.Starting,s.ThreadCommentResourceController),U(0,i.IResourceManagerService),U(1,i.Inject(s.ThreadCommentModel)),U(2,f)],s.ThreadCommentResourceController);const I={id:"thread-comment.mutation.add-comment",type:i.CommandType.MUTATION,handler(r,m,n){if(!m)return!1;const t=r.get(s.ThreadCommentModel),{unitId:e,subUnitId:o,comment:a,sync:d}=m,c=d||(n==null?void 0:n.fromChangeset)&&!a.parentId;return t.addComment(e,o,a,c)}},N={id:"thread-comment.mutation.update-comment",type:i.CommandType.MUTATION,handler(r,m){if(!m)return!1;const n=r.get(s.ThreadCommentModel),{unitId:t,subUnitId:e,payload:o,silent:a}=m;return n.updateComment(t,e,o,a)}},$={id:"thread-comment.mutation.update-comment-ref",type:i.CommandType.MUTATION,handler(r,m){if(!m)return!1;const n=r.get(s.ThreadCommentModel),{unitId:t,subUnitId:e,payload:o,silent:a}=m;return n.updateCommentRef(t,e,o,a)}},O={id:"thread-comment.mutation.resolve-comment",type:i.CommandType.MUTATION,handler(r,m){if(!m)return!1;const n=r.get(s.ThreadCommentModel),{unitId:t,subUnitId:e,resolved:o,commentId:a}=m;return n.resolveComment(t,e,a,o)}},v={id:"thread-comment.mutation.delete-comment",type:i.CommandType.MUTATION,handler(r,m){if(!m)return!1;const n=r.get(s.ThreadCommentModel),{unitId:t,subUnitId:e,commentId:o}=m;return n.deleteComment(t,e,o)}},D={id:"thread-comment.command.add-comment",type:i.CommandType.COMMAND,async handler(r,m){if(!m)return!1;const n=r.get(i.ICommandService),t=r.get(f),{comment:e}=m,o=await t.addComment(e),a=t.syncUpdateMutationToColla,d=!e.parentId,c={id:I.id,params:{...m,comment:o}};return d?await n.executeCommand(c.id,c.params):n.executeCommand(c.id,c.params,{onlyLocal:!a})}},w={id:"thread-comment.command.update-comment",type:i.CommandType.COMMAND,async handler(r,m){if(!m)return!1;const{unitId:n,subUnitId:t,payload:e}=m,o=r.get(i.ICommandService),a=r.get(s.ThreadCommentModel),d=r.get(f),c=d.syncUpdateMutationToColla,h=a.getComment(n,t,e.commentId);if(!h)return!1;const{children:u,...C}=h;if(!await d.updateComment({...C,...e}))return!1;const p={id:N.id,params:m};return o.executeCommand(p.id,p.params,{onlyLocal:!c}),!0}},P={id:"thread-comment.command.resolve-comment",type:i.CommandType.COMMAND,async handler(r,m){if(!m)return!1;const{unitId:n,subUnitId:t,resolved:e,commentId:o}=m,a=r.get(f),c=r.get(s.ThreadCommentModel).getComment(n,t,o),h=a.syncUpdateMutationToColla;return!c||!await a.resolveComment({...c,resolved:e})?!1:r.get(i.ICommandService).executeCommand(O.id,m,{onlyLocal:!h})}},j={id:"thread-comment.command.delete-comment",type:i.CommandType.COMMAND,async handler(r,m){if(!m)return!1;const n=r.get(s.ThreadCommentModel),t=r.get(f),e=r.get(i.ICommandService),{unitId:o,subUnitId:a,commentId:d}=m,c=t.syncUpdateMutationToColla,h=n.getComment(o,a,d);if(!h||!await t.deleteComment(o,a,h.threadId,d))return!1;const u={id:v.id,params:m};return e.executeCommand(u.id,u.params,{onlyLocal:!c})}},A={id:"thread-comment.command.delete-comment-tree",type:i.CommandType.COMMAND,async handler(r,m){if(!m)return!1;const n=r.get(s.ThreadCommentModel),t=r.get(i.ICommandService),e=r.get(f),{unitId:o,subUnitId:a,commentId:d}=m,c=n.getCommentWithChildren(o,a,d);return!c||!await e.deleteComment(o,a,c.root.threadId,d)?!1:await t.executeCommand(v.id,{unitId:o,subUnitId:a,commentId:c.root.id})}};var B=Object.defineProperty,K=Object.getOwnPropertyDescriptor,q=(r,m,n,t)=>{for(var e=t>1?void 0:t?K(m,n):m,o=r.length-1,a;o>=0;o--)(a=r[o])&&(e=(t?a(m,n,e):a(e))||e);return t&&e&&B(m,n,e),e},b=(r,m)=>(n,t)=>m(n,t,r);s.UniverThreadCommentPlugin=(T=class extends i.Plugin{constructor(n,t,e){super();M(this,"_config");this._injector=t,this._commandService=e,this._config=n}onStarting(n){var t;i.mergeOverrideWithDependencies([[f,{useClass:R}],[s.ThreadCommentModel],[s.ThreadCommentResourceController]],(t=this._config)==null?void 0:t.overrides).forEach(e=>{n.add(e)}),[D,w,j,P,A,I,N,$,v,O].forEach(e=>{this._commandService.registerCommand(e)})}},M(T,"pluginName",g),M(T,"type",i.UniverInstanceType.UNIVER_UNKNOWN),T),s.UniverThreadCommentPlugin=q([b(1,i.Inject(i.Injector)),b(2,i.ICommandService)],s.UniverThreadCommentPlugin),s.AddCommentCommand=D,s.AddCommentMutation=I,s.DeleteCommentCommand=j,s.DeleteCommentMutation=v,s.DeleteCommentTreeCommand=A,s.IThreadCommentDataSourceService=f,s.ResolveCommentCommand=P,s.ResolveCommentMutation=O,s.TC_PLUGIN_NAME=g,s.ThreadCommentDataSourceService=R,s.UpdateCommentCommand=w,s.UpdateCommentMutation=N,s.UpdateCommentRefMutation=$,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(s,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("rxjs"),require("@univerjs/core"),require("@wendellhu/redi")):typeof define=="function"&&define.amd?define(["exports","rxjs","@univerjs/core","@wendellhu/redi"],l):(s=typeof globalThis<"u"?globalThis:s||self,l(s.UniverThreadComment={},s.rxjs,s.UniverCore,s["@wendellhu/redi"]))})(this,function(s,l,i,v){"use strict";var z=Object.defineProperty;var F=(s,l,i)=>l in s?z(s,l,{enumerable:!0,configurable:!0,writable:!0,value:i}):s[l]=i;var M=(s,l,i)=>F(s,typeof l!="symbol"?l+"":l,i);var S;class E extends i.Disposable{constructor(){super();M(this,"_dataSource",null);M(this,"syncUpdateMutationToColla",!0)}set dataSource(n){this._dataSource=n}get dataSource(){return this._dataSource}async getThreadComment(n,t,e){return this._dataSource?(await this._dataSource.listComments(n,t,[e]))[0]:null}async addComment(n){return this._dataSource?this._dataSource.addComment(n):n}async updateComment(n){return this._dataSource?this._dataSource.updateComment(n):!0}async resolveComment(n){return this._dataSource?this._dataSource.resolveComment(n):!0}async deleteComment(n,t,e,o){return this._dataSource?this._dataSource.deleteComment(n,t,e,o):!0}async listThreadComments(n,t,e){return this.dataSource?this.dataSource.listComments(n,t,e):[]}saveToSnapshot(n,t){if(this._dataSource){const e={};return Object.keys(n).forEach(o=>{const r=n[o];e[o]=r.map(this.dataSource.saveCommentToSnapshot)}),e}return n}}const f=v.createIdentifier("univer.thread-comment.data-source-service");var V=Object.defineProperty,W=Object.getOwnPropertyDescriptor,J=(a,m,n,t)=>{for(var e=t>1?void 0:t?W(m,n):m,o=a.length-1,r;o>=0;o--)(r=a[o])&&(e=(t?r(m,n,e):r(e))||e);return t&&e&&V(m,n,e),e},$=(a,m)=>(n,t)=>m(n,t,a);s.ThreadCommentModel=class extends i.Disposable{constructor(n,t){super();M(this,"_commentsMap",{});M(this,"_commentsTreeMap",new Map);M(this,"_threadMap",new Map);M(this,"_commentUpdate$",new l.Subject);M(this,"_commentsMap$",new l.BehaviorSubject({}));M(this,"commentUpdate$",this._commentUpdate$.asObservable());M(this,"commentMap$",this._commentsMap$.asObservable());this._dataSourceService=n,this._commandService=t,this.disposeWithMe(()=>{this._commentUpdate$.complete(),this._commentsMap$.complete()})}_ensureCommentMap(n,t){let e=this._commentsMap[n];e||(e={},this._commentsMap[n]=e);let o=e[t];return o||(o={},e[t]=o),o}_ensureCommentChildrenMap(n,t){let e=this._commentsTreeMap.get(n);e||(e=new Map,this._commentsTreeMap.set(n,e));let o=e.get(t);return o||(o=new Map,e.set(t,o)),o}_ensureThreadMap(n){let t=this._threadMap.get(n);return t||(t=new Map,this._threadMap.set(n,t)),t}_refreshCommentsMap$(){this._commentsMap$.next({...this._commentsMap})}ensureMap(n,t){const e=this._ensureCommentMap(n,t),o=this._ensureCommentChildrenMap(n,t);return{commentMap:e,commentChildrenMap:o}}_replaceComment(n,t,e){var c;const{commentMap:o,commentChildrenMap:r}=this.ensureMap(n,t),d=o[e.id];if(d){const h={...e,ref:d.ref};o[e.id]=h,(c=e.children)==null||c.forEach(u=>{o[u.id]={...u,ref:""}}),r.set(e.id,h),this._commentUpdate$.next({unitId:n,subUnitId:t,type:"syncUpdate",payload:h}),!!e.resolved!=!!d.resolved&&this._commentUpdate$.next({unitId:n,subUnitId:t,type:"resolve",payload:{commentId:e.id,resolved:!!e.resolved}})}}async syncThreadComments(n,t,e){const o=await this._dataSourceService.listThreadComments(n,t,e);if(!o.length)return;const r=new Set(e);o.forEach(d=>{this._replaceComment(n,t,d),r.delete(d.threadId)}),r.forEach(d=>{const c=this.getThread(n,d);c&&this.deleteComment(c.unitId,c.subUnitId,c.id)}),this._refreshCommentsMap$()}addComment(n,t,e,o){var C,_;const{commentMap:r,commentChildrenMap:d}=this.ensureMap(n,t),c=e,h=p=>{r[p.id]=p,this._commentUpdate$.next({unitId:n,subUnitId:t,type:"add",payload:p,isRoot:!p.parentId})},u=c.parentId;if(u){const p=r[u];p.children=[...(C=p.children)!=null?C:[],c],h(c)}else d.set(c.id,c),this._ensureThreadMap(n).set(c.threadId,c),h(c),(_=c.children)==null||_.forEach(y=>h({...y,ref:""}));return this._refreshCommentsMap$(),o&&this.syncThreadComments(n,t,[c.threadId]),!0}updateComment(n,t,e,o){const{commentMap:r}=this.ensureMap(n,t),d=r[e.commentId];return d?(d.updated=!0,d.text=e.text,d.attachments=e.attachments,d.updateT=e.updateT,this._commentUpdate$.next({unitId:n,subUnitId:t,type:"update",payload:e,silent:o}),this._refreshCommentsMap$(),!0):!1}updateCommentRef(n,t,e,o){const{commentMap:r}=this.ensureMap(n,t),d=r[e.commentId];return d?(d.ref=e.ref,this._commentUpdate$.next({unitId:n,subUnitId:t,type:"updateRef",payload:e,silent:o}),this._refreshCommentsMap$(),!0):!1}resolveComment(n,t,e,o){const{commentMap:r}=this.ensureMap(n,t),d=r[e];return d?(d.resolved=o,this._commentUpdate$.next({unitId:n,subUnitId:t,type:"resolve",payload:{commentId:e,resolved:o}}),this._refreshCommentsMap$(),!0):!1}getComment(n,t,e){const{commentMap:o}=this.ensureMap(n,t);return o[e]}getComment$(n,t,e){return this._commentsMap$.pipe(l.map(o=>o[n][t][e]))}getCommentWithChildren(n,t,e){var u,C;const{commentMap:o,commentChildrenMap:r}=this.ensureMap(n,t),d=o[e];if(!d)return;const c=new Set,h=r.get(e);if(h)return[h,...(u=h.children)!=null?u:[]].forEach(_=>{var p;c.add(_.personId),(p=_.text.customRanges)==null||p.forEach(y=>{y.rangeType===i.CustomRangeType.MENTION&&c.add(y.rangeId)})}),{root:d,children:(C=h.children)!=null?C:[],relativeUsers:c}}deleteComment(n,t,e){var c;const{commentMap:o,commentChildrenMap:r}=this.ensureMap(n,t),d=o[e];if(!d)return!0;if(d.parentId){const h=r.get(d.parentId);if(h&&h.children){const u=h.children.findIndex(C=>C.id=e);h.children.splice(u,1)}delete o[e]}else{delete o[e];const h=r.get(e);r.delete(e),this._ensureThreadMap(n).delete(d.threadId),(c=h==null?void 0:h.children)==null||c.forEach(C=>{delete o[C.id],this._commentUpdate$.next({unitId:n,subUnitId:t,type:"delete",payload:{commentId:C.id,isRoot:!1,comment:C}})})}return this._commentUpdate$.next({unitId:n,subUnitId:t,type:"delete",payload:{commentId:e,isRoot:!d.parentId,comment:d}}),this._refreshCommentsMap$(),!0}getUnit(n){const t=this._commentsMap[n];return t?Array.from(Object.entries(t)).map(([e,o])=>[e,Array.from(Object.values(o))]):[]}deleteUnit(n){const t=this._commentsMap[n];t&&Object.entries(t).forEach(([e,o])=>{Object.values(o).forEach(r=>{this.deleteComment(n,e,r.id)})})}getRootCommentIds(n,t){const e=this._ensureCommentChildrenMap(n,t);return Array.from(e.keys())}getAll(){return this._commentsMap}getThread(n,t){return this._ensureThreadMap(n).get(t)}},s.ThreadCommentModel=J([$(0,v.Inject(f)),$(1,i.ICommandService)],s.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.UNRECOGNIZED=-1]="UNRECOGNIZED",a))(g||{});const U="THREAD_COMMENT_PLUGIN";var G=Object.defineProperty,H=Object.getOwnPropertyDescriptor,B=(a,m,n,t)=>{for(var e=t>1?void 0:t?H(m,n):m,o=a.length-1,r;o>=0;o--)(r=a[o])&&(e=(t?r(m,n,e):r(e))||e);return t&&e&&G(m,n,e),e},I=(a,m)=>(n,t)=>m(n,t,a);s.ThreadCommentResourceController=class extends i.Disposable{constructor(m,n,t){super(),this._resourceManagerService=m,this._threadCommentModel=n,this._threadCommentDataSourceService=t,this._initSnapshot()}_initSnapshot(){const m=t=>{const e=this._threadCommentModel.getUnit(t),o={};return e?(e.forEach(([r,d])=>{o[r]=d}),JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(o,t))):""},n=t=>{if(!t)return{};try{return JSON.parse(t)}catch{return{}}};this.disposeWithMe(this._resourceManagerService.registerPluginResource({pluginName:`SHEET_${U}`,businesses:[g.UNIVER_SHEET,g.UNIVER_DOC],toJson:t=>m(t),parseJson:t=>n(t),onUnLoad:t=>{this._threadCommentModel.deleteUnit(t)},onLoad:async(t,e)=>{Object.keys(e).forEach(o=>{const r=e[o];r.forEach(d=>{this._threadCommentModel.addComment(t,o,d)}),this._threadCommentModel.syncThreadComments(t,o,r.map(d=>d.threadId))})}}))}},s.ThreadCommentResourceController=B([i.OnLifecycle(i.LifecycleStages.Starting,s.ThreadCommentResourceController),I(0,i.IResourceManagerService),I(1,v.Inject(s.ThreadCommentModel)),I(2,f)],s.ThreadCommentResourceController);const N={id:"thread-comment.mutation.add-comment",type:i.CommandType.MUTATION,handler(a,m,n){if(!m)return!1;const t=a.get(s.ThreadCommentModel),{unitId:e,subUnitId:o,comment:r,sync:d}=m,c=d||(n==null?void 0:n.fromChangeset)&&!r.parentId;return t.addComment(e,o,r,c)}},O={id:"thread-comment.mutation.update-comment",type:i.CommandType.MUTATION,handler(a,m){if(!m)return!1;const n=a.get(s.ThreadCommentModel),{unitId:t,subUnitId:e,payload:o,silent:r}=m;return n.updateComment(t,e,o,r)}},D={id:"thread-comment.mutation.update-comment-ref",type:i.CommandType.MUTATION,handler(a,m){if(!m)return!1;const n=a.get(s.ThreadCommentModel),{unitId:t,subUnitId:e,payload:o,silent:r}=m;return n.updateCommentRef(t,e,o,r)}},R={id:"thread-comment.mutation.resolve-comment",type:i.CommandType.MUTATION,handler(a,m){if(!m)return!1;const n=a.get(s.ThreadCommentModel),{unitId:t,subUnitId:e,resolved:o,commentId:r}=m;return n.resolveComment(t,e,r,o)}},T={id:"thread-comment.mutation.delete-comment",type:i.CommandType.MUTATION,handler(a,m){if(!m)return!1;const n=a.get(s.ThreadCommentModel),{unitId:t,subUnitId:e,commentId:o}=m;return n.deleteComment(t,e,o)}},w={id:"thread-comment.command.add-comment",type:i.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const n=a.get(i.ICommandService),t=a.get(f),{comment:e}=m,o=await t.addComment(e),r=t.syncUpdateMutationToColla,d=!e.parentId,c={id:N.id,params:{...m,comment:o}};return d?await n.executeCommand(c.id,c.params):n.executeCommand(c.id,c.params,{onlyLocal:!r})}},P={id:"thread-comment.command.update-comment",type:i.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const{unitId:n,subUnitId:t,payload:e}=m,o=a.get(i.ICommandService),r=a.get(s.ThreadCommentModel),d=a.get(f),c=d.syncUpdateMutationToColla,h=r.getComment(n,t,e.commentId);if(!h)return!1;const{children:u,...C}=h;if(!await d.updateComment({...C,...e}))return!1;const p={id:O.id,params:m};return o.executeCommand(p.id,p.params,{onlyLocal:!c}),!0}},j={id:"thread-comment.command.resolve-comment",type:i.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const{unitId:n,subUnitId:t,resolved:e,commentId:o}=m,r=a.get(f),c=a.get(s.ThreadCommentModel).getComment(n,t,o),h=r.syncUpdateMutationToColla;return!c||!await r.resolveComment({...c,resolved:e})?!1:a.get(i.ICommandService).executeCommand(R.id,m,{onlyLocal:!h})}},A={id:"thread-comment.command.delete-comment",type:i.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const n=a.get(s.ThreadCommentModel),t=a.get(f),e=a.get(i.ICommandService),{unitId:o,subUnitId:r,commentId:d}=m,c=t.syncUpdateMutationToColla,h=n.getComment(o,r,d);if(!h||!await t.deleteComment(o,r,h.threadId,d))return!1;const u={id:T.id,params:m};return e.executeCommand(u.id,u.params,{onlyLocal:!c})}},b={id:"thread-comment.command.delete-comment-tree",type:i.CommandType.COMMAND,async handler(a,m){if(!m)return!1;const n=a.get(s.ThreadCommentModel),t=a.get(i.ICommandService),e=a.get(f),{unitId:o,subUnitId:r,commentId:d}=m,c=n.getCommentWithChildren(o,r,d);return!c||!await e.deleteComment(o,r,c.root.threadId,d)?!1:await t.executeCommand(T.id,{unitId:o,subUnitId:r,commentId:c.root.id})}};var q=Object.defineProperty,K=Object.getOwnPropertyDescriptor,Z=(a,m,n,t)=>{for(var e=t>1?void 0:t?K(m,n):m,o=a.length-1,r;o>=0;o--)(r=a[o])&&(e=(t?r(m,n,e):r(e))||e);return t&&e&&q(m,n,e),e},L=(a,m)=>(n,t)=>m(n,t,a);s.UniverThreadCommentPlugin=(S=class extends i.Plugin{constructor(n,t,e){super();M(this,"_config");this._injector=t,this._commandService=e,this._config=n}onStarting(n){var t;i.mergeOverrideWithDependencies([[f,{useClass:E}],[s.ThreadCommentModel],[s.ThreadCommentResourceController]],(t=this._config)==null?void 0:t.overrides).forEach(e=>{n.add(e)}),[w,P,A,j,b,N,O,D,T,R].forEach(e=>{this._commandService.registerCommand(e)})}},M(S,"pluginName",U),M(S,"type",i.UniverInstanceType.UNIVER_UNKNOWN),S),s.UniverThreadCommentPlugin=Z([L(1,v.Inject(v.Injector)),L(2,i.ICommandService)],s.UniverThreadCommentPlugin),s.AddCommentCommand=w,s.AddCommentMutation=N,s.DeleteCommentCommand=A,s.DeleteCommentMutation=T,s.DeleteCommentTreeCommand=b,s.IThreadCommentDataSourceService=f,s.ResolveCommentCommand=j,s.ResolveCommentMutation=R,s.TC_PLUGIN_NAME=U,s.ThreadCommentDataSourceService=E,s.UpdateCommentCommand=P,s.UpdateCommentMutation=O,s.UpdateCommentRefMutation=D,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.2.4
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Univer thread comment core plugin",
|
|
6
6
|
"author": "DreamNum <developer@univer.ai>",
|
|
@@ -47,19 +47,21 @@
|
|
|
47
47
|
"lib"
|
|
48
48
|
],
|
|
49
49
|
"peerDependencies": {
|
|
50
|
+
"@wendellhu/redi": "0.16.0",
|
|
50
51
|
"rxjs": ">=7.0.0",
|
|
51
|
-
"@univerjs/core": "0.2.4
|
|
52
|
+
"@univerjs/core": "0.2.4"
|
|
52
53
|
},
|
|
53
54
|
"dependencies": {
|
|
54
55
|
"@univerjs/protocol": "0.1.38-alpha.23"
|
|
55
56
|
},
|
|
56
57
|
"devDependencies": {
|
|
58
|
+
"@wendellhu/redi": "0.16.0",
|
|
57
59
|
"rxjs": "^7.8.1",
|
|
58
60
|
"typescript": "^5.5.3",
|
|
59
61
|
"vite": "^5.3.4",
|
|
60
62
|
"vitest": "^2.0.3",
|
|
61
|
-
"@univerjs/
|
|
62
|
-
"@univerjs/
|
|
63
|
+
"@univerjs/shared": "0.2.4",
|
|
64
|
+
"@univerjs/core": "0.2.4"
|
|
63
65
|
},
|
|
64
66
|
"univerSpace": {
|
|
65
67
|
".": {
|