Add 'save as template' button in file menu for OO apps

This commit is contained in:
yflory 2021-10-15 14:58:41 +02:00
parent 60ce5a1399
commit 75a8d1acba
4 changed files with 61 additions and 6 deletions

View File

@ -653,12 +653,15 @@ define([
button = $('<button>', {
'class': 'cptools cptools-new-template cp-toolbar-icon-template',
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.saveTemplateButton));
if (data.rt) {
if (data.rt || data.callback) {
button
.click(function () {
var title = data.getTitle() || document.title;
var todo = function (val) {
if (typeof(val) !== "string") { return; }
if (data.callback) {
return void data.callback(val);
}
var toSave = data.rt.getUserDoc();
if (val.trim()) {
val = val.trim();

View File

@ -375,6 +375,32 @@ define([
};
var onUploaded = function (ev, data, err) {
if (ev.newTemplate) {
if (err) {
console.error(err);
return void UI.warn(Messages.error);
}
var _content = ev.newTemplate;
_content.hashes = {};
_content.hashes[1] = {
file: data.url,
index: 0,
version: NEW_VERSION
};
_content.version = NEW_VERSION;
_content.channel = Hash.createChannelId();
sframeChan.query('Q_SAVE_AS_TEMPLATE', {
toSave: JSON.stringify({
content: _content
}),
title: ev.title
}, function () {
UI.alert(Messages.templateSaved);
Feedback.send('OO_TEMPLATE_CREATED');
});
return;
}
content.saveLock = undefined;
if (err) {
console.error(err);
@ -2334,10 +2360,11 @@ Uncaught TypeError: Cannot read property 'calculatedType' of null
if (editor) {
try { getEditor().asc_setRestriction(true); } catch (e) {}
}
var content = parsed.content;
var _content = parsed.content;
// Get checkpoint
var hashes = content.hashes || {};
var hashes = _content.hashes || {};
var medias = _content.mediasSources;
var idx = sortCpIndex(hashes);
var lastIndex = idx[idx.length - 1];
var lastCp = hashes[lastIndex] || {};
@ -2347,10 +2374,12 @@ Uncaught TypeError: Cannot read property 'calculatedType' of null
// Last hash
var fromHash = 'NONE';
content.mediasSources = medias;
sframeChan.query('Q_GET_HISTORY_RANGE', {
href: href,
password: pw,
channel: content.channel,
channel: _content.channel,
lastKnownHash: fromHash,
toHash: toHash,
}, function (err, data) {
@ -2615,6 +2644,29 @@ Uncaught TypeError: Cannot read property 'calculatedType' of null
if ($template && typeof($template.appendTo) === 'function') {
$template.appendTo(toolbar.$drawer);
}
// Save as template
if (!metadataMgr.getPrivateData().isTemplate) {
var templateObj = {
//rt: cpNfInner.chainpad,
getTitle: function () { return cpNfInner.metadataMgr.getMetadata().title; },
callback: function (title) {
var newContent = {};
newContent.mediasSources = content.mediasSources;
var text = getContent();
var blob = new Blob([text], {type: 'plain/text'});
var file = getFileType();
blob.name = title || (metadataMgr.getMetadataLazy().title || file.doc) + '.' + file.type;
var data = {
newTemplate: newContent,
title: title
};
APP.FM.handleFile(blob, data);
}
};
var $templateButton = common.createButton('template', true, templateObj);
toolbar.$drawer.append($templateButton);
}
})();
}

View File

@ -2016,7 +2016,7 @@ define([
if (data.template) {
// Start OO with a template...
// Cryptget and give href, password and content to inner
if (parsed.type === "sheet") {
if (isOO) {
var then = function () {
startRealtime(rtConfig);
cb();

View File

@ -728,7 +728,7 @@ define([
displayDoc(hjson);
}
metadataMgr.updateTitle('');
//metadataMgr.updateTitle('');
initializing = false;
$('#cp-app-debug-history').show();