8
0

command.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 EmitterMixin from './emittermixin.js';
  7. import utils from './utils.js';
  8. /**
  9. * The base class for CKEditor commands.
  10. *
  11. * @class core.Command
  12. */
  13. export default class Command {
  14. /**
  15. * Creates a new Command instance.
  16. */
  17. constructor( editor ) {
  18. this.editor = editor;
  19. this.isEnabled = true;
  20. // If schema checking function is specified, add it to the `checkEnabled` listeners.
  21. // Feature will be disabled if it does not apply to schema requirements.
  22. if ( this.checkSchema ) {
  23. this.on( 'checkEnabled', ( evt ) => {
  24. if ( !this.checkSchema() ) {
  25. evt.stop();
  26. return false;
  27. }
  28. } );
  29. }
  30. }
  31. checkEnabled() {
  32. this.isEnabled = this.fire( 'checkEnabled' ) !== false;
  33. }
  34. _disable() {
  35. this.on( 'checkEnabled', disableCallback );
  36. this.checkEnabled();
  37. }
  38. _enable() {
  39. this.off( 'checkEnabled', disableCallback );
  40. this.checkEnabled();
  41. }
  42. execute() {
  43. // Should be overwritten in child class.
  44. }
  45. }
  46. function disableCallback( evt ) {
  47. evt.stop();
  48. return false;
  49. }
  50. utils.mix( Command, EmitterMixin );