mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00:00
fix(editor): remove rootRect and modify layout
This commit is contained in:
39
libs/components/ui/src/PatchElements.tsx
Normal file
39
libs/components/ui/src/PatchElements.tsx
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { useState, Fragment } from 'react';
|
||||
import { has } from '@toeverything/utils';
|
||||
|
||||
export type UnPatchNode = () => void;
|
||||
export type PatchNode = (key: string, node: ReactNode) => UnPatchNode;
|
||||
|
||||
export const usePatchNodes = () => {
|
||||
const [nodes, setNodes] = useState<Record<string, ReactNode>>({});
|
||||
|
||||
const patchNode: PatchNode = (key: string, node: ReactNode) => {
|
||||
setNodes(oldNodes => ({ ...oldNodes, [key]: node }));
|
||||
return () => {
|
||||
setNodes(oldNodes => {
|
||||
const nodes = { ...oldNodes };
|
||||
delete nodes[key];
|
||||
return nodes;
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
const hasNode = (key: string) => {
|
||||
return has(nodes, key);
|
||||
};
|
||||
|
||||
const patchedNodes = (
|
||||
<>
|
||||
{Object.entries(nodes).map(([key, node]) => {
|
||||
return <Fragment key={key}>{node}</Fragment>;
|
||||
})}
|
||||
</>
|
||||
);
|
||||
|
||||
return {
|
||||
patch: patchNode,
|
||||
has: hasNode,
|
||||
patchedNodes: patchedNodes,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user