Optionally make the prompt plugin pre-fill the last entered value

Added an option to the prompt plugin to save the last entered value and have that set as default value the next time the prompt shows. This saves you from having to enter all details for each request while still being able to change them if needed. This should be BC and therefore the option is ignored when a storage key is set explicitly; this will still return the cached value directly.
This commit is contained in:
Maarten de Boer 2019-07-09 20:11:40 +02:00
parent 8d9352aaec
commit 8fb35d0d76

View File

@ -1,23 +1,5 @@
const crypto = require('crypto');
module.exports.responseHooks = [
async context => {
const items = await context.store.all();
const requestId = context.request.getId();
const toRemove = items.filter(v => {
if (v.key && typeof v.key === 'string') {
return v.key.indexOf(requestId) === 0;
}
return false;
});
// Delete cached values we prompt again on the next request
for (const { key } of toRemove) {
await context.store.removeItem(key);
}
},
];
module.exports.templateTags = [
{
displayName: 'Prompt',
@ -57,6 +39,15 @@ module.exports.templateTags = [
help: 'If this is enabled, the value when input will be masked like a password field.',
defaultValue: false,
},
{
displayName: 'Save Last Value',
type: 'boolean',
help:
'If this is enabled, the value will be stored in memory and the next time this prompt' +
'shows, the input field will be pre-filled with this value. This option is ignored ' +
'when the storage key is set.',
defaultValue: true
}
],
async run(context, title, label, defaultValue, explicitStorageKey, maskText) {
if (!title) {
@ -74,11 +65,18 @@ module.exports.templateTags = [
const storageKey = explicitStorageKey || `${context.meta.requestId}.${titleHash}`;
const cachedValue = await context.store.getItem(storageKey);
if (cachedValue) {
// Directly return cached value if using explicitly defined storage key
if (explicitStorageKey && cachedValue) {
console.log(`[prompt] Used cached value under ${storageKey}`);
return cachedValue;
}
// Use cached value as default value
if (cachedValue) {
defaultValue = cachedValue;
console.log(`[prompt] Used cached value under ${storageKey}`);
}
// Only prompt when we're actually sending
if (context.renderPurpose !== 'send') {
if (cachedValue !== null) {