Skip to content

Commit f65d96c

Browse files
committed
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>
1 parent a8b664f commit f65d96c

3 files changed

Lines changed: 74 additions & 47 deletions

File tree

public/js/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ import {
3030
import {
3131
debug,
3232
DROPBOX_APP_KEY,
33-
GOOGLE_API_KEY,
34-
GOOGLE_CLIENT_ID,
3533
noteid,
3634
noteurl,
3735
urlpath,
@@ -569,9 +567,9 @@ var previousFocusOnEditor = null
569567
function checkEditorStyle () {
570568
var desireHeight = editorInstance.statusBar ? (ui.area.edit.height() - editorInstance.statusBar.outerHeight()) : ui.area.edit.height()
571569
if (editorInstance.toolBar) {
572-
desireHeight = desireHeight - editorInstance.toolBar.outerHeight()
570+
desireHeight = desireHeight - editorInstance.toolBar.outerHeight()
573571
}
574-
// set editor height and min height based on scrollbar style and mode
572+
// set editor height and min height based on scrollbar style and mode
575573
var scrollbarStyle = editor.getOption('scrollbarStyle')
576574
if (scrollbarStyle === 'overlay' || appState.currentMode === modeType.both) {
577575
ui.area.codemirrorScroll.css('height', desireHeight + 'px')

public/js/lib/editor/index.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,10 @@ export default class Editor {
139139

140140
addToolBar () {
141141
this.toolBar = $(toolBarTemplate)
142-
//console.log('PLACE', $('#toolbarPlace'))
143-
//$('#toolbarPlace').html(this.toolBar)
144142
this.toolbarPanel = this.editor.addPanel(this.toolBar[0], {
145-
position: 'top'
143+
position: 'top'
146144
})
147145

148-
var insertDemo = $('#insertDemo')
149146
var makeBold = $('#makeBold')
150147
var makeItalic = $('#makeItalic')
151148
var makeStrike = $('#makeStrike')
@@ -162,18 +159,18 @@ export default class Editor {
162159
var makeComment = $('#makeComment')
163160

164161
makeBold.click(() => {
165-
utils.wrapTextWith(this.editor, this.editor, '**')
166-
this.editor.focus()
162+
utils.wrapTextWith(this.editor, this.editor, '**')
163+
this.editor.focus()
167164
})
168165

169166
makeItalic.click(() => {
170-
utils.wrapTextWith(this.editor, this.editor, '*')
171-
this.editor.focus()
167+
utils.wrapTextWith(this.editor, this.editor, '*')
168+
this.editor.focus()
172169
})
173170

174171
makeStrike.click(() => {
175-
utils.wrapTextWith(this.editor, this.editor, '~~')
176-
this.editor.focus()
172+
utils.wrapTextWith(this.editor, this.editor, '~~')
173+
this.editor.focus()
177174
})
178175

179176
makeHeader.click(() => {
@@ -182,7 +179,7 @@ export default class Editor {
182179

183180
makeCode.click(() => {
184181
utils.wrapTextWith(this.editor, this.editor, '```')
185-
this.editor.focus()
182+
this.editor.focus()
186183
})
187184

188185
makeQuote.click(() => {
@@ -202,11 +199,11 @@ export default class Editor {
202199
})
203200

204201
makeLink.click(() => {
205-
utils.insertText(this.editor, '[](https://)', 1)
202+
utils.insertLink(this.editor, false)
206203
})
207204

208205
makeImage.click(() => {
209-
utils.insertText(this.editor, '![](https://)', 4)
206+
utils.insertLink(this.editor, true)
210207
})
211208

212209
makeTable.click(() => {
@@ -218,9 +215,8 @@ export default class Editor {
218215
})
219216

220217
makeComment.click(() => {
221-
utils.insertText(this.editor, '> []', 4)
218+
utils.insertText(this.editor, '> []')
222219
})
223-
224220
}
225221

226222
addStatusBar () {

public/js/lib/editor/utils.js

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,39 @@ export function wrapTextWith (editor, cm, symbol) {
33
if (!cm.getSelection()) {
44
return CodeMirror.Pass
55
} else {
6-
var ranges = cm.listSelections()
7-
for (var i = 0; i < ranges.length; i++) {
8-
var range = ranges[i]
6+
let ranges = cm.listSelections()
7+
for (let i = 0; i < ranges.length; i++) {
8+
let range = ranges[i]
99
if (!range.empty()) {
1010
const from = range.from()
1111
const to = range.to()
1212

1313
if (symbol !== 'Backspace') {
14-
cm.replaceRange(symbol, to, to, '+input')
15-
cm.replaceRange(symbol, from, from, '+input')
16-
// workaround selection range not correct after add symbol
17-
var _ranges = cm.listSelections()
18-
var anchorIndex = editor.indexFromPos(_ranges[i].anchor)
19-
var headIndex = editor.indexFromPos(_ranges[i].head)
14+
let selection = cm.getRange(from, to)
15+
let anchorIndex = editor.indexFromPos(ranges[i].anchor)
16+
let headIndex = editor.indexFromPos(ranges[i].head)
17+
cm.replaceRange(symbol + selection + symbol, from, to, '+input')
2018
if (anchorIndex > headIndex) {
21-
_ranges[i].anchor.ch--
19+
ranges[i].anchor.ch+= symbol.length
20+
ranges[i].head.ch+= symbol.length
2221
} else {
23-
_ranges[i].head.ch--
22+
ranges[i].head.ch+= symbol.length
23+
ranges[i].anchor.ch+= symbol.length
2424
}
25-
cm.setSelections(_ranges)
25+
cm.setSelections(ranges)
2626
} else {
27-
var preEndPos = {
27+
let preEndPos = {
2828
line: to.line,
29-
ch: to.ch + 1
29+
ch: to.ch + symbol.length
3030
}
31-
var preText = cm.getRange(to, preEndPos)
32-
var preIndex = wrapSymbols.indexOf(preText)
33-
var postEndPos = {
31+
let preText = cm.getRange(to, preEndPos)
32+
let preIndex = wrapSymbols.indexOf(preText)
33+
let postEndPos = {
3434
line: from.line,
35-
ch: from.ch - 1
35+
ch: from.ch - symbol.length
3636
}
37-
var postText = cm.getRange(postEndPos, from)
38-
var postIndex = wrapSymbols.indexOf(postText)
37+
let postText = cm.getRange(postEndPos, from)
38+
let postIndex = wrapSymbols.indexOf(postText)
3939
// check if surround symbol are list in array and matched
4040
if (preIndex > -1 && postIndex > -1 && preIndex === postIndex) {
4141
cm.replaceRange('', to, preEndPos, '+delete')
@@ -48,12 +48,44 @@ export function wrapTextWith (editor, cm, symbol) {
4848
}
4949

5050
export function insertText (cm, text, cursorEnd = 0) {
51-
var cursor = cm.getCursor()
51+
let cursor = cm.getCursor()
5252
cm.replaceSelection(text, cursor, cursor)
5353
cm.focus()
5454
cm.setCursor({line: cursor.line, ch: cursor.ch + cursorEnd})
5555
}
5656

57+
export function insertLink(cm, isImage) {
58+
let cursor = cm.getCursor()
59+
let ranges = cm.listSelections()
60+
const linkEnd = '](https://)'
61+
const symbol = (isImage) ? '![' : '['
62+
63+
for (let i = 0; i < ranges.length; i++) {
64+
let range = ranges[i]
65+
if (!range.empty()) {
66+
const from = range.from()
67+
const to = range.to()
68+
let anchorIndex = editor.indexFromPos(ranges[i].anchor)
69+
let headIndex = editor.indexFromPos(ranges[i].head)
70+
let selection = cm.getRange(from, to)
71+
selection = symbol + selection + linkEnd
72+
cm.replaceRange(selection, from, to)
73+
if (anchorIndex > headIndex) {
74+
ranges[i].anchor.ch+= symbol.length
75+
ranges[i].head.ch+= symbol.length
76+
} else {
77+
ranges[i].head.ch+= symbol.length
78+
ranges[i].anchor.ch+= symbol.length
79+
}
80+
cm.setSelections(ranges)
81+
} else {
82+
cm.replaceRange(symbol + linkEnd, cursor, cursor)
83+
cm.setCursor({line: cursor.line, ch: cursor.ch + symbol.length + linkend.length})
84+
}
85+
}
86+
cm.focus()
87+
}
88+
5789
export function insertHeader (cm) {
5890
let cursor = cm.getCursor()
5991
let startOfLine = {line: cursor.line, ch: 0}
@@ -67,22 +99,23 @@ export function insertHeader (cm) {
6799
cm.focus()
68100
}
69101

70-
export function insertOnStartOfLines (cm, symbol, cursorEnd) {
102+
export function insertOnStartOfLines (cm, symbol) {
71103
let cursor = cm.getCursor()
72-
var ranges = cm.listSelections()
104+
let ranges = cm.listSelections()
73105

74106
for (let i = 0; i < ranges.length; i++) {
75-
var range = ranges[i]
107+
let range = ranges[i]
76108
if (!range.empty()) {
77109
const from = range.from()
78110
const to = range.to()
79-
for (let j = from.line; j <= to.line; ++j) {
80-
cm.replaceRange(symbol, {line: j, ch: 0}, {line: j, ch: 0})
81-
}
111+
let selection = cm.getRange({line: from.line, ch: 0}, to)
112+
selection = selection.replace(/\n/g, '\n' + symbol)
113+
selection = symbol + selection
114+
cm.replaceRange(selection, from, to)
82115
} else {
83116
cm.replaceRange(symbol, {line: cursor.line, ch: 0}, {line: cursor.line, ch: 0})
84117
}
85118
}
86-
cm.setCursor({line: cursor.line, ch: (cursorEnd)? cursorEnd : cursor.ch})
119+
cm.setCursor({line: cursor.line, ch: cursor.ch + symbol.length})
87120
cm.focus()
88121
}

0 commit comments

Comments
 (0)