refac
This commit is contained in:
1 parent
5ec16e76e6
commit
b20bcdbba7
3 files changed
+362
-65
No files matched your search
@@ -0,0 +1,274 @@
|
||||
<script lang="ts">
|
||||
import { onDestroy, onMount } from 'svelte';
|
||||
|
||||
const UNSAFE_EDGE = 16;
|
||||
const SWIPE_SLOP = 8;
|
||||
const AXIS_RATIO = 1.2;
|
||||
const FLICK_VELOCITY = 0.55;
|
||||
const SETTLE_MS = 210;
|
||||
|
||||
export let open = false;
|
||||
export let enabled = false;
|
||||
export let width = 245;
|
||||
export let onOpenChange: (open: boolean) => void = () => {};
|
||||
|
||||
let mounted = false;
|
||||
let active = false;
|
||||
let locked = false;
|
||||
let direction: 'open' | 'close' | null = null;
|
||||
let startX = 0;
|
||||
let startY = 0;
|
||||
let lastX = 0;
|
||||
let lastTime = 0;
|
||||
let velocity = 0;
|
||||
let startProgress = 0;
|
||||
let swipeProgress = 0;
|
||||
let settling = false;
|
||||
let settleTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let settleFrame: ReturnType<typeof requestAnimationFrame> | null = null;
|
||||
let scrollLocked = false;
|
||||
let lastOpen: boolean | null = null;
|
||||
let lastEnabled: boolean | null = null;
|
||||
|
||||
$: panelWidth = width || 245;
|
||||
$: progress = enabled ? Math.max(0, Math.min(1, swipeProgress)) : 1;
|
||||
$: visible = open || (enabled && mounted);
|
||||
$: panelStyle = enabled
|
||||
? `transform: translateX(${(progress - 1) * 100}%); transition: ${
|
||||
settling ? `transform ${SETTLE_MS}ms cubic-bezier(0.22, 1, 0.36, 1)` : 'none'
|
||||
};`
|
||||
: '';
|
||||
$: backdropStyle = enabled
|
||||
? `opacity: ${progress * 0.6}; transition: ${
|
||||
settling ? `opacity ${SETTLE_MS}ms cubic-bezier(0.22, 1, 0.36, 1)` : 'none'
|
||||
};`
|
||||
: '';
|
||||
|
||||
$: if (open !== lastOpen || enabled !== lastEnabled) {
|
||||
lastOpen = open;
|
||||
lastEnabled = enabled;
|
||||
|
||||
if (enabled) {
|
||||
animate(open);
|
||||
} else {
|
||||
mounted = false;
|
||||
swipeProgress = open ? 1 : 0;
|
||||
settling = false;
|
||||
setScrollLock(false);
|
||||
}
|
||||
}
|
||||
|
||||
const shouldSkipSwipe = (target: EventTarget | null) => {
|
||||
return (
|
||||
target instanceof Element &&
|
||||
!!target.closest(
|
||||
'input, textarea, select, button, [role="menu"], [contenteditable="true"], [data-sidebar-no-gesture]'
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
const setScrollLock = (locked: boolean) => {
|
||||
if (scrollLocked === locked || typeof document === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
scrollLocked = locked;
|
||||
document.documentElement.classList.toggle('mobile-swipe-panel-active', locked);
|
||||
document.body.classList.toggle('mobile-swipe-panel-active', locked);
|
||||
};
|
||||
|
||||
const clearTimer = () => {
|
||||
if (settleTimer) {
|
||||
clearTimeout(settleTimer);
|
||||
settleTimer = null;
|
||||
}
|
||||
};
|
||||
|
||||
const clearFrame = () => {
|
||||
if (settleFrame) {
|
||||
cancelAnimationFrame(settleFrame);
|
||||
settleFrame = null;
|
||||
}
|
||||
};
|
||||
|
||||
const prefersReducedMotion = () =>
|
||||
typeof window !== 'undefined' &&
|
||||
window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
|
||||
|
||||
const animate = (nextOpen: boolean) => {
|
||||
clearTimer();
|
||||
clearFrame();
|
||||
mounted = nextOpen || mounted;
|
||||
|
||||
const finish = () => {
|
||||
settling = false;
|
||||
mounted = nextOpen;
|
||||
settleTimer = null;
|
||||
};
|
||||
|
||||
const settle = () => {
|
||||
settling = !prefersReducedMotion();
|
||||
swipeProgress = nextOpen ? 1 : 0;
|
||||
settleTimer = setTimeout(finish, settling ? SETTLE_MS : 0);
|
||||
};
|
||||
|
||||
if (nextOpen && swipeProgress <= 0.01) {
|
||||
swipeProgress = 0;
|
||||
settling = false;
|
||||
settleFrame = requestAnimationFrame(() => {
|
||||
settleFrame = null;
|
||||
settle();
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
settle();
|
||||
};
|
||||
|
||||
const resetSwipe = () => {
|
||||
active = false;
|
||||
locked = false;
|
||||
direction = null;
|
||||
velocity = 0;
|
||||
setScrollLock(false);
|
||||
};
|
||||
|
||||
const cancelSwipe = () => {
|
||||
resetSwipe();
|
||||
animate(open);
|
||||
};
|
||||
|
||||
const onTouchStart = (e: TouchEvent) => {
|
||||
if (!enabled || e.touches.length !== 1 || shouldSkipSwipe(e.target)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const touch = e.touches[0];
|
||||
const x = touch.clientX;
|
||||
|
||||
if (!open) {
|
||||
if (x < UNSAFE_EDGE) {
|
||||
return;
|
||||
}
|
||||
|
||||
direction = 'open';
|
||||
startProgress = 0;
|
||||
mounted = true;
|
||||
swipeProgress = 0;
|
||||
} else {
|
||||
direction = 'close';
|
||||
startProgress = swipeProgress || 1;
|
||||
}
|
||||
|
||||
clearTimer();
|
||||
settling = false;
|
||||
active = true;
|
||||
locked = false;
|
||||
startX = x;
|
||||
startY = touch.clientY;
|
||||
lastX = x;
|
||||
lastTime = e.timeStamp;
|
||||
velocity = 0;
|
||||
};
|
||||
|
||||
const onTouchMove = (e: TouchEvent) => {
|
||||
if (!active || !direction || e.touches.length !== 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
const touch = e.touches[0];
|
||||
const dx = touch.clientX - startX;
|
||||
const dy = touch.clientY - startY;
|
||||
const absX = Math.abs(dx);
|
||||
const absY = Math.abs(dy);
|
||||
|
||||
if (!locked) {
|
||||
if ((direction === 'open' && dx > 0) || (direction === 'close' && dx < 0)) {
|
||||
swipeProgress = Math.max(0, Math.min(1, startProgress + dx / panelWidth));
|
||||
}
|
||||
|
||||
if (absX < SWIPE_SLOP && absY < SWIPE_SLOP) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (absY > absX || absX < absY * AXIS_RATIO) {
|
||||
cancelSwipe();
|
||||
return;
|
||||
}
|
||||
|
||||
locked = true;
|
||||
setScrollLock(true);
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
const now = e.timeStamp;
|
||||
const elapsed = Math.max(1, now - lastTime);
|
||||
velocity = (touch.clientX - lastX) / elapsed;
|
||||
lastX = touch.clientX;
|
||||
lastTime = now;
|
||||
|
||||
swipeProgress = Math.max(0, Math.min(1, startProgress + dx / panelWidth));
|
||||
};
|
||||
|
||||
const onTouchEnd = () => {
|
||||
if (!active) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!locked) {
|
||||
cancelSwipe();
|
||||
return;
|
||||
}
|
||||
|
||||
let shouldOpen = direction === 'open' ? swipeProgress > 0.35 : swipeProgress > 0.65;
|
||||
|
||||
if (velocity > FLICK_VELOCITY) {
|
||||
shouldOpen = true;
|
||||
} else if (velocity < -FLICK_VELOCITY) {
|
||||
shouldOpen = false;
|
||||
}
|
||||
|
||||
resetSwipe();
|
||||
animate(shouldOpen);
|
||||
onOpenChange(shouldOpen);
|
||||
};
|
||||
|
||||
const onTouchCancel = () => {
|
||||
if (active) {
|
||||
cancelSwipe();
|
||||
}
|
||||
};
|
||||
|
||||
onMount(() => {
|
||||
window.addEventListener('touchstart', onTouchStart, { passive: true });
|
||||
window.addEventListener('touchmove', onTouchMove, { passive: false });
|
||||
window.addEventListener('touchend', onTouchEnd);
|
||||
window.addEventListener('touchcancel', onTouchCancel);
|
||||
});
|
||||
|
||||
onDestroy(() => {
|
||||
window.removeEventListener('touchstart', onTouchStart);
|
||||
window.removeEventListener('touchmove', onTouchMove);
|
||||
window.removeEventListener('touchend', onTouchEnd);
|
||||
window.removeEventListener('touchcancel', onTouchCancel);
|
||||
clearTimer();
|
||||
clearFrame();
|
||||
setScrollLock(false);
|
||||
});
|
||||
</script>
|
||||
|
||||
<slot {visible} {progress} {panelStyle} {backdropStyle} />
|
||||
|
||||
<style>
|
||||
:global(html),
|
||||
:global(body) {
|
||||
overflow-x: hidden;
|
||||
overscroll-behavior-x: contain;
|
||||
}
|
||||
|
||||
:global(html.mobile-swipe-panel-active),
|
||||
:global(body.mobile-swipe-panel-active) {
|
||||
touch-action: pan-y;
|
||||
}
|
||||
</style>
|
||||
@@ -92,9 +92,13 @@
|
||||
import DropdownMenu from '../common/DropdownMenu.svelte';
|
||||
import CheckIcon from '../icons/Check.svelte';
|
||||
import MoreHorizontalIcon from './Sidebar/icons/MoreHorizontal.svelte';
|
||||
import MobileSwipePanel from '../common/MobileSwipePanel.svelte';
|
||||
|
||||
const BREAKPOINT = 768;
|
||||
const DEFAULT_PINNED_ITEMS = ['notes', 'workspace'];
|
||||
const MOBILE_SIDEBAR_SETTLE_MS = 210;
|
||||
const sidebarMountTransition = (node: Element, params: Record<string, unknown>) =>
|
||||
$mobile ? { duration: 0, css: () => '' } : slide(node, params);
|
||||
|
||||
let scrollTop = 0;
|
||||
|
||||
@@ -554,32 +558,6 @@
|
||||
draggedOver = false; // Reset draggedOver status after drop
|
||||
};
|
||||
|
||||
let touchstart;
|
||||
let touchend;
|
||||
|
||||
function checkDirection() {
|
||||
const screenWidth = window.innerWidth;
|
||||
const swipeDistance = Math.abs(touchend.screenX - touchstart.screenX);
|
||||
if (touchstart.clientX < 40 && swipeDistance >= screenWidth / 8) {
|
||||
if (touchend.screenX < touchstart.screenX) {
|
||||
showSidebar.set(false);
|
||||
}
|
||||
if (touchend.screenX > touchstart.screenX) {
|
||||
showSidebar.set(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const onTouchStart = (e) => {
|
||||
touchstart = e.changedTouches[0];
|
||||
console.log(touchstart.clientX);
|
||||
};
|
||||
|
||||
const onTouchEnd = (e) => {
|
||||
touchend = e.changedTouches[0];
|
||||
checkDirection();
|
||||
};
|
||||
|
||||
const onKeyDown = (e) => {
|
||||
if (e.key === 'Shift') {
|
||||
shiftKey = true;
|
||||
@@ -599,6 +577,21 @@
|
||||
selectedChatId = null;
|
||||
};
|
||||
|
||||
const focusChatInputSoon = () => {
|
||||
setTimeout(() => {
|
||||
document.getElementById('chat-input')?.focus({ preventScroll: true });
|
||||
}, MOBILE_SIDEBAR_SETTLE_MS);
|
||||
};
|
||||
|
||||
const closeMobileSidebar = (focusChatInput = false) => {
|
||||
if ($mobile) {
|
||||
showSidebar.set(false);
|
||||
if (focusChatInput) {
|
||||
focusChatInputSoon();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const MIN_WIDTH = 220;
|
||||
const MAX_WIDTH = 480;
|
||||
|
||||
@@ -701,9 +694,6 @@
|
||||
window.addEventListener('keydown', onKeyDown);
|
||||
window.addEventListener('keyup', onKeyUp);
|
||||
|
||||
window.addEventListener('touchstart', onTouchStart);
|
||||
window.addEventListener('touchend', onTouchEnd);
|
||||
|
||||
window.addEventListener('focus', onFocus);
|
||||
window.addEventListener('blur', onBlur);
|
||||
|
||||
@@ -739,9 +729,6 @@
|
||||
window.removeEventListener('keydown', onKeyDown);
|
||||
window.removeEventListener('keyup', onKeyUp);
|
||||
|
||||
window.removeEventListener('touchstart', onTouchStart);
|
||||
window.removeEventListener('touchend', onTouchEnd);
|
||||
|
||||
window.removeEventListener('focus', onFocus);
|
||||
window.removeEventListener('blur', onBlur);
|
||||
|
||||
@@ -811,27 +798,20 @@
|
||||
const newChatHandler = async () => {
|
||||
selectedChatId = null;
|
||||
selectedFolder.set(null);
|
||||
closeMobileSidebar(true);
|
||||
|
||||
if ($user?.role !== 'admin' && $user?.permissions?.chat?.temporary_enforced) {
|
||||
await temporaryChatEnabled.set(true);
|
||||
} else {
|
||||
await temporaryChatEnabled.set(false);
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
if ($mobile) {
|
||||
showSidebar.set(false);
|
||||
}
|
||||
}, 0);
|
||||
};
|
||||
|
||||
const itemClickHandler = async () => {
|
||||
selectedChatId = null;
|
||||
chatId.set('');
|
||||
|
||||
if ($mobile) {
|
||||
showSidebar.set(false);
|
||||
}
|
||||
closeMobileSidebar();
|
||||
|
||||
await tick();
|
||||
};
|
||||
@@ -887,17 +867,41 @@
|
||||
}}
|
||||
/>
|
||||
|
||||
<svelte:window
|
||||
on:mousemove={(e) => {
|
||||
if (!isResizing) return;
|
||||
resizeSidebarHandler(e.clientX);
|
||||
}}
|
||||
on:mouseup={() => {
|
||||
resizeEndHandler();
|
||||
}}
|
||||
/>
|
||||
|
||||
<MobileSwipePanel
|
||||
open={$showSidebar}
|
||||
enabled={$mobile}
|
||||
width={$sidebarWidth}
|
||||
onOpenChange={(open) => showSidebar.set(open)}
|
||||
let:visible
|
||||
let:progress
|
||||
let:panelStyle
|
||||
let:backdropStyle
|
||||
>
|
||||
<!-- svelte-ignore a11y-no-static-element-interactions -->
|
||||
|
||||
{#if $showSidebar}
|
||||
{#if visible}
|
||||
<div
|
||||
class=" {$isApp
|
||||
? ' ml-[4.5rem] md:ml-0'
|
||||
: ''} fixed md:hidden z-40 top-0 right-0 left-0 bottom-0 bg-black/60 w-full min-h-screen h-screen flex justify-center overflow-hidden overscroll-contain"
|
||||
: ''} fixed md:hidden z-40 top-0 right-0 left-0 bottom-0 bg-black w-full min-h-screen h-screen flex justify-center overflow-hidden overscroll-contain {progress >
|
||||
0.02
|
||||
? ''
|
||||
: 'pointer-events-none'}"
|
||||
style={backdropStyle}
|
||||
on:mousedown={() => {
|
||||
showSidebar.set(!$showSidebar);
|
||||
showSidebar.set(false);
|
||||
}}
|
||||
/>
|
||||
></div>
|
||||
{/if}
|
||||
|
||||
<SearchModal
|
||||
@@ -918,16 +922,6 @@
|
||||
}}
|
||||
/>
|
||||
|
||||
<svelte:window
|
||||
on:mousemove={(e) => {
|
||||
if (!isResizing) return;
|
||||
resizeSidebarHandler(e.clientX);
|
||||
}}
|
||||
on:mouseup={() => {
|
||||
resizeEndHandler();
|
||||
}}
|
||||
/>
|
||||
|
||||
{#if !$mobile && !$showSidebar}
|
||||
<div
|
||||
class="w-[calc(42px*var(--app-text-scale,1))] shrink-0 py-[calc(0.25rem*var(--app-text-scale,1))] px-[calc(0.25rem*var(--app-text-scale,1))] flex flex-col justify-between text-gray-700 dark:text-gray-300 hover:bg-gray-50/30 dark:hover:bg-gray-800/30 h-full z-10 transition-all border-e-[0.5px] border-gray-50 dark:border-gray-850/30"
|
||||
@@ -1106,23 +1100,26 @@
|
||||
<!-- {$i18n.t('New Folder')} -->
|
||||
<!-- {$i18n.t('Pinned')} -->
|
||||
|
||||
{#if $showSidebar}
|
||||
{#if visible}
|
||||
<div
|
||||
bind:this={navElement}
|
||||
id="sidebar"
|
||||
role="navigation"
|
||||
aria-label={$i18n.t('Chat history')}
|
||||
class="h-screen max-h-[100dvh] min-h-screen select-none {$showSidebar
|
||||
class="h-screen max-h-[100dvh] min-h-screen select-none {visible
|
||||
? `${$mobile ? 'bg-gray-50 dark:bg-gray-950' : 'bg-gray-50/70 dark:bg-gray-950/70'} z-50`
|
||||
: ' bg-transparent z-0 '} {$isApp
|
||||
? `ml-[4.5rem] md:ml-0 `
|
||||
: $mobile
|
||||
? ''
|
||||
: ' transition-all duration-300 '} shrink-0 text-gray-700 dark:text-gray-300 text-[0.8125rem] leading-5 fixed top-0 left-0 overflow-x-hidden
|
||||
"
|
||||
transition:slide={{ duration: 250, axis: 'x' }}
|
||||
style={panelStyle}
|
||||
transition:sidebarMountTransition={{ duration: 250, axis: 'x' }}
|
||||
data-state={$showSidebar}
|
||||
>
|
||||
<div
|
||||
class=" my-auto flex flex-col justify-between h-screen max-h-[100dvh] w-[var(--sidebar-width)] overflow-x-hidden scrollbar-hidden z-50 border-e border-gray-50 dark:border-gray-850/30 {$showSidebar
|
||||
class=" my-auto flex flex-col justify-between h-screen max-h-[100dvh] w-[var(--sidebar-width)] overflow-x-hidden scrollbar-hidden z-50 border-e border-gray-50 dark:border-gray-850/30 {visible
|
||||
? ''
|
||||
: 'invisible'}"
|
||||
>
|
||||
@@ -1272,7 +1269,9 @@
|
||||
</div>
|
||||
|
||||
<div class="flex self-center translate-y-[0.5px]">
|
||||
<div class=" self-center text-[0.8125rem] leading-5">{$i18n.t(meta.label)}</div>
|
||||
<div class=" self-center text-[0.8125rem] leading-5">
|
||||
{$i18n.t(meta.label)}
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
@@ -1438,12 +1437,14 @@
|
||||
if (chat) {
|
||||
console.log(chat);
|
||||
if (chat.folder_id) {
|
||||
const res = await updateChatFolderIdById(localStorage.token, chat.id, null).catch(
|
||||
(error) => {
|
||||
const res = await updateChatFolderIdById(
|
||||
localStorage.token,
|
||||
chat.id,
|
||||
null
|
||||
).catch((error) => {
|
||||
toast.error(`${error}`);
|
||||
return null;
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
folderRegistry[chat.folder_id]?.setFolderItems();
|
||||
}
|
||||
@@ -1547,7 +1548,10 @@
|
||||
}
|
||||
|
||||
if (!chat.pinned) {
|
||||
const res = await toggleChatPinnedStatusById(localStorage.token, chat.id);
|
||||
const res = await toggleChatPinnedStatusById(
|
||||
localStorage.token,
|
||||
chat.id
|
||||
);
|
||||
}
|
||||
|
||||
initChatList();
|
||||
@@ -1739,3 +1743,13 @@
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
</MobileSwipePanel>
|
||||
|
||||
<style>
|
||||
@media (max-width: 767px) {
|
||||
#sidebar {
|
||||
will-change: transform;
|
||||
touch-action: pan-y;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -58,6 +58,8 @@
|
||||
import { createMessagesList } from '$lib/utils';
|
||||
import { getOutputText } from '$lib/components/chat/Messages/structuredOutput';
|
||||
|
||||
const MOBILE_SIDEBAR_SETTLE_MS = 210;
|
||||
|
||||
const i18n = getContext('i18n');
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
@@ -111,6 +113,12 @@
|
||||
}
|
||||
};
|
||||
|
||||
const focusChatInputSoon = () => {
|
||||
setTimeout(() => {
|
||||
document.getElementById('chat-input')?.focus({ preventScroll: true });
|
||||
}, MOBILE_SIDEBAR_SETTLE_MS);
|
||||
};
|
||||
|
||||
$: if (openPreview && closeActiveHoverPreview !== closeHoverPreview) {
|
||||
closeActiveHoverPreview?.();
|
||||
closeActiveHoverPreview = closeHoverPreview;
|
||||
@@ -569,6 +577,7 @@
|
||||
|
||||
if ($mobile) {
|
||||
showSidebar.set(false);
|
||||
focusChatInputSoon();
|
||||
}
|
||||
|
||||
// Optimistically mark as read in UI when clicked
|
||||
|
||||
Reference in new issue
Block a user