all files / packages/spell-check/ CorrectionTool.js

0% Statements 0/14
0% Branches 0/4
0% Functions 0/2
0% Lines 0/14
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                                                                                                     
import { Tool } from '../../ui'
 
class CorrectionTool extends Tool {
 
  render($$) {
    let node = this.props.node
    let Button = this.getComponent('button')
    let el = $$('div').addClass('sc-correction-tool')
 
    if (node && node.suggestions.length > 0) {
      node.suggestions.forEach((s) => {
        el.append(
          $$(Button, {
            label: s,
            style: this.props.style
          }).attr('title', this.getLabel('open-link'))
            .attr('data-correction', s)
            .on('click', this._applyCorrection.bind(this, s))
        )
      })
    } else {
      el.append(
        $$(Button, {
          label: 'No suggestions',
          style: this.props.style,
          disabled: true
        })
      )
    }
    return el
  }
 
  _applyCorrection(suggestion) {
    let editorSession = this.context.editorSession
    let node = this.props.node
    editorSession.transaction((tx) => {
      let sel = tx.getSelection()
      tx.setSelection({
        type: 'property',
        path: node.start.path,
        startOffset: node.start.offset,
        endOffset: node.end.offset,
        containerId: sel.containerId
      })
      tx.insertText(suggestion)
    })
  }
}
 
export default CorrectionTool