fix: block hub might not work (#3199)

This commit is contained in:
Alex Yang
2023-07-13 02:02:00 +08:00
committed by GitHub
parent c066224a95
commit 5dda7d83da
3 changed files with 37 additions and 9 deletions

View File

@@ -2,7 +2,7 @@ import type { BlockHub } from '@blocksuite/blocks';
import type { Atom } from 'jotai';
import { useAtomValue } from 'jotai';
import type { HTMLAttributes, ReactElement } from 'react';
import { useRef } from 'react';
import { useEffect, useRef } from 'react';
export interface BlockHubProps extends HTMLAttributes<HTMLDivElement> {
blockHubAtom: Atom<Readonly<BlockHub> | null>;
@@ -11,15 +11,17 @@ export interface BlockHubProps extends HTMLAttributes<HTMLDivElement> {
export const BlockHubWrapper = (props: BlockHubProps): ReactElement => {
const blockHub = useAtomValue(props.blockHubAtom);
const ref = useRef<HTMLDivElement>(null);
if (ref.current) {
const div = ref.current;
if (!blockHub) {
if (div.hasChildNodes()) {
div.removeChild(div.firstChild as ChildNode);
useEffect(() => {
console.log('ref.current', blockHub, ref);
if (ref.current) {
const div = ref.current;
if (blockHub) {
if (div.hasChildNodes()) {
div.removeChild(div.firstChild as ChildNode);
}
div.appendChild(blockHub);
}
} else {
div.appendChild(blockHub);
}
}
}, [blockHub]);
return <div ref={ref} data-testid="block-hub" />;
};