-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.js
237 lines (184 loc) · 6.3 KB
/
main.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var ShareDB = require('sharedb/lib/client');
var Quill = require('quill');
var ReconnectingWebSocket = require('reconnectingwebsocket');
var cursors = require('./cursors' );
var utils = require('./utils');
import QuillCursors from 'quill-cursors/src/cursors';
ShareDB.types.register(require('rich-text').type);
Quill.register('modules/cursors', QuillCursors);
var shareDBSocket = new ReconnectingWebSocket(((location.protocol === 'https:') ? 'wss' : 'ws') + '://' + window.location.host + '/sharedb');
var shareDBConnection = new ShareDB.Connection(shareDBSocket);
var quill = window.quill = new Quill('#editor', {
theme: 'snow',
modules: {
cursors: {
autoRegisterListener: false
},
history: {
userOnly: true
}
},
readOnly: true
});
var doc = shareDBConnection.get('documents', 'foobar');
var cursorsModule = quill.getModule('cursors');
doc.subscribe(function(err) {
if (err) throw err;
if (!doc.type)
doc.create([{
insert: '\n'
}], 'rich-text');
// update editor contents
quill.setContents(doc.data);
// local -> server
quill.on('text-change', function(delta, oldDelta, source) {
if (source == 'user') {
// Check if it's a formatting-only delta
var formattingDelta = delta.reduce(function (check, op) {
return (op.insert || op.delete) ? false : check;
}, true);
// If it's not a formatting-only delta, collapse local selection
if (
!formattingDelta &&
cursors.localConnection.range &&
cursors.localConnection.range.length
) {
cursors.localConnection.range.index += cursors.localConnection.range.length;
cursors.localConnection.range.length = 0;
cursors.update();
}
doc.submitOp(delta, {
source: quill
}, function(err) {
if (err)
console.error('Submit OP returned an error:', err);
});
updateUserList();
}
});
cursorsModule.registerTextChangeListener();
// server -> local
doc.on('op', function(op, source) {
if (source !== quill) {
quill.updateContents(op);
updateUserList();
}
});
//
function sendCursorData(range) {
cursors.localConnection.range = range;
cursors.update();
}
//
var debouncedSendCursorData = utils.debounce(function() {
var range = quill.getSelection();
if (range) {
console.log('[cursors] Stopped typing, sending a cursor update/refresh.');
sendCursorData(range);
}
}, 1500);
doc.on('nothing pending', debouncedSendCursorData);
function updateCursors(source) {
var activeConnections = {},
updateAll = Object.keys(cursorsModule.cursors).length == 0;
cursors.connections.forEach(function(connection) {
if (connection.id != cursors.localConnection.id) {
// Update cursor that sent the update, source (or update all if we're initting)
if ((connection.id == source.id || updateAll) && connection.range) {
cursorsModule.setCursor(
connection.id,
connection.range,
connection.name,
connection.color
);
}
// Add to active connections hashtable
activeConnections[connection.id] = connection;
}
});
// Clear 'disconnected' cursors
Object.keys(cursorsModule.cursors).forEach(function(cursorId) {
if (!activeConnections[cursorId]) {
cursorsModule.removeCursor(cursorId);
}
});
}
quill.on('selection-change', function(range, oldRange, source) {
sendCursorData(range);
});
document.addEventListener('cursors-update', function(e) {
// Handle Removed Connections
e.detail.removedConnections.forEach(function(connection) {
if (cursorsModule.cursors[connection.id])
cursorsModule.removeCursor(connection.id);
});
updateCursors(e.detail.source);
updateUserList();
});
updateCursors(cursors.localConnection);
});
window.cursors = cursors;
var usernameInputEl = document.getElementById('username-input');
var usersListEl = document.getElementById('users-list');
function updateUserList() {
// Wipe the slate clean
usersListEl.innerHTML = null;
cursors.connections.forEach(function(connection) {
var userItemEl = document.createElement('li');
var userNameEl = document.createElement('div');
var userDataEl = document.createElement('div');
userNameEl.innerHTML = '<strong>' + (connection.name || '(Waiting for username...)') + '</strong>';
userNameEl.classList.add('user-name');
if (connection.id == cursors.localConnection.id)
userNameEl.innerHTML += ' (You)';
if (connection.range) {
if (connection.id == cursors.localConnection.id)
connection.range = quill.getSelection();
userDataEl.innerHTML = [
'<div class="user-data">',
' <div>Index: ' + connection.range.index + '</div>',
' <div>Length: ' + connection.range.length + '</div>',
'</div>'
].join('');
} else
userDataEl.innerHTML = '(Not focusing on editor.)';
userItemEl.appendChild(userNameEl);
userItemEl.appendChild(userDataEl);
userItemEl.style.backgroundColor = connection.color;
usersListEl.appendChild(userItemEl);
});
}
usernameInputEl.value = chance.name();
usernameInputEl.focus();
usernameInputEl.select();
document.getElementById('username-form').addEventListener('submit', function(event) {
cursors.localConnection.name = usernameInputEl.value;
cursors.update();
quill.enable();
document.getElementById('connect-panel').style.display = 'none';
document.getElementById('users-panel').style.display = 'block';
event.preventDefault();
return false;
});
// DEBUG
var sharedbSocketStateEl = document.getElementById('sharedb-socket-state');
var sharedbSocketIndicatorEl = document.getElementById('sharedb-socket-indicator');
shareDBConnection.on('state', function(state, reason) {
var indicatorColor;
console.log('[sharedb] New connection state: ' + state + ' Reason: ' + reason);
sharedbSocketStateEl.innerHTML = state.toString();
switch (state.toString()) {
case 'connecting':
indicatorColor = 'silver';
break;
case 'connected':
indicatorColor = 'lime';
break;
case 'disconnected':
case 'closed':
case 'stopped':
indicatorColor = 'red';
break;
}
sharedbSocketIndicatorEl.style.backgroundColor = indicatorColor;
});