-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
documentcolorcollection.ts
93 lines (81 loc) · 2.58 KB
/
documentcolorcollection.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module ui/colorselector/documentcolorcollection
*/
import type { ColorDefinition } from '../colorgrid/colorgridview.js';
import {
Collection,
ObservableMixin,
type CollectionAddEvent,
type CollectionRemoveEvent,
type CollectionChangeEvent
} from '@ckeditor/ckeditor5-utils';
/**
* A collection to store document colors. It enforces colors to be unique.
*/
export default class DocumentColorCollection extends ObservableMixin( Collection<ColorDefinition> ) {
/**
* Indicates whether the document color collection is empty.
*
* @observable
*/
declare public readonly isEmpty: boolean;
constructor( options?: any ) {
super( options );
this.set( 'isEmpty', true );
this.on( 'change', () => {
this.set( 'isEmpty', this.length === 0 );
} );
}
/**
* Adds a color to the document color collection.
*
* This method ensures that no color duplicates are inserted (compared using
* the color value of the {@link module:ui/colorgrid/colorgridview~ColorDefinition}).
*
* If the item does not have an ID, it will be automatically generated and set on the item.
*
* @param index The position of the item in the collection. The item is pushed to the collection when `index` is not specified.
* @fires add
* @fires change
*/
public override add( item: ColorDefinition, index?: number ): this {
if ( this.find( element => element.color === item.color ) ) {
// No duplicates are allowed.
return this;
}
return super.add( item, index );
}
/**
* Checks if an object with given colors is present in the document color collection.
*/
public hasColor( color: string ): boolean {
return !!this.find( item => item.color === color );
}
}
/**
* Fired when the collection was changed due to adding or removing items.
*
* @eventName ~DocumentColorCollection#change
* @param data Changed items.
*/
export type DocumentColorCollectionChangeEvent = CollectionChangeEvent;
/**
* Fired when an item is added to the collection.
*
* @eventName ~DocumentColorCollection#add
* @param item The added item.
* @param index An index where the addition occurred.
*/
export type DocumentColorCollectionAddEvent = CollectionAddEvent;
/**
* Fired when an item is removed from the collection.
*
* @eventName ~DocumentColorCollection#remove
* @param item The removed item.
* @param index Index from which item was removed.
*/
export type DocumentColorCollectionRemoveEvent = CollectionRemoveEvent;