Save and use templates in pad, code and slides

This commit is contained in:
yflory 2017-04-07 18:20:03 +02:00
parent a13261abaa
commit 84d8615cf1
8 changed files with 145 additions and 6 deletions

View File

@ -75,6 +75,11 @@ define(function () {
out.newButton = 'Nouveau';
out.newButtonTitle = 'Créer un nouveau pad';
out.saveTemplateButton = "Sauver en tant que modèle";
out.saveTemplatePrompt = "Choisir un titre pour ce modèle";
out.templateSaved = "Modèle enregistré !";
out.selectTemplate = "Sélectionner un modèle ou appuyer sur Échap";
out.presentButtonTitle = "Entrer en mode présentation";
out.presentSuccess = 'Appuyer sur Échap pour quitter le mode présentation';

View File

@ -77,6 +77,11 @@ define(function () {
out.newButton = 'New';
out.newButtonTitle = 'Create a new pad';
out.saveTemplateButton = "Save as template";
out.saveTemplatePrompt = "Choose a title for the template";
out.templateSaved = "Template saved!";
out.selectTemplate = "Select a template or press escape";
out.presentButtonTitle = "Enter presentation mode";
out.presentSuccess = 'Hit ESC to exit presentation mode';

View File

@ -7,13 +7,14 @@ define([
'json.sortify',
'/bower_components/chainpad-json-validator/json-ot.js',
'/common/cryptpad-common.js',
'/common/cryptget.js',
'/common/modes.js',
'/common/themes.js',
'/common/visible.js',
'/common/notify.js',
'/bower_components/file-saver/FileSaver.min.js',
'/bower_components/jquery/dist/jquery.min.js',
], function (Crypto, Realtime, TextPatcher, Toolbar, JSONSortify, JsonOT, Cryptpad, Modes, Themes, Visible, Notify) {
], function (Crypto, Realtime, TextPatcher, Toolbar, JSONSortify, JsonOT, Cryptpad, Cryptget, Modes, Themes, Visible, Notify) {
var $ = window.jQuery;
var saveAs = window.saveAs;
var Messages = Cryptpad.Messages;
@ -401,6 +402,17 @@ define([
editHash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys);
}
/* save as template */
if (!Cryptpad.isTemplate(window.location.href)) {
var templateObj = {
rt: info.realtime,
Crypt: Cryptget,
getTitle: function () { return document.title; }
};
var $templateButton = Cryptpad.createButton('template', true, templateObj);
$rightside.append($templateButton);
}
/* add an export button */
var $export = Cryptpad.createButton('export', true, {}, exportText);
$rightside.append($export);
@ -532,6 +544,8 @@ define([
var userDoc = module.realtime.getUserDoc();
var isNew = false;
if (userDoc === "" || userDoc === "{}") { isNew = true; }
var newDoc = "";
if(userDoc !== "") {
@ -599,6 +613,9 @@ define([
onLocal();
module.$userNameButton.click();
}
if (isNew) {
Cryptpad.selectTemplate('code', info.realtime, Cryptget);
}
});
};

View File

@ -505,7 +505,7 @@ define([
};
var makePad = function (href, title) {
var now = ''+new Date();
var now = +new Date();
return {
href: href,
atime: now,
@ -564,6 +564,37 @@ define([
getStore().addTemplate(href);
};
var isTemplate = common.isTemplate = function (href) {
var rhref = getRelativeHref(href);
var templates = listTemplates();
return templates.some(function (t) {
return t.href === rhref;
});
};
var selectTemplate = common.selectTemplate = function (type, rt, Crypt) {
var $content = $('<div>');
$('<b>').text(Messages.selectTemplate).appendTo($content);
$('<p>', {id:"selectTemplate"}).appendTo($content);
Cryptpad.alert($content.html(), null, true);
var $p = $('#selectTemplate');
listTemplates(type).forEach(function (t) {
$('<a>', {href: t.href, title: t.title}).text(t.title).click(function (e) {
e.preventDefault();
var parsed = parsePadUrl(t.href);
if(!parsed) { throw new Error("Cannot get template hash"); }
common.addLoadingScreen(null, true);
Crypt.get(parsed.hash, function (err, val) {
if (err) { throw new Error(err); }
var p = parsePadUrl(window.location.href);
Crypt.put(p.hash, val, function (e) {
common.findOKButton().click();
common.removeLoadingScreen();
});
});
}).appendTo($p);
});
common.findOKButton().text(Messages.cancelButton);
};
// STORAGE
/* fetch and migrate your pad history from localStorage */
@ -1069,6 +1100,53 @@ define([
}));
}
break;
case 'template':
button = $('<button>', {
title: Messages.saveTemplateButton,
}).append($('<span>', {'class':'fa fa-bookmark', style: 'font:'+size+' FontAwesome'}));
if (data.rt && data.Crypt) {
button.click(function () {
var title = data.getTitle() || document.title;
var todo = function (val) {
if (typeof(val) !== "string") { return; }
var toSave = data.rt.getUserDoc();
if (val.trim()) {
val = val.trim();
title = val;
try {
var parsed = JSON.parse(toSave);
var meta;
if (Array.isArray(parsed) && typeof(parsed[3]) === "object") {
meta = parsed[3].metadata;
} else {
meta = parsed.metadata;
}
if (typeof(meta) === "object") {
meta.title = val;
meta.defaultTitle = val;
delete meta.users;
}
toSave = JSON.stringify(parsed);
} catch(e) {
console.error("Parse error while setting the title", e);
}
}
var p = parsePadUrl(window.location.href);
if (!p.type) { return; }
var hash = createRandomHash();
var href = '/' + p.type + '/#' + hash;
data.Crypt.put(hash, toSave, function (e) {
if (e) { throw new Error(e); }
common.addTemplate(makePad(href, title));
whenRealtimeSyncs(getStore().getProxy().info.realtime, function () {
common.alert(Messages.templateSaved);
});
});
};
common.prompt(Messages.saveTemplatePrompt, title || document.title, todo);
});
}
break;
case 'forget':
button = $('<button>', {
id: 'cryptpad-forget',

View File

@ -999,7 +999,10 @@ define([
// first, we must add it to FILES_DATA, so the input has to be an fileDAta object
var addTemplate = exp.addTemplate = function (fileData) {
if (workgroup) { return; }
if (typeof fileData !== "object" || !fileData.href || !fileData.title) { return; }
if (typeof fileData !== "object" || !fileData.href || !fileData.title) {
console.error("filedata object expected to add a new template");
return;
}
var href = fileData.href;
var test = files[FILES_DATA].some(function (o) {

View File

@ -219,7 +219,7 @@ define([
// Categories dislayed in the menu
// _WORKGROUP_ : do not display unsorted
var displayedCategories = [ROOT, UNSORTED, TRASH, SEARCH];
var displayedCategories = [ROOT, UNSORTED, TRASH, SEARCH, TEMPLATE];
if (isWorkgroup()) { displayedCategories = [ROOT, TRASH, SEARCH]; }
var lastSelectTime;

View File

@ -10,6 +10,7 @@ define([
'json.sortify',
'/bower_components/textpatcher/TextPatcher.js',
'/common/cryptpad-common.js',
'/common/cryptget.js',
'/common/visible.js',
'/common/notify.js',
'/pad/links.js',
@ -17,7 +18,7 @@ define([
'/bower_components/diff-dom/diffDOM.js',
'/bower_components/jquery/dist/jquery.min.js',
], function (Crypto, realtimeInput, Hyperjson,
Toolbar, Cursor, JsonOT, TypingTest, JSONSortify, TextPatcher, Cryptpad,
Toolbar, Cursor, JsonOT, TypingTest, JSONSortify, TextPatcher, Cryptpad, Cryptget,
Visible, Notify, Links) {
var $ = window.jQuery;
var saveAs = window.saveAs;
@ -616,6 +617,17 @@ define([
$rightside.append($collapse);
}
/* save as template */
if (!Cryptpad.isTemplate(window.location.href)) {
var templateObj = {
rt: info.realtime,
Crypt: Cryptget,
getTitle: function () { return document.title; }
};
var $templateButton = Cryptpad.createButton('template', true, templateObj);
$rightside.append($templateButton);
}
/* add an export button */
var $export = Cryptpad.createButton('export', true, {}, exportFile);
$rightside.append($export);
@ -723,6 +735,7 @@ define([
editor.focus();
if (newPad) {
Cryptpad.selectTemplate('pad', info.realtime, Cryptget);
cursor.setToEnd();
} else {
cursor.setToStart();

View File

@ -7,6 +7,7 @@ define([
'json.sortify',
'/bower_components/chainpad-json-validator/json-ot.js',
'/common/cryptpad-common.js',
'/common/cryptget.js',
'/common/modes.js',
'/common/themes.js',
'/common/visible.js',
@ -14,7 +15,7 @@ define([
'/slide/slide.js',
'/bower_components/file-saver/FileSaver.min.js',
'/bower_components/jquery/dist/jquery.min.js',
], function (Crypto, Realtime, TextPatcher, Toolbar, JSONSortify, JsonOT, Cryptpad, Modes, Themes, Visible, Notify, Slide) {
], function (Crypto, Realtime, TextPatcher, Toolbar, JSONSortify, JsonOT, Cryptpad, Cryptget, Modes, Themes, Visible, Notify, Slide) {
var $ = window.jQuery;
var saveAs = window.saveAs;
@ -532,6 +533,17 @@ define([
editHash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys);
}
/* save as template */
if (!Cryptpad.isTemplate(window.location.href)) {
var templateObj = {
rt: info.realtime,
Crypt: Cryptget,
getTitle: function () { return document.title; }
};
var $templateButton = Cryptpad.createButton('template', true, templateObj);
$rightside.append($templateButton);
}
/* add an export button */
var $export = Cryptpad.createButton('export', true, {}, exportText);
$rightside.append($export);
@ -720,6 +732,9 @@ define([
var userDoc = module.realtime.getUserDoc();
var isNew = false;
if (userDoc === "" || userDoc === "{}") { isNew = true; }
var newDoc = "";
if(userDoc !== "") {
var hjson = JSON.parse(userDoc);
@ -792,6 +807,9 @@ define([
onLocal();
module.$userNameButton.click();
}
if (isNew) {
Cryptpad.selectTemplate('slide', info.realtime, Cryptget);
}
});
};