-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathjson-widget.component.ts
More file actions
75 lines (63 loc) · 1.91 KB
/
json-widget.component.ts
File metadata and controls
75 lines (63 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Component, inject, Input, signal } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { JsonEditorComponent } from '../../json-editor/json-editor.component';
import { AbstractWidget } from '../abstract-widget';
import { FlexModule } from '@angular/flex-layout/flex';
import { MatTooltip } from '@angular/material/tooltip';
import { FormsModule } from '@angular/forms';
import { Helper } from '../../../services/phaser/helper';
@Component({
selector: 'app-json-widget',
templateUrl: './json-widget.component.html',
styleUrls: ['./json-widget.component.scss', '../widget.scss'],
imports: [FlexModule, MatTooltip, FormsModule]
})
export class JsonWidgetComponent extends AbstractWidget {
private dialog = inject(MatDialog);
@Input() noPropName = false;
private timer = -1;
rows = signal(1);
override ngOnInit() {
super.ngOnInit();
this.updateRows();
}
private updateRows(newVal?: string) {
const setting = this.getJsonVal(newVal);
const rows = setting?.split('\n').length ?? 1;
this.rows.set(Helper.clamp(rows, 1, 5));
}
getJsonVal(newVal?: string) {
return JSON.stringify(newVal ?? this.settings[this.key], null, 2);
}
openJsonEditor() {
const ref = this.dialog.open(JsonEditorComponent, {
data: {
val: this.settings[this.key],
key: this.key
}
});
ref.afterClosed().subscribe((res: any) => {
if (res) {
this.setCustomSetting(this.key, JSON.stringify(res));
}
});
}
setCustomSetting(key: string, value: any) {
if (this.timer >= 0) {
clearTimeout(this.timer);
}
// scales textarea when copy pasting into empty field
try {
const newVal = JSON.parse(value);
const prev = this.settings[key];
if (!prev && newVal) {
this.updateRows(newVal);
}
} catch (e) {}
this.timer = window.setTimeout(() => {
value = JSON.parse(value);
this.settings[key] = value;
this.updateType(value);
}, 500);
}
}