2024-01-07 00:11:30 +00:00
|
|
|
import { getRequestHeaders, substituteParams } from '../../../../script.js';
|
2024-05-12 15:15:05 -04:00
|
|
|
import { executeSlashCommands, executeSlashCommandsOnChatInput, executeSlashCommandsWithOptions } from '../../../slash-commands.js';
|
2024-06-18 14:29:29 -04:00
|
|
|
import { SlashCommandParser } from '../../../slash-commands/SlashCommandParser.js';
|
2024-05-12 15:15:05 -04:00
|
|
|
import { SlashCommandScope } from '../../../slash-commands/SlashCommandScope.js';
|
2023-12-22 12:56:06 +00:00
|
|
|
import { debounceAsync, warn } from '../index.js';
|
2023-12-20 13:40:44 +00:00
|
|
|
import { QuickReply } from './QuickReply.js';
|
|
|
|
|
|
|
|
export class QuickReplySet {
|
|
|
|
/**@type {QuickReplySet[]}*/ static list = [];
|
|
|
|
|
|
|
|
|
|
|
|
static from(props) {
|
2023-12-20 21:44:55 +00:00
|
|
|
props.qrList = []; //props.qrList?.map(it=>QuickReply.from(it));
|
2023-12-20 13:40:44 +00:00
|
|
|
const instance = Object.assign(new this(), props);
|
2023-12-20 21:44:55 +00:00
|
|
|
// instance.init();
|
2023-12-20 13:40:44 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {String} name - name of the QuickReplySet
|
|
|
|
*/
|
|
|
|
static get(name) {
|
|
|
|
return this.list.find(it=>it.name == name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**@type {String}*/ name;
|
|
|
|
/**@type {Boolean}*/ disableSend = false;
|
|
|
|
/**@type {Boolean}*/ placeBeforeInput = false;
|
|
|
|
/**@type {Boolean}*/ injectInput = false;
|
|
|
|
/**@type {QuickReply[]}*/ qrList = [];
|
|
|
|
|
|
|
|
/**@type {Number}*/ idIndex = 0;
|
|
|
|
|
2023-12-20 17:56:08 +00:00
|
|
|
/**@type {Boolean}*/ isDeleted = false;
|
2023-12-20 13:40:44 +00:00
|
|
|
|
2023-12-20 17:56:08 +00:00
|
|
|
/**@type {Function}*/ save;
|
2023-12-20 13:40:44 +00:00
|
|
|
|
|
|
|
/**@type {HTMLElement}*/ dom;
|
|
|
|
/**@type {HTMLElement}*/ settingsDom;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor() {
|
2023-12-22 12:56:06 +00:00
|
|
|
this.save = debounceAsync(()=>this.performSave(), 200);
|
2023-12-20 13:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this.qrList.forEach(qr=>this.hookQuickReply(qr));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unrender() {
|
|
|
|
this.dom?.remove();
|
|
|
|
this.dom = null;
|
|
|
|
}
|
|
|
|
render() {
|
|
|
|
this.unrender();
|
|
|
|
if (!this.dom) {
|
|
|
|
const root = document.createElement('div'); {
|
|
|
|
this.dom = root;
|
|
|
|
root.classList.add('qr--buttons');
|
|
|
|
this.qrList.filter(qr=>!qr.isHidden).forEach(qr=>{
|
|
|
|
root.append(qr.render());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.dom;
|
|
|
|
}
|
|
|
|
rerender() {
|
|
|
|
if (!this.dom) return;
|
|
|
|
this.dom.innerHTML = '';
|
|
|
|
this.qrList.filter(qr=>!qr.isHidden).forEach(qr=>{
|
|
|
|
this.dom.append(qr.render());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
renderSettings() {
|
|
|
|
if (!this.settingsDom) {
|
|
|
|
this.settingsDom = document.createElement('div'); {
|
|
|
|
this.settingsDom.classList.add('qr--set-qrListContents');
|
|
|
|
this.qrList.forEach((qr,idx)=>{
|
|
|
|
this.renderSettingsItem(qr, idx);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.settingsDom;
|
|
|
|
}
|
|
|
|
renderSettingsItem(qr, idx) {
|
|
|
|
this.settingsDom.append(qr.renderSettings(idx));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-06-18 14:29:29 -04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {QuickReply} qr
|
|
|
|
*/
|
|
|
|
async debug(qr) {
|
|
|
|
const parser = new SlashCommandParser();
|
|
|
|
const closure = parser.parse(qr.message, true, [], qr.abortController, qr.debugController);
|
|
|
|
closure.onProgress = (done, total) => qr.updateEditorProgress(done, total);
|
2024-07-05 19:14:30 -04:00
|
|
|
closure.scope.setMacro('arg::*', '');
|
2024-06-18 14:29:29 -04:00
|
|
|
// closure.abortController = qr.abortController;
|
|
|
|
// closure.debugController = qr.debugController;
|
|
|
|
// const stepper = closure.executeGenerator();
|
|
|
|
// let step;
|
|
|
|
// let isStepping = false;
|
|
|
|
// while (!step?.done) {
|
|
|
|
// step = await stepper.next(isStepping);
|
|
|
|
// isStepping = yield(step.value);
|
|
|
|
// }
|
|
|
|
// return step.value;
|
|
|
|
return (await closure.execute())?.pipe;
|
|
|
|
}
|
2023-12-20 13:40:44 +00:00
|
|
|
/**
|
2024-05-12 15:15:05 -04:00
|
|
|
*
|
|
|
|
* @param {QuickReply} qr The QR to execute.
|
|
|
|
* @param {object} options
|
|
|
|
* @param {string} [options.message] (null) altered message to be used
|
|
|
|
* @param {boolean} [options.isAutoExecute] (false) whether the execution is triggered by auto execute
|
|
|
|
* @param {boolean} [options.isEditor] (false) whether the execution is triggered by the QR editor
|
|
|
|
* @param {boolean} [options.isRun] (false) whether the execution is triggered by /run or /: (window.executeQuickReplyByName)
|
|
|
|
* @param {SlashCommandScope} [options.scope] (null) scope to be used when running the command
|
|
|
|
* @returns
|
2023-12-20 13:40:44 +00:00
|
|
|
*/
|
2024-05-12 15:15:05 -04:00
|
|
|
async executeWithOptions(qr, options = {}) {
|
|
|
|
options = Object.assign({
|
|
|
|
message:null,
|
|
|
|
isAutoExecute:false,
|
|
|
|
isEditor:false,
|
|
|
|
isRun:false,
|
|
|
|
scope:null,
|
|
|
|
}, options);
|
2023-12-20 13:40:44 +00:00
|
|
|
/**@type {HTMLTextAreaElement}*/
|
|
|
|
const ta = document.querySelector('#send_textarea');
|
2024-05-12 15:15:05 -04:00
|
|
|
const finalMessage = options.message ?? qr.message;
|
2023-12-20 13:40:44 +00:00
|
|
|
let input = ta.value;
|
2024-05-12 15:15:05 -04:00
|
|
|
if (!options.isAutoExecute && !options.isEditor && !options.isRun && this.injectInput && input.length > 0) {
|
2023-12-20 13:40:44 +00:00
|
|
|
if (this.placeBeforeInput) {
|
2023-12-27 12:28:15 +00:00
|
|
|
input = `${finalMessage} ${input}`;
|
2023-12-20 13:40:44 +00:00
|
|
|
} else {
|
2023-12-27 12:28:15 +00:00
|
|
|
input = `${input} ${finalMessage}`;
|
2023-12-20 13:40:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-12-27 12:28:15 +00:00
|
|
|
input = `${finalMessage} `;
|
2023-12-20 13:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (input[0] == '/' && !this.disableSend) {
|
2024-05-12 15:15:05 -04:00
|
|
|
let result;
|
|
|
|
if (options.isAutoExecute || options.isRun) {
|
|
|
|
result = await executeSlashCommandsWithOptions(input, {
|
|
|
|
handleParserErrors: true,
|
|
|
|
scope: options.scope,
|
|
|
|
});
|
|
|
|
} else if (options.isEditor) {
|
|
|
|
result = await executeSlashCommandsWithOptions(input, {
|
|
|
|
handleParserErrors: false,
|
|
|
|
scope: options.scope,
|
|
|
|
abortController: qr.abortController,
|
|
|
|
onProgress: (done, total) => qr.updateEditorProgress(done, total),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
result = await executeSlashCommandsOnChatInput(input, {
|
|
|
|
scope: options.scope,
|
|
|
|
});
|
|
|
|
}
|
2023-12-20 13:40:44 +00:00
|
|
|
return typeof result === 'object' ? result?.pipe : '';
|
|
|
|
}
|
|
|
|
|
2024-01-07 00:11:30 +00:00
|
|
|
ta.value = substituteParams(input);
|
2023-12-20 13:40:44 +00:00
|
|
|
ta.focus();
|
|
|
|
|
|
|
|
if (!this.disableSend) {
|
|
|
|
// @ts-ignore
|
|
|
|
document.querySelector('#send_but').click();
|
|
|
|
}
|
|
|
|
}
|
2024-05-12 15:15:05 -04:00
|
|
|
/**
|
|
|
|
* @param {QuickReply} qr
|
|
|
|
* @param {String} [message] - optional altered message to be used
|
|
|
|
* @param {SlashCommandScope} [scope] - optional scope to be used when running the command
|
|
|
|
*/
|
|
|
|
async execute(qr, message = null, isAutoExecute = false, scope = null) {
|
|
|
|
return this.executeWithOptions(qr, {
|
|
|
|
message,
|
|
|
|
isAutoExecute,
|
|
|
|
scope,
|
|
|
|
});
|
|
|
|
}
|
2023-12-20 13:40:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
addQuickReply() {
|
|
|
|
const id = Math.max(this.idIndex, this.qrList.reduce((max,qr)=>Math.max(max,qr.id),0)) + 1;
|
|
|
|
this.idIndex = id + 1;
|
|
|
|
const qr = QuickReply.from({ id });
|
|
|
|
this.qrList.push(qr);
|
|
|
|
this.hookQuickReply(qr);
|
|
|
|
if (this.settingsDom) {
|
|
|
|
this.renderSettingsItem(qr, this.qrList.length - 1);
|
|
|
|
}
|
|
|
|
if (this.dom) {
|
|
|
|
this.dom.append(qr.render());
|
|
|
|
}
|
|
|
|
this.save();
|
|
|
|
return qr;
|
|
|
|
}
|
|
|
|
|
2024-06-18 14:29:29 -04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {QuickReply} qr
|
|
|
|
*/
|
2023-12-20 13:40:44 +00:00
|
|
|
hookQuickReply(qr) {
|
2024-06-18 14:29:29 -04:00
|
|
|
qr.onDebug = ()=>this.debug(qr);
|
2024-05-12 15:15:05 -04:00
|
|
|
qr.onExecute = (_, options)=>this.executeWithOptions(qr, options);
|
2023-12-20 13:40:44 +00:00
|
|
|
qr.onDelete = ()=>this.removeQuickReply(qr);
|
|
|
|
qr.onUpdate = ()=>this.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
removeQuickReply(qr) {
|
|
|
|
this.qrList.splice(this.qrList.indexOf(qr), 1);
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
version: 2,
|
|
|
|
name: this.name,
|
|
|
|
disableSend: this.disableSend,
|
|
|
|
placeBeforeInput: this.placeBeforeInput,
|
|
|
|
injectInput: this.injectInput,
|
|
|
|
qrList: this.qrList,
|
|
|
|
idIndex: this.idIndex,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async performSave() {
|
2024-03-20 00:46:46 +02:00
|
|
|
const response = await fetch('/api/quick-replies/save', {
|
2023-12-20 13:40:44 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: getRequestHeaders(),
|
|
|
|
body: JSON.stringify(this),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
this.rerender();
|
|
|
|
} else {
|
|
|
|
warn(`Failed to save Quick Reply Set: ${this.name}`);
|
2024-06-28 03:28:16 +02:00
|
|
|
console.error('QR could not be saved', response);
|
2023-12-20 13:40:44 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-20 15:49:27 +00:00
|
|
|
|
|
|
|
async delete() {
|
2024-03-20 00:46:46 +02:00
|
|
|
const response = await fetch('/api/quick-replies/delete', {
|
2023-12-20 15:49:27 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: getRequestHeaders(),
|
|
|
|
body: JSON.stringify(this),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
this.unrender();
|
|
|
|
const idx = QuickReplySet.list.indexOf(this);
|
|
|
|
QuickReplySet.list.splice(idx, 1);
|
2023-12-20 17:56:08 +00:00
|
|
|
this.isDeleted = true;
|
2023-12-20 15:49:27 +00:00
|
|
|
} else {
|
|
|
|
warn(`Failed to delete Quick Reply Set: ${this.name}`);
|
|
|
|
}
|
|
|
|
}
|
2023-12-20 13:40:44 +00:00
|
|
|
}
|