Fix liniting and optimize some functions

First fixed some linting issues. Also optimized some functions to be
undoable with one ctrl+z.

This should also speedup some operations

Signed-off-by: Sheogorath <sheogorath@shivering-isles.com>
This commit is contained in:
Sheogorath 2018-06-23 20:55:32 +02:00
parent a8b664fdb5
commit f65d96c57b
No known key found for this signature in database
GPG Key ID: 1F05CC3635CDDFFD
3 changed files with 74 additions and 47 deletions

View File

@ -30,8 +30,6 @@ import {
import { import {
debug, debug,
DROPBOX_APP_KEY, DROPBOX_APP_KEY,
GOOGLE_API_KEY,
GOOGLE_CLIENT_ID,
noteid, noteid,
noteurl, noteurl,
urlpath, urlpath,

View File

@ -139,13 +139,10 @@ export default class Editor {
addToolBar () { addToolBar () {
this.toolBar = $(toolBarTemplate) this.toolBar = $(toolBarTemplate)
//console.log('PLACE', $('#toolbarPlace'))
//$('#toolbarPlace').html(this.toolBar)
this.toolbarPanel = this.editor.addPanel(this.toolBar[0], { this.toolbarPanel = this.editor.addPanel(this.toolBar[0], {
position: 'top' position: 'top'
}) })
var insertDemo = $('#insertDemo')
var makeBold = $('#makeBold') var makeBold = $('#makeBold')
var makeItalic = $('#makeItalic') var makeItalic = $('#makeItalic')
var makeStrike = $('#makeStrike') var makeStrike = $('#makeStrike')
@ -202,11 +199,11 @@ export default class Editor {
}) })
makeLink.click(() => { makeLink.click(() => {
utils.insertText(this.editor, '[](https://)', 1) utils.insertLink(this.editor, false)
}) })
makeImage.click(() => { makeImage.click(() => {
utils.insertText(this.editor, '![](https://)', 4) utils.insertLink(this.editor, true)
}) })
makeTable.click(() => { makeTable.click(() => {
@ -218,9 +215,8 @@ export default class Editor {
}) })
makeComment.click(() => { makeComment.click(() => {
utils.insertText(this.editor, '> []', 4) utils.insertText(this.editor, '> []')
}) })
} }
addStatusBar () { addStatusBar () {

View File

@ -3,39 +3,39 @@ export function wrapTextWith (editor, cm, symbol) {
if (!cm.getSelection()) { if (!cm.getSelection()) {
return CodeMirror.Pass return CodeMirror.Pass
} else { } else {
var ranges = cm.listSelections() let ranges = cm.listSelections()
for (var i = 0; i < ranges.length; i++) { for (let i = 0; i < ranges.length; i++) {
var range = ranges[i] let 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') {
cm.replaceRange(symbol, to, to, '+input') let selection = cm.getRange(from, to)
cm.replaceRange(symbol, from, from, '+input') let anchorIndex = editor.indexFromPos(ranges[i].anchor)
// workaround selection range not correct after add symbol let headIndex = editor.indexFromPos(ranges[i].head)
var _ranges = cm.listSelections() cm.replaceRange(symbol + selection + symbol, from, to, '+input')
var anchorIndex = editor.indexFromPos(_ranges[i].anchor)
var headIndex = editor.indexFromPos(_ranges[i].head)
if (anchorIndex > headIndex) { if (anchorIndex > headIndex) {
_ranges[i].anchor.ch-- ranges[i].anchor.ch+= symbol.length
ranges[i].head.ch+= symbol.length
} else { } else {
_ranges[i].head.ch-- ranges[i].head.ch+= symbol.length
ranges[i].anchor.ch+= symbol.length
} }
cm.setSelections(_ranges) cm.setSelections(ranges)
} else { } else {
var preEndPos = { let preEndPos = {
line: to.line, line: to.line,
ch: to.ch + 1 ch: to.ch + symbol.length
} }
var preText = cm.getRange(to, preEndPos) let preText = cm.getRange(to, preEndPos)
var preIndex = wrapSymbols.indexOf(preText) let preIndex = wrapSymbols.indexOf(preText)
var postEndPos = { let postEndPos = {
line: from.line, line: from.line,
ch: from.ch - 1 ch: from.ch - symbol.length
} }
var postText = cm.getRange(postEndPos, from) let postText = cm.getRange(postEndPos, from)
var postIndex = wrapSymbols.indexOf(postText) let 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')
@ -48,12 +48,44 @@ export function wrapTextWith (editor, cm, symbol) {
} }
export function insertText (cm, text, cursorEnd = 0) { export function insertText (cm, text, cursorEnd = 0) {
var cursor = cm.getCursor() let 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) {
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) {
ranges[i].anchor.ch+= symbol.length
ranges[i].head.ch+= symbol.length
} else {
ranges[i].head.ch+= symbol.length
ranges[i].anchor.ch+= symbol.length
}
cm.setSelections(ranges)
} else {
cm.replaceRange(symbol + linkEnd, cursor, cursor)
cm.setCursor({line: cursor.line, ch: cursor.ch + symbol.length + linkend.length})
}
}
cm.focus()
}
export function insertHeader (cm) { export function insertHeader (cm) {
let cursor = cm.getCursor() let cursor = cm.getCursor()
let startOfLine = {line: cursor.line, ch: 0} let startOfLine = {line: cursor.line, ch: 0}
@ -67,22 +99,23 @@ export function insertHeader (cm) {
cm.focus() cm.focus()
} }
export function insertOnStartOfLines (cm, symbol, cursorEnd) { export function insertOnStartOfLines (cm, symbol) {
let cursor = cm.getCursor() let cursor = cm.getCursor()
var ranges = cm.listSelections() let ranges = cm.listSelections()
for (let i = 0; i < ranges.length; i++) { for (let i = 0; i < ranges.length; i++) {
var range = ranges[i] let 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()
for (let j = from.line; j <= to.line; ++j) { let selection = cm.getRange({line: from.line, ch: 0}, to)
cm.replaceRange(symbol, {line: j, ch: 0}, {line: j, ch: 0}) selection = selection.replace(/\n/g, '\n' + symbol)
} selection = symbol + selection
cm.replaceRange(selection, from, to)
} else { } else {
cm.replaceRange(symbol, {line: cursor.line, ch: 0}, {line: cursor.line, ch: 0}) cm.replaceRange(symbol, {line: cursor.line, ch: 0}, {line: cursor.line, ch: 0})
} }
} }
cm.setCursor({line: cursor.line, ch: (cursorEnd)? cursorEnd : cursor.ch}) cm.setCursor({line: cursor.line, ch: cursor.ch + symbol.length})
cm.focus() cm.focus()
} }