summaryrefslogtreecommitdiffhomepage
path: root/html/whiteboard.js
blob: b11dc96cb0c588bfadedfecbe5cda002af2a84d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// started on main page load
function init() {
	init_board();
}

var revision;
var modify_in_progress = 0;
var baseline = ""; // data contents relating to revision, acknowledged by server
var baseline_candidate = ""; // will become baseline, after ack by server

// helper for breaking feedback loop
var caretpos = 0;

function set_status(message)
{
 if (message == "") {
	document.getElementById("status").textContent = message;
	document.getElementById("status").style.display = 'inline';
 } else {
	document.getElementById("status").textContent = "";
	document.getElementById("status").style.display = 'none';
 }
}

function showQRWindow()
{
 document.getElementById("qrwindow").style.display = 'block';
}

function hideQRWindow()
{
 document.getElementById("qrwindow").style.display = 'none';
}

var websocket;

//
// Callbacks for websocket data of different types
//

function on_getfile(data, rev, pos)
{
	var board = document.getElementById("board");
	if (board.value != data) {
		board.value = data;
	}
	revision = rev;
        baseline = data;
	textAreaSetPos("board", pos);
}

function on_getdiff(diff, rev, pos)
{
        if (rev != revision + 1)
         console.log("Revision skipped on diff receive: " + rev + " after " + revision);

	var board = document.getElementById("board");

        var old_version_ptr = allocateUTF8(board.value);
        var diff_ptr = allocateUTF8(new XMLSerializer().serializeToString(diff));
        var new_version_ptr = Module._diff_apply(old_version_ptr, diff_ptr);
        var data = UTF8ToString(new_version_ptr);
	board.value = data;
        _free(old_version_ptr);
        _free(new_version_ptr);
        _free(diff_ptr);

	revision = rev;
        baseline = data;
	textAreaSetPos("board", pos);
}

function on_getpos(pos)
{
	textAreaSetPos("board", pos);
}

function on_newid(id)
{
	var new_location = document.location.origin + document.location.pathname + '?id=' + id;
	window.location.href = new_location;
}

function on_qrcode(png)
{
	var url = "data:image/png;base64," + png;
	var img = document.getElementById("qrcode");
	img.src = url;
	showQRWindow();
}

function on_version(version)
{
	document.getElementById("version").textContent = version;
}

function on_modify_ack(rev)
{
        if (rev != revision + 1)
         console.log("Revision skipped on published local change: " + rev + " after " + revision);

	revision = rev;
        baseline = baseline_candidate;
        modify_in_progress = 0;
}

function on_message(e) {
	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString(e.data, "text/xml");
	
	var type = xmlDocument.getElementsByTagName("type")[0].textContent;

	if (type == "getfile") {
		on_getfile(xmlDocument.getElementsByTagName("data")[0].textContent,
			   parseInt(xmlDocument.getElementsByTagName("revision")[0].textContent),
		           parseInt(xmlDocument.getElementsByTagName("pos")[0].textContent));
        } else if (type == "getdiff") {
		on_getdiff(xmlDocument.getElementsByTagName("diff")[0],
			   parseInt(xmlDocument.getElementsByTagName("revision")[0].textContent),
                           parseInt(xmlDocument.getElementsByTagName("pos")[0].textContent));
	} else if (type == "getpos") {
		on_getpos(parseInt(xmlDocument.getElementsByTagName("pos")[0].textContent));
	} else if (type == "modify") {
		on_modify_ack(parseInt(xmlDocument.getElementsByTagName("revision")[0].textContent));
	} else if (type == "newid") {
		on_newid(xmlDocument.getElementsByTagName("id")[0].textContent);
	} else if (type == "qrcode") {
		on_qrcode(xmlDocument.getElementsByTagName("png")[0].textContent);
	} else if (type == "version") {
		on_version(xmlDocument.getElementsByTagName("version")[0].textContent);
	} else if (type == "error") {
		alert(xmlDocument.getElementsByTagName("message")[0].textContent);
	} else {
		alert("Unhandled message type: " + e.data + "|" + type);
	}
}

function handleSelection() {
  const activeElement = document.activeElement

  if (activeElement && activeElement.id === 'board') {
    if (caretpos != activeElement.selectionStart) {
      on_selectionchange(activeElement.selectionStart);
      caretpos = activeElement.selectionStart;
    }
  }
}

function connect_websocket() {
	document.getElementById("reconnect").style.display = 'none';
        set_status("Connecting...");
	var newlocation = location.origin + location.pathname;
	newlocation = newlocation.replace(/^http/, 'ws');
	if (newlocation.slice(-1) != "/")
		newlocation += "/";
	newlocation += "websocket";

	websocket = new WebSocket(newlocation);

	websocket.onmessage = function(e) { on_message(e); };

	websocket.onopen = function(e) {
		const searchParams = (new URL(document.location)).searchParams;
		if (!searchParams.has('id')) {
			redirect_to_new_page();
			return;
		}

		websocket.send("<request><command>getversion</command></request>");
		websocket.send("<request><command>getfile</command><id>" + get_id() + "</id></request>");
                set_status(""); // ok
	};
	
	websocket.onclose = function(e) {
		alert("Server connection closed.");
		document.getElementById("reconnect").style.display = 'inline';
	};

	websocket.onerror = function(e) {
		alert("Error: Server connection closed.");
		document.getElementById("reconnect").style.display = 'inline';
	};
}

// button in html
function on_reconnect_click() {
	connect_websocket();
}

function init_board() {
        set_status("Loading...");
	Module.onRuntimeInitialized = () => {
         connect_websocket();
        };
	
	var board = document.getElementById("board");
	board.addEventListener("input", function() {on_input(); });
	// Need this workaround (different from direct on_selectionchange) for Chrome.
	// Otherwise, callback will not be called on Chrome.
	document.addEventListener("selectionchange", handleSelection);
	//board.addEventListener("selectionchange", function() {on_selectionchange(); });

        document.getElementById("qrwindow").onclick = function() {
         hideQRWindow();
        }

        document.onkeydown = function(evt) {
         if (evt.key == "Escape") {
          hideQRWindow();
         }
        }

	document.getElementById("board").focus();
}

function get_id()
{
        const searchParams = (new URL(document.location)).searchParams;
        return searchParams.get('id');
}

// from html
function on_new_page()
{
	redirect_to_new_page();
}

function redirect_to_new_page()
{
	websocket.send("<request><command>newid</command></request>");
}

// local change done
function on_input()
{
        if (modify_in_progress == 1) {
                console.log("Deferring on_input handler by 100ms");
                setTimeout(function(){on_input();}, 100); // re-try after 100ms
                return;
        }
        modify_in_progress = 1;

	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
	
	var requestElement = xmlDocument.getElementsByTagName("request")[0];

	var commandElement = xmlDocument.createElement("command");
	commandElement.appendChild(document.createTextNode("modify"));
	requestElement.appendChild(commandElement);

	var idElement = xmlDocument.createElement("id");
	idElement.appendChild(document.createTextNode(get_id()));
	requestElement.appendChild(idElement);

        baseline_candidate = document.getElementById("board").value;

        if (baseline == baseline_candidate) {
         modify_in_progress = 0;
         return;
        }

	var revisionElement = xmlDocument.createElement("baserev");
	revisionElement.appendChild(document.createTextNode(revision));
	requestElement.appendChild(revisionElement);
	
	//Module.onRuntimeInitialized = () => { alert("DEBUG: " + Module._getnum(1) + " " + UTF8ToString(Module._getstring(allocateUTF8("abc"))));
		//_free(allocateUTF8("abc"));
	//};
        var old_version = allocateUTF8(baseline);
        var new_version = allocateUTF8(baseline_candidate);
        var diff = Module._diff_create(old_version, new_version);
        var diffDocument = parser.parseFromString(UTF8ToString(diff), "text/xml");
        _free(old_version);
        _free(new_version);
        _free(diff);
	requestElement.appendChild(xmlDocument.importNode(diffDocument.getElementsByTagName("diff")[0], true));
	
        var posElement = xmlDocument.createElement("pos");
	posElement.appendChild(document.createTextNode(document.getElementById("board").selectionStart));
	requestElement.appendChild(posElement);

	websocket.send(new XMLSerializer().serializeToString(xmlDocument));
}

// for cursor position
function on_selectionchange(pos)
{
	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
	
	var requestElement = xmlDocument.getElementsByTagName("request")[0];

	var commandElement = xmlDocument.createElement("command");
	commandElement.appendChild(document.createTextNode("cursorpos"));
	requestElement.appendChild(commandElement);

	var idElement = xmlDocument.createElement("id");
	idElement.appendChild(document.createTextNode(get_id()));
	requestElement.appendChild(idElement);

	var posElement = xmlDocument.createElement("pos");
	posElement.appendChild(document.createTextNode(pos));
	requestElement.appendChild(posElement);

	websocket.send(new XMLSerializer().serializeToString(xmlDocument));
}

function textAreaSetPos(id, pos)
{
	if (document.getElementById(id).selectionStart != pos) {
		document.getElementById(id).selectionStart = pos;
		document.getElementById(id).selectionEnd = pos;
		caretpos = pos;
	}
}

// HTML button
function on_qrcode_click()
{
	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
	
	var requestElement = xmlDocument.getElementsByTagName("request")[0];

	var commandElement = xmlDocument.createElement("command");
	commandElement.appendChild(document.createTextNode("qrcode"));
	requestElement.appendChild(commandElement);

	var idElement = xmlDocument.createElement("url");
	idElement.appendChild(document.createTextNode(document.location));
	requestElement.appendChild(idElement);

	websocket.send(new XMLSerializer().serializeToString(xmlDocument));
}