@cntrl-site/sdk-nextjs 1.11.0-2 → 1.11.1
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 +10 -5
- 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 +26 -4
- package/src/utils/RichTextConverter/RichTextConverter.tsx +11 -5
|
@@ -6,11 +6,11 @@ import {
|
|
|
6
6
|
StructuredBlockAny,
|
|
7
7
|
StructuredBlockType
|
|
8
8
|
} from '@cntrl-site/sdk';
|
|
9
|
-
import { FC, useId } from 'react';
|
|
9
|
+
import { FC, ReactNode, useId } from 'react';
|
|
10
10
|
import JSXStyle from 'styled-jsx/style';
|
|
11
11
|
import { useCntrlContext } from '../../provider/useCntrlContext';
|
|
12
12
|
import { blockPlacement } from '../StructuredBlockItem/blockPlacement';
|
|
13
|
-
import { blocksMap } from '../StructuredBlockItem/blocksMap';
|
|
13
|
+
import { blocksMap, UnknownBlock } from '../StructuredBlockItem/blocksMap';
|
|
14
14
|
|
|
15
15
|
interface Props {
|
|
16
16
|
header: HeaderStructuredBlock;
|
|
@@ -21,18 +21,26 @@ interface Props {
|
|
|
21
21
|
|
|
22
22
|
const TEXT_KINDS: HeaderElementKind[] = ['title', 'subtitle', 'date'];
|
|
23
23
|
|
|
24
|
+
// an element a header stored before its kind was ordered goes last, not first
|
|
25
|
+
function orderOf(order: HeaderElementKind[], kind: HeaderElementKind): number {
|
|
26
|
+
const index = order.indexOf(kind);
|
|
27
|
+
return index === -1 ? order.length : index;
|
|
28
|
+
}
|
|
29
|
+
|
|
24
30
|
/**
|
|
25
31
|
* A content-based section's header, drawn first. Variant A draws the key image full width with the
|
|
26
|
-
* other elements on its bottom edge, the image covering a share of the viewport's height if set to
|
|
27
|
-
*
|
|
28
|
-
*
|
|
32
|
+
* other elements on its bottom edge, the image covering a share of the viewport's height if set to,
|
|
33
|
+
* and staying put on the screen while the header scrolls over it if set to; variant B flows the
|
|
34
|
+
* shown elements in order, each placed as in the stack. The variant, the order and what is shown
|
|
35
|
+
* are a layout's, so every element is drawn once and arranged per layout.
|
|
29
36
|
*/
|
|
30
37
|
export const StructuredHeader: FC<Props> = ({ header, blocks, readingWidth }) => {
|
|
31
38
|
const reactId = useId();
|
|
32
39
|
const { layouts } = useCntrlContext();
|
|
33
40
|
const elementOf = (kind: HeaderElementKind) => blocks.find(block => block.id === header.commonParams[kind]);
|
|
34
41
|
const image = elementOf('image');
|
|
35
|
-
const
|
|
42
|
+
const imageUrl = image?.type === StructuredBlockType.HeaderImage ? (image as HeaderImageStructuredBlock).commonParams.url : '';
|
|
43
|
+
const hasImage = Boolean(imageUrl);
|
|
36
44
|
const texts = TEXT_KINDS.flatMap((kind) => {
|
|
37
45
|
const block = elementOf(kind);
|
|
38
46
|
return block ? [{ kind, block }] : [];
|
|
@@ -40,14 +48,47 @@ export const StructuredHeader: FC<Props> = ({ header, blocks, readingWidth }) =>
|
|
|
40
48
|
const headerClass = `structured-header-${header.id}`;
|
|
41
49
|
const stackClass = `structured-header-stack-${header.id}`;
|
|
42
50
|
const elementClass = (block: StructuredBlockAny) => `structured-header-element-${block.id}`;
|
|
43
|
-
const
|
|
44
|
-
|
|
51
|
+
const pinClass = (block: StructuredBlockAny, part = '') => `structured-header-pin${part}-${block.id}`;
|
|
52
|
+
const renderElement = (block: StructuredBlockAny, extra?: ReactNode) => {
|
|
53
|
+
const BlockComponent = blocksMap[block.type] ?? UnknownBlock;
|
|
45
54
|
return (
|
|
46
55
|
<div key={block.id} className={elementClass(block)}>
|
|
47
56
|
<BlockComponent block={block} readingWidth={readingWidth} />
|
|
57
|
+
{extra}
|
|
48
58
|
</div>
|
|
49
59
|
);
|
|
50
60
|
};
|
|
61
|
+
// a sticky backdrop, the way a section's fixed background is drawn: a screen-tall frame stuck
|
|
62
|
+
// inside a track a screen longer at each end, clipped to the header; the picture itself only
|
|
63
|
+
// sizes the header then
|
|
64
|
+
const renderPin = (block: StructuredBlockAny) => (
|
|
65
|
+
<div className={pinClass(block)}>
|
|
66
|
+
<div className={pinClass(block, '-track')}>
|
|
67
|
+
<div className={pinClass(block, '-frame')} style={{ backgroundImage: `url(${imageUrl})` }} />
|
|
68
|
+
</div>
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
const pinStyles = (block: StructuredBlockAny) => `
|
|
72
|
+
.${pinClass(block)} {
|
|
73
|
+
position: absolute;
|
|
74
|
+
inset: 0;
|
|
75
|
+
overflow: clip;
|
|
76
|
+
pointer-events: none;
|
|
77
|
+
}
|
|
78
|
+
.${pinClass(block, '-track')} {
|
|
79
|
+
position: relative;
|
|
80
|
+
width: 100%;
|
|
81
|
+
height: calc(100% + 200vh);
|
|
82
|
+
transform: translateY(-100vh);
|
|
83
|
+
}
|
|
84
|
+
.${pinClass(block, '-frame')} {
|
|
85
|
+
position: sticky;
|
|
86
|
+
top: 100vh;
|
|
87
|
+
width: 100%;
|
|
88
|
+
height: 100vh;
|
|
89
|
+
background-repeat: no-repeat;
|
|
90
|
+
}
|
|
91
|
+
`;
|
|
51
92
|
const elementStyles = (kind: HeaderElementKind, block: StructuredBlockAny) => getLayoutStyles(
|
|
52
93
|
layouts,
|
|
53
94
|
[header.layoutParams, block.area, block.layoutParams] as Record<string, any>[],
|
|
@@ -55,35 +96,46 @@ export const StructuredHeader: FC<Props> = ({ header, blocks, readingWidth }) =>
|
|
|
55
96
|
const isBackdrop = kind === 'image' && layoutParams.variant === 'A';
|
|
56
97
|
const isHidden = !isBackdrop && layoutParams.hiddenElements.includes(kind);
|
|
57
98
|
const isCover = isBackdrop && layoutParams.imageFit === 'cover';
|
|
99
|
+
const isSticky = isBackdrop && Boolean(layoutParams.stickyImage);
|
|
100
|
+
// every value is written on every layout: the media queries stack upward, so a layout's
|
|
101
|
+
// backdrop or cover would otherwise carry into the larger layouts drawn another way
|
|
58
102
|
return (`
|
|
59
103
|
.${elementClass(block)} {
|
|
60
104
|
display: ${isHidden ? 'none' : 'block'};
|
|
61
105
|
position: relative;
|
|
62
|
-
order: ${layoutParams.order
|
|
106
|
+
order: ${orderOf(layoutParams.order, kind)};
|
|
63
107
|
${isBackdrop
|
|
64
108
|
? `width: 100%; left: 0; height: ${isCover ? '100%' : 'auto'}; padding: 0;`
|
|
65
109
|
: blockPlacement(area, blockLayoutParams)}
|
|
66
110
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
111
|
+
.${elementClass(block)} .structured-caption-${block.id} {
|
|
112
|
+
display: ${isBackdrop ? 'none' : 'block'};
|
|
113
|
+
}
|
|
114
|
+
.${elementClass(block)} .structured-header-image-${block.id},
|
|
115
|
+
.${elementClass(block)} .structured-header-picture-${block.id} {
|
|
116
|
+
height: ${isCover ? '100%' : 'auto'};
|
|
117
|
+
}
|
|
118
|
+
.${elementClass(block)} .structured-header-picture-${block.id} {
|
|
119
|
+
object-fit: ${isCover ? 'cover' : 'fill'};
|
|
120
|
+
visibility: ${isSticky ? 'hidden' : 'visible'};
|
|
121
|
+
}
|
|
122
|
+
${kind === 'image'
|
|
123
|
+
? `
|
|
124
|
+
.${pinClass(block)} {
|
|
125
|
+
display: ${isSticky ? 'block' : 'none'};
|
|
126
|
+
}
|
|
127
|
+
.${pinClass(block, '-frame')} {
|
|
128
|
+
background-size: ${isCover ? 'cover' : '100% auto'};
|
|
129
|
+
background-position: ${isCover ? 'center' : 'top center'};
|
|
130
|
+
opacity: ${blockLayoutParams?.opacity ?? 1};
|
|
131
|
+
}`
|
|
132
|
+
: ''}
|
|
81
133
|
`);
|
|
82
134
|
}
|
|
83
135
|
);
|
|
84
136
|
return (
|
|
85
137
|
<div className={headerClass}>
|
|
86
|
-
{image && renderElement(image)}
|
|
138
|
+
{image && renderElement(image, hasImage && renderPin(image))}
|
|
87
139
|
<div className={stackClass}>
|
|
88
140
|
{texts.map(({ block }) => renderElement(block))}
|
|
89
141
|
</div>
|
|
@@ -92,6 +144,7 @@ export const StructuredHeader: FC<Props> = ({ header, blocks, readingWidth }) =>
|
|
|
92
144
|
position: relative;
|
|
93
145
|
width: 100%;
|
|
94
146
|
}
|
|
147
|
+
${image && hasImage ? pinStyles(image) : ''}
|
|
95
148
|
${getLayoutStyles(layouts, [header.layoutParams, header.area] as Record<string, any>[], ([layoutParams, area]) => {
|
|
96
149
|
const isA = layoutParams.variant === 'A';
|
|
97
150
|
const isCover = isA && layoutParams.imageFit === 'cover';
|
|
@@ -100,6 +153,7 @@ export const StructuredHeader: FC<Props> = ({ header, blocks, readingWidth }) =>
|
|
|
100
153
|
display: ${isA ? 'block' : 'flex'};
|
|
101
154
|
flex-direction: column;
|
|
102
155
|
z-index: ${area?.zIndex ?? 0};
|
|
156
|
+
margin-top: ${area?.paddingTop ? `${area.paddingTop * 100}vw` : 0};
|
|
103
157
|
height: ${isCover ? `${layoutParams.coverHeight * 100}vh` : 'auto'};
|
|
104
158
|
height: ${isCover ? `${layoutParams.coverHeight * 100}svh` : 'auto'};
|
|
105
159
|
}
|
|
@@ -11,6 +11,7 @@ import { useLayoutContext } from '../../useLayoutContext';
|
|
|
11
11
|
import { useItemGeometry } from '../../../ItemGeometry/useItemGeometry';
|
|
12
12
|
import { LinkWrapper } from '../LinkWrapper';
|
|
13
13
|
import { mergeComponentSettings } from '../../../utils/mergeComponentSettings';
|
|
14
|
+
import { pendingElementProps, useComponentElement } from '../../../common/useComponentElement';
|
|
14
15
|
|
|
15
16
|
export const ComponentItem: FC<ItemProps<TComponentItem>> = ({ item, sectionId, onResize, interactionCtrl }) => {
|
|
16
17
|
const sdk = useCntrlContext();
|
|
@@ -20,7 +21,8 @@ export const ComponentItem: FC<ItemProps<TComponentItem>> = ({ item, sectionId,
|
|
|
20
21
|
const fallbackLayout = layouts[0]?.id;
|
|
21
22
|
const effectiveLayout = layout ?? fallbackLayout;
|
|
22
23
|
const layoutValues: Record<string, any>[] = [item.area, item.layoutParams];
|
|
23
|
-
const
|
|
24
|
+
const { Element, isPending, isFailed } = useComponentElement(item.commonParams.componentId);
|
|
25
|
+
const keepsServerMarkup = isPending || isFailed;
|
|
24
26
|
const [ref, setRef] = useState<HTMLDivElement | null>(null);
|
|
25
27
|
const { opacity: itemOpacity, blur: itemBlur } = useComponentItem(item, sectionId);
|
|
26
28
|
useRegisterResize(ref, onResize);
|
|
@@ -29,7 +31,6 @@ export const ComponentItem: FC<ItemProps<TComponentItem>> = ({ item, sectionId,
|
|
|
29
31
|
const angle = getStyleFromItemStateAndParams(stateParams?.styles?.angle, itemAngle);
|
|
30
32
|
const opacity = getStyleFromItemStateAndParams(stateParams?.styles?.opacity, itemOpacity);
|
|
31
33
|
const blur = getStyleFromItemStateAndParams(stateParams?.styles?.blur, itemBlur);
|
|
32
|
-
const Element = component ? component.element : undefined;
|
|
33
34
|
const layoutParameters = effectiveLayout ? item.layoutParams[effectiveLayout]?.parameters : undefined;
|
|
34
35
|
const commonParameters = item.commonParams.parameters;
|
|
35
36
|
const parameters = layoutParameters ? {
|
|
@@ -49,20 +50,22 @@ export const ComponentItem: FC<ItemProps<TComponentItem>> = ({ item, sectionId,
|
|
|
49
50
|
>
|
|
50
51
|
<>
|
|
51
52
|
<div
|
|
53
|
+
key={keepsServerMarkup ? 'pending' : 'ready'}
|
|
52
54
|
className={`custom-component-${item.id}`}
|
|
53
55
|
ref={setRef}
|
|
54
56
|
style={{
|
|
55
57
|
// preventing layout shift while supporting SSG for proper SEO
|
|
56
58
|
...(opacity !== undefined
|
|
57
|
-
? { opacity: layout == null ? 0 : opacity }
|
|
58
|
-
: layout == null ? { opacity: 0 } : {}),
|
|
59
|
+
? { opacity: layout == null || isPending ? 0 : opacity }
|
|
60
|
+
: layout == null || isPending ? { opacity: 0 } : {}),
|
|
59
61
|
...(angle !== undefined ? { transform: `rotate(${angle}deg)` } : {}),
|
|
60
62
|
...(blur !== undefined && blur !== 0 ? { filter: `blur(${blur * 100}vw)` } : {}),
|
|
61
63
|
willChange: blur !== 0 && blur !== undefined ? 'transform' : 'unset',
|
|
62
64
|
transition: stateParams?.transition ?? 'none'
|
|
63
65
|
}}
|
|
66
|
+
{...(keepsServerMarkup ? pendingElementProps : {})}
|
|
64
67
|
>
|
|
65
|
-
{parameters && Element && (
|
|
68
|
+
{keepsServerMarkup ? null : parameters && Element && (
|
|
66
69
|
<Element
|
|
67
70
|
metadata={{
|
|
68
71
|
itemId: item.id,
|
|
@@ -15,6 +15,7 @@ import { useElementRect } from '../../../utils/useElementRect';
|
|
|
15
15
|
import { useItemFXData } from '../../../common/useItemFXData';
|
|
16
16
|
import { getFill } from '../../../utils/getFill';
|
|
17
17
|
import { useItemGeometry } from '../../../ItemGeometry/useItemGeometry';
|
|
18
|
+
import { useLazyVideo } from '../../../common/useLazyVideo';
|
|
18
19
|
|
|
19
20
|
export const VideoItem: FC<ItemProps<TVideoItem>> = ({ item, sectionId, onResize, interactionCtrl, onVisibilityChange }) => {
|
|
20
21
|
const reactId = useId();
|
|
@@ -79,6 +80,7 @@ export const VideoItem: FC<ItemProps<TVideoItem>> = ({ item, sectionId, onResize
|
|
|
79
80
|
);
|
|
80
81
|
useRegisterResize(ref, onResize);
|
|
81
82
|
useItemGeometry(item.id, ref);
|
|
83
|
+
useLazyVideo(videoRef, layoutParams?.play === 'auto');
|
|
82
84
|
const inlineStyles = {
|
|
83
85
|
...(radius !== undefined ? { borderRadius: `${radius * 100}vw` } : {}),
|
|
84
86
|
...(strokeWidth !== undefined ? {
|
|
@@ -150,8 +152,7 @@ export const VideoItem: FC<ItemProps<TVideoItem>> = ({ item, sectionId, onResize
|
|
|
150
152
|
<video
|
|
151
153
|
poster={item.commonParams.coverUrl ?? ''}
|
|
152
154
|
ref={setVideoRef}
|
|
153
|
-
|
|
154
|
-
preload="auto"
|
|
155
|
+
preload="none"
|
|
155
156
|
onClick={() => {
|
|
156
157
|
setIsVideoInteracted(true);
|
|
157
158
|
}}
|
|
@@ -4,6 +4,7 @@ import { getLayoutStyles, Hyphens, RichTextItem as TRichTextItem } from '@cntrl-
|
|
|
4
4
|
import JSXStyle from 'styled-jsx/style';
|
|
5
5
|
import { ItemProps } from '../Item';
|
|
6
6
|
import { useRichTextItem } from './useRichTextItem';
|
|
7
|
+
import { LIST_STYLES_ID } from '../../../utils/RichTextConverter/RichTextConverter';
|
|
7
8
|
import { useCntrlContext } from '../../../provider/useCntrlContext';
|
|
8
9
|
import { useRichTextItemValues } from '../useRichTextItemValues';
|
|
9
10
|
import { useRegisterResize } from '../../../common/useRegisterResize';
|
|
@@ -5,8 +5,7 @@ import { ReactNode } from 'react';
|
|
|
5
5
|
|
|
6
6
|
const richTextConverter = new RichTextConverter();
|
|
7
7
|
|
|
8
|
-
export
|
|
8
|
+
export function useRichTextItem(item: RichTextItem): [ReactNode[], string, string | undefined] {
|
|
9
9
|
const { layouts } = useCntrlContext();
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
10
|
+
return richTextConverter.toHtml(item, layouts);
|
|
11
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CustomItemRegistry } from './CustomItemRegistry';
|
|
2
2
|
import { Article, Section, Layout, Project, SectionHeight } from '@cntrl-site/sdk';
|
|
3
|
-
import {
|
|
3
|
+
import type { Component as TComponent } from '@cntrl-site/components';
|
|
4
4
|
import { CustomSectionRegistry } from './CustomSectionRegistry';
|
|
5
5
|
|
|
6
6
|
interface SdkContextInitProps {
|
|
@@ -43,7 +43,6 @@ export class CntrlSdkContext {
|
|
|
43
43
|
this._publicApiBase = publicApiBase;
|
|
44
44
|
}
|
|
45
45
|
this.setLayouts(project.layouts);
|
|
46
|
-
this.setComponents(components);
|
|
47
46
|
if (customComponents) {
|
|
48
47
|
this.setCustomComponents(customComponents);
|
|
49
48
|
}
|
|
@@ -68,12 +67,6 @@ export class CntrlSdkContext {
|
|
|
68
67
|
this._layouts = layouts;
|
|
69
68
|
}
|
|
70
69
|
|
|
71
|
-
private setComponents(components: TComponent[]) {
|
|
72
|
-
for (const component of components) {
|
|
73
|
-
this.components.set(component.id, component);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
70
|
setCustomComponents(customComponents: Map<string, TComponent>) {
|
|
78
71
|
for (const [id, component] of customComponents) {
|
|
79
72
|
this.components.set(id, component);
|
|
@@ -34,8 +34,8 @@ describe('RichTextConverter', () => {
|
|
|
34
34
|
expect(styles).not.toContain(RICH_TEXT_LAYOUT_PENDING_CLASS);
|
|
35
35
|
});
|
|
36
36
|
|
|
37
|
-
it('draws list items with their markers, a numbered run restarting after anything else', () => {
|
|
38
|
-
const [content, styles] = converter.toHtml(text([
|
|
37
|
+
it('draws list items with their markers, a numbered run restarting after anything else, the list styles apart for the page to hold once', () => {
|
|
38
|
+
const [content, styles, listStyles] = converter.toHtml(text([
|
|
39
39
|
{ text: 'Intro' },
|
|
40
40
|
{ text: 'One', type: 'ordered-list-item' },
|
|
41
41
|
{ text: 'Two', type: 'ordered-list-item' },
|
|
@@ -48,8 +48,10 @@ describe('RichTextConverter', () => {
|
|
|
48
48
|
'rt-ordered-list-item',
|
|
49
49
|
'rt-unordered-list-item'
|
|
50
50
|
]);
|
|
51
|
-
expect(
|
|
52
|
-
expect(
|
|
51
|
+
expect(listStyles).toContain('counter-increment: cntrl-list');
|
|
52
|
+
expect(listStyles).toContain('content: \'•\'');
|
|
53
|
+
expect(styles).not.toContain('cntrl-list');
|
|
54
|
+
expect(converter.toHtml(text([{ text: 'Plain' }]), layouts)[2]).toBeUndefined();
|
|
53
55
|
});
|
|
54
56
|
|
|
55
57
|
it('draws a highlighted range on its colour, with an rgba fallback where oklch is not supported', () => {
|
|
@@ -58,6 +60,26 @@ describe('RichTextConverter', () => {
|
|
|
58
60
|
expect(styles).toContain('background-color: rgba(255, 255, 0, 1)');
|
|
59
61
|
});
|
|
60
62
|
|
|
63
|
+
it('keeps an empty line a line tall, in a text never typed into as well, and a one-letter last line its letter', () => {
|
|
64
|
+
const untyped: RichTextSource = {
|
|
65
|
+
id: 'text',
|
|
66
|
+
commonParams: { text: '', blocks: [{ start: 0, end: 0, type: 'unstyled', entities: [] }] },
|
|
67
|
+
layoutParams: { desktop: { rangeStyles: [], textAlign: TextAlign.Left } }
|
|
68
|
+
};
|
|
69
|
+
const emptied = text([{ text: '' }], []);
|
|
70
|
+
for (const source of [untyped, emptied]) {
|
|
71
|
+
const { container } = render(<>{converter.toHtml(source, layouts)[0]}</>);
|
|
72
|
+
expect(container.innerHTML).toBe('<div class="rt_text_br_0"><br></div>');
|
|
73
|
+
}
|
|
74
|
+
const unterminated: RichTextSource = {
|
|
75
|
+
id: 'text',
|
|
76
|
+
commonParams: { text: 'a', blocks: [{ start: 0, end: 1, type: 'unstyled', entities: [] }] },
|
|
77
|
+
layoutParams: { desktop: { rangeStyles: [], textAlign: TextAlign.Left } }
|
|
78
|
+
};
|
|
79
|
+
const { container } = render(<>{converter.toHtml(unterminated, layouts)[0]}</>);
|
|
80
|
+
expect(container.textContent).toBe('a');
|
|
81
|
+
});
|
|
82
|
+
|
|
61
83
|
it('leaves text without lists as it was', () => {
|
|
62
84
|
const [content, styles] = converter.toHtml(text([{ text: 'Plain' }]), layouts);
|
|
63
85
|
const { container } = render(<>{content}</>);
|
|
@@ -69,6 +69,9 @@ const LIST_ITEM_CLASSES: Record<string, string> = {
|
|
|
69
69
|
'ordered-list-item': 'rt-ordered-list-item'
|
|
70
70
|
};
|
|
71
71
|
|
|
72
|
+
/** The id the list styles are rendered under: one copy a page, however many texts have lists. */
|
|
73
|
+
export const LIST_STYLES_ID = 'cntrl-rich-text-lists';
|
|
74
|
+
|
|
72
75
|
// a list item's marker hangs before its first line; a numbered run restarts after anything else
|
|
73
76
|
const LIST_STYLES = `
|
|
74
77
|
.rt-unordered-list-item,
|
|
@@ -98,11 +101,12 @@ const LIST_STYLES = `
|
|
|
98
101
|
`;
|
|
99
102
|
|
|
100
103
|
export class RichTextConverter {
|
|
104
|
+
/** 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. */
|
|
101
105
|
toHtml(
|
|
102
106
|
richText: RichTextSource,
|
|
103
107
|
layouts: Layout[],
|
|
104
108
|
{ isScaledToViewport = true }: ToHtmlOptions = {}
|
|
105
|
-
): [ReactNode[], string] {
|
|
109
|
+
): [ReactNode[], string, string | undefined] {
|
|
106
110
|
const { text, blocks = [] } = richText.commonParams;
|
|
107
111
|
const hasLists = blocks.some(block => block.type in LIST_ITEM_CLASSES);
|
|
108
112
|
const root: ReactElement[] = [];
|
|
@@ -119,9 +123,10 @@ export class RichTextConverter {
|
|
|
119
123
|
for (let blockIndex = 0; blockIndex < blocks.length; blockIndex++) {
|
|
120
124
|
const block = blocks[blockIndex];
|
|
121
125
|
const content = text.slice(block.start, block.end + 1);
|
|
122
|
-
const entities = block.entities
|
|
126
|
+
const entities = [...(block.entities ?? [])].sort((a, b) => a.start - b.start);
|
|
123
127
|
const typeClass = hasLists ? ` ${LIST_ITEM_CLASSES[block.type] ?? 'rt-paragraph'}` : '';
|
|
124
|
-
|
|
128
|
+
// an empty line keeps its height, as in the editor; a text never typed into comes without even its line break
|
|
129
|
+
if (content === '' || content === '\n') {
|
|
125
130
|
const id = `rt_${richText.id}_br_${blockIndex}`;
|
|
126
131
|
root.push(<div key={id} className={`${id}${typeClass}`}><br /></div>);
|
|
127
132
|
layouts.forEach(l => {
|
|
@@ -255,10 +260,11 @@ export class RichTextConverter {
|
|
|
255
260
|
${getLayoutMediaQuery(l.id, layouts)} {
|
|
256
261
|
${styleRules[l.id].join('\n')}
|
|
257
262
|
}
|
|
258
|
-
`).join('\n')
|
|
263
|
+
`).join('\n');
|
|
259
264
|
return [
|
|
260
265
|
root,
|
|
261
|
-
styles
|
|
266
|
+
styles,
|
|
267
|
+
hasLists ? LIST_STYLES : undefined
|
|
262
268
|
];
|
|
263
269
|
}
|
|
264
270
|
|