@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,149 @@
|
|
|
1
|
+
import type { Id } from '../data/types';
|
|
2
|
+
import {
|
|
3
|
+
moveTo,
|
|
4
|
+
selectionOf,
|
|
5
|
+
toggleRow,
|
|
6
|
+
visibleRowOf,
|
|
7
|
+
type KeyModifiers,
|
|
8
|
+
type Selection,
|
|
9
|
+
type VisibleRows,
|
|
10
|
+
} from './selection';
|
|
11
|
+
|
|
12
|
+
/** A key press, as `KeyboardEvent.key` plus the modifiers held. */
|
|
13
|
+
export interface KeyInput extends KeyModifiers {
|
|
14
|
+
readonly key: string;
|
|
15
|
+
/** `KeyboardEvent.code`: finds shortcuts (Ctrl+Z, Ctrl+A) on layouts without latin letters. */
|
|
16
|
+
readonly code?: string | undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface KeyContext extends VisibleRows {
|
|
20
|
+
readonly selection: Selection;
|
|
21
|
+
readonly collapsed: ReadonlySet<Id>;
|
|
22
|
+
/** Rows that fit in the viewport, for PageUp and PageDown. */
|
|
23
|
+
readonly pageRows: number;
|
|
24
|
+
readonly multiSelect: boolean;
|
|
25
|
+
readonly deleteKey: boolean;
|
|
26
|
+
readonly undoRedo: boolean;
|
|
27
|
+
readonly cellEdit: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** What a key press does; carried out by the controller. */
|
|
31
|
+
export type KeyCommand =
|
|
32
|
+
| { readonly type: 'select'; readonly selection: Selection }
|
|
33
|
+
| { readonly type: 'expand'; readonly id: Id; readonly expanded: boolean }
|
|
34
|
+
| { readonly type: 'delete'; readonly ids: readonly Id[]; readonly selection: Selection }
|
|
35
|
+
| { readonly type: 'edit'; readonly id: Id }
|
|
36
|
+
| { readonly type: 'menu'; readonly id: Id }
|
|
37
|
+
| { readonly type: 'undo' }
|
|
38
|
+
| { readonly type: 'redo' };
|
|
39
|
+
|
|
40
|
+
/** The keyboard of the task grid. `null` for keys it leaves alone. */
|
|
41
|
+
export function keyCommand(input: KeyInput, context: KeyContext): KeyCommand | null {
|
|
42
|
+
const { key } = input;
|
|
43
|
+
if (typeof key !== 'string' || input.alt === true) return null;
|
|
44
|
+
const mod = input.ctrl === true || input.meta === true;
|
|
45
|
+
const shift = input.shift === true;
|
|
46
|
+
const { selection, rowIds } = context;
|
|
47
|
+
const count = rowIds.length;
|
|
48
|
+
const lower = shortcutLetter(key, input.code);
|
|
49
|
+
|
|
50
|
+
if (mod && (lower === 'z' || lower === 'y')) {
|
|
51
|
+
if (!context.undoRedo) return null;
|
|
52
|
+
return lower === 'y' || shift ? { type: 'redo' } : { type: 'undo' };
|
|
53
|
+
}
|
|
54
|
+
if (key === 'Delete' || key === 'Backspace') {
|
|
55
|
+
return context.deleteKey && selection.ids.length > 0 ? deletion(context) : null;
|
|
56
|
+
}
|
|
57
|
+
if (key === 'Escape') {
|
|
58
|
+
return selection.ids.length > 0
|
|
59
|
+
? { type: 'select', selection: selectionOf(selection, [], selection.active, selection.anchor) }
|
|
60
|
+
: null;
|
|
61
|
+
}
|
|
62
|
+
if (count === 0) return null;
|
|
63
|
+
if (mod && lower === 'a') {
|
|
64
|
+
if (!context.multiSelect) return null;
|
|
65
|
+
// A copy: the selection is handed to the app, and rowIds is the engine's own.
|
|
66
|
+
return {
|
|
67
|
+
type: 'select',
|
|
68
|
+
selection: selectionOf(selection, [...rowIds], selection.active, selection.anchor),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const current = visibleRowOf(selection.active, context);
|
|
73
|
+
const move = (target: number): KeyCommand => ({
|
|
74
|
+
type: 'select',
|
|
75
|
+
selection: moveTo(
|
|
76
|
+
selection,
|
|
77
|
+
Math.max(0, Math.min(count - 1, target)),
|
|
78
|
+
context,
|
|
79
|
+
{ extend: shift, add: mod },
|
|
80
|
+
context.multiSelect,
|
|
81
|
+
),
|
|
82
|
+
});
|
|
83
|
+
const page = Math.max(1, context.pageRows);
|
|
84
|
+
switch (key) {
|
|
85
|
+
case 'ArrowDown':
|
|
86
|
+
return move(current === undefined ? 0 : current + 1);
|
|
87
|
+
case 'ArrowUp':
|
|
88
|
+
return move(current === undefined ? 0 : current - 1);
|
|
89
|
+
case 'PageDown':
|
|
90
|
+
return move(current === undefined ? 0 : current + page);
|
|
91
|
+
case 'PageUp':
|
|
92
|
+
return move(current === undefined ? 0 : current - page);
|
|
93
|
+
case 'Home':
|
|
94
|
+
return move(0);
|
|
95
|
+
case 'End':
|
|
96
|
+
return move(count - 1);
|
|
97
|
+
}
|
|
98
|
+
if (current === undefined) return null;
|
|
99
|
+
const id = rowIds[current] as Id;
|
|
100
|
+
const isParent = !context.tree.isLeaf(id);
|
|
101
|
+
switch (key) {
|
|
102
|
+
case 'ArrowRight':
|
|
103
|
+
if (!isParent) return null;
|
|
104
|
+
if (context.collapsed.has(id)) return { type: 'expand', id, expanded: true };
|
|
105
|
+
return move(current + 1); // its first child
|
|
106
|
+
case 'ArrowLeft': {
|
|
107
|
+
if (isParent && !context.collapsed.has(id)) return { type: 'expand', id, expanded: false };
|
|
108
|
+
const parent = context.tree.ancestors(id)[0];
|
|
109
|
+
const index = parent === undefined ? undefined : context.rowIndex.get(parent);
|
|
110
|
+
return index === undefined ? null : move(index);
|
|
111
|
+
}
|
|
112
|
+
case ' ':
|
|
113
|
+
return { type: 'select', selection: toggleRow(selection, current, context, context.multiSelect) };
|
|
114
|
+
case 'ContextMenu':
|
|
115
|
+
return { type: 'menu', id };
|
|
116
|
+
case 'F10':
|
|
117
|
+
return shift && !mod ? { type: 'menu', id } : null;
|
|
118
|
+
case 'Enter':
|
|
119
|
+
case 'F2':
|
|
120
|
+
return context.cellEdit && !mod && !shift ? { type: 'edit', id } : null;
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The letter of a key for shortcuts; from the physical key when the layout's key isn't a latin letter. */
|
|
126
|
+
function shortcutLetter(key: string, code: string | undefined): string {
|
|
127
|
+
const letter = key.length === 1 ? key.toLowerCase() : key;
|
|
128
|
+
if (/^[a-z]$/.test(letter) || typeof code !== 'string' || !/^Key[A-Z]$/.test(code)) return letter;
|
|
129
|
+
return code.slice(3).toLowerCase();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Deletes the selected rows (a task inside another selected one goes with it). Selected tasks hidden in a
|
|
134
|
+
* collapsed parent are left alone: a key press never deletes what can't be seen. The cursor moves to the row
|
|
135
|
+
* that takes the place of the active one, or the one before when nothing comes after.
|
|
136
|
+
*/
|
|
137
|
+
function deletion(context: KeyContext): KeyCommand | null {
|
|
138
|
+
const { selection, tree, rowIds, rowIndex } = context;
|
|
139
|
+
const ids = selection.ids.filter(
|
|
140
|
+
(id) => rowIndex.has(id) && !tree.ancestors(id).some((ancestor) => selection.set.has(ancestor)),
|
|
141
|
+
);
|
|
142
|
+
if (ids.length === 0) return null;
|
|
143
|
+
const gone = new Set(ids);
|
|
144
|
+
const stays = (id: Id) => !gone.has(id) && !tree.ancestors(id).some((ancestor) => gone.has(ancestor));
|
|
145
|
+
const from = visibleRowOf(selection.active, context) ?? context.rowIndex.get(ids[0] as Id) ?? 0;
|
|
146
|
+
const after = rowIds.slice(from).find(stays);
|
|
147
|
+
const next = after ?? rowIds.slice(0, from).reverse().find(stays) ?? null;
|
|
148
|
+
return { type: 'delete', ids, selection: selectionOf(selection, [], next, next) };
|
|
149
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { QuartzioError } from '../util/errors';
|
|
2
|
+
|
|
3
|
+
/** The task list is never narrower than this, so the divider can always be found and dragged back. */
|
|
4
|
+
export const MIN_LIST_WIDTH = 80;
|
|
5
|
+
/** Room the timeline keeps when the list is widened. */
|
|
6
|
+
export const MIN_TIMELINE_WIDTH = 120;
|
|
7
|
+
const KEY_STEP = 10;
|
|
8
|
+
const BIG_KEY_STEP = 50;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The width of the task list (its pane, border included), as the divider between it and the timeline sets it.
|
|
12
|
+
* `width: null` means automatic: as wide as the columns, up to `--qz-list-max-width`.
|
|
13
|
+
*/
|
|
14
|
+
export interface ListPaneState {
|
|
15
|
+
readonly width: number | null;
|
|
16
|
+
/** The divider is being dragged. */
|
|
17
|
+
readonly resizing: boolean;
|
|
18
|
+
/** Bounds for the divider: at most as wide as the columns (the chart may show it narrower). */
|
|
19
|
+
readonly min: number;
|
|
20
|
+
readonly max: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* What the divider does, sent from the renderer with measured sizes: `width` is the list pane as shown now,
|
|
25
|
+
* `chartWidth` the whole chart's. `x` is any horizontal coordinate that moves with the pointer.
|
|
26
|
+
*/
|
|
27
|
+
export type ListResizeAction =
|
|
28
|
+
| { readonly type: 'start'; readonly x: number; readonly width: number; readonly chartWidth: number }
|
|
29
|
+
| { readonly type: 'move'; readonly x: number }
|
|
30
|
+
| { readonly type: 'end' }
|
|
31
|
+
| { readonly type: 'cancel' }
|
|
32
|
+
| {
|
|
33
|
+
readonly type: 'key';
|
|
34
|
+
readonly key: string;
|
|
35
|
+
readonly shift?: boolean | undefined;
|
|
36
|
+
readonly width: number;
|
|
37
|
+
readonly chartWidth: number;
|
|
38
|
+
}
|
|
39
|
+
/** Back to automatic (double-click on the divider). */
|
|
40
|
+
| { readonly type: 'reset' };
|
|
41
|
+
|
|
42
|
+
/** The `listWidth` option: a positive number of pixels, or `null`/`undefined` for automatic. */
|
|
43
|
+
export function toListWidth(value: unknown): number | null {
|
|
44
|
+
if (value === undefined || value === null) return null;
|
|
45
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0 || value > 100_000) {
|
|
46
|
+
throw new QuartzioError('Gantt options: "listWidth" must be a positive number of pixels, or null.');
|
|
47
|
+
}
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const finite = (...values: readonly unknown[]): boolean =>
|
|
52
|
+
values.every((value) => typeof value === 'number' && Number.isFinite(value));
|
|
53
|
+
|
|
54
|
+
export interface ListWidth {
|
|
55
|
+
readonly state: () => ListPaneState;
|
|
56
|
+
/** A new `listWidth` option. The same value again keeps a dragged width; another one ends a drag. */
|
|
57
|
+
readonly setOption: (value: number | null) => void;
|
|
58
|
+
/** The columns' total width changed (it bounds the list). */
|
|
59
|
+
readonly setColumnsWidth: (width: number) => void;
|
|
60
|
+
/**
|
|
61
|
+
* Applies an action: `used` says whether it was one the divider acts on, `changed` the new width to report
|
|
62
|
+
* (`undefined` when there's nothing to report).
|
|
63
|
+
*/
|
|
64
|
+
readonly act: (action: ListResizeAction) => { used: boolean; changed?: number | null };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The divider between the task list and the timeline. Like zooming and the `preset` option: dragging overrides
|
|
69
|
+
* the `listWidth` option until the option changes (compared by value), and the new width is reported.
|
|
70
|
+
*/
|
|
71
|
+
export function createListWidth(option: number | null, columnsWidth: number): ListWidth {
|
|
72
|
+
// undefined: nothing dragged since the option was set.
|
|
73
|
+
let override: number | null | undefined;
|
|
74
|
+
let columns = columnsWidth;
|
|
75
|
+
let drag: {
|
|
76
|
+
x: number;
|
|
77
|
+
width: number;
|
|
78
|
+
chartWidth: number;
|
|
79
|
+
// What to go back to on cancel, and what the drop is compared with.
|
|
80
|
+
override: number | null | undefined;
|
|
81
|
+
effective: number | null;
|
|
82
|
+
} | null = null;
|
|
83
|
+
|
|
84
|
+
function maxOf(columnsTotal: number): number {
|
|
85
|
+
return Math.max(MIN_LIST_WIDTH, Math.round(columnsTotal) + 1);
|
|
86
|
+
}
|
|
87
|
+
// An option below the minimum (a width stored from another layout, say) is shown at the minimum.
|
|
88
|
+
const effective = (): number | null => {
|
|
89
|
+
const width = override === undefined ? option : override;
|
|
90
|
+
return width === null ? null : Math.max(MIN_LIST_WIDTH, width);
|
|
91
|
+
};
|
|
92
|
+
const clamp = (width: number, chartWidth: number) =>
|
|
93
|
+
Math.round(
|
|
94
|
+
Math.min(
|
|
95
|
+
Math.max(width, MIN_LIST_WIDTH),
|
|
96
|
+
Math.max(MIN_LIST_WIDTH, Math.min(maxOf(columns), chartWidth - MIN_TIMELINE_WIDTH)),
|
|
97
|
+
),
|
|
98
|
+
);
|
|
99
|
+
let current: ListPaneState = {
|
|
100
|
+
width: effective(),
|
|
101
|
+
resizing: false,
|
|
102
|
+
min: MIN_LIST_WIDTH,
|
|
103
|
+
max: maxOf(columns),
|
|
104
|
+
};
|
|
105
|
+
const update = (): void => {
|
|
106
|
+
const next = { width: effective(), resizing: drag !== null, min: MIN_LIST_WIDTH, max: maxOf(columns) };
|
|
107
|
+
if (next.width !== current.width || next.resizing !== current.resizing || next.max !== current.max) {
|
|
108
|
+
current = next;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
state: () => current,
|
|
114
|
+
|
|
115
|
+
setOption(value) {
|
|
116
|
+
if (value === option) return;
|
|
117
|
+
option = value;
|
|
118
|
+
override = undefined;
|
|
119
|
+
drag = null;
|
|
120
|
+
update();
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
setColumnsWidth(width) {
|
|
124
|
+
columns = width;
|
|
125
|
+
update();
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
act(action) {
|
|
129
|
+
// Plain JavaScript may pass anything.
|
|
130
|
+
if (typeof action !== 'object' || (action as unknown) === null) return { used: false };
|
|
131
|
+
switch (action.type) {
|
|
132
|
+
case 'start': {
|
|
133
|
+
// One drag at a time: a second finger doesn't restart it.
|
|
134
|
+
if (drag || !finite(action.x, action.width, action.chartWidth) || action.chartWidth <= 0)
|
|
135
|
+
return { used: false };
|
|
136
|
+
drag = {
|
|
137
|
+
x: action.x,
|
|
138
|
+
width: action.width,
|
|
139
|
+
chartWidth: action.chartWidth,
|
|
140
|
+
override,
|
|
141
|
+
effective: effective(),
|
|
142
|
+
};
|
|
143
|
+
update();
|
|
144
|
+
return { used: true };
|
|
145
|
+
}
|
|
146
|
+
case 'move': {
|
|
147
|
+
if (!drag || !finite(action.x)) return { used: false };
|
|
148
|
+
const width = clamp(drag.width + action.x - drag.x, drag.chartWidth);
|
|
149
|
+
// A press that hasn't moved the width yet (a click) leaves an automatic width automatic.
|
|
150
|
+
if (override !== drag.override || width !== Math.round(drag.width)) override = width;
|
|
151
|
+
update();
|
|
152
|
+
return { used: true };
|
|
153
|
+
}
|
|
154
|
+
case 'end': {
|
|
155
|
+
if (!drag) return { used: false };
|
|
156
|
+
const moved = effective() !== drag.effective;
|
|
157
|
+
drag = null;
|
|
158
|
+
update();
|
|
159
|
+
return moved ? { used: true, changed: effective() } : { used: true };
|
|
160
|
+
}
|
|
161
|
+
case 'cancel': {
|
|
162
|
+
if (!drag) return { used: false };
|
|
163
|
+
override = drag.override;
|
|
164
|
+
drag = null;
|
|
165
|
+
update();
|
|
166
|
+
return { used: true };
|
|
167
|
+
}
|
|
168
|
+
case 'key': {
|
|
169
|
+
if (drag || !finite(action.width, action.chartWidth) || action.chartWidth <= 0)
|
|
170
|
+
return { used: false };
|
|
171
|
+
const step = action.shift ? BIG_KEY_STEP : KEY_STEP;
|
|
172
|
+
const target =
|
|
173
|
+
action.key === 'ArrowLeft'
|
|
174
|
+
? action.width - step
|
|
175
|
+
: action.key === 'ArrowRight'
|
|
176
|
+
? action.width + step
|
|
177
|
+
: action.key === 'Home'
|
|
178
|
+
? MIN_LIST_WIDTH
|
|
179
|
+
: action.key === 'End'
|
|
180
|
+
? Number.POSITIVE_INFINITY
|
|
181
|
+
: null;
|
|
182
|
+
if (target === null) return { used: false };
|
|
183
|
+
// A key at a bound is used, but moves nothing (an automatic width stays automatic).
|
|
184
|
+
const width = clamp(target, action.chartWidth);
|
|
185
|
+
if (width === Math.round(action.width)) return { used: true };
|
|
186
|
+
override = width;
|
|
187
|
+
update();
|
|
188
|
+
return { used: true, changed: width };
|
|
189
|
+
}
|
|
190
|
+
case 'reset': {
|
|
191
|
+
if (drag || effective() === null) return { used: false };
|
|
192
|
+
override = null;
|
|
193
|
+
update();
|
|
194
|
+
return { used: true, changed: null };
|
|
195
|
+
}
|
|
196
|
+
default:
|
|
197
|
+
return { used: false };
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
}
|
package/src/view/menu.ts
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import type { TreeIndex } from '../data/tree';
|
|
2
|
+
import type { Id, Task } from '../data/types';
|
|
3
|
+
import { VIEW_PRESETS, type ViewPreset } from '../timeaxis/presets';
|
|
4
|
+
import type { TimeUnit } from '../util/time';
|
|
5
|
+
import { isEqual } from '../util/equal';
|
|
6
|
+
import type { KeyInput } from './keyboard';
|
|
7
|
+
|
|
8
|
+
/** What a menu is for: a task (its row or bar), or the time axis (its header). */
|
|
9
|
+
export type MenuTarget = { readonly kind: 'task'; readonly id: Id } | { readonly kind: 'timeAxis' };
|
|
10
|
+
|
|
11
|
+
/** What an item's `action` (and the item customizers) get. */
|
|
12
|
+
export interface MenuContext {
|
|
13
|
+
readonly target: MenuTarget;
|
|
14
|
+
/** The task the menu is for, or `null` for the time axis. */
|
|
15
|
+
readonly task: Task | null;
|
|
16
|
+
readonly selection: readonly Id[];
|
|
17
|
+
readonly preset: ViewPreset;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface MenuItem {
|
|
21
|
+
readonly id: string;
|
|
22
|
+
readonly label: string;
|
|
23
|
+
readonly disabled?: boolean | undefined;
|
|
24
|
+
/** Shown checked (e.g. the current preset). */
|
|
25
|
+
readonly checked?: boolean | undefined;
|
|
26
|
+
/** A line before it. */
|
|
27
|
+
readonly separator?: boolean | undefined;
|
|
28
|
+
/** A submenu (one level). */
|
|
29
|
+
readonly items?: readonly MenuItem[] | undefined;
|
|
30
|
+
/** For your own items: what choosing it does. Built-in items are carried out by the engine. */
|
|
31
|
+
readonly action?: ((context: MenuContext) => void) | undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The open menu. */
|
|
35
|
+
export interface MenuState {
|
|
36
|
+
readonly kind: 'task' | 'timeAxis';
|
|
37
|
+
readonly taskId: Id | null;
|
|
38
|
+
/** Where it opens, in the chart's coordinates (from the root element's corner). */
|
|
39
|
+
readonly x: number;
|
|
40
|
+
readonly y: number;
|
|
41
|
+
readonly items: readonly MenuItem[];
|
|
42
|
+
/** The highlighted item (in the open submenu, if there is one). */
|
|
43
|
+
readonly active: string | null;
|
|
44
|
+
/** The item whose submenu is open. */
|
|
45
|
+
readonly submenu: string | null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type MenuCustomizer = (items: readonly MenuItem[], context: MenuContext) => readonly MenuItem[];
|
|
49
|
+
|
|
50
|
+
/** A task menu's built-in item ids, carried out by the engine. */
|
|
51
|
+
export type TaskMenuItemId =
|
|
52
|
+
| 'edit'
|
|
53
|
+
| 'addTaskAbove'
|
|
54
|
+
| 'addTaskBelow'
|
|
55
|
+
| 'addSubtask'
|
|
56
|
+
| 'addMilestone'
|
|
57
|
+
| 'addSuccessor'
|
|
58
|
+
| 'addPredecessor'
|
|
59
|
+
| 'indent'
|
|
60
|
+
| 'outdent'
|
|
61
|
+
| 'convertToMilestone'
|
|
62
|
+
| 'delete';
|
|
63
|
+
|
|
64
|
+
/** The tasks an action on `id` applies to: the selection when it includes `id`, without tasks inside others. */
|
|
65
|
+
export function targetsOf(id: Id, selection: readonly Id[], tree: TreeIndex): Id[] {
|
|
66
|
+
const ids = selection.includes(id) ? selection : [id];
|
|
67
|
+
const set = new Set(ids);
|
|
68
|
+
const roots = ids.filter((each) => !tree.ancestors(each).some((ancestor) => set.has(ancestor)));
|
|
69
|
+
// In tree order, so moving several keeps their order.
|
|
70
|
+
const order = tree.flatten();
|
|
71
|
+
const position = new Map(order.map((each, index) => [each, index]));
|
|
72
|
+
return roots.sort((a, b) => (position.get(a) ?? 0) - (position.get(b) ?? 0));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const hasSiblingBefore = (tree: TreeIndex, id: Id, parentOf: (id: Id) => Id | null) =>
|
|
76
|
+
tree.children(parentOf(id)).indexOf(id) > 0;
|
|
77
|
+
|
|
78
|
+
/** The built-in task menu. */
|
|
79
|
+
export function taskMenuItems(
|
|
80
|
+
task: Task,
|
|
81
|
+
targets: readonly Id[],
|
|
82
|
+
tree: TreeIndex,
|
|
83
|
+
parentOf: (id: Id) => Id | null,
|
|
84
|
+
{ edit }: { edit: boolean },
|
|
85
|
+
): MenuItem[] {
|
|
86
|
+
const isParent = !tree.isLeaf(task.id);
|
|
87
|
+
const items: MenuItem[] = [];
|
|
88
|
+
if (edit) items.push({ id: 'edit', label: 'Edit' });
|
|
89
|
+
items.push(
|
|
90
|
+
{
|
|
91
|
+
id: 'add',
|
|
92
|
+
label: 'Add',
|
|
93
|
+
items: [
|
|
94
|
+
{ id: 'addTaskAbove', label: 'Task above' },
|
|
95
|
+
{ id: 'addTaskBelow', label: 'Task below' },
|
|
96
|
+
{ id: 'addSubtask', label: 'Subtask' },
|
|
97
|
+
{ id: 'addMilestone', label: 'Milestone' },
|
|
98
|
+
{ id: 'addSuccessor', label: 'Successor' },
|
|
99
|
+
{ id: 'addPredecessor', label: 'Predecessor' },
|
|
100
|
+
],
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
id: 'indent',
|
|
104
|
+
label: 'Indent',
|
|
105
|
+
separator: true,
|
|
106
|
+
disabled: !targets.some((id) => hasSiblingBefore(tree, id, parentOf)),
|
|
107
|
+
},
|
|
108
|
+
{ id: 'outdent', label: 'Outdent', disabled: !targets.some((id) => parentOf(id) !== null) },
|
|
109
|
+
{ id: 'convertToMilestone', label: 'Convert to milestone', disabled: isParent || task.duration === 0 },
|
|
110
|
+
{ id: 'delete', label: 'Delete', separator: true },
|
|
111
|
+
);
|
|
112
|
+
return items;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const PRESET_LABELS: Record<string, string> = {
|
|
116
|
+
hourAndDay: 'Hours',
|
|
117
|
+
dayAndWeek: 'Days',
|
|
118
|
+
weekAndDay: 'Weeks and days',
|
|
119
|
+
weekAndMonth: 'Weeks',
|
|
120
|
+
monthAndYear: 'Months',
|
|
121
|
+
quarterAndYear: 'Quarters',
|
|
122
|
+
manyYears: 'Years',
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/** The built-in time axis menu: zooming, and the presets. */
|
|
126
|
+
export function timeAxisMenuItems(preset: ViewPreset): MenuItem[] {
|
|
127
|
+
return [
|
|
128
|
+
{ id: 'zoomIn', label: 'Zoom in', disabled: zoomStep(preset, 'in') === undefined },
|
|
129
|
+
{ id: 'zoomOut', label: 'Zoom out', disabled: zoomStep(preset, 'out') === undefined },
|
|
130
|
+
...VIEW_PRESETS.map((each, index) => ({
|
|
131
|
+
id: `preset:${each.id}`,
|
|
132
|
+
label: PRESET_LABELS[each.id] ?? each.id,
|
|
133
|
+
checked: each === preset || isEqual(each, preset),
|
|
134
|
+
separator: index === 0,
|
|
135
|
+
})),
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Rough lengths, only to compare how zoomed in presets are.
|
|
140
|
+
const UNIT_MS: Record<TimeUnit, number> = {
|
|
141
|
+
millisecond: 1,
|
|
142
|
+
second: 1000,
|
|
143
|
+
minute: 60_000,
|
|
144
|
+
hour: 3_600_000,
|
|
145
|
+
day: 86_400_000,
|
|
146
|
+
week: 604_800_000,
|
|
147
|
+
month: 2_629_800_000,
|
|
148
|
+
quarter: 7_889_400_000,
|
|
149
|
+
year: 31_557_600_000,
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/** Pixels per millisecond: how zoomed in a preset is. */
|
|
153
|
+
const zoomLevel = (preset: ViewPreset) =>
|
|
154
|
+
preset.tickWidth / (UNIT_MS[preset.tickUnit] * preset.tickIncrement);
|
|
155
|
+
|
|
156
|
+
/** The built-in preset one step in (more pixels per time) or out, from any preset (custom ones too). */
|
|
157
|
+
export function zoomStep(preset: ViewPreset, direction: 'in' | 'out'): ViewPreset | undefined {
|
|
158
|
+
const level = zoomLevel(preset);
|
|
159
|
+
let best: ViewPreset | undefined;
|
|
160
|
+
for (const candidate of VIEW_PRESETS) {
|
|
161
|
+
const other = zoomLevel(candidate);
|
|
162
|
+
if (direction === 'in' ? other <= level * 1.0001 : other >= level * 0.9999) continue;
|
|
163
|
+
if (!best || (direction === 'in' ? other < zoomLevel(best) : other > zoomLevel(best))) best = candidate;
|
|
164
|
+
}
|
|
165
|
+
return best;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Items that can be picked in the list shown (the open submenu, or the menu). */
|
|
169
|
+
const shownItems = (menu: MenuState): readonly MenuItem[] =>
|
|
170
|
+
menu.submenu === null ? menu.items : (menu.items.find((item) => item.id === menu.submenu)?.items ?? []);
|
|
171
|
+
|
|
172
|
+
const enabled = (items: readonly MenuItem[]) => items.filter((item) => item.disabled !== true);
|
|
173
|
+
|
|
174
|
+
/** An item by id, in the menu or its submenus. */
|
|
175
|
+
export function findItem(items: readonly MenuItem[], id: string): MenuItem | undefined {
|
|
176
|
+
for (const item of items) {
|
|
177
|
+
if (item.id === id) return item;
|
|
178
|
+
const inner = item.items ? findItem(item.items, id) : undefined;
|
|
179
|
+
if (inner) return inner;
|
|
180
|
+
}
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** What a key does in a menu: the new state (`null` closes it) and an item to carry out, or `undefined` when unused. */
|
|
185
|
+
export function menuKey(
|
|
186
|
+
menu: MenuState,
|
|
187
|
+
input: KeyInput,
|
|
188
|
+
): { menu: MenuState | null; pick?: string } | undefined {
|
|
189
|
+
const items = enabled(shownItems(menu));
|
|
190
|
+
const index = items.findIndex((item) => item.id === menu.active);
|
|
191
|
+
const at = (i: number) => items[(i + items.length) % items.length]?.id ?? null;
|
|
192
|
+
const openSubmenu = (item: MenuItem | undefined) => {
|
|
193
|
+
const children = enabled(item?.items ?? []);
|
|
194
|
+
return item && children.length > 0
|
|
195
|
+
? { ...menu, submenu: item.id, active: children[0]?.id ?? null }
|
|
196
|
+
: null;
|
|
197
|
+
};
|
|
198
|
+
switch (input.key) {
|
|
199
|
+
case 'ArrowDown':
|
|
200
|
+
return { menu: { ...menu, active: index < 0 ? at(0) : at(index + 1) } };
|
|
201
|
+
case 'ArrowUp':
|
|
202
|
+
return { menu: { ...menu, active: index < 0 ? at(-1) : at(index - 1) } };
|
|
203
|
+
case 'Home':
|
|
204
|
+
return { menu: { ...menu, active: at(0) } };
|
|
205
|
+
case 'End':
|
|
206
|
+
return { menu: { ...menu, active: at(-1) } };
|
|
207
|
+
case 'ArrowRight': {
|
|
208
|
+
const opened = menu.submenu === null ? openSubmenu(items[index]) : null;
|
|
209
|
+
return opened ? { menu: opened } : { menu };
|
|
210
|
+
}
|
|
211
|
+
case 'ArrowLeft':
|
|
212
|
+
return { menu: menu.submenu === null ? menu : { ...menu, active: menu.submenu, submenu: null } };
|
|
213
|
+
case 'Enter':
|
|
214
|
+
case ' ': {
|
|
215
|
+
const item = items[index];
|
|
216
|
+
if (!item) return { menu };
|
|
217
|
+
if (item.items) return { menu: openSubmenu(item) ?? menu };
|
|
218
|
+
return { menu: null, pick: item.id };
|
|
219
|
+
}
|
|
220
|
+
case 'Escape':
|
|
221
|
+
return { menu: menu.submenu === null ? null : { ...menu, active: menu.submenu, submenu: null } };
|
|
222
|
+
case 'Tab':
|
|
223
|
+
return { menu: null };
|
|
224
|
+
}
|
|
225
|
+
return undefined;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** The pointer over an item: it's highlighted, and a submenu opens (another one closes). */
|
|
229
|
+
export function menuHover(menu: MenuState, id: string): MenuState {
|
|
230
|
+
const top = menu.items.find((item) => item.id === id);
|
|
231
|
+
if (top) {
|
|
232
|
+
if (top.disabled === true) return menu.submenu === null ? menu : { ...menu, submenu: null, active: null };
|
|
233
|
+
return { ...menu, active: id, submenu: top.items ? id : null };
|
|
234
|
+
}
|
|
235
|
+
const inSubmenu = shownItems(menu).find((item) => item.id === id);
|
|
236
|
+
return inSubmenu && inSubmenu.disabled !== true && menu.submenu !== null ? { ...menu, active: id } : menu;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Items from a customizer, checked (it's app code): anything unusable falls back to the built-in items. */
|
|
240
|
+
export function customized(
|
|
241
|
+
builtIn: readonly MenuItem[],
|
|
242
|
+
customize: MenuCustomizer | undefined,
|
|
243
|
+
context: MenuContext,
|
|
244
|
+
): readonly MenuItem[] {
|
|
245
|
+
if (!customize) return builtIn;
|
|
246
|
+
try {
|
|
247
|
+
const items: unknown = customize(builtIn, context);
|
|
248
|
+
const ids = new Set<string>();
|
|
249
|
+
// An item with an id not seen yet, a label, an action if any, and (at the top) one level of submenu.
|
|
250
|
+
const valid = (entry: unknown, top: boolean): boolean => {
|
|
251
|
+
if (typeof entry !== 'object' || entry === null) return false;
|
|
252
|
+
const { id, label, action, items: children } = entry as Record<string, unknown>;
|
|
253
|
+
if (typeof id !== 'string' || ids.has(id) || typeof label !== 'string') return false;
|
|
254
|
+
if (action !== undefined && typeof action !== 'function') return false;
|
|
255
|
+
ids.add(id);
|
|
256
|
+
if (children === undefined) return true;
|
|
257
|
+
return top && Array.isArray(children) && children.every((child) => valid(child, false));
|
|
258
|
+
};
|
|
259
|
+
return Array.isArray(items) && items.every((entry) => valid(entry, true))
|
|
260
|
+
? (items as MenuItem[])
|
|
261
|
+
: builtIn;
|
|
262
|
+
} catch {
|
|
263
|
+
return builtIn;
|
|
264
|
+
}
|
|
265
|
+
}
|