File size: 1,461 Bytes
0bd62e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
function positive_mod(n: number, m: number): number {
	return ((n % m) + m) % m;
}

export function handle_filter(

	choices: [string, string | number][],

	input_text: string

): number[] {
	return choices.reduce((filtered_indices, o, index) => {
		if (
			input_text ? o[0].toLowerCase().includes(input_text.toLowerCase()) : true
		) {
			filtered_indices.push(index);
		}
		return filtered_indices;
	}, [] as number[]);
}

export function handle_change(

	dispatch: any,

	value: string | number | (string | number)[] | undefined,

	value_is_output: boolean

): void {
	dispatch("change", value);
	if (!value_is_output) {
		dispatch("input");
	}
}

export function handle_shared_keys(

	e: KeyboardEvent,

	active_index: number | null,

	filtered_indices: number[]

): [boolean, number | null] {
	if (e.key === "Escape") {
		return [false, active_index];
	}
	if (e.key === "ArrowDown" || e.key === "ArrowUp") {
		if (filtered_indices.length >= 0) {
			if (active_index === null) {
				active_index =
					e.key === "ArrowDown"
						? filtered_indices[0]
						: filtered_indices[filtered_indices.length - 1];
			} else {
				const index_in_filtered = filtered_indices.indexOf(active_index);
				const increment = e.key === "ArrowUp" ? -1 : 1;
				active_index =
					filtered_indices[
						positive_mod(index_in_filtered + increment, filtered_indices.length)
					];
			}
		}
	}
	return [true, active_index];
}