XWR-190: Make CryptPad version with rtwiki/CodeMirror

pull/1/head
mouhb 9 years ago
parent 9626dfc365
commit af80bed07b

@ -4,11 +4,13 @@ define([
//'RTWiki_ErrorBox', //'RTWiki_ErrorBox',
//'RTWiki_WebHome_chainpad' //'RTWiki_WebHome_chainpad'
'/common/crypto.js', '/common/crypto.js',
'/bower_components/jquery/dist/jquery.min.js',
'/code/errorbox.js', '/code/errorbox.js',
'/common/messages.js',
'/common/toolbar.js',
'/common/chainpad.js', '/common/chainpad.js',
'/common/otaml.js' '/common/otaml.js',
], function(Crypto, TextArea, ErrorBox) { '/bower_components/jquery/dist/jquery.min.js'
], function(Crypto, ErrorBox, Messages, Toolbar) {
var $ = window.jQuery; var $ = window.jQuery;
var ChainPad = window.ChainPad; var ChainPad = window.ChainPad;
var Otaml = window.Otaml; var Otaml = window.Otaml;
@ -643,21 +645,19 @@ define([
userName, userName,
channel, channel,
messages, messages,
demoMode,
language,
cryptKey) cryptKey)
{ {
debug("Opening websocket"); debug("Opening websocket");
var toolbar = createRealtimeToolbar('#cme_toolbox');/* To check */
var textArea = windowCM.document.getElementById('editor1'); var textArea = windowCM.document.getElementById('editor1');
var cmDiv = windowCM.document.getElementsByClassName('CodeMirror')[0]; var cmDiv = windowCM.document.getElementsByClassName('CodeMirror')[0];
var cmEditor = cmDiv.CodeMirror; var cmEditor = cmDiv.CodeMirror;
var onEvent = function () { };
var socket = new WebSocket(websocketUrl); var socket = new WebSocket(websocketUrl);
socket.onClose = []; socket.onClose = [];
socket.onMessage = []; socket.onMessage = [];
var initState = $(textArea).val(); var initState = $(textArea).val();
var realtime = socket.realtime = ChainPad.create(userName, 'x', channel, initState); var realtime = socket.realtime = ChainPad.create(userName, 'x', channel, initState);
var toolbar = realtime.toolbar = Toolbar.create(windowCM.$('#cme_toolbox'), userName, realtime);
// for debugging // for debugging
window.rtwiki_chainpad = realtime; window.rtwiki_chainpad = realtime;
@ -692,6 +692,7 @@ define([
if (socket.intentionallyClosing || isErrorState) { return false; } if (socket.intentionallyClosing || isErrorState) { return false; }
if (isSocketDisconnected(socket, realtime)) { if (isSocketDisconnected(socket, realtime)) {
realtime.abort(); realtime.abort();
realtime.toolbar.failed();
socket.close(); socket.close();
ErrorBox.show('disconnected'); ErrorBox.show('disconnected');
isErrorState = true; isErrorState = true;
@ -706,16 +707,16 @@ define([
var userListElement = createUserList(realtime, var userListElement = createUserList(realtime,
userName, userName,
toolbar.find('.rtwiki-toolbar-leftside'), [],
messages); messages);
userListElement.text(messages.initializing); userListElement.text(messages.initializing);
createLagElement(socket, createLagElement(socket,
realtime, realtime,
toolbar.find('.rtwiki-toolbar-rightside'), [],
messages); messages);
setAutosaveHiddenState(true); setAutosaveHiddenState(true);
socket.onEvent = function () { onEvent = function () {
if (isErrorState) { return; } if (isErrorState) { return; }
if (initializing) { return; } if (initializing) { return; }
var oldDocText = realtime.getUserDoc(); var oldDocText = realtime.getUserDoc();
@ -741,8 +742,6 @@ define([
realtime.message(message); realtime.message(message);
}); });
realtime.onMessage(function (message) { message = Crypto.encrypt(message, cryptKey);socket.send(message); }); realtime.onMessage(function (message) { message = Crypto.encrypt(message, cryptKey);socket.send(message); });
bindAllEvents(cmDiv, socket.onEvent, false);
$(textArea).attr("disabled", "disabled");
var userDocBeforePatch; var userDocBeforePatch;
var incomingPatch = function () { var incomingPatch = function () {
@ -787,24 +786,24 @@ define([
} }
}; };
realtime.onPatch(incomingPatch); realtime.onPatch(incomingPatch);
//**//
realtime.onUserListChange(function (userList) { realtime.onUserListChange(function (userList) {
if (initializing && userList.indexOf(userName) > -1) { if (initializing && userList.indexOf(userName) > -1) {
initializing = false; initializing = false;
cmEditor.setValue(realtime.getUserDoc());
incomingPatch(); incomingPatch();
/*$(textArea).val(realtime.getUserDoc());
TextArea.attach($(textArea)[0], realtime);
$(textArea).removeAttr("disabled");*/
} }
if (!initializing) { if (!initializing) {
updateUserList(userName, userListElement, userList, messages); updateUserList(userName, userListElement, userList, messages);
} }
}); });
debug("Bound websocket"); debug("Bound websocket");
bindAllEvents(cmDiv, onEvent, false);
setInterval(function () {
if (isErrorState || checkSocket()) {
toolbar.reconnecting();
}
}, 200);
realtime.start(); realtime.start();
toolbar.connected();
}; };
socket.onclose = function (evt) { socket.onclose = function (evt) {
for (var i = 0; i < socket.onClose.length; i++) { for (var i = 0; i < socket.onClose.length; i++) {
@ -829,13 +828,14 @@ define([
toolbar.remove(); toolbar.remove();
setAutosaveHiddenState(false); setAutosaveHiddenState(false);
}); });
socket.onEvent = function(){
onEvent();
};
return socket; return socket;
}; };
var cmEditor = function (cmWindow, websocketUrl, userName, messages, channel, demoMode, language, cryptkey) { var cmEditor = function (cmWindow, websocketUrl, userName, messages, channel, cryptkey) {
var cmTextarea = $(cmWindow.document.getElementById('editor1')); var cmTextarea = $(cmWindow.document.getElementById('editor1'));
if (!cmTextarea.length) { if (!cmTextarea.length) {
warn("WARNING: Could not find textarea to bind to"); warn("WARNING: Could not find textarea to bind to");
return; return;
@ -845,10 +845,8 @@ define([
userName, userName,
channel, channel,
messages, messages,
demoMode,
language,
cryptkey); cryptkey);
return { return {
onEvent: function() { onEvent: function() {
socket.onEvent(); socket.onEvent();
} }
@ -863,11 +861,7 @@ define([
if (!websocketUrl) { if (!websocketUrl) {
throw new Error("No WebSocket URL, please ensure Realtime Backend is installed."); throw new Error("No WebSocket URL, please ensure Realtime Backend is installed.");
} }
var demoMode = false; var cme = cmEditor(window, websocketUrl, userName, Messages, channel, cryptkey);
var language = "en";
var messages = [];
// Do not forget to remove demoMode and language variables, also check for messages
var cme = cmEditor(window, websocketUrl, userName, messages, channel, demoMode, language, cryptkey);
return { return {
onEvent: function () { onEvent: function () {
cme.onEvent(); cme.onEvent();

Loading…
Cancel
Save