cryptpad/www/pad/main.js

58 lines
2 KiB
JavaScript
Raw Normal View History

2014-10-31 15:42:58 +00:00
define([
2015-01-29 16:55:18 +00:00
'/api/config?cb=' + Math.random().toString(16).substring(2),
'/pad/realtime-wysiwyg.js',
'/common/messages.js',
'/bower_components/jquery/dist/jquery.min.js',
'/bower_components/ckeditor/ckeditor.js',
'/bower_components/tweetnacl/nacl-fast.min.js',
], function (Config, RTWysiwyg, Messages) {
2014-10-31 15:42:58 +00:00
var Ckeditor = window.CKEDITOR;
var Nacl = window.nacl;
var $ = jQuery;
var module = { exports: {} };
var parseKey = function (str) {
var array = Nacl.util.decodeBase64(str);
var hash = Nacl.hash(array);
return { lookupKey: hash.subarray(32), cryptKey: hash.subarray(0,32) };
};
var genKey = function () {
return Nacl.util.encodeBase64(Nacl.randomBytes(18));
};
var userName = function () {
return Nacl.util.encodeBase64(Nacl.randomBytes(8));
};
$(function () {
$(window).on('hashchange', function() {
window.location.reload();
});
2015-01-28 16:58:55 +00:00
if (window.location.href.indexOf('#') === -1) {
2015-01-29 16:55:18 +00:00
window.location.href = window.location.href + '#' + genKey();
2015-01-28 16:58:55 +00:00
return;
}
2014-10-31 15:42:58 +00:00
var key = parseKey(window.location.hash.substring(1));
var editor = Ckeditor.replace('editor1', {
removeButtons: 'Source,Maximize',
// This plugin inserts html crap into the document which is not part of the document
// itself and causes problems when it's sent across the wire and reflected back.
2014-11-04 09:53:49 +00:00
removePlugins: 'magicline'
2014-10-31 15:42:58 +00:00
});
editor.on('instanceReady', function () {
2014-10-31 16:05:09 +00:00
editor.execCommand('maximize');
2014-10-31 15:42:58 +00:00
var ifr = window.ifr = $('iframe')[0];
ifr.contentDocument.body.innerHTML = Messages.initialState;
2014-10-31 15:42:58 +00:00
var rtw =
RTWysiwyg.start(Config.websocketURL,
2014-10-31 15:42:58 +00:00
userName(),
Nacl.util.encodeBase64(key.lookupKey).substring(0,10),
key.cryptKey);
editor.on('change', function () { rtw.onEvent(); });
});
});
});