@tristan2828/ui-foundation 4.118.0 → 4.119.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/bin/ui-foundation.mjs +28 -3
- package/conventions/docs/add-an-entity.md +24 -19
- package/conventions/docs/cell-patterns.md +8 -4
- package/conventions/docs/entity-plan-template.md +3 -1
- package/conventions/docs/reminders.md +29 -1
- package/dist/api/schema.d.ts +1 -1
- package/dist/components/app/edit-in-place-presets.d.ts +5 -1
- package/dist/components/app/edit-in-place-presets.js +2 -2
- package/dist/components/app/recurrence-field.d.ts +15 -2
- package/dist/components/app/recurrence-field.js +54 -13
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/lib/recurrence-format.d.ts +13 -4
- package/dist/lib/recurrence-format.js +14 -7
- package/dist/lib/recurrence-phrase.d.ts +24 -2
- package/dist/lib/recurrence-phrase.js +26 -8
- package/dist/lib/recurrence.d.ts +16 -1
- package/dist/lib/recurrence.js +29 -2
- package/openapi/foundation.yaml +9 -2
- package/package.json +1 -1
package/bin/ui-foundation.mjs
CHANGED
|
@@ -143,7 +143,7 @@ function sync({ check }) {
|
|
|
143
143
|
// ---------------------------------------------------------- check-contract
|
|
144
144
|
|
|
145
145
|
// Wording may differ between the app's spec and the foundation's; shapes may not.
|
|
146
|
-
const IGNORED_KEYS = new Set(['description', 'summary', 'example', 'examples', 'tags', 'x-optional'])
|
|
146
|
+
const IGNORED_KEYS = new Set(['description', 'summary', 'example', 'examples', 'tags', 'x-optional', 'x-earlier-patterns'])
|
|
147
147
|
|
|
148
148
|
function normalise(value) {
|
|
149
149
|
if (Array.isArray(value)) return value.map(normalise)
|
|
@@ -160,6 +160,31 @@ function normalise(value) {
|
|
|
160
160
|
|
|
161
161
|
const same = (a, b) => JSON.stringify(normalise(a)) === JSON.stringify(normalise(b))
|
|
162
162
|
|
|
163
|
+
// A pattern the foundation widened keeps the ones before it
|
|
164
|
+
// (`x-earlier-patterns`, Recurrence's rule since 4.119), and an app's copy
|
|
165
|
+
// with an earlier one still matches: it allows less, so the package reads
|
|
166
|
+
// everything it can hold, and the app copies the new one when it wants
|
|
167
|
+
// what that allows. The nth earlier version takes every such node's nth.
|
|
168
|
+
function withEarlierPattern(value, nth) {
|
|
169
|
+
if (Array.isArray(value)) return value.map((each) => withEarlierPattern(each, nth))
|
|
170
|
+
if (!value || typeof value !== 'object') return value
|
|
171
|
+
const out = Object.fromEntries(Object.entries(value).map(([key, child]) => [key, withEarlierPattern(child, nth)]))
|
|
172
|
+
const earlier = value['x-earlier-patterns']
|
|
173
|
+
if (Array.isArray(earlier) && nth < earlier.length) out.pattern = earlier[nth]
|
|
174
|
+
return out
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function earlierCount(value) {
|
|
178
|
+
if (Array.isArray(value)) return Math.max(0, ...value.map(earlierCount))
|
|
179
|
+
if (!value || typeof value !== 'object') return 0
|
|
180
|
+
const own = Array.isArray(value['x-earlier-patterns']) ? value['x-earlier-patterns'].length : 0
|
|
181
|
+
return Math.max(own, ...Object.values(value).map(earlierCount))
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const matches = (app, foundation) =>
|
|
185
|
+
same(app, foundation) ||
|
|
186
|
+
Array.from({ length: earlierCount(foundation) }, (_, nth) => withEarlierPattern(foundation, nth)).some((earlier) => same(app, earlier))
|
|
187
|
+
|
|
163
188
|
// Every component a value reaches through $refs, following refs inside the
|
|
164
189
|
// components themselves, as 'section/name' keys.
|
|
165
190
|
function reachableComponents(roots, components) {
|
|
@@ -207,7 +232,7 @@ function checkContract(specPath) {
|
|
|
207
232
|
const appItem = app.paths?.[route]
|
|
208
233
|
if (appItem === undefined) {
|
|
209
234
|
if (!item['x-optional']) problems.push(`missing path ${route}`)
|
|
210
|
-
} else if (!
|
|
235
|
+
} else if (!matches(appItem, item)) {
|
|
211
236
|
problems.push(`path ${route} differs`)
|
|
212
237
|
}
|
|
213
238
|
}
|
|
@@ -224,7 +249,7 @@ function checkContract(specPath) {
|
|
|
224
249
|
if (!entry['x-optional'] && !optionalOnly.has(`${section}/${name}`)) {
|
|
225
250
|
problems.push(`missing components.${section}.${name}`)
|
|
226
251
|
}
|
|
227
|
-
} else if (!
|
|
252
|
+
} else if (!matches(appEntry, entry)) {
|
|
228
253
|
problems.push(`components.${section}.${name} differs`)
|
|
229
254
|
}
|
|
230
255
|
}
|
|
@@ -4,29 +4,29 @@
|
|
|
4
4
|
## Index
|
|
5
5
|
|
|
6
6
|
- [The reference files](#the-reference-files) · lines 45–67
|
|
7
|
-
- [Before anything: the entity plan](#before-anything-the-entity-plan) · lines 69–
|
|
7
|
+
- [Before anything: the entity plan](#before-anything-the-entity-plan) · lines 69–855
|
|
8
8
|
- Step 1: Add `<Entity>` to `openapi.yaml` · lines 111–118
|
|
9
9
|
- Step 2: `npm run gen:api` · lines 119–120
|
|
10
10
|
- Step 3: Add gateway tests in `tests/gateway/<entity>.test.ts`, derived from the spec · lines 121–132
|
|
11
11
|
- Step 4: Add MSW handlers in `src/mocks/<entity>.ts` · lines 133–142
|
|
12
12
|
- Step 5: Add a gateway module in `src/api/gateway/<entity>.ts` · lines 143–149
|
|
13
|
-
- Step 6: Copy the reference files, one for one, not just the two screens · lines 150–
|
|
14
|
-
- Step 7: Register routes · lines
|
|
15
|
-
- Step 8: Add Playwright specs for all three screens · lines
|
|
16
|
-
- Step 9: Register the nav entry and route names · lines
|
|
17
|
-
- Step 10: `npm run verify`. · lines
|
|
18
|
-
- [How a list opens a record](#how-a-list-opens-a-record) · lines
|
|
19
|
-
- [The record view](#the-record-view) · lines
|
|
20
|
-
- [Forms](#forms) · lines
|
|
21
|
-
- [Dialogs](#dialogs) · lines
|
|
22
|
-
- [Toasts and feedback](#toasts-and-feedback) · lines
|
|
23
|
-
- [What not to copy](#what-not-to-copy) · lines
|
|
24
|
-
- [After a commit, in the backend](#after-a-commit-in-the-backend) · lines
|
|
25
|
-
- [Deleting](#deleting) · lines
|
|
26
|
-
- [Fields only a script writes](#fields-only-a-script-writes) · lines
|
|
27
|
-
- [Searching one part of the app](#searching-one-part-of-the-app) · lines
|
|
28
|
-
- [What a chat is told about an entity](#what-a-chat-is-told-about-an-entity) · lines
|
|
29
|
-
- [Behind a sign-in proxy](#behind-a-sign-in-proxy) · lines
|
|
13
|
+
- Step 6: Copy the reference files, one for one, not just the two screens · lines 150–743
|
|
14
|
+
- Step 7: Register routes · lines 744–759
|
|
15
|
+
- Step 8: Add Playwright specs for all three screens · lines 760–837
|
|
16
|
+
- Step 9: Register the nav entry and route names · lines 838–852
|
|
17
|
+
- Step 10: `npm run verify`. · lines 853–855
|
|
18
|
+
- [How a list opens a record](#how-a-list-opens-a-record) · lines 857–1303
|
|
19
|
+
- [The record view](#the-record-view) · lines 1010–1148
|
|
20
|
+
- [Forms](#forms) · lines 1150–1220
|
|
21
|
+
- [Dialogs](#dialogs) · lines 1222–1253
|
|
22
|
+
- [Toasts and feedback](#toasts-and-feedback) · lines 1255–1303
|
|
23
|
+
- [What not to copy](#what-not-to-copy) · lines 1305–1337
|
|
24
|
+
- [After a commit, in the backend](#after-a-commit-in-the-backend) · lines 1339–1356
|
|
25
|
+
- [Deleting](#deleting) · lines 1358–1389
|
|
26
|
+
- [Fields only a script writes](#fields-only-a-script-writes) · lines 1391–1412
|
|
27
|
+
- [Searching one part of the app](#searching-one-part-of-the-app) · lines 1414–1423
|
|
28
|
+
- [What a chat is told about an entity](#what-a-chat-is-told-about-an-entity) · lines 1426–1468
|
|
29
|
+
- [Behind a sign-in proxy](#behind-a-sign-in-proxy) · lines 1470–1506
|
|
30
30
|
|
|
31
31
|
<!-- /index -->
|
|
32
32
|
|
|
@@ -265,7 +265,12 @@ is a filled-in example while the Widgets demo is still here).
|
|
|
265
265
|
the schema checks it with `recurrenceProblem(value)`, and a phrase the
|
|
266
266
|
field can't read gives `null`, so a required one is refused. The table
|
|
267
267
|
shows `formatRecurrence(value)` and a Next column (cell pattern 27);
|
|
268
|
-
editing in place is `inPlace.recurrence({ value, save })`.
|
|
268
|
+
editing in place is `inPlace.recurrence({ value, save })`. A record
|
|
269
|
+
whose times are fields of its own (an event's start and end) gives the
|
|
270
|
+
field and the preset `times={false}`: dates only, no "at". An app whose
|
|
271
|
+
weeks start on Sunday gives them `weekStart="SU"`, which adds
|
|
272
|
+
`WKST=SU` to the weekly rules they build (reminders.md "Repeating
|
|
273
|
+
reminders" says when that moves a day). The backend
|
|
269
274
|
column is JSON, read and checked with
|
|
270
275
|
`foundation_backend.recurrence.Recurrence` (a 422 on the field for a
|
|
271
276
|
rule it refuses); `next_after` and `occurrences` say when it falls
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
- [25. A list's tools: views, filters, columns, row actions, a phone's rows](#25-a-lists-tools-views-filters-columns-row-actions-a-phones-rows) · lines 1965–2184
|
|
44
44
|
- [The framed look, and sibling lists beside the title](#the-framed-look-and-sibling-lists-beside-the-title) · lines 2113–2184
|
|
45
45
|
- [26. A record's related list, capped](#26-a-records-related-list-capped) · lines 2186–2222
|
|
46
|
-
- [27. A repeat and its next time](#27-a-repeat-and-its-next-time) · lines 2224–
|
|
46
|
+
- [27. A repeat and its next time](#27-a-repeat-and-its-next-time) · lines 2224–2273
|
|
47
47
|
|
|
48
48
|
<!-- /index -->
|
|
49
49
|
|
|
@@ -2257,10 +2257,14 @@ import { formatOccurrence, formatRecurrence, nextOccurrence, EmptyText } from '@
|
|
|
2257
2257
|
- **The label** reads "Every 2nd Tue at 9:00 AM", "Every Mon and Thu at
|
|
2258
2258
|
7:00 AM and 7:30 PM", "Every 30 days after done, at 9:00 AM" (from
|
|
2259
2259
|
completion), "…, until Mar 1, 2027", "…, 10 times". `times: false`
|
|
2260
|
-
leaves the times out, for a table with its own time column.
|
|
2261
|
-
|
|
2260
|
+
leaves the times out, for a table with its own time column. An app
|
|
2261
|
+
whose weeks start on Sunday passes `weekStart: 'SU'`: an every-N-weeks
|
|
2262
|
+
rule whose WKST isn't the app's says so (", weeks start Mon"), and a
|
|
2263
|
+
week's days read from its first ("Sun and Wed"). A rule it can't read
|
|
2264
|
+
shows as stored, never blank.
|
|
2262
2265
|
- **Next** is "Tue, Dec 8, 9:00 AM", the year only when it isn't this
|
|
2263
|
-
one
|
|
2266
|
+
one, or "Tue, Dec 8" with `{ time: false }` (a record whose times are
|
|
2267
|
+
its own); a rule that has ended (UNTIL passed, COUNT used up) says Ended, in
|
|
2264
2268
|
muted text. For a `fromCompletion` repeat whose next depends on being
|
|
2265
2269
|
done, use the server's `next_at` if it sends one; `nextOccurrence`
|
|
2266
2270
|
counts from the schedule's start.
|
|
@@ -57,7 +57,9 @@ One or two sentences: what one record is, and who uses the screens.
|
|
|
57
57
|
year, no year: `'MM-DD'`; the foundation's `MonthDayField`; say
|
|
58
58
|
whether it's optional), `recurrence` (how it repeats: the
|
|
59
59
|
foundation's `Recurrence` schema and `RecurrenceField`; say whether
|
|
60
|
-
it's optional
|
|
60
|
+
it's optional, whether its times are the record's own fields
|
|
61
|
+
(`times={false}`), and the day the app's weeks start on when it
|
|
62
|
+
isn't Monday (`weekStart`)).
|
|
61
63
|
- *Not supported yet:* a yes/no that can also be unset (three states),
|
|
62
64
|
file, and anything else.
|
|
63
65
|
Write it anyway with a note; the playbook will stop and raise it
|
|
@@ -126,10 +126,29 @@ the same rules and agree on every next occurrence.
|
|
|
126
126
|
- **The value**: `{ rule, start, times, timeZone, fromCompletion }`.
|
|
127
127
|
`rule` is RFC 5545's RRULE without DTSTART (`FREQ=MONTHLY;BYDAY=2TU`):
|
|
128
128
|
FREQ daily to yearly, INTERVAL, BYDAY with an nth (`2TU`, `-1FR`),
|
|
129
|
-
BYMONTHDAY (`-1` the last day), BYMONTH, UNTIL (a date) or COUNT
|
|
129
|
+
BYMONTHDAY (`-1` the last day), BYMONTH, UNTIL (a date) or COUNT, and
|
|
130
|
+
WKST (4.119, below).
|
|
130
131
|
`start` is the first day it can fall on; each of `times` is an
|
|
131
132
|
occurrence on every day the rule falls on, read in `timeZone`. A day a
|
|
132
133
|
month lacks is skipped (the 31st, Feb 29), as in RFC 5545.
|
|
134
|
+
- **Which day a week starts on (WKST, 4.119)**: weeks start on Monday
|
|
135
|
+
unless the rule says `WKST=SU` (or any weekday; `WKST=MO` is the
|
|
136
|
+
default and isn't stored). It moves only an every-N-weeks rule with
|
|
137
|
+
days: from Wednesday 2026-11-04, `FREQ=WEEKLY;INTERVAL=2;BYDAY=SU,WE`
|
|
138
|
+
next falls on Sunday the 8th (its Monday week runs to the 8th), and
|
|
139
|
+
with `;WKST=SU` on Sunday the 15th (its Sunday week began on the 1st,
|
|
140
|
+
before the start). Both sides agree, as python-dateutil and `rrule` do.
|
|
141
|
+
An app whose weeks start on Sunday gives `RecurrenceField` and
|
|
142
|
+
`inPlace.recurrence` `weekStart="SU"`: every weekly rule they build
|
|
143
|
+
(the presets and what's typed) gets `WKST=SU`, and "weeks start mon"
|
|
144
|
+
typed says otherwise. Give the same `weekStart` to `formatRecurrence`
|
|
145
|
+
in a Repeats cell, which otherwise says ", weeks start Sun" on a rule
|
|
146
|
+
where it matters. A rule stored without WKST keeps counting from
|
|
147
|
+
Monday; an app that wants its old every-N-weeks rules counted from
|
|
148
|
+
Sunday rewrites them in a migration of its own (append `;WKST=SU`).
|
|
149
|
+
The rule pattern in `openapi/foundation.yaml` allows WKST since 4.119:
|
|
150
|
+
an app copies the new `Recurrence` when it stores one (an older copy
|
|
151
|
+
still passes `check-contract`, but its mocks and spec refuse WKST).
|
|
133
152
|
- **The columns**: the app's reminders model inherits
|
|
134
153
|
`foundation_backend.recurrence_models.RecurringReminderFields` and its
|
|
135
154
|
migration adds them: `recurrence` (JSON), `next_at`, `sent_at`,
|
|
@@ -164,6 +183,15 @@ the same rules and agree on every next occurrence.
|
|
|
164
183
|
list `formatRecurrence` for a Repeats column and `nextOccurrence` with
|
|
165
184
|
`formatOccurrence` for a Next one (cell-patterns.md "A repeat and its
|
|
166
185
|
next time").
|
|
186
|
+
- **A record whose times are its own fields** (a calendar event with a
|
|
187
|
+
start and an end, 4.119): `RecurrenceField times={false}` (and
|
|
188
|
+
`inPlace.recurrence({ times: false })`) says only dates: the preview
|
|
189
|
+
lists the next days from today on ("Sun, Nov 15"), the label and the
|
|
190
|
+
box leave the times out, and "at 9am" typed is refused. The value still
|
|
191
|
+
carries `times`, as the schema needs one: the value's own, else
|
|
192
|
+
`defaultTimes` (09:00 when not given; `['00:00']` reads best for a
|
|
193
|
+
calendar). In the list, `formatRecurrence(value, { times: false })` and
|
|
194
|
+
`formatOccurrence(next, zone, { time: false })`.
|
|
167
195
|
|
|
168
196
|
## A Done button on the notification (4.104)
|
|
169
197
|
|
package/dist/api/schema.d.ts
CHANGED
|
@@ -261,7 +261,7 @@ export interface components {
|
|
|
261
261
|
};
|
|
262
262
|
/** @description A repeating schedule (RecurrenceField, foundation_backend.recurrence). The rule's days, each at every one of its times, read in its zone. */
|
|
263
263
|
Recurrence: {
|
|
264
|
-
/** @description RFC 5545's RRULE value, without DTSTART: FREQ (daily, weekly, monthly, yearly), INTERVAL, BYMONTH, BYMONTHDAY (-1 is the last day), BYDAY (an nth for monthly and yearly: 2TU, -1FR), UNTIL (a date, YYYYMMDD, the last day it can fall on) or COUNT (occurrences, each time of each day). "FREQ=MONTHLY;BYDAY=2TU". */
|
|
264
|
+
/** @description RFC 5545's RRULE value, without DTSTART: FREQ (daily, weekly, monthly, yearly), INTERVAL, BYMONTH, BYMONTHDAY (-1 is the last day), BYDAY (an nth for monthly and yearly: 2TU, -1FR), UNTIL (a date, YYYYMMDD, the last day it can fall on) or COUNT (occurrences, each time of each day), and WKST (the day weeks start on, MO when left out: it moves an every-N-weeks rule's weeks). "FREQ=MONTHLY;BYDAY=2TU", "FREQ=WEEKLY;INTERVAL=2;BYDAY=SU,WE;WKST=SU". */
|
|
265
265
|
rule: string;
|
|
266
266
|
/**
|
|
267
267
|
* Format: date
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ReactNode } from 'react';
|
|
2
2
|
import { type MonthDay } from '../../lib/month-day.js';
|
|
3
3
|
import { type PlainDate } from '../../lib/plain-date.js';
|
|
4
|
-
import { type Recurrence } from '../../lib/recurrence.js';
|
|
4
|
+
import { type Recurrence, type WeekdayCode } from '../../lib/recurrence.js';
|
|
5
5
|
import { type ChoiceButtonsOption } from './choice-buttons.js';
|
|
6
6
|
import type { EditInPlace } from './edit-in-place.js';
|
|
7
7
|
/** What the value saves as: with `required: true` never `null` (a blank is refused). */
|
|
@@ -92,6 +92,10 @@ export type InPlaceRecurrenceConfig<TRequired extends boolean = boolean> = {
|
|
|
92
92
|
timeZone?: string;
|
|
93
93
|
/** The times a new repeat starts with. Defaults to 09:00. */
|
|
94
94
|
defaultTimes?: string[];
|
|
95
|
+
/** RecurrenceField's `times` (4.114): false for a record whose times are its own fields; the value at rest says no times either. */
|
|
96
|
+
times?: boolean;
|
|
97
|
+
/** RecurrenceField's `weekStart` (4.114): `'SU'` for an app whose weeks start on Sunday. */
|
|
98
|
+
weekStart?: WeekdayCode;
|
|
95
99
|
/** Today, `'YYYY-MM-DD'`, and "now", for a fixed preview (tests). Default to the clock. */
|
|
96
100
|
today?: string;
|
|
97
101
|
now?: Date;
|
|
@@ -190,8 +190,8 @@ function recurrenceKind(config) {
|
|
|
190
190
|
const problem = recurrenceProblem(draft);
|
|
191
191
|
return problem ? { ok: false, message: problem } : { ok: true, value: normalizeRecurrence(draft) };
|
|
192
192
|
},
|
|
193
|
-
show: (value) => (value ? formatRecurrence(value, { locale: config.locale }) : null),
|
|
194
|
-
control: (props) => (_jsx(RecurrenceField, { id: props.id, value: props.value, onChange: props.onChange, onProblem: props.onProblem, timeZone: config.timeZone, defaultTimes: config.defaultTimes, today: config.today, now: config.now, locale: config.locale, invalid: props.invalid, readOnly: props.disabled, "aria-label": props.label, "aria-describedby": props.describedBy })),
|
|
193
|
+
show: (value) => (value ? formatRecurrence(value, { locale: config.locale, times: config.times, weekStart: config.weekStart }) : null),
|
|
194
|
+
control: (props) => (_jsx(RecurrenceField, { id: props.id, value: props.value, onChange: props.onChange, onProblem: props.onProblem, timeZone: config.timeZone, defaultTimes: config.defaultTimes, times: config.times, weekStart: config.weekStart, today: config.today, now: config.now, locale: config.locale, invalid: props.invalid, readOnly: props.disabled, "aria-label": props.label, "aria-describedby": props.describedBy })),
|
|
195
195
|
};
|
|
196
196
|
}
|
|
197
197
|
function selectKind(config) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Recurrence } from '../../lib/recurrence.js';
|
|
1
|
+
import { type Recurrence, type WeekdayCode } from '../../lib/recurrence.js';
|
|
2
2
|
export type RecurrenceFieldProps = {
|
|
3
3
|
/** The phrase box's id, for the field's `<FieldLabel htmlFor>`. */
|
|
4
4
|
id?: string;
|
|
@@ -18,6 +18,19 @@ export type RecurrenceFieldProps = {
|
|
|
18
18
|
previewCount?: number;
|
|
19
19
|
/** Show the preset buttons (the default). */
|
|
20
20
|
presets?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Show times of day (the default). False, for a record whose times are
|
|
23
|
+
* its own fields: the preview lists dates only, from today on, the label
|
|
24
|
+
* and the box leave the times out, and "at …" in the box is refused.
|
|
25
|
+
*/
|
|
26
|
+
times?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* The day the app's weeks start on (`'MO'` to `'SU'`; Monday when left
|
|
29
|
+
* out). Not Monday: every weekly rule the presets and the box build gets
|
|
30
|
+
* it as WKST (`'SU'` adds `WKST=SU`), which moves an every-N-weeks rule's
|
|
31
|
+
* weeks.
|
|
32
|
+
*/
|
|
33
|
+
weekStart?: WeekdayCode;
|
|
21
34
|
locale?: string;
|
|
22
35
|
/** The value was refused: `aria-invalid` on the box. */
|
|
23
36
|
invalid?: boolean;
|
|
@@ -33,4 +46,4 @@ export type RecurrenceFieldProps = {
|
|
|
33
46
|
'aria-label'?: string;
|
|
34
47
|
'aria-describedby'?: string;
|
|
35
48
|
};
|
|
36
|
-
export declare function RecurrenceField({ id, value, onChange, timeZone, defaultTimes, today, now, previewCount, presets, locale, invalid, readOnly, onProblem, onBlur, className, 'aria-label': ariaLabel, 'aria-describedby': describedBy, }: RecurrenceFieldProps): import("react").JSX.Element;
|
|
49
|
+
export declare function RecurrenceField({ id, value, onChange, timeZone, defaultTimes, today, now, previewCount, presets, times, weekStart, locale, invalid, readOnly, onProblem, onBlur, className, 'aria-label': ariaLabel, 'aria-describedby': describedBy, }: RecurrenceFieldProps): import("react").JSX.Element;
|
|
@@ -15,6 +15,17 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
|
|
|
15
15
|
// - **The preview**: the rule's label ("Every 2nd Tue at 9:00 AM") and its
|
|
16
16
|
// next five occurrences, in the rule's zone, or why it can't be read.
|
|
17
17
|
//
|
|
18
|
+
// Two options an app's own records may want (4.114):
|
|
19
|
+
// - `times={false}`, for a record whose times are fields of its own (a
|
|
20
|
+
// calendar's events): the preview lists dates only ("Tue, Dec 8", from
|
|
21
|
+
// today on), the label and the box say no times, and typing "at 9am" is
|
|
22
|
+
// refused. The value still carries `times` (the value's, `defaultTimes`
|
|
23
|
+
// or 09:00), as the contract needs one.
|
|
24
|
+
// - `weekStart`, the day the app's weeks start on: `'SU'` adds `WKST=SU`
|
|
25
|
+
// to every weekly rule the presets and the box build, so "every other
|
|
26
|
+
// week on sun, wed" counts its weeks Sunday to Saturday. A rule whose
|
|
27
|
+
// week start differs, where it matters, says so ("weeks start mon").
|
|
28
|
+
//
|
|
18
29
|
// One control for the form (a react-hook-form `Controller`) and for
|
|
19
30
|
// editing in place (`inPlace.recurrence`: Enter in the box or leaving the
|
|
20
31
|
// field saves). The box is the field's labelled control (`id`); the
|
|
@@ -26,15 +37,16 @@ import { Input } from '../ui/input.js';
|
|
|
26
37
|
import { ToggleGroup, ToggleGroupItem } from '../ui/toggle-group.js';
|
|
27
38
|
import { formatOccurrence, formatRecurrence, todayIn } from '../../lib/recurrence-format.js';
|
|
28
39
|
import { ordinalWord, parseRecurrencePhrase, recurrencePhrase } from '../../lib/recurrence-phrase.js';
|
|
29
|
-
import { nextOccurrences, normalizeRecurrence, recurrenceProblem, WEEKDAY_CODES } from '../../lib/recurrence.js';
|
|
40
|
+
import { atLocal, nextOccurrences, normalizeRecurrence, recurrenceProblem, wallClock, WEEKDAY_CODES, } from '../../lib/recurrence.js';
|
|
30
41
|
function weekdayOf(day) {
|
|
31
42
|
return (new Date(`${day}T00:00:00Z`).getUTCDay() + 6) % 7;
|
|
32
43
|
}
|
|
33
44
|
function dayName(day, locale) {
|
|
34
45
|
return new Intl.DateTimeFormat(locale, { weekday: 'short', timeZone: 'UTC' }).format(Date.UTC(2024, 0, 1 + day));
|
|
35
46
|
}
|
|
36
|
-
/** The presets for a start day: each a rule built from that day. */
|
|
37
|
-
function presetsFor(start, locale) {
|
|
47
|
+
/** The presets for a start day: each a rule built from that day; a weekly one with the app's week start. */
|
|
48
|
+
function presetsFor(start, locale, weekStart) {
|
|
49
|
+
const wkst = weekStart === 'MO' ? '' : `;WKST=${weekStart}`;
|
|
38
50
|
const [year, month, date] = start.split('-').map(Number);
|
|
39
51
|
const weekday = weekdayOf(start);
|
|
40
52
|
const lastOfMonth = new Date(Date.UTC(year, month, 0)).getUTCDate();
|
|
@@ -44,8 +56,8 @@ function presetsFor(start, locale) {
|
|
|
44
56
|
const yearDay = new Intl.DateTimeFormat(locale, { month: 'short', day: 'numeric', timeZone: 'UTC' }).format(Date.UTC(2024, month - 1, date));
|
|
45
57
|
return [
|
|
46
58
|
{ id: 'daily', label: 'Daily', rule: 'FREQ=DAILY' },
|
|
47
|
-
{ id: 'weekdays', label: 'Weekdays', rule:
|
|
48
|
-
{ id: 'weekly', label: `Weekly on ${dayName(weekday, locale)}`, rule: `FREQ=WEEKLY;BYDAY=${code}` },
|
|
59
|
+
{ id: 'weekdays', label: 'Weekdays', rule: `FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR${wkst}` },
|
|
60
|
+
{ id: 'weekly', label: `Weekly on ${dayName(weekday, locale)}`, rule: `FREQ=WEEKLY;BYDAY=${code}${wkst}` },
|
|
49
61
|
{ id: 'monthly', label: `Monthly on the ${ordinalWord(date)}`, rule: `FREQ=MONTHLY;BYMONTHDAY=${date}` },
|
|
50
62
|
{
|
|
51
63
|
id: 'nth',
|
|
@@ -65,7 +77,27 @@ function browserZone() {
|
|
|
65
77
|
}
|
|
66
78
|
}
|
|
67
79
|
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
68
|
-
|
|
80
|
+
// Every preset repeats each week or less often, where WKST changes nothing:
|
|
81
|
+
// a stored weekly rule with or without one is still its preset.
|
|
82
|
+
const sameDays = (a, b) => a.replace(/;WKST=[A-Z]{2}/, '') === b.replace(/;WKST=[A-Z]{2}/, '');
|
|
83
|
+
/** The next `count` days the value falls on, from today (in its zone) on: a date-only preview. */
|
|
84
|
+
function nextDays(value, today, count) {
|
|
85
|
+
const days = [];
|
|
86
|
+
const seen = new Set();
|
|
87
|
+
const from = new Date(atLocal(today, '00:00', value.timeZone).getTime() - 1);
|
|
88
|
+
// Each day once, however many times it has: ask for enough to fill the list.
|
|
89
|
+
for (const moment of nextOccurrences(value, from, count * value.times.length)) {
|
|
90
|
+
const day = wallClock(moment, value.timeZone).day;
|
|
91
|
+
if (seen.has(day))
|
|
92
|
+
continue;
|
|
93
|
+
seen.add(day);
|
|
94
|
+
days.push(moment);
|
|
95
|
+
if (days.length === count)
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
return days;
|
|
99
|
+
}
|
|
100
|
+
export function RecurrenceField({ id, value, onChange, timeZone, defaultTimes, today, now, previewCount = 5, presets = true, times = true, weekStart = 'MO', locale, invalid = false, readOnly = false, onProblem, onBlur, className, 'aria-label': ariaLabel, 'aria-describedby': describedBy, }) {
|
|
69
101
|
const zone = value?.timeZone ?? timeZone ?? browserZone();
|
|
70
102
|
const day = today ?? todayIn(zone, now);
|
|
71
103
|
const ownId = useId();
|
|
@@ -75,13 +107,14 @@ export function RecurrenceField({ id, value, onChange, timeZone, defaultTimes, t
|
|
|
75
107
|
const box = useRef(null);
|
|
76
108
|
// The box's text, and the value it last read (or was given). A new value
|
|
77
109
|
// from outside (a preset, the form's reset) is written back as its phrase.
|
|
78
|
-
const
|
|
110
|
+
const phraseOf = (recurrence) => recurrencePhrase(recurrence, day, { times, weekStart }) ?? '';
|
|
111
|
+
const [text, setText] = useState(() => (value ? phraseOf(value) : ''));
|
|
79
112
|
const [problem, setProblem] = useState(null);
|
|
80
113
|
const [known, setKnown] = useState(value);
|
|
81
114
|
if (!same(known, value)) {
|
|
82
115
|
setKnown(value);
|
|
83
116
|
if (value !== null || problem === null) {
|
|
84
|
-
setText(value ? (
|
|
117
|
+
setText(value ? phraseOf(value) : '');
|
|
85
118
|
setProblem(null);
|
|
86
119
|
}
|
|
87
120
|
}
|
|
@@ -102,6 +135,8 @@ export function RecurrenceField({ id, value, onChange, timeZone, defaultTimes, t
|
|
|
102
135
|
timeZone: zone,
|
|
103
136
|
times: value?.times ?? defaultTimes,
|
|
104
137
|
start: value?.start,
|
|
138
|
+
weekStart,
|
|
139
|
+
readTimes: times,
|
|
105
140
|
});
|
|
106
141
|
if (result.ok)
|
|
107
142
|
emit(result.value, null);
|
|
@@ -109,8 +144,10 @@ export function RecurrenceField({ id, value, onChange, timeZone, defaultTimes, t
|
|
|
109
144
|
emit(null, result.error);
|
|
110
145
|
};
|
|
111
146
|
const start = value?.start ?? day;
|
|
112
|
-
const options = presetsFor(start, locale);
|
|
113
|
-
const pressed = value
|
|
147
|
+
const options = presetsFor(start, locale, weekStart);
|
|
148
|
+
const pressed = value
|
|
149
|
+
? (options.find((option) => option.rule !== null && sameDays(option.rule, value.rule))?.id ?? 'custom')
|
|
150
|
+
: null;
|
|
114
151
|
const pick = (preset) => {
|
|
115
152
|
if (preset.rule === null) {
|
|
116
153
|
box.current?.focus();
|
|
@@ -123,7 +160,7 @@ export function RecurrenceField({ id, value, onChange, timeZone, defaultTimes, t
|
|
|
123
160
|
timeZone: zone,
|
|
124
161
|
fromCompletion: false,
|
|
125
162
|
});
|
|
126
|
-
setText(
|
|
163
|
+
setText(phraseOf(next));
|
|
127
164
|
emit(next, null);
|
|
128
165
|
};
|
|
129
166
|
// A press on a preset is its own: Enter there mustn't also reach editing
|
|
@@ -134,10 +171,14 @@ export function RecurrenceField({ id, value, onChange, timeZone, defaultTimes, t
|
|
|
134
171
|
};
|
|
135
172
|
const stored = value ? recurrenceProblem(value) : null;
|
|
136
173
|
const shownProblem = problem ?? stored;
|
|
137
|
-
const upcoming = value && !shownProblem && previewCount > 0
|
|
174
|
+
const upcoming = value && !shownProblem && previewCount > 0
|
|
175
|
+
? times
|
|
176
|
+
? nextOccurrences(value, now ?? new Date(), previewCount)
|
|
177
|
+
: nextDays(value, day, previewCount)
|
|
178
|
+
: [];
|
|
138
179
|
return (_jsxs("div", { "data-slot": "recurrence-field", className: cn('flex min-w-0 flex-col gap-2', className), children: [presets && (_jsx(ToggleGroup, { "data-slot": "recurrence-presets", variant: "outline", spacing: 1, "aria-label": "Presets", value: pressed ? [pressed] : [], onValueChange: (next) => {
|
|
139
180
|
const chosen = options.find((option) => option.id === (next[0] ?? pressed));
|
|
140
181
|
if (chosen)
|
|
141
182
|
pick(chosen);
|
|
142
|
-
}, onKeyDown: keepEnter, disabled: readOnly, className: "max-w-full flex-wrap", children: options.map((option) => (_jsx(ToggleGroupItem, { value: option.id, className: "h-(--control-height,--spacing(8)) px-3 font-normal text-muted-foreground aria-pressed:font-medium aria-pressed:text-foreground", children: option.label }, option.id))) })), _jsxs("div", { className: "relative", children: [_jsx(RepeatIcon, { "aria-hidden": "true", className: "pointer-events-none absolute top-1/2 left-2.5 size-4 -translate-y-1/2 text-muted-foreground" }), _jsx(Input, { ref: box, id: boxId, value: text, onChange: (event) => read(event.target.value), onBlur: onBlur, readOnly: readOnly, placeholder:
|
|
183
|
+
}, onKeyDown: keepEnter, disabled: readOnly, className: "max-w-full flex-wrap", children: options.map((option) => (_jsx(ToggleGroupItem, { value: option.id, className: "h-(--control-height,--spacing(8)) px-3 font-normal text-muted-foreground aria-pressed:font-medium aria-pressed:text-foreground", children: option.label }, option.id))) })), _jsxs("div", { className: "relative", children: [_jsx(RepeatIcon, { "aria-hidden": "true", className: "pointer-events-none absolute top-1/2 left-2.5 size-4 -translate-y-1/2 text-muted-foreground" }), _jsx(Input, { ref: box, id: boxId, value: text, onChange: (event) => read(event.target.value), onBlur: onBlur, readOnly: readOnly, placeholder: times ? 'every 2nd tue at 9am' : 'every 2nd tue', autoComplete: "off", spellCheck: false, className: "pl-8", "aria-label": ariaLabel, "aria-invalid": invalid || !!shownProblem || undefined, "aria-describedby": [shownProblem ? messageId : previewId, describedBy].filter(Boolean).join(' ') || undefined })] }), _jsx("div", { "data-slot": "recurrence-preview", "aria-live": "polite", className: "text-sm", children: shownProblem ? (_jsx("p", { id: messageId, className: "text-destructive-text", children: shownProblem })) : value ? (_jsxs("div", { id: previewId, children: [_jsx("p", { className: "font-medium text-foreground", children: formatRecurrence(value, { locale, times, weekStart }) }), previewCount > 0 && (_jsx(_Fragment, { children: upcoming.length ? (_jsx("ol", { "aria-label": "Next", className: "mt-1 flex flex-col gap-0.5 text-muted-foreground tabular-nums", children: upcoming.map((moment) => (_jsx("li", { children: formatOccurrence(moment, value.timeZone, { locale, now, time: times }) }, moment.getTime()))) })) : (_jsx("p", { className: "mt-1 text-muted-foreground", children: "No more to come." })) }))] })) : (_jsx("p", { id: previewId, className: "text-muted-foreground", children: "Doesn\u2019t repeat. Pick a preset or type how it repeats." })) })] }));
|
|
143
184
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -100,8 +100,8 @@ export { DEFAULT_IMAGE_TYPES, type ImageUpload, type ImageUploadResult } from '.
|
|
|
100
100
|
export { formatPlainDate, parsePlainDate, plainDateExamples, unreadableDateMessage, type ParsePlainDateOptions, type PlainDate, } from './lib/plain-date.js';
|
|
101
101
|
export { formatMonthDay, isMonthDay, monthDayProblem, type MonthDay } from './lib/month-day.js';
|
|
102
102
|
export { RecurrenceField, type RecurrenceFieldProps } from './components/app/recurrence-field.js';
|
|
103
|
-
export { atLocal, formatRule, nextAfterCompletion, nextOccurrence, nextOccurrences, normalizeRecurrence, parseRule, RecurrenceError, recurrenceFromWeekdays, recurrenceProblem, type Recurrence, type RuleParts, } from './lib/recurrence.js';
|
|
104
|
-
export { parseRecurrencePhrase, recurrencePhrase, type PhraseOptions, type PhraseResult, } from './lib/recurrence-phrase.js';
|
|
103
|
+
export { atLocal, formatRule, nextAfterCompletion, nextOccurrence, nextOccurrences, normalizeRecurrence, parseRule, RecurrenceError, recurrenceFromWeekdays, recurrenceProblem, weekStartMatters, type Recurrence, type RuleParts, type WeekdayCode, } from './lib/recurrence.js';
|
|
104
|
+
export { parseRecurrencePhrase, recurrencePhrase, type PhraseOptions, type PhraseResult, type RecurrencePhraseOptions, } from './lib/recurrence-phrase.js';
|
|
105
105
|
export { formatOccurrence, formatRecurrence, todayIn, type OccurrenceFormatOptions, type RecurrenceFormatOptions, } from './lib/recurrence-format.js';
|
|
106
106
|
export { ChoiceCell, LinkCell, LinkOut, NameCell, type ChoiceCellProps, type LinkCellProps, type NameCellProps, } from './components/app/row-edit-cells.js';
|
|
107
107
|
export { ModuleCard, ModuleStrip, type CategorySlot, type ModuleCardLink, type ModuleCardPlace, type ModuleCardProps, type ModuleStripMobile, type ModuleStripProps, } from './components/app/module-card.js';
|
package/dist/index.js
CHANGED
|
@@ -106,7 +106,7 @@ export { DEFAULT_IMAGE_TYPES } from './lib/image-file.js';
|
|
|
106
106
|
export { formatPlainDate, parsePlainDate, plainDateExamples, unreadableDateMessage, } from './lib/plain-date.js';
|
|
107
107
|
export { formatMonthDay, isMonthDay, monthDayProblem } from './lib/month-day.js';
|
|
108
108
|
export { RecurrenceField } from './components/app/recurrence-field.js';
|
|
109
|
-
export { atLocal, formatRule, nextAfterCompletion, nextOccurrence, nextOccurrences, normalizeRecurrence, parseRule, RecurrenceError, recurrenceFromWeekdays, recurrenceProblem, } from './lib/recurrence.js';
|
|
109
|
+
export { atLocal, formatRule, nextAfterCompletion, nextOccurrence, nextOccurrences, normalizeRecurrence, parseRule, RecurrenceError, recurrenceFromWeekdays, recurrenceProblem, weekStartMatters, } from './lib/recurrence.js';
|
|
110
110
|
export { parseRecurrencePhrase, recurrencePhrase, } from './lib/recurrence-phrase.js';
|
|
111
111
|
export { formatOccurrence, formatRecurrence, todayIn, } from './lib/recurrence-format.js';
|
|
112
112
|
export { ChoiceCell, LinkCell, LinkOut, NameCell, } from './components/app/row-edit-cells.js';
|
|
@@ -1,25 +1,34 @@
|
|
|
1
|
-
import { type Recurrence } from './recurrence.js';
|
|
1
|
+
import { type Recurrence, type WeekdayCode } from './recurrence.js';
|
|
2
2
|
export type RecurrenceFormatOptions = {
|
|
3
3
|
locale?: string;
|
|
4
4
|
/** Leave the times out ("Every 2nd Tue"), for a cell beside a separate time. */
|
|
5
5
|
times?: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* The app's week start (4.114; Monday when left out). An every-N-weeks
|
|
8
|
+
* rule whose WKST differs says so: "Every other week on Sun and Wed,
|
|
9
|
+
* weeks start Sun".
|
|
10
|
+
*/
|
|
11
|
+
weekStart?: WeekdayCode;
|
|
6
12
|
};
|
|
7
13
|
/**
|
|
8
14
|
* The repeat's label: its days, "after done" when it repeats from
|
|
9
15
|
* completion, its times, and how it ends. "Every 2nd Tue at 9:00 AM",
|
|
10
16
|
* "Every 3 days after done, at 9:00 AM", "Every weekday at 8:00 AM and
|
|
11
|
-
* 5:30 PM, until Mar 1, 2027", "Every day at 9:00 AM, 10 times"
|
|
12
|
-
* it
|
|
17
|
+
* 5:30 PM, until Mar 1, 2027", "Every day at 9:00 AM, 10 times", and the
|
|
18
|
+
* week start when it matters and isn't the app's ("Every other week on Sun
|
|
19
|
+
* and Wed, weeks start Sun"). A rule it can't read shows as stored.
|
|
13
20
|
*/
|
|
14
21
|
export declare function formatRecurrence(value: Recurrence, options?: RecurrenceFormatOptions): string;
|
|
15
22
|
export type OccurrenceFormatOptions = {
|
|
16
23
|
locale?: string;
|
|
17
24
|
/** What "now" is, for leaving out this year. Defaults to the clock. */
|
|
18
25
|
now?: Date;
|
|
26
|
+
/** Leave the time out ("Tue, Dec 8"), for a record whose times are its own (4.114). */
|
|
27
|
+
time?: boolean;
|
|
19
28
|
};
|
|
20
29
|
/**
|
|
21
30
|
* One occurrence's day and time in the rule's zone: "Tue, Dec 8, 9:00 AM",
|
|
22
|
-
* with the year when it isn't this year's
|
|
31
|
+
* with the year when it isn't this year's; "Tue, Dec 8" with `time: false`.
|
|
23
32
|
*/
|
|
24
33
|
export declare function formatOccurrence(moment: Date, timeZone: string, options?: OccurrenceFormatOptions): string;
|
|
25
34
|
/** Today's date in `timeZone`, `'YYYY-MM-DD'`: where a phrase's days count from. */
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// locale given (the browser's when left out); the words around them are
|
|
6
6
|
// English, like the rest of the package.
|
|
7
7
|
import { listWords, ordinalWord } from './recurrence-phrase.js';
|
|
8
|
-
import { parseRule, wallClock } from './recurrence.js';
|
|
8
|
+
import { parseRule, wallClock, WEEKDAY_CODES, weekStartMatters } from './recurrence.js';
|
|
9
9
|
const WEEKDAYS_MO_FR = [0, 1, 2, 3, 4];
|
|
10
10
|
function dayName(day, locale, width = 'short') {
|
|
11
11
|
// 2024-01-01 was a Monday.
|
|
@@ -45,7 +45,10 @@ function days(parts, start, locale) {
|
|
|
45
45
|
return `${every(parts, 'day')}${inMonths}`;
|
|
46
46
|
case 'WEEKLY': {
|
|
47
47
|
const chosen = weekdays.length ? weekdays : [startWeekday];
|
|
48
|
-
|
|
48
|
+
// From the week's first day: "Sun and Wed" when weeks start on Sunday.
|
|
49
|
+
const first = parts.weekStart ?? 0;
|
|
50
|
+
const inWeek = [...chosen].sort((a, b) => ((a - first + 7) % 7) - ((b - first + 7) % 7));
|
|
51
|
+
const names = listWords(inWeek.map((day) => dayName(day, locale)));
|
|
49
52
|
if (parts.interval === 1) {
|
|
50
53
|
if (chosen.join() === WEEKDAYS_MO_FR.join())
|
|
51
54
|
return `Every weekday${inMonths}`;
|
|
@@ -92,8 +95,9 @@ function monthName(month, locale) {
|
|
|
92
95
|
* The repeat's label: its days, "after done" when it repeats from
|
|
93
96
|
* completion, its times, and how it ends. "Every 2nd Tue at 9:00 AM",
|
|
94
97
|
* "Every 3 days after done, at 9:00 AM", "Every weekday at 8:00 AM and
|
|
95
|
-
* 5:30 PM, until Mar 1, 2027", "Every day at 9:00 AM, 10 times"
|
|
96
|
-
* it
|
|
98
|
+
* 5:30 PM, until Mar 1, 2027", "Every day at 9:00 AM, 10 times", and the
|
|
99
|
+
* week start when it matters and isn't the app's ("Every other week on Sun
|
|
100
|
+
* and Wed, weeks start Sun"). A rule it can't read shows as stored.
|
|
97
101
|
*/
|
|
98
102
|
export function formatRecurrence(value, options = {}) {
|
|
99
103
|
let parts;
|
|
@@ -114,11 +118,15 @@ export function formatRecurrence(value, options = {}) {
|
|
|
114
118
|
label += `, until ${plainDay(parts.until, locale)}`;
|
|
115
119
|
if (parts.count !== null)
|
|
116
120
|
label += `, ${parts.count} ${parts.count === 1 ? 'time' : 'times'}`;
|
|
121
|
+
const weekStart = parts.weekStart ?? 0;
|
|
122
|
+
if (weekStartMatters(parts) && weekStart !== WEEKDAY_CODES.indexOf(options.weekStart ?? 'MO')) {
|
|
123
|
+
label += `, weeks start ${dayName(weekStart, locale)}`;
|
|
124
|
+
}
|
|
117
125
|
return label;
|
|
118
126
|
}
|
|
119
127
|
/**
|
|
120
128
|
* One occurrence's day and time in the rule's zone: "Tue, Dec 8, 9:00 AM",
|
|
121
|
-
* with the year when it isn't this year's
|
|
129
|
+
* with the year when it isn't this year's; "Tue, Dec 8" with `time: false`.
|
|
122
130
|
*/
|
|
123
131
|
export function formatOccurrence(moment, timeZone, options = {}) {
|
|
124
132
|
const thisYear = wallClock(options.now ?? new Date(), timeZone).day.slice(0, 4);
|
|
@@ -129,8 +137,7 @@ export function formatOccurrence(moment, timeZone, options = {}) {
|
|
|
129
137
|
month: 'short',
|
|
130
138
|
day: 'numeric',
|
|
131
139
|
year: year === thisYear ? undefined : 'numeric',
|
|
132
|
-
hour: 'numeric',
|
|
133
|
-
minute: '2-digit',
|
|
140
|
+
...(options.time === false ? {} : { hour: 'numeric', minute: '2-digit' }),
|
|
134
141
|
}).format(moment);
|
|
135
142
|
}
|
|
136
143
|
/** Today's date in `timeZone`, `'YYYY-MM-DD'`: where a phrase's days count from. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Recurrence } from './recurrence.js';
|
|
1
|
+
import { type Recurrence, type WeekdayCode } from './recurrence.js';
|
|
2
2
|
export type PhraseOptions = {
|
|
3
3
|
/** Today, `'YYYY-MM-DD'` in the rule's zone: where a day with no year counts from. */
|
|
4
4
|
today: string;
|
|
@@ -8,6 +8,18 @@ export type PhraseOptions = {
|
|
|
8
8
|
times?: string[];
|
|
9
9
|
/** The start when the phrase has no "starting". Defaults to `today`. */
|
|
10
10
|
start?: string;
|
|
11
|
+
/**
|
|
12
|
+
* The day weeks start on (4.114): a weekly rule read with no "weeks
|
|
13
|
+
* start" gets it as its WKST (`'SU'` adds `WKST=SU`). Defaults to
|
|
14
|
+
* Monday, which is no WKST.
|
|
15
|
+
*/
|
|
16
|
+
weekStart?: WeekdayCode;
|
|
17
|
+
/**
|
|
18
|
+
* Whether the phrase may say its times ("at 9am"; the default). False
|
|
19
|
+
* refuses them, for a record whose times are fields of its own: the
|
|
20
|
+
* rule keeps `times` (or 09:00).
|
|
21
|
+
*/
|
|
22
|
+
readTimes?: boolean;
|
|
11
23
|
};
|
|
12
24
|
export type PhraseResult = {
|
|
13
25
|
ok: true;
|
|
@@ -23,11 +35,21 @@ export type PhraseResult = {
|
|
|
23
35
|
export declare function parseRecurrencePhrase(text: string, options: PhraseOptions): PhraseResult;
|
|
24
36
|
declare const ordinal: (value: number) => string;
|
|
25
37
|
declare const listWords: (words: string[], and?: string) => string;
|
|
38
|
+
export type RecurrencePhraseOptions = {
|
|
39
|
+
/** Leave out "at …" (false), for a field whose times are the record's own. */
|
|
40
|
+
times?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* The week start the phrase is read back with (PhraseOptions'
|
|
43
|
+
* `weekStart`; Monday when left out). An every-N-weeks rule whose WKST
|
|
44
|
+
* differs says it: "…, weeks start sun".
|
|
45
|
+
*/
|
|
46
|
+
weekStart?: WeekdayCode;
|
|
47
|
+
};
|
|
26
48
|
/**
|
|
27
49
|
* The Recurrence as a phrase parseRecurrencePhrase reads back to it
|
|
28
50
|
* ("every 2nd tue at 9am until mar 1"), or null for a rule it can't say
|
|
29
51
|
* (several month days, BYMONTH on a daily rule). The start is said only
|
|
30
52
|
* when it's after `today`.
|
|
31
53
|
*/
|
|
32
|
-
export declare function recurrencePhrase(value: Recurrence, today?: string): string | null;
|
|
54
|
+
export declare function recurrencePhrase(value: Recurrence, today?: string, options?: RecurrencePhraseOptions): string | null;
|
|
33
55
|
export { ordinal as ordinalWord, listWords };
|
|
@@ -13,9 +13,10 @@
|
|
|
13
13
|
// - a day of the year: `jan 1`, `1 jan`
|
|
14
14
|
//
|
|
15
15
|
// then, in any order: `at 9am, 5:30pm` (`noon`, `17:00`), `starting dec 1`
|
|
16
|
-
// (`from`), `until mar 1` (`ending`), `for 10 times
|
|
17
|
-
// the next one: from today for
|
|
18
|
-
|
|
16
|
+
// (`from`), `until mar 1` (`ending`), `for 10 times`, `weeks start sun`
|
|
17
|
+
// (WKST, 4.114). A day with no year is the next one: from today for
|
|
18
|
+
// `starting`, from the start for `until`.
|
|
19
|
+
import { formatRule, normalizeRecurrence, parseRule, RecurrenceError, WEEKDAY_CODES, weekStartMatters, } from './recurrence.js';
|
|
19
20
|
const WEEKDAY_WORDS = [
|
|
20
21
|
[/^mon(day)?$/, 0],
|
|
21
22
|
[/^tue(s|sday)?$/, 1],
|
|
@@ -306,6 +307,7 @@ function readPhrase(text, options) {
|
|
|
306
307
|
let untilAt;
|
|
307
308
|
let untilText;
|
|
308
309
|
let count = null;
|
|
310
|
+
let weekStart;
|
|
309
311
|
const seen = new Set();
|
|
310
312
|
const once = (word) => {
|
|
311
313
|
if (seen.has(word))
|
|
@@ -316,7 +318,14 @@ function readPhrase(text, options) {
|
|
|
316
318
|
reader.read(',');
|
|
317
319
|
if (reader.done)
|
|
318
320
|
break;
|
|
319
|
-
|
|
321
|
+
const week = reader.read(String.raw `weeks?\s+(?:start|starts|starting|begin|begins|beginning)(?:\s+on)?\s+(${WEEKDAY})`);
|
|
322
|
+
if (week) {
|
|
323
|
+
once('weeks start');
|
|
324
|
+
weekStart = weekdayOf(week[1]);
|
|
325
|
+
}
|
|
326
|
+
else if (reader.read('at')) {
|
|
327
|
+
if (options.readTimes === false)
|
|
328
|
+
throw new Refusal('The times are set on their own here: leave out “at …”.');
|
|
320
329
|
once('at');
|
|
321
330
|
times = readTimes(reader);
|
|
322
331
|
}
|
|
@@ -353,8 +362,9 @@ function readPhrase(text, options) {
|
|
|
353
362
|
}
|
|
354
363
|
const first = start ?? options.start ?? options.today;
|
|
355
364
|
const until = untilText === undefined ? null : readDate(new Reader(untilText), first, 'until');
|
|
365
|
+
const defaultStart = body.freq === 'WEEKLY' && options.weekStart ? WEEKDAY_CODES.indexOf(options.weekStart) : 0;
|
|
356
366
|
return normalizeRecurrence({
|
|
357
|
-
rule: formatRule({ ...body, until, count }),
|
|
367
|
+
rule: formatRule({ ...body, until, count, weekStart: weekStart ?? defaultStart }),
|
|
358
368
|
start: first,
|
|
359
369
|
times: times ?? options.times ?? ['09:00'],
|
|
360
370
|
timeZone: options.timeZone,
|
|
@@ -385,6 +395,9 @@ function phraseDate(day, today) {
|
|
|
385
395
|
const WEEKDAYS_MO_FR = 'MO,TU,WE,TH,FR';
|
|
386
396
|
/** The days part of a rule as a phrase ("every 2nd tue"), or null for one the reader can't write. */
|
|
387
397
|
function phraseBody(parts) {
|
|
398
|
+
const first = parts.weekStart ?? 0;
|
|
399
|
+
// A week's days from its first day: "on sun and wed" when weeks start on Sunday.
|
|
400
|
+
const inWeek = (days) => [...days].sort((a, b) => ((a.day - first + 7) % 7) - ((b.day - first + 7) % 7));
|
|
388
401
|
const { freq, interval, byDay, byMonthDay, byMonth } = parts;
|
|
389
402
|
const codes = byDay.map(({ day }) => WEEKDAY_CODES[day]).join(',');
|
|
390
403
|
const plain = byDay.every(({ nth }) => nth === null);
|
|
@@ -400,7 +413,7 @@ function phraseBody(parts) {
|
|
|
400
413
|
return every;
|
|
401
414
|
if (!plain)
|
|
402
415
|
return null;
|
|
403
|
-
const names = listWords(byDay.map(({ day }) => DAY_WORDS[day]));
|
|
416
|
+
const names = listWords(inWeek(byDay).map(({ day }) => DAY_WORDS[day]));
|
|
404
417
|
if (interval === 1)
|
|
405
418
|
return codes === WEEKDAYS_MO_FR ? 'weekday' : codes === 'SA,SU' ? 'weekend' : names;
|
|
406
419
|
return `${every} on ${names}`;
|
|
@@ -442,7 +455,7 @@ function phraseBody(parts) {
|
|
|
442
455
|
* (several month days, BYMONTH on a daily rule). The start is said only
|
|
443
456
|
* when it's after `today`.
|
|
444
457
|
*/
|
|
445
|
-
export function recurrencePhrase(value, today) {
|
|
458
|
+
export function recurrencePhrase(value, today, options = {}) {
|
|
446
459
|
let parts;
|
|
447
460
|
try {
|
|
448
461
|
parts = parseRule(value.rule);
|
|
@@ -454,13 +467,18 @@ export function recurrencePhrase(value, today) {
|
|
|
454
467
|
if (body === null)
|
|
455
468
|
return null;
|
|
456
469
|
const words = [`${value.fromCompletion ? 'every!' : 'every'} ${body}`];
|
|
457
|
-
|
|
470
|
+
if (options.times !== false)
|
|
471
|
+
words.push(`at ${value.times.map(phraseTime).join(', ')}`);
|
|
458
472
|
if (today && value.start > today)
|
|
459
473
|
words.push(`starting ${phraseDate(value.start, today)}`);
|
|
460
474
|
if (parts.until)
|
|
461
475
|
words.push(`until ${phraseDate(parts.until, today && value.start > today ? value.start : today)}`);
|
|
462
476
|
if (parts.count !== null)
|
|
463
477
|
words.push(`for ${parts.count} times`);
|
|
478
|
+
const weekStart = parts.weekStart ?? 0;
|
|
479
|
+
if (weekStartMatters(parts) && weekStart !== WEEKDAY_CODES.indexOf(options.weekStart ?? 'MO')) {
|
|
480
|
+
return `${words.join(' ')}, weeks start ${DAY_WORDS[weekStart]}`;
|
|
481
|
+
}
|
|
464
482
|
return words.join(' ');
|
|
465
483
|
}
|
|
466
484
|
export { ordinal as ordinalWord, listWords };
|
package/dist/lib/recurrence.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ export declare const FREQUENCIES: readonly ["DAILY", "WEEKLY", "MONTHLY", "YEARL
|
|
|
5
5
|
export type Frequency = (typeof FREQUENCIES)[number];
|
|
6
6
|
/** RFC 5545's weekday codes, Monday first (Monday is 0 everywhere here). */
|
|
7
7
|
export declare const WEEKDAY_CODES: readonly ["MO", "TU", "WE", "TH", "FR", "SA", "SU"];
|
|
8
|
+
/** A weekday's code, `'MO'` to `'SU'`: a week start (WKST) as an app gives it. */
|
|
9
|
+
export type WeekdayCode = (typeof WEEKDAY_CODES)[number];
|
|
8
10
|
export type ByDay = {
|
|
9
11
|
nth: number | null;
|
|
10
12
|
day: number;
|
|
@@ -18,6 +20,12 @@ export type RuleParts = {
|
|
|
18
20
|
byMonth: number[];
|
|
19
21
|
until: string | null;
|
|
20
22
|
count: number | null;
|
|
23
|
+
/**
|
|
24
|
+
* WKST: the day weeks start on (Monday 0 to Sunday 6), or null for
|
|
25
|
+
* Monday, RFC 5545's default. `WKST=MO` reads as null, so it isn't stored.
|
|
26
|
+
* Optional (4.114), so parts an app builds without it still are parts.
|
|
27
|
+
*/
|
|
28
|
+
weekStart?: number | null;
|
|
21
29
|
};
|
|
22
30
|
/** What's wrong with a rule or a recurrence, in words a person can act on. */
|
|
23
31
|
export declare class RecurrenceError extends Error {
|
|
@@ -27,7 +35,7 @@ export declare function parseRule(text: string): RuleParts;
|
|
|
27
35
|
/**
|
|
28
36
|
* The stored form, its parts always in one order (so two rules that mean
|
|
29
37
|
* the same are the same text): FREQ, INTERVAL (left out at 1), BYMONTH,
|
|
30
|
-
* BYMONTHDAY, BYDAY, UNTIL, COUNT.
|
|
38
|
+
* BYMONTHDAY, BYDAY, UNTIL, COUNT, WKST (left out for Monday).
|
|
31
39
|
*/
|
|
32
40
|
export declare function formatRule(parts: RuleParts): string;
|
|
33
41
|
/** Whether `name` is an IANA zone this browser knows. */
|
|
@@ -68,6 +76,13 @@ export declare function nextOccurrence(value: Recurrence, after: Date): Date | n
|
|
|
68
76
|
* first one after that day. A schedule rule's is just its next.
|
|
69
77
|
*/
|
|
70
78
|
export declare function nextAfterCompletion(value: Recurrence, doneAt: Date): Date | null;
|
|
79
|
+
/**
|
|
80
|
+
* Whether the rule's WKST can change the days it falls on: only an
|
|
81
|
+
* every-N-weeks rule (`FREQ=WEEKLY`, INTERVAL over 1) with BYDAY counts
|
|
82
|
+
* its weeks from a week's first day. (BYWEEKNO, the other part it moves,
|
|
83
|
+
* isn't supported.)
|
|
84
|
+
*/
|
|
85
|
+
export declare function weekStartMatters(parts: Pick<RuleParts, 'freq' | 'interval' | 'byDay'>): boolean;
|
|
71
86
|
/**
|
|
72
87
|
* The rule for a weekday set and one time (Monday 0, or `'mon,wed,fri'`
|
|
73
88
|
* as the reminders table stores it), exactly: due on the same days at the
|
package/dist/lib/recurrence.js
CHANGED
|
@@ -16,6 +16,12 @@
|
|
|
16
16
|
// month doesn't have is skipped: the 31st falls only in months with 31
|
|
17
17
|
// days, Feb 29 only in leap years, and -1 is every month's last day. COUNT
|
|
18
18
|
// counts occurrences, each time of each day.
|
|
19
|
+
//
|
|
20
|
+
// WKST (4.114) says which day a week starts on, for the weeks an
|
|
21
|
+
// every-N-weeks rule counts: `FREQ=WEEKLY;INTERVAL=2;BYDAY=SU,WE;WKST=SU`
|
|
22
|
+
// from Wednesday 2026-11-04 next falls on Sunday the 15th (with weeks from
|
|
23
|
+
// Monday, the default, on the 8th). It changes nothing else here; a rule
|
|
24
|
+
// without it counts weeks from Monday, as before.
|
|
19
25
|
import * as rruleModule from 'rrule';
|
|
20
26
|
// rrule ships an ES module build without `"type": "module"`: a bundler reads
|
|
21
27
|
// its named exports, Node its UMD build, whose exports sit on `default`.
|
|
@@ -31,7 +37,7 @@ const TIME = /^([01]\d|2[0-3]):[0-5]\d$/;
|
|
|
31
37
|
const BYDAY = /^([+-]?[1-5])?(MO|TU|WE|TH|FR|SA|SU)$/;
|
|
32
38
|
const UNTIL = /^(\d{4})(\d{2})(\d{2})$/;
|
|
33
39
|
const DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
34
|
-
const PARTS = new Set(['FREQ', 'INTERVAL', 'BYDAY', 'BYMONTHDAY', 'BYMONTH', 'UNTIL', 'COUNT']);
|
|
40
|
+
const PARTS = new Set(['FREQ', 'INTERVAL', 'BYDAY', 'BYMONTHDAY', 'BYMONTH', 'UNTIL', 'COUNT', 'WKST']);
|
|
35
41
|
// How far ahead a rule must fall at least once (a 31st of February never does).
|
|
36
42
|
const HORIZON_YEARS = 28;
|
|
37
43
|
const DAY_MS = 86_400_000;
|
|
@@ -96,6 +102,13 @@ export function parseRule(text) {
|
|
|
96
102
|
const count = fields.has('COUNT') ? integers('COUNT', fields.get('COUNT'), 1, 9999)[0] : null;
|
|
97
103
|
if (until !== null && count !== null)
|
|
98
104
|
throw new RecurrenceError('A rule has UNTIL or COUNT, not both.');
|
|
105
|
+
let weekStart = null;
|
|
106
|
+
if (fields.has('WKST')) {
|
|
107
|
+
const day = WEEKDAY_CODES.indexOf(fields.get('WKST'));
|
|
108
|
+
if (day < 0)
|
|
109
|
+
throw new RecurrenceError('WKST must be a weekday, MO to SU.');
|
|
110
|
+
weekStart = day === 0 ? null : day;
|
|
111
|
+
}
|
|
99
112
|
return {
|
|
100
113
|
freq,
|
|
101
114
|
interval,
|
|
@@ -104,12 +117,13 @@ export function parseRule(text) {
|
|
|
104
117
|
byMonth: byMonth.sort((a, b) => a - b),
|
|
105
118
|
until,
|
|
106
119
|
count,
|
|
120
|
+
weekStart,
|
|
107
121
|
};
|
|
108
122
|
}
|
|
109
123
|
/**
|
|
110
124
|
* The stored form, its parts always in one order (so two rules that mean
|
|
111
125
|
* the same are the same text): FREQ, INTERVAL (left out at 1), BYMONTH,
|
|
112
|
-
* BYMONTHDAY, BYDAY, UNTIL, COUNT.
|
|
126
|
+
* BYMONTHDAY, BYDAY, UNTIL, COUNT, WKST (left out for Monday).
|
|
113
127
|
*/
|
|
114
128
|
export function formatRule(parts) {
|
|
115
129
|
const out = [`FREQ=${parts.freq}`];
|
|
@@ -125,6 +139,8 @@ export function formatRule(parts) {
|
|
|
125
139
|
out.push(`UNTIL=${parts.until.replaceAll('-', '')}`);
|
|
126
140
|
if (parts.count !== null)
|
|
127
141
|
out.push(`COUNT=${parts.count}`);
|
|
142
|
+
if (parts.weekStart)
|
|
143
|
+
out.push(`WKST=${WEEKDAY_CODES[parts.weekStart]}`);
|
|
128
144
|
return out.join(';');
|
|
129
145
|
}
|
|
130
146
|
function isRealDay(value) {
|
|
@@ -226,6 +242,8 @@ function* days(parts, start, stop) {
|
|
|
226
242
|
bymonthday: parts.byMonthDay.length ? parts.byMonthDay : null,
|
|
227
243
|
bymonth: parts.byMonth.length ? parts.byMonth : null,
|
|
228
244
|
until: last ? toUtcDay(last) : null,
|
|
245
|
+
// Monday (rrule's default) when the rule has no WKST.
|
|
246
|
+
wkst: parts.weekStart ?? 0,
|
|
229
247
|
});
|
|
230
248
|
let given = 0;
|
|
231
249
|
for (let limit = 64;; limit *= 4) {
|
|
@@ -340,6 +358,15 @@ export function nextAfterCompletion(value, doneAt) {
|
|
|
340
358
|
return null;
|
|
341
359
|
}
|
|
342
360
|
// ------------------------------------------ a weekday set and one time
|
|
361
|
+
/**
|
|
362
|
+
* Whether the rule's WKST can change the days it falls on: only an
|
|
363
|
+
* every-N-weeks rule (`FREQ=WEEKLY`, INTERVAL over 1) with BYDAY counts
|
|
364
|
+
* its weeks from a week's first day. (BYWEEKNO, the other part it moves,
|
|
365
|
+
* isn't supported.)
|
|
366
|
+
*/
|
|
367
|
+
export function weekStartMatters(parts) {
|
|
368
|
+
return parts.freq === 'WEEKLY' && parts.interval > 1 && parts.byDay.length > 0;
|
|
369
|
+
}
|
|
343
370
|
const DAY_NAMES = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];
|
|
344
371
|
/**
|
|
345
372
|
* The rule for a weekday set and one time (Monday 0, or `'mon,wed,fri'`
|
package/openapi/foundation.yaml
CHANGED
|
@@ -348,13 +348,20 @@ components:
|
|
|
348
348
|
type: string
|
|
349
349
|
minLength: 1
|
|
350
350
|
maxLength: 200
|
|
351
|
-
pattern: "^FREQ=(DAILY|WEEKLY|MONTHLY|YEARLY)(;(INTERVAL|BYMONTH|BYMONTHDAY|BYDAY|UNTIL|COUNT)=[A-Z0-9,+-]+)*$"
|
|
351
|
+
pattern: "^FREQ=(DAILY|WEEKLY|MONTHLY|YEARLY)(;(INTERVAL|BYMONTH|BYMONTHDAY|BYDAY|UNTIL|COUNT|WKST)=[A-Z0-9,+-]+)*$"
|
|
352
|
+
# The pattern before WKST (4.95 to 4.113). check-contract still
|
|
353
|
+
# accepts an app's copy with it: such an app can't store WKST,
|
|
354
|
+
# and needn't copy this one until it wants to.
|
|
355
|
+
x-earlier-patterns:
|
|
356
|
+
- "^FREQ=(DAILY|WEEKLY|MONTHLY|YEARLY)(;(INTERVAL|BYMONTH|BYMONTHDAY|BYDAY|UNTIL|COUNT)=[A-Z0-9,+-]+)*$"
|
|
352
357
|
description: >
|
|
353
358
|
RFC 5545's RRULE value, without DTSTART: FREQ (daily, weekly,
|
|
354
359
|
monthly, yearly), INTERVAL, BYMONTH, BYMONTHDAY (-1 is the last
|
|
355
360
|
day), BYDAY (an nth for monthly and yearly: 2TU, -1FR), UNTIL
|
|
356
361
|
(a date, YYYYMMDD, the last day it can fall on) or COUNT
|
|
357
|
-
(occurrences, each time of each day)
|
|
362
|
+
(occurrences, each time of each day), and WKST (the day weeks
|
|
363
|
+
start on, MO when left out: it moves an every-N-weeks rule's
|
|
364
|
+
weeks). "FREQ=MONTHLY;BYDAY=2TU", "FREQ=WEEKLY;INTERVAL=2;BYDAY=SU,WE;WKST=SU".
|
|
358
365
|
start:
|
|
359
366
|
type: string
|
|
360
367
|
format: date
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tristan2828/ui-foundation",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.119.0",
|
|
4
4
|
"description": "Shared UI foundation for contract-first, database-backed React apps: shadcn primitives, DataTable/EntityForm composites, auth, the gateway error seam, design tokens, lint rules and the AI playbook.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|