Skip to content

Commit b86ecb1

Browse files
committed
Extract selection update from updateStatusbar
1 parent 81666a7 commit b86ecb1

3 files changed

Lines changed: 38 additions & 28 deletions

File tree

public/js/index.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -320,33 +320,10 @@ window.editor = editor
320320
var inlineAttach = inlineAttachment.editors.codemirror4.attach(editor)
321321
defaultTextHeight = parseInt($('.CodeMirror').css('line-height'))
322322

323-
var selection = null
324-
325323
function updateStatusBar () {
326324
if (!editorInstance.statusBar) return
327325
var cursor = editor.getCursor()
328326
var cursorText = 'Line ' + (cursor.line + 1) + ', Columns ' + (cursor.ch + 1)
329-
if (selection) {
330-
var anchor = selection.anchor
331-
var head = selection.head
332-
var start = head.line <= anchor.line ? head : anchor
333-
var end = head.line >= anchor.line ? head : anchor
334-
var selectionText = ' — Selected '
335-
var selectionCharCount = Math.abs(head.ch - anchor.ch)
336-
// borrow from brackets EditorStatusBar.js
337-
if (start.line !== end.line) {
338-
var lines = end.line - start.line + 1
339-
if (end.ch === 0) {
340-
lines--
341-
}
342-
selectionText += lines + ' lines'
343-
} else if (selectionCharCount > 0) {
344-
selectionText += selectionCharCount + ' columns'
345-
}
346-
if (start.line !== end.line || selectionCharCount > 0) {
347-
cursorText += selectionText
348-
}
349-
}
350327
editorInstance.statusCursor.text(cursorText)
351328
var fileText = ' — ' + editor.lineCount() + ' Lines'
352329
editorInstance.statusFile.text(fileText)
@@ -2726,9 +2703,38 @@ editorInstance.on('cursorActivity', function (cm) {
27262703
updateStatusBar()
27272704
cursorActivity()
27282705
})
2706+
2707+
editorInstance.on('beforeSelectionChange', updateStatusBar)
27292708
editorInstance.on('beforeSelectionChange', function (doc, selections) {
2730-
if (selections) { selection = selections.ranges[0] } else { selection = null }
2731-
updateStatusBar()
2709+
// check selection and whether the statusbar has added
2710+
if (selections && editorInstance.statusSelection) {
2711+
const selection = selections.ranges[0]
2712+
2713+
const anchor = selection.anchor
2714+
const head = selection.head
2715+
const start = head.line <= anchor.line ? head : anchor
2716+
const end = head.line >= anchor.line ? head : anchor
2717+
const selectionCharCount = Math.abs(head.ch - anchor.ch)
2718+
2719+
let selectionText = ' — Selected '
2720+
2721+
// borrow from brackets EditorStatusBar.js
2722+
if (start.line !== end.line) {
2723+
var lines = end.line - start.line + 1
2724+
if (end.ch === 0) {
2725+
lines--
2726+
}
2727+
selectionText += lines + ' lines'
2728+
} else if (selectionCharCount > 0) {
2729+
selectionText += selectionCharCount + ' columns'
2730+
}
2731+
2732+
if (start.line !== end.line || selectionCharCount > 0) {
2733+
editorInstance.statusSelection.text(selectionText)
2734+
} else {
2735+
editorInstance.statusSelection.text('')
2736+
}
2737+
}
27322738
})
27332739

27342740
var cursorActivity = _.debounce(cursorActivityInner, cursorActivityDebounce)

public/js/lib/editor/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ export default class Editor {
144144
return
145145
}
146146
this.statusBar = $(this.statusBarTemplate)
147-
this.statusCursor = this.statusBar.find('.status-cursor')
147+
this.statusCursor = this.statusBar.find('.status-cursor > .status-line-column')
148+
this.statusSelection = this.statusBar.find('.status-cursor > .status-selection')
148149
this.statusFile = this.statusBar.find('.status-file')
149150
this.statusIndicators = this.statusBar.find('.status-indicators')
150151
this.statusIndent = this.statusBar.find('.status-indent')

public/views/statusbar.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<div class="status-bar">
22
<div class="status-info">
3-
<div class="status-cursor"></div>
3+
<div class="status-cursor">
4+
<span class="status-line-column"></span>
5+
<span class="status-selection"></span>
6+
</div>
47
<div class="status-file"></div>
58
</div>
69
<div class="status-indicators">
@@ -35,4 +38,4 @@
3538
<a class="ui-spellcheck-toggle" title="Toggle spellcheck"><i class="fa fa-check fa-fw"></i></a>
3639
</div>
3740
</div>
38-
</div>
41+
</div>

0 commit comments

Comments
 (0)