From 86c4a69a97e4fa6a1c2250b434e1251d69382fc8 Mon Sep 17 00:00:00 2001 From: Caleb James DeLisle Date: Thu, 11 Feb 2016 16:10:44 +0100 Subject: [PATCH 1/5] Switch to patching with DiffDOM for testing :D --- bower.json | 3 ++- www/common/realtime-input.js | 6 +++--- www/vdom/main.js | 20 +++++++++----------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/bower.json b/bower.json index bdb976482..2f1277bc5 100644 --- a/bower.json +++ b/bower.json @@ -25,6 +25,7 @@ "ckeditor": "~4.5.6", "requirejs": "~2.1.15", "modalBox": "~1.0.2", - "reconnectingWebsocket": "" + "reconnectingWebsocket": "", + "diff-dom": "~2.0.0" } } diff --git a/www/common/realtime-input.js b/www/common/realtime-input.js index 3a0788401..146206dd1 100644 --- a/www/common/realtime-input.js +++ b/www/common/realtime-input.js @@ -230,8 +230,8 @@ define([ //verbose("Received own message"); } else { //verbose("Received remote message"); - // obviously this is only going to get called if - onRemote && onRemote(realtime.getAuthDoc()); + // obviously this is only going to get called if + onRemote && onRemote(realtime.getUserDoc()); } } } @@ -290,7 +290,7 @@ define([ }); return { onEvent: function () { - onEvent(); + onEvent(); }, bumpSharejs: function () { bump(); } }; diff --git a/www/vdom/main.js b/www/vdom/main.js index b734039b1..350e5e8ca 100644 --- a/www/vdom/main.js +++ b/www/vdom/main.js @@ -6,12 +6,15 @@ define([ '/common/convert.js', '/common/toolbar.js', '/common/cursor.js', + '/bower_components/diff-dom/diffDOM.js', + '/bower_components/jquery/dist/jquery.min.js', '/customize/pad.js' ], function (Config, Messages, Crypto, realtimeInput, Convert, Toolbar, Cursor) { var $ = window.jQuery; var ifrw = $('#pad-iframe')[0].contentWindow; window.Ckeditor = ifrw.CKEDITOR; + var DiffDom = window.diffDOM; var userName = Crypto.rand64(8), toolbar; @@ -59,16 +62,11 @@ define([ var applyHjson = function (shjson) { console.log("Applying HJSON"); - // before integrating external changes, check in your own - vdom1 = Convert.dom.to.vdom(inner); - // the authoritative document is hyperjson, parse it - var authDoc = JSON.parse(shjson); - // use the authdoc to construct a second vdom - var vdom2 = Convert.hjson.to.vdom(authDoc); - // diff it against your version - var patches = Vdom.diff(vdom1, vdom2); - // apply the resulting patches - Vdom.patch(inner, patches); + var userDocStateDom = Vdom.create(Convert.hjson.to.vdom(JSON.parse(shjson))); + userDocStateDom.setAttribute("contentEditable", "true"); // lol wtf + var patch = (new DiffDom()).diff(inner, userDocStateDom); + console.log(userDocStateDom.outerHTML); + (new DiffDom()).apply(inner, patch); }; var onRemote = function (shjson) { @@ -104,7 +102,7 @@ define([ onInit: onInit, transformFunction : function (text, toTransform, transformBy) { - /* FIXME + /* FIXME operational transform on json shouldn't be in all editors just those transmitting/expecting JSON */ From a7da21ccba9e9fe4a49e04d0c8cbca448ff621a4 Mon Sep 17 00:00:00 2001 From: Caleb James DeLisle Date: Thu, 11 Feb 2016 16:22:59 +0100 Subject: [PATCH 2/5] Remove the cursor treatment --- www/vdom/main.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/www/vdom/main.js b/www/vdom/main.js index 350e5e8ca..1f18f4f6f 100644 --- a/www/vdom/main.js +++ b/www/vdom/main.js @@ -65,20 +65,19 @@ define([ var userDocStateDom = Vdom.create(Convert.hjson.to.vdom(JSON.parse(shjson))); userDocStateDom.setAttribute("contentEditable", "true"); // lol wtf var patch = (new DiffDom()).diff(inner, userDocStateDom); - console.log(userDocStateDom.outerHTML); (new DiffDom()).apply(inner, patch); }; var onRemote = function (shjson) { // remember where the cursor is - cursor.update() + //cursor.update() applyHjson(shjson); - cursor.find(); + //cursor.find(); // put the cursor back where you left it - cursor.replace(); + //cursor.replace(); }; var onInit = function (info) { @@ -139,14 +138,14 @@ define([ $textarea.val(JSON.stringify(hjson)); rti.bumpSharejs(); - cursor.update() + //cursor.update() }); - ['mouseup', 'keyup'].forEach(function (type) { + /*['mouseup', 'keyup'].forEach(function (type) { editor.document.on(type, function (e) { cursor.update(); }); - }); + });*/ }); }; From 943a291903fd3d4239f5e972f9205654b2120db4 Mon Sep 17 00:00:00 2001 From: Caleb James DeLisle Date: Thu, 11 Feb 2016 16:42:21 +0100 Subject: [PATCH 3/5] Do not blow up if the error is that a chain cannot be found --- storage/lvl.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/storage/lvl.js b/storage/lvl.js index 179d1fc73..18c604d93 100644 --- a/storage/lvl.js +++ b/storage/lvl.js @@ -3,8 +3,8 @@ var nThen = require('nthen'); var getIndex = function(db, cName, cb) { db.get(cName+'=>index', function(e, out){ - if (e) { throw e; } - cb(parseInt(out)); + if (e && !e.notFound) { throw e; } + cb(parseInt(out || 0)); }); }; From 13360c335b9af0341905f5c0d31b67bb7dc6254b Mon Sep 17 00:00:00 2001 From: Caleb James DeLisle Date: Thu, 11 Feb 2016 16:55:26 +0100 Subject: [PATCH 4/5] I'm an idiot - killer off-by-one ate the storage --- storage/lvl.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/storage/lvl.js b/storage/lvl.js index 18c604d93..14a091b2b 100644 --- a/storage/lvl.js +++ b/storage/lvl.js @@ -3,19 +3,27 @@ var nThen = require('nthen'); var getIndex = function(db, cName, cb) { db.get(cName+'=>index', function(e, out){ - if (e && !e.notFound) { throw e; } - cb(parseInt(out || 0)); + if (e) { + if (e.notFound) { + cb(-1); + } else { + throw e; + } + return; + } + cb(parseInt(out)); }); }; var insert = function (db, channelName, content, cb) { var index; nThen(function (waitFor) { - getIndex(db, channelName, waitFor(function (i) { index = i; })); - }).nThen(function (waitFor) { - db.put(channelName+'=>index', ''+index, waitFor(function (e) { if (e) { throw e; } })); + getIndex(db, channelName, waitFor(function (i) { index = i+1; })); }).nThen(function (waitFor) { db.put(channelName+'=>'+index, content, waitFor(function (e) { if (e) { throw e; } })); + }).nThen(function (waitFor) { + db.put(channelName+'=>index', ''+index, waitFor(function (e) { if (e) { throw e; } })); + console.log(index); }).nThen(cb); }; From 7553ccff68a2b8c49abd40d210f28f795cb1ba90 Mon Sep 17 00:00:00 2001 From: Caleb James DeLisle Date: Fri, 12 Feb 2016 08:24:46 +0100 Subject: [PATCH 5/5] Remove a log line which was leftover after debugging --- storage/lvl.js | 1 - 1 file changed, 1 deletion(-) diff --git a/storage/lvl.js b/storage/lvl.js index 14a091b2b..84ff57862 100644 --- a/storage/lvl.js +++ b/storage/lvl.js @@ -23,7 +23,6 @@ var insert = function (db, channelName, content, cb) { db.put(channelName+'=>'+index, content, waitFor(function (e) { if (e) { throw e; } })); }).nThen(function (waitFor) { db.put(channelName+'=>index', ''+index, waitFor(function (e) { if (e) { throw e; } })); - console.log(index); }).nThen(cb); };