mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-23 21:06:22 +08:00
feat: expose editor by ref
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/* eslint-disable filename-rules/match */
|
/* eslint-disable filename-rules/match */
|
||||||
import { useEffect } from 'react';
|
import { useEffect, useRef } from 'react';
|
||||||
import { useParams } from 'react-router';
|
import { useParams } from 'react-router';
|
||||||
import {
|
import {
|
||||||
MuiBox as Box,
|
MuiBox as Box,
|
||||||
@@ -24,6 +24,7 @@ import { WorkspaceName } from './workspace-name';
|
|||||||
import { CollapsiblePageTree } from './collapsible-page-tree';
|
import { CollapsiblePageTree } from './collapsible-page-tree';
|
||||||
import TemplatesPortal from './templates-portal';
|
import TemplatesPortal from './templates-portal';
|
||||||
import { useFlag } from '@toeverything/datasource/feature-flags';
|
import { useFlag } from '@toeverything/datasource/feature-flags';
|
||||||
|
import { type BlockEditor } from '@toeverything/components/editor-core';
|
||||||
|
|
||||||
type PageProps = {
|
type PageProps = {
|
||||||
workspace: string;
|
workspace: string;
|
||||||
@@ -36,6 +37,7 @@ export function Page(props: PageProps) {
|
|||||||
const { user } = useUserAndSpaces();
|
const { user } = useUserAndSpaces();
|
||||||
const templatesPortalFlag = useFlag('BooleanTemplatesPortal', false);
|
const templatesPortalFlag = useFlag('BooleanTemplatesPortal', false);
|
||||||
const dailyNotesFlag = useFlag('BooleanDailyNotes', false);
|
const dailyNotesFlag = useFlag('BooleanDailyNotes', false);
|
||||||
|
const editor = useRef<BlockEditor>();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!user?.id || !page_id) return;
|
if (!user?.id || !page_id) return;
|
||||||
@@ -107,6 +109,7 @@ export function Page(props: PageProps) {
|
|||||||
<AffineEditor
|
<AffineEditor
|
||||||
workspace={props.workspace}
|
workspace={props.workspace}
|
||||||
rootBlockId={page_id}
|
rootBlockId={page_id}
|
||||||
|
ref={editor}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<Box
|
<Box
|
||||||
|
|||||||
@@ -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 { useCurrentEditors } from '@toeverything/datasource/state';
|
||||||
|
|
||||||
import { createEditor } from './create-editor';
|
import { createEditor } from './create-editor';
|
||||||
@@ -23,41 +27,40 @@ function useConstant<T>(init: () => T): T {
|
|||||||
return ref.current;
|
return ref.current;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const AffineEditor = ({
|
export const AffineEditor = forwardRef<BlockEditor, AffineEditorProps>(
|
||||||
workspace,
|
({ workspace, rootBlockId, scrollBlank = true, isWhiteboard }, ref) => {
|
||||||
rootBlockId,
|
const { setCurrentEditors } = useCurrentEditors();
|
||||||
scrollBlank = true,
|
const editor = useConstant(() => {
|
||||||
isWhiteboard,
|
const editor = createEditor(workspace, isWhiteboard);
|
||||||
}: AffineEditorProps) => {
|
|
||||||
const { setCurrentEditors } = useCurrentEditors();
|
|
||||||
const editor = useConstant(() => {
|
|
||||||
const editor = createEditor(workspace, isWhiteboard);
|
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
globalThis.virgo = editor;
|
globalThis.virgo = editor;
|
||||||
return editor;
|
return editor;
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useImperativeHandle(ref, () => editor);
|
||||||
if (rootBlockId) {
|
|
||||||
editor.setRootBlockId(rootBlockId);
|
|
||||||
} else {
|
|
||||||
console.error('rootBlockId for page is required. ');
|
|
||||||
}
|
|
||||||
}, [editor, rootBlockId]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!rootBlockId) return;
|
if (rootBlockId) {
|
||||||
setCurrentEditors(prev => ({ ...prev, [rootBlockId]: editor }));
|
editor.setRootBlockId(rootBlockId);
|
||||||
}, [editor, rootBlockId, setCurrentEditors]);
|
} else {
|
||||||
|
console.error('rootBlockId for page is required. ');
|
||||||
|
}
|
||||||
|
}, [editor, rootBlockId]);
|
||||||
|
|
||||||
return (
|
useEffect(() => {
|
||||||
<RenderRoot
|
if (!rootBlockId) return;
|
||||||
editor={editor}
|
setCurrentEditors(prev => ({ ...prev, [rootBlockId]: editor }));
|
||||||
editorElement={AffineEditor as any}
|
}, [editor, rootBlockId, setCurrentEditors]);
|
||||||
scrollBlank={scrollBlank}
|
|
||||||
>
|
return (
|
||||||
<RenderBlock blockId={rootBlockId} />
|
<RenderRoot
|
||||||
</RenderRoot>
|
editor={editor}
|
||||||
);
|
editorElement={AffineEditor as any}
|
||||||
};
|
scrollBlank={scrollBlank}
|
||||||
|
>
|
||||||
|
<RenderBlock blockId={rootBlockId} />
|
||||||
|
</RenderRoot>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user