feat: add useBlocksuiteEditor hooks (#5366)

This commit is contained in:
JimmFly
2023-12-28 08:36:35 +00:00
parent 431f7cfac9
commit e11e8277ca
6 changed files with 32 additions and 29 deletions

View File

@@ -1,4 +0,0 @@
import type { AffineEditorContainer } from '@blocksuite/presets';
import { atom } from 'jotai';
export const editorContainerAtom = atom<AffineEditorContainer | null>(null);

View File

@@ -1,9 +1,9 @@
import { assertExists } from '@blocksuite/global/utils';
import { AffineEditorContainer } from '@blocksuite/presets';
import type { Page } from '@blocksuite/store';
import { useBlocksuiteEditor } from '@toeverything/hooks/use-block-suite-editor';
import clsx from 'clsx';
import { use } from 'foxact/use';
import { useAtom } from 'jotai';
import type { CSSProperties, ReactElement } from 'react';
import {
memo,
@@ -20,11 +20,8 @@ import {
blockSuiteEditorHeaderStyle,
blockSuiteEditorStyle,
} from './index.css';
import { editorContainerAtom } from './index.jotai';
import { editorSpecs } from './specs';
export { editorContainerAtom } from './index.jotai';
interface BlockElement extends Element {
path: string[];
}
@@ -151,7 +148,7 @@ const BlockSuiteEditorImpl = ({
}: EditorProps): ReactElement => {
usePageRoot(page);
const [, setEditorContainer] = useAtom(editorContainerAtom);
const [, setEditorContainer] = useBlocksuiteEditor();
assertExists(page, 'page should not be null');
const editorRef = useRef<AffineEditorContainer | null>(null);
if (editorRef.current === null) {
@@ -191,8 +188,6 @@ const BlockSuiteEditorImpl = ({
const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const editor = editorRef.current;
assertExists(editor);
const container = containerRef.current;
if (!container) {
return;
@@ -201,6 +196,7 @@ const BlockSuiteEditorImpl = ({
setEditorContainer(editor);
return () => {
editor.remove();
setEditorContainer(null);
};
}, [editor, setEditorContainer]);