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.

✨ feat(page-editor): register video node and add Video slash item (#21925)

Innei538eb2c70a5a
diff --git a/locales/en-US/editor.json b/locales/en-US/editor.json
index 99a4fa0b8eb..5800b15a3b2 100644
--- a/locales/en-US/editor.json
+++ b/locales/en-US/editor.json
@@ -142,6 +142,7 @@
"slash.table": "Table",
"slash.tex": "TeX Formula",
"slash.translate": "Translate",
+ "slash.video": "Video",
"table.align": "Align",
"table.alignCenter": "Align center",
"table.alignLeft": "Align left",
diff --git a/locales/zh-CN/editor.json b/locales/zh-CN/editor.json
index a73a1591b49..38c6e7723f4 100644
--- a/locales/zh-CN/editor.json
+++ b/locales/zh-CN/editor.json
@@ -142,6 +142,7 @@
"slash.table": "表格",
"slash.tex": "TeX 公式",
"slash.translate": "翻译",
+ "slash.video": "视频",
"table.align": "对齐",
"table.alignCenter": "居中对齐",
"table.alignLeft": "左对齐",
diff --git a/package.json b/package.json
index d6291f7b015..260c4e5abb7 100644
--- a/package.json
+++ b/package.json
@@ -326,7 +326,7 @@
"@lobehub/analytics": "^1.6.4",
"@lobehub/charts": "^6.0.0",
"@lobehub/desktop-ipc-typings": "workspace:*",
- "@lobehub/editor": "7.0.4",
+ "@lobehub/editor": "7.1.0",
"@lobehub/icons": "^6.13.0",
"@lobehub/market-sdk": "0.43.0",
"@lobehub/ui": "^6.9.0",
diff --git a/packages/locales/src/default/editor.ts b/packages/locales/src/default/editor.ts
index d6f0fff7592..0d97ad2a017 100644
--- a/packages/locales/src/default/editor.ts
+++ b/packages/locales/src/default/editor.ts
@@ -70,6 +70,7 @@ export default {
'slash.table': 'Table',
'slash.tex': 'TeX Formula',
'slash.translate': 'Translate',
+ 'slash.video': 'Video',
'skillFrontmatter.edit': 'Edit metadata',
'skillFrontmatter.empty': 'No metadata',
'skillFrontmatter.invalid.descriptionInvalid': 'Description must be single-line text.',
diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml
index 7b52b9c4470..40392a30083 100644
--- a/pnpm-workspace.yaml
+++ b/pnpm-workspace.yaml
@@ -67,7 +67,7 @@ overrides:
'@lobehub/icons': ^6.0.0
# Exact: an editor release can move its lexical version, and the root pins
# lexical; lift both together.
- '@lobehub/editor': 7.0.4
+ '@lobehub/editor': 7.1.0
'@lobehub/charts': ^6.0.0
'@lobehub/fluent-emoji': ^5.0.0
# Stay on the latest 1.9.x: @opentelemetry/*@2.x peers reject 1.10.0, and mixing both splits drizzle-orm into duplicate peer variants.
diff --git a/src/features/EditorCanvas/InternalEditor.readonly.test.tsx b/src/features/EditorCanvas/InternalEditor.readonly.test.tsx
index 09613edeb5a..45168e8248f 100644
--- a/src/features/EditorCanvas/InternalEditor.readonly.test.tsx
+++ b/src/features/EditorCanvas/InternalEditor.readonly.test.tsx
@@ -7,6 +7,7 @@ import {
ReactChartPlugin,
ReactFootnotePlugin,
ReactToolbarPlugin,
+ ReactVideoPlugin,
} from '@lobehub/editor';
import { act, render, screen } from '@testing-library/react';
import { StrictMode } from 'react';
@@ -58,6 +59,7 @@ vi.mock('@lobehub/editor', () => ({
ReactMentionPlugin: vi.fn(),
ReactTablePlugin: vi.fn(),
ReactToolbarPlugin: vi.fn(),
+ ReactVideoPlugin: vi.fn(),
}));
vi.mock('@/features/ChatInput/InputEditor/plugins', () => ({
@@ -137,11 +139,16 @@ describe('InternalEditor readonly state', () => {
});
});
- it('registers the alert, footnote and chart nodes the document service can write', () => {
+ it('registers the alert, footnote, chart and video nodes the document service can write', () => {
render(<InternalEditor editor={editor} />);
expect(editorProps.last?.plugins).toEqual(
- expect.arrayContaining([ReactAlertPlugin, ReactChartPlugin, ReactFootnotePlugin]),
+ expect.arrayContaining([
+ ReactAlertPlugin,
+ ReactChartPlugin,
+ ReactFootnotePlugin,
+ ReactVideoPlugin,
+ ]),
);
});
diff --git a/src/features/EditorCanvas/InternalEditor.test.tsx b/src/features/EditorCanvas/InternalEditor.test.tsx
index ebfc5bd7cf1..508da253766 100644
--- a/src/features/EditorCanvas/InternalEditor.test.tsx
+++ b/src/features/EditorCanvas/InternalEditor.test.tsx
@@ -154,6 +154,37 @@ describe('InternalEditor', () => {
});
});
+ describe('video', () => {
+ it('reads a markdown <video> into a video node with the default plugins', async () => {
+ let editorInstance: IEditor | undefined;
+
+ render(
+ <TestWrapper
+ onEditorReady={(editor) => {
+ editorInstance = editor;
+ }}
+ />,
+ );
+
+ await waitFor(() => {
+ expect(editorInstance).toBeDefined();
+ });
+
+ await act(async () => {
+ editorInstance!.setDocument(
+ 'markdown',
+ '<video src="https://cdn.test/a.mp4" style="width: 50%" controls></video>',
+ );
+ await moment();
+ });
+
+ expect(JSON.stringify(editorInstance!.getDocument('json'))).toContain('"type":"video"');
+ expect(editorInstance!.getDocument('markdown')).toContain(
+ '<video src="https://cdn.test/a.mp4" style="width: 50%" controls></video>',
+ );
+ });
+ });
+
describe('onInit callback', () => {
it('should call onInit when editor initializes', async () => {
const onInit = vi.fn();
diff --git a/src/features/EditorCanvas/InternalEditor.tsx b/src/features/EditorCanvas/InternalEditor.tsx
index c4f0c6289fb..b15c95fd918 100644
--- a/src/features/EditorCanvas/InternalEditor.tsx
+++ b/src/features/EditorCanvas/InternalEditor.tsx
@@ -13,6 +13,7 @@ import {
ReactMentionPlugin,
ReactTablePlugin,
ReactToolbarPlugin,
+ ReactVideoPlugin,
} from '@lobehub/editor';
import { Editor, useEditorState } from '@lobehub/editor/react';
import { createStaticStyles } from '@lobehub/ui';
@@ -33,10 +34,19 @@ import { registerImageFilePastePreference } from './registerImageFilePastePrefer
import { needsImageRehost, rehostImage } from './rehostImage';
import { useFileUpload, useImageUpload } from './useImageUpload';
-const IMAGE_FILTERS = [
+interface FileFilter {
+ extensions: string[];
+ name: string;
+}
+
+const IMAGE_FILTERS: FileFilter[] = [
{ extensions: ['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'avif'], name: 'Images' },
];
+const VIDEO_FILTERS: FileFilter[] = [
+ { extensions: ['mp4', 'webm', 'mov', 'm4v', 'ogv'], name: 'Videos' },
+];
+
// Force the Lexical FileNode's outer `<span>` to render as its own block-
// level row inside the paragraph. The inner card visuals (icon + name + size
// + download button) live in `LinearFilePlugin`.
@@ -144,17 +154,17 @@ const InternalEditor = memo<InternalEditorProps>(
const handleImageUpload = useImageUpload();
const handleFileUpload = useFileUpload();
- const handlePickFile = useCallback(async (): Promise<File | null> => {
+ const pickDesktopFile = useCallback(async (filters: FileFilter[]): Promise<File | null> => {
if (!isDesktop) return null;
const { ensureElectronIpc } = await import('@/utils/electron/ipc');
const ipc = ensureElectronIpc();
- const result = await (ipc as any).localSystem.handlePickFile({
- filters: IMAGE_FILTERS,
- });
+ const result = await (ipc as any).localSystem.handlePickFile({ filters });
if (result.canceled || !result.file) return null;
const { data, mimeType, name } = result.file;
return new File([data], name, { type: mimeType });
}, []);
+ const handlePickImage = useCallback(() => pickDesktopFile(IMAGE_FILTERS), [pickDesktopFile]);
+ const handlePickVideo = useCallback(() => pickDesktopFile(VIDEO_FILTERS), [pickDesktopFile]);
const finalPlaceholder = placeholder || t('pageEditor.editorPlaceholder');
const wrapperStyle = useMemo<CSSProperties>(
@@ -181,7 +191,12 @@ const InternalEditor = memo<InternalEditorProps>(
handleUpload: handleImageUpload,
needRehost: (url: string) =>
!!editor.getLexicalEditor?.()?.isEditable() && needsImageRehost(url),
- onPickFile: isDesktop ? handlePickFile : undefined,
+ onPickFile: isDesktop ? handlePickImage : undefined,
+ });
+
+ const videoPlugin = Editor.withProps(ReactVideoPlugin, {
+ handleUpload: handleFileUpload,
+ onPickFile: isDesktop ? handlePickVideo : undefined,
});
const filePlugin = Editor.withProps(LinearFilePlugin, {
@@ -193,8 +208,8 @@ const InternalEditor = memo<InternalEditorProps>(
// Build base plugins with optional extra plugins prepended
const basePlugins = extraPlugins
- ? [...extraPlugins, ...STATIC_PLUGINS, mathPlugin, imagePlugin, filePlugin]
- : [...STATIC_PLUGINS, mathPlugin, imagePlugin, filePlugin];
+ ? [...extraPlugins, ...STATIC_PLUGINS, mathPlugin, imagePlugin, videoPlugin, filePlugin]
+ : [...STATIC_PLUGINS, mathPlugin, imagePlugin, videoPlugin, filePlugin];
if (!floatingToolbar || disabled) return basePlugins;
@@ -249,7 +264,8 @@ const InternalEditor = memo<InternalEditorProps>(
floatingToolbar,
handleFileUpload,
handleImageUpload,
- handlePickFile,
+ handlePickImage,
+ handlePickVideo,
readonlySelectionItems,
toolbarExtraItems,
]);
diff --git a/src/features/PageEditor/EditorCanvas/useSlashItems.test.ts b/src/features/PageEditor/EditorCanvas/useSlashItems.test.ts
index 2704f1f231c..af158a438cd 100644
--- a/src/features/PageEditor/EditorCanvas/useSlashItems.test.ts
+++ b/src/features/PageEditor/EditorCanvas/useSlashItems.test.ts
@@ -2,6 +2,7 @@ import {
INSERT_ALERT_COMMAND,
INSERT_CHART_COMMAND,
INSERT_FOOTNOTE_COMMAND,
+ INSERT_VIDEO_COMMAND,
type SlashOptions,
} from '@lobehub/editor';
import { renderHook } from '@testing-library/react';
@@ -22,6 +23,7 @@ vi.mock('@lobehub/editor', () => ({
INSERT_ORDERED_LIST_COMMAND: 'INSERT_ORDERED_LIST_COMMAND',
INSERT_TABLE_COMMAND: 'INSERT_TABLE_COMMAND',
INSERT_UNORDERED_LIST_COMMAND: 'INSERT_UNORDERED_LIST_COMMAND',
+ INSERT_VIDEO_COMMAND: 'INSERT_VIDEO_COMMAND',
}));
vi.mock('@/features/EditorCanvas', () => ({ openFileSelector: vi.fn() }));
@@ -46,4 +48,15 @@ describe('useSlashItems', () => {
expect(editor.dispatchCommand).toHaveBeenCalledWith(command, undefined);
});
+
+ it('inserts an empty video block from the slash menu', () => {
+ const { result } = renderHook(() => useSlashItems());
+ const item = findItem(result.current, 'video');
+ const editor = { dispatchCommand: vi.fn(), focus: vi.fn() };
+
+ expect(item).toBeDefined();
+ item!.onSelect!(editor as any, {} as any);
+
+ expect(editor.dispatchCommand).toHaveBeenCalledWith(INSERT_VIDEO_COMMAND, {});
+ });
});
diff --git a/src/features/PageEditor/EditorCanvas/useSlashItems.tsx b/src/features/PageEditor/EditorCanvas/useSlashItems.tsx
index 40f2f9de2af..f13c848d596 100644
--- a/src/features/PageEditor/EditorCanvas/useSlashItems.tsx
+++ b/src/features/PageEditor/EditorCanvas/useSlashItems.tsx
@@ -12,6 +12,7 @@ import {
INSERT_ORDERED_LIST_COMMAND,
INSERT_TABLE_COMMAND,
INSERT_UNORDERED_LIST_COMMAND,
+ INSERT_VIDEO_COMMAND,
} from '@lobehub/editor';
import { Text } from '@lobehub/ui';
import {
@@ -29,6 +30,7 @@ import {
SquareDashedBottomCodeIcon,
SuperscriptIcon,
Table2Icon,
+ VideoIcon,
} from 'lucide-react';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
@@ -108,6 +110,14 @@ export const useSlashItems = (): SlashOptions['items'] => {
}, 'image/*');
},
},
+ {
+ icon: VideoIcon,
+ key: 'video',
+ label: t('slash.video'),
+ onSelect: (editor) => {
+ editor.dispatchCommand(INSERT_VIDEO_COMMAND, {});
+ },
+ },
{
icon: MinusIcon,
key: 'hr',