Versioned hash for onlyoffice

pull/1/head
yflory 4 years ago
parent 68423f49bc
commit 16af013bce

@ -40,13 +40,16 @@ define([
if (endWithCp) { cpIndex = 0; } if (endWithCp) { cpIndex = 0; }
var $version, $time; var $version, $time, $share;
var $hist = $toolbar.find('.cp-toolbar-history'); var $hist = $toolbar.find('.cp-toolbar-history');
var $bottom = $toolbar.find('.cp-toolbar-bottom'); var $bottom = $toolbar.find('.cp-toolbar-bottom');
var showVersion = function (initial) { var getVersion = function () {
var major = sortedCp.length - cpIndex; var major = sortedCp.length - cpIndex;
var v = major + '.' + msgIndex; return major + '.' + msgIndex;
};
var showVersion = function (initial) {
var v = getVersion();
if (initial) { if (initial) {
v = "Latest"; // XXX v = "Latest"; // XXX
} }
@ -113,6 +116,7 @@ define([
fillOO(id, messages); fillOO(id, messages);
loading = false; loading = false;
config.onCheckpoint(cp); config.onCheckpoint(cp);
$share.show();
}); });
}; };
@ -199,10 +203,10 @@ define([
$next.hide().appendTo($hist); $next.hide().appendTo($hist);
$fastNext.hide().appendTo($hist); $fastNext.hide().appendTo($hist);
var $share = $('<button>', { $share = $('<button>', {
'class': 'fa fa-shhare-alt buttonPrimary', 'class': 'fa fa-shhare-alt buttonPrimary',
title: Messages.shareButton title: Messages.shareButton
}).appendTo($hist); }).hide().appendTo($hist);
$('<span>', {'class': 'cp-history-filler'}).appendTo($hist); $('<span>', {'class': 'cp-history-filler'}).appendTo($hist);
$time = $(h('div')).appendTo($hist); $time = $(h('div')).appendTo($hist);
var $close = $('<button>', { var $close = $('<button>', {
@ -247,6 +251,9 @@ define([
// Versioned link // Versioned link
$share.click(function () { $share.click(function () {
// XXX // XXX
common.getSframeChannel().event('EV_SHARE_OPEN', {
versionHash: getVersion()
});
}); });
// Push one patch // Push one patch

@ -409,7 +409,7 @@ define([
} }
myUniqueOOId = undefined; myUniqueOOId = undefined;
setMyId(); setMyId();
APP.docEditor.destroyEditor(); // Kill the old editor if (APP.docEditor) { APP.docEditor.destroyEditor(); } // Kill the old editor
$('iframe[name="frameEditor"]').after(h('div#cp-app-oo-placeholder')).remove(); $('iframe[name="frameEditor"]').after(h('div#cp-app-oo-placeholder')).remove();
ooLoaded = false; ooLoaded = false;
oldLocks = {}; oldLocks = {};
@ -546,6 +546,9 @@ define([
return new Blob([newText], {type: 'text/plain'}); return new Blob([newText], {type: 'text/plain'});
}; };
var loadLastDocument = function (lastCp, onCpError, cb) { var loadLastDocument = function (lastCp, onCpError, cb) {
if (!lastCp || !lastCp.file) {
return void onCpError('EEMPTY');
}
ooChannel.cpIndex = lastCp.index || 0; ooChannel.cpIndex = lastCp.index || 0;
ooChannel.lastHash = lastCp.hash; ooChannel.lastHash = lastCp.hash;
var parsed = Hash.parsePadUrl(lastCp.file); var parsed = Hash.parsePadUrl(lastCp.file);
@ -611,6 +614,52 @@ define([
}); });
}; };
var openVersionHash = function (version) {
readOnly = true;
var hashes = content.hashes || {};
var sortedCp = Object.keys(hashes).map(Number).sort(function (a, b) {
return hashes[a].index - hashes[b].index;
});
var s = version.split('.');
if (s.length !== 2) { return; }
var major = Number(s[0]);
var cpId = sortedCp[major - 1];
var nextCpId = sortedCp[major];
var cp = hashes[cpId] || {};
var minor = Number(s[1]) + 1;
var toHash = cp.hash || 'NONE';
var fromHash = nextCpId ? hashes[nextCpId].hash : 'NONE';
sframeChan.query('Q_GET_HISTORY_RANGE', {
channel: content.channel,
lastKnownHash: fromHash,
toHash: toHash,
}, function (err, data) {
if (err) { return void console.error(err); }
if (!Array.isArray(data.messages)) { return void console.error('Not an array!'); }
var messages = (data.messages || []).slice(1, minor).forEach(function (obj) {
try { obj.msg = JSON.parse(obj.msg); } catch (e) { console.error(e); }
});
loadLastDocument(cp, function () {
var file = getFileType();
var type = common.getMetadataMgr().getPrivateData().ooType;
var blob = loadInitDocument(type, true);
ooChannel.queue = messages;
resetData(blob, file);
UI.removeLoadingScreen();
}, function (blob, file) {
ooChannel.queue = messages;
resetData(blob, file);
UI.removeLoadingScreen();
});
});
};
var openRtChannel = function (cb) { var openRtChannel = function (cb) {
if (rtChannel.ready) { return void cb(); } if (rtChannel.ready) { return void cb(); }
var chan = content.channel || Hash.createChannelId(); var chan = content.channel || Hash.createChannelId();
@ -1763,75 +1812,75 @@ define([
makeCheckpoint(true); makeCheckpoint(true);
}); });
$save.appendTo(toolbar.$bottomM); $save.appendTo(toolbar.$bottomM);
}
(function () { (function () {
/* add a history button */ /* add a history button */
// XXX move out of dev mode var commit = function () {
var commit = function () { APP.stopHistory = true;
APP.stopHistory = true; makeCheckpoint(true);
makeCheckpoint(true); };
}; var loadCp = function (cp) {
var loadCp = function (cp) { loadLastDocument(cp, function () {
loadLastDocument(cp, function () { var file = getFileType();
var file = getFileType(); var type = common.getMetadataMgr().getPrivateData().ooType;
var type = common.getMetadataMgr().getPrivateData().ooType; var blob = loadInitDocument(type, true);
var blob = loadInitDocument(type, true);
ooChannel.queue = [];
resetData(blob, file);
}, function (blob, file) {
ooChannel.queue = [];
resetData(blob, file);
});
};
var onPatch = function (patch) {
// Patch on the current cp
ooChannel.send(JSON.parse(patch.msg));
};
var onCheckpoint = function (cp) {
// We want to load a checkpoint:
loadCp(cp);
};
var setHistoryMode = function (bool) {
if (bool) {
APP.history = true;
getEditor().setViewModeDisconnect();
return;
}
// Cancel button: redraw from lastCp
APP.history = false;
ooChannel.queue = []; ooChannel.queue = [];
ooChannel.ready = false; resetData(blob, file);
rtChannel.getHistory(function () { }, function (blob, file) {
var lastCp = getLastCp(); ooChannel.queue = [];
loadCp(lastCp); resetData(blob, file);
}); });
};
var onPatch = function (patch) {
// Patch on the current cp
ooChannel.send(JSON.parse(patch.msg));
};
var onCheckpoint = function (cp) {
// We want to load a checkpoint:
loadCp(cp);
};
var setHistoryMode = function (bool) {
if (bool) {
APP.history = true;
getEditor().setViewModeDisconnect();
return;
}
// Cancel button: redraw from lastCp
APP.history = false;
ooChannel.queue = [];
ooChannel.ready = false;
rtChannel.getHistory(function () {
var lastCp = getLastCp();
loadCp(lastCp);
});
};
common.createButton('', true, {
name: 'history',
icon: 'fa-history',
text: Messages.historyText,
tippy: Messages.historyButton
}).click(function () {
ooChannel.historyLastHash = ooChannel.lastHash;
ooChannel.currentIndex = ooChannel.cpIndex;
//Feedback.send('OO_HISTORY'); // XXX pull Feedback from require
var histConfig = {
onPatch: onPatch,
onCheckpoint: onCheckpoint,
onRevert: commit,
setHistory: setHistoryMode,
onlyoffice: {
hashes: content.hashes || {},
channel: content.channel,
lastHash: ooChannel.lastHash
},
$toolbar: $('.cp-toolbar-container')
}; };
History.create(common, histConfig);
}).appendTo(toolbar.$drawer);
})();
common.createButton('', true, {
name: 'history',
icon: 'fa-history',
text: Messages.historyText,
tippy: Messages.historyButton
}).click(function () {
ooChannel.historyLastHash = ooChannel.lastHash;
ooChannel.currentIndex = ooChannel.cpIndex;
//Feedback.send('OO_HISTORY'); // XXX pull Feedback from require
var histConfig = {
onPatch: onPatch,
onCheckpoint: onCheckpoint,
onRevert: commit,
setHistory: setHistoryMode,
onlyoffice: {
hashes: content.hashes || {},
channel: content.channel,
lastHash: ooChannel.lastHash
},
$toolbar: $('.cp-toolbar-container')
};
History.create(common, histConfig);
}).appendTo(toolbar.$drawer);
})();
}
if (window.CP_DEV_MODE || DISPLAY_RESTORE_BUTTON) { if (window.CP_DEV_MODE || DISPLAY_RESTORE_BUTTON) {
common.createButton('', true, { common.createButton('', true, {
name: 'delete', name: 'delete',
@ -1978,6 +2027,11 @@ define([
sframeChan.event('EV_BURN_PAD', content.channel); sframeChan.event('EV_BURN_PAD', content.channel);
} }
if (privateData.ooVersionHash) {
return void openVersionHash(privateData.ooVersionHash);
}
var useNewDefault = content.version && content.version >= 2; var useNewDefault = content.version && content.version >= 2;
openRtChannel(function () { openRtChannel(function () {
setMyId(); setMyId();

@ -4,12 +4,13 @@ define([
'/api/config', '/api/config',
'/common/dom-ready.js', '/common/dom-ready.js',
'/common/requireconfig.js', '/common/requireconfig.js',
'/common/common-hash.js',
'/common/sframe-common-outer.js' '/common/sframe-common-outer.js'
], function (nThen, ApiConfig, DomReady, RequireConfig, SFCommonO) { ], function (nThen, ApiConfig, DomReady, RequireConfig, Hash, SFCommonO) {
var requireConfig = RequireConfig(); var requireConfig = RequireConfig();
// Loaded in load #2 // Loaded in load #2
var hash, href; var hash, href, version;
nThen(function (waitFor) { nThen(function (waitFor) {
DomReady.onReady(waitFor()); DomReady.onReady(waitFor());
}).nThen(function (waitFor) { }).nThen(function (waitFor) {
@ -27,6 +28,14 @@ define([
if (window.history && window.history.replaceState && hash) { if (window.history && window.history.replaceState && hash) {
window.history.replaceState({}, window.document.title, '#'); window.history.replaceState({}, window.document.title, '#');
} }
var parsed = Hash.parsePadUrl(href);
var opts = parsed.getOptions();
version = opts.versionHash;
opts.versionHash = "";
href = parsed.getUrl(opts);
hash = parsed.hashData.getHash(opts);
document.getElementById('sbox-iframe').setAttribute('src', document.getElementById('sbox-iframe').setAttribute('src',
ApiConfig.httpSafeOrigin + window.location.pathname + 'inner.html?' + ApiConfig.httpSafeOrigin + window.location.pathname + 'inner.html?' +
requireConfig.urlArgs + '#' + encodeURIComponent(JSON.stringify(req))); requireConfig.urlArgs + '#' + encodeURIComponent(JSON.stringify(req)));
@ -46,6 +55,7 @@ define([
}).nThen(function (/*waitFor*/) { }).nThen(function (/*waitFor*/) {
var addData = function (obj) { var addData = function (obj) {
obj.ooType = window.location.pathname.replace(/^\//, '').replace(/\/$/, ''); obj.ooType = window.location.pathname.replace(/^\//, '').replace(/\/$/, '');
obj.ooVersionHash = version;
obj.ooForceVersion = localStorage.CryptPad_ooVersion || sessionStorage.CryptPad_ooVersion || ""; obj.ooForceVersion = localStorage.CryptPad_ooVersion || sessionStorage.CryptPad_ooVersion || "";
}; };
var addRpc = function (sframeChan, Cryptpad, Utils) { var addRpc = function (sframeChan, Cryptpad, Utils) {

@ -2035,8 +2035,6 @@ define([
var lastKnownHash; var lastKnownHash;
var txid = Util.uid(); var txid = Util.uid();
console.error(data);
var onMsg = function (msg) { var onMsg = function (msg) {
if (completed) { return; } if (completed) { return; }
var parsed = parse(msg); var parsed = parse(msg);

Loading…
Cancel
Save