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.

feat(docs-mention-ui): support doc mention (#4093)

zhangw5b8048158d39
diff --git a/examples/src/sheets/main.ts b/examples/src/sheets/main.ts
index a7fba766bb..edba642b78 100644
--- a/examples/src/sheets/main.ts
+++ b/examples/src/sheets/main.ts
@@ -14,12 +14,12 @@
* limitations under the License.
*/
-import type { Nullable } from '@univerjs/core';
import type { IUniverRPCMainThreadConfig } from '@univerjs/rpc';
import { FUniver, LocaleType, LogLevel, Univer, UniverInstanceType, UserManagerService } from '@univerjs/core';
import { UniverDebuggerPlugin } from '@univerjs/debugger';
import { defaultTheme } from '@univerjs/design';
import { UniverDocsPlugin } from '@univerjs/docs';
+import { UniverDocsMentionUIPlugin } from '@univerjs/docs-mention-ui';
import { UniverDocsUIPlugin } from '@univerjs/docs-ui';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
@@ -39,7 +39,7 @@ import { UniverSheetsThreadCommentPlugin } from '@univerjs/sheets-thread-comment
import { UniverSheetsThreadCommentUIPlugin } from '@univerjs/sheets-thread-comment-ui';
import { UniverSheetsUIPlugin } from '@univerjs/sheets-ui';
import { UniverSheetsZenEditorPlugin } from '@univerjs/sheets-zen-editor';
-import { IThreadCommentMentionDataService, type IThreadCommentMentionDataSource, UniverThreadCommentUIPlugin } from '@univerjs/thread-comment-ui';
+import { UniverThreadCommentUIPlugin } from '@univerjs/thread-comment-ui';
import { UniverUIPlugin } from '@univerjs/ui';
import { enUS, faIR, ruRU, viVN, zhCN, zhTW } from '../locales';
@@ -69,28 +69,6 @@ export const mockUser = {
canBindAnonymous: false,
};
-class CustomMentionDataService implements IThreadCommentMentionDataService {
- dataSource: Nullable<IThreadCommentMentionDataSource>;
- trigger: string = '@';
-
- async getMentions() {
- return [
- {
- id: mockUser.userID,
- label: mockUser.name,
- type: 'user',
- icon: mockUser.avatar,
- },
- {
- id: '2',
- label: 'User2',
- type: 'user',
- icon: mockUser.avatar,
- },
- ];
- }
-}
-
// univer
const univer = new Univer({
theme: defaultTheme,
@@ -114,6 +92,7 @@ univer.registerPlugin(UniverDocsPlugin);
univer.registerPlugin(UniverRenderEnginePlugin);
univer.registerPlugin(UniverUIPlugin, { container: 'app' });
univer.registerPlugin(UniverDocsUIPlugin);
+univer.registerPlugin(UniverDocsMentionUIPlugin);
univer.registerPlugin(UniverSheetsPlugin, { notExecuteFormula: true });
univer.registerPlugin(UniverSheetsUIPlugin);
@@ -128,9 +107,7 @@ univer.registerPlugin(UniverSheetsConditionalFormattingPlugin);
univer.registerPlugin(UniverSheetsFilterPlugin);
univer.registerPlugin(UniverSheetsSortPlugin);
univer.registerPlugin(UniverSheetsHyperLinkPlugin);
-univer.registerPlugin(UniverThreadCommentUIPlugin, {
- overrides: [[IThreadCommentMentionDataService, { useClass: CustomMentionDataService }]],
-});
+univer.registerPlugin(UniverThreadCommentUIPlugin);
univer.registerPlugin(UniverSheetsThreadCommentPlugin);
univer.registerPlugin(UniverSheetsThreadCommentUIPlugin);
diff --git a/examples/src/uni/main.ts b/examples/src/uni/main.ts
index da0ba546ce..d7ce102e2a 100644
--- a/examples/src/uni/main.ts
+++ b/examples/src/uni/main.ts
@@ -14,9 +14,7 @@
* limitations under the License.
*/
-import type { Nullable } from '@univerjs/core';
import type { IUniverRPCMainThreadConfig } from '@univerjs/rpc';
-import type { IThreadCommentMentionDataSource } from '@univerjs/thread-comment-ui';
import { FUniver, Injector, LocaleType, LogLevel, Univer, UniverInstanceType } from '@univerjs/core';
import { UniverDebuggerPlugin } from '@univerjs/debugger';
import { defaultTheme } from '@univerjs/design';
@@ -41,7 +39,7 @@ import { UniverSheetsThreadCommentPlugin } from '@univerjs/sheets-thread-comment
import { SheetUIController, UniverSheetsUIPlugin } from '@univerjs/sheets-ui';
import { UniverSlidesPlugin } from '@univerjs/slides';
import { SlidesUIController, UniverSlidesUIPlugin } from '@univerjs/slides-ui';
-import { IThreadCommentMentionDataService, UniverThreadCommentUIPlugin } from '@univerjs/thread-comment-ui';
+import { UniverThreadCommentUIPlugin } from '@univerjs/thread-comment-ui';
import { UniDocsUIController } from '@univerjs/uni-docs-ui';
import { UniverDocUniFormulaPlugin } from '@univerjs/uni-formula';
import { UniSheetsUIController } from '@univerjs/uni-sheets-ui';
@@ -67,36 +65,6 @@ registerSheetPlugins(univer);
registerSlidePlugins(univer);
registerUniPlugins(univer);
-const mockUser = {
- userID: 'Owner_qxVnhPbQ',
- name: 'Owner',
- avatar: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAInSURBVHgBtZU9TxtBEIbfWRzFSIdkikhBSqRQkJqkCKTCFkqVInSUSaT0wC8w/gXxD4gU2nRJkXQWhAZowDUUWKIwEgWWbEEB3mVmx3dn4DA2nB/ppNuPeWd29mMIPXDr+RxwtgRHeW6+guNPRxogqnL7Dwz9psJ27S4NShaeZTH3kwXy6I81dlRKcmRui88swdq9AcSFL7Buz1Vmlns64MiLsCjzwnIYHLH57tbfFbs7KRaXyEU8FVZofqccOfA5l7Q8LPIkGrwnb2RPNEXWFVMUF3L+kDCk0btDDAMzOm5YfAHDwp4tG74wnzAsiOYMnJ3GoDybA7IT98/jm5+JNnfiIzAS6LlqHQBN/i6b2t/cV1Hh6BfwYlHnHP4AXi5q/8kmMMpOs8+BixZw/Fd6xUEHEbnkgclvQP2fGp7uShRKnQ3G32rkjV1th8JhIGG7tR/JyjGteSOZELwGMmNqIIigRCLRh2OZIE6BjItdd7pCW6Uhm1zzkUtungSxwEUzNpQ+GQumtH1ej1MqgmNT6vwmhCq5yuwq56EYTbgeQUz3yvrpV1b4ok3nYJ+eYhgYmjRUqErx2EDq0Fr8FhG++iqVGqxlUJI/70Ar0UgJaWHj6hYVHJrfKssAHot1JfqwE9WVWzXZVd5z2Ws/4PnmtEjkXeKJDvxUecLbWOXH/DP6QQ4J72NS0adedp1aseBfXP8odlZFfPvBF7SN/8hky1TYuPOAXAEipMx15u5ToAAAAABJRU5ErkJggg==',
- anonymous: false,
- canBindAnonymous: false,
-};
-
-class CustomMentionDataService implements IThreadCommentMentionDataService {
- dataSource: Nullable<IThreadCommentMentionDataSource>;
- trigger: string = '@';
-
- async getMentions() {
- return [
- {
- id: mockUser.userID,
- label: mockUser.name,
- type: 'user',
- icon: mockUser.avatar,
- },
- {
- id: '2',
- label: 'User2',
- type: 'user',
- icon: mockUser.avatar,
- },
- ];
- }
-}
-
// create univer instances
univer.createUnit(UniverInstanceType.UNIVER_SHEET, DEFAULT_WORKBOOK_DATA_DEMO);
univer.createUnit(UniverInstanceType.UNIVER_DOC, DEFAULT_DOCUMENT_DATA_EN);
@@ -149,9 +117,7 @@ function registerBasicPlugins(univer: Univer) {
univer.registerPlugin(UniverRPCMainThreadPlugin, {
workerURL: new Worker(new URL('./worker.js', import.meta.url), { type: 'module' }),
} as IUniverRPCMainThreadConfig);
- univer.registerPlugin(UniverThreadCommentUIPlugin, {
- overrides: [[IThreadCommentMentionDataService, { useClass: CustomMentionDataService }]],
- });
+ univer.registerPlugin(UniverThreadCommentUIPlugin);
univer.registerPlugin(UniverDocsDrawingUIPlugin);
}
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 7196b4fb30..8a413e2e6b 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -72,6 +72,8 @@ export { getCustomDecorationSlice, getCustomRangeSlice, normalizeBody } from './
export { EventState, EventSubject, fromEventSubject, type IEventObserver } from './observer/observable';
export { AuthzIoLocalService } from './services/authz-io/authz-io-local.service';
export { IAuthzIoService } from './services/authz-io/type';
+export { MentionIOLocalService } from './services/mention-io/mention-io-local.service';
+export { type IListMentionParam, type IListMentionResponse, IMentionIOService, type ITypeMentionList } from './services/mention-io/type';
export {
type CommandListener,
CommandService,
diff --git a/packages/core/src/services/mention-io/mention-io-local.service.ts b/packages/core/src/services/mention-io/mention-io-local.service.ts
new file mode 100644
index 0000000000..d11fcdbf63
--- /dev/null
+++ b/packages/core/src/services/mention-io/mention-io-local.service.ts
@@ -0,0 +1,49 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { IListMentionParam, IListMentionResponse, IMentionIOService } from './type';
+import { Inject } from '@wendellhu/redi';
+import { MentionType } from '../../types/enum';
+import { UserManagerService } from '../user-manager/user-manager.service';
+
+export class MentionIOLocalService implements IMentionIOService {
+ constructor(
+ @Inject(UserManagerService) private _userManagerService: UserManagerService
+ ) {}
+
+ async list(params: IListMentionParam): Promise<IListMentionResponse> {
+ return {
+ list: [{
+ type: MentionType.PERSON,
+ mentions: [
+ {
+ objectType: MentionType.PERSON,
+ objectId: this._userManagerService.getCurrentUser().userID,
+ label: this._userManagerService.getCurrentUser().name,
+ metadata: {
+ icon: this._userManagerService.getCurrentUser().avatar,
+ },
+ },
+ ],
+ metadata: {},
+ title: 'PEOPLE',
+ }],
+ page: params.page,
+ size: params.size,
+ total: 1,
+ };
+ }
+}
diff --git a/packages/core/src/services/mention-io/type.ts b/packages/core/src/services/mention-io/type.ts
new file mode 100644
index 0000000000..cda49a039f
--- /dev/null
+++ b/packages/core/src/services/mention-io/type.ts
@@ -0,0 +1,47 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { MentionType } from '../../types/enum';
+import type { IMention } from '../../types/interfaces';
+import { createIdentifier } from '@wendellhu/redi';
+
+export interface IListMentionParam {
+ search?: string;
+ type?: MentionType;
+ page?: number;
+ size?: number;
+ unitId?: string;
+}
+
+export interface ITypeMentionList {
+ mentions: IMention[];
+ type: MentionType;
+ metadata: Record<string, string>;
+ title: string;
+}
+
+export interface IListMentionResponse {
+ list: ITypeMentionList[];
+ page?: number;
+ size?: number;
+ total?: number;
+}
+
+export interface IMentionIOService {
+ list (params: IListMentionParam): Promise<IListMentionResponse>;
+}
+
+export const IMentionIOService = createIdentifier<IMentionIOService>('univer.service.mention-io');
diff --git a/packages/core/src/types/enum/index.ts b/packages/core/src/types/enum/index.ts
index 9d8fc74481..719a26ced4 100644
--- a/packages/core/src/types/enum/index.ts
+++ b/packages/core/src/types/enum/index.ts
@@ -26,6 +26,7 @@ export * from './direction';
export * from './format-type';
export * from './interpolation-point-type';
export * from './locale-type';
+export * from './mention-type';
export * from './protection-type';
export * from './prst-geom-type';
export * from './relative-date';
diff --git a/packages/docs-mention-ui/src/types/enums/mention-type.ts b/packages/core/src/types/enum/mention-type.ts
similarity index 93%
rename from packages/docs-mention-ui/src/types/enums/mention-type.ts
rename to packages/core/src/types/enum/mention-type.ts
index c2d74c09bd..eb017be6c9 100644
--- a/packages/docs-mention-ui/src/types/enums/mention-type.ts
+++ b/packages/core/src/types/enum/mention-type.ts
@@ -16,4 +16,8 @@
export enum MentionType {
PERSON,
+ FILE,
+ DATE,
+ LOCATION,
+ EVENT,
}
diff --git a/packages/core/src/types/interfaces/i-document-data.ts b/packages/core/src/types/interfaces/i-document-data.ts
index 736920428a..374030c1bf 100644
--- a/packages/core/src/types/interfaces/i-document-data.ts
+++ b/packages/core/src/types/interfaces/i-document-data.ts
@@ -17,6 +17,7 @@
import type { IAbsoluteTransform, ISize } from '../../shared/shape';
import type { Nullable } from '../../shared/types';
import type { BooleanNumber, CellValueType, HorizontalAlign, LocaleType, TextDirection, VerticalAlign, WrapStrategy } from '../enum';
+import type { IMention } from './i-mention';
import type { IColorStyle, IStyleBase } from './i-style-data';
// Attention: all dimensional units, unless otherwise stated, refer to pt,1 pt = 1 / 72 in
@@ -360,6 +361,8 @@ export interface ICustomRange<T extends Record<string, any> = Record<string, any
export type IHyperLinkCustomRange = ICustomRange<{ url: string }>;
+export type IMentionCustomRange = ICustomRange<IMention>;
+
export enum CustomRangeType {
HYPERLINK,
FIELD, // 17.16 Fields and Hyperlinks
diff --git a/packages/docs-mention-ui/src/types/interfaces/i-mention.ts b/packages/core/src/types/interfaces/i-mention.ts
similarity index 85%
rename from packages/docs-mention-ui/src/types/interfaces/i-mention.ts
rename to packages/core/src/types/interfaces/i-mention.ts
index 7099a5eea1..d409fe68d3 100644
--- a/packages/docs-mention-ui/src/types/interfaces/i-mention.ts
+++ b/packages/core/src/types/interfaces/i-mention.ts
@@ -14,16 +14,15 @@
* limitations under the License.
*/
-import type { MentionType } from '../enums/mention-type';
+import type { MentionType } from '../enum/mention-type';
export interface IDocMention extends IMention {
id: string;
}
export interface IMention {
- // mention payload
objectType: MentionType;
objectId: string;
label: string;
- extra?: Record<string, any>;
+ metadata?: Record<string, string | number | boolean | undefined>;
}
diff --git a/packages/core/src/types/interfaces/index.ts b/packages/core/src/types/interfaces/index.ts
index 404e1bc244..e9cef83e08 100644
--- a/packages/core/src/types/interfaces/index.ts
+++ b/packages/core/src/types/interfaces/index.ts
@@ -17,6 +17,7 @@
export * from './i-document-data';
export * from './i-document-data-interceptor';
export * from './i-image-properties';
+export * from './i-mention';
export * from './i-need-check-disposable';
export * from './i-placeholder';
export * from './i-shape-properties';
diff --git a/packages/core/src/univer.ts b/packages/core/src/univer.ts
index 07b58fb71b..aca4a00044 100644
--- a/packages/core/src/univer.ts
+++ b/packages/core/src/univer.ts
@@ -40,6 +40,8 @@ import { LifecycleStages } from './services/lifecycle/lifecycle';
import { LifecycleService } from './services/lifecycle/lifecycle.service';
import { LocaleService } from './services/locale/locale.service';
import { DesktopLogService, ILogService } from './services/log/log.service';
+import { MentionIOLocalService } from './services/mention-io/mention-io-local.service';
+import { IMentionIOService } from './services/mention-io/type';
import { PermissionService } from './services/permission/permission.service';
import { IPermissionService } from './services/permission/type';
import { PluginService } from './services/plugin/plugin.service';
@@ -194,6 +196,7 @@ function createUniverInjector(parentInjector?: Injector, override?: DependencyOv
[IResourceManagerService, { useClass: ResourceManagerService, lazy: true }],
[IResourceLoaderService, { useClass: ResourceLoaderService, lazy: true }],
[IAuthzIoService, { useClass: AuthzIoLocalService, lazy: true }],
+ [IMentionIOService, { useClass: MentionIOLocalService, lazy: true }],
], override);
const injector = parentInjector ? parentInjector.createChild(dependencies) : new Injector(dependencies);
diff --git a/packages/docs-mention-ui/src/commands/commands/doc-mention.command.ts b/packages/docs-mention-ui/src/commands/commands/doc-mention.command.ts
index 1e7c8dae9e..bb5db840f5 100644
--- a/packages/docs-mention-ui/src/commands/commands/doc-mention.command.ts
+++ b/packages/docs-mention-ui/src/commands/commands/doc-mention.command.ts
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-import type { ICommand, IDocumentBody } from '@univerjs/core';
-import type { IDocMention } from '../../types/interfaces/i-mention';
+import type { ICommand, IDocMention, IDocumentBody } from '@univerjs/core';
import { CommandType, CustomRangeType, ICommandService } from '@univerjs/core';
import { deleteCustomRangeFactory, DocSelectionManagerService, replaceSelectionFactory } from '@univerjs/docs';
@@ -41,8 +40,8 @@ export const AddDocMentionCommand: ICommand<IAddDocMentionCommandParams> = {
return false;
}
- const { extra, ...mentionConfig } = mention;
- const dataStream = ` @${mention.label} `;
+ const { metadata, ...mentionConfig } = mention;
+ const dataStream = `@${mention.label}`;
const body: IDocumentBody = {
dataStream,
customRanges: [{
@@ -53,7 +52,7 @@ export const AddDocMentionCommand: ICommand<IAddDocMentionCommandParams> = {
wholeEntity: true,
properties: {
...mentionConfig,
- ...extra,
+ ...metadata,
},
}],
};
diff --git a/packages/docs-mention-ui/src/controllers/doc-mention-trigger.controller.ts b/packages/docs-mention-ui/src/controllers/doc-mention-trigger.controller.ts
index 0b681f593a..a47c412b3a 100644
--- a/packages/docs-mention-ui/src/controllers/doc-mention-trigger.controller.ts
+++ b/packages/docs-mention-ui/src/controllers/doc-mention-trigger.controller.ts
@@ -41,10 +41,12 @@ export class DocMentionTriggerController extends Disposable {
const params = commandInfo.params as IInsertCommandParams;
const activeRange = this._textSelectionManagerService.getActiveTextRange();
if (params.body.dataStream === '@' && activeRange && !Tools.isDefine(this._docMentionService.editing)) {
- this._commandService.executeCommand(ShowMentionEditPopupOperation.id, {
- startIndex: activeRange.startOffset - 1,
- unitId: params.unitId,
- });
+ setTimeout(() => {
+ this._commandService.executeCommand(ShowMentionEditPopupOperation.id, {
+ startIndex: activeRange.startOffset - 1,
+ unitId: params.unitId,
+ });
+ }, 100);
}
}
diff --git a/packages/docs-mention-ui/src/services/doc-mention-popup.service.ts b/packages/docs-mention-ui/src/services/doc-mention-popup.service.ts
index 9e3c5024d8..34d6c76c30 100644
--- a/packages/docs-mention-ui/src/services/doc-mention-popup.service.ts
+++ b/packages/docs-mention-ui/src/services/doc-mention-popup.service.ts
@@ -28,7 +28,7 @@ export class DocMentionPopupService extends Disposable {
return this._infoPopup$.value;
}
- private readonly _editPopup$ = new BehaviorSubject<Nullable<{ anchor: number; popup: IDisposable }>>(undefined);
+ private readonly _editPopup$ = new BehaviorSubject<Nullable<{ anchor: number; popup: IDisposable; unitId: string }>>(undefined);
readonly editPopup$ = this._editPopup$.asObservable();
get editPopup() {
return this._editPopup$.value;
@@ -66,7 +66,7 @@ export class DocMentionPopupService extends Disposable {
},
unitId
);
- this._editPopup$.next({ popup: dispose, anchor: index });
+ this._editPopup$.next({ popup: dispose, anchor: index, unitId });
}
closeEditPopup() {
diff --git a/packages/docs-mention-ui/src/views/mention-edit-popup/index.tsx b/packages/docs-mention-ui/src/views/mention-edit-popup/index.tsx
index 220e16da11..91be5187e2 100644
--- a/packages/docs-mention-ui/src/views/mention-edit-popup/index.tsx
+++ b/packages/docs-mention-ui/src/views/mention-edit-popup/index.tsx
@@ -14,42 +14,40 @@
* limitations under the License.
*/
-import { ICommandService, IUniverInstanceService, Tools, UniverInstanceType, useDependency, useObservable, UserManagerService } from '@univerjs/core';
-import { DocSelectionRenderService } from '@univerjs/docs-ui';
-import { IRenderManagerService } from '@univerjs/engine-render';
-import React, { useEffect } from 'react';
+import type { DocumentDataModel, ITypeMentionList } from '@univerjs/core';
+import { ICommandService, IMentionIOService, IUniverInstanceService, Tools, UniverInstanceType, useDependency, useObservable } from '@univerjs/core';
+import { DocSelectionManagerService } from '@univerjs/docs';
+import React, { useEffect, useMemo, useState } from 'react';
+import { filter } from 'rxjs';
import { AddDocMentionCommand } from '../../commands/commands/doc-mention.command';
import { DocMentionPopupService } from '../../services/doc-mention-popup.service';
-import { MentionType } from '../../types/enums/mention-type';
import { MentionList } from '../mention-list';
-import type { IMention } from '../../types/interfaces/i-mention';
export const MentionEditPopup = () => {
const popupService = useDependency(DocMentionPopupService);
const commandService = useDependency(ICommandService);
const univerInstanceService = useDependency(IUniverInstanceService);
const editPopup = useObservable(popupService.editPopup$);
- const userService = useDependency(UserManagerService);
+ const mentionIOService = useDependency(IMentionIOService);
+ const documentDataModel = editPopup ? univerInstanceService.getUnit<DocumentDataModel>(editPopup.unitId) : null;
+ const textSelectionService = useDependency(DocSelectionManagerService);
+ const [mentions, setMentions] = useState<ITypeMentionList[]>([]);
+ const textSelection$ = useMemo(() =>
+ textSelectionService.textSelection$.pipe(
+ filter((selection) => selection.unitId === editPopup?.unitId)
+ ), [textSelectionService.textSelection$, editPopup]);
- const mentions: IMention[] = userService.list().map((user) => ({
- objectId: user.userID,
- label: user.name,
- objectType: MentionType.PERSON,
- extra: {
- icon: user.avatar,
- },
- }));
-
- const renderManagerService = useDependency(IRenderManagerService);
-
- const docSelectionRenderService = renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_DOC)?.with(DocSelectionRenderService);
+ const textSelection = useObservable(textSelection$);
+ const search = editPopup ? documentDataModel?.getBody()?.dataStream.slice(editPopup.anchor, textSelection?.textRanges[0].startOffset) : '';
useEffect(() => {
- docSelectionRenderService?.blur();
- return () => {
- docSelectionRenderService?.focus();
- };
- }, [docSelectionRenderService]);
+ (async () => {
+ if (editPopup) {
+ const res = await mentionIOService.list({ unitId: editPopup.unitId, search });
+ setMentions(res.list);
+ }
+ })();
+ }, [mentionIOService, editPopup, search]);
if (!editPopup) {
return null;
diff --git a/packages/docs-mention-ui/src/views/mention-list/index.module.less b/packages/docs-mention-ui/src/views/mention-list/index.module.less
index fb57468153..63690c4136 100644
--- a/packages/docs-mention-ui/src/views/mention-list/index.module.less
+++ b/packages/docs-mention-ui/src/views/mention-list/index.module.less
@@ -16,6 +16,7 @@
align-items: center;
flex-direction: row;
cursor: pointer;
+ border-radius: var(--border-radius-base);
&-icon {
width: 24px;
@@ -24,10 +25,10 @@
margin-right: 6px;
flex: 0 0 auto;
border-radius: 6px;
+ }
- &-active {
- background: rgb(var(--grey-50));
- }
+ &-active {
+ background: rgb(var(--grey-50));
}
&-icon:hover {
diff --git a/packages/docs-mention-ui/src/views/mention-list/index.tsx b/packages/docs-mention-ui/src/views/mention-list/index.tsx
index d916ce565e..2912f77102 100644
--- a/packages/docs-mention-ui/src/views/mention-list/index.tsx
+++ b/packages/docs-mention-ui/src/views/mention-list/index.tsx
@@ -14,14 +14,13 @@
* limitations under the License.
*/
-import React, { useEffect, useRef, useState } from 'react';
+import type { IMention, ITypeMentionList } from '@univerjs/core';
import cs from 'clsx';
-import { KeyCode } from '@univerjs/ui';
-import type { IMention } from '../../types/interfaces/i-mention';
+import React, { useRef, useState } from 'react';
import styles from './index.module.less';
export interface IMentionListProps {
- mentions: IMention[];
+ mentions: ITypeMentionList[];
active?: string;
onSelect?: (item: IMention) => void;
onClick?: () => void;
@@ -30,40 +29,31 @@ export interface IMentionListProps {
export const MentionList = (props: IMentionListProps) => {
const { mentions, active, onSelect, onClick } = props;
const ref = useRef<HTMLDivElement>(null);
- const [activeId, setActiveId] = useState(active ?? mentions[0]?.objectId);
+ const [activeId, setActiveId] = useState(active ?? mentions[0]?.mentions[0]?.objectId);
const handleSelect = (item: IMention) => {
onSelect?.(item);
};
- useEffect(() => {
- ref.current?.focus();
- }, []);
-
- const handleKeyDown = (evt: React.KeyboardEvent<HTMLDivElement>) => {
- const index = mentions.findIndex((i) => i.objectId === activeId);
- if (evt.keyCode === KeyCode.ARROW_UP) {
- setActiveId(mentions[index - 1]?.objectId);
- }
-
- if (evt.keyCode === KeyCode.ARROW_DOWN) {
- setActiveId(mentions[index + 1]?.objectId);
- }
-
- if (evt.keyCode === KeyCode.ENTER && mentions[index]) {
- handleSelect(mentions[index]);
- }
- };
+ // useEffect(() => {
+ // ref.current?.focus();
+ // }, []);
return (
- <div ref={ref} tabIndex={0} className={styles.docMentionPanel} onKeyDown={handleKeyDown} onClick={onClick}>
- {mentions.map((mention) => (
- <div
- key={mention.objectId}
- className={cs(styles.docMention, { [styles.docMentionActive]: activeId === mention.objectId })}
- onClick={() => handleSelect(mention)}
- >
- <img className={styles.docMentionIcon} src={mention.extra?.icon} />
- <div className={styles.docMentionLabel}>{mention.label}</div>
+ <div ref={ref} tabIndex={0} className={styles.docMentionPanel} onClick={onClick}>
+ {mentions.map((typeMentions) => (
+ <div key={typeMentions.type}>
+ <div className={styles.docMentionType}>{typeMentions.title}</div>
+ {typeMentions.mentions.map((mention) => (
+ <div
+ key={mention.objectId}
+ className={cs(styles.docMention, { [styles.docMentionActive]: activeId === mention.objectId })}
+ onClick={() => handleSelect(mention)}
+ onMouseEnter={() => setActiveId(mention.objectId)}
+ >
+ <img className={styles.docMentionIcon} src={mention.metadata?.icon as string} />
+ <div className={styles.docMentionLabel}>{mention.label}</div>
+ </div>
+ ))}
</div>
))}
</div>
diff --git a/packages/docs-ui/src/basics/custom-decoration-factory.ts b/packages/docs-ui/src/basics/custom-decoration-factory.ts
index 9458407b3e..428c99d9fb 100644
--- a/packages/docs-ui/src/basics/custom-decoration-factory.ts
+++ b/packages/docs-ui/src/basics/custom-decoration-factory.ts
@@ -37,7 +37,7 @@ export function addCustomDecorationFactory(param: IAddCustomDecorationParam) {
unitId,
actions: [],
textRanges: undefined,
- noHistory: true,
+ // noHistory: true,
segmentId,
},
};
@@ -109,7 +109,7 @@ export function deleteCustomDecorationFactory(accessor: IAccessor, params: IDele
unitId,
actions: [],
textRanges: undefined,
- noHistory: true,
+ // noHistory: true,
segmentId,
},
};
diff --git a/packages/sheets-thread-comment-ui/README.md b/packages/sheets-thread-comment-ui/README.md
index 893f74bd2a..4f53928aa4 100644
--- a/packages/sheets-thread-comment-ui/README.md
+++ b/packages/sheets-thread-comment-ui/README.md
@@ -57,9 +57,7 @@ class CustomMentionDataService implements IThreadCommentMentionDataService {
}
}
-univer.registerPlugin(UniverSheetsThreadCommentPlugin, {
- overrides: [[IThreadCommentMentionDataService, { useClass: CustomMentionDataService }]],
-});
+univer.registerPlugin(UniverSheetsThreadCommentPlugin);
```
### API
diff --git a/packages/sheets-thread-comment-ui/src/index.ts b/packages/sheets-thread-comment-ui/src/index.ts
index da4ed6e522..5e56084e0b 100644
--- a/packages/sheets-thread-comment-ui/src/index.ts
+++ b/packages/sheets-thread-comment-ui/src/index.ts
@@ -20,10 +20,8 @@ export { SheetsThreadCommentPopupService } from './services/sheets-thread-commen
export { SHEETS_THREAD_COMMENT } from './types/const';
export { IThreadCommentDataSourceService } from '@univerjs/thread-comment';
export { UniverThreadCommentUIPlugin } from '@univerjs/thread-comment-ui';
-export { IThreadCommentMentionDataService } from '@univerjs/thread-comment-ui';
// #region - all commands
-
export {
AddCommentCommand,
DeleteCommentCommand,
diff --git a/packages/thread-comment-ui/src/index.ts b/packages/thread-comment-ui/src/index.ts
index 6d8971cbf2..26d95fcb60 100644
--- a/packages/thread-comment-ui/src/index.ts
+++ b/packages/thread-comment-ui/src/index.ts
@@ -21,7 +21,6 @@ export type { IThreadCommentPanelProps } from './views/thread-comment-panel';
export { ThreadCommentTree } from './views/thread-comment-tree';
export type { IThreadCommentTreeProps } from './views/thread-comment-tree';
export { THREAD_COMMENT_PANEL } from './types/const';
-export { IThreadCommentMentionDataService, type IThreadCommentMentionDataSource } from './services/thread-comment-mention-data.service';
// #region - all commands
diff --git a/packages/thread-comment-ui/src/plugin.ts b/packages/thread-comment-ui/src/plugin.ts
index 73aff900f3..ec82fad83d 100644
--- a/packages/thread-comment-ui/src/plugin.ts
+++ b/packages/thread-comment-ui/src/plugin.ts
@@ -20,7 +20,6 @@ import { DependentOn, ICommandService, IConfigService, Inject, Injector, mergeOv
import { UniverThreadCommentPlugin } from '@univerjs/thread-comment';
import { SetActiveCommentOperation, ToggleSheetCommentPanelOperation } from './commands/operations/comment.operations';
import { defaultPluginConfig, PLUGIN_CONFIG_KEY } from './controllers/config.schema';
-import { IThreadCommentMentionDataService, ThreadCommentMentionDataService } from './services/thread-comment-mention-data.service';
import { ThreadCommentPanelService } from './services/thread-comment-panel.service';
import { PLUGIN_NAME } from './types/const';
@@ -48,7 +47,6 @@ export class UniverThreadCommentUIPlugin extends Plugin {
override onStarting(): void {
(mergeOverrideWithDependencies([
[ThreadCommentPanelService],
- [IThreadCommentMentionDataService, { useClass: ThreadCommentMentionDataService }],
], this._config?.overrides) as Dependency[]).forEach((dep) => {
this._injector.add(dep);
});
diff --git a/packages/thread-comment-ui/src/services/thread-comment-mention-data.service.ts b/packages/thread-comment-ui/src/services/thread-comment-mention-data.service.ts
deleted file mode 100644
index 668312059b..0000000000
--- a/packages/thread-comment-ui/src/services/thread-comment-mention-data.service.ts
+++ /dev/null
@@ -1,49 +0,0 @@
-/**
- * Copyright 2023-present DreamNum Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import type { Nullable } from '@univerjs/core';
-import type { MentionProps } from '@univerjs/design';
-import type { IThreadCommentMention } from '@univerjs/thread-comment';
-import { createIdentifier } from '@univerjs/core';
-
-export interface IThreadCommentMentionDataSource {
- getMentions(search: string, unitId: string, subUnitId: string): Promise<IThreadCommentMention[]>;
-}
-
-export interface IThreadCommentMentionDataService {
- dataSource: Nullable<IThreadCommentMentionDataSource>;
-
- getMentions: (search: string, unitId: string, subUnitId: string) => Promise<IThreadCommentMention[]>;
- trigger: string;
- renderSuggestion?: MentionProps['renderSuggestion'];
-}
-
-export class ThreadCommentMentionDataService implements IThreadCommentMentionDataService {
- dataSource: Nullable<IThreadCommentMentionDataSource>;
-
- renderSuggestion: MentionProps['renderSuggestion'];
-
- async getMentions(search: string, unitId: string, subUnitId: string) {
- if (this.dataSource) {
- return this.dataSource.getMentions(search, unitId, subUnitId);
- }
- return [];
- }
-
- trigger = '@';
-}
-
-export const IThreadCommentMentionDataService = createIdentifier<IThreadCommentMentionDataService>('thread-comment.mention-data.service');
diff --git a/packages/thread-comment-ui/src/views/thread-comment-editor/index.tsx b/packages/thread-comment-ui/src/views/thread-comment-editor/index.tsx
index 24b31ae2f2..85ac407ffb 100644
--- a/packages/thread-comment-ui/src/views/thread-comment-editor/index.tsx
+++ b/packages/thread-comment-ui/src/views/thread-comment-editor/index.tsx
@@ -17,16 +17,15 @@
import type { IDocumentBody } from '@univerjs/core';
import type { MentionProps } from '@univerjs/design';
import type { IThreadComment } from '@univerjs/thread-comment';
-import { ICommandService, LocaleService, UniverInstanceType, useDependency } from '@univerjs/core';
+import { ICommandService, IMentionIOService, LocaleService, UniverInstanceType, useDependency } from '@univerjs/core';
import { Button, Mention, Mentions } from '@univerjs/design';
import { DocSelectionManagerService } from '@univerjs/docs';
import { DocSelectionRenderService } from '@univerjs/docs-ui';
import { IRenderManagerService } from '@univerjs/engine-render';
import React, { forwardRef, useImperativeHandle, useRef, useState } from 'react';
import { SetActiveCommentOperation } from '../../commands/operations/comment.operations';
-import { IThreadCommentMentionDataService } from '../../services/thread-comment-mention-data.service';
import styles from './index.module.less';
-import { parseMentions, transformDocument2TextNodes, transformMention, transformTextNode2Text, transformTextNodes2Document } from './util';
+import { parseMentions, transformDocument2TextNodes, transformTextNode2Text, transformTextNodes2Document } from './util';
export interface IThreadCommentEditorProps {
id?: string;
@@ -55,8 +54,8 @@ const defaultRenderSuggestion: MentionProps['renderSuggestion'] = (mention, sear
};
export const ThreadCommentEditor = forwardRef<IThreadCommentEditorInstance, IThreadCommentEditorProps>((props, ref) => {
- const { comment, onSave, id, onCancel, autoFocus, unitId, subUnitId } = props;
- const mentionDataService = useDependency(IThreadCommentMentionDataService);
+ const { comment, onSave, id, onCancel, autoFocus, unitId } = props;
+ const mentionIOService = useDependency(IMentionIOService);
const commandService = useDependency(ICommandService);
const localeService = useDependency(LocaleService);
const [localComment, setLocalComment] = useState({ ...comment });
@@ -114,13 +113,23 @@ export const ThreadCommentEditor = forwardRef<IThreadCommentEditorInstance, IThr
}}
>
<Mention
- key={mentionDataService.trigger}
- trigger={mentionDataService.trigger}
- data={(query, callback) => mentionDataService.getMentions!(query, unitId, subUnitId)
- .then((res) => res.map(transformMention))
+ key="@"
+ trigger="@"
+ data={(query, callback) => mentionIOService.list({ search: query, unitId })
+ .then((res) => res.list.map(
+ (typeMentions) => (
+ typeMentions.mentions.map(
+ (mention) => ({
+ id: mention.objectId,
+ display: mention.label,
+ raw: mention,
+ })
+ )
+ )
+ ).flat())
.then(callback) as any}
displayTransform={(id, label) => `@${label} `}
- renderSuggestion={mentionDataService.renderSuggestion ?? defaultRenderSuggestion}
+ renderSuggestion={defaultRenderSuggestion}
/>
</Mentions>