feat: json rich text editor, blocks, asides, general structure

This commit is contained in:
Simon Emanuelsson
2024-02-07 11:57:36 +01:00
parent 2bd4e25403
commit 66faa41e98
53 changed files with 966 additions and 211 deletions

View File

@@ -1,9 +1,34 @@
import { Fragment } from "react"
import Puff from "./Asides/Puff"
import { AsideTypenameEnum } from "@/types/requests/utils/typename"
import type { AsideProps } from "@/types/components/current/aside"
export default function Aside({ }: AsideProps) {
return (
<>
export default function Aside({ blocks }: AsideProps) {
if (!blocks?.length) {
return null
}
</>
return (
<aside>
{blocks.map((block, idx) => {
const type = block.__typename
switch (type) {
case AsideTypenameEnum.CurrentBlocksPageAsideContact:
return null
case AsideTypenameEnum.CurrentBlocksPageAsidePuff:
return (
<Fragment key={`puff-${idx}`}>
{block.puff.puffConnection.edges.map(puff => (
<Puff key={puff.node.system.uid} {...puff.node} />
))}
</Fragment>
)
default:
return null
}
})}
</aside>
)
}

View File

@@ -0,0 +1,60 @@
import { renderOptions } from "./renderOptions"
import Image from "next/image"
import JsonToHtml from "@/components/JsonToHtml"
import Link from "next/link"
import styles from "./puff.module.css"
import type { PuffProps } from "@/types/components/current/asides/puff"
export default function Puff({ imageConnection, is_internal, link, link_text, pageConnection, system, text, title }: PuffProps) {
return is_internal ? (
<Link className={styles.link} href={link.href}>
<PuffContent
imageConnection={imageConnection}
text={text}
title={title}
/>
</Link>
) : (
<a className={styles.link} href={link.href} target="_blank">
<PuffContent
imageConnection={imageConnection}
text={text}
title={title}
/>
</a>
)
}
function PuffContent({ imageConnection, text, title }: Pick<PuffProps, "imageConnection" | "text" | "title">) {
return (
<article>
{imageConnection.edges.map(image => (
<Image
alt={image.node.title}
className={styles.image}
height={image.node.dimension.height}
key={image.node.system.uid}
src={image.node.url}
unoptimized
width={image.node.dimension.width}
/>
))}
<section className={styles.content}>
<header className="content-teaser__body-wrapper">
<h3 className={styles.heading}>
{title}
</h3>
</header>
<JsonToHtml
embeds={text.embedded_itemsConnection.edges}
nodes={text.json.children}
renderOptions={renderOptions}
/>
</section>
</article>
)
}

View File

@@ -0,0 +1,47 @@
.link {
display: inline-block;
transition: 200ms ease;
}
.link:hover {
text-decoration: none;
transform: scale(1.01);
}
.image {
height: auto;
object-fit: contain;
object-position: center;
}
.content {
padding: 20px 0px;
}
.heading {
color: #00838e;
font-family: Helvetica, Arial, sans-serif;
font-weight: 400;
line-height: normal;
margin-bottom: 0;
text-decoration: none;
text-transform: none;
}
.link:hover .heading {
text-decoration: underline;
}
.p {
color: #333;
line-height: 1.3;
margin-bottom: 0;
padding-top: 7px;
text-decoration: none;
}
@media screen and (min-width: 950px) {
.heading {
font-size: 1.375rem;
}
}

View File

@@ -0,0 +1,13 @@
import styles from "./puff.module.css"
import { RTETypeEnum } from "@/types/rte/enums"
import type { EmbedByUid } from "@/types/components/jsontohtml"
import type { RTENext, RTEDefaultNode } from "@/types/rte/node"
import type { RenderOptions } from "@/types/rte/option"
export const renderOptions: RenderOptions = {
[RTETypeEnum.p]: (node: RTEDefaultNode, embeds: EmbedByUid, next: RTENext, fullRenderOptions: RenderOptions) => {
return <p className={styles.p}>{next(node.children, embeds, fullRenderOptions)}</p>
},
}

View File

@@ -1,5 +1,4 @@
import List from "./Blocks/List"
import Preamble from "./Blocks/Preamble"
import Puffs from "./Blocks/Puffs"
import Text from "./Blocks/Text"
@@ -12,14 +11,12 @@ export default function Blocks({ blocks }: BlocksProps) {
}
return (
<>
<section id="mainbody_personalized">
{blocks.map(block => {
const type = block.__typename
switch (type) {
case BlocksTypenameEnum.CurrentBlocksPageBlocksList:
return <List key={block.__typename} {...block} />
case BlocksTypenameEnum.CurrentBlocksPageBlocksPreamble:
return <Preamble key={block.__typename} {...block} />
case BlocksTypenameEnum.CurrentBlocksPageBlocksPuffs:
return <Puffs key={block.__typename} {...block} />
case BlocksTypenameEnum.CurrentBlocksPageBlocksText:
@@ -29,6 +26,7 @@ export default function Blocks({ blocks }: BlocksProps) {
return null
}
})}
</>
<div id="mainarea_personalized"></div>
</section>
)
}

View File

@@ -1,7 +0,0 @@
export default function Preamble() {
return (
<>
</>
)
}

View File

@@ -1,32 +1,12 @@
import { rteType } from "@/types/rte";
import Image from "next/image";
import JsonToHtml from "@/components/JsonToHtml";
import type { TextProps } from "@/types/components/current/blocks/text"
export default function Text({ text }: TextProps) {
return (
<>
<pre>{JSON.stringify(text.content.json, null, 2)}</pre>
{text.content.json.children.map(block => {
switch (block.type) {
case rteType.reference: {
if (block.attrs.type === rteType.asset) {
// return (
// <Image
// alt={block.attrs.alt}
// src={block.attrs["asset-link"]}
// />
// )
}
return null
}
default:
break;
}
})}
</>
<JsonToHtml
embeds={text.content.embedded_itemsConnection.edges}
nodes={text.content.json.children}
/>
)
}

View File

@@ -18,12 +18,13 @@ export default async function Footer({ lang }: LangParams) {
<div className="global-footer__content">
<h2 className="global-footer__content__heading">
<Image
src={footerData.logoConnection.edges[0].node.url}
data-js="scandiclogoimg"
alt={footerData.logoConnection.edges[0].node.title}
height="22"
width="102"
data-js="scandiclogoimg"
data-nosvgsrc="/Static/img/scandic-logotype-white.png" // what here?
height="22"
src={footerData.logoConnection.edges[0].node.url}
unoptimized
width="102"
/>
<span className="hidden--accessible">Scandic</span>
</h2>
@@ -72,12 +73,13 @@ export default async function Footer({ lang }: LangParams) {
rel="noopener"
>
<Image
height={45}
width={135}
src={footerData.app_downloads.app_store.imageConnection.edges[0].node.url}
alt={
footerData.app_downloads.app_store.imageConnection.edges[0].node.title
}
height={45}
src={footerData.app_downloads.app_store.imageConnection.edges[0].node.url}
unoptimized
width={135}
/>
</a>
</td>
@@ -95,14 +97,15 @@ export default async function Footer({ lang }: LangParams) {
rel="noopener"
>
<Image
height={45}
width={135}
src={
footerData.app_downloads.google_play.imageConnection.edges[0].node.url
}
alt={
footerData.app_downloads.google_play.imageConnection.edges[0].node.title
}
height={45}
src={
footerData.app_downloads.google_play.imageConnection.edges[0].node.url
}
unoptimized
width={135}
/>
</a>
</td>
@@ -215,10 +218,11 @@ display: none;
<div className="global-footer__trip-advisor">
<h3 className="heading-5 u-uppercase">{footerData.trip_advisor.title}</h3>
<Image
width={160}
alt="Trip Advisor logotype"
height={24}
src={footerData.trip_advisor.logoConnection.edges[0].node.url}
alt="Trip Advisor logotype"
unoptimized
width={160}
/>
</div>
</div>

View File

@@ -0,0 +1,32 @@
.nav {
padding-bottom: 8px;
}
.list {
align-items: center;
display: grid;
gap: 7px;
grid-auto-flow: column;
justify-content: flex-start;
}
.link {
color: #333;
}
.currentPage {
color: #7f7369;
margin-bottom: 0px;
}
.currentPage,
.li {
font-size: .875rem;
line-height: 1.4em;
}
.li::before,
.currentPage::before {
content: "";
margin-right: 4px;
}

View File

@@ -0,0 +1,31 @@
import Link from "next/link"
import styles from "./breadcrumbs.module.css"
import type { BreadcrumbsProps } from "@/types/components/current/breadcrumbs"
export default function Breadcrumbs({ breadcrumbs, parent, title }: BreadcrumbsProps) {
return (
<nav className={styles.nav}>
<ul className={styles.list}>
{parent ? (
<li className="breadcrumb-list__parent hidden-medium hidden-large">
<Link href={parent.node.url}>
{parent.node.breadcrumbs?.title ?? parent.node.title}
</Link>
</li>
) : null}
{breadcrumbs.edges.map(breadcrumb => (
<li className={styles.li} itemProp="breadcrumb" key={breadcrumb.node.title}>
<Link className={styles.link} href={breadcrumb.node.url}>
{breadcrumb.node.breadcrumbs?.title ?? breadcrumb.node.title}
</Link>
</li>
))}
<li className={styles.currentPage}>
<span>{title}</span>
</li>
</ul>
</nav>
)
}

View File

@@ -0,0 +1,26 @@
import { renderOptions } from "./renderOptions"
import Breadcrumbs from "./Breadcrumbs"
import JsonToHtml from "@/components/JsonToHtml"
import styles from "./preamble.module.css"
import type { PreambleProps } from "@/types/components/current/preamble"
export default function Preamble({ breadcrumbs, breadcrumbParent, breadcrumbTitle, preamble, title }: PreambleProps) {
return (
<section className={styles.container}>
<section>
<Breadcrumbs breadcrumbs={breadcrumbs} parent={breadcrumbParent} title={breadcrumbTitle} />
<h1>{title}</h1>
{preamble?.text ? (
<JsonToHtml
embeds={preamble.text.embedded_itemsConnection.edges}
nodes={preamble.text.json.children}
renderOptions={renderOptions}
/>
) : null}
</section>
</section>
)
}

View File

@@ -0,0 +1,25 @@
.container {
display: grid;
gap: 60px;
grid-template-columns: 2fr 1fr;
margin: 0 auto;
max-width: 1200px;
padding: 30px 0px 45px;
}
.preamble {
color: #333;
font-family: Helvetica Neue, Roboto, Helvetica, Arial, sans-serif;
font-size: 1.25rem;
font-weight: 300;
line-height: normal;
margin-bottom: 0px;
text-transform: none;
}
@media screen and (min-width: 950px) {
.preamble {
font-size: 1.5rem;
line-height: 2.25rem;
}
}

View File

@@ -0,0 +1,13 @@
import styles from "./preamble.module.css"
import { RTETypeEnum } from "@/types/rte/enums"
import type { EmbedByUid } from "@/types/components/jsontohtml"
import type { RTENext, RTEDefaultNode } from "@/types/rte/node"
import type { RenderOptions } from "@/types/rte/option"
export const renderOptions: RenderOptions = {
[RTETypeEnum.p]: (node: RTEDefaultNode, embeds: EmbedByUid, next: RTENext, fullRenderOptions: RenderOptions) => {
return <p className={styles.preamble}>{next(node.children, embeds, fullRenderOptions)}</p>
},
}

View File

@@ -0,0 +1,11 @@
import styles from "./section.module.css"
export default function Section({ children }: React.PropsWithChildren) {
return (
<div className={styles.wrapper}>
<section className={styles.section}>
{children}
</section>
</div>
)
}

View File

@@ -0,0 +1,25 @@
.wrapper {
background-color: #fff;
}
.section {
box-sizing: content-box;
display: grid;
gap: 70px;
grid-template-columns: 2fr 1fr;
margin: 0 auto;
max-width: 1200px;
padding: 20px 10px 5px;
}
@media screen and (min-width: 740px) {
.section {
padding: 30px 30px 15px;
}
}
@media screen and (min-width: 1200px) {
.section {
padding: 50px 30px 35px;
}
}

View File

@@ -0,0 +1,31 @@
import Link from "next/link"
import type { SubnavMobileProps } from "@/types/components/current/subnavMobile"
export default async function SubnavMobile({ breadcrumbs, parent, title }: SubnavMobileProps) {
return (
<div className="subnav-mobile hidden-small hidden-medium hidden-large">
<nav className="u-flex">
<ul className="breadcrumb-list hidden-small hidden-medium hidden-large">
{parent ? (
<li className="breadcrumb-list__parent hidden-medium hidden-large">
<Link href={parent.node.url}>
{parent.node.breadcrumbs?.title ?? parent.node.title}
</Link>
</li>
) : null}
{breadcrumbs.edges.map(breadcrumb => (
<li className="breadcrumb-list__body" key={breadcrumb.node.url}>
<Link href={breadcrumb.node.url}>
{breadcrumb.node.breadcrumbs?.title ?? breadcrumb.node.title}
</Link>
</li>
))}
<li className="breadcrumb-list__body">
<span>{title}</span>
</li>
</ul>
</nav>
</div>
)
}

View File

@@ -0,0 +1,10 @@
import { nodesToHtml } from "./utils"
import type { JsonToHtmlProps } from "@/types/components/jsontohtml"
export default function JsonToHtml({ embeds, nodes, renderOptions = {} }: JsonToHtmlProps) {
if (!Array.isArray(nodes) || !nodes.length) {
return null
}
return <>{nodesToHtml(nodes, embeds, renderOptions).filter(Boolean)}</>
}

View File

@@ -0,0 +1,6 @@
.image {
height: auto;
margin-bottom: 16px;
max-width: 100%;
object-fit: cover;
}

View File

@@ -0,0 +1,116 @@
import Image from "next/image"
import Link from "next/link"
import { EmbedEnum } from "@/types/requests/utils/embeds"
import { RTEItemTypeEnum, RTETypeEnum } from "@/types/rte/enums"
import { RTEMarkType } from "@/types/rte/node"
import styles from "./jsontohtml.module.css"
import type { EmbedByUid } from "@/types/components/jsontohtml"
import type { RTENext, RTEDefaultNode, RTENode, RTERegularNode } from "@/types/rte/node"
import type { RenderOptions } from "@/types/rte/option"
export const renderOptions: RenderOptions = {
[RTETypeEnum.a]: (node: RTERegularNode, embeds: EmbedByUid, next: RTENext, fullRenderOptions: RenderOptions) => {
if (node.attrs.url) {
return (
<a href={node.attrs.url} target={node.attrs.target ?? "_blank"}>{next(node.children, embeds, fullRenderOptions)}</a>
)
}
return null
},
[RTETypeEnum.h1]: (node: RTEDefaultNode, embeds: EmbedByUid, next: RTENext, fullRenderOptions: RenderOptions) => {
return <h1 className={node.attrs["class-name"] ?? ""}>{next(node.children, embeds, fullRenderOptions)}</h1>
},
[RTETypeEnum.h2]: (node: RTEDefaultNode, embeds: EmbedByUid, next: RTENext, fullRenderOptions: RenderOptions) => {
return <h2 className={node.attrs["class-name"] ?? ""}>{next(node.children, embeds, fullRenderOptions)}</h2>
},
[RTETypeEnum.h3]: (node: RTEDefaultNode, embeds: EmbedByUid, next: RTENext, fullRenderOptions: RenderOptions) => {
return <h3 className={node.attrs["class-name"] ?? ""}>{next(node.children, embeds, fullRenderOptions)}</h3>
},
[RTETypeEnum.h4]: (node: RTEDefaultNode, embeds: EmbedByUid, next: RTENext, fullRenderOptions: RenderOptions) => {
return <h4 className={node.attrs["class-name"] ?? ""}>{next(node.children, embeds, fullRenderOptions)}</h4>
},
[RTETypeEnum.h5]: (node: RTEDefaultNode, embeds: EmbedByUid, next: RTENext, fullRenderOptions: RenderOptions) => {
return <h5 className={node.attrs["class-name"] ?? ""}>{next(node.children, embeds, fullRenderOptions)}</h5>
},
[RTETypeEnum.h6]: (node: RTEDefaultNode, embeds: EmbedByUid, next: RTENext, fullRenderOptions: RenderOptions) => {
return <h6 className={node.attrs["class-name"] ?? ""}>{next(node.children, embeds, fullRenderOptions)}</h6>
},
[RTETypeEnum.p]: (node: RTEDefaultNode, embeds: EmbedByUid, next: RTENext, fullRenderOptions: RenderOptions) => {
return <p className={node.attrs["class-name"] ?? ""}>{next(node.children, embeds, fullRenderOptions)}</p>
},
[RTETypeEnum.reference]: (node: RTENode, embeds: EmbedByUid, next: RTENext, fullRenderOptions: RenderOptions) => {
if ("attrs" in node) {
const type = node.attrs.type
if (type === RTEItemTypeEnum.asset) {
const image = embeds?.[node?.attrs?.["asset-uid"]]
if (image.node.__typename === EmbedEnum.SysAsset) {
const alt = image?.node?.title ?? node.attrs.alt
return (
<Image
alt={alt}
className={styles.image}
height={image.node.dimension.height}
src={image?.node?.url}
unoptimized
width={image.node.dimension.width}
/>
)
}
} else {
return (
<Link href={node.attrs.href}>
{next(node.children, embeds, fullRenderOptions)}
</Link>
)
}
}
return null
},
[RTEMarkType.bold]: (children: React.ReactNode) => {
return <strong>{children}</strong>
},
[RTEMarkType.italic]: (children: React.ReactNode) => {
return <em>{children}</em>
},
[RTEMarkType.underline]: (children: React.ReactNode) => {
return <u>{children}</u>
},
[RTEMarkType.strikethrough]: (children: React.ReactNode) => {
return <s>{children}</s>
},
[RTEMarkType.inlineCode]: (children: React.ReactNode) => {
return <span>{children}</span>
},
[RTEMarkType.subscript]: (children: React.ReactNode) => {
return <sub>{children}</sub>
},
[RTEMarkType.superscript]: (children: React.ReactNode) => {
return <sup>{children}</sup>
},
[RTEMarkType.break]: (children: React.ReactNode) => {
return (
<>
<br />
{children}
</>
)
},
[RTEMarkType.classnameOrId]: (children: React.ReactNode, className?: string, id?: string) => {
let props = {
className,
id,
}
if (!className) {
delete props.className
}
if (!id) {
delete props.id
}
return <span {...props}>{children}</span>
}
}

View File

@@ -0,0 +1,85 @@
import { renderOptions } from "./renderOptions"
import { RTEMarkType, RTERenderMark } from "@/types/rte/node"
import { RTETypeEnum } from "@/types/rte/enums"
import type { EmbedByUid } from "@/types/components/jsontohtml"
import type { Node } from "@/types/requests/utils/edges"
import type { RenderOptions } from "@/types/rte/option"
import type { RTENode, RTETextNode, RTERenderOptionComponent } from "@/types/rte/node"
import type { Embeds } from "@/types/requests/embeds"
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 => nodeToHtml(node, embeds, fullRenderOptions))
}
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 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))
}