8
0

fontsizeui.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module font/fontsize/fontsizeui
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import Model from '@ckeditor/ckeditor5-ui/src/model';
  10. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  11. import { createDropdown, addListToDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
  12. import { normalizeOptions } from '../fontsize/utils';
  13. import fontSizeIcon from '../../theme/icons/font-size.svg';
  14. import '../../theme/fontsize.css';
  15. /**
  16. * The font size UI plugin. It introduces the `'fontSize'` dropdown.
  17. *
  18. * @extends module:core/plugin~Plugin
  19. */
  20. export default class FontSizeUI extends Plugin {
  21. /**
  22. * @inheritDoc
  23. */
  24. init() {
  25. const editor = this.editor;
  26. const t = editor.t;
  27. const options = this._getLocalizedOptions();
  28. const command = editor.commands.get( 'fontSize' );
  29. // Register UI component.
  30. editor.ui.componentFactory.add( 'fontSize', locale => {
  31. const dropdownView = createDropdown( locale );
  32. addListToDropdown( dropdownView, _prepareListOptions( options, command ) );
  33. // Create dropdown model.
  34. dropdownView.buttonView.set( {
  35. label: t( 'Font Size' ),
  36. icon: fontSizeIcon,
  37. tooltip: true
  38. } );
  39. dropdownView.extendTemplate( {
  40. attributes: {
  41. class: [
  42. 'ck-font-size-dropdown'
  43. ]
  44. }
  45. } );
  46. dropdownView.bind( 'isEnabled' ).to( command );
  47. // Execute command when an item from the dropdown is selected.
  48. this.listenTo( dropdownView, 'execute', evt => {
  49. editor.execute( evt.source.commandName, { value: evt.source.commandParam } );
  50. editor.editing.view.focus();
  51. } );
  52. return dropdownView;
  53. } );
  54. }
  55. /**
  56. * Returns options as defined in `config.fontSize.options` but processed to account for
  57. * editor localization, i.e. to display {@link module:font/fontsize~FontSizeOption}
  58. * in the correct language.
  59. *
  60. * Note: The reason behind this method is that there is no way to use {@link module:utils/locale~Locale#t}
  61. * when the user configuration is defined because the editor does not exist yet.
  62. *
  63. * @private
  64. * @returns {Array.<module:font/fontsize~FontSizeOption>}.
  65. */
  66. _getLocalizedOptions() {
  67. const editor = this.editor;
  68. const t = editor.t;
  69. const localizedTitles = {
  70. Default: t( 'Default' ),
  71. Tiny: t( 'Tiny' ),
  72. Small: t( 'Small' ),
  73. Big: t( 'Big' ),
  74. Huge: t( 'Huge' )
  75. };
  76. const options = normalizeOptions( editor.config.get( 'fontSize.options' ) );
  77. return options.map( option => {
  78. const title = localizedTitles[ option.title ];
  79. if ( title && title != option.title ) {
  80. // Clone the option to avoid altering the original `namedPresets` from `./utils.js`.
  81. option = Object.assign( {}, option, { title } );
  82. }
  83. return option;
  84. } );
  85. }
  86. }
  87. // Prepares FontSize dropdown items.
  88. // @private
  89. // @param {Array.<module:font/fontsize~FontSizeOption>} options
  90. // @param {module:font/fontsize/fontsizecommand~FontSizeCommand} command
  91. function _prepareListOptions( options, command ) {
  92. const itemDefinitions = new Collection();
  93. for ( const option of options ) {
  94. const def = {
  95. type: 'button',
  96. model: new Model( {
  97. commandName: 'fontSize',
  98. commandParam: option.model,
  99. label: option.title,
  100. class: 'ck-fontsize-option',
  101. withText: true
  102. } )
  103. };
  104. if ( option.view && option.view.styles ) {
  105. def.model.set( 'labelStyle', `font-size:${ option.view.styles[ 'font-size' ] }` );
  106. }
  107. if ( option.view && option.view.classes ) {
  108. def.model.set( 'class', `${ def.model.class } ${ option.view.classes }` );
  109. }
  110. def.model.bind( 'isOn' ).to( command, 'value', value => value === option.model );
  111. // Add the option to the collection.
  112. itemDefinitions.add( def );
  113. }
  114. return itemDefinitions;
  115. }