Skip to content

Commit 4c0dc07

Browse files
committed
added changed filter
1 parent dfbf839 commit 4c0dc07

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-diff-viewer-component",
3-
"version": "0.1.1",
3+
"version": "0.2.0",
44
"type": "module",
55
"description": "Vanilla JS web component for side-by-side JSON diff visualization",
66
"keywords": [

src/lib/styles.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ export default `
4444
.preview::after { content: ' items'; }
4545
.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; }
4646
.stats-buttons { display: flex; gap: 0.5rem; }
47-
.btn-collapse, .btn-expand { padding: 0.5rem; background: var(--bg2); border: 1px solid var(--bdr); border-radius: 6px; color: var(--txt); cursor: pointer; transition: background .15s, border-color .15s; display: flex; align-items: center; justify-content: center; }
48-
.btn-collapse svg, .btn-expand svg { width: 18px; height: 18px; }
49-
.btn-collapse:hover, .btn-expand:hover { background: rgba(255,255,255,.05); border-color: var(--dim); }
47+
.btn-filter, .btn-collapse, .btn-expand { padding: 0.5rem; background: var(--bg2); border: 1px solid var(--bdr); border-radius: 6px; color: var(--txt); cursor: pointer; transition: background .15s, border-color .15s; display: flex; align-items: center; justify-content: center; }
48+
.btn-filter svg, .btn-collapse svg, .btn-expand svg { width: 18px; height: 18px; }
49+
.btn-filter:hover, .btn-collapse:hover, .btn-expand:hover { background: rgba(255,255,255,.05); border-color: var(--dim); }
50+
.btn-filter .checkbox-icon { opacity: 0.3; transition: opacity .15s; }
51+
.btn-filter .checkbox-icon.checked { opacity: 1; }
5052
.stat { display: grid; grid-template-columns: auto 1fr; align-items: baseline; gap: .35rem; }
5153
.stat .dot { width: 8px; height: 8px; }
5254
.stat-added .dot { background: var(--add); }

src/lib/viewer.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class JsonDiffViewer extends HTMLElement {
2626
},
2727
});
2828
#stats = {};
29+
#showOnlyChanged = false;
2930

3031
static observedAttributes = ["left", "right"];
3132
constructor() {
@@ -105,6 +106,11 @@ class JsonDiffViewer extends HTMLElement {
105106
<div class="stats">
106107
${STAT_TYPES.map((t) => `<div class="stat stat-${t}"><span class="dot"></span>${this.#stats[t]} ${t.replace("_", " ")}</div>`).join("")}
107108
<div class="stats-buttons">
109+
<button class="btn-filter" data-action="filter" aria-label="Show only changed">
110+
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" class="checkbox-icon ${this.#showOnlyChanged ? 'checked' : ''}">
111+
<path fill="currentColor" d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/>
112+
</svg>
113+
</button>
108114
<button class="btn-collapse" data-action="collapse"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="currentColor" d="M9 15H6q-.425 0-.712-.288T5 14t.288-.712T6 13h4q.425 0 .713.288T11 14v4q0 .425-.288.713T10 19t-.712-.288T9 18zm6-6h3q.425 0 .713.288T19 10t-.288.713T18 11h-4q-.425 0-.712-.288T13 10V6q0-.425.288-.712T14 5t.713.288T15 6z"/></svg></button>
109115
<button class="btn-expand" data-action="expand"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="currentColor" d="M7 17h3q.425 0 .713.288T11 18t-.288.713T10 19H6q-.425 0-.712-.288T5 18v-4q0-.425.288-.712T6 13t.713.288T7 14zM17 7h-3q-.425 0-.712-.288T13 6t.288-.712T14 5h4q.425 0 .713.288T19 6v4q0 .425-.288.713T18 11t-.712-.288T17 10z"/></svg></button>
110116
</div>
@@ -140,11 +146,14 @@ class JsonDiffViewer extends HTMLElement {
140146

141147
const [open, close] = node.isArray ? ["[", "]"] : ["{", "}"];
142148
const isExpanded = this.#proxy[currentPath] !== false;
149+
const filteredChildren = this.#showOnlyChanged
150+
? node.children?.filter((c) => c.hasDiff) || []
151+
: node.children || [];
143152
const childrenHtml =
144-
node.children
145-
?.map((c) => this.#node(c, side, currentPath, false))
153+
filteredChildren
154+
.map((c) => this.#node(c, side, currentPath, false))
146155
.join("") || "";
147-
const preview = `${node.children?.length || 0}`;
156+
const preview = `${filteredChildren.length}`;
148157

149158
if (!isExpanded) {
150159
return `<div class="node${rootClass}"><div class="line ${diffClass}" data-p="${currentPath}"><span class="tog">▶</span>${dot}${keyHtml}<span class="br">${open}</span><span class="preview">${preview}</span><span class="br">${close}</span></div></div>`;
@@ -175,6 +184,12 @@ class JsonDiffViewer extends HTMLElement {
175184
};
176185
}
177186

187+
this.shadowRoot
188+
.querySelector('[data-action="filter"]')
189+
?.addEventListener("click", () => {
190+
this.#showOnlyChanged = !this.#showOnlyChanged;
191+
this.#render();
192+
});
178193
this.shadowRoot
179194
.querySelector('[data-action="collapse"]')
180195
?.addEventListener("click", () => this.#collapseAll());

0 commit comments

Comments
 (0)