all files / ui/ AbstractEditor.js

64.71% Statements 22/34
16.67% Branches 1/6
40% Functions 4/10
64.71% Lines 22/34
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                                        205× 205×       205×     205× 205×   205× 205× 205× 205× 205×     205× 205× 205× 205× 205× 205× 205× 205×   205×   205×                                                   410×                                                                              
import Component from './Component'
import ResourceManager from './ResourceManager'
import DOMSelection from './DOMSelection'
 
/**
  Reusable abstract editor implementation.
 
  @example
 
  ```js
  class SimpleWriter extends AbstractEditor {
    render($$) {
      // render editor
    }
  }
  ```
*/
class AbstractEditor extends Component {
 
  constructor(...args) {
    super(...args)
    this._initialize(this.props)
  }
 
  _initialize(props) {
    Iif (!props.editorSession) {
      throw new Error('EditorSession instance required');
    }
    this.editorSession = props.editorSession
    this.doc = this.editorSession.getDocument()
 
    let configurator = this.editorSession.getConfigurator()
    this.componentRegistry = configurator.getComponentRegistry()
    this.toolGroups = configurator.getToolGroups()
    this.labelProvider = configurator.getLabelProvider()
    this.iconProvider = configurator.getIconProvider()
 
    // legacy
    this.surfaceManager = this.editorSession.surfaceManager
    this.commandManager = this.editorSession.commandManager
    this.dragManager = this.editorSession.dragManager
    this.macroManager = this.editorSession.macroManager
    this.converterRegistry = this.editorSession.converterRegistry
    this.globalEventHandler = this.editorSession.globalEventHandler
    this.editingBehavior = this.editorSession.editingBehavior
    this.markersManager = this.editorSession.markersManager
 
    this.resourceManager = new ResourceManager(this.editorSession, this.getChildContext())
 
    this.domSelection = new DOMSelection(this)
  }
 
  willReceiveProps(nextProps) {
    let newSession = nextProps.editorSession
    let shouldDispose = newSession && newSession !== this.editorSession
    if (shouldDispose) {
      this._dispose()
      this._initialize(nextProps)
    }
  }
 
  dispose() {
    this._dispose()
  }
 
  _dispose() {
    // Note: we need to clear everything, as the childContext
    // changes which is immutable
    this.empty()
    // not necessary
    // this.domSelection.dispose()
    this.resourceManager.dispose()
  }
 
  getChildContext() {
    return {
      editor: this,
      editorSession: this.editorSession,
      doc: this.doc, // TODO: remove in favor of editorSession
      componentRegistry: this.componentRegistry,
      surfaceManager: this.surfaceManager,
      domSelection: this.domSelection,
      commandManager: this.commandManager,
      markersManager: this.markersManager,
      converterRegistry: this.converterRegistry,
      dragManager: this.dragManager,
      editingBehavior: this.editingBehavior,
      globalEventHandler: this.globalEventHandler,
      iconProvider: this.iconProvider,
      labelProvider: this.labelProvider,
      resourceManager: this.resourceManager,
      // ATTENTION: this is a map of tool target names to maps of tool names to tools
      // i.e. a declarative way to map tools to tool groups
      toolGroups: this.toolGroups,
    }
  }
 
  getDocument() {
    return this.editorSession.getDocument()
  }
 
  getConfigurator() {
    return this.editorSession.getConfigurator()
  }
 
  getEditorSession() {
    return this.editorSession
  }
 
  getComponentRegistry() {
    return this.componentRegistry
  }
}
 
export default AbstractEditor