-
-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathindex.js
145 lines (125 loc) · 3.29 KB
/
index.js
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
window.CUSDIS = {}
const makeIframeContent = (target) => {
const host = target.dataset.host || 'https://cusdis.com'
const iframeJsPath = target.dataset.iframe || `${host}/js/iframe.umd.js`
const cssPath = `${host}/js/style.css`
return `<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="${cssPath}">
<base target="_parent" />
<link>
<script>
window.CUSDIS_LOCALE = ${JSON.stringify(window.CUSDIS_LOCALE)}
window.__DATA__ = ${JSON.stringify(target.dataset)}
</script>
<style>
:root {
color-scheme: light;
}
</style>
</head>
<body>
<div id="root"></div>
<script src="${iframeJsPath}" type="module">
</script>
</body>
</html>`
}
let singleTonIframe
function createIframe(target) {
if (!singleTonIframe) {
singleTonIframe = document.createElement('iframe')
listenEvent(singleTonIframe, target)
}
// srcdoc dosen't work on IE11
singleTonIframe.srcdoc = makeIframeContent(target)
singleTonIframe.style.width = '100%'
singleTonIframe.style.border = '0'
return singleTonIframe
}
function postMessage(event, data) {
if (singleTonIframe) {
singleTonIframe.contentWindow.postMessage(
JSON.stringify({
from: 'cusdis',
event,
data,
}),
)
}
}
function listenEvent(iframe, target) {
const darkModeQuery = window.matchMedia('(prefers-color-scheme: dark)')
const onMessage = (e) => {
try {
const msg = JSON.parse(e.data)
if (msg.from === 'cusdis') {
switch (msg.event) {
case 'onload':
{
if (target.dataset.theme === 'auto') {
postMessage(
'setTheme',
darkModeQuery.matches ? 'dark' : 'light',
)
}
}
break
case 'resize':
{
iframe.style.height = msg.data + 'px'
}
break
}
}
} catch (e) {}
}
window.addEventListener('message', onMessage)
function onChangeColorScheme(e) {
const isDarkMode = e.matches
if (target.dataset.theme === 'auto') {
postMessage('setTheme', isDarkMode ? 'dark' : 'light')
}
}
darkModeQuery.addEventListener('change', onChangeColorScheme)
return () => {
darkModeQuery.removeEventListener('change', onChangeColorScheme)
window.removeEventListener('message', onMessage)
}
}
function render(target) {
if (target) {
target.innerHTML = ''
const iframe = createIframe(target)
target.appendChild(iframe)
}
}
// deprecated
window.renderCusdis = render
window.CUSDIS.renderTo = render
window.CUSDIS.setTheme = function (theme) {
postMessage('setTheme', theme)
}
function initial() {
let target
if (window.cusdisElementId) {
target = document.querySelector(`#${window.cusdisElementId}`)
} else if (document.querySelector('#cusdis_thread')) {
target = document.querySelector('#cusdis_thread')
} else if (document.querySelector('#cusdis')) {
console.warn(
'id `cusdis` is deprecated. Please use `cusdis_thread` instead',
)
target = document.querySelector('#cusdis')
}
if (window.CUSDIS_PREVENT_INITIAL_RENDER === true) {
} else {
if (target) {
render(target)
}
}
}
// initialize
window.CUSDIS.initial = initial
initial()