@quartzio/gantt 0.0.0-stage → 0.0.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/API.md +533 -0
- package/LICENSE +21 -0
- package/README.md +83 -2
- package/dist/index.d.ts +1341 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6556 -0
- package/dist/index.js.map +1 -0
- package/dist/styles.css +1299 -0
- package/package.json +51 -3
- package/src/calendar/duration.ts +42 -0
- package/src/calendar/project.ts +29 -0
- package/src/calendar/workingCalendar.ts +218 -0
- package/src/data/draft.ts +78 -0
- package/src/data/graph.ts +215 -0
- package/src/data/normalize.ts +661 -0
- package/src/data/operations.ts +112 -0
- package/src/data/project.ts +194 -0
- package/src/data/serialize.ts +26 -0
- package/src/data/transaction.ts +460 -0
- package/src/data/tree.ts +156 -0
- package/src/data/types.ts +229 -0
- package/src/index.ts +139 -0
- package/src/scheduling/schedule.ts +310 -0
- package/src/styles.css +1299 -0
- package/src/timeaxis/align.ts +120 -0
- package/src/timeaxis/format.ts +72 -0
- package/src/timeaxis/presets.ts +219 -0
- package/src/timeaxis/timeAxis.ts +205 -0
- package/src/util/civil.ts +38 -0
- package/src/util/emitter.ts +53 -0
- package/src/util/equal.ts +23 -0
- package/src/util/errors.ts +4 -0
- package/src/util/parse.ts +70 -0
- package/src/util/time.ts +18 -0
- package/src/util/zone.ts +303 -0
- package/src/view/bars.ts +63 -0
- package/src/view/binding.ts +162 -0
- package/src/view/cellEditor.ts +239 -0
- package/src/view/cells.ts +89 -0
- package/src/view/columns.ts +197 -0
- package/src/view/createGantt.ts +1164 -0
- package/src/view/dates.ts +14 -0
- package/src/view/dependencies.ts +147 -0
- package/src/view/editing.ts +303 -0
- package/src/view/history.ts +72 -0
- package/src/view/interaction.ts +671 -0
- package/src/view/keyboard.ts +149 -0
- package/src/view/listWidth.ts +201 -0
- package/src/view/menu.ts +265 -0
- package/src/view/nonWorking.ts +113 -0
- package/src/view/range.ts +41 -0
- package/src/view/rows.ts +235 -0
- package/src/view/selection.ts +156 -0
- package/src/view/taskActions.ts +71 -0
- package/src/view/taskEditor.ts +553 -0
- package/src/view/timeline.ts +155 -0
- package/src/view/tooltip.ts +117 -0
- package/src/view/types.ts +328 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Task } from '../data/types';
|
|
2
|
+
|
|
3
|
+
/** A task's dates as shown (scheduling keeps them in the task itself). */
|
|
4
|
+
export interface TaskDates {
|
|
5
|
+
readonly start: number;
|
|
6
|
+
readonly end: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Start and end; zero-length when only one is set (unscheduled data); `null` when neither is. */
|
|
10
|
+
export function taskDates(task: Task): TaskDates | null {
|
|
11
|
+
const start = task.startDate ?? task.endDate;
|
|
12
|
+
const end = task.endDate ?? task.startDate;
|
|
13
|
+
return start === null || end === null ? null : { start, end: Math.max(start, end) };
|
|
14
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { getTreeIndex } from '../data/tree';
|
|
2
|
+
import type { Dependency, DependencyType, Id, ProjectState, Task } from '../data/types';
|
|
3
|
+
import type { TimeAxis } from '../timeaxis/timeAxis';
|
|
4
|
+
import { computeBar, type Bar } from './bars';
|
|
5
|
+
import { taskDates } from './dates';
|
|
6
|
+
import { rowKey, type RowsState } from './rows';
|
|
7
|
+
|
|
8
|
+
/** A dependency drawn as an arrow between two bars, in timeline coordinates. */
|
|
9
|
+
export interface DependencyLine {
|
|
10
|
+
/** Unique string key for rendering lists (ids 1 and "1" are different dependencies). */
|
|
11
|
+
readonly key: string;
|
|
12
|
+
readonly id: Id;
|
|
13
|
+
readonly from: Id;
|
|
14
|
+
readonly to: Id;
|
|
15
|
+
readonly type: DependencyType;
|
|
16
|
+
/** SVG path data, orthogonal, ending at the successor (put the arrowhead there with `marker-end`). */
|
|
17
|
+
readonly path: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Horizontal distance a line runs straight out of a bar, and straight into one. */
|
|
21
|
+
const STUB = 8;
|
|
22
|
+
/** Half the width of a milestone diamond, so lines end at its tips (matches the default `--qz-milestone-size`). */
|
|
23
|
+
const MILESTONE_RADIUS = 7;
|
|
24
|
+
|
|
25
|
+
const EMPTY: readonly DependencyLine[] = [];
|
|
26
|
+
|
|
27
|
+
export interface DependencyLinesInput {
|
|
28
|
+
readonly project: ProjectState;
|
|
29
|
+
readonly timeAxis: TimeAxis;
|
|
30
|
+
readonly rows: RowsState;
|
|
31
|
+
/** Row index of every visible task (collapsed-away tasks are missing). */
|
|
32
|
+
readonly rowIndex: ReadonlyMap<Id, number>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Lines for the dependencies near the rendered rows: those with an end in them, or passing through them.
|
|
37
|
+
* Dependencies to unscheduled or collapsed-away tasks aren't drawn. The array is reused while nothing it
|
|
38
|
+
* depends on changes (e.g. while scrolling within the rendered rows).
|
|
39
|
+
*/
|
|
40
|
+
export function createDependencyView(): {
|
|
41
|
+
linesFor: (input: DependencyLinesInput) => readonly DependencyLine[];
|
|
42
|
+
} {
|
|
43
|
+
let cache: { key: unknown[]; lines: readonly DependencyLine[] } | undefined;
|
|
44
|
+
// Lines by key from the last call: unchanged ones keep their identity, so renderers can skip them.
|
|
45
|
+
let previous = new Map<string, DependencyLine>();
|
|
46
|
+
|
|
47
|
+
const linesFor = ({
|
|
48
|
+
project,
|
|
49
|
+
timeAxis,
|
|
50
|
+
rows,
|
|
51
|
+
rowIndex,
|
|
52
|
+
}: DependencyLinesInput): readonly DependencyLine[] => {
|
|
53
|
+
const key = [project.dependencies, project.tasks, timeAxis, rows.items, rowIndex];
|
|
54
|
+
if (cache && key.every((part, i) => part === cache?.key[i])) return cache.lines;
|
|
55
|
+
|
|
56
|
+
const first = rows.items[0]?.index;
|
|
57
|
+
const last = rows.items.at(-1)?.index;
|
|
58
|
+
let lines: readonly DependencyLine[] = EMPTY;
|
|
59
|
+
if (first !== undefined && last !== undefined && project.dependencies.order.length > 0) {
|
|
60
|
+
const tree = getTreeIndex(project.tasks);
|
|
61
|
+
// Bars of rendered rows, and of other tasks computed once per call (a task can have many dependencies).
|
|
62
|
+
const bars = new Map<Id, Bar | null>(rows.items.map((row) => [row.id, row.bar]));
|
|
63
|
+
const barOf = (id: Id): Bar | null => {
|
|
64
|
+
let bar = bars.get(id);
|
|
65
|
+
if (bar === undefined) {
|
|
66
|
+
const task = project.tasks.byId.get(id) as Task;
|
|
67
|
+
bar = computeBar(task, taskDates(task), !tree.isLeaf(id), timeAxis);
|
|
68
|
+
bars.set(id, bar);
|
|
69
|
+
}
|
|
70
|
+
return bar;
|
|
71
|
+
};
|
|
72
|
+
const result: DependencyLine[] = [];
|
|
73
|
+
const kept = new Map<string, DependencyLine>();
|
|
74
|
+
for (const id of project.dependencies.order) {
|
|
75
|
+
const dependency = project.dependencies.byId.get(id) as Dependency;
|
|
76
|
+
const fromRow = rowIndex.get(dependency.from);
|
|
77
|
+
const toRow = rowIndex.get(dependency.to);
|
|
78
|
+
if (fromRow === undefined || toRow === undefined) continue;
|
|
79
|
+
if (Math.max(fromRow, toRow) < first || Math.min(fromRow, toRow) > last) continue;
|
|
80
|
+
const fromBar = barOf(dependency.from);
|
|
81
|
+
const toBar = barOf(dependency.to);
|
|
82
|
+
if (!fromBar || !toBar) continue;
|
|
83
|
+
const lineKey = rowKey(id);
|
|
84
|
+
const path = route(dependency.type, fromBar, fromRow, toBar, toRow, rows.rowHeight);
|
|
85
|
+
const old = previous.get(lineKey);
|
|
86
|
+
const line =
|
|
87
|
+
old?.path === path &&
|
|
88
|
+
old.from === dependency.from &&
|
|
89
|
+
old.to === dependency.to &&
|
|
90
|
+
old.type === dependency.type
|
|
91
|
+
? old
|
|
92
|
+
: { key: lineKey, id, from: dependency.from, to: dependency.to, type: dependency.type, path };
|
|
93
|
+
kept.set(lineKey, line);
|
|
94
|
+
result.push(line);
|
|
95
|
+
}
|
|
96
|
+
lines = result;
|
|
97
|
+
previous = kept;
|
|
98
|
+
}
|
|
99
|
+
cache = { key, lines };
|
|
100
|
+
return lines;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
return { linesFor };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function edge(bar: Bar, side: 'start' | 'end'): number {
|
|
107
|
+
if (bar.kind === 'milestone') return bar.x + (side === 'start' ? -MILESTONE_RADIUS : MILESTONE_RADIUS);
|
|
108
|
+
return side === 'start' ? bar.x : bar.x + bar.width;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const round = (value: number) => Math.round(value * 10) / 10;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* An orthogonal path, like Bryntum's: straight out of the predecessor's side (its end for FS/FF, its start for
|
|
115
|
+
* SS/SF), one vertical run, and straight into the successor's side (its start for FS/SS, its end for FF/SF).
|
|
116
|
+
* When no single vertical run leaves room for both straight parts, the line goes around along the boundary of
|
|
117
|
+
* the successor's row.
|
|
118
|
+
*/
|
|
119
|
+
function route(
|
|
120
|
+
type: DependencyType,
|
|
121
|
+
from: Bar,
|
|
122
|
+
fromRow: number,
|
|
123
|
+
to: Bar,
|
|
124
|
+
toRow: number,
|
|
125
|
+
rowHeight: number,
|
|
126
|
+
): string {
|
|
127
|
+
const fromEnd = type === 'FS' || type === 'FF';
|
|
128
|
+
const toStart = type === 'FS' || type === 'SS';
|
|
129
|
+
const x1 = edge(from, fromEnd ? 'end' : 'start');
|
|
130
|
+
const x2 = edge(to, toStart ? 'start' : 'end');
|
|
131
|
+
const y1 = fromRow * rowHeight + rowHeight / 2;
|
|
132
|
+
const y2 = toRow * rowHeight + rowHeight / 2;
|
|
133
|
+
const out = fromEnd ? 1 : -1; // direction out of the predecessor
|
|
134
|
+
const into = toStart ? 1 : -1; // direction into the successor
|
|
135
|
+
|
|
136
|
+
// Where the vertical run can be: at least STUB out of the predecessor and STUB before the successor.
|
|
137
|
+
const lower = Math.max(out === 1 ? x1 + STUB : -Infinity, into === 1 ? -Infinity : x2 + STUB);
|
|
138
|
+
const upper = Math.min(out === 1 ? Infinity : x1 - STUB, into === 1 ? x2 - STUB : Infinity);
|
|
139
|
+
const f = (...values: number[]) => values.map((value) => String(round(value)));
|
|
140
|
+
if (lower <= upper) {
|
|
141
|
+
const [a, b, c, d, e] = f(x1, y1, out === 1 ? lower : upper, y2, x2);
|
|
142
|
+
return `M${a} ${b} H${c} V${d} H${e}`;
|
|
143
|
+
}
|
|
144
|
+
const boundary = toRow > fromRow ? y2 - rowHeight / 2 : y2 + rowHeight / 2;
|
|
145
|
+
const [a, b, c, d, e, g, h] = f(x1, y1, x1 + out * STUB, boundary, x2 - into * STUB, y2, x2);
|
|
146
|
+
return `M${a} ${b} H${c} V${d} H${e} V${g} H${h}`;
|
|
147
|
+
}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { workingMsPerUnit } from '../calendar/duration';
|
|
2
|
+
import type { Transaction } from '../data/transaction';
|
|
3
|
+
import type { Id, ProjectSettings, Task } from '../data/types';
|
|
4
|
+
import type { TimeUnit } from '../util/time';
|
|
5
|
+
import { daysInMonth } from '../util/civil';
|
|
6
|
+
import { fromWallTime, startOfUnit, toWallTime, type TimeZone } from '../util/zone';
|
|
7
|
+
import type { BuiltInColumnId } from './columns';
|
|
8
|
+
import type { TaskDates } from './dates';
|
|
9
|
+
import type { ProposedChange } from './interaction';
|
|
10
|
+
|
|
11
|
+
/** The kind of field a cell is edited in. */
|
|
12
|
+
export type EditKind = 'text' | 'duration' | 'percent' | 'date' | 'datetime';
|
|
13
|
+
|
|
14
|
+
/** The cell being edited. */
|
|
15
|
+
export interface CellEdit {
|
|
16
|
+
readonly taskId: Id;
|
|
17
|
+
/** Key of the task's row. */
|
|
18
|
+
readonly rowKey: string;
|
|
19
|
+
readonly columnId: string;
|
|
20
|
+
readonly kind: EditKind;
|
|
21
|
+
/** What's in the field now. */
|
|
22
|
+
readonly text: string;
|
|
23
|
+
/** Why the last attempt to save was refused, until the text changes. */
|
|
24
|
+
readonly error: string | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Dates must stay within what the data accepts. */
|
|
28
|
+
const MIN_YEAR = 1000;
|
|
29
|
+
const MAX_YEAR = 9999;
|
|
30
|
+
const SUB_DAY = new Set<TimeUnit>(['millisecond', 'second', 'minute', 'hour']);
|
|
31
|
+
/**
|
|
32
|
+
* Longer durations and spans are refused: most likely a typo (a year off by decades), and the scheduler can't
|
|
33
|
+
* place tasks that long (it would keep the old dates).
|
|
34
|
+
*/
|
|
35
|
+
export const MAX_WORKING_DAYS = 5000;
|
|
36
|
+
const MAX_SPAN_YEARS = 20;
|
|
37
|
+
const MAX_SPAN = MAX_SPAN_YEARS * 365.25 * 24 * 60 * 60 * 1000;
|
|
38
|
+
|
|
39
|
+
/** Dates are edited with a time when the time axis resolves to less than a day. */
|
|
40
|
+
export const dateKind = (resolution: TimeUnit): 'date' | 'datetime' =>
|
|
41
|
+
SUB_DAY.has(resolution) ? 'datetime' : 'date';
|
|
42
|
+
|
|
43
|
+
/** The field a cell is edited in, or `null` when it can't be edited. */
|
|
44
|
+
export function editKind(
|
|
45
|
+
field: BuiltInColumnId | null,
|
|
46
|
+
task: Task,
|
|
47
|
+
isParent: boolean,
|
|
48
|
+
dates: TaskDates | null,
|
|
49
|
+
dateField: 'date' | 'datetime',
|
|
50
|
+
): EditKind | null {
|
|
51
|
+
if (field === null) return null;
|
|
52
|
+
if (field === 'name') return 'text';
|
|
53
|
+
// A parent's dates, duration and progress roll up from its children.
|
|
54
|
+
if (isParent) return null;
|
|
55
|
+
switch (field) {
|
|
56
|
+
case 'startDate':
|
|
57
|
+
return dateField;
|
|
58
|
+
case 'endDate':
|
|
59
|
+
// A milestone has one date; an unscheduled task has no start to end after.
|
|
60
|
+
return dates && dates.end > dates.start ? dateField : null;
|
|
61
|
+
case 'duration':
|
|
62
|
+
return 'duration';
|
|
63
|
+
case 'percentDone':
|
|
64
|
+
return 'percent';
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const pad = (value: number, length = 2) => String(value).padStart(length, '0');
|
|
69
|
+
|
|
70
|
+
/** A time as an `<input type="date">` or `datetime-local` value, in the project's time zone. */
|
|
71
|
+
export function inputValue(time: number, kind: 'date' | 'datetime', zone: TimeZone): string {
|
|
72
|
+
const wall = toWallTime(time, zone);
|
|
73
|
+
const date = `${pad(wall.year, 4)}-${pad(wall.month)}-${pad(wall.day)}`;
|
|
74
|
+
return kind === 'date' ? date : `${date}T${pad(wall.hour)}:${pad(wall.minute)}`;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Units as MS Project writes them; `m` is minutes, `mo` months.
|
|
78
|
+
const UNIT_ABBREVIATIONS: Record<TimeUnit, string> = {
|
|
79
|
+
millisecond: 'ms',
|
|
80
|
+
second: 's',
|
|
81
|
+
minute: 'm',
|
|
82
|
+
hour: 'h',
|
|
83
|
+
day: 'd',
|
|
84
|
+
week: 'w',
|
|
85
|
+
month: 'mo',
|
|
86
|
+
quarter: 'q',
|
|
87
|
+
year: 'y',
|
|
88
|
+
};
|
|
89
|
+
const UNIT_NAMES: Record<string, TimeUnit> = {
|
|
90
|
+
ms: 'millisecond',
|
|
91
|
+
millisecond: 'millisecond',
|
|
92
|
+
milliseconds: 'millisecond',
|
|
93
|
+
s: 'second',
|
|
94
|
+
sec: 'second',
|
|
95
|
+
secs: 'second',
|
|
96
|
+
second: 'second',
|
|
97
|
+
seconds: 'second',
|
|
98
|
+
m: 'minute',
|
|
99
|
+
min: 'minute',
|
|
100
|
+
mins: 'minute',
|
|
101
|
+
minute: 'minute',
|
|
102
|
+
minutes: 'minute',
|
|
103
|
+
h: 'hour',
|
|
104
|
+
hr: 'hour',
|
|
105
|
+
hrs: 'hour',
|
|
106
|
+
hour: 'hour',
|
|
107
|
+
hours: 'hour',
|
|
108
|
+
d: 'day',
|
|
109
|
+
day: 'day',
|
|
110
|
+
days: 'day',
|
|
111
|
+
w: 'week',
|
|
112
|
+
wk: 'week',
|
|
113
|
+
wks: 'week',
|
|
114
|
+
week: 'week',
|
|
115
|
+
weeks: 'week',
|
|
116
|
+
mo: 'month',
|
|
117
|
+
mon: 'month',
|
|
118
|
+
month: 'month',
|
|
119
|
+
months: 'month',
|
|
120
|
+
q: 'quarter',
|
|
121
|
+
quarter: 'quarter',
|
|
122
|
+
quarters: 'quarter',
|
|
123
|
+
y: 'year',
|
|
124
|
+
yr: 'year',
|
|
125
|
+
yrs: 'year',
|
|
126
|
+
year: 'year',
|
|
127
|
+
years: 'year',
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/** What the field shows when editing starts. */
|
|
131
|
+
export function initialText(
|
|
132
|
+
field: BuiltInColumnId,
|
|
133
|
+
kind: EditKind,
|
|
134
|
+
task: Task,
|
|
135
|
+
dates: TaskDates | null,
|
|
136
|
+
zone: TimeZone,
|
|
137
|
+
): string {
|
|
138
|
+
switch (field) {
|
|
139
|
+
case 'name':
|
|
140
|
+
return task.name;
|
|
141
|
+
case 'startDate':
|
|
142
|
+
return dates && kind !== 'text'
|
|
143
|
+
? inputValue(dates.start, kind === 'date' ? 'date' : 'datetime', zone)
|
|
144
|
+
: '';
|
|
145
|
+
case 'endDate': {
|
|
146
|
+
if (!dates) return '';
|
|
147
|
+
if (kind === 'datetime') return inputValue(dates.end, 'datetime', zone);
|
|
148
|
+
// As in the column: an end at midnight is the end of the day before.
|
|
149
|
+
return inputValue(atMidnight(dates, zone) ? dates.end - 1 : dates.end, 'date', zone);
|
|
150
|
+
}
|
|
151
|
+
case 'duration':
|
|
152
|
+
return task.duration === null ? '' : durationText(task.duration, task.durationUnit);
|
|
153
|
+
case 'percentDone':
|
|
154
|
+
return String(task.percentDone);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** A duration as the field shows it, e.g. `4d` or `-1.5h`. */
|
|
159
|
+
export const durationText = (value: number, unit: TimeUnit): string =>
|
|
160
|
+
`${String(Math.round(value * 100) / 100)}${UNIT_ABBREVIATIONS[unit]}`;
|
|
161
|
+
|
|
162
|
+
const atMidnight = (dates: TaskDates, zone: TimeZone) =>
|
|
163
|
+
dates.end > dates.start && startOfUnit(dates.end, 'day', zone) === dates.end;
|
|
164
|
+
|
|
165
|
+
/** What an edit changes, and the change to propose to `validateChange` once it's planned. */
|
|
166
|
+
export interface ParsedEdit {
|
|
167
|
+
readonly change: (tx: Transaction) => void;
|
|
168
|
+
/**
|
|
169
|
+
* The change for `validateChange`: the task as it is (as a drag gives it), with where the plan puts it
|
|
170
|
+
* (`dates`, after the change). `null` for nothing to validate.
|
|
171
|
+
*/
|
|
172
|
+
readonly propose: (task: Task, dates: TaskDates | null) => ProposedChange | null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Reads the field's text into a change, or says why it can't. Never throws. */
|
|
176
|
+
export function parseEdit(
|
|
177
|
+
field: BuiltInColumnId,
|
|
178
|
+
kind: EditKind,
|
|
179
|
+
text: string,
|
|
180
|
+
task: Task,
|
|
181
|
+
dates: TaskDates | null,
|
|
182
|
+
settings: ProjectSettings,
|
|
183
|
+
): ParsedEdit | { readonly error: string } {
|
|
184
|
+
const id = task.id;
|
|
185
|
+
const zone = settings.timeZone;
|
|
186
|
+
switch (field) {
|
|
187
|
+
case 'name':
|
|
188
|
+
return {
|
|
189
|
+
change: (tx) => {
|
|
190
|
+
tx.tasks.update(id, { name: text });
|
|
191
|
+
},
|
|
192
|
+
propose: () => null,
|
|
193
|
+
};
|
|
194
|
+
case 'startDate': {
|
|
195
|
+
const wall = parseInputDate(text, kind);
|
|
196
|
+
if (typeof wall === 'string') return { error: wall };
|
|
197
|
+
// A date alone keeps a manual task's time of day (an automatic one starts on the day's first working time).
|
|
198
|
+
const old = dates && kind === 'date' && task.manuallyScheduled ? toWallTime(dates.start, zone) : null;
|
|
199
|
+
const start = fromWallTime(old ? { ...wall, hour: old.hour, minute: old.minute } : wall, zone);
|
|
200
|
+
// Without a duration, a start alone would make it a milestone.
|
|
201
|
+
const duration = task.duration === null ? { duration: 1 } : {};
|
|
202
|
+
return {
|
|
203
|
+
change: (tx) => {
|
|
204
|
+
if (task.manuallyScheduled) tx.tasks.update(id, { startDate: start, ...duration });
|
|
205
|
+
else
|
|
206
|
+
tx.tasks.update(id, { constraintType: 'startnoearlierthan', constraintDate: start, ...duration });
|
|
207
|
+
},
|
|
208
|
+
propose: (before, next) =>
|
|
209
|
+
next ? { kind: 'move', task: before, start: next.start, end: next.end } : null,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
case 'endDate': {
|
|
213
|
+
if (!dates) return { error: 'Set a start date first.' };
|
|
214
|
+
const wall = parseInputDate(text, kind);
|
|
215
|
+
if (typeof wall === 'string') return { error: wall };
|
|
216
|
+
let end: number;
|
|
217
|
+
if (kind === 'datetime') {
|
|
218
|
+
end = fromWallTime(wall, zone);
|
|
219
|
+
} else if (atMidnight(dates, zone)) {
|
|
220
|
+
// The whole day: up to the next midnight.
|
|
221
|
+
end = fromWallTime({ ...wall, day: wall.day + 1 }, zone);
|
|
222
|
+
} else {
|
|
223
|
+
const old = toWallTime(dates.end, zone);
|
|
224
|
+
end = fromWallTime({ ...wall, hour: old.hour, minute: old.minute }, zone);
|
|
225
|
+
}
|
|
226
|
+
if (end <= dates.start) return { error: 'The end must be after the start.' };
|
|
227
|
+
if (end - dates.start > MAX_SPAN) {
|
|
228
|
+
return { error: `A task can span at most ${String(MAX_SPAN_YEARS)} years.` };
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
change: (tx) => {
|
|
232
|
+
tx.tasks.update(id, { endDate: end });
|
|
233
|
+
},
|
|
234
|
+
propose: (before, next) =>
|
|
235
|
+
next ? { kind: 'resize', task: before, start: next.start, end: next.end } : null,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
case 'duration': {
|
|
239
|
+
const parsed = parseDuration(text, task.durationUnit);
|
|
240
|
+
if (typeof parsed === 'string') return { error: parsed };
|
|
241
|
+
const days =
|
|
242
|
+
(parsed.value * workingMsPerUnit(parsed.unit, settings)) / workingMsPerUnit('day', settings);
|
|
243
|
+
if (!(days <= MAX_WORKING_DAYS)) {
|
|
244
|
+
return { error: `Enter a duration of at most ${String(MAX_WORKING_DAYS)} working days.` };
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
change: (tx) => {
|
|
248
|
+
tx.tasks.update(id, { duration: parsed.value, durationUnit: parsed.unit });
|
|
249
|
+
},
|
|
250
|
+
propose: (before, next) =>
|
|
251
|
+
next ? { kind: 'resize', task: before, start: next.start, end: next.end } : null,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
case 'percentDone': {
|
|
255
|
+
const match = /^\s*(\d+(?:[.,]\d+)?)\s*%?\s*$/.exec(text);
|
|
256
|
+
const value = match ? Number((match[1] as string).replace(',', '.')) : Number.NaN;
|
|
257
|
+
if (!(value >= 0 && value <= 100)) return { error: 'Enter a percentage from 0 to 100.' };
|
|
258
|
+
return {
|
|
259
|
+
change: (tx) => {
|
|
260
|
+
tx.tasks.update(id, { percentDone: value });
|
|
261
|
+
},
|
|
262
|
+
propose: (before) => ({ kind: 'progress', task: before, percentDone: value }),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** A duration like `4d`, `2 weeks` or `1,5` (in the task's unit). */
|
|
269
|
+
export function parseDuration(text: string, fallback: TimeUnit): { value: number; unit: TimeUnit } | string {
|
|
270
|
+
const match = /^\s*(\d+(?:[.,]\d+)?)\s*([a-z]*)\s*$/i.exec(text);
|
|
271
|
+
const message = 'Enter a duration, like 4d, 2w or 3h.';
|
|
272
|
+
if (!match) return message;
|
|
273
|
+
const value = Number((match[1] as string).replace(',', '.'));
|
|
274
|
+
const name = (match[2] as string).toLowerCase();
|
|
275
|
+
const unit = name === '' ? fallback : UNIT_NAMES[name];
|
|
276
|
+
if (unit === undefined || !Number.isFinite(value)) return message;
|
|
277
|
+
return { value, unit };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** The wall-clock date (and time) of an `<input type="date">` or `datetime-local` value. */
|
|
281
|
+
export function parseInputDate(
|
|
282
|
+
text: string,
|
|
283
|
+
kind: EditKind,
|
|
284
|
+
): { year: number; month: number; day: number; hour: number; minute: number } | string {
|
|
285
|
+
const match =
|
|
286
|
+
kind === 'datetime'
|
|
287
|
+
? /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::\d{2}(?:\.\d+)?)?$/.exec(text.trim())
|
|
288
|
+
: /^(\d{4})-(\d{2})-(\d{2})$/.exec(text.trim());
|
|
289
|
+
if (!match) return kind === 'datetime' ? 'Enter a date and time.' : 'Enter a date.';
|
|
290
|
+
const [year, month, day, hour = 0, minute = 0] = match.slice(1).map(Number) as [
|
|
291
|
+
number,
|
|
292
|
+
number,
|
|
293
|
+
number,
|
|
294
|
+
number?,
|
|
295
|
+
number?,
|
|
296
|
+
];
|
|
297
|
+
if (year < MIN_YEAR || year > MAX_YEAR)
|
|
298
|
+
return `Enter a year from ${String(MIN_YEAR)} to ${String(MAX_YEAR)}.`;
|
|
299
|
+
if (month < 1 || month > 12 || day < 1 || day > daysInMonth(year, month) || hour > 23 || minute > 59) {
|
|
300
|
+
return 'That date does not exist.';
|
|
301
|
+
}
|
|
302
|
+
return { year, month, day, hour, minute };
|
|
303
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { Patch } from '../data/types';
|
|
2
|
+
|
|
3
|
+
/** A change as it goes into the history: an edit, or an undo or redo of an earlier one (its patch). */
|
|
4
|
+
export interface HistoryAction {
|
|
5
|
+
readonly type: 'edit' | 'undo' | 'redo';
|
|
6
|
+
readonly patch: Patch;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface HistoryState {
|
|
10
|
+
readonly canUndo: boolean;
|
|
11
|
+
readonly canRedo: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface History {
|
|
15
|
+
readonly state: () => HistoryState;
|
|
16
|
+
/** The change an undo would revert, and a redo would redo. */
|
|
17
|
+
readonly undoable: () => Patch | undefined;
|
|
18
|
+
readonly redoable: () => Patch | undefined;
|
|
19
|
+
/** Records changes that took effect (in controlled mode: once the app passed the data back). */
|
|
20
|
+
readonly confirm: (actions: readonly HistoryAction[]) => void;
|
|
21
|
+
readonly clear: () => void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const LIMIT = 100;
|
|
25
|
+
|
|
26
|
+
/** Undo and redo stacks of patches. The state object is the same while nothing changes. */
|
|
27
|
+
export function createHistory(): History {
|
|
28
|
+
let undo: Patch[] = [];
|
|
29
|
+
let redo: Patch[] = [];
|
|
30
|
+
let state: HistoryState = { canUndo: false, canRedo: false };
|
|
31
|
+
const update = () => {
|
|
32
|
+
const next = { canUndo: undo.length > 0, canRedo: redo.length > 0 };
|
|
33
|
+
if (next.canUndo !== state.canUndo || next.canRedo !== state.canRedo) state = next;
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
state: () => state,
|
|
37
|
+
undoable: () => undo.at(-1),
|
|
38
|
+
redoable: () => redo.at(-1),
|
|
39
|
+
confirm(actions) {
|
|
40
|
+
for (const { type, patch } of actions) {
|
|
41
|
+
if (type === 'edit') {
|
|
42
|
+
undo.push(patch);
|
|
43
|
+
if (undo.length > LIMIT) undo.shift();
|
|
44
|
+
redo = [];
|
|
45
|
+
} else if (type === 'undo') {
|
|
46
|
+
// Only the step on top can be undone. Anything else means the history no longer matches the data.
|
|
47
|
+
if (undo.at(-1) !== patch) {
|
|
48
|
+
undo = [];
|
|
49
|
+
redo = [];
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
undo.pop();
|
|
53
|
+
redo.push(patch);
|
|
54
|
+
} else {
|
|
55
|
+
if (redo.at(-1) !== patch) {
|
|
56
|
+
undo = [];
|
|
57
|
+
redo = [];
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
redo.pop();
|
|
61
|
+
undo.push(patch);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
update();
|
|
65
|
+
},
|
|
66
|
+
clear() {
|
|
67
|
+
undo = [];
|
|
68
|
+
redo = [];
|
|
69
|
+
update();
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|