| 1 |
function textarea_content_height(textarea) { |
| 2 |
// This trick only works if display is not 'none'. |
| 3 |
let old_display = textarea.style.display; |
| 4 |
textarea.style.display = 'block'; |
| 5 |
// The trick is to measure the height of the text in the text area. |
| 6 |
let old_height = textarea.style.height; |
| 7 |
textarea.style.height = '0px'; |
| 8 |
let height = textarea.scrollHeight; |
| 9 |
// Reset it. |
| 10 |
textarea.style.display = old_display; |
| 11 |
textarea.style.height = old_height; |
| 12 |
return height; |
| 13 |
} |
| 14 |
|
| 15 |
/** Make the text area large enough for its content. */ |
| 16 |
function resize_textarea(textarea) { |
| 17 |
let height = textarea_content_height(textarea); |
| 18 |
textarea.style.height = height + 'px'; |
| 19 |
} |
| 20 |
|
| 21 |
/** Make the text area large enough for its content (but not too big). */ |
| 22 |
function resize_textarea_max(max_height, output_textarea) { |
| 23 |
let height = textarea_content_height(output_textarea); |
| 24 |
height = Math.min(height, max_height); |
| 25 |
output_textarea.style.height = height + 'px'; |
| 26 |
} |
| 27 |
|
| 28 |
/** Fix the content of a textarea. |
| 29 |
* |
| 30 |
* In HTML, if the </textarea> is not on the final line of the text, or if the |
| 31 |
* content of the textarea is indented, extra whitespace appears on the page. |
| 32 |
* Defensively written HTML can avoid this, but it is hard to read / maintain. |
| 33 |
* Therefore, we use some javascript to correct it instead. |
| 34 |
*/ |
| 35 |
function fix_textarea(output_textarea) { |
| 36 |
let lines = output_textarea.value.split(/\r\n|\r|\n/); |
| 37 |
// Assume that no hard tabs are used. |
| 38 |
let max_ws = 10000; |
| 39 |
for (let line of lines) { |
| 40 |
let match = /[^ ]/.exec(line); |
| 41 |
if (match) { |
| 42 |
if (max_ws == null || max_ws > match.index) |
| 43 |
max_ws = match.index; |
| 44 |
} |
| 45 |
} |
| 46 |
for(let i = 0, len = lines.length; i < len; i++) { |
| 47 |
lines[i] = lines[i].substring(max_ws).trimRight(); |
| 48 |
} |
| 49 |
for (let line of lines) { |
| 50 |
let match = /[^ ]/.exec(line); |
| 51 |
if (match) { |
| 52 |
if (max_ws == null || max_ws > match.index) |
| 53 |
max_ws = match.index; |
| 54 |
} |
| 55 |
} |
| 56 |
while (lines.length > 0 && lines[lines.length - 1].length == 0) lines.pop(); |
| 57 |
output_textarea.value = lines.join('\n'); |
| 58 |
} |
| 59 |
|
| 60 |
let counter = 0; |
| 61 |
|
| 62 |
function aux_demo(input_id, input_files, main_file, output_id, process_func) { |
| 63 |
// input is a div.tab-window-input, it contains tab header and textareas. |
| 64 |
let input = document.getElementById(input_id); |
| 65 |
// input is a div.tab-window-output, it also contains tab header and textareas. |
| 66 |
let output = document.getElementById(output_id); |
| 67 |
// Will populate this below in the function. |
| 68 |
let editor_by_textarea_id = {}; |
| 69 |
|
| 70 |
// Create a bunch of editors, assign ids, and populate max_height and editor_by_textarea_id. |
| 71 |
let selected = 'selected'; |
| 72 |
for (let input_child of input.childNodes) { |
| 73 |
if (input_child.tagName == 'TEXTAREA') { |
| 74 |
// Fix whitespace from HTML |
| 75 |
fix_textarea(input_child); |
| 76 |
// Resize it to match its content |
| 77 |
resize_textarea(input_child); |
| 78 |
let language = input_files[input_child.id].split('.').pop(); |
| 79 |
if (language == 'libsonnet') |
| 80 |
language = 'jsonnet'; |
| 81 |
let editor = CodeMirror.fromTextArea(input_child, { |
| 82 |
mode: {name: language}, |
| 83 |
lineNumbers: true, |
| 84 |
indentUnit: 2, |
| 85 |
tabSize: 2, |
| 86 |
matchBrackets: true, |
| 87 |
scrollbarStyle: null, |
| 88 |
extraKeys: { |
| 89 |
Tab: (cm) => { |
| 90 |
if (cm.somethingSelected()) { |
| 91 |
cm.indentSelection('add'); |
| 92 |
} else { |
| 93 |
cm.execCommand('insertSoftTab'); |
| 94 |
} |
| 95 |
}, |
| 96 |
'Shift-Tab': (cm) => { |
| 97 |
cm.indentSelection('subtract'); |
| 98 |
} |
| 99 |
} |
| 100 |
}); |
| 101 |
let editor_div = editor.getWrapperElement() |
| 102 |
let id = 'editor-' + counter++; |
| 103 |
editor_div.classList.add(selected); |
| 104 |
selected = 'unselected'; |
| 105 |
editor_div.setAttribute('id', id); |
| 106 |
editor_by_textarea_id[input_child.id] = editor; |
| 107 |
} |
| 108 |
} |
| 109 |
for (let input_child of input.childNodes) { |
| 110 |
if (input_child.className == 'tab-header') { |
| 111 |
// Should be only one tab-header. |
| 112 |
let selected = 'selected'; |
| 113 |
for (let id in input_files) { |
| 114 |
let file = input_files[id]; |
| 115 |
let editor_div = editor_by_textarea_id[id].getWrapperElement(); |
| 116 |
input_child.innerHTML += |
| 117 |
"<div class=" + selected + " onclick='tab_input_click(this, \"" + editor_div.id + "\")'>" |
| 118 |
+ file + "</div>"; |
| 119 |
selected = 'unselected'; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
for (let textarea_id in editor_by_textarea_id) { |
| 125 |
let editor = editor_by_textarea_id[textarea_id]; |
| 126 |
// Serialize processing of change events, otherwise they can interfere with each other. |
| 127 |
let ongoingChange = Promise.resolve(true); |
| 128 |
editor.on('change', () => { |
| 129 |
return (ongoingChange = ongoingChange.then(async () => { |
| 130 |
try { |
| 131 |
// Clean out the existing textareas and tabs. |
| 132 |
// There should be only one. |
| 133 |
let tab_header = output.getElementsByClassName('tab-header')[0]; |
| 134 |
let last_selected; |
| 135 |
while (tab_header.firstChild) { |
| 136 |
let child = tab_header.firstChild; |
| 137 |
if (child.className == 'selected') { |
| 138 |
last_selected = child.innerHTML; |
| 139 |
} |
| 140 |
tab_header.removeChild(child); |
| 141 |
} |
| 142 |
|
| 143 |
let condemned_textareas = output.getElementsByTagName('TEXTAREA'); |
| 144 |
let last_scroll = 0; |
| 145 |
while (condemned_textareas[0]) { |
| 146 |
if (condemned_textareas[0].classList.contains('selected')) { |
| 147 |
last_scroll = condemned_textareas[0].scrollTop; |
| 148 |
} |
| 149 |
output.removeChild(condemned_textareas[0]); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
let input_files_content = {}; |
| 154 |
for (let id in input_files) { |
| 155 |
let file = input_files[id]; |
| 156 |
let file_editor = editor_by_textarea_id[id]; |
| 157 |
input_files_content[file] = file_editor.getValue(); |
| 158 |
} |
| 159 |
function add_textarea_and_tab(filename, selected, scroll, output_content, output_class) { |
| 160 |
let output_textarea = document.createElement('TEXTAREA'); |
| 161 |
let id = 'output-' + counter++; |
| 162 |
output_textarea.setAttribute('id', id); |
| 163 |
output_textarea.value = output_content; |
| 164 |
output.appendChild(output_textarea); |
| 165 |
|
| 166 |
let selected_class = 'unselected'; |
| 167 |
if (filename == selected) { |
| 168 |
selected_class = 'selected'; |
| 169 |
output_textarea.scrollTop = scroll; |
| 170 |
} |
| 171 |
output_textarea.className = selected_class + ' ' + output_class; |
| 172 |
let max_height = 300; |
| 173 |
for (let id in editor_by_textarea_id) { |
| 174 |
let editor_div = editor_by_textarea_id[id].getWrapperElement(); |
| 175 |
max_height = Math.max(max_height, editor_div.offsetHeight); |
| 176 |
} |
| 177 |
resize_textarea_max(max_height, output_textarea); |
| 178 |
|
| 179 |
let output_tab = document.createElement('DIV'); |
| 180 |
tab_header.appendChild(output_tab); |
| 181 |
output_tab.className = selected_class; |
| 182 |
output_tab.onclick = function() { tab_output_click(this, output_textarea.id); }; |
| 183 |
output_tab.innerHTML = filename; |
| 184 |
} |
| 185 |
try { |
| 186 |
await process_func( |
| 187 |
main_file, input_files_content, last_selected, last_scroll, add_textarea_and_tab); |
| 188 |
} catch (e) { |
| 189 |
if (typeof e === 'string') { |
| 190 |
// Remove last \n |
| 191 |
e = e.replace(/\n$/, ''); |
| 192 |
} |
| 193 |
add_textarea_and_tab(last_selected, last_selected, last_scroll, e, 'code-error'); |
| 194 |
} |
| 195 |
} catch (e) { |
| 196 |
console.error(`error while processing CodeMirror edit/change: ${e}`); |
| 197 |
} |
| 198 |
})); |
| 199 |
}); |
| 200 |
} |
| 201 |
|
| 202 |
for (let output_child of output.childNodes) { |
| 203 |
if (output_child.tagName == 'TEXTAREA') { |
| 204 |
fix_textarea(output_child); |
| 205 |
let max_height = 300; |
| 206 |
for (let id in editor_by_textarea_id) { |
| 207 |
let editor_div = editor_by_textarea_id[id].getWrapperElement(); |
| 208 |
max_height = Math.max(max_height, editor_div.offsetHeight); |
| 209 |
} |
| 210 |
resize_textarea_max(max_height, output_child); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
function tab_input_click(tab, editor_div_id) { |
| 216 |
let output = document.getElementById(editor_div_id); |
| 217 |
for (let sibling of output.parentElement.childNodes) { |
| 218 |
if (!sibling.classList || !sibling.classList.contains('CodeMirror')) continue; |
| 219 |
sibling.classList.remove('selected'); |
| 220 |
sibling.classList.add('unselected'); |
| 221 |
} |
| 222 |
output.classList.remove('unselected'); |
| 223 |
output.classList.add('selected'); |
| 224 |
for (let sibling of tab.parentElement.childNodes) { |
| 225 |
if (sibling.nodeName == "#text") continue; |
| 226 |
sibling.classList.remove('selected'); |
| 227 |
sibling.classList.add('unselected'); |
| 228 |
} |
| 229 |
tab.classList.remove('unselected'); |
| 230 |
tab.classList.add('selected'); |
| 231 |
} |
| 232 |
|
| 233 |
function tab_output_click(tab, output_id) { |
| 234 |
let output = document.getElementById(output_id); |
| 235 |
for (let sibling of output.parentElement.childNodes) { |
| 236 |
if (sibling.tagName != 'TEXTAREA') continue; |
| 237 |
sibling.classList.remove('selected'); |
| 238 |
sibling.classList.add('unselected'); |
| 239 |
} |
| 240 |
output.classList.remove('unselected'); |
| 241 |
output.classList.add('selected'); |
| 242 |
for (let sibling of tab.parentElement.childNodes) { |
| 243 |
if (sibling.nodeName == "#text") continue; |
| 244 |
sibling.classList.remove('selected'); |
| 245 |
sibling.classList.add('unselected'); |
| 246 |
} |
| 247 |
tab.classList.remove('unselected'); |
| 248 |
tab.classList.add('selected'); |
| 249 |
} |
| 250 |
|
| 251 |
function handle_parsed_content(parsed_content, string_out) { |
| 252 |
if (string_out) { |
| 253 |
if (typeof(parsed_content) != 'string') { |
| 254 |
throw 'RUNTIME ERROR: expected string result.'; |
| 255 |
} |
| 256 |
// Remove last \n |
| 257 |
return parsed_content.replace(/\n$/, ''); |
| 258 |
} else { |
| 259 |
// Original JSON is sorted, ordering is preserved here. |
| 260 |
return JSON.stringify(parsed_content, null, 2); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/** Take a bunch of existing HTML and make it into an active Jsonnet demo. |
| 265 |
* |
| 266 |
* The best way to understand how to use this function is to read where it is used. |
| 267 |
* |
| 268 |
* input_id: The id of the tab-window-input div. |
| 269 |
* input_files: A dict mapping input filenames to the ids of the corresponding textarea elements. |
| 270 |
* main_file: The one that Jsonnet is executed on (it imports the others). |
| 271 |
* output_id: The id of the tab-window-output div. |
| 272 |
* multi: True to enable multi-output mode. |
| 273 |
* top_level: A dict to configure top-level parameterization (tla / ext var). |
| 274 |
*/ |
| 275 |
async function demo(input_id, input_files, main_file, output_id, multi, string_out, top_level) { |
| 276 |
top_level = top_level || {}; |
| 277 |
let ext_str = top_level['ext_str'] || {}; |
| 278 |
let ext_code = top_level['ext_code'] || {}; |
| 279 |
let tla_str = top_level['tla_str'] || {}; |
| 280 |
let tla_code = top_level['tla_code'] || {}; |
| 281 |
|
| 282 |
aux_demo( |
| 283 |
input_id, input_files, main_file, output_id, |
| 284 |
async function(main_file, input_files_content, last_selected, last_scroll, add_textarea_and_tab) { |
| 285 |
let json_str = await jsonnet_evaluate_snippet( |
| 286 |
main_file, input_files_content[main_file], input_files_content, |
| 287 |
ext_str, ext_code, tla_str, tla_code); |
| 288 |
let parsed_output = JSON.parse(json_str); |
| 289 |
if (multi == true) { |
| 290 |
if (typeof parsed_output != 'object') { |
| 291 |
throw 'RUNTIME ERROR: multi mode: top-level object was a ' + typeof parsed_output |
| 292 |
+ ', should be an object whose keys are filenames and values hold the JSON for' |
| 293 |
+ ' that file.'; |
| 294 |
} |
| 295 |
if (Object.keys(parsed_output).length == 0) { |
| 296 |
throw 'RUNTIME ERROR: multi mode: top-level object had no keys.'; |
| 297 |
} |
| 298 |
let selected = Object.keys(parsed_output)[0]; |
| 299 |
let scroll = 0; |
| 300 |
if (typeof parsed_output === 'object' && last_selected in parsed_output) { |
| 301 |
selected = last_selected; |
| 302 |
scroll = last_scroll; |
| 303 |
} |
| 304 |
for (let filename in parsed_output) { |
| 305 |
output_content = handle_parsed_content(parsed_output[filename], string_out); |
| 306 |
add_textarea_and_tab(filename, selected, scroll, output_content, 'code-json'); |
| 307 |
} |
| 308 |
} else { |
| 309 |
// Original JSON is sorted, ordering is preserved here. |
| 310 |
output_content = handle_parsed_content(parsed_output, string_out); |
| 311 |
add_textarea_and_tab(last_selected, last_selected, last_scroll, output_content, 'code-json'); |
| 312 |
} |
| 313 |
}); |
| 314 |
} |
| 315 |
|
| 316 |
/** Take a bunch of existing HTML and make it into an active Jsonnet formatter demo. |
| 317 |
* |
| 318 |
* textarea_id: The id of the textarea containing the Jsonnet code. |
| 319 |
* fliename: The filename Jsonnet is formatting. |
| 320 |
* output_id: The id of the tab-window-output div. |
| 321 |
*/ |
| 322 |
function fmt_demo(input_id, textarea_id, filename, output_id) { |
| 323 |
let process_func = async function( |
| 324 |
main_file, input_files_content, last_selected, last_scroll, add_textarea_and_tab) { |
| 325 |
let content = input_files_content[main_file]; |
| 326 |
let jsonnet_str = await jsonnet_fmt_snippet(filename, content) |
| 327 |
jsonnet_str = jsonnet_str.replace(/\n$/, ''); |
| 328 |
add_textarea_and_tab( |
| 329 |
last_selected, last_selected, last_scroll, jsonnet_str, 'code-json'); |
| 330 |
} |
| 331 |
let map = {}; |
| 332 |
map[textarea_id] = filename; |
| 333 |
aux_demo(input_id, map, filename, output_id, process_func); |
| 334 |
} |
| 335 |
|
| 336 |
/** Take a bunch of existing HTML and make it into an active YAML conversion demo. |
| 337 |
* |
| 338 |
* textarea_id: The id of the textarea containing the YAML code. |
| 339 |
* fliename: The filename Jsonnet is formatting. |
| 340 |
* output_id: The id of the tab-window-output div. |
| 341 |
*/ |
| 342 |
function yaml_conv_demo(input_id, textarea_id, filename, output_id) { |
| 343 |
let process_func = async function( |
| 344 |
main_file, input_files_content, last_selected, last_scroll, add_textarea_and_tab) { |
| 345 |
let content = input_files_content[main_file]; |
| 346 |
let parsed_yaml = jsyaml.loadAll(content); |
| 347 |
let yaml_json = JSON.stringify(parsed_yaml, null, 2); |
| 348 |
let jsonnet_str = await jsonnet_fmt_snippet(filename, yaml_json) |
| 349 |
jsonnet_str = jsonnet_str.replace(/\n$/, ''); |
| 350 |
add_textarea_and_tab( |
| 351 |
last_selected, last_selected, last_scroll, jsonnet_str, 'code-json'); |
| 352 |
} |
| 353 |
let map = {}; |
| 354 |
map[textarea_id] = filename; |
| 355 |
aux_demo(input_id, map, filename, output_id, process_func); |
| 356 |
} |