mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-17 06:16:59 +08:00
fix(editor): multiple block hub when switching pages (#1042)
This commit is contained in:
@@ -11,6 +11,7 @@ import Head from 'next/head';
|
|||||||
import { useTranslation } from '@affine/i18n';
|
import { useTranslation } from '@affine/i18n';
|
||||||
import { useGlobalState } from '@/store/app';
|
import { useGlobalState } from '@/store/app';
|
||||||
import exampleMarkdown from '@/templates/Welcome-to-AFFiNE-Alpha-Downhills.md';
|
import exampleMarkdown from '@/templates/Welcome-to-AFFiNE-Alpha-Downhills.md';
|
||||||
|
import { assertEquals } from '@blocksuite/store';
|
||||||
|
|
||||||
const DynamicBlocksuite = dynamic(() => import('@/components/editor'), {
|
const DynamicBlocksuite = dynamic(() => import('@/components/editor'), {
|
||||||
ssr: false,
|
ssr: false,
|
||||||
@@ -20,7 +21,14 @@ const BlockHubAppender = () => {
|
|||||||
const setBlockHub = useGlobalState(store => store.setBlockHub);
|
const setBlockHub = useGlobalState(store => store.setBlockHub);
|
||||||
const editor = useGlobalState(store => store.editor);
|
const editor = useGlobalState(store => store.editor);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
// todo(himself65): refactor with `useRef`
|
||||||
|
const abortController = new AbortController();
|
||||||
let blockHubElement: HTMLElement | null = null;
|
let blockHubElement: HTMLElement | null = null;
|
||||||
|
abortController.signal.addEventListener('abort', () => {
|
||||||
|
blockHubElement?.remove();
|
||||||
|
const toolWrapper = document.querySelector('#toolWrapper');
|
||||||
|
assertEquals(toolWrapper?.childNodes.length, 0);
|
||||||
|
});
|
||||||
|
|
||||||
editor?.createBlockHub().then(blockHub => {
|
editor?.createBlockHub().then(blockHub => {
|
||||||
const toolWrapper = document.querySelector('#toolWrapper');
|
const toolWrapper = document.querySelector('#toolWrapper');
|
||||||
@@ -28,12 +36,15 @@ const BlockHubAppender = () => {
|
|||||||
// In an invitation page there is no toolWrapper, which contains helper icon and blockHub icon
|
// In an invitation page there is no toolWrapper, which contains helper icon and blockHub icon
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (abortController.signal.aborted) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
blockHubElement = blockHub;
|
blockHubElement = blockHub;
|
||||||
setBlockHub(blockHub);
|
setBlockHub(blockHub);
|
||||||
toolWrapper.appendChild(blockHub);
|
toolWrapper.appendChild(blockHub);
|
||||||
});
|
});
|
||||||
return () => {
|
return () => {
|
||||||
blockHubElement?.remove();
|
abortController.abort();
|
||||||
};
|
};
|
||||||
}, [editor, setBlockHub]);
|
}, [editor, setBlockHub]);
|
||||||
return null;
|
return null;
|
||||||
@@ -73,7 +84,7 @@ const Page: NextPageWithLayout = () => {
|
|||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<BlockHubAppender />
|
<BlockHubAppender key={currentWorkspace.id + currentPage.id} />
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
|
|||||||
Reference in New Issue
Block a user