-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-writer.js
102 lines (82 loc) · 3.19 KB
/
json-writer.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
let editor;
document.addEventListener('DOMContentLoaded', function(){
let element = document.querySelector(".editorArea");
theme = document.querySelector('body').classList.contains("dark") ? "custom-dark" : "vs-light";
require.config({ paths: { 'vs': 'https://cdn.jsdelivr.net/npm/monaco-editor/min/vs' } });
require(['vs/editor/editor.main'], function () {
monaco.editor.defineTheme('custom-dark', {
base: 'vs-dark',
inherit: true,
rules: [],
colors: {
'editor.background': '#212529',
}
});
editor = monaco.editor.create(element, {
value: "{}",
language: 'json',
theme: theme,
readOnly: false,
stickyScroll: {
enabled: false
}
});
let observableDiv = document.querySelector("body");
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.attributeName === 'class') {
const newClassList = observableDiv.classList;
if (newClassList.contains('dark')) {
monaco.editor.setTheme('custom-dark');
} else {
monaco.editor.setTheme('vs-light');
}
}
});
});
observer.observe(observableDiv, { attributes: true });
var resizeObserver = new ResizeObserver(function (entries) {
for (let entry of entries) {
editor.layout();
}
});
resizeObserver.observe(document.querySelector(".editorArea"));
editor.onKeyUp(setLineColIndicator);
editor.onKeyDown(setLineColIndicator);
function setLineColIndicator(){
const position = editor.getPosition();
const line = position.lineNumber;
const column = position.column;
document.getElementById("row_call_indicator").innerText = "Ln " + line + ", Col " + column;
}
});
});
function downloadFormattedJson(){
if(document.querySelector(".saveButton").classList.contains('disabled')){
return;
}
let content = editor.getValue();
var blob = new Blob([content], { type: 'text/plain;charset=utf-8' });
var link = document.createElement('a');
link.download = 'created-json.json';
link.href = window.URL.createObjectURL(blob);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
document.querySelector('#formatBtn').addEventListener("click", function(){
editor.getAction('editor.action.formatDocument').run();
let content = editor.getValue();
var toastObject = new bootstrap.Toast(document.getElementById("liveToast"));
var liveToast = document.getElementById("liveToast");
try{
if(content.trim() != ""){
JSON.parse(content);
}
}catch(error){
liveToast.style.backgroundColor = "#FECDD3";
liveToast.style.color = 'red';
document.getElementById('toast-alert-message').innerHTML = "File contain errors, unable to format!";
toastObject.show(1);
}
});