Client Commands
- 3 minutes to read
Available commands are a powerful tool for manipulating document content on the client in code. Most commands replicate the RichEdit’s default UI commands available through the editor’s ribbon. These commands are useful when you are required to implement a custom toolbar for the RichEdit instead of the default built-in ribbon. For an example of such a custom toolbar, refer to the Rich Text Editor - Custom Toolbar via Client API demo.
The client RichEdit ASPxClientRichEdit.commands property is an entry point to all available client commands. All commands are implemented as individual (command type related) objects inherited from the CommandBase. Command objects are exposed through the properties of the RichEditCommands object returned by the RichEdit’s ASPxClientRichEdit.commands property. In code, a command can be accessed in the following notation.
[clientRichEditName].commands.[commandName]
Each command object exposes the execute and getState methods. The execute method may result in no action taken if a command’s state obtained using the getState method does not allow command execution (for instance, in a case when a command is not enabled in the ribbon).
The execute method
This is the command’s main method. It accomplishes the task for which the command is implemented. For its execution, a command may require additional data to be passed as the execute method’s parameter. See the signature of each command’s execute method for details.
richEdit.commands.changeFontFormatting.execute({bold: "true", size: 25, foreColor: "#789"});
The getState method
This method is called automatically before command execution. The method’s return value is a command type specific object (CommandState<T>) that can contain the following state-related settings.
There are specificities in using commands to define certain elements (such as fonts or shortcuts). These specificities are described in the following sections.
Working with Fonts
When you format fonts and need to specify the font size value (which is measured in points), use an integer number or a fractional number representing half font sizes (such as 11, 11.5, 12, 12.5 and etc.). Use the ASPxClientRichEdit.unitConverter to convert size measure units (inches, twips, pixels, centimeters) to points.
Working with Sizes
When you modify a size value in the document model (e.g., page size or margin size), assign these values in twips. Use the ASPxClientRichEdit.unitConverter to convert other size measure units (inches, points, pixels, centimeters) to twips.
Working with Client Events
The client commands that change the document’s content (or its selection) trigger the corresponding client-side events as well as changes made through the RichEdit’s UI. Set the ASPxRichEditBehaviorSettings.RaiseClientEventsOnModificationsViaAPI property to false, if you need to ignore client commands calls, when you handle the client-side events.
Working with Shortcuts
To define a shortcut, use the RichEditCommands.assignShortcut command whose AssignShortcutCommand.execute methods accepts two parameters - keyCode and callback.
keyCode
A specifically generated code that uniquely identifies the combination of keys specified for a shortcut. This code is specified using the ASPxClientUtils.GetShortcutCode method.
callback
A callback function to execute when a shortcut is activated.
The following code demonstrates how to define a shortcut that updates a document field on pressing the “Alt + u” keys.
richEdit.commands.assignShortcut.execute(ASPxClientUtils.GetShortcutCode(ASPx.KeyCode.Key_u, false, false, false, false), function(){ rich.commands.updateField.execute(); } );