@cntrl-site/sdk-nextjs 1.11.0-2 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/jest.config.js +4 -1
- package/lib/common/useComponentElement.js +40 -0
- package/lib/common/useLazyVideo.js +102 -0
- package/lib/components/Head.js +3 -3
- package/lib/components/Navigation/NavigationComponent.js +5 -4
- package/lib/components/Section/SectionVideo.js +4 -2
- package/lib/components/StructuredBlockItem/StructuredBlockItem.js +4 -4
- package/lib/components/StructuredBlockItem/StructuredCode/StructuredCode.js +5 -1
- package/lib/components/StructuredBlockItem/StructuredComponent/StructuredComponent.js +6 -5
- package/lib/components/StructuredBlockItem/StructuredEmbed/StructuredYoutubeEmbed.js +13 -1
- package/lib/components/StructuredBlockItem/StructuredImage/StructuredImage.js +3 -1
- package/lib/components/StructuredBlockItem/StructuredRichText/StructuredRichText.js +5 -5
- package/lib/components/StructuredBlockItem/StructuredTable/StructuredTable.js +102 -0
- package/lib/components/StructuredBlockItem/StructuredVideo/StructuredVideo.js +6 -7
- package/lib/components/StructuredBlockItem/blocksMap.js +7 -5
- package/lib/components/StructuredBlockItem/textStylesRule.js +7 -9
- package/lib/components/StructuredContent/StructuredContent.js +3 -2
- package/lib/components/StructuredContent/StructuredHeader.js +71 -23
- package/lib/components/items/ComponentItem/ComponentItem.js +7 -6
- package/lib/components/items/FileItem/VideoItem.js +3 -1
- package/lib/components/items/RichTextItem/useRichTextItem.js +3 -4
- package/lib/provider/CntrlSdkContext.js +0 -7
- package/lib/utils/RichTextConverter/RichTextConverter.js +8 -4
- package/package.json +3 -3
- package/src/common/useComponentElement.test.ts +71 -0
- package/src/common/useComponentElement.ts +48 -0
- package/src/common/useLazyVideo.test.tsx +215 -0
- package/src/common/useLazyVideo.ts +100 -0
- package/src/components/Head.tsx +2 -2
- package/src/components/Navigation/NavigationComponent.tsx +7 -4
- package/src/components/Section/SectionVideo.tsx +3 -2
- package/src/components/StructuredBlockItem/StructuredBlockItem.tsx +6 -4
- package/src/components/StructuredBlockItem/StructuredCode/StructuredCode.tsx +5 -1
- package/src/components/StructuredBlockItem/StructuredComponent/StructuredComponent.tsx +7 -4
- package/src/components/StructuredBlockItem/StructuredEmbed/StructuredYoutubeEmbed.tsx +12 -1
- package/src/components/StructuredBlockItem/StructuredImage/StructuredImage.tsx +3 -1
- package/src/components/StructuredBlockItem/StructuredRichText/StructuredRichText.tsx +4 -3
- package/src/components/StructuredBlockItem/StructuredTable/StructuredTable.tsx +118 -0
- package/src/components/StructuredBlockItem/StructuredVideo/StructuredVideo.tsx +7 -10
- package/src/components/StructuredBlockItem/blocksMap.ts +7 -6
- package/src/components/StructuredBlockItem/textStylesRule.ts +7 -9
- package/src/components/StructuredContent/StructuredContent.test.tsx +141 -14
- package/src/components/StructuredContent/StructuredContent.tsx +3 -2
- package/src/components/StructuredContent/StructuredHeader.tsx +78 -24
- package/src/components/items/ComponentItem/ComponentItem.tsx +8 -5
- package/src/components/items/FileItem/VideoItem.tsx +3 -2
- package/src/components/items/RichTextItem/RichTextItem.tsx +1 -0
- package/src/components/items/RichTextItem/useRichTextItem.ts +3 -4
- package/src/provider/CntrlSdkContext.ts +1 -8
- package/src/utils/RichTextConverter/RichTextConverter.test.tsx +6 -4
- package/src/utils/RichTextConverter/RichTextConverter.tsx +9 -4
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { getLayoutStyles, Layout, StructuredBlockTableCell, TableStructuredBlock } from '@cntrl-site/sdk';
|
|
2
|
+
import { FC, ReactNode, useId, useMemo, useState } from 'react';
|
|
3
|
+
import JSXStyle from 'styled-jsx/style';
|
|
4
|
+
import { useCntrlContext } from '../../../provider/useCntrlContext';
|
|
5
|
+
import { useItemGeometry } from '../../../ItemGeometry/useItemGeometry';
|
|
6
|
+
import { LIST_STYLES_ID, RichTextConverter } from '../../../utils/RichTextConverter/RichTextConverter';
|
|
7
|
+
import { StructuredBlockItemProps } from '../StructuredBlockItem';
|
|
8
|
+
import { colorRule, textStylesRule } from '../textStylesRule';
|
|
9
|
+
|
|
10
|
+
// the lines between the cells, in pixels of the layout, scaled with it like everything drawn in it
|
|
11
|
+
const BORDER_PX = 1;
|
|
12
|
+
|
|
13
|
+
const richTextConverter = new RichTextConverter();
|
|
14
|
+
|
|
15
|
+
/** Every cell as elements, with the styles of its ranges; the list styles once, where any cell has a list. */
|
|
16
|
+
function convertCells(block: TableStructuredBlock, layouts: Layout[]): { cells: Map<string, ReactNode[]>; rangeStyles: string; listStyles: string | undefined } {
|
|
17
|
+
const { header, rows } = block.commonParams;
|
|
18
|
+
const cells = new Map<string, ReactNode[]>();
|
|
19
|
+
const rangeStyles: string[] = [];
|
|
20
|
+
let listStyles: string | undefined;
|
|
21
|
+
for (const cell of [...header, ...rows.flat()]) {
|
|
22
|
+
const source = {
|
|
23
|
+
id: `${block.id}-${cell.id}`,
|
|
24
|
+
commonParams: cell,
|
|
25
|
+
layoutParams: Object.fromEntries(layouts.map(layout => [layout.id, { rangeStyles: cell.styles[layout.id] ?? [], textAlign: block.layoutParams[layout.id].textAlign }]))
|
|
26
|
+
};
|
|
27
|
+
const [content, styles, lists] = richTextConverter.toHtml(source, layouts, { isScaledToViewport: false });
|
|
28
|
+
cells.set(cell.id, content);
|
|
29
|
+
rangeStyles.push(styles);
|
|
30
|
+
listStyles ??= lists;
|
|
31
|
+
}
|
|
32
|
+
return { cells, rangeStyles: rangeStyles.join('\n'), listStyles };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A table of one of three kinds, as served: its cells rich text set, spaced above and below and
|
|
37
|
+
* lined in the styles it comes with, their ranges styled over them, each column its share of the
|
|
38
|
+
* width and padded on either side as its own values say. Cells draw a heading row over a grid; rows
|
|
39
|
+
* draw every row plain, the two fills alternating from the first; lines draw the heading and a line
|
|
40
|
+
* between each pair of rows only.
|
|
41
|
+
*/
|
|
42
|
+
export const StructuredTable: FC<StructuredBlockItemProps<TableStructuredBlock>> = ({ block }) => {
|
|
43
|
+
const reactId = useId();
|
|
44
|
+
const { layouts } = useCntrlContext();
|
|
45
|
+
const [ref, setRef] = useState<HTMLTableElement | null>(null);
|
|
46
|
+
useItemGeometry(block.id, ref);
|
|
47
|
+
const { kind, header, rows } = block.commonParams;
|
|
48
|
+
const { cells: content, rangeStyles, listStyles } = useMemo(() => convertCells(block, layouts), [block, layouts]);
|
|
49
|
+
const headingAsRow = kind === 'rows';
|
|
50
|
+
const Heading = headingAsRow ? 'td' : 'th';
|
|
51
|
+
const table = `.structured-table-${block.id}`;
|
|
52
|
+
const cells = `${table} th, ${table} td`;
|
|
53
|
+
const cellContent = (cell: StructuredBlockTableCell) => content.get(cell.id);
|
|
54
|
+
return (
|
|
55
|
+
<>
|
|
56
|
+
<table ref={setRef} className={`structured-table-${block.id}`}>
|
|
57
|
+
<colgroup>
|
|
58
|
+
{header.map((_, col) => <col key={col} />)}
|
|
59
|
+
</colgroup>
|
|
60
|
+
<thead className={`structured-table-heading-${block.id}`}>
|
|
61
|
+
<tr>{header.map(cell => <Heading key={cell.id}>{cellContent(cell)}</Heading>)}</tr>
|
|
62
|
+
</thead>
|
|
63
|
+
<tbody className={`structured-table-body-${block.id}`}>
|
|
64
|
+
{rows.map((row, index) => <tr key={row[0]?.id ?? index}>{row.map(cell => <td key={cell.id}>{cellContent(cell)}</td>)}</tr>)}
|
|
65
|
+
</tbody>
|
|
66
|
+
</table>
|
|
67
|
+
{listStyles && <JSXStyle id={LIST_STYLES_ID}>{listStyles}</JSXStyle>}
|
|
68
|
+
<JSXStyle id={`${reactId}-table-${block.id}`}>
|
|
69
|
+
{rangeStyles}
|
|
70
|
+
{`
|
|
71
|
+
${table} {
|
|
72
|
+
width: 100%;
|
|
73
|
+
table-layout: fixed;
|
|
74
|
+
border-collapse: collapse;
|
|
75
|
+
}
|
|
76
|
+
${cells} {
|
|
77
|
+
box-sizing: border-box;
|
|
78
|
+
vertical-align: top;
|
|
79
|
+
text-align: inherit;
|
|
80
|
+
font-weight: inherit;
|
|
81
|
+
border-style: solid;
|
|
82
|
+
white-space: pre-wrap;
|
|
83
|
+
overflow-wrap: anywhere;
|
|
84
|
+
}
|
|
85
|
+
${getLayoutStyles(layouts, [block.layoutParams], ([layoutParams], exemplary) => {
|
|
86
|
+
const vw = (value: number) => `${value * 100}vw`;
|
|
87
|
+
const border = vw(BORDER_PX / exemplary);
|
|
88
|
+
const fill = (selector: string, color: string | undefined) => (color ? colorRule(selector, 'background-color', color) : '');
|
|
89
|
+
return (`
|
|
90
|
+
${textStylesRule(table, layoutParams)}
|
|
91
|
+
${cells} {
|
|
92
|
+
padding-top: ${vw(layoutParams.cellPaddingTop)};
|
|
93
|
+
padding-bottom: ${vw(layoutParams.cellPaddingBottom)};
|
|
94
|
+
border-width: ${kind === 'lines' ? '0' : border};
|
|
95
|
+
}
|
|
96
|
+
${kind === 'lines' ? `${table} tbody > tr > th, ${table} tbody > tr > td { border-top-width: ${border}; }` : ''}
|
|
97
|
+
${layoutParams.columns.map((column, index) => `
|
|
98
|
+
${table} col:nth-child(${index + 1}) {
|
|
99
|
+
width: ${column.width * 100}%;
|
|
100
|
+
}
|
|
101
|
+
${table} th:nth-child(${index + 1}), ${table} td:nth-child(${index + 1}) {
|
|
102
|
+
padding-left: ${vw(column.paddingLeft)};
|
|
103
|
+
padding-right: ${vw(column.paddingRight)};
|
|
104
|
+
}
|
|
105
|
+
`).join('')}
|
|
106
|
+
${layoutParams.borderColor ? colorRule(cells, 'border-color', layoutParams.borderColor) : ''}
|
|
107
|
+
${headingAsRow
|
|
108
|
+
? `${fill(`${table} thead tr, ${table} tbody tr:nth-child(even)`, layoutParams.headingBackground)}
|
|
109
|
+
${fill(`${table} tbody tr:nth-child(odd)`, layoutParams.bodyBackground)}`
|
|
110
|
+
: `${fill(`.structured-table-heading-${block.id}`, layoutParams.headingBackground)}
|
|
111
|
+
${fill(`.structured-table-body-${block.id}`, layoutParams.bodyBackground)}`}
|
|
112
|
+
`);
|
|
113
|
+
})}
|
|
114
|
+
`}
|
|
115
|
+
</JSXStyle>
|
|
116
|
+
</>
|
|
117
|
+
);
|
|
118
|
+
};
|
|
@@ -16,7 +16,6 @@ export const StructuredVideo: FC<StructuredBlockItemProps<VideoStructuredBlock>>
|
|
|
16
16
|
const [ref, setRef] = useState<HTMLDivElement | null>(null);
|
|
17
17
|
const [video, setVideo] = useState<HTMLVideoElement | null>(null);
|
|
18
18
|
const [isLoaded, setIsLoaded] = useState(false);
|
|
19
|
-
const [isPlaying, setIsPlaying] = useState(false);
|
|
20
19
|
const [hasPlayed, setHasPlayed] = useState(false);
|
|
21
20
|
useItemGeometry(block.id, ref);
|
|
22
21
|
const { url, coverUrl } = block.commonParams;
|
|
@@ -25,11 +24,12 @@ export const StructuredVideo: FC<StructuredBlockItemProps<VideoStructuredBlock>>
|
|
|
25
24
|
const start = () => {
|
|
26
25
|
if (!video) return;
|
|
27
26
|
setHasPlayed(true);
|
|
28
|
-
|
|
27
|
+
// a play the browser refuses -- an unmuted hover, a pause before it began -- leaves the cover up
|
|
28
|
+
video.play().catch(() => setHasPlayed(false));
|
|
29
29
|
};
|
|
30
30
|
return (
|
|
31
31
|
<div ref={setRef} className={`structured-video-${block.id}${isLoaded ? '' : ` structured-video-loading-${block.id}`}`}>
|
|
32
|
-
{layoutParams && (
|
|
32
|
+
{layoutParams && url && (
|
|
33
33
|
<>
|
|
34
34
|
<video
|
|
35
35
|
key={layoutId}
|
|
@@ -41,13 +41,10 @@ export const StructuredVideo: FC<StructuredBlockItemProps<VideoStructuredBlock>>
|
|
|
41
41
|
controls={layoutParams.controls}
|
|
42
42
|
loop
|
|
43
43
|
playsInline
|
|
44
|
-
|
|
44
|
+
// a video behind its cover is fetched once it is asked for, not with the page
|
|
45
|
+
preload={play === 'auto' ? 'auto' : 'metadata'}
|
|
45
46
|
onLoadedMetadata={() => setIsLoaded(true)}
|
|
46
|
-
onPlay={() =>
|
|
47
|
-
setIsPlaying(true);
|
|
48
|
-
setHasPlayed(true);
|
|
49
|
-
}}
|
|
50
|
-
onPause={() => setIsPlaying(false)}
|
|
47
|
+
onPlay={() => setHasPlayed(true)}
|
|
51
48
|
onMouseEnter={() => {
|
|
52
49
|
if (play === 'on-hover') start();
|
|
53
50
|
}}
|
|
@@ -69,7 +66,7 @@ export const StructuredVideo: FC<StructuredBlockItemProps<VideoStructuredBlock>>
|
|
|
69
66
|
{play === 'on-click' && !layoutParams.controls && (
|
|
70
67
|
<div
|
|
71
68
|
className={`structured-video-overlay-${block.id}`}
|
|
72
|
-
onClick={() => (
|
|
69
|
+
onClick={() => (video && !video.paused ? video.pause() : start())}
|
|
73
70
|
/>
|
|
74
71
|
)}
|
|
75
72
|
</>
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { StructuredBlockType } from '@cntrl-site/sdk';
|
|
1
|
+
import { StackBlockType, StructuredBlockType } from '@cntrl-site/sdk';
|
|
2
2
|
import { ComponentType } from 'react';
|
|
3
3
|
import type { StructuredBlockItemProps } from './StructuredBlockItem';
|
|
4
4
|
import { StructuredComponent } from './StructuredComponent/StructuredComponent';
|
|
@@ -8,12 +8,14 @@ import { StructuredHeaderImage } from './StructuredHeaderImage/StructuredHeaderI
|
|
|
8
8
|
import { StructuredVideo } from './StructuredVideo/StructuredVideo';
|
|
9
9
|
import { StructuredDivider } from './StructuredDivider/StructuredDivider';
|
|
10
10
|
import { StructuredCode } from './StructuredCode/StructuredCode';
|
|
11
|
+
import { StructuredTable } from './StructuredTable/StructuredTable';
|
|
11
12
|
import { StructuredVimeoEmbed } from './StructuredEmbed/StructuredVimeoEmbed';
|
|
12
13
|
import { StructuredYoutubeEmbed } from './StructuredEmbed/StructuredYoutubeEmbed';
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
/** Drawn for a block of a type this build does not know, should one reach it: nothing, rather than a failed page. */
|
|
16
|
+
export const UnknownBlock = () => null;
|
|
15
17
|
|
|
16
|
-
export const blocksMap: Record<
|
|
18
|
+
export const blocksMap: Record<StackBlockType, ComponentType<StructuredBlockItemProps<any>>> = {
|
|
17
19
|
[StructuredBlockType.Component]: StructuredComponent,
|
|
18
20
|
[StructuredBlockType.RichText]: StructuredRichText,
|
|
19
21
|
[StructuredBlockType.Quote]: StructuredRichText,
|
|
@@ -23,8 +25,7 @@ export const blocksMap: Record<StructuredBlockType, ComponentType<StructuredBloc
|
|
|
23
25
|
[StructuredBlockType.Video]: StructuredVideo,
|
|
24
26
|
[StructuredBlockType.Divider]: StructuredDivider,
|
|
25
27
|
[StructuredBlockType.Code]: StructuredCode,
|
|
28
|
+
[StructuredBlockType.Table]: StructuredTable,
|
|
26
29
|
[StructuredBlockType.VimeoEmbed]: StructuredVimeoEmbed,
|
|
27
|
-
[StructuredBlockType.YoutubeEmbed]: StructuredYoutubeEmbed
|
|
28
|
-
// a section's header is drawn by its section, never in the stack
|
|
29
|
-
[StructuredBlockType.Header]: noop
|
|
30
|
+
[StructuredBlockType.YoutubeEmbed]: StructuredYoutubeEmbed
|
|
30
31
|
};
|
|
@@ -4,7 +4,6 @@ import { getFontFamilyValue } from '../../utils/getFontFamilyValue';
|
|
|
4
4
|
|
|
5
5
|
/** A block's text styles as a CSS rule for `selector`, its lengths in viewport units as the block's own. */
|
|
6
6
|
export function textStylesRule(selector: string, styles: StructuredBlockTextStyles): string {
|
|
7
|
-
const color = CntrlColor.parse(styles.color);
|
|
8
7
|
return `
|
|
9
8
|
${selector} {
|
|
10
9
|
font-family: ${getFontFamilyValue(styles.typeFace)};
|
|
@@ -18,17 +17,16 @@ export function textStylesRule(selector: string, styles: StructuredBlockTextStyl
|
|
|
18
17
|
text-transform: ${styles.textTransform};
|
|
19
18
|
vertical-align: ${styles.verticalAlign};
|
|
20
19
|
text-align: ${styles.textAlign};
|
|
21
|
-
color: ${color.fmt('oklch')};
|
|
22
|
-
}
|
|
23
|
-
@supports not (color: oklch(42% 0.3 90 / 1)) {
|
|
24
|
-
${selector} {
|
|
25
|
-
color: ${color.fmt('rgba')};
|
|
26
|
-
}
|
|
27
20
|
}
|
|
21
|
+
${colorRule(selector, 'color', styles.color)}
|
|
28
22
|
`;
|
|
29
23
|
}
|
|
30
24
|
|
|
31
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* The columns a text block flows in, the gap between them in viewport units, and whether its lines
|
|
27
|
+
* hyphenate. One column is written out too: the layouts' media queries stack upward, so a smaller
|
|
28
|
+
* layout's columns would otherwise carry into a larger layout set to one.
|
|
29
|
+
*/
|
|
32
30
|
export function textColumnsRule(
|
|
33
31
|
selector: string,
|
|
34
32
|
{ columns = 1, columnGap = 0, hyphens = Hyphens.None }: Pick<StructuredBlockRichTextLayoutParams, 'columns' | 'columnGap' | 'hyphens'>
|
|
@@ -37,7 +35,7 @@ export function textColumnsRule(
|
|
|
37
35
|
${selector} {
|
|
38
36
|
hyphens: ${hyphens};
|
|
39
37
|
-webkit-hyphens: ${hyphens};
|
|
40
|
-
${columns > 1 ? `column-count: ${columns}; column-gap: ${columnGap * 100}vw; column-fill: balance;` : ''}
|
|
38
|
+
${columns > 1 ? `column-count: ${columns}; column-gap: ${columnGap * 100}vw; column-fill: balance;` : 'column-count: auto; column-gap: normal;'}
|
|
41
39
|
}
|
|
42
40
|
`;
|
|
43
41
|
}
|
|
@@ -60,7 +60,10 @@ const quote = block(StructuredBlockType.Quote, 'quote', { ...paragraph('A quote'
|
|
|
60
60
|
...textStyles,
|
|
61
61
|
rangeStyles: [],
|
|
62
62
|
background: '#EEEEEE',
|
|
63
|
-
|
|
63
|
+
boxPaddingTop: 0.02,
|
|
64
|
+
boxPaddingRight: 0.03,
|
|
65
|
+
boxPaddingBottom: 0.02,
|
|
66
|
+
boxPaddingLeft: 0.01,
|
|
64
67
|
boxRadius: 0.01
|
|
65
68
|
});
|
|
66
69
|
const date = block(StructuredBlockType.Date, 'date', { ...paragraph('23 Sep 2026'), date: '2026-09-23', format: 'D MMM YYYY' }, {
|
|
@@ -81,10 +84,28 @@ const code = block(StructuredBlockType.Code, 'code', {
|
|
|
81
84
|
code: 'const a = 1;',
|
|
82
85
|
language: 'typescript',
|
|
83
86
|
theme: 'dracula',
|
|
84
|
-
lines: [[{ content: 'const', color: '#FF79C6', fontStyle: 2 }, { content: ' a = 1;', color: '#F8F8F2' }]],
|
|
87
|
+
lines: [[{ content: 'const', color: '#FF79C6', fontStyle: 2 }, { content: ' a = 1;', color: '#F8F8F2' }, { content: ' // gone', color: '#6272A4', fontStyle: 12 }]],
|
|
85
88
|
colors: { background: '#282A36', foreground: '#F8F8F2' }
|
|
86
89
|
}, {});
|
|
87
90
|
const divider = block(StructuredBlockType.Divider, 'divider', {}, { color: '#C0C0C0' }, { ...onGrid, height: 0.001 });
|
|
91
|
+
const tableCells = {
|
|
92
|
+
...textStyles,
|
|
93
|
+
cellPaddingTop: 0.005,
|
|
94
|
+
cellPaddingBottom: 0.005,
|
|
95
|
+
columns: [{ width: 0.6, paddingLeft: 0.01, paddingRight: 0.01 }, { width: 0.4, paddingLeft: 0.02, paddingRight: 0 }],
|
|
96
|
+
borderColor: '#C0C0C0',
|
|
97
|
+
headingBackground: '#EEEEEE',
|
|
98
|
+
bodyBackground: '#F8F8F8'
|
|
99
|
+
};
|
|
100
|
+
const cell = (id: string, text: string, styles: object = {}) => ({ id, ...paragraph(text), styles: { desktop: [], mobile: [], ...styles } });
|
|
101
|
+
const bold = [{ start: 0, end: 4, style: 'FONTWEIGHT', value: '700' }];
|
|
102
|
+
function tableContent(ids: string) {
|
|
103
|
+
return {
|
|
104
|
+
header: [cell(`${ids}-h0`, 'Name', { desktop: bold, mobile: bold }), cell(`${ids}-h1`, 'Price')],
|
|
105
|
+
rows: [[cell(`${ids}-r00`, 'Tea'), cell(`${ids}-r01`, '3')], [cell(`${ids}-r10`, 'Coffee'), cell(`${ids}-r11`, '4')]]
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
const table = block(StructuredBlockType.Table, 'table', { kind: 'cells', ...tableContent('t') }, tableCells);
|
|
88
109
|
const vimeo = block(StructuredBlockType.VimeoEmbed, 'vimeo', {
|
|
89
110
|
url: 'https://player.vimeo.com/video/1',
|
|
90
111
|
coverUrl: null,
|
|
@@ -97,7 +118,7 @@ const header = {
|
|
|
97
118
|
type: StructuredBlockType.Header,
|
|
98
119
|
hidden: {},
|
|
99
120
|
state: {},
|
|
100
|
-
area: { desktop: { zIndex: 2 }, mobile: { zIndex: 2 } },
|
|
121
|
+
area: { desktop: { zIndex: 2, paddingTop: 0.02 }, mobile: { zIndex: 2 } },
|
|
101
122
|
commonParams: { title: 'title', subtitle: 'subtitle', date: 'header-date', image: 'key-image' },
|
|
102
123
|
layoutParams: {
|
|
103
124
|
desktop: { variant: 'A', order: ['image', 'title', 'subtitle', 'date'], hiddenElements: [], imageFit: 'cover', coverHeight: 0.8 },
|
|
@@ -105,9 +126,9 @@ const header = {
|
|
|
105
126
|
}
|
|
106
127
|
};
|
|
107
128
|
|
|
108
|
-
const stack = [text, quote, date, gallery, video, code, divider, vimeo, component];
|
|
129
|
+
const stack = [text, quote, date, gallery, video, code, table, divider, vimeo, component];
|
|
109
130
|
|
|
110
|
-
function section(withHeader: boolean) {
|
|
131
|
+
function section(withHeader: boolean, type: Section['type'] = 'content-based') {
|
|
111
132
|
return {
|
|
112
133
|
id: 'content',
|
|
113
134
|
items: [],
|
|
@@ -115,7 +136,7 @@ function section(withHeader: boolean) {
|
|
|
115
136
|
minHeight: {},
|
|
116
137
|
color: {},
|
|
117
138
|
hidden: {},
|
|
118
|
-
type
|
|
139
|
+
type,
|
|
119
140
|
structuredContent: [title, headerImage, ...stack],
|
|
120
141
|
structuredContentSettings: { paddingBottom: { desktop: 0.02, mobile: 0.02 }, defaultWidth: { desktop: 0.3, mobile: 0.8 } },
|
|
121
142
|
...(withHeader && { header })
|
|
@@ -143,9 +164,15 @@ describe('StructuredContent', () => {
|
|
|
143
164
|
expect(items).toEqual(['title', 'key-image', ...stack.map(block => block.id)]);
|
|
144
165
|
});
|
|
145
166
|
|
|
146
|
-
it('places a block drawn from a preset on the content grid
|
|
167
|
+
it('places a block drawn from a preset on the content grid with its inner paddings, and no gap above it whatever its area holds', () => {
|
|
147
168
|
draw(section(false));
|
|
148
|
-
expect(css()).toMatch(/\.structured-block-item-text \{[^}]*margin-top:
|
|
169
|
+
expect(css()).toMatch(/\.structured-block-item-text \{[^}]*margin-top: 0;[^}]*left: 35vw;[^}]*width: 30vw;[^}]*padding-top: 0\.5vw;[^}]*padding-bottom: 0\.6vw;/);
|
|
170
|
+
expect(css()).not.toMatch(/\.structured-block-item-[\w-]+ \{[^}]*margin-top: [1-9]/);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('keeps the gap above a block of a default section', () => {
|
|
174
|
+
draw(section(false, 'default'));
|
|
175
|
+
expect(css()).toMatch(/\.structured-block-item-text \{[^}]*margin-top: 1vw;/);
|
|
149
176
|
expect(css()).toMatch(/\.structured-block-item-component \{[^}]*align-self: flex-start;/);
|
|
150
177
|
});
|
|
151
178
|
|
|
@@ -174,7 +201,7 @@ describe('StructuredContent', () => {
|
|
|
174
201
|
it('draws a quote in its box and a date as the text it reads as', () => {
|
|
175
202
|
const { container } = draw(section(false));
|
|
176
203
|
expect(container.querySelector('.structured-quote-box-quote')!.textContent!.trim()).toBe('A quote');
|
|
177
|
-
expect(css()).toMatch(/\.structured-quote-box-quote \{[^}]*padding: 2vw;[^}]*border-radius: 1vw;/);
|
|
204
|
+
expect(css()).toMatch(/\.structured-quote-box-quote \{[^}]*padding: 2vw 3vw 2vw 1vw;[^}]*border-radius: 1vw;/);
|
|
178
205
|
expect(container.querySelector('.structured-text-date')!.textContent!.trim()).toBe('23 Sep 2026');
|
|
179
206
|
});
|
|
180
207
|
|
|
@@ -211,20 +238,87 @@ describe('StructuredContent', () => {
|
|
|
211
238
|
it('draws code coloured as it comes, in its theme\'s box', () => {
|
|
212
239
|
const { container } = draw(section(false));
|
|
213
240
|
const tokens = [...container.querySelectorAll<HTMLSpanElement>('.structured-code-text-code span')];
|
|
214
|
-
expect(tokens.map(token => [token.textContent, token.style.color, token.style.fontWeight])).toEqual([
|
|
215
|
-
['const', 'rgb(255, 121, 198)', 'bold'],
|
|
216
|
-
[' a = 1;', 'rgb(248, 248, 242)', '']
|
|
241
|
+
expect(tokens.map(token => [token.textContent, token.style.color, token.style.fontWeight, token.style.textDecoration])).toEqual([
|
|
242
|
+
['const', 'rgb(255, 121, 198)', 'bold', ''],
|
|
243
|
+
[' a = 1;', 'rgb(248, 248, 242)', '', ''],
|
|
244
|
+
[' // gone', 'rgb(98, 114, 164)', '', 'underline line-through']
|
|
217
245
|
]);
|
|
218
246
|
expect(css()).toContain('background: #282A36');
|
|
219
247
|
});
|
|
220
248
|
|
|
221
|
-
it('
|
|
249
|
+
it('draws a table of cells: the heading over its rows, the cells set in its text styles, each column its share of the width and its own paddings', () => {
|
|
250
|
+
const { container } = draw(section(false));
|
|
251
|
+
expect([...container.querySelectorAll('.structured-table-table th')].map(cell => cell.textContent!.trim())).toEqual(['Name', 'Price']);
|
|
252
|
+
expect([...container.querySelectorAll('.structured-table-table td')].map(cell => cell.textContent!.trim())).toEqual(['Tea', '3', 'Coffee', '4']);
|
|
253
|
+
expect(container.querySelectorAll('.structured-table-table col')).toHaveLength(2);
|
|
254
|
+
expect(css()).toMatch(/\.structured-table-table \{[^}]*font-size: 2vw;[^}]*text-align: center;/);
|
|
255
|
+
expect(css()).toMatch(/\.structured-table-table th, \.structured-table-table td \{[^}]*padding-top: 0\.5vw;[^}]*padding-bottom: 0\.5vw;[^}]*border-width: /);
|
|
256
|
+
expect(css()).toMatch(/\.structured-table-table col:nth-child\(1\) \{\s*width: 60%;/);
|
|
257
|
+
expect(css()).toMatch(/\.structured-table-table th:nth-child\(2\), \.structured-table-table td:nth-child\(2\) \{\s*padding-left: 2vw;\s*padding-right: 0vw;/);
|
|
258
|
+
expect(css()).toMatch(/\.structured-table-table th, \.structured-table-table td \{\s*border-color: oklch/);
|
|
259
|
+
expect(css()).toMatch(/\.structured-table-heading-table \{\s*background-color: oklch/);
|
|
260
|
+
expect(css()).toMatch(/\.structured-table-body-table \{\s*background-color: oklch/);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it('sets a cell\'s ranges in their styles, as a text block\'s, and draws a list in a cell with the list styles once', () => {
|
|
264
|
+
const listed = block(StructuredBlockType.Table, 'listed', {
|
|
265
|
+
kind: 'cells',
|
|
266
|
+
header: [cell('l-h0', 'Steps')],
|
|
267
|
+
rows: [[{
|
|
268
|
+
id: 'l-r00',
|
|
269
|
+
text: 'Boil\nPour\n',
|
|
270
|
+
blocks: [{ start: 0, end: 4, type: 'ordered-list-item', entities: [] }, { start: 5, end: 9, type: 'ordered-list-item', entities: [] }],
|
|
271
|
+
styles: { desktop: [], mobile: [] }
|
|
272
|
+
}]]
|
|
273
|
+
}, tableCells);
|
|
274
|
+
const { container } = draw({ ...section(false), structuredContent: [table, listed] } as Section);
|
|
275
|
+
const heading = container.querySelector('.structured-table-table th')!;
|
|
276
|
+
expect(heading.textContent!.trim()).toBe('Name');
|
|
277
|
+
expect(heading.querySelector('[class*="rt_table-t-h0"]')).not.toBeNull();
|
|
278
|
+
expect(css()).toMatch(/rt_table-t-h0[^{]*\{[^}]*font-weight: 700/);
|
|
279
|
+
expect([...container.querySelectorAll('.structured-table-listed td .rt-ordered-list-item')].map(item => item.textContent!.trim())).toEqual(['Boil', 'Pour']);
|
|
280
|
+
expect(css().match(/counter\(cntrl-list\) '\.'/g)).toHaveLength(1);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it('draws a table of rows with every row plain and the two fills alternating from the first, and one of lines with a line between the rows only', () => {
|
|
284
|
+
const rows = block(StructuredBlockType.Table, 'zebra', { kind: 'rows', ...tableContent('z') }, tableCells);
|
|
285
|
+
const lines = block(StructuredBlockType.Table, 'ruled', { kind: 'lines', ...tableContent('r') }, tableCells);
|
|
286
|
+
const { container } = draw({ ...section(false), structuredContent: [rows, lines] } as Section);
|
|
287
|
+
expect(container.querySelectorAll('.structured-table-zebra th')).toHaveLength(0);
|
|
288
|
+
expect(container.querySelectorAll('.structured-table-zebra td')).toHaveLength(6);
|
|
289
|
+
expect(css()).toMatch(/\.structured-table-zebra thead tr, \.structured-table-zebra tbody tr:nth-child\(even\) \{\s*background-color: oklch/);
|
|
290
|
+
expect(css()).toMatch(/\.structured-table-zebra tbody tr:nth-child\(odd\) \{\s*background-color: oklch/);
|
|
291
|
+
expect(css()).not.toMatch(/\.structured-table-heading-zebra \{/);
|
|
292
|
+
|
|
293
|
+
expect(container.querySelectorAll('.structured-table-ruled th')).toHaveLength(2);
|
|
294
|
+
expect(css()).toMatch(/\.structured-table-ruled th, \.structured-table-ruled td \{[^}]*border-width: 0;/);
|
|
295
|
+
expect(css()).toMatch(/\.structured-table-ruled tbody > tr > th, \.structured-table-ruled tbody > tr > td \{\s*border-top-width: /);
|
|
296
|
+
expect(css()).toMatch(/\.structured-table-heading-ruled \{\s*background-color: oklch/);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it('covers a video played on click until it plays, and fetches no more than its metadata until then', () => {
|
|
222
300
|
const { container } = draw(section(false));
|
|
223
301
|
expect(container.querySelector('video')!.getAttribute('src')).toBe('https://cdn.cntrl.site/clip.mp4');
|
|
302
|
+
expect(container.querySelector('video')!.getAttribute('preload')).toBe('metadata');
|
|
224
303
|
expect(container.querySelector('.structured-video-cover-video')).not.toBeNull();
|
|
225
304
|
expect(container.querySelector('.structured-video-overlay-video')).not.toBeNull();
|
|
226
305
|
});
|
|
227
306
|
|
|
307
|
+
it('writes one column out too, so a smaller layout\'s columns stop at the layout set to one', () => {
|
|
308
|
+
const plain = block(StructuredBlockType.RichText, 'plain', paragraph('Plain'), { ...textStyles, rangeStyles: [] });
|
|
309
|
+
draw({ ...section(false), structuredContent: [plain] } as Section);
|
|
310
|
+
expect(css()).toMatch(/\.structured-text-plain \{[^}]*column-count: auto; column-gap: normal;/);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it('skips a block of a type it does not know, and an image the API could not resolve', () => {
|
|
314
|
+
const later = { ...code, id: 'later', type: 'chart-block' };
|
|
315
|
+
const missing = block(StructuredBlockType.Image, 'missing', { urls: ['https://cdn.cntrl.site/a.jpg', ''], altText: '' }, { opacity: 1 });
|
|
316
|
+
const { container } = draw({ ...section(false), structuredContent: [text, later, missing] } as Section);
|
|
317
|
+
const skipped = container.querySelector('.structured-block-item-later')!;
|
|
318
|
+
expect([...skipped.children].filter(child => child.tagName !== 'STYLE')).toHaveLength(0);
|
|
319
|
+
expect(container.querySelectorAll('.structured-gallery-image-missing')).toHaveLength(1);
|
|
320
|
+
});
|
|
321
|
+
|
|
228
322
|
it('draws the header first, with its elements left out of the stack', () => {
|
|
229
323
|
const { container } = draw(section(true));
|
|
230
324
|
const content = container.firstElementChild!;
|
|
@@ -240,8 +334,41 @@ describe('StructuredContent', () => {
|
|
|
240
334
|
expect(css()).toMatch(/@media \(min-width: 800px\) \{[\s\S]*\.structured-header-header \{[^}]*height: 80svh;/);
|
|
241
335
|
expect(css()).toMatch(/\.structured-header-stack-header \{[^}]*position: absolute;/);
|
|
242
336
|
expect(css()).toMatch(/\.structured-header-element-key-image \.structured-caption-key-image \{\s*display: none;/);
|
|
243
|
-
// mobile, variant B: the elements flow in its order, the image hidden there
|
|
337
|
+
// mobile, variant B: the elements flow in its order, the image hidden there; the caption and the
|
|
338
|
+
// cover sizing written back, so they end where the variant does
|
|
244
339
|
expect(css()).toMatch(/\.structured-header-stack-header \{\s*display: contents;/);
|
|
245
340
|
expect(css()).toMatch(/\.structured-header-element-key-image \{\s*display: none;/);
|
|
341
|
+
expect(css()).toMatch(/\.structured-header-element-key-image \.structured-caption-key-image \{\s*display: block;/);
|
|
342
|
+
expect(css()).toMatch(/\.structured-header-element-key-image \.structured-header-picture-key-image \{\s*object-fit: fill;/);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it('keeps a sticky key image still on the screen, as a fixed section background, on the layouts that stick it', () => {
|
|
346
|
+
const sticky = { ...header, layoutParams: { ...header.layoutParams, desktop: { ...header.layoutParams.desktop, stickyImage: true } } };
|
|
347
|
+
const { container } = draw({ ...section(true), header: sticky } as unknown as Section);
|
|
348
|
+
const frame = container.querySelector<HTMLElement>('.structured-header-pin-frame-key-image');
|
|
349
|
+
expect(frame?.style.backgroundImage).toContain('https://cdn.cntrl.site/key.jpg');
|
|
350
|
+
expect(css()).toMatch(/\.structured-header-pin-frame-key-image \{[^}]*position: sticky;[^}]*top: 100vh;[^}]*height: 100vh;/);
|
|
351
|
+
// desktop sticks its covering image, which only sizes the header now; mobile, in variant B, draws it as it is
|
|
352
|
+
expect(css()).toMatch(/@media \(min-width: 800px\) \{[\s\S]*\.structured-header-pin-key-image \{\s*display: block;[\s\S]*background-size: cover;/);
|
|
353
|
+
expect(css()).toMatch(/\.structured-header-picture-key-image \{[^}]*visibility: hidden;/);
|
|
354
|
+
expect(css()).toMatch(/\.structured-header-pin-key-image \{\s*display: none;/);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it('draws a key image no layout sticks as it is', () => {
|
|
358
|
+
draw(section(true));
|
|
359
|
+
expect(css()).not.toMatch(/\.structured-header-pin-key-image \{\s*display: block;/);
|
|
360
|
+
expect(css()).not.toMatch(/visibility: hidden;/);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it('keeps the gap above the header a block of the stack has, on the layout that sets one', () => {
|
|
364
|
+
draw(section(true));
|
|
365
|
+
expect(css()).toMatch(/@media \(min-width: 800px\) \{[\s\S]*\.structured-header-header \{[^}]*margin-top: 2vw;/);
|
|
366
|
+
expect(css()).toMatch(/\.structured-header-header \{[^}]*margin-top: 0;/);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it('puts an element an older header never ordered last', () => {
|
|
370
|
+
const unordered = { ...header, layoutParams: { ...header.layoutParams, mobile: { ...header.layoutParams.mobile, order: ['image', 'subtitle', 'date'] } } };
|
|
371
|
+
draw({ ...section(true), header: unordered } as unknown as Section);
|
|
372
|
+
expect(css()).toMatch(/\.structured-header-element-title \{[^}]*order: 3;/);
|
|
246
373
|
});
|
|
247
374
|
});
|
|
@@ -12,7 +12,8 @@ interface Props {
|
|
|
12
12
|
/**
|
|
13
13
|
* A section's blocks, one under another across the section's whole width: a block drawn from a
|
|
14
14
|
* preset is placed on the content grid by its own area. A content-based section's header comes
|
|
15
|
-
* first, drawing the blocks it points at, which the stack leaves out
|
|
15
|
+
* first, drawing the blocks it points at, which the stack leaves out; only the header keeps a gap
|
|
16
|
+
* above it there, where a default section's blocks keep theirs.
|
|
16
17
|
*/
|
|
17
18
|
export const StructuredContent: FC<Props> = ({ section }) => {
|
|
18
19
|
const reactId = useId();
|
|
@@ -28,7 +29,7 @@ export const StructuredContent: FC<Props> = ({ section }) => {
|
|
|
28
29
|
{header && <StructuredHeader header={header} blocks={section.structuredContent} readingWidth={readingWidth} />}
|
|
29
30
|
{section.structuredContent
|
|
30
31
|
.filter(block => !headerElementIds.includes(block.id))
|
|
31
|
-
.map(block => <StructuredBlockItem block={block} key={block.id} readingWidth={readingWidth} />)}
|
|
32
|
+
.map(block => <StructuredBlockItem block={block} key={block.id} readingWidth={readingWidth} hasGap={section.type !== 'content-based'} />)}
|
|
32
33
|
<JSXStyle id={id}>
|
|
33
34
|
{`
|
|
34
35
|
.structured-content-${section.id} {
|