indentui.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 indent/indentui
  7. */
  8. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  9. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  10. import indentIcon from '../theme/icons/indent.svg';
  11. import outdentIcon from '../theme/icons/outdent.svg';
  12. /**
  13. * The indent UI feature.
  14. *
  15. * This plugin registers the `'indent'` and `'outdent'` buttons.
  16. *
  17. * **Note**: In order for the commands to work, at least one of the compatible features is required. Read more in
  18. * the {@link module:indent/indent~Indent indent feature} API documentation.
  19. *
  20. * @extends module:core/plugin~Plugin
  21. */
  22. export default class IndentUI extends Plugin {
  23. /**
  24. * @inheritDoc
  25. */
  26. static get pluginName() {
  27. return 'IndentUI';
  28. }
  29. /**
  30. * @inheritDoc
  31. */
  32. init() {
  33. const editor = this.editor;
  34. const locale = editor.locale;
  35. const t = editor.t;
  36. const localizedIndentIcon = locale.uiLanguageDirection == 'ltr' ? indentIcon : outdentIcon;
  37. const localizedOutdentIcon = locale.uiLanguageDirection == 'ltr' ? outdentIcon : indentIcon;
  38. this._defineButton( 'indent', t( 'Increase indent' ), localizedIndentIcon );
  39. this._defineButton( 'outdent', t( 'Decrease indent' ), localizedOutdentIcon );
  40. }
  41. /**
  42. * Defines a UI button.
  43. *
  44. * @param {String} commandName
  45. * @param {String} label
  46. * @param {String} icon
  47. * @private
  48. */
  49. _defineButton( commandName, label, icon ) {
  50. const editor = this.editor;
  51. editor.ui.componentFactory.add( commandName, locale => {
  52. const command = editor.commands.get( commandName );
  53. const view = new ButtonView( locale );
  54. view.set( {
  55. label,
  56. icon,
  57. tooltip: true
  58. } );
  59. view.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );
  60. this.listenTo( view, 'execute', () => {
  61. editor.execute( commandName );
  62. editor.editing.view.focus();
  63. } );
  64. return view;
  65. } );
  66. }
  67. }