fontsizeui.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 createListDropdown from '@ckeditor/ckeditor5-ui/src/dropdown/list/createlistdropdown';
  12. import { normalizeOptions } from '../fontsize/utils';
  13. import fontSizeIcon from '../../theme/icons/font-size.svg';
  14. /**
  15. * @extends module:core/plugin~Plugin
  16. */
  17. export default class FontSizeUI extends Plugin {
  18. /**
  19. * @inheritDoc
  20. */
  21. init() {
  22. const editor = this.editor;
  23. const dropdownItems = new Collection();
  24. const options = this._getLocalizedOptions();
  25. const t = editor.t;
  26. const command = editor.commands.get( 'fontSize' );
  27. for ( const option of options ) {
  28. const itemModel = new Model( {
  29. commandName: 'fontSize',
  30. commandParam: option.model,
  31. label: option.title,
  32. class: option.class
  33. } );
  34. itemModel.bind( 'isActive' ).to( command, 'value', value => value === option.model );
  35. // Add the option to the collection.
  36. dropdownItems.add( itemModel );
  37. }
  38. // Create dropdown model.
  39. const dropdownModel = new Model( {
  40. icon: fontSizeIcon,
  41. withText: false,
  42. items: dropdownItems,
  43. tooltip: t( 'Font Size' )
  44. } );
  45. dropdownModel.bind( 'isEnabled' ).to( command, 'isEnabled' );
  46. // Register UI component.
  47. editor.ui.componentFactory.add( 'fontSize', locale => {
  48. const dropdown = createListDropdown( dropdownModel, locale );
  49. dropdown.extendTemplate( {
  50. attributes: {
  51. class: [
  52. 'ck-font-size-dropdown'
  53. ]
  54. }
  55. } );
  56. // Execute command when an item from the dropdown is selected.
  57. this.listenTo( dropdown, 'execute', evt => {
  58. editor.execute( evt.source.commandName, { value: evt.source.commandParam } );
  59. editor.editing.view.focus();
  60. } );
  61. return dropdown;
  62. } );
  63. }
  64. /**
  65. * Returns options as defined in `config.fontSize.options` but processed to consider
  66. * editor localization, i.e. to display {@link module:font/fontsize~FontSizeOption}
  67. * in the correct language.
  68. *
  69. * Note: The reason behind this method is that there's no way to use {@link module:utils/locale~Locale#t}
  70. * when the user config is defined because the editor does not exist yet.
  71. *
  72. * @private
  73. * @returns {Array.<module:font/fontsize~FontSizeOption>}.
  74. */
  75. _getLocalizedOptions() {
  76. const editor = this.editor;
  77. const t = editor.t;
  78. const localizedTitles = {
  79. Normal: t( 'Normal' ),
  80. Tiny: t( 'Tiny' ),
  81. Small: t( 'Small' ),
  82. Big: t( 'Big' ),
  83. Huge: t( 'Huge' )
  84. };
  85. const options = normalizeOptions( editor.config.get( 'fontSize.options' ) );
  86. return options.map( option => {
  87. const title = localizedTitles[ option.title ];
  88. if ( title && title != option.title ) {
  89. // Clone the option to avoid altering the original `namedPresets` from `./utils.js`.
  90. option = Object.assign( {}, option, { title } );
  91. }
  92. return option;
  93. } );
  94. }
  95. }