Files
web/apps/scandic-web/components/DeprecatedJsonToHtml/utils.tsx
Anton Gunnarsson cbf9e7b7c2 Merged in chore/next15 (pull request #1999)
chore (SW-834): Upgrade to Next 15

* wip: apply codemod and upgrade swc plugin

* wip: design-system to react 19, fix issues from async (search)params

* wip: fix remaining issues from codemod

serverClient is now async because context use headers()
getLang is now async because it uses headers()

* Minor cleanup

* Inline react-material-symbols package

Package is seemingly not maintained any more and doesn't support
React 19. This copies the package source into `design-system`,
makes the necessary changes for 19 and export it for others to use.

* Fix missing awaits

* Disable modal exit animations

Enabling modal exit animations via isExiting prop is causing
modals to be rendered in "hidden" state and never unmount.
Seems to be an issue with react-aria-components,
see https://github.com/adobe/react-spectrum/issues/7563.
Can probably be fixed by rewriting to a solution similar to
https://react-spectrum.adobe.com/react-aria/examples/framer-modal-sheet.html

* Remove unstable cache implementation and use in memory cache locally

* Fix ref type in SelectFilter

* Use cloneElement to add key prop to element


Approved-by: Linus Flood
2025-06-02 11:11:50 +00:00

148 lines
3.7 KiB
TypeScript

import { cloneElement } from "react"
import { renderOptions } from "./renderOptions"
import type { EmbedByUid } from "@/types/components/deprecatedjsontohtml"
import type { Embeds } from "@/types/requests/embeds"
import type { Node } from "@/types/requests/utils/edges"
import {
AvailableParagraphFormatEnum,
AvailableULFormatEnum,
RTETypeEnum,
} from "@/types/rte/enums"
import {
RTEMarkType,
type RTENode,
type RTERenderMark,
type RTERenderOptionComponent,
type RTETextNode,
} from "@/types/rte/node"
import type { RenderOptions } from "@/types/rte/option"
export function groupEmbedsByUid(embedsArray: Node<Embeds>[]) {
const embedsByUid = embedsArray.reduce<EmbedByUid>((acc, embed) => {
if (embed.node.system?.uid) {
acc[embed.node.system.uid] = embed
}
return acc
}, {})
return embedsByUid
}
export function nodeChildrenToHtml(
nodes: RTENode[],
embeds: EmbedByUid,
fullRenderOptions: RenderOptions
): any {
return nodes
.map((node, i) => {
// This function either returns a JSX element or null
const element = nodeToHtml(node, embeds, fullRenderOptions)
if (!element) {
return null
}
return cloneElement(element, {
key: `child-rte-${i}`,
})
})
.filter(Boolean)
}
export function textNodeToHtml(
node: RTETextNode,
fullRenderOptions: RenderOptions
) {
let text = <>{node.text}</>
if (node.classname || node.id) {
text = (fullRenderOptions[RTEMarkType.classnameOrId] as RTERenderMark)(
text,
node.classname,
node.id
)
}
if (node.break) {
text = (fullRenderOptions[RTEMarkType.break] as RTERenderMark)(text)
}
if (node.superscript) {
text = (fullRenderOptions[RTEMarkType.superscript] as RTERenderMark)(text)
}
if (node.subscript) {
text = (fullRenderOptions[RTEMarkType.subscript] as RTERenderMark)(text)
}
if (node.inlineCode) {
text = (fullRenderOptions[RTEMarkType.inlineCode] as RTERenderMark)(text)
}
if (node.strikethrough) {
text = (fullRenderOptions[RTEMarkType.strikethrough] as RTERenderMark)(text)
}
if (node.underline) {
text = (fullRenderOptions[RTEMarkType.underline] as RTERenderMark)(text)
}
if (node.italic) {
text = (fullRenderOptions[RTEMarkType.italic] as RTERenderMark)(text)
}
if (node.bold) {
text = (fullRenderOptions[RTEMarkType.bold] as RTERenderMark)(text)
}
return text
}
function next(
nodes: RTENode[],
embeds: EmbedByUid,
fullRenderOptions: RenderOptions
) {
return nodeChildrenToHtml(nodes, embeds, fullRenderOptions)
}
export function hasAvailableParagraphFormat(className?: string) {
if (!className) {
return false
}
return Object.keys(AvailableParagraphFormatEnum).includes(className)
}
export function hasAvailableULFormat(className?: string) {
if (!className) {
return false
}
return Object.keys(AvailableULFormatEnum).includes(className)
}
export function nodeToHtml(
node: RTENode,
embeds: EmbedByUid,
fullRenderOptions: RenderOptions
) {
if ("type" in node === false) {
return textNodeToHtml(node, fullRenderOptions)
} else {
if (fullRenderOptions[node.type] !== undefined) {
if (node.type === RTETypeEnum.doc) {
return null
}
return (fullRenderOptions[node.type] as RTERenderOptionComponent)(
node,
embeds,
next,
fullRenderOptions
)
} else {
return next(node.children, embeds, fullRenderOptions)
}
}
}
export function nodesToHtml(
nodes: RTENode[],
embedsArray: Node<Embeds>[],
overrideRenderOptions: RenderOptions
) {
const embeds = groupEmbedsByUid(embedsArray)
const fullRenderOptions = { ...renderOptions, ...overrideRenderOptions }
return nodes.map((node) => nodeToHtml(node, embeds, fullRenderOptions))
}