feat: expose editor by ref

This commit is contained in:
qishaoxuan
2022-07-27 15:24:40 +08:00
parent 5844abe078
commit c4b1c10ffc
2 changed files with 43 additions and 37 deletions
@@ -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<BlockEditor>();
useEffect(() => {
if (!user?.id || !page_id) return;
@@ -107,6 +109,7 @@ export function Page(props: PageProps) {
<AffineEditor
workspace={props.workspace}
rootBlockId={page_id}
ref={editor}
/>
) : (
<Box
+39 -36
View File
@@ -1,6 +1,10 @@
import { useEffect, useRef } from 'react';
import { forwardRef, useEffect, useImperativeHandle, useRef } from 'react';
import { RenderRoot, RenderBlock } from '@toeverything/components/editor-core';
import {
RenderRoot,
RenderBlock,
type BlockEditor,
} from '@toeverything/components/editor-core';
import { useCurrentEditors } from '@toeverything/datasource/state';
import { createEditor } from './create-editor';
@@ -23,41 +27,40 @@ function useConstant<T>(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<BlockEditor, AffineEditorProps>(
({ 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 (
<RenderRoot
editor={editor}
editorElement={AffineEditor as any}
scrollBlank={scrollBlank}
>
<RenderBlock blockId={rootBlockId} />
</RenderRoot>
);
};
useEffect(() => {
if (!rootBlockId) return;
setCurrentEditors(prev => ({ ...prev, [rootBlockId]: editor }));
}, [editor, rootBlockId, setCurrentEditors]);
return (
<RenderRoot
editor={editor}
editorElement={AffineEditor as any}
scrollBlank={scrollBlank}
>
<RenderBlock blockId={rootBlockId} />
</RenderRoot>
);
}
);