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
237
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.2.14 to 0.2.15

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 rxjs=require("rxjs"),core=require("@univerjs/core"),_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 I=(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))(I||{});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:[I.UNIVER_SHEET,I.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([core.OnLifecycle(core.LifecycleStages.Starting,exports.ThreadCommentResourceController),__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)})}},__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){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 I=(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))(I||{});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:[I.UNIVER_SHEET,I.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([core.OnLifecycle(core.LifecycleStages.Starting,exports.ThreadCommentResourceController),__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)})}},__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 { Disposable, createIdentifier, CustomRangeType, Inject, ICommandService, OnLifecycle, LifecycleStages, IResourceManagerService, CommandType, Plugin, UniverInstanceType, mergeOverrideWithDependencies, Injector, IConfigService } from "@univerjs/core";
import { Subject, BehaviorSubject, map } from "rxjs";
import { Disposable, createIdentifier, CustomRangeType, Inject, ICommandService, OnLifecycle, LifecycleStages, IResourceManagerService, CommandType, Plugin, UniverInstanceType, mergeOverrideWithDependencies, Injector, IConfigService } from "@univerjs/core";
const _ThreadCommentDataSourceService = class _ThreadCommentDataSourceService extends Disposable {

@@ -229,3 +229,3 @@ constructor() {

if (root && root.children) {
const index = root.children.findIndex((comment) => comment.id = commentId);
const index = root.children.findIndex((comment) => comment.id === commentId);
root.children.splice(index, 1);

@@ -232,0 +232,0 @@ }

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

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

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

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

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

(function(global,factory){typeof exports=="object"&&typeof module<"u"?factory(exports,require("rxjs"),require("@univerjs/core")):typeof define=="function"&&define.amd?define(["exports","rxjs","@univerjs/core"],factory):(global=typeof globalThis<"u"?globalThis:global||self,factory(global.UniverThreadComment={},global.rxjs,global.UniverCore))})(this,function(exports2,rxjs,core){"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 I=(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))(I||{});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:[I.UNIVER_SHEET,I.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([core.OnLifecycle(core.LifecycleStages.Starting,exports2.ThreadCommentResourceController),__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)})}},__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){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 I=(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))(I||{});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:[I.UNIVER_SHEET,I.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([core.OnLifecycle(core.LifecycleStages.Starting,exports2.ThreadCommentResourceController),__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)})}},__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.2.14",
"version": "0.2.15",
"private": false,

@@ -51,7 +51,7 @@ "description": "Univer thread comment core plugin",

"rxjs": ">=7.0.0",
"@univerjs/core": "0.2.14"
"@univerjs/core": "0.2.15"
},
"dependencies": {
"@univerjs/protocol": "0.1.39-alpha.15",
"@univerjs/core": "0.2.14"
"@univerjs/core": "0.2.15"
},

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

"vitest": "^2.0.5",
"@univerjs-infra/shared": "0.2.14"
"@univerjs-infra/shared": "0.2.15"
},

@@ -66,0 +66,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