Move updateStatusBar method into editor class

This commit is contained in:
Yukai Huang 2017-03-28 17:16:32 +08:00
parent df743ab902
commit f5b95c5d36
3 changed files with 35 additions and 27 deletions

View file

@ -79,6 +79,7 @@ var renderer = require('./render')
var preventXSS = renderer.preventXSS var preventXSS = renderer.preventXSS
import Editor from './lib/editor' import Editor from './lib/editor'
import EditorConfig from './lib/editor/config'
import getUIElements from './lib/editor/ui-elements' import getUIElements from './lib/editor/ui-elements'
@ -320,27 +321,6 @@ window.editor = editor
var inlineAttach = inlineAttachment.editors.codemirror4.attach(editor) var inlineAttach = inlineAttachment.editors.codemirror4.attach(editor)
defaultTextHeight = parseInt($('.CodeMirror').css('line-height')) defaultTextHeight = parseInt($('.CodeMirror').css('line-height'))
function updateStatusBar () {
if (!editorInstance.statusBar) return
var cursor = editor.getCursor()
var cursorText = 'Line ' + (cursor.line + 1) + ', Columns ' + (cursor.ch + 1)
editorInstance.statusCursor.text(cursorText)
var fileText = ' — ' + editor.lineCount() + ' Lines'
editorInstance.statusFile.text(fileText)
var docLength = editor.getValue().length
editorInstance.statusLength.text('Length ' + docLength)
if (docLength > (docmaxlength * 0.95)) {
editorInstance.statusLength.css('color', 'red')
editorInstance.statusLength.attr('title', 'Your almost reach note max length limit.')
} else if (docLength > (docmaxlength * 0.8)) {
editorInstance.statusLength.css('color', 'orange')
editorInstance.statusLength.attr('title', 'You nearly fill the note, consider to make more pieces.')
} else {
editorInstance.statusLength.css('color', 'white')
editorInstance.statusLength.attr('title', 'You could write up to ' + docmaxlength + ' characters in this note.')
}
}
// initalize ui reference // initalize ui reference
const ui = getUIElements() const ui = getUIElements()
// FIXME: fix global ui element expose // FIXME: fix global ui element expose
@ -830,7 +810,7 @@ function changeMode (type) {
// add and update status bar // add and update status bar
if (!editorInstance.statusBar) { if (!editorInstance.statusBar) {
editorInstance.addStatusBar() editorInstance.addStatusBar()
updateStatusBar() editorInstance.updateStatusBar()
} }
// work around foldGutter might not init properly // work around foldGutter might not init properly
editor.setOption('foldGutter', false) editor.setOption('foldGutter', false)
@ -2105,12 +2085,12 @@ socket.on('check', function (data) {
socket.on('permission', function (data) { socket.on('permission', function (data) {
updatePermission(data.permission) updatePermission(data.permission)
}) })
var docmaxlength = null
var permission = null var permission = null
socket.on('refresh', function (data) { socket.on('refresh', function (data) {
// console.log(data); // console.log(data);
docmaxlength = data.docmaxlength EditorConfig.docmaxlength = data.docmaxlength
editor.setOption('maxLength', docmaxlength) editor.setOption('maxLength', EditorConfig.docmaxlength)
updateInfo(data) updateInfo(data)
updatePermission(data.permission) updatePermission(data.permission)
if (!window.loaded) { if (!window.loaded) {
@ -2714,10 +2694,10 @@ function cursorActivityInner (editor) {
} }
} }
editorInstance.on('cursorActivity', updateStatusBar) editorInstance.on('cursorActivity', editorInstance.updateStatusBar)
editorInstance.on('cursorActivity', cursorActivity) editorInstance.on('cursorActivity', cursorActivity)
editorInstance.on('beforeSelectionChange', updateStatusBar) editorInstance.on('beforeSelectionChange', editorInstance.updateStatusBar)
editorInstance.on('beforeSelectionChange', function (doc, selections) { editorInstance.on('beforeSelectionChange', function (doc, selections) {
// check selection and whether the statusbar has added // check selection and whether the statusbar has added
if (selections && editorInstance.statusSelection) { if (selections && editorInstance.statusSelection) {

View file

@ -0,0 +1,5 @@
let config = {
docmaxlength: null
}
export default config

View file

@ -1,4 +1,5 @@
import * as utils from './utils' import * as utils from './utils'
import config from './config'
/* config section */ /* config section */
const isMac = CodeMirror.keyMap.default === CodeMirror.keyMap.macDefault const isMac = CodeMirror.keyMap.default === CodeMirror.keyMap.macDefault
@ -167,6 +168,28 @@ export default class Editor {
} }
} }
updateStatusBar () {
if (!this.statusBar) return
var cursor = this.editor.getCursor()
var cursorText = 'Line ' + (cursor.line + 1) + ', Columns ' + (cursor.ch + 1)
this.statusCursor.text(cursorText)
var fileText = ' — ' + editor.lineCount() + ' Lines'
this.statusFile.text(fileText)
var docLength = editor.getValue().length
this.statusLength.text('Length ' + docLength)
if (docLength > (config.docmaxlength * 0.95)) {
this.statusLength.css('color', 'red')
this.statusLength.attr('title', 'Your almost reach note max length limit.')
} else if (docLength > (config.docmaxlength * 0.8)) {
this.statusLength.css('color', 'orange')
this.statusLength.attr('title', 'You nearly fill the note, consider to make more pieces.')
} else {
this.statusLength.css('color', 'white')
this.statusLength.attr('title', 'You could write up to ' + config.docmaxlength + ' characters in this note.')
}
}
setIndent () { setIndent () {
var cookieIndentType = Cookies.get('indent_type') var cookieIndentType = Cookies.get('indent_type')
var cookieTabSize = parseInt(Cookies.get('tab_size')) var cookieTabSize = parseInt(Cookies.get('tab_size'))