all files / packages/quote-marks/ InsertQuoteMarkCommand.js

10% Statements 2/20
0% Branches 0/8
0% Functions 0/2
10% Lines 2/20
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                                                                                   
import { Command } from '../../ui'
 
const LEFT_QUOTE = "\u201C"
const RIGHT_QUOTE = "\u201D"
 
class InsertQuoteMarkCommand extends Command {
 
  getCommandState(params, context) { // eslint-disable-line
    // TODO: maybe only enable for specific selections?
    // let enabled = params.selection.isPropertySelection()
    return {
      disabled: false
    }
  }
 
  execute(params, context) { // eslint-disable-line
    let editorSession = params.editorSession
    let sel = editorSession.getSelection()
    let doc = editorSession.getDocument()
    if (sel.isPropertySelection()) {
      let nodeId = sel.start.getNodeId()
      let node = doc.get(nodeId)
      if (node.isText()) {
        let text = node.getText()
        let offset = sel.start.offset
        let mark
        if (offset === 0 || /\s/.exec(text.slice(offset-1, offset))) {
          mark = LEFT_QUOTE
        } else {
          mark = RIGHT_QUOTE
        }
        editorSession.transaction((tx) => {
          tx.insertText(mark)
        })
        return true
      }
    }
    return false
  }
 
}
 
export default InsertQuoteMarkCommand