Files
AFFiNE-Mirror/libs/components/editor-plugins/src/utils/plugin-render-root.ts
T
2022-07-22 15:49:21 +08:00

37 lines
822 B
TypeScript

import type { ReactNode } from 'react';
import type { PatchNode, UnPatchNode } from '@toeverything/components/ui';
interface PluginRenderRootProps {
name: string;
render: PatchNode;
}
export class PluginRenderRoot {
readonly name: string;
readonly patch: PatchNode;
private un_patch: UnPatchNode;
private root: ReactNode;
public isMounted = false;
constructor({ name, render }: PluginRenderRootProps) {
this.name = name;
this.patch = render;
}
render(node?: ReactNode) {
this.root = node;
if (this.isMounted) {
this.mount();
}
}
mount() {
this.un_patch = this.patch(this.name, this.root);
this.isMounted = true;
}
unmount() {
this.un_patch?.();
this.isMounted = false;
}
}