From 4eedb599d8de5559daa4678c7520bb36968e767e Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 28 Jan 2023 21:24:11 +0100 Subject: Server to client: diffs --- html/whiteboard.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'html/whiteboard.js') diff --git a/html/whiteboard.js b/html/whiteboard.js index 58023fe..3e09621 100644 --- a/html/whiteboard.js +++ b/html/whiteboard.js @@ -48,6 +48,26 @@ function on_getfile(data, rev, pos) textAreaSetPos("board", pos); } +function on_getdiff(diff, rev) +{ + if (rev != revision + 1) + alert("Revision skipped: " + 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; +} + function on_getpos(pos) { textAreaSetPos("board", pos); @@ -88,6 +108,9 @@ function on_message(e) { 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)); } else if (type == "getpos") { on_getpos(parseInt(xmlDocument.getElementsByTagName("pos")[0].textContent)); } else if (type == "modify") { @@ -216,10 +239,6 @@ function on_input() idElement.appendChild(document.createTextNode(get_id())); requestElement.appendChild(idElement); - var dataElement = xmlDocument.createElement("data"); - dataElement.appendChild(document.createTextNode(document.getElementById("board").value)); - requestElement.appendChild(dataElement); - baseline_candidate = document.getElementById("board").value; var revisionElement = xmlDocument.createElement("baserev"); -- cgit v1.2.3