New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@univerjs/thread-comment

Package Overview
Dependencies
Maintainers
0
Versions
247
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@univerjs/thread-comment - npm Package Compare versions

Comparing version 0.4.1 to 0.4.2-nightly.202410291606

2

lib/cjs/index.js

@@ -1,1 +0,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){return this._dataSource?this._dataSource.addComment(comment):comment}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):[]}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,_commandService){super();__publicField(this,"_commentsMap",{});__publicField(this,"_commentsTreeMap",new Map);__publicField(this,"_threadMap",new Map);__publicField(this,"_commentUpdate$",new rxjs.Subject);__publicField(this,"_commentsMap$",new rxjs.BehaviorSubject({}));__publicField(this,"commentUpdate$",this._commentUpdate$.asObservable());__publicField(this,"commentMap$",this._commentsMap$.asObservable());this._dataSourceService=_dataSourceService,this._commandService=_commandService,this.disposeWithMe(()=>{this._commentUpdate$.complete(),this._commentsMap$.complete()})}_ensureCommentMap(unitId,subUnitId){let unitMap=this._commentsMap[unitId];unitMap||(unitMap={},this._commentsMap[unitId]=unitMap);let subUnitMap=unitMap[subUnitId];return subUnitMap||(subUnitMap={},unitMap[subUnitId]=subUnitMap),subUnitMap}_ensureCommentChildrenMap(unitId,subUnitId){let unitMap=this._commentsTreeMap.get(unitId);unitMap||(unitMap=new Map,this._commentsTreeMap.set(unitId,unitMap));let subUnitMap=unitMap.get(subUnitId);return subUnitMap||(subUnitMap=new Map,unitMap.set(subUnitId,subUnitMap)),subUnitMap}_ensureThreadMap(unitId){let unitMap=this._threadMap.get(unitId);return unitMap||(unitMap=new Map,this._threadMap.set(unitId,unitMap)),unitMap}_refreshCommentsMap$(){this._commentsMap$.next({...this._commentsMap})}ensureMap(unitId,subUnitId){const commentMap=this._ensureCommentMap(unitId,subUnitId),commentChildrenMap=this._ensureCommentChildrenMap(unitId,subUnitId);return{commentMap,commentChildrenMap}}_replaceComment(unitId,subUnitId,comment){var _a4;const{commentMap,commentChildrenMap}=this.ensureMap(unitId,subUnitId),currentComment=commentMap[comment.id];if(currentComment){const newComment={...comment,ref:currentComment.ref};commentMap[comment.id]=newComment,(_a4=comment.children)==null||_a4.forEach(child=>{commentMap[child.id]={...child,ref:""}}),commentChildrenMap.set(comment.id,newComment),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){const comments=await this._dataSourceService.listThreadComments(unitId,subUnitId,threadIds);if(!comments.length)return;const deleteThreads=new Set(threadIds);comments.forEach(comment=>{this._replaceComment(unitId,subUnitId,comment),deleteThreads.delete(comment.threadId)}),deleteThreads.forEach(id=>{const thread=this.getThread(unitId,id);thread&&this.deleteComment(thread.unitId,thread.subUnitId,thread.id)}),this._refreshCommentsMap$()}addComment(unitId,subUnitId,origin,shouldSync){var _a4,_b;const{commentMap,commentChildrenMap}=this.ensureMap(unitId,subUnitId),comment=origin,addCommentItem=__name(item=>{commentMap[item.id]=item,this._commentUpdate$.next({unitId,subUnitId,type:"add",payload:item,isRoot:!item.parentId})},"addCommentItem"),parentId=comment.parentId;if(parentId){const parent=commentMap[parentId];parent.children=[...(_a4=parent.children)!=null?_a4:[],comment],addCommentItem(comment)}else commentChildrenMap.set(comment.id,comment),this._ensureThreadMap(unitId).set(comment.threadId,comment),addCommentItem(comment),(_b=comment.children)==null||_b.forEach(child=>addCommentItem({...child,ref:""}));return this._refreshCommentsMap$(),shouldSync&&this.syncThreadComments(unitId,subUnitId,[comment.threadId]),!0}updateComment(unitId,subUnitId,payload,silent){const{commentMap}=this.ensureMap(unitId,subUnitId),oldComment=commentMap[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}),this._refreshCommentsMap$(),!0):!1}updateCommentRef(unitId,subUnitId,payload,silent){const{commentMap}=this.ensureMap(unitId,subUnitId),oldComment=commentMap[payload.commentId];return oldComment?(oldComment.ref=payload.ref,this._commentUpdate$.next({unitId,subUnitId,type:"updateRef",payload,silent}),this._refreshCommentsMap$(),!0):!1}resolveComment(unitId,subUnitId,commentId,resolved){const{commentMap}=this.ensureMap(unitId,subUnitId),oldComment=commentMap[commentId];return oldComment?(oldComment.resolved=resolved,this._commentUpdate$.next({unitId,subUnitId,type:"resolve",payload:{commentId,resolved}}),this._refreshCommentsMap$(),!0):!1}getComment(unitId,subUnitId,commentId){const{commentMap}=this.ensureMap(unitId,subUnitId);return commentMap[commentId]}getComment$(unitId,subUnitId,commentId){return this._commentsMap$.pipe(rxjs.map(records=>records[unitId][subUnitId][commentId]))}getCommentWithChildren(unitId,subUnitId,commentId){var _a4,_b;const{commentMap,commentChildrenMap}=this.ensureMap(unitId,subUnitId),current=commentMap[commentId];if(!current)return;const relativeUsers=new Set,root=commentChildrenMap.get(commentId);if(root)return[root,...(_a4=root.children)!=null?_a4:[]].forEach(comment=>{var _a5;relativeUsers.add(comment.personId),(_a5=comment.text.customRanges)==null||_a5.forEach(range=>{range.rangeType===core.CustomRangeType.MENTION&&relativeUsers.add(range.rangeId)})}),{root:current,children:(_b=root.children)!=null?_b:[],relativeUsers}}deleteComment(unitId,subUnitId,commentId){var _a4;const{commentMap,commentChildrenMap}=this.ensureMap(unitId,subUnitId),current=commentMap[commentId];if(!current)return!0;if(current.parentId){const root=commentChildrenMap.get(current.parentId);if(root&&root.children){const index=root.children.findIndex(comment=>comment.id===commentId);root.children.splice(index,1)}delete commentMap[commentId]}else{delete commentMap[commentId];const comment=commentChildrenMap.get(commentId);commentChildrenMap.delete(commentId),this._ensureThreadMap(unitId).delete(current.threadId),(_a4=comment==null?void 0:comment.children)==null||_a4.forEach(child=>{delete commentMap[child.id],this._commentUpdate$.next({unitId,subUnitId,type:"delete",payload:{commentId:child.id,isRoot:!1,comment:child}})})}return this._commentUpdate$.next({unitId,subUnitId,type:"delete",payload:{commentId,isRoot:!current.parentId,comment:current}}),this._refreshCommentsMap$(),!0}getUnit(unitId){const unitMap=this._commentsMap[unitId];return unitMap?Array.from(Object.entries(unitMap)).map(([subUnitId,subUnitMap])=>[subUnitId,Array.from(Object.values(subUnitMap))]):[]}deleteUnit(unitId){const unitMap=this._commentsMap[unitId];unitMap&&Object.entries(unitMap).forEach(([subUnitId,subUnitMap])=>{Object.values(subUnitMap).forEach(comment=>{this.deleteComment(unitId,subUnitId,comment.id)})})}getRootCommentIds(unitId,subUnitId){const commentChildrenMap=this._ensureCommentChildrenMap(unitId,subUnitId);return Array.from(commentChildrenMap.keys())}getAll(){return this._commentsMap}getThread(unitId,threadId){return this._ensureThreadMap(unitId).get(threadId)}},__name(_a,"ThreadCommentModel"),_a);exports.ThreadCommentModel=__decorateClass$2([__decorateParam$2(0,core.Inject(IThreadCommentDataSourceService)),__decorateParam$2(1,core.ICommandService)],exports.ThreadCommentModel);var R=(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))(R||{});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(([key,v])=>{resultMap[key]=v}),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:[R.UNIVER_SHEET,R.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;
"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;

@@ -5,4 +5,4 @@ var __defProp = Object.defineProperty;

var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key != "symbol" ? key + "" : key, value);
import { createIdentifier, Disposable, Inject, CustomRangeType, ICommandService, IResourceManagerService, CommandType, Plugin, UniverInstanceType, mergeOverrideWithDependencies, Injector, IConfigService } from "@univerjs/core";
import { Subject, BehaviorSubject, map } from "rxjs";
import { createIdentifier, Disposable, Inject, LifecycleService, LifecycleStages, IResourceManagerService, CommandType, ICommandService, Plugin, UniverInstanceType, mergeOverrideWithDependencies, Injector, IConfigService } from "@univerjs/core";
import { Subject } from "rxjs";
const _ThreadCommentDataSourceService = class _ThreadCommentDataSourceService extends Disposable {

@@ -24,3 +24,4 @@ constructor() {

async addComment(comment) {
return this._dataSource ? this._dataSource.addComment(comment) : comment;
var _a4;
return this._dataSource ? this._dataSource.addComment(comment) : { ...comment, threadId: (_a4 = comment.threadId) != null ? _a4 : comment.id };
}

@@ -37,11 +38,11 @@ async updateComment(comment) {

async listThreadComments(unitId, subUnitId, threadIds) {
return this.dataSource ? this.dataSource.listComments(unitId, subUnitId, threadIds) : [];
return this.dataSource ? this.dataSource.listComments(unitId, subUnitId, threadIds) : !1;
}
saveToSnapshot(unitComments, unitId) {
if (this._dataSource) {
const map2 = {};
const map = {};
return Object.keys(unitComments).forEach((subUnitId) => {
const comments = unitComments[subUnitId];
map2[subUnitId] = comments.map(this.dataSource.saveCommentToSnapshot);
}), map2;
map[subUnitId] = comments.map(this.dataSource.saveCommentToSnapshot);
}), map;
}

@@ -60,57 +61,55 @@ return unitComments;

let ThreadCommentModel = (_a = class extends Disposable {
constructor(_dataSourceService, _commandService) {
constructor(_dataSourceService, _lifecycleService) {
super();
__publicField(this, "_commentsMap", {});
__publicField(this, "_commentsTreeMap", /* @__PURE__ */ new Map());
__publicField(this, "_commentsMap", /* @__PURE__ */ new Map());
__publicField(this, "_threadMap", /* @__PURE__ */ new Map());
__publicField(this, "_commentUpdate$", new Subject());
__publicField(this, "_commentsMap$", new BehaviorSubject({}));
__publicField(this, "commentUpdate$", this._commentUpdate$.asObservable());
__publicField(this, "commentMap$", this._commentsMap$.asObservable());
this._dataSourceService = _dataSourceService, this._commandService = _commandService, this.disposeWithMe(() => {
this._commentUpdate$.complete(), this._commentsMap$.complete();
});
__publicField(this, "_tasks", []);
this._dataSourceService = _dataSourceService, this._lifecycleService = _lifecycleService, this.disposeWithMe(() => {
this._commentUpdate$.complete();
}), this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe((stage) => {
const taskMap = /* @__PURE__ */ new Map();
stage === LifecycleStages.Rendered && (this._tasks.forEach(({ unitId, subUnitId, threadIds }) => {
let unitMap = taskMap.get(unitId);
unitMap || (unitMap = /* @__PURE__ */ new Map(), taskMap.set(unitId, unitMap));
let subUnitMap = unitMap.get(subUnitId);
subUnitMap || (subUnitMap = /* @__PURE__ */ 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[unitId];
unitMap || (unitMap = {}, this._commentsMap[unitId] = unitMap);
let subUnitMap = unitMap[subUnitId];
return subUnitMap || (subUnitMap = {}, unitMap[subUnitId] = subUnitMap), subUnitMap;
}
_ensureCommentChildrenMap(unitId, subUnitId) {
let unitMap = this._commentsTreeMap.get(unitId);
unitMap || (unitMap = /* @__PURE__ */ new Map(), this._commentsTreeMap.set(unitId, unitMap));
let unitMap = this._commentsMap.get(unitId);
unitMap || (unitMap = /* @__PURE__ */ new Map(), this._commentsMap.set(unitId, unitMap));
let subUnitMap = unitMap.get(subUnitId);
return subUnitMap || (subUnitMap = /* @__PURE__ */ new Map(), unitMap.set(subUnitId, subUnitMap)), subUnitMap;
}
_ensureThreadMap(unitId) {
ensureMap(unitId, subUnitId) {
return this._ensureCommentMap(unitId, subUnitId);
}
_ensureThreadMap(unitId, subUnitId) {
let unitMap = this._threadMap.get(unitId);
return unitMap || (unitMap = /* @__PURE__ */ new Map(), this._threadMap.set(unitId, unitMap)), unitMap;
unitMap || (unitMap = /* @__PURE__ */ new Map(), this._threadMap.set(unitId, unitMap));
let subUnitMap = unitMap.get(subUnitId);
return subUnitMap || (subUnitMap = /* @__PURE__ */ new Map(), unitMap.set(subUnitId, subUnitMap)), subUnitMap;
}
_refreshCommentsMap$() {
this._commentsMap$.next({
...this._commentsMap
});
}
ensureMap(unitId, subUnitId) {
const commentMap = this._ensureCommentMap(unitId, subUnitId), commentChildrenMap = this._ensureCommentChildrenMap(unitId, subUnitId);
return {
commentMap,
commentChildrenMap
};
}
_replaceComment(unitId, subUnitId, comment) {
var _a4;
const { commentMap, commentChildrenMap } = this.ensureMap(unitId, subUnitId), currentComment = commentMap[comment.id];
const commentMap = this._ensureCommentMap(unitId, subUnitId), currentComment = commentMap.get(comment.id);
if (currentComment) {
const newComment = {
...comment,
const { children, ...rest } = comment, newComment = {
...rest,
ref: currentComment.ref
};
commentMap[comment.id] = newComment, (_a4 = comment.children) == null || _a4.forEach((child) => {
commentMap[child.id] = {
commentMap.set(comment.id, newComment), children == null || children.forEach((child) => {
commentMap.set(child.id, {
...child,
ref: ""
};
}), commentChildrenMap.set(comment.id, newComment), this._commentUpdate$.next({
});
}), this._commentUpdate$.next({
unitId,

@@ -132,5 +131,9 @@ subUnitId,

async syncThreadComments(unitId, subUnitId, threadIds) {
const comments = await this._dataSourceService.listThreadComments(unitId, subUnitId, threadIds);
if (!comments.length)
if (this._lifecycleService.stage < 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);

@@ -140,10 +143,13 @@ comments.forEach((comment) => {

}), deleteThreads.forEach((id) => {
const thread = this.getThread(unitId, id);
thread && this.deleteComment(thread.unitId, thread.subUnitId, thread.id);
}), this._refreshCommentsMap$();
threadMap.delete(id), commentMap.forEach((comment, commentId) => {
comment.threadId === id && commentMap.delete(commentId);
});
});
}
addComment(unitId, subUnitId, origin, shouldSync) {
var _a4, _b;
const { commentMap, commentChildrenMap } = this.ensureMap(unitId, subUnitId), comment = origin, addCommentItem = /* @__PURE__ */ __name((item) => {
commentMap[item.id] = item, this._commentUpdate$.next({
const commentMap = this._ensureCommentMap(unitId, subUnitId), { parentId, children = [], ...rest } = origin, comment = {
...rest,
parentId: parentId === origin.id ? void 0 : parentId
}, addCommentItem = /* @__PURE__ */ __name((item) => {
commentMap.set(item.id, item), this._commentUpdate$.next({
unitId,

@@ -155,19 +161,15 @@ subUnitId,

});
}, "addCommentItem"), parentId = comment.parentId;
if (parentId) {
const parent = commentMap[parentId];
parent.children = [
...(_a4 = parent.children) != null ? _a4 : [],
comment
], addCommentItem(comment);
} else
commentChildrenMap.set(comment.id, comment), this._ensureThreadMap(unitId).set(comment.threadId, comment), addCommentItem(comment), (_b = comment.children) == null || _b.forEach((child) => addCommentItem({
...child,
ref: ""
}));
return this._refreshCommentsMap$(), shouldSync && this.syncThreadComments(unitId, subUnitId, [comment.threadId]), !0;
}, "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 { commentMap } = this.ensureMap(unitId, subUnitId), oldComment = commentMap[payload.commentId];
return oldComment ? (oldComment.updated = !0, oldComment.text = payload.text, oldComment.attachments = payload.attachments, oldComment.updateT = payload.updateT, this._commentUpdate$.next({
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,

@@ -178,6 +180,6 @@ subUnitId,

silent
}), this._refreshCommentsMap$(), !0) : !1;
})), !0;
}
updateCommentRef(unitId, subUnitId, payload, silent) {
const { commentMap } = this.ensureMap(unitId, subUnitId), oldComment = commentMap[payload.commentId];
const oldComment = this._ensureCommentMap(unitId, subUnitId).get(payload.commentId);
return oldComment ? (oldComment.ref = payload.ref, this._commentUpdate$.next({

@@ -188,7 +190,8 @@ unitId,

payload,
silent
}), this._refreshCommentsMap$(), !0) : !1;
silent,
threadId: oldComment.threadId
}), !0) : !1;
}
resolveComment(unitId, subUnitId, commentId, resolved) {
const { commentMap } = this.ensureMap(unitId, subUnitId), oldComment = commentMap[commentId];
const oldComment = this._ensureCommentMap(unitId, subUnitId).get(commentId);
return oldComment ? (oldComment.resolved = resolved, this._commentUpdate$.next({

@@ -202,58 +205,34 @@ unitId,

}
}), this._refreshCommentsMap$(), !0) : !1;
}), !0) : !1;
}
getComment(unitId, subUnitId, commentId) {
const { commentMap } = this.ensureMap(unitId, subUnitId);
return commentMap[commentId];
return this._ensureCommentMap(unitId, subUnitId).get(commentId);
}
getComment$(unitId, subUnitId, commentId) {
return this._commentsMap$.pipe(map((records) => records[unitId][subUnitId][commentId]));
getRootComment(unitId, subUnitId, threadId) {
return this._ensureThreadMap(unitId, subUnitId).get(threadId);
}
getCommentWithChildren(unitId, subUnitId, commentId) {
var _a4, _b;
const { commentMap, commentChildrenMap } = this.ensureMap(unitId, subUnitId), current = commentMap[commentId];
if (!current)
return;
const relativeUsers = /* @__PURE__ */ new Set(), root = commentChildrenMap.get(commentId);
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 = /* @__PURE__ */ new Set();
for (const comment of comments)
comment.parentId ? children.push(comment) : root = comment, relativeUsers.add(comment.personId);
if (root)
return [root, ...(_a4 = root.children) != null ? _a4 : []].forEach((comment) => {
var _a5;
relativeUsers.add(comment.personId), (_a5 = comment.text.customRanges) == null || _a5.forEach((range) => {
range.rangeType === CustomRangeType.MENTION && relativeUsers.add(range.rangeId);
});
}), {
root: current,
children: (_b = root.children) != null ? _b : [],
relativeUsers
return {
root,
children,
relativeUsers,
unitId,
subUnitId,
threadId
};
}
deleteComment(unitId, subUnitId, commentId) {
var _a4;
const { commentMap, commentChildrenMap } = this.ensureMap(unitId, subUnitId), current = commentMap[commentId];
if (!current)
return !0;
if (current.parentId) {
const root = commentChildrenMap.get(current.parentId);
if (root && root.children) {
const index = root.children.findIndex((comment) => comment.id === commentId);
root.children.splice(index, 1);
}
delete commentMap[commentId];
} else {
delete commentMap[commentId];
const comment = commentChildrenMap.get(commentId);
commentChildrenMap.delete(commentId), this._ensureThreadMap(unitId).delete(current.threadId), (_a4 = comment == null ? void 0 : comment.children) == null || _a4.forEach((child) => {
delete commentMap[child.id], this._commentUpdate$.next({
unitId,
subUnitId,
type: "delete",
payload: {
commentId: child.id,
isRoot: !1,
comment: child
}
});
});
}
return this._commentUpdate$.next({
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,

@@ -267,12 +246,17 @@ subUnitId,

}
}), this._refreshCommentsMap$(), !0;
}));
}
getUnit(unitId) {
const unitMap = this._commentsMap[unitId];
return unitMap ? Array.from(Object.entries(unitMap)).map(([subUnitId, subUnitMap]) => [subUnitId, Array.from(Object.values(subUnitMap))]) : [];
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[unitId];
unitMap && Object.entries(unitMap).forEach(([subUnitId, subUnitMap]) => {
Object.values(subUnitMap).forEach((comment) => {
const unitMap = this._commentsMap.get(unitId);
unitMap && unitMap.forEach((subUnitMap, subUnitId) => {
subUnitMap.forEach((comment) => {
this.deleteComment(unitId, subUnitId, comment.id);

@@ -282,18 +266,29 @@ });

}
getRootCommentIds(unitId, subUnitId) {
const commentChildrenMap = this._ensureCommentChildrenMap(unitId, subUnitId);
return Array.from(commentChildrenMap.keys());
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() {
return this._commentsMap;
const all = [];
return this._commentsMap.forEach((unitMap, unitId) => {
all.push({
unitId,
threads: this.getUnit(unitId)
});
}), all;
}
getThread(unitId, threadId) {
return this._ensureThreadMap(unitId).get(threadId);
}
}, __name(_a, "ThreadCommentModel"), _a);
ThreadCommentModel = __decorateClass$2([
__decorateParam$2(0, Inject(IThreadCommentDataSourceService)),
__decorateParam$2(1, ICommandService)
__decorateParam$2(1, Inject(LifecycleService))
], ThreadCommentModel);
var R = /* @__PURE__ */ ((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))(R || {});
var _ = /* @__PURE__ */ ((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";

@@ -311,5 +306,10 @@ var __defProp$1 = Object.defineProperty, __getOwnPropDesc$1 = Object.getOwnPropertyDescriptor, __decorateClass$1 = /* @__PURE__ */ __name((decorators, target, key, kind) => {

const toJson = /* @__PURE__ */ __name((unitID) => {
const map2 = this._threadCommentModel.getUnit(unitID), resultMap = {};
return map2 ? (map2.forEach(([key, v]) => {
resultMap[key] = v;
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))) : "";

@@ -328,3 +328,3 @@ }, "toJson"), parseJson = /* @__PURE__ */ __name((json) => {

pluginName: `SHEET_${TC_PLUGIN_NAME}`,
businesses: [R.UNIVER_SHEET, R.UNIVER_DOC],
businesses: [_.UNIVER_SHEET, _.UNIVER_DOC],
toJson: /* @__PURE__ */ __name((unitID) => toJson(unitID), "toJson"),

@@ -331,0 +331,0 @@ parseJson: /* @__PURE__ */ __name((json) => parseJson(json), "parseJson"),

@@ -0,4 +1,4 @@

import { IThreadComment } from '../../types/interfaces/i-thread-comment';
import { ICommand } from '@univerjs/core';
import { IUpdateCommentPayload } from '../mutations/comment.mutation';
import { IThreadComment } from '../../types/interfaces/i-thread-comment';
export interface IAddCommentCommandParams {

@@ -5,0 +5,0 @@ unitId: string;

@@ -1,5 +0,5 @@

import { Disposable, ICommandService } from '@univerjs/core';
import { Nullable, Disposable, LifecycleService } from '@univerjs/core';
import { IUpdateCommentPayload, IUpdateCommentRefPayload } from '../commands/mutations/comment.mutation';
import { IThreadComment } from '../types/interfaces/i-thread-comment';
import { IThreadCommentDataSourceService } from '../services/tc-datasource.service';
import { IUpdateCommentPayload, IUpdateCommentRefPayload } from '../commands/mutations/comment.mutation';
import { IBaseComment, IThreadComment } from '../types/interfaces/i-thread-comment';
export type CommentUpdate = {

@@ -26,2 +26,3 @@ unitId: string;

payload: IUpdateCommentRefPayload;
threadId: string;
} | {

@@ -37,21 +38,22 @@ type: 'resolve';

});
export interface IThreadInfo {
unitId: string;
subUnitId: string;
threadId: string;
root: IThreadComment;
children: IThreadComment[];
relativeUsers: Set<string>;
}
export declare class ThreadCommentModel extends Disposable {
private readonly _dataSourceService;
private readonly _commandService;
private readonly _lifecycleService;
private _commentsMap;
private _commentsTreeMap;
private _threadMap;
private _commentUpdate$;
private _commentsMap$;
commentUpdate$: import('rxjs').Observable<CommentUpdate>;
commentMap$: import('rxjs').Observable<Record<string, Record<string, Record<string, IThreadComment>>>>;
constructor(_dataSourceService: IThreadCommentDataSourceService, _commandService: ICommandService);
private _tasks;
constructor(_dataSourceService: IThreadCommentDataSourceService, _lifecycleService: LifecycleService);
private _ensureCommentMap;
private _ensureCommentChildrenMap;
ensureMap(unitId: string, subUnitId: string): Map<string, IThreadComment>;
private _ensureThreadMap;
private _refreshCommentsMap$;
ensureMap(unitId: string, subUnitId: string): {
commentMap: Record<string, IThreadComment>;
commentChildrenMap: Map<string, IThreadComment>;
};
private _replaceComment;

@@ -64,14 +66,14 @@ syncThreadComments(unitId: string, subUnitId: string, threadIds: string[]): Promise<void>;

getComment(unitId: string, subUnitId: string, commentId: string): IThreadComment | undefined;
getComment$(unitId: string, subUnitId: string, commentId: string): import('rxjs').Observable<IThreadComment>;
getCommentWithChildren(unitId: string, subUnitId: string, commentId: string): {
root: IThreadComment;
children: IBaseComment[];
relativeUsers: Set<string>;
} | undefined;
getRootComment(unitId: string, subUnitId: string, threadId: string): IThreadComment | undefined;
getThread(unitId: string, subUnitId: string, threadId: string): Nullable<IThreadInfo>;
getCommentWithChildren(unitId: string, subUnitId: string, commentId: string): Nullable<IThreadInfo>;
private _deleteComment;
deleteThread(unitId: string, subUnitId: string, threadId: string): void;
deleteComment(unitId: string, subUnitId: string, commentId: string): boolean;
getUnit(unitId: string): (readonly [string, IThreadComment[]])[];
deleteUnit(unitId: string): void;
getRootCommentIds(unitId: string, subUnitId: string): string[];
getAll(): Record<string, Record<string, Record<string, IThreadComment>>>;
getThread(unitId: string, threadId: string): IThreadComment | undefined;
getUnit(unitId: string): IThreadInfo[];
getAll(): {
unitId: string;
threads: IThreadInfo[];
}[];
}

@@ -1,2 +0,2 @@

import { Disposable, Nullable } from '@univerjs/core';
import { Nullable, Disposable } from '@univerjs/core';
import { IBaseComment, IThreadComment } from '../types/interfaces/i-thread-comment';

@@ -53,3 +53,3 @@ export type ThreadCommentJSON = {

getThreadComment: (unitId: string, subUnitId: string, threadId: string) => Promise<Nullable<IBaseComment>>;
listThreadComments: (unitId: string, subUnitId: string, threadId: string[]) => Promise<IBaseComment[]>;
listThreadComments: (unitId: string, subUnitId: string, threadId: string[]) => Promise<IBaseComment[] | false>;
}

@@ -70,3 +70,3 @@ /**

deleteComment(unitId: string, subUnitId: string, threadId: string, commentId: string): Promise<boolean>;
listThreadComments(unitId: string, subUnitId: string, threadIds: string[]): Promise<IBaseComment[]>;
listThreadComments(unitId: string, subUnitId: string, threadIds: string[]): Promise<false | IBaseComment[]>;
saveToSnapshot(unitComments: Record<string, IThreadComment[]>, unitId: string): Record<string, ThreadCommentJSON[]>;

@@ -73,0 +73,0 @@ }

@@ -1,1 +0,1 @@

(function(global,factory){typeof exports=="object"&&typeof module<"u"?factory(exports,require("@univerjs/core"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs"],factory):(global=typeof globalThis<"u"?globalThis:global||self,factory(global.UniverThreadComment={},global.UniverCore,global.rxjs))})(this,function(exports2,core,rxjs){"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);var _a,_b,_c;const _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){return this._dataSource?this._dataSource.addComment(comment):comment}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):[]}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");exports2.ThreadCommentModel=(_a=class extends core.Disposable{constructor(_dataSourceService,_commandService){super();__publicField(this,"_commentsMap",{});__publicField(this,"_commentsTreeMap",new Map);__publicField(this,"_threadMap",new Map);__publicField(this,"_commentUpdate$",new rxjs.Subject);__publicField(this,"_commentsMap$",new rxjs.BehaviorSubject({}));__publicField(this,"commentUpdate$",this._commentUpdate$.asObservable());__publicField(this,"commentMap$",this._commentsMap$.asObservable());this._dataSourceService=_dataSourceService,this._commandService=_commandService,this.disposeWithMe(()=>{this._commentUpdate$.complete(),this._commentsMap$.complete()})}_ensureCommentMap(unitId,subUnitId){let unitMap=this._commentsMap[unitId];unitMap||(unitMap={},this._commentsMap[unitId]=unitMap);let subUnitMap=unitMap[subUnitId];return subUnitMap||(subUnitMap={},unitMap[subUnitId]=subUnitMap),subUnitMap}_ensureCommentChildrenMap(unitId,subUnitId){let unitMap=this._commentsTreeMap.get(unitId);unitMap||(unitMap=new Map,this._commentsTreeMap.set(unitId,unitMap));let subUnitMap=unitMap.get(subUnitId);return subUnitMap||(subUnitMap=new Map,unitMap.set(subUnitId,subUnitMap)),subUnitMap}_ensureThreadMap(unitId){let unitMap=this._threadMap.get(unitId);return unitMap||(unitMap=new Map,this._threadMap.set(unitId,unitMap)),unitMap}_refreshCommentsMap$(){this._commentsMap$.next({...this._commentsMap})}ensureMap(unitId,subUnitId){const commentMap=this._ensureCommentMap(unitId,subUnitId),commentChildrenMap=this._ensureCommentChildrenMap(unitId,subUnitId);return{commentMap,commentChildrenMap}}_replaceComment(unitId,subUnitId,comment){var _a2;const{commentMap,commentChildrenMap}=this.ensureMap(unitId,subUnitId),currentComment=commentMap[comment.id];if(currentComment){const newComment={...comment,ref:currentComment.ref};commentMap[comment.id]=newComment,(_a2=comment.children)==null||_a2.forEach(child=>{commentMap[child.id]={...child,ref:""}}),commentChildrenMap.set(comment.id,newComment),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){const comments=await this._dataSourceService.listThreadComments(unitId,subUnitId,threadIds);if(!comments.length)return;const deleteThreads=new Set(threadIds);comments.forEach(comment=>{this._replaceComment(unitId,subUnitId,comment),deleteThreads.delete(comment.threadId)}),deleteThreads.forEach(id=>{const thread=this.getThread(unitId,id);thread&&this.deleteComment(thread.unitId,thread.subUnitId,thread.id)}),this._refreshCommentsMap$()}addComment(unitId,subUnitId,origin,shouldSync){var _a2,_b2;const{commentMap,commentChildrenMap}=this.ensureMap(unitId,subUnitId),comment=origin,addCommentItem=__name(item=>{commentMap[item.id]=item,this._commentUpdate$.next({unitId,subUnitId,type:"add",payload:item,isRoot:!item.parentId})},"addCommentItem"),parentId=comment.parentId;if(parentId){const parent=commentMap[parentId];parent.children=[...(_a2=parent.children)!=null?_a2:[],comment],addCommentItem(comment)}else commentChildrenMap.set(comment.id,comment),this._ensureThreadMap(unitId).set(comment.threadId,comment),addCommentItem(comment),(_b2=comment.children)==null||_b2.forEach(child=>addCommentItem({...child,ref:""}));return this._refreshCommentsMap$(),shouldSync&&this.syncThreadComments(unitId,subUnitId,[comment.threadId]),!0}updateComment(unitId,subUnitId,payload,silent){const{commentMap}=this.ensureMap(unitId,subUnitId),oldComment=commentMap[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}),this._refreshCommentsMap$(),!0):!1}updateCommentRef(unitId,subUnitId,payload,silent){const{commentMap}=this.ensureMap(unitId,subUnitId),oldComment=commentMap[payload.commentId];return oldComment?(oldComment.ref=payload.ref,this._commentUpdate$.next({unitId,subUnitId,type:"updateRef",payload,silent}),this._refreshCommentsMap$(),!0):!1}resolveComment(unitId,subUnitId,commentId,resolved){const{commentMap}=this.ensureMap(unitId,subUnitId),oldComment=commentMap[commentId];return oldComment?(oldComment.resolved=resolved,this._commentUpdate$.next({unitId,subUnitId,type:"resolve",payload:{commentId,resolved}}),this._refreshCommentsMap$(),!0):!1}getComment(unitId,subUnitId,commentId){const{commentMap}=this.ensureMap(unitId,subUnitId);return commentMap[commentId]}getComment$(unitId,subUnitId,commentId){return this._commentsMap$.pipe(rxjs.map(records=>records[unitId][subUnitId][commentId]))}getCommentWithChildren(unitId,subUnitId,commentId){var _a2,_b2;const{commentMap,commentChildrenMap}=this.ensureMap(unitId,subUnitId),current=commentMap[commentId];if(!current)return;const relativeUsers=new Set,root=commentChildrenMap.get(commentId);if(root)return[root,...(_a2=root.children)!=null?_a2:[]].forEach(comment=>{var _a3;relativeUsers.add(comment.personId),(_a3=comment.text.customRanges)==null||_a3.forEach(range=>{range.rangeType===core.CustomRangeType.MENTION&&relativeUsers.add(range.rangeId)})}),{root:current,children:(_b2=root.children)!=null?_b2:[],relativeUsers}}deleteComment(unitId,subUnitId,commentId){var _a2;const{commentMap,commentChildrenMap}=this.ensureMap(unitId,subUnitId),current=commentMap[commentId];if(!current)return!0;if(current.parentId){const root=commentChildrenMap.get(current.parentId);if(root&&root.children){const index=root.children.findIndex(comment=>comment.id===commentId);root.children.splice(index,1)}delete commentMap[commentId]}else{delete commentMap[commentId];const comment=commentChildrenMap.get(commentId);commentChildrenMap.delete(commentId),this._ensureThreadMap(unitId).delete(current.threadId),(_a2=comment==null?void 0:comment.children)==null||_a2.forEach(child=>{delete commentMap[child.id],this._commentUpdate$.next({unitId,subUnitId,type:"delete",payload:{commentId:child.id,isRoot:!1,comment:child}})})}return this._commentUpdate$.next({unitId,subUnitId,type:"delete",payload:{commentId,isRoot:!current.parentId,comment:current}}),this._refreshCommentsMap$(),!0}getUnit(unitId){const unitMap=this._commentsMap[unitId];return unitMap?Array.from(Object.entries(unitMap)).map(([subUnitId,subUnitMap])=>[subUnitId,Array.from(Object.values(subUnitMap))]):[]}deleteUnit(unitId){const unitMap=this._commentsMap[unitId];unitMap&&Object.entries(unitMap).forEach(([subUnitId,subUnitMap])=>{Object.values(subUnitMap).forEach(comment=>{this.deleteComment(unitId,subUnitId,comment.id)})})}getRootCommentIds(unitId,subUnitId){const commentChildrenMap=this._ensureCommentChildrenMap(unitId,subUnitId);return Array.from(commentChildrenMap.keys())}getAll(){return this._commentsMap}getThread(unitId,threadId){return this._ensureThreadMap(unitId).get(threadId)}},__name(_a,"ThreadCommentModel"),_a),exports2.ThreadCommentModel=__decorateClass$2([__decorateParam$2(0,core.Inject(IThreadCommentDataSourceService)),__decorateParam$2(1,core.ICommandService)],exports2.ThreadCommentModel);var R=(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))(R||{});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");exports2.ThreadCommentResourceController=(_b=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(([key,v])=>{resultMap[key]=v}),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:[R.UNIVER_SHEET,R.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(_b,"ThreadCommentResourceController"),_b),exports2.ThreadCommentResourceController=__decorateClass$1([__decorateParam$1(0,core.IResourceManagerService),__decorateParam$1(1,core.Inject(exports2.ThreadCommentModel)),__decorateParam$1(2,IThreadCommentDataSourceService)],exports2.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(exports2.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(exports2.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(exports2.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(exports2.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(exports2.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(exports2.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(exports2.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(exports2.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(exports2.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");exports2.UniverThreadCommentPlugin=(_c=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 _a2;core.mergeOverrideWithDependencies([[IThreadCommentDataSourceService,{useClass:ThreadCommentDataSourceService}],[exports2.ThreadCommentModel],[exports2.ThreadCommentResourceController]],(_a2=this._config)==null?void 0:_a2.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(exports2.ThreadCommentResourceController)}},__name(_c,"UniverThreadCommentPlugin"),__publicField(_c,"pluginName",TC_PLUGIN_NAME),__publicField(_c,"type",core.UniverInstanceType.UNIVER_UNKNOWN),_c),exports2.UniverThreadCommentPlugin=__decorateClass([__decorateParam(1,core.Inject(core.Injector)),__decorateParam(2,core.ICommandService),__decorateParam(3,core.IConfigService)],exports2.UniverThreadCommentPlugin),exports2.AddCommentCommand=AddCommentCommand,exports2.AddCommentMutation=AddCommentMutation,exports2.DeleteCommentCommand=DeleteCommentCommand,exports2.DeleteCommentMutation=DeleteCommentMutation,exports2.DeleteCommentTreeCommand=DeleteCommentTreeCommand,exports2.IThreadCommentDataSourceService=IThreadCommentDataSourceService,exports2.ResolveCommentCommand=ResolveCommentCommand,exports2.ResolveCommentMutation=ResolveCommentMutation,exports2.TC_PLUGIN_NAME=TC_PLUGIN_NAME,exports2.ThreadCommentDataSourceService=ThreadCommentDataSourceService,exports2.UpdateCommentCommand=UpdateCommentCommand,exports2.UpdateCommentMutation=UpdateCommentMutation,exports2.UpdateCommentRefMutation=UpdateCommentRefMutation,Object.defineProperty(exports2,Symbol.toStringTag,{value:"Module"})});
(function(global,factory){typeof exports=="object"&&typeof module<"u"?factory(exports,require("@univerjs/core"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs"],factory):(global=typeof globalThis<"u"?globalThis:global||self,factory(global.UniverThreadComment={},global.UniverCore,global.rxjs))})(this,function(exports2,core,rxjs){"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);var _a,_b,_c;const _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 _a2;return this._dataSource?this._dataSource.addComment(comment):{...comment,threadId:(_a2=comment.threadId)!=null?_a2: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");exports2.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),exports2.ThreadCommentModel=__decorateClass$2([__decorateParam$2(0,core.Inject(IThreadCommentDataSourceService)),__decorateParam$2(1,core.Inject(core.LifecycleService))],exports2.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");exports2.ThreadCommentResourceController=(_b=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 _a2;const subUnitComments=(_a2=resultMap[info.subUnitId])!=null?_a2:[];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(_b,"ThreadCommentResourceController"),_b),exports2.ThreadCommentResourceController=__decorateClass$1([__decorateParam$1(0,core.IResourceManagerService),__decorateParam$1(1,core.Inject(exports2.ThreadCommentModel)),__decorateParam$1(2,IThreadCommentDataSourceService)],exports2.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(exports2.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(exports2.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(exports2.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(exports2.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(exports2.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(exports2.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(exports2.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(exports2.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(exports2.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");exports2.UniverThreadCommentPlugin=(_c=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 _a2;core.mergeOverrideWithDependencies([[IThreadCommentDataSourceService,{useClass:ThreadCommentDataSourceService}],[exports2.ThreadCommentModel],[exports2.ThreadCommentResourceController]],(_a2=this._config)==null?void 0:_a2.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(exports2.ThreadCommentResourceController)}},__name(_c,"UniverThreadCommentPlugin"),__publicField(_c,"pluginName",TC_PLUGIN_NAME),__publicField(_c,"type",core.UniverInstanceType.UNIVER_UNKNOWN),_c),exports2.UniverThreadCommentPlugin=__decorateClass([__decorateParam(1,core.Inject(core.Injector)),__decorateParam(2,core.ICommandService),__decorateParam(3,core.IConfigService)],exports2.UniverThreadCommentPlugin),exports2.AddCommentCommand=AddCommentCommand,exports2.AddCommentMutation=AddCommentMutation,exports2.DeleteCommentCommand=DeleteCommentCommand,exports2.DeleteCommentMutation=DeleteCommentMutation,exports2.DeleteCommentTreeCommand=DeleteCommentTreeCommand,exports2.IThreadCommentDataSourceService=IThreadCommentDataSourceService,exports2.ResolveCommentCommand=ResolveCommentCommand,exports2.ResolveCommentMutation=ResolveCommentMutation,exports2.TC_PLUGIN_NAME=TC_PLUGIN_NAME,exports2.ThreadCommentDataSourceService=ThreadCommentDataSourceService,exports2.UpdateCommentCommand=UpdateCommentCommand,exports2.UpdateCommentMutation=UpdateCommentMutation,exports2.UpdateCommentRefMutation=UpdateCommentRefMutation,Object.defineProperty(exports2,Symbol.toStringTag,{value:"Module"})});
{
"name": "@univerjs/thread-comment",
"version": "0.4.1",
"version": "0.4.2-nightly.202410291606",
"private": false,

@@ -50,4 +50,4 @@ "description": "Univer thread comment core plugin",

"dependencies": {
"@univerjs/protocol": "0.1.39-alpha.30",
"@univerjs/core": "0.4.1"
"@univerjs/protocol": "0.1.39-alpha.38",
"@univerjs/core": "0.4.2-nightly.202410291606"
},

@@ -59,3 +59,3 @@ "devDependencies": {

"vitest": "^2.1.2",
"@univerjs-infra/shared": "0.4.1"
"@univerjs-infra/shared": "0.4.2"
},

@@ -62,0 +62,0 @@ "univerSpace": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc