-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
100 lines (96 loc) · 2.87 KB
/
index.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JSON Formatter</title>
<style>
textarea {
width: 25em;
height: 25em;
}
#warn {
color: red;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-yaml/3.8.4/js-yaml.min.js"></script>
<script>
window.addEventListener("load", function() {
var modeInputs = document.getElementsByName("mode");
var modeIndicator = document.getElementById("modeIndicator");
var mode;
var input = document.getElementById("input");
var yaml = document.getElementById("yaml");
var json = document.getElementById("json");
var minify = document.getElementById("minify");
var warn = document.getElementById("warn");
var warnDetail = document.getElementById("warnDetail");
var modeChange = function() {
modeInputs.forEach(function(modeInput) {
if (modeInput.checked) mode = modeInput.value;
});
modeIndicator.textContent = mode;
};
var processInput = function() {
var inputText = input.value;
var obj;
try {
if (mode === "JSON") {
obj = JSON.parse(inputText);
} else {
obj = jsyaml.safeLoad(inputText);
}
} catch (e) {
warn.textContent = "INPUT " + mode + " is not valid"
warnDetail.textContent = e.message;
return;
}
warn.textContent = "";
warnDetail.textContent = "";
json.value = JSON.stringify(obj, null, " ");
minify.value = JSON.stringify(obj);
yaml.value = jsyaml.safeDump(obj);
};
modeInputs.forEach(function(modeInput) {
modeInput.addEventListener("click", function() {
modeChange();
processInput();
});
});
modeChange();
input.addEventListener("keyup", processInput);
});
</script>
</head>
<body>
<p>
<input id="jsonMode" name="mode" value="JSON" type="radio" checked>
<label for="jsonMode">JSON</label>
<input id="yamlMode" name="mode" value="YAML" type="radio">
<label for="yamlMode">YAML</label>
</p>
<table>
<thead>
<tr>
<th>INPUT(<span id="modeIndicator"></span>)</th>
<th>YAML</th>
<th>JSON</th>
<th>minify</th>
</tr>
</thead>
<tbody>
<tr>
<td><textarea id="input"></textarea></td>
<td><textarea id="yaml" readonly></textarea></td>
<td><textarea id="json" readonly></textarea></td>
<td><textarea id="minify" readonly></textarea></td>
</tr>
</tbody>
</table>
<p>
<span id="warn"></span>
</p>
<p>
<span id="warnDetail"></span>
</p>
</body>
</html>