@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,215 @@
|
|
|
1
|
+
import { act, render } from '@testing-library/react';
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import { SectionVideo } from '../components/Section/SectionVideo';
|
|
4
|
+
import { useLazyVideo } from './useLazyVideo';
|
|
5
|
+
|
|
6
|
+
class FakeIntersectionObserver {
|
|
7
|
+
static instances: FakeIntersectionObserver[] = [];
|
|
8
|
+
readonly targets = new Set<Element>();
|
|
9
|
+
|
|
10
|
+
constructor(private readonly callback: IntersectionObserverCallback, readonly options: IntersectionObserverInit = {}) {
|
|
11
|
+
FakeIntersectionObserver.instances.push(this);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
observe(target: Element) {
|
|
15
|
+
this.targets.add(target);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
unobserve(target: Element) {
|
|
19
|
+
this.targets.delete(target);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
disconnect() {
|
|
23
|
+
this.targets.clear();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
takeRecords() {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
emit(target: Element, isIntersecting: boolean) {
|
|
31
|
+
this.callback([{ target, isIntersecting } as IntersectionObserverEntry], this as unknown as IntersectionObserver);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const NEAR = '100% 0px';
|
|
36
|
+
const VISIBLE = '0px';
|
|
37
|
+
|
|
38
|
+
function emit(rootMargin: string, target: Element, isIntersecting: boolean) {
|
|
39
|
+
const observer = FakeIntersectionObserver.instances.find(instance => instance.options.rootMargin === rootMargin);
|
|
40
|
+
if (!observer) throw new Error(`no observer for ${rootMargin}`);
|
|
41
|
+
act(() => observer.emit(target, isIntersecting));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function isObserved(rootMargin: string, target: Element) {
|
|
45
|
+
return FakeIntersectionObserver.instances.some(instance => instance.options.rootMargin === rootMargin && instance.targets.has(target));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const paused = new WeakMap<HTMLMediaElement, boolean>();
|
|
49
|
+
const play = jest.fn();
|
|
50
|
+
const pause = jest.fn();
|
|
51
|
+
|
|
52
|
+
function TestVideo({ autoPlay }: { autoPlay: boolean }) {
|
|
53
|
+
const [video, setVideo] = useState<HTMLVideoElement | null>(null);
|
|
54
|
+
useLazyVideo(video, autoPlay);
|
|
55
|
+
return <video ref={setVideo} preload="none" muted loop playsInline />;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
describe('useLazyVideo', () => {
|
|
59
|
+
const originalIntersectionObserver = window.IntersectionObserver;
|
|
60
|
+
|
|
61
|
+
beforeEach(() => {
|
|
62
|
+
window.IntersectionObserver = FakeIntersectionObserver as unknown as typeof IntersectionObserver;
|
|
63
|
+
play.mockReset();
|
|
64
|
+
pause.mockReset();
|
|
65
|
+
jest.spyOn(HTMLMediaElement.prototype, 'paused', 'get').mockImplementation(function (this: HTMLMediaElement) {
|
|
66
|
+
return paused.get(this) ?? true;
|
|
67
|
+
});
|
|
68
|
+
jest.spyOn(HTMLMediaElement.prototype, 'play').mockImplementation(function (this: HTMLMediaElement) {
|
|
69
|
+
play(this);
|
|
70
|
+
paused.set(this, false);
|
|
71
|
+
this.dispatchEvent(new Event('play'));
|
|
72
|
+
return Promise.resolve();
|
|
73
|
+
});
|
|
74
|
+
jest.spyOn(HTMLMediaElement.prototype, 'pause').mockImplementation(function (this: HTMLMediaElement) {
|
|
75
|
+
pause(this);
|
|
76
|
+
paused.set(this, true);
|
|
77
|
+
this.dispatchEvent(new Event('pause'));
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
afterEach(() => {
|
|
82
|
+
jest.restoreAllMocks();
|
|
83
|
+
window.IntersectionObserver = originalIntersectionObserver;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('loads nothing while the video is far from the viewport', () => {
|
|
87
|
+
const { container } = render(<TestVideo autoPlay />);
|
|
88
|
+
const video = container.querySelector('video')!;
|
|
89
|
+
emit(NEAR, video, false);
|
|
90
|
+
emit(VISIBLE, video, false);
|
|
91
|
+
expect(video.preload).toBe('none');
|
|
92
|
+
expect(play).not.toHaveBeenCalled();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('starts loading once the video is within a viewport of the screen', () => {
|
|
96
|
+
const { container } = render(<TestVideo autoPlay />);
|
|
97
|
+
const video = container.querySelector('video')!;
|
|
98
|
+
emit(NEAR, video, true);
|
|
99
|
+
expect(video.preload).toBe('auto');
|
|
100
|
+
expect(play).not.toHaveBeenCalled();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('plays an autoplay video while it is visible and pauses it when it leaves', () => {
|
|
104
|
+
const { container } = render(<TestVideo autoPlay />);
|
|
105
|
+
const video = container.querySelector('video')!;
|
|
106
|
+
emit(NEAR, video, true);
|
|
107
|
+
emit(VISIBLE, video, true);
|
|
108
|
+
expect(play).toHaveBeenCalledTimes(1);
|
|
109
|
+
emit(VISIBLE, video, false);
|
|
110
|
+
expect(pause).toHaveBeenCalledTimes(1);
|
|
111
|
+
emit(VISIBLE, video, true);
|
|
112
|
+
expect(play).toHaveBeenCalledTimes(2);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('keeps a video the viewer paused paused until they play it again', () => {
|
|
116
|
+
const { container } = render(<TestVideo autoPlay />);
|
|
117
|
+
const video = container.querySelector('video')!;
|
|
118
|
+
emit(NEAR, video, true);
|
|
119
|
+
emit(VISIBLE, video, true);
|
|
120
|
+
act(() => video.pause());
|
|
121
|
+
emit(VISIBLE, video, false);
|
|
122
|
+
emit(VISIBLE, video, true);
|
|
123
|
+
expect(play).toHaveBeenCalledTimes(1);
|
|
124
|
+
|
|
125
|
+
act(() => {
|
|
126
|
+
void video.play();
|
|
127
|
+
});
|
|
128
|
+
emit(VISIBLE, video, false);
|
|
129
|
+
emit(VISIBLE, video, true);
|
|
130
|
+
expect(play).toHaveBeenCalledTimes(3);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('resumes a video the browser paused while the page was hidden', () => {
|
|
134
|
+
const { container } = render(<TestVideo autoPlay />);
|
|
135
|
+
const video = container.querySelector('video')!;
|
|
136
|
+
emit(NEAR, video, true);
|
|
137
|
+
emit(VISIBLE, video, true);
|
|
138
|
+
const hidden = jest.spyOn(document, 'hidden', 'get').mockReturnValue(true);
|
|
139
|
+
act(() => video.pause());
|
|
140
|
+
hidden.mockRestore();
|
|
141
|
+
emit(VISIBLE, video, false);
|
|
142
|
+
emit(VISIBLE, video, true);
|
|
143
|
+
expect(play).toHaveBeenCalledTimes(2);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('only preloads a video that does not autoplay', () => {
|
|
147
|
+
const { container } = render(<TestVideo autoPlay={false} />);
|
|
148
|
+
const video = container.querySelector('video')!;
|
|
149
|
+
emit(NEAR, video, true);
|
|
150
|
+
expect(video.preload).toBe('auto');
|
|
151
|
+
expect(isObserved(VISIBLE, video)).toBe(false);
|
|
152
|
+
expect(play).not.toHaveBeenCalled();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('stops observing on unmount', () => {
|
|
156
|
+
const { container, unmount } = render(<TestVideo autoPlay />);
|
|
157
|
+
const video = container.querySelector('video')!;
|
|
158
|
+
unmount();
|
|
159
|
+
expect(isObserved(NEAR, video)).toBe(false);
|
|
160
|
+
expect(isObserved(VISIBLE, video)).toBe(false);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('shares one observer per margin across videos', () => {
|
|
164
|
+
const { container } = render(
|
|
165
|
+
<>
|
|
166
|
+
<TestVideo autoPlay />
|
|
167
|
+
<TestVideo autoPlay />
|
|
168
|
+
</>
|
|
169
|
+
);
|
|
170
|
+
const videos = Array.from(container.querySelectorAll('video'));
|
|
171
|
+
const nearObservers = FakeIntersectionObserver.instances.filter(instance => instance.options.rootMargin === NEAR);
|
|
172
|
+
expect(nearObservers).toHaveLength(1);
|
|
173
|
+
expect(nearObservers[0].targets).toEqual(new Set(videos));
|
|
174
|
+
emit(NEAR, videos[0], true);
|
|
175
|
+
expect(videos.map(video => video.preload)).toEqual(['auto', 'none']);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('falls back to preload and autoplay without IntersectionObserver', () => {
|
|
179
|
+
window.IntersectionObserver = undefined as unknown as typeof IntersectionObserver;
|
|
180
|
+
const { container } = render(<TestVideo autoPlay />);
|
|
181
|
+
const video = container.querySelector('video')!;
|
|
182
|
+
expect(video.preload).toBe('auto');
|
|
183
|
+
expect(video.autoplay).toBe(true);
|
|
184
|
+
expect(play).not.toHaveBeenCalled();
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
describe('SectionVideo', () => {
|
|
189
|
+
const media = {
|
|
190
|
+
url: 'https://cdn.cntrl.site/video.mp4',
|
|
191
|
+
size: 'cover',
|
|
192
|
+
type: 'video' as const,
|
|
193
|
+
play: 'auto' as const,
|
|
194
|
+
position: 'local',
|
|
195
|
+
coverUrl: null,
|
|
196
|
+
offsetX: null
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
it('renders the background video idle and plays it once it is on screen', () => {
|
|
200
|
+
const originalIntersectionObserver = window.IntersectionObserver;
|
|
201
|
+
window.IntersectionObserver = FakeIntersectionObserver as unknown as typeof IntersectionObserver;
|
|
202
|
+
const playSpy = jest.spyOn(HTMLMediaElement.prototype, 'play').mockResolvedValue(undefined);
|
|
203
|
+
const { container } = render(<SectionVideo container={document.createElement('div')} sectionId="s" media={media} />);
|
|
204
|
+
const video = container.querySelector('video')!;
|
|
205
|
+
expect(video.hasAttribute('autoplay')).toBe(false);
|
|
206
|
+
expect(video.getAttribute('preload')).toBe('none');
|
|
207
|
+
expect(playSpy).not.toHaveBeenCalled();
|
|
208
|
+
emit(NEAR, video, true);
|
|
209
|
+
emit(VISIBLE, video, true);
|
|
210
|
+
expect(video.preload).toBe('auto');
|
|
211
|
+
expect(playSpy).toHaveBeenCalled();
|
|
212
|
+
playSpy.mockRestore();
|
|
213
|
+
window.IntersectionObserver = originalIntersectionObserver;
|
|
214
|
+
});
|
|
215
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
const NEAR_VIEWPORT_MARGIN = '100% 0px';
|
|
4
|
+
|
|
5
|
+
type IntersectionListener = (isIntersecting: boolean) => void;
|
|
6
|
+
|
|
7
|
+
type SharedObserver = {
|
|
8
|
+
observer: IntersectionObserver;
|
|
9
|
+
listeners: Map<Element, Set<IntersectionListener>>;
|
|
10
|
+
states: Map<Element, boolean>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const sharedObservers = new Map<string, SharedObserver>();
|
|
14
|
+
|
|
15
|
+
function getSharedObserver(rootMargin: string): SharedObserver {
|
|
16
|
+
const existing = sharedObservers.get(rootMargin);
|
|
17
|
+
if (existing) return existing;
|
|
18
|
+
const listeners = new Map<Element, Set<IntersectionListener>>();
|
|
19
|
+
const states = new Map<Element, boolean>();
|
|
20
|
+
const observer = new IntersectionObserver((entries) => {
|
|
21
|
+
for (const entry of entries) {
|
|
22
|
+
states.set(entry.target, entry.isIntersecting);
|
|
23
|
+
listeners.get(entry.target)?.forEach(listener => listener(entry.isIntersecting));
|
|
24
|
+
}
|
|
25
|
+
}, { rootMargin });
|
|
26
|
+
const shared = { observer, listeners, states };
|
|
27
|
+
sharedObservers.set(rootMargin, shared);
|
|
28
|
+
return shared;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function observeIntersection(element: Element, rootMargin: string, listener: IntersectionListener): () => void {
|
|
32
|
+
const { observer, listeners, states } = getSharedObserver(rootMargin);
|
|
33
|
+
const elementListeners = listeners.get(element) ?? new Set<IntersectionListener>();
|
|
34
|
+
if (elementListeners.size === 0) {
|
|
35
|
+
listeners.set(element, elementListeners);
|
|
36
|
+
observer.observe(element);
|
|
37
|
+
}
|
|
38
|
+
elementListeners.add(listener);
|
|
39
|
+
const knownState = states.get(element);
|
|
40
|
+
if (knownState !== undefined) listener(knownState);
|
|
41
|
+
return () => {
|
|
42
|
+
elementListeners.delete(listener);
|
|
43
|
+
if (elementListeners.size > 0) return;
|
|
44
|
+
listeners.delete(element);
|
|
45
|
+
states.delete(element);
|
|
46
|
+
observer.unobserve(element);
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function loadVideo(video: HTMLVideoElement) {
|
|
51
|
+
if (video.preload !== 'auto') video.preload = 'auto';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function playVideo(video: HTMLVideoElement) {
|
|
55
|
+
const playPromise = video.play();
|
|
56
|
+
if (playPromise) playPromise.catch(() => {});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function useLazyVideo(video: HTMLVideoElement | null, autoPlay: boolean) {
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (!video) return;
|
|
62
|
+
if (typeof IntersectionObserver === 'undefined') {
|
|
63
|
+
loadVideo(video);
|
|
64
|
+
video.autoplay = autoPlay;
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const stopNear = observeIntersection(video, NEAR_VIEWPORT_MARGIN, (isNear) => {
|
|
68
|
+
if (isNear) loadVideo(video);
|
|
69
|
+
});
|
|
70
|
+
if (!autoPlay) return stopNear;
|
|
71
|
+
let isPausedByUser = false;
|
|
72
|
+
let isPausingOffscreen = false;
|
|
73
|
+
const handlePlay = () => {
|
|
74
|
+
isPausedByUser = false;
|
|
75
|
+
};
|
|
76
|
+
const handlePause = () => {
|
|
77
|
+
if (isPausingOffscreen) {
|
|
78
|
+
isPausingOffscreen = false;
|
|
79
|
+
} else if (!document.hidden) {
|
|
80
|
+
isPausedByUser = true;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
video.addEventListener('play', handlePlay);
|
|
84
|
+
video.addEventListener('pause', handlePause);
|
|
85
|
+
const stopVisible = observeIntersection(video, '0px', (isVisible) => {
|
|
86
|
+
if (isVisible) {
|
|
87
|
+
if (video.paused && !isPausedByUser) playVideo(video);
|
|
88
|
+
} else if (!video.paused) {
|
|
89
|
+
isPausingOffscreen = true;
|
|
90
|
+
video.pause();
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
return () => {
|
|
94
|
+
stopNear();
|
|
95
|
+
stopVisible();
|
|
96
|
+
video.removeEventListener('play', handlePlay);
|
|
97
|
+
video.removeEventListener('pause', handlePause);
|
|
98
|
+
};
|
|
99
|
+
}, [video, autoPlay]);
|
|
100
|
+
}
|
package/src/components/Head.tsx
CHANGED
|
@@ -21,7 +21,7 @@ export const CNTRLHead: FC<Props> = ({ meta, project, slug, siteUrl, fontsVault
|
|
|
21
21
|
};
|
|
22
22
|
const customFonts = project.fonts.custom;
|
|
23
23
|
const htmlHead = HTMLReactParser(project.html.head);
|
|
24
|
-
const ffGenerator = new FontFaceGenerator([...
|
|
24
|
+
const ffGenerator = new FontFaceGenerator([...customFonts, ...fontsVault]);
|
|
25
25
|
const links = Object.values(parsedFonts as ReturnType<typeof domToReact>).map((value) => {
|
|
26
26
|
if (!value) return null;
|
|
27
27
|
const rel = value?.rel || value.props?.rel;
|
|
@@ -48,12 +48,12 @@ export const CNTRLHead: FC<Props> = ({ meta, project, slug, siteUrl, fontsVault
|
|
|
48
48
|
<meta name="twitter:description" content={meta.description} />
|
|
49
49
|
<meta name="generator" content="https://cntrl.site" />
|
|
50
50
|
<link rel="icon" href={meta.favicon} />
|
|
51
|
+
{links}
|
|
51
52
|
<style
|
|
52
53
|
dangerouslySetInnerHTML={{
|
|
53
54
|
__html: ffGenerator.generate()
|
|
54
55
|
}}
|
|
55
56
|
/>
|
|
56
|
-
{links}
|
|
57
57
|
{htmlHead}
|
|
58
58
|
|
|
59
59
|
</Head>
|
|
@@ -4,6 +4,7 @@ import { FC, useId } from 'react';
|
|
|
4
4
|
import { useCntrlContext } from '../../provider/useCntrlContext';
|
|
5
5
|
import { useLayoutContext } from '../useLayoutContext';
|
|
6
6
|
import { mergeComponentSettings } from '../../utils/mergeComponentSettings';
|
|
7
|
+
import { pendingElementProps, useComponentElement } from '../../common/useComponentElement';
|
|
7
8
|
|
|
8
9
|
interface Props {
|
|
9
10
|
component: TNavigationComponent;
|
|
@@ -24,8 +25,8 @@ export const NavigationComponent: FC<Props> = ({
|
|
|
24
25
|
const fallbackLayout = layouts[0]?.id;
|
|
25
26
|
const effectiveLayout = layout ?? fallbackLayout;
|
|
26
27
|
const layoutValues: Record<string, any>[] = [block.layoutParams];
|
|
27
|
-
const
|
|
28
|
-
const
|
|
28
|
+
const { Element, isPending, isFailed } = useComponentElement(block.commonParams.componentId);
|
|
29
|
+
const keepsServerMarkup = isPending || isFailed;
|
|
29
30
|
const layoutParameters = effectiveLayout ? block.layoutParams[effectiveLayout]?.parameters : undefined;
|
|
30
31
|
const commonParameters = block.commonParams.parameters;
|
|
31
32
|
const parameters = layoutParameters ? {
|
|
@@ -38,10 +39,12 @@ export const NavigationComponent: FC<Props> = ({
|
|
|
38
39
|
return (
|
|
39
40
|
<>
|
|
40
41
|
<div
|
|
42
|
+
key={keepsServerMarkup ? 'pending' : 'ready'}
|
|
41
43
|
className={`custom-component-${block.id}`}
|
|
42
|
-
style={{ opacity: layout == null ? 0 : undefined }}
|
|
44
|
+
style={{ opacity: layout == null || isPending ? 0 : undefined }}
|
|
45
|
+
{...(keepsServerMarkup ? pendingElementProps : {})}
|
|
43
46
|
>
|
|
44
|
-
{parameters && Element && (
|
|
47
|
+
{keepsServerMarkup ? null : parameters && Element && (
|
|
45
48
|
<Element
|
|
46
49
|
metadata={{
|
|
47
50
|
itemId: block.id,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CSSProperties, FC, useEffect, useId, useRef, useState } from 'react';
|
|
2
2
|
import JSXStyle from 'styled-jsx/style';
|
|
3
|
+
import { useLazyVideo } from '../../common/useLazyVideo';
|
|
3
4
|
|
|
4
5
|
export type TSectionVideo = {
|
|
5
6
|
url: string;
|
|
@@ -26,6 +27,7 @@ export const SectionVideo: FC<Props> = ({ container, sectionId, media }) => {
|
|
|
26
27
|
const [isPlaying, setIsPlaying] = useState(false);
|
|
27
28
|
const [userPaused, setUserPaused] = useState(false);
|
|
28
29
|
const [isClickedOnCover, setIsClickedOnCover] = useState(false);
|
|
30
|
+
useLazyVideo(video, play === 'auto');
|
|
29
31
|
|
|
30
32
|
const handleCoverClick = () => {
|
|
31
33
|
if (!video || play !== 'on-click') return;
|
|
@@ -87,7 +89,6 @@ export const SectionVideo: FC<Props> = ({ container, sectionId, media }) => {
|
|
|
87
89
|
>
|
|
88
90
|
<video
|
|
89
91
|
ref={setVideo}
|
|
90
|
-
autoPlay={play === 'auto'}
|
|
91
92
|
loop
|
|
92
93
|
style={{
|
|
93
94
|
opacity: !isClickedOnCover && play === 'on-click' && coverUrl ? 0 : opacity,
|
|
@@ -101,7 +102,7 @@ export const SectionVideo: FC<Props> = ({ container, sectionId, media }) => {
|
|
|
101
102
|
controls={play === 'on-click'}
|
|
102
103
|
muted={play === 'auto'}
|
|
103
104
|
playsInline
|
|
104
|
-
preload="
|
|
105
|
+
preload="none"
|
|
105
106
|
className={`video-background-${sectionId}`}
|
|
106
107
|
onPlay={() => setIsPlaying(true)}
|
|
107
108
|
onPause={() => setIsPlaying(false)}
|
|
@@ -3,7 +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 { blockPlacement } from './blockPlacement';
|
|
6
|
-
import { blocksMap } from './blocksMap';
|
|
6
|
+
import { blocksMap, UnknownBlock } from './blocksMap';
|
|
7
7
|
|
|
8
8
|
export interface StructuredBlockItemProps<I extends StructuredBlockAny> {
|
|
9
9
|
block: I;
|
|
@@ -14,14 +14,16 @@ export interface StructuredBlockItemProps<I extends StructuredBlockAny> {
|
|
|
14
14
|
interface Props {
|
|
15
15
|
block: StructuredBlockAny;
|
|
16
16
|
readingWidth: Record<string, number>;
|
|
17
|
+
/** Whether the block keeps the gap above it its area holds: a default section's blocks do, a content-based section's never. */
|
|
18
|
+
hasGap: boolean;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
|
-
export const StructuredBlockItem: FC<Props> = ({ block, readingWidth }) => {
|
|
21
|
+
export const StructuredBlockItem: FC<Props> = ({ block, readingWidth, hasGap }) => {
|
|
20
22
|
const reactId = useId();
|
|
21
23
|
const id = `${reactId}-item-${block.id}`;
|
|
22
24
|
const { layouts } = useCntrlContext();
|
|
23
25
|
const layoutValues: Record<string, any>[] = [block.area, block.layoutParams, block.hidden ?? {}];
|
|
24
|
-
const BlockComponent = blocksMap[block.type];
|
|
26
|
+
const BlockComponent = blocksMap[block.type] ?? UnknownBlock;
|
|
25
27
|
return (
|
|
26
28
|
<div className={`structured-block-item-${block.id}`}>
|
|
27
29
|
<BlockComponent block={block} readingWidth={readingWidth} />
|
|
@@ -31,7 +33,7 @@ export const StructuredBlockItem: FC<Props> = ({ block, readingWidth }) => {
|
|
|
31
33
|
display: ${hidden ? 'none' : 'block'};
|
|
32
34
|
position: relative;
|
|
33
35
|
z-index: ${area.zIndex};
|
|
34
|
-
margin-top: ${area.paddingTop ? `${area.paddingTop * 100}vw` : 0};
|
|
36
|
+
margin-top: ${hasGap && area.paddingTop ? `${area.paddingTop * 100}vw` : 0};
|
|
35
37
|
outline: none;
|
|
36
38
|
${blockPlacement(area, layoutParams)}
|
|
37
39
|
}
|
|
@@ -14,13 +14,17 @@ const DEFAULT_COLORS = { background: '#FFFFFF', foreground: '#000000' };
|
|
|
14
14
|
const FONT_STYLE_ITALIC = 1;
|
|
15
15
|
const FONT_STYLE_BOLD = 2;
|
|
16
16
|
const FONT_STYLE_UNDERLINE = 4;
|
|
17
|
+
const FONT_STYLE_STRIKETHROUGH = 8;
|
|
17
18
|
|
|
18
19
|
function tokenStyle({ color, fontStyle = 0 }: CodeToken): CSSProperties {
|
|
19
20
|
return {
|
|
20
21
|
color,
|
|
21
22
|
fontStyle: fontStyle & FONT_STYLE_ITALIC ? 'italic' : undefined,
|
|
22
23
|
fontWeight: fontStyle & FONT_STYLE_BOLD ? 'bold' : undefined,
|
|
23
|
-
textDecoration:
|
|
24
|
+
textDecoration: [
|
|
25
|
+
fontStyle & FONT_STYLE_UNDERLINE ? 'underline' : '',
|
|
26
|
+
fontStyle & FONT_STYLE_STRIKETHROUGH ? 'line-through' : ''
|
|
27
|
+
].filter(Boolean).join(' ') || undefined
|
|
24
28
|
};
|
|
25
29
|
}
|
|
26
30
|
|
|
@@ -5,6 +5,7 @@ import { useCntrlContext } from '../../../provider/useCntrlContext';
|
|
|
5
5
|
import { useLayoutContext } from '../../useLayoutContext';
|
|
6
6
|
import { useItemGeometry } from '../../../ItemGeometry/useItemGeometry';
|
|
7
7
|
import { mergeComponentSettings } from '../../../utils/mergeComponentSettings';
|
|
8
|
+
import { pendingElementProps, useComponentElement } from '../../../common/useComponentElement';
|
|
8
9
|
|
|
9
10
|
interface Props {
|
|
10
11
|
block: StructuredBlock<StructuredBlockType.Component>;
|
|
@@ -17,10 +18,10 @@ export const StructuredComponent: FC<Props> = ({ block }) => {
|
|
|
17
18
|
const fallbackLayout = layouts[0]?.id;
|
|
18
19
|
const effectiveLayout = layout ?? fallbackLayout;
|
|
19
20
|
const layoutValues: Record<string, any>[] = [block.layoutParams];
|
|
20
|
-
const
|
|
21
|
+
const { Element, isPending, isFailed } = useComponentElement(block.commonParams.componentId);
|
|
22
|
+
const keepsServerMarkup = isPending || isFailed;
|
|
21
23
|
const [ref, setRef] = useState<HTMLDivElement | null>(null);
|
|
22
24
|
useItemGeometry(block.id, ref);
|
|
23
|
-
const Element = component ? component.element : undefined;
|
|
24
25
|
const layoutParameters = effectiveLayout ? block.layoutParams[effectiveLayout]?.parameters : undefined;
|
|
25
26
|
const commonParameters = block.commonParams.parameters;
|
|
26
27
|
const parameters = layoutParameters ? {
|
|
@@ -31,12 +32,14 @@ export const StructuredComponent: FC<Props> = ({ block }) => {
|
|
|
31
32
|
return (
|
|
32
33
|
<>
|
|
33
34
|
<div
|
|
35
|
+
key={keepsServerMarkup ? 'pending' : 'ready'}
|
|
34
36
|
className={`custom-component-${block.id}`}
|
|
35
37
|
ref={setRef}
|
|
36
38
|
// preventing layout shift while supporting SSG for proper SEO
|
|
37
|
-
style={{ opacity: layout == null ? 0 : undefined }}
|
|
39
|
+
style={{ opacity: layout == null || isPending ? 0 : undefined }}
|
|
40
|
+
{...(keepsServerMarkup ? pendingElementProps : {})}
|
|
38
41
|
>
|
|
39
|
-
{parameters && Element && (
|
|
42
|
+
{keepsServerMarkup ? null : parameters && Element && (
|
|
40
43
|
<Element
|
|
41
44
|
metadata={{
|
|
42
45
|
itemId: block.id,
|
|
@@ -10,6 +10,15 @@ import { useLayoutContext } from '../../useLayoutContext';
|
|
|
10
10
|
import { StructuredBlockItemProps } from '../StructuredBlockItem';
|
|
11
11
|
import { embedFrameStyles } from './embedFrameStyles';
|
|
12
12
|
|
|
13
|
+
// a link the id cannot be read from -- empty, or a host not whitelisted -- gives no player, not a failed page
|
|
14
|
+
function youtubeIdOf(url: string): string | undefined {
|
|
15
|
+
try {
|
|
16
|
+
return getYoutubeId(new URL(url));
|
|
17
|
+
} catch {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
13
22
|
/** A YouTube video playing as the YouTube item does, its cover over it until it plays. */
|
|
14
23
|
export const StructuredYoutubeEmbed: FC<StructuredBlockItemProps<YoutubeEmbedStructuredBlock>> = ({ block }) => {
|
|
15
24
|
const reactId = useId();
|
|
@@ -26,12 +35,14 @@ export const StructuredYoutubeEmbed: FC<StructuredBlockItemProps<YoutubeEmbedStr
|
|
|
26
35
|
|
|
27
36
|
useEffect(() => {
|
|
28
37
|
if (!YT || !frame || !layoutParams) return;
|
|
38
|
+
const videoId = youtubeIdOf(url);
|
|
39
|
+
if (videoId === undefined) return;
|
|
29
40
|
const { play, controls } = layoutParams;
|
|
30
41
|
setIsCoverVisible(play !== 'auto' && coverUrl !== null);
|
|
31
42
|
const placeholder = document.createElement('div');
|
|
32
43
|
frame.appendChild(placeholder);
|
|
33
44
|
const created = new YT.Player(placeholder, {
|
|
34
|
-
videoId
|
|
45
|
+
videoId,
|
|
35
46
|
playerVars: {
|
|
36
47
|
autoplay: play === 'auto' ? '1' : '0',
|
|
37
48
|
controls: controls ? '1' : '0'
|
|
@@ -18,7 +18,9 @@ export const StructuredImage: FC<StructuredBlockItemProps<ImageStructuredBlock>>
|
|
|
18
18
|
const { layouts } = useCntrlContext();
|
|
19
19
|
const [ref, setRef] = useState<HTMLDivElement | null>(null);
|
|
20
20
|
useItemGeometry(block.id, ref);
|
|
21
|
-
const {
|
|
21
|
+
const { altText } = block.commonParams;
|
|
22
|
+
// a file the API could not resolve comes as an empty url: nothing to draw for it
|
|
23
|
+
const urls = block.commonParams.urls.filter(url => url !== '');
|
|
22
24
|
const caption = getShownCaption(block.commonParams);
|
|
23
25
|
const [ratios, setRatios] = useState<Record<string, number>>({});
|
|
24
26
|
// an image already loaded when the page hydrates fires no load event of its own
|
|
@@ -3,7 +3,7 @@ import { FC, useId, useMemo, useState } from 'react';
|
|
|
3
3
|
import JSXStyle from 'styled-jsx/style';
|
|
4
4
|
import { useCntrlContext } from '../../../provider/useCntrlContext';
|
|
5
5
|
import { useItemGeometry } from '../../../ItemGeometry/useItemGeometry';
|
|
6
|
-
import { RichTextConverter } from '../../../utils/RichTextConverter/RichTextConverter';
|
|
6
|
+
import { LIST_STYLES_ID, RichTextConverter } from '../../../utils/RichTextConverter/RichTextConverter';
|
|
7
7
|
import { detectLang } from '../../../utils/detectLang';
|
|
8
8
|
import { getHeadingTag } from '../../../utils/getHeadingTag';
|
|
9
9
|
import { StructuredBlockItemProps } from '../StructuredBlockItem';
|
|
@@ -23,7 +23,7 @@ export const StructuredRichText: FC<StructuredBlockItemProps<TextBlock>> = ({ bl
|
|
|
23
23
|
const { layouts } = useCntrlContext();
|
|
24
24
|
const [ref, setRef] = useState<HTMLDivElement | null>(null);
|
|
25
25
|
useItemGeometry(block.id, ref);
|
|
26
|
-
const [content, rangeStyles] = useMemo(
|
|
26
|
+
const [content, rangeStyles, listStyles] = useMemo(
|
|
27
27
|
() => richTextConverter.toHtml(block, layouts, { isScaledToViewport: false }),
|
|
28
28
|
[block, layouts]
|
|
29
29
|
);
|
|
@@ -38,6 +38,7 @@ export const StructuredRichText: FC<StructuredBlockItemProps<TextBlock>> = ({ bl
|
|
|
38
38
|
<Wrapper ref={setRef} lang={lang} className={`structured-text-${block.id}`}>
|
|
39
39
|
{isQuote ? <div className={`structured-quote-box-${block.id}`}>{content}</div> : content}
|
|
40
40
|
</Wrapper>
|
|
41
|
+
{listStyles && <JSXStyle id={LIST_STYLES_ID}>{listStyles}</JSXStyle>}
|
|
41
42
|
<JSXStyle id={id}>
|
|
42
43
|
{rangeStyles}
|
|
43
44
|
{`
|
|
@@ -50,7 +51,7 @@ export const StructuredRichText: FC<StructuredBlockItemProps<TextBlock>> = ({ bl
|
|
|
50
51
|
${isText ? textColumnsRule(`.structured-text-${block.id}`, layoutParams) : ''}
|
|
51
52
|
${isQuote ? `
|
|
52
53
|
.structured-quote-box-${block.id} {
|
|
53
|
-
padding: ${
|
|
54
|
+
padding: ${[layoutParams.boxPaddingTop, layoutParams.boxPaddingRight, layoutParams.boxPaddingBottom, layoutParams.boxPaddingLeft].map(side => `${(side ?? 0) * 100}vw`).join(' ')};
|
|
54
55
|
border-radius: ${(layoutParams.boxRadius ?? 0) * 100}vw;
|
|
55
56
|
background-color: transparent;
|
|
56
57
|
}
|