-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
134 lines (119 loc) · 4.28 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* hive.js
* Copyright (C) 2013-2016 Marcel Klehr <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public License version 2
* as published by the Mozilla Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the Mozilla Public License
* along with this program. If not, see <https://www.mozilla.org/en-US/MPL/2.0/>.
*/
var vdom = require('virtual-dom')
, h = vdom.h
, jsonParse = require('json-stream')
, through = require('through2')
const UPDATE_CURSORS = 'CURSORBROADCASTCODEMIRROR_UPDATE_CURSORS'
module.exports = setup
module.exports.consumes = ['ui', 'editor', 'presence']
module.exports.provides = ['cursorBroadcastCodemirror']
function setup(plugin, imports, register) {
var ui = imports.ui
, editor = imports.editor
ui.reduxReducerMap.cursorBroadcastCodemirror = reducer
function reducer(state, action) {
if(!state) {
return {
cursors: {}
}
}
if(UPDATE_CURSORS === action.type) {
var newState = {...state, cursors: {...state.cursors, ...action.payload}}
for(var userId in newState.cursors) {
if(!newState.cursors[userId]) delete newState.cursors[userId]
}
return newState
}
return state
}
var cursorBroadcast = {
action_updateCursors: function(cursors) {
return {type: UPDATE_CURSORS, payload: cursors}
}
, stream: null
, markers: []
}
editor.onLoad((editableDocument, broadcast, onClose) => {
// This plugin works with ckeditor only
if(ui.store.getState().editor.editor !== 'CodeMirror') return
var lastCursors
var dispose = ui.store.subscribe(function() {
var state = ui.store.getState()
, cursors = state.cursorBroadcastCodemirror.cursors
, currentAuthor = state.session.user.id
if(cursors === lastCursors) return
lastCursors = cursors
// remove the old markers
cursorBroadcast.markers.forEach((marker) => {
marker.clear()
})
// add the new markers
Object.keys(cursors)
//.filter((authorId) => authorId !== currentAuthor)
.filter((authorId) => !!state.presence.users[authorId])
.forEach((authorId) => {
cursorBroadcast.markers = cursorBroadcast.markers.concat(
cursors[authorId].map((sel) => {
var user = state.presence.users[authorId]
, empty = (sel.anchor.line === sel.head.line && sel.anchor.ch === sel.head.ch)
if(empty) {
return editableDocument.codemirror.markText(sel.anchor, sel.head, {
clearWhenEmpty: false
, replacedWith: vdom.create(h('span', {
title: user.attributes.name
, style: {
border:'2px solid '+(user.attributes.color || '#777')
, display: 'inline-block'
, width: '0'
}
}, ' '))
})
}else{
return editableDocument.codemirror.markText(sel.anchor, sel.head, {
title: user.attributes.name
, clearWhenEmpty: false
, css: 'border: 2px solid '+(user.attributes.color || '#777')
})
}
})
)
})
})
cursorBroadcast.stream = broadcast.createDuplexStream(new Buffer('codemirror-cursors'))
// As soon as doc is initialized, listen on broadcast
editableDocument.on('init', function() {
cursorBroadcast.stream
.pipe(jsonParse())
.pipe(through.obj(function(cursors, enc, cb) {
// update
ui.store.dispatch(cursorBroadcast.action_updateCursors(cursors))
cb()
}))
})
editableDocument.codemirror.on("cursorActivity", collectCursor)
function collectCursor() {
var sel = editableDocument.codemirror.listSelections()
cursorBroadcast.stream.write(JSON.stringify(sel)+'\n')
}
onClose(_=> {
dispose()
editableDocument.codemirror.off('cursorActivity', collectCursor)
})
})
register(null, {cursorBroadcastCodemirror: cursorBroadcast})
}