@feitio/vanilla 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/dist/feitio.js +766 -6
- package/dist/kanban.js +789 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/kanban/actions/kanban-flip.d.ts +33 -0
- package/dist/types/kanban/actions/move-card.d.ts +19 -0
- package/dist/types/kanban/actions/sync-kanban.d.ts +4 -0
- package/dist/types/kanban/actions/wire-create.d.ts +12 -0
- package/dist/types/kanban/actions/wire-drag.d.ts +8 -0
- package/dist/types/kanban/actions/wire-kanbans.d.ts +6 -0
- package/dist/types/kanban/actions/wire-minimap.d.ts +6 -0
- package/dist/types/kanban/actions/wire-move-menu.d.ts +5 -0
- package/dist/types/kanban/actions/wire-scroll-row.d.ts +5 -0
- package/dist/types/kanban/calculations/kanban-motion.d.ts +45 -0
- package/dist/types/kanban/calculations/kanban-rules.d.ts +8 -0
- package/dist/types/kanban/index.d.ts +11 -0
- package/package.json +11 -6
- package/src/index.ts +1 -0
- package/src/kanban/actions/kanban-flip.ts +172 -0
- package/src/kanban/actions/move-card.ts +61 -0
- package/src/kanban/actions/sync-kanban.ts +31 -0
- package/src/kanban/actions/wire-create.ts +84 -0
- package/src/kanban/actions/wire-drag.ts +240 -0
- package/src/kanban/actions/wire-kanbans.ts +36 -0
- package/src/kanban/actions/wire-minimap.ts +66 -0
- package/src/kanban/actions/wire-move-menu.ts +80 -0
- package/src/kanban/actions/wire-scroll-row.ts +58 -0
- package/src/kanban/calculations/kanban-motion.ts +98 -0
- package/src/kanban/calculations/kanban-rules.ts +19 -0
- package/src/kanban/index.ts +28 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import {
|
|
2
|
+
autoScrollSpeed,
|
|
3
|
+
type Follow,
|
|
4
|
+
followStep,
|
|
5
|
+
insertionIndex,
|
|
6
|
+
SETTLE,
|
|
7
|
+
} from '../calculations/kanban-motion.ts';
|
|
8
|
+
import { targetsOf } from '../calculations/kanban-rules.ts';
|
|
9
|
+
import { animateFrom, play, reduced, snapshot } from './kanban-flip.ts';
|
|
10
|
+
import { moveCard } from './move-card.ts';
|
|
11
|
+
|
|
12
|
+
interface Held extends Follow {
|
|
13
|
+
card: HTMLElement;
|
|
14
|
+
ghost: HTMLElement;
|
|
15
|
+
slot: HTMLElement;
|
|
16
|
+
home: HTMLElement;
|
|
17
|
+
homeNext: Node | null;
|
|
18
|
+
offX: number;
|
|
19
|
+
offY: number;
|
|
20
|
+
px: number;
|
|
21
|
+
py: number;
|
|
22
|
+
running: boolean;
|
|
23
|
+
opened?: HTMLElement;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Holding and dragging a card, on a wide board, with a mouse or a pen (on a phone, the move button
|
|
28
|
+
* does it). The card follows the hand on a spring: it lags a little, leans toward where it goes and
|
|
29
|
+
* sways when the hand stops. The folder under it opens a place where it would land and the cards
|
|
30
|
+
* there make room; folders that do not take it fade. Near the sides the board scrolls by itself.
|
|
31
|
+
* Letting go moves it from where it was let go; Esc, or letting go outside, takes it home.
|
|
32
|
+
*/
|
|
33
|
+
export function wireDrag(window: Window, board: HTMLElement): void {
|
|
34
|
+
const doc = window.document;
|
|
35
|
+
const cols = (): HTMLElement | null => board.querySelector<HTMLElement>('.cols');
|
|
36
|
+
board.addEventListener('pointerdown', (event) => {
|
|
37
|
+
const card = (event.target as Element).closest<HTMLElement>('.col > .feitio-kanban-card');
|
|
38
|
+
if (!card || !board.contains(card) || card.classList.contains('draft')) return;
|
|
39
|
+
if (event.button !== 0 || event.pointerType === 'touch') return;
|
|
40
|
+
if ((event.target as Element).closest('button, a, input, textarea, select, label')) return;
|
|
41
|
+
if (!targetsOf(card.dataset.to).length) return;
|
|
42
|
+
const row = cols();
|
|
43
|
+
if (!row || window.getComputedStyle(row).display !== 'flex') return;
|
|
44
|
+
event.preventDefault();
|
|
45
|
+
const sx = event.clientX;
|
|
46
|
+
const sy = event.clientY;
|
|
47
|
+
let held: Held | undefined;
|
|
48
|
+
const stop = (): void => {
|
|
49
|
+
window.removeEventListener('pointermove', onMove);
|
|
50
|
+
window.removeEventListener('pointerup', onUp);
|
|
51
|
+
window.removeEventListener('keydown', onKey);
|
|
52
|
+
};
|
|
53
|
+
const onMove = (e: PointerEvent): void => {
|
|
54
|
+
if (!held) {
|
|
55
|
+
if (Math.hypot(e.clientX - sx, e.clientY - sy) < 5) return;
|
|
56
|
+
held = lift(e, card);
|
|
57
|
+
}
|
|
58
|
+
held.px = e.clientX;
|
|
59
|
+
held.py = e.clientY;
|
|
60
|
+
hover(held);
|
|
61
|
+
};
|
|
62
|
+
const onUp = (): void => {
|
|
63
|
+
stop();
|
|
64
|
+
if (held) letGo(held, false);
|
|
65
|
+
};
|
|
66
|
+
const onKey = (e: KeyboardEvent): void => {
|
|
67
|
+
if (e.key !== 'Escape' || !held) return;
|
|
68
|
+
stop();
|
|
69
|
+
letGo(held, true);
|
|
70
|
+
};
|
|
71
|
+
window.addEventListener('pointermove', onMove);
|
|
72
|
+
window.addEventListener('pointerup', onUp);
|
|
73
|
+
window.addEventListener('keydown', onKey);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
function lift(event: PointerEvent, card: HTMLElement): Held {
|
|
77
|
+
const r = card.getBoundingClientRect();
|
|
78
|
+
const ghost = doc.createElement('div');
|
|
79
|
+
ghost.className = 'feitio-kanban-ghost';
|
|
80
|
+
ghost.dataset.motion = 'direct';
|
|
81
|
+
ghost.setAttribute('aria-hidden', 'true');
|
|
82
|
+
ghost.style.width = `${r.width}px`;
|
|
83
|
+
const copy = card.cloneNode(true) as HTMLElement;
|
|
84
|
+
copy.removeAttribute('id');
|
|
85
|
+
ghost.append(copy);
|
|
86
|
+
doc.body.append(ghost);
|
|
87
|
+
const slot = doc.createElement('div');
|
|
88
|
+
slot.className = 'slot';
|
|
89
|
+
slot.style.blockSize = `${r.height}px`;
|
|
90
|
+
const home = card.closest<HTMLElement>('.col') as HTMLElement;
|
|
91
|
+
const homeNext = card.nextSibling;
|
|
92
|
+
card.replaceWith(slot);
|
|
93
|
+
doc.documentElement.setAttribute('data-kanban-dragging', '');
|
|
94
|
+
board.setAttribute('data-dragging', '');
|
|
95
|
+
const targets = targetsOf(card.dataset.to);
|
|
96
|
+
for (const col of board.querySelectorAll<HTMLElement>('.col')) {
|
|
97
|
+
if (col !== home && !targets.includes(col.dataset.col ?? ''))
|
|
98
|
+
col.setAttribute('data-blocked', '');
|
|
99
|
+
}
|
|
100
|
+
const held: Held = {
|
|
101
|
+
card,
|
|
102
|
+
ghost,
|
|
103
|
+
slot,
|
|
104
|
+
home,
|
|
105
|
+
homeNext,
|
|
106
|
+
offX: event.clientX - r.left,
|
|
107
|
+
offY: event.clientY - r.top,
|
|
108
|
+
x: r.left,
|
|
109
|
+
y: r.top,
|
|
110
|
+
vx: 0,
|
|
111
|
+
vy: 0,
|
|
112
|
+
tilt: 0,
|
|
113
|
+
px: event.clientX,
|
|
114
|
+
py: event.clientY,
|
|
115
|
+
running: true,
|
|
116
|
+
};
|
|
117
|
+
follow(held);
|
|
118
|
+
return held;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function follow(held: Held): void {
|
|
122
|
+
if (!held.running) return;
|
|
123
|
+
const tx = held.px - held.offX;
|
|
124
|
+
const ty = held.py - held.offY;
|
|
125
|
+
if (reduced(window)) Object.assign(held, { x: tx, y: ty, vx: 0, vy: 0, tilt: 0 });
|
|
126
|
+
else Object.assign(held, followStep(held, tx, ty));
|
|
127
|
+
held.ghost.style.transform = `translate(${held.x}px, ${held.y}px) rotate(${held.tilt}deg) scale(1.04)`;
|
|
128
|
+
const row = cols();
|
|
129
|
+
if (row) {
|
|
130
|
+
const r = row.getBoundingClientRect();
|
|
131
|
+
const v = autoScrollSpeed(held.px, r.left, r.right);
|
|
132
|
+
if (v) {
|
|
133
|
+
row.scrollLeft += v;
|
|
134
|
+
hover(held);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
window.requestAnimationFrame(() => follow(held));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function hover(held: Held): void {
|
|
141
|
+
const under = doc.elementFromPoint(held.px, held.py)?.closest<HTMLElement>('.col');
|
|
142
|
+
const ok = Boolean(
|
|
143
|
+
under && board.contains(under) && under !== held.home && !under.hasAttribute('data-blocked'),
|
|
144
|
+
);
|
|
145
|
+
const dest = ok && under ? under : held.home;
|
|
146
|
+
let before: Element | null;
|
|
147
|
+
if (ok) {
|
|
148
|
+
const cards = [
|
|
149
|
+
...dest.querySelectorAll<HTMLElement>(':scope > .feitio-kanban-card:not(.draft)'),
|
|
150
|
+
];
|
|
151
|
+
const i = insertionIndex(
|
|
152
|
+
cards.map((c) => {
|
|
153
|
+
const r = c.getBoundingClientRect();
|
|
154
|
+
return r.top + r.height / 2;
|
|
155
|
+
}),
|
|
156
|
+
held.py,
|
|
157
|
+
);
|
|
158
|
+
before = cards[i] ?? dest.querySelector(':scope > .add');
|
|
159
|
+
} else {
|
|
160
|
+
before =
|
|
161
|
+
held.homeNext && held.homeNext.parentNode === held.home
|
|
162
|
+
? (held.homeNext as Element)
|
|
163
|
+
: held.home.querySelector(':scope > .add');
|
|
164
|
+
}
|
|
165
|
+
for (const col of board.querySelectorAll<HTMLElement>('.col'))
|
|
166
|
+
col.toggleAttribute('data-over', ok && col === under);
|
|
167
|
+
if (held.slot.parentNode === dest && held.slot.nextSibling === before) return;
|
|
168
|
+
const moving = [
|
|
169
|
+
...board.querySelectorAll<HTMLElement>('.col > .feitio-kanban-card, .col > .add'),
|
|
170
|
+
];
|
|
171
|
+
const first = new Map(moving.map((m) => [m, m.getBoundingClientRect()]));
|
|
172
|
+
const widths = new Map(
|
|
173
|
+
[...board.querySelectorAll<HTMLElement>('.col')].map((c) => [
|
|
174
|
+
c,
|
|
175
|
+
c.getBoundingClientRect().width,
|
|
176
|
+
]),
|
|
177
|
+
);
|
|
178
|
+
// An empty folder that is about to take the card opens; the one it leaves closes again.
|
|
179
|
+
if (held.opened && held.opened !== dest) {
|
|
180
|
+
held.opened.setAttribute('data-empty', '');
|
|
181
|
+
held.opened = undefined;
|
|
182
|
+
}
|
|
183
|
+
if (dest !== held.home && dest.hasAttribute('data-empty')) {
|
|
184
|
+
dest.removeAttribute('data-empty');
|
|
185
|
+
held.opened = dest;
|
|
186
|
+
}
|
|
187
|
+
dest.insertBefore(held.slot, before);
|
|
188
|
+
if (reduced(window)) return;
|
|
189
|
+
for (const [col, w0] of widths) {
|
|
190
|
+
const w1 = col.getBoundingClientRect().width;
|
|
191
|
+
if (Math.abs(w1 - w0) > 1)
|
|
192
|
+
play(col, SETTLE, (p) => ({
|
|
193
|
+
flexBasis: `${w0 + (w1 - w0) * p}px`,
|
|
194
|
+
flexGrow: 0,
|
|
195
|
+
flexShrink: 0,
|
|
196
|
+
}));
|
|
197
|
+
}
|
|
198
|
+
for (const m of moving) {
|
|
199
|
+
const a = first.get(m) as DOMRect;
|
|
200
|
+
const b = m.getBoundingClientRect();
|
|
201
|
+
const dx = a.left - b.left;
|
|
202
|
+
const dy = a.top - b.top;
|
|
203
|
+
if (Math.abs(dx) > 0.5 || Math.abs(dy) > 0.5)
|
|
204
|
+
play(m, SETTLE, (p) => ({ transform: `translate(${dx * (1 - p)}px, ${dy * (1 - p)}px)` }));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function letGo(held: Held, cancel: boolean): void {
|
|
209
|
+
held.running = false;
|
|
210
|
+
doc.documentElement.removeAttribute('data-kanban-dragging');
|
|
211
|
+
board.removeAttribute('data-dragging');
|
|
212
|
+
for (const col of board.querySelectorAll<HTMLElement>('.col')) {
|
|
213
|
+
col.removeAttribute('data-blocked');
|
|
214
|
+
col.removeAttribute('data-over');
|
|
215
|
+
}
|
|
216
|
+
const dest = held.slot.closest<HTMLElement>('.col') as HTMLElement;
|
|
217
|
+
const index = [
|
|
218
|
+
...dest.querySelectorAll(':scope > .feitio-kanban-card:not(.draft), :scope > .slot'),
|
|
219
|
+
].indexOf(held.slot);
|
|
220
|
+
const id = held.card.dataset.id ?? '';
|
|
221
|
+
// The picture of before is the board as the hand left it; the card starts where it was let go.
|
|
222
|
+
const before = snapshot(board);
|
|
223
|
+
const from = { x: held.x, y: held.y, tilt: held.tilt };
|
|
224
|
+
held.ghost.remove();
|
|
225
|
+
// The card goes back home first, so whoever moves it (this board, a React state) starts from the truth.
|
|
226
|
+
held.home.insertBefore(
|
|
227
|
+
held.card,
|
|
228
|
+
held.homeNext && held.homeNext.parentNode === held.home ? held.homeNext : null,
|
|
229
|
+
);
|
|
230
|
+
held.slot.remove();
|
|
231
|
+
if (held.opened) held.opened.setAttribute('data-empty', '');
|
|
232
|
+
if (
|
|
233
|
+
!cancel &&
|
|
234
|
+
dest !== held.home &&
|
|
235
|
+
moveCard(window, board, held.card, dest.dataset.col ?? '', index, before, from)
|
|
236
|
+
)
|
|
237
|
+
return;
|
|
238
|
+
window.requestAnimationFrame(() => animateFrom(window, board, before, { id, from }));
|
|
239
|
+
}
|
|
240
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { chooseStage, syncKanban } from './sync-kanban.ts';
|
|
2
|
+
import { wireCreate } from './wire-create.ts';
|
|
3
|
+
import { wireDrag } from './wire-drag.ts';
|
|
4
|
+
import { wireMinimap } from './wire-minimap.ts';
|
|
5
|
+
import { wireMoveMenu } from './wire-move-menu.ts';
|
|
6
|
+
import { wireScrollRow } from './wire-scroll-row.ts';
|
|
7
|
+
|
|
8
|
+
/** Boards already wired: an Astro script and a React hook may both see one. */
|
|
9
|
+
const wired = new WeakSet<HTMLElement>();
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Wires every Kanban under root, each once: the stage tabs of a phone, the fading sides, the
|
|
13
|
+
* miniature, the move button, holding and dragging, and the new-card button. Any change to the cards,
|
|
14
|
+
* by this board or by the page, keeps counts and empty folders in line.
|
|
15
|
+
*/
|
|
16
|
+
export function wireKanbans(window: Window, root: ParentNode): void {
|
|
17
|
+
const self = (root as Element).matches?.('.feitio-kanban') ? [root as HTMLElement] : [];
|
|
18
|
+
const boards: HTMLElement[] = [...self, ...root.querySelectorAll<HTMLElement>('.feitio-kanban')];
|
|
19
|
+
for (const board of boards) {
|
|
20
|
+
if (wired.has(board)) continue;
|
|
21
|
+
wired.add(board);
|
|
22
|
+
board.querySelector('.tabs')?.addEventListener('click', (event) => {
|
|
23
|
+
const tab = (event.target as Element).closest<HTMLElement>('[data-col]');
|
|
24
|
+
if (tab?.dataset.col) chooseStage(board, tab.dataset.col);
|
|
25
|
+
});
|
|
26
|
+
for (const box of board.querySelectorAll<HTMLElement>(':scope > .feitio-kanban-scroll'))
|
|
27
|
+
wireScrollRow(box);
|
|
28
|
+
const rebuild = wireMinimap(window, board);
|
|
29
|
+
wireMoveMenu(window, board);
|
|
30
|
+
wireDrag(window, board);
|
|
31
|
+
wireCreate(window, board);
|
|
32
|
+
board.addEventListener('feitio-kanban-move', () => window.requestAnimationFrame(rebuild));
|
|
33
|
+
board.addEventListener('feitio-kanban-create', () => window.requestAnimationFrame(rebuild));
|
|
34
|
+
syncKanban(board);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { minimapScale, minimapScroll } from '../calculations/kanban-rules.ts';
|
|
2
|
+
|
|
3
|
+
const WIDTH = 176;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The board in miniature, at the bottom right, while the columns do not fit: a rectangle per column
|
|
7
|
+
* (dashed when empty), a line per card, the part in view in orange. A touch or a drag on it takes the
|
|
8
|
+
* board there. Rebuilt when the board changes size or its cards change.
|
|
9
|
+
*/
|
|
10
|
+
export function wireMinimap(window: Window, board: HTMLElement): () => void {
|
|
11
|
+
const map = board.querySelector<HTMLElement>(':scope > .minimap');
|
|
12
|
+
const cols = board.querySelector<HTMLElement>('.cols');
|
|
13
|
+
if (!map || !cols) return () => {};
|
|
14
|
+
let scale = 0;
|
|
15
|
+
const pad = 4;
|
|
16
|
+
const place = (): void => {
|
|
17
|
+
const view = map.querySelector<HTMLElement>('.view');
|
|
18
|
+
if (!view) return;
|
|
19
|
+
view.style.insetInlineStart = `${pad + cols.scrollLeft * scale}px`;
|
|
20
|
+
view.style.inlineSize = `${cols.clientWidth * scale}px`;
|
|
21
|
+
};
|
|
22
|
+
const build = (): void => {
|
|
23
|
+
const wide = window.getComputedStyle(cols).display === 'flex';
|
|
24
|
+
const over = wide && cols.scrollWidth > cols.clientWidth + 8;
|
|
25
|
+
map.hidden = !over;
|
|
26
|
+
if (!over) return;
|
|
27
|
+
scale = minimapScale(WIDTH, cols.scrollWidth);
|
|
28
|
+
map.replaceChildren();
|
|
29
|
+
for (const col of cols.querySelectorAll<HTMLElement>('.col')) {
|
|
30
|
+
const m = window.document.createElement('div');
|
|
31
|
+
m.className = 'm-col';
|
|
32
|
+
m.style.inlineSize = `${Math.max(6, col.offsetWidth * scale)}px`;
|
|
33
|
+
if (col.hasAttribute('data-empty')) m.dataset.empty = '';
|
|
34
|
+
for (const _ of col.querySelectorAll(':scope > .feitio-kanban-card:not(.draft)')) {
|
|
35
|
+
const c = window.document.createElement('i');
|
|
36
|
+
c.className = 'm-card';
|
|
37
|
+
m.append(c);
|
|
38
|
+
}
|
|
39
|
+
map.append(m);
|
|
40
|
+
}
|
|
41
|
+
const view = window.document.createElement('span');
|
|
42
|
+
view.className = 'view';
|
|
43
|
+
map.append(view);
|
|
44
|
+
place();
|
|
45
|
+
};
|
|
46
|
+
const go = (event: PointerEvent | MouseEvent): void => {
|
|
47
|
+
const r = map.getBoundingClientRect();
|
|
48
|
+
cols.scrollLeft = minimapScroll(event.clientX - r.left - pad, scale, cols.clientWidth);
|
|
49
|
+
};
|
|
50
|
+
let dragging = false;
|
|
51
|
+
map.addEventListener('pointerdown', (event) => {
|
|
52
|
+
dragging = true;
|
|
53
|
+
map.setPointerCapture?.(event.pointerId);
|
|
54
|
+
go(event);
|
|
55
|
+
});
|
|
56
|
+
map.addEventListener('pointermove', (event) => {
|
|
57
|
+
if (dragging) go(event);
|
|
58
|
+
});
|
|
59
|
+
map.addEventListener('pointerup', () => {
|
|
60
|
+
dragging = false;
|
|
61
|
+
});
|
|
62
|
+
cols.addEventListener('scroll', place, { passive: true });
|
|
63
|
+
if (typeof ResizeObserver === 'function') new ResizeObserver(build).observe(cols);
|
|
64
|
+
build();
|
|
65
|
+
return build;
|
|
66
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { menuPosition, nextMenuIndex } from '../../menu/calculations/menu-focus.ts';
|
|
2
|
+
import { targetsOf } from '../calculations/kanban-rules.ts';
|
|
3
|
+
import { moveCard } from './move-card.ts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The card's move button: a Menu list with only the columns the card may go to. The way to move on a
|
|
7
|
+
* phone and with the keyboard. After the move, the focus stays on the card's button.
|
|
8
|
+
*/
|
|
9
|
+
export function wireMoveMenu(window: Window, board: HTMLElement): void {
|
|
10
|
+
const doc = window.document;
|
|
11
|
+
let list: HTMLElement | undefined;
|
|
12
|
+
let opener: HTMLElement | undefined;
|
|
13
|
+
// The list is made on the first use, at the end of the page: nothing is added to the board itself.
|
|
14
|
+
const make = (): HTMLElement => {
|
|
15
|
+
const ul = doc.createElement('ul');
|
|
16
|
+
ul.className = 'feitio-menu-list feitio-kanban-menu';
|
|
17
|
+
ul.id = `${board.id || 'kanban'}-move`;
|
|
18
|
+
ul.setAttribute('popover', 'auto');
|
|
19
|
+
doc.body.append(ul);
|
|
20
|
+
ul.addEventListener('toggle', (event) => {
|
|
21
|
+
if ((event as ToggleEvent).newState === 'closed')
|
|
22
|
+
opener?.setAttribute('aria-expanded', 'false');
|
|
23
|
+
});
|
|
24
|
+
ul.addEventListener('keydown', (event) => {
|
|
25
|
+
const all = items();
|
|
26
|
+
const next = nextMenuIndex(
|
|
27
|
+
event.key,
|
|
28
|
+
all.indexOf(doc.activeElement as HTMLElement),
|
|
29
|
+
all.length,
|
|
30
|
+
);
|
|
31
|
+
if (next === undefined) return;
|
|
32
|
+
event.preventDefault();
|
|
33
|
+
all[next]?.focus();
|
|
34
|
+
});
|
|
35
|
+
return ul;
|
|
36
|
+
};
|
|
37
|
+
const items = (): HTMLElement[] => [...(list?.querySelectorAll<HTMLElement>('button') ?? [])];
|
|
38
|
+
board.addEventListener('click', (event) => {
|
|
39
|
+
const button = (event.target as Element).closest<HTMLElement>('.feitio-kanban-card > .move');
|
|
40
|
+
if (!button || !board.contains(button)) return;
|
|
41
|
+
const card = button.parentElement as HTMLElement;
|
|
42
|
+
opener = button;
|
|
43
|
+
list ??= make();
|
|
44
|
+
const menu = list;
|
|
45
|
+
menu.replaceChildren();
|
|
46
|
+
const head = doc.createElement('li');
|
|
47
|
+
head.className = 'head';
|
|
48
|
+
head.setAttribute('role', 'presentation');
|
|
49
|
+
head.textContent = board.dataset.moveLabel ?? 'Move to';
|
|
50
|
+
menu.append(head);
|
|
51
|
+
for (const to of targetsOf(card.dataset.to)) {
|
|
52
|
+
const title = board.querySelector(`.col[data-col="${to}"] .col-head h3`)?.textContent?.trim();
|
|
53
|
+
if (!title) continue;
|
|
54
|
+
const li = doc.createElement('li');
|
|
55
|
+
const choice = doc.createElement('button');
|
|
56
|
+
choice.type = 'button';
|
|
57
|
+
choice.textContent = title;
|
|
58
|
+
choice.addEventListener('click', () => {
|
|
59
|
+
menu.hidePopover?.();
|
|
60
|
+
if (moveCard(window, board, card, to))
|
|
61
|
+
window.requestAnimationFrame(() =>
|
|
62
|
+
card.querySelector<HTMLElement>(':scope > .move')?.focus({ preventScroll: true }),
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
li.append(choice);
|
|
66
|
+
menu.append(li);
|
|
67
|
+
}
|
|
68
|
+
menu.showPopover?.();
|
|
69
|
+
button.setAttribute('aria-expanded', 'true');
|
|
70
|
+
const place = menuPosition(
|
|
71
|
+
button.getBoundingClientRect(),
|
|
72
|
+
menu.getBoundingClientRect(),
|
|
73
|
+
{ width: window.innerWidth, height: window.innerHeight },
|
|
74
|
+
8,
|
|
75
|
+
);
|
|
76
|
+
menu.style.left = `${place.left}px`;
|
|
77
|
+
menu.style.top = `${place.top}px`;
|
|
78
|
+
items()[0]?.focus();
|
|
79
|
+
});
|
|
80
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { edgesVisible } from '../calculations/kanban-motion.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A row that scrolls sideways (the tabs, the columns): its sides fade while there is more to see. The
|
|
5
|
+
* tab row also scrolls with the mouse wheel and by dragging it with the mouse; touch scrolls by itself.
|
|
6
|
+
*/
|
|
7
|
+
export function wireScrollRow(box: HTMLElement): void {
|
|
8
|
+
const row = box.querySelector<HTMLElement>(':scope > .tabs, :scope > .cols');
|
|
9
|
+
const start = box.querySelector<HTMLElement>(':scope > .edge.start');
|
|
10
|
+
const end = box.querySelector<HTMLElement>(':scope > .edge.end');
|
|
11
|
+
if (!row) return;
|
|
12
|
+
const sync = (): void => {
|
|
13
|
+
const seen = edgesVisible(row.scrollLeft, row.clientWidth, row.scrollWidth);
|
|
14
|
+
start?.toggleAttribute('data-visible', seen.start);
|
|
15
|
+
end?.toggleAttribute('data-visible', seen.end);
|
|
16
|
+
};
|
|
17
|
+
row.addEventListener('scroll', sync, { passive: true });
|
|
18
|
+
if (typeof ResizeObserver === 'function') new ResizeObserver(sync).observe(row);
|
|
19
|
+
sync();
|
|
20
|
+
if (!row.classList.contains('tabs')) return;
|
|
21
|
+
row.addEventListener(
|
|
22
|
+
'wheel',
|
|
23
|
+
(event) => {
|
|
24
|
+
if (row.scrollWidth <= row.clientWidth || Math.abs(event.deltaX) > Math.abs(event.deltaY))
|
|
25
|
+
return;
|
|
26
|
+
event.preventDefault();
|
|
27
|
+
row.scrollLeft += event.deltaY;
|
|
28
|
+
},
|
|
29
|
+
{ passive: false },
|
|
30
|
+
);
|
|
31
|
+
let down: { x: number; scroll: number; moved: boolean; id: number } | undefined;
|
|
32
|
+
row.addEventListener('pointerdown', (event) => {
|
|
33
|
+
if (event.pointerType === 'mouse')
|
|
34
|
+
down = { x: event.clientX, scroll: row.scrollLeft, moved: false, id: event.pointerId };
|
|
35
|
+
});
|
|
36
|
+
row.addEventListener('pointermove', (event) => {
|
|
37
|
+
if (!down) return;
|
|
38
|
+
const d = event.clientX - down.x;
|
|
39
|
+
if (!down.moved && Math.abs(d) > 4) {
|
|
40
|
+
down.moved = true;
|
|
41
|
+
row.setPointerCapture?.(down.id);
|
|
42
|
+
}
|
|
43
|
+
if (down.moved) row.scrollLeft = down.scroll - d;
|
|
44
|
+
});
|
|
45
|
+
row.addEventListener('pointerup', () => {
|
|
46
|
+
// A drag is not a click on the tab it ended on.
|
|
47
|
+
if (down?.moved)
|
|
48
|
+
row.addEventListener(
|
|
49
|
+
'click',
|
|
50
|
+
(e) => {
|
|
51
|
+
e.stopPropagation();
|
|
52
|
+
e.preventDefault();
|
|
53
|
+
},
|
|
54
|
+
{ capture: true, once: true },
|
|
55
|
+
);
|
|
56
|
+
down = undefined;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/** A spring: how stiff it is and how much it brakes. */
|
|
2
|
+
export interface Spring {
|
|
3
|
+
stiffness: number;
|
|
4
|
+
damping: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** The card that flies to another column: it passes the mark a little and comes back. */
|
|
8
|
+
export const FLY: Spring = { stiffness: 170, damping: 14 };
|
|
9
|
+
/** The cards that make room: firm, no bounce to speak of. */
|
|
10
|
+
export const SETTLE: Spring = { stiffness: 230, damping: 24 };
|
|
11
|
+
/** The tab that receives a card on a phone: a short hop. */
|
|
12
|
+
export const HOP: Spring = { stiffness: 320, damping: 11 };
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Where a mass on a spring is, frame by frame at 60 per second, from 0 until it rests at 1 (passing
|
|
16
|
+
* it a little when the spring is loose). The last frame is always exactly 1.
|
|
17
|
+
*/
|
|
18
|
+
export function springCurve(spring: Spring, maxFrames = 240): number[] {
|
|
19
|
+
let x = 0;
|
|
20
|
+
let v = 0;
|
|
21
|
+
const out = [0];
|
|
22
|
+
const dt = 1 / 60;
|
|
23
|
+
for (let i = 0; i < maxFrames; i++) {
|
|
24
|
+
v += (-spring.stiffness * (x - 1) - spring.damping * v) * dt;
|
|
25
|
+
x += v * dt;
|
|
26
|
+
out.push(x);
|
|
27
|
+
if (Math.abs(x - 1) < 0.002 && Math.abs(v) < 0.01) break;
|
|
28
|
+
}
|
|
29
|
+
out[out.length - 1] = 1;
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** How long a curve of springCurve lasts, in milliseconds. */
|
|
34
|
+
export function curveDuration(curve: number[]): number {
|
|
35
|
+
return (curve.length * 1000) / 60;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The card in the hand: where it is, how fast it goes, and how far it leans. */
|
|
39
|
+
export interface Follow {
|
|
40
|
+
x: number;
|
|
41
|
+
y: number;
|
|
42
|
+
vx: number;
|
|
43
|
+
vy: number;
|
|
44
|
+
tilt: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* One frame of the card following the pointer: it is pulled toward the target by a spring, so it lags
|
|
49
|
+
* a little, leans toward where it is going (up to 14 degrees) and sways when the pointer stops.
|
|
50
|
+
*/
|
|
51
|
+
export function followStep(state: Follow, targetX: number, targetY: number): Follow {
|
|
52
|
+
const vx = (state.vx + (targetX - state.x) * 0.3) * 0.62;
|
|
53
|
+
const vy = (state.vy + (targetY - state.y) * 0.3) * 0.62;
|
|
54
|
+
const lean = Math.max(-14, Math.min(14, vx * 1.2));
|
|
55
|
+
return {
|
|
56
|
+
x: state.x + vx,
|
|
57
|
+
y: state.y + vy,
|
|
58
|
+
vx,
|
|
59
|
+
vy,
|
|
60
|
+
tilt: state.tilt + (lean - state.tilt) * 0.18,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** How far a card leans on its way to another column: with the distance, up to 9 degrees. */
|
|
65
|
+
export function flightTilt(dx: number): number {
|
|
66
|
+
return Math.max(-9, Math.min(9, dx / 35));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Whether the sides fade: there is more than a few pixels to scroll on that side. */
|
|
70
|
+
export function edgesVisible(
|
|
71
|
+
scrollLeft: number,
|
|
72
|
+
clientWidth: number,
|
|
73
|
+
scrollWidth: number,
|
|
74
|
+
): { start: boolean; end: boolean } {
|
|
75
|
+
return { start: scrollLeft > 8, end: scrollLeft + clientWidth < scrollWidth - 8 };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* How fast the board scrolls by itself while a card is held near its sides: nothing out of the zone,
|
|
80
|
+
* up to max pixels a frame at the very edge.
|
|
81
|
+
*/
|
|
82
|
+
export function autoScrollSpeed(
|
|
83
|
+
pointerX: number,
|
|
84
|
+
left: number,
|
|
85
|
+
right: number,
|
|
86
|
+
zone = 90,
|
|
87
|
+
max = 16,
|
|
88
|
+
): number {
|
|
89
|
+
if (pointerX < left + zone) return -((left + zone - pointerX) / zone) * max;
|
|
90
|
+
if (pointerX > right - zone) return ((pointerX - (right - zone)) / zone) * max;
|
|
91
|
+
return 0;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Where a card dropped at height y goes among cards whose middles are given: before the first one below it. */
|
|
95
|
+
export function insertionIndex(middles: number[], y: number): number {
|
|
96
|
+
const i = middles.findIndex((m) => y < m);
|
|
97
|
+
return i === -1 ? middles.length : i;
|
|
98
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** The columns a card may go to, as its data-to lists them (separated by spaces). */
|
|
2
|
+
export function targetsOf(attribute: string | null | undefined): string[] {
|
|
3
|
+
return (attribute ?? '').split(/\s+/).filter(Boolean);
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/** Whether a card may move from one column to another: never to where it is, only where it lists. */
|
|
7
|
+
export function canMove(targets: string[], from: string, to: string): boolean {
|
|
8
|
+
return to !== from && targets.includes(to);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The scale of the miniature: its width over the board's whole scroll width. */
|
|
12
|
+
export function minimapScale(mapWidth: number, scrollWidth: number): number {
|
|
13
|
+
return scrollWidth > 0 ? mapWidth / scrollWidth : 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Where the board scrolls to when the miniature is touched at x: that point in the middle of the view. */
|
|
17
|
+
export function minimapScroll(x: number, scale: number, clientWidth: number): number {
|
|
18
|
+
return scale > 0 ? Math.max(0, x / scale - clientWidth / 2) : 0;
|
|
19
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { moveCard } from './actions/move-card.ts';
|
|
2
|
+
import { syncKanban } from './actions/sync-kanban.ts';
|
|
3
|
+
import { wireKanbans } from './actions/wire-kanbans.ts';
|
|
4
|
+
|
|
5
|
+
export type { KanbanMoveDetail } from './actions/move-card.ts';
|
|
6
|
+
export type { KanbanCreateDetail } from './actions/wire-create.ts';
|
|
7
|
+
export { canMove, targetsOf } from './calculations/kanban-rules.ts';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The Kanban boards of a page, or of a part of it: wires them, and hands back move (the same move a
|
|
11
|
+
* drag or the move button makes, for the page to call) and sync (after the page changed the cards).
|
|
12
|
+
*/
|
|
13
|
+
export function kanbans(
|
|
14
|
+
window: Window,
|
|
15
|
+
root: ParentNode = window.document,
|
|
16
|
+
): {
|
|
17
|
+
move: (card: HTMLElement, to: string, index?: number) => boolean;
|
|
18
|
+
sync: (board: HTMLElement) => void;
|
|
19
|
+
} {
|
|
20
|
+
wireKanbans(window, root);
|
|
21
|
+
return {
|
|
22
|
+
move: (card, to, index) => {
|
|
23
|
+
const board = card.closest<HTMLElement>('.feitio-kanban');
|
|
24
|
+
return board ? moveCard(window, board, card, to, index) : false;
|
|
25
|
+
},
|
|
26
|
+
sync: syncKanban,
|
|
27
|
+
};
|
|
28
|
+
}
|