1
1
import { Dialog } from '@angular/cdk/dialog' ;
2
2
import { Component , HostListener , OnInit , computed , inject , input , signal } from '@angular/core' ;
3
3
import { toObservable , toSignal } from '@angular/core/rxjs-interop' ;
4
+ import { Validators } from '@angular/forms' ;
4
5
import { Router , RouterLink } from '@angular/router' ;
5
6
import { NgIconComponent } from '@ng-icons/core' ;
6
7
import {
@@ -26,10 +27,12 @@ import { ToastService } from '../shared/data-access/toast.service';
26
27
import { TrackingEventAction , TrackingEventCategory } from '../shared/enums/tracking-event' ;
27
28
import { Color , Shade } from '../shared/model' ;
28
29
import { NoPaletteComponent } from '../shared/ui/no-palette/no-palette.component' ;
30
+ import { deduplicateName } from '../shared/utils/deduplicate-name' ;
29
31
import { IS_RUNNING_TEST } from '../shared/utils/is-running-test' ;
30
32
import { sleep } from '../shared/utils/sleep' ;
31
33
import { ImportColorData } from './ui/import-color/import-color.component' ;
32
34
import { ViewPaletteComponent } from './ui/view-palette/view-palette.component' ;
35
+ import { duplicateValidator } from './utils/duplicate.validator' ;
33
36
import { UnsavedChangesComponent } from './utils/unsaved-changes.guard' ;
34
37
35
38
@Component ( {
@@ -183,13 +186,26 @@ export default class ViewComponent implements OnInit, UnsavedChangesComponent {
183
186
}
184
187
185
188
public async renameColor ( color : Color ) : Promise < void > {
189
+ // Get all color names except the current one
190
+ const colorNames =
191
+ this . palette ( )
192
+ ?. colors . filter ( ( c ) => c !== color )
193
+ . map ( ( c ) => c . name ) ?? [ ] ;
194
+
186
195
const newName = await this . _dialogService . prompt ( {
187
196
title : 'common.rename' ,
188
197
message : 'view.color.rename' ,
189
198
confirmLabel : 'common.rename' ,
190
199
initialValue : color . name ,
191
200
label : 'common.name' ,
192
- placeholder : 'common.name'
201
+ placeholder : 'common.name' ,
202
+ validation : {
203
+ validators : [ Validators . required , duplicateValidator ( colorNames ) ] ,
204
+ errorMessageKeys : {
205
+ required : 'common.required' ,
206
+ duplicate : 'view.color.duplicate-name'
207
+ }
208
+ }
193
209
} ) ;
194
210
195
211
if ( ! newName || newName === color . name ) {
@@ -242,13 +258,23 @@ export default class ViewComponent implements OnInit, UnsavedChangesComponent {
242
258
}
243
259
244
260
public async addColor ( ) : Promise < void > {
261
+ // Check if a palette exists
245
262
const palette = this . palette ( ) ;
246
263
if ( ! palette ) {
247
264
return ;
248
265
}
249
266
267
+ // Create a new random color
250
268
const color = await this . _colorService . randomColor ( ) ;
269
+
270
+ // Check if color name already exists
271
+ const colorNames = palette . colors . map ( ( c ) => c . name ) ;
272
+ color . name = deduplicateName ( color . name , colorNames ) ;
273
+
274
+ // Add the color to the palette
251
275
palette . addColor ( color ) ;
276
+
277
+ // Set unsaved changes
252
278
this . _hasUnsavedChanges . set ( true ) ;
253
279
}
254
280
0 commit comments