Commands are used to perform UI triggered actions on the document. For instance the ui/AnnotationCommand takes care of creating, expanding, truncating and deleting annotations such as strong and emphasis. It does so by determining a commandState by inspecting the current selection, which is used to parametrize the corresponding tool component. E.g. the strong tool gets active and clickable in create mode when a word in the text is selected. Triggered by a click on the tool, or a keyboard shortcut, the command gets executed by running the code specified in the execute method.
class MyCommand extends Command {
getCommandState(params, context) {
// determine commandState based on params and context
}
execute(params, context) {
// perform operations on the document
}
}
Construcutor is only used internally.
Get the command name specified at command registration. See util/Configurator#addCommand
Determines command state, based on passed params and context. The command state is usually used as props for tool components.
params | Object | Provides editorSession, selectionState, surface, selection |
context | Object | Provides app-specific context. |
This shows the implementation of EditAnnotationCommand#getCommandState
getCommandState(params) {
const sel = this._getSelection(params)
const annos = params.selectionState.getAnnotationsForType(this.config.nodeType)
const newState = {
disabled: true,
}
if (annos.length === 1 && sel.isPropertySelection()) {
newState.disabled = false
newState.node = annos[0]
}
}
Execute command and perform operations on the document
params | Object | Provides commandState, editorSession, selectionState, surface, selection |
context | Object | Provides app-specific context. |
Object | info object with execution details |