Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(editor): open the @ member menu when members load after mount (#23731)

Rdmclin2e04d50f5bad0
diff --git a/src/features/EditorCanvas/InternalEditor.test.tsx b/src/features/EditorCanvas/InternalEditor.test.tsx
index ffaff335312..15bab56b535 100644
--- a/src/features/EditorCanvas/InternalEditor.test.tsx
+++ b/src/features/EditorCanvas/InternalEditor.test.tsx
@@ -1,9 +1,9 @@
/**
* @vitest-environment happy-dom
*/
-import { type IEditor } from '@lobehub/editor';
-import { moment } from '@lobehub/editor';
-import { useEditor } from '@lobehub/editor/react';
+import { type IEditor, type ISlashMenuOption, type ISlashService } from '@lobehub/editor';
+import { genServiceId, moment } from '@lobehub/editor';
+import { type EditorProps, useEditor } from '@lobehub/editor/react';
import { act, cleanup, render, waitFor } from '@testing-library/react';
import { memo, useEffect, useRef } from 'react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -852,4 +852,71 @@ describe('InternalEditor', () => {
);
});
});
+
+ describe('mention menu', () => {
+ // The package exports the service type but not its id; ids are keyed by name.
+ const SLASH_SERVICE_ID = genServiceId<ISlashService>('SlashService');
+ const member: ISlashMenuOption = { key: 'member-lin', label: 'Lin' };
+ const mentionOptionWith = (items: ISlashMenuOption[]): EditorProps['mentionOption'] => ({
+ items,
+ onSelect: vi.fn(),
+ });
+
+ // Workspace members load asynchronously, so a comment box usually mounts
+ // before they arrive. The kernel only instantiates plugins that registered
+ // before it initialised: the `@` menu must already be live at mount and
+ // pick up the members once they land.
+ it('serves members that arrive after the editor mounted', async () => {
+ let editor: IEditor | undefined;
+ const onEditorReady = (instance: IEditor) => {
+ editor = instance;
+ };
+
+ const { rerender } = render(
+ <TestWrapper
+ floatingToolbar={false}
+ mentionOption={mentionOptionWith([])}
+ onEditorReady={onEditorReady}
+ />,
+ );
+
+ await waitFor(() => expect(editor?.getLexicalEditor?.()).toBeTruthy());
+
+ rerender(
+ <TestWrapper
+ floatingToolbar={false}
+ mentionOption={mentionOptionWith([member])}
+ onEditorReady={onEditorReady}
+ />,
+ );
+
+ await waitFor(() =>
+ expect(editor!.requireService(SLASH_SERVICE_ID)?.getSlashOptions('@')?.items).toEqual([
+ member,
+ ]),
+ );
+ });
+
+ it('keeps the slash menu next to the mention menu', async () => {
+ let editor: IEditor | undefined;
+ const slashItem: ISlashMenuOption = { key: 'h1', label: 'Heading 1' };
+
+ render(
+ <TestWrapper
+ floatingToolbar={false}
+ mentionOption={mentionOptionWith([member])}
+ slashItems={[slashItem]}
+ onEditorReady={(instance) => {
+ editor = instance;
+ }}
+ />,
+ );
+
+ await waitFor(() => expect(editor?.getLexicalEditor?.()).toBeTruthy());
+
+ const service = editor!.requireService(SLASH_SERVICE_ID);
+ expect(service?.getSlashOptions('/')?.items).toEqual([slashItem]);
+ expect(service?.getSlashOptions('@')?.items).toEqual([member]);
+ });
+ });
});
diff --git a/src/features/EditorCanvas/InternalEditor.tsx b/src/features/EditorCanvas/InternalEditor.tsx
index 5a84b952ddc..527685afe48 100644
--- a/src/features/EditorCanvas/InternalEditor.tsx
+++ b/src/features/EditorCanvas/InternalEditor.tsx
@@ -11,6 +11,8 @@ import {
ReactLiteXmlPlugin,
ReactMathPlugin,
ReactMentionPlugin,
+ ReactSlashOption,
+ ReactSlashPlugin,
ReactTablePlugin,
ReactToolbarPlugin,
ReactVideoPlugin,
@@ -273,6 +275,36 @@ const InternalEditor = memo<InternalEditorProps>(
[allowContentBleed, disabled],
);
+ // The `/` and `@` menus. `<Editor>` only mounts its slash plugin once a
+ // menu has items, but the kernel instantiates a plugin only if it
+ // registered before the editor initialised — so `@` members that load
+ // after mount (the usual case for a comment box) would never open a menu.
+ // Own the plugin here instead: it registers at mount whenever either menu
+ // is configured, and later items reach it as an options update.
+ const slashPlugin = useMemo(() => {
+ if (!slashItems && !mentionOption) return;
+
+ // The member chip's markdown writer is pinned on the mention plugin in
+ // STATIC_PLUGINS; the menu only takes the search and select options.
+ const { markdownWriter: _markdownWriter, ...mentionMenu } = mentionOption ?? {};
+
+ return Editor.withProps(ReactSlashPlugin, {
+ children: [
+ slashItems && <ReactSlashOption items={slashItems} key="/" maxLength={8} trigger="/" />,
+ mentionOption && (
+ <ReactSlashOption
+ key="@"
+ maxLength={8}
+ trigger="@"
+ {...mentionMenu}
+ items={mentionMenu.items ?? []}
+ />
+ ),
+ ],
+ getPopupContainer,
+ });
+ }, [getPopupContainer, mentionOption, slashItems]);
+
// Build plugins array
const plugins = useMemo(() => {
// If custom plugins provided, use them directly
@@ -299,10 +331,18 @@ const InternalEditor = memo<InternalEditorProps>(
const mathPlugin = Editor.withProps(ReactMathPlugin, { enableInlineMath });
- // Build base plugins with optional extra plugins prepended
- const basePlugins = extraPlugins
- ? [...extraPlugins, ...STATIC_PLUGINS, mathPlugin, imagePlugin, videoPlugin, filePlugin]
- : [...STATIC_PLUGINS, mathPlugin, imagePlugin, videoPlugin, filePlugin];
+ // Build base plugins with optional extra plugins prepended. The slash
+ // plugin leads so its element keeps a stable position (plugins are
+ // keyed by index) when `extraPlugins` grows, e.g. once collab attaches.
+ const basePlugins = [
+ ...(slashPlugin ? [slashPlugin] : []),
+ ...(extraPlugins ?? []),
+ ...STATIC_PLUGINS,
+ mathPlugin,
+ imagePlugin,
+ videoPlugin,
+ filePlugin,
+ ];
if (!floatingToolbar || disabled) return basePlugins;
@@ -360,6 +400,7 @@ const InternalEditor = memo<InternalEditorProps>(
handlePickImage,
handlePickVideo,
readonlySelectionItems,
+ slashPlugin,
toolbarExtraItems,
]);
@@ -503,10 +544,12 @@ const InternalEditor = memo<InternalEditorProps>(
editable={editable && !disabled}
editor={editor}
getPopupContainer={getPopupContainer}
- mentionOption={mentionOption}
+ // Custom plugins replace the built-in set, slash plugin included,
+ // so those callers keep `<Editor>`'s own menus.
+ mentionOption={customPlugins ? mentionOption : undefined}
placeholder={finalPlaceholder}
plugins={plugins}
- slashOption={slashItems ? { items: slashItems } : undefined}
+ slashOption={customPlugins && slashItems ? { items: slashItems } : undefined}
type={'text'}
style={{
paddingBottom: 32,