add optional named arguments to /run command

This commit is contained in:
LenAnderson
2023-12-27 12:28:15 +00:00
parent 678a702d6e
commit 03b80900d6
4 changed files with 24 additions and 14 deletions

View File

@ -85,9 +85,7 @@ export class QuickReply {
this.showEditor();
return;
}
if (this.message?.length > 0 && this.onExecute) {
this.onExecute(this);
}
this.execute();
});
const lbl = document.createElement('div'); {
this.domLabel = lbl;
@ -370,7 +368,7 @@ export class QuickReply {
document.querySelector('#shadow_popup').classList.add('qr--hide');
}
try {
executePromise = this.onExecute();
executePromise = this.execute();
await executePromise;
} catch (ex) {
executeErrors.textContent = ex.message;
@ -461,6 +459,16 @@ export class QuickReply {
}
async execute(args = {}) {
if (this.message?.length > 0 && this.onExecute) {
const message = this.message.replace(/\{\{arg::([^}]+)\}\}/g, (_, key) => {
return args[key] ?? '';
});
this.onExecute(this, message);
}
}
toJSON() {

View File

@ -101,19 +101,21 @@ export class QuickReplySet {
/**
* @param {QuickReply} qr
* @param {String} [message] - optional altered message to be used
*/
async execute(qr) {
async execute(qr, message = null) {
/**@type {HTMLTextAreaElement}*/
const ta = document.querySelector('#send_textarea');
const finalMessage = message ?? qr.message;
let input = ta.value;
if (this.injectInput && input.length > 0) {
if (this.placeBeforeInput) {
input = `${qr.message} ${input}`;
input = `${finalMessage} ${input}`;
} else {
input = `${input} ${qr.message}`;
input = `${input} ${finalMessage}`;
}
} else {
input = `${qr.message} `;
input = `${finalMessage} `;
}
if (input[0] == '/' && !this.disableSend) {
@ -150,7 +152,7 @@ export class QuickReplySet {
}
hookQuickReply(qr) {
qr.onExecute = ()=>this.execute(qr);
qr.onExecute = (_, message)=>this.execute(qr, message);
qr.onDelete = ()=>this.removeQuickReply(qr);
qr.onUpdate = ()=>this.save();
}