fix: dispose on editor props.onInit (#2521)

This commit is contained in:
Himself65
2023-05-25 14:11:41 +08:00
committed by himself65
parent ad4737850d
commit 3d423c3299
2 changed files with 17 additions and 11 deletions

View File

@@ -311,7 +311,7 @@ export const AffinePlugin: WorkspaceAdapter<WorkspaceFlavour.AFFINE> = {
</Suspense> </Suspense>
); );
}, },
PageDetail: ({ currentWorkspace, currentPageId }) => { PageDetail: ({ currentWorkspace, currentPageId, onLoadEditor }) => {
const page = currentWorkspace.blockSuiteWorkspace.getPage(currentPageId); const page = currentWorkspace.blockSuiteWorkspace.getPage(currentPageId);
if (!page) { if (!page) {
throw new PageNotFoundError( throw new PageNotFoundError(
@@ -325,6 +325,7 @@ export const AffinePlugin: WorkspaceAdapter<WorkspaceFlavour.AFFINE> = {
pageId={currentPageId} pageId={currentPageId}
workspace={currentWorkspace} workspace={currentWorkspace}
onInit={initPage} onInit={initPage}
onLoad={onLoadEditor}
/> />
</> </>
); );

View File

@@ -43,10 +43,10 @@ const ImagePreviewModal = lazy(() =>
); );
const BlockSuiteEditorImpl = (props: EditorProps): ReactElement => { const BlockSuiteEditorImpl = (props: EditorProps): ReactElement => {
const { onLoad, page, mode, style, onInit } = props;
const JotaiEditorContainer = useAtomValue( const JotaiEditorContainer = useAtomValue(
editorContainerModuleAtom editorContainerModuleAtom
) as typeof EditorContainer; ) as typeof EditorContainer;
const page = props.page;
assertExists(page, 'page should not be null'); assertExists(page, 'page should not be null');
const editorRef = useRef<EditorContainer | null>(null); const editorRef = useRef<EditorContainer | null>(null);
const blockHubRef = useRef<BlockHub | null>(null); const blockHubRef = useRef<BlockHub | null>(null);
@@ -57,19 +57,24 @@ const BlockSuiteEditorImpl = (props: EditorProps): ReactElement => {
} }
const editor = editorRef.current; const editor = editorRef.current;
assertExists(editorRef, 'editorRef.current should not be null'); assertExists(editorRef, 'editorRef.current should not be null');
if (editor.mode !== props.mode) { if (editor.mode !== mode) {
editor.mode = props.mode; editor.mode = mode;
} }
useEffect(() => { useEffect(() => {
if (editor.page !== props.page) { if (editor.page !== page) {
editor.page = props.page; editor.page = page;
if (page.root === null) { if (page.root === null) {
props.onInit(page, editor); onInit(page, editor);
} }
return props.onLoad?.(page, editor);
} }
}, [props.page, props.onInit, props.onLoad, editor, props, page]); }, [onInit, editor, props, page]);
useEffect(() => {
if (editor.page && onLoad) {
return onLoad(page, editor);
}
}, [editor, editor.page, page, onLoad]);
const ref = useRef<HTMLDivElement>(null); const ref = useRef<HTMLDivElement>(null);
@@ -108,9 +113,9 @@ const BlockSuiteEditorImpl = (props: EditorProps): ReactElement => {
const className = `editor-wrapper ${editor.mode}-mode`; const className = `editor-wrapper ${editor.mode}-mode`;
return ( return (
<div <div
data-testid={`editor-${props.page.id}`} data-testid={`editor-${page.id}`}
className={className} className={className}
style={props.style} style={style}
ref={ref} ref={ref}
/> />
); );