Skip to content

Commit e6aef39

Browse files
committed
fix nested diff toggle and VA QOL
1 parent cc6b0dd commit e6aef39

File tree

5 files changed

+26
-80
lines changed

5 files changed

+26
-80
lines changed

data/a1.json

Lines changed: 0 additions & 30 deletions
This file was deleted.

data/a2.json

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/lib/diff.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ const getContainerProps = (val) => {
2626
};
2727

2828
const mapChildrenForSide = (val, side) => {
29+
const type = side === 'added' ? TYPE.ADDED : TYPE.REMOVED;
2930
const createChildNode = (value, key) => {
3031
const leftValue = side === 'added' ? undefined : value;
3132
const rightValue = side === 'added' ? value : undefined;
3233
const childExtra = isObj(value) ? {
3334
children: mapChildrenForSide(value, side),
3435
...getContainerProps(value)
3536
} : {};
36-
return createNode(key, TYPE.UNCHANGED, leftValue, rightValue, childExtra);
37+
return createNode(key, type, leftValue, rightValue, childExtra);
3738
};
3839

3940
if (Array.isArray(val)) {

src/lib/viewer.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class JsonDiffViewer extends HTMLElement {
6464
},
6565
});
6666
#stats = {};
67-
#showOnlyChanged = false;
67+
#showOnlyChanged = true;
6868

6969
static observedAttributes = ["left", "right"];
7070
constructor() {
@@ -139,6 +139,8 @@ class JsonDiffViewer extends HTMLElement {
139139
this.shadowRoot.innerHTML = `<style>${styles}</style><div class="empty">Provide left and right JSON</div>`;
140140
return;
141141
}
142+
const panel = this.shadowRoot.querySelector('.panel');
143+
const scroll = { top: panel?.scrollTop || 0, left: panel?.scrollLeft || 0 };
142144
this.shadowRoot.innerHTML = `
143145
<style>${styles}</style>
144146
<div class="stats">
@@ -163,6 +165,10 @@ class JsonDiffViewer extends HTMLElement {
163165
.join("")}
164166
</div>`;
165167
this.#bind();
168+
this.shadowRoot.querySelectorAll('.panel').forEach(p => {
169+
p.scrollTop = scroll.top;
170+
p.scrollLeft = scroll.left;
171+
});
166172
}
167173

168174
#renderNode(node, side, path, root = true, placeholderParam = false) {
@@ -173,10 +179,11 @@ class JsonDiffViewer extends HTMLElement {
173179
const hidden = placeholder ? ' style="visibility: hidden;"' : "";
174180
const keyHtml = buildKeyHtml(node.key, root, placeholder);
175181

176-
if (value === undefined) {
177-
if (!node.children?.length) {
178-
return `<div class="node${rootClass}"><div class="line placeholder">${keyHtml}</div></div>`;
179-
}
182+
if (value === undefined && !node.children?.length) {
183+
const otherValue = side === 'left' ? node.right : node.left;
184+
const [val, type] = format(otherValue);
185+
const hiddenKey = buildKeyHtml(node.key, root, true);
186+
return `<div class="node${rootClass}"><div class="line placeholder">${hiddenKey}<span class="val-${type}" style="visibility: hidden;">${val}</span></div></div>`;
180187
}
181188

182189
if (value !== undefined && !node.isArray && !node.isObject) {

src/main.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,26 @@ const themes = {
2020
}
2121
}
2222

23+
const themeLabels = { dark: "Light", light: "Dark" };
24+
2325
const applyTheme = (theme) => {
24-
const tokens = themes[theme];
25-
for (const [key, value] of Object.entries(tokens)) {
26+
Object.entries(themes[theme]).forEach(([key, value]) => {
2627
viewer.style.setProperty(`--${key}`, value);
27-
}
28-
toggle.textContent = theme === "dark" ? "Light" : "Dark";
28+
});
29+
toggle.textContent = themeLabels[theme];
2930
document.documentElement.setAttribute("data-theme", theme);
3031
localStorage.setItem("json-diff-viewer-theme", theme);
3132
};
3233

33-
const getTheme = () => localStorage.getItem("json-diff-viewer-theme") || "dark"
34-
applyTheme(getTheme())
34+
const getTheme = () => localStorage.getItem("json-diff-viewer-theme") || "dark";
35+
36+
const toggleTheme = (current) => current === "dark" ? "light" : "dark";
37+
38+
applyTheme(getTheme());
3539

3640
toggle.addEventListener("click", () => {
37-
const current = getTheme()
38-
const next = current === "dark" ? "light" : "dark"
39-
applyTheme(next)
40-
})
41+
applyTheme(toggleTheme(getTheme()));
42+
});
4143

4244
Promise.all([
4345
fetch(`${import.meta.env.BASE_URL}data/a1.json`).then((r) => r.json()),

0 commit comments

Comments
 (0)