From 2f4c04055ce05f0610a949b5bcd6d056dc4f781a Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 15 Oct 2024 02:12:39 -0700 Subject: [PATCH] enh: drag and drop chat to pin --- src/lib/components/common/DragGhost.svelte | 7 +- src/lib/components/common/Folder.svelte | 120 +++++++++----- src/lib/components/layout/Sidebar.svelte | 153 +++++++++++------- .../components/layout/Sidebar/ChatItem.svelte | 16 +- 4 files changed, 195 insertions(+), 101 deletions(-) diff --git a/src/lib/components/common/DragGhost.svelte b/src/lib/components/common/DragGhost.svelte index a7f9c2934..6d97a4517 100644 --- a/src/lib/components/common/DragGhost.svelte +++ b/src/lib/components/common/DragGhost.svelte @@ -22,8 +22,9 @@
- +
+ +
diff --git a/src/lib/components/common/Folder.svelte b/src/lib/components/common/Folder.svelte index a9883520e..fe356c187 100644 --- a/src/lib/components/common/Folder.svelte +++ b/src/lib/components/common/Folder.svelte @@ -1,5 +1,5 @@ -
- { - dispatch('change', e.detail); - }} - > - +
+ {#if dragged}
{ - e.stopPropagation(); + class="absolute top-0 left-0 w-full h-full rounded-sm bg-gray-200 bg-opacity-50 dark:bg-opacity-10 z-50 pointer-events-none touch-none" + >
+ {/if} + + {#if collapsible} + { + dispatch('change', e.detail); }} - on:drop={(e) => { - console.log('Dropped on the Button'); - }} - on:dragleave={(e) => {}} > - -
+
+ {name} +
+ +
-
- -
- +
+ +
+ + {:else} + + {/if} diff --git a/src/lib/components/layout/Sidebar.svelte b/src/lib/components/layout/Sidebar.svelte index 8372d77b1..c3d310320 100644 --- a/src/lib/components/layout/Sidebar.svelte +++ b/src/lib/components/layout/Sidebar.svelte @@ -35,7 +35,9 @@ cloneChatById, getChatListBySearchText, createNewChat, - getPinnedChatList + getPinnedChatList, + toggleChatPinnedStatusById, + getChatPinnedStatusById } from '$lib/apis/chats'; import { WEBUI_BASE_URL } from '$lib/constants'; @@ -363,8 +365,8 @@ bind:this={navElement} id="sidebar" class="h-screen max-h-[100dvh] min-h-screen select-none {$showSidebar - ? 'md:relative w-[260px]' - : '-translate-x-[260px] w-[0px]'} bg-gray-50 text-gray-900 dark:bg-gray-950 dark:text-gray-200 text-sm transition fixed z-50 top-0 left-0 + ? 'md:relative w-[260px] max-w-[260px]' + : '-translate-x-[260px] w-[0px]'} bg-gray-50 text-gray-900 dark:bg-gray-950 dark:text-gray-200 text-sm transition fixed z-50 top-0 left-0 overflow-x-hidden " data-state={$showSidebar} > @@ -381,7 +383,7 @@ {/if}
@@ -517,13 +519,27 @@ {/if} {#if !search && $pinnedChats.length > 0} -
+
{ localStorage.setItem('showPinnedChat', e.detail); console.log(e.detail); }} + on:drop={async (e) => { + const { id } = e.detail; + + const status = await getChatPinnedStatusById(localStorage.token, id); + + if (!status) { + const res = await toggleChatPinnedStatusById(localStorage.token, id); + + if (res) { + await pinnedChats.set(await getPinnedChatList(localStorage.token)); + initChatList(); + } + } + }} name={$i18n.t('Pinned')} >
@@ -557,17 +573,36 @@
{/if} -
- {#if $chats} - {#each $chats as chat, idx} - {#if idx === 0 || (idx > 0 && chat.time_range !== $chats[idx - 1].time_range)} -
- {$i18n.t(chat.time_range)} - -
- {/if} +
+ {/if} - { - selectedChatId = chat.id; - }} - on:unselect={() => { - selectedChatId = null; - }} - on:delete={(e) => { - if ((e?.detail ?? '') === 'shift') { - deleteChatHandler(chat.id); - } else { - deleteChat = chat; - showDeleteConfirm = true; - } - }} - on:tag={(e) => { - const { type, name } = e.detail; - tagEventHandler(type, name, chat.id); - }} - /> - {/each} + { + selectedChatId = chat.id; + }} + on:unselect={() => { + selectedChatId = null; + }} + on:delete={(e) => { + if ((e?.detail ?? '') === 'shift') { + deleteChatHandler(chat.id); + } else { + deleteChat = chat; + showDeleteConfirm = true; + } + }} + on:tag={(e) => { + const { type, name } = e.detail; + tagEventHandler(type, name, chat.id); + }} + /> + {/each} - {#if $scrollPaginationEnabled && !allChatsLoaded} - { - if (!chatListLoading) { - loadMoreChats(); - } - }} - > + {#if $scrollPaginationEnabled && !allChatsLoaded} + { + if (!chatListLoading) { + loadMoreChats(); + } + }} + > +
+ +
Loading...
+
+
+ {/if} + {:else}
Loading...
-
- {/if} - {:else} -
- -
Loading...
+ {/if}
- {/if} +
diff --git a/src/lib/components/layout/Sidebar/ChatItem.svelte b/src/lib/components/layout/Sidebar/ChatItem.svelte index 7682dd3c5..a6ce47e5f 100644 --- a/src/lib/components/layout/Sidebar/ChatItem.svelte +++ b/src/lib/components/layout/Sidebar/ChatItem.svelte @@ -104,6 +104,14 @@ const onDragStart = (event) => { event.dataTransfer.setDragImage(dragImage, 0, 0); + // Set the data to be transferred + event.dataTransfer.setData( + 'text/plain', + JSON.stringify({ + id: chat.id + }) + ); + drag = true; itemElement.style.opacity = '0.5'; // Optional: Visual cue to show it's being dragged }; @@ -114,8 +122,8 @@ }; const onDragEnd = (event) => { - drag = false; itemElement.style.opacity = '1'; // Reset visual cue after drag + drag = false; }; onMount(() => { @@ -142,9 +150,9 @@ {#if drag && x && y} -
+
-
+
{chat.title}
@@ -169,7 +177,7 @@
{:else}