From a46baea85bb7135c2891e4c3e33f33007a2f9033 Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 17 Sep 2020 17:36:05 +0200 Subject: [PATCH] OnlyOffice history --- www/common/onlyoffice/inner.js | 63 ++++++++++++++++++++++++++++- www/common/sframe-common-history.js | 27 +++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/www/common/onlyoffice/inner.js b/www/common/onlyoffice/inner.js index 5c36f6604..4a5a4a9e5 100644 --- a/www/common/onlyoffice/inner.js +++ b/www/common/onlyoffice/inner.js @@ -404,6 +404,7 @@ define([ clearTimeout(pendingChanges[key]); delete pendingChanges[key]; }); + if (APP.stopHistory) { APP.history = false; } startOO(blob, type, true); }; @@ -619,6 +620,7 @@ define([ removeClient(obj.data); break; case 'MESSAGE': + if (APP.history) { return; } if (ooChannel.ready) { // In read-only mode, push the message to the queue and prompt // the user to refresh OO (without reloading the page) @@ -1041,7 +1043,7 @@ define([ startOO = function (blob, file, force) { if (APP.ooconfig && !force) { return void console.error('already started'); } var url = URL.createObjectURL(blob); - var lock = readOnly || APP.migrate; + var lock = readOnly || APP.migrate || APP.history; // Starting from version 3, we can use the view mode again // defined but never used @@ -1732,6 +1734,65 @@ define([ makeCheckpoint(true); }); $save.appendTo(toolbar.$bottomM); + + (function () { + /* add a history button */ + // XXX move out of dev mode + var commit = function () { + // ~ Commit current version + // XXX make checkpoint? + }; + var loadCp = function (cp) { + loadLastDocument(cp, function () { + var file = getFileType(); + var type = common.getMetadataMgr().getPrivateData().ooType; + var blob = loadInitDocument(type, true); + resetData(blob, file); + }, function (blob, file) { + // XXX ? + resetData(blob, file); + }); + }; + var draw = function (cp, patch) { + if (typeof(cp) === "undefined") { + // Patch on the current cp + // XXX send patch to oo + return; + } + // We want to load a checkpoint: + loadCp(cp); + console.error('draw'); + }; + var setHistoryMode = function (bool, update) { + if (bool) { + APP.history = true; + getEditor().setViewModeDisconnect(); + return; + } + APP.stopHistory = true; + if (update) { + // We want to commit the current version + // Do nothing: we're going to create a cp + return; + } + // Cancel button: redraw from lastCp + var lastCp = getLastCp(); + loadCp(lastCp); + + //redraw(); + }; + var histConfig = { + onLocal: draw, + onRemote: function () {}, + setHistory: setHistoryMode, + applyVal: draw, + onlyoffice: content.hashes || {}, + $toolbar: $(toolbarContainer) + }; + var $hist = common.createButton('history', true, {histConfig: histConfig}); + $hist.addClass('cp-hidden-if-readonly'); + toolbar.$drawer.append($hist); + })(); } if (window.CP_DEV_MODE || DISPLAY_RESTORE_BUTTON) { common.createButton('', true, { diff --git a/www/common/sframe-common-history.js b/www/common/sframe-common-history.js index 399357106..74a5dd1d7 100644 --- a/www/common/sframe-common-history.js +++ b/www/common/sframe-common-history.js @@ -54,6 +54,29 @@ define([ }); }; + var cpIndex = 0; + var sortedCp; + if (config.onlyoffice) { + sortedCp = Object.keys(config.onlyoffice).map(Number).sort(function (a, b) { + return a-b; + }); + } + var loadMoreOOHistory = function () { + if (!Array.isArray(sortedCp)) { + return void console.error("Wrong type"); + } + var hashes = config.onlyoffice; + if (!sortedCp.length) { + // XXX no cp + } + var cp = {}; + if (cpIndex < sortedCp.length) { + var idx = sortedCp[sortedCp.length - 1 - cpIndex]; + cp = config.onlyoffice[idx]; + } + config.onLocal(cp); + }; + var allMessages = []; var lastKnownHash; var isComplete = false; @@ -334,6 +357,10 @@ define([ config.onOpen(); } + if (config.onlyoffice) { + return void loadMoreOOHistory(); + } + // Load all the history messages into a new chainpad object loadMoreHistory(config, common, function (err, newRt, isFull) { History.readOnly = common.getMetadataMgr().getPrivateData().readOnly;