add optional named arguments to /run command
This commit is contained in:
parent
678a702d6e
commit
03b80900d6
|
@ -155,7 +155,7 @@ const init = async () => {
|
||||||
buttons.show();
|
buttons.show();
|
||||||
settings.onSave = ()=>buttons.refresh();
|
settings.onSave = ()=>buttons.refresh();
|
||||||
|
|
||||||
window['executeQuickReplyByName'] = async(name) => {
|
window['executeQuickReplyByName'] = async(name, args = {}) => {
|
||||||
let qr = [...settings.config.setList, ...(settings.chatConfig?.setList ?? [])]
|
let qr = [...settings.config.setList, ...(settings.chatConfig?.setList ?? [])]
|
||||||
.map(it=>it.set.qrList)
|
.map(it=>it.set.qrList)
|
||||||
.flat()
|
.flat()
|
||||||
|
@ -170,7 +170,7 @@ const init = async () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (qr && qr.onExecute) {
|
if (qr && qr.onExecute) {
|
||||||
return await qr.onExecute();
|
return await qr.execute(args);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -85,9 +85,7 @@ export class QuickReply {
|
||||||
this.showEditor();
|
this.showEditor();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (this.message?.length > 0 && this.onExecute) {
|
this.execute();
|
||||||
this.onExecute(this);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
const lbl = document.createElement('div'); {
|
const lbl = document.createElement('div'); {
|
||||||
this.domLabel = lbl;
|
this.domLabel = lbl;
|
||||||
|
@ -370,7 +368,7 @@ export class QuickReply {
|
||||||
document.querySelector('#shadow_popup').classList.add('qr--hide');
|
document.querySelector('#shadow_popup').classList.add('qr--hide');
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
executePromise = this.onExecute();
|
executePromise = this.execute();
|
||||||
await executePromise;
|
await executePromise;
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
executeErrors.textContent = ex.message;
|
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() {
|
toJSON() {
|
||||||
|
|
|
@ -101,19 +101,21 @@ export class QuickReplySet {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {QuickReply} qr
|
* @param {QuickReply} qr
|
||||||
|
* @param {String} [message] - optional altered message to be used
|
||||||
*/
|
*/
|
||||||
async execute(qr) {
|
async execute(qr, message = null) {
|
||||||
/**@type {HTMLTextAreaElement}*/
|
/**@type {HTMLTextAreaElement}*/
|
||||||
const ta = document.querySelector('#send_textarea');
|
const ta = document.querySelector('#send_textarea');
|
||||||
|
const finalMessage = message ?? qr.message;
|
||||||
let input = ta.value;
|
let input = ta.value;
|
||||||
if (this.injectInput && input.length > 0) {
|
if (this.injectInput && input.length > 0) {
|
||||||
if (this.placeBeforeInput) {
|
if (this.placeBeforeInput) {
|
||||||
input = `${qr.message} ${input}`;
|
input = `${finalMessage} ${input}`;
|
||||||
} else {
|
} else {
|
||||||
input = `${input} ${qr.message}`;
|
input = `${input} ${finalMessage}`;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
input = `${qr.message} `;
|
input = `${finalMessage} `;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input[0] == '/' && !this.disableSend) {
|
if (input[0] == '/' && !this.disableSend) {
|
||||||
|
@ -150,7 +152,7 @@ export class QuickReplySet {
|
||||||
}
|
}
|
||||||
|
|
||||||
hookQuickReply(qr) {
|
hookQuickReply(qr) {
|
||||||
qr.onExecute = ()=>this.execute(qr);
|
qr.onExecute = (_, message)=>this.execute(qr, message);
|
||||||
qr.onDelete = ()=>this.removeQuickReply(qr);
|
qr.onDelete = ()=>this.removeQuickReply(qr);
|
||||||
qr.onUpdate = ()=>this.save();
|
qr.onUpdate = ()=>this.save();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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('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('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('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('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('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);
|
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');
|
return messages.join('\n\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runCallback(_, name) {
|
async function runCallback(args, name) {
|
||||||
if (!name) {
|
if (!name) {
|
||||||
toastr.warning('No name provided for /run command');
|
toastr.warning('No name provided for /run command');
|
||||||
return '';
|
return '';
|
||||||
|
@ -458,7 +458,7 @@ async function runCallback(_, name) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
name = name.trim();
|
name = name.trim();
|
||||||
return await window['executeQuickReplyByName'](name);
|
return await window['executeQuickReplyByName'](name, args);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toastr.error(`Error running Quick Reply "${name}": ${error.message}`, 'Error');
|
toastr.error(`Error running Quick Reply "${name}": ${error.message}`, 'Error');
|
||||||
return '';
|
return '';
|
||||||
|
|
Loading…
Reference in New Issue