First version of OO save

This commit is contained in:
yflory 2024-10-03 16:59:55 +02:00
parent 66e5250e62
commit 8edf6001f2
4 changed files with 62 additions and 10 deletions

View File

@ -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}`;

View File

@ -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");
}

View File

@ -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}`;

View File

@ -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