-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathInteractiveBlockComponent.importable.tsx
More file actions
485 lines (438 loc) · 12.7 KB
/
InteractiveBlockComponent.importable.tsx
File metadata and controls
485 lines (438 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import { css } from '@emotion/react';
import type { SerializedStyles } from '@emotion/react';
import { getErrorMessage, isUndefined, log } from '@guardian/libs';
import {
article17,
from,
space,
textSans17,
} from '@guardian/source/foundations';
import libDebounce from 'lodash.debounce';
import { useRef, useState } from 'react';
import { interactiveLegacyFigureClasses } from '../layouts/lib/interactiveLegacyStyling';
import { type ArticleFormat, ArticleSpecial } from '../lib/articleFormat';
import { useOnce } from '../lib/useOnce';
import { palette as themePalette } from '../palette';
import type { RoleType } from '../types/content';
import { Caption } from './Caption';
import { useConfig } from './ConfigContext';
import { defaultRoleStyles } from './Figure';
import { Placeholder } from './Placeholder';
type Props = {
url?: string;
scriptUrl?: string;
alt?: string;
role?: RoleType;
caption?: string;
format: ArticleFormat;
elementId?: string;
isMainMedia: boolean;
};
/*
HISTORY
A CAPI response for an interactive elements looks like:
{
"type": "interactive",
"assets": [],
"interactiveTypeData": {
"originalUrl": "https://interactive.guim.co.uk/charts/embed/jan/2019-01-05T14:22:01/embed.html",
"source": "Guardian",
"alt": "china 1978 gdp",
"scriptUrl": "https://interactive.guim.co.uk/embed/iframe-wrapper/0.1/boot.js",
"html": "<a href=\"https://interactive.guim.co.uk/charts/embed/jan/2019-01-05T14:22:01/embed.html\">china 1978 gdp</a>",
"scriptName": "iframe-wrapper",
"iframeUrl": "https://interactive.guim.co.uk/charts/embed/jan/2019-01-05T14:22:01/embed.html",
"role": "supporting",
"isMandatory": false
}
}
On Frontend, the HTML that we send to the browser is:
<figure class="element element-interactive interactive element--supporting" data-interactive="https://interactive.guim.co.uk/embed/iframe-wrapper/0.1/boot.js" data-canonical-url="https://interactive.guim.co.uk/charts/embed/jan/2019-01-05T14:22:01/embed.html" data-alt="china 1978 gdp">
<a href="https://interactive.guim.co.uk/charts/embed/jan/2019-01-05T14:22:01/embed.html" data-link-name="in body link" class="u-underline">china 1978 gdp</a>
</figure>
https://github.com/guardian/frontend/blob/e38da96c59eaaef143b9f89b2318eab9dcf2fc5a/common/app/views/fragments/inlineJSNonBlocking.scala.html#L52
We use the curl AMD loader to load the boot script from the data-interactive attribute:
require([el.getAttribute('data-interactive')], function (interactive) {
interactive.boot(el, document, window.guardian.config);
}
Pulling from Kibana, 99% of articles use the standard iframe-wrapper/1.0/boot.js
A vast minority of pre interactive atom use a customised boot.js
https://gist.github.com/gtrufitt/c8f08caef0ae810a42fde5a4c0549ad0
THE STANDARD BOOT.js
boot.js is defined from https://github.com/guardian/interactive-boot-scripts/blob/master/iframe-wrapper/boot.js
and is sent with all interactive elements in the scriptUrl from CAPI to do a two simple things:
- Create an iframe using the href of the anchor and set the src
- Add event listener to the window to listen for 'message' of the following types
- set-height
- navigate
- scroll-to
- get-location
- get-position
- monitor-position
It has not been updated since 2016.
MIGRATION FROM FRONTEND
- For the standard boot.js, we have re-written the behavior in modern JS to avoid the requirement of an AMD loader
and to avoid loading the boot.js file.
- For all other files that do not load the standard boot.js, we'll add a AMD loader to the page
For the remaining few we dynamically load and AMD loader and support the contract as defined with curl AMD loader
*/
const decideHeight = (role: RoleType) => {
switch (role) {
case 'supporting':
return 200;
default:
return 500;
}
};
const getMinHeight = (role: RoleType, loaded: boolean) => {
if (loaded) {
return `auto`;
}
return `${decideHeight(role)}px`;
};
const wrapperStyle = ({
format,
role,
loaded,
isDatawrapperGraphic,
}: {
format: ArticleFormat;
role: RoleType;
loaded: boolean;
isDatawrapperGraphic: boolean;
}) => css`
${format.theme === ArticleSpecial.Labs ? textSans17 : article17}
background-color: ${themePalette(
isDatawrapperGraphic
? '--interactive-block-background-datawrapper'
: '--interactive-block-background',
)};
min-height: ${getMinHeight(role, loaded)};
position: relative;
display: flex;
flex-direction: column;
${isDatawrapperGraphic
? `
border-top: 1px solid ${themePalette('--branding-border')};
padding-top: ${space[2]}px;
`
: ''}
`;
const datawrapperRoleStyles = (role: RoleType): SerializedStyles | null => {
switch (role) {
case 'inline':
return datawrapperRoleCss.inline;
case 'supporting':
return datawrapperRoleCss.supporting;
case 'immersive':
return datawrapperRoleCss.immersive;
case 'showcase':
return datawrapperRoleCss.showcase;
case 'thumbnail':
case 'halfWidth':
return null;
}
};
const datawrapperRoleCss = {
inline: css`
margin-top: ${space[6]}px;
margin-bottom: ${space[6]}px;
`,
supporting: css`
clear: left;
margin-top: ${space[6]}px;
margin-bottom: ${space[6]}px;
${from.leftCol} {
margin-bottom: ${space[2]}px;
padding-bottom: ${space[4]}px;
border-bottom: 1px solid ${themePalette('--branding-border')};
}
${from.wide} {
width: 380px;
margin-left: -240px;
}
`,
immersive: css`
margin-top: ${space[6]}px;
margin-bottom: ${space[6]}px;
${from.leftCol} {
margin-bottom: ${space[4]}px;
padding-bottom: ${space[4]}px;
border-bottom: 1px solid ${themePalette('--branding-border')};
}
`,
showcase: css`
margin-top: ${space[6]}px;
margin-bottom: ${space[6]}px;
${from.leftCol} {
margin-bottom: ${space[4]}px;
padding-bottom: ${space[4]}px;
border-bottom: 1px solid ${themePalette('--branding-border')};
}
`,
};
const placeholderLinkStyle = css`
position: absolute;
bottom: ${space[1]}px;
left: ${space[1]}px;
`;
const mainMediaFigureStyles = css`
height: 100%;
`;
// https://interactive.guim.co.uk/embed/iframe-wrapper/0.1/boot.js
const setupWindowListeners = (iframe: HTMLIFrameElement) => {
// Calls func on trailing edge of the wait period
const postMessage = (message: Record<string, unknown>) => {
if (iframe.contentWindow) {
iframe.contentWindow.postMessage(JSON.stringify(message), '*');
}
};
window.addEventListener(
'message',
(event) => {
if (event.source !== iframe.contentWindow) {
return;
}
let message: Record<string, unknown> | undefined;
try {
message = JSON.parse(event.data);
} catch (e) {
log(
'dotcom',
getErrorMessage(e),
'Json parse Failed on in interactiveBlockComponent',
);
}
if (isUndefined(message)) {
return;
}
const postPositionMessage = (subscribe?: boolean) => {
const iframeBox = iframe.getBoundingClientRect();
postMessage({
id: message?.id || '',
type: message?.type || '',
subscribe: !!subscribe,
iframeTop: iframeBox.top,
iframeRight: iframeBox.right,
iframeBottom: iframeBox.bottom,
iframeLeft: iframeBox.left,
innerHeight: window.innerHeight,
innerWidth: window.innerWidth,
pageYOffset: window.pageYOffset,
});
};
// Actions
switch (message.type) {
case 'set-height':
if (typeof message.value === 'number') {
iframe.height = message.value.toString();
}
break;
case 'navigate':
if (typeof message.value === 'string') {
document.location.href = message.value;
}
break;
case 'scroll-to':
if (
typeof message.x === 'number' &&
typeof message.y === 'number'
) {
window.scrollTo(message.x, message.y);
}
break;
case 'get-location':
postMessage({
id: message.id,
type: message.type,
hash: window.location.hash,
host: window.location.host,
hostname: window.location.hostname,
href: window.location.href,
origin: window.location.origin,
pathname: window.location.pathname,
port: window.location.port,
protocol: window.location.protocol,
search: window.location.search,
});
break;
case 'get-position':
postPositionMessage();
break;
case 'monitor-position':
// Send initial position
postPositionMessage(true);
// Send updated position on scroll or resize
window.addEventListener(
'scroll',
libDebounce(() => {
postPositionMessage(true);
}, 50),
);
window.addEventListener(
'resize',
libDebounce(() => {
postPositionMessage(true);
}, 50),
);
break;
}
},
false,
);
};
export const InteractiveBlockComponent = ({
url,
scriptUrl,
alt,
role = 'inline',
caption,
format,
elementId = '',
isMainMedia,
}: Props) => {
const wrapperRef = useRef<HTMLDivElement>(null);
const placeholderLinkRef = useRef<HTMLAnchorElement>(null);
const [loaded, setLoaded] = useState(false);
const { darkModeAvailable, renderingTarget } = useConfig();
// Define some one-time flags
const isDatawrapperGraphic = url?.includes(
'interactive.guim.co.uk/datawrapper',
)
? true
: false;
const isUploaderEmbedPath =
url &&
(url.includes('interactive.guim.co.uk/uploader/embed/') ||
url.includes('superyacht') ||
url.includes('choropleth_map_maker'))
? true
: false;
const scriptUrlIsBoot = scriptUrl?.includes(
'interactive.guim.co.uk/embed/iframe-wrapper/0.1/boot.js',
)
? true
: false;
useOnce(() => {
// We've brought the behavior from boot.js into this file to avoid loading 2 extra scripts
const prefersDarkScheme = window.matchMedia(
'(prefers-color-scheme: dark)',
).matches;
const requiresDarkMode =
darkModeAvailable && prefersDarkScheme ? true : false;
if (url && scriptUrlIsBoot && placeholderLinkRef.current) {
// Prepare for graphic url dynamic updates
const graphicUrl = new URL(url);
// Begin creating new iframe element
const iframe = document.createElement('iframe');
iframe.style.width = '100%';
iframe.style.border = 'none';
iframe.height = decideHeight(role).toString();
iframe.title = caption ?? alt ?? 'Interactive Content';
// Fix for Datawrapper graphic
if (isDatawrapperGraphic) {
iframe.scrolling = 'no';
}
// Fix darkmode for web environment
if (renderingTarget === 'Web' && !requiresDarkMode) {
if (isDatawrapperGraphic || isUploaderEmbedPath) {
// Add the 'dark=false' search param
if (graphicUrl.search.length) {
graphicUrl.search += '&dark=false';
} else {
// Embed URLs without a trailing slash are redirected and the
// search param is lost so we need to add it to the pathname
const hasTrailingSlash = graphicUrl.pathname.endsWith(
'/',
)
? true
: false;
graphicUrl.pathname += hasTrailingSlash ? '' : '/';
graphicUrl.search += '?dark=false';
}
}
}
// Always serve graphic over https, not http
graphicUrl.protocol = 'https:';
// Finalise new iframe element
iframe.src = graphicUrl.href;
setupWindowListeners(iframe);
wrapperRef.current?.prepend(iframe);
setLoaded(true);
} else if (scriptUrl) {
// We're going to use curl AMD loader to load the script that the
// interactive has given us.
window.require(
[scriptUrl],
(interactive: {
boot: (
el: HTMLDivElement,
document: Document,
config: unknown,
) => void;
}) => {
if (wrapperRef.current !== null) {
interactive.boot(
wrapperRef.current,
document,
window.guardian.config,
);
}
},
);
setLoaded(true);
}
}, [loaded]);
return (
<>
<figure
id={elementId} // boot scripts use id when inserting interactive content
ref={wrapperRef}
css={[
isMainMedia
? mainMediaFigureStyles
: defaultRoleStyles(role, format),
isDatawrapperGraphic ? datawrapperRoleStyles(role) : null,
wrapperStyle({
format,
role,
loaded,
isDatawrapperGraphic,
}),
]}
className={interactiveLegacyFigureClasses(
'model.dotcomrendering.pageElements.InteractiveBlockElement',
role,
)}
data-alt={alt} // for compatibility with custom boot scripts
data-testid={`interactive-element-${encodeURI(alt ?? '')}`}
data-spacefinder-role={role}
>
{!loaded && (
<>
<Placeholder // removed by HydrateInteractiveOnce
heights={new Map([['mobile', decideHeight(role)]])}
shouldShimmer={false}
/>
<a
ref={placeholderLinkRef}
data-name="placeholder" // removed by HydrateInteractiveOnce
css={placeholderLinkStyle}
href={url}
>
{alt}
</a>
</>
)}
{!!caption && (
<Caption
captionText={caption}
format={format}
isMainMedia={isMainMedia}
isImmersive={role === 'immersive'}
/>
)}
</figure>
</>
);
};