@univerjs/thread-comment 0.4.2 → 0.5.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var __defProp=Object.defineProperty;var __defNormalProp=(obj,key,value)=>key in obj?__defProp(obj,key,{enumerable:!0,configurable:!0,writable:!0,value}):obj[key]=value;var __name=(target,value)=>__defProp(target,"name",{value,configurable:!0});var __publicField=(obj,key,value)=>__defNormalProp(obj,typeof key!="symbol"?key+"":key,value);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const core=require("@univerjs/core"),rxjs=require("rxjs"),_ThreadCommentDataSourceService=class _ThreadCommentDataSourceService extends core.Disposable{constructor(){super();__publicField(this,"_dataSource",null);__publicField(this,"syncUpdateMutationToColla",!0)}set dataSource(dataSource){this._dataSource=dataSource}get dataSource(){return this._dataSource}async getThreadComment(unitId,subUnitId,threadId){return this._dataSource?(await this._dataSource.listComments(unitId,subUnitId,[threadId]))[0]:null}async addComment(comment){var _a4;return this._dataSource?this._dataSource.addComment(comment):{...comment,threadId:(_a4=comment.threadId)!=null?_a4:comment.id}}async updateComment(comment){return this._dataSource?this._dataSource.updateComment(comment):!0}async resolveComment(comment){return this._dataSource?this._dataSource.resolveComment(comment):!0}async deleteComment(unitId,subUnitId,threadId,commentId){return this._dataSource?this._dataSource.deleteComment(unitId,subUnitId,threadId,commentId):!0}async listThreadComments(unitId,subUnitId,threadIds){return this.dataSource?this.dataSource.listComments(unitId,subUnitId,threadIds):!1}saveToSnapshot(unitComments,unitId){if(this._dataSource){const map={};return Object.keys(unitComments).forEach(subUnitId=>{const comments=unitComments[subUnitId];map[subUnitId]=comments.map(this.dataSource.saveCommentToSnapshot)}),map}return unitComments}};__name(_ThreadCommentDataSourceService,"ThreadCommentDataSourceService");let ThreadCommentDataSourceService=_ThreadCommentDataSourceService;const IThreadCommentDataSourceService=core.createIdentifier("univer.thread-comment.data-source-service");var __defProp$2=Object.defineProperty,__getOwnPropDesc$2=Object.getOwnPropertyDescriptor,__decorateClass$2=__name((decorators,target,key,kind)=>{for(var result=kind>1?void 0:kind?__getOwnPropDesc$2(target,key):target,i=decorators.length-1,decorator;i>=0;i--)(decorator=decorators[i])&&(result=(kind?decorator(target,key,result):decorator(result))||result);return kind&&result&&__defProp$2(target,key,result),result},"__decorateClass$2"),__decorateParam$2=__name((index,decorator)=>(target,key)=>decorator(target,key,index),"__decorateParam$2"),_a;exports.ThreadCommentModel=(_a=class extends core.Disposable{constructor(_dataSourceService,_lifecycleService){super();__publicField(this,"_commentsMap",new Map);__publicField(this,"_threadMap",new Map);__publicField(this,"_commentUpdate$",new rxjs.Subject);__publicField(this,"commentUpdate$",this._commentUpdate$.asObservable());__publicField(this,"_tasks",[]);this._dataSourceService=_dataSourceService,this._lifecycleService=_lifecycleService,this.disposeWithMe(()=>{this._commentUpdate$.complete()}),this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe(stage=>{const taskMap=new Map;stage===core.LifecycleStages.Rendered&&(this._tasks.forEach(({unitId,subUnitId,threadIds})=>{let unitMap=taskMap.get(unitId);unitMap||(unitMap=new Map,taskMap.set(unitId,unitMap));let subUnitMap=unitMap.get(subUnitId);subUnitMap||(subUnitMap=new Set,unitMap.set(subUnitId,subUnitMap));for(const threadId of threadIds)subUnitMap.add(threadId)}),this._tasks=[],taskMap.forEach((subUnitMap,unitId)=>{subUnitMap.forEach((threadIds,subUnitId)=>{this.syncThreadComments(unitId,subUnitId,Array.from(threadIds))})}))}))}_ensureCommentMap(unitId,subUnitId){let unitMap=this._commentsMap.get(unitId);unitMap||(unitMap=new Map,this._commentsMap.set(unitId,unitMap));let subUnitMap=unitMap.get(subUnitId);return subUnitMap||(subUnitMap=new Map,unitMap.set(subUnitId,subUnitMap)),subUnitMap}ensureMap(unitId,subUnitId){return this._ensureCommentMap(unitId,subUnitId)}_ensureThreadMap(unitId,subUnitId){let unitMap=this._threadMap.get(unitId);unitMap||(unitMap=new Map,this._threadMap.set(unitId,unitMap));let subUnitMap=unitMap.get(subUnitId);return subUnitMap||(subUnitMap=new Map,unitMap.set(subUnitId,subUnitMap)),subUnitMap}_replaceComment(unitId,subUnitId,comment){const commentMap=this._ensureCommentMap(unitId,subUnitId),currentComment=commentMap.get(comment.id);if(currentComment){const{children,...rest}=comment,newComment={...rest,ref:currentComment.ref};commentMap.set(comment.id,newComment),children==null||children.forEach(child=>{commentMap.set(child.id,{...child,ref:""})}),this._commentUpdate$.next({unitId,subUnitId,type:"syncUpdate",payload:newComment}),!!comment.resolved!=!!currentComment.resolved&&this._commentUpdate$.next({unitId,subUnitId,type:"resolve",payload:{commentId:comment.id,resolved:!!comment.resolved}})}}async syncThreadComments(unitId,subUnitId,threadIds){if(this._lifecycleService.stage<core.LifecycleStages.Rendered){this._tasks.push({unitId,subUnitId,threadIds});return}const threadMap=this._ensureThreadMap(unitId,subUnitId),commentMap=this._ensureCommentMap(unitId,subUnitId),comments=await this._dataSourceService.listThreadComments(unitId,subUnitId,threadIds);if(!comments)return;const deleteThreads=new Set(threadIds);comments.forEach(comment=>{this._replaceComment(unitId,subUnitId,comment),deleteThreads.delete(comment.threadId)}),deleteThreads.forEach(id=>{threadMap.delete(id),commentMap.forEach((comment,commentId)=>{comment.threadId===id&&commentMap.delete(commentId)})})}addComment(unitId,subUnitId,origin,shouldSync){const commentMap=this._ensureCommentMap(unitId,subUnitId),{parentId,children=[],...rest}=origin,comment={...rest,parentId:parentId===origin.id?void 0:parentId},addCommentItem=__name(item=>{commentMap.set(item.id,item),this._commentUpdate$.next({unitId,subUnitId,type:"add",payload:item,isRoot:!item.parentId})},"addCommentItem");addCommentItem(comment);const threadMap=this._ensureThreadMap(unitId,subUnitId);if(!comment.parentId){threadMap.set(comment.threadId,comment);for(const child of children)addCommentItem(child)}return shouldSync&&this.syncThreadComments(unitId,subUnitId,[comment.threadId]),!0}updateComment(unitId,subUnitId,payload,silent){const oldComment=this._ensureCommentMap(unitId,subUnitId).get(payload.commentId);return oldComment&&(oldComment.updated=!0,oldComment.text=payload.text,oldComment.attachments=payload.attachments,oldComment.updateT=payload.updateT,this._commentUpdate$.next({unitId,subUnitId,type:"update",payload,silent})),!0}updateCommentRef(unitId,subUnitId,payload,silent){const oldComment=this._ensureCommentMap(unitId,subUnitId).get(payload.commentId);return oldComment?(oldComment.ref=payload.ref,this._commentUpdate$.next({unitId,subUnitId,type:"updateRef",payload,silent,threadId:oldComment.threadId}),!0):!1}resolveComment(unitId,subUnitId,commentId,resolved){const oldComment=this._ensureCommentMap(unitId,subUnitId).get(commentId);return oldComment?(oldComment.resolved=resolved,this._commentUpdate$.next({unitId,subUnitId,type:"resolve",payload:{commentId,resolved}}),!0):!1}getComment(unitId,subUnitId,commentId){return this._ensureCommentMap(unitId,subUnitId).get(commentId)}getRootComment(unitId,subUnitId,threadId){return this._ensureThreadMap(unitId,subUnitId).get(threadId)}getThread(unitId,subUnitId,threadId){const commentMap=this._ensureCommentMap(unitId,subUnitId),comments=Array.from(commentMap.values()).filter(comment=>comment.threadId===threadId);let root;const children=[],relativeUsers=new Set;for(const comment of comments)comment.parentId?children.push(comment):root=comment,relativeUsers.add(comment.personId);if(root)return{root,children,relativeUsers,unitId,subUnitId,threadId}}getCommentWithChildren(unitId,subUnitId,commentId){const comment=this.getComment(unitId,subUnitId,commentId);if(comment)return this.getThread(unitId,subUnitId,comment.threadId)}_deleteComment(unitId,subUnitId,commentId){const commentMap=this._ensureCommentMap(unitId,subUnitId),current=commentMap.get(commentId);current&&(commentMap.delete(commentId),this._commentUpdate$.next({unitId,subUnitId,type:"delete",payload:{commentId,isRoot:!current.parentId,comment:current}}))}deleteThread(unitId,subUnitId,threadId){this._ensureThreadMap(unitId,subUnitId).delete(threadId),this._ensureCommentMap(unitId,subUnitId).forEach(comment=>{comment.threadId===threadId&&this._deleteComment(unitId,subUnitId,comment.id)})}deleteComment(unitId,subUnitId,commentId){const current=this._ensureCommentMap(unitId,subUnitId).get(commentId);return current&&(current.parentId?this._deleteComment(unitId,subUnitId,commentId):this.deleteThread(unitId,subUnitId,current.threadId)),!0}deleteUnit(unitId){const unitMap=this._commentsMap.get(unitId);unitMap&&unitMap.forEach((subUnitMap,subUnitId)=>{subUnitMap.forEach(comment=>{this.deleteComment(unitId,subUnitId,comment.id)})})}getUnit(unitId){const unitMap=this._threadMap.get(unitId);if(!unitMap)return[];const threads=[];return unitMap.forEach((subUnitSet,subUnitId)=>{subUnitSet.forEach((threadComment,threadId)=>{const thread=this.getThread(unitId,subUnitId,threadId);thread&&threads.push(thread)})}),threads}getAll(){const all=[];return this._commentsMap.forEach((unitMap,unitId)=>{all.push({unitId,threads:this.getUnit(unitId)})}),all}},__name(_a,"ThreadCommentModel"),_a);exports.ThreadCommentModel=__decorateClass$2([__decorateParam$2(0,core.Inject(IThreadCommentDataSourceService)),__decorateParam$2(1,core.Inject(core.LifecycleService))],exports.ThreadCommentModel);var _=(E=>(E[E.UNIVER_UNKNOWN=0]="UNIVER_UNKNOWN",E[E.UNIVER_DOC=1]="UNIVER_DOC",E[E.UNIVER_SHEET=2]="UNIVER_SHEET",E[E.UNIVER_SLIDE=3]="UNIVER_SLIDE",E[E.UNIVER_PROJECT=4]="UNIVER_PROJECT",E[E.UNRECOGNIZED=-1]="UNRECOGNIZED",E))(_||{});const TC_PLUGIN_NAME="UNIVER_THREAD_COMMENT_PLUGIN";var __defProp$1=Object.defineProperty,__getOwnPropDesc$1=Object.getOwnPropertyDescriptor,__decorateClass$1=__name((decorators,target,key,kind)=>{for(var result=kind>1?void 0:kind?__getOwnPropDesc$1(target,key):target,i=decorators.length-1,decorator;i>=0;i--)(decorator=decorators[i])&&(result=(kind?decorator(target,key,result):decorator(result))||result);return kind&&result&&__defProp$1(target,key,result),result},"__decorateClass$1"),__decorateParam$1=__name((index,decorator)=>(target,key)=>decorator(target,key,index),"__decorateParam$1"),_a2;exports.ThreadCommentResourceController=(_a2=class extends core.Disposable{constructor(_resourceManagerService,_threadCommentModel,_threadCommentDataSourceService){super(),this._resourceManagerService=_resourceManagerService,this._threadCommentModel=_threadCommentModel,this._threadCommentDataSourceService=_threadCommentDataSourceService,this._initSnapshot()}_initSnapshot(){const toJson=__name(unitID=>{const map=this._threadCommentModel.getUnit(unitID),resultMap={};return map?(map.forEach(info=>{var _a4;const subUnitComments=(_a4=resultMap[info.subUnitId])!=null?_a4:[];subUnitComments.push({...info.root,children:info.children}),resultMap[info.unitId]=subUnitComments}),JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(resultMap,unitID))):""},"toJson"),parseJson=__name(json=>{if(!json)return{};try{return JSON.parse(json)}catch{return{}}},"parseJson");this.disposeWithMe(this._resourceManagerService.registerPluginResource({pluginName:`SHEET_${TC_PLUGIN_NAME}`,businesses:[_.UNIVER_SHEET,_.UNIVER_DOC],toJson:__name(unitID=>toJson(unitID),"toJson"),parseJson:__name(json=>parseJson(json),"parseJson"),onUnLoad:__name(unitID=>{this._threadCommentModel.deleteUnit(unitID)},"onUnLoad"),onLoad:__name(async(unitID,value)=>{Object.keys(value).forEach(subunitId=>{const commentList=value[subunitId];commentList.forEach(comment=>{this._threadCommentModel.addComment(unitID,subunitId,comment)}),this._threadCommentModel.syncThreadComments(unitID,subunitId,commentList.map(i=>i.threadId))})},"onLoad")}))}},__name(_a2,"ThreadCommentResourceController"),_a2);exports.ThreadCommentResourceController=__decorateClass$1([__decorateParam$1(0,core.IResourceManagerService),__decorateParam$1(1,core.Inject(exports.ThreadCommentModel)),__decorateParam$1(2,IThreadCommentDataSourceService)],exports.ThreadCommentResourceController);const AddCommentMutation={id:"thread-comment.mutation.add-comment",type:core.CommandType.MUTATION,handler(accessor,params,options){if(!params)return!1;const threadCommentModel=accessor.get(exports.ThreadCommentModel),{unitId,subUnitId,comment,sync}=params,shouldSync=sync||(options==null?void 0:options.fromChangeset)&&!comment.parentId;return threadCommentModel.addComment(unitId,subUnitId,comment,shouldSync)}},UpdateCommentMutation={id:"thread-comment.mutation.update-comment",type:core.CommandType.MUTATION,handler(accessor,params){if(!params)return!1;const threadCommentModel=accessor.get(exports.ThreadCommentModel),{unitId,subUnitId,payload,silent}=params;return threadCommentModel.updateComment(unitId,subUnitId,payload,silent)}},UpdateCommentRefMutation={id:"thread-comment.mutation.update-comment-ref",type:core.CommandType.MUTATION,handler(accessor,params){if(!params)return!1;const threadCommentModel=accessor.get(exports.ThreadCommentModel),{unitId,subUnitId,payload,silent}=params;return threadCommentModel.updateCommentRef(unitId,subUnitId,payload,silent)}},ResolveCommentMutation={id:"thread-comment.mutation.resolve-comment",type:core.CommandType.MUTATION,handler(accessor,params){if(!params)return!1;const threadCommentModel=accessor.get(exports.ThreadCommentModel),{unitId,subUnitId,resolved,commentId}=params;return threadCommentModel.resolveComment(unitId,subUnitId,commentId,resolved)}},DeleteCommentMutation={id:"thread-comment.mutation.delete-comment",type:core.CommandType.MUTATION,handler(accessor,params){if(!params)return!1;const threadCommentModel=accessor.get(exports.ThreadCommentModel),{unitId,subUnitId,commentId}=params;return threadCommentModel.deleteComment(unitId,subUnitId,commentId)}},AddCommentCommand={id:"thread-comment.command.add-comment",type:core.CommandType.COMMAND,async handler(accessor,params){if(!params)return!1;const commandService=accessor.get(core.ICommandService),dataSourceService=accessor.get(IThreadCommentDataSourceService),{comment:originComment}=params,comment=await dataSourceService.addComment(originComment),syncUpdateMutationToColla=dataSourceService.syncUpdateMutationToColla,isRoot=!originComment.parentId,redo={id:AddCommentMutation.id,params:{...params,comment}};return isRoot?await commandService.executeCommand(redo.id,redo.params):commandService.executeCommand(redo.id,redo.params,{onlyLocal:!syncUpdateMutationToColla})}},UpdateCommentCommand={id:"thread-comment.command.update-comment",type:core.CommandType.COMMAND,async handler(accessor,params){if(!params)return!1;const{unitId,subUnitId,payload}=params,commandService=accessor.get(core.ICommandService),threadCommentModel=accessor.get(exports.ThreadCommentModel),dataSourceService=accessor.get(IThreadCommentDataSourceService),syncUpdateMutationToColla=dataSourceService.syncUpdateMutationToColla,current=threadCommentModel.getComment(unitId,subUnitId,payload.commentId);if(!current)return!1;const{children,...currentComment}=current;if(!await dataSourceService.updateComment({...currentComment,...payload}))return!1;const redo={id:UpdateCommentMutation.id,params};return commandService.executeCommand(redo.id,redo.params,{onlyLocal:!syncUpdateMutationToColla}),!0}},ResolveCommentCommand={id:"thread-comment.command.resolve-comment",type:core.CommandType.COMMAND,async handler(accessor,params){if(!params)return!1;const{unitId,subUnitId,resolved,commentId}=params,dataSourceService=accessor.get(IThreadCommentDataSourceService),currentComment=accessor.get(exports.ThreadCommentModel).getComment(unitId,subUnitId,commentId),syncUpdateMutationToColla=dataSourceService.syncUpdateMutationToColla;return!currentComment||!await dataSourceService.resolveComment({...currentComment,resolved})?!1:accessor.get(core.ICommandService).executeCommand(ResolveCommentMutation.id,params,{onlyLocal:!syncUpdateMutationToColla})}},DeleteCommentCommand={id:"thread-comment.command.delete-comment",type:core.CommandType.COMMAND,async handler(accessor,params){if(!params)return!1;const threadCommentModel=accessor.get(exports.ThreadCommentModel),dataSourceService=accessor.get(IThreadCommentDataSourceService),commandService=accessor.get(core.ICommandService),{unitId,subUnitId,commentId}=params,syncUpdateMutationToColla=dataSourceService.syncUpdateMutationToColla,comment=threadCommentModel.getComment(unitId,subUnitId,commentId);if(!comment||!await dataSourceService.deleteComment(unitId,subUnitId,comment.threadId,commentId))return!1;const redo={id:DeleteCommentMutation.id,params};return commandService.executeCommand(redo.id,redo.params,{onlyLocal:!syncUpdateMutationToColla})}},DeleteCommentTreeCommand={id:"thread-comment.command.delete-comment-tree",type:core.CommandType.COMMAND,async handler(accessor,params){if(!params)return!1;const threadCommentModel=accessor.get(exports.ThreadCommentModel),commandService=accessor.get(core.ICommandService),dataSourceService=accessor.get(IThreadCommentDataSourceService),{unitId,subUnitId,commentId}=params,commentWithChildren=threadCommentModel.getCommentWithChildren(unitId,subUnitId,commentId);return!commentWithChildren||!await dataSourceService.deleteComment(unitId,subUnitId,commentWithChildren.root.threadId,commentId)?!1:await commandService.executeCommand(DeleteCommentMutation.id,{unitId,subUnitId,commentId:commentWithChildren.root.id})}},PLUGIN_CONFIG_KEY="thread-comment.config",defaultPluginConfig={};var __defProp2=Object.defineProperty,__getOwnPropDesc=Object.getOwnPropertyDescriptor,__decorateClass=__name((decorators,target,key,kind)=>{for(var result=kind>1?void 0:kind?__getOwnPropDesc(target,key):target,i=decorators.length-1,decorator;i>=0;i--)(decorator=decorators[i])&&(result=(kind?decorator(target,key,result):decorator(result))||result);return kind&&result&&__defProp2(target,key,result),result},"__decorateClass"),__decorateParam=__name((index,decorator)=>(target,key)=>decorator(target,key,index),"__decorateParam"),_a3;exports.UniverThreadCommentPlugin=(_a3=class extends core.Plugin{constructor(_config=defaultPluginConfig,_injector,_commandService,_configService){super(),this._config=_config,this._injector=_injector,this._commandService=_commandService,this._configService=_configService;const{...rest}=this._config;this._configService.setConfig(PLUGIN_CONFIG_KEY,rest)}onStarting(){var _a4;core.mergeOverrideWithDependencies([[IThreadCommentDataSourceService,{useClass:ThreadCommentDataSourceService}],[exports.ThreadCommentModel],[exports.ThreadCommentResourceController]],(_a4=this._config)==null?void 0:_a4.overrides).forEach(d=>{this._injector.add(d)}),[AddCommentCommand,UpdateCommentCommand,DeleteCommentCommand,ResolveCommentCommand,DeleteCommentTreeCommand,AddCommentMutation,UpdateCommentMutation,UpdateCommentRefMutation,DeleteCommentMutation,ResolveCommentMutation].forEach(command=>{this._commandService.registerCommand(command)}),this._injector.get(exports.ThreadCommentResourceController)}},__name(_a3,"UniverThreadCommentPlugin"),__publicField(_a3,"pluginName",TC_PLUGIN_NAME),__publicField(_a3,"type",core.UniverInstanceType.UNIVER_UNKNOWN),_a3);exports.UniverThreadCommentPlugin=__decorateClass([__decorateParam(1,core.Inject(core.Injector)),__decorateParam(2,core.ICommandService),__decorateParam(3,core.IConfigService)],exports.UniverThreadCommentPlugin);exports.AddCommentCommand=AddCommentCommand;exports.AddCommentMutation=AddCommentMutation;exports.DeleteCommentCommand=DeleteCommentCommand;exports.DeleteCommentMutation=DeleteCommentMutation;exports.DeleteCommentTreeCommand=DeleteCommentTreeCommand;exports.IThreadCommentDataSourceService=IThreadCommentDataSourceService;exports.ResolveCommentCommand=ResolveCommentCommand;exports.ResolveCommentMutation=ResolveCommentMutation;exports.TC_PLUGIN_NAME=TC_PLUGIN_NAME;exports.ThreadCommentDataSourceService=ThreadCommentDataSourceService;exports.UpdateCommentCommand=UpdateCommentCommand;exports.UpdateCommentMutation=UpdateCommentMutation;exports.UpdateCommentRefMutation=UpdateCommentRefMutation;
1
+ "use strict";var Me=Object.defineProperty;var _e=(a,s,e)=>s in a?Me(a,s,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[s]=e;var N=(a,s,e)=>_e(a,typeof s!="symbol"?s+"":s,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const M=require("@univerjs/core"),ve=require("rxjs");class ae extends M.Disposable{constructor(){super();N(this,"_dataSource",null);N(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,r){return this._dataSource?this._dataSource.deleteComment(e,t,n,r):!0}async listThreadComments(e,t,n){return this.dataSource?this.dataSource.listComments(e,t,n):!1}saveToSnapshot(e,t){if(this._dataSource){const n={};return Object.keys(e).forEach(r=>{const o=e[r];n[r]=o.map(this.dataSource.saveCommentToSnapshot)}),n}return e}}const I=M.createIdentifier("univer.thread-comment.data-source-service");var ge=Object.defineProperty,ye=Object.getOwnPropertyDescriptor,Se=(a,s,e,t)=>{for(var n=t>1?void 0:t?ye(s,e):s,r=a.length-1,o;r>=0;r--)(o=a[r])&&(n=(t?o(s,e,n):o(n))||n);return t&&n&&ge(s,e,n),n},oe=(a,s)=>(e,t)=>s(e,t,a);exports.ThreadCommentModel=class extends M.Disposable{constructor(e,t){super();N(this,"_commentsMap",new Map);N(this,"_threadMap",new Map);N(this,"_commentUpdate$",new ve.Subject);N(this,"commentUpdate$",this._commentUpdate$.asObservable());N(this,"_tasks",[]);this._dataSourceService=e,this._lifecycleService=t,this.disposeWithMe(()=>{this._commentUpdate$.complete()}),this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe(n=>{const r=new Map;n===M.LifecycleStages.Rendered&&(this._tasks.forEach(({unitId:o,subUnitId:d,threadIds:l})=>{let f=r.get(o);f||(f=new Map,r.set(o,f));let _=f.get(d);_||(_=new Set,f.set(d,_));for(const y of l)_.add(y)}),this._tasks=[],r.forEach((o,d)=>{o.forEach((l,f)=>{this.syncThreadComments(d,f,Array.from(l))})}))}))}_ensureCommentMap(e,t){let n=this._commentsMap.get(e);n||(n=new Map,this._commentsMap.set(e,n));let r=n.get(t);return r||(r=new Map,n.set(t,r)),r}ensureMap(e,t){return this._ensureCommentMap(e,t)}_ensureThreadMap(e,t){let n=this._threadMap.get(e);n||(n=new Map,this._threadMap.set(e,n));let r=n.get(t);return r||(r=new Map,n.set(t,r)),r}_replaceComment(e,t,n){const r=this._ensureCommentMap(e,t),o=r.get(n.id);if(o){const{children:d,...l}=n,f={...l,ref:o.ref};r.set(n.id,f),d==null||d.forEach(_=>{r.set(_.id,{..._,ref:""})}),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"syncUpdate",payload:f}),!!n.resolved!=!!o.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<M.LifecycleStages.Rendered){this._tasks.push({unitId:e,subUnitId:t,threadIds:n});return}const r=this._ensureThreadMap(e,t),o=this._ensureCommentMap(e,t),d=await this._dataSourceService.listThreadComments(e,t,n);if(!d)return;const l=new Set(n);d.forEach(f=>{this._replaceComment(e,t,f),l.delete(f.threadId)}),l.forEach(f=>{r.delete(f),o.forEach((_,y)=>{_.threadId===f&&o.delete(y)})})}addComment(e,t,n,r){const o=this._ensureCommentMap(e,t),{parentId:d,children:l=[],...f}=n,_={...f,parentId:d===n.id?void 0:d},y=T=>{o.set(T.id,T),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"add",payload:T,isRoot:!T.parentId})};y(_);const A=this._ensureThreadMap(e,t);if(!_.parentId){A.set(_.threadId,_);for(const T of l)y(T)}return r&&this.syncThreadComments(e,t,[_.threadId]),!0}updateComment(e,t,n,r){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:r})),!0}updateCommentRef(e,t,n,r){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:r,threadId:d.threadId}),!0):!1}resolveComment(e,t,n,r){const d=this._ensureCommentMap(e,t).get(n);return d?(d.resolved=r,this._commentUpdate$.next({unitId:e,subUnitId:t,type:"resolve",payload:{commentId:n,resolved:r}}),!0):!1}getComment(e,t,n){return this._ensureCommentMap(e,t).get(n)}getRootComment(e,t,n){return this._ensureThreadMap(e,t).get(n)}getThread(e,t,n){const r=this._ensureCommentMap(e,t),o=Array.from(r.values()).filter(_=>_.threadId===n);let d;const l=[],f=new Set;for(const _ of o)_.parentId?l.push(_):d=_,f.add(_.personId);if(d)return{root:d,children:l,relativeUsers:f,unitId:e,subUnitId:t,threadId:n}}getCommentWithChildren(e,t,n){const r=this.getComment(e,t,n);if(r)return this.getThread(e,t,r.threadId)}_deleteComment(e,t,n){const r=this._ensureCommentMap(e,t),o=r.get(n);o&&(r.delete(n),this._commentUpdate$.next({unitId:e,subUnitId:t,type:"delete",payload:{commentId:n,isRoot:!o.parentId,comment:o}}))}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 o=this._ensureCommentMap(e,t).get(n);return o&&(o.parentId?this._deleteComment(e,t,n):this.deleteThread(e,t,o.threadId)),!0}deleteUnit(e){const t=this._commentsMap.get(e);t&&t.forEach((n,r)=>{n.forEach(o=>{this.deleteComment(e,r,o.id)})})}getUnit(e){const t=this._threadMap.get(e);if(!t)return[];const n=[];return t.forEach((r,o)=>{r.forEach((d,l)=>{const f=this.getThread(e,o,l);f&&n.push(f)})}),n}getAll(){const e=[];return this._commentsMap.forEach((t,n)=>{e.push({unitId:n,threads:this.getUnit(n)})}),e}};exports.ThreadCommentModel=Se([oe(0,M.Inject(I)),oe(1,M.Inject(M.LifecycleService))],exports.ThreadCommentModel);var q=(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))(q||{});const z="UNIVER_THREAD_COMMENT_PLUGIN";var Te=Object.defineProperty,$e=Object.getOwnPropertyDescriptor,De=(a,s,e,t)=>{for(var n=t>1?void 0:t?$e(s,e):s,r=a.length-1,o;r>=0;r--)(o=a[r])&&(n=(t?o(s,e,n):o(n))||n);return t&&n&&Te(s,e,n),n},B=(a,s)=>(e,t)=>s(e,t,a);exports.ThreadCommentResourceController=class extends M.Disposable{constructor(s,e,t){super(),this._resourceManagerService=s,this._threadCommentModel=e,this._threadCommentDataSourceService=t,this._initSnapshot()}_initSnapshot(){const s=t=>{const n=this._threadCommentModel.getUnit(t),r={};return n?(n.forEach(o=>{var l;const d=(l=r[o.subUnitId])!=null?l:[];d.push({...o.root,children:o.children}),r[o.unitId]=d}),JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(r,t))):""},e=t=>{if(!t)return{};try{return JSON.parse(t)}catch{return{}}};this.disposeWithMe(this._resourceManagerService.registerPluginResource({pluginName:`SHEET_${z}`,businesses:[q.UNIVER_SHEET,q.UNIVER_DOC],toJson:t=>s(t),parseJson:t=>e(t),onUnLoad:t=>{this._threadCommentModel.deleteUnit(t)},onLoad:async(t,n)=>{Object.keys(n).forEach(r=>{const o=n[r];o.forEach(d=>{this._threadCommentModel.addComment(t,r,d)}),this._threadCommentModel.syncThreadComments(t,r,o.map(d=>d.threadId))})}}))}};exports.ThreadCommentResourceController=De([B(0,M.IResourceManagerService),B(1,M.Inject(exports.ThreadCommentModel)),B(2,I)],exports.ThreadCommentResourceController);const Q={id:"thread-comment.mutation.add-comment",type:M.CommandType.MUTATION,handler(a,s,e){if(!s)return!1;const t=a.get(exports.ThreadCommentModel),{unitId:n,subUnitId:r,comment:o,sync:d}=s,l=d||(e==null?void 0:e.fromChangeset)&&!o.parentId;return t.addComment(n,r,o,l)}},X={id:"thread-comment.mutation.update-comment",type:M.CommandType.MUTATION,handler(a,s){if(!s)return!1;const e=a.get(exports.ThreadCommentModel),{unitId:t,subUnitId:n,payload:r,silent:o}=s;return e.updateComment(t,n,r,o)}},se={id:"thread-comment.mutation.update-comment-ref",type:M.CommandType.MUTATION,handler(a,s){if(!s)return!1;const e=a.get(exports.ThreadCommentModel),{unitId:t,subUnitId:n,payload:r,silent:o}=s;return e.updateCommentRef(t,n,r,o)}},ee={id:"thread-comment.mutation.resolve-comment",type:M.CommandType.MUTATION,handler(a,s){if(!s)return!1;const e=a.get(exports.ThreadCommentModel),{unitId:t,subUnitId:n,resolved:r,commentId:o}=s;return e.resolveComment(t,n,o,r)}},k={id:"thread-comment.mutation.delete-comment",type:M.CommandType.MUTATION,handler(a,s){if(!s)return!1;const e=a.get(exports.ThreadCommentModel),{unitId:t,subUnitId:n,commentId:r}=s;return e.deleteComment(t,n,r)}},ie={id:"thread-comment.command.add-comment",type:M.CommandType.COMMAND,async handler(a,s){if(!s)return!1;const e=a.get(M.ICommandService),t=a.get(I),{comment:n}=s,r=await t.addComment(n),o=t.syncUpdateMutationToColla,d=!n.parentId,l={id:Q.id,params:{...s,comment:r}};return d?await e.executeCommand(l.id,l.params):e.executeCommand(l.id,l.params,{onlyLocal:!o})}},me={id:"thread-comment.command.update-comment",type:M.CommandType.COMMAND,async handler(a,s){if(!s)return!1;const{unitId:e,subUnitId:t,payload:n}=s,r=a.get(M.ICommandService),o=a.get(exports.ThreadCommentModel),d=a.get(I),l=d.syncUpdateMutationToColla,f=o.getComment(e,t,n.commentId);if(!f)return!1;const{children:_,...y}=f;if(!await d.updateComment({...y,...n}))return!1;const T={id:X.id,params:s};return r.executeCommand(T.id,T.params,{onlyLocal:!l}),!0}},ce={id:"thread-comment.command.resolve-comment",type:M.CommandType.COMMAND,async handler(a,s){if(!s)return!1;const{unitId:e,subUnitId:t,resolved:n,commentId:r}=s,o=a.get(I),l=a.get(exports.ThreadCommentModel).getComment(e,t,r),f=o.syncUpdateMutationToColla;return!l||!await o.resolveComment({...l,resolved:n})?!1:a.get(M.ICommandService).executeCommand(ee.id,s,{onlyLocal:!f})}},de={id:"thread-comment.command.delete-comment",type:M.CommandType.COMMAND,async handler(a,s){if(!s)return!1;const e=a.get(exports.ThreadCommentModel),t=a.get(I),n=a.get(M.ICommandService),{unitId:r,subUnitId:o,commentId:d}=s,l=t.syncUpdateMutationToColla,f=e.getComment(r,o,d);if(!f||!await t.deleteComment(r,o,f.threadId,d))return!1;const _={id:k.id,params:s};return n.executeCommand(_.id,_.params,{onlyLocal:!l})}},ue={id:"thread-comment.command.delete-comment-tree",type:M.CommandType.COMMAND,async handler(a,s){if(!s)return!1;const e=a.get(exports.ThreadCommentModel),t=a.get(M.ICommandService),n=a.get(I),{unitId:r,subUnitId:o,commentId:d}=s,l=e.getCommentWithChildren(r,o,d);return!l||!await n.deleteComment(r,o,l.root.threadId,d)?!1:await t.executeCommand(k.id,{unitId:r,subUnitId:o,commentId:l.root.id})}},Oe="thread-comment.config",Ue={};var we=Object.defineProperty,Ne=Object.getOwnPropertyDescriptor,Ie=(a,s,e,t)=>{for(var n=t>1?void 0:t?Ne(s,e):s,r=a.length-1,o;r>=0;r--)(o=a[r])&&(n=(t?o(s,e,n):o(n))||n);return t&&n&&we(s,e,n),n},K=(a,s)=>(e,t)=>s(e,t,a),G;exports.UniverThreadCommentPlugin=(G=class extends M.Plugin{constructor(s=Ue,e,t,n){super(),this._config=s,this._injector=e,this._commandService=t,this._configService=n;const{...r}=this._config;this._configService.setConfig(Oe,r)}onStarting(){var s;M.mergeOverrideWithDependencies([[I,{useClass:ae}],[exports.ThreadCommentModel],[exports.ThreadCommentResourceController]],(s=this._config)==null?void 0:s.overrides).forEach(e=>{this._injector.add(e)}),[ie,me,de,ce,ue,Q,X,se,k,ee].forEach(e=>{this._commandService.registerCommand(e)}),this._injector.get(exports.ThreadCommentResourceController)}},N(G,"pluginName",z),N(G,"type",M.UniverInstanceType.UNIVER_UNKNOWN),G);exports.UniverThreadCommentPlugin=Ie([K(1,M.Inject(M.Injector)),K(2,M.ICommandService),K(3,M.IConfigService)],exports.UniverThreadCommentPlugin);var Ee=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{};function Re(a){return a&&a.__esModule&&Object.prototype.hasOwnProperty.call(a,"default")?a.default:a}var he={exports:{}};(function(a,s){(function(e,t){a.exports=t()})(Ee,function(){var e=1e3,t=6e4,n=36e5,r="millisecond",o="second",d="minute",l="hour",f="day",_="week",y="month",A="quarter",T="year",x="date",te="Invalid Date",le=/^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/,fe=/\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,pe={name:"en",weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_"),ordinal:function(h){var c=["th","st","nd","rd"],i=h%100;return"["+h+(c[(i-20)%10]||c[i]||c[0])+"]"}},F=function(h,c,i){var u=String(h);return!u||u.length>=c?h:""+Array(c+1-u.length).join(i)+h},Ce={s:F,z:function(h){var c=-h.utcOffset(),i=Math.abs(c),u=Math.floor(i/60),m=i%60;return(c<=0?"+":"-")+F(u,2,"0")+":"+F(m,2,"0")},m:function h(c,i){if(c.date()<i.date())return-h(i,c);var u=12*(i.year()-c.year())+(i.month()-c.month()),m=c.clone().add(u,y),p=i-m<0,C=c.clone().add(u+(p?-1:1),y);return+(-(u+(i-m)/(p?m-C:C-m))||0)},a:function(h){return h<0?Math.ceil(h)||0:Math.floor(h)},p:function(h){return{M:y,y:T,w:_,d:f,D:x,h:l,m:d,s:o,ms:r,Q:A}[h]||String(h||"").toLowerCase().replace(/s$/,"")},u:function(h){return h===void 0}},L="en",R={};R[L]=pe;var ne="$isDayjsObject",Z=function(h){return h instanceof V||!(!h||!h[ne])},Y=function h(c,i,u){var m;if(!c)return L;if(typeof c=="string"){var p=c.toLowerCase();R[p]&&(m=p),i&&(R[p]=i,m=p);var C=c.split("-");if(!m&&C.length>1)return h(C[0])}else{var g=c.name;R[g]=c,m=g}return!u&&m&&(L=m),m||!u&&L},$=function(h,c){if(Z(h))return h.clone();var i=typeof c=="object"?c:{};return i.date=h,i.args=arguments,new V(i)},v=Ce;v.l=Y,v.i=Z,v.w=function(h,c){return $(h,{locale:c.$L,utc:c.$u,x:c.$x,$offset:c.$offset})};var V=function(){function h(i){this.$L=Y(i.locale,null,!0),this.parse(i),this.$x=this.$x||i.x||{},this[ne]=!0}var c=h.prototype;return c.parse=function(i){this.$d=function(u){var m=u.date,p=u.utc;if(m===null)return new Date(NaN);if(v.u(m))return new Date;if(m instanceof Date)return new Date(m);if(typeof m=="string"&&!/Z$/i.test(m)){var C=m.match(le);if(C){var g=C[2]-1||0,S=(C[7]||"0").substring(0,3);return p?new Date(Date.UTC(C[1],g,C[3]||1,C[4]||0,C[5]||0,C[6]||0,S)):new Date(C[1],g,C[3]||1,C[4]||0,C[5]||0,C[6]||0,S)}}return new Date(m)}(i),this.init()},c.init=function(){var i=this.$d;this.$y=i.getFullYear(),this.$M=i.getMonth(),this.$D=i.getDate(),this.$W=i.getDay(),this.$H=i.getHours(),this.$m=i.getMinutes(),this.$s=i.getSeconds(),this.$ms=i.getMilliseconds()},c.$utils=function(){return v},c.isValid=function(){return this.$d.toString()!==te},c.isSame=function(i,u){var m=$(i);return this.startOf(u)<=m&&m<=this.endOf(u)},c.isAfter=function(i,u){return $(i)<this.startOf(u)},c.isBefore=function(i,u){return this.endOf(u)<$(i)},c.$g=function(i,u,m){return v.u(i)?this[u]:this.set(m,i)},c.unix=function(){return Math.floor(this.valueOf()/1e3)},c.valueOf=function(){return this.$d.getTime()},c.startOf=function(i,u){var m=this,p=!!v.u(u)||u,C=v.p(i),g=function(P,U){var E=v.w(m.$u?Date.UTC(m.$y,U,P):new Date(m.$y,U,P),m);return p?E:E.endOf(f)},S=function(P,U){return v.w(m.toDate()[P].apply(m.toDate("s"),(p?[0,0,0,0]:[23,59,59,999]).slice(U)),m)},D=this.$W,O=this.$M,w=this.$D,j="set"+(this.$u?"UTC":"");switch(C){case T:return p?g(1,0):g(31,11);case y:return p?g(1,O):g(0,O+1);case _:var b=this.$locale().weekStart||0,H=(D<b?D+7:D)-b;return g(p?w-H:w+(6-H),O);case f:case x:return S(j+"Hours",0);case l:return S(j+"Minutes",1);case d:return S(j+"Seconds",2);case o:return S(j+"Milliseconds",3);default:return this.clone()}},c.endOf=function(i){return this.startOf(i,!1)},c.$set=function(i,u){var m,p=v.p(i),C="set"+(this.$u?"UTC":""),g=(m={},m[f]=C+"Date",m[x]=C+"Date",m[y]=C+"Month",m[T]=C+"FullYear",m[l]=C+"Hours",m[d]=C+"Minutes",m[o]=C+"Seconds",m[r]=C+"Milliseconds",m)[p],S=p===f?this.$D+(u-this.$W):u;if(p===y||p===T){var D=this.clone().set(x,1);D.$d[g](S),D.init(),this.$d=D.set(x,Math.min(this.$D,D.daysInMonth())).$d}else g&&this.$d[g](S);return this.init(),this},c.set=function(i,u){return this.clone().$set(i,u)},c.get=function(i){return this[v.p(i)]()},c.add=function(i,u){var m,p=this;i=Number(i);var C=v.p(u),g=function(O){var w=$(p);return v.w(w.date(w.date()+Math.round(O*i)),p)};if(C===y)return this.set(y,this.$M+i);if(C===T)return this.set(T,this.$y+i);if(C===f)return g(1);if(C===_)return g(7);var S=(m={},m[d]=t,m[l]=n,m[o]=e,m)[C]||1,D=this.$d.getTime()+i*S;return v.w(D,this)},c.subtract=function(i,u){return this.add(-1*i,u)},c.format=function(i){var u=this,m=this.$locale();if(!this.isValid())return m.invalidDate||te;var p=i||"YYYY-MM-DDTHH:mm:ssZ",C=v.z(this),g=this.$H,S=this.$m,D=this.$M,O=m.weekdays,w=m.months,j=m.meridiem,b=function(U,E,W,J){return U&&(U[E]||U(u,p))||W[E].slice(0,J)},H=function(U){return v.s(g%12||12,U,"0")},P=j||function(U,E,W){var J=U<12?"AM":"PM";return W?J.toLowerCase():J};return p.replace(fe,function(U,E){return E||function(W){switch(W){case"YY":return String(u.$y).slice(-2);case"YYYY":return v.s(u.$y,4,"0");case"M":return D+1;case"MM":return v.s(D+1,2,"0");case"MMM":return b(m.monthsShort,D,w,3);case"MMMM":return b(w,D);case"D":return u.$D;case"DD":return v.s(u.$D,2,"0");case"d":return String(u.$W);case"dd":return b(m.weekdaysMin,u.$W,O,2);case"ddd":return b(m.weekdaysShort,u.$W,O,3);case"dddd":return O[u.$W];case"H":return String(g);case"HH":return v.s(g,2,"0");case"h":return H(1);case"hh":return H(2);case"a":return P(g,S,!0);case"A":return P(g,S,!1);case"m":return String(S);case"mm":return v.s(S,2,"0");case"s":return String(u.$s);case"ss":return v.s(u.$s,2,"0");case"SSS":return v.s(u.$ms,3,"0");case"Z":return C}return null}(U)||C.replace(":","")})},c.utcOffset=function(){return 15*-Math.round(this.$d.getTimezoneOffset()/15)},c.diff=function(i,u,m){var p,C=this,g=v.p(u),S=$(i),D=(S.utcOffset()-this.utcOffset())*t,O=this-S,w=function(){return v.m(C,S)};switch(g){case T:p=w()/12;break;case y:p=w();break;case A:p=w()/3;break;case _:p=(O-D)/6048e5;break;case f:p=(O-D)/864e5;break;case l:p=O/n;break;case d:p=O/t;break;case o:p=O/e;break;default:p=O}return m?p:v.a(p)},c.daysInMonth=function(){return this.endOf(y).$D},c.$locale=function(){return R[this.$L]},c.locale=function(i,u){if(!i)return this.$L;var m=this.clone(),p=Y(i,u,!0);return p&&(m.$L=p),m},c.clone=function(){return v.w(this.$d,this)},c.toDate=function(){return new Date(this.valueOf())},c.toJSON=function(){return this.isValid()?this.toISOString():null},c.toISOString=function(){return this.$d.toISOString()},c.toString=function(){return this.$d.toUTCString()},h}(),re=V.prototype;return $.prototype=re,[["$ms",r],["$s",o],["$m",d],["$H",l],["$W",f],["$M",y],["$y",T],["$D",x]].forEach(function(h){re[h[1]]=function(c){return this.$g(c,h[0],h[1])}}),$.extend=function(h,c){return h.$i||(h(c,V,$),h.$i=!0),$},$.locale=Y,$.isDayjs=Z,$.unix=function(h){return $(1e3*h)},$.en=R[L],$.Ls=R,$.p={},$})})(he);var be=he.exports;const Pe=Re(be);function xe(){return Pe().format("YYYY/MM/DD HH:mm")}exports.AddCommentCommand=ie;exports.AddCommentMutation=Q;exports.DeleteCommentCommand=de;exports.DeleteCommentMutation=k;exports.DeleteCommentTreeCommand=ue;exports.IThreadCommentDataSourceService=I;exports.ResolveCommentCommand=ce;exports.ResolveCommentMutation=ee;exports.TC_PLUGIN_NAME=z;exports.ThreadCommentDataSourceService=ae;exports.UpdateCommentCommand=me;exports.UpdateCommentMutation=X;exports.UpdateCommentRefMutation=se;exports.getDT=xe;