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.

🗑️ chore(chat-input): drop the editor root lifecycle workaround (#19431)

Inneiafcf9dc73b87
diff --git a/src/features/ChatInput/ChatInputProvider.tsx b/src/features/ChatInput/ChatInputProvider.tsx
index 31458a6fdf7..721b7270700 100644
--- a/src/features/ChatInput/ChatInputProvider.tsx
+++ b/src/features/ChatInput/ChatInputProvider.tsx
@@ -7,7 +7,6 @@ import { createStore, Provider } from './store';
import { DEFAULT_CHAT_INPUT_FEATURE } from './store/initialState';
import type { StoreUpdaterProps } from './StoreUpdater';
import StoreUpdater from './StoreUpdater';
-import { useEditorRootLifecycle } from './useEditorRootLifecycle';
interface ChatInputProviderProps extends StoreUpdaterProps {
children: ReactNode;
@@ -39,7 +38,6 @@ export const ChatInputProvider = memo<ChatInputProviderProps>(
}) => {
const editor = useEditor();
const slashMenuRef = useRef<HTMLDivElement>(null);
- useEditorRootLifecycle(editor);
return (
<Provider
diff --git a/src/features/ChatInput/useEditorRootLifecycle.test.ts b/src/features/ChatInput/useEditorRootLifecycle.test.ts
deleted file mode 100644
index d411562c368..00000000000
--- a/src/features/ChatInput/useEditorRootLifecycle.test.ts
+++ /dev/null
@@ -1,174 +0,0 @@
-// @vitest-environment happy-dom
-
-import type { IEditor } from '@lobehub/editor';
-import { getKernelFromEditorConfig } from '@lobehub/editor';
-import { Editor, useEditor } from '@lobehub/editor/react';
-import { render, waitFor } from '@testing-library/react';
-import { Activity, createElement, type Ref } from 'react';
-import { describe, expect, it, vi } from 'vitest';
-
-import { useEditorRootLifecycle } from './useEditorRootLifecycle';
-
-const Probe = ({ editor, rootRef }: { editor: IEditor; rootRef: Ref<HTMLDivElement> }) => {
- useEditorRootLifecycle(editor);
- return createElement('div', { ref: rootRef });
-};
-
-describe('useEditorRootLifecycle', () => {
- it('restores a hidden root to its owning editor and destroys it only after unmount', async () => {
- let ownerRoot: HTMLElement | null = null;
- let replacementRoot: HTMLElement | null = document.createElement('div');
- const destroy = vi.fn();
- const ownerSetRootElement = vi.fn((root: HTMLElement | null) => {
- ownerRoot = root;
- });
- const ownerLexicalEditor = {
- getRootElement: vi.fn(() => ownerRoot),
- setRootElement: ownerSetRootElement,
- };
- const replacementSetRootElement = vi.fn((root: HTMLElement | null) => {
- replacementRoot = root;
- });
- const replacementLexicalEditor = {
- getRootElement: vi.fn(() => replacementRoot),
- setRootElement: replacementSetRootElement,
- };
- const editor = {
- destroy,
- getLexicalEditor: vi
- .fn()
- .mockReturnValueOnce(ownerLexicalEditor)
- .mockReturnValue(replacementLexicalEditor),
- } as unknown as IEditor;
- const rootRef = (root: HTMLDivElement | null) => {
- if (root) ownerRoot = root;
- };
- const tree = (mode: 'hidden' | 'visible') =>
- createElement(Activity, { children: createElement(Probe, { editor, rootRef }), mode });
-
- const { rerender, unmount } = render(tree('visible'));
- const element = ownerRoot;
-
- rerender(tree('hidden'));
- expect(ownerSetRootElement).toHaveBeenLastCalledWith(null);
- expect(destroy).not.toHaveBeenCalled();
-
- rerender(tree('visible'));
- expect(ownerSetRootElement).toHaveBeenLastCalledWith(element);
- expect(replacementSetRootElement).not.toHaveBeenCalled();
- expect(editor.getLexicalEditor).toHaveBeenCalledOnce();
-
- rerender(tree('hidden'));
- unmount();
- expect(ownerSetRootElement).toHaveBeenLastCalledWith(null);
- await waitFor(() => expect(destroy).toHaveBeenCalledOnce());
- });
-});
-
-const isRegistered = (editor: IEditor) => {
- const theme = (editor.getLexicalEditor() as any)?._config?.theme;
- return !!theme && getKernelFromEditorConfig({ theme } as any) === (editor as any);
-};
-
-const rootOf = (editor: IEditor) => editor.getLexicalEditor()!.getRootElement();
-
-const renderRealEditor = ({ lifecycle }: { lifecycle: boolean }) => {
- let captured!: IEditor;
- const Host = () => {
- const editor = useEditor();
- captured = editor;
- useEditorRootLifecycle(lifecycle ? editor : ({ getLexicalEditor: () => null } as IEditor));
- return createElement(Editor, { content: '', editor, type: 'text' });
- };
- const tree = (mode: 'hidden' | 'visible') =>
- createElement(Activity, { children: createElement(Host), mode });
- return { ...render(tree('visible')), editor: () => captured, tree };
-};
-
-describe('useEditorRootLifecycle with a real editor', () => {
- it('leaves the kernel alive when the lifecycle is not applied', async () => {
- const { editor, unmount } = renderRealEditor({ lifecycle: false });
- await waitFor(() => expect(rootOf(editor())).toBeTruthy());
- const destroy = vi.spyOn(editor(), 'destroy');
-
- unmount();
- await new Promise((resolve) => setTimeout(resolve, 20));
- // Whether an orphaned kernel lingers in the global registry is the editor
- // library's concern (>= 4.25.1 unregisters it on root detach); what the
- // lifecycle adds is destroying it, so that is the observable difference.
- expect(destroy).not.toHaveBeenCalled();
- expect(editor().getLexicalEditor()).not.toBeNull();
- });
-
- it('destroys the kernel on a direct visible unmount', async () => {
- const { editor, unmount } = renderRealEditor({ lifecycle: true });
- await waitFor(() => expect(rootOf(editor())).toBeTruthy());
-
- unmount();
- await waitFor(() => expect(isRegistered(editor())).toBe(false));
- });
-
- it('detaches the root but keeps the kernel while hidden', async () => {
- const { editor, rerender, tree } = renderRealEditor({ lifecycle: true });
- await waitFor(() => expect(rootOf(editor())).toBeTruthy());
- const root = rootOf(editor());
- const lexicalEditor = editor().getLexicalEditor();
- const destroy = vi.spyOn(editor(), 'destroy');
-
- rerender(tree('hidden'));
- expect(rootOf(editor())).toBeNull();
- expect(root!.isConnected).toBe(true);
- // Registry membership while hidden depends on the editor version (>= 4.25.1
- // unregisters until the root is re-attached, see the "revealed" case); the
- // hook's guarantee is that the kernel itself survives hiding.
- expect(editor().getLexicalEditor()).toBe(lexicalEditor);
- expect(destroy).not.toHaveBeenCalled();
- });
-
- it('restores the same root to the same kernel when revealed', async () => {
- const { editor, rerender, tree } = renderRealEditor({ lifecycle: true });
- await waitFor(() => expect(rootOf(editor())).toBeTruthy());
- const root = rootOf(editor());
- const lexicalEditor = editor().getLexicalEditor();
-
- rerender(tree('hidden'));
- rerender(tree('visible'));
- expect(rootOf(editor())).toBe(root);
- expect(editor().getLexicalEditor()).toBe(lexicalEditor);
- expect(isRegistered(editor())).toBe(true);
- });
-
- it('destroys the kernel when a hidden tree unmounts', async () => {
- const { editor, rerender, tree, unmount } = renderRealEditor({ lifecycle: true });
- await waitFor(() => expect(rootOf(editor())).toBeTruthy());
-
- rerender(tree('hidden'));
- unmount();
- await waitFor(() => expect(isRegistered(editor())).toBe(false));
- });
-
- it('destroys the kernel when the root is detached by a ref cleanup before the effect', async () => {
- let root: HTMLElement | null = null;
- const destroy = vi.fn();
- const lexicalEditor = {
- getRootElement: () => root,
- setRootElement: (next: HTMLElement | null) => {
- root = next;
- },
- };
- const editor = { destroy, getLexicalEditor: () => lexicalEditor } as unknown as IEditor;
- const Host = () => {
- useEditorRootLifecycle(editor);
- return createElement('div', {
- ref: (node: HTMLElement | null) => {
- lexicalEditor.setRootElement(node);
- return () => lexicalEditor.setRootElement(null);
- },
- });
- };
-
- const { unmount } = render(createElement(Host));
- unmount();
- await waitFor(() => expect(destroy).toHaveBeenCalledOnce());
- });
-});
diff --git a/src/features/ChatInput/useEditorRootLifecycle.ts b/src/features/ChatInput/useEditorRootLifecycle.ts
deleted file mode 100644
index f241a125b44..00000000000
--- a/src/features/ChatInput/useEditorRootLifecycle.ts
+++ /dev/null
@@ -1,56 +0,0 @@
-import type { IEditor } from '@lobehub/editor';
-import { useEffect, useRef } from 'react';
-
-/**
- * React Activity disconnects effects while preserving hidden DOM. Detach the
- * Lexical root while inactive, then reconnect the same element when visible.
- */
-export const useEditorRootLifecycle = (editor: IEditor) => {
- const disconnectObserverRef = useRef<MutationObserver | null>(null);
- const destroyedEditorRef = useRef<IEditor | null>(null);
- const rootRef = useRef<HTMLElement | null>(null);
- const inactiveEditorRef = useRef<{
- lexicalEditor: NonNullable<ReturnType<IEditor['getLexicalEditor']>>;
- root: HTMLElement;
- } | null>(null);
-
- useEffect(() => {
- disconnectObserverRef.current?.disconnect();
- disconnectObserverRef.current = null;
-
- const inactiveEditor = inactiveEditorRef.current;
- const lexicalEditor = inactiveEditor?.lexicalEditor ?? editor.getLexicalEditor();
- if (!lexicalEditor) return;
-
- if (inactiveEditor) {
- lexicalEditor.setRootElement(inactiveEditor.root);
- inactiveEditorRef.current = null;
- }
- rootRef.current = lexicalEditor.getRootElement();
-
- return () => {
- // The root is read from our own ref as well, because a future editor
- // release may detach it from a ref cleanup, which React runs before this
- // passive cleanup — leaving getRootElement() already null.
- const root = lexicalEditor.getRootElement() ?? rootRef.current;
- lexicalEditor.setRootElement(null);
- rootRef.current = null;
-
- if (!root) return;
- inactiveEditorRef.current = { lexicalEditor, root };
-
- const destroyWhenDisconnected = () => {
- if (root.isConnected || destroyedEditorRef.current === editor) return;
- destroyedEditorRef.current = editor;
- observer.disconnect();
- if (disconnectObserverRef.current === observer) disconnectObserverRef.current = null;
- inactiveEditorRef.current = null;
- editor.destroy();
- };
- const observer = new MutationObserver(destroyWhenDisconnected);
- observer.observe(root.ownerDocument, { childList: true, subtree: true });
- disconnectObserverRef.current = observer;
- queueMicrotask(destroyWhenDisconnected);
- };
- }, [editor]);
-};