@cntrl-site/sdk-nextjs 1.11.1 → 1.11.3

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.
Files changed (24) hide show
  1. package/lib/components/Article.js +4 -1
  2. package/lib/components/Section/Section.js +5 -3
  3. package/lib/components/Section/SectionImage.js +2 -2
  4. package/lib/components/Section/SectionVideo.js +3 -3
  5. package/lib/components/StructuredBlockItem/BlockCaption/BlockCaption.js +19 -8
  6. package/lib/components/StructuredBlockItem/StructuredRichText/StructuredRichText.js +11 -18
  7. package/lib/components/StructuredBlockItem/StructuredTable/StructuredTable.js +10 -8
  8. package/lib/components/StructuredBlockItem/textStylesRule.js +24 -6
  9. package/lib/components/StructuredBlockItem/vw.js +8 -0
  10. package/lib/utils/RichTextConverter/RichTextConverter.js +15 -5
  11. package/package.json +2 -2
  12. package/src/components/Article.test.tsx +52 -0
  13. package/src/components/Article.tsx +3 -0
  14. package/src/components/Section/Section.tsx +8 -4
  15. package/src/components/Section/SectionImage.tsx +3 -2
  16. package/src/components/Section/SectionVideo.tsx +4 -3
  17. package/src/components/StructuredBlockItem/BlockCaption/BlockCaption.tsx +18 -8
  18. package/src/components/StructuredBlockItem/StructuredRichText/StructuredRichText.tsx +11 -14
  19. package/src/components/StructuredBlockItem/StructuredTable/StructuredTable.tsx +18 -14
  20. package/src/components/StructuredBlockItem/textStylesRule.ts +24 -6
  21. package/src/components/StructuredBlockItem/vw.ts +4 -0
  22. package/src/components/StructuredContent/StructuredContent.test.tsx +30 -3
  23. package/src/utils/RichTextConverter/RichTextConverter.test.tsx +17 -0
  24. package/src/utils/RichTextConverter/RichTextConverter.tsx +19 -5
@@ -16,6 +16,7 @@ const WebGLContextManagerContext_1 = require("../provider/WebGLContextManagerCon
16
16
  const effects_1 = require("@cntrl-site/effects");
17
17
  const Navigation_1 = require("./Navigation/Navigation");
18
18
  const Article = ({ article, sectionData, navigation, pages = [] }) => {
19
+ var _a;
19
20
  const articleRef = (0, react_1.useRef)(null);
20
21
  const articleRectObserver = (0, useArticleRectObserver_1.useArticleRectObserver)(articleRef.current);
21
22
  const id = (0, react_1.useId)();
@@ -29,9 +30,11 @@ const Article = ({ article, sectionData, navigation, pages = [] }) => {
29
30
  }, [articleRectObserver]);
30
31
  const webglContextManager = (0, react_1.useMemo)(() => new effects_1.WebGLContextManager(), []);
31
32
  const sectionsLength = article.sections.length;
33
+ // a page has one main content: its first content-based section
34
+ const mainSectionId = (_a = article.sections.find(section => section.type === 'content-based')) === null || _a === void 0 ? void 0 : _a.id;
32
35
  return ((0, jsx_runtime_1.jsx)(ArticleRectContext_1.ArticleRectContext.Provider, { value: articleRectObserver, children: (0, jsx_runtime_1.jsxs)(InteractionsContext_1.InteractionsProvider, { article: article, children: [(0, jsx_runtime_1.jsxs)(ArticleWrapper_1.ArticleWrapper, { children: [navigation && ((0, jsx_runtime_1.jsx)(Navigation_1.Navigation, { navigation: navigation, pages: pages, hidden: article.navigationHidden })), (0, jsx_runtime_1.jsxs)("div", { className: "article", ref: articleRef, children: [(0, jsx_runtime_1.jsx)(WebGLContextManagerContext_1.WebglContextManagerContext.Provider, { value: webglContextManager, children: article.sections.map((section, i) => {
33
36
  const data = section.name ? sectionData[section.name] : {};
34
- return ((0, jsx_runtime_1.jsx)(Section_1.Section, { articleHeight: articleHeight, section: section, data: data, zIndex: sectionsLength - i }, section.id));
37
+ return ((0, jsx_runtime_1.jsx)(Section_1.Section, { articleHeight: articleHeight, section: section, data: data, zIndex: sectionsLength - i, isMain: section.id === mainSectionId }, section.id));
35
38
  }) }), (0, jsx_runtime_1.jsx)("div", { id: "component-portal" }), (0, jsx_runtime_1.jsx)("div", { id: "grid-component-lightbox-portal" })] })] }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
36
39
  .article {
37
40
  position: relative;
@@ -18,7 +18,7 @@ const checkOverflowClipSupport_1 = require("../../utils/checkOverflowClipSupport
18
18
  const StructuredContent_1 = require("../StructuredContent/StructuredContent");
19
19
  const Item_1 = require("../items/Item");
20
20
  const DEFAULT_COLOR = 'rgba(0, 0, 0, 0)';
21
- const Section = ({ section, data, zIndex, articleHeight }) => {
21
+ const Section = ({ section, data, zIndex, articleHeight, isMain = false }) => {
22
22
  var _a;
23
23
  const reactId = (0, react_1.useId)();
24
24
  const id = `${reactId}-section-${section.id}`;
@@ -32,6 +32,8 @@ const Section = ({ section, data, zIndex, articleHeight }) => {
32
32
  (_a = section.media) !== null && _a !== void 0 ? _a : {}
33
33
  ];
34
34
  const SectionComponent = section.name ? customSections.getComponent(section.name) : undefined;
35
+ // a content-based section holds a post's own content: the first is the page's main content, any other a section of it
36
+ const Tag = (section.type === 'content-based' ? (isMain ? 'main' : 'section') : 'div');
35
37
  (0, useSectionRegistry_1.useSectionRegistry)(section.id, sectionRef.current);
36
38
  const layoutMedia = layout && section.media && section.media[layout] ? section.media[layout] : undefined;
37
39
  const media = (0, react_1.useMemo)(() => {
@@ -67,8 +69,8 @@ const Section = ({ section, data, zIndex, articleHeight }) => {
67
69
  }, []);
68
70
  const children = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(StructuredContent_1.StructuredContent, { section: section }), section.items.map(item => ((0, jsx_runtime_1.jsx)(Item_1.Item, { sectionHeight: sectionHeight, item: item, sectionId: section.id, articleHeight: articleHeight }, item.id)))] }));
69
71
  if (SectionComponent)
70
- return (0, jsx_runtime_1.jsx)("div", { ref: sectionRef, children: (0, jsx_runtime_1.jsx)(SectionComponent, { data: data, children: children }) });
71
- return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)("div", { className: `section-${section.id}`, id: section.name, ref: sectionRef, style: { zIndex }, children: [media && sectionRef.current && ((0, jsx_runtime_1.jsx)("div", { className: `section-background-overlay-${section.id}`, children: (0, jsx_runtime_1.jsxs)("div", { className: `section-background-wrapper-${section.id}`, style: Object.assign({ transform: media.position === 'fixed' ? 'translateY(-100vh)' : 'unset' }, (sectionHeight && { height: media.position === 'fixed' ? `calc(${sectionHeight}px + 200vh)` : `${sectionHeight}px` })), children: [media.type === 'video' && ((0, jsx_runtime_1.jsx)(SectionVideo_1.SectionVideo, { container: sectionRef.current, sectionId: section.id, media: media })), media.type === 'image' && ((0, jsx_runtime_1.jsx)(SectionImage_1.SectionImage, { media: media, sectionId: section.id }))] }, `section-background-wrapper-${section.id}`) })), children] }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
72
+ return (0, jsx_runtime_1.jsx)(Tag, { ref: sectionRef, children: (0, jsx_runtime_1.jsx)(SectionComponent, { data: data, children: children }) });
73
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(Tag, { className: `section-${section.id}`, id: section.name, ref: sectionRef, style: { zIndex }, children: [media && sectionRef.current && ((0, jsx_runtime_1.jsx)("div", { className: `section-background-overlay-${section.id}`, children: (0, jsx_runtime_1.jsxs)("div", { className: `section-background-wrapper-${section.id}`, style: Object.assign({ transform: media.position === 'fixed' ? 'translateY(-100vh)' : 'unset' }, (sectionHeight && { height: media.position === 'fixed' ? `calc(${sectionHeight}px + 200vh)` : `${sectionHeight}px` })), children: [media.type === 'video' && ((0, jsx_runtime_1.jsx)(SectionVideo_1.SectionVideo, { container: sectionRef.current, sectionId: section.id, media: media })), media.type === 'image' && ((0, jsx_runtime_1.jsx)(SectionImage_1.SectionImage, { media: media, sectionId: section.id }))] }, `section-background-wrapper-${section.id}`) })), children] }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
72
74
  ${(0, sdk_1.getLayoutStyles)(layouts, layoutValues, ([height, color, media]) => (`
73
75
  .section-${section.id} {
74
76
  min-height: ${getSectionHeightFallback(height)};
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SectionImage = void 0;
4
4
  const jsx_runtime_1 = require("react/jsx-runtime");
5
5
  const SectionImage = ({ media, sectionId }) => {
6
- const { url, size, position, offsetX, opacity, tileSize } = media;
6
+ const { url, size, position, offsetX, opacity, tileSize, scale = 1 } = media;
7
7
  const isContainHeight = size === 'contain-height';
8
8
  const isTile = size === 'tile';
9
9
  const hasOffsetX = offsetX !== null && size === 'contain';
@@ -27,7 +27,7 @@ const SectionImage = ({ media, sectionId }) => {
27
27
  } })) : ((0, jsx_runtime_1.jsx)("img", { src: url, className: `image-background-${sectionId}`, style: {
28
28
  objectFit: isContainHeight ? 'unset' : (size !== null && size !== void 0 ? size : 'cover'),
29
29
  width: isContainHeight || hasOffsetX ? 'auto' : '100%',
30
- transform: isContainHeight ? 'translateX(-50%)' : 'none',
30
+ transform: `${isContainHeight ? 'translateX(-50%) ' : ''}scale(${scale})`,
31
31
  position: 'relative',
32
32
  left: isContainHeight ? '50%' : (hasOffsetX ? `${offsetX * 100}vw` : '0'),
33
33
  height: '100%',
@@ -8,7 +8,7 @@ const SectionVideo = ({ container, sectionId, media }) => {
8
8
  const [video, setVideo] = (0, react_1.useState)(null);
9
9
  const [videoWrapper, setVideoWrapper] = (0, react_1.useState)(null);
10
10
  const [isVideoWidthOverflow, setIsVideoWidthOverflow] = (0, react_1.useState)(false);
11
- const { url, size, position, offsetX, coverUrl, play, opacity } = media;
11
+ const { url, size, position, offsetX, coverUrl, play, opacity, scale = 1 } = media;
12
12
  const [isPlaying, setIsPlaying] = (0, react_1.useState)(false);
13
13
  const [userPaused, setUserPaused] = (0, react_1.useState)(false);
14
14
  const [isClickedOnCover, setIsClickedOnCover] = (0, react_1.useState)(false);
@@ -69,7 +69,7 @@ const SectionVideo = ({ container, sectionId, media }) => {
69
69
  opacity: !isClickedOnCover && play === 'on-click' && coverUrl ? 0 : opacity,
70
70
  objectFit: isContainHeight ? 'cover' : (size !== null && size !== void 0 ? size : 'cover'),
71
71
  width: isContainHeight && !isVideoWidthOverflow ? 'auto' : '100%',
72
- transform: isContainHeight ? 'translateX(-50%)' : 'none',
72
+ transform: `${isContainHeight ? 'translateX(-50%) ' : ''}scale(${scale})`,
73
73
  left: isContainHeight ? '50%' : (hasOffsetX ? `${offsetX * 100}vw` : '0'),
74
74
  height: '100%',
75
75
  position: 'relative'
@@ -84,7 +84,7 @@ const SectionVideo = ({ container, sectionId, media }) => {
84
84
  left: isContainHeight ? '50%' : (hasOffsetX ? `${offsetX * 100}vw` : '0'),
85
85
  width: isContainHeight ? 'auto' : '100%',
86
86
  objectFit: isContainHeight ? 'unset' : (size !== null && size !== void 0 ? size : 'cover'),
87
- transform: isContainHeight ? 'translateX(-50%)' : 'none',
87
+ transform: `${isContainHeight ? 'translateX(-50%) ' : ''}scale(${scale})`,
88
88
  position: 'relative',
89
89
  height: '100%',
90
90
  transition: 'opacity 0.1s ease-in-out'
@@ -10,8 +10,12 @@ const react_1 = require("react");
10
10
  const style_1 = __importDefault(require("styled-jsx/style"));
11
11
  const useCntrlContext_1 = require("../../../provider/useCntrlContext");
12
12
  const textStylesRule_1 = require("../textStylesRule");
13
+ const vw_1 = require("../vw");
13
14
  const CAPTION_GAP_PX = 5;
14
- /** The caption under an image: as wide as the reading column, or as the image where it is narrower, and centered under it. */
15
+ /**
16
+ * The caption under an image, centered under it: as wide as the caption preset says, else as the
17
+ * reading column, or as the image where that is narrower; with the preset's space above and below its text.
18
+ */
15
19
  const BlockCaption = ({ blockId, caption, layoutParams, readingWidth }) => {
16
20
  const reactId = (0, react_1.useId)();
17
21
  const { layouts } = (0, useCntrlContext_1.useCntrlContext)();
@@ -23,13 +27,20 @@ const BlockCaption = ({ blockId, caption, layoutParams, readingWidth }) => {
23
27
  white-space: pre-wrap;
24
28
  overflow-wrap: break-word;
25
29
  }
26
- ${(0, sdk_1.getLayoutStyles)(layouts, layoutValues, ([params, width], exemplary) => (`
27
- .structured-caption-${blockId} {
28
- width: ${width !== undefined ? `min(${width * 100}vw, 100%)` : '100%'};
29
- margin-top: ${CAPTION_GAP_PX / exemplary * 100}vw;
30
- }
31
- ${(params === null || params === void 0 ? void 0 : params.captionStyles) ? (0, textStylesRule_1.textStylesRule)(`.structured-caption-${blockId}`, params.captionStyles) : ''}
32
- `))}
30
+ ${(0, sdk_1.getLayoutStyles)(layouts, layoutValues, ([params, readingColumn], exemplary) => {
31
+ var _a;
32
+ const captionStyles = params === null || params === void 0 ? void 0 : params.captionStyles;
33
+ const width = (_a = captionStyles === null || captionStyles === void 0 ? void 0 : captionStyles.maxWidth) !== null && _a !== void 0 ? _a : readingColumn;
34
+ return `
35
+ .structured-caption-${blockId} {
36
+ width: ${width !== undefined ? `min(${(0, vw_1.vw)(width)}, 100%)` : '100%'};
37
+ margin-top: ${CAPTION_GAP_PX / exemplary * 100}vw;
38
+ padding-top: ${(0, vw_1.vw)(captionStyles === null || captionStyles === void 0 ? void 0 : captionStyles.innerPaddingTop)};
39
+ padding-bottom: ${(0, vw_1.vw)(captionStyles === null || captionStyles === void 0 ? void 0 : captionStyles.innerPaddingBottom)};
40
+ }
41
+ ${captionStyles ? (0, textStylesRule_1.textStylesRule)(`.structured-caption-${blockId}`, captionStyles) : ''}
42
+ `;
43
+ })}
33
44
  ` })] }));
34
45
  };
35
46
  exports.BlockCaption = BlockCaption;
@@ -17,40 +17,33 @@ const textStylesRule_1 = require("../textStylesRule");
17
17
  const richTextConverter = new RichTextConverter_1.RichTextConverter();
18
18
  /**
19
19
  * A block of text: rich text, a quote, which is drawn in a box of its own, or a date, which comes as
20
- * the text it reads as. Its text is set in the styles it was drawn with, its ranges styled over them.
20
+ * the text it reads as. Its text is set in the styles it was drawn with, its ranges styled over them,
21
+ * and each paragraph is one of the page's; a quote is a quotation, and a heading, which takes no
22
+ * paragraphs, holds its lines in spans that look the same.
21
23
  */
22
24
  const StructuredRichText = ({ block }) => {
23
- var _a;
24
25
  const reactId = (0, react_1.useId)();
25
26
  const id = `${reactId}-structured-text-${block.id}`;
26
27
  const { layouts } = (0, useCntrlContext_1.useCntrlContext)();
27
28
  const [ref, setRef] = (0, react_1.useState)(null);
28
29
  (0, useItemGeometry_1.useItemGeometry)(block.id, ref);
29
- const [content, rangeStyles, listStyles] = (0, react_1.useMemo)(() => richTextConverter.toHtml(block, layouts, { isScaledToViewport: false }), [block, layouts]);
30
- const lang = (0, react_1.useMemo)(() => (0, detectLang_1.detectLang)(block.commonParams.text), [block.commonParams.text]);
30
+ const headingTag = (0, getHeadingTag_1.getHeadingTag)(block);
31
31
  const isQuote = block.type === sdk_1.StructuredBlockType.Quote;
32
+ const paragraphTag = headingTag ? 'span' : 'p';
33
+ const [content, rangeStyles, listStyles] = (0, react_1.useMemo)(() => richTextConverter.toHtml(block, layouts, { isScaledToViewport: false, paragraphTag }), [block, layouts, paragraphTag]);
34
+ const lang = (0, react_1.useMemo)(() => (0, detectLang_1.detectLang)(block.commonParams.text), [block.commonParams.text]);
32
35
  // only a text block of the stack flows in columns: a quote keeps to its box, a date to its line
33
36
  const isText = block.type === sdk_1.StructuredBlockType.RichText;
34
- const Wrapper = ((_a = (0, getHeadingTag_1.getHeadingTag)(block)) !== null && _a !== void 0 ? _a : 'div');
37
+ const Wrapper = (headingTag !== null && headingTag !== void 0 ? headingTag : (isQuote ? 'blockquote' : 'div'));
35
38
  const layoutValues = [block.layoutParams];
36
39
  return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Wrapper, { ref: setRef, lang: lang, className: `structured-text-${block.id}`, children: isQuote ? (0, jsx_runtime_1.jsx)("div", { className: `structured-quote-box-${block.id}`, children: content }) : content }), listStyles && (0, jsx_runtime_1.jsx)(style_1.default, { id: RichTextConverter_1.LIST_STYLES_ID, children: listStyles }), (0, jsx_runtime_1.jsxs)(style_1.default, { id: id, children: [rangeStyles, `
37
40
  .structured-text-${block.id} {
38
41
  margin: 0;
39
42
  }
40
- `, `${(0, sdk_1.getLayoutStyles)(layouts, layoutValues, ([layoutParams]) => {
41
- var _a;
42
- return (`
43
+ `, `${(0, sdk_1.getLayoutStyles)(layouts, layoutValues, ([layoutParams]) => (`
43
44
  ${(0, textStylesRule_1.textStylesRule)(`.structured-text-${block.id}`, layoutParams)}
44
45
  ${isText ? (0, textStylesRule_1.textColumnsRule)(`.structured-text-${block.id}`, layoutParams) : ''}
45
- ${isQuote ? `
46
- .structured-quote-box-${block.id} {
47
- padding: ${[layoutParams.boxPaddingTop, layoutParams.boxPaddingRight, layoutParams.boxPaddingBottom, layoutParams.boxPaddingLeft].map(side => `${(side !== null && side !== void 0 ? side : 0) * 100}vw`).join(' ')};
48
- border-radius: ${((_a = layoutParams.boxRadius) !== null && _a !== void 0 ? _a : 0) * 100}vw;
49
- background-color: transparent;
50
- }
51
- ${layoutParams.background ? (0, textStylesRule_1.colorRule)(`.structured-quote-box-${block.id}`, 'background-color', layoutParams.background) : ''}
52
- ` : ''}
53
- `);
54
- })}`] })] }));
46
+ ${isQuote ? (0, textStylesRule_1.boxRule)(`.structured-quote-box-${block.id}`, layoutParams) : ''}
47
+ `))}`] })] }));
55
48
  };
56
49
  exports.StructuredRichText = StructuredRichText;
@@ -12,6 +12,7 @@ const useCntrlContext_1 = require("../../../provider/useCntrlContext");
12
12
  const useItemGeometry_1 = require("../../../ItemGeometry/useItemGeometry");
13
13
  const RichTextConverter_1 = require("../../../utils/RichTextConverter/RichTextConverter");
14
14
  const textStylesRule_1 = require("../textStylesRule");
15
+ const vw_1 = require("../vw");
15
16
  // the lines between the cells, in pixels of the layout, scaled with it like everything drawn in it
16
17
  const BORDER_PX = 1;
17
18
  const richTextConverter = new RichTextConverter_1.RichTextConverter();
@@ -39,7 +40,7 @@ function convertCells(block, layouts) {
39
40
  * lined in the styles it comes with, their ranges styled over them, each column its share of the
40
41
  * width and padded on either side as its own values say. Cells draw a heading row over a grid; rows
41
42
  * draw every row plain, the two fills alternating from the first; lines draw the heading and a line
42
- * between each pair of rows only.
43
+ * between each pair of rows only. The table is drawn in a box, which grows out past its columns.
43
44
  */
44
45
  const StructuredTable = ({ block }) => {
45
46
  const reactId = (0, react_1.useId)();
@@ -51,9 +52,10 @@ const StructuredTable = ({ block }) => {
51
52
  const headingAsRow = kind === 'rows';
52
53
  const Heading = headingAsRow ? 'td' : 'th';
53
54
  const table = `.structured-table-${block.id}`;
55
+ const box = `.structured-table-box-${block.id}`;
54
56
  const cells = `${table} th, ${table} td`;
55
57
  const cellContent = (cell) => content.get(cell.id);
56
- return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)("table", { ref: setRef, className: `structured-table-${block.id}`, children: [(0, jsx_runtime_1.jsx)("colgroup", { children: header.map((_, col) => (0, jsx_runtime_1.jsx)("col", {}, col)) }), (0, jsx_runtime_1.jsx)("thead", { className: `structured-table-heading-${block.id}`, children: (0, jsx_runtime_1.jsx)("tr", { children: header.map(cell => (0, jsx_runtime_1.jsx)(Heading, { children: cellContent(cell) }, cell.id)) }) }), (0, jsx_runtime_1.jsx)("tbody", { className: `structured-table-body-${block.id}`, children: rows.map((row, index) => { var _a, _b; return (0, jsx_runtime_1.jsx)("tr", { children: row.map(cell => (0, jsx_runtime_1.jsx)("td", { children: cellContent(cell) }, cell.id)) }, (_b = (_a = row[0]) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : index); }) })] }), listStyles && (0, jsx_runtime_1.jsx)(style_1.default, { id: RichTextConverter_1.LIST_STYLES_ID, children: listStyles }), (0, jsx_runtime_1.jsxs)(style_1.default, { id: `${reactId}-table-${block.id}`, children: [rangeStyles, `
58
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: `structured-table-box-${block.id}`, children: (0, jsx_runtime_1.jsxs)("table", { ref: setRef, className: `structured-table-${block.id}`, children: [(0, jsx_runtime_1.jsx)("colgroup", { children: header.map((_, col) => (0, jsx_runtime_1.jsx)("col", {}, col)) }), (0, jsx_runtime_1.jsx)("thead", { className: `structured-table-heading-${block.id}`, children: (0, jsx_runtime_1.jsx)("tr", { children: header.map(cell => (0, jsx_runtime_1.jsx)(Heading, { children: cellContent(cell) }, cell.id)) }) }), (0, jsx_runtime_1.jsx)("tbody", { className: `structured-table-body-${block.id}`, children: rows.map((row, index) => { var _a, _b; return (0, jsx_runtime_1.jsx)("tr", { children: row.map(cell => (0, jsx_runtime_1.jsx)("td", { children: cellContent(cell) }, cell.id)) }, (_b = (_a = row[0]) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : index); }) })] }) }), listStyles && (0, jsx_runtime_1.jsx)(style_1.default, { id: RichTextConverter_1.LIST_STYLES_ID, children: listStyles }), (0, jsx_runtime_1.jsxs)(style_1.default, { id: `${reactId}-table-${block.id}`, children: [rangeStyles, `
57
59
  ${table} {
58
60
  width: 100%;
59
61
  table-layout: fixed;
@@ -69,14 +71,14 @@ const StructuredTable = ({ block }) => {
69
71
  overflow-wrap: anywhere;
70
72
  }
71
73
  ${(0, sdk_1.getLayoutStyles)(layouts, [block.layoutParams], ([layoutParams], exemplary) => {
72
- const vw = (value) => `${value * 100}vw`;
73
- const border = vw(BORDER_PX / exemplary);
74
+ const border = (0, vw_1.vw)(BORDER_PX / exemplary);
74
75
  const fill = (selector, color) => (color ? (0, textStylesRule_1.colorRule)(selector, 'background-color', color) : '');
75
76
  return (`
77
+ ${(0, textStylesRule_1.boxRule)(box, layoutParams)}
76
78
  ${(0, textStylesRule_1.textStylesRule)(table, layoutParams)}
77
79
  ${cells} {
78
- padding-top: ${vw(layoutParams.cellPaddingTop)};
79
- padding-bottom: ${vw(layoutParams.cellPaddingBottom)};
80
+ padding-top: ${(0, vw_1.vw)(layoutParams.cellPaddingTop)};
81
+ padding-bottom: ${(0, vw_1.vw)(layoutParams.cellPaddingBottom)};
80
82
  border-width: ${kind === 'lines' ? '0' : border};
81
83
  }
82
84
  ${kind === 'lines' ? `${table} tbody > tr > th, ${table} tbody > tr > td { border-top-width: ${border}; }` : ''}
@@ -85,8 +87,8 @@ const StructuredTable = ({ block }) => {
85
87
  width: ${column.width * 100}%;
86
88
  }
87
89
  ${table} th:nth-child(${index + 1}), ${table} td:nth-child(${index + 1}) {
88
- padding-left: ${vw(column.paddingLeft)};
89
- padding-right: ${vw(column.paddingRight)};
90
+ padding-left: ${(0, vw_1.vw)(column.paddingLeft)};
91
+ padding-right: ${(0, vw_1.vw)(column.paddingRight)};
90
92
  }
91
93
  `).join('')}
92
94
  ${layoutParams.borderColor ? (0, textStylesRule_1.colorRule)(cells, 'border-color', layoutParams.borderColor) : ''}
@@ -1,18 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.colorRule = exports.textColumnsRule = exports.textStylesRule = void 0;
3
+ exports.colorRule = exports.boxRule = exports.textColumnsRule = exports.textStylesRule = void 0;
4
4
  const color_1 = require("@cntrl-site/color");
5
5
  const sdk_1 = require("@cntrl-site/sdk");
6
6
  const getFontFamilyValue_1 = require("../../utils/getFontFamilyValue");
7
+ const vw_1 = require("./vw");
7
8
  /** A block's text styles as a CSS rule for `selector`, its lengths in viewport units as the block's own. */
8
9
  function textStylesRule(selector, styles) {
9
10
  return `
10
11
  ${selector} {
11
12
  font-family: ${(0, getFontFamilyValue_1.getFontFamilyValue)(styles.typeFace)};
12
- font-size: ${styles.fontSize * 100}vw;
13
- line-height: ${styles.lineHeight * 100}vw;
14
- letter-spacing: ${styles.letterSpacing * 100}vw;
15
- word-spacing: ${styles.wordSpacing * 100}vw;
13
+ font-size: ${(0, vw_1.vw)(styles.fontSize)};
14
+ line-height: ${(0, vw_1.vw)(styles.lineHeight)};
15
+ letter-spacing: ${(0, vw_1.vw)(styles.letterSpacing)};
16
+ word-spacing: ${(0, vw_1.vw)(styles.wordSpacing)};
16
17
  font-weight: ${styles.fontWeight};
17
18
  font-style: ${styles.fontStyle || 'normal'};
18
19
  font-variant: ${styles.fontVariant};
@@ -34,11 +35,28 @@ function textColumnsRule(selector, { columns = 1, columnGap = 0, hyphens = sdk_1
34
35
  ${selector} {
35
36
  hyphens: ${hyphens};
36
37
  -webkit-hyphens: ${hyphens};
37
- ${columns > 1 ? `column-count: ${columns}; column-gap: ${columnGap * 100}vw; column-fill: balance;` : 'column-count: auto; column-gap: normal;'}
38
+ ${columns > 1 ? `column-count: ${columns}; column-gap: ${(0, vw_1.vw)(columnGap)}; column-fill: balance;` : 'column-count: auto; column-gap: normal;'}
38
39
  }
39
40
  `;
40
41
  }
41
42
  exports.textColumnsRule = textColumnsRule;
43
+ /**
44
+ * The box a quote's text or a table is drawn in, as a CSS rule for `selector`: padded inside, its
45
+ * sides grown out past the columns the block is placed on by as much as they pad it, so what it
46
+ * holds stays on them; rounded, and filled where it has a fill.
47
+ */
48
+ function boxRule(selector, box) {
49
+ return `
50
+ ${selector} {
51
+ padding: ${[box.boxPaddingTop, box.boxPaddingRight, box.boxPaddingBottom, box.boxPaddingLeft].map(vw_1.vw).join(' ')};
52
+ margin: 0 -${(0, vw_1.vw)(box.boxPaddingRight)} 0 -${(0, vw_1.vw)(box.boxPaddingLeft)};
53
+ border-radius: ${(0, vw_1.vw)(box.boxRadius)};
54
+ background-color: transparent;
55
+ }
56
+ ${box.boxBackground ? colorRule(selector, 'background-color', box.boxBackground) : ''}
57
+ `;
58
+ }
59
+ exports.boxRule = boxRule;
42
60
  /** A colour of the palette as CSS for `property`, with a fallback where oklch is not supported. */
43
61
  function colorRule(selector, property, value) {
44
62
  const color = color_1.CntrlColor.parse(value);
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.vw = void 0;
4
+ /** A share of the layout width as CSS: the layout is the viewport's width, so a share is so many `vw`. */
5
+ function vw(share) {
6
+ return `${(share !== null && share !== void 0 ? share : 0) * 100}vw`;
7
+ }
8
+ exports.vw = vw;
@@ -48,11 +48,13 @@ const LIST_STYLES = `
48
48
  `;
49
49
  class RichTextConverter {
50
50
  /** The text as elements, the styles of its ranges, and, where it has a list, the list styles every text shares, for the caller to render once. */
51
- toHtml(richText, layouts, { isScaledToViewport = true } = {}) {
51
+ toHtml(richText, layouts, { isScaledToViewport = true, paragraphTag: Paragraph = 'div' } = {}) {
52
52
  var _a, _b, _c, _d;
53
53
  const { text, blocks = [] } = richText.commonParams;
54
54
  const hasLists = blocks.some(block => block.type in LIST_ITEM_CLASSES);
55
55
  const root = [];
56
+ // what a div is of itself, a paragraph drawn in another element is told: a block, without margins
57
+ const paragraphRules = [];
56
58
  const styleRules = layouts.reduce((rec, layout) => {
57
59
  rec[layout.id] = [];
58
60
  return rec;
@@ -71,7 +73,9 @@ class RichTextConverter {
71
73
  // an empty line keeps its height, as in the editor; a text never typed into comes without even its line break
72
74
  if (content === '' || content === '\n') {
73
75
  const id = `rt_${richText.id}_br_${blockIndex}`;
74
- root.push((0, jsx_runtime_1.jsx)("div", { className: `${id}${typeClass}`, children: (0, jsx_runtime_1.jsx)("br", {}) }, id));
76
+ root.push((0, jsx_runtime_1.jsx)(Paragraph, { className: `${id}${typeClass}`, children: (0, jsx_runtime_1.jsx)("br", {}) }, id));
77
+ if (Paragraph !== 'div')
78
+ paragraphRules.push(`.${id} { display: block; margin: 0; }`);
75
79
  layouts.forEach(l => {
76
80
  const lhForLayout = currentLineHeight[l.id];
77
81
  if (lhForLayout === undefined)
@@ -179,14 +183,20 @@ class RichTextConverter {
179
183
  }
180
184
  }
181
185
  }
182
- root.push((0, jsx_runtime_1.jsx)("div", { className: `${blockClass}${typeClass}`, children: kids }, blockClass));
186
+ root.push((0, jsx_runtime_1.jsx)(Paragraph, { className: `${blockClass}${typeClass}`, children: kids }, blockClass));
187
+ // each layout shows or hides the paragraph itself
188
+ if (Paragraph !== 'div')
189
+ paragraphRules.push(`.${blockClass} { margin: 0; }`);
183
190
  }
184
191
  }
185
- const styles = layouts.map(l => `
192
+ const styles = [
193
+ ...paragraphRules,
194
+ ...layouts.map(l => `
186
195
  ${(0, sdk_1.getLayoutMediaQuery)(l.id, layouts)} {
187
196
  ${styleRules[l.id].join('\n')}
188
197
  }
189
- `).join('\n');
198
+ `)
199
+ ].join('\n');
190
200
  return [
191
201
  root,
192
202
  styles,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntrl-site/sdk-nextjs",
3
- "version": "1.11.1",
3
+ "version": "1.11.3",
4
4
  "description": "SDK for Next.js",
5
5
  "author": "arsen@momdesign.nyc",
6
6
  "license": "MIT",
@@ -32,7 +32,7 @@
32
32
  "@cntrl-site/color": "^1.0.0",
33
33
  "@cntrl-site/components": "^1.0.106",
34
34
  "@cntrl-site/effects": "^1.4.2",
35
- "@cntrl-site/sdk": "^1.29.1",
35
+ "@cntrl-site/sdk": "^1.29.5",
36
36
  "@types/vimeo__player": "^2.18.0",
37
37
  "@vimeo/player": "^2.25.0",
38
38
  "franc-min": "^5.0.0",
@@ -0,0 +1,52 @@
1
+ import { render } from '@testing-library/react';
2
+ import { Article as TArticle, Layout, Section, SectionHeightMode } from '@cntrl-site/sdk';
3
+ import { CntrlContext } from '../provider/CntrlContext';
4
+ import { CntrlSdkContext } from '../provider/CntrlSdkContext';
5
+ import { CustomItemRegistry } from '../provider/CustomItemRegistry';
6
+ import { CustomSectionRegistry } from '../provider/CustomSectionRegistry';
7
+ import { Article } from './Article';
8
+
9
+ jest.mock('styled-jsx/style', () => ({ __esModule: true, default: () => null }));
10
+ // jsdom measures nothing, so a section's height is never observed
11
+ global.ResizeObserver = class { observe() {} unobserve() {} disconnect() {} } as unknown as typeof ResizeObserver;
12
+
13
+ const layouts: Layout[] = [
14
+ { id: 'desktop', title: 'Desktop', startsWith: 800, exemplary: 1440 },
15
+ { id: 'mobile', title: 'Mobile', startsWith: 0, exemplary: 375 }
16
+ ];
17
+
18
+ function section(id: string, type: Section['type']): Section {
19
+ return {
20
+ id,
21
+ type,
22
+ items: [],
23
+ minHeight: Object.fromEntries(layouts.map(layout => [layout.id, { mode: SectionHeightMode.ControlUnits, units: 0.5 }])),
24
+ color: {},
25
+ hidden: {},
26
+ structuredContent: [],
27
+ structuredContentSettings: { paddingBottom: {}, defaultWidth: {} }
28
+ } as unknown as Section;
29
+ }
30
+
31
+ function draw(sections: Section[]) {
32
+ const sdk = new CntrlSdkContext(new CustomItemRegistry(), new CustomSectionRegistry());
33
+ sdk.setLayouts(layouts);
34
+ const article: TArticle = { id: 'article', sections, interactions: {} };
35
+ return render(
36
+ <CntrlContext.Provider value={sdk}>
37
+ <Article article={article} sectionData={{}} />
38
+ </CntrlContext.Provider>
39
+ );
40
+ }
41
+
42
+ const tagOf = (container: HTMLElement, id: string) => container.querySelector(`.section-${id}`)?.tagName;
43
+
44
+ describe('Article', () => {
45
+ it('draws its first content-based section as the page\'s main content, any other as a section, the rest as they were', () => {
46
+ const { container } = draw([section('intro', 'default'), section('post', 'content-based'), section('more', 'content-based')]);
47
+ expect(tagOf(container, 'intro')).toBe('DIV');
48
+ expect(tagOf(container, 'post')).toBe('MAIN');
49
+ expect(tagOf(container, 'more')).toBe('SECTION');
50
+ expect(container.querySelectorAll('main')).toHaveLength(1);
51
+ });
52
+ });
@@ -32,6 +32,8 @@ export const Article: FC<Props> = ({ article, sectionData, navigation, pages = [
32
32
 
33
33
  const webglContextManager = useMemo(() => new WebGLContextManager(), []);
34
34
  const sectionsLength = article.sections.length;
35
+ // a page has one main content: its first content-based section
36
+ const mainSectionId = article.sections.find(section => section.type === 'content-based')?.id;
35
37
 
36
38
  return (
37
39
  <ArticleRectContext.Provider value={articleRectObserver}>
@@ -55,6 +57,7 @@ export const Article: FC<Props> = ({ article, sectionData, navigation, pages = [
55
57
  key={section.id}
56
58
  data={data}
57
59
  zIndex={sectionsLength - i}
60
+ isMain={section.id === mainSectionId}
58
61
  />
59
62
  );
60
63
  })}
@@ -24,9 +24,11 @@ interface Props {
24
24
  zIndex: number;
25
25
  data?: any;
26
26
  articleHeight: number;
27
+ /** Whether it holds the page's main content: the article's first content-based section drawn does. */
28
+ isMain?: boolean;
27
29
  }
28
30
 
29
- export const Section: FC<PropsWithChildren<Props>> = ({ section, data, zIndex, articleHeight }) => {
31
+ export const Section: FC<PropsWithChildren<Props>> = ({ section, data, zIndex, articleHeight, isMain = false }) => {
30
32
  const reactId = useId();
31
33
  const id = `${reactId}-section-${section.id}`;
32
34
  const sectionRef = useRef<HTMLDivElement | null>(null);
@@ -40,6 +42,8 @@ export const Section: FC<PropsWithChildren<Props>> = ({ section, data, zIndex, a
40
42
  ];
41
43
 
42
44
  const SectionComponent = section.name ? customSections.getComponent(section.name) : undefined;
45
+ // a content-based section holds a post's own content: the first is the page's main content, any other a section of it
46
+ const Tag = (section.type === 'content-based' ? (isMain ? 'main' : 'section') : 'div') as 'div';
43
47
  useSectionRegistry(section.id, sectionRef.current);
44
48
  const layoutMedia = layout && section.media && section.media[layout] ? section.media[layout] : undefined;
45
49
 
@@ -94,11 +98,11 @@ export const Section: FC<PropsWithChildren<Props>> = ({ section, data, zIndex, a
94
98
  </>
95
99
  );
96
100
 
97
- if (SectionComponent) return <div ref={sectionRef}><SectionComponent data={data}>{children}</SectionComponent></div>;
101
+ if (SectionComponent) return <Tag ref={sectionRef}><SectionComponent data={data}>{children}</SectionComponent></Tag>;
98
102
 
99
103
  return (
100
104
  <>
101
- <div
105
+ <Tag
102
106
  className={`section-${section.id}`}
103
107
  id={section.name}
104
108
  ref={sectionRef}
@@ -124,7 +128,7 @@ export const Section: FC<PropsWithChildren<Props>> = ({ section, data, zIndex, a
124
128
  </div>
125
129
  )}
126
130
  {children}
127
- </div>
131
+ </Tag>
128
132
  <JSXStyle id={id}>{`
129
133
  ${
130
134
  getLayoutStyles(layouts, layoutValues, ([height, color, media]) => (`
@@ -9,6 +9,7 @@ export type TSectionImage = {
9
9
  offsetX: number | null;
10
10
  opacity?: number;
11
11
  tileSize?: number;
12
+ scale?: number;
12
13
  };
13
14
 
14
15
  interface Props {
@@ -17,7 +18,7 @@ interface Props {
17
18
  }
18
19
 
19
20
  export const SectionImage: FC<Props> = ({ media, sectionId }) => {
20
- const { url, size, position, offsetX, opacity, tileSize } = media;
21
+ const { url, size, position, offsetX, opacity, tileSize, scale = 1 } = media;
21
22
  const isContainHeight = size === 'contain-height';
22
23
  const isTile = size === 'tile';
23
24
  const hasOffsetX = offsetX !== null && size === 'contain';
@@ -56,7 +57,7 @@ export const SectionImage: FC<Props> = ({ media, sectionId }) => {
56
57
  style={{
57
58
  objectFit: isContainHeight ? 'unset' : (size ?? 'cover') as CSSProperties['objectFit'],
58
59
  width: isContainHeight || hasOffsetX ? 'auto' : '100%',
59
- transform: isContainHeight ? 'translateX(-50%)' : 'none',
60
+ transform: `${isContainHeight ? 'translateX(-50%) ' : ''}scale(${scale})`,
60
61
  position: 'relative',
61
62
  left: isContainHeight ? '50%' : (hasOffsetX ? `${offsetX * 100}vw` : '0'),
62
63
  height: '100%',
@@ -11,6 +11,7 @@ export type TSectionVideo = {
11
11
  coverUrl: string | null;
12
12
  offsetX: number | null;
13
13
  opacity?: number;
14
+ scale?: number;
14
15
  };
15
16
 
16
17
  interface Props {
@@ -23,7 +24,7 @@ export const SectionVideo: FC<Props> = ({ container, sectionId, media }) => {
23
24
  const [video, setVideo] = useState<HTMLVideoElement | null>(null);
24
25
  const [videoWrapper, setVideoWrapper] = useState<HTMLDivElement | null>(null);
25
26
  const [isVideoWidthOverflow, setIsVideoWidthOverflow] = useState(false);
26
- const { url, size, position, offsetX, coverUrl, play, opacity } = media;
27
+ const { url, size, position, offsetX, coverUrl, play, opacity, scale = 1 } = media;
27
28
  const [isPlaying, setIsPlaying] = useState(false);
28
29
  const [userPaused, setUserPaused] = useState(false);
29
30
  const [isClickedOnCover, setIsClickedOnCover] = useState(false);
@@ -94,7 +95,7 @@ export const SectionVideo: FC<Props> = ({ container, sectionId, media }) => {
94
95
  opacity: !isClickedOnCover && play === 'on-click' && coverUrl ? 0 : opacity,
95
96
  objectFit: isContainHeight ? 'cover' : (size ?? 'cover') as CSSProperties['objectFit'],
96
97
  width: isContainHeight && !isVideoWidthOverflow ? 'auto' : '100%',
97
- transform: isContainHeight ? 'translateX(-50%)' : 'none',
98
+ transform: `${isContainHeight ? 'translateX(-50%) ' : ''}scale(${scale})`,
98
99
  left: isContainHeight ? '50%' : (hasOffsetX ? `${offsetX * 100}vw` : '0'),
99
100
  height: '100%',
100
101
  position: 'relative'
@@ -131,7 +132,7 @@ export const SectionVideo: FC<Props> = ({ container, sectionId, media }) => {
131
132
  left: isContainHeight ? '50%' : (hasOffsetX ? `${offsetX * 100}vw` : '0'),
132
133
  width: isContainHeight ? 'auto' : '100%',
133
134
  objectFit: isContainHeight ? 'unset' : (size ?? 'cover') as CSSProperties['objectFit'],
134
- transform: isContainHeight ? 'translateX(-50%)' : 'none',
135
+ transform: `${isContainHeight ? 'translateX(-50%) ' : ''}scale(${scale})`,
135
136
  position: 'relative',
136
137
  height: '100%',
137
138
  transition: 'opacity 0.1s ease-in-out'
@@ -3,6 +3,7 @@ import { FC, useId } from 'react';
3
3
  import JSXStyle from 'styled-jsx/style';
4
4
  import { useCntrlContext } from '../../../provider/useCntrlContext';
5
5
  import { textStylesRule } from '../textStylesRule';
6
+ import { vw } from '../vw';
6
7
 
7
8
  interface Props {
8
9
  blockId: string;
@@ -13,7 +14,10 @@ interface Props {
13
14
 
14
15
  const CAPTION_GAP_PX = 5;
15
16
 
16
- /** The caption under an image: as wide as the reading column, or as the image where it is narrower, and centered under it. */
17
+ /**
18
+ * The caption under an image, centered under it: as wide as the caption preset says, else as the
19
+ * reading column, or as the image where that is narrower; with the preset's space above and below its text.
20
+ */
17
21
  export const BlockCaption: FC<Props> = ({ blockId, caption, layoutParams, readingWidth }) => {
18
22
  const reactId = useId();
19
23
  const { layouts } = useCntrlContext();
@@ -28,13 +32,19 @@ export const BlockCaption: FC<Props> = ({ blockId, caption, layoutParams, readin
28
32
  white-space: pre-wrap;
29
33
  overflow-wrap: break-word;
30
34
  }
31
- ${getLayoutStyles(layouts, layoutValues, ([params, width], exemplary) => (`
32
- .structured-caption-${blockId} {
33
- width: ${width !== undefined ? `min(${width * 100}vw, 100%)` : '100%'};
34
- margin-top: ${CAPTION_GAP_PX / exemplary * 100}vw;
35
- }
36
- ${params?.captionStyles ? textStylesRule(`.structured-caption-${blockId}`, params.captionStyles) : ''}
37
- `))}
35
+ ${getLayoutStyles(layouts, layoutValues, ([params, readingColumn], exemplary) => {
36
+ const captionStyles = params?.captionStyles;
37
+ const width = captionStyles?.maxWidth ?? readingColumn;
38
+ return `
39
+ .structured-caption-${blockId} {
40
+ width: ${width !== undefined ? `min(${vw(width)}, 100%)` : '100%'};
41
+ margin-top: ${CAPTION_GAP_PX / exemplary * 100}vw;
42
+ padding-top: ${vw(captionStyles?.innerPaddingTop)};
43
+ padding-bottom: ${vw(captionStyles?.innerPaddingBottom)};
44
+ }
45
+ ${captionStyles ? textStylesRule(`.structured-caption-${blockId}`, captionStyles) : ''}
46
+ `;
47
+ })}
38
48
  `}
39
49
  </JSXStyle>
40
50
  </>
@@ -7,7 +7,7 @@ import { LIST_STYLES_ID, RichTextConverter } from '../../../utils/RichTextConver
7
7
  import { detectLang } from '../../../utils/detectLang';
8
8
  import { getHeadingTag } from '../../../utils/getHeadingTag';
9
9
  import { StructuredBlockItemProps } from '../StructuredBlockItem';
10
- import { colorRule, textColumnsRule, textStylesRule } from '../textStylesRule';
10
+ import { boxRule, textColumnsRule, textStylesRule } from '../textStylesRule';
11
11
 
12
12
  type TextBlock = RichTextStructuredBlock | QuoteStructuredBlock | DateStructuredBlock;
13
13
 
@@ -15,7 +15,9 @@ const richTextConverter = new RichTextConverter();
15
15
 
16
16
  /**
17
17
  * A block of text: rich text, a quote, which is drawn in a box of its own, or a date, which comes as
18
- * the text it reads as. Its text is set in the styles it was drawn with, its ranges styled over them.
18
+ * the text it reads as. Its text is set in the styles it was drawn with, its ranges styled over them,
19
+ * and each paragraph is one of the page's; a quote is a quotation, and a heading, which takes no
20
+ * paragraphs, holds its lines in spans that look the same.
19
21
  */
20
22
  export const StructuredRichText: FC<StructuredBlockItemProps<TextBlock>> = ({ block }) => {
21
23
  const reactId = useId();
@@ -23,15 +25,17 @@ export const StructuredRichText: FC<StructuredBlockItemProps<TextBlock>> = ({ bl
23
25
  const { layouts } = useCntrlContext();
24
26
  const [ref, setRef] = useState<HTMLDivElement | null>(null);
25
27
  useItemGeometry(block.id, ref);
28
+ const headingTag = getHeadingTag(block);
29
+ const isQuote = block.type === StructuredBlockType.Quote;
30
+ const paragraphTag = headingTag ? 'span' : 'p';
26
31
  const [content, rangeStyles, listStyles] = useMemo(
27
- () => richTextConverter.toHtml(block, layouts, { isScaledToViewport: false }),
28
- [block, layouts]
32
+ () => richTextConverter.toHtml(block, layouts, { isScaledToViewport: false, paragraphTag }),
33
+ [block, layouts, paragraphTag]
29
34
  );
30
35
  const lang = useMemo(() => detectLang(block.commonParams.text), [block.commonParams.text]);
31
- const isQuote = block.type === StructuredBlockType.Quote;
32
36
  // only a text block of the stack flows in columns: a quote keeps to its box, a date to its line
33
37
  const isText = block.type === StructuredBlockType.RichText;
34
- const Wrapper = (getHeadingTag(block) ?? 'div') as 'div';
38
+ const Wrapper = (headingTag ?? (isQuote ? 'blockquote' : 'div')) as 'div';
35
39
  const layoutValues: Record<string, any>[] = [block.layoutParams];
36
40
  return (
37
41
  <>
@@ -49,14 +53,7 @@ export const StructuredRichText: FC<StructuredBlockItemProps<TextBlock>> = ({ bl
49
53
  {`${getLayoutStyles(layouts, layoutValues, ([layoutParams]) => (`
50
54
  ${textStylesRule(`.structured-text-${block.id}`, layoutParams)}
51
55
  ${isText ? textColumnsRule(`.structured-text-${block.id}`, layoutParams) : ''}
52
- ${isQuote ? `
53
- .structured-quote-box-${block.id} {
54
- padding: ${[layoutParams.boxPaddingTop, layoutParams.boxPaddingRight, layoutParams.boxPaddingBottom, layoutParams.boxPaddingLeft].map(side => `${(side ?? 0) * 100}vw`).join(' ')};
55
- border-radius: ${(layoutParams.boxRadius ?? 0) * 100}vw;
56
- background-color: transparent;
57
- }
58
- ${layoutParams.background ? colorRule(`.structured-quote-box-${block.id}`, 'background-color', layoutParams.background) : ''}
59
- ` : ''}
56
+ ${isQuote ? boxRule(`.structured-quote-box-${block.id}`, layoutParams) : ''}
60
57
  `))}`}
61
58
  </JSXStyle>
62
59
  </>
@@ -5,7 +5,8 @@ import { useCntrlContext } from '../../../provider/useCntrlContext';
5
5
  import { useItemGeometry } from '../../../ItemGeometry/useItemGeometry';
6
6
  import { LIST_STYLES_ID, RichTextConverter } from '../../../utils/RichTextConverter/RichTextConverter';
7
7
  import { StructuredBlockItemProps } from '../StructuredBlockItem';
8
- import { colorRule, textStylesRule } from '../textStylesRule';
8
+ import { boxRule, colorRule, textStylesRule } from '../textStylesRule';
9
+ import { vw } from '../vw';
9
10
 
10
11
  // the lines between the cells, in pixels of the layout, scaled with it like everything drawn in it
11
12
  const BORDER_PX = 1;
@@ -37,7 +38,7 @@ function convertCells(block: TableStructuredBlock, layouts: Layout[]): { cells:
37
38
  * lined in the styles it comes with, their ranges styled over them, each column its share of the
38
39
  * width and padded on either side as its own values say. Cells draw a heading row over a grid; rows
39
40
  * 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
+ * between each pair of rows only. The table is drawn in a box, which grows out past its columns.
41
42
  */
42
43
  export const StructuredTable: FC<StructuredBlockItemProps<TableStructuredBlock>> = ({ block }) => {
43
44
  const reactId = useId();
@@ -49,21 +50,24 @@ export const StructuredTable: FC<StructuredBlockItemProps<TableStructuredBlock>>
49
50
  const headingAsRow = kind === 'rows';
50
51
  const Heading = headingAsRow ? 'td' : 'th';
51
52
  const table = `.structured-table-${block.id}`;
53
+ const box = `.structured-table-box-${block.id}`;
52
54
  const cells = `${table} th, ${table} td`;
53
55
  const cellContent = (cell: StructuredBlockTableCell) => content.get(cell.id);
54
56
  return (
55
57
  <>
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>
58
+ <div className={`structured-table-box-${block.id}`}>
59
+ <table ref={setRef} className={`structured-table-${block.id}`}>
60
+ <colgroup>
61
+ {header.map((_, col) => <col key={col} />)}
62
+ </colgroup>
63
+ <thead className={`structured-table-heading-${block.id}`}>
64
+ <tr>{header.map(cell => <Heading key={cell.id}>{cellContent(cell)}</Heading>)}</tr>
65
+ </thead>
66
+ <tbody className={`structured-table-body-${block.id}`}>
67
+ {rows.map((row, index) => <tr key={row[0]?.id ?? index}>{row.map(cell => <td key={cell.id}>{cellContent(cell)}</td>)}</tr>)}
68
+ </tbody>
69
+ </table>
70
+ </div>
67
71
  {listStyles && <JSXStyle id={LIST_STYLES_ID}>{listStyles}</JSXStyle>}
68
72
  <JSXStyle id={`${reactId}-table-${block.id}`}>
69
73
  {rangeStyles}
@@ -83,10 +87,10 @@ export const StructuredTable: FC<StructuredBlockItemProps<TableStructuredBlock>>
83
87
  overflow-wrap: anywhere;
84
88
  }
85
89
  ${getLayoutStyles(layouts, [block.layoutParams], ([layoutParams], exemplary) => {
86
- const vw = (value: number) => `${value * 100}vw`;
87
90
  const border = vw(BORDER_PX / exemplary);
88
91
  const fill = (selector: string, color: string | undefined) => (color ? colorRule(selector, 'background-color', color) : '');
89
92
  return (`
93
+ ${boxRule(box, layoutParams)}
90
94
  ${textStylesRule(table, layoutParams)}
91
95
  ${cells} {
92
96
  padding-top: ${vw(layoutParams.cellPaddingTop)};
@@ -1,16 +1,17 @@
1
1
  import { CntrlColor } from '@cntrl-site/color';
2
- import { Hyphens, StructuredBlockRichTextLayoutParams, StructuredBlockTextStyles } from '@cntrl-site/sdk';
2
+ import { Hyphens, StructuredBlockBoxParams, StructuredBlockRichTextLayoutParams, StructuredBlockTextStyles } from '@cntrl-site/sdk';
3
3
  import { getFontFamilyValue } from '../../utils/getFontFamilyValue';
4
+ import { vw } from './vw';
4
5
 
5
6
  /** A block's text styles as a CSS rule for `selector`, its lengths in viewport units as the block's own. */
6
7
  export function textStylesRule(selector: string, styles: StructuredBlockTextStyles): string {
7
8
  return `
8
9
  ${selector} {
9
10
  font-family: ${getFontFamilyValue(styles.typeFace)};
10
- font-size: ${styles.fontSize * 100}vw;
11
- line-height: ${styles.lineHeight * 100}vw;
12
- letter-spacing: ${styles.letterSpacing * 100}vw;
13
- word-spacing: ${styles.wordSpacing * 100}vw;
11
+ font-size: ${vw(styles.fontSize)};
12
+ line-height: ${vw(styles.lineHeight)};
13
+ letter-spacing: ${vw(styles.letterSpacing)};
14
+ word-spacing: ${vw(styles.wordSpacing)};
14
15
  font-weight: ${styles.fontWeight};
15
16
  font-style: ${styles.fontStyle || 'normal'};
16
17
  font-variant: ${styles.fontVariant};
@@ -35,11 +36,28 @@ export function textColumnsRule(
35
36
  ${selector} {
36
37
  hyphens: ${hyphens};
37
38
  -webkit-hyphens: ${hyphens};
38
- ${columns > 1 ? `column-count: ${columns}; column-gap: ${columnGap * 100}vw; column-fill: balance;` : 'column-count: auto; column-gap: normal;'}
39
+ ${columns > 1 ? `column-count: ${columns}; column-gap: ${vw(columnGap)}; column-fill: balance;` : 'column-count: auto; column-gap: normal;'}
39
40
  }
40
41
  `;
41
42
  }
42
43
 
44
+ /**
45
+ * The box a quote's text or a table is drawn in, as a CSS rule for `selector`: padded inside, its
46
+ * sides grown out past the columns the block is placed on by as much as they pad it, so what it
47
+ * holds stays on them; rounded, and filled where it has a fill.
48
+ */
49
+ export function boxRule(selector: string, box: StructuredBlockBoxParams): string {
50
+ return `
51
+ ${selector} {
52
+ padding: ${[box.boxPaddingTop, box.boxPaddingRight, box.boxPaddingBottom, box.boxPaddingLeft].map(vw).join(' ')};
53
+ margin: 0 -${vw(box.boxPaddingRight)} 0 -${vw(box.boxPaddingLeft)};
54
+ border-radius: ${vw(box.boxRadius)};
55
+ background-color: transparent;
56
+ }
57
+ ${box.boxBackground ? colorRule(selector, 'background-color', box.boxBackground) : ''}
58
+ `;
59
+ }
60
+
43
61
  /** A colour of the palette as CSS for `property`, with a fallback where oklch is not supported. */
44
62
  export function colorRule(selector: string, property: string, value: string): string {
45
63
  const color = CntrlColor.parse(value);
@@ -0,0 +1,4 @@
1
+ /** A share of the layout width as CSS: the layout is the viewport's width, so a share is so many `vw`. */
2
+ export function vw(share: number | undefined): string {
3
+ return `${(share ?? 0) * 100}vw`;
4
+ }
@@ -59,7 +59,7 @@ const text = block(StructuredBlockType.RichText, 'text', paragraph('Read the pos
59
59
  const quote = block(StructuredBlockType.Quote, 'quote', { ...paragraph('A quote'), kind: 'boxed' }, {
60
60
  ...textStyles,
61
61
  rangeStyles: [],
62
- background: '#EEEEEE',
62
+ boxBackground: '#EEEEEE',
63
63
  boxPaddingTop: 0.02,
64
64
  boxPaddingRight: 0.03,
65
65
  boxPaddingBottom: 0.02,
@@ -74,7 +74,7 @@ const gallery = block(StructuredBlockType.Image, 'gallery', {
74
74
  urls: ['https://cdn.cntrl.site/a.jpg', 'https://cdn.cntrl.site/b.jpg'],
75
75
  altText: 'Two views',
76
76
  caption: 'Seen from the hill'
77
- }, { opacity: 0.8, captionStyles: { ...textStyles, fontSize: 0.01 } });
77
+ }, { opacity: 0.8, captionStyles: { ...textStyles, fontSize: 0.01, maxWidth: 0.45, innerPaddingTop: 0.02, innerPaddingBottom: 0.01 } });
78
78
  const video = block(StructuredBlockType.Video, 'video', { url: 'https://cdn.cntrl.site/clip.mp4', coverUrl: 'https://cdn.cntrl.site/cover.jpg' }, {
79
79
  play: 'on-click',
80
80
  muted: true,
@@ -182,6 +182,9 @@ describe('StructuredContent', () => {
182
182
  expect(container.querySelector('.structured-text-heading')!.tagName).toBe('H1');
183
183
  expect(container.querySelector('.structured-text-text')!.tagName).toBe('DIV');
184
184
  expect(css()).toMatch(/\.structured-text-heading \{\s*margin: 0;/);
185
+ // a heading takes no paragraphs: its lines are spans drawn as blocks; other text's are paragraphs
186
+ expect([...container.querySelector('.structured-text-heading')!.children].map(line => line.tagName)).toEqual(['SPAN']);
187
+ expect([...container.querySelector('.structured-text-text')!.children].map(line => line.tagName)).toEqual(['P']);
185
188
  });
186
189
 
187
190
  it('sets text in its styles, its ranges in viewport units', () => {
@@ -201,8 +204,15 @@ describe('StructuredContent', () => {
201
204
  it('draws a quote in its box and a date as the text it reads as', () => {
202
205
  const { container } = draw(section(false));
203
206
  expect(container.querySelector('.structured-quote-box-quote')!.textContent!.trim()).toBe('A quote');
204
- expect(css()).toMatch(/\.structured-quote-box-quote \{[^}]*padding: 2vw 3vw 2vw 1vw;[^}]*border-radius: 1vw;/);
207
+ // its sides grow out past the columns the text is placed on, which it stays on
208
+ expect(css()).toMatch(/\.structured-quote-box-quote \{[^}]*padding: 2vw 3vw 2vw 1vw;[^}]*margin: 0 -3vw 0 -1vw;[^}]*border-radius: 1vw;/);
209
+ expect(css()).toMatch(/\.structured-quote-box-quote \{\s*background-color: oklch/);
205
210
  expect(container.querySelector('.structured-text-date')!.textContent!.trim()).toBe('23 Sep 2026');
211
+ // a quote is a quotation of paragraphs, kept from the margins of both
212
+ expect(container.querySelector('.structured-text-quote')!.tagName).toBe('BLOCKQUOTE');
213
+ expect(container.querySelector('.structured-quote-box-quote > p')!.textContent!.trim()).toBe('A quote');
214
+ expect(css()).toMatch(/\.structured-text-quote \{\s*margin: 0;/);
215
+ expect(container.querySelector('.structured-text-date > p')!.textContent!.trim()).toBe('23 Sep 2026');
206
216
  });
207
217
 
208
218
  it('draws a gallery\'s images side by side, its caption under them', () => {
@@ -212,6 +222,7 @@ describe('StructuredContent', () => {
212
222
  // equally wide until their own proportions are known
213
223
  expect(images.map(image => image.style.flexGrow)).toEqual(['0.5', '0.5']);
214
224
  expect(container.querySelector('.structured-caption-gallery')!.textContent).toBe('Seen from the hill');
225
+ expect(css()).toMatch(/\.structured-caption-gallery \{[^}]*width: min\(45vw, 100%\);[^}]*padding-top: 2vw;[^}]*padding-bottom: 1vw;/);
215
226
  });
216
227
 
217
228
  it('draws no caption an image has switched off, nor one that says nothing', () => {
@@ -260,6 +271,22 @@ describe('StructuredContent', () => {
260
271
  expect(css()).toMatch(/\.structured-table-body-table \{\s*background-color: oklch/);
261
272
  });
262
273
 
274
+ it('draws a table in its box, its sides grown out past the columns the table stays on, filled and rounded', () => {
275
+ const boxed = block(StructuredBlockType.Table, 'boxed', { kind: 'rows', ...tableContent('boxed') }, {
276
+ ...tableCells,
277
+ boxBackground: '#FAFAFA',
278
+ boxPaddingTop: 0.01,
279
+ boxPaddingRight: 0.02,
280
+ boxPaddingBottom: 0.01,
281
+ boxPaddingLeft: 0.02,
282
+ boxRadius: 0.005
283
+ });
284
+ const { container } = draw({ ...section(false), structuredContent: [boxed] } as Section);
285
+ expect(container.querySelector('.structured-table-box-boxed > .structured-table-boxed')).not.toBeNull();
286
+ expect(css()).toMatch(/\.structured-table-box-boxed \{[^}]*padding: 1vw 2vw 1vw 2vw;[^}]*margin: 0 -2vw 0 -2vw;[^}]*border-radius: 0\.5vw;/);
287
+ expect(css()).toMatch(/\.structured-table-box-boxed \{\s*background-color: oklch/);
288
+ });
289
+
263
290
  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
291
  const listed = block(StructuredBlockType.Table, 'listed', {
265
292
  kind: 'cells',
@@ -80,6 +80,23 @@ describe('RichTextConverter', () => {
80
80
  expect(container.textContent).toBe('a');
81
81
  });
82
82
 
83
+ it('draws each paragraph in the element asked for, without its margins, a span as a block; a div unless asked', () => {
84
+ const source = text([{ text: 'Read on' }, { text: '' }]);
85
+ const [paragraphs, paragraphStyles] = converter.toHtml(source, layouts, { paragraphTag: 'p' });
86
+ const { container } = render(<>{paragraphs}</>);
87
+ expect([...container.children].map(child => child.tagName)).toEqual(['P', 'P']);
88
+ expect(paragraphStyles).toMatch(/\.rt_text-b0_1 \{ margin: 0; \}/);
89
+ expect(paragraphStyles).toMatch(/\.rt_text_br_1 \{ display: block; margin: 0; \}/);
90
+
91
+ const [lines, lineStyles] = converter.toHtml(source, layouts, { paragraphTag: 'span' });
92
+ expect([...render(<>{lines}</>).container.children].map(child => child.tagName)).toEqual(['SPAN', 'SPAN']);
93
+ expect(lineStyles).toMatch(/\.rt_text_br_1 \{ display: block; margin: 0; \}/);
94
+
95
+ const [divs, divStyles] = converter.toHtml(source, layouts);
96
+ expect([...render(<>{divs}</>).container.children].map(child => child.tagName)).toEqual(['DIV', 'DIV']);
97
+ expect(divStyles).not.toContain('margin: 0');
98
+ });
99
+
83
100
  it('leaves text without lists as it was', () => {
84
101
  const [content, styles] = converter.toHtml(text([{ text: 'Plain' }]), layouts);
85
102
  const { container } = render(<>{content}</>);
@@ -33,6 +33,12 @@ interface ToHtmlOptions {
33
33
  * scale would leave at the unscaled size, takes viewport units throughout.
34
34
  */
35
35
  isScaledToViewport?: boolean;
36
+ /**
37
+ * The element each paragraph is drawn in: a div unless said otherwise; a p for text read as
38
+ * paragraphs, or, inside a heading, which takes no paragraphs, a span drawn as a block. A p or a
39
+ * span keeps none of its own margins, so the text looks the same whichever it is drawn in.
40
+ */
41
+ paragraphTag?: 'div' | 'p' | 'span';
36
42
  }
37
43
 
38
44
  interface StyleGroup {
@@ -105,11 +111,13 @@ export class RichTextConverter {
105
111
  toHtml(
106
112
  richText: RichTextSource,
107
113
  layouts: Layout[],
108
- { isScaledToViewport = true }: ToHtmlOptions = {}
114
+ { isScaledToViewport = true, paragraphTag: Paragraph = 'div' }: ToHtmlOptions = {}
109
115
  ): [ReactNode[], string, string | undefined] {
110
116
  const { text, blocks = [] } = richText.commonParams;
111
117
  const hasLists = blocks.some(block => block.type in LIST_ITEM_CLASSES);
112
118
  const root: ReactElement[] = [];
119
+ // what a div is of itself, a paragraph drawn in another element is told: a block, without margins
120
+ const paragraphRules: string[] = [];
113
121
  const styleRules = layouts.reduce<Record<string, string[]>>((rec, layout) => {
114
122
  rec[layout.id] = [];
115
123
  return rec;
@@ -128,7 +136,8 @@ export class RichTextConverter {
128
136
  // an empty line keeps its height, as in the editor; a text never typed into comes without even its line break
129
137
  if (content === '' || content === '\n') {
130
138
  const id = `rt_${richText.id}_br_${blockIndex}`;
131
- root.push(<div key={id} className={`${id}${typeClass}`}><br /></div>);
139
+ root.push(<Paragraph key={id} className={`${id}${typeClass}`}><br /></Paragraph>);
140
+ if (Paragraph !== 'div') paragraphRules.push(`.${id} { display: block; margin: 0; }`);
132
141
  layouts.forEach(l => {
133
142
  const lhForLayout = currentLineHeight[l.id];
134
143
  if (lhForLayout === undefined) return;
@@ -253,14 +262,19 @@ export class RichTextConverter {
253
262
  }
254
263
  }
255
264
  }
256
- root.push(<div key={blockClass} className={`${blockClass}${typeClass}`}>{kids}</div>);
265
+ root.push(<Paragraph key={blockClass} className={`${blockClass}${typeClass}`}>{kids}</Paragraph>);
266
+ // each layout shows or hides the paragraph itself
267
+ if (Paragraph !== 'div') paragraphRules.push(`.${blockClass} { margin: 0; }`);
257
268
  }
258
269
  }
259
- const styles = layouts.map(l => `
270
+ const styles = [
271
+ ...paragraphRules,
272
+ ...layouts.map(l => `
260
273
  ${getLayoutMediaQuery(l.id, layouts)} {
261
274
  ${styleRules[l.id].join('\n')}
262
275
  }
263
- `).join('\n');
276
+ `)
277
+ ].join('\n');
264
278
  return [
265
279
  root,
266
280
  styles,