summaryrefslogtreecommitdiffstatshomepage
path: root/frontend/src/lib/selection/Selectable.svelte
blob: 439d6b721dc3e2f413e792501200058f2eb5ba87 (plain) (blame)
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
<script lang="ts">
	import type { Snippet } from 'svelte';
	import { getSelectionContext } from './Selection.svelte';

	interface SnippetProps {
		onclick: (event: MouseEvent) => void;
		onauxclick: (event: MouseEvent) => void;
		selected: boolean;
	}

	interface Props {
		id: number;
		index: number;
		onclick?: (id: number) => void;
		onauxclick?: (id: number) => void;
		children?: Snippet<[SnippetProps]>;
	}

	let {
		id,
		index,
		onclick: onclick = undefined,
		onauxclick = undefined,
		children
	}: Props = $props();

	let selection = getSelectionContext();

	const click = (event: MouseEvent) => {
		if (selection.active) {
			selection.update(index, event.shiftKey);
			event.preventDefault();
		} else if (event.ctrlKey && onauxclick) {
			onauxclick(id);
		} else if (onclick) {
			onclick(id);
			event.preventDefault();
		}
	};

	const auxclick = (event: MouseEvent) => {
		if (event.button === 1 && onauxclick) {
			onauxclick(id);
		}
	};
</script>

{@render children?.({ onclick: click, onauxclick: auxclick, selected: selection.contains(id) })}