forked from TryGhost/Ghost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannouncement-bar-preview.tsx
More file actions
87 lines (75 loc) · 3.51 KB
/
Copy pathannouncement-bar-preview.tsx
File metadata and controls
87 lines (75 loc) · 3.51 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
import IframeBuffering from '../../../../utils/iframe-buffering';
import React, {useCallback, useMemo} from 'react';
const getPreviewData = (announcementBackgroundColor?: string, announcementContent?: string, visibility?: string[]) => {
const params = new URLSearchParams();
params.append('announcement_bg', announcementBackgroundColor || 'accent');
params.append('announcement', announcementContent || '');
if (visibility && visibility.length > 0) {
params.append('announcement_vis', visibility?.join(',') || '');
}
return params.toString();
};
type AnnouncementBarSettings = {
announcementBackgroundColor?: string;
announcementContent?: string;
url: string;
visibility?: string[];
};
const AnnouncementBarPreview: React.FC<AnnouncementBarSettings> = ({announcementBackgroundColor, announcementContent, url, visibility}) => {
// Avoid re-rendering iframe if an equivalent array is initialised each render
const visibilityMemo = useMemo(() => visibility, [visibility?.join(',')]); // eslint-disable-line react-hooks/exhaustive-deps
const injectContentIntoIframe = useCallback((iframe: HTMLIFrameElement) => {
if (!url) {
return;
}
const previewUrl = new URL(url);
previewUrl.searchParams.set('admin_toolbar', '0');
fetch(previewUrl.toString(), {
method: 'POST',
headers: {
'Content-Type': 'text/html;charset=utf-8',
'x-ghost-preview': getPreviewData(
announcementBackgroundColor,
announcementContent,
visibilityMemo
),
Accept: 'text/html'
},
mode: 'cors',
credentials: 'include'
})
.then(response => response.text())
.then((data) => {
// inject extra CSS to disable navigation and prevent clicks
const injectedCss = `html { pointer-events: none; }`;
const domParser = new DOMParser();
const htmlDoc = domParser.parseFromString(data, 'text/html');
const stylesheet = htmlDoc.querySelector('style') as HTMLStyleElement;
const originalCSS = stylesheet.innerHTML;
stylesheet.innerHTML = `${originalCSS}\n\n${injectedCss}`;
// replace the iframe contents with the doctored preview html
const doctype = htmlDoc.doctype ? new XMLSerializer().serializeToString(htmlDoc.doctype) : '';
let finalDoc = doctype + htmlDoc.documentElement.outerHTML;
// Send the data to the iframe's window using postMessage
// Inject the received content into the iframe
iframe.contentDocument?.open();
iframe.contentDocument?.write(finalDoc);
iframe.contentDocument?.close();
})
.catch(() => {
// handle error in fetching data
});
}, [announcementBackgroundColor, announcementContent, url, visibilityMemo]);
return (
<IframeBuffering
addDelay={true}
className="absolute size-[110%] origin-top-left scale-[.90909] bg-white max-[1600px]:size-[130%] max-[1600px]:scale-[.76923]"
generateContent={injectContentIntoIframe}
height='100%'
parentClassName="relative h-full w-full"
testId='announcement-bar-preview-iframe'
width='100%'
/>
);
};
export default AnnouncementBarPreview;