all files / ui/ Command.js

64.71% Statements 11/17
58.33% Branches 7/12
57.14% Functions 4/7
64.71% Lines 11/17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                                                                        1645× 1645× 1645×                           6088×                                                                                                     4572× 4572×     4572×              
/**
  Commands are used to perform UI triggered actions on the document. For instance the
  {@link 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 Command
  @abstract
 
  @example
 
  ```
  class MyCommand extends Command {
    getCommandState(params, context) {
      // determine commandState based on params and context
    }
 
    execute(params, context) {
      // perform operations on the document
    }
  }
  ```
*/
class Command {
 
  /**
    Construcutor is only used internally.
 
    @constructor
    @param {Object} config    Config provided during command registration
  */
  constructor(config) {
    this.config = config || {}
    this.name = this.config.name
    Iif (!this.name) {
      throw new Error("'name' is required");
    }
  }
 
  get isAsync() {
    return false
  }
 
  /**
    Get the command name specified at command registration. See
    {@link Configurator#addCommand}
  */
  getName() {
    return this.name
  }
 
  /**
    Determines command state, based on passed params and context. The command
    state is usually used as props for tool components.
 
    @example
 
    This shows the implementation of {@link 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]
      }
    }
    ```
 
    @param {Object} params      Provides editorSession, selectionState, surface, selection
    @param {Object} context     Provides app-specific context.
  */
  getCommandState(params, context) { // eslint-disable-line
    throw new Error('Command.getCommandState() is abstract.')
  }
 
  /**
    Execute command and perform operations on the document
 
    @param {Object} params      Provides commandState, editorSession, selectionState, surface, selection
    @param {Object} context     Provides app-specific context.
 
    @return {Object} info object with execution details
  */
  execute(params, context) { // eslint-disable-line
    throw new Error('Command.execute() is abstract.')
  }
 
  _getEditorSession(params, context) {
    let editorSession = params.editorSession || context.editorSession
    Iif (!editorSession) {
      throw new Error("'editorSession' is required.")
    }
    return editorSession
  }
 
  _getSelection(params) {
    let sel = params.selection || params.selectionState.getSelection()
    Iif (!sel) {
      throw new Error("'selection' is required.")
    }
    return sel
  }
 
}
 
Command.prototype._isCommand = true
 
export default Command