undoui.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module undo/undoui
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  10. import undoIcon from '../theme/icons/undo.svg';
  11. import redoIcon from '../theme/icons/redo.svg';
  12. /**
  13. * The undo UI feature. It introduces the `'undo'` and `'redo'` buttons to the editor.
  14. *
  15. * @extends module:core/plugin~Plugin
  16. */
  17. export default class UndoUI extends Plugin {
  18. /**
  19. * @inheritDoc
  20. */
  21. init() {
  22. const editor = this.editor;
  23. const locale = editor.locale;
  24. const t = editor.t;
  25. const localizedUndoIcon = locale.uiLanguageDirection == 'ltr' ? undoIcon : redoIcon;
  26. const localizedRedoIcon = locale.uiLanguageDirection == 'ltr' ? redoIcon : undoIcon;
  27. this._addButton( 'undo', t( 'Undo' ), 'CTRL+Z', localizedUndoIcon );
  28. this._addButton( 'redo', t( 'Redo' ), 'CTRL+Y', localizedRedoIcon );
  29. }
  30. /**
  31. * Creates a button for the specified command.
  32. *
  33. * @private
  34. * @param {String} name Command name.
  35. * @param {String} label Button label.
  36. * @param {String} keystroke Command keystroke.
  37. * @param {String} Icon Source of the icon.
  38. */
  39. _addButton( name, label, keystroke, Icon ) {
  40. const editor = this.editor;
  41. editor.ui.componentFactory.add( name, locale => {
  42. const command = editor.commands.get( name );
  43. const view = new ButtonView( locale );
  44. view.set( {
  45. label,
  46. icon: Icon,
  47. keystroke,
  48. tooltip: true
  49. } );
  50. view.bind( 'isEnabled' ).to( command, 'isEnabled' );
  51. this.listenTo( view, 'execute', () => editor.execute( name ) );
  52. return view;
  53. } );
  54. }
  55. }