From 939d69fe461b1e93f5f452da9ee56fc935bfc438 Mon Sep 17 00:00:00 2001 From: Webifi Date: Mon, 5 Jun 2023 08:51:40 -0500 Subject: [PATCH] Use modal for setting field change confirmations --- src/lib/Chat.svelte | 1 + src/lib/ChatOptionMenu.svelte | 1 + src/lib/ChatSettingField.svelte | 51 +++++++++++++---------- src/lib/ChatSettingsModal.svelte | 1 + src/lib/EditMessage.svelte | 1 + src/lib/PromptConfirm.svelte | 70 ++++++++++++++++++++++++++++++++ src/lib/PromptInput.svelte | 3 +- src/lib/Types.svelte | 4 +- 8 files changed, 109 insertions(+), 23 deletions(-) create mode 100644 src/lib/PromptConfirm.svelte diff --git a/src/lib/Chat.svelte b/src/lib/Chat.svelte index d69d8ac..32e095d 100644 --- a/src/lib/Chat.svelte +++ b/src/lib/Chat.svelte @@ -545,6 +545,7 @@ on:keydown={e => { // Only send if Enter is pressed, not Shift+Enter if (e.key === 'Enter' && !e.shiftKey) { + e.stopPropagation() submitForm() e.preventDefault() } diff --git a/src/lib/ChatOptionMenu.svelte b/src/lib/ChatOptionMenu.svelte index 768611e..8b57c5b 100644 --- a/src/lib/ChatOptionMenu.svelte +++ b/src/lib/ChatOptionMenu.svelte @@ -143,6 +143,7 @@ { if (event.key === 'Escape') { + event.stopPropagation() showChatMenu = false } }} diff --git a/src/lib/ChatSettingField.svelte b/src/lib/ChatSettingField.svelte index 0b40dec..2c4b6a4 100644 --- a/src/lib/ChatSettingField.svelte +++ b/src/lib/ChatSettingField.svelte @@ -6,6 +6,8 @@ import { autoGrowInputOnEvent } from './Util.svelte' // import { replace } from 'svelte-spa-router' import Fa from 'svelte-fa/src/fa.svelte' + import { openModal } from 'svelte-modals' + import PromptConfirm from './PromptConfirm.svelte' export let setting:ChatSetting export let chatSettings:ChatSettings @@ -32,7 +34,8 @@ const settingChecks:Record = { profile: [ { - prompt: 'Unsaved changes to the current profile will be lost.\n Continue?', + title: 'Unsaved Profile Changes', + message: 'Unsaved changes to the current profile will be lost.\n Continue?', checkPrompt: (setting, newVal, oldVal) => { return !!chatSettings.isDirty && newVal !== oldVal }, @@ -113,31 +116,37 @@ const c = checks[i] if (c.passed) continue if (c.checkPrompt(setting, newVal, val)) { - // eventually this needs to be an async call to a confirmation modal where - // "passed", not really being used here, will be reworked to some other - // state to deal with inevitable issues once a non-blocking modal is used. - if (window.confirm(c.prompt)) { - c.passed = true - if (c.onYes && c.onYes(setting, newVal, val)) { - resetSettingCheck(setting.key) - return + openModal(PromptConfirm, { + title: c.title, + message: c.message, + class: c.class || 'is-warning', + onConfirm: () => { + c.passed = true + if (c.onYes && c.onYes(setting, newVal, val)) { + resetSettingCheck(setting.key) + } else { + queueSettingValueChange(event, setting) + } + }, + onCancel: () => { + // roll-back + if (!c.onNo || !c.onNo(setting, newVal, val)) { + resetSettingCheck(setting.key) + setChatSettingValue(chatId, setting, val) + // refresh setting modal, if open + c.onNo && c.onNo(setting, newVal, val) + refreshSettings() + } else { + queueSettingValueChange(event, setting) + } } - } else { - // roll-back - if (!c.onNo || !c.onNo(setting, newVal, val)) { - setChatSettingValue(chatId, setting, val) - // refresh setting modal, if open - c.onNo && c.onNo(setting, newVal, val) - refreshSettings() - resetSettingCheck(setting.key) - return - } - } + }) } else { c.passed = true } } - // passed all + // passed all? + if (checks.find(c => !c.passed)) return resetSettingCheck(setting.key) debounce = setTimeout(doSet, 250) } diff --git a/src/lib/ChatSettingsModal.svelte b/src/lib/ChatSettingsModal.svelte index 79f4904..31304b8 100644 --- a/src/lib/ChatSettingsModal.svelte +++ b/src/lib/ChatSettingsModal.svelte @@ -326,6 +326,7 @@ { if (event.key === 'Escape') { + event.stopPropagation() closeSettings() } }} diff --git a/src/lib/EditMessage.svelte b/src/lib/EditMessage.svelte index ddc199b..38ed667 100644 --- a/src/lib/EditMessage.svelte +++ b/src/lib/EditMessage.svelte @@ -68,6 +68,7 @@ const keydown = (event:KeyboardEvent) => { if (event.key === 'Escape') { + event.stopPropagation() event.preventDefault() message.content = original editing = false diff --git a/src/lib/PromptConfirm.svelte b/src/lib/PromptConfirm.svelte new file mode 100644 index 0000000..cc8ce39 --- /dev/null +++ b/src/lib/PromptConfirm.svelte @@ -0,0 +1,70 @@ + + +{#if isOpen} +