-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (78 loc) · 2.83 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
const livePreviewFrame = document.getElementById('live-preview');
const htmlEditor = document.getElementById('html');
const cssEditor = document.getElementById('css');
const jsEditor = document.getElementById('js');
function initializeLivePreview() {
livePreviewFrame.contentWindow.document.body.innerHTML = '';
const styleElement = document.createElement('style');
styleElement.setAttribute('id', 'live-preview-style');
livePreviewFrame.contentWindow.document.head.appendChild(styleElement);
const pagedJsScript = document.createElement('script');
pagedJsScript.src = 'https://unpkg.com/pagedjs/dist/paged.legacy.polyfill.js';
livePreviewFrame.contentWindow.document.head.appendChild(pagedJsScript);
}
function updateLiveHTMLPreview(codeEditors) {
livePreviewFrame.contentWindow.document.body.innerHTML = codeEditors.html.getValue();
}
function updateLiveCSSPreview(codeEditors) {
const styleElement = livePreviewFrame.contentWindow.document.getElementById('live-preview-style');
styleElement.innerHTML = codeEditors.css.getValue();
}
function updateLiveJSPreview(codeEditors) {
const scriptElement = document.createElement('script');
scriptElement.innerHTML = codeEditors.js.getValue();
livePreviewFrame.contentWindow.document.body.appendChild(scriptElement);
}
function initializeCodeEditors() {
function getDefaultOptions(object) {
const defaultOptions = {
lineNumbers: true,
autoCloseTags: true,
autoCloseBrackets: true,
theme: 'panda-syntax'
};
if (object) {
const keys = Object.keys(object);
for (const key of keys) {
defaultOptions[key] = object[key];
}
}
return defaultOptions;
}
const codeEditors = {
html: CodeMirror(htmlEditor, getDefaultOptions({
mode: 'text/html',
value: '',
})),
css: CodeMirror(cssEditor, getDefaultOptions({
mode: 'css',
value: '',
extraKeys: { 'Ctrl-Space': 'autocomplete' },
hintOptions: {
completeSingle: false,
closeOnUnfocus: false
}
})),
js: CodeMirror(jsEditor, getDefaultOptions({
mode: 'javascript',
value: ''
})),
};
return codeEditors;
}
function setupLivePreviewStudio() {
const codeEditors = initializeCodeEditors();
CodeMirror.on(codeEditors.html, 'change', () => {
updateLiveHTMLPreview(codeEditors);
});
CodeMirror.on(codeEditors.css, 'change', () => {
updateLiveCSSPreview(codeEditors);
});
CodeMirror.on(codeEditors.js, 'change', () => {
updateLiveJSPreview(codeEditors);
});
}
document.addEventListener('DOMContentLoaded', () => {
initializeLivePreview();
setupLivePreviewStudio();
});