2017-03-09 07:39:42 +00:00
|
|
|
const wrapSymbols = ['*', '_', '~', '^', '+', '=']
|
|
|
|
export function wrapTextWith (editor, cm, symbol) {
|
|
|
|
if (!cm.getSelection()) {
|
|
|
|
return CodeMirror.Pass
|
|
|
|
} else {
|
2018-06-23 18:55:32 +00:00
|
|
|
let ranges = cm.listSelections()
|
|
|
|
for (let i = 0; i < ranges.length; i++) {
|
|
|
|
let range = ranges[i]
|
2017-03-09 07:39:42 +00:00
|
|
|
if (!range.empty()) {
|
|
|
|
const from = range.from()
|
|
|
|
const to = range.to()
|
|
|
|
|
|
|
|
if (symbol !== 'Backspace') {
|
2018-06-23 18:55:32 +00:00
|
|
|
let selection = cm.getRange(from, to)
|
|
|
|
let anchorIndex = editor.indexFromPos(ranges[i].anchor)
|
|
|
|
let headIndex = editor.indexFromPos(ranges[i].head)
|
|
|
|
cm.replaceRange(symbol + selection + symbol, from, to, '+input')
|
2017-03-09 07:39:42 +00:00
|
|
|
if (anchorIndex > headIndex) {
|
2018-06-23 19:27:05 +00:00
|
|
|
ranges[i].anchor.ch += symbol.length
|
|
|
|
ranges[i].head.ch += symbol.length
|
2017-03-09 07:39:42 +00:00
|
|
|
} else {
|
2018-06-23 19:27:05 +00:00
|
|
|
ranges[i].head.ch += symbol.length
|
|
|
|
ranges[i].anchor.ch += symbol.length
|
2017-03-09 07:39:42 +00:00
|
|
|
}
|
2018-06-23 18:55:32 +00:00
|
|
|
cm.setSelections(ranges)
|
2017-03-09 07:39:42 +00:00
|
|
|
} else {
|
2018-06-23 18:55:32 +00:00
|
|
|
let preEndPos = {
|
2017-03-09 07:39:42 +00:00
|
|
|
line: to.line,
|
2018-06-23 18:55:32 +00:00
|
|
|
ch: to.ch + symbol.length
|
2017-03-09 07:39:42 +00:00
|
|
|
}
|
2018-06-23 18:55:32 +00:00
|
|
|
let preText = cm.getRange(to, preEndPos)
|
|
|
|
let preIndex = wrapSymbols.indexOf(preText)
|
|
|
|
let postEndPos = {
|
2017-03-09 07:39:42 +00:00
|
|
|
line: from.line,
|
2018-06-23 18:55:32 +00:00
|
|
|
ch: from.ch - symbol.length
|
2017-03-09 07:39:42 +00:00
|
|
|
}
|
2018-06-23 18:55:32 +00:00
|
|
|
let postText = cm.getRange(postEndPos, from)
|
|
|
|
let postIndex = wrapSymbols.indexOf(postText)
|
2017-03-09 07:39:42 +00:00
|
|
|
// 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')
|
|
|
|
}
|
2017-03-07 14:35:54 +00:00
|
|
|
}
|
2017-03-09 07:39:42 +00:00
|
|
|
}
|
2017-03-07 14:35:54 +00:00
|
|
|
}
|
2017-03-09 07:39:42 +00:00
|
|
|
}
|
2017-03-07 14:35:54 +00:00
|
|
|
}
|
2018-06-19 14:03:32 +00:00
|
|
|
|
|
|
|
export function insertText (cm, text, cursorEnd = 0) {
|
2018-06-23 18:55:32 +00:00
|
|
|
let cursor = cm.getCursor()
|
2018-06-19 14:03:32 +00:00
|
|
|
cm.replaceSelection(text, cursor, cursor)
|
|
|
|
cm.focus()
|
2019-04-12 09:56:18 +00:00
|
|
|
cm.setCursor({ line: cursor.line, ch: cursor.ch + cursorEnd })
|
2018-06-19 14:03:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 19:27:05 +00:00
|
|
|
export function insertLink (cm, isImage) {
|
2018-06-23 18:55:32 +00:00
|
|
|
let cursor = cm.getCursor()
|
|
|
|
let ranges = cm.listSelections()
|
|
|
|
const linkEnd = '](https://)'
|
|
|
|
const symbol = (isImage) ? '![' : '['
|
|
|
|
|
|
|
|
for (let i = 0; i < ranges.length; i++) {
|
|
|
|
let range = ranges[i]
|
|
|
|
if (!range.empty()) {
|
|
|
|
const from = range.from()
|
|
|
|
const to = range.to()
|
|
|
|
let anchorIndex = editor.indexFromPos(ranges[i].anchor)
|
|
|
|
let headIndex = editor.indexFromPos(ranges[i].head)
|
|
|
|
let selection = cm.getRange(from, to)
|
|
|
|
selection = symbol + selection + linkEnd
|
|
|
|
cm.replaceRange(selection, from, to)
|
|
|
|
if (anchorIndex > headIndex) {
|
2018-06-23 19:27:05 +00:00
|
|
|
ranges[i].anchor.ch += symbol.length
|
|
|
|
ranges[i].head.ch += symbol.length
|
2018-06-23 18:55:32 +00:00
|
|
|
} else {
|
2018-06-23 19:27:05 +00:00
|
|
|
ranges[i].head.ch += symbol.length
|
|
|
|
ranges[i].anchor.ch += symbol.length
|
2018-06-23 18:55:32 +00:00
|
|
|
}
|
|
|
|
cm.setSelections(ranges)
|
|
|
|
} else {
|
|
|
|
cm.replaceRange(symbol + linkEnd, cursor, cursor)
|
2019-04-12 09:56:18 +00:00
|
|
|
cm.setCursor({ line: cursor.line, ch: cursor.ch + symbol.length + linkEnd.length })
|
2018-06-23 18:55:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cm.focus()
|
|
|
|
}
|
|
|
|
|
2018-06-19 14:03:32 +00:00
|
|
|
export function insertHeader (cm) {
|
|
|
|
let cursor = cm.getCursor()
|
2019-04-12 09:56:18 +00:00
|
|
|
let startOfLine = { line: cursor.line, ch: 0 }
|
|
|
|
let startOfLineText = cm.getRange(startOfLine, { line: cursor.line, ch: 1 })
|
2018-06-19 14:03:32 +00:00
|
|
|
// See if it is already a header
|
|
|
|
if (startOfLineText === '#') {
|
|
|
|
cm.replaceRange('#', startOfLine, startOfLine)
|
|
|
|
} else {
|
|
|
|
cm.replaceRange('# ', startOfLine, startOfLine)
|
|
|
|
}
|
|
|
|
cm.focus()
|
|
|
|
}
|
|
|
|
|
2018-06-23 18:55:32 +00:00
|
|
|
export function insertOnStartOfLines (cm, symbol) {
|
2018-06-19 14:03:32 +00:00
|
|
|
let cursor = cm.getCursor()
|
2018-06-23 18:55:32 +00:00
|
|
|
let ranges = cm.listSelections()
|
2018-06-19 14:03:32 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < ranges.length; i++) {
|
2018-06-23 18:55:32 +00:00
|
|
|
let range = ranges[i]
|
2018-06-19 14:03:32 +00:00
|
|
|
if (!range.empty()) {
|
|
|
|
const from = range.from()
|
|
|
|
const to = range.to()
|
2019-04-12 09:56:18 +00:00
|
|
|
let selection = cm.getRange({ line: from.line, ch: 0 }, to)
|
2018-06-23 18:55:32 +00:00
|
|
|
selection = selection.replace(/\n/g, '\n' + symbol)
|
|
|
|
selection = symbol + selection
|
|
|
|
cm.replaceRange(selection, from, to)
|
2018-06-19 14:03:32 +00:00
|
|
|
} else {
|
2019-04-12 09:56:18 +00:00
|
|
|
cm.replaceRange(symbol, { line: cursor.line, ch: 0 }, { line: cursor.line, ch: 0 })
|
2018-06-19 14:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-12 09:56:18 +00:00
|
|
|
cm.setCursor({ line: cursor.line, ch: cursor.ch + symbol.length })
|
2018-06-19 14:03:32 +00:00
|
|
|
cm.focus()
|
|
|
|
}
|