@@ -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