/
fontsizeui.js
142 lines (116 loc) · 3.82 KB
/
fontsizeui.js
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
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module font/fontsize/fontsizeui
*/
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import Model from '@ckeditor/ckeditor5-ui/src/model';
import Collection from '@ckeditor/ckeditor5-utils/src/collection';
import { createDropdown, addListToDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
import { normalizeOptions } from './utils';
import { FONT_SIZE } from '../utils';
import fontSizeIcon from '../../theme/icons/font-size.svg';
import '../../theme/fontsize.css';
/**
* The font size UI plugin. It introduces the `'fontSize'` dropdown.
*
* @extends module:core/plugin~Plugin
*/
export default class FontSizeUI extends Plugin {
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const t = editor.t;
const options = this._getLocalizedOptions();
const command = editor.commands.get( FONT_SIZE );
// Register UI component.
editor.ui.componentFactory.add( FONT_SIZE, locale => {
const dropdownView = createDropdown( locale );
addListToDropdown( dropdownView, _prepareListOptions( options, command ) );
// Create dropdown model.
dropdownView.buttonView.set( {
label: t( 'Font Size' ),
icon: fontSizeIcon,
tooltip: true
} );
dropdownView.extendTemplate( {
attributes: {
class: [
'ck-font-size-dropdown'
]
}
} );
dropdownView.bind( 'isEnabled' ).to( command );
// Execute command when an item from the dropdown is selected.
this.listenTo( dropdownView, 'execute', evt => {
editor.execute( evt.source.commandName, { value: evt.source.commandParam } );
editor.editing.view.focus();
} );
return dropdownView;
} );
}
/**
* Returns options as defined in `config.fontSize.options` but processed to account for
* editor localization, i.e. to display {@link module:font/fontsize~FontSizeOption}
* 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.
*
* @private
* @returns {Array.<module:font/fontsize~FontSizeOption>}.
*/
_getLocalizedOptions() {
const editor = this.editor;
const t = editor.t;
const localizedTitles = {
Default: t( 'Default' ),
Tiny: t( 'Tiny' ),
Small: t( 'Small' ),
Big: t( 'Big' ),
Huge: t( 'Huge' )
};
const options = normalizeOptions( editor.config.get( FONT_SIZE ).options );
return options.map( option => {
const title = localizedTitles[ option.title ];
if ( title && title != option.title ) {
// Clone the option to avoid altering the original `namedPresets` from `./utils.js`.
option = Object.assign( {}, option, { title } );
}
return option;
} );
}
}
// Prepares FontSize dropdown items.
// @private
// @param {Array.<module:font/fontsize~FontSizeOption>} options
// @param {module:font/fontsize/fontsizecommand~FontSizeCommand} command
function _prepareListOptions( options, command ) {
const itemDefinitions = new Collection();
for ( const option of options ) {
const def = {
type: 'button',
model: new Model( {
commandName: FONT_SIZE,
commandParam: option.model,
label: option.title,
class: 'ck-fontsize-option',
withText: true
} )
};
if ( option.view && option.view.styles ) {
def.model.set( 'labelStyle', `font-size:${ option.view.styles[ 'font-size' ] }` );
}
if ( option.view && option.view.classes ) {
def.model.set( 'class', `${ def.model.class } ${ option.view.classes }` );
}
def.model.bind( 'isOn' ).to( command, 'value', value => value === option.model );
// Add the option to the collection.
itemDefinitions.add( def );
}
return itemDefinitions;
}