This repository was archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathfontfamilyui.js
133 lines (110 loc) · 3.68 KB
/
fontfamilyui.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
/**
* @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/fontfamily/fontfamilyui
*/
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_FAMILY } from '../utils';
import fontFamilyIcon from '../../theme/icons/font-family.svg';
/**
* The font family UI plugin. It introduces the `'fontFamily'` dropdown.
*
* @extends module:core/plugin~Plugin
*/
export default class FontFamilyUI extends Plugin {
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const t = editor.t;
const options = this._getLocalizedOptions();
const command = editor.commands.get( FONT_FAMILY );
// Register UI component.
editor.ui.componentFactory.add( FONT_FAMILY, locale => {
const dropdownView = createDropdown( locale );
addListToDropdown( dropdownView, _prepareListOptions( options, command ) );
dropdownView.buttonView.set( {
label: t( 'Font Family' ),
icon: fontFamilyIcon,
tooltip: true
} );
dropdownView.extendTemplate( {
attributes: {
class: 'ck-font-family-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.fontFamily.options` but processed to account for
* editor localization, i.e. to display {@link module:font/fontfamily~FontFamilyOption}
* 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/fontfamily~FontFamilyOption>}.
*/
_getLocalizedOptions() {
const editor = this.editor;
const t = editor.t;
const options = normalizeOptions( editor.config.get( FONT_FAMILY ).options );
return options.map( option => {
// The only title to localize is "Default" others are font names.
if ( option.title === 'Default' ) {
option.title = t( 'Default' );
}
return option;
} );
}
}
// Prepares FontFamily 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();
// Create dropdown items.
for ( const option of options ) {
const def = {
type: 'button',
model: new Model( {
commandName: FONT_FAMILY,
commandParam: option.model,
label: option.title,
withText: true
} )
};
def.model.bind( 'isOn' ).to( command, 'value', value => {
// "Default" or check in strict font-family converters mode.
if ( value === option.model ) {
return true;
}
if ( !value || !option.model ) {
return false;
}
return value.split( ',' )[ 0 ].replace( /'/g, '' ).toLowerCase() === option.model.toLowerCase();
} );
// Try to set a dropdown list item style.
if ( option.view && option.view.styles ) {
def.model.set( 'labelStyle', `font-family: ${ option.view.styles[ 'font-family' ] }` );
}
itemDefinitions.add( def );
}
return itemDefinitions;
}