Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(docs-thread-comment-ui): doc comment ot (#3832)

zhangw1fd499a370ac
diff --git a/packages/docs-thread-comment-ui/src/commands/commands/add-doc-comment.command.ts b/packages/docs-thread-comment-ui/src/commands/commands/add-doc-comment.command.ts
index ae985ce921..8a0d90e5f0 100644
--- a/packages/docs-thread-comment-ui/src/commands/commands/add-doc-comment.command.ts
+++ b/packages/docs-thread-comment-ui/src/commands/commands/add-doc-comment.command.ts
@@ -14,12 +14,12 @@
* limitations under the License.
*/
-import { CommandType, CustomDecorationType, ICommandService, sequenceExecuteAsync } from '@univerjs/core';
+import type { ICommand, ITextRange } from '@univerjs/core';
+import type { IThreadComment } from '@univerjs/thread-comment';
+import { CommandType, CustomDecorationType, ICommandService, sequenceExecute } from '@univerjs/core';
import { addCustomDecorationBySelectionFactory } from '@univerjs/docs-ui';
import { AddCommentMutation, IThreadCommentDataSourceService } from '@univerjs/thread-comment';
import { SetActiveCommentOperation } from '@univerjs/thread-comment-ui';
-import type { ICommand, ITextRange } from '@univerjs/core';
-import type { IThreadComment } from '@univerjs/thread-comment';
import { DEFAULT_DOC_SUBUNIT_ID } from '../../common/const';
export interface IAddDocCommentComment {
@@ -43,7 +43,7 @@ export const AddDocCommentComment: ICommand<IAddDocCommentComment> = {
const doMutation = addCustomDecorationBySelectionFactory(
accessor,
{
- id: comment.id,
+ id: comment.threadId,
type: CustomDecorationType.COMMENT,
}
);
@@ -66,7 +66,7 @@ export const AddDocCommentComment: ICommand<IAddDocCommentComment> = {
},
};
- return (await sequenceExecuteAsync([addComment, doMutation, activeOperation], commandService)).result;
+ return (await sequenceExecute([addComment, doMutation, activeOperation], commandService)).result;
}
return false;
diff --git a/packages/docs-thread-comment-ui/src/controllers/render-controllers/render.controller.ts b/packages/docs-thread-comment-ui/src/controllers/render-controllers/render.controller.ts
index ff56b3f00f..4de3fcae59 100644
--- a/packages/docs-thread-comment-ui/src/controllers/render-controllers/render.controller.ts
+++ b/packages/docs-thread-comment-ui/src/controllers/render-controllers/render.controller.ts
@@ -15,9 +15,10 @@
*/
import type { DocumentDataModel } from '@univerjs/core';
+import type { IRichTextEditingMutationParams } from '@univerjs/docs';
import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';
-import { Disposable, Inject, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
-import { DOC_INTERCEPTOR_POINT, DocInterceptorService } from '@univerjs/docs';
+import { CustomDecorationType, Disposable, ICommandService, Inject, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
+import { DOC_INTERCEPTOR_POINT, DocInterceptorService, RichTextEditingMutation } from '@univerjs/docs';
import { DocRenderController } from '@univerjs/docs-ui';
import { ThreadCommentModel } from '@univerjs/thread-comment';
import { ThreadCommentPanelService } from '@univerjs/thread-comment-ui';
@@ -30,12 +31,14 @@ export class DocThreadCommentRenderController extends Disposable implements IRen
@Inject(ThreadCommentPanelService) private readonly _threadCommentPanelService: ThreadCommentPanelService,
@Inject(DocRenderController) private readonly _docRenderController: DocRenderController,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
- @Inject(ThreadCommentModel) private readonly _threadCommentModel: ThreadCommentModel
+ @Inject(ThreadCommentModel) private readonly _threadCommentModel: ThreadCommentModel,
+ @ICommandService private readonly _commandService: ICommandService
) {
super();
this._interceptorViewModel();
this._initReRender();
+ this._initSyncComments();
}
private _initReRender() {
@@ -86,4 +89,61 @@ export class DocThreadCommentRenderController extends Disposable implements IRen
},
});
}
+
+ private _initSyncComments() {
+ const unitId = this._context.unit.getUnitId();
+ const subUnitId = DEFAULT_DOC_SUBUNIT_ID;
+ const threadIds = this._context.unit.getBody()?.customDecorations?.filter((i) => i.type === CustomDecorationType.COMMENT).map((i) => i.id) ?? [];
+ threadIds.forEach((id) => {
+ const comment = this._threadCommentModel.getComment(unitId, subUnitId, id);
+ if (!comment) {
+ this._threadCommentModel.addComment(unitId, subUnitId, { id, threadId: id, ref: '', dT: '', personId: '', text: { dataStream: '' }, unitId, subUnitId });
+ }
+ });
+ threadIds.length && this._threadCommentModel.syncThreadComments(this._context.unit.getUnitId(), DEFAULT_DOC_SUBUNIT_ID, threadIds);
+
+ let prevThreadIds: string[] = threadIds.sort();
+ this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === RichTextEditingMutation.id) {
+ const params = commandInfo.params as IRichTextEditingMutationParams;
+ if (params.unitId !== this._context.unit.getUnitId()) {
+ return;
+ }
+
+ const currentThreadIds = this._context.unit.getBody()?.customDecorations?.filter((i) => i.type === CustomDecorationType.COMMENT).map((i) => i.id) ?? [];
+ const currentThreadIdsSorted = currentThreadIds.sort();
+ if (JSON.stringify(prevThreadIds) !== JSON.stringify(currentThreadIdsSorted)) {
+ const preIds = new Set(prevThreadIds);
+ const currentIds = new Set(currentThreadIdsSorted);
+ const addIds = new Set<string>();
+ const deleteIds = new Set<string>();
+
+ currentThreadIds.forEach((id) => {
+ if (!preIds.has(id)) {
+ addIds.add(id);
+ }
+ });
+
+ prevThreadIds.forEach((id) => {
+ if (!currentIds.has(id)) {
+ deleteIds.add(id);
+ }
+ });
+
+ prevThreadIds = currentThreadIdsSorted;
+ addIds.forEach((id) => {
+ const comment = this._threadCommentModel.getComment(unitId, subUnitId, id);
+ if (!comment) {
+ this._threadCommentModel.addComment(unitId, subUnitId, { id, threadId: id, ref: '', dT: '', personId: '', text: { dataStream: '' }, unitId, subUnitId });
+ }
+ });
+ deleteIds.forEach((id) => {
+ this._threadCommentModel.deleteThread(unitId, subUnitId, id);
+ });
+
+ this._threadCommentModel.syncThreadComments(unitId, subUnitId, [...addIds]);
+ }
+ }
+ }));
+ }
}
diff --git a/packages/sheets-thread-comment-base/src/controllers/sheets-thread-comment-ref-range.controller.ts b/packages/sheets-thread-comment-base/src/controllers/sheets-thread-comment-ref-range.controller.ts
index 5abc2859d7..1bae37bcb8 100644
--- a/packages/sheets-thread-comment-base/src/controllers/sheets-thread-comment-ref-range.controller.ts
+++ b/packages/sheets-thread-comment-base/src/controllers/sheets-thread-comment-ref-range.controller.ts
@@ -158,23 +158,18 @@ export class SheetsThreadCommentRefRangeController extends Disposable {
}
private _initData() {
- const data = this._threadCommentModel.getAll();
-
- for (const unitId in data) {
- const unitMap = data[unitId];
- for (const subUnitId in unitMap) {
- const subUnitMap = unitMap[subUnitId];
- for (const id in subUnitMap) {
- const comment = subUnitMap[id];
- const ref = comment.ref;
- const pos = singleReferenceToGrid(ref);
- const sheetComment = {
- ...comment,
- ...pos,
- };
- this._register(unitId, subUnitId, sheetComment);
- this._watch(unitId, subUnitId, sheetComment);
- }
+ const datas = this._threadCommentModel.getAll();
+
+ for (const data of datas) {
+ for (const thread of data.threads) {
+ const { unitId, subUnitId, root } = thread;
+ const pos = singleReferenceToGrid(root.ref);
+ const sheetComment = {
+ ...root,
+ ...pos,
+ };
+ this._register(unitId, subUnitId, sheetComment);
+ this._watch(unitId, subUnitId, sheetComment);
}
}
}
diff --git a/packages/sheets-thread-comment-base/src/models/sheets-thread-comment.model.ts b/packages/sheets-thread-comment-base/src/models/sheets-thread-comment.model.ts
index ce698bc887..d8a864dc90 100644
--- a/packages/sheets-thread-comment-base/src/models/sheets-thread-comment.model.ts
+++ b/packages/sheets-thread-comment-base/src/models/sheets-thread-comment.model.ts
@@ -14,10 +14,10 @@
* limitations under the License.
*/
-import { Disposable, Inject, IUniverInstanceService, ObjectMatrix, UniverInstanceType } from '@univerjs/core';
import type { CommentUpdate, IThreadComment } from '@univerjs/thread-comment';
-import { ThreadCommentModel } from '@univerjs/thread-comment';
+import { Disposable, Inject, IUniverInstanceService, ObjectMatrix, UniverInstanceType } from '@univerjs/core';
import { singleReferenceToGrid } from '@univerjs/engine-formula';
+import { ThreadCommentModel } from '@univerjs/thread-comment';
import { Subject } from 'rxjs';
export type SheetCommentUpdate = CommentUpdate & {
@@ -107,16 +107,12 @@ export class SheetsThreadCommentModel extends Disposable {
}
private _initData() {
- const data = this._threadCommentModel.getAll();
-
- for (const unitId in data) {
- const unitMap = data[unitId];
- for (const subUnitId in unitMap) {
- const subUnitMap = unitMap[subUnitId];
- for (const id in subUnitMap) {
- const comment = subUnitMap[id];
- this._addComment(unitId, subUnitId, comment);
- }
+ const datas = this._threadCommentModel.getAll();
+
+ for (const data of datas) {
+ for (const thread of data.threads) {
+ const { unitId, subUnitId, root } = thread;
+ this._addComment(unitId, subUnitId, root);
}
}
}
@@ -132,17 +128,21 @@ export class SheetsThreadCommentModel extends Disposable {
locationMap.set(commentId, { row, column });
}
- this._commentUpdate$.next({
- unitId,
- subUnitId,
- payload: comment,
- type: 'add',
- isRoot: !parentId,
- ...location,
- });
+ if (!parentId) {
+ this._commentUpdate$.next({
+ unitId,
+ subUnitId,
+ payload: comment,
+ type: 'add',
+ isRoot: !parentId,
+ ...location,
+ });
+ }
}
+ // eslint-disable-next-line max-lines-per-function
private _initUpdateTransform() {
+ // eslint-disable-next-line max-lines-per-function
this.disposeWithMe(this._threadCommentModel.commentUpdate$.subscribe((update) => {
const { unitId, subUnitId } = update;
@@ -252,7 +252,11 @@ export class SheetsThreadCommentModel extends Disposable {
if (!commentId) {
return undefined;
}
- return this._threadCommentModel.getCommentWithChildren(unitId, subUnitId, commentId);
+ const comment = this.getComment(unitId, subUnitId, commentId);
+ if (!comment) {
+ return undefined;
+ }
+ return this._threadCommentModel.getThread(unitId, subUnitId, comment.threadId);
}
showCommentMarker(unitId: string, subUnitId: string, row: number, column: number) {
@@ -270,6 +274,6 @@ export class SheetsThreadCommentModel extends Disposable {
}
getSubUnitAll(unitId: string, subUnitId: string) {
- return Object.values(this._threadCommentModel.getAll()[unitId]?.[subUnitId] ?? {});
+ return this._threadCommentModel.getUnit(unitId).filter((i) => i.subUnitId === subUnitId).map((i) => i.root);
}
}
diff --git a/packages/sheets-thread-comment/src/controllers/sheets-thread-comment-remove.controller.ts b/packages/sheets-thread-comment/src/controllers/sheets-thread-comment-remove.controller.ts
index d4da29a38d..db6b95b9b9 100644
--- a/packages/sheets-thread-comment/src/controllers/sheets-thread-comment-remove.controller.ts
+++ b/packages/sheets-thread-comment/src/controllers/sheets-thread-comment-remove.controller.ts
@@ -47,9 +47,9 @@ export class ThreadCommentRemoveSheetsController extends Disposable {
if (!subUnitId) {
return { redos: [], undos: [] };
}
- const { commentMap } = this._threadCommentModel.ensureMap(unitId, subUnitId);
+ const commentMap = this._threadCommentModel.ensureMap(unitId, subUnitId);
- const comments = Array.from(Object.values(commentMap)).filter((comment) => !comment.parentId);
+ const comments = Array.from(commentMap.values()).filter((comment) => !comment.parentId);
const ids = comments.map((comment) => comment.id);
const shouldSync = this._threadCommentDataSourceService.syncUpdateMutationToColla;
diff --git a/packages/thread-comment-ui/src/views/thread-comment-panel/index.tsx b/packages/thread-comment-ui/src/views/thread-comment-panel/index.tsx
index c9b49af0eb..19604d925b 100644
--- a/packages/thread-comment-ui/src/views/thread-comment-panel/index.tsx
+++ b/packages/thread-comment-ui/src/views/thread-comment-panel/index.tsx
@@ -14,20 +14,20 @@
* limitations under the License.
*/
+import type { Nullable } from '@univerjs/core';
+import type { IThreadComment } from '@univerjs/thread-comment';
+import type { Observable } from 'rxjs';
+import type { IThreadCommentTreeProps } from '../thread-comment-tree';
import { ICommandService, LocaleService, UniverInstanceType, useDependency, UserManagerService } from '@univerjs/core';
import { Button, Select } from '@univerjs/design';
import { IncreaseSingle } from '@univerjs/icons';
import { ThreadCommentModel } from '@univerjs/thread-comment';
import { useObservable } from '@univerjs/ui';
import React, { useEffect, useMemo, useRef, useState } from 'react';
-import type { Nullable } from '@univerjs/core';
-import type { IThreadComment } from '@univerjs/thread-comment';
-import type { Observable } from 'rxjs';
import { SetActiveCommentOperation } from '../../commands/operations/comment.operations';
import { ThreadCommentPanelService } from '../../services/thread-comment-panel.service';
import { ThreadCommentTree } from '../thread-comment-tree';
import styles from './index.module.less';
-import type { IThreadCommentTreeProps } from '../thread-comment-tree';
export interface IThreadCommentPanelProps {
unitId: string;
@@ -46,6 +46,10 @@ export interface IThreadCommentPanelProps {
showComments?: string[];
}
+interface IThreadCommentWithUsers extends IThreadComment {
+ users: Set<string>;
+}
+
export const ThreadCommentPanel = (props: IThreadCommentPanelProps) => {
const {
unitId,
@@ -79,21 +83,20 @@ export const ThreadCommentPanel = (props: IThreadCommentPanelProps) => {
const currentUser = useObservable(userService.currentUser$);
const comments = useMemo(() => {
const allComments =
- (unit === 'all' ? unitComments.map((i) => i[1]).flat() : unitComments.find((i) => i[0] === subUnitId)?.[1] ?? [])
- .filter((i) => !i.parentId);
+ (unit === 'all' ? unitComments : unitComments.filter((i) => i.subUnitId === subUnitId) ?? []);
const sort = sortComments ?? ((a) => a);
- const res = allComments;
+ const res: IThreadCommentWithUsers[] = allComments.map((i) => ({ ...i.root, children: i.children ?? [], users: i.relativeUsers }));
if (showComments) {
- const map = new Map<string, IThreadComment>();
+ const map = new Map<string, IThreadCommentWithUsers>();
res.forEach((comment) => {
map.set(comment.id, comment);
});
- return [...showComments, ''].map((id) => map.get(id)).filter(Boolean) as IThreadComment[];
+ return [...showComments, ''].map((id) => map.get(id)).filter(Boolean) as IThreadCommentWithUsers[];
} else {
- return sort(res);
+ return sort(res) as IThreadCommentWithUsers[];
}
}, [showComments, unit, unitComments, sortComments, subUnitId]);
@@ -115,16 +118,11 @@ export const ThreadCommentPanel = (props: IThreadCommentPanelProps) => {
return commentsSorted;
}
- return commentsSorted.map((comment) => threadCommentModel.getCommentWithChildren(comment.unitId, comment.subUnitId, comment.id)).map((comment) => {
- if (comment?.relativeUsers.has(currentUser.userID)) {
- return comment.root;
- }
- return null;
- }).filter(Boolean) as IThreadComment[];
+ return commentsSorted.filter((comment) => comment?.users.has(currentUser.userID));
}
return commentsSorted;
- }, [commentsSorted, currentUser?.userID, status, threadCommentModel]);
+ }, [commentsSorted, currentUser?.userID, status]);
const renderComments = tempComment
? [tempComment, ...statuedComments]
diff --git a/packages/thread-comment-ui/src/views/thread-comment-tree/index.tsx b/packages/thread-comment-ui/src/views/thread-comment-tree/index.tsx
index 596398b2ef..f6c931b1d2 100644
--- a/packages/thread-comment-ui/src/views/thread-comment-tree/index.tsx
+++ b/packages/thread-comment-ui/src/views/thread-comment-tree/index.tsx
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+import type { IAddCommentCommandParams, IThreadComment, IUpdateCommentCommandParams } from '@univerjs/thread-comment';
+import type { IThreadCommentEditorInstance } from '../thread-comment-editor';
import { generateRandomId, useDependency } from '@univerjs/core';
import { ICommandService, type IUser, LocaleService, type UniverInstanceType, UserManagerService } from '@univerjs/core';
import { Dropdown, Menu, MenuItem, Tooltip } from '@univerjs/design';
@@ -21,14 +23,13 @@ import { DeleteSingle, MoreHorizontalSingle, ReplyToCommentSingle, ResolvedSingl
import { AddCommentCommand, DeleteCommentCommand, DeleteCommentTreeCommand, ResolveCommentCommand, ThreadCommentModel, UpdateCommentCommand } from '@univerjs/thread-comment';
import { useObservable } from '@univerjs/ui';
import cs from 'clsx';
-import React, { useEffect, useRef, useState } from 'react';
-import type { IAddCommentCommandParams, IThreadComment, IUpdateCommentCommandParams } from '@univerjs/thread-comment';
+import React, { useEffect, useMemo, useRef, useState } from 'react';
+import { debounceTime } from 'rxjs';
import { SetActiveCommentOperation } from '../../commands/operations/comment.operations';
import { getDT } from '../../common/utils';
import { ThreadCommentEditor } from '../thread-comment-editor';
import { transformDocument2TextNodes, transformTextNodes2Document } from '../thread-comment-editor/util';
import styles from './index.module.less';
-import type { IThreadCommentEditorInstance } from '../thread-comment-editor';
export interface IThreadCommentTreeProps {
id?: string;
@@ -204,7 +205,8 @@ export const ThreadCommentTree = (props: IThreadCommentTreeProps) => {
const threadCommentModel = useDependency(ThreadCommentModel);
const [isHover, setIsHover] = useState(false);
const [editingId, setEditingId] = useState('');
- useObservable(threadCommentModel.commentMap$);
+ const updte$ = useMemo(() => threadCommentModel.commentUpdate$.pipe(debounceTime(16)), [threadCommentModel]);
+ useObservable(updte$);
const comments = id ? threadCommentModel.getCommentWithChildren(unitId, subUnitId, id) : null;
const commandService = useDependency(ICommandService);
const userManagerService = useDependency(UserManagerService);
diff --git a/packages/thread-comment/src/commands/commands/comment.command.ts b/packages/thread-comment/src/commands/commands/comment.command.ts
index 9b38036280..025e4bb11c 100644
--- a/packages/thread-comment/src/commands/commands/comment.command.ts
+++ b/packages/thread-comment/src/commands/commands/comment.command.ts
@@ -14,11 +14,11 @@
* limitations under the License.
*/
+import type { IThreadComment } from '../../types/interfaces/i-thread-comment';
import { CommandType, type ICommand, ICommandService } from '@univerjs/core';
import { ThreadCommentModel } from '../../models/thread-comment.model';
import { IThreadCommentDataSourceService } from '../../services/tc-datasource.service';
import { AddCommentMutation, DeleteCommentMutation, type IUpdateCommentPayload, ResolveCommentMutation, UpdateCommentMutation } from '../mutations/comment.mutation';
-import type { IThreadComment } from '../../types/interfaces/i-thread-comment';
export interface IAddCommentCommandParams {
unitId: string;
diff --git a/packages/thread-comment/src/controllers/tc-resource.controller.ts b/packages/thread-comment/src/controllers/tc-resource.controller.ts
index bf8204e5ec..fc20d7ef3e 100644
--- a/packages/thread-comment/src/controllers/tc-resource.controller.ts
+++ b/packages/thread-comment/src/controllers/tc-resource.controller.ts
@@ -38,8 +38,13 @@ export class ThreadCommentResourceController extends Disposable {
const map = this._threadCommentModel.getUnit(unitID);
const resultMap: UnitThreadCommentJSON = {};
if (map) {
- map.forEach(([key, v]) => {
- resultMap[key] = v;
+ map.forEach((info) => {
+ const subUnitComments = resultMap[info.subUnitId] ?? [];
+ subUnitComments.push({
+ ...info.root,
+ children: info.children,
+ });
+ resultMap[info.unitId] = subUnitComments;
});
return JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(resultMap, unitID));
diff --git a/packages/thread-comment/src/models/thread-comment.model.ts b/packages/thread-comment/src/models/thread-comment.model.ts
index a3ad205145..ff80fdbf5d 100644
--- a/packages/thread-comment/src/models/thread-comment.model.ts
+++ b/packages/thread-comment/src/models/thread-comment.model.ts
@@ -14,10 +14,11 @@
* limitations under the License.
*/
+import type { Nullable } from '@univerjs/core';
import type { IUpdateCommentPayload, IUpdateCommentRefPayload } from '../commands/mutations/comment.mutation';
import type { IBaseComment, IThreadComment } from '../types/interfaces/i-thread-comment';
-import { CustomRangeType, Disposable, ICommandService, Inject, LifecycleService, LifecycleStages } from '@univerjs/core';
-import { BehaviorSubject, map, Subject } from 'rxjs';
+import { Disposable, Inject, LifecycleService, LifecycleStages } from '@univerjs/core';
+import { Subject } from 'rxjs';
import { IThreadCommentDataSourceService } from '../services/tc-datasource.service';
export type CommentUpdate = {
@@ -41,6 +42,7 @@ export type CommentUpdate = {
} | {
type: 'updateRef';
payload: IUpdateCommentRefPayload;
+ threadId: string;
} | {
type: 'resolve';
payload: {
@@ -52,29 +54,32 @@ export type CommentUpdate = {
payload: IThreadComment;
});
+export interface IThreadInfo {
+ unitId: string;
+ subUnitId: string;
+ threadId: string;
+ root: IThreadComment;
+ children: IThreadComment[];
+ relativeUsers: Set<string>;
+}
+
export class ThreadCommentModel extends Disposable {
- private _commentsMap: Record<string, Record<string, Record<string, IThreadComment>>> = {};
- private _commentsTreeMap: Map<string, Map<string, Map<string, IThreadComment>>> = new Map();
- private _threadMap: Map<string, Map<string, IThreadComment>> = new Map();
+ private _commentsMap: Map<string, Map<string, Map<string, IThreadComment>>> = new Map();
+ private _threadMap: Map<string, Map<string, Map<string, IThreadComment>>> = new Map();
private _commentUpdate$ = new Subject<CommentUpdate>();
- private _commentsMap$ = new BehaviorSubject<Record<string, Record<string, Record<string, IThreadComment>>>>({});
-
commentUpdate$ = this._commentUpdate$.asObservable();
- commentMap$ = this._commentsMap$.asObservable();
private _tasks: { unitId: string; subUnitId: string; threadIds: string[] }[] = [];
constructor(
@Inject(IThreadCommentDataSourceService) private readonly _dataSourceService: IThreadCommentDataSourceService,
- @ICommandService private readonly _commandService: ICommandService,
@Inject(LifecycleService) private readonly _lifecycleService: LifecycleService
) {
super();
this.disposeWithMe(() => {
this._commentUpdate$.complete();
- this._commentsMap$.complete();
});
this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe((stage) => {
@@ -108,28 +113,11 @@ export class ThreadCommentModel extends Disposable {
}
private _ensureCommentMap(unitId: string, subUnitId: string) {
- let unitMap = this._commentsMap[unitId];
-
- if (!unitMap) {
- unitMap = {};
- this._commentsMap[unitId] = unitMap;
- }
-
- let subUnitMap = unitMap[subUnitId];
- if (!subUnitMap) {
- subUnitMap = {};
- unitMap[subUnitId] = subUnitMap;
- }
-
- return subUnitMap;
- }
-
- private _ensureCommentChildrenMap(unitId: string, subUnitId: string) {
- let unitMap = this._commentsTreeMap.get(unitId);
+ let unitMap = this._commentsMap.get(unitId);
if (!unitMap) {
unitMap = new Map();
- this._commentsTreeMap.set(unitId, unitMap);
+ this._commentsMap.set(unitId, unitMap);
}
let subUnitMap = unitMap.get(subUnitId);
@@ -137,11 +125,14 @@ export class ThreadCommentModel extends Disposable {
subUnitMap = new Map();
unitMap.set(subUnitId, subUnitMap);
}
-
return subUnitMap;
}
- private _ensureThreadMap(unitId: string) {
+ ensureMap(unitId: string, subUnitId: string) {
+ return this._ensureCommentMap(unitId, subUnitId);
+ }
+
+ private _ensureThreadMap(unitId: string, subUnitId: string) {
let unitMap = this._threadMap.get(unitId);
if (!unitMap) {
@@ -149,42 +140,34 @@ export class ThreadCommentModel extends Disposable {
this._threadMap.set(unitId, unitMap);
}
- return unitMap;
- }
-
- private _refreshCommentsMap$() {
- this._commentsMap$.next({
- ...this._commentsMap,
- });
- }
-
- ensureMap(unitId: string, subUnitId: string) {
- const commentMap = this._ensureCommentMap(unitId, subUnitId);
- const commentChildrenMap = this._ensureCommentChildrenMap(unitId, subUnitId);
+ let subUnitMap = unitMap.get(subUnitId);
+ if (!subUnitMap) {
+ subUnitMap = new Map();
+ unitMap.set(subUnitId, subUnitMap);
+ }
- return {
- commentMap,
- commentChildrenMap,
- };
+ return subUnitMap;
}
private _replaceComment(unitId: string, subUnitId: string, comment: IBaseComment) {
- const { commentMap, commentChildrenMap } = this.ensureMap(unitId, subUnitId);
- const currentComment = commentMap[comment.id];
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
+ const currentComment = commentMap.get(comment.id);
if (currentComment) {
+ const { children, ...rest } = comment;
const newComment = {
- ...comment,
+ ...rest,
ref: currentComment.ref,
};
- commentMap[comment.id] = newComment;
- comment.children?.forEach((child) => {
- commentMap[child.id] = {
+ commentMap.set(comment.id, newComment);
+
+ children?.forEach((child) => {
+ commentMap.set(child.id, {
...child,
ref: '',
- };
+ });
});
- commentChildrenMap.set(comment.id, newComment);
+
this._commentUpdate$.next({
unitId,
subUnitId,
@@ -212,8 +195,10 @@ export class ThreadCommentModel extends Disposable {
return;
}
+ const threadMap = this._ensureThreadMap(unitId, subUnitId);
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
const comments = await this._dataSourceService.listThreadComments(unitId, subUnitId, threadIds);
- if (!comments.length) {
+ if (!comments) {
return;
}
const deleteThreads = new Set<string>(threadIds);
@@ -221,23 +206,27 @@ export class ThreadCommentModel extends Disposable {
this._replaceComment(unitId, subUnitId, comment);
deleteThreads.delete(comment.threadId);
});
+
deleteThreads.forEach((id) => {
- const thread = this.getThread(unitId, id);
- if (thread) {
- this.deleteComment(thread.unitId, thread.subUnitId, thread.id);
- }
+ threadMap.delete(id);
+ commentMap.forEach((comment, commentId) => {
+ if (comment.threadId === id) {
+ commentMap.delete(commentId);
+ }
+ });
});
- this._refreshCommentsMap$();
}
addComment(unitId: string, subUnitId: string, origin: IThreadComment, shouldSync?: boolean) {
- const { commentMap, commentChildrenMap } = this.ensureMap(unitId, subUnitId);
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
+ const { parentId, children = [], ...rest } = origin;
const comment = {
- ...origin,
- parentId: origin.parentId === origin.id ? undefined : origin.parentId,
+ ...rest,
+ parentId: parentId === origin.id ? undefined : parentId,
};
+
const addCommentItem = (item: IThreadComment) => {
- commentMap[item.id] = item;
+ commentMap.set(item.id, item);
this._commentUpdate$.next({
unitId,
subUnitId,
@@ -247,26 +236,15 @@ export class ThreadCommentModel extends Disposable {
});
};
- const parentId = comment.parentId;
- if (parentId) {
- const parent = commentMap[parentId];
- parent.children = [
- ...parent.children ?? [],
- comment,
- ];
- addCommentItem(comment);
- } else {
- commentChildrenMap.set(comment.id, comment);
- const threadMap = this._ensureThreadMap(unitId);
+ addCommentItem(comment);
+ const threadMap = this._ensureThreadMap(unitId, subUnitId);
+ if (!comment.parentId) {
threadMap.set(comment.threadId, comment);
- addCommentItem(comment);
- comment.children?.forEach((child) => addCommentItem({
- ...child,
- ref: '',
- }));
+ for (const child of children) {
+ addCommentItem(child as IThreadComment);
+ }
}
- this._refreshCommentsMap$();
if (shouldSync) {
this.syncThreadComments(unitId, subUnitId, [comment.threadId]);
}
@@ -274,11 +252,12 @@ export class ThreadCommentModel extends Disposable {
}
updateComment(unitId: string, subUnitId: string, payload: IUpdateCommentPayload, silent?: boolean) {
- const { commentMap } = this.ensureMap(unitId, subUnitId);
- const oldComment = commentMap[payload.commentId];
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
+ const oldComment = commentMap.get(payload.commentId);
if (!oldComment) {
- return false;
+ return true;
}
+
oldComment.updated = true;
oldComment.text = payload.text;
oldComment.attachments = payload.attachments;
@@ -291,13 +270,12 @@ export class ThreadCommentModel extends Disposable {
payload,
silent,
});
- this._refreshCommentsMap$();
return true;
}
updateCommentRef(unitId: string, subUnitId: string, payload: IUpdateCommentRefPayload, silent?: boolean) {
- const { commentMap } = this.ensureMap(unitId, subUnitId);
- const oldComment = commentMap[payload.commentId];
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
+ const oldComment = commentMap.get(payload.commentId);
if (!oldComment) {
return false;
}
@@ -309,14 +287,14 @@ export class ThreadCommentModel extends Disposable {
type: 'updateRef',
payload,
silent,
+ threadId: oldComment.threadId,
});
- this._refreshCommentsMap$();
return true;
}
resolveComment(unitId: string, subUnitId: string, commentId: string, resolved: boolean) {
- const { commentMap } = this.ensureMap(unitId, subUnitId);
- const oldComment = commentMap[commentId];
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
+ const oldComment = commentMap.get(commentId);
if (!oldComment) {
return false;
}
@@ -331,84 +309,68 @@ export class ThreadCommentModel extends Disposable {
resolved,
},
});
- this._refreshCommentsMap$();
return true;
}
getComment(unitId: string, subUnitId: string, commentId: string) {
- const { commentMap } = this.ensureMap(unitId, subUnitId);
- return commentMap[commentId] as IThreadComment | undefined;
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
+ return commentMap.get(commentId) as IThreadComment | undefined;
}
- getComment$(unitId: string, subUnitId: string, commentId: string) {
- return this._commentsMap$.pipe(map((records) => records[unitId][subUnitId][commentId]));
+ getRootComment(unitId: string, subUnitId: string, threadId: string) {
+ const threadMap = this._ensureThreadMap(unitId, subUnitId);
+ return threadMap.get(threadId);
}
- getCommentWithChildren(unitId: string, subUnitId: string, commentId: string) {
- const { commentMap, commentChildrenMap } = this.ensureMap(unitId, subUnitId);
- const current = commentMap[commentId];
- if (!current) {
- return undefined;
- }
+ getThread(unitId: string, subUnitId: string, threadId: string): Nullable<IThreadInfo> {
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
+ const comments = Array.from(commentMap.values()).filter((comment) => comment.threadId === threadId);
+ let root: IThreadComment | undefined;
+ const children: IThreadComment[] = [];
const relativeUsers = new Set<string>();
- const root = commentChildrenMap.get(commentId);
+
+ for (const comment of comments) {
+ if (!comment.parentId) {
+ root = comment;
+ } else {
+ children.push(comment);
+ }
+
+ relativeUsers.add(comment.personId);
+ }
if (!root) {
return undefined;
}
- [root, ...root.children ?? []].forEach((comment) => {
- relativeUsers.add(comment.personId);
- comment.text.customRanges?.forEach((range) => {
- if (range.rangeType === CustomRangeType.MENTION) {
- relativeUsers.add(range.rangeId);
- }
- });
- });
-
return {
- root: current,
- children: root.children ?? [],
+ root,
+ children,
relativeUsers,
+ unitId,
+ subUnitId,
+ threadId,
};
}
- deleteComment(unitId: string, subUnitId: string, commentId: string) {
- const { commentMap, commentChildrenMap } = this.ensureMap(unitId, subUnitId);
- const current = commentMap[commentId];
- if (!current) {
- return true;
+ getCommentWithChildren(unitId: string, subUnitId: string, commentId: string) {
+ const comment = this.getComment(unitId, subUnitId, commentId);
+ if (!comment) {
+ return undefined;
}
+ return this.getThread(unitId, subUnitId, comment.threadId);
+ }
- 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);
- const threadMap = this._ensureThreadMap(unitId);
- threadMap.delete(current.threadId);
- comment?.children?.forEach((child) => {
- delete commentMap[child.id];
- this._commentUpdate$.next({
- unitId,
- subUnitId,
- type: 'delete',
- payload: {
- commentId: child.id,
- isRoot: false,
- comment: child as IThreadComment,
- },
- });
- });
+ private _deleteComment(unitId: string, subUnitId: string, commentId: string) {
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
+ const current = commentMap.get(commentId);
+ if (!current) {
+ return;
}
+ commentMap.delete(commentId);
+
this._commentUpdate$.next({
unitId,
subUnitId,
@@ -419,42 +381,78 @@ export class ThreadCommentModel extends Disposable {
comment: current,
},
});
- this._refreshCommentsMap$();
- return true;
}
- getUnit(unitId: string) {
- const unitMap = this._commentsMap[unitId];
- if (!unitMap) {
- return [];
+ deleteThread(unitId: string, subUnitId: string, threadId: string) {
+ const threadMap = this._ensureThreadMap(unitId, subUnitId);
+ threadMap.delete(threadId);
+
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
+ commentMap.forEach((comment) => {
+ if (comment.threadId === threadId) {
+ this._deleteComment(unitId, subUnitId, comment.id);
+ }
+ });
+ }
+
+ deleteComment(unitId: string, subUnitId: string, commentId: string) {
+ const commentMap = this._ensureCommentMap(unitId, subUnitId);
+ const current = commentMap.get(commentId);
+ if (!current) {
+ return true;
+ }
+
+ if (current.parentId) {
+ this._deleteComment(unitId, subUnitId, commentId);
+ } else {
+ this.deleteThread(unitId, subUnitId, current.threadId);
}
- return Array.from(Object.entries(unitMap)).map(([subUnitId, subUnitMap]) => [subUnitId, Array.from(Object.values(subUnitMap))] as const);
+ return true;
}
deleteUnit(unitId: string) {
- const unitMap = this._commentsMap[unitId];
+ const unitMap = this._commentsMap.get(unitId);
if (!unitMap) {
return;
}
- Object.entries(unitMap).forEach(([subUnitId, subUnitMap]) => {
- Object.values(subUnitMap).forEach((comment) => {
+ unitMap.forEach((subUnitMap, subUnitId) => {
+ subUnitMap.forEach((comment) => {
this.deleteComment(unitId, subUnitId, comment.id);
});
});
}
- getRootCommentIds(unitId: string, subUnitId: string) {
- const commentChildrenMap = this._ensureCommentChildrenMap(unitId, subUnitId);
- return Array.from(commentChildrenMap.keys());
+ getUnit(unitId: string) {
+ const unitMap = this._threadMap.get(unitId);
+ if (!unitMap) {
+ return [];
+ }
+
+ const threads: IThreadInfo[] = [];
+
+ unitMap.forEach((subUnitSet, subUnitId) => {
+ subUnitSet.forEach((threadComment, threadId) => {
+ const thread = this.getThread(unitId, subUnitId, threadId);
+ if (thread) {
+ threads.push(thread);
+ }
+ });
+ });
+
+ return threads;
}
getAll() {
- return this._commentsMap;
- }
+ const all: { unitId: string; threads: IThreadInfo[] }[] = [];
+ this._commentsMap.forEach((unitMap, unitId) => {
+ all.push({
+ unitId,
+ threads: this.getUnit(unitId),
+ });
+ });
- getThread(unitId: string, threadId: string) {
- return this._ensureThreadMap(unitId).get(threadId);
+ return all;
}
}
diff --git a/packages/thread-comment/src/services/tc-datasource.service.ts b/packages/thread-comment/src/services/tc-datasource.service.ts
index d81ee2505f..a24f57f45d 100644
--- a/packages/thread-comment/src/services/tc-datasource.service.ts
+++ b/packages/thread-comment/src/services/tc-datasource.service.ts
@@ -14,9 +14,9 @@
* limitations under the License.
*/
-import { createIdentifier, Disposable } from '@univerjs/core';
import type { Nullable } from '@univerjs/core';
import type { IBaseComment, IThreadComment } from '../types/interfaces/i-thread-comment';
+import { createIdentifier, Disposable } from '@univerjs/core';
export type ThreadCommentJSON = {
id: string;
@@ -73,7 +73,7 @@ export interface IThreadCommentDataSourceService {
deleteComment: (unitId: string, subUnitId: string, threadId: string, commentId: string) => Promise<Success>;
saveToSnapshot: (unitComments: Record<string, IThreadComment[]>, unitId: string) => Record<string, ThreadCommentJSON[]>;
getThreadComment: (unitId: string, subUnitId: string, threadId: string) => Promise<Nullable<IBaseComment>>;
- listThreadComments: (unitId: string, subUnitId: string, threadId: string[]) => Promise<IBaseComment[]>;
+ listThreadComments: (unitId: string, subUnitId: string, threadId: string[]) => Promise<IBaseComment[] | false>;
}
/**
@@ -108,7 +108,7 @@ export class ThreadCommentDataSourceService extends Disposable implements IThrea
if (this._dataSource) {
return this._dataSource.addComment(comment);
}
- return comment;
+ return { ...comment, threadId: comment.threadId ?? comment.id };
}
async updateComment(comment: IThreadComment) {
@@ -137,7 +137,7 @@ export class ThreadCommentDataSourceService extends Disposable implements IThrea
return this.dataSource.listComments(unitId, subUnitId, threadIds);
}
- return [];
+ return false;
}
saveToSnapshot(unitComments: Record<string, IThreadComment[]>, unitId: string) {