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/index.js | 18 +++++++++--------- public/js/lib/editor/index.js | 13 +++++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) (limited to 'public/js') diff --git a/public/js/index.js b/public/js/index.js index 5dff54d9..e909eb4e 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -2104,7 +2104,7 @@ function iterateLine (line) { } } } -editor.on('update', function () { +editorInstance.on('update', function () { $('.authorship-gutter:not([data-original-title])').tooltip({ container: '.CodeMirror-lines', placement: 'right', @@ -2655,7 +2655,7 @@ function enforceMaxLength (cm, change) { return false } var ignoreEmitEvents = ['setValue', 'ignoreHistory'] -editor.on('beforeChange', function (cm, change) { +editorInstance.on('beforeChange', function (cm, change) { if (debug) { console.debug(change) } removeNullByte(cm, change) if (enforceMaxLength(cm, change)) { @@ -2683,13 +2683,13 @@ editor.on('beforeChange', function (cm, change) { } if (cmClient && !socket.connected) { cmClient.editorAdapter.ignoreNextChange = true } }) -editor.on('cut', function () { +editorInstance.on('cut', function () { // na }) -editor.on('paste', function () { +editorInstance.on('paste', function () { // na }) -editor.on('changes', function (cm, changes) { +editorInstance.on('changes', function (cm, changes) { updateHistory() var docLength = editor.getValue().length // workaround for big documents @@ -2713,7 +2713,7 @@ editor.on('changes', function (cm, changes) { } } }) -editor.on('focus', function (cm) { +editorInstance.on('focus', function (cm) { for (var i = 0; i < window.onlineUsers.length; i++) { if (window.onlineUsers[i].id === window.personalInfo.id) { window.onlineUsers[i].cursor = editor.getCursor() @@ -2722,11 +2722,11 @@ editor.on('focus', function (cm) { window.personalInfo['cursor'] = editor.getCursor() socket.emit('cursor focus', editor.getCursor()) }) -editor.on('cursorActivity', function (cm) { +editorInstance.on('cursorActivity', function (cm) { updateStatusBar() cursorActivity() }) -editor.on('beforeSelectionChange', function (doc, selections) { +editorInstance.on('beforeSelectionChange', function (doc, selections) { if (selections) { selection = selections.ranges[0] } else { selection = null } updateStatusBar() }) @@ -2744,7 +2744,7 @@ function cursorActivityInner () { socket.emit('cursor activity', editor.getCursor()) } } -editor.on('blur', function (cm) { +editorInstance.on('blur', function (cm) { for (var i = 0; i < window.onlineUsers.length; i++) { if (window.onlineUsers[i].id === window.personalInfo.id) { window.onlineUsers[i].cursor = null 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