From c4b1c10ffc8abdea4b014f783a55226cb7358e0b Mon Sep 17 00:00:00 2001 From: qishaoxuan Date: Wed, 27 Jul 2022 15:24:40 +0800 Subject: [PATCH] feat: expose editor by ref --- .../src/pages/workspace/docs/Page.tsx | 5 +- libs/components/affine-editor/src/Editor.tsx | 75 ++++++++++--------- 2 files changed, 43 insertions(+), 37 deletions(-) diff --git a/apps/ligo-virgo/src/pages/workspace/docs/Page.tsx b/apps/ligo-virgo/src/pages/workspace/docs/Page.tsx index f592863e20..3d4abd6cea 100644 --- a/apps/ligo-virgo/src/pages/workspace/docs/Page.tsx +++ b/apps/ligo-virgo/src/pages/workspace/docs/Page.tsx @@ -1,5 +1,5 @@ /* eslint-disable filename-rules/match */ -import { useEffect } from 'react'; +import { useEffect, useRef } from 'react'; import { useParams } from 'react-router'; import { MuiBox as Box, @@ -24,6 +24,7 @@ import { WorkspaceName } from './workspace-name'; import { CollapsiblePageTree } from './collapsible-page-tree'; import TemplatesPortal from './templates-portal'; import { useFlag } from '@toeverything/datasource/feature-flags'; +import { type BlockEditor } from '@toeverything/components/editor-core'; type PageProps = { workspace: string; @@ -36,6 +37,7 @@ export function Page(props: PageProps) { const { user } = useUserAndSpaces(); const templatesPortalFlag = useFlag('BooleanTemplatesPortal', false); const dailyNotesFlag = useFlag('BooleanDailyNotes', false); + const editor = useRef(); useEffect(() => { if (!user?.id || !page_id) return; @@ -107,6 +109,7 @@ export function Page(props: PageProps) { ) : ( (init: () => T): T { return ref.current; } -export const AffineEditor = ({ - workspace, - rootBlockId, - scrollBlank = true, - isWhiteboard, -}: AffineEditorProps) => { - const { setCurrentEditors } = useCurrentEditors(); - const editor = useConstant(() => { - const editor = createEditor(workspace, isWhiteboard); +export const AffineEditor = forwardRef( + ({ workspace, rootBlockId, scrollBlank = true, isWhiteboard }, ref) => { + const { setCurrentEditors } = useCurrentEditors(); + const editor = useConstant(() => { + const editor = createEditor(workspace, isWhiteboard); - // @ts-ignore - globalThis.virgo = editor; - return editor; - }); + // @ts-ignore + globalThis.virgo = editor; + return editor; + }); - useEffect(() => { - if (rootBlockId) { - editor.setRootBlockId(rootBlockId); - } else { - console.error('rootBlockId for page is required. '); - } - }, [editor, rootBlockId]); + useImperativeHandle(ref, () => editor); - useEffect(() => { - if (!rootBlockId) return; - setCurrentEditors(prev => ({ ...prev, [rootBlockId]: editor })); - }, [editor, rootBlockId, setCurrentEditors]); + useEffect(() => { + if (rootBlockId) { + editor.setRootBlockId(rootBlockId); + } else { + console.error('rootBlockId for page is required. '); + } + }, [editor, rootBlockId]); - return ( - - - - ); -}; + useEffect(() => { + if (!rootBlockId) return; + setCurrentEditors(prev => ({ ...prev, [rootBlockId]: editor })); + }, [editor, rootBlockId, setCurrentEditors]); + + return ( + + + + ); + } +);