@classic-homes/gantt-core 0.3.2 → 0.3.4

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.
@@ -4,7 +4,7 @@
4
4
  var MINUTE = 6e4;
5
5
  var HOUR = 60 * MINUTE;
6
6
  var DAY = 24 * HOUR;
7
- var ISO_RE = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?/;
7
+ var ISO_RE = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3}))?)?(?:Z|[+-](?:[01]\d|2[0-3]):?[0-5]\d)?)?$/;
8
8
  function isDateOnly(input) {
9
9
  return typeof input === "string" && /^\d{4}-\d{2}-\d{2}$/.test(input.trim());
10
10
  }
@@ -15,21 +15,25 @@ function parseTime(input) {
15
15
  }
16
16
  if (input instanceof Date) {
17
17
  if (Number.isNaN(input.getTime())) throw new RangeError("Invalid Date");
18
- return Date.UTC(
19
- input.getFullYear(),
20
- input.getMonth(),
21
- input.getDate(),
18
+ const date2 = /* @__PURE__ */ new Date(0);
19
+ date2.setUTCFullYear(input.getFullYear(), input.getMonth(), input.getDate());
20
+ date2.setUTCHours(
22
21
  input.getHours(),
23
22
  input.getMinutes(),
24
- input.getSeconds()
23
+ input.getSeconds(),
24
+ input.getMilliseconds()
25
25
  );
26
+ return date2.getTime();
26
27
  }
27
28
  const m = ISO_RE.exec(input.trim());
28
29
  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;
30
+ const [, y, mo, d, h = "0", mi = "0", s = "0", fraction = "0"] = m;
31
+ const date = /* @__PURE__ */ new Date(0);
32
+ date.setUTCFullYear(+y, +mo - 1, +d);
33
+ date.setUTCHours(+h, +mi, +s, +fraction.padEnd(3, "0"));
34
+ if (date.getUTCFullYear() !== +y || date.getUTCMonth() !== +mo - 1 || date.getUTCDate() !== +d || date.getUTCHours() !== +h || date.getUTCMinutes() !== +mi || date.getUTCSeconds() !== +s)
35
+ throw new RangeError(`Invalid time: "${input}"`);
36
+ return date.getTime();
33
37
  }
34
38
  function startOfDay(t) {
35
39
  return Math.floor(t / DAY) * DAY;
@@ -245,18 +249,21 @@ function buildTaskTree(tasks) {
245
249
  if (p === t.id) invalidParents.push({ id: t.id, reason: "cycle" });
246
250
  }
247
251
  }
252
+ const done = /* @__PURE__ */ new Set();
248
253
  for (const t of tasks) {
249
- const seen = /* @__PURE__ */ new Set([t.id]);
250
- let cur = parent.get(t.id) ?? null;
251
- while (cur !== null) {
252
- if (seen.has(cur)) {
253
- parent.set(t.id, null);
254
- invalidParents.push({ id: t.id, reason: "cycle" });
254
+ if (done.has(t.id)) continue;
255
+ const path = /* @__PURE__ */ new Set();
256
+ let cur = t.id;
257
+ while (cur !== null && !done.has(cur)) {
258
+ if (path.has(cur)) {
259
+ parent.set(cur, null);
260
+ invalidParents.push({ id: cur, reason: "cycle" });
255
261
  break;
256
262
  }
257
- seen.add(cur);
263
+ path.add(cur);
258
264
  cur = parent.get(cur) ?? null;
259
265
  }
266
+ for (const id of path) done.add(id);
260
267
  }
261
268
  const children = /* @__PURE__ */ new Map();
262
269
  for (const t of tasks) {
@@ -317,26 +324,27 @@ function findCycle(nodes, successors) {
317
324
  const allowed = new Set(nodes);
318
325
  const state = /* @__PURE__ */ new Map();
319
326
  const path = [];
320
- const visit = (n) => {
321
- state.set(n, 1);
322
- path.push(n);
323
- for (const s of successors.get(n) ?? []) {
324
- if (!allowed.has(s)) continue;
325
- const st = state.get(s);
326
- if (st === 1) return path.slice(path.indexOf(s));
327
- if (st === void 0) {
328
- const found = visit(s);
329
- if (found) return found;
330
- }
331
- }
332
- path.pop();
333
- state.set(n, 2);
334
- return null;
335
- };
336
327
  for (const n of nodes) {
337
- if (!state.has(n)) {
338
- const found = visit(n);
339
- if (found) return found;
328
+ if (state.has(n)) continue;
329
+ const stack = [];
330
+ const enter = (id) => {
331
+ state.set(id, 1);
332
+ path.push(id);
333
+ stack.push({ id, next: (successors.get(id) ?? /* @__PURE__ */ new Set()).values() });
334
+ };
335
+ enter(n);
336
+ while (stack.length) {
337
+ const frame = stack[stack.length - 1];
338
+ const next = frame.next.next();
339
+ if (next.done) {
340
+ stack.pop();
341
+ path.pop();
342
+ state.set(frame.id, 2);
343
+ } else if (allowed.has(next.value)) {
344
+ const st = state.get(next.value);
345
+ if (st === 1) return path.slice(path.indexOf(next.value));
346
+ if (st === void 0) enter(next.value);
347
+ }
340
348
  }
341
349
  }
342
350
  return null;
@@ -396,6 +404,30 @@ function parseFinish(cal, end) {
396
404
  const t = parseTime(end);
397
405
  return cal.prevWorking(isDateOnly(end) ? t + DAY : t);
398
406
  }
407
+ function latestLagSource(cal, bound, lag) {
408
+ if (lag === 0) return bound;
409
+ const candidate = cal.add(bound, -lag);
410
+ let lo = candidate;
411
+ let hi = candidate;
412
+ let span = DAY;
413
+ if (cal.add(candidate, lag) <= bound) {
414
+ do {
415
+ hi = candidate + span;
416
+ span *= 2;
417
+ } while (cal.add(hi, lag) <= bound);
418
+ } else {
419
+ do {
420
+ lo = candidate - span;
421
+ span *= 2;
422
+ } while (cal.add(lo, lag) > bound);
423
+ }
424
+ while (hi - lo > 1) {
425
+ const mid = lo + Math.floor((hi - lo) / 2);
426
+ if (cal.add(mid, lag) <= bound) lo = mid;
427
+ else hi = mid;
428
+ }
429
+ return lo;
430
+ }
399
431
  function scheduleProject(tasks, dependencies = [], calendars = [], options = {}) {
400
432
  const conflicts = [];
401
433
  const cals = compileCalendars(calendars);
@@ -666,16 +698,16 @@ function scheduleProject(tasks, dependencies = [], calendars = [], options = {})
666
698
  const t = nodes.get(tid);
667
699
  switch (e.type) {
668
700
  case "FS":
669
- lf = min(lf, t.cal.add(t.ls, -e.lag));
701
+ lf = min(lf, latestLagSource(t.cal, t.ls, e.lag));
670
702
  break;
671
703
  case "SS":
672
- ls = min(ls, t.cal.add(t.ls, -e.lag));
704
+ ls = min(ls, latestLagSource(t.cal, t.ls, e.lag));
673
705
  break;
674
706
  case "FF":
675
- lf = min(lf, t.cal.add(t.lf, -e.lag));
707
+ lf = min(lf, latestLagSource(t.cal, t.lf, e.lag));
676
708
  break;
677
709
  case "SF":
678
- ls = min(ls, t.cal.add(t.lf, -e.lag));
710
+ ls = min(ls, latestLagSource(t.cal, t.lf, e.lag));
679
711
  break;
680
712
  }
681
713
  }
@@ -706,7 +738,10 @@ function scheduleProject(tasks, dependencies = [], calendars = [], options = {})
706
738
  if (n.deadline !== void 0) lf = min(lf, n.deadline);
707
739
  let finish = n.duration === 0 ? lf : n.cal.prevWorking(lf);
708
740
  if (ls !== INF)
709
- finish = min(finish, n.duration === 0 ? ls : n.cal.add(n.cal.prevWorking(ls), n.duration));
741
+ finish = min(
742
+ finish,
743
+ n.duration === 0 ? ls : n.cal.add(n.cal.isWorking(ls) ? ls : n.cal.prevWorking(ls) - 1, n.duration)
744
+ );
710
745
  n.lf = finish;
711
746
  n.ls = startFromFinish(n, finish);
712
747
  }
@@ -755,16 +790,16 @@ function scheduleProject(tasks, dependencies = [], calendars = [], options = {})
755
790
  let allowed;
756
791
  switch (e.type) {
757
792
  case "FS":
758
- allowed = n.cal.between(n.ef, n.cal.prevWorking(t.cal.add(t.es, -e.lag)));
793
+ allowed = n.cal.between(n.ef, n.cal.prevWorking(latestLagSource(t.cal, t.es, e.lag)));
759
794
  break;
760
795
  case "SS":
761
- allowed = n.cal.between(n.es, t.cal.add(t.es, -e.lag));
796
+ allowed = n.cal.between(n.es, latestLagSource(t.cal, t.es, e.lag));
762
797
  break;
763
798
  case "FF":
764
- allowed = n.cal.between(n.ef, t.cal.add(t.ef, -e.lag));
799
+ allowed = n.cal.between(n.ef, latestLagSource(t.cal, t.ef, e.lag));
765
800
  break;
766
801
  case "SF":
767
- allowed = n.cal.between(n.es, t.cal.add(t.ef, -e.lag));
802
+ allowed = n.cal.between(n.es, latestLagSource(t.cal, t.ef, e.lag));
768
803
  break;
769
804
  }
770
805
  freeSlack = min(freeSlack, allowed);
@@ -777,23 +812,34 @@ function scheduleProject(tasks, dependencies = [], calendars = [], options = {})
777
812
  if (isCritical) critical.push(id);
778
813
  result.set(id, toScheduled(n, totalSlack, freeSlack, isCritical));
779
814
  }
815
+ const aggregates = /* @__PURE__ */ new Map();
780
816
  for (const id of order) {
781
817
  const n = nodes.get(id);
782
- if (!n.isSummary) continue;
783
- const leafIds = leaves(id);
784
- let weighted = 0;
785
- let weight = 0;
786
- let slack = INF;
787
- let anyCritical = false;
788
- for (const l of leafIds) {
789
- const st = result.get(l);
790
- weighted += st.progress * st.duration;
791
- weight += st.duration;
792
- slack = min(slack, st.totalSlack);
793
- anyCritical || (anyCritical = st.critical);
794
- }
795
- n.progress = weight > 0 ? weighted / weight : leafIds.reduce((s, l) => s + result.get(l).progress, 0) / Math.max(1, leafIds.length);
796
- result.set(id, toScheduled(n, slack === INF ? 0 : slack, 0, anyCritical));
818
+ if (!n.isSummary) {
819
+ const st = result.get(id);
820
+ aggregates.set(id, {
821
+ weighted: st.progress * st.duration,
822
+ weight: st.duration,
823
+ progress: st.progress,
824
+ count: 1,
825
+ slack: st.totalSlack,
826
+ critical: st.critical
827
+ });
828
+ continue;
829
+ }
830
+ const sum = { weighted: 0, weight: 0, progress: 0, count: 0, slack: INF, critical: false };
831
+ for (const child of tree.children.get(id) ?? []) {
832
+ const a = aggregates.get(child);
833
+ sum.weighted += a.weighted;
834
+ sum.weight += a.weight;
835
+ sum.progress += a.progress;
836
+ sum.count += a.count;
837
+ sum.slack = min(sum.slack, a.slack);
838
+ sum.critical || (sum.critical = a.critical);
839
+ }
840
+ aggregates.set(id, sum);
841
+ n.progress = sum.weight > 0 ? sum.weighted / sum.weight : sum.progress / Math.max(1, sum.count);
842
+ result.set(id, toScheduled(n, sum.slack === INF ? 0 : sum.slack, 0, sum.critical));
797
843
  }
798
844
  function toScheduled(n, totalSlack, freeSlack, isCritical) {
799
845
  const baseline = n.task.baseline;
@@ -1,3 +1,3 @@
1
- export { ancestorsOf, buildOrderingGraph, buildTaskTree, descendantsOf, edgeKey, findCycle, isAncestor, leavesOf, scheduleProject, taskDaySpan, topoSort, wouldCreateCycle } from '../chunk-742OJYSO.js';
2
- import '../chunk-3GIZDFDV.js';
3
- import '../chunk-LPH2V7XM.js';
1
+ export { ancestorsOf, buildOrderingGraph, buildTaskTree, descendantsOf, edgeKey, findCycle, isAncestor, leavesOf, scheduleProject, taskDaySpan, topoSort, wouldCreateCycle } from '../chunk-INRQCGC4.js';
2
+ import '../chunk-LQGY5EOM.js';
3
+ import '../chunk-FV63P7UY.js';
@@ -66,7 +66,7 @@ interface GanttTask {
66
66
  baseline?: GanttBaseline;
67
67
  /** Any CSS colour for the bar */
68
68
  color?: string;
69
- /** Blocks every edit of this task in the UI */
69
+ /** Blocks edits to this task and its descendants, including touching dependencies. */
70
70
  readonly?: boolean;
71
71
  meta?: Record<string, unknown>;
72
72
  }
@@ -202,7 +202,7 @@ interface ScheduledDateChange {
202
202
  interface GanttChange {
203
203
  source: 'edit' | 'undo' | 'redo';
204
204
  label?: string;
205
- /** Operations applied to the input data, in order */
205
+ /** Edit descriptions in order; persist the snapshots below for exact undo restoration. */
206
206
  operations: GanttOperation[];
207
207
  /** Tasks whose scheduled dates moved, including cascaded successors */
208
208
  dates: ScheduledDateChange[];
@@ -66,7 +66,7 @@ interface GanttTask {
66
66
  baseline?: GanttBaseline;
67
67
  /** Any CSS colour for the bar */
68
68
  color?: string;
69
- /** Blocks every edit of this task in the UI */
69
+ /** Blocks edits to this task and its descendants, including touching dependencies. */
70
70
  readonly?: boolean;
71
71
  meta?: Record<string, unknown>;
72
72
  }
@@ -202,7 +202,7 @@ interface ScheduledDateChange {
202
202
  interface GanttChange {
203
203
  source: 'edit' | 'undo' | 'redo';
204
204
  label?: string;
205
- /** Operations applied to the input data, in order */
205
+ /** Edit descriptions in order; persist the snapshots below for exact undo restoration. */
206
206
  operations: GanttOperation[];
207
207
  /** Tasks whose scheduled dates moved, including cascaded successors */
208
208
  dates: ScheduledDateChange[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/gantt-core",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
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",