-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
utils.ts
116 lines (106 loc) · 3.05 KB
/
utils.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* @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/colorgrid/utils
*/
import type { Locale } from '@ckeditor/ckeditor5-utils';
export type ColorOption = string | {
color: string;
label?: string;
hasBorder?: boolean;
};
export interface NormalizedColorOption {
model: string;
label: string;
hasBorder: boolean;
view: {
name: string;
styles: {
color: string;
};
};
}
/**
* Returns color configuration options as defined in `editor.config.(fontColor|fontBackgroundColor).colors` or
* `editor.config.table.(tableProperties|tableCellProperties).(background|border).colors
* but processed to account for editor localization in the correct language.
*
* Note: The reason behind this method is that there is no way to use {@link module:utils/locale~Locale#t}
* when the user configuration is defined because the editor does not exist yet.
*
* @param locale The {@link module:core/editor/editor~Editor#locale} instance.
*/
export function getLocalizedColorOptions(
locale: Locale,
options: Array<NormalizedColorOption>
): Array<NormalizedColorOption> {
const t = locale.t;
const localizedColorNames: Record<string, string | undefined> = {
Black: t( 'Black' ),
'Dim grey': t( 'Dim grey' ),
Grey: t( 'Grey' ),
'Light grey': t( 'Light grey' ),
White: t( 'White' ),
Red: t( 'Red' ),
Orange: t( 'Orange' ),
Yellow: t( 'Yellow' ),
'Light green': t( 'Light green' ),
Green: t( 'Green' ),
Aquamarine: t( 'Aquamarine' ),
Turquoise: t( 'Turquoise' ),
'Light blue': t( 'Light blue' ),
Blue: t( 'Blue' ),
Purple: t( 'Purple' )
};
return options.map( colorOption => {
const label = localizedColorNames[ colorOption.label ];
if ( label && label != colorOption.label ) {
colorOption.label = label;
}
return colorOption;
} );
}
/**
* Creates a unified color definition object from color configuration options.
* The object contains the information necessary to both render the UI and initialize the conversion.
*/
export function normalizeColorOptions( options: Array<ColorOption> ): Array<NormalizedColorOption> {
return options
.map( normalizeSingleColorDefinition )
.filter( option => !!option );
}
/**
* Creates a normalized color definition from the user-defined configuration.
* The "normalization" means it will create full
* {@link module:ui/colorgrid/colorgridview~ColorDefinition `ColorDefinition-like`}
* object for string values, and add a `view` property, for each definition.
*/
export function normalizeSingleColorDefinition( color: ColorOption ): NormalizedColorOption {
if ( typeof color === 'string' ) {
return {
model: color,
label: color,
hasBorder: false,
view: {
name: 'span',
styles: {
color
}
}
};
} else {
return {
model: color.color,
label: color.label || color.color,
hasBorder: color.hasBorder === undefined ? false : color.hasBorder,
view: {
name: 'span',
styles: {
color: `${ color.color }`
}
}
};
}
}