cryptpad/www/hack/main.js

163 lines
4.9 KiB
JavaScript
Raw Normal View History

define([
'/api/config?cb=' + Math.random().toString(16).substring(2),
2016-06-06 10:14:07 +00:00
'/bower_components/chainpad-netflux/chainpad-netflux.js',
'/bower_components/chainpad-crypto/crypto.js',
'/bower_components/textpatcher/TextPatcher.amd.js',
2016-06-21 13:17:09 +00:00
'/common/cryptpad-common.js',
'/bower_components/jquery/dist/jquery.min.js'
2016-06-21 13:17:09 +00:00
], function (Config, Realtime, Crypto, TextPatcher, Cryptpad) {
2016-02-15 15:07:46 +00:00
var $ = window.jQuery;
2016-04-22 07:47:26 +00:00
2016-06-21 13:17:09 +00:00
var secret = Cryptpad.getSecrets();
var $textarea = $('textarea'),
$run = $('#run');
var module = {};
var config = {
2016-04-22 07:47:26 +00:00
initialState: '',
websocketURL: Config.websocketURL,
2016-06-21 13:17:09 +00:00
channel: secret.channel,
crypto: Crypto.createEncryptor(secret.key),
};
2016-03-04 15:47:59 +00:00
var initializing = true;
var setEditable = function (bool) { $textarea.attr('disabled', !bool); };
var canonicalize = function (text) { return text.replace(/\r\n/g, '\n'); };
setEditable(false);
2016-03-04 15:47:59 +00:00
2016-04-22 07:47:26 +00:00
var onInit = config.onInit = function (info) {
2016-06-21 13:17:09 +00:00
window.location.hash = info.channel + secret.key;
2016-04-22 16:54:24 +00:00
$(window).on('hashchange', function() { window.location.reload(); });
2016-04-22 07:47:26 +00:00
};
2016-03-04 15:47:59 +00:00
var onRemote = config.onRemote = function (info) {
2016-03-04 15:47:59 +00:00
if (initializing) { return; }
var userDoc = info.realtime.getUserDoc();
var current = canonicalize($textarea.val());
var op = TextPatcher.diff(current, userDoc);
var elem = $textarea[0];
var selects = ['selectionStart', 'selectionEnd'].map(function (attr) {
return TextPatcher.transformCursor(elem[attr], op);
});
$textarea.val(userDoc);
elem.selectionStart = selects[0];
elem.selectionEnd = selects[1];
2016-03-04 15:47:59 +00:00
// TODO do something on external messages
// http://webdesign.tutsplus.com/tutorials/how-to-display-update-notifications-in-the-browser-tab--cms-23458
};
var onReady = config.onReady = function (info) {
module.patchText = TextPatcher.create({
realtime: info.realtime
// logging: true
});
2016-03-04 15:47:59 +00:00
initializing = false;
setEditable(true);
$textarea.val(info.realtime.getUserDoc());
2016-03-04 15:47:59 +00:00
};
var onAbort = config.onAbort = function (info) {
setEditable(false);
2016-03-04 15:47:59 +00:00
window.alert("Server Connection Lost");
};
var onLocal = config.onLocal = function () {
if (initializing) { return; }
module.patchText(canonicalize($textarea.val()));
};
var rt = window.rt = Realtime.start(config);
var splice = function (str, index, chars) {
var count = chars.length;
return str.slice(0, index) + chars + str.slice((index -1) + count);
};
var setSelectionRange = function (input, start, end) {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(start, end);
} else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', start);
range.select();
}
};
var setCursor = function (el, pos) {
setSelectionRange(el, pos, pos);
};
2016-04-12 16:51:03 +00:00
var state = {};
// TODO
$textarea.on('keydown', function (e) {
// track when control keys are pushed down
//switch (e.key) { }
});
// TODO
$textarea.on('keyup', function (e) {
// track when control keys are released
});
//$textarea.on('change', onLocal);
$textarea.on('keypress', function (e) {
onLocal();
switch (e.key) {
case 'Tab':
// insert a tab wherever the cursor is...
2016-04-12 16:51:03 +00:00
var start = $textarea.prop('selectionStart');
var end = $textarea.prop('selectionEnd');
if (typeof start !== 'undefined') {
if (start === end) {
$textarea.val(function (i, val) {
return splice(val, start, "\t");
});
setCursor($textarea[0], start +1);
} else {
// indentation?? this ought to be fun.
}
}
// simulate a keypress so the event goes through..
// prevent default behaviour for tab
e.preventDefault();
onLocal();
break;
default:
break;
}
});
['cut', 'paste', 'change', 'keyup', 'keydown', 'select', 'textInput']
.forEach(function (evt) {
$textarea.on(evt, onLocal);
});
$run.click(function (e) {
e.preventDefault();
var content = $textarea.val();
try {
2016-02-12 10:10:59 +00:00
eval(content); // jshint ignore:line
} catch (err) {
2016-02-15 15:07:46 +00:00
// FIXME don't use alert, make an errorbox
window.alert(err.message);
2016-03-04 15:47:59 +00:00
console.error(err);
}
});
});