Fix debug app history

This commit is contained in:
yflory 2020-09-29 12:01:27 +02:00
parent 2f6d4d1fc6
commit e83af300a8

View file

@ -587,6 +587,10 @@ define([
var setHistory = function (bool, update) { var setHistory = function (bool, update) {
history = bool; history = bool;
if (!bool && update) { config.onRemote(); } if (!bool && update) { config.onRemote(); }
else {
setTimeout(cpNfInner.metadataMgr.refresh);
}
return true;
}; };
var displayDoc = function (doc) { var displayDoc = function (doc) {
@ -594,8 +598,33 @@ define([
console.log(doc); 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) { config.onLocal = function (a, restore) {
if (!toRestore || !restore) { return; } if (!toRestore || !restore) { return; }
cpNfInner.chainpad.contentUpdate(toRestore); cpNfInner.chainpad.contentUpdate(toRestore);
@ -633,13 +662,35 @@ define([
/* add a history button */ /* add a history button */
var histConfig = { var histConfig = {
onLocal: function () { 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); config.onLocal(null, true);
}, },
onRemote: config.onRemote, onRemote: config.onRemote,
setHistory: setHistory, setHistory: setHistory,
extractMetadata: extractMetadata, extractMetadata: extractMetadata,
getLastMetadata: getLastMetadata, // get from authdoc
setLastMetadata: setLastMetadata, // set to userdoc/authdoc
applyVal: function (val) { applyVal: function (val) {
toRestore = val; toRestore = val;
var newContent = JSON.parse(val);
var meta = extractMetadata(newContent);
cpNfInner.metadataMgr.updateMetadata(meta);
displayDoc(JSON.parse(val) || {}); displayDoc(JSON.parse(val) || {});
}, },
$toolbar: $bar, $toolbar: $bar,