-
Notifications
You must be signed in to change notification settings - Fork 8
/
script.js
169 lines (137 loc) · 4.52 KB
/
script.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Dev 2.0
// (c) Copyright 2015-2019 Stone App Technology Studio.
let $ = (selector) => {
let elements = document.querySelectorAll(selector);
if (elements.length == 1) return document.querySelector(selector);
else return elements;
}
function click(el, listener) {
return el.addEventListener('click', (e) => {
try {
listener(e);
} catch (error) {
console.error(error);
}
});
}
async function sleep(ms) {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms);
});
}
// initialize the variable
let editors = {};
let previewFrame = $("#preview iframe");
let titleLabel = $('#label');
let dimmer = $('.ts.dimmer');
const languages = ["html","css","javascript"];
const exampleContent = `<!DOCTYPE html>\n<html>\n <head>\n <title>Title</title>\n </head>\n <body>\n <p>試試看,雖然我覺得不行。</p>\n </body>\n</html>`;
// initialize the environment
languages.forEach((lang) => {
let editor = ace.edit(lang);
editors[lang] = editor;
editor.setTheme("ace/theme/chrome");
editor.getSession().setMode(`ace/mode/${lang}`);
editor.getSession().setUseWrapMode(true);
editor.$blockScrolling = Infinity;
editor.commands.addCommand({
name: "renderPage",
bindKey: {win: "Ctrl-Enter", mac: "Command-Enter"},
exec: function(_editor) {
render();
}
});
});
ts('.menu .item').tab(); // ts menu tab
ts('.left.sidebar').sidebar({
dimPage: true,
closeable: false
});
// setup menu listener
languages.forEach((lang) => {
click($(`a[data-tab="${lang}"]`), (e) => {
editors[lang].renderer.updateFull();
});
});
click($('#sidebarMenu'), (e) => {
ts('.ts.sidebar').sidebar('toggle');
});
click($('#run'), render);
window.addEventListener('message', async (e) => {
console.log('onmessage');
let data = e.data;
if (data.title) {
titleLabel.textContent = data.title;
}
await sleep(300);
dimmer.classList.toggle('active', false);
});
click($('#reset'), (e) => {
editors.html.setValue(exampleContent);
editors.css.setValue("");
editors.javascript.setValue("");
});
editors.html.setValue(exampleContent);
render();
function combinePage(HTMLSource, cssSource, jsSource) {
let parser = new DOMParser();
let shadowDOM = parser.parseFromString(HTMLSource, 'text/html');
if (cssSource.trim() != "") {
let stylesheet = document.createElement('style');
stylesheet.textContent = cssSource;
shadowDOM.querySelector('head').appendChild(stylesheet);
}
if (jsSource.trim() != "") {
let script = document.createElement('script');
script.textContent = jsSource;
shadowDOM.querySelector('body').appendChild(script);
}
// post process to disable dimmer and update title
let postSource = `(() => {
console.log('iframe ready');
parent.postMessage({"status": "ready", "title": (document.title || "No title")}, "*");
// observe title changes
let observer = new MutationObserver((mutation, observer) => {
parent.postMessage({
"status": "titleUpdate",
"title": (document.title || "No title")
}, "*");
});
observer.observe(document.querySelector('title'), { childList: true, subtree: true });
})();`;
let postScript = document.createElement('script');
postScript.textContent = postSource;
shadowDOM.querySelector('body').appendChild(postScript);
return shadowDOM;
}
function render(...args) {
console.log('render')
dimmer.classList.toggle("active", true);
let source = languages.map((lang, _i) => {
return editors[lang].getValue();
});
let newDocument = combinePage(...source);
previewFrame.srcdoc = newDocument.documentElement.outerHTML;
}
// welcome
if (!localStorage.getItem("visited")) {
welcomeMessage();
}
function setState() {
localStorage.setItem('visited','true');
}
function welcomeMessage() {
swal({
html: '<h1 class="ts center aligned header">歡迎!第一次使用!</h1>',
type: "success"
});
setState();
}
function info() {
swal({
type: "info",
html: '<h2 class="ts center aligned header">About</h2>'+'<a target="_blank" href="https://tocas-ui.com/">TocasUI</a>'+' / '+'<a target="_blank" href="https://ace.c9.io/">Ace Editor</a>'
+'<p>(c) Copyright 2015-2019 Stone App Technology Studio.</p>',
showConfirmButton: false
});
}