diff --git a/projects/demo/src/app/app.component.html b/projects/demo/src/app/app.component.html index 97a7884..f460067 100644 --- a/projects/demo/src/app/app.component.html +++ b/projects/demo/src/app/app.component.html @@ -34,13 +34,14 @@

Clickable Nodes

} -

Primatives

+

Simple Types

+

Arrays

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 1adba9b..6ef17d4 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,13 +1,17 @@
@if (segments().length === 0) { -
+ @let sectionClass = + ['object', 'array'].includes(rootType()) + ? 'punctuation' + : 'segment-type-' + rootType(); +
{{ asString() }}
} @else { @if (_currentDepth() === 0) { - {{ openingBrace() }} + {{ openingBrace() }} } @for ( segment of segments(); @@ -17,6 +21,7 @@ ) {
@let expandable = isExpandable(segment); + @let empty = isEmpty(segment); @let openingBrace = openingBraceForSegment(segment); @let closingBrace = closingBraceForSegment(segment); @let clickableValue = isClickable(segment); @@ -32,8 +37,13 @@ } {{ `"${segment.key}"` }} - : - @if (!expandable || !segment.expanded) { + : + @if (empty) { + {{ `${openingBrace}${closingBrace}${i < len - 1 ? ',' : ''}` }} + } @else if (!expandable || !segment.expanded) { {{ segment.description }} - {{ i < len - 1 ? ',' : '' }} + {{ i < len - 1 ? ',' : '' }} } @else { @if (openingBrace) { - + {{ openingBrace }} } @@ -64,7 +74,7 @@ [_currentDepth]="_currentDepth() + 1" (onValueClick)="onValueClickHandler($event)" /> @if (closingBrace) { - {{ closingBrace }}{{ i < len - 1 ? ',' : '' }} } @@ -73,7 +83,7 @@
} @if (_currentDepth() === 0) { - {{ closingBrace() }} + {{ closingBrace() }} } }
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 22f9962..a433065 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 @@ -100,7 +100,7 @@ $type-colors: ( } } - .puctuation { + .punctuation { color: var(--ngx-json-punctuation, #000); } 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 584d5ff..6011a34 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 @@ -142,7 +142,9 @@ export class NgxJsonTreeviewComponent { return ']'; } else return '}'; }); - asString = computed(() => JSON.stringify(this.json(), null, 2)); + asString = computed(() => + JSON.stringify(this.json(), null, 2).trim() + ); isExpandable(segment: Segment) { return ( @@ -151,6 +153,13 @@ export class NgxJsonTreeviewComponent { ); } + isEmpty(segment: Segment) { + return ( + (segment.type === 'object' && Object.keys(segment.value).length === 0) || + (segment.type === 'array' && segment.value.length === 0) + ); + } + isClickable(segment: Segment) { return this.enableClickableValues() && this.isClickableValue()(segment); }