@classic-homes/gantt-core 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -2
- package/dist/calendar/index.cjs +262 -0
- package/dist/calendar/index.d.cts +71 -0
- package/dist/calendar/index.d.ts +71 -0
- package/dist/calendar/index.js +2 -0
- package/dist/chunk-7OPVG2DP.js +166 -0
- package/dist/chunk-FVSW2GH6.js +654 -0
- package/dist/chunk-FXPGR372.js +1 -0
- package/dist/chunk-LPH2V7XM.js +80 -0
- package/dist/chunk-MDRXY4HU.js +302 -0
- package/dist/index.cjs +1994 -0
- package/dist/index.d.cts +248 -0
- package/dist/index.d.ts +248 -0
- package/dist/index.js +743 -0
- package/dist/layout/index.cjs +331 -0
- package/dist/layout/index.d.cts +131 -0
- package/dist/layout/index.d.ts +131 -0
- package/dist/layout/index.js +2 -0
- package/dist/scheduling/index.cjs +848 -0
- package/dist/scheduling/index.d.cts +81 -0
- package/dist/scheduling/index.d.ts +81 -0
- package/dist/scheduling/index.js +3 -0
- package/dist/types/index.cjs +2 -0
- package/dist/types/index.d.cts +227 -0
- package/dist/types/index.d.ts +227 -0
- package/dist/types/index.js +1 -0
- package/package.json +92 -4
package/README.md
CHANGED
|
@@ -1,3 +1,75 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @classic-homes/gantt-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Framework-agnostic engine for Classic Theme Gantt charts: working calendars, a critical-path scheduler, an editable project model with undo/redo, timeline geometry and drag math. No DOM, no framework and no runtime dependencies. `@classic-homes/gantt-svelte` (and later `gantt-react`) render it.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @classic-homes/gantt-core
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Scheduling
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { scheduleProject, formatTime } from '@classic-homes/gantt-core';
|
|
13
|
+
|
|
14
|
+
const result = scheduleProject(
|
|
15
|
+
[
|
|
16
|
+
{ id: 'design', name: 'Design', start: '2026-03-02', duration: 3 },
|
|
17
|
+
{ id: 'build', name: 'Build', duration: 5 },
|
|
18
|
+
{ id: 'launch', name: 'Launch', duration: 0 }, // milestone
|
|
19
|
+
],
|
|
20
|
+
[
|
|
21
|
+
{ id: 'd1', from: 'design', to: 'build' }, // FS by default
|
|
22
|
+
{ id: 'd2', from: 'build', to: 'launch', type: 'FF', lag: 1 },
|
|
23
|
+
]
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
formatTime(result.tasks.get('build')!.start); // '2026-03-05T08:00'
|
|
27
|
+
result.critical; // ['design', 'build', 'launch']
|
|
28
|
+
result.conflicts; // [] — cycles, broken constraints, missed deadlines…
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Each `ScheduledTask` carries `start`/`finish`, early and late dates, total and free slack (working minutes) and `critical`.
|
|
32
|
+
|
|
33
|
+
### Model
|
|
34
|
+
|
|
35
|
+
| Concept | Notes |
|
|
36
|
+
| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
37
|
+
| Time | Zone-less ISO strings (`2026-03-02`, `2026-03-02T08:00`), `Date` (local fields) or wall-clock ms. Wall clock throughout: DST never moves a bar. |
|
|
38
|
+
| Date-only `end` | The end of that day's work: `start: '2026-03-02', end: '2026-03-06'` is five days. |
|
|
39
|
+
| Duration / lag | Working days (fractional allowed); `minutesPerDay` defaults to the project calendar's day (480). |
|
|
40
|
+
| Dependencies | `FS`, `SS`, `FF`, `SF`, with lag or lead. Lag uses the successor's calendar. |
|
|
41
|
+
| Constraints | `ASAP`, `ALAP`, `SNET`, `SNLT`, `FNET`, `FNLT`, `MSO`, `MFO`, plus `deadline`. |
|
|
42
|
+
| Summaries | Any task with children. Dates and duration-weighted progress roll up. Dependencies into a summary bound its whole subtree (FS/SS only). |
|
|
43
|
+
| Manual tasks | `manual: true` (or `mode: 'manual'`) keeps given dates; dependencies only report conflicts. |
|
|
44
|
+
| Calendars | Working weekdays, multiple working intervals per day, holiday and working-day exceptions. Defaults to Mon–Fri, 08–12 + 13–17. |
|
|
45
|
+
| Cycles | Reported with the path; one dependency on the cycle is ignored so the rest still schedules. |
|
|
46
|
+
|
|
47
|
+
By default a task's own `start` is a floor: dependencies push it later but never pull it earlier. Pass `respectTaskStart: false` for pure CPM.
|
|
48
|
+
|
|
49
|
+
## Editable model
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { createGanttModel } from '@classic-homes/gantt-core';
|
|
53
|
+
|
|
54
|
+
const model = createGanttModel({ tasks, dependencies, calendars });
|
|
55
|
+
model.subscribe((state) => render(state.rows, state.schedule));
|
|
56
|
+
model.onChange((change) => persist(change)); // edits, undo and redo
|
|
57
|
+
|
|
58
|
+
model.apply({ type: 'update-task', id: 'design', patch: { duration: 4 } }, 'Extend design');
|
|
59
|
+
model.undo();
|
|
60
|
+
model.validate({ type: 'add-dependency', dependency: { id: 'x', from: 'launch', to: 'design' } });
|
|
61
|
+
// → 'This link would create a circular dependency'
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`GanttChange.dates` lists every task whose scheduled dates moved, including cascaded successors. `preview()` schedules pending operations without committing (drag previews). Rows come flattened with WBS codes, levels and expand state; selection supports replace, toggle and range.
|
|
65
|
+
|
|
66
|
+
## Layout and interaction
|
|
67
|
+
|
|
68
|
+
- `createTimeScale`, `timelineRange`, `ZOOM_PRESETS` (hour → year), `scaleTicks` for two-tier headers with `Intl` labels.
|
|
69
|
+
- `virtualWindow` for row virtualisation, `barGeometry` for task/summary/milestone/baseline rectangles, `hitTestBar`.
|
|
70
|
+
- `routeDependency` + `pathFromPoints` for orthogonal dependency arrows with rounded corners.
|
|
71
|
+
- `createDragSession`, `shiftTask`, `editOperation` turn pointer travel or keyboard nudges into snapped `update-task` operations that keep your data's shape (start + end stays start + end, date-only stays date-only).
|
|
72
|
+
- `rescheduleDates` / `rescheduleOperation` (typed dates/durations, same shape rules), `parseDuration` (`3d`, `4h`, `2w`), `predecessorOperations` (diff a task's incoming links), `indentOperation`, `outdentOperations` and `insertTaskOperation` for form and grid editing.
|
|
73
|
+
- `describeTask`, `describeSchedule`, `ganttTable`, `tableToCsv`, `exportProject`, `importProject`.
|
|
74
|
+
|
|
75
|
+
Subpath exports: `/types`, `/calendar`, `/scheduling`, `/layout`.
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/calendar/time.ts
|
|
4
|
+
var MINUTE = 6e4;
|
|
5
|
+
var HOUR = 60 * MINUTE;
|
|
6
|
+
var DAY = 24 * HOUR;
|
|
7
|
+
var ISO_RE = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?/;
|
|
8
|
+
function isDateOnly(input) {
|
|
9
|
+
return typeof input === "string" && /^\d{4}-\d{2}-\d{2}$/.test(input.trim());
|
|
10
|
+
}
|
|
11
|
+
function parseTime(input) {
|
|
12
|
+
if (typeof input === "number") {
|
|
13
|
+
if (!Number.isFinite(input)) throw new RangeError(`Invalid time: ${input}`);
|
|
14
|
+
return input;
|
|
15
|
+
}
|
|
16
|
+
if (input instanceof Date) {
|
|
17
|
+
if (Number.isNaN(input.getTime())) throw new RangeError("Invalid Date");
|
|
18
|
+
return Date.UTC(
|
|
19
|
+
input.getFullYear(),
|
|
20
|
+
input.getMonth(),
|
|
21
|
+
input.getDate(),
|
|
22
|
+
input.getHours(),
|
|
23
|
+
input.getMinutes(),
|
|
24
|
+
input.getSeconds()
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
const m = ISO_RE.exec(input.trim());
|
|
28
|
+
if (!m) throw new RangeError(`Invalid time: "${input}"`);
|
|
29
|
+
const [, y, mo, d, h = "0", mi = "0", s = "0"] = m;
|
|
30
|
+
const t = Date.UTC(+y, +mo - 1, +d, +h, +mi, +s);
|
|
31
|
+
if (Number.isNaN(t)) throw new RangeError(`Invalid time: "${input}"`);
|
|
32
|
+
return t;
|
|
33
|
+
}
|
|
34
|
+
function tryParseTime(input) {
|
|
35
|
+
if (input === void 0 || input === null || input === "") return void 0;
|
|
36
|
+
try {
|
|
37
|
+
return parseTime(input);
|
|
38
|
+
} catch {
|
|
39
|
+
return void 0;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
var pad = (n) => String(n).padStart(2, "0");
|
|
43
|
+
function formatTime(t, options = {}) {
|
|
44
|
+
const d = new Date(t);
|
|
45
|
+
const date = `${d.getUTCFullYear()}-${pad(d.getUTCMonth() + 1)}-${pad(d.getUTCDate())}`;
|
|
46
|
+
if (options.dateOnly) return date;
|
|
47
|
+
return `${date}T${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())}`;
|
|
48
|
+
}
|
|
49
|
+
function toLocalDate(t) {
|
|
50
|
+
const d = new Date(t);
|
|
51
|
+
return new Date(
|
|
52
|
+
d.getUTCFullYear(),
|
|
53
|
+
d.getUTCMonth(),
|
|
54
|
+
d.getUTCDate(),
|
|
55
|
+
d.getUTCHours(),
|
|
56
|
+
d.getUTCMinutes(),
|
|
57
|
+
d.getUTCSeconds()
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
function startOfDay(t) {
|
|
61
|
+
return Math.floor(t / DAY) * DAY;
|
|
62
|
+
}
|
|
63
|
+
function dayIndex(t) {
|
|
64
|
+
return Math.floor(t / DAY);
|
|
65
|
+
}
|
|
66
|
+
function weekday(t) {
|
|
67
|
+
return new Date(t).getUTCDay();
|
|
68
|
+
}
|
|
69
|
+
function createTimeFormatter(locale, options) {
|
|
70
|
+
const key = `${locale ?? ""}|${JSON.stringify(options)}`;
|
|
71
|
+
let format = formatterCache.get(key);
|
|
72
|
+
if (!format) {
|
|
73
|
+
const fmt = new Intl.DateTimeFormat(locale, { ...options, timeZone: "UTC" });
|
|
74
|
+
format = (t) => fmt.format(t);
|
|
75
|
+
if (formatterCache.size > 64) formatterCache.clear();
|
|
76
|
+
formatterCache.set(key, format);
|
|
77
|
+
}
|
|
78
|
+
return format;
|
|
79
|
+
}
|
|
80
|
+
var formatterCache = /* @__PURE__ */ new Map();
|
|
81
|
+
|
|
82
|
+
// src/calendar/index.ts
|
|
83
|
+
var STANDARD_CALENDAR_ID = "standard";
|
|
84
|
+
var STANDARD_CALENDAR = {
|
|
85
|
+
id: STANDARD_CALENDAR_ID,
|
|
86
|
+
name: "Standard",
|
|
87
|
+
workingDays: [1, 2, 3, 4, 5],
|
|
88
|
+
workingHours: [
|
|
89
|
+
["08:00", "12:00"],
|
|
90
|
+
["13:00", "17:00"]
|
|
91
|
+
]
|
|
92
|
+
};
|
|
93
|
+
var CONTINUOUS_CALENDAR = {
|
|
94
|
+
id: "continuous",
|
|
95
|
+
name: "24 hours",
|
|
96
|
+
workingDays: [0, 1, 2, 3, 4, 5, 6],
|
|
97
|
+
workingHours: [["00:00", "24:00"]]
|
|
98
|
+
};
|
|
99
|
+
var MAX_SCAN_DAYS = 366 * 50;
|
|
100
|
+
function parseClock(value) {
|
|
101
|
+
const m = /^(\d{1,2}):(\d{2})$/.exec(value.trim());
|
|
102
|
+
if (!m) throw new RangeError(`Invalid time of day: "${value}"`);
|
|
103
|
+
const minutes = +m[1] * 60 + +m[2];
|
|
104
|
+
if (minutes < 0 || minutes > 24 * 60 || +m[2] > 59) {
|
|
105
|
+
throw new RangeError(`Invalid time of day: "${value}"`);
|
|
106
|
+
}
|
|
107
|
+
return minutes * MINUTE;
|
|
108
|
+
}
|
|
109
|
+
function compileHours(hours, calendarId) {
|
|
110
|
+
const parsed = hours.map(([s, e]) => [parseClock(s), parseClock(e)]).sort((a, b) => a[0] - b[0]);
|
|
111
|
+
for (let i = 0; i < parsed.length; i++) {
|
|
112
|
+
const [s, e] = parsed[i];
|
|
113
|
+
if (e <= s) throw new RangeError(`Calendar "${calendarId}": empty working interval`);
|
|
114
|
+
if (i > 0 && s < parsed[i - 1][1]) {
|
|
115
|
+
throw new RangeError(`Calendar "${calendarId}": overlapping working intervals`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return parsed;
|
|
119
|
+
}
|
|
120
|
+
function compileCalendar(def) {
|
|
121
|
+
const weekHours = compileHours(def.workingHours ?? STANDARD_CALENDAR.workingHours, def.id);
|
|
122
|
+
const workingDays = new Set(def.workingDays ?? STANDARD_CALENDAR.workingDays);
|
|
123
|
+
const exceptions = /* @__PURE__ */ new Map();
|
|
124
|
+
for (const ex of def.exceptions ?? []) {
|
|
125
|
+
const first = dayIndex(parseTime(ex.date));
|
|
126
|
+
const last = dayIndex(parseTime(ex.end ?? ex.date));
|
|
127
|
+
const hours = ex.working ? compileHours(ex.hours ?? def.workingHours ?? STANDARD_CALENDAR.workingHours, def.id) : [];
|
|
128
|
+
for (let d = first; d <= last; d++) exceptions.set(d, hours);
|
|
129
|
+
}
|
|
130
|
+
const hasWork = workingDays.size > 0 && weekHours.length > 0 || [...exceptions.values()].some((h) => h.length > 0);
|
|
131
|
+
if (!hasWork) throw new RangeError(`Calendar "${def.id}" has no working time`);
|
|
132
|
+
const rawIntervals = (day) => {
|
|
133
|
+
const ex = exceptions.get(day);
|
|
134
|
+
if (ex) return ex;
|
|
135
|
+
const wd = ((day + 4) % 7 + 7) % 7;
|
|
136
|
+
return workingDays.has(wd) ? weekHours : [];
|
|
137
|
+
};
|
|
138
|
+
const intervalsOn = (day) => {
|
|
139
|
+
const base = day * DAY;
|
|
140
|
+
return rawIntervals(day).map(([s, e]) => [base + s, base + e]);
|
|
141
|
+
};
|
|
142
|
+
const nextWorking = (t) => {
|
|
143
|
+
let day = dayIndex(t);
|
|
144
|
+
for (let i = 0; i < MAX_SCAN_DAYS; i++, day++) {
|
|
145
|
+
for (const [s, e] of intervalsOn(day)) {
|
|
146
|
+
if (t < e) return Math.max(s, t);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
throw new RangeError(
|
|
150
|
+
`Calendar "${def.id}": no working time after ${new Date(t).toISOString()}`
|
|
151
|
+
);
|
|
152
|
+
};
|
|
153
|
+
const prevWorking = (t) => {
|
|
154
|
+
let day = dayIndex(t - 1);
|
|
155
|
+
for (let i = 0; i < MAX_SCAN_DAYS; i++, day--) {
|
|
156
|
+
const list = intervalsOn(day);
|
|
157
|
+
for (let j = list.length - 1; j >= 0; j--) {
|
|
158
|
+
const [s, e] = list[j];
|
|
159
|
+
if (t > s) return Math.min(e, t);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
throw new RangeError(
|
|
163
|
+
`Calendar "${def.id}": no working time before ${new Date(t).toISOString()}`
|
|
164
|
+
);
|
|
165
|
+
};
|
|
166
|
+
const add = (t, minutes) => {
|
|
167
|
+
if (minutes === 0) return t;
|
|
168
|
+
let remaining = Math.abs(minutes) * MINUTE;
|
|
169
|
+
if (minutes > 0) {
|
|
170
|
+
let cursor = nextWorking(t);
|
|
171
|
+
let day = dayIndex(cursor);
|
|
172
|
+
for (let i = 0; i < MAX_SCAN_DAYS; i++, day++) {
|
|
173
|
+
for (const [s, e] of intervalsOn(day)) {
|
|
174
|
+
if (e <= cursor) continue;
|
|
175
|
+
const from = Math.max(s, cursor);
|
|
176
|
+
const available = e - from;
|
|
177
|
+
if (remaining <= available) return from + remaining;
|
|
178
|
+
remaining -= available;
|
|
179
|
+
}
|
|
180
|
+
cursor = (day + 1) * DAY;
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
let cursor = prevWorking(t);
|
|
184
|
+
let day = dayIndex(cursor - 1);
|
|
185
|
+
for (let i = 0; i < MAX_SCAN_DAYS; i++, day--) {
|
|
186
|
+
const list = intervalsOn(day);
|
|
187
|
+
for (let j = list.length - 1; j >= 0; j--) {
|
|
188
|
+
const [s, e] = list[j];
|
|
189
|
+
if (s >= cursor) continue;
|
|
190
|
+
const to = Math.min(e, cursor);
|
|
191
|
+
const available = to - s;
|
|
192
|
+
if (remaining <= available) return to - remaining;
|
|
193
|
+
remaining -= available;
|
|
194
|
+
}
|
|
195
|
+
cursor = day * DAY;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
throw new RangeError(`Calendar "${def.id}": working-time arithmetic ran out of range`);
|
|
199
|
+
};
|
|
200
|
+
const between = (a, b) => {
|
|
201
|
+
if (b < a) return -between(b, a);
|
|
202
|
+
let total = 0;
|
|
203
|
+
const last = dayIndex(b);
|
|
204
|
+
for (let day = dayIndex(a); day <= last; day++) {
|
|
205
|
+
for (const [s, e] of intervalsOn(day)) {
|
|
206
|
+
const from = Math.max(s, a);
|
|
207
|
+
const to = Math.min(e, b);
|
|
208
|
+
if (to > from) total += to - from;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return total / MINUTE;
|
|
212
|
+
};
|
|
213
|
+
const minutesPerDay = weekHours.reduce((sum, [s, e]) => sum + (e - s), 0) / MINUTE || 480;
|
|
214
|
+
return {
|
|
215
|
+
id: def.id,
|
|
216
|
+
name: def.name ?? def.id,
|
|
217
|
+
intervalsOn,
|
|
218
|
+
isWorkingDay: (day) => rawIntervals(day).length > 0,
|
|
219
|
+
isWorking: (t) => intervalsOn(dayIndex(t)).some(([s, e]) => t >= s && t < e),
|
|
220
|
+
nextWorking,
|
|
221
|
+
prevWorking,
|
|
222
|
+
add,
|
|
223
|
+
between,
|
|
224
|
+
minutesPerDay
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
function compileCalendars(defs = []) {
|
|
228
|
+
const map = /* @__PURE__ */ new Map();
|
|
229
|
+
map.set(STANDARD_CALENDAR_ID, compileCalendar(STANDARD_CALENDAR));
|
|
230
|
+
for (const def of defs) map.set(def.id, compileCalendar(def));
|
|
231
|
+
return map;
|
|
232
|
+
}
|
|
233
|
+
function nonWorkingSpans(calendar, from, to) {
|
|
234
|
+
const spans = [];
|
|
235
|
+
const last = dayIndex(to);
|
|
236
|
+
for (let day = dayIndex(from); day <= last; day++) {
|
|
237
|
+
if (calendar.isWorkingDay(day)) continue;
|
|
238
|
+
const prev = spans[spans.length - 1];
|
|
239
|
+
if (prev && prev[1] === day * DAY) prev[1] = (day + 1) * DAY;
|
|
240
|
+
else spans.push([day * DAY, (day + 1) * DAY]);
|
|
241
|
+
}
|
|
242
|
+
return spans;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
exports.CONTINUOUS_CALENDAR = CONTINUOUS_CALENDAR;
|
|
246
|
+
exports.DAY = DAY;
|
|
247
|
+
exports.HOUR = HOUR;
|
|
248
|
+
exports.MINUTE = MINUTE;
|
|
249
|
+
exports.STANDARD_CALENDAR = STANDARD_CALENDAR;
|
|
250
|
+
exports.STANDARD_CALENDAR_ID = STANDARD_CALENDAR_ID;
|
|
251
|
+
exports.compileCalendar = compileCalendar;
|
|
252
|
+
exports.compileCalendars = compileCalendars;
|
|
253
|
+
exports.createTimeFormatter = createTimeFormatter;
|
|
254
|
+
exports.dayIndex = dayIndex;
|
|
255
|
+
exports.formatTime = formatTime;
|
|
256
|
+
exports.isDateOnly = isDateOnly;
|
|
257
|
+
exports.nonWorkingSpans = nonWorkingSpans;
|
|
258
|
+
exports.parseTime = parseTime;
|
|
259
|
+
exports.startOfDay = startOfDay;
|
|
260
|
+
exports.toLocalDate = toLocalDate;
|
|
261
|
+
exports.tryParseTime = tryParseTime;
|
|
262
|
+
exports.weekday = weekday;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { WallTime, TimeInput, GanttCalendar } from '../types/index.cjs';
|
|
2
|
+
|
|
3
|
+
declare const MINUTE = 60000;
|
|
4
|
+
declare const HOUR: number;
|
|
5
|
+
declare const DAY: number;
|
|
6
|
+
/** True when the input carries a date but no time of day. */
|
|
7
|
+
declare function isDateOnly(input: TimeInput): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Parse a time into wall-clock milliseconds.
|
|
10
|
+
*
|
|
11
|
+
* Strings are read as wall-clock (any zone suffix is ignored), `Date` objects
|
|
12
|
+
* contribute their local fields, numbers pass through.
|
|
13
|
+
*/
|
|
14
|
+
declare function parseTime(input: TimeInput): WallTime;
|
|
15
|
+
/** Parse, returning undefined for missing or invalid input. */
|
|
16
|
+
declare function tryParseTime(input: TimeInput | null | undefined): WallTime | undefined;
|
|
17
|
+
/** `YYYY-MM-DD`, or `YYYY-MM-DDTHH:mm` unless `dateOnly`. */
|
|
18
|
+
declare function formatTime(t: WallTime, options?: {
|
|
19
|
+
dateOnly?: boolean;
|
|
20
|
+
}): string;
|
|
21
|
+
/** A local `Date` showing the same wall-clock fields, for date pickers and the like. */
|
|
22
|
+
declare function toLocalDate(t: WallTime): Date;
|
|
23
|
+
/** Midnight at the start of the wall-clock day. */
|
|
24
|
+
declare function startOfDay(t: WallTime): WallTime;
|
|
25
|
+
/** Day number since the epoch. */
|
|
26
|
+
declare function dayIndex(t: WallTime): number;
|
|
27
|
+
/** 0 = Sunday … 6 = Saturday */
|
|
28
|
+
declare function weekday(t: WallTime): number;
|
|
29
|
+
/** Formats wall times with `Intl`, reading them in UTC so no zone shift applies. */
|
|
30
|
+
declare function createTimeFormatter(locale: string | undefined, options: Intl.DateTimeFormatOptions): (t: WallTime) => string;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Working-time calendars.
|
|
34
|
+
*
|
|
35
|
+
* A compiled calendar answers "is this instant working time?" and does
|
|
36
|
+
* working-time arithmetic. A working interval is half-open on the start side
|
|
37
|
+
* for starts (`[s, e)`) and on the end side for finishes (`(s, e]`), so a task
|
|
38
|
+
* finishing at 17:00 is followed by one starting at 08:00 the next working day.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
declare const STANDARD_CALENDAR_ID = "standard";
|
|
42
|
+
declare const STANDARD_CALENDAR: GanttCalendar;
|
|
43
|
+
/** Every day, all day. Useful for continuous processes and tests. */
|
|
44
|
+
declare const CONTINUOUS_CALENDAR: GanttCalendar;
|
|
45
|
+
interface WorkCalendar {
|
|
46
|
+
id: string;
|
|
47
|
+
name: string;
|
|
48
|
+
/** Absolute working intervals of the day containing `t`, ascending */
|
|
49
|
+
intervalsOn(day: number): Array<[WallTime, WallTime]>;
|
|
50
|
+
isWorkingDay(day: number): boolean;
|
|
51
|
+
/** True when `t` lies inside working time (start-side semantics) */
|
|
52
|
+
isWorking(t: WallTime): boolean;
|
|
53
|
+
/** Earliest working instant at or after `t` (snaps a start forward) */
|
|
54
|
+
nextWorking(t: WallTime): WallTime;
|
|
55
|
+
/** Latest working instant at or before `t` (snaps a finish backward) */
|
|
56
|
+
prevWorking(t: WallTime): WallTime;
|
|
57
|
+
/** Add (or, when negative, subtract) working minutes */
|
|
58
|
+
add(t: WallTime, minutes: number): WallTime;
|
|
59
|
+
/** Working minutes between two instants (negative when `b < a`) */
|
|
60
|
+
between(a: WallTime, b: WallTime): number;
|
|
61
|
+
/** Working minutes in a standard working day of this calendar */
|
|
62
|
+
readonly minutesPerDay: number;
|
|
63
|
+
}
|
|
64
|
+
/** Compile a calendar definition for fast working-time arithmetic. */
|
|
65
|
+
declare function compileCalendar(def: GanttCalendar): WorkCalendar;
|
|
66
|
+
/** Compile a set of calendars keyed by id, always including the standard calendar. */
|
|
67
|
+
declare function compileCalendars(defs?: GanttCalendar[]): Map<string, WorkCalendar>;
|
|
68
|
+
/** Whole non-working days in a range, merged into spans, for timeline shading. */
|
|
69
|
+
declare function nonWorkingSpans(calendar: WorkCalendar, from: WallTime, to: WallTime): Array<[WallTime, WallTime]>;
|
|
70
|
+
|
|
71
|
+
export { CONTINUOUS_CALENDAR, DAY, HOUR, MINUTE, STANDARD_CALENDAR, STANDARD_CALENDAR_ID, type WorkCalendar, compileCalendar, compileCalendars, createTimeFormatter, dayIndex, formatTime, isDateOnly, nonWorkingSpans, parseTime, startOfDay, toLocalDate, tryParseTime, weekday };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { WallTime, TimeInput, GanttCalendar } from '../types/index.js';
|
|
2
|
+
|
|
3
|
+
declare const MINUTE = 60000;
|
|
4
|
+
declare const HOUR: number;
|
|
5
|
+
declare const DAY: number;
|
|
6
|
+
/** True when the input carries a date but no time of day. */
|
|
7
|
+
declare function isDateOnly(input: TimeInput): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Parse a time into wall-clock milliseconds.
|
|
10
|
+
*
|
|
11
|
+
* Strings are read as wall-clock (any zone suffix is ignored), `Date` objects
|
|
12
|
+
* contribute their local fields, numbers pass through.
|
|
13
|
+
*/
|
|
14
|
+
declare function parseTime(input: TimeInput): WallTime;
|
|
15
|
+
/** Parse, returning undefined for missing or invalid input. */
|
|
16
|
+
declare function tryParseTime(input: TimeInput | null | undefined): WallTime | undefined;
|
|
17
|
+
/** `YYYY-MM-DD`, or `YYYY-MM-DDTHH:mm` unless `dateOnly`. */
|
|
18
|
+
declare function formatTime(t: WallTime, options?: {
|
|
19
|
+
dateOnly?: boolean;
|
|
20
|
+
}): string;
|
|
21
|
+
/** A local `Date` showing the same wall-clock fields, for date pickers and the like. */
|
|
22
|
+
declare function toLocalDate(t: WallTime): Date;
|
|
23
|
+
/** Midnight at the start of the wall-clock day. */
|
|
24
|
+
declare function startOfDay(t: WallTime): WallTime;
|
|
25
|
+
/** Day number since the epoch. */
|
|
26
|
+
declare function dayIndex(t: WallTime): number;
|
|
27
|
+
/** 0 = Sunday … 6 = Saturday */
|
|
28
|
+
declare function weekday(t: WallTime): number;
|
|
29
|
+
/** Formats wall times with `Intl`, reading them in UTC so no zone shift applies. */
|
|
30
|
+
declare function createTimeFormatter(locale: string | undefined, options: Intl.DateTimeFormatOptions): (t: WallTime) => string;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Working-time calendars.
|
|
34
|
+
*
|
|
35
|
+
* A compiled calendar answers "is this instant working time?" and does
|
|
36
|
+
* working-time arithmetic. A working interval is half-open on the start side
|
|
37
|
+
* for starts (`[s, e)`) and on the end side for finishes (`(s, e]`), so a task
|
|
38
|
+
* finishing at 17:00 is followed by one starting at 08:00 the next working day.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
declare const STANDARD_CALENDAR_ID = "standard";
|
|
42
|
+
declare const STANDARD_CALENDAR: GanttCalendar;
|
|
43
|
+
/** Every day, all day. Useful for continuous processes and tests. */
|
|
44
|
+
declare const CONTINUOUS_CALENDAR: GanttCalendar;
|
|
45
|
+
interface WorkCalendar {
|
|
46
|
+
id: string;
|
|
47
|
+
name: string;
|
|
48
|
+
/** Absolute working intervals of the day containing `t`, ascending */
|
|
49
|
+
intervalsOn(day: number): Array<[WallTime, WallTime]>;
|
|
50
|
+
isWorkingDay(day: number): boolean;
|
|
51
|
+
/** True when `t` lies inside working time (start-side semantics) */
|
|
52
|
+
isWorking(t: WallTime): boolean;
|
|
53
|
+
/** Earliest working instant at or after `t` (snaps a start forward) */
|
|
54
|
+
nextWorking(t: WallTime): WallTime;
|
|
55
|
+
/** Latest working instant at or before `t` (snaps a finish backward) */
|
|
56
|
+
prevWorking(t: WallTime): WallTime;
|
|
57
|
+
/** Add (or, when negative, subtract) working minutes */
|
|
58
|
+
add(t: WallTime, minutes: number): WallTime;
|
|
59
|
+
/** Working minutes between two instants (negative when `b < a`) */
|
|
60
|
+
between(a: WallTime, b: WallTime): number;
|
|
61
|
+
/** Working minutes in a standard working day of this calendar */
|
|
62
|
+
readonly minutesPerDay: number;
|
|
63
|
+
}
|
|
64
|
+
/** Compile a calendar definition for fast working-time arithmetic. */
|
|
65
|
+
declare function compileCalendar(def: GanttCalendar): WorkCalendar;
|
|
66
|
+
/** Compile a set of calendars keyed by id, always including the standard calendar. */
|
|
67
|
+
declare function compileCalendars(defs?: GanttCalendar[]): Map<string, WorkCalendar>;
|
|
68
|
+
/** Whole non-working days in a range, merged into spans, for timeline shading. */
|
|
69
|
+
declare function nonWorkingSpans(calendar: WorkCalendar, from: WallTime, to: WallTime): Array<[WallTime, WallTime]>;
|
|
70
|
+
|
|
71
|
+
export { CONTINUOUS_CALENDAR, DAY, HOUR, MINUTE, STANDARD_CALENDAR, STANDARD_CALENDAR_ID, type WorkCalendar, compileCalendar, compileCalendars, createTimeFormatter, dayIndex, formatTime, isDateOnly, nonWorkingSpans, parseTime, startOfDay, toLocalDate, tryParseTime, weekday };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { CONTINUOUS_CALENDAR, STANDARD_CALENDAR, STANDARD_CALENDAR_ID, compileCalendar, compileCalendars, nonWorkingSpans } from '../chunk-7OPVG2DP.js';
|
|
2
|
+
export { DAY, HOUR, MINUTE, createTimeFormatter, dayIndex, formatTime, isDateOnly, parseTime, startOfDay, toLocalDate, tryParseTime, weekday } from '../chunk-LPH2V7XM.js';
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { dayIndex, parseTime, MINUTE, DAY } from './chunk-LPH2V7XM.js';
|
|
2
|
+
|
|
3
|
+
// src/calendar/index.ts
|
|
4
|
+
var STANDARD_CALENDAR_ID = "standard";
|
|
5
|
+
var STANDARD_CALENDAR = {
|
|
6
|
+
id: STANDARD_CALENDAR_ID,
|
|
7
|
+
name: "Standard",
|
|
8
|
+
workingDays: [1, 2, 3, 4, 5],
|
|
9
|
+
workingHours: [
|
|
10
|
+
["08:00", "12:00"],
|
|
11
|
+
["13:00", "17:00"]
|
|
12
|
+
]
|
|
13
|
+
};
|
|
14
|
+
var CONTINUOUS_CALENDAR = {
|
|
15
|
+
id: "continuous",
|
|
16
|
+
name: "24 hours",
|
|
17
|
+
workingDays: [0, 1, 2, 3, 4, 5, 6],
|
|
18
|
+
workingHours: [["00:00", "24:00"]]
|
|
19
|
+
};
|
|
20
|
+
var MAX_SCAN_DAYS = 366 * 50;
|
|
21
|
+
function parseClock(value) {
|
|
22
|
+
const m = /^(\d{1,2}):(\d{2})$/.exec(value.trim());
|
|
23
|
+
if (!m) throw new RangeError(`Invalid time of day: "${value}"`);
|
|
24
|
+
const minutes = +m[1] * 60 + +m[2];
|
|
25
|
+
if (minutes < 0 || minutes > 24 * 60 || +m[2] > 59) {
|
|
26
|
+
throw new RangeError(`Invalid time of day: "${value}"`);
|
|
27
|
+
}
|
|
28
|
+
return minutes * MINUTE;
|
|
29
|
+
}
|
|
30
|
+
function compileHours(hours, calendarId) {
|
|
31
|
+
const parsed = hours.map(([s, e]) => [parseClock(s), parseClock(e)]).sort((a, b) => a[0] - b[0]);
|
|
32
|
+
for (let i = 0; i < parsed.length; i++) {
|
|
33
|
+
const [s, e] = parsed[i];
|
|
34
|
+
if (e <= s) throw new RangeError(`Calendar "${calendarId}": empty working interval`);
|
|
35
|
+
if (i > 0 && s < parsed[i - 1][1]) {
|
|
36
|
+
throw new RangeError(`Calendar "${calendarId}": overlapping working intervals`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return parsed;
|
|
40
|
+
}
|
|
41
|
+
function compileCalendar(def) {
|
|
42
|
+
const weekHours = compileHours(def.workingHours ?? STANDARD_CALENDAR.workingHours, def.id);
|
|
43
|
+
const workingDays = new Set(def.workingDays ?? STANDARD_CALENDAR.workingDays);
|
|
44
|
+
const exceptions = /* @__PURE__ */ new Map();
|
|
45
|
+
for (const ex of def.exceptions ?? []) {
|
|
46
|
+
const first = dayIndex(parseTime(ex.date));
|
|
47
|
+
const last = dayIndex(parseTime(ex.end ?? ex.date));
|
|
48
|
+
const hours = ex.working ? compileHours(ex.hours ?? def.workingHours ?? STANDARD_CALENDAR.workingHours, def.id) : [];
|
|
49
|
+
for (let d = first; d <= last; d++) exceptions.set(d, hours);
|
|
50
|
+
}
|
|
51
|
+
const hasWork = workingDays.size > 0 && weekHours.length > 0 || [...exceptions.values()].some((h) => h.length > 0);
|
|
52
|
+
if (!hasWork) throw new RangeError(`Calendar "${def.id}" has no working time`);
|
|
53
|
+
const rawIntervals = (day) => {
|
|
54
|
+
const ex = exceptions.get(day);
|
|
55
|
+
if (ex) return ex;
|
|
56
|
+
const wd = ((day + 4) % 7 + 7) % 7;
|
|
57
|
+
return workingDays.has(wd) ? weekHours : [];
|
|
58
|
+
};
|
|
59
|
+
const intervalsOn = (day) => {
|
|
60
|
+
const base = day * DAY;
|
|
61
|
+
return rawIntervals(day).map(([s, e]) => [base + s, base + e]);
|
|
62
|
+
};
|
|
63
|
+
const nextWorking = (t) => {
|
|
64
|
+
let day = dayIndex(t);
|
|
65
|
+
for (let i = 0; i < MAX_SCAN_DAYS; i++, day++) {
|
|
66
|
+
for (const [s, e] of intervalsOn(day)) {
|
|
67
|
+
if (t < e) return Math.max(s, t);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
throw new RangeError(
|
|
71
|
+
`Calendar "${def.id}": no working time after ${new Date(t).toISOString()}`
|
|
72
|
+
);
|
|
73
|
+
};
|
|
74
|
+
const prevWorking = (t) => {
|
|
75
|
+
let day = dayIndex(t - 1);
|
|
76
|
+
for (let i = 0; i < MAX_SCAN_DAYS; i++, day--) {
|
|
77
|
+
const list = intervalsOn(day);
|
|
78
|
+
for (let j = list.length - 1; j >= 0; j--) {
|
|
79
|
+
const [s, e] = list[j];
|
|
80
|
+
if (t > s) return Math.min(e, t);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
throw new RangeError(
|
|
84
|
+
`Calendar "${def.id}": no working time before ${new Date(t).toISOString()}`
|
|
85
|
+
);
|
|
86
|
+
};
|
|
87
|
+
const add = (t, minutes) => {
|
|
88
|
+
if (minutes === 0) return t;
|
|
89
|
+
let remaining = Math.abs(minutes) * MINUTE;
|
|
90
|
+
if (minutes > 0) {
|
|
91
|
+
let cursor = nextWorking(t);
|
|
92
|
+
let day = dayIndex(cursor);
|
|
93
|
+
for (let i = 0; i < MAX_SCAN_DAYS; i++, day++) {
|
|
94
|
+
for (const [s, e] of intervalsOn(day)) {
|
|
95
|
+
if (e <= cursor) continue;
|
|
96
|
+
const from = Math.max(s, cursor);
|
|
97
|
+
const available = e - from;
|
|
98
|
+
if (remaining <= available) return from + remaining;
|
|
99
|
+
remaining -= available;
|
|
100
|
+
}
|
|
101
|
+
cursor = (day + 1) * DAY;
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
let cursor = prevWorking(t);
|
|
105
|
+
let day = dayIndex(cursor - 1);
|
|
106
|
+
for (let i = 0; i < MAX_SCAN_DAYS; i++, day--) {
|
|
107
|
+
const list = intervalsOn(day);
|
|
108
|
+
for (let j = list.length - 1; j >= 0; j--) {
|
|
109
|
+
const [s, e] = list[j];
|
|
110
|
+
if (s >= cursor) continue;
|
|
111
|
+
const to = Math.min(e, cursor);
|
|
112
|
+
const available = to - s;
|
|
113
|
+
if (remaining <= available) return to - remaining;
|
|
114
|
+
remaining -= available;
|
|
115
|
+
}
|
|
116
|
+
cursor = day * DAY;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
throw new RangeError(`Calendar "${def.id}": working-time arithmetic ran out of range`);
|
|
120
|
+
};
|
|
121
|
+
const between = (a, b) => {
|
|
122
|
+
if (b < a) return -between(b, a);
|
|
123
|
+
let total = 0;
|
|
124
|
+
const last = dayIndex(b);
|
|
125
|
+
for (let day = dayIndex(a); day <= last; day++) {
|
|
126
|
+
for (const [s, e] of intervalsOn(day)) {
|
|
127
|
+
const from = Math.max(s, a);
|
|
128
|
+
const to = Math.min(e, b);
|
|
129
|
+
if (to > from) total += to - from;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return total / MINUTE;
|
|
133
|
+
};
|
|
134
|
+
const minutesPerDay = weekHours.reduce((sum, [s, e]) => sum + (e - s), 0) / MINUTE || 480;
|
|
135
|
+
return {
|
|
136
|
+
id: def.id,
|
|
137
|
+
name: def.name ?? def.id,
|
|
138
|
+
intervalsOn,
|
|
139
|
+
isWorkingDay: (day) => rawIntervals(day).length > 0,
|
|
140
|
+
isWorking: (t) => intervalsOn(dayIndex(t)).some(([s, e]) => t >= s && t < e),
|
|
141
|
+
nextWorking,
|
|
142
|
+
prevWorking,
|
|
143
|
+
add,
|
|
144
|
+
between,
|
|
145
|
+
minutesPerDay
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function compileCalendars(defs = []) {
|
|
149
|
+
const map = /* @__PURE__ */ new Map();
|
|
150
|
+
map.set(STANDARD_CALENDAR_ID, compileCalendar(STANDARD_CALENDAR));
|
|
151
|
+
for (const def of defs) map.set(def.id, compileCalendar(def));
|
|
152
|
+
return map;
|
|
153
|
+
}
|
|
154
|
+
function nonWorkingSpans(calendar, from, to) {
|
|
155
|
+
const spans = [];
|
|
156
|
+
const last = dayIndex(to);
|
|
157
|
+
for (let day = dayIndex(from); day <= last; day++) {
|
|
158
|
+
if (calendar.isWorkingDay(day)) continue;
|
|
159
|
+
const prev = spans[spans.length - 1];
|
|
160
|
+
if (prev && prev[1] === day * DAY) prev[1] = (day + 1) * DAY;
|
|
161
|
+
else spans.push([day * DAY, (day + 1) * DAY]);
|
|
162
|
+
}
|
|
163
|
+
return spans;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export { CONTINUOUS_CALENDAR, STANDARD_CALENDAR, STANDARD_CALENDAR_ID, compileCalendar, compileCalendars, nonWorkingSpans };
|