Editor.addCommand
Description
Registers a Command object with the editor. You can reference the command from one of these user interface components:
- ribbon-normalbutton
- ribbon-normalbuttondropdown
- ribbon-bigbutton
- ribbon-bigbuttondropdown
- ribbon_smallbutton
The functions defined in a command should use scope variables, and they will be executed whenever there is a change in the scope. The following example uses the custom scope variable show.
//declare only once
Editor.getScope().declare("show", false);
var ShowDraftCommentsCommand =
{
execute: function (scope) {
scope.set("show", !scope.get("show"));
var canvas = Editor.Canvas.getActiveCanvas();
canvas.setViewParam("show-draft-comments", scope.get("show"));
},
getEnabled: function(scope)
{
var canvas = Editor.Canvas.getActiveCanvas();
return !!canvas;
},
getChecked: function (scope) {
return scope.get("show"); //the button shows a 'pressed' state
},
getLabel: function (scope) {
if (scope.get("show"))
return "Hide Draft Comments";
else
return "Show Draft Comments";
}
}
Editor.addCommand("showDraftCommentsCommand", ShowDraftCommentsCommand);
Alternatively, you can use the built-in scope variable xmlSelection . This way the function will be executed whenever the selection changes in the editor. The value of the xmlSelection scope variable is equal to the Editor.Selection object. In this example, the button is active in paragraphs (or elements named para in this case) only.
getEnabled: function(scope) {
var selection = scope.get("xmlSelection");
var enabled = false;
if (selection && selection.getRange()) {
var context = selection.getRange().getCommonAncestorContainer();
if (context.getNodeType() == 3)
context = context.getParentNode();
if (context.getLocalName() == "paragraph")
enabled = true;
}
return enabled;
}
Syntax
Editor.addCommand (name : String, command : Command) : Object
Arguments
- name
-
String. A unique name for the command. This name binds theuserCommandobject to the user interface component. - command
-
Command. An object that implements the Command. For example, the code that is run when the command executes.
Return Value
Object.