alignmentui.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 '@ckeditor/ckeditor5-core/theme/icons/align-left.svg';
  13. import alignRightIcon from '@ckeditor/ckeditor5-core/theme/icons/align-right.svg';
  14. import alignCenterIcon from '@ckeditor/ckeditor5-core/theme/icons/align-center.svg';
  15. import alignJustifyIcon from '@ckeditor/ckeditor5-core/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 `'alignment'` dropdown.
  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.toolbarView.ariaLabel = t( 'Text alignment toolbar' );
  83. dropdownView.extendTemplate( {
  84. attributes: {
  85. class: 'ck-alignment-dropdown'
  86. }
  87. } );
  88. // The default icon depends on the direction of the content.
  89. const defaultIcon = locale.contentLanguageDirection === 'rtl' ? alignRightIcon : alignLeftIcon;
  90. // Change icon to reflect current selection's alignment.
  91. dropdownView.buttonView.bind( 'icon' ).toMany( buttons, 'isOn', ( ...areActive ) => {
  92. // Get the index of an active button.
  93. const index = areActive.findIndex( value => value );
  94. // If none of the commands is active, display either defaultIcon or the first button's icon.
  95. if ( index < 0 ) {
  96. return defaultIcon;
  97. }
  98. // Return active button's icon.
  99. return buttons[ index ].icon;
  100. } );
  101. // Enable button if any of the buttons is enabled.
  102. dropdownView.bind( 'isEnabled' ).toMany( buttons, 'isEnabled', ( ...areEnabled ) => areEnabled.some( isEnabled => isEnabled ) );
  103. return dropdownView;
  104. } );
  105. }
  106. /**
  107. * Helper method for initializing the button and linking it with an appropriate command.
  108. *
  109. * @private
  110. * @param {String} option The name of the alignment option for which the button is added.
  111. */
  112. _addButton( option ) {
  113. const editor = this.editor;
  114. editor.ui.componentFactory.add( `alignment:${ option }`, locale => {
  115. const command = editor.commands.get( 'alignment' );
  116. const buttonView = new ButtonView( locale );
  117. buttonView.set( {
  118. label: this.localizedOptionTitles[ option ],
  119. icon: icons.get( option ),
  120. tooltip: true,
  121. isToggleable: true
  122. } );
  123. // Bind button model to command.
  124. buttonView.bind( 'isEnabled' ).to( command );
  125. buttonView.bind( 'isOn' ).to( command, 'value', value => value === option );
  126. // Execute command.
  127. this.listenTo( buttonView, 'execute', () => {
  128. editor.execute( 'alignment', { value: option } );
  129. editor.editing.view.focus();
  130. } );
  131. return buttonView;
  132. } );
  133. }
  134. }