@connextar/house 0.27.0 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -0
- package/dist/calendar/index.d.ts +9 -0
- package/dist/calendar/index.d.ts.map +1 -0
- package/dist/calendar/index.js +9 -0
- package/dist/calendar/index.js.map +1 -0
- package/dist/calendar/planner.d.ts +145 -0
- package/dist/calendar/planner.d.ts.map +1 -0
- package/dist/calendar/planner.js +315 -0
- package/dist/calendar/planner.js.map +1 -0
- package/package.json +5 -1
- package/src/calendar/index.ts +45 -0
- package/src/calendar/planner.ts +387 -0
package/README.md
CHANGED
|
@@ -2536,6 +2536,46 @@ const parsed = inboxActionSchema.safeParse(await request.json());
|
|
|
2536
2536
|
await db.notification.updateMany({ where: { id: { in: parsed.data.ids }, userId }, data: actionChanges(action) });
|
|
2537
2537
|
```
|
|
2538
2538
|
|
|
2539
|
+
### `@connextar/house/calendar`
|
|
2540
|
+
|
|
2541
|
+
The rules of a day / week / month calendar — which view is showing and which
|
|
2542
|
+
day it is on, what that view covers, how ‹ › and the quick ranges move it, what
|
|
2543
|
+
the date button says, and how a day's timed items are laid out and dragged —
|
|
2544
|
+
with no markup and no database in it. quickie built the planner first; AltEd
|
|
2545
|
+
ported it in 0.190.0 and found that everything except the look and the items was
|
|
2546
|
+
the same code with a different time zone, so the shared part lives here.
|
|
2547
|
+
|
|
2548
|
+
- **Days are keys, times are wall-clock.** A day is a `yyyy-MM-dd` key, and day
|
|
2549
|
+
arithmetic (`addDays`, `addMonths`, `startOfWeek`, `startOfMonth`) works on the
|
|
2550
|
+
key itself, never on a local `Date`, so neither the server's zone nor the
|
|
2551
|
+
browser's can move a day. Times are minutes past midnight in a named zone:
|
|
2552
|
+
`zonedInstant(day, minutes, zone)` and `placeOf(instant, zone)` go between
|
|
2553
|
+
them, checked twice so the day the clocks change lands right. Weeks start on
|
|
2554
|
+
Monday.
|
|
2555
|
+
- **The address.** `parseCalendarQuery` reads `?view=&date=` from a request or a
|
|
2556
|
+
page's `searchParams`, falling back to the default view — the app's, or the
|
|
2557
|
+
reader's remembered one — and today; `calendarQuery` writes them back.
|
|
2558
|
+
- **What a view covers.** `visibleRange` (the day, its week, or the whole weeks
|
|
2559
|
+
around the month), `visibleDays` (optionally weekdays only) and
|
|
2560
|
+
`visibleWindow(view, anchor, zone)`, the `[from, to)` instants to read. Read
|
|
2561
|
+
only that window — and for a repeating item, each time it happens in it.
|
|
2562
|
+
- **Moving.** `stepAnchor` for ‹ ›; `QUICK_RANGES` (Today … Last month), with
|
|
2563
|
+
`matchingQuickRange` and `rangeLabel` so the button says "This week" rather
|
|
2564
|
+
than dates when it is; `rangeHeading` for the title over the grid.
|
|
2565
|
+
- **The grid.** `gridSlots` gives the rows, widened to take in anything outside
|
|
2566
|
+
the hours; `layoutDay` gives each timed item its lane among the ones it
|
|
2567
|
+
overlaps; `movedTimes` is a drop's new start and end, keeping the length.
|
|
2568
|
+
|
|
2569
|
+
The app owns the rest: its items and who may change them, its settings and where
|
|
2570
|
+
they're kept, and its grids' markup.
|
|
2571
|
+
|
|
2572
|
+
```ts
|
|
2573
|
+
const today = todayIn("Europe/London");
|
|
2574
|
+
const { view, anchor } = parseCalendarQuery(await searchParams, today, prefs.lastView);
|
|
2575
|
+
const { from, to } = visibleWindow(view, anchor, "Europe/London");
|
|
2576
|
+
const events = await db.event.findMany({ where: { startTime: { lt: to }, endTime: { gt: from } } });
|
|
2577
|
+
```
|
|
2578
|
+
|
|
2539
2579
|
### `@connextar/house/web-search`
|
|
2540
2580
|
|
|
2541
2581
|
Google's organic results for a query, from whichever search API a deployment
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/calendar` — the rules of a day / week / month calendar.
|
|
3
|
+
*
|
|
4
|
+
* Views and the address they live in, what each view covers, stepping and quick ranges, the date button's words, and
|
|
5
|
+
* the day grid's rows, lanes and drag-to-move arithmetic — in a named time zone, with no markup and no database in it:
|
|
6
|
+
* Zod, and nothing else. The app owns its items, its settings, its storage and its look.
|
|
7
|
+
*/
|
|
8
|
+
export { CALENDAR_VIEWS, CALENDAR_VIEW_NAMES, QUICK_RANGES, addDays, addMonths, calendarQuery, calendarViewSchema, daysBetween, endOfMonth, gridSlots, hhmm, isCalendarView, isDayKey, layoutDay, longDate, matchingQuickRange, monthTitle, movedTimes, parseCalendarQuery, placeOf, rangeHeading, rangeLabel, shortDate, startOfMonth, startOfWeek, stepAnchor, todayIn, visibleDays, visibleRange, visibleWindow, weekdayName, weekdayOf, zonedInstant, type CalendarView, type GridHours, type QuickRange, } from "./planner.js";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/calendar/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,YAAY,EACZ,OAAO,EACP,SAAS,EACT,aAAa,EACb,kBAAkB,EAClB,WAAW,EACX,UAAU,EACV,SAAS,EACT,IAAI,EACJ,cAAc,EACd,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,kBAAkB,EAClB,UAAU,EACV,UAAU,EACV,kBAAkB,EAClB,OAAO,EACP,YAAY,EACZ,UAAU,EACV,SAAS,EACT,YAAY,EACZ,WAAW,EACX,UAAU,EACV,OAAO,EACP,WAAW,EACX,YAAY,EACZ,aAAa,EACb,WAAW,EACX,SAAS,EACT,YAAY,EACZ,KAAK,YAAY,EACjB,KAAK,SAAS,EACd,KAAK,UAAU,GAChB,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/calendar` — the rules of a day / week / month calendar.
|
|
3
|
+
*
|
|
4
|
+
* Views and the address they live in, what each view covers, stepping and quick ranges, the date button's words, and
|
|
5
|
+
* the day grid's rows, lanes and drag-to-move arithmetic — in a named time zone, with no markup and no database in it:
|
|
6
|
+
* Zod, and nothing else. The app owns its items, its settings, its storage and its look.
|
|
7
|
+
*/
|
|
8
|
+
export { CALENDAR_VIEWS, CALENDAR_VIEW_NAMES, QUICK_RANGES, addDays, addMonths, calendarQuery, calendarViewSchema, daysBetween, endOfMonth, gridSlots, hhmm, isCalendarView, isDayKey, layoutDay, longDate, matchingQuickRange, monthTitle, movedTimes, parseCalendarQuery, placeOf, rangeHeading, rangeLabel, shortDate, startOfMonth, startOfWeek, stepAnchor, todayIn, visibleDays, visibleRange, visibleWindow, weekdayName, weekdayOf, zonedInstant, } from "./planner.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/calendar/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,YAAY,EACZ,OAAO,EACP,SAAS,EACT,aAAa,EACb,kBAAkB,EAClB,WAAW,EACX,UAAU,EACV,SAAS,EACT,IAAI,EACJ,cAAc,EACd,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,kBAAkB,EAClB,UAAU,EACV,UAAU,EACV,kBAAkB,EAClB,OAAO,EACP,YAAY,EACZ,UAAU,EACV,SAAS,EACT,YAAY,EACZ,WAAW,EACX,UAAU,EACV,OAAO,EACP,WAAW,EACX,YAAY,EACZ,aAAa,EACb,WAAW,EACX,SAAS,EACT,YAAY,GAIb,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* The rules of a day / week / month calendar, with no markup and no database in them.
|
|
4
|
+
*
|
|
5
|
+
* Which view is showing and which day it is anchored on (`parseCalendarQuery`, `calendarQuery`), what that view covers
|
|
6
|
+
* (`visibleRange`, `visibleDays`, `visibleWindow`), how ‹ › and the quick ranges move it (`stepAnchor`,
|
|
7
|
+
* `QUICK_RANGES`), what the date button and heading say (`rangeLabel`, `rangeHeading`), and how a day's timed items
|
|
8
|
+
* are laid out and moved (`gridSlots`, `layoutDay`, `movedTimes`). Built from quickie's planner and AltEd's port of it.
|
|
9
|
+
*
|
|
10
|
+
* Days are calendar days as `yyyy-MM-dd` keys and times are wall-clock minutes past midnight in a named time zone, so a
|
|
11
|
+
* 10:00 lesson sits at 10:00 all year. Day arithmetic is done on the keys themselves — never on a local `Date` — so the
|
|
12
|
+
* server's zone and the browser's can't move a day. Weeks start on Monday (en-GB).
|
|
13
|
+
*/
|
|
14
|
+
export declare const CALENDAR_VIEW_NAMES: readonly ["day", "week", "month"];
|
|
15
|
+
export type CalendarView = (typeof CALENDAR_VIEW_NAMES)[number];
|
|
16
|
+
export declare const CALENDAR_VIEWS: readonly {
|
|
17
|
+
value: CalendarView;
|
|
18
|
+
label: string;
|
|
19
|
+
}[];
|
|
20
|
+
/** A view's name, for a request body or a stored setting. */
|
|
21
|
+
export declare const calendarViewSchema: z.ZodEnum<{
|
|
22
|
+
day: "day";
|
|
23
|
+
week: "week";
|
|
24
|
+
month: "month";
|
|
25
|
+
}>;
|
|
26
|
+
export declare function isCalendarView(value: unknown): value is CalendarView;
|
|
27
|
+
/** Whether `value` is a real day key between the years 1900 and 2200 — not "2026-02-30". */
|
|
28
|
+
export declare function isDayKey(value: unknown): value is string;
|
|
29
|
+
export declare function addDays(key: string, days: number): string;
|
|
30
|
+
/** The same day `months` on, or the month's last day when it hasn't got one (31 January + 1 → 28 February). */
|
|
31
|
+
export declare function addMonths(key: string, months: number): string;
|
|
32
|
+
/** 0 for Sunday to 6 for Saturday, as `Date.getDay`. */
|
|
33
|
+
export declare function weekdayOf(key: string): number;
|
|
34
|
+
/** The Monday of the day's week. */
|
|
35
|
+
export declare function startOfWeek(key: string): string;
|
|
36
|
+
export declare function startOfMonth(key: string): string;
|
|
37
|
+
export declare function endOfMonth(key: string): string;
|
|
38
|
+
/** The day keys from `start` to `end`, both included. */
|
|
39
|
+
export declare function daysBetween(start: string, end: string): string[];
|
|
40
|
+
/**
|
|
41
|
+
* The instant `day` at `minutes` past midnight happens in `timeZone`. Worked out twice, so a time on the day the clocks
|
|
42
|
+
* change lands right too.
|
|
43
|
+
*/
|
|
44
|
+
export declare function zonedInstant(day: string, minutes: number, timeZone: string): Date;
|
|
45
|
+
/** Where an instant falls on the calendar in `timeZone`: its day and minutes past midnight. */
|
|
46
|
+
export declare function placeOf(instant: string | Date, timeZone: string): {
|
|
47
|
+
day: string;
|
|
48
|
+
minutes: number;
|
|
49
|
+
};
|
|
50
|
+
/** Today in `timeZone`, as a day key. */
|
|
51
|
+
export declare function todayIn(timeZone: string, now?: Date): string;
|
|
52
|
+
type QueryInput = URLSearchParams | Record<string, string | string[] | undefined>;
|
|
53
|
+
/**
|
|
54
|
+
* The view and anchor day from `?view=&date=` — a request's search params or a page's `searchParams`. Anything
|
|
55
|
+
* unreadable is the default view (the app's, or the reader's remembered one) and today.
|
|
56
|
+
*/
|
|
57
|
+
export declare function parseCalendarQuery(query: QueryInput, todayKey: string, defaultView?: CalendarView): {
|
|
58
|
+
view: CalendarView;
|
|
59
|
+
anchor: string;
|
|
60
|
+
};
|
|
61
|
+
/** The query string for a view and day, `view=week&date=2026-11-23`, to put after the calendar's path and a `?`. */
|
|
62
|
+
export declare function calendarQuery(view: CalendarView, anchor: string): string;
|
|
63
|
+
/** The first and last day a view shows: the day, its Monday–Sunday week, or the whole weeks around its month. */
|
|
64
|
+
export declare function visibleRange(view: CalendarView, anchor: string): {
|
|
65
|
+
start: string;
|
|
66
|
+
end: string;
|
|
67
|
+
};
|
|
68
|
+
/** Every day a view shows, in order; with `weekdaysOnly`, the week and month views leave out Saturday and Sunday. */
|
|
69
|
+
export declare function visibleDays(view: CalendarView, anchor: string, weekdaysOnly?: boolean): string[];
|
|
70
|
+
/**
|
|
71
|
+
* The instants a view covers, `[from, to)`: midnight of its first day to midnight after its last, in `timeZone`. Read
|
|
72
|
+
* only what falls in it — and for a repeating item, every time it happens in it.
|
|
73
|
+
*/
|
|
74
|
+
export declare function visibleWindow(view: CalendarView, anchor: string, timeZone: string): {
|
|
75
|
+
from: Date;
|
|
76
|
+
to: Date;
|
|
77
|
+
};
|
|
78
|
+
/** The anchor one step back or on: a day, a week, or the first of the month before or after. */
|
|
79
|
+
export declare function stepAnchor(view: CalendarView, anchor: string, by: 1 | -1): string;
|
|
80
|
+
export interface QuickRange {
|
|
81
|
+
key: string;
|
|
82
|
+
label: string;
|
|
83
|
+
view: CalendarView;
|
|
84
|
+
anchor: (todayKey: string) => string;
|
|
85
|
+
}
|
|
86
|
+
/** The ranges a date button offers beside its months. */
|
|
87
|
+
export declare const QUICK_RANGES: readonly QuickRange[];
|
|
88
|
+
/** The quick range a view and anchor are, if any ("This week"), so the button can say so. */
|
|
89
|
+
export declare function matchingQuickRange(view: CalendarView, anchor: string, todayKey: string): QuickRange | null;
|
|
90
|
+
/** "12/11/2026". */
|
|
91
|
+
export declare function shortDate(key: string): string;
|
|
92
|
+
/** "Thursday 12 November 2026". */
|
|
93
|
+
export declare function longDate(key: string): string;
|
|
94
|
+
/** "November 2026". */
|
|
95
|
+
export declare function monthTitle(key: string): string;
|
|
96
|
+
/** "Mon", "Tue"… for a day key; `long` gives "Monday". */
|
|
97
|
+
export declare function weekdayName(key: string, long?: boolean): string;
|
|
98
|
+
/**
|
|
99
|
+
* What the date button says: the quick range's name when it is one ("Last week"), otherwise the dates —
|
|
100
|
+
* "Thu 12/11/2026", "02/11/2026 – 08/11/2026" or "November 2026".
|
|
101
|
+
*/
|
|
102
|
+
export declare function rangeLabel(view: CalendarView, anchor: string, todayKey: string): string;
|
|
103
|
+
/** The heading over the grid: "Thursday 12 November 2026", "2 – 8 Nov 2026", "30 Nov – 6 Dec 2026", "November 2026". */
|
|
104
|
+
export declare function rangeHeading(view: CalendarView, anchor: string): string;
|
|
105
|
+
/** Minutes past midnight as `HH:mm`. */
|
|
106
|
+
export declare function hhmm(minutes: number): string;
|
|
107
|
+
export interface GridHours {
|
|
108
|
+
/** The first hour the grid shows, 0–23. */
|
|
109
|
+
startHour: number;
|
|
110
|
+
/** The hour it ends at, 1–24, after `startHour`. */
|
|
111
|
+
endHour: number;
|
|
112
|
+
slotMinutes: 30 | 60;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The rows of a day or week grid, as minutes past midnight: every slot from the first hour to the last, widened to the
|
|
116
|
+
* hour to take in anything timed outside them.
|
|
117
|
+
*/
|
|
118
|
+
export declare function gridSlots(hours: GridHours, spans: readonly {
|
|
119
|
+
start: number;
|
|
120
|
+
end: number;
|
|
121
|
+
}[]): number[];
|
|
122
|
+
/**
|
|
123
|
+
* Each timed item's place in its day: its lane among the ones it overlaps, and how many lanes that cluster of
|
|
124
|
+
* overlapping items has — so two lessons at 15:00 sit side by side at half width each.
|
|
125
|
+
*/
|
|
126
|
+
export declare function layoutDay<T extends {
|
|
127
|
+
start: number;
|
|
128
|
+
end: number;
|
|
129
|
+
}>(items: readonly T[]): (T & {
|
|
130
|
+
lane: number;
|
|
131
|
+
lanes: number;
|
|
132
|
+
})[];
|
|
133
|
+
/**
|
|
134
|
+
* An item's new times when it's dropped on `day` at `minutes` past midnight in `timeZone` — or, with `minutes` null (a
|
|
135
|
+
* month cell), at its own time of day — keeping its length. ISO strings in, ISO strings out.
|
|
136
|
+
*/
|
|
137
|
+
export declare function movedTimes(item: {
|
|
138
|
+
startTime: string;
|
|
139
|
+
endTime: string;
|
|
140
|
+
}, day: string, minutes: number | null, timeZone: string): {
|
|
141
|
+
startTime: string;
|
|
142
|
+
endTime: string;
|
|
143
|
+
};
|
|
144
|
+
export {};
|
|
145
|
+
//# sourceMappingURL=planner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"planner.d.ts","sourceRoot":"","sources":["../../src/calendar/planner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB;;;;;;;;;;;GAWG;AAEH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,SAAS,EAAE,KAAK,EAAE,SAAS;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,EAAE,GAAG,MAAM,EAAE,CAMtG;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,EAChE,KAAK,EAAE,SAAS,CAAC,EAAE,GAClB,CAAC,CAAC,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,EAAE,CAuBzC;AAED;;;GAGG;AACH,wBAAgB,UAAU,CACxB,IAAI,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,EAC5C,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,MAAM,GAAG,IAAI,EACtB,QAAQ,EAAE,MAAM,GACf;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAKxC"}
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* The rules of a day / week / month calendar, with no markup and no database in them.
|
|
4
|
+
*
|
|
5
|
+
* Which view is showing and which day it is anchored on (`parseCalendarQuery`, `calendarQuery`), what that view covers
|
|
6
|
+
* (`visibleRange`, `visibleDays`, `visibleWindow`), how ‹ › and the quick ranges move it (`stepAnchor`,
|
|
7
|
+
* `QUICK_RANGES`), what the date button and heading say (`rangeLabel`, `rangeHeading`), and how a day's timed items
|
|
8
|
+
* are laid out and moved (`gridSlots`, `layoutDay`, `movedTimes`). Built from quickie's planner and AltEd's port of it.
|
|
9
|
+
*
|
|
10
|
+
* Days are calendar days as `yyyy-MM-dd` keys and times are wall-clock minutes past midnight in a named time zone, so a
|
|
11
|
+
* 10:00 lesson sits at 10:00 all year. Day arithmetic is done on the keys themselves — never on a local `Date` — so the
|
|
12
|
+
* server's zone and the browser's can't move a day. Weeks start on Monday (en-GB).
|
|
13
|
+
*/
|
|
14
|
+
export const CALENDAR_VIEW_NAMES = ["day", "week", "month"];
|
|
15
|
+
export const CALENDAR_VIEWS = [
|
|
16
|
+
{ value: "day", label: "Day" },
|
|
17
|
+
{ value: "week", label: "Week" },
|
|
18
|
+
{ value: "month", label: "Month" },
|
|
19
|
+
];
|
|
20
|
+
/** A view's name, for a request body or a stored setting. */
|
|
21
|
+
export const calendarViewSchema = z.enum(CALENDAR_VIEW_NAMES);
|
|
22
|
+
export function isCalendarView(value) {
|
|
23
|
+
return typeof value === "string" && CALENDAR_VIEW_NAMES.includes(value);
|
|
24
|
+
}
|
|
25
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
26
|
+
// Day keys
|
|
27
|
+
const KEY_PATTERN = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
28
|
+
const DAY_MS = 86_400_000;
|
|
29
|
+
const MONTHS = [
|
|
30
|
+
"January",
|
|
31
|
+
"February",
|
|
32
|
+
"March",
|
|
33
|
+
"April",
|
|
34
|
+
"May",
|
|
35
|
+
"June",
|
|
36
|
+
"July",
|
|
37
|
+
"August",
|
|
38
|
+
"September",
|
|
39
|
+
"October",
|
|
40
|
+
"November",
|
|
41
|
+
"December",
|
|
42
|
+
];
|
|
43
|
+
const WEEKDAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
|
|
44
|
+
function utcOf(key) {
|
|
45
|
+
const [, y, m, d] = KEY_PATTERN.exec(key) ?? [];
|
|
46
|
+
return Date.UTC(Number(y), Number(m) - 1, Number(d));
|
|
47
|
+
}
|
|
48
|
+
function keyOf(utc) {
|
|
49
|
+
return new Date(utc).toISOString().slice(0, 10);
|
|
50
|
+
}
|
|
51
|
+
/** Whether `value` is a real day key between the years 1900 and 2200 — not "2026-02-30". */
|
|
52
|
+
export function isDayKey(value) {
|
|
53
|
+
if (typeof value !== "string" || !KEY_PATTERN.test(value))
|
|
54
|
+
return false;
|
|
55
|
+
const year = Number(value.slice(0, 4));
|
|
56
|
+
return year > 1900 && year < 2200 && keyOf(utcOf(value)) === value;
|
|
57
|
+
}
|
|
58
|
+
export function addDays(key, days) {
|
|
59
|
+
return keyOf(utcOf(key) + days * DAY_MS);
|
|
60
|
+
}
|
|
61
|
+
/** The same day `months` on, or the month's last day when it hasn't got one (31 January + 1 → 28 February). */
|
|
62
|
+
export function addMonths(key, months) {
|
|
63
|
+
const date = new Date(utcOf(key));
|
|
64
|
+
const target = date.getUTCMonth() + months;
|
|
65
|
+
const last = new Date(Date.UTC(date.getUTCFullYear(), target + 1, 0)).getUTCDate();
|
|
66
|
+
return keyOf(Date.UTC(date.getUTCFullYear(), target, Math.min(date.getUTCDate(), last)));
|
|
67
|
+
}
|
|
68
|
+
/** 0 for Sunday to 6 for Saturday, as `Date.getDay`. */
|
|
69
|
+
export function weekdayOf(key) {
|
|
70
|
+
return new Date(utcOf(key)).getUTCDay();
|
|
71
|
+
}
|
|
72
|
+
/** The Monday of the day's week. */
|
|
73
|
+
export function startOfWeek(key) {
|
|
74
|
+
return addDays(key, -((weekdayOf(key) + 6) % 7));
|
|
75
|
+
}
|
|
76
|
+
export function startOfMonth(key) {
|
|
77
|
+
return `${key.slice(0, 7)}-01`;
|
|
78
|
+
}
|
|
79
|
+
export function endOfMonth(key) {
|
|
80
|
+
return addDays(addMonths(startOfMonth(key), 1), -1);
|
|
81
|
+
}
|
|
82
|
+
/** The day keys from `start` to `end`, both included. */
|
|
83
|
+
export function daysBetween(start, end) {
|
|
84
|
+
const days = [];
|
|
85
|
+
for (let d = start; d <= end; d = addDays(d, 1))
|
|
86
|
+
days.push(d);
|
|
87
|
+
return days;
|
|
88
|
+
}
|
|
89
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
90
|
+
// Time zones
|
|
91
|
+
function zoneParts(instant, timeZone) {
|
|
92
|
+
return Object.fromEntries(new Intl.DateTimeFormat("en-CA", {
|
|
93
|
+
timeZone,
|
|
94
|
+
hourCycle: "h23",
|
|
95
|
+
year: "numeric",
|
|
96
|
+
month: "2-digit",
|
|
97
|
+
day: "2-digit",
|
|
98
|
+
hour: "2-digit",
|
|
99
|
+
minute: "2-digit",
|
|
100
|
+
second: "2-digit",
|
|
101
|
+
})
|
|
102
|
+
.formatToParts(instant)
|
|
103
|
+
.map((part) => [part.type, part.value]));
|
|
104
|
+
}
|
|
105
|
+
function offsetMinutes(instant, timeZone) {
|
|
106
|
+
const p = zoneParts(instant, timeZone);
|
|
107
|
+
const asUtc = Date.UTC(Number(p.year), Number(p.month) - 1, Number(p.day), Number(p.hour), Number(p.minute), Number(p.second));
|
|
108
|
+
return Math.round((asUtc - Math.floor(instant.getTime() / 1000) * 1000) / 60_000);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The instant `day` at `minutes` past midnight happens in `timeZone`. Worked out twice, so a time on the day the clocks
|
|
112
|
+
* change lands right too.
|
|
113
|
+
*/
|
|
114
|
+
export function zonedInstant(day, minutes, timeZone) {
|
|
115
|
+
const wallClockAsUtc = utcOf(day) + minutes * 60_000;
|
|
116
|
+
const first = wallClockAsUtc - offsetMinutes(new Date(wallClockAsUtc), timeZone) * 60_000;
|
|
117
|
+
return new Date(wallClockAsUtc - offsetMinutes(new Date(first), timeZone) * 60_000);
|
|
118
|
+
}
|
|
119
|
+
/** Where an instant falls on the calendar in `timeZone`: its day and minutes past midnight. */
|
|
120
|
+
export function placeOf(instant, timeZone) {
|
|
121
|
+
const p = zoneParts(typeof instant === "string" ? new Date(instant) : instant, timeZone);
|
|
122
|
+
return { day: `${p.year}-${p.month}-${p.day}`, minutes: Number(p.hour) * 60 + Number(p.minute) };
|
|
123
|
+
}
|
|
124
|
+
/** Today in `timeZone`, as a day key. */
|
|
125
|
+
export function todayIn(timeZone, now = new Date()) {
|
|
126
|
+
return placeOf(now, timeZone).day;
|
|
127
|
+
}
|
|
128
|
+
function first(query, name) {
|
|
129
|
+
if (query instanceof URLSearchParams)
|
|
130
|
+
return query.get(name) ?? undefined;
|
|
131
|
+
const value = query[name];
|
|
132
|
+
return Array.isArray(value) ? value[0] : value;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* The view and anchor day from `?view=&date=` — a request's search params or a page's `searchParams`. Anything
|
|
136
|
+
* unreadable is the default view (the app's, or the reader's remembered one) and today.
|
|
137
|
+
*/
|
|
138
|
+
export function parseCalendarQuery(query, todayKey, defaultView = "month") {
|
|
139
|
+
const view = first(query, "view");
|
|
140
|
+
const date = first(query, "date");
|
|
141
|
+
return { view: isCalendarView(view) ? view : defaultView, anchor: isDayKey(date) ? date : todayKey };
|
|
142
|
+
}
|
|
143
|
+
/** The query string for a view and day, `view=week&date=2026-11-23`, to put after the calendar's path and a `?`. */
|
|
144
|
+
export function calendarQuery(view, anchor) {
|
|
145
|
+
return new URLSearchParams({ view, date: anchor }).toString();
|
|
146
|
+
}
|
|
147
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
148
|
+
// What a view covers
|
|
149
|
+
/** The first and last day a view shows: the day, its Monday–Sunday week, or the whole weeks around its month. */
|
|
150
|
+
export function visibleRange(view, anchor) {
|
|
151
|
+
if (view === "day")
|
|
152
|
+
return { start: anchor, end: anchor };
|
|
153
|
+
if (view === "week")
|
|
154
|
+
return { start: startOfWeek(anchor), end: addDays(startOfWeek(anchor), 6) };
|
|
155
|
+
return { start: startOfWeek(startOfMonth(anchor)), end: addDays(startOfWeek(endOfMonth(anchor)), 6) };
|
|
156
|
+
}
|
|
157
|
+
/** Every day a view shows, in order; with `weekdaysOnly`, the week and month views leave out Saturday and Sunday. */
|
|
158
|
+
export function visibleDays(view, anchor, weekdaysOnly = false) {
|
|
159
|
+
const { start, end } = visibleRange(view, anchor);
|
|
160
|
+
const days = daysBetween(start, end);
|
|
161
|
+
return view !== "day" && weekdaysOnly ? days.filter((d) => ![0, 6].includes(weekdayOf(d))) : days;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The instants a view covers, `[from, to)`: midnight of its first day to midnight after its last, in `timeZone`. Read
|
|
165
|
+
* only what falls in it — and for a repeating item, every time it happens in it.
|
|
166
|
+
*/
|
|
167
|
+
export function visibleWindow(view, anchor, timeZone) {
|
|
168
|
+
const { start, end } = visibleRange(view, anchor);
|
|
169
|
+
return { from: zonedInstant(start, 0, timeZone), to: zonedInstant(addDays(end, 1), 0, timeZone) };
|
|
170
|
+
}
|
|
171
|
+
/** The anchor one step back or on: a day, a week, or the first of the month before or after. */
|
|
172
|
+
export function stepAnchor(view, anchor, by) {
|
|
173
|
+
if (view === "day")
|
|
174
|
+
return addDays(anchor, by);
|
|
175
|
+
if (view === "week")
|
|
176
|
+
return addDays(anchor, 7 * by);
|
|
177
|
+
return startOfMonth(addMonths(startOfMonth(anchor), by));
|
|
178
|
+
}
|
|
179
|
+
/** The ranges a date button offers beside its months. */
|
|
180
|
+
export const QUICK_RANGES = [
|
|
181
|
+
{ key: "today", label: "Today", view: "day", anchor: (t) => t },
|
|
182
|
+
{ key: "tomorrow", label: "Tomorrow", view: "day", anchor: (t) => addDays(t, 1) },
|
|
183
|
+
{ key: "yesterday", label: "Yesterday", view: "day", anchor: (t) => addDays(t, -1) },
|
|
184
|
+
{ key: "this-week", label: "This week", view: "week", anchor: (t) => startOfWeek(t) },
|
|
185
|
+
{ key: "next-week", label: "Next week", view: "week", anchor: (t) => startOfWeek(addDays(t, 7)) },
|
|
186
|
+
{ key: "last-week", label: "Last week", view: "week", anchor: (t) => startOfWeek(addDays(t, -7)) },
|
|
187
|
+
{ key: "this-month", label: "This month", view: "month", anchor: (t) => startOfMonth(t) },
|
|
188
|
+
{ key: "next-month", label: "Next month", view: "month", anchor: (t) => addMonths(startOfMonth(t), 1) },
|
|
189
|
+
{ key: "last-month", label: "Last month", view: "month", anchor: (t) => addMonths(startOfMonth(t), -1) },
|
|
190
|
+
];
|
|
191
|
+
/** The quick range a view and anchor are, if any ("This week"), so the button can say so. */
|
|
192
|
+
export function matchingQuickRange(view, anchor, todayKey) {
|
|
193
|
+
const shown = visibleRange(view, anchor);
|
|
194
|
+
return (QUICK_RANGES.find((range) => {
|
|
195
|
+
if (range.view !== view)
|
|
196
|
+
return false;
|
|
197
|
+
const theirs = visibleRange(range.view, range.anchor(todayKey));
|
|
198
|
+
return theirs.start === shown.start && theirs.end === shown.end;
|
|
199
|
+
}) ?? null);
|
|
200
|
+
}
|
|
201
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
202
|
+
// Words
|
|
203
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
204
|
+
function parts(key) {
|
|
205
|
+
const date = new Date(utcOf(key));
|
|
206
|
+
return { y: date.getUTCFullYear(), m: date.getUTCMonth(), d: date.getUTCDate(), w: date.getUTCDay() };
|
|
207
|
+
}
|
|
208
|
+
/** "12/11/2026". */
|
|
209
|
+
export function shortDate(key) {
|
|
210
|
+
const { y, m, d } = parts(key);
|
|
211
|
+
return `${pad(d)}/${pad(m + 1)}/${y}`;
|
|
212
|
+
}
|
|
213
|
+
/** "Thursday 12 November 2026". */
|
|
214
|
+
export function longDate(key) {
|
|
215
|
+
const { y, m, d, w } = parts(key);
|
|
216
|
+
return `${WEEKDAYS[w]} ${d} ${MONTHS[m]} ${y}`;
|
|
217
|
+
}
|
|
218
|
+
/** "November 2026". */
|
|
219
|
+
export function monthTitle(key) {
|
|
220
|
+
const { y, m } = parts(key);
|
|
221
|
+
return `${MONTHS[m]} ${y}`;
|
|
222
|
+
}
|
|
223
|
+
/** "Mon", "Tue"… for a day key; `long` gives "Monday". */
|
|
224
|
+
export function weekdayName(key, long = false) {
|
|
225
|
+
const name = WEEKDAYS[weekdayOf(key)] ?? "";
|
|
226
|
+
return long ? name : name.slice(0, 3);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* What the date button says: the quick range's name when it is one ("Last week"), otherwise the dates —
|
|
230
|
+
* "Thu 12/11/2026", "02/11/2026 – 08/11/2026" or "November 2026".
|
|
231
|
+
*/
|
|
232
|
+
export function rangeLabel(view, anchor, todayKey) {
|
|
233
|
+
const quick = matchingQuickRange(view, anchor, todayKey);
|
|
234
|
+
if (quick)
|
|
235
|
+
return quick.label;
|
|
236
|
+
if (view === "day")
|
|
237
|
+
return `${weekdayName(anchor)} ${shortDate(anchor)}`;
|
|
238
|
+
if (view === "month")
|
|
239
|
+
return monthTitle(anchor);
|
|
240
|
+
const { start, end } = visibleRange("week", anchor);
|
|
241
|
+
return `${shortDate(start)} – ${shortDate(end)}`;
|
|
242
|
+
}
|
|
243
|
+
/** The heading over the grid: "Thursday 12 November 2026", "2 – 8 Nov 2026", "30 Nov – 6 Dec 2026", "November 2026". */
|
|
244
|
+
export function rangeHeading(view, anchor) {
|
|
245
|
+
if (view === "day")
|
|
246
|
+
return longDate(anchor);
|
|
247
|
+
if (view === "month")
|
|
248
|
+
return monthTitle(anchor);
|
|
249
|
+
const { start, end } = visibleRange("week", anchor);
|
|
250
|
+
const s = parts(start);
|
|
251
|
+
const e = parts(end);
|
|
252
|
+
const mon = (m) => (MONTHS[m] ?? "").slice(0, 3);
|
|
253
|
+
if (s.y !== e.y)
|
|
254
|
+
return `${s.d} ${mon(s.m)} ${s.y} – ${e.d} ${mon(e.m)} ${e.y}`;
|
|
255
|
+
return s.m === e.m ? `${s.d} – ${e.d} ${mon(e.m)} ${e.y}` : `${s.d} ${mon(s.m)} – ${e.d} ${mon(e.m)} ${e.y}`;
|
|
256
|
+
}
|
|
257
|
+
/** Minutes past midnight as `HH:mm`. */
|
|
258
|
+
export function hhmm(minutes) {
|
|
259
|
+
return `${pad(Math.floor(minutes / 60))}:${pad(minutes % 60)}`;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* The rows of a day or week grid, as minutes past midnight: every slot from the first hour to the last, widened to the
|
|
263
|
+
* hour to take in anything timed outside them.
|
|
264
|
+
*/
|
|
265
|
+
export function gridSlots(hours, spans) {
|
|
266
|
+
const earliest = Math.min(hours.startHour * 60, ...spans.map((s) => Math.floor(s.start / 60) * 60));
|
|
267
|
+
const latest = Math.max(hours.endHour * 60, ...spans.map((s) => Math.ceil(s.end / 60) * 60));
|
|
268
|
+
const slots = [];
|
|
269
|
+
for (let m = Math.max(0, earliest); m < Math.min(24 * 60, latest); m += hours.slotMinutes)
|
|
270
|
+
slots.push(m);
|
|
271
|
+
return slots;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Each timed item's place in its day: its lane among the ones it overlaps, and how many lanes that cluster of
|
|
275
|
+
* overlapping items has — so two lessons at 15:00 sit side by side at half width each.
|
|
276
|
+
*/
|
|
277
|
+
export function layoutDay(items) {
|
|
278
|
+
const sorted = [...items].sort((a, b) => a.start - b.start || b.end - a.end);
|
|
279
|
+
const placed = [];
|
|
280
|
+
let cluster = [];
|
|
281
|
+
let clusterEnd = -1;
|
|
282
|
+
let laneEnds = [];
|
|
283
|
+
const close = () => {
|
|
284
|
+
for (const p of cluster)
|
|
285
|
+
p.lanes = laneEnds.length;
|
|
286
|
+
cluster = [];
|
|
287
|
+
laneEnds = [];
|
|
288
|
+
};
|
|
289
|
+
for (const item of sorted) {
|
|
290
|
+
if (item.start >= clusterEnd)
|
|
291
|
+
close();
|
|
292
|
+
let lane = laneEnds.findIndex((end) => end <= item.start);
|
|
293
|
+
if (lane < 0)
|
|
294
|
+
lane = laneEnds.push(item.end) - 1;
|
|
295
|
+
else
|
|
296
|
+
laneEnds[lane] = item.end;
|
|
297
|
+
const p = { ...item, lane, lanes: 1 };
|
|
298
|
+
cluster.push(p);
|
|
299
|
+
placed.push(p);
|
|
300
|
+
clusterEnd = Math.max(clusterEnd, item.end);
|
|
301
|
+
}
|
|
302
|
+
close();
|
|
303
|
+
return placed;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* An item's new times when it's dropped on `day` at `minutes` past midnight in `timeZone` — or, with `minutes` null (a
|
|
307
|
+
* month cell), at its own time of day — keeping its length. ISO strings in, ISO strings out.
|
|
308
|
+
*/
|
|
309
|
+
export function movedTimes(item, day, minutes, timeZone) {
|
|
310
|
+
const start = new Date(item.startTime);
|
|
311
|
+
const length = new Date(item.endTime).getTime() - start.getTime();
|
|
312
|
+
const next = zonedInstant(day, minutes ?? placeOf(start, timeZone).minutes, timeZone);
|
|
313
|
+
return { startTime: next.toISOString(), endTime: new Date(next.getTime() + length).toISOString() };
|
|
314
|
+
}
|
|
315
|
+
//# sourceMappingURL=planner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"planner.js","sourceRoot":"","sources":["../../src/calendar/planner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB;;;;;;;;;;;GAWG;AAEH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAU,CAAC;AAGrE,MAAM,CAAC,MAAM,cAAc,GAAsD;IAC/E,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE;IAC9B,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE;IAChC,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE;CACnC,CAAC;AAEF,6DAA6D;AAC7D,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;AAE9D,MAAM,UAAU,cAAc,CAAC,KAAc;IAC3C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAK,mBAAyC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AACjG,CAAC;AAED,wHAAwH;AACxH,WAAW;AAEX,MAAM,WAAW,GAAG,2BAA2B,CAAC;AAChD,MAAM,MAAM,GAAG,UAAU,CAAC;AAC1B,MAAM,MAAM,GAAG;IACb,SAAS;IACT,UAAU;IACV,OAAO;IACP,OAAO;IACP,KAAK;IACL,MAAM;IACN,MAAM;IACN,QAAQ;IACR,WA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|
{
|
|
2
2
|
"name": "@connextar/house",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.28.0",
|
|
4
4
|
"description": "The pieces every app we build needs and none of them should own a copy of.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -76,6 +76,10 @@
|
|
|
76
76
|
"types": "./dist/changelog/index.d.ts",
|
|
77
77
|
"default": "./dist/changelog/index.js"
|
|
78
78
|
},
|
|
79
|
+
"./calendar": {
|
|
80
|
+
"types": "./dist/calendar/index.d.ts",
|
|
81
|
+
"default": "./dist/calendar/index.js"
|
|
82
|
+
},
|
|
79
83
|
"./markdown": {
|
|
80
84
|
"types": "./dist/markdown/index.d.ts",
|
|
81
85
|
"default": "./dist/markdown/index.js"
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/calendar` — the rules of a day / week / month calendar.
|
|
3
|
+
*
|
|
4
|
+
* Views and the address they live in, what each view covers, stepping and quick ranges, the date button's words, and
|
|
5
|
+
* the day grid's rows, lanes and drag-to-move arithmetic — in a named time zone, with no markup and no database in it:
|
|
6
|
+
* Zod, and nothing else. The app owns its items, its settings, its storage and its look.
|
|
7
|
+
*/
|
|
8
|
+
export {
|
|
9
|
+
CALENDAR_VIEWS,
|
|
10
|
+
CALENDAR_VIEW_NAMES,
|
|
11
|
+
QUICK_RANGES,
|
|
12
|
+
addDays,
|
|
13
|
+
addMonths,
|
|
14
|
+
calendarQuery,
|
|
15
|
+
calendarViewSchema,
|
|
16
|
+
daysBetween,
|
|
17
|
+
endOfMonth,
|
|
18
|
+
gridSlots,
|
|
19
|
+
hhmm,
|
|
20
|
+
isCalendarView,
|
|
21
|
+
isDayKey,
|
|
22
|
+
layoutDay,
|
|
23
|
+
longDate,
|
|
24
|
+
matchingQuickRange,
|
|
25
|
+
monthTitle,
|
|
26
|
+
movedTimes,
|
|
27
|
+
parseCalendarQuery,
|
|
28
|
+
placeOf,
|
|
29
|
+
rangeHeading,
|
|
30
|
+
rangeLabel,
|
|
31
|
+
shortDate,
|
|
32
|
+
startOfMonth,
|
|
33
|
+
startOfWeek,
|
|
34
|
+
stepAnchor,
|
|
35
|
+
todayIn,
|
|
36
|
+
visibleDays,
|
|
37
|
+
visibleRange,
|
|
38
|
+
visibleWindow,
|
|
39
|
+
weekdayName,
|
|
40
|
+
weekdayOf,
|
|
41
|
+
zonedInstant,
|
|
42
|
+
type CalendarView,
|
|
43
|
+
type GridHours,
|
|
44
|
+
type QuickRange,
|
|
45
|
+
} from "./planner.js";
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The rules of a day / week / month calendar, with no markup and no database in them.
|
|
5
|
+
*
|
|
6
|
+
* Which view is showing and which day it is anchored on (`parseCalendarQuery`, `calendarQuery`), what that view covers
|
|
7
|
+
* (`visibleRange`, `visibleDays`, `visibleWindow`), how ‹ › and the quick ranges move it (`stepAnchor`,
|
|
8
|
+
* `QUICK_RANGES`), what the date button and heading say (`rangeLabel`, `rangeHeading`), and how a day's timed items
|
|
9
|
+
* are laid out and moved (`gridSlots`, `layoutDay`, `movedTimes`). Built from quickie's planner and AltEd's port of it.
|
|
10
|
+
*
|
|
11
|
+
* Days are calendar days as `yyyy-MM-dd` keys and times are wall-clock minutes past midnight in a named time zone, so a
|
|
12
|
+
* 10:00 lesson sits at 10:00 all year. Day arithmetic is done on the keys themselves — never on a local `Date` — so the
|
|
13
|
+
* server's zone and the browser's can't move a day. Weeks start on Monday (en-GB).
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export const CALENDAR_VIEW_NAMES = ["day", "week", "month"] as const;
|
|
17
|
+
export type CalendarView = (typeof CALENDAR_VIEW_NAMES)[number];
|
|
18
|
+
|
|
19
|
+
export const CALENDAR_VIEWS: readonly { value: CalendarView; label: string }[] = [
|
|
20
|
+
{ value: "day", label: "Day" },
|
|
21
|
+
{ value: "week", label: "Week" },
|
|
22
|
+
{ value: "month", label: "Month" },
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
/** A view's name, for a request body or a stored setting. */
|
|
26
|
+
export const calendarViewSchema = z.enum(CALENDAR_VIEW_NAMES);
|
|
27
|
+
|
|
28
|
+
export function isCalendarView(value: unknown): value is CalendarView {
|
|
29
|
+
return typeof value === "string" && (CALENDAR_VIEW_NAMES as readonly string[]).includes(value);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
33
|
+
// Day keys
|
|
34
|
+
|
|
35
|
+
const KEY_PATTERN = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
36
|
+
const DAY_MS = 86_400_000;
|
|
37
|
+
const MONTHS = [
|
|
38
|
+
"January",
|
|
39
|
+
"February",
|
|
40
|
+
"March",
|
|
41
|
+
"April",
|
|
42
|
+
"May",
|
|
43
|
+
"June",
|
|
44
|
+
"July",
|
|
45
|
+
"August",
|
|
46
|
+
"September",
|
|
47
|
+
"October",
|
|
48
|
+
"November",
|
|
49
|
+
"December",
|
|
50
|
+
];
|
|
51
|
+
const WEEKDAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
|
|
52
|
+
|
|
53
|
+
function utcOf(key: string): number {
|
|
54
|
+
const [, y, m, d] = KEY_PATTERN.exec(key) ?? [];
|
|
55
|
+
return Date.UTC(Number(y), Number(m) - 1, Number(d));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function keyOf(utc: number): string {
|
|
59
|
+
return new Date(utc).toISOString().slice(0, 10);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Whether `value` is a real day key between the years 1900 and 2200 — not "2026-02-30". */
|
|
63
|
+
export function isDayKey(value: unknown): value is string {
|
|
64
|
+
if (typeof value !== "string" || !KEY_PATTERN.test(value)) return false;
|
|
65
|
+
const year = Number(value.slice(0, 4));
|
|
66
|
+
return year > 1900 && year < 2200 && keyOf(utcOf(value)) === value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function addDays(key: string, days: number): string {
|
|
70
|
+
return keyOf(utcOf(key) + days * DAY_MS);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The same day `months` on, or the month's last day when it hasn't got one (31 January + 1 → 28 February). */
|
|
74
|
+
export function addMonths(key: string, months: number): string {
|
|
75
|
+
const date = new Date(utcOf(key));
|
|
76
|
+
const target = date.getUTCMonth() + months;
|
|
77
|
+
const last = new Date(Date.UTC(date.getUTCFullYear(), target + 1, 0)).getUTCDate();
|
|
78
|
+
return keyOf(Date.UTC(date.getUTCFullYear(), target, Math.min(date.getUTCDate(), last)));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** 0 for Sunday to 6 for Saturday, as `Date.getDay`. */
|
|
82
|
+
export function weekdayOf(key: string): number {
|
|
83
|
+
return new Date(utcOf(key)).getUTCDay();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The Monday of the day's week. */
|
|
87
|
+
export function startOfWeek(key: string): string {
|
|
88
|
+
return addDays(key, -((weekdayOf(key) + 6) % 7));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function startOfMonth(key: string): string {
|
|
92
|
+
return `${key.slice(0, 7)}-01`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function endOfMonth(key: string): string {
|
|
96
|
+
return addDays(addMonths(startOfMonth(key), 1), -1);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The day keys from `start` to `end`, both included. */
|
|
100
|
+
export function daysBetween(start: string, end: string): string[] {
|
|
101
|
+
const days: string[] = [];
|
|
102
|
+
for (let d = start; d <= end; d = addDays(d, 1)) days.push(d);
|
|
103
|
+
return days;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
107
|
+
// Time zones
|
|
108
|
+
|
|
109
|
+
function zoneParts(instant: Date, timeZone: string): Record<string, string> {
|
|
110
|
+
return Object.fromEntries(
|
|
111
|
+
new Intl.DateTimeFormat("en-CA", {
|
|
112
|
+
timeZone,
|
|
113
|
+
hourCycle: "h23",
|
|
114
|
+
year: "numeric",
|
|
115
|
+
month: "2-digit",
|
|
116
|
+
day: "2-digit",
|
|
117
|
+
hour: "2-digit",
|
|
118
|
+
minute: "2-digit",
|
|
119
|
+
second: "2-digit",
|
|
120
|
+
})
|
|
121
|
+
.formatToParts(instant)
|
|
122
|
+
.map((part) => [part.type, part.value]),
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function offsetMinutes(instant: Date, timeZone: string): number {
|
|
127
|
+
const p = zoneParts(instant, timeZone);
|
|
128
|
+
const asUtc = Date.UTC(
|
|
129
|
+
Number(p.year),
|
|
130
|
+
Number(p.month) - 1,
|
|
131
|
+
Number(p.day),
|
|
132
|
+
Number(p.hour),
|
|
133
|
+
Number(p.minute),
|
|
134
|
+
Number(p.second),
|
|
135
|
+
);
|
|
136
|
+
return Math.round((asUtc - Math.floor(instant.getTime() / 1000) * 1000) / 60_000);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The instant `day` at `minutes` past midnight happens in `timeZone`. Worked out twice, so a time on the day the clocks
|
|
141
|
+
* change lands right too.
|
|
142
|
+
*/
|
|
143
|
+
export function zonedInstant(day: string, minutes: number, timeZone: string): Date {
|
|
144
|
+
const wallClockAsUtc = utcOf(day) + minutes * 60_000;
|
|
145
|
+
const first = wallClockAsUtc - offsetMinutes(new Date(wallClockAsUtc), timeZone) * 60_000;
|
|
146
|
+
return new Date(wallClockAsUtc - offsetMinutes(new Date(first), timeZone) * 60_000);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Where an instant falls on the calendar in `timeZone`: its day and minutes past midnight. */
|
|
150
|
+
export function placeOf(instant: string | Date, timeZone: string): { day: string; minutes: number } {
|
|
151
|
+
const p = zoneParts(typeof instant === "string" ? new Date(instant) : instant, timeZone);
|
|
152
|
+
return { day: `${p.year}-${p.month}-${p.day}`, minutes: Number(p.hour) * 60 + Number(p.minute) };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Today in `timeZone`, as a day key. */
|
|
156
|
+
export function todayIn(timeZone: string, now: Date = new Date()): string {
|
|
157
|
+
return placeOf(now, timeZone).day;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
161
|
+
// The address
|
|
162
|
+
|
|
163
|
+
type QueryInput = URLSearchParams | Record<string, string | string[] | undefined>;
|
|
164
|
+
|
|
165
|
+
function first(query: QueryInput, name: string): string | undefined {
|
|
166
|
+
if (query instanceof URLSearchParams) return query.get(name) ?? undefined;
|
|
167
|
+
const value = query[name];
|
|
168
|
+
return Array.isArray(value) ? value[0] : value;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* The view and anchor day from `?view=&date=` — a request's search params or a page's `searchParams`. Anything
|
|
173
|
+
* unreadable is the default view (the app's, or the reader's remembered one) and today.
|
|
174
|
+
*/
|
|
175
|
+
export function parseCalendarQuery(
|
|
176
|
+
query: QueryInput,
|
|
177
|
+
todayKey: string,
|
|
178
|
+
defaultView: CalendarView = "month",
|
|
179
|
+
): { view: CalendarView; anchor: string } {
|
|
180
|
+
const view = first(query, "view");
|
|
181
|
+
const date = first(query, "date");
|
|
182
|
+
return { view: isCalendarView(view) ? view : defaultView, anchor: isDayKey(date) ? date : todayKey };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** The query string for a view and day, `view=week&date=2026-11-23`, to put after the calendar's path and a `?`. */
|
|
186
|
+
export function calendarQuery(view: CalendarView, anchor: string): string {
|
|
187
|
+
return new URLSearchParams({ view, date: anchor }).toString();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
191
|
+
// What a view covers
|
|
192
|
+
|
|
193
|
+
/** The first and last day a view shows: the day, its Monday–Sunday week, or the whole weeks around its month. */
|
|
194
|
+
export function visibleRange(view: CalendarView, anchor: string): { start: string; end: string } {
|
|
195
|
+
if (view === "day") return { start: anchor, end: anchor };
|
|
196
|
+
if (view === "week") return { start: startOfWeek(anchor), end: addDays(startOfWeek(anchor), 6) };
|
|
197
|
+
return { start: startOfWeek(startOfMonth(anchor)), end: addDays(startOfWeek(endOfMonth(anchor)), 6) };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Every day a view shows, in order; with `weekdaysOnly`, the week and month views leave out Saturday and Sunday. */
|
|
201
|
+
export function visibleDays(view: CalendarView, anchor: string, weekdaysOnly = false): string[] {
|
|
202
|
+
const { start, end } = visibleRange(view, anchor);
|
|
203
|
+
const days = daysBetween(start, end);
|
|
204
|
+
return view !== "day" && weekdaysOnly ? days.filter((d) => ![0, 6].includes(weekdayOf(d))) : days;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* The instants a view covers, `[from, to)`: midnight of its first day to midnight after its last, in `timeZone`. Read
|
|
209
|
+
* only what falls in it — and for a repeating item, every time it happens in it.
|
|
210
|
+
*/
|
|
211
|
+
export function visibleWindow(view: CalendarView, anchor: string, timeZone: string): { from: Date; to: Date } {
|
|
212
|
+
const { start, end } = visibleRange(view, anchor);
|
|
213
|
+
return { from: zonedInstant(start, 0, timeZone), to: zonedInstant(addDays(end, 1), 0, timeZone) };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** The anchor one step back or on: a day, a week, or the first of the month before or after. */
|
|
217
|
+
export function stepAnchor(view: CalendarView, anchor: string, by: 1 | -1): string {
|
|
218
|
+
if (view === "day") return addDays(anchor, by);
|
|
219
|
+
if (view === "week") return addDays(anchor, 7 * by);
|
|
220
|
+
return startOfMonth(addMonths(startOfMonth(anchor), by));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export interface QuickRange {
|
|
224
|
+
key: string;
|
|
225
|
+
label: string;
|
|
226
|
+
view: CalendarView;
|
|
227
|
+
anchor: (todayKey: string) => string;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** The ranges a date button offers beside its months. */
|
|
231
|
+
export const QUICK_RANGES: readonly QuickRange[] = [
|
|
232
|
+
{ key: "today", label: "Today", view: "day", anchor: (t) => t },
|
|
233
|
+
{ key: "tomorrow", label: "Tomorrow", view: "day", anchor: (t) => addDays(t, 1) },
|
|
234
|
+
{ key: "yesterday", label: "Yesterday", view: "day", anchor: (t) => addDays(t, -1) },
|
|
235
|
+
{ key: "this-week", label: "This week", view: "week", anchor: (t) => startOfWeek(t) },
|
|
236
|
+
{ key: "next-week", label: "Next week", view: "week", anchor: (t) => startOfWeek(addDays(t, 7)) },
|
|
237
|
+
{ key: "last-week", label: "Last week", view: "week", anchor: (t) => startOfWeek(addDays(t, -7)) },
|
|
238
|
+
{ key: "this-month", label: "This month", view: "month", anchor: (t) => startOfMonth(t) },
|
|
239
|
+
{ key: "next-month", label: "Next month", view: "month", anchor: (t) => addMonths(startOfMonth(t), 1) },
|
|
240
|
+
{ key: "last-month", label: "Last month", view: "month", anchor: (t) => addMonths(startOfMonth(t), -1) },
|
|
241
|
+
];
|
|
242
|
+
|
|
243
|
+
/** The quick range a view and anchor are, if any ("This week"), so the button can say so. */
|
|
244
|
+
export function matchingQuickRange(view: CalendarView, anchor: string, todayKey: string): QuickRange | null {
|
|
245
|
+
const shown = visibleRange(view, anchor);
|
|
246
|
+
return (
|
|
247
|
+
QUICK_RANGES.find((range) => {
|
|
248
|
+
if (range.view !== view) return false;
|
|
249
|
+
const theirs = visibleRange(range.view, range.anchor(todayKey));
|
|
250
|
+
return theirs.start === shown.start && theirs.end === shown.end;
|
|
251
|
+
}) ?? null
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
256
|
+
// Words
|
|
257
|
+
|
|
258
|
+
const pad = (n: number) => String(n).padStart(2, "0");
|
|
259
|
+
|
|
260
|
+
function parts(key: string) {
|
|
261
|
+
const date = new Date(utcOf(key));
|
|
262
|
+
return { y: date.getUTCFullYear(), m: date.getUTCMonth(), d: date.getUTCDate(), w: date.getUTCDay() };
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** "12/11/2026". */
|
|
266
|
+
export function shortDate(key: string): string {
|
|
267
|
+
const { y, m, d } = parts(key);
|
|
268
|
+
return `${pad(d)}/${pad(m + 1)}/${y}`;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** "Thursday 12 November 2026". */
|
|
272
|
+
export function longDate(key: string): string {
|
|
273
|
+
const { y, m, d, w } = parts(key);
|
|
274
|
+
return `${WEEKDAYS[w]} ${d} ${MONTHS[m]} ${y}`;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** "November 2026". */
|
|
278
|
+
export function monthTitle(key: string): string {
|
|
279
|
+
const { y, m } = parts(key);
|
|
280
|
+
return `${MONTHS[m]} ${y}`;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** "Mon", "Tue"… for a day key; `long` gives "Monday". */
|
|
284
|
+
export function weekdayName(key: string, long = false): string {
|
|
285
|
+
const name = WEEKDAYS[weekdayOf(key)] ?? "";
|
|
286
|
+
return long ? name : name.slice(0, 3);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* What the date button says: the quick range's name when it is one ("Last week"), otherwise the dates —
|
|
291
|
+
* "Thu 12/11/2026", "02/11/2026 – 08/11/2026" or "November 2026".
|
|
292
|
+
*/
|
|
293
|
+
export function rangeLabel(view: CalendarView, anchor: string, todayKey: string): string {
|
|
294
|
+
const quick = matchingQuickRange(view, anchor, todayKey);
|
|
295
|
+
if (quick) return quick.label;
|
|
296
|
+
if (view === "day") return `${weekdayName(anchor)} ${shortDate(anchor)}`;
|
|
297
|
+
if (view === "month") return monthTitle(anchor);
|
|
298
|
+
const { start, end } = visibleRange("week", anchor);
|
|
299
|
+
return `${shortDate(start)} – ${shortDate(end)}`;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** The heading over the grid: "Thursday 12 November 2026", "2 – 8 Nov 2026", "30 Nov – 6 Dec 2026", "November 2026". */
|
|
303
|
+
export function rangeHeading(view: CalendarView, anchor: string): string {
|
|
304
|
+
if (view === "day") return longDate(anchor);
|
|
305
|
+
if (view === "month") return monthTitle(anchor);
|
|
306
|
+
const { start, end } = visibleRange("week", anchor);
|
|
307
|
+
const s = parts(start);
|
|
308
|
+
const e = parts(end);
|
|
309
|
+
const mon = (m: number) => (MONTHS[m] ?? "").slice(0, 3);
|
|
310
|
+
if (s.y !== e.y) return `${s.d} ${mon(s.m)} ${s.y} – ${e.d} ${mon(e.m)} ${e.y}`;
|
|
311
|
+
return s.m === e.m ? `${s.d} – ${e.d} ${mon(e.m)} ${e.y}` : `${s.d} ${mon(s.m)} – ${e.d} ${mon(e.m)} ${e.y}`;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** Minutes past midnight as `HH:mm`. */
|
|
315
|
+
export function hhmm(minutes: number): string {
|
|
316
|
+
return `${pad(Math.floor(minutes / 60))}:${pad(minutes % 60)}`;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
320
|
+
// The day and week grids
|
|
321
|
+
|
|
322
|
+
export interface GridHours {
|
|
323
|
+
/** The first hour the grid shows, 0–23. */
|
|
324
|
+
startHour: number;
|
|
325
|
+
/** The hour it ends at, 1–24, after `startHour`. */
|
|
326
|
+
endHour: number;
|
|
327
|
+
slotMinutes: 30 | 60;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* The rows of a day or week grid, as minutes past midnight: every slot from the first hour to the last, widened to the
|
|
332
|
+
* hour to take in anything timed outside them.
|
|
333
|
+
*/
|
|
334
|
+
export function gridSlots(hours: GridHours, spans: readonly { start: number; end: number }[]): number[] {
|
|
335
|
+
const earliest = Math.min(hours.startHour * 60, ...spans.map((s) => Math.floor(s.start / 60) * 60));
|
|
336
|
+
const latest = Math.max(hours.endHour * 60, ...spans.map((s) => Math.ceil(s.end / 60) * 60));
|
|
337
|
+
const slots: number[] = [];
|
|
338
|
+
for (let m = Math.max(0, earliest); m < Math.min(24 * 60, latest); m += hours.slotMinutes) slots.push(m);
|
|
339
|
+
return slots;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Each timed item's place in its day: its lane among the ones it overlaps, and how many lanes that cluster of
|
|
344
|
+
* overlapping items has — so two lessons at 15:00 sit side by side at half width each.
|
|
345
|
+
*/
|
|
346
|
+
export function layoutDay<T extends { start: number; end: number }>(
|
|
347
|
+
items: readonly T[],
|
|
348
|
+
): (T & { lane: number; lanes: number })[] {
|
|
349
|
+
const sorted = [...items].sort((a, b) => a.start - b.start || b.end - a.end);
|
|
350
|
+
const placed: (T & { lane: number; lanes: number })[] = [];
|
|
351
|
+
let cluster: (T & { lane: number; lanes: number })[] = [];
|
|
352
|
+
let clusterEnd = -1;
|
|
353
|
+
let laneEnds: number[] = [];
|
|
354
|
+
const close = () => {
|
|
355
|
+
for (const p of cluster) p.lanes = laneEnds.length;
|
|
356
|
+
cluster = [];
|
|
357
|
+
laneEnds = [];
|
|
358
|
+
};
|
|
359
|
+
for (const item of sorted) {
|
|
360
|
+
if (item.start >= clusterEnd) close();
|
|
361
|
+
let lane = laneEnds.findIndex((end) => end <= item.start);
|
|
362
|
+
if (lane < 0) lane = laneEnds.push(item.end) - 1;
|
|
363
|
+
else laneEnds[lane] = item.end;
|
|
364
|
+
const p = { ...item, lane, lanes: 1 };
|
|
365
|
+
cluster.push(p);
|
|
366
|
+
placed.push(p);
|
|
367
|
+
clusterEnd = Math.max(clusterEnd, item.end);
|
|
368
|
+
}
|
|
369
|
+
close();
|
|
370
|
+
return placed;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* An item's new times when it's dropped on `day` at `minutes` past midnight in `timeZone` — or, with `minutes` null (a
|
|
375
|
+
* month cell), at its own time of day — keeping its length. ISO strings in, ISO strings out.
|
|
376
|
+
*/
|
|
377
|
+
export function movedTimes(
|
|
378
|
+
item: { startTime: string; endTime: string },
|
|
379
|
+
day: string,
|
|
380
|
+
minutes: number | null,
|
|
381
|
+
timeZone: string,
|
|
382
|
+
): { startTime: string; endTime: string } {
|
|
383
|
+
const start = new Date(item.startTime);
|
|
384
|
+
const length = new Date(item.endTime).getTime() - start.getTime();
|
|
385
|
+
const next = zonedInstant(day, minutes ?? placeOf(start, timeZone).minutes, timeZone);
|
|
386
|
+
return { startTime: next.toISOString(), endTime: new Date(next.getTime() + length).toISOString() };
|
|
387
|
+
}
|