pad

minimal etherpad alternative  https://pad.ce9e.org
git clone https://git.ce9e.org/pad.git

commit
2ac7d8c3e07ed46ccee4669107259a7cc050b21d
parent
c9474ff9eb0e02ce693d98a0de2aa231ecae4446
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-05-03 10:28
preserve selection

Diffstat

M context.mjs 18 +++++++++++++-----
M index.js 7 ++++++-

2 files changed, 19 insertions, 6 deletions


diff --git a/context.mjs b/context.mjs

@@ -1,17 +1,25 @@
    1     1 import * as fuzzy from './fuzzy.mjs';
    2     2 
    3    -1 var applyExact = function(text, [pos, before, after]) {
   -1     3 var applyExact = function(text, [pos, before, after], selection) {
    4     4 	if (text.slice(pos).startsWith(before)) {
   -1     5 		if (selection) {
   -1     6 			if (pos < selection[0]) {
   -1     7 				selection[0] += after.length - before.length;
   -1     8 			}
   -1     9 			if (pos < selection[1]) {
   -1    10 				selection[1] += after.length - before.length;
   -1    11 			}
   -1    12 		}
    5    13 		return text.slice(0, pos) + after + text.slice(pos + before.length);
    6    14 	} else {
    7    15 		throw 'no match';
    8    16 	}
    9    17 };
   10    18 
   11    -1 export var apply = function(text, [pos, before, after]) {
   -1    19 export var apply = function(text, [pos, before, after], selection) {
   12    20 	// try exact match
   13    21 	try {
   14    -1 		return applyExact(text, [pos, before, after]);
   -1    22 		return applyExact(text, [pos, before, after], selection);
   15    23 	} catch (e) {}
   16    24 
   17    25 	// try exact match in similar position
@@ -27,7 +35,7 @@ export var apply = function(text, [pos, before, after]) {
   27    35 		}
   28    36 	}
   29    37 	if (best !== -1) {
   30    -1 		return applyExact(text, [best, before, after]);
   -1    38 		return applyExact(text, [best, before, after], selection);
   31    39 	}
   32    40 
   33    41 	// fall back to fuzzy merge
@@ -36,7 +44,7 @@ export var apply = function(text, [pos, before, after]) {
   36    44 	var ctxEnd = Math.min(pos + before.length + ctxLen, text.length);
   37    45 	var ctx = text.slice(ctxStart, ctxEnd);
   38    46 	var ctxAfter = fuzzy.mergeText(before, after, ctx);
   39    -1 	return applyExact(text, [ctxStart, ctx, ctxAfter]);
   -1    47 	return applyExact(text, [ctxStart, ctx, ctxAfter], selection);
   40    48 };
   41    49 
   42    50 export var diff = function(text1, text2, ctx) {

diff --git a/index.js b/index.js

@@ -26,11 +26,16 @@ setInterval(function() {
   26    26 	}
   27    27 
   28    28 	var text = el.value;
   -1    29 	var selection = [el.selectionStart, el.selectionEnd];
   29    30 	while (remoteChanges.length) {
   30    -1 		text = context.apply(text, remoteChanges.shift());
   -1    31 		text = context.apply(text, remoteChanges.shift(), selection);
   31    32 	}
   32    33 	el.value = text;
   33    34 	old = text;
   -1    35 	if (el.selectionStart !== selection[0] || el.selectionEnd !== selection[1]) {
   -1    36 		el.selectionStart = selection[0];
   -1    37 		el.selectionEnd = selection[1];
   -1    38 	}
   34    39 }, 500);
   35    40 
   36    41 signal.listen(room, function(msg) {