| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /**
- * @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 '../fontsize/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( 'fontSize' );
- // Register UI component.
- editor.ui.componentFactory.add( 'fontSize', 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( 'fontSize.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: 'fontSize',
- 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;
- }
|