You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.9 KiB
JavaScript
163 lines
4.9 KiB
JavaScript
9 years ago
|
define([
|
||
|
'/api/config?cb=' + Math.random().toString(16).substring(2),
|
||
9 years ago
|
'/bower_components/chainpad-netflux/chainpad-netflux.js',
|
||
|
'/bower_components/chainpad-crypto/crypto.js',
|
||
9 years ago
|
'/bower_components/textpatcher/TextPatcher.amd.js',
|
||
9 years ago
|
'/common/cryptpad-common.js',
|
||
9 years ago
|
'/bower_components/jquery/dist/jquery.min.js'
|
||
9 years ago
|
], function (Config, Realtime, Crypto, TextPatcher, Cryptpad) {
|
||
9 years ago
|
var $ = window.jQuery;
|
||
9 years ago
|
|
||
9 years ago
|
var secret = Cryptpad.getSecrets();
|
||
9 years ago
|
|
||
|
var $textarea = $('textarea'),
|
||
|
$run = $('#run');
|
||
|
|
||
9 years ago
|
var module = {};
|
||
|
|
||
9 years ago
|
var config = {
|
||
9 years ago
|
initialState: '',
|
||
|
websocketURL: Config.websocketURL,
|
||
9 years ago
|
channel: secret.channel,
|
||
|
crypto: Crypto.createEncryptor(secret.key),
|
||
9 years ago
|
};
|
||
9 years ago
|
var initializing = true;
|
||
|
|
||
9 years ago
|
var setEditable = function (bool) { $textarea.attr('disabled', !bool); };
|
||
|
var canonicalize = function (text) { return text.replace(/\r\n/g, '\n'); };
|
||
|
|
||
|
setEditable(false);
|
||
9 years ago
|
|
||
9 years ago
|
var onInit = config.onInit = function (info) {
|
||
9 years ago
|
window.location.hash = info.channel + secret.key;
|
||
9 years ago
|
$(window).on('hashchange', function() { window.location.reload(); });
|
||
9 years ago
|
};
|
||
9 years ago
|
|
||
9 years ago
|
var onRemote = config.onRemote = function (info) {
|
||
9 years ago
|
if (initializing) { return; }
|
||
9 years ago
|
|
||
|
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];
|
||
|
|
||
9 years ago
|
// 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) {
|
||
9 years ago
|
module.patchText = TextPatcher.create({
|
||
|
realtime: info.realtime
|
||
|
// logging: true
|
||
|
});
|
||
9 years ago
|
initializing = false;
|
||
9 years ago
|
setEditable(true);
|
||
|
$textarea.val(info.realtime.getUserDoc());
|
||
9 years ago
|
};
|
||
|
|
||
|
var onAbort = config.onAbort = function (info) {
|
||
9 years ago
|
setEditable(false);
|
||
9 years ago
|
window.alert("Server Connection Lost");
|
||
|
};
|
||
|
|
||
9 years ago
|
var onLocal = config.onLocal = function () {
|
||
|
if (initializing) { return; }
|
||
|
module.patchText(canonicalize($textarea.val()));
|
||
|
};
|
||
9 years ago
|
|
||
9 years ago
|
var rt = window.rt = Realtime.start(config);
|
||
9 years ago
|
|
||
|
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);
|
||
|
};
|
||
|
|
||
9 years ago
|
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
|
||
|
});
|
||
|
|
||
9 years ago
|
//$textarea.on('change', onLocal);
|
||
9 years ago
|
$textarea.on('keypress', function (e) {
|
||
9 years ago
|
onLocal();
|
||
9 years ago
|
switch (e.key) {
|
||
|
case 'Tab':
|
||
|
// insert a tab wherever the cursor is...
|
||
9 years ago
|
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.
|
||
|
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
// simulate a keypress so the event goes through..
|
||
9 years ago
|
// prevent default behaviour for tab
|
||
|
e.preventDefault();
|
||
9 years ago
|
|
||
|
onLocal();
|
||
9 years ago
|
break;
|
||
9 years ago
|
default:
|
||
|
break;
|
||
|
}
|
||
|
});
|
||
|
|
||
9 years ago
|
['cut', 'paste', 'change', 'keyup', 'keydown', 'select', 'textInput']
|
||
|
.forEach(function (evt) {
|
||
|
$textarea.on(evt, onLocal);
|
||
|
});
|
||
9 years ago
|
|
||
|
$run.click(function (e) {
|
||
|
e.preventDefault();
|
||
|
var content = $textarea.val();
|
||
|
|
||
|
try {
|
||
9 years ago
|
eval(content); // jshint ignore:line
|
||
9 years ago
|
} catch (err) {
|
||
9 years ago
|
// FIXME don't use alert, make an errorbox
|
||
|
window.alert(err.message);
|
||
9 years ago
|
console.error(err);
|
||
9 years ago
|
}
|
||
|
});
|
||
|
});
|