Browse Source

Add private `runCommand`, `stopCommand` and use them with Buttons

pull/1382/head
Artur Arseniev 8 years ago
parent
commit
aa2d2868a5
  1. 69
      src/commands/index.js
  2. 10
      src/panels/view/ButtonView.js
  3. 2
      test/specs/commands/view/CommandAbstract.js

69
src/commands/index.js

@ -21,7 +21,7 @@
* * [run](#run)
* * [stop](#stop)
* * [isActive](#isactive)
* * [getactive](#getactive)
* * [getActive](#getactive)
*
* @module Commands
*/
@ -274,19 +274,7 @@ module.exports = () => {
* commands.run('myCommand', { someOption: 1 });
*/
run(id, options = {}) {
let result;
const command = this.get(id);
const editor = em.get('Editor');
if (command) {
result = command.callRun(editor, options);
// Check if the command is stoppable to add it as an active
if (command.stop && !command.noStop) {
active[id] = result;
}
}
return result;
return this.runCommand(this.get(id), options);
},
/**
@ -298,15 +286,7 @@ module.exports = () => {
* commands.stop('myCommand', { someOption: 1 });
*/
stop(id, options = {}) {
let result;
const command = this.get(id);
const editor = em.get('Editor');
if (command) {
result = command.callStop(editor, options);
delete active[id];
}
return result;
return this.runCommand(this.get(id), options);
},
/**
@ -352,6 +332,49 @@ module.exports = () => {
return this;
},
/**
* Run command via its object
* @param {Object} command
* @param {Object} options
* @return {*} Result of the command
* @private
*/
runCommand(command, options = {}) {
let result;
if (command && command.run) {
const id = command.id;
const editor = em.get('Editor');
result = command.callRun(editor, options);
if (id && command.stop && !command.noStop) {
active[id] = result;
}
}
return result;
},
/**
* [runCommand description]
* @param {Object} command
* @param {Object} options
* @return {*} Result of the command
* @private
*/
stopCommand(command, options = {}) {
let result;
if (command && command.run) {
const id = command.id;
const editor = em.get('Editor');
result = command.callStop(editor, options);
if (id) delete active[id];
}
return result;
},
/**
* Create anonymous Command instance
* @param {Object} command Command object

10
src/panels/view/ButtonView.js

@ -88,20 +88,14 @@ module.exports = Backbone.View.extend({
if (model.get('active')) {
model.collection.deactivateAll(context);
model.set('active', true, { silent: true }).trigger('checkActive');
if (command.run) {
command.callRun(editor, { ...options, sender: model });
}
commands.runCommand(command, { ...options, sender: model });
// Disable button if the command was just a function
cmdIsFunc && model.set('active', false);
} else {
this.$el.removeClass(this.activeCls);
model.collection.deactivateAll(context);
if (command.stop) {
command.callStop(editor, { ...options, sender: model });
}
commands.stopCommand(command, { ...options, sender: model });
}
},

2
test/specs/commands/view/CommandAbstract.js

@ -1,4 +1,4 @@
const CommandAbstract = require('commands/view/CommandAbstract');
import CommandAbstract from 'commands/view/CommandAbstract';
const Editor = require('editor/model/Editor');
module.exports = {

Loading…
Cancel
Save