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

View File

@ -375,6 +375,32 @@ define([
}; };
var onUploaded = function (ev, data, err) { 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; content.saveLock = undefined;
if (err) { if (err) {
console.error(err); console.error(err);
@ -2334,10 +2360,11 @@ Uncaught TypeError: Cannot read property 'calculatedType' of null
if (editor) { if (editor) {
try { getEditor().asc_setRestriction(true); } catch (e) {} try { getEditor().asc_setRestriction(true); } catch (e) {}
} }
var content = parsed.content; var _content = parsed.content;
// Get checkpoint // Get checkpoint
var hashes = content.hashes || {}; var hashes = _content.hashes || {};
var medias = _content.mediasSources;
var idx = sortCpIndex(hashes); var idx = sortCpIndex(hashes);
var lastIndex = idx[idx.length - 1]; var lastIndex = idx[idx.length - 1];
var lastCp = hashes[lastIndex] || {}; var lastCp = hashes[lastIndex] || {};
@ -2347,10 +2374,12 @@ Uncaught TypeError: Cannot read property 'calculatedType' of null
// Last hash // Last hash
var fromHash = 'NONE'; var fromHash = 'NONE';
content.mediasSources = medias;
sframeChan.query('Q_GET_HISTORY_RANGE', { sframeChan.query('Q_GET_HISTORY_RANGE', {
href: href, href: href,
password: pw, password: pw,
channel: content.channel, channel: _content.channel,
lastKnownHash: fromHash, lastKnownHash: fromHash,
toHash: toHash, toHash: toHash,
}, function (err, data) { }, function (err, data) {
@ -2615,6 +2644,29 @@ Uncaught TypeError: Cannot read property 'calculatedType' of null
if ($template && typeof($template.appendTo) === 'function') { if ($template && typeof($template.appendTo) === 'function') {
$template.appendTo(toolbar.$drawer); $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) { if (data.template) {
// Start OO with a template... // Start OO with a template...
// Cryptget and give href, password and content to inner // Cryptget and give href, password and content to inner
if (parsed.type === "sheet") { if (isOO) {
var then = function () { var then = function () {
startRealtime(rtConfig); startRealtime(rtConfig);
cb(); cb();

View File

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