-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
188 lines (151 loc) · 5.42 KB
/
Copy pathcode.js
File metadata and controls
188 lines (151 loc) · 5.42 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//create content for iframe
function createFrameContent(exampleContent, highlightTheme, highlighter = 'prism' ) {
let docFrame = '';
const prismHeader =`
<head>
<link href="https://unpkg.com/prismjs@1.29.0/themes/${highlightTheme}.css" rel="stylesheet" />
<script src="https://unpkg.com/prismjs@1.29.0/components/prism-core.min.js"></sc`+`ript>
<script src="https://unpkg.com/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js"></sc`+`ript>
`;
const hTheme = highlightTheme.split ('highlight-')[1];
const highlightHeader = `
<head>
<link href="https://unpkg.com/@highlightjs/cdn-assets@11.9.0/styles/${hTheme}.min.css" rel="stylesheet" />
<script src="https://unpkg.com/@highlightjs/cdn-assets@11.9.0/highlight.js"></sc`+`ript>
<script>hljs.highlightAll();</scr`+`ipt>
`;
if (highlighter === 'prism') {
docFrame += prismHeader;
} else if (highlighter === 'highlight') {
docFrame += highlightHeader;
}
docFrame += `
<style>
html{
font-size: 1rem;
}
.console{
font-family: monospace;
background-color: darkslategray;
padding: 0.2rem;
font-size: 0.8rem;
}
.console>span {
color: lightgray;
}
.output {
border-top: 1px solid gray;
margin-top: 0.5rem;
}
.result{
color: lime;
padding: 0.2rem;
}
.error {
color: red;
padding: 0.2rem;
}
</style>
</head>
<body style="padding:0; margin:0">
`;
docFrame += `<pre><code class="language-javascript"> ${exampleContent} </code></pre>`;
//add code runner and console output
const output = `<div class='console'><span>Console output:<span><div class='output'></div></div>`;
const codeRunner = `
<script>
const out = document.querySelector('.output');
console.log = function (...args) {
const logString = args.join(' ');
out.innerHTML+= '<div class="result">>> '+logString+'</div>';
};
try{
eval(\`${exampleContent}\`);
} catch (error) {
out.innerHTML+= '<div class="error">>> '+error+'</div>';
}
</scr`+`ipt>
`;
docFrame += output;
docFrame += codeRunner;
return docFrame;
}
//resize iframe height for content
function resizeFrameHeight(frame) {
frame.style.height = frame.contentWindow.document.documentElement.scrollHeight + 'px';
}
async function getExamplesFromJSON (fileJSON) {
let response;
try{
response = await fetch(fileJSON);
const data = await response.json();
return data;
}
catch(error) {
console.warn(error);
}
}
//create iframes content
async function createExamples() {
const examplesList = document.querySelectorAll('iframe[id^="codeviewer"]');
const optionsHighlight = document.getElementById('highlight');
const selectedTheme = optionsHighlight[optionsHighlight.selectedIndex].value;
const highlighter = selectedTheme.split('-')[0];
const examplesFromJSON = await getExamplesFromJSON('./examples.json');
for (const frame of examplesList) {
const frameNumber = frame.id.split(/(\d)/)[1];
const currentExample = examplesFromJSON.examples[frameNumber-1];
const docFrame = createFrameContent(currentExample.code, selectedTheme, highlighter);
frame.style.height = 0;
frame.onload = ()=>{
return resizeFrameHeight(frame);
};
frame.srcdoc = docFrame;
}
}
//set row visibility
function visibilityRow(currentRow, state) {
if (state === 'collapse') {
currentRow.style.transition = 'all 0.2s';
currentRow.style.opacity = 0;
currentRow.style.visibility = 'collapse';
} else if (state === 'visible') {
currentRow.style.transition = 'all 0.7s';
currentRow.style.opacity = 1;
currentRow.style.visibility = 'visible';
}
}
function switchExampleState(event) {
let selectedRowId;
try {
selectedRowId = event.target.closest('tr').id;
}
catch {
selectedRowId = false;
}
// if click on table caption
if (!selectedRowId){
return false;
}
const rowNumber = selectedRowId.split(/(\d)/)[1];
const currentExample = document.getElementById('example-row'+rowNumber);
//collapse all examples exclude current
const exampleRowList = document.querySelectorAll('tr[id^="example-row"]');
for (const row of exampleRowList) {
if (row.style.visibility === 'visible' && row.id !== currentExample.id) {
visibilityRow(row, "collapse");
}
}
//switch current example state
if (currentExample.style.visibility === 'collapse' || currentExample.style.visibility === ''){
visibilityRow(currentExample, 'visible')
} else {
visibilityRow(currentExample, 'collapse')
}
}
// start create iframes content after loading page
window.onload = createExamples();
const tableVars = document.querySelector('table');
tableVars.addEventListener("click", switchExampleState);
const optionsHighlight = document.getElementById('highlight');
optionsHighlight.addEventListener("change", createExamples)