json-diff-viewer-component 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +189 -0
- package/package.json +32 -0
- package/src/lib/diff.js +38 -0
- package/src/lib/styles.js +56 -0
- package/src/lib/viewer.js +109 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 metaory
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
# json-diff-viewer
|
|
2
|
+
|
|
3
|
+
**Compare JSON side-by-side, visually**
|
|
4
|
+
|
|
5
|
+
A zero-dependency web component for visualizing JSON differences with synchronized scrolling, collapsible nodes, and syntax highlighting. Perfect for debugging, API comparisons, and configuration diffs
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Deep nested JSON comparison
|
|
10
|
+
- Side-by-side synchronized scrolling
|
|
11
|
+
- Collapsible nodes (synced between panels)
|
|
12
|
+
- Diff indicators bubble up to parent nodes
|
|
13
|
+
- Stats summary (added/removed/modified/type-changed)
|
|
14
|
+
- Syntax highlighting
|
|
15
|
+
- Zero dependencies
|
|
16
|
+
- Shadow DOM encapsulation
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm i json-diff-viewer-component
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### ES Module
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import "json-diff-viewer-component";
|
|
30
|
+
|
|
31
|
+
const viewer = document.querySelector("json-diff-viewer");
|
|
32
|
+
viewer.setData(leftObj, rightObj);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### HTML Attributes
|
|
36
|
+
|
|
37
|
+
```html
|
|
38
|
+
<json-diff-viewer
|
|
39
|
+
left='{"name":"foo"}'
|
|
40
|
+
right='{"name":"bar"}'
|
|
41
|
+
></json-diff-viewer>
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Properties
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
viewer.left = { name: "foo" };
|
|
48
|
+
viewer.right = { name: "bar" };
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Method
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
viewer.setData(leftObj, rightObj);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
<details>
|
|
58
|
+
<summary>Framework Examples</summary>
|
|
59
|
+
|
|
60
|
+
### React
|
|
61
|
+
|
|
62
|
+
```jsx
|
|
63
|
+
import { useEffect, useRef } from "react";
|
|
64
|
+
import "json-diff-viewer-component";
|
|
65
|
+
|
|
66
|
+
function DiffViewer({ left, right }) {
|
|
67
|
+
const viewerRef = useRef(null);
|
|
68
|
+
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
if (viewerRef.current) {
|
|
71
|
+
viewerRef.current.setData(left, right);
|
|
72
|
+
}
|
|
73
|
+
}, [left, right]);
|
|
74
|
+
|
|
75
|
+
return <json-diff-viewer ref={viewerRef} />;
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Vue
|
|
80
|
+
|
|
81
|
+
```vue
|
|
82
|
+
<template>
|
|
83
|
+
<json-diff-viewer ref="viewerRef" />
|
|
84
|
+
</template>
|
|
85
|
+
|
|
86
|
+
<script setup>
|
|
87
|
+
import { ref, watch } from "vue";
|
|
88
|
+
import "json-diff-viewer-component";
|
|
89
|
+
|
|
90
|
+
const props = defineProps({
|
|
91
|
+
left: Object,
|
|
92
|
+
right: Object,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const viewerRef = ref(null);
|
|
96
|
+
|
|
97
|
+
watch(
|
|
98
|
+
() => [props.left, props.right],
|
|
99
|
+
() => {
|
|
100
|
+
if (viewerRef.value) {
|
|
101
|
+
viewerRef.value.setData(props.left, props.right);
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
{ immediate: true }
|
|
105
|
+
);
|
|
106
|
+
</script>
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
</details>
|
|
110
|
+
|
|
111
|
+
## Diff Types
|
|
112
|
+
|
|
113
|
+
| Type | Color | Description |
|
|
114
|
+
| ------------ | ------ | ------------------------------------ |
|
|
115
|
+
| Added | Green | Key exists only in right |
|
|
116
|
+
| Removed | Red | Key exists only in left |
|
|
117
|
+
| Modified | Yellow | Value changed |
|
|
118
|
+
| Type Changed | Orange | Type mismatch (e.g. number → string) |
|
|
119
|
+
|
|
120
|
+
## Styling
|
|
121
|
+
|
|
122
|
+
Override CSS custom properties:
|
|
123
|
+
|
|
124
|
+
```css
|
|
125
|
+
json-diff-viewer {
|
|
126
|
+
/* Diff colors */
|
|
127
|
+
--added: #22c55e;
|
|
128
|
+
--removed: #ef4444;
|
|
129
|
+
--modified: #eab308;
|
|
130
|
+
--type-changed: #f97316;
|
|
131
|
+
--unchanged: #71717a;
|
|
132
|
+
|
|
133
|
+
/* Background */
|
|
134
|
+
--bg: #18181b;
|
|
135
|
+
--bg-panel: #27272a;
|
|
136
|
+
--border: #3f3f46;
|
|
137
|
+
|
|
138
|
+
/* Text */
|
|
139
|
+
--text: #fafafa;
|
|
140
|
+
--text-dim: #a1a1aa;
|
|
141
|
+
|
|
142
|
+
/* Syntax */
|
|
143
|
+
--key: #38bdf8;
|
|
144
|
+
--string: #a78bfa;
|
|
145
|
+
--number: #34d399;
|
|
146
|
+
--boolean: #fb923c;
|
|
147
|
+
--null: #f472b6;
|
|
148
|
+
--bracket: #71717a;
|
|
149
|
+
|
|
150
|
+
/* Layout */
|
|
151
|
+
--radius: 12px;
|
|
152
|
+
--font: "JetBrains Mono", monospace;
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Light Theme
|
|
157
|
+
|
|
158
|
+
```css
|
|
159
|
+
json-diff-viewer {
|
|
160
|
+
--bg: #fafafa;
|
|
161
|
+
--bg-panel: #ffffff;
|
|
162
|
+
--border: #e4e4e7;
|
|
163
|
+
--text: #18181b;
|
|
164
|
+
--text-dim: #71717a;
|
|
165
|
+
--key: #0284c7;
|
|
166
|
+
--string: #7c3aed;
|
|
167
|
+
--number: #059669;
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Sizing
|
|
172
|
+
|
|
173
|
+
```css
|
|
174
|
+
json-diff-viewer {
|
|
175
|
+
height: 600px;
|
|
176
|
+
border-radius: 16px;
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Dev
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
npm run dev # start dev server
|
|
184
|
+
npm run build # build for production
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## License
|
|
188
|
+
|
|
189
|
+
[MIT](LICENSE)
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "json-diff-viewer-component",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Vanilla JS web component for side-by-side JSON diff visualization",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"json",
|
|
8
|
+
"diff",
|
|
9
|
+
"viewer",
|
|
10
|
+
"web-component",
|
|
11
|
+
"custom-element",
|
|
12
|
+
"comparison"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./src/lib/viewer.js"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"src/lib/**",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"dev": "vite",
|
|
25
|
+
"build": "vite build",
|
|
26
|
+
"preview": "vite preview"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@fontsource/bungee": "^5.2.7",
|
|
30
|
+
"vite": "^7.2.4"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/lib/diff.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const TYPE = { UNCHANGED: 'unchanged', ADDED: 'added', REMOVED: 'removed', MODIFIED: 'modified', TYPE_CHANGED: 'type_changed' }
|
|
2
|
+
|
|
3
|
+
const typeOf = v => v === null ? 'null' : Array.isArray(v) ? 'array' : typeof v
|
|
4
|
+
const isObj = v => v !== null && typeof v === 'object'
|
|
5
|
+
const keys = (a, b) => [...new Set([...Object.keys(a || {}), ...Object.keys(b || {})])]
|
|
6
|
+
|
|
7
|
+
const node = (key, type, left, right, extra = {}) => ({ key, type, left, right, hasDiff: type !== TYPE.UNCHANGED, ...extra })
|
|
8
|
+
|
|
9
|
+
const container = (val, isArr) => isArr ? { isArray: true } : isObj(val) ? { isObject: true } : {}
|
|
10
|
+
|
|
11
|
+
const childMap = (val, side) => (v, k) => node(
|
|
12
|
+
k, TYPE.UNCHANGED,
|
|
13
|
+
side === 'added' ? undefined : v,
|
|
14
|
+
side === 'added' ? v : undefined,
|
|
15
|
+
isObj(v) && { children: mapChildren(v, side), ...container(v, Array.isArray(v)) }
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
const mapChildren = (val, side) =>
|
|
19
|
+
Array.isArray(val) ? val.map(childMap(val, side)) :
|
|
20
|
+
isObj(val) ? Object.entries(val).map(([k, v]) => childMap(val, side)(v, k)) : []
|
|
21
|
+
|
|
22
|
+
const diffContainer = (left, right, key, isArr) => {
|
|
23
|
+
const items = isArr
|
|
24
|
+
? Array.from({ length: Math.max(left?.length || 0, right?.length || 0) }, (_, i) => diff(left?.[i], right?.[i], i))
|
|
25
|
+
: keys(left, right).map(k => diff(left?.[k], right?.[k], k))
|
|
26
|
+
const hasDiff = items.some(c => c.hasDiff)
|
|
27
|
+
return node(key, hasDiff ? TYPE.MODIFIED : TYPE.UNCHANGED, left, right, { children: items, ...container(left, isArr) })
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const diff = (left, right, key = 'root') => {
|
|
31
|
+
if (left === undefined) return node(key, TYPE.ADDED, left, right, isObj(right) && { children: mapChildren(right, 'added'), ...container(right, Array.isArray(right)) })
|
|
32
|
+
if (right === undefined) return node(key, TYPE.REMOVED, left, right, isObj(left) && { children: mapChildren(left, 'removed'), ...container(left, Array.isArray(left)) })
|
|
33
|
+
if (!isObj(left) && !isObj(right)) return node(key, left === right ? TYPE.UNCHANGED : typeOf(left) !== typeOf(right) ? TYPE.TYPE_CHANGED : TYPE.MODIFIED, left, right)
|
|
34
|
+
if (typeOf(left) !== typeOf(right)) return node(key, TYPE.TYPE_CHANGED, left, right, { children: [], ...container(left, Array.isArray(left)) })
|
|
35
|
+
return diffContainer(left, right, key, Array.isArray(left))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { diff, TYPE }
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export default `
|
|
2
|
+
:host {
|
|
3
|
+
--add: #22c55e; --rem: #ef4444; --mod: #eab308; --typ: #f97316;
|
|
4
|
+
--bg: #18181b; --bg2: #27272a; --bdr: #3f3f46;
|
|
5
|
+
--txt: #fafafa; --dim: #a1a1aa;
|
|
6
|
+
--key: #38bdf8; --str: #a78bfa; --num: #34d399; --bool: #fb923c; --nul: #f472b6; --br: #71717a;
|
|
7
|
+
display: flex; flex-direction: column; font: 13px 'JetBrains Mono', 'Fira Code', monospace;
|
|
8
|
+
background: var(--bg); color: var(--txt); border-radius: 12px; overflow: hidden;
|
|
9
|
+
}
|
|
10
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
11
|
+
.container { display: grid; grid-template-columns: 1fr 1fr; flex: 1; min-height: 0; }
|
|
12
|
+
.panel { overflow: auto; padding: 1rem; background: var(--bg2); scrollbar-width: thin; scrollbar-color: var(--bdr) transparent; }
|
|
13
|
+
.panel:first-child { border-right: 2px solid var(--bdr); }
|
|
14
|
+
.header { padding: 0.25rem 0 0.5rem; font-weight: 600; color: var(--dim); border-bottom: 1px solid var(--bdr); margin-bottom: 0.5rem; font-size: 11px; }
|
|
15
|
+
.node { padding-left: 1.25rem; }
|
|
16
|
+
.node.root { padding-left: 0; }
|
|
17
|
+
.line { display: flex; align-items: flex-start; gap: 0.5rem; padding: 2px 4px; border-radius: 4px; cursor: pointer; transition: background .15s; }
|
|
18
|
+
.line:hover { background: rgba(255,255,255,.05); }
|
|
19
|
+
.tog { width: 1rem; flex-shrink: 0; color: var(--br); user-select: none; }
|
|
20
|
+
.tog:hover { color: var(--txt); }
|
|
21
|
+
.key { color: var(--key); }
|
|
22
|
+
.colon { color: var(--dim); margin-right: 0.25rem; }
|
|
23
|
+
.val-string { color: var(--str); }
|
|
24
|
+
.val-string::before, .val-string::after { content: '"'; }
|
|
25
|
+
.val-number { color: var(--num); }
|
|
26
|
+
.val-boolean { color: var(--bool); }
|
|
27
|
+
.val-null { color: var(--nul); font-style: italic; }
|
|
28
|
+
.br { color: var(--br); }
|
|
29
|
+
.diff-added { background: rgba(34,197,94,.15); }
|
|
30
|
+
.diff-removed { background: rgba(239,68,68,.15); }
|
|
31
|
+
.diff-modified { background: rgba(234,179,8,.15); }
|
|
32
|
+
.diff-type_changed { background: rgba(249,115,22,.15); }
|
|
33
|
+
.diff-added .key { color: var(--add); }
|
|
34
|
+
.diff-removed .key { color: var(--rem); }
|
|
35
|
+
.diff-modified .key { color: var(--mod); }
|
|
36
|
+
.diff-type_changed .key { color: var(--typ); }
|
|
37
|
+
.dot { width: 6px; height: 6px; border-radius: 50%; flex-shrink: 0; margin-top: 6px; }
|
|
38
|
+
.dot-added { background: var(--add); }
|
|
39
|
+
.dot-removed { background: var(--rem); }
|
|
40
|
+
.dot-modified { background: var(--mod); }
|
|
41
|
+
.dot-type_changed { background: var(--typ); }
|
|
42
|
+
.preview { color: var(--dim); font-style: italic; }
|
|
43
|
+
.preview::before { content: ' '; }
|
|
44
|
+
.preview::after { content: ' items'; }
|
|
45
|
+
.stats { display: flex; justify-content: space-between; align-items: center; gap: 1rem; padding: .75rem 1rem; background: var(--bg); border-bottom: 2px solid var(--bdr); font-size: 12px; }
|
|
46
|
+
.stats-buttons { display: flex; gap: 0.5rem; }
|
|
47
|
+
.btn-collapse, .btn-expand { padding: 0.35rem 0.75rem; background: var(--bg2); border: 1px solid var(--bdr); border-radius: 6px; color: var(--txt); font-size: 11px; font-family: inherit; cursor: pointer; transition: background .15s, border-color .15s; }
|
|
48
|
+
.btn-collapse:hover, .btn-expand:hover { background: rgba(255,255,255,.05); border-color: var(--dim); }
|
|
49
|
+
.stat { display: grid; grid-template-columns: auto 1fr; align-items: baseline; gap: .35rem; }
|
|
50
|
+
.stat .dot { width: 8px; height: 8px; }
|
|
51
|
+
.stat-added .dot { background: var(--add); }
|
|
52
|
+
.stat-removed .dot { background: var(--rem); }
|
|
53
|
+
.stat-modified .dot { background: var(--mod); }
|
|
54
|
+
.stat-type_changed .dot { background: var(--typ); }
|
|
55
|
+
.empty { padding: 2rem; color: var(--dim); }
|
|
56
|
+
`
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { diff, TYPE } from './diff.js'
|
|
2
|
+
import styles from './styles.js'
|
|
3
|
+
|
|
4
|
+
const STAT_TYPES = ['added', 'removed', 'modified', 'type_changed']
|
|
5
|
+
|
|
6
|
+
const format = val => ({
|
|
7
|
+
null: ['null', 'null'],
|
|
8
|
+
undefined: ['undefined', 'null'],
|
|
9
|
+
string: [val, 'string'],
|
|
10
|
+
number: [String(val), 'number'],
|
|
11
|
+
boolean: [String(val), 'boolean']
|
|
12
|
+
})[val === null ? 'null' : typeof val] || [JSON.stringify(val), 'string']
|
|
13
|
+
|
|
14
|
+
class JsonDiffViewer extends HTMLElement {
|
|
15
|
+
#left = null
|
|
16
|
+
#right = null
|
|
17
|
+
#tree = null
|
|
18
|
+
#exp = {}
|
|
19
|
+
#proxy = new Proxy(this.#exp, { set: (t, k, v) => (t[k] = v, this.#render(), true) })
|
|
20
|
+
#stats = {}
|
|
21
|
+
|
|
22
|
+
static observedAttributes = ['left', 'right']
|
|
23
|
+
constructor() { super(); this.attachShadow({ mode: 'open' }) }
|
|
24
|
+
connectedCallback() { this.#render() }
|
|
25
|
+
attributeChangedCallback(n, _, v) { n === 'left' ? this.#left = JSON.parse(v) : this.#right = JSON.parse(v); this.#compute() }
|
|
26
|
+
set left(v) { this.#left = v; this.#compute() }
|
|
27
|
+
set right(v) { this.#right = v; this.#compute() }
|
|
28
|
+
get left() { return this.#left }
|
|
29
|
+
get right() { return this.#right }
|
|
30
|
+
setData(l, r) { this.#left = l; this.#right = r; this.#compute() }
|
|
31
|
+
|
|
32
|
+
#compute() {
|
|
33
|
+
if (!this.#left || !this.#right) return
|
|
34
|
+
this.#tree = diff(this.#left, this.#right)
|
|
35
|
+
this.#stats = Object.fromEntries(STAT_TYPES.map(t => [t, 0]))
|
|
36
|
+
this.#walk(this.#tree, n => n.type !== TYPE.UNCHANGED && this.#stats[n.type]++)
|
|
37
|
+
for (const k in this.#exp) delete this.#exp[k]
|
|
38
|
+
this.#walk(this.#tree, (n, p) => (n.isArray || n.isObject) && !n.hasDiff && (this.#exp[p] = false))
|
|
39
|
+
this.#render()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
#walk(node, fn, path = '') {
|
|
43
|
+
const p = path ? `${path}.${node.key}` : String(node.key)
|
|
44
|
+
fn(node, p)
|
|
45
|
+
for (const c of node.children || []) this.#walk(c, fn, p)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
#collapseAll() {
|
|
49
|
+
if (!this.#tree) return
|
|
50
|
+
this.#walk(this.#tree, (n, p) => (n.isArray || n.isObject) && (this.#exp[p] = false))
|
|
51
|
+
this.#render()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
#expandAll() {
|
|
55
|
+
for (const k in this.#exp) delete this.#exp[k]
|
|
56
|
+
this.#render()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
#render() {
|
|
60
|
+
if (!this.#tree) return (this.shadowRoot.innerHTML = `<style>${styles}</style><div class="empty">Provide left and right JSON</div>`)
|
|
61
|
+
this.shadowRoot.innerHTML = `
|
|
62
|
+
<style>${styles}</style>
|
|
63
|
+
<div class="stats">
|
|
64
|
+
${STAT_TYPES.map(t => `<div class="stat stat-${t}"><span class="dot"></span>${this.#stats[t]} ${t.replace('_', ' ')}</div>`).join('')}
|
|
65
|
+
<div class="stats-buttons">
|
|
66
|
+
<button class="btn-collapse" data-action="collapse">Collapse All</button>
|
|
67
|
+
<button class="btn-expand" data-action="expand">Expand All</button>
|
|
68
|
+
</div>
|
|
69
|
+
</div>
|
|
70
|
+
<div class="container">
|
|
71
|
+
${['left', 'right'].map(s => `<div class="panel" data-side="${s}"><div class="header">${s === 'left' ? 'Original' : 'Modified'}</div>${this.#node(this.#tree, s, '')}</div>`).join('')}
|
|
72
|
+
</div>`
|
|
73
|
+
this.#bind()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
#node(n, side, path, root = true) {
|
|
77
|
+
const p = path ? `${path}.${n.key}` : String(n.key)
|
|
78
|
+
const val = n[side]
|
|
79
|
+
const cls = n.hasDiff && n.type !== TYPE.UNCHANGED ? `diff-${n.type}` : ''
|
|
80
|
+
const dot = n.hasDiff && n.children?.some(c => c.hasDiff) ? `<span class="dot dot-${n.type === TYPE.UNCHANGED ? 'modified' : n.type}"></span>` : ''
|
|
81
|
+
const key = root ? '' : `<span class="key">${n.key}</span><span class="colon">:</span>`
|
|
82
|
+
|
|
83
|
+
if (!n.isArray && !n.isObject) {
|
|
84
|
+
const [v, t] = format(val)
|
|
85
|
+
return `<div class="node${root ? ' root' : ''}"><div class="line ${cls}"><span class="tog"></span>${dot}${key}<span class="val-${t}">${v}</span></div></div>`
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const [open, close] = n.isArray ? ['[', ']'] : ['{', '}']
|
|
89
|
+
const exp = this.#proxy[p] !== false
|
|
90
|
+
|
|
91
|
+
if (!exp) return `<div class="node${root ? ' root' : ''}"><div class="line ${cls}" data-p="${p}"><span class="tog">▶</span>${dot}${key}<span class="br">${open}</span><span class="preview">${n.children?.length || 0}</span><span class="br">${close}</span></div></div>`
|
|
92
|
+
|
|
93
|
+
return `<div class="node${root ? ' root' : ''}"><div class="line ${cls}" data-p="${p}"><span class="tog">▼</span>${dot}${key}<span class="br">${open}</span></div>${n.children?.map(c => this.#node(c, side, p, false)).join('') || ''}<div class="line"><span class="tog"></span><span class="br">${close}</span></div></div>`
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
#bind() {
|
|
97
|
+
const [l, r] = this.shadowRoot.querySelectorAll('.panel')
|
|
98
|
+
let sync = false
|
|
99
|
+
const scroll = src => e => { if (sync) return; sync = true; const t = src === l ? r : l; t.scrollTop = src.scrollTop; t.scrollLeft = src.scrollLeft; sync = false }
|
|
100
|
+
l?.addEventListener('scroll', scroll(l))
|
|
101
|
+
r?.addEventListener('scroll', scroll(r))
|
|
102
|
+
for (const el of this.shadowRoot.querySelectorAll('[data-p]')) el.onclick = () => (this.#proxy[el.dataset.p] = this.#proxy[el.dataset.p] === false)
|
|
103
|
+
this.shadowRoot.querySelector('[data-action="collapse"]')?.addEventListener('click', () => this.#collapseAll())
|
|
104
|
+
this.shadowRoot.querySelector('[data-action="expand"]')?.addEventListener('click', () => this.#expandAll())
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
customElements.define('json-diff-viewer', JsonDiffViewer)
|
|
109
|
+
export { JsonDiffViewer }
|