2020-12-21 21:19:00 +01:00
|
|
|
'use strict';
|
|
|
|
|
2011-12-04 16:33:56 +01:00
|
|
|
/**
|
2015-12-31 13:19:23 +01:00
|
|
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
2011-12-04 16:33:56 +01:00
|
|
|
* This helps other people to understand this code better and helps them to improve it.
|
|
|
|
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
|
|
|
|
*/
|
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Google Inc.
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const chat = require('./chat').chat;
|
|
|
|
const hooks = require('./pluginfw/hooks');
|
2021-02-21 16:07:39 +01:00
|
|
|
const browser = require('./vendors/browser');
|
2012-01-16 05:16:11 +01:00
|
|
|
|
2012-01-17 09:43:11 +01:00
|
|
|
// Dependency fill on init. This exists for `pad.socket` only.
|
|
|
|
// TODO: bind directly to the socket.
|
2020-11-23 19:24:19 +01:00
|
|
|
let pad = undefined;
|
2020-12-21 21:19:00 +01:00
|
|
|
const getSocket = () => pad && pad.socket;
|
2012-01-17 09:43:11 +01:00
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
/** Call this when the document is ready, and a new Ace2Editor() has been created and inited.
|
|
|
|
ACE's ready callback does not need to have fired yet.
|
|
|
|
"serverVars" are from calling doc.getCollabClientVars() on the server. */
|
2021-02-17 16:03:30 +01:00
|
|
|
const getCollabClient = (ace2editor, serverVars, initialUserInfo, options, _pad) => {
|
2020-11-23 19:24:19 +01:00
|
|
|
const editor = ace2editor;
|
2012-01-17 09:43:11 +01:00
|
|
|
pad = _pad; // Inject pad to avoid a circular dependency.
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
let rev = serverVars.rev;
|
2021-03-27 01:02:22 +01:00
|
|
|
let committing = false;
|
2020-11-23 19:24:19 +01:00
|
|
|
let stateMessage;
|
|
|
|
let channelState = 'CONNECTING';
|
|
|
|
let lastCommitTime = 0;
|
|
|
|
let initialStartConnectTime = 0;
|
2021-04-01 21:52:39 +02:00
|
|
|
let commitDelay = 500;
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const userId = initialUserInfo.userId;
|
|
|
|
// var socket;
|
|
|
|
const userSet = {}; // userId -> userInfo
|
2011-03-26 14:10:41 +01:00
|
|
|
userSet[userId] = initialUserInfo;
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
let isPendingRevision = false;
|
2018-02-10 18:00:22 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const callbacks = {
|
2020-12-21 21:19:00 +01:00
|
|
|
onUserJoin: () => {},
|
|
|
|
onUserLeave: () => {},
|
|
|
|
onUpdateUserInfo: () => {},
|
|
|
|
onChannelStateChange: () => {},
|
|
|
|
onClientMessage: () => {},
|
|
|
|
onInternalAction: () => {},
|
|
|
|
onConnectionTrouble: () => {},
|
|
|
|
onServerMessage: () => {},
|
2011-03-26 14:10:41 +01:00
|
|
|
};
|
2020-11-23 19:24:19 +01:00
|
|
|
if (browser.firefox) {
|
2011-03-26 14:10:41 +01:00
|
|
|
// Prevent "escape" from taking effect and canceling a comet connection;
|
|
|
|
// doesn't work if focus is on an iframe.
|
2020-11-23 19:24:19 +01:00
|
|
|
$(window).bind('keydown', (evt) => {
|
2020-12-21 21:19:00 +01:00
|
|
|
if (evt.which === 27) {
|
2020-11-23 19:24:19 +01:00
|
|
|
evt.preventDefault();
|
2011-07-07 19:59:34 +02:00
|
|
|
}
|
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const handleUserChanges = () => {
|
2021-03-29 08:50:32 +02:00
|
|
|
if (editor.getInInternationalComposition()) {
|
|
|
|
// handleUserChanges() will be called again once composition ends so there's no need to set up
|
|
|
|
// a future call before returning.
|
|
|
|
return;
|
|
|
|
}
|
2021-03-27 01:20:54 +01:00
|
|
|
const now = Date.now();
|
2020-12-21 21:19:00 +01:00
|
|
|
if ((!getSocket()) || channelState === 'CONNECTING') {
|
2021-03-27 01:20:54 +01:00
|
|
|
if (channelState === 'CONNECTING' && (now - initialStartConnectTime) > 20000) {
|
2020-11-23 19:24:19 +01:00
|
|
|
setChannelState('DISCONNECTED', 'initsocketfail');
|
|
|
|
} else {
|
2011-03-26 14:10:41 +01:00
|
|
|
// check again in a bit
|
2021-03-27 00:46:46 +01:00
|
|
|
setTimeout(handleUserChanges, 1000);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-27 01:02:22 +01:00
|
|
|
if (committing) {
|
2021-03-29 08:50:32 +02:00
|
|
|
if (now - lastCommitTime > 20000) {
|
2011-03-26 14:10:41 +01:00
|
|
|
// a commit is taking too long
|
2020-11-23 19:24:19 +01:00
|
|
|
setChannelState('DISCONNECTED', 'slowcommit');
|
2021-03-29 08:50:32 +02:00
|
|
|
} else if (now - lastCommitTime > 5000) {
|
2020-11-23 19:24:19 +01:00
|
|
|
callbacks.onConnectionTrouble('SLOW');
|
|
|
|
} else {
|
2011-03-26 14:10:41 +01:00
|
|
|
// run again in a few seconds, to detect a disconnect
|
2021-03-27 00:46:46 +01:00
|
|
|
setTimeout(handleUserChanges, 3000);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-01 21:52:39 +02:00
|
|
|
const earliestCommit = lastCommitTime + commitDelay;
|
2021-03-27 01:20:54 +01:00
|
|
|
if (now < earliestCommit) {
|
|
|
|
setTimeout(handleUserChanges, earliestCommit - now);
|
2011-03-26 14:10:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
let sentMessage = false;
|
2018-04-03 15:21:14 +02:00
|
|
|
// Check if there are any pending revisions to be received from server.
|
|
|
|
// Allow only if there are no pending revisions to be received from server
|
2020-11-23 19:24:19 +01:00
|
|
|
if (!isPendingRevision) {
|
|
|
|
const userChangesData = editor.prepareUserChangeset();
|
|
|
|
if (userChangesData.changeset) {
|
2021-03-27 01:20:54 +01:00
|
|
|
lastCommitTime = now;
|
2021-03-27 01:02:22 +01:00
|
|
|
committing = true;
|
2020-11-23 19:24:19 +01:00
|
|
|
stateMessage = {
|
|
|
|
type: 'USER_CHANGES',
|
|
|
|
baseRev: rev,
|
|
|
|
changeset: userChangesData.changeset,
|
|
|
|
apool: userChangesData.apool,
|
|
|
|
};
|
|
|
|
sendMessage(stateMessage);
|
|
|
|
sentMessage = true;
|
|
|
|
callbacks.onInternalAction('commitPerformed');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// run again in a few seconds, to check if there was a reconnection attempt
|
2021-03-27 00:46:46 +01:00
|
|
|
setTimeout(handleUserChanges, 3000);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (sentMessage) {
|
2011-03-26 14:10:41 +01:00
|
|
|
// run again in a few seconds, to detect a disconnect
|
2021-03-27 00:46:46 +01:00
|
|
|
setTimeout(handleUserChanges, 3000);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2021-03-27 00:33:14 +01:00
|
|
|
const acceptCommit = () => {
|
|
|
|
editor.applyPreparedChangesetToBase();
|
|
|
|
setStateIdle();
|
2021-03-27 00:46:46 +01:00
|
|
|
try {
|
2021-03-27 00:33:14 +01:00
|
|
|
callbacks.onInternalAction('commitAcceptedByServer');
|
|
|
|
callbacks.onConnectionTrouble('OK');
|
2021-03-27 00:46:46 +01:00
|
|
|
} catch (err) { /* intentionally ignored */ }
|
2021-03-27 00:33:14 +01:00
|
|
|
handleUserChanges();
|
|
|
|
};
|
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const setUpSocket = () => {
|
2020-11-23 19:24:19 +01:00
|
|
|
setChannelState('CONNECTED');
|
2011-07-07 19:59:34 +02:00
|
|
|
doDeferredActions();
|
|
|
|
|
2021-03-27 01:20:54 +01:00
|
|
|
initialStartConnectTime = Date.now();
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const sendMessage = (msg) => {
|
2012-01-17 09:43:11 +01:00
|
|
|
getSocket().json.send(
|
2020-11-23 19:24:19 +01:00
|
|
|
{
|
|
|
|
type: 'COLLABROOM',
|
|
|
|
component: 'pad',
|
|
|
|
data: msg,
|
|
|
|
});
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2021-03-29 08:50:32 +02:00
|
|
|
const serverMessageTaskQueue = new class {
|
|
|
|
constructor() {
|
|
|
|
this._promiseChain = Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
async enqueue(fn) {
|
|
|
|
const taskPromise = this._promiseChain.then(fn);
|
|
|
|
// Use .catch() to prevent rejections from halting the queue.
|
|
|
|
this._promiseChain = taskPromise.catch(() => {});
|
|
|
|
// Do NOT do `return await this._promiseChain;` because the caller would not see an error if
|
|
|
|
// fn() throws/rejects (due to the .catch() added above).
|
|
|
|
return await taskPromise;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const handleMessageFromServer = (evt) => {
|
2012-01-17 09:43:11 +01:00
|
|
|
if (!getSocket()) return;
|
2011-07-07 19:59:34 +02:00
|
|
|
if (!evt.data) return;
|
2020-11-23 19:24:19 +01:00
|
|
|
const wrapper = evt;
|
2020-12-21 21:19:00 +01:00
|
|
|
if (wrapper.type !== 'COLLABROOM' && wrapper.type !== 'CUSTOM') return;
|
2020-11-23 19:24:19 +01:00
|
|
|
const msg = wrapper.data;
|
2013-03-16 14:19:12 +01:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
if (msg.type === 'NEW_CHANGES') {
|
2021-03-29 08:50:32 +02:00
|
|
|
serverMessageTaskQueue.enqueue(async () => {
|
|
|
|
// Avoid updating the DOM while the user is composing a character. Notes about this `await`:
|
|
|
|
// * `await null;` is equivalent to `await Promise.resolve(null);`, so if the user is not
|
|
|
|
// currently composing a character then execution will continue without error.
|
|
|
|
// * We assume that it is not possible for a new 'compositionstart' event to fire after
|
|
|
|
// the `await` but before the next line of code after the `await` (or, if it is
|
|
|
|
// possible, that the chances are so small or the consequences so minor that it's not
|
|
|
|
// worth addressing).
|
|
|
|
await editor.getInInternationalComposition();
|
|
|
|
const {newRev, changeset, author = '', apool} = msg;
|
|
|
|
if (newRev !== (rev + 1)) {
|
|
|
|
window.console.warn(`bad message revision on NEW_CHANGES: ${newRev} not ${rev + 1}`);
|
2013-02-27 20:29:59 +01:00
|
|
|
// setChannelState("DISCONNECTED", "badmessage_newchanges");
|
2012-09-13 16:19:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-03-29 08:50:32 +02:00
|
|
|
rev = newRev;
|
|
|
|
editor.applyChangesToBase(changeset, author, apool);
|
|
|
|
});
|
2020-12-21 21:19:00 +01:00
|
|
|
} else if (msg.type === 'ACCEPT_COMMIT') {
|
2021-03-29 08:50:32 +02:00
|
|
|
serverMessageTaskQueue.enqueue(() => {
|
|
|
|
const newRev = msg.newRev;
|
|
|
|
if (newRev !== (rev + 1)) {
|
|
|
|
window.console.warn(`bad message revision on ACCEPT_COMMIT: ${newRev} not ${rev + 1}`);
|
2013-02-27 20:29:59 +01:00
|
|
|
// setChannelState("DISCONNECTED", "badmessage_acceptcommit");
|
2012-09-13 16:19:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-03-29 08:50:32 +02:00
|
|
|
rev = newRev;
|
|
|
|
acceptCommit();
|
|
|
|
});
|
2020-12-21 21:19:00 +01:00
|
|
|
} else if (msg.type === 'CLIENT_RECONNECT') {
|
|
|
|
// Server sends a CLIENT_RECONNECT message when there is a client reconnect.
|
|
|
|
// Server also returns all pending revisions along with this CLIENT_RECONNECT message
|
2021-03-29 08:50:32 +02:00
|
|
|
serverMessageTaskQueue.enqueue(() => {
|
|
|
|
if (msg.noChanges) {
|
|
|
|
// If no revisions are pending, just make everything normal
|
|
|
|
setIsPendingRevision(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const {headRev, newRev, changeset, author = '', apool} = msg;
|
|
|
|
if (newRev !== (rev + 1)) {
|
|
|
|
window.console.warn(`bad message revision on CLIENT_RECONNECT: ${newRev} not ${rev + 1}`);
|
2018-02-15 21:01:47 +01:00
|
|
|
// setChannelState("DISCONNECTED", "badmessage_acceptcommit");
|
|
|
|
return;
|
2018-02-10 18:00:22 +01:00
|
|
|
}
|
2021-03-29 08:50:32 +02:00
|
|
|
rev = newRev;
|
|
|
|
if (author === pad.getUserId()) {
|
|
|
|
acceptCommit();
|
|
|
|
} else {
|
|
|
|
editor.applyChangesToBase(changeset, author, apool);
|
|
|
|
}
|
|
|
|
if (newRev === headRev) {
|
|
|
|
// Once we have applied all pending revisions, make everything normal
|
|
|
|
setIsPendingRevision(false);
|
|
|
|
}
|
|
|
|
});
|
2020-12-21 21:19:00 +01:00
|
|
|
} else if (msg.type === 'USER_NEWINFO') {
|
|
|
|
const userInfo = msg.userInfo;
|
|
|
|
const id = userInfo.userId;
|
2012-09-15 23:48:04 +02:00
|
|
|
|
|
|
|
// Avoid a race condition when setting colors. If our color was set by a
|
|
|
|
// query param, ignore our own "new user" message's color value.
|
2020-11-23 19:24:19 +01:00
|
|
|
if (id === initialUserInfo.userId && initialUserInfo.globalUserColor) {
|
2012-09-15 23:48:04 +02:00
|
|
|
msg.userInfo.colorId = initialUserInfo.globalUserColor;
|
|
|
|
}
|
|
|
|
|
2015-12-31 13:19:23 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (userSet[id]) {
|
2011-03-26 14:10:41 +01:00
|
|
|
userSet[id] = userInfo;
|
|
|
|
callbacks.onUpdateUserInfo(userInfo);
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2011-03-26 14:10:41 +01:00
|
|
|
userSet[id] = userInfo;
|
|
|
|
callbacks.onUserJoin(userInfo);
|
|
|
|
}
|
|
|
|
tellAceActiveAuthorInfo(userInfo);
|
2020-12-21 21:19:00 +01:00
|
|
|
} else if (msg.type === 'USER_LEAVE') {
|
|
|
|
const userInfo = msg.userInfo;
|
|
|
|
const id = userInfo.userId;
|
2020-11-23 19:24:19 +01:00
|
|
|
if (userSet[id]) {
|
2011-03-26 14:10:41 +01:00
|
|
|
delete userSet[userInfo.userId];
|
|
|
|
fadeAceAuthorInfo(userInfo);
|
|
|
|
callbacks.onUserLeave(userInfo);
|
|
|
|
}
|
2020-12-21 21:19:00 +01:00
|
|
|
} else if (msg.type === 'CLIENT_MESSAGE') {
|
2011-03-26 14:10:41 +01:00
|
|
|
callbacks.onClientMessage(msg.payload);
|
2020-12-21 21:19:00 +01:00
|
|
|
} else if (msg.type === 'CHAT_MESSAGE') {
|
2013-01-06 16:11:48 +01:00
|
|
|
chat.addMessage(msg, true, false);
|
2020-12-21 21:19:00 +01:00
|
|
|
} else if (msg.type === 'CHAT_MESSAGES') {
|
2020-11-23 19:24:19 +01:00
|
|
|
for (let i = msg.messages.length - 1; i >= 0; i--) {
|
2013-01-06 16:11:48 +01:00
|
|
|
chat.addMessage(msg.messages[i], true, true);
|
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
if (!chat.gotInitalMessages) {
|
2013-01-06 16:11:48 +01:00
|
|
|
chat.scrollDown();
|
2013-01-07 17:36:03 +01:00
|
|
|
chat.gotInitalMessages = true;
|
|
|
|
chat.historyPointer = clientVars.chatHead - msg.messages.length;
|
2013-01-06 16:11:48 +01:00
|
|
|
}
|
2013-01-07 19:15:55 +01:00
|
|
|
|
|
|
|
// messages are loaded, so hide the loading-ball
|
2020-11-23 19:24:19 +01:00
|
|
|
$('#chatloadmessagesball').css('display', 'none');
|
2013-01-07 19:15:55 +01:00
|
|
|
|
|
|
|
// there are less than 100 messages or we reached the top
|
2020-12-21 21:19:00 +01:00
|
|
|
if (chat.historyPointer <= 0) {
|
|
|
|
$('#chatloadmessagesbutton').css('display', 'none');
|
|
|
|
} else {
|
|
|
|
// there are still more messages, re-show the load-button
|
|
|
|
$('#chatloadmessagesbutton').css('display', 'block');
|
|
|
|
}
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2015-12-31 13:19:23 +01:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
// HACKISH: User messages do not have "payload" but "userInfo", so that all
|
|
|
|
// "handleClientMessage_USER_" hooks would work, populate payload
|
|
|
|
// FIXME: USER_* messages to have "payload" property instead of "userInfo",
|
|
|
|
// seems like a quite a big work
|
2020-11-23 19:24:19 +01:00
|
|
|
if (msg.type.indexOf('USER_') > -1) {
|
2015-12-31 13:19:23 +01:00
|
|
|
msg.payload = msg.userInfo;
|
|
|
|
}
|
2020-06-12 13:26:33 +02:00
|
|
|
// Similar for NEW_CHANGES
|
2020-11-23 19:24:19 +01:00
|
|
|
if (msg.type === 'NEW_CHANGES') msg.payload = msg;
|
2020-06-12 13:26:33 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
hooks.callAll(`handleClientMessage_${msg.type}`, {payload: msg.payload});
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const updateUserInfo = (userInfo) => {
|
2011-03-26 14:10:41 +01:00
|
|
|
userInfo.userId = userId;
|
|
|
|
userSet[userId] = userInfo;
|
|
|
|
tellAceActiveAuthorInfo(userInfo);
|
2012-01-17 09:43:11 +01:00
|
|
|
if (!getSocket()) return;
|
2011-07-07 19:59:34 +02:00
|
|
|
sendMessage(
|
2020-11-23 19:24:19 +01:00
|
|
|
{
|
|
|
|
type: 'USERINFO_UPDATE',
|
|
|
|
userInfo,
|
|
|
|
});
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const tellAceActiveAuthorInfo = (userInfo) => {
|
2011-03-26 14:10:41 +01:00
|
|
|
tellAceAuthorInfo(userInfo.userId, userInfo.colorId);
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const tellAceAuthorInfo = (userId, colorId, inactive) => {
|
2020-11-23 19:24:19 +01:00
|
|
|
if (typeof colorId === 'number') {
|
2011-08-21 19:53:30 +02:00
|
|
|
colorId = clientVars.colorPalette[colorId];
|
|
|
|
}
|
2015-12-31 13:19:23 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const cssColor = colorId;
|
|
|
|
if (inactive) {
|
2011-08-20 19:22:10 +02:00
|
|
|
editor.setAuthorInfo(userId, {
|
|
|
|
bgcolor: cssColor,
|
2020-11-23 19:24:19 +01:00
|
|
|
fade: 0.5,
|
2011-08-20 19:22:10 +02:00
|
|
|
});
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2011-08-20 19:22:10 +02:00
|
|
|
editor.setAuthorInfo(userId, {
|
2020-11-23 19:24:19 +01:00
|
|
|
bgcolor: cssColor,
|
2011-08-20 19:22:10 +02:00
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const fadeAceAuthorInfo = (userInfo) => {
|
2011-03-26 14:10:41 +01:00
|
|
|
tellAceAuthorInfo(userInfo.userId, userInfo.colorId, true);
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const getConnectedUsers = () => valuesArray(userSet);
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const tellAceAboutHistoricalAuthors = (hadata) => {
|
|
|
|
for (const [author, data] of Object.entries(hadata)) {
|
2020-11-23 19:24:19 +01:00
|
|
|
if (!userSet[author]) {
|
2011-03-26 14:10:41 +01:00
|
|
|
tellAceAuthorInfo(author, data.colorId, true);
|
|
|
|
}
|
|
|
|
}
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const setChannelState = (newChannelState, moreInfo) => {
|
|
|
|
if (newChannelState !== channelState) {
|
2011-03-26 14:10:41 +01:00
|
|
|
channelState = newChannelState;
|
|
|
|
callbacks.onChannelStateChange(channelState, moreInfo);
|
|
|
|
}
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const valuesArray = (obj) => {
|
2020-11-23 19:24:19 +01:00
|
|
|
const array = [];
|
|
|
|
$.each(obj, (k, v) => {
|
2011-07-07 19:59:34 +02:00
|
|
|
array.push(v);
|
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
return array;
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
|
|
|
// We need to present a working interface even before the socket
|
|
|
|
// is connected for the first time.
|
2020-11-23 19:24:19 +01:00
|
|
|
let deferredActions = [];
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const defer = (func, tag) => function (...args) {
|
|
|
|
const action = () => {
|
|
|
|
func.call(this, ...args);
|
2020-11-23 19:24:19 +01:00
|
|
|
};
|
2020-12-21 21:19:00 +01:00
|
|
|
action.tag = tag;
|
|
|
|
if (channelState === 'CONNECTING') {
|
|
|
|
deferredActions.push(action);
|
|
|
|
} else {
|
|
|
|
action();
|
|
|
|
}
|
|
|
|
};
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const doDeferredActions = (tag) => {
|
2020-11-23 19:24:19 +01:00
|
|
|
const newArray = [];
|
|
|
|
for (let i = 0; i < deferredActions.length; i++) {
|
|
|
|
const a = deferredActions[i];
|
2020-12-21 21:19:00 +01:00
|
|
|
if ((!tag) || (tag === a.tag)) {
|
2011-03-26 14:10:41 +01:00
|
|
|
a();
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2011-03-26 14:10:41 +01:00
|
|
|
newArray.push(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deferredActions = newArray;
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const sendClientMessage = (msg) => {
|
2011-07-07 19:59:34 +02:00
|
|
|
sendMessage(
|
2020-11-23 19:24:19 +01:00
|
|
|
{
|
|
|
|
type: 'CLIENT_MESSAGE',
|
|
|
|
payload: msg,
|
|
|
|
});
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const getCurrentRevisionNumber = () => rev;
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const getMissedChanges = () => {
|
2020-11-23 19:24:19 +01:00
|
|
|
const obj = {};
|
2011-03-26 14:10:41 +01:00
|
|
|
obj.userInfo = userSet[userId];
|
|
|
|
obj.baseRev = rev;
|
2021-03-27 01:02:22 +01:00
|
|
|
if (committing && stateMessage) {
|
2011-03-26 14:10:41 +01:00
|
|
|
obj.committedChangeset = stateMessage.changeset;
|
|
|
|
obj.committedChangesetAPool = stateMessage.apool;
|
|
|
|
editor.applyPreparedChangesetToBase();
|
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
const userChangesData = editor.prepareUserChangeset();
|
|
|
|
if (userChangesData.changeset) {
|
2011-03-26 14:10:41 +01:00
|
|
|
obj.furtherChangeset = userChangesData.changeset;
|
|
|
|
obj.furtherChangesetAPool = userChangesData.apool;
|
|
|
|
}
|
|
|
|
return obj;
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const setStateIdle = () => {
|
2021-03-27 01:02:22 +01:00
|
|
|
committing = false;
|
2020-11-23 19:24:19 +01:00
|
|
|
callbacks.onInternalAction('newlyIdle');
|
2011-03-26 14:10:41 +01:00
|
|
|
schedulePerhapsCallIdleFuncs();
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const setIsPendingRevision = (value) => {
|
2018-04-03 15:21:14 +02:00
|
|
|
isPendingRevision = value;
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const idleFuncs = [];
|
2018-04-03 15:21:14 +02:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const callWhenNotCommitting = (func) => {
|
2011-03-26 14:10:41 +01:00
|
|
|
idleFuncs.push(func);
|
|
|
|
schedulePerhapsCallIdleFuncs();
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
const schedulePerhapsCallIdleFuncs = () => {
|
2020-11-23 19:24:19 +01:00
|
|
|
setTimeout(() => {
|
2021-03-27 01:02:22 +01:00
|
|
|
if (!committing) {
|
2020-11-23 19:24:19 +01:00
|
|
|
while (idleFuncs.length > 0) {
|
|
|
|
const f = idleFuncs.shift();
|
2011-03-26 14:10:41 +01:00
|
|
|
f();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 0);
|
2020-12-21 21:19:00 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const self = {
|
2020-12-21 21:19:00 +01:00
|
|
|
setOnUserJoin: (cb) => {
|
2011-07-07 19:59:34 +02:00
|
|
|
callbacks.onUserJoin = cb;
|
|
|
|
},
|
2020-12-21 21:19:00 +01:00
|
|
|
setOnUserLeave: (cb) => {
|
2011-07-07 19:59:34 +02:00
|
|
|
callbacks.onUserLeave = cb;
|
|
|
|
},
|
2020-12-21 21:19:00 +01:00
|
|
|
setOnUpdateUserInfo: (cb) => {
|
2011-07-07 19:59:34 +02:00
|
|
|
callbacks.onUpdateUserInfo = cb;
|
|
|
|
},
|
2020-12-21 21:19:00 +01:00
|
|
|
setOnChannelStateChange: (cb) => {
|
2011-07-07 19:59:34 +02:00
|
|
|
callbacks.onChannelStateChange = cb;
|
|
|
|
},
|
2020-12-21 21:19:00 +01:00
|
|
|
setOnClientMessage: (cb) => {
|
2011-07-07 19:59:34 +02:00
|
|
|
callbacks.onClientMessage = cb;
|
|
|
|
},
|
2020-12-21 21:19:00 +01:00
|
|
|
setOnInternalAction: (cb) => {
|
2011-07-07 19:59:34 +02:00
|
|
|
callbacks.onInternalAction = cb;
|
|
|
|
},
|
2020-12-21 21:19:00 +01:00
|
|
|
setOnConnectionTrouble: (cb) => {
|
2011-07-07 19:59:34 +02:00
|
|
|
callbacks.onConnectionTrouble = cb;
|
|
|
|
},
|
2011-03-26 14:10:41 +01:00
|
|
|
updateUserInfo: defer(updateUserInfo),
|
2020-11-23 19:24:19 +01:00
|
|
|
handleMessageFromServer,
|
|
|
|
getConnectedUsers,
|
|
|
|
sendClientMessage,
|
|
|
|
sendMessage,
|
|
|
|
getCurrentRevisionNumber,
|
|
|
|
getMissedChanges,
|
|
|
|
callWhenNotCommitting,
|
2011-11-26 00:24:10 +01:00
|
|
|
addHistoricalAuthors: tellAceAboutHistoricalAuthors,
|
2020-11-23 19:24:19 +01:00
|
|
|
setChannelState,
|
|
|
|
setStateIdle,
|
|
|
|
setIsPendingRevision,
|
2021-04-01 21:52:39 +02:00
|
|
|
set commitDelay(ms) { commitDelay = ms; },
|
|
|
|
get commitDelay() { return commitDelay; },
|
2012-01-29 02:05:29 +01:00
|
|
|
};
|
|
|
|
|
2020-12-21 21:19:00 +01:00
|
|
|
tellAceAboutHistoricalAuthors(serverVars.historicalAuthorData);
|
|
|
|
tellAceActiveAuthorInfo(initialUserInfo);
|
|
|
|
|
|
|
|
editor.setProperty('userAuthor', userId);
|
|
|
|
editor.setBaseAttributedText(serverVars.initialAttributedText, serverVars.apool);
|
2021-03-27 00:46:46 +01:00
|
|
|
editor.setUserChangeNotificationCallback(handleUserChanges);
|
2020-12-21 21:19:00 +01:00
|
|
|
|
2020-07-10 16:28:32 +02:00
|
|
|
setUpSocket();
|
2012-01-29 02:05:29 +01:00
|
|
|
return self;
|
2021-02-17 16:03:30 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2012-01-16 02:23:48 +01:00
|
|
|
exports.getCollabClient = getCollabClient;
|