@classic-homes/gantt-core 0.2.0 → 0.3.1

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 CHANGED
@@ -69,6 +69,7 @@ model.validate({ type: 'add-dependency', dependency: { id: 'x', from: 'launch',
69
69
  - `virtualWindow` for row virtualisation, `barGeometry` for task/summary/milestone/baseline rectangles, `hitTestBar`.
70
70
  - `routeDependency` + `pathFromPoints` for orthogonal dependency arrows with rounded corners.
71
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.
72
73
  - `describeTask`, `describeSchedule`, `ganttTable`, `tableToCsv`, `exportProject`, `importProject`.
73
74
 
74
75
  Subpath exports: `/types`, `/calendar`, `/scheduling`, `/layout`.
@@ -142,8 +142,9 @@ function compileCalendar(def) {
142
142
  const nextWorking = (t) => {
143
143
  let day = dayIndex(t);
144
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);
145
+ const base = day * DAY;
146
+ for (const [s, e] of rawIntervals(day)) {
147
+ if (t < base + e) return Math.max(base + s, t);
147
148
  }
148
149
  }
149
150
  throw new RangeError(
@@ -153,62 +154,107 @@ function compileCalendar(def) {
153
154
  const prevWorking = (t) => {
154
155
  let day = dayIndex(t - 1);
155
156
  for (let i = 0; i < MAX_SCAN_DAYS; i++, day--) {
156
- const list = intervalsOn(day);
157
+ const base = day * DAY;
158
+ const list = rawIntervals(day);
157
159
  for (let j = list.length - 1; j >= 0; j--) {
158
160
  const [s, e] = list[j];
159
- if (t > s) return Math.min(e, t);
161
+ if (t > base + s) return Math.min(base + e, t);
160
162
  }
161
163
  }
162
164
  throw new RangeError(
163
165
  `Calendar "${def.id}": no working time before ${new Date(t).toISOString()}`
164
166
  );
165
167
  };
168
+ let origin = 0;
169
+ let cum = [];
170
+ const dayWork = (day) => {
171
+ let ms = 0;
172
+ for (const [s, e] of rawIntervals(day)) ms += e - s;
173
+ return ms;
174
+ };
175
+ const ensure = (day) => {
176
+ if (cum.length === 0) {
177
+ origin = day - 366;
178
+ cum = [0];
179
+ }
180
+ if (day < origin) {
181
+ const from = Math.max(day - 366, origin - MAX_SCAN_DAYS);
182
+ if (day < from) throw new RangeError(`Calendar "${def.id}": date out of range`);
183
+ const head = [0];
184
+ for (let d = from; d < origin; d++) head.push(head[head.length - 1] + dayWork(d));
185
+ const shift = head[head.length - 1];
186
+ cum = head.concat(cum.slice(1).map((v) => v + shift));
187
+ origin = from;
188
+ }
189
+ const need = day - origin + 1;
190
+ if (need >= cum.length) {
191
+ if (need - cum.length > MAX_SCAN_DAYS)
192
+ throw new RangeError(`Calendar "${def.id}": date out of range`);
193
+ const target = need + 366;
194
+ for (let i = cum.length; i <= target; i++) cum.push(cum[i - 1] + dayWork(origin + i - 1));
195
+ }
196
+ };
197
+ const workAt = (t) => {
198
+ const day = dayIndex(t);
199
+ ensure(day);
200
+ const base = day * DAY;
201
+ let ms = cum[day - origin];
202
+ for (const [s, e] of rawIntervals(day)) {
203
+ if (t <= base + s) break;
204
+ ms += Math.min(t, base + e) - (base + s);
205
+ }
206
+ return ms;
207
+ };
166
208
  const add = (t, minutes) => {
167
209
  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;
210
+ const forward = minutes > 0;
211
+ let target = 0;
212
+ for (let i = 0; ; i++) {
213
+ if (i > MAX_SCAN_DAYS / 366)
214
+ throw new RangeError(`Calendar "${def.id}": working-time arithmetic ran out of range`);
215
+ target = workAt(t) + minutes * MINUTE;
216
+ if (forward ? cum[cum.length - 1] >= target : cum[0] <= target) break;
217
+ ensure(forward ? origin + cum.length + 366 : origin - 1);
218
+ }
219
+ if (forward) {
220
+ let lo = 0;
221
+ let hi = cum.length - 1;
222
+ while (lo < hi) {
223
+ const mid = lo + hi >> 1;
224
+ if (cum[mid + 1] >= target) hi = mid;
225
+ else lo = mid + 1;
226
+ }
227
+ const day = origin + lo;
228
+ const base = day * DAY;
229
+ let r = target - cum[lo];
230
+ for (const [s, e] of rawIntervals(day)) {
231
+ if (r <= e - s) return base + s + r;
232
+ r -= e - s;
181
233
  }
182
234
  } 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;
235
+ let lo = 0;
236
+ let hi = cum.length - 2;
237
+ while (lo < hi) {
238
+ const mid = lo + hi + 1 >> 1;
239
+ if (cum[mid] <= target) lo = mid;
240
+ else hi = mid - 1;
241
+ }
242
+ const day = origin + lo;
243
+ const base = day * DAY;
244
+ let r = target - cum[lo];
245
+ const list = rawIntervals(day);
246
+ for (let j = 0; j < list.length; j++) {
247
+ const [s, e] = list[j];
248
+ if (r < e - s || j === list.length - 1) return base + s + r;
249
+ r -= e - s;
196
250
  }
197
251
  }
198
252
  throw new RangeError(`Calendar "${def.id}": working-time arithmetic ran out of range`);
199
253
  };
200
254
  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;
255
+ ensure(dayIndex(a));
256
+ ensure(dayIndex(b));
257
+ return (workAt(b) - workAt(a)) / MINUTE || 0;
212
258
  };
213
259
  const minutesPerDay = weekHours.reduce((sum, [s, e]) => sum + (e - s), 0) / MINUTE || 480;
214
260
  return {
@@ -1,2 +1,2 @@
1
- export { CONTINUOUS_CALENDAR, STANDARD_CALENDAR, STANDARD_CALENDAR_ID, compileCalendar, compileCalendars, nonWorkingSpans } from '../chunk-7OPVG2DP.js';
1
+ export { CONTINUOUS_CALENDAR, STANDARD_CALENDAR, STANDARD_CALENDAR_ID, compileCalendar, compileCalendars, nonWorkingSpans } from '../chunk-3GIZDFDV.js';
2
2
  export { DAY, HOUR, MINUTE, createTimeFormatter, dayIndex, formatTime, isDateOnly, parseTime, startOfDay, toLocalDate, tryParseTime, weekday } from '../chunk-LPH2V7XM.js';
@@ -63,8 +63,9 @@ function compileCalendar(def) {
63
63
  const nextWorking = (t) => {
64
64
  let day = dayIndex(t);
65
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);
66
+ const base = day * DAY;
67
+ for (const [s, e] of rawIntervals(day)) {
68
+ if (t < base + e) return Math.max(base + s, t);
68
69
  }
69
70
  }
70
71
  throw new RangeError(
@@ -74,62 +75,107 @@ function compileCalendar(def) {
74
75
  const prevWorking = (t) => {
75
76
  let day = dayIndex(t - 1);
76
77
  for (let i = 0; i < MAX_SCAN_DAYS; i++, day--) {
77
- const list = intervalsOn(day);
78
+ const base = day * DAY;
79
+ const list = rawIntervals(day);
78
80
  for (let j = list.length - 1; j >= 0; j--) {
79
81
  const [s, e] = list[j];
80
- if (t > s) return Math.min(e, t);
82
+ if (t > base + s) return Math.min(base + e, t);
81
83
  }
82
84
  }
83
85
  throw new RangeError(
84
86
  `Calendar "${def.id}": no working time before ${new Date(t).toISOString()}`
85
87
  );
86
88
  };
89
+ let origin = 0;
90
+ let cum = [];
91
+ const dayWork = (day) => {
92
+ let ms = 0;
93
+ for (const [s, e] of rawIntervals(day)) ms += e - s;
94
+ return ms;
95
+ };
96
+ const ensure = (day) => {
97
+ if (cum.length === 0) {
98
+ origin = day - 366;
99
+ cum = [0];
100
+ }
101
+ if (day < origin) {
102
+ const from = Math.max(day - 366, origin - MAX_SCAN_DAYS);
103
+ if (day < from) throw new RangeError(`Calendar "${def.id}": date out of range`);
104
+ const head = [0];
105
+ for (let d = from; d < origin; d++) head.push(head[head.length - 1] + dayWork(d));
106
+ const shift = head[head.length - 1];
107
+ cum = head.concat(cum.slice(1).map((v) => v + shift));
108
+ origin = from;
109
+ }
110
+ const need = day - origin + 1;
111
+ if (need >= cum.length) {
112
+ if (need - cum.length > MAX_SCAN_DAYS)
113
+ throw new RangeError(`Calendar "${def.id}": date out of range`);
114
+ const target = need + 366;
115
+ for (let i = cum.length; i <= target; i++) cum.push(cum[i - 1] + dayWork(origin + i - 1));
116
+ }
117
+ };
118
+ const workAt = (t) => {
119
+ const day = dayIndex(t);
120
+ ensure(day);
121
+ const base = day * DAY;
122
+ let ms = cum[day - origin];
123
+ for (const [s, e] of rawIntervals(day)) {
124
+ if (t <= base + s) break;
125
+ ms += Math.min(t, base + e) - (base + s);
126
+ }
127
+ return ms;
128
+ };
87
129
  const add = (t, minutes) => {
88
130
  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;
131
+ const forward = minutes > 0;
132
+ let target = 0;
133
+ for (let i = 0; ; i++) {
134
+ if (i > MAX_SCAN_DAYS / 366)
135
+ throw new RangeError(`Calendar "${def.id}": working-time arithmetic ran out of range`);
136
+ target = workAt(t) + minutes * MINUTE;
137
+ if (forward ? cum[cum.length - 1] >= target : cum[0] <= target) break;
138
+ ensure(forward ? origin + cum.length + 366 : origin - 1);
139
+ }
140
+ if (forward) {
141
+ let lo = 0;
142
+ let hi = cum.length - 1;
143
+ while (lo < hi) {
144
+ const mid = lo + hi >> 1;
145
+ if (cum[mid + 1] >= target) hi = mid;
146
+ else lo = mid + 1;
147
+ }
148
+ const day = origin + lo;
149
+ const base = day * DAY;
150
+ let r = target - cum[lo];
151
+ for (const [s, e] of rawIntervals(day)) {
152
+ if (r <= e - s) return base + s + r;
153
+ r -= e - s;
102
154
  }
103
155
  } 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;
156
+ let lo = 0;
157
+ let hi = cum.length - 2;
158
+ while (lo < hi) {
159
+ const mid = lo + hi + 1 >> 1;
160
+ if (cum[mid] <= target) lo = mid;
161
+ else hi = mid - 1;
162
+ }
163
+ const day = origin + lo;
164
+ const base = day * DAY;
165
+ let r = target - cum[lo];
166
+ const list = rawIntervals(day);
167
+ for (let j = 0; j < list.length; j++) {
168
+ const [s, e] = list[j];
169
+ if (r < e - s || j === list.length - 1) return base + s + r;
170
+ r -= e - s;
117
171
  }
118
172
  }
119
173
  throw new RangeError(`Calendar "${def.id}": working-time arithmetic ran out of range`);
120
174
  };
121
175
  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;
176
+ ensure(dayIndex(a));
177
+ ensure(dayIndex(b));
178
+ return (workAt(b) - workAt(a)) / MINUTE || 0;
133
179
  };
134
180
  const minutesPerDay = weekHours.reduce((sum, [s, e]) => sum + (e - s), 0) / MINUTE || 480;
135
181
  return {
@@ -1,4 +1,4 @@
1
- import { compileCalendars, STANDARD_CALENDAR_ID } from './chunk-7OPVG2DP.js';
1
+ import { compileCalendars, STANDARD_CALENDAR_ID } from './chunk-3GIZDFDV.js';
2
2
  import { parseTime, startOfDay, dayIndex, isDateOnly, DAY } from './chunk-LPH2V7XM.js';
3
3
 
4
4
  // src/scheduling/graph.ts
package/dist/index.cjs CHANGED
@@ -142,8 +142,9 @@ function compileCalendar(def) {
142
142
  const nextWorking = (t) => {
143
143
  let day = dayIndex(t);
144
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);
145
+ const base = day * DAY;
146
+ for (const [s, e] of rawIntervals(day)) {
147
+ if (t < base + e) return Math.max(base + s, t);
147
148
  }
148
149
  }
149
150
  throw new RangeError(
@@ -153,62 +154,107 @@ function compileCalendar(def) {
153
154
  const prevWorking = (t) => {
154
155
  let day = dayIndex(t - 1);
155
156
  for (let i = 0; i < MAX_SCAN_DAYS; i++, day--) {
156
- const list = intervalsOn(day);
157
+ const base = day * DAY;
158
+ const list = rawIntervals(day);
157
159
  for (let j = list.length - 1; j >= 0; j--) {
158
160
  const [s, e] = list[j];
159
- if (t > s) return Math.min(e, t);
161
+ if (t > base + s) return Math.min(base + e, t);
160
162
  }
161
163
  }
162
164
  throw new RangeError(
163
165
  `Calendar "${def.id}": no working time before ${new Date(t).toISOString()}`
164
166
  );
165
167
  };
168
+ let origin = 0;
169
+ let cum = [];
170
+ const dayWork = (day) => {
171
+ let ms = 0;
172
+ for (const [s, e] of rawIntervals(day)) ms += e - s;
173
+ return ms;
174
+ };
175
+ const ensure = (day) => {
176
+ if (cum.length === 0) {
177
+ origin = day - 366;
178
+ cum = [0];
179
+ }
180
+ if (day < origin) {
181
+ const from = Math.max(day - 366, origin - MAX_SCAN_DAYS);
182
+ if (day < from) throw new RangeError(`Calendar "${def.id}": date out of range`);
183
+ const head = [0];
184
+ for (let d = from; d < origin; d++) head.push(head[head.length - 1] + dayWork(d));
185
+ const shift = head[head.length - 1];
186
+ cum = head.concat(cum.slice(1).map((v) => v + shift));
187
+ origin = from;
188
+ }
189
+ const need = day - origin + 1;
190
+ if (need >= cum.length) {
191
+ if (need - cum.length > MAX_SCAN_DAYS)
192
+ throw new RangeError(`Calendar "${def.id}": date out of range`);
193
+ const target = need + 366;
194
+ for (let i = cum.length; i <= target; i++) cum.push(cum[i - 1] + dayWork(origin + i - 1));
195
+ }
196
+ };
197
+ const workAt = (t) => {
198
+ const day = dayIndex(t);
199
+ ensure(day);
200
+ const base = day * DAY;
201
+ let ms = cum[day - origin];
202
+ for (const [s, e] of rawIntervals(day)) {
203
+ if (t <= base + s) break;
204
+ ms += Math.min(t, base + e) - (base + s);
205
+ }
206
+ return ms;
207
+ };
166
208
  const add = (t, minutes) => {
167
209
  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;
210
+ const forward = minutes > 0;
211
+ let target = 0;
212
+ for (let i = 0; ; i++) {
213
+ if (i > MAX_SCAN_DAYS / 366)
214
+ throw new RangeError(`Calendar "${def.id}": working-time arithmetic ran out of range`);
215
+ target = workAt(t) + minutes * MINUTE;
216
+ if (forward ? cum[cum.length - 1] >= target : cum[0] <= target) break;
217
+ ensure(forward ? origin + cum.length + 366 : origin - 1);
218
+ }
219
+ if (forward) {
220
+ let lo = 0;
221
+ let hi = cum.length - 1;
222
+ while (lo < hi) {
223
+ const mid = lo + hi >> 1;
224
+ if (cum[mid + 1] >= target) hi = mid;
225
+ else lo = mid + 1;
226
+ }
227
+ const day = origin + lo;
228
+ const base = day * DAY;
229
+ let r = target - cum[lo];
230
+ for (const [s, e] of rawIntervals(day)) {
231
+ if (r <= e - s) return base + s + r;
232
+ r -= e - s;
181
233
  }
182
234
  } 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;
235
+ let lo = 0;
236
+ let hi = cum.length - 2;
237
+ while (lo < hi) {
238
+ const mid = lo + hi + 1 >> 1;
239
+ if (cum[mid] <= target) lo = mid;
240
+ else hi = mid - 1;
241
+ }
242
+ const day = origin + lo;
243
+ const base = day * DAY;
244
+ let r = target - cum[lo];
245
+ const list = rawIntervals(day);
246
+ for (let j = 0; j < list.length; j++) {
247
+ const [s, e] = list[j];
248
+ if (r < e - s || j === list.length - 1) return base + s + r;
249
+ r -= e - s;
196
250
  }
197
251
  }
198
252
  throw new RangeError(`Calendar "${def.id}": working-time arithmetic ran out of range`);
199
253
  };
200
254
  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;
255
+ ensure(dayIndex(a));
256
+ ensure(dayIndex(b));
257
+ return (workAt(b) - workAt(a)) / MINUTE || 0;
212
258
  };
213
259
  const minutesPerDay = weekHours.reduce((sum, [s, e]) => sum + (e - s), 0) / MINUTE || 480;
214
260
  return {
@@ -1646,6 +1692,109 @@ function linkOperation(from, to, type = "FS", extra = {}) {
1646
1692
  dependency: { id: extra.id ?? createId("dep"), from, to, type, ...extra }
1647
1693
  };
1648
1694
  }
1695
+ function rescheduleDates(ctx, edit) {
1696
+ const { task, calendar: cal, minutesPerDay } = ctx;
1697
+ const byDay = ctx.snap !== "hour";
1698
+ const start = edit.start === void 0 ? task.start : cal.nextWorking(byDay ? startOfDay(edit.start) : edit.start);
1699
+ let finish;
1700
+ if (edit.duration !== void 0) {
1701
+ finish = cal.add(start, Math.max(0, edit.duration) * minutesPerDay);
1702
+ } else if (edit.finish !== void 0) {
1703
+ finish = cal.prevWorking(byDay ? startOfDay(edit.finish) + DAY : edit.finish);
1704
+ } else {
1705
+ finish = task.type === "milestone" ? start : cal.add(start, task.duration);
1706
+ }
1707
+ finish = Math.max(start, finish);
1708
+ return { start, finish, changed: start !== task.start || finish !== task.finish };
1709
+ }
1710
+ function rescheduleOperation(ctx, edit) {
1711
+ const { task, calendar: cal } = ctx;
1712
+ const byDay = ctx.snap !== "hour";
1713
+ const wasMilestone = task.type === "milestone";
1714
+ const preview = rescheduleDates(ctx, edit);
1715
+ const { start, finish } = preview;
1716
+ if (!preview.changed) return null;
1717
+ const moved = start !== task.start;
1718
+ const length = cal.between(start, finish);
1719
+ const resized = length !== task.duration;
1720
+ const c = {
1721
+ ...ctx,
1722
+ snap: byDay ? "day" : "hour",
1723
+ task: wasMilestone && length > 0 ? { ...task, type: "task" } : task
1724
+ };
1725
+ const patch = {};
1726
+ if (moved) Object.assign(patch, patchOf(editOperation(c, "move", preview)));
1727
+ if (resized) {
1728
+ if (length === 0) patch.duration = 0;
1729
+ else Object.assign(patch, patchOf(editOperation(c, "resize-end", preview)));
1730
+ }
1731
+ return Object.keys(patch).length ? { type: "update-task", id: ctx.source.id, patch } : null;
1732
+ }
1733
+ var patchOf = (op) => op?.type === "update-task" ? op.patch : {};
1734
+ function predecessorOperations(taskId, dependencies, drafts) {
1735
+ const existing = dependencies.filter((d) => d.to === taskId);
1736
+ const kept = new Set(drafts.map((d) => d.id).filter(Boolean));
1737
+ const ops = existing.filter((d) => !kept.has(d.id)).map((d) => ({ type: "remove-dependency", id: d.id }));
1738
+ const adds = [];
1739
+ for (const draft of drafts) {
1740
+ const type = draft.type ?? "FS";
1741
+ const lag = draft.lag ?? 0;
1742
+ const prev = draft.id ? existing.find((d) => d.id === draft.id) : void 0;
1743
+ if (!prev) {
1744
+ adds.push(linkOperation(draft.from, taskId, type, lag ? { lag } : {}));
1745
+ continue;
1746
+ }
1747
+ const patch = {};
1748
+ if (prev.from !== draft.from) patch.from = draft.from;
1749
+ if ((prev.type ?? "FS") !== type) patch.type = type;
1750
+ if ((prev.lag ?? 0) !== lag) patch.lag = lag;
1751
+ if (Object.keys(patch).length) ops.push({ type: "update-dependency", id: prev.id, patch });
1752
+ }
1753
+ return [...ops, ...adds];
1754
+ }
1755
+ function parseDuration(text, minutesPerDay = 480) {
1756
+ const m = /^\s*(\d+(?:[.,]\d+)?)\s*([a-z]*)\s*$/i.exec(text);
1757
+ if (!m) return void 0;
1758
+ const n = Number(m[1].replace(",", "."));
1759
+ const unit = m[2].toLowerCase();
1760
+ if (!unit || /^d(ays?)?$/.test(unit)) return n;
1761
+ if (/^h((ou)?rs?)?$/.test(unit)) return round(n * 60 / minutesPerDay);
1762
+ if (/^w((ee)?ks?)?$/.test(unit)) return n * 5;
1763
+ return void 0;
1764
+ }
1765
+ function indentOperation(tasks, id) {
1766
+ const tree = buildTaskTree(tasks);
1767
+ const siblings = tree.children.get(tree.parent.get(id) ?? null) ?? [];
1768
+ const i = siblings.indexOf(id);
1769
+ if (i <= 0) return null;
1770
+ return { type: "update-task", id, patch: { parentId: siblings[i - 1] } };
1771
+ }
1772
+ function outdentOperations(tasks, id) {
1773
+ const tree = buildTaskTree(tasks);
1774
+ const parent = tree.parent.get(id) ?? null;
1775
+ if (parent === null) return [];
1776
+ const grand = tree.parent.get(parent) ?? null;
1777
+ const siblings = tree.children.get(parent) ?? [];
1778
+ const below = siblings.slice(siblings.indexOf(id) + 1);
1779
+ return [
1780
+ { type: "update-task", id, patch: { parentId: grand ?? void 0 } },
1781
+ ...below.map(
1782
+ (s) => ({ type: "update-task", id: s, patch: { parentId: id } })
1783
+ )
1784
+ ];
1785
+ }
1786
+ function insertTaskOperation(tasks, afterId, task) {
1787
+ const after = afterId ? tasks.find((t) => t.id === afterId) : void 0;
1788
+ const full = { ...task, id: task.id ?? createId("task") };
1789
+ if (!after) return { type: "add-task", task: full };
1790
+ if (after.parentId) full.parentId = after.parentId;
1791
+ const subtree = /* @__PURE__ */ new Set([after.id, ...descendantsOf(buildTaskTree(tasks), after.id)]);
1792
+ let last = -1;
1793
+ tasks.forEach((t, i) => {
1794
+ if (subtree.has(t.id)) last = i;
1795
+ });
1796
+ return { type: "add-task", task: full, index: last + 1 };
1797
+ }
1649
1798
 
1650
1799
  // src/a11y/index.ts
1651
1800
  var DEP_WORDS = {
@@ -1858,14 +2007,21 @@ exports.formatTime = formatTime;
1858
2007
  exports.ganttTable = ganttTable;
1859
2008
  exports.hitTestBar = hitTestBar;
1860
2009
  exports.importProject = importProject;
2010
+ exports.indentOperation = indentOperation;
2011
+ exports.insertTaskOperation = insertTaskOperation;
1861
2012
  exports.isAncestor = isAncestor;
1862
2013
  exports.isDateOnly = isDateOnly;
1863
2014
  exports.leavesOf = leavesOf;
1864
2015
  exports.linkOperation = linkOperation;
1865
2016
  exports.nonWorkingSpans = nonWorkingSpans;
2017
+ exports.outdentOperations = outdentOperations;
2018
+ exports.parseDuration = parseDuration;
1866
2019
  exports.parseTime = parseTime;
1867
2020
  exports.pathFromPoints = pathFromPoints;
2021
+ exports.predecessorOperations = predecessorOperations;
1868
2022
  exports.progressAt = progressAt;
2023
+ exports.rescheduleDates = rescheduleDates;
2024
+ exports.rescheduleOperation = rescheduleOperation;
1869
2025
  exports.routeDependency = routeDependency;
1870
2026
  exports.scaleTicks = scaleTicks;
1871
2027
  exports.scheduleProject = scheduleProject;
package/dist/index.d.cts CHANGED
@@ -139,6 +139,60 @@ declare function dependencyTypeFor(fromSide: LinkSide, toSide: LinkSide): Depend
139
139
  /** A reasonably unique id for new tasks and dependencies. */
140
140
  declare function createId(prefix?: string): string;
141
141
  declare function linkOperation(from: string, to: string, type?: DependencyType, extra?: Partial<Omit<GanttDependency, 'from' | 'to' | 'type'>>): GanttOperation;
142
+ /** The `update-task` member of `GanttOperation`. */
143
+ type UpdateTaskOperation = Extract<GanttOperation, {
144
+ type: 'update-task';
145
+ }>;
146
+ interface TaskDateEdit {
147
+ /** New start. With a day (or coarser) snap it names a day: work starts on it. */
148
+ start?: WallTime;
149
+ /** New finish. With a day snap it names the day the work ends on. */
150
+ finish?: WallTime;
151
+ /** New length in working days; wins over `finish` */
152
+ duration?: number;
153
+ }
154
+ /** Dates a form edit resolves to: snapped to working time, finish never before start. */
155
+ declare function rescheduleDates(ctx: EditContext, edit: TaskDateEdit): EditPreview;
156
+ /**
157
+ * The `update-task` operation for dates typed into a form, keeping the
158
+ * consumer's data shape like drags do. Moving the start keeps the length
159
+ * unless a new finish or duration comes with it. Returns null when nothing
160
+ * changes. A finish before the start is clamped to the start.
161
+ */
162
+ declare function rescheduleOperation(ctx: EditContext, edit: TaskDateEdit): UpdateTaskOperation | null;
163
+ /** A predecessor row in a form. Rows without an `id` are new links. */
164
+ interface PredecessorDraft {
165
+ id?: string;
166
+ from: string;
167
+ type?: DependencyType;
168
+ /** Working days; negative is a lead */
169
+ lag?: number;
170
+ }
171
+ /**
172
+ * Operations turning a task's incoming dependencies into `drafts`: removals
173
+ * first, then updates, then additions, so swapping a link never trips the
174
+ * duplicate check.
175
+ */
176
+ declare function predecessorOperations(taskId: string, dependencies: GanttDependency[], drafts: PredecessorDraft[]): GanttOperation[];
177
+ /**
178
+ * Working days in a typed duration: `3`, `3d`, `1.5 days`, `4h`, `2w`
179
+ * (a week is five working days). Undefined when it doesn't parse.
180
+ */
181
+ declare function parseDuration(text: string, minutesPerDay?: number): number | undefined;
182
+ /** Make a task the last child of the sibling above it. Null when there is none. */
183
+ declare function indentOperation(tasks: GanttTask[], id: string): UpdateTaskOperation | null;
184
+ /**
185
+ * Move a task up one level, right after its old parent. Like most project
186
+ * tools, the siblings below it become its children, so no row moves.
187
+ */
188
+ declare function outdentOperations(tasks: GanttTask[], id: string): UpdateTaskOperation[];
189
+ /**
190
+ * Insert a task as the next sibling of `afterId` (after its whole subtree),
191
+ * or at the end when `afterId` is null or unknown.
192
+ */
193
+ declare function insertTaskOperation(tasks: GanttTask[], afterId: string | null, task: Omit<GanttTask, 'id' | 'parentId'> & {
194
+ id?: string;
195
+ }): GanttOperation;
142
196
 
143
197
  /**
144
198
  * Screen-reader text and tabular fallbacks.
@@ -191,4 +245,4 @@ declare function importProject(input: string | unknown): GanttProjectData;
191
245
  /** RFC 4180 CSV for a table. */
192
246
  declare function tableToCsv(table: GanttTable): string;
193
247
 
194
- export { DependencyType, type DescribeOptions, type EditContext, type EditMode, type EditPreview, GanttCalendar, GanttChange, GanttDependency, GanttEditError, type GanttModel, type GanttModelOptions, type GanttModelState, GanttOperation, type GanttProjectData, GanttRow, type GanttTable, type GanttTableColumn, GanttTask, type LinkSide, ScheduleOptions, ScheduleResult, ScheduledTask, type SelectMode, TaskTree, TimeScale, TimeUnit, WallTime, WorkCalendar, createDragSession, createGanttModel, createId, dependencyTypeFor, describeSchedule, describeTask, editOperation, exportProject, flattenTasks, formatDuration, ganttTable, importProject, linkOperation, progressAt, shiftTask, tableToCsv, unitsForDelta };
248
+ export { DependencyType, type DescribeOptions, type EditContext, type EditMode, type EditPreview, GanttCalendar, GanttChange, GanttDependency, GanttEditError, type GanttModel, type GanttModelOptions, type GanttModelState, GanttOperation, type GanttProjectData, GanttRow, type GanttTable, type GanttTableColumn, GanttTask, type LinkSide, type PredecessorDraft, ScheduleOptions, ScheduleResult, ScheduledTask, type SelectMode, type TaskDateEdit, TaskTree, TimeScale, TimeUnit, type UpdateTaskOperation, WallTime, WorkCalendar, createDragSession, createGanttModel, createId, dependencyTypeFor, describeSchedule, describeTask, editOperation, exportProject, flattenTasks, formatDuration, ganttTable, importProject, indentOperation, insertTaskOperation, linkOperation, outdentOperations, parseDuration, predecessorOperations, progressAt, rescheduleDates, rescheduleOperation, shiftTask, tableToCsv, unitsForDelta };
package/dist/index.d.ts CHANGED
@@ -139,6 +139,60 @@ declare function dependencyTypeFor(fromSide: LinkSide, toSide: LinkSide): Depend
139
139
  /** A reasonably unique id for new tasks and dependencies. */
140
140
  declare function createId(prefix?: string): string;
141
141
  declare function linkOperation(from: string, to: string, type?: DependencyType, extra?: Partial<Omit<GanttDependency, 'from' | 'to' | 'type'>>): GanttOperation;
142
+ /** The `update-task` member of `GanttOperation`. */
143
+ type UpdateTaskOperation = Extract<GanttOperation, {
144
+ type: 'update-task';
145
+ }>;
146
+ interface TaskDateEdit {
147
+ /** New start. With a day (or coarser) snap it names a day: work starts on it. */
148
+ start?: WallTime;
149
+ /** New finish. With a day snap it names the day the work ends on. */
150
+ finish?: WallTime;
151
+ /** New length in working days; wins over `finish` */
152
+ duration?: number;
153
+ }
154
+ /** Dates a form edit resolves to: snapped to working time, finish never before start. */
155
+ declare function rescheduleDates(ctx: EditContext, edit: TaskDateEdit): EditPreview;
156
+ /**
157
+ * The `update-task` operation for dates typed into a form, keeping the
158
+ * consumer's data shape like drags do. Moving the start keeps the length
159
+ * unless a new finish or duration comes with it. Returns null when nothing
160
+ * changes. A finish before the start is clamped to the start.
161
+ */
162
+ declare function rescheduleOperation(ctx: EditContext, edit: TaskDateEdit): UpdateTaskOperation | null;
163
+ /** A predecessor row in a form. Rows without an `id` are new links. */
164
+ interface PredecessorDraft {
165
+ id?: string;
166
+ from: string;
167
+ type?: DependencyType;
168
+ /** Working days; negative is a lead */
169
+ lag?: number;
170
+ }
171
+ /**
172
+ * Operations turning a task's incoming dependencies into `drafts`: removals
173
+ * first, then updates, then additions, so swapping a link never trips the
174
+ * duplicate check.
175
+ */
176
+ declare function predecessorOperations(taskId: string, dependencies: GanttDependency[], drafts: PredecessorDraft[]): GanttOperation[];
177
+ /**
178
+ * Working days in a typed duration: `3`, `3d`, `1.5 days`, `4h`, `2w`
179
+ * (a week is five working days). Undefined when it doesn't parse.
180
+ */
181
+ declare function parseDuration(text: string, minutesPerDay?: number): number | undefined;
182
+ /** Make a task the last child of the sibling above it. Null when there is none. */
183
+ declare function indentOperation(tasks: GanttTask[], id: string): UpdateTaskOperation | null;
184
+ /**
185
+ * Move a task up one level, right after its old parent. Like most project
186
+ * tools, the siblings below it become its children, so no row moves.
187
+ */
188
+ declare function outdentOperations(tasks: GanttTask[], id: string): UpdateTaskOperation[];
189
+ /**
190
+ * Insert a task as the next sibling of `afterId` (after its whole subtree),
191
+ * or at the end when `afterId` is null or unknown.
192
+ */
193
+ declare function insertTaskOperation(tasks: GanttTask[], afterId: string | null, task: Omit<GanttTask, 'id' | 'parentId'> & {
194
+ id?: string;
195
+ }): GanttOperation;
142
196
 
143
197
  /**
144
198
  * Screen-reader text and tabular fallbacks.
@@ -191,4 +245,4 @@ declare function importProject(input: string | unknown): GanttProjectData;
191
245
  /** RFC 4180 CSV for a table. */
192
246
  declare function tableToCsv(table: GanttTable): string;
193
247
 
194
- export { DependencyType, type DescribeOptions, type EditContext, type EditMode, type EditPreview, GanttCalendar, GanttChange, GanttDependency, GanttEditError, type GanttModel, type GanttModelOptions, type GanttModelState, GanttOperation, type GanttProjectData, GanttRow, type GanttTable, type GanttTableColumn, GanttTask, type LinkSide, ScheduleOptions, ScheduleResult, ScheduledTask, type SelectMode, TaskTree, TimeScale, TimeUnit, WallTime, WorkCalendar, createDragSession, createGanttModel, createId, dependencyTypeFor, describeSchedule, describeTask, editOperation, exportProject, flattenTasks, formatDuration, ganttTable, importProject, linkOperation, progressAt, shiftTask, tableToCsv, unitsForDelta };
248
+ export { DependencyType, type DescribeOptions, type EditContext, type EditMode, type EditPreview, GanttCalendar, GanttChange, GanttDependency, GanttEditError, type GanttModel, type GanttModelOptions, type GanttModelState, GanttOperation, type GanttProjectData, GanttRow, type GanttTable, type GanttTableColumn, GanttTask, type LinkSide, type PredecessorDraft, ScheduleOptions, ScheduleResult, ScheduledTask, type SelectMode, type TaskDateEdit, TaskTree, TimeScale, TimeUnit, type UpdateTaskOperation, WallTime, WorkCalendar, createDragSession, createGanttModel, createId, dependencyTypeFor, describeSchedule, describeTask, editOperation, exportProject, flattenTasks, formatDuration, ganttTable, importProject, indentOperation, insertTaskOperation, linkOperation, outdentOperations, parseDuration, predecessorOperations, progressAt, rescheduleDates, rescheduleOperation, shiftTask, tableToCsv, unitsForDelta };
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import './chunk-FXPGR372.js';
2
- import { buildTaskTree, scheduleProject, wouldCreateCycle, descendantsOf } from './chunk-FVSW2GH6.js';
3
- export { ancestorsOf, buildOrderingGraph, buildTaskTree, descendantsOf, edgeKey, findCycle, isAncestor, leavesOf, scheduleProject, taskDaySpan, topoSort, wouldCreateCycle } from './chunk-FVSW2GH6.js';
4
- export { CONTINUOUS_CALENDAR, STANDARD_CALENDAR, STANDARD_CALENDAR_ID, compileCalendar, compileCalendars, nonWorkingSpans } from './chunk-7OPVG2DP.js';
2
+ import { buildTaskTree, descendantsOf, scheduleProject, wouldCreateCycle } from './chunk-742OJYSO.js';
3
+ export { ancestorsOf, buildOrderingGraph, buildTaskTree, descendantsOf, edgeKey, findCycle, isAncestor, leavesOf, scheduleProject, taskDaySpan, topoSort, wouldCreateCycle } from './chunk-742OJYSO.js';
4
+ export { CONTINUOUS_CALENDAR, STANDARD_CALENDAR, STANDARD_CALENDAR_ID, compileCalendar, compileCalendars, nonWorkingSpans } from './chunk-3GIZDFDV.js';
5
5
  import { addUnits } from './chunk-MDRXY4HU.js';
6
6
  export { ZOOM_LEVELS, ZOOM_PRESETS, addUnits, barGeometry, createTimeScale, floorUnit, hitTestBar, pathFromPoints, routeDependency, scaleTicks, snapToUnit, timelineRange, virtualWindow } from './chunk-MDRXY4HU.js';
7
- import { DAY, HOUR, formatTime, parseTime, isDateOnly, createTimeFormatter } from './chunk-LPH2V7XM.js';
7
+ import { DAY, HOUR, formatTime, startOfDay, parseTime, isDateOnly, createTimeFormatter } from './chunk-LPH2V7XM.js';
8
8
  export { DAY, HOUR, MINUTE, createTimeFormatter, dayIndex, formatTime, isDateOnly, parseTime, startOfDay, toLocalDate, tryParseTime, weekday } from './chunk-LPH2V7XM.js';
9
9
 
10
10
  // src/model/tree.ts
@@ -462,6 +462,109 @@ function linkOperation(from, to, type = "FS", extra = {}) {
462
462
  dependency: { id: extra.id ?? createId("dep"), from, to, type, ...extra }
463
463
  };
464
464
  }
465
+ function rescheduleDates(ctx, edit) {
466
+ const { task, calendar: cal, minutesPerDay } = ctx;
467
+ const byDay = ctx.snap !== "hour";
468
+ const start = edit.start === void 0 ? task.start : cal.nextWorking(byDay ? startOfDay(edit.start) : edit.start);
469
+ let finish;
470
+ if (edit.duration !== void 0) {
471
+ finish = cal.add(start, Math.max(0, edit.duration) * minutesPerDay);
472
+ } else if (edit.finish !== void 0) {
473
+ finish = cal.prevWorking(byDay ? startOfDay(edit.finish) + DAY : edit.finish);
474
+ } else {
475
+ finish = task.type === "milestone" ? start : cal.add(start, task.duration);
476
+ }
477
+ finish = Math.max(start, finish);
478
+ return { start, finish, changed: start !== task.start || finish !== task.finish };
479
+ }
480
+ function rescheduleOperation(ctx, edit) {
481
+ const { task, calendar: cal } = ctx;
482
+ const byDay = ctx.snap !== "hour";
483
+ const wasMilestone = task.type === "milestone";
484
+ const preview = rescheduleDates(ctx, edit);
485
+ const { start, finish } = preview;
486
+ if (!preview.changed) return null;
487
+ const moved = start !== task.start;
488
+ const length = cal.between(start, finish);
489
+ const resized = length !== task.duration;
490
+ const c = {
491
+ ...ctx,
492
+ snap: byDay ? "day" : "hour",
493
+ task: wasMilestone && length > 0 ? { ...task, type: "task" } : task
494
+ };
495
+ const patch = {};
496
+ if (moved) Object.assign(patch, patchOf(editOperation(c, "move", preview)));
497
+ if (resized) {
498
+ if (length === 0) patch.duration = 0;
499
+ else Object.assign(patch, patchOf(editOperation(c, "resize-end", preview)));
500
+ }
501
+ return Object.keys(patch).length ? { type: "update-task", id: ctx.source.id, patch } : null;
502
+ }
503
+ var patchOf = (op) => op?.type === "update-task" ? op.patch : {};
504
+ function predecessorOperations(taskId, dependencies, drafts) {
505
+ const existing = dependencies.filter((d) => d.to === taskId);
506
+ const kept = new Set(drafts.map((d) => d.id).filter(Boolean));
507
+ const ops = existing.filter((d) => !kept.has(d.id)).map((d) => ({ type: "remove-dependency", id: d.id }));
508
+ const adds = [];
509
+ for (const draft of drafts) {
510
+ const type = draft.type ?? "FS";
511
+ const lag = draft.lag ?? 0;
512
+ const prev = draft.id ? existing.find((d) => d.id === draft.id) : void 0;
513
+ if (!prev) {
514
+ adds.push(linkOperation(draft.from, taskId, type, lag ? { lag } : {}));
515
+ continue;
516
+ }
517
+ const patch = {};
518
+ if (prev.from !== draft.from) patch.from = draft.from;
519
+ if ((prev.type ?? "FS") !== type) patch.type = type;
520
+ if ((prev.lag ?? 0) !== lag) patch.lag = lag;
521
+ if (Object.keys(patch).length) ops.push({ type: "update-dependency", id: prev.id, patch });
522
+ }
523
+ return [...ops, ...adds];
524
+ }
525
+ function parseDuration(text, minutesPerDay = 480) {
526
+ const m = /^\s*(\d+(?:[.,]\d+)?)\s*([a-z]*)\s*$/i.exec(text);
527
+ if (!m) return void 0;
528
+ const n = Number(m[1].replace(",", "."));
529
+ const unit = m[2].toLowerCase();
530
+ if (!unit || /^d(ays?)?$/.test(unit)) return n;
531
+ if (/^h((ou)?rs?)?$/.test(unit)) return round(n * 60 / minutesPerDay);
532
+ if (/^w((ee)?ks?)?$/.test(unit)) return n * 5;
533
+ return void 0;
534
+ }
535
+ function indentOperation(tasks, id) {
536
+ const tree = buildTaskTree(tasks);
537
+ const siblings = tree.children.get(tree.parent.get(id) ?? null) ?? [];
538
+ const i = siblings.indexOf(id);
539
+ if (i <= 0) return null;
540
+ return { type: "update-task", id, patch: { parentId: siblings[i - 1] } };
541
+ }
542
+ function outdentOperations(tasks, id) {
543
+ const tree = buildTaskTree(tasks);
544
+ const parent = tree.parent.get(id) ?? null;
545
+ if (parent === null) return [];
546
+ const grand = tree.parent.get(parent) ?? null;
547
+ const siblings = tree.children.get(parent) ?? [];
548
+ const below = siblings.slice(siblings.indexOf(id) + 1);
549
+ return [
550
+ { type: "update-task", id, patch: { parentId: grand ?? void 0 } },
551
+ ...below.map(
552
+ (s) => ({ type: "update-task", id: s, patch: { parentId: id } })
553
+ )
554
+ ];
555
+ }
556
+ function insertTaskOperation(tasks, afterId, task) {
557
+ const after = afterId ? tasks.find((t) => t.id === afterId) : void 0;
558
+ const full = { ...task, id: task.id ?? createId("task") };
559
+ if (!after) return { type: "add-task", task: full };
560
+ if (after.parentId) full.parentId = after.parentId;
561
+ const subtree = /* @__PURE__ */ new Set([after.id, ...descendantsOf(buildTaskTree(tasks), after.id)]);
562
+ let last = -1;
563
+ tasks.forEach((t, i) => {
564
+ if (subtree.has(t.id)) last = i;
565
+ });
566
+ return { type: "add-task", task: full, index: last + 1 };
567
+ }
465
568
 
466
569
  // src/a11y/index.ts
467
570
  var DEP_WORDS = {
@@ -637,4 +740,4 @@ function tableToCsv(table) {
637
740
  return lines.join("\r\n");
638
741
  }
639
742
 
640
- export { GanttEditError, createDragSession, createGanttModel, createId, dependencyTypeFor, describeSchedule, describeTask, editOperation, exportProject, flattenTasks, formatDuration, ganttTable, importProject, linkOperation, progressAt, shiftTask, tableToCsv, unitsForDelta };
743
+ export { GanttEditError, createDragSession, createGanttModel, createId, dependencyTypeFor, describeSchedule, describeTask, editOperation, exportProject, flattenTasks, formatDuration, ganttTable, importProject, indentOperation, insertTaskOperation, linkOperation, outdentOperations, parseDuration, predecessorOperations, progressAt, rescheduleDates, rescheduleOperation, shiftTask, tableToCsv, unitsForDelta };
@@ -95,8 +95,9 @@ function compileCalendar(def) {
95
95
  const nextWorking = (t) => {
96
96
  let day = dayIndex(t);
97
97
  for (let i = 0; i < MAX_SCAN_DAYS; i++, day++) {
98
- for (const [s, e] of intervalsOn(day)) {
99
- if (t < e) return Math.max(s, t);
98
+ const base = day * DAY;
99
+ for (const [s, e] of rawIntervals(day)) {
100
+ if (t < base + e) return Math.max(base + s, t);
100
101
  }
101
102
  }
102
103
  throw new RangeError(
@@ -106,62 +107,107 @@ function compileCalendar(def) {
106
107
  const prevWorking = (t) => {
107
108
  let day = dayIndex(t - 1);
108
109
  for (let i = 0; i < MAX_SCAN_DAYS; i++, day--) {
109
- const list = intervalsOn(day);
110
+ const base = day * DAY;
111
+ const list = rawIntervals(day);
110
112
  for (let j = list.length - 1; j >= 0; j--) {
111
113
  const [s, e] = list[j];
112
- if (t > s) return Math.min(e, t);
114
+ if (t > base + s) return Math.min(base + e, t);
113
115
  }
114
116
  }
115
117
  throw new RangeError(
116
118
  `Calendar "${def.id}": no working time before ${new Date(t).toISOString()}`
117
119
  );
118
120
  };
121
+ let origin = 0;
122
+ let cum = [];
123
+ const dayWork = (day) => {
124
+ let ms = 0;
125
+ for (const [s, e] of rawIntervals(day)) ms += e - s;
126
+ return ms;
127
+ };
128
+ const ensure = (day) => {
129
+ if (cum.length === 0) {
130
+ origin = day - 366;
131
+ cum = [0];
132
+ }
133
+ if (day < origin) {
134
+ const from = Math.max(day - 366, origin - MAX_SCAN_DAYS);
135
+ if (day < from) throw new RangeError(`Calendar "${def.id}": date out of range`);
136
+ const head = [0];
137
+ for (let d = from; d < origin; d++) head.push(head[head.length - 1] + dayWork(d));
138
+ const shift = head[head.length - 1];
139
+ cum = head.concat(cum.slice(1).map((v) => v + shift));
140
+ origin = from;
141
+ }
142
+ const need = day - origin + 1;
143
+ if (need >= cum.length) {
144
+ if (need - cum.length > MAX_SCAN_DAYS)
145
+ throw new RangeError(`Calendar "${def.id}": date out of range`);
146
+ const target = need + 366;
147
+ for (let i = cum.length; i <= target; i++) cum.push(cum[i - 1] + dayWork(origin + i - 1));
148
+ }
149
+ };
150
+ const workAt = (t) => {
151
+ const day = dayIndex(t);
152
+ ensure(day);
153
+ const base = day * DAY;
154
+ let ms = cum[day - origin];
155
+ for (const [s, e] of rawIntervals(day)) {
156
+ if (t <= base + s) break;
157
+ ms += Math.min(t, base + e) - (base + s);
158
+ }
159
+ return ms;
160
+ };
119
161
  const add = (t, minutes) => {
120
162
  if (minutes === 0) return t;
121
- let remaining = Math.abs(minutes) * MINUTE;
122
- if (minutes > 0) {
123
- let cursor = nextWorking(t);
124
- let day = dayIndex(cursor);
125
- for (let i = 0; i < MAX_SCAN_DAYS; i++, day++) {
126
- for (const [s, e] of intervalsOn(day)) {
127
- if (e <= cursor) continue;
128
- const from = Math.max(s, cursor);
129
- const available = e - from;
130
- if (remaining <= available) return from + remaining;
131
- remaining -= available;
132
- }
133
- cursor = (day + 1) * DAY;
163
+ const forward = minutes > 0;
164
+ let target = 0;
165
+ for (let i = 0; ; i++) {
166
+ if (i > MAX_SCAN_DAYS / 366)
167
+ throw new RangeError(`Calendar "${def.id}": working-time arithmetic ran out of range`);
168
+ target = workAt(t) + minutes * MINUTE;
169
+ if (forward ? cum[cum.length - 1] >= target : cum[0] <= target) break;
170
+ ensure(forward ? origin + cum.length + 366 : origin - 1);
171
+ }
172
+ if (forward) {
173
+ let lo = 0;
174
+ let hi = cum.length - 1;
175
+ while (lo < hi) {
176
+ const mid = lo + hi >> 1;
177
+ if (cum[mid + 1] >= target) hi = mid;
178
+ else lo = mid + 1;
179
+ }
180
+ const day = origin + lo;
181
+ const base = day * DAY;
182
+ let r = target - cum[lo];
183
+ for (const [s, e] of rawIntervals(day)) {
184
+ if (r <= e - s) return base + s + r;
185
+ r -= e - s;
134
186
  }
135
187
  } else {
136
- let cursor = prevWorking(t);
137
- let day = dayIndex(cursor - 1);
138
- for (let i = 0; i < MAX_SCAN_DAYS; i++, day--) {
139
- const list = intervalsOn(day);
140
- for (let j = list.length - 1; j >= 0; j--) {
141
- const [s, e] = list[j];
142
- if (s >= cursor) continue;
143
- const to = Math.min(e, cursor);
144
- const available = to - s;
145
- if (remaining <= available) return to - remaining;
146
- remaining -= available;
147
- }
148
- cursor = day * DAY;
188
+ let lo = 0;
189
+ let hi = cum.length - 2;
190
+ while (lo < hi) {
191
+ const mid = lo + hi + 1 >> 1;
192
+ if (cum[mid] <= target) lo = mid;
193
+ else hi = mid - 1;
194
+ }
195
+ const day = origin + lo;
196
+ const base = day * DAY;
197
+ let r = target - cum[lo];
198
+ const list = rawIntervals(day);
199
+ for (let j = 0; j < list.length; j++) {
200
+ const [s, e] = list[j];
201
+ if (r < e - s || j === list.length - 1) return base + s + r;
202
+ r -= e - s;
149
203
  }
150
204
  }
151
205
  throw new RangeError(`Calendar "${def.id}": working-time arithmetic ran out of range`);
152
206
  };
153
207
  const between = (a, b) => {
154
- if (b < a) return -between(b, a);
155
- let total = 0;
156
- const last = dayIndex(b);
157
- for (let day = dayIndex(a); day <= last; day++) {
158
- for (const [s, e] of intervalsOn(day)) {
159
- const from = Math.max(s, a);
160
- const to = Math.min(e, b);
161
- if (to > from) total += to - from;
162
- }
163
- }
164
- return total / MINUTE;
208
+ ensure(dayIndex(a));
209
+ ensure(dayIndex(b));
210
+ return (workAt(b) - workAt(a)) / MINUTE || 0;
165
211
  };
166
212
  const minutesPerDay = weekHours.reduce((sum, [s, e]) => sum + (e - s), 0) / MINUTE || 480;
167
213
  return {
@@ -1,3 +1,3 @@
1
- export { ancestorsOf, buildOrderingGraph, buildTaskTree, descendantsOf, edgeKey, findCycle, isAncestor, leavesOf, scheduleProject, taskDaySpan, topoSort, wouldCreateCycle } from '../chunk-FVSW2GH6.js';
2
- import '../chunk-7OPVG2DP.js';
1
+ export { ancestorsOf, buildOrderingGraph, buildTaskTree, descendantsOf, edgeKey, findCycle, isAncestor, leavesOf, scheduleProject, taskDaySpan, topoSort, wouldCreateCycle } from '../chunk-742OJYSO.js';
2
+ import '../chunk-3GIZDFDV.js';
3
3
  import '../chunk-LPH2V7XM.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/gantt-core",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Framework-agnostic Gantt scheduling engine, calendars, layout and interaction math for Classic Theme",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",