From e83af300a8e9b25717968bc2443fa33ec1ef2418 Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 29 Sep 2020 12:01:27 +0200 Subject: [PATCH] Fix debug app history --- www/debug/inner.js | 53 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/www/debug/inner.js b/www/debug/inner.js index 6ca7cab60..a8bacb840 100644 --- a/www/debug/inner.js +++ b/www/debug/inner.js @@ -587,6 +587,10 @@ define([ var setHistory = function (bool, update) { history = bool; if (!bool && update) { config.onRemote(); } + else { + setTimeout(cpNfInner.metadataMgr.refresh); + } + return true; }; var displayDoc = function (doc) { @@ -594,8 +598,33 @@ define([ console.log(doc); }; - var toRestore; + // Get the realtime metadata when in history mode + var getLastMetadata = function () { + var newContentStr = cpNfInner.chainpad.getUserDoc(); + var newContent = JSON.parse(newContentStr); + var meta = extractMetadata(newContent); + return meta; + }; + var setLastMetadata = function (md) { + var newContentStr = cpNfInner.chainpad.getAuthDoc(); + var newContent = JSON.parse(newContentStr); + if (Array.isArray(newContent)) { + newContent[3] = { + metadata: md + }; + } else { + newContent.metadata = md; + } + try { + cpNfInner.chainpad.contentUpdate(JSONSortify(newContent)); + return true; + } catch (e) { + console.error(e); + return false; + } + }; + var toRestore; config.onLocal = function (a, restore) { if (!toRestore || !restore) { return; } cpNfInner.chainpad.contentUpdate(toRestore); @@ -633,13 +662,35 @@ define([ /* add a history button */ var histConfig = { onLocal: function () { + // The following lines allow us to restore an old version from the debug app + // without affecting the snapshots. + // It's parsing, updating and stringifying text data which is not a clean way + // to change metadata, so we're disabling it by default. + if (window.cp_snapshots) { + var md = Util.clone(cpNfInner.metadataMgr.getMetadata()); + var _snapshots = md.snapshots; + var newContent = JSON.parse(toRestore); + try { + if (Array.isArray(newContent)) { + newContent[3].metadata.snapshots = _snapshots; + } else { + newContent.metadata.snapshots = _snapshots; + } + } catch (e) { console.error(e); } + toRestore = JSONSortify(newContent); + } config.onLocal(null, true); }, onRemote: config.onRemote, setHistory: setHistory, extractMetadata: extractMetadata, + getLastMetadata: getLastMetadata, // get from authdoc + setLastMetadata: setLastMetadata, // set to userdoc/authdoc applyVal: function (val) { toRestore = val; + var newContent = JSON.parse(val); + var meta = extractMetadata(newContent); + cpNfInner.metadataMgr.updateMetadata(meta); displayDoc(JSON.parse(val) || {}); }, $toolbar: $bar,