alignmentui.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module alignment/alignmentui
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  10. import { createDropdown, addToolbarToDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
  11. import { isSupported } from './utils';
  12. import alignLeftIcon from '../theme/icons/align-left.svg';
  13. import alignRightIcon from '../theme/icons/align-right.svg';
  14. import alignCenterIcon from '../theme/icons/align-center.svg';
  15. import alignJustifyIcon from '../theme/icons/align-justify.svg';
  16. const icons = new Map( [
  17. [ 'left', alignLeftIcon ],
  18. [ 'right', alignRightIcon ],
  19. [ 'center', alignCenterIcon ],
  20. [ 'justify', alignJustifyIcon ]
  21. ] );
  22. /**
  23. * The default alignment UI plugin.
  24. *
  25. * It introduces the `'alignment:left'`, `'alignment:right'`, `'alignment:center'` and `'alignment:justify'` buttons
  26. * and the `'alignmentDropdown'` drop-down.
  27. *
  28. * @extends module:core/plugin~Plugin
  29. */
  30. export default class AlignmentUI extends Plugin {
  31. /**
  32. * Returns the localized option titles provided by the plugin.
  33. *
  34. * The following localized titles corresponding with
  35. * {@link module:alignment/alignment~AlignmentConfig#options} are available:
  36. *
  37. * * `'left'`,
  38. * * `'right'`,
  39. * * `'center'`,
  40. * * `'justify'`.
  41. *
  42. * @readonly
  43. * @type {Object.<String,String>}
  44. */
  45. get localizedOptionTitles() {
  46. const t = this.editor.t;
  47. return {
  48. 'left': t( 'Align left' ),
  49. 'right': t( 'Align right' ),
  50. 'center': t( 'Align center' ),
  51. 'justify': t( 'Justify' )
  52. };
  53. }
  54. /**
  55. * @inheritDoc
  56. */
  57. static get pluginName() {
  58. return 'AlignmentUI';
  59. }
  60. /**
  61. * @inheritDoc
  62. */
  63. init() {
  64. const editor = this.editor;
  65. const componentFactory = editor.ui.componentFactory;
  66. const t = editor.t;
  67. const options = editor.config.get( 'alignment.options' );
  68. options
  69. .filter( isSupported )
  70. .forEach( option => this._addButton( option ) );
  71. componentFactory.add( 'alignment', locale => {
  72. const dropdownView = createDropdown( locale );
  73. // Add existing alignment buttons to dropdown's toolbar.
  74. const buttons = options.map( option => componentFactory.create( `alignment:${ option }` ) );
  75. addToolbarToDropdown( dropdownView, buttons );
  76. // Configure dropdown properties an behavior.
  77. dropdownView.buttonView.set( {
  78. label: t( 'Text alignment' ),
  79. tooltip: true
  80. } );
  81. dropdownView.toolbarView.isVertical = true;
  82. dropdownView.extendTemplate( {
  83. attributes: {
  84. class: 'ck-alignment-dropdown'
  85. }
  86. } );
  87. // The default icon is align left as we do not support RTL yet (see #3).
  88. const defaultIcon = alignLeftIcon;
  89. // Change icon to reflect current selection's alignment.
  90. dropdownView.buttonView.bind( 'icon' ).toMany( buttons, 'isOn', ( ...areActive ) => {
  91. // Get the index of an active button.
  92. const index = areActive.findIndex( value => value );
  93. // If none of the commands is active, display either defaultIcon or the first button's icon.
  94. if ( index < 0 ) {
  95. return defaultIcon;
  96. }
  97. // Return active button's icon.
  98. return buttons[ index ].icon;
  99. } );
  100. // Enable button if any of the buttons is enabled.
  101. dropdownView.bind( 'isEnabled' ).toMany( buttons, 'isEnabled', ( ...areEnabled ) => areEnabled.some( isEnabled => isEnabled ) );
  102. return dropdownView;
  103. } );
  104. }
  105. /**
  106. * Helper method for initializing the button and linking it with an appropriate command.
  107. *
  108. * @private
  109. * @param {String} option The name of the alignment option for which the button is added.
  110. */
  111. _addButton( option ) {
  112. const editor = this.editor;
  113. editor.ui.componentFactory.add( `alignment:${ option }`, locale => {
  114. const command = editor.commands.get( 'alignment' );
  115. const buttonView = new ButtonView( locale );
  116. buttonView.set( {
  117. label: this.localizedOptionTitles[ option ],
  118. icon: icons.get( option ),
  119. tooltip: true
  120. } );
  121. // Bind button model to command.
  122. buttonView.bind( 'isEnabled' ).to( command );
  123. buttonView.bind( 'isOn' ).to( command, 'value', value => value === option );
  124. // Execute command.
  125. this.listenTo( buttonView, 'execute', () => {
  126. editor.execute( 'alignment', { value: option } );
  127. editor.editing.view.focus();
  128. } );
  129. return buttonView;
  130. } );
  131. }
  132. }