From 6884b0890ac0e3e4378de438cd02cc9f02a1cf61 Mon Sep 17 00:00:00 2001 From: Yann Flory Date: Thu, 14 Apr 2016 12:24:17 +0200 Subject: [PATCH] Fix the typong tests not working properly --- www/common/TypingTests.js | 2 +- www/common/netflux-client.js | 2 +- www/pad/main.js | 50 ++++++++++++++++++++++++++++-------- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/www/common/TypingTests.js b/www/common/TypingTests.js index af09c72c8..16db8c0d5 100644 --- a/www/common/TypingTests.js +++ b/www/common/TypingTests.js @@ -33,7 +33,7 @@ define(function () { interval = setRandomizedInterval(function () { cb(); try { - el.replaceData(j, 0, input.charAt(i)); + el.replaceData(Math.min(j, el.length), 0, input.charAt(i)); } catch (err) { errors++; if (errors >= max_errors) { diff --git a/www/common/netflux-client.js b/www/common/netflux-client.js index 902abb483..455b07c5b 100644 --- a/www/common/netflux-client.js +++ b/www/common/netflux-client.js @@ -133,7 +133,7 @@ const onMessage = (ctx, evt) => { return; } if (msg[2] === 'PING') { - msg[1] = 'PONG'; + msg[2] = 'PONG'; ctx.ws.send(JSON.stringify(msg)); return; } diff --git a/www/pad/main.js b/www/pad/main.js index 91b9d009c..59c9de2ae 100644 --- a/www/pad/main.js +++ b/www/pad/main.js @@ -32,7 +32,8 @@ define([ }; var module = window.REALTIME_MODULE = { - localChangeInProgress: 0 + Hyperjson: Hyperjson, + Hyperscript: Hyperscript }; var userName = Crypto.rand64(8), @@ -104,8 +105,7 @@ define([ var cursor = window.cursor = Cursor(inner); var setEditable = function (bool) { - inner.setAttribute('contenteditable', - (typeof (bool) !== 'undefined'? bool : true)); + inner.setAttribute('contenteditable', bool); }; // don't let the user edit until the pad is ready @@ -113,25 +113,53 @@ define([ var diffOptions = { preDiffApply: function (info) { - /* Don't remove local instances of the magicline plugin */ + /* DiffDOM will filter out magicline plugin elements + in practice this will make it impossible to use it + while someone else is typing, which could be annoying. + + we should check when such an element is going to be + removed, and prevent that from happening. */ if (info.node && info.node.tagName === 'SPAN' && - info.node.getAttribute('contentEditable') === 'false') { - return true; + info.node.getAttribute('contentEditable') === "false") { + // it seems to be a magicline plugin element... + if (info.diff.action === 'removeElement') { + // and you're about to remove it... + // this probably isn't what you want + + /* + I have never seen this in the console, but the + magic line is still getting removed on remote + edits. This suggests that it's getting removed + by something other than diffDom. + */ + console.log("preventing removal of the magic line!"); + + // return true to prevent diff application + return true; + } } + // no use trying to recover the cursor if it doesn't exist if (!cursor.exists()) { return; } + + /* frame is either 0, 1, 2, or 3, depending on which + cursor frames were affected: none, first, last, or both + */ var frame = info.frame = cursor.inNode(info.node); + if (!frame) { return; } - if (typeof info.diff.oldValue === 'string' && - typeof info.diff.newValue === 'string') { - var pushes = cursor.pushDelta(info.diff.oldValue, - info.diff.newValue); + + if (typeof info.diff.oldValue === 'string' && typeof info.diff.newValue === 'string') { + var pushes = cursor.pushDelta(info.diff.oldValue, info.diff.newValue); + if (frame & 1) { + // push cursor start if necessary if (pushes.commonStart < cursor.Range.start.offset) { cursor.Range.start.offset += pushes.delta; } } if (frame & 2) { + // push cursor end if necessary if (pushes.commonStart < cursor.Range.end.offset) { cursor.Range.end.offset += pushes.delta; } @@ -143,7 +171,7 @@ define([ if (info.node) { if (info.frame & 1) { cursor.fixStart(info.node); } if (info.frame & 2) { cursor.fixEnd(info.node); } - } else { console.log("info.node did not exist"); } + } else { console.error("info.node did not exist"); } var sel = cursor.makeSelection(); var range = cursor.makeRange();