Skip to content

Commit f5b95c5

Browse files
committed
Move updateStatusBar method into editor class
1 parent df743ab commit f5b95c5

3 files changed

Lines changed: 35 additions & 27 deletions

File tree

public/js/index.js

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ var renderer = require('./render')
7979
var preventXSS = renderer.preventXSS
8080

8181
import Editor from './lib/editor'
82+
import EditorConfig from './lib/editor/config'
8283

8384
import getUIElements from './lib/editor/ui-elements'
8485

@@ -320,27 +321,6 @@ window.editor = editor
320321
var inlineAttach = inlineAttachment.editors.codemirror4.attach(editor)
321322
defaultTextHeight = parseInt($('.CodeMirror').css('line-height'))
322323

323-
function updateStatusBar () {
324-
if (!editorInstance.statusBar) return
325-
var cursor = editor.getCursor()
326-
var cursorText = 'Line ' + (cursor.line + 1) + ', Columns ' + (cursor.ch + 1)
327-
editorInstance.statusCursor.text(cursorText)
328-
var fileText = ' — ' + editor.lineCount() + ' Lines'
329-
editorInstance.statusFile.text(fileText)
330-
var docLength = editor.getValue().length
331-
editorInstance.statusLength.text('Length ' + docLength)
332-
if (docLength > (docmaxlength * 0.95)) {
333-
editorInstance.statusLength.css('color', 'red')
334-
editorInstance.statusLength.attr('title', 'Your almost reach note max length limit.')
335-
} else if (docLength > (docmaxlength * 0.8)) {
336-
editorInstance.statusLength.css('color', 'orange')
337-
editorInstance.statusLength.attr('title', 'You nearly fill the note, consider to make more pieces.')
338-
} else {
339-
editorInstance.statusLength.css('color', 'white')
340-
editorInstance.statusLength.attr('title', 'You could write up to ' + docmaxlength + ' characters in this note.')
341-
}
342-
}
343-
344324
// initalize ui reference
345325
const ui = getUIElements()
346326
// FIXME: fix global ui element expose
@@ -830,7 +810,7 @@ function changeMode (type) {
830810
// add and update status bar
831811
if (!editorInstance.statusBar) {
832812
editorInstance.addStatusBar()
833-
updateStatusBar()
813+
editorInstance.updateStatusBar()
834814
}
835815
// work around foldGutter might not init properly
836816
editor.setOption('foldGutter', false)
@@ -2105,12 +2085,12 @@ socket.on('check', function (data) {
21052085
socket.on('permission', function (data) {
21062086
updatePermission(data.permission)
21072087
})
2108-
var docmaxlength = null
2088+
21092089
var permission = null
21102090
socket.on('refresh', function (data) {
21112091
// console.log(data);
2112-
docmaxlength = data.docmaxlength
2113-
editor.setOption('maxLength', docmaxlength)
2092+
EditorConfig.docmaxlength = data.docmaxlength
2093+
editor.setOption('maxLength', EditorConfig.docmaxlength)
21142094
updateInfo(data)
21152095
updatePermission(data.permission)
21162096
if (!window.loaded) {
@@ -2714,10 +2694,10 @@ function cursorActivityInner (editor) {
27142694
}
27152695
}
27162696

2717-
editorInstance.on('cursorActivity', updateStatusBar)
2697+
editorInstance.on('cursorActivity', editorInstance.updateStatusBar)
27182698
editorInstance.on('cursorActivity', cursorActivity)
27192699

2720-
editorInstance.on('beforeSelectionChange', updateStatusBar)
2700+
editorInstance.on('beforeSelectionChange', editorInstance.updateStatusBar)
27212701
editorInstance.on('beforeSelectionChange', function (doc, selections) {
27222702
// check selection and whether the statusbar has added
27232703
if (selections && editorInstance.statusSelection) {

public/js/lib/editor/config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let config = {
2+
docmaxlength: null
3+
}
4+
5+
export default config

public/js/lib/editor/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as utils from './utils'
2+
import config from './config'
23

34
/* config section */
45
const isMac = CodeMirror.keyMap.default === CodeMirror.keyMap.macDefault
@@ -167,6 +168,28 @@ export default class Editor {
167168
}
168169
}
169170

171+
updateStatusBar () {
172+
if (!this.statusBar) return
173+
174+
var cursor = this.editor.getCursor()
175+
var cursorText = 'Line ' + (cursor.line + 1) + ', Columns ' + (cursor.ch + 1)
176+
this.statusCursor.text(cursorText)
177+
var fileText = ' — ' + editor.lineCount() + ' Lines'
178+
this.statusFile.text(fileText)
179+
var docLength = editor.getValue().length
180+
this.statusLength.text('Length ' + docLength)
181+
if (docLength > (config.docmaxlength * 0.95)) {
182+
this.statusLength.css('color', 'red')
183+
this.statusLength.attr('title', 'Your almost reach note max length limit.')
184+
} else if (docLength > (config.docmaxlength * 0.8)) {
185+
this.statusLength.css('color', 'orange')
186+
this.statusLength.attr('title', 'You nearly fill the note, consider to make more pieces.')
187+
} else {
188+
this.statusLength.css('color', 'white')
189+
this.statusLength.attr('title', 'You could write up to ' + config.docmaxlength + ' characters in this note.')
190+
}
191+
}
192+
170193
setIndent () {
171194
var cookieIndentType = Cookies.get('indent_type')
172195
var cookieTabSize = parseInt(Cookies.get('tab_size'))

0 commit comments

Comments
 (0)