1
1
import { DIALOG_DATA , DialogRef } from '@angular/cdk/dialog' ;
2
2
import { DecimalPipe } from '@angular/common' ;
3
- import { Component , ElementRef , computed , effect , inject , signal , viewChild } from '@angular/core' ;
3
+ import { Component , computed , effect , inject , signal } from '@angular/core' ;
4
4
import { TranslateModule , TranslateService } from '@ngx-translate/core' ;
5
5
import { ColorService } from '../shared/data-access/color.service' ;
6
6
import { Color , Shade } from '../shared/model' ;
7
7
import { ColorInputComponent } from '../shared/ui/color-input/color-input.component' ;
8
+ import { ColorRangeSliderComponent } from '../shared/ui/color-range-slider/color-range-slider.component' ;
8
9
import { textColor } from '../shared/utils/text-color' ;
9
- import { EditorRangeComponent } from './ui/editor-range/editor-range.component' ;
10
+
11
+ /**
12
+ * Data for the editor dialog.
13
+ */
14
+ export type EditorData = {
15
+ /**
16
+ * Color to edit.
17
+ */
18
+ color : Color ;
19
+ /**
20
+ * Shade to open the editor with.
21
+ */
22
+ shadeIndex ?: number ;
23
+ } ;
10
24
11
25
export enum UpdateType {
12
26
HEX = 'hex' ,
@@ -18,20 +32,25 @@ export enum UpdateType {
18
32
@Component ( {
19
33
selector : 'rp-editor' ,
20
34
standalone : true ,
21
- imports : [ ColorInputComponent , TranslateModule , DecimalPipe , EditorRangeComponent ] ,
22
- templateUrl : './editor.component.html'
35
+ imports : [ ColorInputComponent , TranslateModule , DecimalPipe , ColorRangeSliderComponent ] ,
36
+ templateUrl : './editor.component.html' ,
37
+ host : {
38
+ '[style.--editor-saturation]' : 'shade().hsl.S + "%"' ,
39
+ '[style.--editor-lightness]' : 'shade().hsl.L + "%"' ,
40
+ '[style.--editor-hue]' : 'shade().hsl.H'
41
+ }
23
42
} )
24
43
export class EditorComponent {
25
44
protected readonly UpdateType = UpdateType ;
26
45
protected readonly textColor = textColor ;
27
46
28
- private readonly _data = inject < { color : Color ; shadeIndex ?: number } > ( DIALOG_DATA ) ;
29
- private readonly _dialogRef = inject ( DialogRef ) ;
30
- private readonly _colorService = inject ( ColorService ) ;
31
- private readonly _translateService = inject ( TranslateService ) ;
47
+ readonly #data = inject < EditorData > ( DIALOG_DATA ) ;
48
+ readonly #dialogRef = inject ( DialogRef ) ;
49
+ readonly #colorService = inject ( ColorService ) ;
50
+ readonly #translateService = inject ( TranslateService ) ;
32
51
33
- protected readonly color = signal ( this . _data . color . copy ( ) ) ;
34
- protected readonly shadeIndex = signal ( this . _data . shadeIndex ?? 0 ) ;
52
+ protected readonly color = signal ( this . #data . color . copy ( ) ) ;
53
+ protected readonly shadeIndex = signal ( this . #data . shadeIndex ?? 0 ) ;
35
54
36
55
protected readonly shade = computed < Shade > ( ( ) => {
37
56
const selectedShade = this . color ( ) . shades . find ( ( shade ) => shade . index === this . shadeIndex ( ) ) ;
@@ -48,20 +67,12 @@ export class EditorComponent {
48
67
} ) ;
49
68
50
69
protected readonly hasUnsavedChanges = computed < boolean > (
51
- ( ) => this . _data . color . toString ( ) !== this . color ( ) . toString ( )
70
+ ( ) => this . #data . color . toString ( ) !== this . color ( ) . toString ( )
52
71
) ;
53
72
54
- private readonly _editor = viewChild . required < ElementRef < HTMLElement > > ( 'editor' ) ;
55
-
56
73
public constructor ( ) {
57
74
effect ( ( ) => {
58
- this . _editor ( ) . nativeElement . style . setProperty ( '--editor-hue' , `${ this . shade ( ) . hsl . H } ` ) ;
59
- this . _editor ( ) . nativeElement . style . setProperty ( '--editor-saturation' , `${ this . shade ( ) . hsl . S } %` ) ;
60
- this . _editor ( ) . nativeElement . style . setProperty ( '--editor-lightness' , `${ this . shade ( ) . hsl . L } %` ) ;
61
- } ) ;
62
-
63
- effect ( ( ) => {
64
- this . _dialogRef . disableClose = this . hasUnsavedChanges ( ) ;
75
+ this . #dialogRef. disableClose = this . hasUnsavedChanges ( ) ;
65
76
} ) ;
66
77
}
67
78
@@ -86,7 +97,7 @@ export class EditorComponent {
86
97
}
87
98
88
99
shade . fixed = false ;
89
- this . _updateColor ( ) ;
100
+ this . updateColor ( ) ;
90
101
}
91
102
92
103
public update ( type : UpdateType , value : string | number ) : void {
@@ -108,36 +119,36 @@ export class EditorComponent {
108
119
break ;
109
120
}
110
121
111
- this . _updateColor ( ) ;
122
+ this . updateColor ( ) ;
112
123
113
124
const editedShade = this . color ( ) . shades . find ( ( s ) => s . hex === shade . hex ) ;
114
125
this . shadeIndex . set ( editedShade ?. index ?? - 1 ) ;
115
126
}
116
127
117
128
protected cancel ( ) : void {
118
- this . _dialogRef . close ( undefined ) ;
119
- this . color . set ( this . _data . color . copy ( ) ) ;
129
+ this . #dialogRef . close ( undefined ) ;
130
+ this . color . set ( this . #data . color . copy ( ) ) ;
120
131
}
121
132
122
133
protected save ( ) : void {
123
- this . _dialogRef . close ( this . color ( ) ) ;
134
+ this . #dialogRef . close ( this . color ( ) ) ;
124
135
}
125
136
126
- private _updateColor ( ) : void {
137
+ private updateColor ( ) : void {
127
138
const updatedColor = this . color ( ) . copy ( ) ;
128
- this . _colorService . regenerateShades ( updatedColor ) ;
139
+ this . #colorService . regenerateShades ( updatedColor ) ;
129
140
this . color . set ( updatedColor ) ;
130
141
}
131
142
132
143
protected getTooltip ( shade : Shade , selected : boolean ) : string {
133
144
const tooltips : Array < string > = [ ] ;
134
145
135
146
if ( ! selected ) {
136
- tooltips . push ( this . _translateService . instant ( 'editor.shades' ) ) ;
147
+ tooltips . push ( this . #translateService . instant ( 'editor.shades' ) ) ;
137
148
}
138
149
139
150
if ( shade . fixed ) {
140
- tooltips . push ( this . _translateService . instant ( 'editor.unfix' ) ) ;
151
+ tooltips . push ( this . #translateService . instant ( 'editor.unfix' ) ) ;
141
152
}
142
153
143
154
return tooltips . join ( '\n' ) ;
0 commit comments