char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Stores architecture improvements (#26910)094e53db1

char/aliceai-t5-moe
1.6 KiB76 linesraw
1/**
2 * Drag-and-drop state machine for the ChatScreen.
3 *
4 * Tracks pointer enter/leave nesting so the overlay stays visible while the
5 * cursor traverses child elements, then routes the dropped files either to
6 * the active message-edit handler (if a message is being edited) or to the
7 * caller's onDrop callback.
8 */
9
10import { chatStore } from '$lib/stores';
11
12interface UseChatScreenDragAndDropOptions {
13	/** Called when the user drops files and no message is being edited. */
14	onDrop: (files: File[]) => void;
15}
16
17export function useChatScreenDragAndDrop(options: UseChatScreenDragAndDropOptions) {
18	let dragCounter = $state(0);
19	let isDragOver = $state(false);
20
21	function handleDragEnter(event: DragEvent) {
22		event.preventDefault();
23		dragCounter++;
24
25		if (event.dataTransfer?.types.includes('Files')) {
26			isDragOver = true;
27		}
28	}
29
30	function handleDragLeave(event: DragEvent) {
31		event.preventDefault();
32		dragCounter--;
33
34		if (dragCounter === 0) {
35			isDragOver = false;
36		}
37	}
38
39	function handleDragOver(event: DragEvent) {
40		event.preventDefault();
41	}
42
43	async function handleDrop(event: DragEvent) {
44		event.preventDefault();
45		isDragOver = false;
46		dragCounter = 0;
47
48		if (!event.dataTransfer?.files) return;
49
50		const files = Array.from(event.dataTransfer.files);
51
52		if (chatStore.isEditing()) {
53			const handler = chatStore.getAddFilesHandler();
54
55			if (handler) {
56				handler(files);
57
58				return;
59			}
60		}
61
62		options.onDrop(files);
63	}
64
65	return {
66		dragHandlers: {
67			dragenter: handleDragEnter,
68			dragleave: handleDragLeave,
69			dragover: handleDragOver,
70			drop: handleDrop
71		},
72		get isDragOver() {
73			return isDragOver;
74		}
75	};
76}