From 81666a726c831af23e1d04d18e8efae3ecc0930d Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Tue, 28 Mar 2017 11:18:36 +0800 Subject: Impl multiple codemirror event listener --- public/js/lib/editor/index.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'public/js/lib/editor') diff --git a/public/js/lib/editor/index.js b/public/js/lib/editor/index.js index 6ae40d82..8d617247 100644 --- a/public/js/lib/editor/index.js +++ b/public/js/lib/editor/index.js @@ -116,6 +116,19 @@ export default class Editor { utils.wrapTextWith(this.editor, cm, 'Backspace') } } + this.eventListeners = {} + } + + on (event, cb) { + if (!this.eventListeners[event]) { + this.eventListeners[event] = [cb] + } else { + this.eventListeners[event].push(cb) + } + + this.editor.on(event, (...args) => { + this.eventListeners[event].forEach(cb => cb(...args)) + }) } getStatusBarTemplate (callback) { -- cgit v1.2.3 From b86ecb1342673628151f729edc0c8714b1d07de0 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Tue, 28 Mar 2017 11:57:44 +0800 Subject: Extract selection update from updateStatusbar --- public/js/lib/editor/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'public/js/lib/editor') diff --git a/public/js/lib/editor/index.js b/public/js/lib/editor/index.js index 8d617247..6eec34ad 100644 --- a/public/js/lib/editor/index.js +++ b/public/js/lib/editor/index.js @@ -144,7 +144,8 @@ export default class Editor { return } this.statusBar = $(this.statusBarTemplate) - this.statusCursor = this.statusBar.find('.status-cursor') + this.statusCursor = this.statusBar.find('.status-cursor > .status-line-column') + this.statusSelection = this.statusBar.find('.status-cursor > .status-selection') this.statusFile = this.statusBar.find('.status-file') this.statusIndicators = this.statusBar.find('.status-indicators') this.statusIndent = this.statusBar.find('.status-indent') -- cgit v1.2.3 From 46ed658d8bd4fb916dd4964692aa23dc18c128ca Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Tue, 28 Mar 2017 15:24:52 +0800 Subject: Promisify getStatusBarTemplate method --- public/js/lib/editor/index.js | 54 ++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 26 deletions(-) (limited to 'public/js/lib/editor') diff --git a/public/js/lib/editor/index.js b/public/js/lib/editor/index.js index 6eec34ad..a68092ff 100644 --- a/public/js/lib/editor/index.js +++ b/public/js/lib/editor/index.js @@ -131,38 +131,40 @@ export default class Editor { }) } - getStatusBarTemplate (callback) { - $.get(window.serverurl + '/views/statusbar.html', template => { - this.statusBarTemplate = template - if (callback) callback() + getStatusBarTemplate () { + return new Promise((resolve, reject) => { + $.get(window.serverurl + '/views/statusbar.html').done(template => { + this.statusBarTemplate = template + resolve() + }).fail(reject) }) } addStatusBar () { if (!this.statusBarTemplate) { - this.getStatusBarTemplate(this.addStatusBar) - return - } - this.statusBar = $(this.statusBarTemplate) - this.statusCursor = this.statusBar.find('.status-cursor > .status-line-column') - this.statusSelection = this.statusBar.find('.status-cursor > .status-selection') - this.statusFile = this.statusBar.find('.status-file') - this.statusIndicators = this.statusBar.find('.status-indicators') - this.statusIndent = this.statusBar.find('.status-indent') - this.statusKeymap = this.statusBar.find('.status-keymap') - this.statusLength = this.statusBar.find('.status-length') - this.statusTheme = this.statusBar.find('.status-theme') - this.statusSpellcheck = this.statusBar.find('.status-spellcheck') - this.statusPreferences = this.statusBar.find('.status-preferences') - this.statusPanel = this.editor.addPanel(this.statusBar[0], { - position: 'bottom' - }) + this.getStatusBarTemplate.then(this.addStatusBar) + } else { + this.statusBar = $(this.statusBarTemplate) + this.statusCursor = this.statusBar.find('.status-cursor > .status-line-column') + this.statusSelection = this.statusBar.find('.status-cursor > .status-selection') + this.statusFile = this.statusBar.find('.status-file') + this.statusIndicators = this.statusBar.find('.status-indicators') + this.statusIndent = this.statusBar.find('.status-indent') + this.statusKeymap = this.statusBar.find('.status-keymap') + this.statusLength = this.statusBar.find('.status-length') + this.statusTheme = this.statusBar.find('.status-theme') + this.statusSpellcheck = this.statusBar.find('.status-spellcheck') + this.statusPreferences = this.statusBar.find('.status-preferences') + this.statusPanel = this.editor.addPanel(this.statusBar[0], { + position: 'bottom' + }) - this.setIndent() - this.setKeymap() - this.setTheme() - this.setSpellcheck() - this.setPreferences() + this.setIndent() + this.setKeymap() + this.setTheme() + this.setSpellcheck() + this.setPreferences() + } } setIndent () { -- cgit v1.2.3 From df743ab902e1427b12842ff9542ce9189c37f2b6 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Tue, 28 Mar 2017 17:11:20 +0800 Subject: Fix listener “this” context --- public/js/lib/editor/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'public/js/lib/editor') diff --git a/public/js/lib/editor/index.js b/public/js/lib/editor/index.js index a68092ff..dead0c40 100644 --- a/public/js/lib/editor/index.js +++ b/public/js/lib/editor/index.js @@ -127,7 +127,7 @@ export default class Editor { } this.editor.on(event, (...args) => { - this.eventListeners[event].forEach(cb => cb(...args)) + this.eventListeners[event].forEach(cb => cb.bind(this)(...args)) }) } -- cgit v1.2.3 From f5b95c5d3692169b48daff3c6f3538f53dd62380 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Tue, 28 Mar 2017 17:16:32 +0800 Subject: Move updateStatusBar method into editor class --- public/js/lib/editor/config.js | 5 +++++ public/js/lib/editor/index.js | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 public/js/lib/editor/config.js (limited to 'public/js/lib/editor') diff --git a/public/js/lib/editor/config.js b/public/js/lib/editor/config.js new file mode 100644 index 00000000..9508b847 --- /dev/null +++ b/public/js/lib/editor/config.js @@ -0,0 +1,5 @@ +let config = { + docmaxlength: null +} + +export default config diff --git a/public/js/lib/editor/index.js b/public/js/lib/editor/index.js index dead0c40..c807a17f 100644 --- a/public/js/lib/editor/index.js +++ b/public/js/lib/editor/index.js @@ -1,4 +1,5 @@ import * as utils from './utils' +import config from './config' /* config section */ 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 () { var cookieIndentType = Cookies.get('indent_type') var cookieTabSize = parseInt(Cookies.get('tab_size')) -- cgit v1.2.3