/
utils.ts
186 lines (165 loc) · 5.01 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* @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 font/fontsize/utils
*/
import { CKEditorError } from 'ckeditor5/src/utils.js';
import { type FontSizeOption } from '../fontconfig.js';
/**
* Normalizes and translates the {@link module:font/fontconfig~FontSizeConfig#options configuration options}
* to the {@link module:font/fontconfig~FontSizeOption} format.
*
* @param configuredOptions An array of options taken from the configuration.
*/
export function normalizeOptions( configuredOptions: Array<string | number | FontSizeOption> ): Array<FontSizeOption> {
// Convert options to objects.
return configuredOptions
.map( item => getOptionDefinition( item ) )
// Filter out undefined values that `getOptionDefinition` might return.
.filter( ( option ): option is FontSizeOption => option !== undefined );
}
// Default named presets map. Always create a new instance of the preset object in order to avoid modifying references.
const namedPresets: Record<string, FontSizeOption> = {
get tiny(): FontSizeOption {
return {
title: 'Tiny',
model: 'tiny',
view: {
name: 'span',
classes: 'text-tiny',
priority: 7
}
};
},
get small(): FontSizeOption {
return {
title: 'Small',
model: 'small',
view: {
name: 'span',
classes: 'text-small',
priority: 7
}
};
},
get big(): FontSizeOption {
return {
title: 'Big',
model: 'big',
view: {
name: 'span',
classes: 'text-big',
priority: 7
}
};
},
get huge(): FontSizeOption {
return {
title: 'Huge',
model: 'huge',
view: {
name: 'span',
classes: 'text-huge',
priority: 7
}
};
}
};
/**
* Returns an option definition either from preset or creates one from number shortcut.
* If object is passed then this method will return it without alternating it. Returns undefined for item than cannot be parsed.
*/
function getOptionDefinition( option: string | number | FontSizeOption ): FontSizeOption | undefined {
if ( typeof option === 'number' ) {
option = String( option );
}
// Check whether passed option is a full item definition provided by user in configuration.
if ( typeof option === 'object' && isFullItemDefinition( option ) ) {
return attachPriority( option );
}
const preset = findPreset( option );
// Item is a named preset.
if ( preset ) {
return attachPriority( preset );
}
// 'Default' font size. It will be used to remove the fontSize attribute.
if ( option === 'default' ) {
return {
model: undefined,
title: 'Default'
};
}
// At this stage we probably have numerical value to generate a preset so parse it's value.
// Discard any faulty values.
if ( isNumericalDefinition( option ) ) {
return undefined;
}
// Return font size definition from size value.
return generatePixelPreset( option );
}
/**
* Creates a predefined preset for pixel size.
* @param definition Font size in pixels.
* @returns
*/
function generatePixelPreset( definition: string | FontSizeOption ): FontSizeOption {
// Extend a short (numeric value) definition.
if ( typeof definition === 'string' ) {
definition = {
title: definition,
model: `${ parseFloat( definition ) }px`
};
}
definition.view = {
name: 'span',
styles: {
'font-size': definition.model!
}
};
return attachPriority( definition );
}
/**
* Adds the priority to the view element definition if missing. It's required due to ckeditor/ckeditor5#2291
*/
function attachPriority( definition: FontSizeOption ): FontSizeOption {
if ( definition.view && typeof definition.view !== 'string' && !definition.view.priority ) {
definition.view!.priority = 7;
}
return definition;
}
/**
* Returns a prepared preset definition. If passed an object, a name of preset should be defined as `model` value.
*
* @param definition.model A preset name.
*/
function findPreset( definition: string | { model?: string } ): FontSizeOption | undefined {
return typeof definition === 'string' ? namedPresets[ definition ] : namedPresets[ definition.model! ];
}
/**
* We treat `definition` as completed if it is an object that contains `title`, `model` and `view` values.
*/
function isFullItemDefinition( definition: Record<string, any> ): boolean {
return definition.title && definition.model && definition.view;
}
function isNumericalDefinition( definition: string | FontSizeOption ): boolean {
let numberValue;
if ( typeof definition === 'object' ) {
if ( !definition.model ) {
/**
* Provided value as an option for {@link module:font/fontsize~FontSize} seems to invalid.
*
* See valid examples described in the {@link module:font/fontconfig~FontSizeConfig#options plugin configuration}.
*
* @error font-size-invalid-definition
*/
throw new CKEditorError( 'font-size-invalid-definition', null, definition );
} else {
numberValue = parseFloat( definition.model );
}
} else {
numberValue = parseFloat( definition );
}
return isNaN( numberValue );
}