Skip to content

Commit 3a09050

Browse files
committed
feat(renderer): implement renderer
1 parent d677f90 commit 3a09050

File tree

10 files changed

+998
-140
lines changed

10 files changed

+998
-140
lines changed

src/demo/index.html

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,44 @@ <h2>Interactive Example</h2>
4444
></textarea>
4545
<button id="parse-button">Parse JSON</button>
4646
</div>
47+
48+
4749
<andypf-json-viewer
48-
id="interactive-demo"
4950
show-toolbar
50-
expanded
51+
id="interactive-demo3"
52+
expanded="5"
53+
theme="monokai"
54+
indent="2"
55+
expand-icon-type="square"
56+
show-size
57+
show-data-types
58+
show-infos
5159
show-copy
52-
show-size
53-
toolbar-show-copy
54-
toolbar-show-size
55-
toolbar-show-search
56-
toolbar-show-expand-controls
57-
toolbar-show-indent-controls
58-
toolbar-show-theme-select
59-
toolbar-show-infos
6060
>
61-
{"test":"ètest"}
61+
{
62+
"string": "this is a test",
63+
"integer": 42,
64+
"array": [
65+
1,
66+
2,
67+
3,
68+
"test",
69+
null
70+
],
71+
"float": 3.14159,
72+
"object": {
73+
"first-child": true,
74+
"second-child": false,
75+
"last-child": null,
76+
"strings": {
77+
"veryLong": "The 'json-viewer' is a powerful tool designed specifically for displaying and analyzing JSON data. JSON (JavaScript Object Notation) is a widely used data format for transmission and storage. It's easy to understand and write, but when it comes to visualizing extensive or complex JSON data, it can be a challenge."
78+
}
79+
},
80+
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
81+
"string_number": "1234",
82+
"date": "2025-10-24T10:16:59.631Z"
83+
}
6284
</andypf-json-viewer>
63-
<andypf-json-viewer
64-
show-toolbar
65-
id="interactive-demo2"
66-
data="https://microsoftedge.github.io/Demos/json-dummy-data/64KB.json"
67-
></andypf-json-viewer>
6885
</div>
6986
<script>
7087
const textArea = document.getElementById("json-input")
@@ -85,6 +102,36 @@ <h2>Interactive Example</h2>
85102
console.warn("Indent value must be between 1 and 10")
86103
}
87104
})
105+
106+
const interactiveDemo4 = document.createElement("andypf-json-viewer")
107+
interactiveDemo4.id = "interactive-demo4"
108+
interactiveDemo4.setAttribute("show-toolbar", "")
109+
interactiveDemo4.setAttribute("theme", "light")
110+
interactiveDemo4.setAttribute("indent", "2")
111+
interactiveDemo4.setAttribute("expanded", "5")
112+
interactiveDemo4.setAttribute("expand-icon-type", "square")
113+
interactiveDemo4.setAttribute("show-size", "")
114+
interactiveDemo4.setAttribute("show-data-types", "")
115+
interactiveDemo4.setAttribute("show-infos", "")
116+
interactiveDemo4.setAttribute("show-copy", "")
117+
118+
119+
document.body.appendChild(interactiveDemo4)
120+
interactiveDemo4.setAttribute("data", JSON.stringify({
121+
122+
example: "This JSON viewer instance was created dynamically via JavaScript!",
123+
number: 12345,
124+
float: 67.89,
125+
boolean: true,
126+
nested: {
127+
array: [1, 2, 3, 4, 5],
128+
object: {
129+
key: "value"
130+
}
131+
},
132+
today: new Date()
133+
}, null, 2))
134+
88135
</script>
89136
</body>
90137
</html>

src/package/assets/styles.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
padding: 2px 5px;
3030
}
3131

32+
3233
.toolbar select.theme-select {
3334
background: transparent;
3435
display: flex;
@@ -37,6 +38,7 @@
3738
padding: 0;
3839
border: none;
3940
box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--base0D) 30%, transparent);
41+
border-radius: 5px;
4042
}
4143

4244
.toolbar .icon.copy {

src/package/assets/themes.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,9 +1430,11 @@ export const availableThemes: string[] = Object.keys(themes)
14301430
export const themeStyles = (nameOrObject: string | Record<string, string>): string => {
14311431
let theme
14321432
if (typeof nameOrObject === "string") {
1433-
if (themes[nameOrObject] === undefined) {
1434-
throw new Error(`${nameOrObject} not found`)
1433+
if (availableThemes.indexOf(nameOrObject) === -1) {
1434+
console.warn(`${nameOrObject} not found, falling back to default theme`)
1435+
nameOrObject = "default-light"
14351436
}
1437+
14361438
theme = themes[nameOrObject].reduce(
14371439
(map: Record<string, string>, v, i) => {
14381440
const key = `base0${i.toString(16).toUpperCase()}`

0 commit comments

Comments
 (0)