-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathcomposerMenuHighlight.ts
More file actions
37 lines (32 loc) · 1.12 KB
/
Copy pathcomposerMenuHighlight.ts
File metadata and controls
37 lines (32 loc) · 1.12 KB
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
export function resolveComposerMenuActiveItemId(input: {
items: ReadonlyArray<{ id: string }>;
highlightedItemId: string | null;
currentSearchKey: string | null;
highlightedSearchKey: string | null;
}): string | null {
if (input.items.length === 0) {
return null;
}
if (
input.currentSearchKey === input.highlightedSearchKey &&
input.highlightedItemId &&
input.items.some((item) => item.id === input.highlightedItemId)
) {
return input.highlightedItemId;
}
return input.items[0]?.id ?? null;
}
export function resolveComposerMenuNudgedItemId(input: {
items: ReadonlyArray<{ id: string }>;
activeItemId: string | null;
direction: "next" | "previous";
}): string | null {
if (input.items.length === 0) {
return null;
}
const activeIndex = input.items.findIndex((item) => item.id === input.activeItemId);
const normalizedIndex = activeIndex >= 0 ? activeIndex : input.direction === "next" ? -1 : 0;
const offset = input.direction === "next" ? 1 : -1;
const nextIndex = (normalizedIndex + offset + input.items.length) % input.items.length;
return input.items[nextIndex]?.id ?? null;
}