mirror of
https://github.com/outline/outline.git
synced 2026-11-21 21:44:30 +03:00
* Upgrade Mobx * fix: Enforce MobX action boundaries * fix: Wrap static observable updates in actions * Remove iteration tests
54 lines
1.2 KiB
TypeScript
54 lines
1.2 KiB
TypeScript
import { isEqual } from "es-toolkit/compat";
|
|
import { action, computed, makeObservable, 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;
|
|
}
|
|
|
|
let nextRendererId = 0;
|
|
|
|
export class NodeViewRenderer<T extends object> implements PortalRenderer {
|
|
@observable public props: T;
|
|
|
|
/**
|
|
* Stable identity used as the React key when renderers are rendered
|
|
*/
|
|
public readonly key = `renderer-${nextRendererId++}`;
|
|
|
|
public constructor(
|
|
public element: HTMLElement,
|
|
private Component: FunctionComponent<T>,
|
|
props: T
|
|
) {
|
|
this.props = props;
|
|
makeObservable(this);
|
|
}
|
|
|
|
@computed
|
|
public get content() {
|
|
return createPortal(
|
|
<this.Component {...this.props} />,
|
|
this.element,
|
|
this.key
|
|
);
|
|
}
|
|
|
|
@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;
|
|
}
|
|
}
|