cryptpad/www/code/main.js

144 lines
4.6 KiB
JavaScript
Raw Normal View History

define([
'/api/config?cb=' + Math.random().toString(16).substring(2),
// '/code/rt_codemirror.js',
'/common/messages.js',
'/common/crypto.js',
2016-04-22 22:15:39 +00:00
'/common/realtime-input.js',
'/common/TextPatcher.js',
'/bower_components/jquery/dist/jquery.min.js'
], function (Config, /*RTCode,*/ Messages, Crypto, Realtime, TextPatcher) {
var $ = window.jQuery;
var module = window.APP = {};
var ifrw = module.ifrw = $('#pad-iframe')[0].contentWindow;
$(function () {
2016-04-22 22:15:39 +00:00
var userName = Crypto.rand64(8);
var key;
var channel = '';
var hash = false;
2016-04-22 22:15:39 +00:00
if (!/#/.test(window.location.href)) {
key = Crypto.genKey();
} else {
hash = window.location.hash.slice(1);
2016-04-22 22:15:39 +00:00
channel = hash.slice(0, 32);
key = hash.slice(32);
}
2016-04-22 22:15:39 +00:00
var config = {
//initialState: Messages.codeInitialState,
2016-04-22 22:15:39 +00:00
userName: userName,
websocketURL: Config.websocketURL,
channel: channel,
cryptKey: key,
crypto: Crypto,
};
var andThen = function (CMeditor) {
2016-04-22 22:15:39 +00:00
var $pad = $('#pad-iframe');
var $textarea = $pad.contents().find('#editor1');
var editor = module.editor = CMeditor.fromTextArea($textarea[0], {
lineNumbers: true,
lineWrapping: true,
autoCloseBrackets: true,
matchBrackets : true,
showTrailingSpace : true,
styleActiveLine : true,
search: true,
highlightSelectionMatches: {showToken: /\w+/},
extraKeys: {"Ctrl-Q": function(cm){ cm.foldCode(cm.getCursor()); }},
foldGutter: true,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
mode: "javascript"
});
2016-04-22 22:15:39 +00:00
// TODO lock editor until chain is synced
// then unlock
var setEditable = function () { };
var canonicalize = function (t) { return t.replace(/\r\n/g, '\n'); };
2016-04-22 22:15:39 +00:00
var initializing = true;
var onInit = config.onInit = function (info) {
window.location.hash = info.channel + key;
var realtime = module.realtime = info.realtime;
2016-04-22 22:15:39 +00:00
module.patchText = TextPatcher.create({
realtime: realtime,
logging: true,
//initialState: Messages.codeInitialState
2016-04-22 22:15:39 +00:00
});
if (!hash) {
editor.setValue(Messages.codeInitialState);
module.patchText(Messages.codeInitialState);
module.patchText(Messages.codeInitialState);
editor.setValue(Messages.codeInitialState);
}
//$(window).on('hashchange', function() { window.location.reload(); });
2016-04-22 22:15:39 +00:00
};
var onReady = config.onReady = function (info) {
console.log("READY!");
var userDoc = module.realtime.getUserDoc();
editor.setValue(userDoc);
2016-04-22 22:15:39 +00:00
initializing = false;
};
var onRemote = config.onRemote = function (info) {
if (initializing) { return; }
console.log("REMOTE");
var oldDoc = $textarea.val();
var userDoc = module.realtime.getUserDoc();
$textarea.val(userDoc);
editor.setValue(userDoc);
editor.save();
2016-04-22 22:15:39 +00:00
// check cursor
// apply changes to textarea
// replace cursor
};
var onLocal = config.onLocal = function () {
if (initializing) { return; }
console.log("LOCAL");
editor.save();
module.patchText(canonicalize($textarea.val()));
2016-04-22 22:15:39 +00:00
};
var onAbort = config.onAbort = function (info) {
// TODO alert the user
// inform of network disconnect
window.alert("Network Connection Lost!");
2016-04-22 22:15:39 +00:00
};
var realtime = module.realtime = Realtime.start(config);
editor.on('change', onLocal);
};
var interval = 100;
var first = function () {
if (ifrw.CodeMirror) {
// it exists, call your continuation
andThen(ifrw.CodeMirror);
} else {
console.log("CodeMirror was not defined. Trying again in %sms", interval);
// try again in 'interval' ms
setTimeout(first, interval);
}
};
first();
});
});