From 278e08f8be6596e31090641ed54fe9586f64f0fa Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 1 Oct 2020 13:54:30 +0200 Subject: [PATCH] OnlyOffice snapshots --- www/common/onlyoffice/history.js | 51 +++++++++++++++++++++++---- www/common/onlyoffice/inner.js | 60 +++++++++++++++++++++++++++++++- www/common/onlyoffice/main.js | 7 ++++ 3 files changed, 111 insertions(+), 7 deletions(-) diff --git a/www/common/onlyoffice/history.js b/www/common/onlyoffice/history.js index 026477d1d..fa80c68cc 100644 --- a/www/common/onlyoffice/history.js +++ b/www/common/onlyoffice/history.js @@ -14,7 +14,7 @@ define([ var sframeChan = common.getSframeChannel(); History.readOnly = common.getMetadataMgr().getPrivateData().readOnly || !common.isLoggedIn(); - if (!config.onlyoffice || !config.setHistory || !config.onCheckpoint || !config.onPatch) { + if (!config.onlyoffice || !config.setHistory || !config.onCheckpoint || !config.onPatch || !config.makeSnapshot) { throw new Error("Missing config element"); } @@ -23,6 +23,7 @@ define([ var ooMessages = {}; var loading = false; var update = function () {}; + var currentTime; // Get an array of the checkpoint IDs sorted their patch index var hashes = config.onlyoffice.hashes; @@ -68,7 +69,7 @@ define([ var p = 100*((msgIndex+1) / (msgs.length)); $pos.css('margin-left', p+'%'); - var time = msgs[msgIndex] && msgs[msgIndex].time; + var time = currentTime = msgs[msgIndex] && msgs[msgIndex].time; if (time) { $time.text(new Date(time).toLocaleString()); } else { $time.text(''); } }; @@ -221,13 +222,11 @@ define([ ]) ]) ]); - /* var snapshot = h('button', { title: Messages.snapshots_new, }, [ h('i.fa.fa-camera') ]); - */ var share = h('button', { title: Messages.history_shareTitle }, [ h('i.fa.fa-shhare-alt'), h('span', Messages.shareButton) @@ -244,7 +243,7 @@ define([ ]); var actions = h('div.cp-toolbar-history-actions', [ h('span.cp-history-actions-first', [ - //snapshot, + snapshot, share ]), h('span.cp-history-actions-last', [ @@ -254,7 +253,7 @@ define([ ]); if (History.readOnly) { - //snapshot.disabled = true; + snapshot.disabled = true; restore.disabled = true; } @@ -312,6 +311,46 @@ define([ versionHash: getVersion() }); }); + Messages.snapshots_ooPickVersion = "You must select a version before creating a snapshot"; // XXX + $(snapshot).click(function () { + if (cpIndex === -1 && msgIndex === -1) { return void UI.warn(Messages.snapshots_ooPickVersion); } + var input = h('input', { + placeholder: Messages.snapshots_placeholder + }); + var $input = $(input); + var content = h('div', [ + h('h5', Messages.snapshots_new), + input + ]); + + var buttons = [{ + className: 'cancel', + name: Messages.filePicker_close, + onClick: function () {}, + keys: [27], + }, { + className: 'primary', + iconClass: '.fa.fa-camera', + name: Messages.snapshots_new, + onClick: function () { + var val = $input.val(); + if (!val) { return true; } + config.makeSnapshot(val, function (err) { + if (err) { return; } + UI.log(Messages.saved); + }, { + hash: getVersion(), + time: currentTime || 0 + }); + }, + keys: [13], + }]; + + UI.openCustomModal(UI.dialog.customModal(content, {buttons: buttons })); + setTimeout(function () { + $input.focus(); + }); + }); // Close & restore buttons $(close).click(function () { diff --git a/www/common/onlyoffice/inner.js b/www/common/onlyoffice/inner.js index 24a57c1c0..ed17c1a39 100644 --- a/www/common/onlyoffice/inner.js +++ b/www/common/onlyoffice/inner.js @@ -641,10 +641,21 @@ define([ 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) { + var messages = (data.messages || []).slice(1, minor); + messages.forEach(function (obj) { try { obj.msg = JSON.parse(obj.msg); } catch (e) { console.error(e); } }); + if (!privateData.embed) { + Messages.infobar_versionHash = "You're currently viewing an old version of this document ({0})."; // XXX (duplicate from history branch) + var vTime = (messages[messages.length - 1] || {}).time; + var vTimeStr = vTime ? new Date(vTime).toLocaleString() + : 'v' + privateData.ooVersionHash; + var vTxt = Messages._getKey('infobar_versionHash',  [vTimeStr]); + var vHashEl = h('div.alert.alert-warning.cp-burn-after-reading', vTxt); + $('#cp-app-oo-editor').prepend(vHashEl); + } + loadLastDocument(cp, function () { var file = getFileType(); var type = common.getMetadataMgr().getPrivateData().ooType; @@ -1814,6 +1825,7 @@ define([ $save.appendTo(toolbar.$bottomM); } + if (!privateData.ooVersionHash) { (function () { /* add a history button */ var commit = function () { @@ -1856,6 +1868,42 @@ define([ }); }; + var deleteSnapshot = function (hash) { + var md = Util.clone(cpNfInner.metadataMgr.getMetadata()); + var snapshots = md.snapshots = md.snapshots || {}; + delete snapshots[hash]; + metadataMgr.updateMetadata(md); + APP.onLocal(); + }; + var makeSnapshot = function (title, cb, obj) { + var hash, time; + if (obj && obj.hash && obj.time) { + hash = obj.hash; + time = obj.time + } else { + var major = Object.keys(content.hashes).length; + var cpIndex = getLastCp().index || 0; + var minor = ooChannel.cpIndex - cpIndex; + hash = major+'.'+minor; + time = +new Date(); + } + var md = Util.clone(metadataMgr.getMetadata()); + var snapshots = md.snapshots = md.snapshots || {}; + if (snapshots[hash]) { cb('EEXISTS'); return void UI.warn(Messages.error); } // XXX + snapshots[hash] = { + title: title, + time: time + }; + metadataMgr.updateMetadata(md); + APP.onLocal(); + APP.realtime.onSettle(cb); + }; + var loadSnapshot = function (hash) { + sframeChan.event('EV_OO_OPENVERSION', { + hash: hash + }); + }; + common.createButton('', true, { name: 'history', icon: 'fa-history', @@ -1870,6 +1918,7 @@ define([ onCheckpoint: onCheckpoint, onRevert: commit, setHistory: setHistoryMode, + makeSnapshot: makeSnapshot, onlyoffice: { hashes: content.hashes || {}, channel: content.channel, @@ -1879,7 +1928,16 @@ define([ }; History.create(common, histConfig); }).appendTo(toolbar.$drawer); + + // Snapshots + var $snapshot = common.createButton('snapshots', true, { + remove: deleteSnapshot, + make: makeSnapshot, + load: loadSnapshot + }); + toolbar.$drawer.append($snapshot); })(); + } if (window.CP_DEV_MODE || DISPLAY_RESTORE_BUTTON) { common.createButton('', true, { diff --git a/www/common/onlyoffice/main.js b/www/common/onlyoffice/main.js index 5fcf251ea..157ddf230 100644 --- a/www/common/onlyoffice/main.js +++ b/www/common/onlyoffice/main.js @@ -147,6 +147,13 @@ define([ } Cryptpad.onlyoffice.execCommand(obj, cb); }); + sframeChan.on('EV_OO_OPENVERSION', function (obj, cb) { + if (!obj || !obj.hash) { return; } + var parsed = Hash.parsePadUrl(window.location.href); + var opts = parsed.getOptions(); + opts.versionHash = obj.hash; + window.open(parsed.getUrl(opts)); + }); Cryptpad.onlyoffice.onEvent.reg(function (obj) { if (obj.ev === 'MESSAGE' && !/^cp\|/.test(obj.data)) { try {