SillyTavern/public/scripts/extensions/quick-reply/index.js

897 lines
34 KiB
JavaScript
Raw Normal View History

2023-11-24 00:32:02 +01:00
import { saveSettingsDebounced, callPopup, getRequestHeaders, substituteParams, eventSource, event_types } from "../../../script.js";
2023-07-20 19:32:15 +02:00
import { getContext, extension_settings } from "../../extensions.js";
2023-12-02 14:19:44 +01:00
import { initScrollHeight, resetScrollHeight, getSortableDelay, escapeHtml } from "../../utils.js";
import { executeSlashCommands, registerSlashCommand } from "../../slash-commands.js";
2023-11-23 13:21:25 +01:00
import { ContextMenu } from "./src/ContextMenu.js";
import { MenuItem } from "./src/MenuItem.js";
import { MenuHeader } from "./src/MenuHeader.js";
2023-11-27 01:48:05 +01:00
import { loadMovingUIState } from "../../power-user.js";
import { dragElement } from "../../RossAscends-mods.js";
2023-07-29 23:22:03 +02:00
2023-07-20 19:32:15 +02:00
export { MODULE_NAME };
const MODULE_NAME = 'quick-reply';
const UPDATE_INTERVAL = 1000;
2023-07-29 23:22:03 +02:00
let presets = [];
let selected_preset = '';
2023-07-20 19:32:15 +02:00
const defaultSettings = {
2023-08-29 17:04:10 +02:00
quickReplyEnabled: false,
2023-07-20 19:32:15 +02:00
numberOfSlots: 5,
quickReplySlots: [],
placeBeforeInputEnabled: false,
quickActionEnabled: false,
2023-10-21 15:09:25 +02:00
AutoInputInject: true,
2023-07-20 19:32:15 +02:00
}
2023-07-29 23:22:03 +02:00
//method from worldinfo
async function updateQuickReplyPresetList() {
2023-11-12 21:35:17 +01:00
const result = await fetch("/getsettings", {
2023-07-29 23:22:03 +02:00
method: "POST",
headers: getRequestHeaders(),
body: JSON.stringify({}),
});
if (result.ok) {
var data = await result.json();
presets = data.quickReplyPresets?.length ? data.quickReplyPresets : [];
console.debug('Quick Reply presets', presets);
2023-07-29 23:22:03 +02:00
$("#quickReplyPresets").find('option[value!=""]').remove();
if (presets !== undefined) {
2023-10-21 15:09:25 +02:00
presets.forEach((item) => {
const option = document.createElement('option');
option.value = item.name;
option.innerText = item.name;
option.selected = selected_preset.includes(item.name);
$("#quickReplyPresets").append(option);
2023-07-29 23:22:03 +02:00
});
}
}
}
async function loadSettings(type) {
if (type === 'init') {
await updateQuickReplyPresetList()
}
2023-07-20 19:32:15 +02:00
if (Object.keys(extension_settings.quickReply).length === 0) {
Object.assign(extension_settings.quickReply, defaultSettings);
}
2023-10-21 15:09:25 +02:00
if (extension_settings.quickReply.AutoInputInject === undefined) {
extension_settings.quickReply.AutoInputInject = true;
}
2023-07-20 19:32:15 +02:00
// If the user has an old version of the extension, update it
if (!Array.isArray(extension_settings.quickReply.quickReplySlots)) {
extension_settings.quickReply.quickReplySlots = [];
extension_settings.quickReply.numberOfSlots = defaultSettings.numberOfSlots;
for (let i = 1; i <= extension_settings.quickReply.numberOfSlots; i++) {
extension_settings.quickReply.quickReplySlots.push({
mes: extension_settings.quickReply[`quickReply${i}Mes`],
label: extension_settings.quickReply[`quickReply${i}Label`],
enabled: true,
});
delete extension_settings.quickReply[`quickReply${i}Mes`];
delete extension_settings.quickReply[`quickReply${i}Label`];
}
}
initializeEmptySlots(extension_settings.quickReply.numberOfSlots);
generateQuickReplyElements();
for (let i = 1; i <= extension_settings.quickReply.numberOfSlots; i++) {
$(`#quickReply${i}Mes`).val(extension_settings.quickReply.quickReplySlots[i - 1]?.mes).trigger('input');
$(`#quickReply${i}Label`).val(extension_settings.quickReply.quickReplySlots[i - 1]?.label).trigger('input');
}
$('#quickReplyEnabled').prop('checked', extension_settings.quickReply.quickReplyEnabled);
$('#quickReplyNumberOfSlots').val(extension_settings.quickReply.numberOfSlots);
$('#placeBeforeInputEnabled').prop('checked', extension_settings.quickReply.placeBeforeInputEnabled);
$('#quickActionEnabled').prop('checked', extension_settings.quickReply.quickActionEnabled);
$('#AutoInputInject').prop('checked', extension_settings.quickReply.AutoInputInject);
2023-07-20 19:32:15 +02:00
}
function onQuickReplyInput(id) {
extension_settings.quickReply.quickReplySlots[id - 1].mes = $(`#quickReply${id}Mes`).val();
2023-10-21 15:09:25 +02:00
$(`#quickReply${id}`).attr('title', String($(`#quickReply${id}Mes`).val()));
2023-07-20 19:32:15 +02:00
resetScrollHeight($(`#quickReply${id}Mes`));
saveSettingsDebounced();
}
function onQuickReplyLabelInput(id) {
extension_settings.quickReply.quickReplySlots[id - 1].label = $(`#quickReply${id}Label`).val();
2023-11-23 21:34:20 +01:00
addQuickReplyBar();
2023-11-23 18:42:19 +01:00
saveSettingsDebounced();
}
async function onQuickReplyContextMenuChange(id) {
extension_settings.quickReply.quickReplySlots[id - 1].contextMenu = JSON.parse($(`#quickReplyContainer > [data-order="${id}"]`).attr('data-contextMenu'))
2023-07-20 19:32:15 +02:00
saveSettingsDebounced();
}
2023-11-23 18:42:19 +01:00
async function onQuickReplyCtxButtonClick(id) {
const editorHtml = $(await $.get('scripts/extensions/quick-reply/contextMenuEditor.html'));
2023-11-24 00:32:02 +01:00
const popupResult = callPopup(editorHtml, "confirm", undefined, { okButton: "Save", wide: false, large: false, rows: 1 });
2023-11-23 18:42:19 +01:00
const qr = extension_settings.quickReply.quickReplySlots[id - 1];
if (!qr.contextMenu) {
qr.contextMenu = [];
}
/**@type {HTMLTemplateElement}*/
const tpl = document.querySelector('#quickReply_contextMenuEditor_itemTemplate');
const fillPresetSelect = (select, item) => {
2023-11-24 00:32:02 +01:00
[{ name: 'Select a preset', value: '' }, ...presets].forEach(preset => {
2023-11-23 18:42:19 +01:00
const opt = document.createElement('option'); {
opt.value = preset.value ?? preset.name;
opt.textContent = preset.name;
opt.selected = preset.name == item.preset;
select.append(opt);
}
});
};
const addCtxItem = (item, idx) => {
const dom = tpl.content.cloneNode(true);
const ctxItem = dom.querySelector('.quickReplyContextMenuEditor_item');
ctxItem.setAttribute('data-order', idx);
const select = ctxItem.querySelector('.quickReply_contextMenuEditor_preset');
fillPresetSelect(select, item);
dom.querySelector('.quickReply_contextMenuEditor_chaining').checked = item.chain;
2023-11-24 00:32:02 +01:00
$('.quickReply_contextMenuEditor_remove', ctxItem).on('click', () => ctxItem.remove());
2023-11-23 18:42:19 +01:00
document.querySelector('#quickReply_contextMenuEditor_content').append(ctxItem);
}
2023-11-24 00:32:02 +01:00
[...qr.contextMenu, {}].forEach((item, idx) => {
2023-11-23 18:42:19 +01:00
addCtxItem(item, idx)
});
2023-11-24 00:32:02 +01:00
$('#quickReply_contextMenuEditor_addPreset').on('click', () => {
2023-11-23 18:42:19 +01:00
addCtxItem({}, document.querySelector('#quickReply_contextMenuEditor_content').children.length);
});
$('#quickReply_contextMenuEditor_content').sortable({
delay: getSortableDelay(),
2023-11-24 00:32:02 +01:00
stop: () => { },
});
$('#quickReply_autoExecute_userMessage').prop('checked', qr.autoExecute_userMessage ?? false);
$('#quickReply_autoExecute_botMessage').prop('checked', qr.autoExecute_botMessage ?? false);
$('#quickReply_autoExecute_chatLoad').prop('checked', qr.autoExecute_chatLoad ?? false);
2023-11-26 01:12:31 +01:00
$('#quickReply_autoExecute_appStartup').prop('checked', qr.autoExecute_appStartup ?? false);
$('#quickReply_hidden').prop('checked', qr.hidden ?? false);
$('#quickReply_hidden').on('input', () => {
const state = !!$('#quickReply_hidden').prop('checked');
qr.hidden = state;
saveSettingsDebounced();
});
2023-11-24 00:32:02 +01:00
2023-11-26 01:12:31 +01:00
$('#quickReply_autoExecute_appStartup').on('input', () => {
const state = !!$('#quickReply_autoExecute_appStartup').prop('checked');
qr.autoExecute_appStartup = state;
saveSettingsDebounced();
});
2023-11-24 00:32:02 +01:00
$('#quickReply_autoExecute_userMessage').on('input', () => {
const state = !!$('#quickReply_autoExecute_userMessage').prop('checked');
qr.autoExecute_userMessage = state;
saveSettingsDebounced();
});
$('#quickReply_autoExecute_botMessage').on('input', () => {
const state = !!$('#quickReply_autoExecute_botMessage').prop('checked');
qr.autoExecute_botMessage = state;
saveSettingsDebounced();
2023-11-23 18:42:19 +01:00
});
$('#quickReply_autoExecute_chatLoad').on('input', () => {
const state = !!$('#quickReply_autoExecute_chatLoad').prop('checked');
qr.autoExecute_chatLoad = state;
saveSettingsDebounced();
});
2023-11-23 18:42:19 +01:00
if (await popupResult) {
qr.contextMenu = Array.from(document.querySelectorAll('#quickReply_contextMenuEditor_content > .quickReplyContextMenuEditor_item'))
2023-11-24 00:32:02 +01:00
.map(item => ({
2023-11-23 18:42:19 +01:00
preset: item.querySelector('.quickReply_contextMenuEditor_preset').value,
chain: item.querySelector('.quickReply_contextMenuEditor_chaining').checked,
}))
2023-11-24 00:32:02 +01:00
.filter(item => item.preset);
2023-11-23 18:42:19 +01:00
$(`#quickReplyContainer[data-order="${id}"]`).attr('data-contextMenu', JSON.stringify(qr.contextMenu));
updateQuickReplyPreset();
onQuickReplyLabelInput(id);
}
}
2023-07-20 19:32:15 +02:00
async function onQuickReplyEnabledInput() {
let isEnabled = $(this).prop('checked')
extension_settings.quickReply.quickReplyEnabled = !!isEnabled;
if (isEnabled === true) {
$("#quickReplyBar").show();
} else { $("#quickReplyBar").hide(); }
saveSettingsDebounced();
}
2023-09-07 04:27:03 +02:00
// New function to handle input on quickActionEnabled
async function onQuickActionEnabledInput() {
extension_settings.quickReply.quickActionEnabled = !!$(this).prop('checked');
saveSettingsDebounced();
}
async function onPlaceBeforeInputEnabledInput() {
extension_settings.quickReply.placeBeforeInputEnabled = !!$(this).prop('checked');
saveSettingsDebounced();
}
async function onAutoInputInject() {
extension_settings.quickReply.AutoInputInject = !!$(this).prop('checked');
2023-09-07 04:27:03 +02:00
saveSettingsDebounced();
}
2023-07-20 19:32:15 +02:00
async function sendQuickReply(index) {
const prompt = extension_settings.quickReply.quickReplySlots[index]?.mes || '';
return await performQuickReply(prompt, index);
2023-11-23 13:21:25 +01:00
}
async function executeQuickReplyByName(name) {
if (!extension_settings.quickReply.quickReplyEnabled) {
throw new Error('Quick Reply is disabled');
}
const qr = extension_settings.quickReply.quickReplySlots.find(x => x.label == name);
if (!qr) {
throw new Error(`Quick Reply "${name}" not found`);
}
return await performQuickReply(qr.mes);
}
window['executeQuickReplyByName'] = executeQuickReplyByName;
2023-11-23 13:21:25 +01:00
async function performQuickReply(prompt, index) {
2023-07-20 19:32:15 +02:00
if (!prompt) {
console.warn(`Quick reply slot ${index} is empty! Aborting.`);
return;
}
2023-11-23 13:21:25 +01:00
const existingText = $("#send_textarea").val();
2023-07-20 19:32:15 +02:00
2023-09-07 04:27:03 +02:00
let newText;
2023-10-21 15:09:25 +02:00
if (existingText && extension_settings.quickReply.AutoInputInject) {
if (extension_settings.quickReply.placeBeforeInputEnabled) {
newText = `${prompt} ${existingText} `;
} else {
newText = `${existingText} ${prompt} `;
}
2023-09-07 04:27:03 +02:00
} else {
// If no existing text and placeBeforeInputEnabled false, add prompt only (with a trailing space)
newText = `${prompt} `;
2023-09-07 04:27:03 +02:00
}
// the prompt starts with '/' - execute slash commands natively
if (prompt.startsWith('/')) {
const result = await executeSlashCommands(newText);
return result?.pipe;
}
newText = substituteParams(newText);
2023-09-07 04:27:03 +02:00
$("#send_textarea").val(newText);
// Set the focus back to the textarea
2023-10-21 15:09:25 +02:00
$("#send_textarea").trigger('focus');
2023-09-07 04:27:03 +02:00
2023-10-21 15:09:25 +02:00
// Only trigger send button if quickActionEnabled is not checked or
if (!extension_settings.quickReply.quickActionEnabled) {
2023-09-07 04:27:03 +02:00
$("#send_but").trigger('click');
}
2023-07-20 19:32:15 +02:00
}
2023-09-07 23:08:21 +02:00
2023-11-24 00:32:02 +01:00
function buildContextMenu(qr, chainMes = null, hierarchy = [], labelHierarchy = []) {
2023-11-23 13:21:25 +01:00
const tree = {
label: qr.label,
2023-11-24 00:32:02 +01:00
mes: (chainMes && qr.mes ? `${chainMes} | ` : '') + qr.mes,
2023-11-23 13:21:25 +01:00
children: [],
};
2023-11-24 00:32:02 +01:00
qr.contextMenu?.forEach(ctxItem => {
2023-11-23 18:42:19 +01:00
let chain = ctxItem.chain;
let subName = ctxItem.preset;
2023-11-24 00:32:02 +01:00
const sub = presets.find(it => it.name == subName);
2023-11-23 18:42:19 +01:00
if (sub) {
// prevent circular references
if (hierarchy.indexOf(sub.name) == -1) {
const nextHierarchy = [...hierarchy, sub.name];
const nextLabelHierarchy = [...labelHierarchy, tree.label];
tree.children.push(new MenuHeader(sub.name));
2023-11-24 00:32:02 +01:00
sub.quickReplySlots.forEach(subQr => {
const subInfo = buildContextMenu(subQr, chain ? tree.mes : null, nextHierarchy, nextLabelHierarchy);
2023-11-23 18:42:19 +01:00
tree.children.push(new MenuItem(
subInfo.label,
subInfo.mes,
2023-11-24 00:32:02 +01:00
(evt) => {
2023-11-23 18:42:19 +01:00
evt.stopPropagation();
2023-11-24 00:32:02 +01:00
performQuickReply(subInfo.mes.replace(/%%parent(-\d+)?%%/g, (_, index) => {
2023-11-23 18:42:19 +01:00
return nextLabelHierarchy.slice(parseInt(index ?? '-1'))[0];
}));
},
subInfo.children,
));
});
2023-11-23 13:21:25 +01:00
}
2023-11-23 18:42:19 +01:00
}
});
2023-11-23 13:21:25 +01:00
return tree;
}
2023-11-27 01:48:05 +01:00
async function doQuickReplyBarPopout() {
//shared elements
const newQuickRepliesDiv = `<div id="quickReplies"></div>`
const popoutButtonClone = $("#quickReplyPopoutButton")
if ($("#quickReplyBarPopout").length === 0) {
console.debug('did not see popout yet, creating')
const template = $('#zoomed_avatar_template').html();
const controlBarHtml = `<div class="panelControlBar flex-container">
<div id="quickReplyBarPopoutheader" class="fa-solid fa-grip drag-grabber hoverglow"></div>
<div id="quickReplyBarPopoutClose" class="fa-solid fa-circle-xmark hoverglow"></div>
</div>`
const newElement = $(template);
let quickRepliesClone = $('#quickReplies').html()
newElement.attr('id', 'quickReplyBarPopout')
.removeClass('zoomed_avatar')
.addClass('draggable scrollY')
.empty()
.append(controlBarHtml)
.append(newQuickRepliesDiv)
//empty original bar
$("#quickReplyBar").empty()
//add clone in popout
$('body').append(newElement);
$("#quickReplies").append(quickRepliesClone).css('margin-top', '1em')
$('.quickReplyButton').on('click', function () {
let index = $(this).data('index');
sendQuickReply(index);
});
2023-11-27 23:02:20 +01:00
$('.quickReplyButton > .ctx-expander').on('click', function (evt) {
evt.stopPropagation();
let index = $(this.closest('.quickReplyButton')).data('index');
const qr = extension_settings.quickReply.quickReplySlots[index];
if (qr.contextMenu?.length) {
evt.preventDefault();
const tree = buildContextMenu(qr);
const menu = new ContextMenu(tree.children);
menu.show(evt);
}
})
$('.quickReplyButton').on('contextmenu', function (evt) {
let index = $(this).data('index');
const qr = extension_settings.quickReply.quickReplySlots[index];
if (qr.contextMenu?.length) {
evt.preventDefault();
const tree = buildContextMenu(qr);
const menu = new ContextMenu(tree.children);
menu.show(evt);
}
});
2023-11-27 01:48:05 +01:00
loadMovingUIState();
$("#quickReplyBarPopout").fadeIn(250)
dragElement(newElement)
$('#quickReplyBarPopoutClose').off('click').on('click', function () {
console.debug('saw existing popout, removing')
let quickRepliesClone = $('#quickReplies').html()
$("#quickReplyBar").append(newQuickRepliesDiv)
$("#quickReplies").prepend(quickRepliesClone)
$("#quickReplyBar").append(popoutButtonClone).fadeIn(250)
$("#quickReplyBarPopout").fadeOut(250, () => { $("#quickReplyBarPopout").remove() });
$('.quickReplyButton').on('click', function () {
let index = $(this).data('index');
sendQuickReply(index);
});
2023-11-27 23:02:20 +01:00
$('.quickReplyButton > .ctx-expander').on('click', function (evt) {
evt.stopPropagation();
let index = $(this.closest('.quickReplyButton')).data('index');
const qr = extension_settings.quickReply.quickReplySlots[index];
if (qr.contextMenu?.length) {
evt.preventDefault();
const tree = buildContextMenu(qr);
const menu = new ContextMenu(tree.children);
menu.show(evt);
}
})
$('.quickReplyButton').on('contextmenu', function (evt) {
let index = $(this).data('index');
const qr = extension_settings.quickReply.quickReplySlots[index];
if (qr.contextMenu?.length) {
evt.preventDefault();
const tree = buildContextMenu(qr);
const menu = new ContextMenu(tree.children);
menu.show(evt);
}
});
2023-11-27 01:48:05 +01:00
$("#quickReplyPopoutButton").off('click').on('click', doQuickReplyBarPopout)
})
}
}
2023-07-20 19:32:15 +02:00
function addQuickReplyBar() {
let quickReplyButtonHtml = '';
2023-11-27 01:48:05 +01:00
var targetContainer;
if ($("#quickReplyBarPopout").length !== 0) {
targetContainer = 'popout'
} else {
targetContainer = 'bar'
$("#quickReplyBar").remove();
}
2023-07-20 19:32:15 +02:00
for (let i = 0; i < extension_settings.quickReply.numberOfSlots; i++) {
const qr = extension_settings.quickReply.quickReplySlots[i];
const quickReplyMes = qr?.mes || '';
const quickReplyLabel = qr?.label || '';
const hidden = qr?.hidden ?? false;
2023-11-23 21:34:20 +01:00
let expander = '';
2023-11-23 18:42:19 +01:00
if (extension_settings.quickReply.quickReplySlots[i]?.contextMenu?.length) {
2023-11-23 21:34:20 +01:00
expander = '<span class="ctx-expander" title="Open context menu">⋮</span>';
2023-11-23 13:21:25 +01:00
}
2023-12-02 14:19:44 +01:00
quickReplyButtonHtml += `<div title="${escapeHtml(quickReplyMes)}" class="quickReplyButton ${hidden ? 'displayNone' : ''}" data-index="${i}" id="quickReply${i + 1}">${DOMPurify.sanitize(quickReplyLabel)}${expander}</div>`;
2023-07-20 19:32:15 +02:00
}
const quickReplyBarFullHtml = `
<div id="quickReplyBar" class="flex-container flexGap5">
<div id="quickReplies">
${quickReplyButtonHtml}
</div>
2023-11-27 01:48:05 +01:00
<div id="quickReplyPopoutButton" class="fa-solid fa-window-restore menu_button"></div>
2023-07-20 19:32:15 +02:00
</div>
`;
2023-11-27 01:48:05 +01:00
if (targetContainer === 'bar') {
$('#send_form').prepend(quickReplyBarFullHtml);
} else {
$("#quickReplies").empty().append(quickReplyButtonHtml)
}
2023-07-20 19:32:15 +02:00
$('.quickReplyButton').on('click', function () {
let index = $(this).data('index');
sendQuickReply(index);
});
2023-11-27 01:48:05 +01:00
$("#quickReplyPopoutButton").off('click').on('click', doQuickReplyBarPopout)
2023-11-23 21:34:20 +01:00
$('.quickReplyButton > .ctx-expander').on('click', function (evt) {
evt.stopPropagation();
let index = $(this.closest('.quickReplyButton')).data('index');
const qr = extension_settings.quickReply.quickReplySlots[index];
if (qr.contextMenu?.length) {
evt.preventDefault();
const tree = buildContextMenu(qr);
const menu = new ContextMenu(tree.children);
menu.show(evt);
}
})
2023-11-23 13:21:25 +01:00
$('.quickReplyButton').on('contextmenu', function (evt) {
let index = $(this).data('index');
const qr = extension_settings.quickReply.quickReplySlots[index];
2023-11-23 18:42:19 +01:00
if (qr.contextMenu?.length) {
evt.preventDefault();
const tree = buildContextMenu(qr);
const menu = new ContextMenu(tree.children);
menu.show(evt);
}
2023-11-23 13:21:25 +01:00
});
2023-07-20 19:32:15 +02:00
}
async function moduleWorker() {
if (extension_settings.quickReply.quickReplyEnabled === true) {
$('#quickReplyBar').toggle(getContext().onlineStatus !== 'no_connection');
}
2023-07-29 23:22:03 +02:00
if (extension_settings.quickReply.selectedPreset) {
selected_preset = extension_settings.quickReply.selectedPreset;
}
}
async function saveQuickReplyPreset() {
const name = await callPopup('Enter a name for the Quick Reply Preset:', 'input');
if (!name) {
return;
}
const quickReplyPreset = {
name: name,
quickReplyEnabled: extension_settings.quickReply.quickReplyEnabled,
quickReplySlots: extension_settings.quickReply.quickReplySlots,
numberOfSlots: extension_settings.quickReply.numberOfSlots,
2023-10-31 11:27:40 +01:00
AutoInputInject: extension_settings.quickReply.AutoInputInject,
selectedPreset: name,
2023-07-29 23:22:03 +02:00
}
const response = await fetch('/savequickreply', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify(quickReplyPreset)
});
if (response.ok) {
const quickReplyPresetIndex = presets.findIndex(x => x.name == name);
if (quickReplyPresetIndex == -1) {
presets.push(quickReplyPreset);
const option = document.createElement('option');
option.selected = true;
option.value = name;
option.innerText = name;
$('#quickReplyPresets').append(option);
}
else {
presets[quickReplyPresetIndex] = quickReplyPreset;
2023-10-21 15:09:25 +02:00
$(`#quickReplyPresets option[value="${name}"]`).prop('selected', true);
2023-07-29 23:22:03 +02:00
}
saveSettingsDebounced();
} else {
toastr.warning('Failed to save Quick Reply Preset.')
}
2023-07-20 19:32:15 +02:00
}
2023-11-23 11:49:15 +01:00
//just a copy of save function with the name hardcoded to currently selected preset
async function updateQuickReplyPreset() {
const name = $("#quickReplyPresets").val()
if (!name) {
return;
}
const quickReplyPreset = {
name: name,
quickReplyEnabled: extension_settings.quickReply.quickReplyEnabled,
quickReplySlots: extension_settings.quickReply.quickReplySlots,
numberOfSlots: extension_settings.quickReply.numberOfSlots,
AutoInputInject: extension_settings.quickReply.AutoInputInject,
selectedPreset: name,
}
const response = await fetch('/savequickreply', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify(quickReplyPreset)
});
if (response.ok) {
const quickReplyPresetIndex = presets.findIndex(x => x.name == name);
if (quickReplyPresetIndex == -1) {
presets.push(quickReplyPreset);
const option = document.createElement('option');
option.selected = true;
option.value = name;
option.innerText = name;
$('#quickReplyPresets').append(option);
}
else {
presets[quickReplyPresetIndex] = quickReplyPreset;
$(`#quickReplyPresets option[value="${name}"]`).prop('selected', true);
}
saveSettingsDebounced();
} else {
toastr.warning('Failed to save Quick Reply Preset.')
}
}
2023-07-20 19:32:15 +02:00
async function onQuickReplyNumberOfSlotsInput() {
const $input = $('#quickReplyNumberOfSlots');
let numberOfSlots = Number($input.val());
if (isNaN(numberOfSlots)) {
numberOfSlots = defaultSettings.numberOfSlots;
}
// Clamp min and max values (from input attributes)
if (numberOfSlots < Number($input.attr('min'))) {
numberOfSlots = Number($input.attr('min'));
} else if (numberOfSlots > Number($input.attr('max'))) {
numberOfSlots = Number($input.attr('max'));
}
extension_settings.quickReply.numberOfSlots = numberOfSlots;
extension_settings.quickReply.quickReplySlots.length = numberOfSlots;
// Initialize new slots
initializeEmptySlots(numberOfSlots);
await loadSettings();
addQuickReplyBar();
moduleWorker();
saveSettingsDebounced();
}
function initializeEmptySlots(numberOfSlots) {
for (let i = 0; i < numberOfSlots; i++) {
if (!extension_settings.quickReply.quickReplySlots[i]) {
extension_settings.quickReply.quickReplySlots[i] = {
mes: '',
label: '',
enabled: true,
};
}
}
}
function generateQuickReplyElements() {
let quickReplyHtml = '';
for (let i = 1; i <= extension_settings.quickReply.numberOfSlots; i++) {
2023-11-23 11:49:15 +01:00
let itemNumber = i + 1
2023-07-20 19:32:15 +02:00
quickReplyHtml += `
2023-11-23 18:42:19 +01:00
<div class="flex-container alignitemscenter" data-order="${i}">
2023-11-23 11:49:15 +01:00
<span class="drag-handle ui-sortable-handle"></span>
2023-10-21 15:17:17 +02:00
<input class="text_pole wide30p" id="quickReply${i}Label" placeholder="(Button label)">
<span class="menu_button menu_button_icon" id="quickReply${i}CtxButton" title="Additional options: context menu, auto-execution"></span>
<textarea id="quickReply${i}Mes" placeholder="(Custom message or /command)" class="text_pole widthUnset flex1 autoSetHeight" rows="2"></textarea>
2023-07-20 19:32:15 +02:00
</div>
`;
}
$('#quickReplyContainer').empty().append(quickReplyHtml);
for (let i = 1; i <= extension_settings.quickReply.numberOfSlots; i++) {
$(`#quickReply${i}Mes`).on('input', function () { onQuickReplyInput(i); });
$(`#quickReply${i}Label`).on('input', function () { onQuickReplyLabelInput(i); });
2023-11-23 18:42:19 +01:00
$(`#quickReply${i}CtxButton`).on('click', function () { onQuickReplyCtxButtonClick(i); });
2023-11-24 00:32:02 +01:00
$(`#quickReplyContainer > [data-order="${i}"]`).attr('data-contextMenu', JSON.stringify(extension_settings.quickReply.quickReplySlots[i - 1]?.contextMenu ?? []));
2023-07-20 19:32:15 +02:00
}
$('.quickReplySettings .inline-drawer-toggle').off('click').on('click', function () {
for (let i = 1; i <= extension_settings.quickReply.numberOfSlots; i++) {
initScrollHeight($(`#quickReply${i}Mes`));
}
});
}
2023-07-29 23:22:03 +02:00
async function applyQuickReplyPreset(name) {
const quickReplyPreset = presets.find(x => x.name == name);
if (!quickReplyPreset) {
2023-08-03 07:44:23 +02:00
toastr.warning(`error, QR preset '${name}' not found. Confirm you are using proper case sensitivity!`)
2023-07-29 23:22:03 +02:00
return;
}
extension_settings.quickReply = quickReplyPreset;
extension_settings.quickReply.selectedPreset = name;
saveSettingsDebounced()
loadSettings('init')
addQuickReplyBar();
moduleWorker();
2023-10-21 15:09:25 +02:00
$(`#quickReplyPresets option[value="${name}"]`).prop('selected', true);
2023-07-29 23:22:03 +02:00
console.debug('QR Preset applied: ' + name);
2023-08-03 07:44:23 +02:00
}
async function doQRPresetSwitch(_, text) {
text = String(text)
applyQuickReplyPreset(text)
}
async function doQR(_, text) {
if (!text) {
toastr.warning('must specify which QR # to use')
return
}
text = Number(text)
//use scale starting with 0
2023-08-03 07:44:23 +02:00
//ex: user inputs "/qr 2" >> qr with data-index 1 (but 2nd item displayed) gets triggered
let QRnum = Number(text - 1)
if (QRnum <= 0) { QRnum = 0 }
const whichQR = $("#quickReplies").find(`[data-index='${QRnum}']`);
whichQR.trigger('click')
2023-07-29 23:22:03 +02:00
}
2023-11-23 11:49:15 +01:00
function saveQROrder() {
//update html-level order data to match new sort
let i = 1
$('#quickReplyContainer').children().each(function () {
$(this).attr('data-order', i)
$(this).find('input').attr('id', `quickReply${i}Label`)
$(this).find('textarea').attr('id', `quickReply${i}Mes`)
i++
});
//rebuild the extension_Settings array based on new order
i = 1
$('#quickReplyContainer').children().each(function () {
2023-11-23 18:42:19 +01:00
onQuickReplyContextMenuChange(i)
2023-11-23 11:49:15 +01:00
onQuickReplyLabelInput(i)
onQuickReplyInput(i)
i++
});
}
2023-11-27 01:18:36 +01:00
let onMessageSentExecuting = false;
let onMessageReceivedExecuting = false;
let onChatChangedExecuting = false;
/**
* Executes quick replies on message received.
* @param {number} index New message index
* @returns {Promise<void>}
*/
async function onMessageReceived(index) {
2023-11-24 12:32:27 +01:00
if (!extension_settings.quickReply.quickReplyEnabled) return;
2023-11-27 01:18:36 +01:00
if (onMessageReceivedExecuting) return;
try {
onMessageReceivedExecuting = true;
for (let i = 0; i < extension_settings.quickReply.numberOfSlots; i++) {
const qr = extension_settings.quickReply.quickReplySlots[i];
if (qr?.autoExecute_botMessage) {
const message = getContext().chat[index];
if (message?.mes && message?.mes !== '...') {
await sendQuickReply(i);
}
}
2023-11-24 00:32:02 +01:00
}
2023-11-27 01:18:36 +01:00
} finally {
onMessageReceivedExecuting = false;
2023-11-24 00:32:02 +01:00
}
}
/**
* Executes quick replies on message sent.
* @param {number} index New message index
* @returns {Promise<void>}
*/
async function onMessageSent(index) {
2023-11-24 12:32:27 +01:00
if (!extension_settings.quickReply.quickReplyEnabled) return;
2023-11-27 01:18:36 +01:00
if (onMessageSentExecuting) return;
try {
onMessageSentExecuting = true;
for (let i = 0; i < extension_settings.quickReply.numberOfSlots; i++) {
const qr = extension_settings.quickReply.quickReplySlots[i];
if (qr?.autoExecute_userMessage) {
const message = getContext().chat[index];
if (message?.mes && message?.mes !== '...') {
await sendQuickReply(i);
}
}
2023-11-24 00:32:02 +01:00
}
2023-11-27 01:18:36 +01:00
} finally {
onMessageSentExecuting = false;
2023-11-24 00:32:02 +01:00
}
}
/**
* Executes quick replies on chat changed.
* @param {string} chatId New chat id
* @returns {Promise<void>}
*/
async function onChatChanged(chatId) {
if (!extension_settings.quickReply.quickReplyEnabled) return;
2023-11-27 01:18:36 +01:00
if (onChatChangedExecuting) return;
try {
onChatChangedExecuting = true;
for (let i = 0; i < extension_settings.quickReply.numberOfSlots; i++) {
const qr = extension_settings.quickReply.quickReplySlots[i];
if (qr?.autoExecute_chatLoad && chatId) {
await sendQuickReply(i);
}
}
2023-11-27 01:18:36 +01:00
} finally {
onChatChangedExecuting = false;
}
}
2023-11-26 01:12:31 +01:00
/**
* Executes quick replies on app ready.
* @returns {Promise<void>}
*/
async function onAppReady() {
if (!extension_settings.quickReply.quickReplyEnabled) return;
for (let i = 0; i < extension_settings.quickReply.numberOfSlots; i++) {
const qr = extension_settings.quickReply.quickReplySlots[i];
if (qr?.autoExecute_appStartup) {
await sendQuickReply(i);
}
}
}
2023-07-20 19:32:15 +02:00
jQuery(async () => {
moduleWorker();
setInterval(moduleWorker, UPDATE_INTERVAL);
const settingsHtml = `
<div class="quickReplySettings">
<div class="inline-drawer">
<div class="inline-drawer-toggle inline-drawer-header">
<b>Quick Reply</b>
<div class="inline-drawer-icon fa-solid fa-circle-chevron-down down"></div>
</div>
<div class="inline-drawer-content">
<div>
<label class="checkbox_label">
2023-07-29 23:22:03 +02:00
<input id="quickReplyEnabled" type="checkbox" />
2023-10-21 15:09:25 +02:00
Enable Quick Replies
2023-07-29 23:22:03 +02:00
</label>
<label class="checkbox_label">
2023-09-07 04:27:03 +02:00
<input id="quickActionEnabled" type="checkbox" />
2023-10-21 15:09:25 +02:00
Disable Send / Insert In User Input
2023-09-07 04:27:03 +02:00
</label>
<label class="checkbox_label marginBot10">
<input id="placeBeforeInputEnabled" type="checkbox" />
2023-10-21 15:09:25 +02:00
Place Quick-reply before the Input
</label>
<label class="checkbox_label marginBot10">
<input id="AutoInputInject" type="checkbox" />
2023-10-21 15:09:25 +02:00
Inject user input automatically<br>(If disabled, use {{input}} macro for manual injection)
</label>
2023-10-21 15:17:17 +02:00
<label for="quickReplyPresets">Quick Reply presets:</label>
2023-07-29 23:22:03 +02:00
<div class="flex-container flexnowrap wide100p">
2023-10-21 15:17:17 +02:00
<select id="quickReplyPresets" name="quickreply-preset" class="flex1 text_pole">
2023-07-29 23:22:03 +02:00
</select>
2023-10-21 15:17:17 +02:00
<div id="quickReplyPresetSaveButton" class="menu_button menu_button_icon">
<div class="fa-solid fa-save"></div>
2023-11-23 11:49:15 +01:00
<span>Save New</span>
</div>
<div id="quickReplyPresetUpdateButton" class="menu_button menu_button_icon">
<span>Update</span>
2023-10-21 15:17:17 +02:00
</div>
2023-07-29 23:22:03 +02:00
</div>
<label for="quickReplyNumberOfSlots">Number of slots:</label>
</div>
2023-07-20 19:32:15 +02:00
<div class="flex-container flexGap5 flexnowrap">
<input id="quickReplyNumberOfSlots" class="text_pole" type="number" min="1" max="100" value="" />
<div class="menu_button menu_button_icon" id="quickReplyNumberOfSlotsApply">
<div class="fa-solid fa-check"></div>
<span>Apply</span>
</div>
</div>
<small><i>Customize your Quick Replies:</i></small><br>
<div id="quickReplyContainer">
</div>
</div>
</div>`;
$('#extensions_settings2').append(settingsHtml);
2023-10-21 15:09:25 +02:00
2023-09-07 04:27:03 +02:00
// Add event handler for quickActionEnabled
$('#quickActionEnabled').on('input', onQuickActionEnabledInput);
$('#placeBeforeInputEnabled').on('input', onPlaceBeforeInputEnabledInput);
$('#AutoInputInject').on('input', onAutoInputInject);
2023-07-20 19:32:15 +02:00
$('#quickReplyEnabled').on('input', onQuickReplyEnabledInput);
$('#quickReplyNumberOfSlotsApply').on('click', onQuickReplyNumberOfSlotsInput);
2023-07-29 23:22:03 +02:00
$("#quickReplyPresetSaveButton").on('click', saveQuickReplyPreset);
2023-11-23 11:49:15 +01:00
$("#quickReplyPresetUpdateButton").on('click', updateQuickReplyPreset);
$('#quickReplyContainer').sortable({
delay: getSortableDelay(),
stop: saveQROrder,
});
2023-07-20 19:32:15 +02:00
2023-07-29 23:22:03 +02:00
$("#quickReplyPresets").on('change', async function () {
const quickReplyPresetSelected = $(this).find(':selected').val();
extension_settings.quickReplyPreset = quickReplyPresetSelected;
applyQuickReplyPreset(quickReplyPresetSelected);
saveSettingsDebounced();
});
await loadSettings('init');
2023-07-20 19:32:15 +02:00
addQuickReplyBar();
2023-11-24 00:32:02 +01:00
eventSource.on(event_types.MESSAGE_RECEIVED, onMessageReceived);
eventSource.on(event_types.MESSAGE_SENT, onMessageSent);
eventSource.on(event_types.CHAT_CHANGED, onChatChanged);
2023-11-26 01:12:31 +01:00
eventSource.on(event_types.APP_READY, onAppReady);
2023-07-20 19:32:15 +02:00
});
2023-10-21 15:09:25 +02:00
jQuery(() => {
registerSlashCommand('qr', doQR, [], '<span class="monospace">(number)</span> activates the specified Quick Reply', true, true);
registerSlashCommand('qrset', doQRPresetSwitch, [], '<span class="monospace">(name)</span> swaps to the specified Quick Reply Preset', true, true);
2023-08-03 07:44:23 +02:00
})