This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html-playground.html
executable file
·117 lines (100 loc) · 2.92 KB
/
html-playground.html
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
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<link rel="stylesheet" href="node_modules/codemirror/lib/codemirror.css">
<link rel="stylesheet" href="node_modules/codemirror/theme/twilight.css">
<link rel="stylesheet" href="node_modules/codemirror-inlet/inlet.css" type="text/css" media="screen" title="no title" charset="utf-8">
<style>
body {
overflow: hidden;
}
iframe {
background-color: #ecf0f1;
border: none;
overflow: hidden;
}
.html-editor-container {
font-size: 14px;
}
</style>
<body>
</body>
<script src="scripts/d3.v4.js"></script>
<script src="scripts/debounce.js"></script>
<script src="scripts/template-modifier.js"></script>
<script src="node_modules/codemirror/lib/codemirror.js"></script>
<script src="node_modules/codemirror/mode/javascript/javascript.js"></script>
<script src="node_modules/codemirror/mode/xml/xml.js"></script>
<script src="node_modules/codemirror/mode/css/css.js"></script>
<script src="node_modules/codemirror/mode/htmlmixed/htmlmixed.js"></script>
<script src="node_modules/codemirror-inlet/inlet.js"></script>
<script>
'use strict'
var width = window.innerWidth, height = window.innerHeight
var slug = window.location.hash.slice(1)
var defaultContent = '<!DOCTYPE html><html></html>'
if (slug)
d3.text('./examples/' + slug + '.html', function(err, res) {
gotContent(err ? defaultContent : res)
})
else gotContent(defaultContent)
function gotContent(content) {
var body = d3.select('body')
var main = body.append('div')
.style({
width: width + 'px',
height: height + 'px',
position: 'absolute',
top: '0px',
left: '0px',
})
var iframe = main.append('iframe').classed('stage', true)
.style({
position: 'absolute',
top: '0',
left: width / 2 + 'px',
width: width / 2 + 'px',
height: height + 'px'
})
var htmlEditorContainer = main.append('div')
.classed('html-editor-container', true)
.style({
width: width / 2 + 'px',
height: height + 'px',
position: 'absolute',
top: '0',
left: '0',
border: 'none',
})
var htmlCM = CodeMirror(htmlEditorContainer.node(), {
tabSize: 2,
value: content,
mode: 'htmlmixed',
lineNumbers: true,
theme: 'twilight',
lineWrapping: true,
})
htmlCM.setSize(width / 2, height)
htmlCM.on('change', function() {
updateHTML()
})
Inlet(htmlCM)
var blobUrl
var updateHTML = debounce(function updateHTML() {
var template = htmlCM.getValue()
d3.xhr('./examples/' + slug + '.html')
.header('Content-Type', 'text/html')
.post(template, function(err, res) {
if(err) throw err
})
template = templateModifier(template)
//remove the last instance
window.URL.revokeObjectURL(blobUrl)
var blob = new Blob([template], {type: 'text/html'})
blobUrl = URL.createObjectURL(blob)
iframe.node().src = blobUrl
}, 100)
updateHTML()
}
</script>
</html>