forked from wavedrom/wavedrom.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
133 lines (110 loc) · 3.66 KB
/
init.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
(function(){
'use strict';
var e, val;
function ssvg () {
var svg, ser;
svg = document.getElementById('svgcontent_0');
ser = new XMLSerializer();
return '<?xml version="1.0" standalone="no"?>\n'
+ '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'
+ '<!-- Created with WaveDrom -->\n'
+ ser.serializeToString(svg);
}
function pngdata () {
var svgdata, img, canvas, context;
svgdata = 'data:image/svg+xml;base64,' + btoa(ssvg());
img = new Image();
img.src = svgdata;
canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
context = canvas.getContext('2d');
context.drawImage(img, 0, 0);
return canvas.toDataURL('image/png');
}
// die if Enternet Explorer
function killBill () {
if (navigator.appName.indexOf('Explorer') >= 0) {
var content;
content = document.getElementById('content');
content.parentNode.removeChild(e);
document.write('<h1>Sorry, Internet Explorer is not supported.</h1>');
document.body.style.backgroundColor = '#ffdddd';
window.stop();
}
}
function nodeWebkit () {
// node-webkit detection
val = localStorage.waveform;
if (val === undefined || val === '' || val === null) {
val = '{signal: [\n {name: \'clk\', wave: \'p.....|...\'},\n {name: \'dat\', wave: \'x.345x|=.x\', data: [\'head\', \'body\', \'tail\', \'data\']},\n {name: \'req\', wave: \'0.1..0|1.0\'},\n {},\n {name: \'ack\', wave: \'1.....|01.\'}\n]}\n';
localStorage.waveform = val;
}
if (typeof process !== 'object') {
return;
}
var gui = require('nw.gui'),
fs = require('fs'),
path = require('path');
var args = gui.App.argv,
data,
rawdata;
if (args[0] && args[0] === 'source' && args[1]) {
val = fs.readFileSync(
path.resolve(process.env.PWD, args[1]),
{encoding: 'utf8'}
);
localStorage.waveform = val;
} else {
return;
}
if (args[2] && args[3]) {
e.value = val;
WaveDrom.EditorRefresh();
if (args[2] === 'svg') {
data = ssvg();
}
if (args[2] === 'png') {
rawdata = pngdata().replace(/^data:image\/\w+;base64,/, '');
data = new Buffer(rawdata, 'base64');
}
fs.writeFileSync(
path.resolve(process.env.PWD, args[3]),
data,
{encoding: 'utf8'}
);
gui.App.quit();
}
}
killBill();
e = document.getElementById('InputJSON_0');
nodeWebkit();
e.value = val;
var cm = CodeMirror.fromTextArea(
e,
{
lineNumbers: true,
mode: {
name: 'javascript',
json: true
},
matchBrackets: true,
autoCloseBrackets: true,
highlightSelectionMatche: true,
autofocus: true
}
);
cm.on('change', function (cmm) {
function c () {
var v = cmm.getValue();
e.value = v;
localStorage.waveform = v;
WaveDrom.EditorRefresh();
}
setTimeout(c, 750);
});
WaveDrom.cm = cm;
})();
/* eslint-env browser */
/* global CodeMirror, WaveDrom */
/* eslint new-cap:0, no-underscore-dangle:1 */