cryptpad/www/code/main.js

415 lines
15 KiB
JavaScript
Raw Normal View History

define([
'jquery',
2016-06-06 10:14:07 +00:00
'/bower_components/chainpad-crypto/crypto.js',
'/bower_components/chainpad-netflux/chainpad-netflux.js',
'/bower_components/textpatcher/TextPatcher.js',
2017-05-04 14:08:12 +00:00
'/common/toolbar2.js',
'json.sortify',
2016-06-06 10:14:07 +00:00
'/bower_components/chainpad-json-validator/json-ot.js',
2016-06-21 13:17:09 +00:00
'/common/cryptpad-common.js',
'/common/cryptget.js',
'/common/diffMarked.js',
], function ($, Crypto, Realtime, TextPatcher, Toolbar, JSONSortify, JsonOT, Cryptpad,
Cryptget, DiffMd) {
var Messages = Cryptpad.Messages;
var APP = window.APP = {
Cryptpad: Cryptpad,
};
2017-02-17 14:39:34 +00:00
$(function () {
Cryptpad.addLoadingScreen();
var ifrw = APP.ifrw = $('#pad-iframe')[0].contentWindow;
2017-02-17 14:39:34 +00:00
var stringify = function (obj) {
return JSONSortify(obj);
};
var toolbar;
var editor;
var $iframe = $('#pad-iframe').contents();
var $previewContainer = $iframe.find('#previewContainer');
var $preview = $iframe.find('#preview');
$preview.click(function (e) {
if (!e.target) { return; }
var $t = $(e.target);
if ($t.is('a') || $t.parents('a').length) {
e.preventDefault();
var $a = $t.is('a') ? $t : $t.parents('a').first();
var href = $a.attr('href');
window.open(href);
}
});
2016-04-22 22:15:39 +00:00
2016-06-21 13:17:09 +00:00
var secret = Cryptpad.getSecrets();
2016-09-20 16:22:40 +00:00
var readOnly = secret.keys && !secret.keys.editKeyStr;
if (!secret.keys) {
secret.keys = secret.key;
}
2017-05-04 14:37:25 +00:00
var onConnectError = function () {
2016-12-21 17:33:21 +00:00
Cryptpad.errorLoadingScreen(Messages.websocketError);
};
var andThen = function (CMeditor) {
var CodeMirror = Cryptpad.createCodemirror(CMeditor, ifrw, Cryptpad);
editor = CodeMirror.editor;
2016-04-22 22:15:39 +00:00
var $bar = $('#pad-iframe')[0].contentWindow.$('#cme_toolbox');
2017-04-21 15:31:47 +00:00
var isHistoryMode = false;
var setEditable = APP.setEditable = function (bool) {
if (readOnly && bool) { return; }
editor.setOption('readOnly', !bool);
};
var Title;
var UserList;
var Metadata;
2016-05-20 11:39:40 +00:00
var config = {
initialState: '{}',
2016-10-04 15:13:15 +00:00
websocketURL: Cryptpad.getWebsocketURL(),
2016-06-21 13:17:09 +00:00
channel: secret.channel,
// our public key
validateKey: secret.keys.validateKey || undefined,
readOnly: readOnly,
crypto: Crypto.createEncryptor(secret.keys),
2017-02-13 10:59:49 +00:00
network: Cryptpad.getNetwork(),
2017-02-09 16:20:13 +00:00
transformFunction: JsonOT.validate,
2016-05-20 11:39:40 +00:00
};
var canonicalize = function (t) { return t.replace(/\r\n/g, '\n'); };
2017-04-21 15:31:47 +00:00
var setHistory = function (bool, update) {
isHistoryMode = bool;
setEditable(!bool);
if (!bool && update) {
config.onRemote();
}
};
2016-05-20 11:39:40 +00:00
var initializing = true;
2016-10-25 15:29:13 +00:00
var stringifyInner = function (textValue) {
var obj = {
content: textValue,
metadata: {
users: UserList.userData,
defaultTitle: Title.defaultTitle
2016-10-25 15:29:13 +00:00
}
};
2017-01-12 14:15:10 +00:00
if (!initializing) {
obj.metadata.title = Title.title;
2017-01-12 14:15:10 +00:00
}
2016-10-25 15:29:13 +00:00
// set mode too...
obj.highlightMode = CodeMirror.highlightMode;
2016-10-25 15:29:13 +00:00
// stringify the json and send it into chainpad
return stringify(obj);
};
2017-05-26 10:41:30 +00:00
var forceDrawPreview = function () {
try {
DiffMd.apply(DiffMd.render(editor.getValue()), $preview);
} catch (e) { console.error(e); }
};
var drawPreview = Cryptpad.throttle(function () {
if (CodeMirror.highlightMode !== 'markdown') { return; }
if (!$previewContainer.is(':visible')) { return; }
forceDrawPreview();
}, 150);
2016-05-20 11:39:40 +00:00
var onLocal = config.onLocal = function () {
if (initializing) { return; }
2017-04-21 15:31:47 +00:00
if (isHistoryMode) { return; }
2016-09-22 14:09:20 +00:00
if (readOnly) { return; }
2016-05-20 11:39:40 +00:00
editor.save();
2017-05-26 10:41:30 +00:00
drawPreview();
var textValue = canonicalize(CodeMirror.$textarea.val());
2016-10-25 15:29:13 +00:00
var shjson = stringifyInner(textValue);
2016-05-20 11:39:40 +00:00
APP.patchText(shjson);
2016-05-20 11:39:40 +00:00
if (APP.realtime.getUserDoc() !== shjson) {
2016-05-20 11:39:40 +00:00
console.error("realtime.getUserDoc() !== shjson");
}
};
var onModeChanged = function (mode) {
var $codeMirror = $iframe.find('.CodeMirror');
if (mode === "markdown") {
APP.$previewButton.show();
$previewContainer.show();
$codeMirror.removeClass('fullPage');
return;
}
APP.$previewButton.hide();
$previewContainer.hide();
$codeMirror.addClass('fullPage');
};
2016-10-25 15:29:13 +00:00
config.onInit = function (info) {
UserList = Cryptpad.createUserList(info, config.onLocal, Cryptget, Cryptpad);
var titleCfg = { getHeadingText: CodeMirror.getHeadingText };
Title = Cryptpad.createTitle(titleCfg, config.onLocal, Cryptpad);
Metadata = Cryptpad.createMetadata(UserList, Title);
2017-04-21 15:31:47 +00:00
var configTb = {
2017-05-04 14:08:12 +00:00
displayed: ['title', 'useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit'],
userList: UserList.getToolbarConfig(),
share: {
secret: secret,
channel: info.channel
},
title: Title.getTitleConfig(),
2017-05-04 14:08:12 +00:00
common: Cryptpad,
readOnly: readOnly,
ifrw: ifrw,
2017-05-04 14:08:12 +00:00
realtime: info.realtime,
network: info.network,
$container: $bar
};
toolbar = APP.toolbar = Toolbar.create(configTb);
2016-06-29 10:00:12 +00:00
Title.setToolbar(toolbar);
CodeMirror.init(config.onLocal, Title, toolbar);
var $rightside = toolbar.$rightside;
2016-09-20 09:35:57 +00:00
var editHash;
if (!readOnly) {
editHash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys);
}
2017-04-21 15:31:47 +00:00
/* add a history button */
2017-05-10 11:39:14 +00:00
var histConfig = {
2017-05-29 15:25:06 +00:00
onLocal: config.onLocal,
onRemote: config.onRemote,
2017-05-10 11:39:14 +00:00
setHistory: setHistory,
applyVal: function (val) {
var remoteDoc = JSON.parse(val || '{}').content;
2017-04-21 15:31:47 +00:00
editor.setValue(remoteDoc || '');
editor.save();
2017-05-10 11:39:14 +00:00
},
$toolbar: $bar
2017-04-21 15:31:47 +00:00
};
var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig});
2017-04-14 16:36:36 +00:00
$rightside.append($hist);
/* save as template */
if (!Cryptpad.isTemplate(window.location.href)) {
var templateObj = {
rt: info.realtime,
Crypt: Cryptget,
getTitle: Title.getTitle
};
var $templateButton = Cryptpad.createButton('template', true, templateObj);
$rightside.append($templateButton);
}
/* add an export button */
var $export = Cryptpad.createButton('export', true, {}, CodeMirror.exportText);
$rightside.append($export);
if (!readOnly) {
/* add an import button */
var $import = Cryptpad.createButton('import', true, {}, CodeMirror.importText);
$rightside.append($import);
}
/* add a forget button */
2017-05-04 14:37:25 +00:00
var forgetCb = function (err) {
if (err) { return; }
2017-02-24 14:22:26 +00:00
setEditable(false);
};
var $forgetPad = Cryptpad.createButton('forget', true, {}, forgetCb);
$rightside.append($forgetPad);
2016-06-30 08:51:19 +00:00
var $previewButton = APP.$previewButton = Cryptpad.createButton(null, true);
$previewButton.removeClass('fa-question').addClass('fa-eye');
$previewButton.attr('title', Messages.previewButtonTitle);
$previewButton.click(function () {
var $codeMirror = $iframe.find('.CodeMirror');
if (CodeMirror.highlightMode !== 'markdown') {
$previewContainer.show();
}
$previewContainer.toggle();
if ($previewContainer.is(':visible')) {
$codeMirror.removeClass('fullPage');
} else {
$codeMirror.addClass('fullPage');
}
});
$rightside.append($previewButton);
if (!readOnly) {
CodeMirror.configureTheme(function () {
CodeMirror.configureLanguage(null, onModeChanged);
});
}
else {
CodeMirror.configureTheme();
}
2016-06-29 09:51:53 +00:00
// set the hash
if (!readOnly) { Cryptpad.replaceHash(editHash); }
2016-04-22 22:15:39 +00:00
};
2017-05-04 14:37:25 +00:00
config.onReady = function (info) {
if (APP.realtime !== info.realtime) {
var realtime = APP.realtime = info.realtime;
APP.patchText = TextPatcher.create({
realtime: realtime,
//logging: true
});
}
var userDoc = APP.realtime.getUserDoc();
var isNew = false;
if (userDoc === "" || userDoc === "{}") { isNew = true; }
var newDoc = "";
if(userDoc !== "") {
var hjson = JSON.parse(userDoc);
if (typeof (hjson) !== 'object' || Array.isArray(hjson)) {
var errorText = Messages.typeError;
Cryptpad.errorLoadingScreen(errorText);
throw new Error(errorText);
}
newDoc = hjson.content;
if (hjson.highlightMode) {
CodeMirror.setMode(hjson.highlightMode, onModeChanged);
}
}
if (!CodeMirror.highlightMode) {
CodeMirror.setMode('markdown', onModeChanged);
console.log("%s => %s", CodeMirror.highlightMode, CodeMirror.$language.val());
}
// Update the user list (metadata) from the hyperjson
Metadata.update(userDoc);
if (newDoc) {
editor.setValue(newDoc);
}
if (Cryptpad.initialName && Title.isDefaultTitle()) {
Title.updateTitle(Cryptpad.initialName);
2017-02-21 16:42:58 +00:00
}
2016-12-21 17:33:21 +00:00
Cryptpad.removeLoadingScreen();
setEditable(true);
2016-04-22 22:15:39 +00:00
initializing = false;
onLocal(); // push local state to avoid parse errors later.
2017-05-23 13:54:04 +00:00
if (readOnly) {
config.onRemote();
return;
}
UserList.getLastName(toolbar.$userNameButton, isNew);
2016-04-22 22:15:39 +00:00
};
2017-05-04 14:37:25 +00:00
config.onRemote = function () {
2016-04-22 22:15:39 +00:00
if (initializing) { return; }
2017-04-21 15:31:47 +00:00
if (isHistoryMode) { return; }
var oldDoc = canonicalize(CodeMirror.$textarea.val());
var shjson = APP.realtime.getUserDoc();
// Update the user list (metadata) from the hyperjson
Metadata.update(shjson);
var hjson = JSON.parse(shjson);
var remoteDoc = hjson.content;
var highlightMode = hjson.highlightMode;
if (highlightMode && highlightMode !== APP.highlightMode) {
CodeMirror.setMode(highlightMode, onModeChanged);
}
CodeMirror.setValueAndCursor(oldDoc, remoteDoc, TextPatcher);
2017-05-26 10:41:30 +00:00
drawPreview();
if (!readOnly) {
var textValue = canonicalize(CodeMirror.$textarea.val());
2016-10-25 15:29:13 +00:00
var shjson2 = stringifyInner(textValue);
if (shjson2 !== shjson) {
console.error("shjson2 !== shjson");
TextPatcher.log(shjson, TextPatcher.diff(shjson, shjson2));
APP.patchText(shjson2);
}
}
if (oldDoc !== remoteDoc) { Cryptpad.notify(); }
2016-04-22 22:15:39 +00:00
};
2017-05-04 14:37:25 +00:00
config.onAbort = function () {
2016-04-22 22:15:39 +00:00
// inform of network disconnect
setEditable(false);
toolbar.failed();
2017-03-02 16:45:48 +00:00
Cryptpad.alert(Messages.common_connectionLost, undefined, true);
2016-04-22 22:15:39 +00:00
};
2017-05-04 14:37:25 +00:00
config.onConnectionChange = function (info) {
setEditable(info.state);
toolbar.failed();
if (info.state) {
initializing = true;
toolbar.reconnecting(info.myId);
Cryptpad.findOKButton().click();
} else {
2017-03-02 16:45:48 +00:00
Cryptpad.alert(Messages.common_connectionLost, undefined, true);
}
};
2017-05-04 14:37:25 +00:00
config.onError = onConnectError;
2016-12-08 15:01:46 +00:00
APP.realtime = Realtime.start(config);
2016-04-22 22:15:39 +00:00
editor.on('change', onLocal);
Cryptpad.onLogout(function () { setEditable(false); });
};
var interval = 100;
var second = function (CM) {
2017-05-04 14:37:25 +00:00
Cryptpad.ready(function () {
andThen(CM);
2017-04-13 10:18:08 +00:00
Cryptpad.reportAppUsage();
});
Cryptpad.onError(function (info) {
if (info && info.type === "store") {
onConnectError();
}
});
};
var first = function () {
if (ifrw.CodeMirror) {
// it exists, call your continuation
second(ifrw.CodeMirror);
} else {
console.log("CodeMirror was not defined. Trying again in %sms", interval);
// try again in 'interval' ms
setTimeout(first, interval);
}
};
first();
});
});