@univerjs/thread-comment 1.0.0-insiders.20260813-7c9aa50 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/umd/index.js CHANGED
@@ -1 +1,2 @@
1
- (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverThreadComment={},e.UniverCore,e.rxjs))})(this,function(e,t,n){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function r(e){"@babel/helpers - typeof";return r=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},r(e)}function i(e,t){if(r(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var i=n.call(e,t||`default`);if(r(i)!=`object`)return i;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function a(e){var t=i(e,`string`);return r(t)==`symbol`?t:t+``}function o(e,t,n){return(t=a(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var s=class extends t.Disposable{set dataSource(e){this._dataSource=e}get dataSource(){return this._dataSource}constructor(){super(),o(this,`_dataSource`,null),o(this,`syncUpdateMutationToColla`,!0)}async getThreadComment(e,t,n){return this._dataSource?(await this._dataSource.listComments(e,t,[n]))[0]:null}async addComment(e){let t=this._dataSource?await this._dataSource.addComment(e):e;return{...t,threadId:t.threadId||t.id}}async updateComment(e){return!this._dataSource||this._dataSource.updateComment(e)}async resolveComment(e){return!this._dataSource||this._dataSource.resolveComment(e)}async deleteComment(e,t,n,r){return!this._dataSource||this._dataSource.deleteComment(e,t,n,r)}async listThreadComments(e,t,n){return this.dataSource?this.dataSource.listComments(e,t,n):!1}saveToSnapshot(e,t){if(this._dataSource){let t={};return Object.keys(e).forEach(n=>{let r=e[n];t[n]=r.map(this.dataSource.saveCommentToSnapshot)}),t}return e}};let c=(0,t.createIdentifier)(`univer.thread-comment.data-source-service`);function l(e,t){return function(n,r){t(n,r,e)}}function u(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let d=class extends t.Disposable{constructor(e,r){super(),this._dataSourceService=e,this._lifecycleService=r,o(this,`_commentsMap`,new Map),o(this,`_threadMap`,new Map),o(this,`_commentUpdate$`,new n.Subject),o(this,`commentUpdate$`,this._commentUpdate$.asObservable()),o(this,`_tasks`,[]),this.disposeWithMe(()=>{this._commentUpdate$.complete()}),this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe(e=>{let n=new Map;e===t.LifecycleStages.Rendered&&(this._tasks.forEach(({unitId:e,subUnitId:t,threadIds:r})=>{let i=n.get(e);i||(i=new Map,n.set(e,i));let a=i.get(t);a||(a=new Set,i.set(t,a));for(let e of r)a.add(e)}),this._tasks=[],n.forEach((e,t)=>{e.forEach((e,n)=>{this.syncThreadComments(t,n,Array.from(e))})}))}))}_ensureCommentMap(e,t){let n=this._commentsMap.get(e);n||(n=new Map,this._commentsMap.set(e,n));let r=n.get(t);return r||(r=new Map,n.set(t,r)),r}ensureMap(e,t){return this._ensureCommentMap(e,t)}_ensureThreadMap(e,t){let n=this._threadMap.get(e);n||(n=new Map,this._threadMap.set(e,n));let r=n.get(t);return r||(r=new Map,n.set(t,r)),r}_replaceComment(e,t,n){let r=this._ensureCommentMap(e,t),i=r.get(n.id);if(!i){this.addComment(e,t,n);return}let{children:a,...o}=n,s={...o,ref:i.ref};r.set(n.id,s),a==null||a.forEach(e=>{r.set(e.id,{...e,ref:``})}),this._commentUpdate$.next({unitId:e,subUnitId:t,type:`syncUpdate`,payload:s}),!!n.resolved!=!!i.resolved&&this._commentUpdate$.next({unitId:e,subUnitId:t,type:`resolve`,payload:{commentId:n.id,resolved:!!n.resolved}})}async syncThreadComments(e,n,r){if(this._lifecycleService.stage<t.LifecycleStages.Rendered){this._tasks.push({unitId:e,subUnitId:n,threadIds:r});return}let i=this._ensureThreadMap(e,n),a=this._ensureCommentMap(e,n),o=await this._dataSourceService.listThreadComments(e,n,r);if(!o)return;let s=new Set(r);o.forEach(t=>{s.has(t.threadId)&&(this._replaceComment(e,n,t),s.delete(t.threadId))}),s.forEach(e=>{i.delete(e),a.forEach((t,n)=>{t.threadId===e&&a.delete(n)})})}addComment(e,t,n,r){let i=this._ensureCommentMap(e,t),{parentId:a,children:o=[],...s}=n,c={...s,parentId:a===n.id?void 0:a};c.threadId||=c.parentId||c.id;let l=n=>{i.set(n.id,n),this._commentUpdate$.next({unitId:e,subUnitId:t,type:`add`,payload:n,isRoot:!n.parentId})};l(c);let u=this._ensureThreadMap(e,t);if(!c.parentId){u.set(c.threadId,c);for(let e of o)l(e)}return r&&this.syncThreadComments(e,t,[c.threadId]),!0}updateComment(e,t,n,r){let i=this._ensureCommentMap(e,t).get(n.commentId);return i?(i.updated=!0,i.text=n.text,i.attachments=n.attachments,i.updateT=n.updateT,this._commentUpdate$.next({unitId:e,subUnitId:t,type:`update`,payload:n,silent:r}),!0):!0}updateCommentRef(e,t,n,r){let i=this._ensureCommentMap(e,t).get(n.commentId);return i?(i.ref=n.ref,this._commentUpdate$.next({unitId:e,subUnitId:t,type:`updateRef`,payload:n,silent:r,threadId:i.threadId}),!0):!1}resolveComment(e,t,n,r){let i=this._ensureCommentMap(e,t).get(n);return i?(i.resolved=r,this._commentUpdate$.next({unitId:e,subUnitId:t,type:`resolve`,payload:{commentId:n,resolved:r}}),!0):!1}getComment(e,t,n){return this._ensureCommentMap(e,t).get(n)}getRootComment(e,t,n){return this._ensureThreadMap(e,t).get(n)}getThread(e,t,n){let r=this._ensureCommentMap(e,t),i=Array.from(r.values()).filter(e=>e.threadId===n),a,o=[],s=new Set;for(let e of i)e.parentId?o.push(e):a=e,s.add(e.personId);if(a)return{root:a,children:o,relativeUsers:s,unitId:e,subUnitId:t,threadId:n}}getCommentWithChildren(e,t,n){let r=this.getComment(e,t,n);if(r)return this.getThread(e,t,r.threadId)}_deleteComment(e,t,n){let r=this._ensureCommentMap(e,t),i=r.get(n);i&&(r.delete(n),this._commentUpdate$.next({unitId:e,subUnitId:t,type:`delete`,payload:{commentId:n,isRoot:!i.parentId,comment:i}}))}deleteThread(e,t,n){this._ensureThreadMap(e,t).delete(n),this._ensureCommentMap(e,t).forEach(r=>{r.threadId===n&&this._deleteComment(e,t,r.id)})}deleteComment(e,t,n){let r=this._ensureCommentMap(e,t).get(n);return r&&(r.parentId?this._deleteComment(e,t,n):this.deleteThread(e,t,r.threadId)),!0}deleteUnit(e){let t=this._commentsMap.get(e);t&&t.forEach((t,n)=>{t.forEach(t=>{this.deleteComment(e,n,t.id)})})}getUnit(e){let t=this._threadMap.get(e);if(!t)return[];let n=[];return t.forEach((t,r)=>{t.forEach((t,i)=>{let a=this.getThread(e,r,i);a&&n.push(a)})}),n}getAll(){let e=[];return this._commentsMap.forEach((t,n)=>{e.push({unitId:n,threads:this.getUnit(n)})}),e}};d=u([l(0,(0,t.Inject)(c)),l(1,(0,t.Inject)(t.LifecycleService))],d);let f={id:`thread-comment.mutation.add-comment`,type:t.CommandType.MUTATION,handler(e,t,n){if(!t)return!1;let r=e.get(d),{unitId:i,subUnitId:a,comment:o,sync:s}=t,c=s||(n==null?void 0:n.fromChangeset)&&!o.parentId;return r.addComment(i,a,o,c)}},p={id:`thread-comment.mutation.update-comment`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(d),{unitId:r,subUnitId:i,payload:a,silent:o}=t;return n.updateComment(r,i,a,o)}},m={id:`thread-comment.mutation.update-comment-ref`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(d),{unitId:r,subUnitId:i,payload:a,silent:o}=t;return n.updateCommentRef(r,i,a,o)}},h={id:`thread-comment.mutation.resolve-comment`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(d),{unitId:r,subUnitId:i,resolved:a,commentId:o}=t;return n.resolveComment(r,i,o,a)}},g={id:`thread-comment.mutation.delete-comment`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(d),{unitId:r,subUnitId:i,commentId:a}=t;return n.deleteComment(r,i,a)}},_={id:`thread-comment.command.add-comment`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let r=e.get(t.ICommandService),i=e.get(c),{comment:a}=n,o=await i.addComment(a),s=i.syncUpdateMutationToColla,l=!a.parentId,u={id:f.id,params:{...n,comment:o}};return l?await r.executeCommand(u.id,u.params):r.executeCommand(u.id,u.params,{onlyLocal:!s})}},v={id:`thread-comment.command.update-comment`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let{unitId:r,subUnitId:i,payload:a}=n,o=e.get(t.ICommandService),s=e.get(d),l=e.get(c),u=l.syncUpdateMutationToColla,f=s.getComment(r,i,a.commentId);if(!f)return!1;let{children:m,...h}=f;if(!await l.updateComment({...h,...a}))return!1;let g={id:p.id,params:n};return o.executeCommand(g.id,g.params,{onlyLocal:!u}),!0}},y={id:`thread-comment.command.resolve-comment`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let{unitId:r,subUnitId:i,resolved:a,commentId:o}=n,s=e.get(c),l=e.get(d).getComment(r,i,o),u=s.syncUpdateMutationToColla;return!l||!await s.resolveComment({...l,resolved:a})?!1:e.get(t.ICommandService).executeCommand(h.id,n,{onlyLocal:!u})}},b={id:`thread-comment.command.delete-comment`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let r=e.get(d),i=e.get(c),a=e.get(t.ICommandService),{unitId:o,subUnitId:s,commentId:l}=n,u=i.syncUpdateMutationToColla,f=r.getComment(o,s,l);if(!f||!await i.deleteComment(o,s,f.threadId,l))return!1;let p={id:g.id,params:n};return a.executeCommand(p.id,p.params,{onlyLocal:!u})}},x={id:`thread-comment.command.delete-comment-tree`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let r=e.get(d),i=e.get(t.ICommandService),a=e.get(c),{unitId:o,subUnitId:s,commentId:l}=n,u=r.getCommentWithChildren(o,s,l);return!u||!await a.deleteComment(o,s,u.root.threadId,l)?!1:await i.executeCommand(g.id,{unitId:o,subUnitId:s,commentId:u.root.id})}};function S(e){return(0,t.dateKit)(e).format(`YYYY/MM/DD HH:mm`)}let C=`UNIVER_THREAD_COMMENT_PLUGIN`,w=`SHEET_${C}`,T=class extends t.Disposable{constructor(e,t,n){super(),this._resourceManagerService=e,this._threadCommentModel=t,this._threadCommentDataSourceService=n,this._initSnapshot()}_initSnapshot(){let e=e=>{let t=this._threadCommentModel.getUnit(e),n={};return t?(t.forEach(e=>{let t=n[e.subUnitId]??[];t.push({...e.root,children:e.children}),n[e.subUnitId]=t}),JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(n,e))):``},n=e=>{if(!e)return{};try{return JSON.parse(e)}catch{return{}}};this.disposeWithMe(this._resourceManagerService.registerPluginResource({pluginName:w,businesses:[t.UniverInstanceType.UNIVER_SHEET,t.UniverInstanceType.UNIVER_DOC],toJson:t=>e(t),parseJson:e=>n(e),onUnLoad:e=>{this._threadCommentModel.deleteUnit(e)},onLoad:async(e,t)=>{Object.keys(t).forEach(n=>{let r=t[n];r.forEach(t=>{this._threadCommentModel.addComment(e,n,t)}),this._threadCommentModel.syncThreadComments(e,n,r.map(e=>e.threadId))})}}))}};T=u([l(0,t.IResourceManagerService),l(1,(0,t.Inject)(d)),l(2,c)],T);var E=`@univerjs/thread-comment`,D=`1.0.0-insiders.20260813-7c9aa50`;let O={},k=class extends t.Plugin{constructor(e=O,n,r,i){super(),this._config=e,this._injector=n,this._commandService=r,this._configService=i;let{...a}=(0,t.merge)({},O,this._config);this._configService.setConfig(`thread-comment.config`,a)}onStarting(){var e;(0,t.mergeOverrideWithDependencies)([[c,{useClass:s}],[d],[T]],(e=this._config)==null?void 0:e.overrides).forEach(e=>{this._injector.add(e)}),[_,v,b,y,x,f,p,m,g,h].forEach(e=>{this._commandService.registerCommand(e)}),this._injector.get(T)}};o(k,`pluginName`,C),o(k,`packageName`,E),o(k,`version`,D),o(k,`type`,t.UniverInstanceType.UNIVER_UNKNOWN),k=u([l(1,(0,t.Inject)(t.Injector)),l(2,t.ICommandService),l(3,t.IConfigService)],k),e.AddCommentCommand=_,e.AddCommentMutation=f,e.DeleteCommentCommand=b,e.DeleteCommentMutation=g,e.DeleteCommentTreeCommand=x,e.IThreadCommentDataSourceService=c,e.ResolveCommentCommand=y,e.ResolveCommentMutation=h,e.SHEET_UNIVER_THREAD_COMMENT_PLUGIN=w,e.TC_PLUGIN_NAME=C,e.ThreadCommentDataSourceService=s,Object.defineProperty(e,"ThreadCommentModel",{enumerable:!0,get:function(){return d}}),Object.defineProperty(e,"ThreadCommentResourceController",{enumerable:!0,get:function(){return T}}),Object.defineProperty(e,"UniverThreadCommentPlugin",{enumerable:!0,get:function(){return k}}),e.UpdateCommentCommand=v,e.UpdateCommentMutation=p,e.UpdateCommentRefMutation=m,e.getDT=S});
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverThreadComment={},e.UniverCore,e.rxjs))})(this,function(e,t,n){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function r(e){"@babel/helpers - typeof";return r=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},r(e)}function i(e,t){if(r(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var i=n.call(e,t||`default`);if(r(i)!=`object`)return i;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function a(e){var t=i(e,`string`);return r(t)==`symbol`?t:t+``}function o(e,t,n){return(t=a(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var s=class extends t.Disposable{set dataSource(e){this._dataSource=e}get dataSource(){return this._dataSource}constructor(){super(),o(this,`_dataSource`,null),o(this,`_pendingAdds`,new Map),o(this,`syncUpdateMutationToColla`,!0)}async getThreadComment(e,t,n){return this._dataSource?(await this._dataSource.listComments(e,t,[n]))[0]:null}addComment(e){let t=`${e.unitId}\0${e.subUnitId}\0${e.id}`,n=this._pendingAdds.get(t);if(n)return n;let r=(async()=>{let t=this._dataSource?await this._dataSource.addComment(e):e;return{...t,threadId:t.threadId||t.id}})();return this._pendingAdds.set(t,r),r.finally(()=>{this._pendingAdds.get(t)===r&&this._pendingAdds.delete(t)}).catch(()=>void 0),r}async updateComment(e){return!this._dataSource||this._dataSource.updateComment(e)}async resolveComment(e){return!this._dataSource||this._dataSource.resolveComment(e)}async deleteComment(e,t,n,r){return!this._dataSource||this._dataSource.deleteComment(e,t,n,r)}async deleteThread(e,t,n){var r;return(r=this._dataSource)!=null&&r.deleteThread?this._dataSource.deleteThread(e,t,n):this.deleteComment(e,t,n,n)}async listThreadComments(e,t,n){return this.dataSource?this.dataSource.listComments(e,t,n):!1}saveToSnapshot(e,t){if(this._dataSource){let t={};return Object.keys(e).forEach(n=>{let r=e[n];t[n]=r.map(this.dataSource.saveCommentToSnapshot)}),t}return e}};let c=(0,t.createIdentifier)(`univer.thread-comment.data-source-service`),l=function(e){return e.SHEET_CELL=`sheet-cell`,e.SHEET_DRAWING=`sheet-drawing`,e.DOC_TEXT_RANGE=`doc-text-range`,e.DOC_DRAWING=`doc-drawing`,e.SLIDE_ELEMENT=`slide-element`,e.SLIDE_POSITION=`slide-position`,e.BOARD_ELEMENT=`board-element`,e.BOARD_POSITION=`board-position`,e.BASE_RECORD=`base-record`,e}({}),u=`univer-comment-anchor:`;function d(e){return typeof e==`string`&&e.length>0}function f(e){return e===void 0||d(e)}function p(e){return typeof e==`number`&&Number.isFinite(e)}function m(e){if(!e||typeof e!=`object`)return!1;switch(Reflect.get(e,`kind`)){case`sheet-drawing`:case`doc-drawing`:case`slide-element`:case`board-element`:return d(Reflect.get(e,`elementId`))&&f(Reflect.get(e,`pageId`));case`slide-position`:case`board-position`:return p(Reflect.get(e,`x`))&&p(Reflect.get(e,`y`))&&f(Reflect.get(e,`pageId`));case`base-record`:return d(Reflect.get(e,`tableId`))&&d(Reflect.get(e,`recordId`));default:return!1}}function h(e){if(!m(e))throw TypeError(`Invalid thread comment anchor`);return`${u}${JSON.stringify({v:1,anchor:e})}`}function g(e){if(!e.startsWith(u))return null;try{let t=JSON.parse(e.slice(22));if(!t||typeof t!=`object`||Reflect.get(t,`v`)!==1)return null;let n=Reflect.get(t,`anchor`);return m(n)?n:null}catch{return null}}function _(e,t){return function(n,r){t(n,r,e)}}function v(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let y=class extends t.Disposable{constructor(e,r,i){super(),this._dataSourceService=e,this._lifecycleService=r,this._univerInstanceService=i,o(this,`_commentsMap`,new Map),o(this,`_threadMap`,new Map),o(this,`_syncVersions`,new Map),o(this,`_nextSyncVersion`,0),o(this,`_commentUpdate$`,new n.Subject),o(this,`commentUpdate$`,this._commentUpdate$.asObservable()),o(this,`_tasks`,[]),this.disposeWithMe(()=>{this._commentUpdate$.complete()}),this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe(e=>{let n=new Map;e===t.LifecycleStages.Rendered&&(this._tasks.forEach(({unitId:e,subUnitId:t,threadIds:r})=>{let i=n.get(e);i||(i=new Map,n.set(e,i));let a=i.get(t);a||(a=new Set,i.set(t,a));for(let e of r)a.add(e)}),this._tasks=[],n.forEach((e,t)=>{e.forEach((e,n)=>{this.syncThreadComments(t,n,Array.from(e))})}))}))}_ensureCommentMap(e,t){let n=this._commentsMap.get(e);n||(n=new Map,this._commentsMap.set(e,n));let r=n.get(t);return r||(r=new Map,n.set(t,r)),r}ensureMap(e,t){return this._ensureCommentMap(e,t)}_ensureThreadMap(e,t){let n=this._threadMap.get(e);n||(n=new Map,this._threadMap.set(e,n));let r=n.get(t);return r||(r=new Map,n.set(t,r)),r}_syncKey(e,t,n){return`${e}\0${t}\0${n}`}_invalidateSync(e,t,n){this._syncVersions.delete(this._syncKey(e,t,n))}_replaceComment(e,t,n){let r=this._ensureCommentMap(e,t),i=r.get(n.id);if(!i){`ref`in n&&typeof n.ref==`string`&&this.addComment(e,t,{...n,ref:n.ref});return}let{children:a=[],...o}=n,s=new Set([n.id]),c=a.filter(e=>{let t=r.get(e.id);return e.threadId!==n.threadId||s.has(e.id)||t&&t.threadId!==n.threadId?!1:(s.add(e.id),!0)}),l={...o,authorName:o.authorName??(o.personId===i.personId?i.authorName:void 0),ref:i.ref};r.set(n.id,l),l.parentId||this._ensureThreadMap(e,t).set(l.threadId,l);let u=new Set([n.id,...c.map(e=>e.id)]);r.forEach((r,i)=>{r.threadId===n.threadId&&!u.has(i)&&this._deleteComment(e,t,i)}),c.forEach(e=>{var t,i;r.set(e.id,{...e,authorName:e.authorName??(e.personId===((t=r.get(e.id))==null?void 0:t.personId)?(i=r.get(e.id))==null?void 0:i.authorName:void 0),parentId:n.id,ref:``})}),this._commentUpdate$.next({unitId:e,subUnitId:t,type:`syncUpdate`,payload:l}),!!n.resolved!=!!i.resolved&&this._commentUpdate$.next({unitId:e,subUnitId:t,type:`resolve`,payload:{commentId:n.id,resolved:!!n.resolved}})}async syncThreadComments(e,n,r){if(this._lifecycleService.stage<t.LifecycleStages.Rendered){this._tasks.push({unitId:e,subUnitId:n,threadIds:r});return}let i=new Map(r.map(t=>{let r=this._syncKey(e,n,t),i=++this._nextSyncVersion;return this._syncVersions.set(r,i),[t,{key:r,version:i}]})),a=()=>i.forEach(({key:e,version:t})=>{this._syncVersions.get(e)===t&&this._syncVersions.delete(e)}),o;try{o=await this._dataSourceService.listThreadComments(e,n,r)}catch(e){throw a(),e}if(!o){a();return}let s=new Set(r.filter(e=>{let t=i.get(e);return this._syncVersions.get(t.key)===t.version}));if(!s.size){a();return}let c=this._ensureThreadMap(e,n),l=this._ensureCommentMap(e,n);o.forEach(t=>{s.has(t.threadId)&&(this._replaceComment(e,n,t),s.delete(t.threadId))}),s.forEach(t=>{c.delete(t),l.forEach((r,i)=>{r.threadId===t&&this._deleteComment(e,n,i)})}),a()}addComment(e,t,n,r){let i=this._ensureCommentMap(e,t);if(i.has(n.id))return!0;let{parentId:a,children:o=[],...s}=n,c={...s,parentId:a===n.id?void 0:a};c.threadId||=c.parentId||c.id,this._invalidateSync(e,t,c.threadId);let l=n=>{i.set(n.id,n),this._commentUpdate$.next({unitId:e,subUnitId:t,type:`add`,payload:n,isRoot:!n.parentId})};l(c);let u=this._ensureThreadMap(e,t);if(!c.parentId){u.set(c.threadId,c);for(let e of o)e.id===c.id||e.threadId!==c.threadId||i.has(e.id)||l({...e,parentId:c.id,ref:``})}return r&&this.syncThreadComments(e,t,[c.threadId]),!0}updateComment(e,t,n,r){let i=this._ensureCommentMap(e,t).get(n.commentId);return i?(this._invalidateSync(e,t,i.threadId),i.updated=!0,i.text=n.text,i.attachments=n.attachments,i.updateT=n.updateT,this._commentUpdate$.next({unitId:e,subUnitId:t,type:`update`,payload:n,silent:r}),!0):!1}updateCommentRef(e,t,n,r){let i=this._ensureCommentMap(e,t).get(n.commentId);return i?(this._invalidateSync(e,t,i.threadId),i.ref=n.ref,this._commentUpdate$.next({unitId:e,subUnitId:t,type:`updateRef`,payload:n,silent:r,threadId:i.threadId}),!0):!1}resolveComment(e,t,n,r){let i=this._ensureCommentMap(e,t).get(n);return i?(this._invalidateSync(e,t,i.threadId),i.resolved=r,this._commentUpdate$.next({unitId:e,subUnitId:t,type:`resolve`,payload:{commentId:n,resolved:r}}),!0):!1}getComment(e,t,n){return this._ensureCommentMap(e,t).get(n)}getRootComment(e,t,n){return this._ensureThreadMap(e,t).get(n)}getThread(e,t,n){let r=this._ensureCommentMap(e,t),i=Array.from(r.values()).filter(e=>e.threadId===n),a,o=[],s=new Set;for(let e of i)e.parentId?o.push(e):a=e,s.add(e.personId);if(a)return{root:a,children:o,relativeUsers:s,unitId:e,subUnitId:t,threadId:n}}getCommentWithChildren(e,t,n){let r=this.getComment(e,t,n);if(r)return this.getThread(e,t,r.threadId)}_deleteComment(e,t,n){let r=this._ensureCommentMap(e,t),i=r.get(n);i&&(this._invalidateSync(e,t,i.threadId),r.delete(n),this._commentUpdate$.next({unitId:e,subUnitId:t,type:`delete`,payload:{commentId:n,isRoot:!i.parentId,comment:i}}))}deleteThread(e,t,n){this._invalidateSync(e,t,n),this._ensureThreadMap(e,t).delete(n),this._ensureCommentMap(e,t).forEach(r=>{r.threadId===n&&this._deleteComment(e,t,r.id)})}deleteComment(e,t,n){let r=this._ensureCommentMap(e,t).get(n);return r&&(r.parentId?this._deleteComment(e,t,n):this.deleteThread(e,t,r.threadId)),!0}deleteUnit(e){let t=this._commentsMap.get(e);t==null||t.forEach((t,n)=>{t.forEach(t=>{this.deleteComment(e,n,t.id)})}),this._commentsMap.delete(e),this._threadMap.delete(e),this._tasks=this._tasks.filter(t=>t.unitId!==e);let n=`${e}\0`;this._syncVersions.forEach((e,t)=>{t.startsWith(n)&&this._syncVersions.delete(t)})}getUnit(e){let t=this._commentsMap.get(e);if(!t)return[];let n=[];return t.forEach((t,r)=>{let i=new Map;t.forEach(t=>{let n=i.get(t.threadId);n||(n={unitId:e,subUnitId:r,threadId:t.threadId,children:[],relativeUsers:new Set},i.set(t.threadId,n)),t.parentId?n.children.push(t):n.root=t,n.relativeUsers.add(t.personId)}),i.forEach(e=>{e.root&&n.push({...e,root:e.root})})}),n}getAll(){let e=[];return this._commentsMap.forEach((t,n)=>{e.push({unitId:n,threads:this.getUnit(n)})}),e}getAnchorKind(e,n){let r=g(n);if(r)return r.kind;switch(this._univerInstanceService.getUnitType(e)){case t.UniverInstanceType.UNIVER_SHEET:return`sheet-cell`;case t.UniverInstanceType.UNIVER_DOC:return`doc-text-range`;default:return null}}query(e={}){let t=e.unitIds?new Set(e.unitIds):null,n=e.subUnitIds?new Set(e.subUnitIds):null,r=e.anchorKinds?new Set(e.anchorKinds):null,i=e.authorIds?new Set(e.authorIds):null;return this.getAll().flatMap(({unitId:a,threads:o})=>t&&!t.has(a)?[]:o.filter(t=>{if(n&&!n.has(t.subUnitId)||e.resolved!==void 0&&!!t.root.resolved!==e.resolved)return!1;if(r){let e=this.getAnchorKind(a,t.root.ref);if(!e||!r.has(e))return!1}return!(i&&![t.root,...t.children].some(e=>i.has(e.personId)))}))}};y=v([_(0,(0,t.Inject)(c)),_(1,(0,t.Inject)(t.LifecycleService)),_(2,t.IUniverInstanceService)],y);let b={id:`thread-comment.mutation.add-comment`,type:t.CommandType.MUTATION,handler(e,t,n){if(!t)return!1;let r=e.get(y),{unitId:i,subUnitId:a,comment:o,sync:s}=t,c=s||(n==null?void 0:n.fromChangeset)&&!o.parentId;return r.addComment(i,a,o,c)}},x={id:`thread-comment.mutation.update-comment`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(y),{unitId:r,subUnitId:i,payload:a,silent:o}=t;return n.updateComment(r,i,a,o)}},S={id:`thread-comment.mutation.update-comment-ref`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(y),{unitId:r,subUnitId:i,payload:a,silent:o}=t;return n.updateCommentRef(r,i,a,o)}},C={id:`thread-comment.mutation.resolve-comment`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(y),{unitId:r,subUnitId:i,resolved:a,commentId:o}=t;return n.resolveComment(r,i,o,a)}},w={id:`thread-comment.mutation.delete-comment`,type:t.CommandType.MUTATION,handler(e,t){if(!t)return!1;let n=e.get(y),{unitId:r,subUnitId:i,commentId:a}=t;return n.deleteComment(r,i,a)}},T=new WeakMap;function E(e,t){let n=T.get(e)??{resolve:0,update:0},r=n[t]+1;return n[t]=r,T.set(e,n),r}function D(e,t,n){var r;return((r=T.get(e))==null?void 0:r[t])===n}let O={id:`thread-comment.command.add-comment`,type:t.CommandType.COMMAND,async handler(e,n){var r,i;if(!n)return!1;let a=e.get(t.ICommandService),o=e.get(c),s=e.get(y),{comment:l}=n,u=!l.parentId;if(!u&&((r=s.getRootComment(n.unitId,n.subUnitId,l.threadId))==null?void 0:r.id)!==l.parentId)return!1;let d={...await o.addComment(l),unitId:n.unitId,subUnitId:n.subUnitId,ref:l.ref,parentId:l.parentId,...u?{}:{threadId:l.threadId}},f=o.syncUpdateMutationToColla;if(!u&&((i=s.getRootComment(n.unitId,n.subUnitId,l.threadId))==null?void 0:i.id)!==l.parentId){try{await o.deleteComment(n.unitId,n.subUnitId,d.threadId,d.id)}catch{}return!1}let p={id:b.id,params:{...n,comment:d}};return u?await a.executeCommand(p.id,p.params):a.executeCommand(p.id,p.params,{onlyLocal:!f})}},k={id:`thread-comment.command.update-comment`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let{unitId:r,subUnitId:i,payload:a}=n,o=e.get(t.ICommandService),s=e.get(y),l=e.get(c),u=l.syncUpdateMutationToColla,d=s.getComment(r,i,a.commentId);if(!d)return!1;let{children:f,...p}=d,m=E(d,`update`);if(!await l.updateComment({...p,...a})||s.getComment(r,i,a.commentId)!==d||!D(d,`update`,m))return!1;let h={id:x.id,params:n};return o.executeCommand(h.id,h.params,{onlyLocal:!u})}},A={id:`thread-comment.command.resolve-comment`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let{unitId:r,subUnitId:i,resolved:a,commentId:o}=n,s=e.get(c),l=e.get(y),u=l.getComment(r,i,o),d=s.syncUpdateMutationToColla;if(!u)return!1;let f=E(u,`resolve`);return!await s.resolveComment({...u,resolved:a})||l.getComment(r,i,o)!==u||!D(u,`resolve`,f)?!1:e.get(t.ICommandService).executeCommand(C.id,n,{onlyLocal:!d})}},j={id:`thread-comment.command.delete-comment`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let r=e.get(y),i=e.get(c),a=e.get(t.ICommandService),{unitId:o,subUnitId:s,commentId:l}=n,u=i.syncUpdateMutationToColla,d=r.getComment(o,s,l);if(!d||!await i.deleteComment(o,s,d.threadId,l))return!1;let f={id:w.id,params:n};return a.executeCommand(f.id,f.params,{onlyLocal:!u})}},M={id:`thread-comment.command.delete-comment-tree`,type:t.CommandType.COMMAND,async handler(e,n){if(!n)return!1;let r=e.get(y),i=e.get(t.ICommandService),a=e.get(c),{unitId:o,subUnitId:s,commentId:l}=n,u=r.getCommentWithChildren(o,s,l);return!u||!(a.deleteThread?await a.deleteThread(o,s,u.root.threadId):await a.deleteComment(o,s,u.root.threadId,u.root.id))?!1:await i.executeCommand(w.id,{unitId:o,subUnitId:s,commentId:u.root.id})}};function N(e){return(0,t.dateKit)(e).format(`YYYY/MM/DD HH:mm`)}let P=[`renderedPageBreaks`,`textRuns`,`paragraphs`,`sectionBreaks`,`customBlocks`,`tables`,`columnGroups`,`blockRanges`,`customRanges`,`customDecorations`];function F(e){if(!e||typeof e!=`object`||typeof Reflect.get(e,`dataStream`)!=`string`||P.some(t=>{let n=Reflect.get(e,t);return n!==void 0&&!Array.isArray(n)}))return!1;let t=Reflect.get(e,`payloads`);return t===void 0||typeof t==`object`&&!!t&&!Array.isArray(t)&&Object.values(t).every(e=>typeof e==`string`)}function I(e){if(typeof e!=`string`){if(!F(e))throw TypeError(`Invalid thread comment content`);if(!e.dataStream.replace(/[\r\n]/g,``).trim())throw TypeError(`Thread comment content cannot be empty`);return e}if(!e.trim())throw TypeError(`Thread comment content cannot be empty`);return{dataStream:e.replace(/\r\n?|\n/g,`\r`).replace(/\r*$/,`\r
2
+ `)}}function L(e,t){return{unitId:e.unitId,subUnitId:e.subUnitId,threadId:e.threadId,root:e.root,children:e.children,anchorKind:t,anchor:g(e.root.ref),relativeUserIds:Array.from(e.relativeUsers)}}let R=class{constructor(e,t,n){this._commandService=e,this._model=t,this._userManagerService=n}createCommentAsync(e){let n=e.id??(0,t.generateRandomId)(),r=e.threadId??n,i=e.personId??this._userManagerService.getCurrentUser().userID;return this._commandService.executeCommand(O.id,{unitId:e.unitId,subUnitId:e.subUnitId,comment:{id:n,threadId:r,unitId:e.unitId,subUnitId:e.subUnitId,ref:h(e.anchor),text:I(e.content),attachments:e.attachments??[],dT:N(e.dateTime),authorName:e.authorName,personId:i}})}replyCommentAsync(e){let n=this._model.getRootComment(e.unitId,e.subUnitId,e.threadId);return n?this._commandService.executeCommand(O.id,{unitId:e.unitId,subUnitId:e.subUnitId,comment:{id:e.id??(0,t.generateRandomId)(),threadId:e.threadId,parentId:n.id,unitId:e.unitId,subUnitId:e.subUnitId,ref:n.ref,text:I(e.content),attachments:e.attachments??[],dT:N(e.dateTime),authorName:e.authorName,personId:e.personId??this._userManagerService.getCurrentUser().userID}}):Promise.resolve(!1)}updateCommentAsync(e){return this._commandService.executeCommand(k.id,{unitId:e.unitId,subUnitId:e.subUnitId,payload:{commentId:e.commentId,text:I(e.content),attachments:e.attachments,updateT:N(e.dateTime)}})}deleteCommentAsync(e){return this._commandService.executeCommand(e.deleteThread?M.id:j.id,{unitId:e.unitId,subUnitId:e.subUnitId,commentId:e.commentId})}resolveCommentAsync(e){return this._commandService.executeCommand(A.id,{unitId:e.unitId,subUnitId:e.subUnitId,commentId:e.commentId,resolved:e.resolved??!0})}getComments(e={}){return this._model.query(e).map(e=>L(e,this._model.getAnchorKind(e.unitId,e.root.ref)))}async listCommentsAsync(e={}){let t=this._model.query({unitIds:e.unitIds,subUnitIds:e.subUnitIds}),n=new Map;return t.forEach(e=>{let t=`${e.unitId}\0${e.subUnitId}`,r=n.get(t)??{unitId:e.unitId,subUnitId:e.subUnitId,threadIds:[]};r.threadIds.push(e.threadId),n.set(t,r)}),await Promise.all(Array.from(n.values()).map(e=>this._model.syncThreadComments(e.unitId,e.subUnitId,e.threadIds))),this._model.query(e).map(e=>L(e,this._model.getAnchorKind(e.unitId,e.root.ref)))}};R=v([_(0,(0,t.Inject)(t.ICommandService)),_(1,(0,t.Inject)(y)),_(2,(0,t.Inject)(t.UserManagerService))],R);function z(e){return Array.isArray(e)&&e.every(e=>typeof e==`string`)}function B(e,t=!0){if(!e||typeof e!=`object`)return!1;let n=e;return typeof n.id==`string`&&n.id.length>0&&typeof n.threadId==`string`&&n.threadId.length>0&&(typeof n.ref==`string`||!t&&n.ref===void 0)&&(n.authorName===void 0||typeof n.authorName==`string`)&&(n.text===void 0||F(n.text))&&(n.attachments===void 0||z(n.attachments))&&(n.mentions===void 0||z(n.mentions))&&(!t||n.children===void 0||Array.isArray(n.children)&&n.children.every(e=>B(e,!1)))}function V(e,t,n,r){return{pluginName:n,businesses:r,toJson:n=>{let r=e.getUnit(n),i={};return r.forEach(e=>{let t=i[e.subUnitId]??[];t.push({...e.root,children:e.children}),i[e.subUnitId]=t}),JSON.stringify(t.saveToSnapshot(i,n))},parseJson:e=>{if(!e)return{};try{let t=JSON.parse(e);return!t||typeof t!=`object`||Array.isArray(t)?{}:Object.fromEntries(Object.entries(t).flatMap(([e,t])=>Array.isArray(t)?[[e,t.filter(e=>B(e))]]:[]))}catch{return{}}},onUnLoad:t=>{e.deleteUnit(t)},onLoad:(t,n)=>{Object.keys(n).forEach(r=>{let i=n[r];i.forEach(n=>{var i;let a=new Set([n.id]),o=(i=n.children)==null?void 0:i.filter(e=>e.threadId!==n.threadId||a.has(e.id)?!1:(a.add(e.id),!0)).map(e=>({...e,unitId:t,subUnitId:r}));e.addComment(t,r,{...n,unitId:t,subUnitId:r,children:o})}),e.syncThreadComments(t,r,i.map(e=>e.threadId))})}}}var H=`@univerjs/thread-comment`,U=`1.0.0`;let W={},G=`UNIVER_THREAD_COMMENT_PLUGIN`,K=class extends t.Plugin{constructor(e=W,n,r,i){super(),this._config=e,this._injector=n,this._commandService=r,this._configService=i;let{...a}=(0,t.merge)({},W,this._config);this._configService.setConfig(`thread-comment.config`,a)}onStarting(){var e;(0,t.mergeOverrideWithDependencies)([[c,{useClass:s}],[y],[R]],(e=this._config)==null?void 0:e.overrides).forEach(e=>{this._injector.add(e)}),[O,k,j,A,M,b,x,S,w,C].forEach(e=>{this._commandService.registerCommand(e)})}};o(K,`pluginName`,G),o(K,`packageName`,H),o(K,`version`,U),o(K,`type`,t.UniverInstanceType.UNIVER_UNKNOWN),K=v([_(1,(0,t.Inject)(t.Injector)),_(2,t.ICommandService),_(3,t.IConfigService)],K),e.AddCommentCommand=O,e.AddCommentMutation=b,e.DeleteCommentCommand=j,e.DeleteCommentMutation=w,e.DeleteCommentTreeCommand=M,e.IThreadCommentDataSourceService=c,e.ResolveCommentCommand=A,e.ResolveCommentMutation=C,e.TC_PLUGIN_NAME=G,e.ThreadCommentAnchorKind=l,e.ThreadCommentDataSourceService=s,Object.defineProperty(e,"ThreadCommentFacadeService",{enumerable:!0,get:function(){return R}}),Object.defineProperty(e,"ThreadCommentModel",{enumerable:!0,get:function(){return y}}),Object.defineProperty(e,"UniverThreadCommentPlugin",{enumerable:!0,get:function(){return K}}),e.UpdateCommentCommand=k,e.UpdateCommentMutation=x,e.UpdateCommentRefMutation=S,e.createThreadCommentResourceHook=V,e.deserializeThreadCommentAnchor=g,e.getDT=N,e.isThreadCommentAnchor=m,e.isThreadCommentDocumentBody=F,e.normalizeThreadCommentContent=I,e.serializeThreadCommentAnchor=h});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/thread-comment",
3
- "version": "1.0.0-insiders.20260813-7c9aa50",
3
+ "version": "1.0.0",
4
4
  "private": false,
5
5
  "description": "Shared thread comment models, commands, and services for Univer.",
6
6
  "author": "DreamNum Co., Ltd. <developer@univer.ai>",
@@ -35,6 +35,16 @@
35
35
  "require": "./lib/cjs/*",
36
36
  "types": "./lib/types/index.d.ts"
37
37
  },
38
+ "./facade": {
39
+ "import": "./lib/es/facade.js",
40
+ "require": "./lib/cjs/facade.js",
41
+ "types": "./lib/types/facade/index.d.ts"
42
+ },
43
+ "./lib/facade": {
44
+ "import": "./lib/es/facade.js",
45
+ "require": "./lib/cjs/facade.js",
46
+ "types": "./lib/types/facade/index.d.ts"
47
+ },
38
48
  "./lib/*": "./lib/*"
39
49
  },
40
50
  "main": "./lib/es/index.js",
@@ -52,13 +62,13 @@
52
62
  "rxjs": ">=7.0.0"
53
63
  },
54
64
  "dependencies": {
55
- "@univerjs/core": "1.0.0-insiders.20260813-7c9aa50"
65
+ "@univerjs/core": "1.0.0"
56
66
  },
57
67
  "devDependencies": {
68
+ "@univerjs-infra/shared": "1.0.0",
58
69
  "rxjs": "^7.8.2",
59
70
  "typescript": "^6.0.3",
60
- "vitest": "^4.1.10",
61
- "@univerjs-infra/shared": "1.0.0-beta.0"
71
+ "vitest": "^5.0.0"
62
72
  },
63
73
  "scripts": {
64
74
  "test": "vitest run",