undo.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Feature from '../feature.js';
  7. import UndoEngine from './undoengine.js';
  8. import Model from '../ui/model.js';
  9. import Button from '../ui/button/button.js';
  10. import ButtonView from '../ui/button/buttonview.js';
  11. /**
  12. * Undo feature. Introduces the "Undo" and "Redo" buttons to the editor.
  13. *
  14. * @memberOf undo
  15. * @extends ckeditor5.Feature
  16. */
  17. export default class Undo extends Feature {
  18. /**
  19. * @inheritDoc
  20. */
  21. static get requires() {
  22. return [ UndoEngine ];
  23. }
  24. /**
  25. * @inheritDoc
  26. */
  27. init() {
  28. const editor = this.editor;
  29. const t = editor.t;
  30. this._addButton( 'undo', t( 'Undo' ) );
  31. this._addButton( 'redo', t( 'Redo' ) );
  32. editor.keystrokes.set( 'CTRL+Z', 'undo' );
  33. editor.keystrokes.set( 'CTRL+Y', 'redo' );
  34. editor.keystrokes.set( 'CTRL+SHIFT+Z', 'redo' );
  35. }
  36. /**
  37. * Creates a button for the specified command.
  38. *
  39. * @private
  40. * @param {String} name Command name.
  41. * @param {String} label Button label.
  42. */
  43. _addButton( name, label ) {
  44. const editor = this.editor;
  45. const command = editor.commands.get( name );
  46. const model = new Model( {
  47. isOn: false,
  48. label: label,
  49. icon: name,
  50. iconAlign: 'LEFT'
  51. } );
  52. model.bind( 'isEnabled' ).to( command, 'isEnabled' );
  53. this.listenTo( model, 'execute', () => editor.execute( name ) );
  54. editor.ui.featureComponents.add( name, Button, ButtonView, model );
  55. }
  56. }