Skip to content

Commit 81666a7

Browse files
committed
Impl multiple codemirror event listener
1 parent fff7ebd commit 81666a7

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

public/js/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,7 +2104,7 @@ function iterateLine (line) {
21042104
}
21052105
}
21062106
}
2107-
editor.on('update', function () {
2107+
editorInstance.on('update', function () {
21082108
$('.authorship-gutter:not([data-original-title])').tooltip({
21092109
container: '.CodeMirror-lines',
21102110
placement: 'right',
@@ -2655,7 +2655,7 @@ function enforceMaxLength (cm, change) {
26552655
return false
26562656
}
26572657
var ignoreEmitEvents = ['setValue', 'ignoreHistory']
2658-
editor.on('beforeChange', function (cm, change) {
2658+
editorInstance.on('beforeChange', function (cm, change) {
26592659
if (debug) { console.debug(change) }
26602660
removeNullByte(cm, change)
26612661
if (enforceMaxLength(cm, change)) {
@@ -2683,13 +2683,13 @@ editor.on('beforeChange', function (cm, change) {
26832683
}
26842684
if (cmClient && !socket.connected) { cmClient.editorAdapter.ignoreNextChange = true }
26852685
})
2686-
editor.on('cut', function () {
2686+
editorInstance.on('cut', function () {
26872687
// na
26882688
})
2689-
editor.on('paste', function () {
2689+
editorInstance.on('paste', function () {
26902690
// na
26912691
})
2692-
editor.on('changes', function (cm, changes) {
2692+
editorInstance.on('changes', function (cm, changes) {
26932693
updateHistory()
26942694
var docLength = editor.getValue().length
26952695
// workaround for big documents
@@ -2713,7 +2713,7 @@ editor.on('changes', function (cm, changes) {
27132713
}
27142714
}
27152715
})
2716-
editor.on('focus', function (cm) {
2716+
editorInstance.on('focus', function (cm) {
27172717
for (var i = 0; i < window.onlineUsers.length; i++) {
27182718
if (window.onlineUsers[i].id === window.personalInfo.id) {
27192719
window.onlineUsers[i].cursor = editor.getCursor()
@@ -2722,11 +2722,11 @@ editor.on('focus', function (cm) {
27222722
window.personalInfo['cursor'] = editor.getCursor()
27232723
socket.emit('cursor focus', editor.getCursor())
27242724
})
2725-
editor.on('cursorActivity', function (cm) {
2725+
editorInstance.on('cursorActivity', function (cm) {
27262726
updateStatusBar()
27272727
cursorActivity()
27282728
})
2729-
editor.on('beforeSelectionChange', function (doc, selections) {
2729+
editorInstance.on('beforeSelectionChange', function (doc, selections) {
27302730
if (selections) { selection = selections.ranges[0] } else { selection = null }
27312731
updateStatusBar()
27322732
})
@@ -2744,7 +2744,7 @@ function cursorActivityInner () {
27442744
socket.emit('cursor activity', editor.getCursor())
27452745
}
27462746
}
2747-
editor.on('blur', function (cm) {
2747+
editorInstance.on('blur', function (cm) {
27482748
for (var i = 0; i < window.onlineUsers.length; i++) {
27492749
if (window.onlineUsers[i].id === window.personalInfo.id) {
27502750
window.onlineUsers[i].cursor = null

public/js/lib/editor/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ export default class Editor {
116116
utils.wrapTextWith(this.editor, cm, 'Backspace')
117117
}
118118
}
119+
this.eventListeners = {}
120+
}
121+
122+
on (event, cb) {
123+
if (!this.eventListeners[event]) {
124+
this.eventListeners[event] = [cb]
125+
} else {
126+
this.eventListeners[event].push(cb)
127+
}
128+
129+
this.editor.on(event, (...args) => {
130+
this.eventListeners[event].forEach(cb => cb(...args))
131+
})
119132
}
120133

121134
getStatusBarTemplate (callback) {

0 commit comments

Comments
 (0)