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

	interface Props {
		id: number;
		index: number;
		edit?: ((id: number) => void) | undefined;
		children?: Snippet<[{ onclick: (event: MouseEvent) => void; selected: boolean }]>;
	}

	let { id, index, edit = undefined, children }: Props = $props();
	let selection = getSelectionContext();

	const onclick = (event: MouseEvent) => {
		if (selection.active) {
			selection.update(index, event.shiftKey);
			event.preventDefault();
		} else if (edit) {
			edit(id);
			event.preventDefault();
		}
	};
</script>

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