From 4c1c62a56b1c9aff1d1a2d8a5a548866d546d119 Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Sat, 11 Jan 2025 01:41:46 +0100 Subject: [PATCH 1/4] Add QR set rename button --- .../extensions/quick-reply/html/settings.html | 1 + .../quick-reply/src/ui/SettingsUi.js | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/public/scripts/extensions/quick-reply/html/settings.html b/public/scripts/extensions/quick-reply/html/settings.html index a9a810fd8..3c8100aae 100644 --- a/public/scripts/extensions/quick-reply/html/settings.html +++ b/public/scripts/extensions/quick-reply/html/settings.html @@ -46,6 +46,7 @@
Edit Quick Replies
+ diff --git a/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js b/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js index 920c2cd30..e655a73f2 100644 --- a/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js +++ b/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js @@ -111,6 +111,7 @@ export class SettingsUi { prepareQrEditor() { // qr editor + this.dom.querySelector('#qr--set-rename').addEventListener('click', async () => this.renameQrSet()); this.dom.querySelector('#qr--set-new').addEventListener('click', async()=>this.addQrSet()); /**@type {HTMLInputElement}*/ const importFile = this.dom.querySelector('#qr--set-importFile'); @@ -303,6 +304,44 @@ export class SettingsUi { this.settings.save(); } + async renameQrSet() { + const newName = await callPopup('Enter new name for the Quick Reply Set:', 'input'); + if (newName && newName.length > 0) { + const existingSet = QuickReplySet.get(newName); + if (existingSet) { + toastr.error(`A Quick Reply Set named "${newName}" already exists.`); + return; + } + const oldName = this.currentQrSet.name; + this.currentQrSet.name = newName; + await this.currentQrSet.save(); + + // Update the option in all select dropdowns + /** @type {NodeListOf} */ + const options = this.dom.querySelectorAll(`#qr--set option[value="${oldName}"], select.qr--set option[value="${oldName}"]`); + options.forEach(option => { + option.value = newName; + option.textContent = newName; + }); + + // Update in in both set lists + this.settings.config.setList.forEach(set => { + if (set.set.name === oldName) { + set.set.name = newName; + } + }); + this.settings.chatConfig?.setList.forEach(set => { + if (set.set.name === oldName) { + set.set.name = newName; + } + }); + + this.settings.save(); + this.currentSet.value = newName; + console.info(`Quick Reply Set renamed from ""${oldName}" to "${newName}".`); + } + } + async addQrSet() { const name = await callPopup('Quick Reply Set Name:', 'input'); if (name && name.length > 0) { From 0f45ebda0376fc26173dae6f36784aced52fe132 Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Sat, 11 Jan 2025 01:59:38 +0100 Subject: [PATCH 2/4] Refactor QR set popups to new popup --- .../extensions/quick-reply/src/ui/SettingsUi.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js b/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js index e655a73f2..de7bc0d7f 100644 --- a/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js +++ b/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js @@ -1,4 +1,4 @@ -import { callPopup } from '../../../../../script.js'; +import { Popup } from '../../../../popup.js'; import { getSortableDelay } from '../../../../utils.js'; import { log, warn } from '../../index.js'; import { QuickReply } from '../QuickReply.js'; @@ -280,7 +280,7 @@ export class SettingsUi { } async deleteQrSet() { - const confirmed = await callPopup(`Are you sure you want to delete the Quick Reply Set "${this.currentQrSet.name}"?
This cannot be undone.`, 'confirm'); + const confirmed = await Popup.show.confirm('Delete Quick Reply Set', `Are you sure you want to delete the Quick Reply Set "${this.currentQrSet.name}"?
This cannot be undone.`); if (confirmed) { await this.doDeleteQrSet(this.currentQrSet); this.rerender(); @@ -305,7 +305,7 @@ export class SettingsUi { } async renameQrSet() { - const newName = await callPopup('Enter new name for the Quick Reply Set:', 'input'); + const newName = await Popup.show.input('Rename Quick Reply Set', 'Enter a new name:', this.currentQrSet.name); if (newName && newName.length > 0) { const existingSet = QuickReplySet.get(newName); if (existingSet) { @@ -343,11 +343,11 @@ export class SettingsUi { } async addQrSet() { - const name = await callPopup('Quick Reply Set Name:', 'input'); + const name = await Popup.show.input('Create a new World Info', 'Enter a name for the new Quick Reply Set:'); if (name && name.length > 0) { const oldQrs = QuickReplySet.get(name); if (oldQrs) { - const replace = await callPopup(`A Quick Reply Set named "${name}" already exists.
Do you want to overwrite the existing Quick Reply Set?
The existing set will be deleted. This cannot be undone.`, 'confirm'); + const replace = Popup.show.confirm('Replace existing World Info', `A Quick Reply Set named "${name}" already exists.
Do you want to overwrite the existing Quick Reply Set?
The existing set will be deleted. This cannot be undone.`); if (replace) { const idx = QuickReplySet.list.indexOf(oldQrs); await this.doDeleteQrSet(oldQrs); @@ -408,7 +408,7 @@ export class SettingsUi { qrs.init(); const oldQrs = QuickReplySet.get(props.name); if (oldQrs) { - const replace = await callPopup(`A Quick Reply Set named "${qrs.name}" already exists.
Do you want to overwrite the existing Quick Reply Set?
The existing set will be deleted. This cannot be undone.`, 'confirm'); + const replace = Popup.show.confirm('Replace existing World Info', `A Quick Reply Set named "${name}" already exists.
Do you want to overwrite the existing Quick Reply Set?
The existing set will be deleted. This cannot be undone.`); if (replace) { const idx = QuickReplySet.list.indexOf(oldQrs); await this.doDeleteQrSet(oldQrs); From 3455da404ac735e94110d57ce11fd102f39dc25f Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Sat, 11 Jan 2025 02:22:10 +0100 Subject: [PATCH 3/4] Add QR set duplicate button --- .../extensions/quick-reply/html/settings.html | 1 + .../quick-reply/src/ui/SettingsUi.js | 37 ++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/public/scripts/extensions/quick-reply/html/settings.html b/public/scripts/extensions/quick-reply/html/settings.html index 3c8100aae..210ad4f32 100644 --- a/public/scripts/extensions/quick-reply/html/settings.html +++ b/public/scripts/extensions/quick-reply/html/settings.html @@ -51,6 +51,7 @@ +
diff --git a/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js b/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js index de7bc0d7f..419275b92 100644 --- a/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js +++ b/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js @@ -120,7 +120,8 @@ export class SettingsUi { importFile.value = null; }); this.dom.querySelector('#qr--set-import').addEventListener('click', ()=>importFile.click()); - this.dom.querySelector('#qr--set-export').addEventListener('click', async()=>this.exportQrSet()); + this.dom.querySelector('#qr--set-export').addEventListener('click', async () => this.exportQrSet()); + this.dom.querySelector('#qr--set-duplicate').addEventListener('click', async () => this.duplicateQrSet()); this.dom.querySelector('#qr--set-delete').addEventListener('click', async()=>this.deleteQrSet()); this.dom.querySelector('#qr--set-add').addEventListener('click', async()=>{ this.currentQrSet.addQuickReply(); @@ -460,6 +461,40 @@ export class SettingsUi { URL.revokeObjectURL(url); } + async duplicateQrSet() { + const newName = await Popup.show.input('Duplicate Quick Reply Set', 'Enter a name for the new Quick Reply Set:', `${this.currentQrSet.name} (Copy)`); + if (newName && newName.length > 0) { + const existingSet = QuickReplySet.get(newName); + if (existingSet) { + toastr.error(`A Quick Reply Set named "${newName}" already exists.`); + return; + } + const newQrSet = QuickReplySet.from(JSON.parse(JSON.stringify(this.currentQrSet))); + newQrSet.name = newName; + newQrSet.qrList = this.currentQrSet.qrList.map(qr => QuickReply.from(JSON.parse(JSON.stringify(qr)))); + newQrSet.addQuickReply(); + const idx = QuickReplySet.list.findIndex(it => it.name.toLowerCase().localeCompare(newName.toLowerCase()) == 1); + if (idx > -1) { + QuickReplySet.list.splice(idx, 0, newQrSet); + } else { + QuickReplySet.list.push(newQrSet); + } + const opt = document.createElement('option'); { + opt.value = newQrSet.name; + opt.textContent = newQrSet.name; + if (idx > -1) { + this.currentSet.children[idx].insertAdjacentElement('beforebegin', opt); + } else { + this.currentSet.append(opt); + } + } + this.currentSet.value = newName; + this.onQrSetChange(); + this.prepareGlobalSetList(); + this.prepareChatSetList(); + } + } + selectQrSet(qrs) { this.currentSet.value = qrs.name; this.onQrSetChange(); From b89d41a70145bcf1ca6dc47d3dd00a21667b3ffe Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Sat, 11 Jan 2025 02:31:14 +0100 Subject: [PATCH 4/4] Refactor prepare/redraw for consistency --- .../quick-reply/src/ui/SettingsUi.js | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js b/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js index 419275b92..df6bb081f 100644 --- a/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js +++ b/public/scripts/extensions/quick-reply/src/ui/SettingsUi.js @@ -317,15 +317,7 @@ export class SettingsUi { this.currentQrSet.name = newName; await this.currentQrSet.save(); - // Update the option in all select dropdowns - /** @type {NodeListOf} */ - const options = this.dom.querySelectorAll(`#qr--set option[value="${oldName}"], select.qr--set option[value="${oldName}"]`); - options.forEach(option => { - option.value = newName; - option.textContent = newName; - }); - - // Update in in both set lists + // Update it in both set lists this.settings.config.setList.forEach(set => { if (set.set.name === oldName) { set.set.name = newName; @@ -336,9 +328,19 @@ export class SettingsUi { set.set.name = newName; } }); - this.settings.save(); + + // Update the option in the current selected QR dropdown. All others will be refreshed via the prepare calls below. + /** @type {HTMLOptionElement} */ + const option = this.currentSet.querySelector(`#qr--set option[value="${oldName}"]`); + option.value = newName; + option.textContent = newName; + this.currentSet.value = newName; + this.onQrSetChange(); + this.prepareGlobalSetList(); + this.prepareChatSetList(); + console.info(`Quick Reply Set renamed from ""${oldName}" to "${newName}".`); } }