move TextPatcher to highest application layer

pull/1/head
ansuz 9 years ago
parent 1d70335228
commit 0769e50414

@ -18,11 +18,10 @@ define([
'/common/messages.js', '/common/messages.js',
'/common/netflux-client.js', '/common/netflux-client.js',
'/common/crypto.js', '/common/crypto.js',
'/common/TextPatcher.js',
'/common/es6-promise.min.js', '/common/es6-promise.min.js',
'/common/chainpad.js', '/common/chainpad.js',
'/bower_components/jquery/dist/jquery.min.js', '/bower_components/jquery/dist/jquery.min.js',
], function (Messages, Netflux, Crypto, TextPatcher) { ], function (Messages, Netflux, Crypto) {
var $ = window.jQuery; var $ = window.jQuery;
var ChainPad = window.ChainPad; var ChainPad = window.ChainPad;
var PARANOIA = true; var PARANOIA = true;
@ -117,6 +116,7 @@ define([
initializing = false; initializing = false;
// execute an onReady callback if one was supplied // execute an onReady callback if one was supplied
// FIXME this should be once the chain has synced
if (config.onReady) { if (config.onReady) {
config.onReady({ config.onReady({
realtime: realtime realtime: realtime
@ -227,13 +227,6 @@ define([
// Open a Chainpad session // Open a Chainpad session
realtime = createRealtime(); realtime = createRealtime();
toReturn.onEvent = function (newText) {
// assert to show that we're not out of sync
if (realtime.getUserDoc() !== newText) {
warn("realtime.getUserDoc() !== newText");
}
};
// Sending a message... // Sending a message...
realtime.onMessage(function(message) { realtime.onMessage(function(message) {
// Filter messages sent by Chainpad to make it compatible with Netflux // Filter messages sent by Chainpad to make it compatible with Netflux
@ -265,12 +258,6 @@ define([
wc.history_keeper = hc; wc.history_keeper = hc;
if (hc) { network.sendto(hc, JSON.stringify(['GET_HISTORY', wc.id])); } if (hc) { network.sendto(hc, JSON.stringify(['GET_HISTORY', wc.id])); }
toReturn.patchText = TextPatcher.create({
realtime: realtime,
logging: true
});
realtime.start(); realtime.start();
}; };
@ -290,6 +277,16 @@ define([
Netflux.connect(websocketUrl).then(function(network) { Netflux.connect(websocketUrl).then(function(network) {
// pass messages that come out of netflux into our local handler // pass messages that come out of netflux into our local handler
network.on('disconnect', function (evt) {
// TODO also abort if Netflux times out
// that will be managed in Netflux-client.js
if (config.onAbort) {
config.onAbort({
reason: evt.reason
});
}
});
network.on('message', function (msg, sender) { // Direct message network.on('message', function (msg, sender) { // Direct message
var wchan = findChannelById(network.webChannels, channel); var wchan = findChannelById(network.webChannels, channel);
if(wchan) { if(wchan) {

@ -11,15 +11,18 @@ define([
'/common/json-ot.js', '/common/json-ot.js',
'/common/TypingTests.js', '/common/TypingTests.js',
'json.sortify', 'json.sortify',
'/common/TextPatcher.js',
'/bower_components/diff-dom/diffDOM.js', '/bower_components/diff-dom/diffDOM.js',
'/bower_components/jquery/dist/jquery.min.js', '/bower_components/jquery/dist/jquery.min.js',
'/customize/pad.js' '/customize/pad.js'
], function (Config, Messages, Crypto, realtimeInput, Hyperjson, Hyperscript, Toolbar, Cursor, JsonOT, TypingTest, JSONSortify) { ], function (Config, Messages, Crypto, realtimeInput, Hyperjson, Hyperscript,
Toolbar, Cursor, JsonOT, TypingTest, JSONSortify, TextPatcher) {
var $ = window.jQuery; var $ = window.jQuery;
var ifrw = $('#pad-iframe')[0].contentWindow; var ifrw = $('#pad-iframe')[0].contentWindow;
var Ckeditor; // to be initialized later... var Ckeditor; // to be initialized later...
var DiffDom = window.diffDOM; var DiffDom = window.diffDOM;
var stringify = function (obj) { var stringify = function (obj) {
return JSONSortify(obj); return JSONSortify(obj);
}; };
@ -288,7 +291,7 @@ define([
var shjson2 = stringifyDOM(inner); var shjson2 = stringifyDOM(inner);
if (shjson2 !== shjson) { if (shjson2 !== shjson) {
console.error("shjson2 !== shjson"); console.error("shjson2 !== shjson");
module.realtimeInput.patchText(shjson2); module.patchText(shjson2);
} }
}; };
@ -304,12 +307,21 @@ define([
/* TODO handle disconnects and such*/ /* TODO handle disconnects and such*/
}; };
// this should only ever get called once, when the chain syncs
var onReady = realtimeOptions.onReady = function (info) { var onReady = realtimeOptions.onReady = function (info) {
console.log("Unlocking editor"); module.patchText = TextPatcher.create({
initializing = false; realtime: info.realtime,
setEditable(true); logging: false,
});
module.realtime = info.realtime;
var shjson = info.realtime.getUserDoc(); var shjson = info.realtime.getUserDoc();
applyHjson(shjson); applyHjson(shjson);
console.log("Unlocking editor");
setEditable(true);
initializing = false;
}; };
var onAbort = realtimeOptions.onAbort = function (info) { var onAbort = realtimeOptions.onAbort = function (info) {
@ -320,21 +332,9 @@ define([
toolbar.failed(); toolbar.failed();
}; };
var onLocal = realtimeOptions.onLocal = function () {
if (initializing) { return; }
var rti = module.realtimeInput = realtimeInput.start(realtimeOptions);
/* It's incredibly important that you assign 'rti.onLocal'
It's used inside of realtimeInput to make sure that all changes
make it into chainpad.
It's being assigned this way because it can't be passed in, and
and can't be easily returned from realtime input without making
the code less extensible.
*/
var propogate = rti.onLocal = function () {
// serialize your DOM into an object // serialize your DOM into an object
var hjson = Hyperjson.fromDOM(inner, isNotMagicLine, brFilter); var hjson = Hyperjson.fromDOM(inner, isNotMagicLine, brFilter);
@ -344,12 +344,15 @@ define([
} }
// stringify the json and send it into chainpad // stringify the json and send it into chainpad
var shjson = stringify(hjson); var shjson = stringify(hjson);
if (!rti.patchText(shjson)) { module.patchText(shjson);
return;
if (module.realtime.getUserDoc() !== shjson) {
console.error("realtime.getUserDoc() !== shjson");
} }
rti.onEvent(shjson);
}; };
var rti = module.realtimeInput = realtimeInput.start(realtimeOptions);
/* hitting enter makes a new line, but places the cursor inside /* hitting enter makes a new line, but places the cursor inside
of the <br> instead of the <p>. This makes it such that you of the <br> instead of the <p>. This makes it such that you
cannot type until you click, which is rather unnacceptable. cannot type until you click, which is rather unnacceptable.
@ -359,7 +362,7 @@ define([
the first such keypress will not be inserted into the P. */ the first such keypress will not be inserted into the P. */
inner.addEventListener('keydown', cursor.brFix); inner.addEventListener('keydown', cursor.brFix);
editor.on('change', propogate); editor.on('change', onLocal);
// export the typing tests to the window. // export the typing tests to the window.
// call like `test = easyTest()` // call like `test = easyTest()`
@ -367,8 +370,8 @@ define([
var easyTest = window.easyTest = function () { var easyTest = window.easyTest = function () {
cursor.update(); cursor.update();
var start = cursor.Range.start; var start = cursor.Range.start;
var test = TypingTest.testInput(inner, start.el, start.offset, propogate); var test = TypingTest.testInput(inner, start.el, start.offset, onLocal);
propogate(); onLocal();
return test; return test;
}; };
}); });

Loading…
Cancel
Save