all files / packages/base/ SelectAllCommand.js

0% Statements 0/21
0% Branches 0/10
0% Functions 0/2
0% Lines 0/21
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                                                                                                             
import { last } from '../../util'
import { Command } from '../../ui'
 
class SelectAll extends Command {
 
  getCommandState(params) {
    let editorSession = params.editorSession
    return {
      disabled: editorSession.getSelection().isNull()
    }
  }
 
  execute(params) {
    let editorSession = params.editorSession
    let doc = editorSession.getDocument()
    let surface = params.surface || editorSession.getFocusedSurface()
    if (surface) {
      let sel
      // TODO: we should move the logic out of the surfaces
      if (surface._isContainerEditor) {
        let container = surface.getContainer()
        if (container.nodes.length === 0) {
          return false
        }
        let firstNodeId = container.nodes[0]
        let lastNodeId = last(container.nodes)
        sel = editorSession.createSelection({
          type: 'container',
          startPath: [firstNodeId],
          startOffset: 0,
          endPath: [lastNodeId],
          endOffset: 1,
          containerId: container.id,
          surfaceId: surface.id
        })
      } else if (surface._isTextPropertyEditor) {
        let path = surface.getPath()
        let text = doc.get(path)
        sel = editorSession.createSelection({
          type: 'property',
          path: path,
          startOffset: 0,
          endOffset: text.length,
          surfaceId: surface.id
        })
      }
      editorSession.setSelection(sel)
      return true
    }
    return false
  }
}
 
export default SelectAll