pad

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

commit
eebf1020c1487478e794e2053b6ec77f0e1f612c
parent
fb244df285c69867dfe50d9131ab25505a8b06f1
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2026-11-07 08:16
refactor js

Diffstat

M static/diff.js 24 ++++++++++++------------
M static/pad.js 30 +++++++++++++++---------------
M static/test.js 8 ++++----
M static/utils.js 12 ++++++------

4 files changed, 37 insertions, 37 deletions


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

@@ -1,4 +1,4 @@
    1    -1 var _apply = function(text, [pos, before, after], selection) {
   -1     1 function _apply(text, [pos, before, after], selection) {
    2     2 	if (selection) {
    3     3 		var d = diff(before, after, 0);
    4     4 		if (pos + d[0] < selection[0]) {
@@ -9,9 +9,9 @@ var _apply = function(text, [pos, before, after], selection) {
    9     9 		}
   10    10 	}
   11    11 	return text.slice(0, pos) + after + text.slice(pos + before.length);
   12    -1 };
   -1    12 }
   13    13 
   14    -1 export var apply = function(text, [start, end, before, after], selection) {
   -1    14 export function apply(text, [start, end, before, after], selection) {
   15    15 	// special handling for resets
   16    16 	if (start === 0 && end === 0 && before === '' && text !== '') {
   17    17 		[start, end, before, after] = diff(text, after, 3);
@@ -40,13 +40,13 @@ export var apply = function(text, [start, end, before, after], selection) {
   40    40 
   41    41 	// otherwise, ignore the change
   42    42 	return text;
   43    -1 };
   -1    43 }
   44    44 
   45    -1 export var unapply = function(text, [start, end, before, after], selection) {
   -1    45 export function unapply(text, [start, end, before, after], selection) {
   46    46 	return apply(text, [start, end, after, before], selection);
   47    -1 };
   -1    47 }
   48    48 
   49    -1 export var diff = function(text1, text2, ctx) {
   -1    49 export function diff(text1, text2, ctx) {
   50    50 	var start = 0;
   51    51 	var end = 0;
   52    52 
@@ -61,9 +61,9 @@ export var diff = function(text1, text2, ctx) {
   61    61 	end = Math.max(0, end - ctx);
   62    62 
   63    63 	return [start, end, text1.slice(start, -end || Infinity), text2.slice(start, -end || Infinity)];
   64    -1 };
   -1    64 }
   65    65 
   66    -1 export var merge = function([start1, end1, before1, after1], [start2, end2, before2, after2]) {
   -1    66 export function merge([start1, end1, before1, after1], [start2, end2, before2, after2]) {
   67    67 	if (start1 + after1.length + end1 === start2 + before2.length + end2) {
   68    68 		var d = start2 - start1;
   69    69 
@@ -84,9 +84,9 @@ export var merge = function([start1, end1, before1, after1], [start2, end2, befo
   84    84 		[start1, end1, before1, after1],
   85    85 		[start2, end2, before2, after2],
   86    86 	];
   87    -1 };
   -1    87 }
   88    88 
   89    -1 export var pushChange = function(changes, change) {
   -1    89 export function pushChange(changes, change) {
   90    90 	if (!changes.length) {
   91    91 		changes.push(change);
   92    92 	} else {
@@ -95,4 +95,4 @@ export var pushChange = function(changes, change) {
   95    95 			changes.push(m);
   96    96 		}
   97    97 	}
   98    -1 };
   -1    98 }

diff --git a/static/pad.js b/static/pad.js

@@ -18,12 +18,12 @@ var lastEventId = 0;
   18    18 
   19    19 document.title += ' - ' + location.hash.slice(1);
   20    20 
   21    -1 var exit = function() {
   -1    21 function exit() {
   22    22 	el.readOnly = true;
   23    23 	eventSource.close();
   24    -1 };
   -1    24 }
   25    25 
   26    -1 var fetchJSON = function(res, options) {
   -1    26 function fetchJSON(res, options) {
   27    27 	return fetch(res, options).then(response => {
   28    28 		if (response.ok) {
   29    29 			return response.json();
@@ -31,7 +31,7 @@ var fetchJSON = function(res, options) {
   31    31 			throw response;
   32    32 		}
   33    33 	});
   34    -1 };
   -1    34 }
   35    35 
   36    36 var sendChanges = utils.throttled(function() {
   37    37 	if (stagedChanges.length) {
@@ -61,7 +61,7 @@ var sendChanges = utils.throttled(function() {
   61    61 	}
   62    62 }, 500);
   63    63 
   64    -1 var applyChanges = function(changes) {
   -1    64 function applyChanges(changes) {
   65    65 	var selection = [el.selectionStart, el.selectionEnd, el.selectionDirection];
   66    66 	var text = el.value;
   67    67 	var myChanges = [].concat(stagedChanges, localChanges);
@@ -84,14 +84,14 @@ var applyChanges = function(changes) {
   84    84 		el.selectionStart = selection[0];
   85    85 		el.selectionEnd = selection[1];
   86    86 		el.selectionDirection = selection[2];
   87    -1 		requestAnimationFrame(function() {
   -1    87 		requestAnimationFrame(() => {
   88    88 			el.scrollTop = scrollTop;
   89    89 		});
   90    90 		old = text;
   91    91 	}
   92    -1 };
   -1    92 }
   93    93 
   94    -1 var handleMessage = function(msg) {
   -1    94 function handleMessage(msg) {
   95    95 	if (msg[1] === 'changes') {
   96    96 		if (msg[0] === id) {
   97    97 			stagedChanges = [];
@@ -99,9 +99,9 @@ var handleMessage = function(msg) {
   99    99 			applyChanges(msg[2]);
  100   100 		}
  101   101 	}
  102    -1 };
   -1   102 }
  103   103 
  104    -1 var consolidate = function() {
   -1   104 function consolidate() {
  105   105 	var text = el.value;
  106   106 	var myChanges = [].concat(stagedChanges, localChanges);
  107   107 
@@ -115,16 +115,16 @@ var consolidate = function() {
  115   115 		body: JSON.stringify(data),
  116   116 		headers: {'Last-Event-ID': lastEventId},
  117   117 	});
  118    -1 };
   -1   118 }
  119   119 
  120    -1 el.addEventListener('input', function() {
   -1   120 el.addEventListener('input', () => {
  121   121 	var change = diff.diff(old, el.value, 3);
  122   122 	diff.pushChange(localChanges, change);
  123   123 	old = el.value;
  124   124 	sendChanges();
  125   125 });
  126   126 
  127    -1 document.querySelector('button').addEventListener('click', function() {
   -1   127 document.querySelector('button').addEventListener('click', () => {
  128   128 	if (!confirm('Are you sure you want to delete all data?')) {
  129   129 		return;
  130   130 	}
@@ -141,10 +141,10 @@ document.querySelector('button').addEventListener('click', function() {
  141   141 });
  142   142 
  143   143 var eventSource = new EventSource(url);
  144    -1 eventSource.onopen = function() {
   -1   144 eventSource.onopen = () => {
  145   145 	el.readOnly = false;
  146   146 };
  147    -1 eventSource.onmessage = function(event) {
   -1   147 eventSource.onmessage = event => {
  148   148 	handleMessage(JSON.parse(event.data));
  149   149 	lastEventId = event.lastEventId;
  150   150 };

diff --git a/static/test.js b/static/test.js

@@ -1,18 +1,18 @@
    1     1 import * as diff from './diff.js';
    2     2 
    3    -1 var assertEqual = function(a, b, msg) {
   -1     3 function assertEqual(a, b, msg) {
    4     4 	if (JSON.stringify(a) !== JSON.stringify(b)) {
    5     5 		console.warn('test failed:', msg || '', a, b);
    6     6 	} else {
    7     7 		console.log('test passed:', msg || '');
    8     8 	}
    9    -1 };
   -1     9 }
   10    10 
   11    -1 var testChange = function(a, b) {
   -1    11 function testChange(a, b) {
   12    12 	var change = diff.diff(a, b, 3);
   13    13 	var actual = diff.apply(a, change);
   14    14 	assertEqual(actual, b, `testChange: "${a}" -> "${b}"`);
   15    -1 };
   -1    15 }
   16    16 
   17    17 testChange('', 'asd');
   18    18 testChange('foobar', 'foobarbaz');

diff --git a/static/utils.js b/static/utils.js

@@ -1,21 +1,21 @@
    1     1 var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    2     2 
    3    -1 export var randomString = function(length) {
   -1     3 export function randomString(length) {
    4     4 	var result = [];
    5     5 	for (var i = 0; i < length; i++) {
    6     6 		var k = Math.floor(Math.random() * chars.length);
    7     7 		result.push(chars[k]);
    8     8 	}
    9     9 	return result.join('');
   10    -1 };
   -1    10 }
   11    11 
   12    -1 export var throttled = function(fn, timeout) {
   -1    12 export function throttled(fn, timeout) {
   13    13 	var blocked = false;
   14    14 
   15    -1 	var wrapper = function() {
   -1    15 	var wrapper = () => {
   16    16 		if (!blocked) {
   17    17 			blocked = true;
   18    -1 			setTimeout(function() {
   -1    18 			setTimeout(() => {
   19    19 				fn();
   20    20 				blocked = false;
   21    21 			}, timeout);
@@ -23,4 +23,4 @@ export var throttled = function(fn, timeout) {
   23    23 	};
   24    24 
   25    25 	return wrapper;
   26    -1 };
   -1    26 }