Merge pull request #334 from toeverything/refactor/render-block
Refactor/render block
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { useBlockRender } from '@toeverything/components/editor-core';
|
import { RenderBlockChildren } from '@toeverything/components/editor-core';
|
||||||
import { ChildrenView, CreateView } from '@toeverything/framework/virgo';
|
import { ChildrenView, CreateView } from '@toeverything/framework/virgo';
|
||||||
|
|
||||||
export const GridItemRender = function (
|
export const GridItemRender = function (
|
||||||
@@ -6,14 +6,7 @@ export const GridItemRender = function (
|
|||||||
) {
|
) {
|
||||||
const GridItem = function (props: CreateView) {
|
const GridItem = function (props: CreateView) {
|
||||||
const { block } = props;
|
const { block } = props;
|
||||||
const { BlockRender } = useBlockRender();
|
const children = <RenderBlockChildren block={block} indent={false} />;
|
||||||
const children = (
|
|
||||||
<>
|
|
||||||
{block.childrenIds.map(id => {
|
|
||||||
return <BlockRender key={id} blockId={id} />;
|
|
||||||
})}
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
return <>{creator({ ...props, children })}</>;
|
return <>{creator({ ...props, children })}</>;
|
||||||
};
|
};
|
||||||
return GridItem;
|
return GridItem;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useBlockRender } from '@toeverything/components/editor-core';
|
import { BlockRender } from '@toeverything/components/editor-core';
|
||||||
import { styled } from '@toeverything/components/ui';
|
import { styled } from '@toeverything/components/ui';
|
||||||
import { Protocol } from '@toeverything/datasource/db-service';
|
import { Protocol } from '@toeverything/datasource/db-service';
|
||||||
import { CreateView } from '@toeverything/framework/virgo';
|
import { CreateView } from '@toeverything/framework/virgo';
|
||||||
@@ -31,7 +31,6 @@ export const Grid = function (props: CreateView) {
|
|||||||
const originalLeftWidth = useRef<number>(gridItemMinWidth);
|
const originalLeftWidth = useRef<number>(gridItemMinWidth);
|
||||||
const originalRightWidth = useRef<number>(gridItemMinWidth);
|
const originalRightWidth = useRef<number>(gridItemMinWidth);
|
||||||
const [alertHandleId, setAlertHandleId] = useState<string>(null);
|
const [alertHandleId, setAlertHandleId] = useState<string>(null);
|
||||||
const { BlockRender } = useBlockRender();
|
|
||||||
|
|
||||||
const getLeftRightGridItemDomByIndex = (index: number) => {
|
const getLeftRightGridItemDomByIndex = (index: number) => {
|
||||||
const gridItems = Array.from(gridContainerRef.current?.children).filter(
|
const gridItems = Array.from(gridContainerRef.current?.children).filter(
|
||||||
|
|||||||
@@ -21,19 +21,6 @@ const SceneMap: Record<RecastScene, ComponentType<CreateView>> = {
|
|||||||
kanban: SceneKanban,
|
kanban: SceneKanban,
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
const GroupBox = styled('div')(({ theme }) => {
|
|
||||||
return {
|
|
||||||
'&:hover': {
|
|
||||||
// Workaround referring to other components
|
|
||||||
// See https://emotion.sh/docs/styled#targeting-another-emotion-component
|
|
||||||
// [GroupActionWrapper.toString()]: {},
|
|
||||||
'& > *': {
|
|
||||||
visibility: 'visible',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const GroupActionWrapper = styled('div')(({ theme }) => ({
|
const GroupActionWrapper = styled('div')(({ theme }) => ({
|
||||||
height: '30px',
|
height: '30px',
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
@@ -59,6 +46,14 @@ const GroupActionWrapper = styled('div')(({ theme }) => ({
|
|||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
const GroupBox = styled('div')({
|
||||||
|
'&:hover': {
|
||||||
|
[GroupActionWrapper.toString()]: {
|
||||||
|
visibility: 'visible',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const GroupContainer = styled('div')<{ isSelect?: boolean }>(
|
const GroupContainer = styled('div')<{ isSelect?: boolean }>(
|
||||||
({ isSelect, theme }) => ({
|
({ isSelect, theme }) => ({
|
||||||
background: theme.affine.palette.white,
|
background: theme.affine.palette.white,
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ export const AddViewMenu = () => {
|
|||||||
onClick={() => setActivePanel(!activePanel)}
|
onClick={() => setActivePanel(!activePanel)}
|
||||||
>
|
>
|
||||||
<AddViewIcon fontSize="small" />
|
<AddViewIcon fontSize="small" />
|
||||||
<span>Add View</span>
|
<span style={{ userSelect: 'none' }}>Add View</span>
|
||||||
{activePanel && (
|
{activePanel && (
|
||||||
<Panel>
|
<Panel>
|
||||||
<PanelItem>
|
<PanelItem>
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export const ViewsMenu = () => {
|
|||||||
useRecastView();
|
useRecastView();
|
||||||
|
|
||||||
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
|
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
|
||||||
setViewName(e.target.value.trim());
|
setViewName(e.target.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
|
const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
|
||||||
@@ -36,7 +36,7 @@ export const ViewsMenu = () => {
|
|||||||
}
|
}
|
||||||
await updateView({
|
await updateView({
|
||||||
...activeView,
|
...activeView,
|
||||||
name: viewName,
|
name: viewName.trim(),
|
||||||
type: viewType,
|
type: viewType,
|
||||||
});
|
});
|
||||||
setActiveView(null);
|
setActiveView(null);
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { createContext, PropsWithChildren, useContext } from 'react';
|
|||||||
import { RenderBlockProps } from './RenderBlock';
|
import { RenderBlockProps } from './RenderBlock';
|
||||||
|
|
||||||
type BlockRenderProps = {
|
type BlockRenderProps = {
|
||||||
blockRender: (args: RenderBlockProps) => JSX.Element | null;
|
blockRender: (args: RenderBlockProps) => JSX.Element;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const BlockRenderContext = createContext<BlockRenderProps>(
|
export const BlockRenderContext = createContext<BlockRenderProps>(
|
||||||
@@ -27,9 +27,14 @@ export const BlockRenderProvider = ({
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useBlockRender = () => {
|
const useBlockRender = () => {
|
||||||
const { blockRender } = useContext(BlockRenderContext);
|
const { blockRender } = useContext(BlockRenderContext);
|
||||||
return {
|
return {
|
||||||
BlockRender: blockRender,
|
BlockRender: blockRender,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const BlockRender = (props: RenderBlockProps) => {
|
||||||
|
const { BlockRender } = useBlockRender();
|
||||||
|
return <BlockRender {...props} />;
|
||||||
|
};
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ import { useBlock } from '../hooks';
|
|||||||
/**
|
/**
|
||||||
* Render nothing
|
* Render nothing
|
||||||
*/
|
*/
|
||||||
export const NullBlockRender = (): null => null;
|
// eslint-disable-next-line react/jsx-no-useless-fragment
|
||||||
|
export const NullBlockRender = () => <></>;
|
||||||
|
|
||||||
export interface RenderBlockProps {
|
export interface RenderBlockProps {
|
||||||
blockId: string;
|
blockId: string;
|
||||||
@@ -22,7 +23,7 @@ export function RenderBlock({
|
|||||||
const { block } = useBlock(blockId);
|
const { block } = useBlock(blockId);
|
||||||
|
|
||||||
const setRef = useCallback(
|
const setRef = useCallback(
|
||||||
(dom: HTMLElement) => {
|
(dom: HTMLElement | null) => {
|
||||||
if (block != null && dom != null) {
|
if (block != null && dom != null) {
|
||||||
block.dom = dom;
|
block.dom = dom;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { styled } from '@toeverything/components/ui';
|
import { styled } from '@toeverything/components/ui';
|
||||||
import type { AsyncBlock } from '../editor';
|
import type { AsyncBlock } from '../editor';
|
||||||
import { useBlockRender } from './Context';
|
import { BlockRender } from './Context';
|
||||||
import { NullBlockRender } from './RenderBlock';
|
import { NullBlockRender } from './RenderBlock';
|
||||||
|
|
||||||
export interface RenderChildrenProps {
|
export interface RenderChildrenProps {
|
||||||
@@ -12,7 +12,6 @@ export const RenderBlockChildren = ({
|
|||||||
block,
|
block,
|
||||||
indent = true,
|
indent = true,
|
||||||
}: RenderChildrenProps) => {
|
}: RenderChildrenProps) => {
|
||||||
const { BlockRender } = useBlockRender();
|
|
||||||
if (BlockRender === NullBlockRender) {
|
if (BlockRender === NullBlockRender) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import type {
|
|||||||
} from 'react';
|
} from 'react';
|
||||||
import { forwardRef } from 'react';
|
import { forwardRef } from 'react';
|
||||||
import { CreateView } from '../editor';
|
import { CreateView } from '../editor';
|
||||||
import { useBlockRender } from './Context';
|
import { BlockRender } from './Context';
|
||||||
import { NullBlockRender } from './RenderBlock';
|
import { NullBlockRender } from './RenderBlock';
|
||||||
|
|
||||||
type WithChildrenConfig = {
|
type WithChildrenConfig = {
|
||||||
@@ -60,7 +60,6 @@ export const withTreeViewChildren = (
|
|||||||
|
|
||||||
return (props: CreateView) => {
|
return (props: CreateView) => {
|
||||||
const { block } = props;
|
const { block } = props;
|
||||||
const { BlockRender } = useBlockRender();
|
|
||||||
const collapsed = block.getProperty('collapsed')?.value;
|
const collapsed = block.getProperty('collapsed')?.value;
|
||||||
const childrenIds = block.childrenIds;
|
const childrenIds = block.childrenIds;
|
||||||
const showChildren =
|
const showChildren =
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export { BlockRenderProvider, useBlockRender } from './Context';
|
export { BlockRender, BlockRenderProvider } from './Context';
|
||||||
export { NullBlockRender, RenderBlock } from './RenderBlock';
|
export { NullBlockRender, RenderBlock } from './RenderBlock';
|
||||||
export { RenderBlockChildren } from './RenderBlockChildren';
|
export { RenderBlockChildren } from './RenderBlockChildren';
|
||||||
export { KanbanBlockRender } from './RenderKanbanBlock';
|
export { KanbanBlockRender } from './RenderKanbanBlock';
|
||||||
|
|||||||
Reference in New Issue
Block a user