-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
utils.ts
135 lines (117 loc) · 4.42 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* @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
*/
import { CKEditorError, logWarning, type Locale } from 'ckeditor5/src/utils.js';
import type { AlignmentFormat, SupportedOption } from './alignmentconfig.js';
/**
* @module alignment/utils
*/
/**
* The list of supported alignment options:
*
* * `'left'`,
* * `'right'`,
* * `'center'`,
* * `'justify'`
*/
export const supportedOptions: ReadonlyArray<SupportedOption> = [ 'left', 'right', 'center', 'justify' ];
/**
* Checks whether the passed option is supported by {@link module:alignment/alignmentediting~AlignmentEditing}.
*
* @param option The option value to check.
*/
export function isSupported( option: string ): boolean {
return ( supportedOptions as Array<string> ).includes( option );
}
/**
* Checks whether alignment is the default one considering the direction
* of the editor content.
*
* @param alignment The name of the alignment to check.
* @param locale The {@link module:core/editor/editor~Editor#locale} instance.
*/
export function isDefault( alignment: string, locale: Locale ): boolean {
// Right now only LTR is supported so the 'left' value is always the default one.
if ( locale.contentLanguageDirection == 'rtl' ) {
return alignment === 'right';
} else {
return alignment === 'left';
}
}
/**
* Brings the configuration to the common form, an array of objects.
*
* @param configuredOptions Alignment plugin configuration.
* @returns Normalized object holding the configuration.
*/
export function normalizeAlignmentOptions( configuredOptions: Array<string | AlignmentFormat> ): Array<AlignmentFormat> {
const normalizedOptions = configuredOptions
.map( option => {
let result;
if ( typeof option == 'string' ) {
result = { name: option };
} else {
result = option;
}
return result as AlignmentFormat;
} )
// Remove all unknown options.
.filter( option => {
const isNameValid = supportedOptions.includes( option.name );
if ( !isNameValid ) {
/**
* The `name` in one of the `alignment.options` is not recognized.
* The available options are: `'left'`, `'right'`, `'center'` and `'justify'`.
*
* @error alignment-config-name-not-recognized
* @param option Options with unknown value of the `name` property.
*/
logWarning( 'alignment-config-name-not-recognized', { option } );
}
return isNameValid;
} );
const classNameCount = normalizedOptions.filter( option => Boolean( option.className ) ).length;
// We either use classes for all styling options or for none.
if ( classNameCount && classNameCount < normalizedOptions.length ) {
/**
* The `className` property has to be defined for all options once at least one option declares `className`.
*
* @error alignment-config-classnames-are-missing
* @param configuredOptions Contents of `alignment.options`.
*/
throw new CKEditorError( 'alignment-config-classnames-are-missing', { configuredOptions } );
}
// Validate resulting config.
normalizedOptions.forEach( ( option, index, allOptions ) => {
const succeedingOptions = allOptions.slice( index + 1 );
const nameAlreadyExists = succeedingOptions.some( item => item.name == option.name );
if ( nameAlreadyExists ) {
/**
* The same `name` in one of the `alignment.options` was already declared.
* Each `name` representing one alignment option can be set exactly once.
*
* @error alignment-config-name-already-defined
* @param option First option that declares given `name`.
* @param configuredOptions Contents of `alignment.options`.
*/
throw new CKEditorError( 'alignment-config-name-already-defined', { option, configuredOptions } );
}
// The `className` property is present. Check for duplicates then.
if ( option.className ) {
const classNameAlreadyExists = succeedingOptions.some( item => item.className == option.className );
if ( classNameAlreadyExists ) {
/**
* The same `className` in one of the `alignment.options` was already declared.
*
* @error alignment-config-classname-already-defined
* @param option First option that declares given `className`.
* @param configuredOptions
* Contents of `alignment.options`.
*/
throw new CKEditorError( 'alignment-config-classname-already-defined', { option, configuredOptions } );
}
}
} );
return normalizedOptions;
}