@telepath-computer/television 1.5.10 → 1.6.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.
Files changed (130) hide show
  1. package/dist/THIRD-PARTY-NOTICES.txt +701 -0
  2. package/dist/cli.cjs +9035 -596
  3. package/dist/onboarding/tv-guide/welcome.html +14 -4
  4. package/dist/skills/television/SKILL.md +39 -33
  5. package/dist/skills/television/resources.md +4 -4
  6. package/dist/skills/television/templates.md +77 -0
  7. package/dist/skills/television/theming.md +31 -17
  8. package/dist/templates/calendar/README.md +17 -0
  9. package/dist/{skills/tv-tasks → templates/calendar}/THIRD-PARTY-NOTICES.txt +22 -22
  10. package/dist/templates/calendar/calendar-attendees.js +131 -0
  11. package/dist/templates/calendar/calendar-event.js +22 -0
  12. package/dist/templates/calendar/calendar-motion.js +273 -0
  13. package/dist/templates/calendar/calendar-page.js +403 -0
  14. package/dist/templates/calendar/calendar-time-axis.js +32 -0
  15. package/dist/templates/calendar/calendar-week.js +711 -0
  16. package/dist/templates/calendar/data.json +3 -0
  17. package/dist/templates/calendar/icon.svg +1 -0
  18. package/dist/templates/calendar/index.html +27 -0
  19. package/dist/templates/calendar/manifest.json +148 -0
  20. package/dist/templates/calendar/styles.css +771 -0
  21. package/dist/templates/checklist/README.md +45 -0
  22. package/dist/templates/checklist/THIRD-PARTY-NOTICES.txt +29 -0
  23. package/dist/templates/checklist/data.json +1 -0
  24. package/dist/templates/checklist/icon.svg +8 -0
  25. package/dist/templates/checklist/icons.css +61 -0
  26. package/dist/templates/checklist/index.html +47 -0
  27. package/dist/templates/checklist/manifest.json +49 -0
  28. package/dist/templates/checklist/model.d.ts +30 -0
  29. package/dist/templates/checklist/model.js +60 -0
  30. package/dist/templates/checklist/page.js +425 -0
  31. package/dist/templates/checklist/styles.css +206 -0
  32. package/dist/templates/document/README.md +55 -0
  33. package/dist/templates/document/THIRD-PARTY-NOTICES.txt +1080 -0
  34. package/dist/templates/document/data.json +3 -0
  35. package/dist/templates/document/editor.css +1 -0
  36. package/dist/templates/document/editor.js +37 -0
  37. package/dist/templates/document/icon.svg +6 -0
  38. package/dist/templates/document/index.html +29 -0
  39. package/dist/templates/document/manifest.json +22 -0
  40. package/dist/templates/document/page.js +190 -0
  41. package/dist/templates/document/styles.css +76 -0
  42. package/dist/templates/map/README.md +85 -0
  43. package/dist/templates/map/THIRD-PARTY-NOTICES.txt +84 -0
  44. package/dist/templates/map/data.json +1 -0
  45. package/dist/templates/map/icon.svg +8 -0
  46. package/dist/templates/map/icons.svg +43 -0
  47. package/dist/templates/map/index.html +95 -0
  48. package/dist/templates/map/manifest.json +227 -0
  49. package/dist/templates/map/map-model.js +18 -0
  50. package/dist/templates/map/map.css +202 -0
  51. package/dist/templates/map/map.js +728 -0
  52. package/dist/templates/map/vendor/LICENSE +26 -0
  53. package/dist/templates/map/vendor/README.md +11 -0
  54. package/dist/templates/map/vendor/leaflet.css +661 -0
  55. package/dist/templates/map/vendor/leaflet.js +6 -0
  56. package/dist/templates/map/vendor/lucide/LICENSE +43 -0
  57. package/dist/templates/map/vendor/lucide/README.md +46 -0
  58. package/dist/templates/map/vendor/lucide/icons.js +832 -0
  59. package/dist/templates/map/vendor/lucide/svg/airport.svg +15 -0
  60. package/dist/templates/map/vendor/lucide/svg/bar.svg +17 -0
  61. package/dist/templates/map/vendor/lucide/svg/beach.svg +17 -0
  62. package/dist/templates/map/vendor/lucide/svg/bridge.svg +20 -0
  63. package/dist/templates/map/vendor/lucide/svg/bus.svg +21 -0
  64. package/dist/templates/map/vendor/lucide/svg/church.svg +19 -0
  65. package/dist/templates/map/vendor/lucide/svg/coffee.svg +18 -0
  66. package/dist/templates/map/vendor/lucide/svg/ferry.svg +19 -0
  67. package/dist/templates/map/vendor/lucide/svg/flag.svg +15 -0
  68. package/dist/templates/map/vendor/lucide/svg/heart.svg +15 -0
  69. package/dist/templates/map/vendor/lucide/svg/home.svg +16 -0
  70. package/dist/templates/map/vendor/lucide/svg/hospital.svg +19 -0
  71. package/dist/templates/map/vendor/lucide/svg/hotel.svg +24 -0
  72. package/dist/templates/map/vendor/lucide/svg/info.svg +17 -0
  73. package/dist/templates/map/vendor/lucide/svg/landmark.svg +20 -0
  74. package/dist/templates/map/vendor/lucide/svg/mountain.svg +15 -0
  75. package/dist/templates/map/vendor/lucide/svg/museum.svg +20 -0
  76. package/dist/templates/map/vendor/lucide/svg/music.svg +17 -0
  77. package/dist/templates/map/vendor/lucide/svg/park.svg +18 -0
  78. package/dist/templates/map/vendor/lucide/svg/parking.svg +16 -0
  79. package/dist/templates/map/vendor/lucide/svg/pin.svg +16 -0
  80. package/dist/templates/map/vendor/lucide/svg/restaurant.svg +17 -0
  81. package/dist/templates/map/vendor/lucide/svg/school.svg +20 -0
  82. package/dist/templates/map/vendor/lucide/svg/shop.svg +17 -0
  83. package/dist/templates/map/vendor/lucide/svg/sports.svg +19 -0
  84. package/dist/templates/map/vendor/lucide/svg/star.svg +15 -0
  85. package/dist/templates/map/vendor/lucide/svg/theatre.svg +23 -0
  86. package/dist/templates/map/vendor/lucide/svg/train.svg +20 -0
  87. package/dist/templates/map/vendor/lucide/svg/viewpoint.svg +20 -0
  88. package/dist/templates/map/vendor/lucide/svg/work.svg +18 -0
  89. package/dist/templates/sticky-note/Caveat-Medium.woff2 +0 -0
  90. package/dist/templates/sticky-note/README.md +47 -0
  91. package/dist/templates/sticky-note/THIRD-PARTY-NOTICES.txt +127 -0
  92. package/dist/templates/sticky-note/data.json +1 -0
  93. package/dist/templates/sticky-note/icon.svg +16 -0
  94. package/dist/templates/sticky-note/icons.css +61 -0
  95. package/dist/templates/sticky-note/index.html +31 -0
  96. package/dist/templates/sticky-note/manifest.json +27 -0
  97. package/dist/templates/sticky-note/model.d.ts +33 -0
  98. package/dist/templates/sticky-note/model.js +152 -0
  99. package/dist/templates/sticky-note/page.js +84 -0
  100. package/dist/templates/sticky-note/styles.css +140 -0
  101. package/dist/templates/table/README.md +107 -0
  102. package/dist/templates/table/data.json +4 -0
  103. package/dist/templates/table/icon.svg +6 -0
  104. package/dist/templates/table/index.html +15 -0
  105. package/dist/templates/table/manifest.json +70 -0
  106. package/dist/templates/table/model.d.ts +164 -0
  107. package/dist/templates/table/model.js +395 -0
  108. package/dist/templates/table/page.js +1045 -0
  109. package/dist/templates/table/selection-model.d.ts +67 -0
  110. package/dist/templates/table/selection-model.js +163 -0
  111. package/dist/templates/table/styles.css +522 -0
  112. package/dist/views/markdown/THIRD-PARTY-NOTICES.txt +408 -0
  113. package/dist/views/markdown/index.html +21 -21
  114. package/dist/web/assets/main-CRSNodQt.js +864 -0
  115. package/dist/web/assets/main-DnvQkpg4.css +1 -0
  116. package/dist/web/index.html +2 -2
  117. package/package.json +1 -1
  118. package/dist/skills/tv-calendar/SKILL.md +0 -100
  119. package/dist/skills/tv-calendar/calendar.css +0 -1
  120. package/dist/skills/tv-calendar/calendar.js +0 -400
  121. package/dist/skills/tv-table/SKILL.md +0 -95
  122. package/dist/skills/tv-tasks/SKILL.md +0 -269
  123. package/dist/skills/tv-tasks/task.css +0 -180
  124. package/dist/skills/tv-tasks/task.js +0 -304
  125. package/dist/web/assets/calendar-DToAnkzj.png +0 -0
  126. package/dist/web/assets/main-CekDfJPz.js +0 -848
  127. package/dist/web/assets/main-CunC2_Nx.css +0 -1
  128. package/dist/web/assets/markdown-DdDrJuVK.png +0 -0
  129. package/dist/web/assets/table-oeG9uAN1.png +0 -0
  130. package/dist/web/assets/tasks-D4xqXFPl.png +0 -0
@@ -0,0 +1,711 @@
1
+ // The tv-calendar week component, ported to an unbundled JavaScript module.
2
+ // Store updates call refreshEvents; only a changed range rebuilds the calendar
3
+ // chrome, so existing event blocks can keep their DOM identity and scroll.
4
+ // The stylesheet derives the height of the day headers, which the all-day
5
+ // band sticks beneath, so nothing here measures them.
6
+ //
7
+ // Timed events that overlap share their day's width in side-by-side columns,
8
+ // as Apple Calendar lays them out: each overlapping cluster gets as many
9
+ // columns as it needs, and an event widens into columns to its right that
10
+ // stay free for its whole time. The CSS reads the result from --column-index,
11
+ // --column-span and --column-count on each calendar-event.
12
+ import "./calendar-time-axis.js";
13
+ import { createAttendeesLine, setAttendees } from "./calendar-attendees.js";
14
+ import { leave, measure, motionAllowed, play } from "./calendar-motion.js";
15
+ const HOUR_COUNT = 24;
16
+ const DEFAULT_START_HOUR = 8;
17
+ const NOW_LINE_INTERVAL_MS = 60_000;
18
+ const MAX_HOUR = 23;
19
+ const MAX_MINUTE = 59;
20
+ const MS_PER_SECOND = 1_000;
21
+ const MINUTES_PER_HOUR = 60;
22
+ const MINUTES_PER_DAY = HOUR_COUNT * MINUTES_PER_HOUR;
23
+ const DAY_MS = MINUTES_PER_DAY * MINUTES_PER_HOUR * MS_PER_SECOND;
24
+ const DATE_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
25
+ const DATETIME_RE = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})$/;
26
+ const REACTIVE_PROPERTIES = ["days", "startDate", "startHour"];
27
+ // Events this short show their title and start time on one line.
28
+ const SHORT_EVENT_MAX_MINUTES = 30;
29
+ const TIME_FORMAT = new Intl.DateTimeFormat(undefined, { hour: "numeric", minute: "2-digit" });
30
+ // A block writes a time range with thin spaces around the dash and an
31
+ // ordinary space before AM or PM, whichever spaces the browser's formatter
32
+ // puts there.
33
+ const RANGE_DASH_RE = /\s*–\s*/;
34
+ const NARROW_SPACE_RE = /\u202f/g;
35
+ // The custom properties each kind of placement sets on a calendar-event.
36
+ const TIMED_PLACEMENT_PROPERTIES = ["--day-index", "--y-start", "--y-end", "--column-index", "--column-span", "--column-count"];
37
+ const ALL_DAY_PLACEMENT_PROPERTIES = ["--day-start", "--day-span", "--lane-index"];
38
+ /** Owns the day chrome and places authored calendar-event children. */
39
+ class CalendarWeekElement extends HTMLElement {
40
+ static observedAttributes = ["days", "start-date", "start-hour"];
41
+ /** Whether changes move into place; the page turns this on once it has drawn. */
42
+ motion = false;
43
+ /** Set while the page slides to a new range, which moves everything itself. */
44
+ sliding = false;
45
+ #allDay = null;
46
+ #days = 0;
47
+ #grid = null;
48
+ #hasConnected = false;
49
+ #headers = null;
50
+ #needsConnectedUpdate = false;
51
+ #nowLineInterval = null;
52
+ #pendingReflections = new Set();
53
+ #preUpgradeProperties = new Map();
54
+ #range = null;
55
+ #rangeSignature = null;
56
+ #didInitialScroll = false;
57
+ #startDate = "";
58
+ #startHour;
59
+ #updateNeeded = false;
60
+ #updateScheduled = false;
61
+ constructor() {
62
+ super();
63
+ const instance = this;
64
+ for (const property of REACTIVE_PROPERTIES) {
65
+ if (!Object.hasOwn(this, property)) continue;
66
+ this.#preUpgradeProperties.set(property, instance[property]);
67
+ delete instance[property];
68
+ }
69
+ }
70
+ get days() {
71
+ return this.#days;
72
+ }
73
+ set days(value) {
74
+ if (Object.is(this.#days, value)) return;
75
+ this.#days = value;
76
+ this.#requestPropertyUpdate("days");
77
+ }
78
+ get startDate() {
79
+ return this.#startDate;
80
+ }
81
+ set startDate(value) {
82
+ if (this.#startDate === value) return;
83
+ this.#startDate = value;
84
+ this.#requestPropertyUpdate("start-date");
85
+ }
86
+ get startHour() {
87
+ return this.#startHour;
88
+ }
89
+ set startHour(value) {
90
+ if (Object.is(this.#startHour, value)) return;
91
+ this.#startHour = value;
92
+ this.#requestPropertyUpdate("start-hour");
93
+ }
94
+ connectedCallback() {
95
+ this.dispatchEvent(new Event("reactive:connect"));
96
+ this.#nowLineInterval = setInterval(() => this.#renderNowLine(), NOW_LINE_INTERVAL_MS);
97
+ if (!this.#hasConnected) {
98
+ this.#hasConnected = true;
99
+ this.#pendingReflections.add("days");
100
+ this.#pendingReflections.add("start-date");
101
+ this.#replayPreUpgradeProperties();
102
+ this.#updateNeeded = true;
103
+ this.#requestUpdate();
104
+ } else if (this.#needsConnectedUpdate || this.#updateNeeded) {
105
+ this.#requestUpdate();
106
+ }
107
+ }
108
+ disconnectedCallback() {
109
+ this.dispatchEvent(new Event("reactive:disconnect"));
110
+ if (this.#nowLineInterval !== null) {
111
+ clearInterval(this.#nowLineInterval);
112
+ this.#nowLineInterval = null;
113
+ }
114
+ }
115
+ attributeChangedCallback(name, oldValue, newValue) {
116
+ if (oldValue === newValue) return;
117
+ if (!this.#setValueFromAttribute(name, newValue)) return;
118
+ this.#updateNeeded = true;
119
+ this.#requestUpdate();
120
+ }
121
+ #replayPreUpgradeProperties() {
122
+ for (const [property, value] of this.#preUpgradeProperties) {
123
+ switch (property) {
124
+ case "days":
125
+ this.days = value;
126
+ break;
127
+ case "startDate":
128
+ this.startDate = value;
129
+ break;
130
+ case "startHour":
131
+ this.startHour = value;
132
+ break;
133
+ }
134
+ }
135
+ this.#preUpgradeProperties.clear();
136
+ }
137
+ #requestPropertyUpdate(attribute) {
138
+ this.#pendingReflections.add(attribute);
139
+ this.#updateNeeded = true;
140
+ this.#requestUpdate();
141
+ }
142
+ #requestUpdate() {
143
+ if (this.#updateScheduled) return;
144
+ this.#updateScheduled = true;
145
+ queueMicrotask(() => this.#flushUpdate());
146
+ }
147
+ #flushUpdate() {
148
+ this.#updateScheduled = false;
149
+ if (!this.#hasConnected) return;
150
+ const shouldRender = this.#updateNeeded || this.#needsConnectedUpdate;
151
+ this.#updateNeeded = false;
152
+ this.#needsConnectedUpdate = false;
153
+ this.#reflectPendingProperties();
154
+ if (!this.isConnected) {
155
+ this.#needsConnectedUpdate = shouldRender;
156
+ return;
157
+ }
158
+ if (shouldRender) this.#changed();
159
+ }
160
+ #reflectPendingProperties() {
161
+ const attributes = [...this.#pendingReflections];
162
+ this.#pendingReflections.clear();
163
+ for (const attribute of attributes) {
164
+ const value = attribute === "days" ? this.#days : attribute === "start-date" ? this.#startDate : this.#startHour;
165
+ const serialized = value === undefined ? null : String(value);
166
+ if (serialized === null) {
167
+ this.removeAttribute(attribute);
168
+ } else if (this.getAttribute(attribute) !== serialized) {
169
+ this.setAttribute(attribute, serialized);
170
+ }
171
+ }
172
+ }
173
+ #setValueFromAttribute(name, value) {
174
+ switch (name) {
175
+ case "days": {
176
+ const next = numberFromAttribute(value);
177
+ if (Object.is(this.#days, next)) return false;
178
+ this.#days = next;
179
+ return true;
180
+ }
181
+ case "start-date": {
182
+ const next = value ?? undefined;
183
+ if (this.#startDate === next) return false;
184
+ this.#startDate = next;
185
+ return true;
186
+ }
187
+ case "start-hour": {
188
+ const next = numberFromAttribute(value);
189
+ if (Object.is(this.#startHour, next)) return false;
190
+ this.#startHour = next;
191
+ return true;
192
+ }
193
+ default:
194
+ return false;
195
+ }
196
+ }
197
+ #changed() {
198
+ try {
199
+ this.changed();
200
+ } catch (error) {
201
+ console.error(error);
202
+ }
203
+ }
204
+ changed() {
205
+ const before = this.motion && !this.sliding && motionAllowed() ? measure(this) : null;
206
+ const previousScrollTop = this.#didInitialScroll ? this.scrollTop : null;
207
+ const events = this.#collectEvents();
208
+ const range = resolveVisibleRange(this.startDate, this.days);
209
+ const placements = events.map((event) => ({
210
+ event,
211
+ placement: range ? resolveEventPlacement(event, range) : null
212
+ }));
213
+ const allDayLaneCount = assignOverlapMetadata(placements);
214
+ const startHour = resolveStartHour(this.startHour, placements.map(({ placement }) => placement));
215
+ const rangeSignature = range ? `${range.days[0].iso}/${range.dayCount}` : null;
216
+ const rangeChanged = rangeSignature !== this.#rangeSignature;
217
+ this.#range = range;
218
+ this.#rangeSignature = rangeSignature;
219
+ setStyleIfChanged(this, "--day-count", String(range?.dayCount ?? 0));
220
+ setStyleIfChanged(this, "--start-hour", String(startHour));
221
+ if (!range) {
222
+ this.#removeSections();
223
+ for (const { event } of placements) {
224
+ clearEventPlacement(event);
225
+ this.append(event);
226
+ }
227
+ return;
228
+ }
229
+ if (rangeChanged) {
230
+ // The section nodes can be replaced only when the visible dates change.
231
+ // Keep authored events attached while their parent section is rebuilt.
232
+ for (const { event } of placements) this.append(event);
233
+ this.#removeAllDaySection();
234
+ this.#renderSections(range);
235
+ }
236
+ this.#routeEvents(placements, range, allDayLaneCount);
237
+ this.#renderNowLine();
238
+ if (previousScrollTop !== null) this.scrollTop = previousScrollTop;
239
+ if (before) play(this, before);
240
+ if (!this.#didInitialScroll) {
241
+ this.#didInitialScroll = true;
242
+ this.#scrollStartHourIntoView();
243
+ }
244
+ }
245
+ /**
246
+ * Scroll the hours to the start hour at the next update, as on first
247
+ * layout; the page asks for this when its first data arrives.
248
+ */
249
+ scrollToStartHour() {
250
+ this.#didInitialScroll = false;
251
+ this.#updateNeeded = true;
252
+ this.#requestUpdate();
253
+ }
254
+
255
+ /** Recompute placement after keyed children change, without rebuilding the range. */
256
+ refreshEvents() {
257
+ this.#updateNeeded = true;
258
+ this.#requestUpdate();
259
+ }
260
+
261
+ /** Removes an event, letting it fade out first when it is on screen. */
262
+ removeEvent(event) {
263
+ const parent = event.parentElement?.localName;
264
+ if (this.motion && !this.sliding && motionAllowed() && (parent === "calendar-grid" || parent === "calendar-allday")) leave(event);
265
+ else event.remove();
266
+ this.refreshEvents();
267
+ }
268
+
269
+ /** Applies a pending update now rather than at the end of the task. */
270
+ updateNow() {
271
+ if (this.#updateScheduled) this.#flushUpdate();
272
+ }
273
+ #scrollStartHourIntoView() {
274
+ requestAnimationFrame(() => {
275
+ const target = this.#grid?.querySelector(":scope > .start-hour-marker");
276
+ if (!target) return;
277
+ const stickyOffset = (this.#headers?.offsetHeight ?? 0) + (this.#allDay?.offsetHeight ?? 0);
278
+ const maxScrollTop = Math.max(this.scrollHeight - this.clientHeight, 0);
279
+ if (maxScrollTop <= 0) return;
280
+ // Hour labels are vertically centered on their line. Leave room for the
281
+ // top half of the label below the sticky header and all-day band.
282
+ const desiredScrollTop = this.scrollTop + (target.getBoundingClientRect().top - this.getBoundingClientRect().top) - stickyOffset - 12;
283
+ this.scrollTop = Math.max(0, Math.min(desiredScrollTop, maxScrollTop));
284
+ });
285
+ }
286
+ #collectEvents() {
287
+ return Array.from(this.querySelectorAll("calendar-event:not([data-leaving])")).sort((left, right) =>
288
+ (left.getAttribute("start") ?? "").localeCompare(right.getAttribute("start") ?? "")
289
+ || (left.dataset.key ?? "").localeCompare(right.dataset.key ?? ""));
290
+ }
291
+ #ensureBaseSections() {
292
+ if (!this.#headers) {
293
+ this.#headers = document.createElement("calendar-headers");
294
+ this.append(this.#headers);
295
+ }
296
+ if (!this.#grid) {
297
+ this.#grid = document.createElement("calendar-grid");
298
+ this.append(this.#grid);
299
+ }
300
+ }
301
+ #ensureAllDaySection(range) {
302
+ if (!this.#allDay) {
303
+ this.#allDay = document.createElement("calendar-allday");
304
+ this.insertBefore(this.#allDay, this.#grid);
305
+ const label = document.createElement("div");
306
+ label.classList.add("allday-label");
307
+ label.textContent = "all-day";
308
+ this.#allDay.replaceChildren(label, ...range.days.map((day, index) => createAllDayCell(day, index + 1)));
309
+ }
310
+ }
311
+ #removeAllDaySection() {
312
+ this.#allDay?.remove();
313
+ this.#allDay = null;
314
+ this.style.removeProperty("--allday-lane-count");
315
+ }
316
+ #removeSections() {
317
+ this.#headers?.remove();
318
+ this.#removeAllDaySection();
319
+ this.#grid?.remove();
320
+ this.#headers = null;
321
+ this.#grid = null;
322
+ this.style.removeProperty("--allday-lane-count");
323
+ }
324
+ #renderSections(range) {
325
+ this.#ensureBaseSections();
326
+ const headerChildren = range.days.map((day, index) => createDayChip(day, index + 1, range.dayCount === 1));
327
+ const gridChildren = [
328
+ document.createElement("calendar-time-axis"),
329
+ ...range.days.map((day, index) => createGridColumn(day, index + 1)),
330
+ createStartHourMarker()
331
+ ];
332
+ this.#headers.replaceChildren(...headerChildren);
333
+ this.#grid.replaceChildren(...gridChildren);
334
+ }
335
+ #routeEvents(entries, range, allDayLaneCount) {
336
+ for (const { event } of entries.filter(({ placement }) => placement === null)) {
337
+ clearEventPlacement(event);
338
+ if (event.parentElement !== this) this.append(event);
339
+ }
340
+ const allDayEntries = entries.filter(isAllDayEntry);
341
+ if (allDayEntries.length > 0) {
342
+ this.#ensureAllDaySection(range);
343
+ setStyleIfChanged(this, "--allday-lane-count", String(allDayLaneCount));
344
+ for (const { event, placement } of allDayEntries) {
345
+ renderEventPlacement(event, placement);
346
+ if (event.parentElement !== this.#allDay) this.#allDay.append(event);
347
+ }
348
+ } else {
349
+ this.#removeAllDaySection();
350
+ }
351
+ // Events go before the now line, so the grid's children keep one order
352
+ // whenever an event is added.
353
+ const nowLine = this.#grid.querySelector(":scope > .now-line-faint");
354
+ for (const { event, placement } of entries.filter(isTimedEntry).sort(compareTimedEntries)) {
355
+ renderEventPlacement(event, placement);
356
+ if (event.parentElement !== this.#grid) this.#grid.insertBefore(event, nowLine);
357
+ }
358
+ }
359
+ #renderNowLine() {
360
+ const existing = this.#grid?.querySelector(":scope > .now-line");
361
+ const existingFaint = this.#grid?.querySelector(":scope > .now-line-faint");
362
+ const existingLabel = this.#grid?.querySelector(":scope > .now-label");
363
+ const removeNowLine = () => {
364
+ existingFaint?.remove();
365
+ existing?.remove();
366
+ existingLabel?.remove();
367
+ };
368
+ if (!this.#grid || !this.#range) {
369
+ removeNowLine();
370
+ return;
371
+ }
372
+ const now = new Date();
373
+ const todayKey = dateKey(now.getFullYear(), now.getMonth() + 1, now.getDate());
374
+ const dayIndex = todayKey - this.#range.startDayKey + 1;
375
+ if (dayIndex < 1 || dayIndex > this.#range.dayCount) {
376
+ removeNowLine();
377
+ return;
378
+ }
379
+ const fraction = (now.getHours() + now.getMinutes() / MINUTES_PER_HOUR) / HOUR_COUNT;
380
+ const faintLine = existingFaint ?? document.createElement("div");
381
+ if (!existingFaint) {
382
+ faintLine.classList.add("now-line-faint");
383
+ this.#grid.append(faintLine);
384
+ }
385
+ const line = existing ?? document.createElement("div");
386
+ if (!existing) {
387
+ line.classList.add("now-line");
388
+ this.#grid.append(line);
389
+ }
390
+ // The current time sits in the hour gutter, over the hour label it meets.
391
+ // The outer strip is pinned with the gutter; the time inside is placed.
392
+ const label = existingLabel ?? document.createElement("div");
393
+ if (!existingLabel) {
394
+ label.classList.add("now-label");
395
+ label.append(document.createElement("span"));
396
+ this.#grid.append(label);
397
+ }
398
+ const time = TIME_FORMAT.format(now);
399
+ if (label.textContent !== time) label.firstElementChild.textContent = time;
400
+ setStyleIfChanged(faintLine, "--y-fraction", String(fraction));
401
+ setStyleIfChanged(line, "--day-index", String(dayIndex));
402
+ setStyleIfChanged(line, "--y-fraction", String(fraction));
403
+ setStyleIfChanged(label, "--y-fraction", String(fraction));
404
+ }
405
+ }
406
+ customElements.define("calendar-week", CalendarWeekElement);
407
+ function createAllDayCell(day, dayIndex) {
408
+ const cell = document.createElement("calendar-cell");
409
+ cell.setAttribute("date", day.iso);
410
+ if (day.isWeekend) cell.setAttribute("data-weekend", "");
411
+ cell.style.setProperty("--day-index", String(dayIndex));
412
+ return cell;
413
+ }
414
+ function createDayChip(day, dayIndex, isOnlyDay) {
415
+ const chip = document.createElement("calendar-day");
416
+ chip.setAttribute("date", day.iso);
417
+ if (day.isWeekend) chip.setAttribute("data-weekend", "");
418
+ const today = new Date();
419
+ const todayIso = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, "0")}-${String(today.getDate()).padStart(2, "0")}`;
420
+ if (day.iso === todayIso) chip.setAttribute("data-today", "");
421
+ chip.style.setProperty("--day-index", String(dayIndex));
422
+ const weekday = document.createElement("span");
423
+ weekday.classList.add("weekday");
424
+ weekday.textContent = isOnlyDay ? day.labelWeekdayLong : day.labelWeekday;
425
+ const dateNum = document.createElement("span");
426
+ dateNum.classList.add("date-num");
427
+ dateNum.textContent = day.labelDay;
428
+ chip.append(weekday, dateNum);
429
+ return chip;
430
+ }
431
+ function createGridColumn(day, dayIndex) {
432
+ const column = document.createElement("calendar-column");
433
+ column.setAttribute("date", day.iso);
434
+ if (day.isWeekend) column.setAttribute("data-weekend", "");
435
+ column.style.setProperty("--day-index", String(dayIndex));
436
+ return column;
437
+ }
438
+ function createStartHourMarker() {
439
+ const marker = document.createElement("div");
440
+ marker.classList.add("start-hour-marker");
441
+ return marker;
442
+ }
443
+ function renderEventPlacement(event, placement) {
444
+ const block = ensureEventBlock(event);
445
+ const title = event.getAttribute("title") ?? "";
446
+ if (block.querySelector("h3").textContent !== title) block.querySelector("h3").textContent = title;
447
+ const detail = event.getAttribute("detail");
448
+ const detailElement = block.querySelector(".event-detail");
449
+ if (detail !== null && detailElement.textContent !== detail) detailElement.textContent = detail;
450
+ if (detailElement.hidden !== (detail === null)) detailElement.hidden = detail === null;
451
+ setAttendees(block.querySelector(".event-attendees"), event.attendees);
452
+ const timeElement = block.querySelector(".event-time");
453
+ if (placement.kind === "all-day") {
454
+ setStyleIfChanged(event, "--day-start", String(placement.dayStart));
455
+ setStyleIfChanged(event, "--day-span", String(placement.daySpan));
456
+ setStyleIfChanged(event, "--lane-index", String(placement.laneIndex));
457
+ if (!timeElement.hidden) timeElement.hidden = true;
458
+ removeStyles(event, TIMED_PLACEMENT_PROPERTIES);
459
+ delete event.dataset.size;
460
+ return;
461
+ }
462
+ setStyleIfChanged(event, "--day-index", String(placement.dayIndex));
463
+ setStyleIfChanged(event, "--y-start", String(placement.yStart));
464
+ setStyleIfChanged(event, "--y-end", String(placement.yEnd));
465
+ setStyleIfChanged(event, "--column-index", String(placement.columnIndex));
466
+ setStyleIfChanged(event, "--column-span", String(placement.columnSpan));
467
+ setStyleIfChanged(event, "--column-count", String(placement.columnCount));
468
+ const isShort = placement.endMinutes - placement.startMinutes <= SHORT_EVENT_MAX_MINUTES;
469
+ const size = isShort ? "short" : "long";
470
+ if (event.dataset.size !== size) event.dataset.size = size;
471
+ // A short event has room for one line, so it shows only its start time.
472
+ const time = isShort
473
+ ? TIME_FORMAT.format(timeOfDay(placement.startMinutes))
474
+ : formatTimeRange(timeOfDay(placement.startMinutes), timeOfDay(placement.endMinutes));
475
+ if (timeElement.textContent !== time) timeElement.textContent = time;
476
+ if (timeElement.hidden) timeElement.hidden = false;
477
+ removeStyles(event, ALL_DAY_PLACEMENT_PROPERTIES);
478
+ }
479
+ function clearEventPlacement(event) {
480
+ event.querySelector(":scope > .event-block")?.remove();
481
+ removeStyles(event, TIMED_PLACEMENT_PROPERTIES);
482
+ removeStyles(event, ALL_DAY_PLACEMENT_PROPERTIES);
483
+ delete event.dataset.size;
484
+ }
485
+ function removeStyles(element, names) {
486
+ for (const name of names) element.style.removeProperty(name);
487
+ }
488
+ function formatTimeRange(from, to) {
489
+ return TIME_FORMAT.formatRange(from, to).replace(RANGE_DASH_RE, "\u2009–\u2009").replace(NARROW_SPACE_RE, " ");
490
+ }
491
+ /** A Date on an arbitrary day at the given minute, for formatting a time of day. */
492
+ function timeOfDay(totalMinutes) {
493
+ return new Date(2000, 0, 1, Math.floor(totalMinutes / MINUTES_PER_HOUR), totalMinutes % MINUTES_PER_HOUR);
494
+ }
495
+ function ensureEventBlock(event) {
496
+ let block = event.querySelector(":scope > .event-block");
497
+ if (block) return block;
498
+ block = document.createElement("div");
499
+ block.classList.add("event-block");
500
+ const inner = document.createElement("div");
501
+ inner.classList.add("event-block-inner");
502
+ const heading = document.createElement("h3");
503
+ const time = document.createElement("div");
504
+ time.className = "event-time";
505
+ const detail = document.createElement("div");
506
+ detail.className = "event-detail";
507
+ inner.append(heading, time, detail, createAttendeesLine());
508
+ block.append(inner);
509
+ event.append(block);
510
+ return block;
511
+ }
512
+ function resolveEventPlacement(event, range) {
513
+ const title = event.getAttribute("title");
514
+ const startValue = event.getAttribute("start");
515
+ const endValue = event.getAttribute("end");
516
+ if (title === null || startValue === null || endValue === null) return null;
517
+ if (event.hasAttribute("all-day")) {
518
+ const start2 = parseCalendarDate(startValue);
519
+ const end2 = parseCalendarDate(endValue);
520
+ if (!start2 || !end2 || end2.dayKey <= start2.dayKey) return null;
521
+ const clippedStart = Math.max(start2.dayKey, range.startDayKey);
522
+ const clippedEnd = Math.min(end2.dayKey, range.endDayKey);
523
+ if (clippedEnd <= clippedStart) return null;
524
+ return {
525
+ kind: "all-day",
526
+ daySpan: clippedEnd - clippedStart,
527
+ dayStart: clippedStart - range.startDayKey + 1,
528
+ laneIndex: 1
529
+ };
530
+ }
531
+ const start = parseCalendarDateTime(startValue);
532
+ const end = parseCalendarDateTime(endValue);
533
+ if (!start || !end) return null;
534
+ if (start.dayKey !== end.dayKey) return null;
535
+ if (end.totalMinutes <= start.totalMinutes) return null;
536
+ const dayIndex = start.dayKey - range.startDayKey + 1;
537
+ if (dayIndex < 1 || dayIndex > range.dayCount) return null;
538
+ return {
539
+ columnCount: 1,
540
+ columnIndex: 0,
541
+ columnSpan: 1,
542
+ kind: "timed",
543
+ dayIndex,
544
+ endMinutes: end.totalMinutes,
545
+ startHour: start.hour,
546
+ startMinutes: start.totalMinutes,
547
+ yEnd: end.totalMinutes / MINUTES_PER_DAY,
548
+ yStart: start.totalMinutes / MINUTES_PER_DAY
549
+ };
550
+ }
551
+ function resolveStartHour(attributeValue, placements) {
552
+ if (Number.isInteger(attributeValue) && attributeValue >= 0 && attributeValue <= MAX_HOUR) {
553
+ return attributeValue;
554
+ }
555
+ const timed = placements.filter((placement) => placement?.kind === "timed").map((placement) => placement.startHour);
556
+ if (timed.length > 0) {
557
+ return Math.max(Math.min(...timed) - 1, 0);
558
+ }
559
+ return DEFAULT_START_HOUR;
560
+ }
561
+ function resolveVisibleRange(startDate, days) {
562
+ if (typeof days !== "number" || !Number.isInteger(days) || days < 1) return null;
563
+ if (startDate === undefined) return null;
564
+ const start = parseCalendarDate(startDate);
565
+ if (!start) return null;
566
+ const rangeDays = Array.from({ length: days }, (_, index) => shiftCalendarDate(start, index));
567
+ return {
568
+ dayCount: days,
569
+ days: rangeDays,
570
+ endDayKey: start.dayKey + days,
571
+ startDayKey: start.dayKey
572
+ };
573
+ }
574
+ function numberFromAttribute(value) {
575
+ return value === null ? undefined : Number(value);
576
+ }
577
+ function parseCalendarDate(value) {
578
+ const match = DATE_RE.exec(value);
579
+ if (!match) return null;
580
+ const [, yearText, monthText, dayText] = match;
581
+ const year = Number(yearText);
582
+ const month = Number(monthText);
583
+ const day = Number(dayText);
584
+ if (!isValidCalendarDate(year, month, day)) return null;
585
+ const displayDate = new Date(year, month - 1, day);
586
+ const weekday = displayDate.getDay();
587
+ return {
588
+ dayKey: dateKey(year, month, day),
589
+ iso: value,
590
+ isWeekend: weekday === 0 || weekday === 6,
591
+ labelDay: String(day),
592
+ labelWeekday: displayDate.toLocaleDateString(undefined, { weekday: "short" }),
593
+ labelWeekdayLong: displayDate.toLocaleDateString(undefined, { weekday: "long" })
594
+ };
595
+ }
596
+ function parseCalendarDateTime(value) {
597
+ const match = DATETIME_RE.exec(value);
598
+ if (!match) return null;
599
+ const [, yearText, monthText, dayText, hourText, minuteText] = match;
600
+ const year = Number(yearText);
601
+ const month = Number(monthText);
602
+ const day = Number(dayText);
603
+ const hour = Number(hourText);
604
+ const minute = Number(minuteText);
605
+ if (!isValidCalendarDate(year, month, day)) return null;
606
+ if (!Number.isInteger(hour) || hour < 0 || hour > MAX_HOUR) return null;
607
+ if (!Number.isInteger(minute) || minute < 0 || minute > MAX_MINUTE) return null;
608
+ return {
609
+ dayKey: dateKey(year, month, day),
610
+ hour,
611
+ totalMinutes: hour * MINUTES_PER_HOUR + minute
612
+ };
613
+ }
614
+ function shiftCalendarDate(day, offset) {
615
+ const next = new Date((day.dayKey + offset) * DAY_MS);
616
+ const year = next.getUTCFullYear();
617
+ const month = next.getUTCMonth() + 1;
618
+ const date = next.getUTCDate();
619
+ return parseCalendarDate(
620
+ `${year}-${String(month).padStart(2, "0")}-${String(date).padStart(2, "0")}`
621
+ );
622
+ }
623
+ function isValidCalendarDate(year, month, day) {
624
+ const probe = new Date(Date.UTC(year, month - 1, day));
625
+ return probe.getUTCFullYear() === year && probe.getUTCMonth() === month - 1 && probe.getUTCDate() === day;
626
+ }
627
+ function dateKey(year, month, day) {
628
+ return Date.UTC(year, month - 1, day) / DAY_MS;
629
+ }
630
+ function assignOverlapMetadata(entries) {
631
+ const lanePlacements = [];
632
+ for (const { placement } of entries.filter(isAllDayEntry)) {
633
+ const placementEnd = placement.dayStart + placement.daySpan;
634
+ let laneIndex = lanePlacements.findIndex(
635
+ (lane) => lane.every((existing) => !overlaps(existing.dayStart, existing.dayStart + existing.daySpan, placement.dayStart, placementEnd))
636
+ );
637
+ if (laneIndex === -1) {
638
+ lanePlacements.push([]);
639
+ laneIndex = lanePlacements.length - 1;
640
+ }
641
+ placement.laneIndex = laneIndex + 1;
642
+ lanePlacements[laneIndex].push(placement);
643
+ }
644
+ const timedPlacementsByDay = new Map();
645
+ for (const { placement } of entries.filter(isTimedEntry)) {
646
+ const dayPlacements = timedPlacementsByDay.get(placement.dayIndex) ?? [];
647
+ dayPlacements.push(placement);
648
+ timedPlacementsByDay.set(placement.dayIndex, dayPlacements);
649
+ }
650
+ for (const dayPlacements of timedPlacementsByDay.values()) {
651
+ for (const cluster of overlapClusters(dayPlacements)) assignColumns(cluster);
652
+ }
653
+ return lanePlacements.length;
654
+ }
655
+ /** Groups one day's timed placements into runs linked by overlapping times. */
656
+ function overlapClusters(placements) {
657
+ // Earlier events come first; among equal starts the longer one takes the
658
+ // leftmost column, as in Apple Calendar.
659
+ const sorted = [...placements].sort((left, right) => left.startMinutes - right.startMinutes || right.endMinutes - left.endMinutes);
660
+ const clusters = [];
661
+ let clusterEnd = -1;
662
+ for (const placement of sorted) {
663
+ if (placement.startMinutes >= clusterEnd) clusters.push([]);
664
+ clusters.at(-1).push(placement);
665
+ clusterEnd = Math.max(clusterEnd, placement.endMinutes);
666
+ }
667
+ return clusters;
668
+ }
669
+ /** Gives each placement in one cluster a column, a column count and a span. */
670
+ function assignColumns(cluster) {
671
+ const columns = [];
672
+ for (const placement of cluster) {
673
+ let columnIndex = columns.findIndex((column) => column.at(-1).endMinutes <= placement.startMinutes);
674
+ if (columnIndex === -1) {
675
+ columns.push([]);
676
+ columnIndex = columns.length - 1;
677
+ }
678
+ columns[columnIndex].push(placement);
679
+ placement.columnIndex = columnIndex;
680
+ }
681
+ for (const placement of cluster) {
682
+ placement.columnCount = columns.length;
683
+ // Widen into each following column that has nothing at this event's time.
684
+ let columnSpan = 1;
685
+ while (
686
+ placement.columnIndex + columnSpan < columns.length
687
+ && columns[placement.columnIndex + columnSpan].every(
688
+ (other) => !overlaps(other.startMinutes, other.endMinutes, placement.startMinutes, placement.endMinutes)
689
+ )
690
+ ) columnSpan += 1;
691
+ placement.columnSpan = columnSpan;
692
+ }
693
+ }
694
+ function compareTimedEntries(left, right) {
695
+ return left.placement.dayIndex - right.placement.dayIndex || left.placement.startMinutes - right.placement.startMinutes || left.placement.endMinutes - right.placement.endMinutes;
696
+ }
697
+ function isAllDayEntry(entry) {
698
+ return entry.placement?.kind === "all-day";
699
+ }
700
+ function isTimedEntry(entry) {
701
+ return entry.placement?.kind === "timed";
702
+ }
703
+ function overlaps(leftStart, leftEnd, rightStart, rightEnd) {
704
+ return leftStart < rightEnd && rightStart < leftEnd;
705
+ }
706
+ function setStyleIfChanged(element, name, value) {
707
+ if (element.style.getPropertyValue(name) !== value) element.style.setProperty(name, value);
708
+ }
709
+ export {
710
+ CalendarWeekElement
711
+ };