diff --git a/.gitignore b/.gitignore index fed4e2439..be3012bd6 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,5 @@ secrets.json /dist /backups/ public/movingUI/ +public/QuickReplies/ content.log diff --git a/public/scripts/extensions/quick-reply/index.js b/public/scripts/extensions/quick-reply/index.js index 9bf3058b8..e47484f39 100644 --- a/public/scripts/extensions/quick-reply/index.js +++ b/public/scripts/extensions/quick-reply/index.js @@ -1,19 +1,48 @@ -import { saveSettingsDebounced } from "../../../script.js"; +import { saveSettingsDebounced, callPopup, getRequestHeaders } from "../../../script.js"; import { getContext, extension_settings } from "../../extensions.js"; import { initScrollHeight, resetScrollHeight } from "../../utils.js"; + export { MODULE_NAME }; const MODULE_NAME = 'quick-reply'; const UPDATE_INTERVAL = 1000; +let presets = []; +let selected_preset = ''; const defaultSettings = { - quickReplyEnabled: false, + quickReplyEnabled: true, numberOfSlots: 5, quickReplySlots: [], } -async function loadSettings() { +//method from worldinfo +async function updateQuickReplyPresetList() { + var result = await fetch("/getsettings", { + method: "POST", + headers: getRequestHeaders(), + body: JSON.stringify({}), + }); + + if (result.ok) { + var data = await result.json(); + presets = data.quickReplyPresets?.length ? data.quickReplyPresets : []; + console.log(presets) + $("#quickReplyPresets").find('option[value!=""]').remove(); + + + if (presets !== undefined) { + presets.forEach((item, i) => { + $("#quickReplyPresets").append(``); + }); + } + } +} + +async function loadSettings(type) { + if (type === 'init') { + await updateQuickReplyPresetList() + } if (Object.keys(extension_settings.quickReply).length === 0) { Object.assign(extension_settings.quickReply, defaultSettings); } @@ -111,6 +140,51 @@ async function moduleWorker() { if (extension_settings.quickReply.quickReplyEnabled === true) { $('#quickReplyBar').toggle(getContext().onlineStatus !== 'no_connection'); } + 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, + 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}"]`).attr('selected', true); + } + saveSettingsDebounced(); + } else { + toastr.warning('Failed to save Quick Reply Preset.') + } } async function onQuickReplyNumberOfSlotsInput() { @@ -178,6 +252,27 @@ function generateQuickReplyElements() { }); } +async function applyQuickReplyPreset(name) { + const quickReplyPreset = presets.find(x => x.name == name); + + if (!quickReplyPreset) { + console.log(`error, QR preset '${name}' not found`) + return; + } + + extension_settings.quickReply = quickReplyPreset; + extension_settings.quickReply.selectedPreset = name; + saveSettingsDebounced() + loadSettings('init') + addQuickReplyBar(); + moduleWorker(); + + $(`#quickReplyPresets option[value="${name}"]`).attr('selected', true); + + console.debug('QR Preset applied: ' + name); + //loadMovingUIState() +} + jQuery(async () => { moduleWorker(); @@ -190,11 +285,18 @@ jQuery(async () => {
- - +
+ +
+ + +
+ +