Skip to content

Commit ba4668b

Browse files
authored
update json widget to automatically scale (#345)
* updated json widget to scale 1-5 rows depending on input * scale json widget when copy pasting into empty field * make scrollbar more visible in json widget
1 parent 1de45ed commit ba4668b

4 files changed

Lines changed: 46 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

66
## [Unreleased]
7+
### Changed
8+
- Changed scaling behaviour for json widget. Depending on content, it scales to 1-5 rows initially. When the user pastes multi-line json it also rescales to fit the content. [#344](https://github.com/CCDirectLink/crosscode-map-editor/issues/344)
9+
710
## [2.0.1] 2026-03-16
811
### Fixed
912
- Added scrollbar back to character selector [#342](https://github.com/CCDirectLink/crosscode-map-editor/issues/342)

webapp/src/app/components/widgets/json-widget/json-widget.component.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
@if (!noPropName) {
33
<label class="property-name">{{ key }}:</label>
44
}
5-
<textarea class="default-input"
6-
[ngModel]="json.stringify(settings[key], null, 2)"
7-
(input)="setCustomSetting(key, $any($event.target).value)">
5+
<textarea
6+
class="default-input"
7+
[ngModel]="getJsonVal()"
8+
(input)="setCustomSetting(key, $any($event.target).value)"
9+
[rows]="rows()"
10+
>
811
</textarea>
912
<button class="mini-button" (click)="openJsonEditor()">+</button>
1013
</div>

webapp/src/app/components/widgets/json-widget/json-widget.component.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
.default-input {
22
background-color: rgba(83, 192, 255, 0.4);
3+
height: auto;
4+
5+
scrollbar-color: var(--gray-800) transparent;
36
}
47

58
.mini-button {

webapp/src/app/components/widgets/json-widget/json-widget.component.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
1-
import { Component, Input, inject } from '@angular/core';
1+
import { Component, inject, Input, signal } from '@angular/core';
22
import { MatDialog } from '@angular/material/dialog';
33
import { JsonEditorComponent } from '../../json-editor/json-editor.component';
44
import { AbstractWidget } from '../abstract-widget';
55
import { FlexModule } from '@angular/flex-layout/flex';
66
import { MatTooltip } from '@angular/material/tooltip';
77
import { FormsModule } from '@angular/forms';
8+
import { Helper } from '../../../services/phaser/helper';
89

910
@Component({
10-
selector: 'app-json-widget',
11-
templateUrl: './json-widget.component.html',
12-
styleUrls: ['./json-widget.component.scss', '../widget.scss'],
13-
imports: [FlexModule, MatTooltip, FormsModule]
11+
selector: 'app-json-widget',
12+
templateUrl: './json-widget.component.html',
13+
styleUrls: ['./json-widget.component.scss', '../widget.scss'],
14+
imports: [FlexModule, MatTooltip, FormsModule]
1415
})
1516
export class JsonWidgetComponent extends AbstractWidget {
1617
private dialog = inject(MatDialog);
17-
1818

1919
@Input() noPropName = false;
2020
private timer = -1;
21-
json = JSON;
21+
22+
rows = signal(1);
23+
24+
override ngOnInit() {
25+
super.ngOnInit();
26+
this.updateRows();
27+
}
28+
29+
private updateRows(newVal?: string) {
30+
const setting = this.getJsonVal(newVal);
31+
const rows = setting?.split('\n').length ?? 1;
32+
this.rows.set(Helper.clamp(rows, 1, 5));
33+
}
34+
35+
getJsonVal(newVal?: string) {
36+
return JSON.stringify(newVal ?? this.settings[this.key], null, 2);
37+
}
2238

2339
openJsonEditor() {
2440
const ref = this.dialog.open(JsonEditorComponent, {
@@ -39,10 +55,21 @@ export class JsonWidgetComponent extends AbstractWidget {
3955
if (this.timer >= 0) {
4056
clearTimeout(this.timer);
4157
}
58+
59+
// scales textarea when copy pasting into empty field
60+
try {
61+
const newVal = JSON.parse(value);
62+
const prev = this.settings[key];
63+
if (!prev && newVal) {
64+
this.updateRows(newVal);
65+
}
66+
} catch (e) {}
67+
4268
this.timer = window.setTimeout(() => {
4369
value = JSON.parse(value);
4470
this.settings[key] = value;
4571
this.updateType(value);
4672
}, 500);
4773
}
74+
4875
}

0 commit comments

Comments
 (0)