@ckeditor/ckeditor5-font 39.0.2 → 40.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/font.js.map +1 -0
- package/package.json +2 -2
- package/src/augmentation.d.ts +58 -58
- package/src/augmentation.js +5 -5
- package/src/font.d.ts +33 -33
- package/src/font.js +37 -37
- package/src/fontbackgroundcolor/fontbackgroundcolorcommand.d.ts +26 -26
- package/src/fontbackgroundcolor/fontbackgroundcolorcommand.js +25 -25
- package/src/fontbackgroundcolor/fontbackgroundcolorediting.d.ts +26 -26
- package/src/fontbackgroundcolor/fontbackgroundcolorediting.js +123 -123
- package/src/fontbackgroundcolor/fontbackgroundcolorui.d.ts +22 -22
- package/src/fontbackgroundcolor/fontbackgroundcolorui.js +33 -33
- package/src/fontbackgroundcolor.d.ts +30 -30
- package/src/fontbackgroundcolor.js +34 -34
- package/src/fontcolor/fontcolorcommand.d.ts +25 -25
- package/src/fontcolor/fontcolorcommand.js +24 -24
- package/src/fontcolor/fontcolorediting.d.ts +26 -26
- package/src/fontcolor/fontcolorediting.js +134 -134
- package/src/fontcolor/fontcolorui.d.ts +22 -22
- package/src/fontcolor/fontcolorui.js +33 -33
- package/src/fontcolor.d.ts +29 -29
- package/src/fontcolor.js +33 -33
- package/src/fontcommand.d.ts +48 -48
- package/src/fontcommand.js +79 -79
- package/src/fontconfig.d.ts +373 -373
- package/src/fontconfig.js +5 -5
- package/src/fontfamily/fontfamilycommand.d.ts +25 -25
- package/src/fontfamily/fontfamilycommand.js +24 -24
- package/src/fontfamily/fontfamilyediting.d.ts +39 -39
- package/src/fontfamily/fontfamilyediting.js +115 -115
- package/src/fontfamily/fontfamilyui.d.ts +30 -30
- package/src/fontfamily/fontfamilyui.js +114 -114
- package/src/fontfamily/utils.d.ts +15 -15
- package/src/fontfamily/utils.js +80 -80
- package/src/fontfamily.d.ts +29 -29
- package/src/fontfamily.js +33 -33
- package/src/fontsize/fontsizecommand.d.ts +25 -25
- package/src/fontsize/fontsizecommand.js +24 -24
- package/src/fontsize/fontsizeediting.d.ts +44 -44
- package/src/fontsize/fontsizeediting.js +165 -165
- package/src/fontsize/fontsizeui.d.ts +31 -31
- package/src/fontsize/fontsizeui.js +122 -122
- package/src/fontsize/utils.d.ts +12 -12
- package/src/fontsize/utils.js +166 -166
- package/src/fontsize.d.ts +37 -37
- package/src/fontsize.js +43 -43
- package/src/index.d.ts +27 -27
- package/src/index.js +21 -21
- package/src/ui/colorui.d.ts +68 -68
- package/src/ui/colorui.js +130 -130
- package/src/utils.d.ts +80 -80
- package/src/utils.js +97 -97
|
@@ -1,122 +1,122 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module font/fontsize/fontsizeui
|
|
7
|
-
*/
|
|
8
|
-
import { Plugin } from 'ckeditor5/src/core';
|
|
9
|
-
import { Model, createDropdown, addListToDropdown } from 'ckeditor5/src/ui';
|
|
10
|
-
import { Collection } from 'ckeditor5/src/utils';
|
|
11
|
-
import { normalizeOptions } from './utils';
|
|
12
|
-
import { FONT_SIZE } from '../utils';
|
|
13
|
-
import '../../theme/fontsize.css';
|
|
14
|
-
import fontSizeIcon from '../../theme/icons/font-size.svg';
|
|
15
|
-
/**
|
|
16
|
-
* The font size UI plugin. It introduces the `'fontSize'` dropdown.
|
|
17
|
-
*/
|
|
18
|
-
export default class FontSizeUI extends Plugin {
|
|
19
|
-
/**
|
|
20
|
-
* @inheritDoc
|
|
21
|
-
*/
|
|
22
|
-
static get pluginName() {
|
|
23
|
-
return 'FontSizeUI';
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* @inheritDoc
|
|
27
|
-
*/
|
|
28
|
-
init() {
|
|
29
|
-
const editor = this.editor;
|
|
30
|
-
const t = editor.t;
|
|
31
|
-
const options = this._getLocalizedOptions();
|
|
32
|
-
const command = editor.commands.get(FONT_SIZE);
|
|
33
|
-
const accessibleLabel = t('Font Size');
|
|
34
|
-
// Register UI component.
|
|
35
|
-
editor.ui.componentFactory.add(FONT_SIZE, locale => {
|
|
36
|
-
const dropdownView = createDropdown(locale);
|
|
37
|
-
addListToDropdown(dropdownView, () => _prepareListOptions(options, command), {
|
|
38
|
-
role: 'menu',
|
|
39
|
-
ariaLabel: accessibleLabel
|
|
40
|
-
});
|
|
41
|
-
// Create dropdown model.
|
|
42
|
-
dropdownView.buttonView.set({
|
|
43
|
-
label: accessibleLabel,
|
|
44
|
-
icon: fontSizeIcon,
|
|
45
|
-
tooltip: true
|
|
46
|
-
});
|
|
47
|
-
dropdownView.extendTemplate({
|
|
48
|
-
attributes: {
|
|
49
|
-
class: [
|
|
50
|
-
'ck-font-size-dropdown'
|
|
51
|
-
]
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
dropdownView.bind('isEnabled').to(command);
|
|
55
|
-
// Execute command when an item from the dropdown is selected.
|
|
56
|
-
this.listenTo(dropdownView, 'execute', evt => {
|
|
57
|
-
editor.execute(evt.source.commandName, { value: evt.source.commandParam });
|
|
58
|
-
editor.editing.view.focus();
|
|
59
|
-
});
|
|
60
|
-
return dropdownView;
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Returns options as defined in `config.fontSize.options` but processed to account for
|
|
65
|
-
* editor localization, i.e. to display {@link module:font/fontconfig~FontSizeOption}
|
|
66
|
-
* in the correct language.
|
|
67
|
-
*
|
|
68
|
-
* Note: The reason behind this method is that there is no way to use {@link module:utils/locale~Locale#t}
|
|
69
|
-
* when the user configuration is defined because the editor does not exist yet.
|
|
70
|
-
*/
|
|
71
|
-
_getLocalizedOptions() {
|
|
72
|
-
const editor = this.editor;
|
|
73
|
-
const t = editor.t;
|
|
74
|
-
const localizedTitles = {
|
|
75
|
-
Default: t('Default'),
|
|
76
|
-
Tiny: t('Tiny'),
|
|
77
|
-
Small: t('Small'),
|
|
78
|
-
Big: t('Big'),
|
|
79
|
-
Huge: t('Huge')
|
|
80
|
-
};
|
|
81
|
-
const options = normalizeOptions((editor.config.get(FONT_SIZE)).options);
|
|
82
|
-
return options.map(option => {
|
|
83
|
-
const title = localizedTitles[option.title];
|
|
84
|
-
if (title && title != option.title) {
|
|
85
|
-
// Clone the option to avoid altering the original `namedPresets` from `./utils.js`.
|
|
86
|
-
option = Object.assign({}, option, { title });
|
|
87
|
-
}
|
|
88
|
-
return option;
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Prepares FontSize dropdown items.
|
|
94
|
-
*/
|
|
95
|
-
function _prepareListOptions(options, command) {
|
|
96
|
-
const itemDefinitions = new Collection();
|
|
97
|
-
for (const option of options) {
|
|
98
|
-
const def = {
|
|
99
|
-
type: 'button',
|
|
100
|
-
model: new Model({
|
|
101
|
-
commandName: FONT_SIZE,
|
|
102
|
-
commandParam: option.model,
|
|
103
|
-
label: option.title,
|
|
104
|
-
class: 'ck-fontsize-option',
|
|
105
|
-
role: 'menuitemradio',
|
|
106
|
-
withText: true
|
|
107
|
-
})
|
|
108
|
-
};
|
|
109
|
-
if (option.view && typeof option.view !== 'string') {
|
|
110
|
-
if (option.view.styles) {
|
|
111
|
-
def.model.set('labelStyle', `font-size:${option.view.styles['font-size']}`);
|
|
112
|
-
}
|
|
113
|
-
if (option.view.classes) {
|
|
114
|
-
def.model.set('class', `${def.model.class} ${option.view.classes}`);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
def.model.bind('isOn').to(command, 'value', value => value === option.model);
|
|
118
|
-
// Add the option to the collection.
|
|
119
|
-
itemDefinitions.add(def);
|
|
120
|
-
}
|
|
121
|
-
return itemDefinitions;
|
|
122
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module font/fontsize/fontsizeui
|
|
7
|
+
*/
|
|
8
|
+
import { Plugin } from 'ckeditor5/src/core';
|
|
9
|
+
import { Model, createDropdown, addListToDropdown } from 'ckeditor5/src/ui';
|
|
10
|
+
import { Collection } from 'ckeditor5/src/utils';
|
|
11
|
+
import { normalizeOptions } from './utils';
|
|
12
|
+
import { FONT_SIZE } from '../utils';
|
|
13
|
+
import '../../theme/fontsize.css';
|
|
14
|
+
import fontSizeIcon from '../../theme/icons/font-size.svg';
|
|
15
|
+
/**
|
|
16
|
+
* The font size UI plugin. It introduces the `'fontSize'` dropdown.
|
|
17
|
+
*/
|
|
18
|
+
export default class FontSizeUI extends Plugin {
|
|
19
|
+
/**
|
|
20
|
+
* @inheritDoc
|
|
21
|
+
*/
|
|
22
|
+
static get pluginName() {
|
|
23
|
+
return 'FontSizeUI';
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* @inheritDoc
|
|
27
|
+
*/
|
|
28
|
+
init() {
|
|
29
|
+
const editor = this.editor;
|
|
30
|
+
const t = editor.t;
|
|
31
|
+
const options = this._getLocalizedOptions();
|
|
32
|
+
const command = editor.commands.get(FONT_SIZE);
|
|
33
|
+
const accessibleLabel = t('Font Size');
|
|
34
|
+
// Register UI component.
|
|
35
|
+
editor.ui.componentFactory.add(FONT_SIZE, locale => {
|
|
36
|
+
const dropdownView = createDropdown(locale);
|
|
37
|
+
addListToDropdown(dropdownView, () => _prepareListOptions(options, command), {
|
|
38
|
+
role: 'menu',
|
|
39
|
+
ariaLabel: accessibleLabel
|
|
40
|
+
});
|
|
41
|
+
// Create dropdown model.
|
|
42
|
+
dropdownView.buttonView.set({
|
|
43
|
+
label: accessibleLabel,
|
|
44
|
+
icon: fontSizeIcon,
|
|
45
|
+
tooltip: true
|
|
46
|
+
});
|
|
47
|
+
dropdownView.extendTemplate({
|
|
48
|
+
attributes: {
|
|
49
|
+
class: [
|
|
50
|
+
'ck-font-size-dropdown'
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
dropdownView.bind('isEnabled').to(command);
|
|
55
|
+
// Execute command when an item from the dropdown is selected.
|
|
56
|
+
this.listenTo(dropdownView, 'execute', evt => {
|
|
57
|
+
editor.execute(evt.source.commandName, { value: evt.source.commandParam });
|
|
58
|
+
editor.editing.view.focus();
|
|
59
|
+
});
|
|
60
|
+
return dropdownView;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns options as defined in `config.fontSize.options` but processed to account for
|
|
65
|
+
* editor localization, i.e. to display {@link module:font/fontconfig~FontSizeOption}
|
|
66
|
+
* in the correct language.
|
|
67
|
+
*
|
|
68
|
+
* Note: The reason behind this method is that there is no way to use {@link module:utils/locale~Locale#t}
|
|
69
|
+
* when the user configuration is defined because the editor does not exist yet.
|
|
70
|
+
*/
|
|
71
|
+
_getLocalizedOptions() {
|
|
72
|
+
const editor = this.editor;
|
|
73
|
+
const t = editor.t;
|
|
74
|
+
const localizedTitles = {
|
|
75
|
+
Default: t('Default'),
|
|
76
|
+
Tiny: t('Tiny'),
|
|
77
|
+
Small: t('Small'),
|
|
78
|
+
Big: t('Big'),
|
|
79
|
+
Huge: t('Huge')
|
|
80
|
+
};
|
|
81
|
+
const options = normalizeOptions((editor.config.get(FONT_SIZE)).options);
|
|
82
|
+
return options.map(option => {
|
|
83
|
+
const title = localizedTitles[option.title];
|
|
84
|
+
if (title && title != option.title) {
|
|
85
|
+
// Clone the option to avoid altering the original `namedPresets` from `./utils.js`.
|
|
86
|
+
option = Object.assign({}, option, { title });
|
|
87
|
+
}
|
|
88
|
+
return option;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Prepares FontSize dropdown items.
|
|
94
|
+
*/
|
|
95
|
+
function _prepareListOptions(options, command) {
|
|
96
|
+
const itemDefinitions = new Collection();
|
|
97
|
+
for (const option of options) {
|
|
98
|
+
const def = {
|
|
99
|
+
type: 'button',
|
|
100
|
+
model: new Model({
|
|
101
|
+
commandName: FONT_SIZE,
|
|
102
|
+
commandParam: option.model,
|
|
103
|
+
label: option.title,
|
|
104
|
+
class: 'ck-fontsize-option',
|
|
105
|
+
role: 'menuitemradio',
|
|
106
|
+
withText: true
|
|
107
|
+
})
|
|
108
|
+
};
|
|
109
|
+
if (option.view && typeof option.view !== 'string') {
|
|
110
|
+
if (option.view.styles) {
|
|
111
|
+
def.model.set('labelStyle', `font-size:${option.view.styles['font-size']}`);
|
|
112
|
+
}
|
|
113
|
+
if (option.view.classes) {
|
|
114
|
+
def.model.set('class', `${def.model.class} ${option.view.classes}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
def.model.bind('isOn').to(command, 'value', value => value === option.model);
|
|
118
|
+
// Add the option to the collection.
|
|
119
|
+
itemDefinitions.add(def);
|
|
120
|
+
}
|
|
121
|
+
return itemDefinitions;
|
|
122
|
+
}
|
package/src/fontsize/utils.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
import { type FontSizeOption } from '../fontconfig';
|
|
6
|
-
/**
|
|
7
|
-
* Normalizes and translates the {@link module:font/fontconfig~FontSizeConfig#options configuration options}
|
|
8
|
-
* to the {@link module:font/fontconfig~FontSizeOption} format.
|
|
9
|
-
*
|
|
10
|
-
* @param configuredOptions An array of options taken from the configuration.
|
|
11
|
-
*/
|
|
12
|
-
export declare function normalizeOptions(configuredOptions: Array<string | number | FontSizeOption>): Array<FontSizeOption>;
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
import { type FontSizeOption } from '../fontconfig';
|
|
6
|
+
/**
|
|
7
|
+
* Normalizes and translates the {@link module:font/fontconfig~FontSizeConfig#options configuration options}
|
|
8
|
+
* to the {@link module:font/fontconfig~FontSizeOption} format.
|
|
9
|
+
*
|
|
10
|
+
* @param configuredOptions An array of options taken from the configuration.
|
|
11
|
+
*/
|
|
12
|
+
export declare function normalizeOptions(configuredOptions: Array<string | number | FontSizeOption>): Array<FontSizeOption>;
|
package/src/fontsize/utils.js
CHANGED
|
@@ -1,166 +1,166 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module font/fontsize/utils
|
|
7
|
-
*/
|
|
8
|
-
import { CKEditorError } from 'ckeditor5/src/utils';
|
|
9
|
-
/**
|
|
10
|
-
* Normalizes and translates the {@link module:font/fontconfig~FontSizeConfig#options configuration options}
|
|
11
|
-
* to the {@link module:font/fontconfig~FontSizeOption} format.
|
|
12
|
-
*
|
|
13
|
-
* @param configuredOptions An array of options taken from the configuration.
|
|
14
|
-
*/
|
|
15
|
-
export function normalizeOptions(configuredOptions) {
|
|
16
|
-
// Convert options to objects.
|
|
17
|
-
return configuredOptions
|
|
18
|
-
.map(item => getOptionDefinition(item))
|
|
19
|
-
// Filter out undefined values that `getOptionDefinition` might return.
|
|
20
|
-
.filter((option) => option !== undefined);
|
|
21
|
-
}
|
|
22
|
-
// Default named presets map. Always create a new instance of the preset object in order to avoid modifying references.
|
|
23
|
-
const namedPresets = {
|
|
24
|
-
get tiny() {
|
|
25
|
-
return {
|
|
26
|
-
title: 'Tiny',
|
|
27
|
-
model: 'tiny',
|
|
28
|
-
view: {
|
|
29
|
-
name: 'span',
|
|
30
|
-
classes: 'text-tiny',
|
|
31
|
-
priority: 7
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
},
|
|
35
|
-
get small() {
|
|
36
|
-
return {
|
|
37
|
-
title: 'Small',
|
|
38
|
-
model: 'small',
|
|
39
|
-
view: {
|
|
40
|
-
name: 'span',
|
|
41
|
-
classes: 'text-small',
|
|
42
|
-
priority: 7
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
},
|
|
46
|
-
get big() {
|
|
47
|
-
return {
|
|
48
|
-
title: 'Big',
|
|
49
|
-
model: 'big',
|
|
50
|
-
view: {
|
|
51
|
-
name: 'span',
|
|
52
|
-
classes: 'text-big',
|
|
53
|
-
priority: 7
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
},
|
|
57
|
-
get huge() {
|
|
58
|
-
return {
|
|
59
|
-
title: 'Huge',
|
|
60
|
-
model: 'huge',
|
|
61
|
-
view: {
|
|
62
|
-
name: 'span',
|
|
63
|
-
classes: 'text-huge',
|
|
64
|
-
priority: 7
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
/**
|
|
70
|
-
* Returns an option definition either from preset or creates one from number shortcut.
|
|
71
|
-
* If object is passed then this method will return it without alternating it. Returns undefined for item than cannot be parsed.
|
|
72
|
-
*/
|
|
73
|
-
function getOptionDefinition(option) {
|
|
74
|
-
if (typeof option === 'number') {
|
|
75
|
-
option = String(option);
|
|
76
|
-
}
|
|
77
|
-
// Check whether passed option is a full item definition provided by user in configuration.
|
|
78
|
-
if (typeof option === 'object' && isFullItemDefinition(option)) {
|
|
79
|
-
return attachPriority(option);
|
|
80
|
-
}
|
|
81
|
-
const preset = findPreset(option);
|
|
82
|
-
// Item is a named preset.
|
|
83
|
-
if (preset) {
|
|
84
|
-
return attachPriority(preset);
|
|
85
|
-
}
|
|
86
|
-
// 'Default' font size. It will be used to remove the fontSize attribute.
|
|
87
|
-
if (option === 'default') {
|
|
88
|
-
return {
|
|
89
|
-
model: undefined,
|
|
90
|
-
title: 'Default'
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
// At this stage we probably have numerical value to generate a preset so parse it's value.
|
|
94
|
-
// Discard any faulty values.
|
|
95
|
-
if (isNumericalDefinition(option)) {
|
|
96
|
-
return undefined;
|
|
97
|
-
}
|
|
98
|
-
// Return font size definition from size value.
|
|
99
|
-
return generatePixelPreset(option);
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Creates a predefined preset for pixel size.
|
|
103
|
-
* @param definition Font size in pixels.
|
|
104
|
-
* @returns
|
|
105
|
-
*/
|
|
106
|
-
function generatePixelPreset(definition) {
|
|
107
|
-
// Extend a short (numeric value) definition.
|
|
108
|
-
if (typeof definition === 'string') {
|
|
109
|
-
definition = {
|
|
110
|
-
title: definition,
|
|
111
|
-
model: `${parseFloat(definition)}px`
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
definition.view = {
|
|
115
|
-
name: 'span',
|
|
116
|
-
styles: {
|
|
117
|
-
'font-size': definition.model
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
return attachPriority(definition);
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Adds the priority to the view element definition if missing. It's required due to ckeditor/ckeditor5#2291
|
|
124
|
-
*/
|
|
125
|
-
function attachPriority(definition) {
|
|
126
|
-
if (definition.view && typeof definition.view !== 'string' && !definition.view.priority) {
|
|
127
|
-
definition.view.priority = 7;
|
|
128
|
-
}
|
|
129
|
-
return definition;
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Returns a prepared preset definition. If passed an object, a name of preset should be defined as `model` value.
|
|
133
|
-
*
|
|
134
|
-
* @param definition.model A preset name.
|
|
135
|
-
*/
|
|
136
|
-
function findPreset(definition) {
|
|
137
|
-
return typeof definition === 'string' ? namedPresets[definition] : namedPresets[definition.model];
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* We treat `definition` as completed if it is an object that contains `title`, `model` and `view` values.
|
|
141
|
-
*/
|
|
142
|
-
function isFullItemDefinition(definition) {
|
|
143
|
-
return definition.title && definition.model && definition.view;
|
|
144
|
-
}
|
|
145
|
-
function isNumericalDefinition(definition) {
|
|
146
|
-
let numberValue;
|
|
147
|
-
if (typeof definition === 'object') {
|
|
148
|
-
if (!definition.model) {
|
|
149
|
-
/**
|
|
150
|
-
* Provided value as an option for {@link module:font/fontsize~FontSize} seems to invalid.
|
|
151
|
-
*
|
|
152
|
-
* See valid examples described in the {@link module:font/fontconfig~FontSizeConfig#options plugin configuration}.
|
|
153
|
-
*
|
|
154
|
-
* @error font-size-invalid-definition
|
|
155
|
-
*/
|
|
156
|
-
throw new CKEditorError('font-size-invalid-definition', null, definition);
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
numberValue = parseFloat(definition.model);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
numberValue = parseFloat(definition);
|
|
164
|
-
}
|
|
165
|
-
return isNaN(numberValue);
|
|
166
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module font/fontsize/utils
|
|
7
|
+
*/
|
|
8
|
+
import { CKEditorError } from 'ckeditor5/src/utils';
|
|
9
|
+
/**
|
|
10
|
+
* Normalizes and translates the {@link module:font/fontconfig~FontSizeConfig#options configuration options}
|
|
11
|
+
* to the {@link module:font/fontconfig~FontSizeOption} format.
|
|
12
|
+
*
|
|
13
|
+
* @param configuredOptions An array of options taken from the configuration.
|
|
14
|
+
*/
|
|
15
|
+
export function normalizeOptions(configuredOptions) {
|
|
16
|
+
// Convert options to objects.
|
|
17
|
+
return configuredOptions
|
|
18
|
+
.map(item => getOptionDefinition(item))
|
|
19
|
+
// Filter out undefined values that `getOptionDefinition` might return.
|
|
20
|
+
.filter((option) => option !== undefined);
|
|
21
|
+
}
|
|
22
|
+
// Default named presets map. Always create a new instance of the preset object in order to avoid modifying references.
|
|
23
|
+
const namedPresets = {
|
|
24
|
+
get tiny() {
|
|
25
|
+
return {
|
|
26
|
+
title: 'Tiny',
|
|
27
|
+
model: 'tiny',
|
|
28
|
+
view: {
|
|
29
|
+
name: 'span',
|
|
30
|
+
classes: 'text-tiny',
|
|
31
|
+
priority: 7
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
get small() {
|
|
36
|
+
return {
|
|
37
|
+
title: 'Small',
|
|
38
|
+
model: 'small',
|
|
39
|
+
view: {
|
|
40
|
+
name: 'span',
|
|
41
|
+
classes: 'text-small',
|
|
42
|
+
priority: 7
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
get big() {
|
|
47
|
+
return {
|
|
48
|
+
title: 'Big',
|
|
49
|
+
model: 'big',
|
|
50
|
+
view: {
|
|
51
|
+
name: 'span',
|
|
52
|
+
classes: 'text-big',
|
|
53
|
+
priority: 7
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
get huge() {
|
|
58
|
+
return {
|
|
59
|
+
title: 'Huge',
|
|
60
|
+
model: 'huge',
|
|
61
|
+
view: {
|
|
62
|
+
name: 'span',
|
|
63
|
+
classes: 'text-huge',
|
|
64
|
+
priority: 7
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Returns an option definition either from preset or creates one from number shortcut.
|
|
71
|
+
* If object is passed then this method will return it without alternating it. Returns undefined for item than cannot be parsed.
|
|
72
|
+
*/
|
|
73
|
+
function getOptionDefinition(option) {
|
|
74
|
+
if (typeof option === 'number') {
|
|
75
|
+
option = String(option);
|
|
76
|
+
}
|
|
77
|
+
// Check whether passed option is a full item definition provided by user in configuration.
|
|
78
|
+
if (typeof option === 'object' && isFullItemDefinition(option)) {
|
|
79
|
+
return attachPriority(option);
|
|
80
|
+
}
|
|
81
|
+
const preset = findPreset(option);
|
|
82
|
+
// Item is a named preset.
|
|
83
|
+
if (preset) {
|
|
84
|
+
return attachPriority(preset);
|
|
85
|
+
}
|
|
86
|
+
// 'Default' font size. It will be used to remove the fontSize attribute.
|
|
87
|
+
if (option === 'default') {
|
|
88
|
+
return {
|
|
89
|
+
model: undefined,
|
|
90
|
+
title: 'Default'
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
// At this stage we probably have numerical value to generate a preset so parse it's value.
|
|
94
|
+
// Discard any faulty values.
|
|
95
|
+
if (isNumericalDefinition(option)) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
// Return font size definition from size value.
|
|
99
|
+
return generatePixelPreset(option);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Creates a predefined preset for pixel size.
|
|
103
|
+
* @param definition Font size in pixels.
|
|
104
|
+
* @returns
|
|
105
|
+
*/
|
|
106
|
+
function generatePixelPreset(definition) {
|
|
107
|
+
// Extend a short (numeric value) definition.
|
|
108
|
+
if (typeof definition === 'string') {
|
|
109
|
+
definition = {
|
|
110
|
+
title: definition,
|
|
111
|
+
model: `${parseFloat(definition)}px`
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
definition.view = {
|
|
115
|
+
name: 'span',
|
|
116
|
+
styles: {
|
|
117
|
+
'font-size': definition.model
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
return attachPriority(definition);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Adds the priority to the view element definition if missing. It's required due to ckeditor/ckeditor5#2291
|
|
124
|
+
*/
|
|
125
|
+
function attachPriority(definition) {
|
|
126
|
+
if (definition.view && typeof definition.view !== 'string' && !definition.view.priority) {
|
|
127
|
+
definition.view.priority = 7;
|
|
128
|
+
}
|
|
129
|
+
return definition;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Returns a prepared preset definition. If passed an object, a name of preset should be defined as `model` value.
|
|
133
|
+
*
|
|
134
|
+
* @param definition.model A preset name.
|
|
135
|
+
*/
|
|
136
|
+
function findPreset(definition) {
|
|
137
|
+
return typeof definition === 'string' ? namedPresets[definition] : namedPresets[definition.model];
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* We treat `definition` as completed if it is an object that contains `title`, `model` and `view` values.
|
|
141
|
+
*/
|
|
142
|
+
function isFullItemDefinition(definition) {
|
|
143
|
+
return definition.title && definition.model && definition.view;
|
|
144
|
+
}
|
|
145
|
+
function isNumericalDefinition(definition) {
|
|
146
|
+
let numberValue;
|
|
147
|
+
if (typeof definition === 'object') {
|
|
148
|
+
if (!definition.model) {
|
|
149
|
+
/**
|
|
150
|
+
* Provided value as an option for {@link module:font/fontsize~FontSize} seems to invalid.
|
|
151
|
+
*
|
|
152
|
+
* See valid examples described in the {@link module:font/fontconfig~FontSizeConfig#options plugin configuration}.
|
|
153
|
+
*
|
|
154
|
+
* @error font-size-invalid-definition
|
|
155
|
+
*/
|
|
156
|
+
throw new CKEditorError('font-size-invalid-definition', null, definition);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
numberValue = parseFloat(definition.model);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
numberValue = parseFloat(definition);
|
|
164
|
+
}
|
|
165
|
+
return isNaN(numberValue);
|
|
166
|
+
}
|