import classNames from 'classnames'; import React from 'react'; import NoopProvider from '../core/components/Cell/NoopProvider'; import { migrateValue } from '../core/migrations/migrate'; import { optimizeRows } from '../core/reducer/value/helper/optimize'; import { setAllSizesAndOptimize } from '../core/reducer/value/helper/setAllSizesAndOptimize'; import type { Cell, CellPluginList, CellSpacing, RenderOptions, Row, ValueWithLegacy, } from '../core/types'; import { getChildCellPlugins } from '../core/utils/getAvailablePlugins'; import { getCellData } from '../core/utils/getCellData'; import { getPluginCellSpacing, normalizeCellSpacing, } from '../core/utils/getCellSpacing'; import { getCellInnerDivStylingProps, getCellOuterDivClassName, } from '../core/utils/getCellStylingProps'; const rowHasInlineChildren = ({ cells }: { cells: Cell[] }) => Boolean(cells.length === 2 && Boolean(cells[0].inline)); const HTMLRow: React.FC< Partial & { lang: string; className?: string; cellPlugins: CellPluginList; cellSpacing: CellSpacing; } > = React.memo(({ cells = [], className, lang, cellPlugins, cellSpacing }) => (
0 ? `0 ${-cellSpacing.x / 2}px` : undefined, }} > {cells.map((c) => ( ))}
)); // eslint-disable-next-line no-empty-function const noop = () => { return; }; const HTMLCell: React.FC< Cell & { lang?: string; cellPlugins: CellPluginList; cellSpacing: CellSpacing; } > = React.memo((props) => { const { lang = 'default', cellPlugins, cellSpacing, ...cell } = props; const { size, hasInlineNeighbour, inline, isDraftI18n, isDraft } = cell; const hasChildren = (cell.rows?.length ?? 0) > 0; if (isDraftI18n?.[lang] ?? isDraft) { return null; } const data = getCellData(cell, lang) ?? {}; const plugin = cell.plugin ? cellPlugins.find((p) => p.id === cell.plugin?.id) : null; const outerClasses = getCellOuterDivClassName({ hasChildren, size, hasInlineNeighbour, inline, }); if (plugin) { const { Renderer } = plugin; const Provider = plugin.Provider && !plugin.disableProviderInReadOnly ? plugin.Provider : NoopProvider; const pluginCellSpacing = getPluginCellSpacing(plugin, data); const normCellSpacing = pluginCellSpacing ? normalizeCellSpacing(pluginCellSpacing) : cellSpacing; const props = { readOnly: true, lang: lang, nodeId: cell.id, data: data, onChange: noop, pluginConfig: plugin, focused: false, isPreviewMode: false, isEditMode: false, }; const childCellPlugins = getChildCellPlugins(cellPlugins, { data, pluginId: plugin?.id, }); const cellOuterStyle = cellSpacing.y !== 0 || cellSpacing.x !== 0 ? { padding: `${cellSpacing.y / 2}px ${cellSpacing.x / 2}px`, } : undefined; const innerStylingProps = getCellInnerDivStylingProps(cell, plugin, data); return (
{cell.rows?.length ? (
0 ? `${-normCellSpacing.y / 2}px 0` : undefined, }} > {cell.rows?.map((r: Row) => ( ))}
) : null}
); } else if ((cell.rows?.length ?? 0) > 0) { return (
0 ? `0 ${cellSpacing.x / 2}px` : undefined, }} > {cell.rows?.map((r: Row) => ( ))}
); } return (
); }); export type HTMLRendererProps = { value: ValueWithLegacy | null; lang?: string; } & RenderOptions; export const HTMLRenderer: React.FC = React.memo( ({ value, cellPlugins, cellSpacing, lang = 'default' }) => { const data = migrateValue(value, { cellPlugins, lang }); const normCellSpacing = normalizeCellSpacing(cellSpacing); if (!data) { return null; } const { rows } = data; const optRows = optimizeRows(rows); return (
0 ? `${-normCellSpacing.y / 2}px 0` : undefined, }} > {setAllSizesAndOptimize(optRows).map((row) => ( ))}
); } );