forked from qiangck/weex-ide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
126 lines (112 loc) · 3.49 KB
/
renderer.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
const fs = require('fs');
const path = require('path');
const transformer = require('weex-transformer');
const qrcode = require('./qrcode');
const config = require('./config.json');
const sample = document.querySelector('#sample');
const codeEl = document.querySelector('#code');
const logsWrapEl = document.querySelector('#logs');
const logsEl = document.querySelector('#logs pre');
const html5El = document.querySelector('#html5');
const qrcodeEl = document.querySelector('#qrcode');
const canvasEl = document.querySelector('#qrcode canvas');
const html5ReadyPromise = new Promise((resolve, reject) => {
html5El.addEventListener('dom-ready', e => resolve(e));
});
const sampleFilepath = path.join(__dirname, 'sources', 'keymaps.we');
const sampleCode = fs.readFileSync(sampleFilepath, 'utf-8');
const cm = CodeMirror(codeEl, {
value: sampleCode,
mode: 'htmlmixed',
theme: 'monokai',
tabSize: 2,
lineWrapping: true,
lineNumbers: true,
foldGutter: true,
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
extraKeys: {
'Cmd-Alt-F': cm => {
cm.setOption('fullScreen', !cm.getOption('fullScreen'));
},
'Cmd-S': cm => {
const value = cm.getValue();
saveCode(value);
},
'Cmd-Alt-L': cm => {
toggleLogs();
},
'Cmd-Alt-R': cm => {
toggleQRCode();
},
'Cmd-Alt-T': cm => {
openDevToolsForPreview();
},
'Esc': cm => {
if (cm.getOption('fullScreen')) {
cm.setOption('fullScreen', false);
}
}
}
});
function printLogs(logs) {
if (logs.length === 0) {
logsEl.innerHTML = `<p class="success">Save at ${new Date().toLocaleString()}</p>`;
} else {
logsEl.innerHTML = logs.map(log => {
var className = log.reason.match(/^([^:]+?)\:/)[1].toLowerCase();
return `<p class="${className}" data-at="${log.line}:${log.column}">${log.reason}</p>`;
}).join('');
}
}
const localIP = require('local-ip');
const IPProimse = new Promise((resolve, reject) => {
localIP('en0', (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
function toggleQRCode() {
if (html5El.style.display === 'none') {
html5El.style.display = '';
qrcodeEl.style.display = '';
} else {
html5El.style.display = 'none';
qrcodeEl.style.display = 'flex';
}
IPProimse.then(ip => {
const url = qrcode.toDataURL(`http://${ip}:${config['serve-port']}/code.js`, {
canvas: canvasEl
});
});
}
function toggleLogs() {
if (logsWrapEl.style.display === 'none') {
logsWrapEl.style.display = '';
} else {
logsWrapEl.style.display = 'none';
}
}
const sourceFilepath = path.join(__dirname, 'sources', 'code.we');
const bundleFilepath = path.join(__dirname, 'bundles', 'code.js');
function saveCode(codeStr) {
fs.writeFileSync(sourceFilepath, codeStr, {
encoding: 'utf-8'
});
const ret = transformer.transform('code', codeStr);
printLogs(ret.logs);
fs.writeFileSync(bundleFilepath, ret.result, {
encoding: 'utf-8'
});
html5El.style.display = '';
qrcodeEl.style.display = '';
html5El.src = `file://${__dirname}/html5.html?_=${Date.now()}`;
}
function openDevToolsForPreview() {
html5ReadyPromise.then(e => {
html5.openDevTools();
});
}
saveCode(sampleCode);