summaryrefslogtreecommitdiff
path: root/public/js/lib/editor/utils.js
blob: 3702a16619bfdc104cd8b9bf1e839bc287e29274 (plain)
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
const wrapSymbols = ['*', '_', '~', '^', '+', '=']
export function wrapTextWith (editor, cm, symbol) {
  if (!cm.getSelection()) {
    return CodeMirror.Pass
  } else {
    var ranges = cm.listSelections()
    for (var i = 0; i < ranges.length; i++) {
      var range = ranges[i]
      if (!range.empty()) {
        const from = range.from()
        const to = range.to()

        if (symbol !== 'Backspace') {
          cm.replaceRange(symbol, to, to, '+input')
          cm.replaceRange(symbol, from, from, '+input')
          // workaround selection range not correct after add symbol
          var _ranges = cm.listSelections()
          var anchorIndex = editor.indexFromPos(_ranges[i].anchor)
          var headIndex = editor.indexFromPos(_ranges[i].head)
          if (anchorIndex > headIndex) {
            _ranges[i].anchor.ch--
          } else {
            _ranges[i].head.ch--
          }
          cm.setSelections(_ranges)
        } else {
          var preEndPos = {
            line: to.line,
            ch: to.ch + 1
          }
          var preText = cm.getRange(to, preEndPos)
          var preIndex = wrapSymbols.indexOf(preText)
          var postEndPos = {
            line: from.line,
            ch: from.ch - 1
          }
          var postText = cm.getRange(postEndPos, from)
          var postIndex = wrapSymbols.indexOf(postText)
          // check if surround symbol are list in array and matched
          if (preIndex > -1 && postIndex > -1 && preIndex === postIndex) {
            cm.replaceRange('', to, preEndPos, '+delete')
            cm.replaceRange('', postEndPos, from, '+delete')
          }
        }
      }
    }
  }
}