forked from bennypowers/json-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
134 lines (109 loc) · 3.24 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><json-viewer></title>
<script type="module" src="https://unpkg.com/@power-elements/json-viewer?module"></script>
<style>
html,
body {
height: 100%;
font-family: sans-serif;
}
output {
display: block;
}
a { color: lightskyblue }
a:visited { color: lightblue }
@media (prefers-color-scheme: dark) {
body {
background: #212529;
color: white;
}
}
</style>
</head>
<body>
<header>
<h1><json-viewer></h1>
<p>A single-file, zero-dependency custom element that renders syntax-highlighted JSON</p>
<p>❤️ Proudly uses <a href="https://open-wc.org" rel="noopener noreferrer">open-wc</a> tools and recommendations.</p>
</header>
<main>
<section>
<hgroup>
<h2>Using JSON Script Elements</h2>
<h3>Regular JSON</h3>
</hgroup>
<json-viewer>
<script type="application/json">
{
"one": 1
}
</script>
</json-viewer>
</section>
<section>
<h3>JSON with Linked Data</h3>
<json-viewer>
<script type="application/ld+json">
{
"@context": "https://json-ld.org/contexts/person.jsonld",
"@id": "http://dbpedia.org/resource/John_Lennon",
"name": "John Lennon",
"born": "1940-10-09",
"spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
}
</script>
</json-viewer>
</section>
<section>
<h2>Using the Object Attribute</h2>
<json-viewer object='{"one":1}'></json-viewer>
</section>
<section>
<hgroup>
<h2>Errors</h2>
<h3>Bad JSON Attribute</h3>
</hgroup>
<json-viewer object='{one:1}'></json-viewer>
</section>
<section>
<hgroup>
<h2>Using JavaScript</h2>
<h3>Setting the <code>object</code> property</h3>
</hgroup>
<json-viewer id="objectProperty"></json-viewer>
<button onclick="objectProperty.object = {type: 'object'}">Set as Object</button>
<button onclick='objectProperty.object = `{"type": "string"}`'>Set as String</button>
<button onclick='objectProperty.object = `{type: }`'>Set as Invalid String</button>
<h3>Handling Errors</h3>
<json-viewer id="firesError"></json-viewer>
<pre>
<output id="firesErrorOutput"></output>
</pre>
<button onclick="firesError.object = '{type: }'">Set as Object</button>
<h3>Objects with HTMLElement properties</h3>
<json-viewer id="elementProps"></json-viewer>
</section>
</main>
<script>
firesError.addEventListener('json-parse-error', event => {
console.log(event);
firesErrorOutput.textContent += `${event.type} fired. Check Console.\n`;
setElementProps();
});
setTimeout(setElementProps);
function setElementProps() {
const viewerElement = firesError;
let error;
if (firesError.error) {
const { name, message } = firesError.error;
error = { name, message };
}
elementProps.object = { viewerElement, error };
}
</script>
</body>
</html>