Impl multiple codemirror event listener

This commit is contained in:
Yukai Huang 2017-03-28 11:18:36 +08:00
parent fff7ebd1b5
commit 81666a726c
2 changed files with 22 additions and 9 deletions

View File

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

View File

@ -116,6 +116,19 @@ export default class Editor {
utils.wrapTextWith(this.editor, cm, 'Backspace') 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) { getStatusBarTemplate (callback) {