From 8edf6001f2dcc2a5b31dca56ed1d83d3757b22e0 Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 3 Oct 2024 16:59:55 +0200 Subject: [PATCH] First version of OO save --- www/common/onlyoffice/inner.js | 12 ++++++ www/common/sframe-common-integration.js | 2 +- www/cryptpad-api.js | 9 ++++- www/integration/main.js | 49 +++++++++++++++++++++---- 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/www/common/onlyoffice/inner.js b/www/common/onlyoffice/inner.js index a29c10c6b..4e878005b 100644 --- a/www/common/onlyoffice/inner.js +++ b/www/common/onlyoffice/inner.js @@ -3230,11 +3230,23 @@ Uncaught TypeError: Cannot read property 'calculatedType' of null }); } integrationChannel.on('Q_INTEGRATION_NEEDSAVE', function (data, cb) { + if (!cfg.autosave) { return; } integrationSave(function (obj) { if (obj && obj.error) { console.error(obj.error); } cb(); }); }); + + if (!cfg.autosave) { + let $save = common.createButton('save', true, {}, function () { + $save.attr('disabled', 'disabled'); + integrationSave(err => { + $save.removeAttr('disabled'); + }); + }); + $('body').prepend($save); + } + if (privateData.initialState && (!content || !content.hashes || !Object.keys(content.hashes).length)) { var blob = privateData.initialState; let title = `document.${cfg.fileType}`; diff --git a/www/common/sframe-common-integration.js b/www/common/sframe-common-integration.js index 8b1ab5374..41d81a7b9 100644 --- a/www/common/sframe-common-integration.js +++ b/www/common/sframe-common-integration.js @@ -17,7 +17,7 @@ define([ var privateData = metadataMgr.getPrivateData(); var config = privateData.integrationConfig; - if (!config.autosave) { return; } + if (!config.autosave) { return void exp; } if (typeof(saveHandler) !== "function") { throw new Error("Incorrect save handler"); } diff --git a/www/cryptpad-api.js b/www/cryptpad-api.js index 9e873cba3..efe065618 100644 --- a/www/cryptpad-api.js +++ b/www/cryptpad-api.js @@ -88,6 +88,7 @@ var start = function (config, chan) { return new Promise(function (resolve, reject) { setTimeout(function () { + var docID = config.document.key; var key = config.document.key; var blob; @@ -116,10 +117,12 @@ chan.send('START', { key: key, application: config.documentType, + name: config.document.title, url: config.document.url, + documentKey: docID, document: blob, ext: config.document.fileType, - autosave: config.autosave || 10, + autosave: config.events.onSave && (config.autosave || 10), editorConfig: config.editorConfig || {} }, function (obj) { if (obj && obj.error) { reject(obj.error); return console.error(obj.error); } @@ -134,9 +137,11 @@ blob = config.document.blob; return start(); } - return start(); // XXX use server only when not zero knowledge? i.e. no save handler? // XXX or when error with client? + if (!config.events.onSave) { + return start(); + } getBlob(function (err, _blob) { if (err) { reject(err); return console.error(err); } _blob.name = `document.${config.document.fileType}`; diff --git a/www/integration/main.js b/www/integration/main.js index 1cbf1f241..cffd22a33 100644 --- a/www/integration/main.js +++ b/www/integration/main.js @@ -129,12 +129,6 @@ define([ }); }); - var save = function (obj, cb) { - chan.send('SAVE', obj.blob, function (err) { - if (err) { return cb({error: err}); } - cb(); - }); - }; var reload = function (data) { chan.send('RELOAD', data); }; @@ -171,7 +165,6 @@ define([ let xhr = new XMLHttpRequest(); let data = encodeURIComponent(documentURL); let url = getInstanceURL() + '/ooapidl?url=' + data; - console.log(url); xhr.open('GET', url, true); xhr.responseType = 'blob'; //xhr.setRequestHeader('Content-Type', 'application/json'); @@ -189,6 +182,30 @@ define([ }; xhr.send(); }; + let saveBlobServer = function (cfg, blob, cb) { + let {callbackUrl, name, key} = cfg; + let xhr = new XMLHttpRequest(); + name = encodeURIComponent(name); + callbackUrl = encodeURIComponent(callbackUrl); + key = encodeURIComponent(key); + let query = `name=${name}&cb=${callbackUrl}&key=${key}` + let url = getInstanceURL() + `/oosave?${query}`; + xhr.open('POST', url, true); + xhr.responseType = 'blob'; + //xhr.setRequestHeader('Content-Type', 'application/json'); + xhr.onload = function () { + console.error(this.status); + if (this.status === 200) { + cb(); + } else { + cb(this.status); + } + }; + xhr.onerror = function (e) { + cb(e.message); + }; + xhr.send(blob); + }; chan.on('START', function (data, cb) { console.warn('INNER START', data); // data.key is a hash @@ -198,6 +215,23 @@ define([ localStorage.setItem(LS_LANG, data.editorConfig.lang); } + let fileName = data.name || `document.${data.ext}`; + var save = function (obj, cb) { + let cbUrl = data.editorConfig.callbackUrl; + if (!data.autosave && cbUrl) { + saveBlobServer({ + callbackUrl: cbUrl, + name: fileName, + key: data.documentKey + }, obj.blob, cb); + return; + } + chan.send('SAVE', obj.blob, function (err) { + if (err) { return cb({error: err}); } + cb(); + }); + }; + console.error(Hash.hrefToHexChannelId(href)); let startApp = function (blob) { window.CP_integration_outer = { @@ -206,6 +240,7 @@ define([ href: href, initialState: blob, config: { + fileName: data.name, fileType: data.ext, autosave: data.autosave, user: data.editorConfig.user