From cc34cdcf74c92d9fb8b1207f75246b1cddc49c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20J=C3=A4derberg?= Date: Thu, 22 May 2025 11:03:29 +0000 Subject: [PATCH] Merged in fix/SW-2848-RTE-with-copied-divs (pull request #2173) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: handle when content has copied divs from episervers RTE * fix: handle when content has copied divs from episervers RTE Approved-by: Matilda Landström --- .../components/JsonToHtml/renderOptions.tsx | 22 ++++++++++++++++ .../components/JsonToHtml/utils.tsx | 26 ++++++++++--------- .../types/transitionTypes/rte/enums.ts | 4 +++ apps/scandic-web/utils/safeTry.ts | 8 ++++++ 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/apps/scandic-web/components/JsonToHtml/renderOptions.tsx b/apps/scandic-web/components/JsonToHtml/renderOptions.tsx index a5c7f10f0..ab36570f6 100644 --- a/apps/scandic-web/components/JsonToHtml/renderOptions.tsx +++ b/apps/scandic-web/components/JsonToHtml/renderOptions.tsx @@ -336,6 +336,28 @@ export const renderOptions: RenderOptions = { {next(node.children, embeds, fullRenderOptions)} ) }, + [RTETypeEnum.div]: ( + node: RTEDefaultNode, + embeds: EmbedByUid, + next: RTENext, + fullRenderOptions: RenderOptions + ) => { + let props = extractPossibleAttributes(node.attrs) + const className = props.className + + if (className) { + if (hasAvailableULFormat(className)) { + // @ts-ignore: We want to set css modules classNames even if it does not correspond + // to an existing class in the module style sheet. Due to our css modules plugin for + // typescript, we cannot do this without the ts-ignore + props.className = styles[className] + } + } + + return ( +
{next(node.children, embeds, fullRenderOptions)}
+ ) + }, [RTETypeEnum.reference]: ( node: RTENode, diff --git a/apps/scandic-web/components/JsonToHtml/utils.tsx b/apps/scandic-web/components/JsonToHtml/utils.tsx index c3748a2fb..c7786c37e 100644 --- a/apps/scandic-web/components/JsonToHtml/utils.tsx +++ b/apps/scandic-web/components/JsonToHtml/utils.tsx @@ -122,16 +122,13 @@ export function nodeToHtml( if ("type" in node === false) { return textNodeToHtml(node, fullRenderOptions) } else { - if (fullRenderOptions[node.type] !== undefined) { + const renderer = fullRenderOptions[node.type] as RTERenderOptionComponent + if (renderer) { if (node.type === RTETypeEnum.doc) { return null } - return (fullRenderOptions[node.type] as RTERenderOptionComponent)( - node, - embeds, - next, - fullRenderOptions - ) + + return renderer(node, embeds, next, fullRenderOptions) } else { return next(node.children, embeds, fullRenderOptions) } @@ -152,11 +149,16 @@ export function nodesToHtml( ) { const embeds = groupEmbedsByUid(embedsArray) const fullRenderOptions = { ...renderOptions, ...overrideRenderOptions } - return nodes.map((node, index) => ( - - {nodeToHtml(node, embeds, fullRenderOptions)} - - )) + + return nodes.map((node, index) => { + const nodeHtml = nodeToHtml(node, embeds, fullRenderOptions) + + return ( + + {nodeHtml} + + ) + }) } export function makeCssModuleCompatibleClassName( diff --git a/apps/scandic-web/types/transitionTypes/rte/enums.ts b/apps/scandic-web/types/transitionTypes/rte/enums.ts index 29a76bf0d..11af3edb8 100644 --- a/apps/scandic-web/types/transitionTypes/rte/enums.ts +++ b/apps/scandic-web/types/transitionTypes/rte/enums.ts @@ -28,6 +28,10 @@ export enum RTETypeEnum { p = "p", reference = "reference", span = "span", + /** + * Included for compatibility when copying RTE from other sources e.g. epi + */ + div = "div", table = "table", tbody = "tbody", td = "td", diff --git a/apps/scandic-web/utils/safeTry.ts b/apps/scandic-web/utils/safeTry.ts index d3bb81596..9b986a17f 100644 --- a/apps/scandic-web/utils/safeTry.ts +++ b/apps/scandic-web/utils/safeTry.ts @@ -9,3 +9,11 @@ export async function safeTry(func: Promise): SafeTryResult { return [undefined, err] } } + +export function safeTrySync(func: () => T): Awaited> { + try { + return [func(), undefined] + } catch (err) { + return [undefined, err] + } +}