- commit
- b876c69b48654e3f5f57ec5cb759109b88514872
- parent
- cc9dd5fe9c74801e7ce141b971fbe382afde1b5d
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2020-05-03 14:47
cleanup
Diffstat
M | static/context.js | 6 | ++---- |
M | static/index.js | 29 | +++++++++++++++-------------- |
2 files changed, 17 insertions, 18 deletions
diff --git a/static/context.js b/static/context.js
@@ -61,10 +61,8 @@ export var apply = function(text, [pos, before, after], selection) { 61 61 // fall back to fuzzy merge 62 62 var ctxLen = 3; 63 63 var ctxStart = Math.max(pos - ctxLen, 0);64 -1 var ctxEnd = Math.min(pos + before.length + ctxLen, text.length);65 -1 var ctx = text.slice(ctxStart, ctxEnd);66 -1 var ctxAfter = fuzzyMerge(after, ctx);67 -1 return applyExact(text, [ctxStart, ctx, ctxAfter], selection);-1 64 var ctx = text.slice(ctxStart, pos + before.length + ctxLen); -1 65 return applyExact(text, [ctxStart, ctx, fuzzyMerge(after, ctx)], selection); 68 66 }; 69 67 70 68 export var diff = function(text1, text2, ctx) {
diff --git a/static/index.js b/static/index.js
@@ -8,7 +8,20 @@ var el = document.querySelector('textarea'); 8 8 var old = el.value; 9 9 10 10 var localChanges = [];11 -1 var remoteChanges = [];-1 11 -1 12 var applyChanges = function(changes) { -1 13 var text = el.value; -1 14 var selection = [el.selectionStart, el.selectionEnd]; -1 15 while (changes.length) { -1 16 text = context.apply(text, changes.shift(), selection); -1 17 } -1 18 if (text !== el.value) { -1 19 el.value = text; -1 20 el.selectionStart = selection[0]; -1 21 el.selectionEnd = selection[1]; -1 22 old = text; -1 23 } -1 24 }; 12 25 13 26 el.addEventListener('input', function() { 14 27 var change = context.diff(old, el.value, 3); @@ -20,7 +33,7 @@ signal.listen(room, function(msg) { 20 33 if (msg.sender === id) { 21 34 return 22 35 } else if (msg.changes) {23 -1 remoteChanges = remoteChanges.concat(msg.changes);-1 36 applyChanges(msg.changes); 24 37 } else if (msg.request === 'text') { 25 38 signal.post(room, {sender: id, text: el.value}); 26 39 } else if (msg.text && !el.value) { @@ -39,18 +52,6 @@ setInterval(function() { 39 52 }); 40 53 localChanges = []; 41 54 }42 -143 -1 var text = el.value;44 -1 var selection = [el.selectionStart, el.selectionEnd];45 -1 while (remoteChanges.length) {46 -1 text = context.apply(text, remoteChanges.shift(), selection);47 -1 }48 -1 if (text !== el.value) {49 -1 el.value = text;50 -1 el.selectionStart = selection[0];51 -1 el.selectionEnd = selection[1];52 -1 old = text;53 -1 }54 55 }, 500); 55 56 56 57 signal.post(room, {sender: id, request: 'text'});