mirror of https://github.com/status-im/codimd.git
refactor: fix lint on public/js/lib/editor/utils.js
Signed-off-by: BoHong Li <raccoon@hackmd.io>
This commit is contained in:
parent
f5c73d3439
commit
88601302c4
|
@ -4,17 +4,17 @@ export function wrapTextWith (editor, cm, symbol) {
|
||||||
if (!cm.getSelection()) {
|
if (!cm.getSelection()) {
|
||||||
return CodeMirror.Pass
|
return CodeMirror.Pass
|
||||||
} else {
|
} else {
|
||||||
let ranges = cm.listSelections()
|
const ranges = cm.listSelections()
|
||||||
for (let i = 0; i < ranges.length; i++) {
|
for (let i = 0; i < ranges.length; i++) {
|
||||||
let range = ranges[i]
|
const range = ranges[i]
|
||||||
if (!range.empty()) {
|
if (!range.empty()) {
|
||||||
const from = range.from()
|
const from = range.from()
|
||||||
const to = range.to()
|
const to = range.to()
|
||||||
|
|
||||||
if (symbol !== 'Backspace') {
|
if (symbol !== 'Backspace') {
|
||||||
let selection = cm.getRange(from, to)
|
const selection = cm.getRange(from, to)
|
||||||
let anchorIndex = editor.indexFromPos(ranges[i].anchor)
|
const anchorIndex = editor.indexFromPos(ranges[i].anchor)
|
||||||
let headIndex = editor.indexFromPos(ranges[i].head)
|
const headIndex = editor.indexFromPos(ranges[i].head)
|
||||||
cm.replaceRange(symbol + selection + symbol, from, to, '+input')
|
cm.replaceRange(symbol + selection + symbol, from, to, '+input')
|
||||||
if (anchorIndex > headIndex) {
|
if (anchorIndex > headIndex) {
|
||||||
ranges[i].anchor.ch += symbol.length
|
ranges[i].anchor.ch += symbol.length
|
||||||
|
@ -25,18 +25,18 @@ export function wrapTextWith (editor, cm, symbol) {
|
||||||
}
|
}
|
||||||
cm.setSelections(ranges)
|
cm.setSelections(ranges)
|
||||||
} else {
|
} else {
|
||||||
let preEndPos = {
|
const preEndPos = {
|
||||||
line: to.line,
|
line: to.line,
|
||||||
ch: to.ch + symbol.length
|
ch: to.ch + symbol.length
|
||||||
}
|
}
|
||||||
let preText = cm.getRange(to, preEndPos)
|
const preText = cm.getRange(to, preEndPos)
|
||||||
let preIndex = wrapSymbols.indexOf(preText)
|
const preIndex = wrapSymbols.indexOf(preText)
|
||||||
let postEndPos = {
|
const postEndPos = {
|
||||||
line: from.line,
|
line: from.line,
|
||||||
ch: from.ch - symbol.length
|
ch: from.ch - symbol.length
|
||||||
}
|
}
|
||||||
let postText = cm.getRange(postEndPos, from)
|
const postText = cm.getRange(postEndPos, from)
|
||||||
let postIndex = wrapSymbols.indexOf(postText)
|
const postIndex = wrapSymbols.indexOf(postText)
|
||||||
// check if surround symbol are list in array and matched
|
// check if surround symbol are list in array and matched
|
||||||
if (preIndex > -1 && postIndex > -1 && preIndex === postIndex) {
|
if (preIndex > -1 && postIndex > -1 && preIndex === postIndex) {
|
||||||
cm.replaceRange('', to, preEndPos, '+delete')
|
cm.replaceRange('', to, preEndPos, '+delete')
|
||||||
|
@ -49,25 +49,25 @@ export function wrapTextWith (editor, cm, symbol) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function insertText (cm, text, cursorEnd = 0) {
|
export function insertText (cm, text, cursorEnd = 0) {
|
||||||
let cursor = cm.getCursor()
|
const cursor = cm.getCursor()
|
||||||
cm.replaceSelection(text, cursor, cursor)
|
cm.replaceSelection(text, cursor, cursor)
|
||||||
cm.focus()
|
cm.focus()
|
||||||
cm.setCursor({ line: cursor.line, ch: cursor.ch + cursorEnd })
|
cm.setCursor({ line: cursor.line, ch: cursor.ch + cursorEnd })
|
||||||
}
|
}
|
||||||
|
|
||||||
export function insertLink (cm, isImage) {
|
export function insertLink (cm, isImage) {
|
||||||
let cursor = cm.getCursor()
|
const cursor = cm.getCursor()
|
||||||
let ranges = cm.listSelections()
|
const ranges = cm.listSelections()
|
||||||
const linkEnd = '](https://)'
|
const linkEnd = '](https://)'
|
||||||
const symbol = (isImage) ? '![' : '['
|
const symbol = (isImage) ? '![' : '['
|
||||||
|
|
||||||
for (let i = 0; i < ranges.length; i++) {
|
for (let i = 0; i < ranges.length; i++) {
|
||||||
let range = ranges[i]
|
const range = ranges[i]
|
||||||
if (!range.empty()) {
|
if (!range.empty()) {
|
||||||
const from = range.from()
|
const from = range.from()
|
||||||
const to = range.to()
|
const to = range.to()
|
||||||
let anchorIndex = editor.indexFromPos(ranges[i].anchor)
|
const anchorIndex = editor.indexFromPos(ranges[i].anchor)
|
||||||
let headIndex = editor.indexFromPos(ranges[i].head)
|
const headIndex = editor.indexFromPos(ranges[i].head)
|
||||||
let selection = cm.getRange(from, to)
|
let selection = cm.getRange(from, to)
|
||||||
selection = symbol + selection + linkEnd
|
selection = symbol + selection + linkEnd
|
||||||
cm.replaceRange(selection, from, to)
|
cm.replaceRange(selection, from, to)
|
||||||
|
@ -88,9 +88,9 @@ export function insertLink (cm, isImage) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function insertHeader (cm) {
|
export function insertHeader (cm) {
|
||||||
let cursor = cm.getCursor()
|
const cursor = cm.getCursor()
|
||||||
let startOfLine = { line: cursor.line, ch: 0 }
|
const startOfLine = { line: cursor.line, ch: 0 }
|
||||||
let startOfLineText = cm.getRange(startOfLine, { line: cursor.line, ch: 1 })
|
const startOfLineText = cm.getRange(startOfLine, { line: cursor.line, ch: 1 })
|
||||||
// See if it is already a header
|
// See if it is already a header
|
||||||
if (startOfLineText === '#') {
|
if (startOfLineText === '#') {
|
||||||
cm.replaceRange('#', startOfLine, startOfLine)
|
cm.replaceRange('#', startOfLine, startOfLine)
|
||||||
|
@ -101,11 +101,11 @@ export function insertHeader (cm) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function insertOnStartOfLines (cm, symbol) {
|
export function insertOnStartOfLines (cm, symbol) {
|
||||||
let cursor = cm.getCursor()
|
const cursor = cm.getCursor()
|
||||||
let ranges = cm.listSelections()
|
const ranges = cm.listSelections()
|
||||||
|
|
||||||
for (let i = 0; i < ranges.length; i++) {
|
for (let i = 0; i < ranges.length; i++) {
|
||||||
let range = ranges[i]
|
const range = ranges[i]
|
||||||
if (!range.empty()) {
|
if (!range.empty()) {
|
||||||
const from = range.from()
|
const from = range.from()
|
||||||
const to = range.to()
|
const to = range.to()
|
||||||
|
|
Loading…
Reference in New Issue