mirror of
https://github.com/outline/outline.git
synced 2026-11-30 17:10:36 +03:00
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import { isEqual } from "es-toolkit/compat";
|
|
import { action, computed, observable } from "mobx";
|
|
import type { FunctionComponent, ReactNode } from "react";
|
|
import { createPortal } from "react-dom";
|
|
|
|
/**
|
|
* The minimal shape the editor needs to include a renderer's React content in
|
|
* its shared tree. Both node views and decoration widgets satisfy this.
|
|
*/
|
|
export interface PortalRenderer {
|
|
readonly content: ReactNode;
|
|
}
|
|
|
|
export class NodeViewRenderer<T extends object> implements PortalRenderer {
|
|
@observable public props: T;
|
|
|
|
public constructor(
|
|
public element: HTMLElement,
|
|
private Component: FunctionComponent<T>,
|
|
props: T
|
|
) {
|
|
this.props = props;
|
|
}
|
|
|
|
@computed
|
|
public get content() {
|
|
return createPortal(<this.Component {...this.props} />, this.element);
|
|
}
|
|
|
|
@action
|
|
public updateProps(props: T) {
|
|
if (!isEqual(props, this.props)) {
|
|
this.props = props;
|
|
}
|
|
}
|
|
|
|
@action
|
|
public setProp<K extends keyof T>(key: K, value: T[K]) {
|
|
this.props[key] = value;
|
|
}
|
|
}
|