import React from 'react';
import { failInvariant } from '../internal/failInvariant';
import {
getCompiledPlatePlugin,
getPlateRuntime,
} from '../internal/plugin/compilePlateModel';
import { getDefaultElementTag } from '../internal/plugin/defaultElementTag';
import type {
AnyBasePluginPortal,
AnyPluginBase,
Editor,
RenderElementProps,
} from '../lib';
import { createPluginContext } from '../lib/plugin/createPluginContext.internal';
import { EditorElement } from './components/plite-nodes';
import {
getStaticElementComponent,
getStaticSlot,
} from './internal/staticPresentation';
import { getRenderNodeStaticProps } from './utils/getRenderNodeStaticProps.internal';
export type PliteRenderElement = (
props: RenderElementProps
) => React.ReactNode | undefined;
export const renderStaticAfterNodeChildren = (editor: Editor, props: object) =>
getPlateRuntime(editor).pluginCache.slots.afterNodeChildren.map((name) => {
const innerPlugin =
getCompiledPlatePlugin(editor, name) ??
failInvariant('Expected value to be defined');
const innerComponent = getStaticSlot(
editor,
innerPlugin,
'afterNodeChildren'
);
const pluginContext = createPluginContext(editor, innerPlugin);
if (typeof innerComponent !== 'function') return null;
const rendered = Reflect.apply(innerComponent, undefined, [
{
...props,
...pluginContext,
},
]) as React.ReactNode;
return {rendered};
});
export const pluginRenderElementStatic = (
editor: Editor,
plugin: AnyBasePluginPortal | AnyPluginBase
): PliteRenderElement =>
function render(initialNodeProps) {
let nodeProps = initialNodeProps;
const nodeComponent = getStaticElementComponent(editor, plugin);
const Component =
nodeComponent && typeof nodeComponent !== 'string'
? nodeComponent
: undefined;
const Element = Component ?? EditorElement;
let { children } = nodeProps;
// Intentional props accumulation pattern.
nodeProps = getRenderNodeStaticProps({
editor,
path: nodeProps.path,
plugin,
props: nodeProps as any,
});
getPlateRuntime(editor).pluginCache.slots.wrapNodeChildren.forEach(
(name) => {
const wrapperPlugin =
getCompiledPlatePlugin(editor, name) ??
failInvariant('Expected value to be defined');
const wrapperContext = createPluginContext(editor, wrapperPlugin);
const renderBelow = getStaticSlot(
editor,
wrapperPlugin,
'wrapNodeChildren'
);
const hoc =
typeof renderBelow === 'function'
? Reflect.apply(renderBelow, undefined, [
{
...nodeProps,
...wrapperContext,
},
])
: undefined;
if (typeof hoc === 'function') {
children = Reflect.apply(hoc, undefined, [
{ ...nodeProps, children },
]);
}
}
);
const tag = nodeComponent ?? getDefaultElementTag(nodeProps.attributes);
const defaultProps = typeof tag === 'string' ? { as: tag } : {};
let renderedNode: React.ReactNode = (
{children}
{renderStaticAfterNodeChildren(editor, {
...defaultProps,
...nodeProps,
})}
);
getPlateRuntime(editor).pluginCache.slots.wrapNode.forEach((name) => {
const wrapperPlugin =
getCompiledPlatePlugin(editor, name) ??
failInvariant('Expected value to be defined');
const wrapperContext = createPluginContext(editor, wrapperPlugin);
const renderAbove = getStaticSlot(editor, wrapperPlugin, 'wrapNode');
const hoc =
typeof renderAbove === 'function'
? Reflect.apply(renderAbove, undefined, [
{
...nodeProps,
...wrapperContext,
},
])
: undefined;
if (typeof hoc === 'function') {
renderedNode = Reflect.apply(hoc, undefined, [
{ ...nodeProps, children: renderedNode },
]);
}
});
return renderedNode;
};