pad

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

commit
c9474ff9eb0e02ce693d98a0de2aa231ecae4446
parent
4f6e091219172b08c720c61baabf9c228c019b0f
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-05-03 09:17
use via for network

Diffstat

M index.html 2 +-
M index.js 84 ++++++++++++++++++++++++++++---------------------------------
A signal.mjs 28 ++++++++++++++++++++++++++++

3 files changed, 68 insertions, 46 deletions


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

@@ -3,7 +3,7 @@
    3     3 <head>
    4     4 	<meta charset="utf-8" />
    5     5 	<title>duct pad</title>
    6    -1 	<meta http-equiv="Content-Security-Policy" content="default-src 'self'">
   -1     6 	<meta http-equiv="Content-Security-Policy" content="default-src 'self'; connect-src http://via.ce9e.org">
    7     7 	<meta name="viewport" content="width=device-width" />
    8     8 	<link rel="stylesheet" href="common.css">
    9     9 	<link rel="stylesheet" href="rtc.css">

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

@@ -1,46 +1,40 @@
    1     1 import * as context from './context.mjs';
    2    -1 
    3    -1 var local = document.querySelector('#local');
    4    -1 var remote = document.querySelector('#remote');
    5    -1 
    6    -1 
    7    -1 var listeners = [];
    8    -1 var send = function(sender, changes) {
    9    -1 	listeners.forEach(l => l(sender, changes));
   10    -1 };
   11    -1 
   12    -1 var pad = function(el) {
   13    -1 	var id = el.id;
   14    -1 	var old = el.value;
   15    -1 
   16    -1 	var localChanges = [];
   17    -1 	var remoteChanges = [];
   18    -1 
   19    -1 	el.addEventListener('input', function() {
   20    -1 		var change = context.diff(old, el.value, 3);
   21    -1 		context.pushChange(localChanges, change);
   22    -1 		old = el.value;
   23    -1 	});
   24    -1 
   25    -1 	setInterval(function() {
   26    -1 		if (localChanges.length) {
   27    -1 			send(id, localChanges);
   28    -1 			localChanges = [];
   29    -1 		}
   30    -1 
   31    -1 		var text = el.value;
   32    -1 		while (remoteChanges.length) {
   33    -1 			text = context.apply(text, remoteChanges.shift());
   34    -1 		}
   35    -1 		el.value = text;
   36    -1 	}, 500);
   37    -1 
   38    -1 	listeners.push(function(sender, changes) {
   39    -1 		if (sender !== id) {
   40    -1 			remoteChanges = remoteChanges.concat(changes);
   41    -1 		}
   42    -1 	});
   43    -1 };
   44    -1 
   45    -1 pad(local);
   46    -1 pad(remote);
   -1     2 import * as signal from './signal.js';
   -1     3 
   -1     4 var room = location.hash.substr(1);
   -1     5 var id = signal.randomString(10);
   -1     6 var el = document.querySelector('textarea');
   -1     7 
   -1     8 var old = el.value;
   -1     9 
   -1    10 var localChanges = [];
   -1    11 var remoteChanges = [];
   -1    12 
   -1    13 el.addEventListener('input', function() {
   -1    14 	var change = context.diff(old, el.value, 3);
   -1    15 	context.pushChange(localChanges, change);
   -1    16 	old = el.value;
   -1    17 });
   -1    18 
   -1    19 setInterval(function() {
   -1    20 	if (localChanges.length) {
   -1    21 		signal.post(room, {
   -1    22 			sender: id,
   -1    23 			changes: localChanges,
   -1    24 		});
   -1    25 		localChanges = [];
   -1    26 	}
   -1    27 
   -1    28 	var text = el.value;
   -1    29 	while (remoteChanges.length) {
   -1    30 		text = context.apply(text, remoteChanges.shift());
   -1    31 	}
   -1    32 	el.value = text;
   -1    33 	old = text;
   -1    34 }, 500);
   -1    35 
   -1    36 signal.listen(room, function(msg) {
   -1    37 	if (msg.sender !== id && msg.changes) {
   -1    38 		remoteChanges = remoteChanges.concat(msg.changes);
   -1    39 	}
   -1    40 });

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

@@ -0,0 +1,28 @@
   -1     1 var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
   -1     2 var baseUrl = 'https://via.ce9e.org/';
   -1     3 
   -1     4 export var randomString = function(length) {
   -1     5 	var result = [];
   -1     6 	for (var i = 0; i < length; i++) {
   -1     7 		var k = Math.floor(Math.random() * chars.length);
   -1     8 		result.push(chars[k]);
   -1     9 	}
   -1    10 	return result.join('');
   -1    11 };
   -1    12 
   -1    13 export var post = function(key, data) {
   -1    14 	return fetch(baseUrl + key, {method: 'POST', body: JSON.stringify(data)});
   -1    15 };
   -1    16 
   -1    17 export var beacon = function(key, data) {
   -1    18 	return navigator.sendBeacon(baseUrl + key, JSON.stringify(data));
   -1    19 };
   -1    20 
   -1    21 export var listen = function(key, fn) {
   -1    22 	var evtSource = new EventSource(baseUrl + key + '?sse');
   -1    23 	evtSource.onmessage = msg => fn(JSON.parse(msg.data));
   -1    24 };
   -1    25 
   -1    26 if (!location.hash) {
   -1    27 	location.hash = randomString(10);
   -1    28 }