UNPKG

1.32 kBPlain TextView Raw
1import { DayFlag } from "../UI.js";
2import type { CalendarDay } from "../classes/index.js";
3import type { Modifiers } from "../types/index.js";
4
5export function calculateFocusTarget(
6 days: CalendarDay[],
7 getModifiers: (day: CalendarDay) => Modifiers,
8 isSelected: (date: Date) => boolean,
9 lastFocused: CalendarDay | undefined
10) {
11 let focusTarget: CalendarDay | undefined;
12
13 let index = 0;
14 let found = false;
15
16 while (index < days.length && !found) {
17 const day = days[index];
18 const modifiers = getModifiers(day);
19
20 if (
21 !modifiers[DayFlag.disabled] &&
22 !modifiers[DayFlag.hidden] &&
23 !modifiers[DayFlag.outside]
24 ) {
25 if (modifiers[DayFlag.focused]) {
26 focusTarget = day;
27 found = true;
28 } else if (lastFocused?.isEqualTo(day)) {
29 focusTarget = day;
30 found = true;
31 } else if (isSelected(day.date)) {
32 focusTarget = day;
33 found = true;
34 } else if (modifiers[DayFlag.today]) {
35 focusTarget = day;
36 found = true;
37 }
38 }
39
40 index++;
41 }
42
43 if (!focusTarget) {
44 // return the first day that is focusable
45 focusTarget = days.find((day) => {
46 const m = getModifiers(day);
47 return !m[DayFlag.disabled] && !m[DayFlag.hidden] && !m[DayFlag.outside];
48 });
49 }
50 return focusTarget;
51}