fix /hack/ to not require sharejs textarea

pull/1/head
ansuz 9 years ago
parent f2db6b83eb
commit bafdd588dd

@ -1,12 +1,10 @@
define([ define([
'/api/config?cb=' + Math.random().toString(16).substring(2), '/api/config?cb=' + Math.random().toString(16).substring(2),
'/common/realtime-input.js', '/common/RealtimeTextarea.js',
'/common/messages.js',
'/common/crypto.js', '/common/crypto.js',
'/common/cursor.js', '/common/TextPatcher.js',
'/bower_components/jquery/dist/jquery.min.js', '/bower_components/jquery/dist/jquery.min.js'
'/customize/pad.js' ], function (Config, Realtime, Crypto, TextPatcher) {
], function (Config, Realtime, Messages, Crypto, Cursor) {
var $ = window.jQuery; var $ = window.jQuery;
$(window).on('hashchange', function() { $(window).on('hashchange', function() {
window.location.reload(); window.location.reload();
@ -21,6 +19,8 @@ define([
var $textarea = $('textarea'), var $textarea = $('textarea'),
$run = $('#run'); $run = $('#run');
var module = {};
/* /*
onRemote onRemote
onInit onInit
@ -31,7 +31,7 @@ define([
var config = { var config = {
textarea: $textarea[0], textarea: $textarea[0],
websocketURL: Config.websocketURL, websocketURL: Config.websocketURL + '_old',
webrtcURL: Config.webrtcURL, webrtcURL: Config.webrtcURL,
userName: Crypto.rand64(8), userName: Crypto.rand64(8),
channel: key.channel, channel: key.channel,
@ -39,29 +39,56 @@ define([
}; };
var initializing = true; var initializing = true;
$textarea.attr('disabled', true); var setEditable = function (bool) { $textarea.attr('disabled', !bool); };
var canonicalize = function (text) { return text.replace(/\r\n/g, '\n'); };
setEditable(false);
var onInit = config.onInit = function (info) { }; var onInit = config.onInit = function (info) { };
var onRemote = config.onRemote = function (contents) { var onRemote = config.onRemote = function (info) {
if (initializing) { return; } 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];
// TODO do something on external messages // TODO do something on external messages
// http://webdesign.tutsplus.com/tutorials/how-to-display-update-notifications-in-the-browser-tab--cms-23458 // http://webdesign.tutsplus.com/tutorials/how-to-display-update-notifications-in-the-browser-tab--cms-23458
}; };
var onReady = config.onReady = function (info) { var onReady = config.onReady = function (info) {
module.patchText = TextPatcher.create({
realtime: info.realtime
// logging: true
});
initializing = false; initializing = false;
$textarea.attr('disabled', false); setEditable(true);
$textarea.val(info.realtime.getUserDoc());
}; };
var onAbort = config.onAbort = function (info) { var onAbort = config.onAbort = function (info) {
$textarea.attr('disabled', true); setEditable(false);
window.alert("Server Connection Lost"); window.alert("Server Connection Lost");
}; };
var rt = window.rt = Realtime.start(config); var onLocal = config.onLocal = function () {
if (initializing) { return; }
module.patchText(canonicalize($textarea.val()));
};
var cursor = Cursor($textarea[0]); var rt = window.rt = Realtime.start(config);
var splice = function (str, index, chars) { var splice = function (str, index, chars) {
var count = chars.length; var count = chars.length;
@ -98,7 +125,9 @@ define([
// track when control keys are released // track when control keys are released
}); });
//$textarea.on('change', onLocal);
$textarea.on('keypress', function (e) { $textarea.on('keypress', function (e) {
onLocal();
switch (e.key) { switch (e.key) {
case 'Tab': case 'Tab':
// insert a tab wherever the cursor is... // insert a tab wherever the cursor is...
@ -118,15 +147,17 @@ define([
// simulate a keypress so the event goes through.. // simulate a keypress so the event goes through..
// prevent default behaviour for tab // prevent default behaviour for tab
e.preventDefault(); e.preventDefault();
rt.bumpSharejs();
onLocal();
break; break;
default: default:
break; break;
} }
}); });
$textarea.on('change', function () { ['cut', 'paste', 'change', 'keyup', 'keydown', 'select', 'textInput']
rt.bumpSharejs(); .forEach(function (evt) {
$textarea.on(evt, onLocal);
}); });
$run.click(function (e) { $run.click(function (e) {

Loading…
Cancel
Save