-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
fontfamilyui.ts
190 lines (152 loc) · 5.21 KB
/
fontfamilyui.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
187
188
189
190
/**
* @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/fontfamily/fontfamilyui
*/
import { Plugin } from 'ckeditor5/src/core.js';
import { Collection } from 'ckeditor5/src/utils.js';
import {
ViewModel,
createDropdown,
addListToDropdown,
MenuBarMenuView,
MenuBarMenuListView,
MenuBarMenuListItemView,
MenuBarMenuListItemButtonView,
type ListDropdownButtonDefinition
} from 'ckeditor5/src/ui.js';
import { normalizeOptions } from './utils.js';
import { FONT_FAMILY } from '../utils.js';
import type { FontFamilyOption } from '../fontconfig.js';
import type FontFamilyCommand from './fontfamilycommand.js';
import fontFamilyIcon from '../../theme/icons/font-family.svg';
/**
* The font family UI plugin. It introduces the `'fontFamily'` dropdown.
*/
export default class FontFamilyUI extends Plugin {
/**
* @inheritDoc
*/
public static get pluginName() {
return 'FontFamilyUI' as const;
}
/**
* @inheritDoc
*/
public init(): void {
const editor = this.editor;
const t = editor.t;
const options = this._getLocalizedOptions();
const command: FontFamilyCommand = editor.commands.get( FONT_FAMILY )!;
const accessibleLabel = t( 'Font Family' );
const listOptions = _prepareListOptions( options, command );
// Register UI component.
editor.ui.componentFactory.add( FONT_FAMILY, locale => {
const dropdownView = createDropdown( locale );
addListToDropdown( dropdownView, listOptions, {
role: 'menu',
ariaLabel: accessibleLabel
} );
dropdownView.buttonView.set( {
label: accessibleLabel,
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 as any ).commandName, { value: ( evt.source as any ).commandParam } );
editor.editing.view.focus();
} );
return dropdownView;
} );
editor.ui.componentFactory.add( `menuBar:${ FONT_FAMILY }`, locale => {
const menuView = new MenuBarMenuView( locale );
menuView.buttonView.set( {
label: accessibleLabel,
icon: fontFamilyIcon
} );
menuView.bind( 'isEnabled' ).to( command );
const listView = new MenuBarMenuListView( locale );
for ( const definition of listOptions ) {
const listItemView = new MenuBarMenuListItemView( locale, menuView );
const buttonView = new MenuBarMenuListItemButtonView( locale );
buttonView.bind( ...Object.keys( definition.model ) as Array<keyof MenuBarMenuListItemButtonView> ).to( definition.model );
buttonView.bind( 'ariaChecked' ).to( buttonView, 'isOn' );
buttonView.delegate( 'execute' ).to( menuView );
buttonView.on( 'execute', () => {
editor.execute( ( definition.model as any ).commandName, {
value: ( definition.model as any ).commandParam
} );
editor.editing.view.focus();
} );
listItemView.children.add( buttonView );
listView.items.add( listItemView );
}
menuView.panelView.children.add( listView );
return menuView;
} );
}
/**
* Returns options as defined in `config.fontFamily.options` but processed to account for
* editor localization, i.e. to display {@link module:font/fontconfig~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 _getLocalizedOptions(): Array<FontFamilyOption> {
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.
*/
function _prepareListOptions( options: Array<FontFamilyOption>, command: FontFamilyCommand ): Collection<ListDropdownButtonDefinition> {
const itemDefinitions = new Collection<ListDropdownButtonDefinition>();
// Create dropdown items.
for ( const option of options ) {
const def = {
type: 'button' as const,
model: new ViewModel( {
commandName: FONT_FAMILY,
commandParam: option.model,
label: option.title,
role: 'menuitemradio',
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 && typeof option.view !== 'string' && option.view.styles ) {
def.model.set( 'labelStyle', `font-family: ${ option.view.styles[ 'font-family' ] }` );
}
itemDefinitions.add( def );
}
return itemDefinitions;
}