This repository has been 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
112 lines (89 loc) · 2.96 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
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @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, addListViewToDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
import { normalizeOptions } from './utils';
import fontFamilyIcon from '../../theme/icons/font-family.svg';
/**
* @extends module:core/plugin~Plugin
*/
export default class FontFamilyUI extends Plugin {
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const t = editor.t;
const dropdownItems = new Collection();
const options = this._getLocalizedOptions();
const command = editor.commands.get( 'fontFamily' );
// Create dropdown items.
for ( const option of options ) {
const itemModel = new Model( {
commandName: 'fontFamily',
commandParam: option.model,
label: option.title
} );
itemModel.bind( 'isActive' ).to( command, 'value', value => value === option.model );
// Try to set a dropdown list item style.
if ( option.view && option.view.style ) {
itemModel.set( 'style', `font-family: ${ option.view.style[ 'font-family' ] }` );
}
dropdownItems.add( itemModel );
}
// Register UI component.
editor.ui.componentFactory.add( 'fontFamily', locale => {
const dropdownView = createDropdown( locale );
addListViewToDropdown( dropdownView, dropdownItems );
dropdownView.set( {
icon: fontFamilyIcon,
withText: false,
tooltip: t( 'Font Family' )
} );
dropdownView.bind( 'isEnabled' ).to( command, 'isEnabled' );
dropdownView.extendTemplate( {
attributes: {
class: [
'ck-font-family-dropdown'
]
}
} );
// 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 consider
* editor localization, i.e. to display {@link module:font/fontfamily~FontFamilyOption}
* in the correct language.
*
* Note: The reason behind this method is that there's no way to use {@link module:utils/locale~Locale#t}
* when the user config 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( 'fontFamily.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;
} );
}
}