diff --git a/projects/demo/src/app/app.component.html b/projects/demo/src/app/app.component.html index 2aeaeb5..5dc28a1 100644 --- a/projects/demo/src/app/app.component.html +++ b/projects/demo/src/app/app.component.html @@ -1,5 +1,13 @@

ngx-json-treeview

+

Primatives

+
+ + + + +
+

Collapsed

diff --git a/projects/demo/src/app/app.component.scss b/projects/demo/src/app/app.component.scss index 14bc1fe..17a9d08 100644 --- a/projects/demo/src/app/app.component.scss +++ b/projects/demo/src/app/app.component.scss @@ -17,6 +17,7 @@ } .preview-pane { + background: #f9f9f9; border-left: 1px solid #ccc; padding: 10px; } diff --git a/projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.html b/projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.html index ef31668..0be7c17 100644 --- a/projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.html +++ b/projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.html @@ -1,69 +1,77 @@
- @if (_currentDepth() === 0) { - {{ '{' }} - } - @for (segment of segments(); track segment) { -
- @let expandable = isExpandable(segment); -
- - @if (expandable) { -
- } - {{ segment.key }} -
- : - @if (!expandable || !segment.expanded) { + @if (segments().length === 0) { +
+ + {{ json() ?? 'null' }} + +
+ } @else { + @if (_currentDepth() === 0) { + {{ '{' }} + } + @for (segment of segments(); track segment) { +
+ @let expandable = isExpandable(segment); +
- {{ segment.description }} - - } @else if (expandable && segment.expanded) { - - @if (segment.type === 'array') { - {{ '[' }} - } @else { - {{ '{' }} + [class.expandable]="expandable" + [class.expanded]="segment.expanded" + (click)="toggle(segment)"> + @if (expandable) { +
} + {{ segment.key }}
- } -
- @if (expandable && segment.expanded) { -
- - @if (['object', 'array'].includes(segment.type ?? '')) { + : + @if (!expandable || !segment.expanded) { + + {{ segment.description }} + + } @else if (expandable && segment.expanded) { @if (segment.type === 'array') { - {{ ']' }} + {{ '[' }} } @else { - {{ '}' }} + {{ '{' }} } }
- } -
- } - @if (_currentDepth() === 0) { - {{ '}' }} + @if (expandable && segment.expanded) { +
+ + @if (['object', 'array'].includes(segment.type ?? '')) { + + @if (segment.type === 'array') { + {{ ']' }} + } @else { + {{ '}' }} + } + + } +
+ } +
+ } + @if (_currentDepth() === 0) { + {{ '}' }} + } }
diff --git a/projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.scss b/projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.scss index abc360a..43f892a 100644 --- a/projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.scss +++ b/projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.scss @@ -105,4 +105,18 @@ $type-colors: ( .puctuation { color: var(--ngx-json-punctuation, #000); } + + @each $type, $color in $type-colors { + .segment-type-#{$type} > .segment-primative { + color: #{$color}; + } + } + .segment-primative { + margin: 0px; + padding: 0px; + } + // special cases that need highlighting + .segment-type-null > .segment-primative { + background-color: var(--ngx-json-null-bg, red); + } } diff --git a/projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.ts b/projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.ts index 3fc4c06..eca5fd0 100644 --- a/projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.ts +++ b/projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.ts @@ -113,17 +113,15 @@ export class NgxJsonTreeviewComponent { */ _currentDepth = input(0); + rootType = computed(() => + this.json() != null ? typeof this.json() : 'null' + ); segments = computed(() => { const json = decycle(this.json()); - const arr = []; - if (typeof json === 'object') { - Object.keys(json).forEach((key) => { - arr.push(this.parseKeyValue(key, json[key])); - }); - } else { - arr.push(this.parseKeyValue(`(${typeof json})`, json)); + if (typeof json === 'object' && json != null) { + return Object.keys(json).map((key) => this.parseKeyValue(key, json[key])); } - return arr; + return []; }); isExpanded = computed( () =>