From 00a7c6e6846e5d38c9e03e355a63b5974ce7c986 Mon Sep 17 00:00:00 2001 From: Ian Wijma Date: Mon, 27 Nov 2023 23:59:41 +1100 Subject: [PATCH] Add dialog functionality and improve the favourite feature Added a new dialog.js file to handle dialog based interactions. The favourite feature has been enhanced, removing the need for users to manually enter the path while adding elements to favourites. Instead, a dialog box opens for users to select the directory. Furthermore, a confirmation is now prompted when users try to remove a favourite, improving UX and preventing accidental deletions. These changes were reflected in index.html and favourites.js. --- src/index.html | 1 - src/libs/dialog.js | 9 +++++++++ src/parts/favourites.js | 20 ++++++++++++-------- 3 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 src/libs/dialog.js diff --git a/src/index.html b/src/index.html index 7e68dd6..aeeb4ac 100644 --- a/src/index.html +++ b/src/index.html @@ -28,7 +28,6 @@
-
diff --git a/src/libs/dialog.js b/src/libs/dialog.js new file mode 100644 index 0000000..a85d44a --- /dev/null +++ b/src/libs/dialog.js @@ -0,0 +1,9 @@ +import {tauriDialog} from "./tauri.js"; + +export const { + ask, + confirm, + message, + open, + save, +} = tauriDialog; \ No newline at end of file diff --git a/src/parts/favourites.js b/src/parts/favourites.js index c3cf51d..c46e97a 100644 --- a/src/parts/favourites.js +++ b/src/parts/favourites.js @@ -2,6 +2,8 @@ import {getElements, getOneElementOrThrow} from "../libs/element.js"; import {addFavourite, getFavourites, removeFavouriteByIndex} from "../libs/config.js"; import {getTemplate} from "../libs/template.js"; import {navigate} from "../libs/navigation.js"; +import {ask, open} from "../libs/dialog.js"; +import {basename} from "../libs/path.js"; export const renderFavourite = () => { const favouriteList = getOneElementOrThrow(document, '[data-ui=favourites]'); @@ -20,10 +22,13 @@ export const renderFavourite = () => { if (system) { removeEl.remove(); } else { - removeEl.addEventListener('click', () => { - removeFavouriteByIndex(index); + removeEl.addEventListener('click', async () => { + const confirm = await ask(`Remove ${name} from favourites?`); + if (confirm) { + removeFavouriteByIndex(index); - renderFavourite(); + renderFavourite(); + } }); } @@ -37,18 +42,18 @@ export const renderFavourite = () => { */ const dialog = getOneElementOrThrow(document, '[data-ui=favourite-dialog]'); dialog.close(); - addEl.addEventListener('click', () => { + addEl.addEventListener('click', async () => { const nameEl = getOneElementOrThrow(dialog, '[data-ui=favourite-name]'); - const pathEl = getOneElementOrThrow(dialog, '[data-ui=favourite-path]'); const submitEl = getOneElementOrThrow(dialog, '[data-ui=favourite-submit]'); const closeEl = getOneElementOrThrow(dialog, '[data-ui=favourite-close]'); + const path = await open({ directory: true }) + nameEl.value = await basename(path); + dialog.showModal(); const handleClose = () => { nameEl.value = ''; - pathEl.value = ''; - dialog.close(); } @@ -56,7 +61,6 @@ export const renderFavourite = () => { submitEl.addEventListener('click', () => { const { value: name } = nameEl; - const { value: path } = pathEl; if (name.trim() && path.trim()) { const favourites = getFavourites();