File size: 7,185 Bytes
82be208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
 * Reusable selection state-machine: shift+click/shift+drag range select plus
 * rubber-band marquee drag, anchored on the last clicked row. Both the sidebar
 * conversation list and the dialog conversations table share this.
 *
 * The hook mutates the consumer's SvelteSet<string> directly; the consumer
 * owns the source of truth and reads it like any other $state. orderedIds
 * must reflect the current visual order of selectable rows so the range
 * matches what the user sees on screen.
 */

import { UI_DATA_ATTRS } from '$lib/constants';
import { SvelteSet } from 'svelte/reactivity';

interface UseMarqueeSelectionOptions {
	/** Latest selected-IDs set. Re-read per selection event so consumer-side reassignment works. */
	selectedIds: () => SvelteSet<string>;
	/** IDs in the current rendered order; used to compute shift+click ranges and gate marquee visibility. */
	orderedIds: () => string[];
	/** Document listeners attach only while the getter returns true. */
	enabled: () => boolean;
	/** Full `data-*` attribute that marks selectable rows. */
	dataAttr?: () => string;
	/** Minimum pixel distance before a press becomes a marquee drag. */
	dragThresholdPx?: number;
}

export function useMarqueeSelection(options: UseMarqueeSelectionOptions) {
	const dragThresholdPx = options.dragThresholdPx ?? 5;

	let dragAnchorId = $state<string | null>(null);
	let isMarqueeDragging = $state(false);
	let mouseDownActive = false;
	let dragStartX = 0;
	let dragStartY = 0;
	let mousedownRowId: string | null = null;
	let dragMode: 'add' | 'remove' | null = null;
	let suppressNextClick = false;

	function resolveDataAttr(): string {
		return options.dataAttr?.() ?? UI_DATA_ATTRS.CONVERSATION_ROW;
	}

	function decideDragMode(startingRowId: string | null, currentlySelected: ReadonlySet<string>) {
		return startingRowId !== null && currentlySelected.has(startingRowId) ? 'remove' : 'add';
	}

	/**
	 * Range-select uses Finder-style toggle-by-target semantics: if the target
	 * row is currently selected the range becomes deselected, otherwise it
	 * becomes selected. Anchor moves to `toId` so chained shift+clicks keep
	 * extending from the previous endpoint.
	 */
	function rangeSelect(fromId: string, toId: string) {
		const selected = options.selectedIds();
		const order = options.orderedIds();
		const fromIdx = order.indexOf(fromId);
		const toIdx = order.indexOf(toId);

		if (fromIdx === -1 || toIdx === -1) return;

		const [lo, hi] = fromIdx < toIdx ? [fromIdx, toIdx] : [toIdx, fromIdx];
		const shouldSelect = !selected.has(toId);

		for (let i = lo; i <= hi; i++) {
			const id = order[i];

			if (shouldSelect) selected.add(id);
			else selected.delete(id);
		}
	}

	function findRowAtPoint(x: number, y: number): string | null {
		const attr = resolveDataAttr();
		const selector = `[${attr}]`;

		let bestMatch: HTMLElement | null = null;
		let bestCenterDistance = Infinity;

		for (const row of document.querySelectorAll<HTMLElement>(selector)) {
			const rect = row.getBoundingClientRect();

			if (y >= rect.top && y <= rect.bottom && x >= rect.left && x <= rect.right) {
				return row.getAttribute(attr);
			}

			if (x >= rect.left && x <= rect.right) {
				const centerDistance = Math.abs(y - (rect.top + rect.height / 2));

				if (centerDistance < bestCenterDistance) {
					bestCenterDistance = centerDistance;
					bestMatch = row;
				}
			}
		}

		return bestMatch ? bestMatch.getAttribute(attr) : null;
	}

	function updateMarqueeRect(currentX: number, currentY: number) {
		const attr = resolveDataAttr();
		const selector = `[${attr}]`;
		const selected = options.selectedIds();
		const left = Math.min(dragStartX, currentX);
		const top = Math.min(dragStartY, currentY);
		const right = Math.max(dragStartX, currentX);
		const bottom = Math.max(dragStartY, currentY);
		const visibleIds = new SvelteSet(options.orderedIds());

		for (const row of document.querySelectorAll<HTMLElement>(selector)) {
			const id = row.getAttribute(attr);

			if (!id || !visibleIds.has(id)) continue;

			const rect = row.getBoundingClientRect();
			const intersects = !(
				rect.right < left ||
				rect.left > right ||
				rect.bottom < top ||
				rect.top > bottom
			);

			if (dragMode === 'add') {
				if (intersects) selected.add(id);
			} else if (dragMode === 'remove') {
				if (intersects && selected.has(id)) selected.delete(id);
			}
		}
	}

	function handleDocumentMouseMove(event: MouseEvent) {
		if (!mouseDownActive) return;

		if (event.shiftKey && dragAnchorId !== null) {
			const target = findRowAtPoint(event.clientX, event.clientY);

			if (target && target !== mousedownRowId) rangeSelect(dragAnchorId, target);

			return;
		}

		if (!isMarqueeDragging) {
			const dx = event.clientX - dragStartX;
			const dy = event.clientY - dragStartY;

			if (Math.hypot(dx, dy) < dragThresholdPx) return;

			isMarqueeDragging = true;
			dragMode = decideDragMode(mousedownRowId, options.selectedIds());
		}

		updateMarqueeRect(event.clientX, event.clientY);
	}

	function handleDocumentMouseUp(event: MouseEvent) {
		if (isMarqueeDragging) {
			suppressNextClick = true;
			const target = findRowAtPoint(event.clientX, event.clientY);

			if (target) dragAnchorId = target;
		}

		isMarqueeDragging = false;
		mouseDownActive = false;
		mousedownRowId = null;
		dragMode = null;
		dragStartX = 0;
		dragStartY = 0;
	}

	function handleClickCapture(event: MouseEvent) {
		if (suppressNextClick) {
			event.stopPropagation();
			event.preventDefault();
			suppressNextClick = false;
		}
	}

	$effect(() => {
		if (!options.enabled()) {
			reset();

			return;
		}

		document.addEventListener('mousemove', handleDocumentMouseMove);
		document.addEventListener('mouseup', handleDocumentMouseUp);
		document.addEventListener('click', handleClickCapture, { capture: true });

		return () => {
			document.removeEventListener('mousemove', handleDocumentMouseMove);
			document.removeEventListener('mouseup', handleDocumentMouseUp);
			document.removeEventListener('click', handleClickCapture, { capture: true });
		};
	});

	function rowMouseDown(id: string, event: MouseEvent) {
		if (!options.enabled()) return;

		if (event.button !== 0) return;

		event.preventDefault();
		mouseDownActive = true;
		mousedownRowId = id;
		dragStartX = event.clientX;
		dragStartY = event.clientY;
		isMarqueeDragging = false;
		dragMode = null;
	}

	function rowClick(id: string, shiftKey: boolean) {
		if (!options.enabled()) return;

		const selected = options.selectedIds();

		if (shiftKey) {
			const anchor = dragAnchorId;

			if (anchor !== null && anchor !== id) {
				rangeSelect(anchor, id);
			} else if (selected.has(id)) {
				selected.delete(id);
			} else {
				selected.add(id);
			}

			dragAnchorId = id;

			return;
		}

		if (selected.has(id)) selected.delete(id);
		else selected.add(id);

		dragAnchorId = id;
	}

	function reset() {
		dragAnchorId = null;
		isMarqueeDragging = false;
		mouseDownActive = false;
		suppressNextClick = false;
		mousedownRowId = null;
		dragMode = null;
		dragStartX = 0;
		dragStartY = 0;
	}

	return {
		get dragAnchorId() {
			return dragAnchorId;
		},
		reset,
		rowClick,
		rowMouseDown
	};
}