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

@ -155,7 +155,7 @@ const init = async () => {
buttons.show();
settings.onSave = ()=>buttons.refresh();
window['executeQuickReplyByName'] = async(name) => {
window['executeQuickReplyByName'] = async(name, args = {}) => {
let qr = [...settings.config.setList, ...(settings.chatConfig?.setList ?? [])]
.map(it=>it.set.qrList)
.flat()
@ -170,7 +170,7 @@ const init = async () => {
}
}
if (qr && qr.onExecute) {
return await qr.onExecute();
return await qr.execute(args);
}
};

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();
}

View File

@ -175,7 +175,7 @@ parser.addCommand('fuzzy', fuzzyCallback, [], 'list=["a","b","c"] (search value)
parser.addCommand('pass', (_, arg) => arg, ['return'], '<span class="monospace">(text)</span> passes the text to the next command through the pipe.', true, true);
parser.addCommand('delay', delayCallback, ['wait', 'sleep'], '<span class="monospace">(milliseconds)</span> delays the next command in the pipe by the specified number of milliseconds.', true, true);
parser.addCommand('input', inputCallback, ['prompt'], '<span class="monospace">(default="string" large=on/off wide=on/off okButton="string" rows=number [text])</span> Shows a popup with the provided text and an input field. The default argument is the default value of the input field, and the text argument is the text to display.', true, true);
parser.addCommand('run', runCallback, ['call', 'exec'], '<span class="monospace">(QR label)</span> runs a Quick Reply with the specified name from the current preset.', true, true);
parser.addCommand('run', runCallback, ['call', 'exec'], '<span class="monospace">[key1=value key2=value ...] ([qrSet.]qrLabel)</span> runs a Quick Reply with the specified name from a currently active preset or from another preset, named arguments can be referenced in a QR with {{arg::key}}.', true, true);
parser.addCommand('messages', getMessagesCallback, ['message'], '<span class="monospace">(names=off/on [message index or range])</span> returns the specified message or range of messages as a string.', true, true);
parser.addCommand('setinput', setInputCallback, [], '<span class="monospace">(text)</span> sets the user input to the specified text and passes it to the next command through the pipe.', true, true);
parser.addCommand('popup', popupCallback, [], '<span class="monospace">(large=on/off wide=on/off okButton="string" text)</span> shows a blocking popup with the specified text and buttons. Returns the input value into the pipe or empty string if canceled.', true, true);
@ -445,7 +445,7 @@ function getMessagesCallback(args, value) {
return messages.join('\n\n');
}
async function runCallback(_, name) {
async function runCallback(args, name) {
if (!name) {
toastr.warning('No name provided for /run command');
return '';
@ -458,7 +458,7 @@ async function runCallback(_, name) {
try {
name = name.trim();
return await window['executeQuickReplyByName'](name);
return await window['executeQuickReplyByName'](name, args);
} catch (error) {
toastr.error(`Error running Quick Reply "${name}": ${error.message}`, 'Error');
return '';