@feitio/vanilla 0.2.0 → 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.
@@ -1,6 +1,7 @@
1
1
  /** Feitio's component behaviors in plain JavaScript. Each one also has its own entry point. */
2
2
  export { dialogs } from './dialog/index.js';
3
3
  export { forms } from './forms/index.js';
4
+ export { kanbans } from './kanban/index.js';
4
5
  export { menus } from './menu/index.js';
5
6
  export { notices } from './notice/index.js';
6
7
  export { scrolls } from './scroll/index.js';
@@ -0,0 +1,33 @@
1
+ import { type Spring } from '../calculations/kanban-motion.js';
2
+ /** Where everything was before a change: each card's and each new-card button's box, each column's width. */
3
+ export interface KanbanSnapshot {
4
+ rects: Map<string, {
5
+ left: number;
6
+ top: number;
7
+ width: number;
8
+ }>;
9
+ widths: Map<string, number>;
10
+ clones: Map<string, HTMLElement>;
11
+ }
12
+ /** What moved and how: the card, where it went, and where the hand let it go (when it was dragged). */
13
+ export interface KanbanMotion {
14
+ id?: string;
15
+ to?: string;
16
+ from?: {
17
+ x: number;
18
+ y: number;
19
+ tilt: number;
20
+ };
21
+ }
22
+ /** Takes the picture of before. The card that is about to move keeps a copy, for a flight to a hidden tab. */
23
+ export declare function snapshot(board: HTMLElement, moving?: string): KanbanSnapshot;
24
+ /** Plays a spring on an element, frame by frame. Pages without the Web Animations API skip it. */
25
+ export declare function play(el: HTMLElement, spring: Spring, frame: (p: number) => Keyframe): Animation | undefined;
26
+ /** Whether the page asked for less motion. */
27
+ export declare function reduced(window: Window): boolean;
28
+ /**
29
+ * Animates from the picture of before to where everything is now: columns that filled or emptied
30
+ * change width on a spring, the cards around make room, and the card that moved flies to its place,
31
+ * leaning, and settles. On a phone, a card that went to a hidden stage flies to its tab, which hops.
32
+ */
33
+ export declare function animateFrom(window: Window, board: HTMLElement, before: KanbanSnapshot, motion: KanbanMotion): void;
@@ -0,0 +1,19 @@
1
+ import { type KanbanSnapshot } from './kanban-flip.js';
2
+ /** What the feitio-kanban-move event says: which card, from where, to where, and at which place. */
3
+ export interface KanbanMoveDetail {
4
+ card: HTMLElement;
5
+ id: string;
6
+ from: string;
7
+ to: string;
8
+ index: number;
9
+ }
10
+ /**
11
+ * Asks to move a card: the board sends feitio-kanban-move (cancelable). Unless the page cancels it to
12
+ * move the card itself (a React state, a server), the card goes to the column, at that place. On the
13
+ * next frame everything animates from where it was, whoever moved it.
14
+ */
15
+ export declare function moveCard(window: Window, board: HTMLElement, card: HTMLElement, to: string, index?: number, before?: KanbanSnapshot, from?: {
16
+ x: number;
17
+ y: number;
18
+ tilt: number;
19
+ }): boolean;
@@ -0,0 +1,4 @@
1
+ /** Brings the counts, the dots, the empty folders and their sentence in line with the cards there. */
2
+ export declare function syncKanban(board: HTMLElement): void;
3
+ /** Shows one stage on a phone: its column, and its tab pressed. */
4
+ export declare function chooseStage(board: HTMLElement, id: string): void;
@@ -0,0 +1,12 @@
1
+ /** What the feitio-kanban-create event says: the column, its element, and what was written. */
2
+ export interface KanbanCreateDetail {
3
+ column: string;
4
+ element: HTMLElement;
5
+ text: string;
6
+ }
7
+ /**
8
+ * The new-card button of a column that allows it: a card with a field takes its place, with Save and
9
+ * Cancel (Esc cancels too). Saving sends feitio-kanban-create; the page adds the card it wants (the
10
+ * content is the page's), and it arrives on a spring.
11
+ */
12
+ export declare function wireCreate(window: Window, board: HTMLElement): void;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Holding and dragging a card, on a wide board, with a mouse or a pen (on a phone, the move button
3
+ * does it). The card follows the hand on a spring: it lags a little, leans toward where it goes and
4
+ * sways when the hand stops. The folder under it opens a place where it would land and the cards
5
+ * there make room; folders that do not take it fade. Near the sides the board scrolls by itself.
6
+ * Letting go moves it from where it was let go; Esc, or letting go outside, takes it home.
7
+ */
8
+ export declare function wireDrag(window: Window, board: HTMLElement): void;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Wires every Kanban under root, each once: the stage tabs of a phone, the fading sides, the
3
+ * miniature, the move button, holding and dragging, and the new-card button. Any change to the cards,
4
+ * by this board or by the page, keeps counts and empty folders in line.
5
+ */
6
+ export declare function wireKanbans(window: Window, root: ParentNode): void;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * The board in miniature, at the bottom right, while the columns do not fit: a rectangle per column
3
+ * (dashed when empty), a line per card, the part in view in orange. A touch or a drag on it takes the
4
+ * board there. Rebuilt when the board changes size or its cards change.
5
+ */
6
+ export declare function wireMinimap(window: Window, board: HTMLElement): () => void;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The card's move button: a Menu list with only the columns the card may go to. The way to move on a
3
+ * phone and with the keyboard. After the move, the focus stays on the card's button.
4
+ */
5
+ export declare function wireMoveMenu(window: Window, board: HTMLElement): void;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * A row that scrolls sideways (the tabs, the columns): its sides fade while there is more to see. The
3
+ * tab row also scrolls with the mouse wheel and by dragging it with the mouse; touch scrolls by itself.
4
+ */
5
+ export declare function wireScrollRow(box: HTMLElement): void;
@@ -0,0 +1,45 @@
1
+ /** A spring: how stiff it is and how much it brakes. */
2
+ export interface Spring {
3
+ stiffness: number;
4
+ damping: number;
5
+ }
6
+ /** The card that flies to another column: it passes the mark a little and comes back. */
7
+ export declare const FLY: Spring;
8
+ /** The cards that make room: firm, no bounce to speak of. */
9
+ export declare const SETTLE: Spring;
10
+ /** The tab that receives a card on a phone: a short hop. */
11
+ export declare const HOP: Spring;
12
+ /**
13
+ * Where a mass on a spring is, frame by frame at 60 per second, from 0 until it rests at 1 (passing
14
+ * it a little when the spring is loose). The last frame is always exactly 1.
15
+ */
16
+ export declare function springCurve(spring: Spring, maxFrames?: number): number[];
17
+ /** How long a curve of springCurve lasts, in milliseconds. */
18
+ export declare function curveDuration(curve: number[]): number;
19
+ /** The card in the hand: where it is, how fast it goes, and how far it leans. */
20
+ export interface Follow {
21
+ x: number;
22
+ y: number;
23
+ vx: number;
24
+ vy: number;
25
+ tilt: number;
26
+ }
27
+ /**
28
+ * One frame of the card following the pointer: it is pulled toward the target by a spring, so it lags
29
+ * a little, leans toward where it is going (up to 14 degrees) and sways when the pointer stops.
30
+ */
31
+ export declare function followStep(state: Follow, targetX: number, targetY: number): Follow;
32
+ /** How far a card leans on its way to another column: with the distance, up to 9 degrees. */
33
+ export declare function flightTilt(dx: number): number;
34
+ /** Whether the sides fade: there is more than a few pixels to scroll on that side. */
35
+ export declare function edgesVisible(scrollLeft: number, clientWidth: number, scrollWidth: number): {
36
+ start: boolean;
37
+ end: boolean;
38
+ };
39
+ /**
40
+ * How fast the board scrolls by itself while a card is held near its sides: nothing out of the zone,
41
+ * up to max pixels a frame at the very edge.
42
+ */
43
+ export declare function autoScrollSpeed(pointerX: number, left: number, right: number, zone?: number, max?: number): number;
44
+ /** Where a card dropped at height y goes among cards whose middles are given: before the first one below it. */
45
+ export declare function insertionIndex(middles: number[], y: number): number;
@@ -0,0 +1,8 @@
1
+ /** The columns a card may go to, as its data-to lists them (separated by spaces). */
2
+ export declare function targetsOf(attribute: string | null | undefined): string[];
3
+ /** Whether a card may move from one column to another: never to where it is, only where it lists. */
4
+ export declare function canMove(targets: string[], from: string, to: string): boolean;
5
+ /** The scale of the miniature: its width over the board's whole scroll width. */
6
+ export declare function minimapScale(mapWidth: number, scrollWidth: number): number;
7
+ /** Where the board scrolls to when the miniature is touched at x: that point in the middle of the view. */
8
+ export declare function minimapScroll(x: number, scale: number, clientWidth: number): number;
@@ -0,0 +1,11 @@
1
+ export type { KanbanMoveDetail } from './actions/move-card.js';
2
+ export type { KanbanCreateDetail } from './actions/wire-create.js';
3
+ export { canMove, targetsOf } from './calculations/kanban-rules.js';
4
+ /**
5
+ * The Kanban boards of a page, or of a part of it: wires them, and hands back move (the same move a
6
+ * drag or the move button makes, for the page to call) and sync (after the page changed the cards).
7
+ */
8
+ export declare function kanbans(window: Window, root?: ParentNode): {
9
+ move: (card: HTMLElement, to: string, index?: number) => boolean;
10
+ sync: (board: HTMLElement) => void;
11
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@feitio/vanilla",
3
- "version": "0.2.0",
4
- "description": "Feitio's component behaviors in plain JavaScript, for any HTML: Notice, Menu, Dialog, form controls, scroll and status.",
3
+ "version": "0.3.0",
4
+ "description": "Feitio's component behaviors in plain JavaScript, for any HTML: Notice, Menu, Dialog, form controls, scroll, status and Kanban.",
5
5
  "keywords": [
6
6
  "feitio",
7
7
  "design-system",
@@ -14,7 +14,7 @@
14
14
  "menu",
15
15
  "notification"
16
16
  ],
17
- "homepage": "https://made-by-maid.github.io/feitio/",
17
+ "homepage": "https://feitio.bymaid.com/",
18
18
  "bugs": {
19
19
  "url": "https://github.com/Made-by-MAID/feitio/issues"
20
20
  },
@@ -64,14 +64,19 @@
64
64
  "types": "./dist/types/dialog/index.d.ts",
65
65
  "default": "./dist/dialog.js"
66
66
  },
67
- "./dialog/source": "./src/dialog/index.ts"
67
+ "./dialog/source": "./src/dialog/index.ts",
68
+ "./kanban": {
69
+ "types": "./dist/types/kanban/index.d.ts",
70
+ "default": "./dist/kanban.js"
71
+ },
72
+ "./kanban/source": "./src/kanban/index.ts"
68
73
  },
69
74
  "scripts": {
70
- "build": "rm -rf dist && bun build src/index.ts src/notice/index.ts src/menu/index.ts src/forms/index.ts src/status/index.ts src/scroll/index.ts src/dialog/index.ts --outdir dist --format esm --entry-naming [dir]/[name].[ext] --root src && bun run rename && tsc -p tsconfig.build.json && bun scripts/fix-declarations.ts",
71
- "rename": "mv dist/index.js dist/feitio.js && for m in notice menu forms status scroll dialog; do mv dist/$m/index.js dist/$m.js && rmdir dist/$m; done"
75
+ "build": "rm -rf dist && bun build src/index.ts src/notice/index.ts src/menu/index.ts src/forms/index.ts src/status/index.ts src/scroll/index.ts src/dialog/index.ts src/kanban/index.ts --outdir dist --format esm --entry-naming [dir]/[name].[ext] --root src && bun run rename && tsc -p tsconfig.build.json && bun scripts/fix-declarations.ts",
76
+ "rename": "mv dist/index.js dist/feitio.js && for m in notice menu forms status scroll dialog kanban; do mv dist/$m/index.js dist/$m.js && rmdir dist/$m; done"
72
77
  },
73
78
  "dependencies": {
74
- "@feitio/core": "0.2.0"
79
+ "@feitio/core": "0.3.0"
75
80
  },
76
81
  "publishConfig": {
77
82
  "access": "public"
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /** Feitio's component behaviors in plain JavaScript. Each one also has its own entry point. */
2
2
  export { dialogs } from './dialog/index.ts';
3
3
  export { forms } from './forms/index.ts';
4
+ export { kanbans } from './kanban/index.ts';
4
5
  export { menus } from './menu/index.ts';
5
6
  export { notices } from './notice/index.ts';
6
7
  export { scrolls } from './scroll/index.ts';
@@ -0,0 +1,172 @@
1
+ import {
2
+ curveDuration,
3
+ FLY,
4
+ flightTilt,
5
+ HOP,
6
+ SETTLE,
7
+ type Spring,
8
+ springCurve,
9
+ } from '../calculations/kanban-motion.ts';
10
+
11
+ /** Where everything was before a change: each card's and each new-card button's box, each column's width. */
12
+ export interface KanbanSnapshot {
13
+ rects: Map<string, { left: number; top: number; width: number }>;
14
+ widths: Map<string, number>;
15
+ clones: Map<string, HTMLElement>;
16
+ }
17
+
18
+ /** What moved and how: the card, where it went, and where the hand let it go (when it was dragged). */
19
+ export interface KanbanMotion {
20
+ id?: string;
21
+ to?: string;
22
+ from?: { x: number; y: number; tilt: number };
23
+ }
24
+
25
+ /** The key that finds a piece again after the change. */
26
+ function keyOf(el: HTMLElement): string | undefined {
27
+ if (el.classList.contains('feitio-kanban-card'))
28
+ return el.dataset.id ? `card:${el.dataset.id}` : undefined;
29
+ const col = el.closest<HTMLElement>('.col');
30
+ return col ? `add:${col.dataset.col}` : undefined;
31
+ }
32
+
33
+ function pieces(board: HTMLElement): HTMLElement[] {
34
+ return [
35
+ ...board.querySelectorAll<HTMLElement>('.col > .feitio-kanban-card:not(.draft), .col > .add'),
36
+ ];
37
+ }
38
+
39
+ /** Takes the picture of before. The card that is about to move keeps a copy, for a flight to a hidden tab. */
40
+ export function snapshot(board: HTMLElement, moving?: string): KanbanSnapshot {
41
+ const rects = new Map<string, { left: number; top: number; width: number }>();
42
+ const clones = new Map<string, HTMLElement>();
43
+ for (const el of pieces(board)) {
44
+ const key = keyOf(el);
45
+ const r = el.getBoundingClientRect();
46
+ if (key && r.width) rects.set(key, { left: r.left, top: r.top, width: r.width });
47
+ if (key && moving && key === `card:${moving}`)
48
+ clones.set(key, el.cloneNode(true) as HTMLElement);
49
+ }
50
+ const widths = new Map<string, number>();
51
+ for (const col of board.querySelectorAll<HTMLElement>('.col')) {
52
+ const w = col.getBoundingClientRect().width;
53
+ if (w) widths.set(col.dataset.col ?? '', w);
54
+ }
55
+ return { rects, widths, clones };
56
+ }
57
+
58
+ /** Plays a spring on an element, frame by frame. Pages without the Web Animations API skip it. */
59
+ export function play(
60
+ el: HTMLElement,
61
+ spring: Spring,
62
+ frame: (p: number) => Keyframe,
63
+ ): Animation | undefined {
64
+ if (typeof el.animate !== 'function') return undefined;
65
+ const curve = springCurve(spring);
66
+ return el.animate(curve.map(frame), { duration: curveDuration(curve), easing: 'linear' });
67
+ }
68
+
69
+ /** Whether the page asked for less motion. */
70
+ export function reduced(window: Window): boolean {
71
+ return window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false;
72
+ }
73
+
74
+ /**
75
+ * Animates from the picture of before to where everything is now: columns that filled or emptied
76
+ * change width on a spring, the cards around make room, and the card that moved flies to its place,
77
+ * leaning, and settles. On a phone, a card that went to a hidden stage flies to its tab, which hops.
78
+ */
79
+ export function animateFrom(
80
+ window: Window,
81
+ board: HTMLElement,
82
+ before: KanbanSnapshot,
83
+ motion: KanbanMotion,
84
+ ): void {
85
+ if (reduced(window)) return;
86
+ const after = new Map<string, [HTMLElement, DOMRect]>();
87
+ for (const el of pieces(board)) {
88
+ const key = keyOf(el);
89
+ const r = el.getBoundingClientRect();
90
+ if (key && r.width) after.set(key, [el, r]);
91
+ }
92
+ for (const col of board.querySelectorAll<HTMLElement>('.col')) {
93
+ const w0 = before.widths.get(col.dataset.col ?? '');
94
+ const w1 = col.getBoundingClientRect().width;
95
+ if (!w0 || !w1 || Math.abs(w0 - w1) < 1) continue;
96
+ play(col, SETTLE, (p) => ({
97
+ flexBasis: `${w0 + (w1 - w0) * p}px`,
98
+ flexGrow: 0,
99
+ flexShrink: 0,
100
+ }));
101
+ }
102
+ const mover = motion.id ? `card:${motion.id}` : undefined;
103
+ for (const [key, [el, r1]] of after) {
104
+ const isMover = key === mover;
105
+ let r0: { left: number; top: number } | undefined = before.rects.get(key);
106
+ if (isMover && motion.from) r0 = { left: motion.from.x, top: motion.from.y };
107
+ if (!r0) {
108
+ if (isMover)
109
+ play(el, FLY, (p) => ({
110
+ transform: `translateY(${-24 * (1 - p)}px) scale(${1 + 0.08 * (1 - p)})`,
111
+ opacity: Math.min(1, p * 2),
112
+ }));
113
+ continue;
114
+ }
115
+ const dx = r0.left - r1.left;
116
+ const dy = r0.top - r1.top;
117
+ if (Math.abs(dx) < 1 && Math.abs(dy) < 1) continue;
118
+ if (!isMover) {
119
+ play(el, SETTLE, (p) => ({ transform: `translate(${dx * (1 - p)}px, ${dy * (1 - p)}px)` }));
120
+ continue;
121
+ }
122
+ const tilt = motion.from ? motion.from.tilt : flightTilt(dx);
123
+ const lift = motion.from ? 0.04 : 0.06;
124
+ el.style.zIndex = '5';
125
+ const run = play(el, FLY, (p) => ({
126
+ transform: `translate(${dx * (1 - p)}px, ${dy * (1 - p)}px) rotate(${tilt * (1 - p)}deg) scale(${1 + lift * Math.max(0, 1 - p)})`,
127
+ }));
128
+ if (run) run.onfinish = () => el.style.removeProperty('z-index');
129
+ }
130
+ if (!mover) return;
131
+ const landed = after.get(mover);
132
+ const cols = board.querySelector<HTMLElement>('.cols');
133
+ // The stage it went to is out of view: the board scrolls there, and the card arrives with it.
134
+ if (landed && !motion.from && cols) {
135
+ const r = landed[1];
136
+ const box = cols.getBoundingClientRect();
137
+ if (r.right > box.right - 72)
138
+ cols.scrollBy({ left: r.right - box.right + 72, behavior: 'smooth' });
139
+ else if (r.left < box.left + 72)
140
+ cols.scrollBy({ left: r.left - box.left - 72, behavior: 'smooth' });
141
+ }
142
+ const r0 = before.rects.get(mover);
143
+ const clone = before.clones.get(mover);
144
+ const tab = board.querySelector<HTMLElement>(`.tabs [data-col="${motion.to}"]`);
145
+ if (landed || !r0 || !clone || !tab || typeof tab.animate !== 'function') return;
146
+ const tr = tab.getBoundingClientRect();
147
+ const ghost = window.document.createElement('div');
148
+ ghost.className = 'feitio-kanban-ghost';
149
+ ghost.setAttribute('aria-hidden', 'true');
150
+ Object.assign(ghost.style, { left: `${r0.left}px`, top: `${r0.top}px`, width: `${r0.width}px` });
151
+ clone.removeAttribute('id');
152
+ ghost.append(clone);
153
+ window.document.body.append(ghost);
154
+ const dx = tr.left + tr.width / 2 - (r0.left + r0.width / 2);
155
+ const dy = tr.top + tr.height / 2 - (r0.top + clone.getBoundingClientRect().height / 2);
156
+ const flight = ghost.animate(
157
+ [
158
+ { transform: 'translate(0, 0) scale(1) rotate(0deg)', opacity: 1 },
159
+ {
160
+ transform: `translate(${dx * 0.35}px, ${dy * 0.35 - 30}px) scale(0.7) rotate(-5deg)`,
161
+ opacity: 1,
162
+ offset: 0.45,
163
+ },
164
+ { transform: `translate(${dx}px, ${dy}px) scale(0.08) rotate(-14deg)`, opacity: 0.2 },
165
+ ],
166
+ { duration: 560, easing: 'cubic-bezier(0.45, 0, 0.8, 0.4)' },
167
+ );
168
+ flight.onfinish = () => {
169
+ ghost.remove();
170
+ play(tab, HOP, (p) => ({ transform: `scale(${1 + 0.4 * (1 - p)})` }));
171
+ };
172
+ }
@@ -0,0 +1,61 @@
1
+ import { canMove, targetsOf } from '../calculations/kanban-rules.ts';
2
+ import { animateFrom, type KanbanSnapshot, snapshot } from './kanban-flip.ts';
3
+ import { syncKanban } from './sync-kanban.ts';
4
+
5
+ /** What the feitio-kanban-move event says: which card, from where, to where, and at which place. */
6
+ export interface KanbanMoveDetail {
7
+ card: HTMLElement;
8
+ id: string;
9
+ from: string;
10
+ to: string;
11
+ index: number;
12
+ }
13
+
14
+ /**
15
+ * Asks to move a card: the board sends feitio-kanban-move (cancelable). Unless the page cancels it to
16
+ * move the card itself (a React state, a server), the card goes to the column, at that place. On the
17
+ * next frame everything animates from where it was, whoever moved it.
18
+ */
19
+ export function moveCard(
20
+ window: Window,
21
+ board: HTMLElement,
22
+ card: HTMLElement,
23
+ to: string,
24
+ index?: number,
25
+ before?: KanbanSnapshot,
26
+ from?: { x: number; y: number; tilt: number },
27
+ ): boolean {
28
+ const fromCol = card.closest<HTMLElement>('.col')?.dataset.col ?? '';
29
+ const id = card.dataset.id ?? '';
30
+ const target = board.querySelector<HTMLElement>(`.col[data-col="${to}"]`);
31
+ if (!target || !canMove(targetsOf(card.dataset.to), fromCol, to)) return false;
32
+ const cards = [
33
+ ...target.querySelectorAll<HTMLElement>(':scope > .feitio-kanban-card:not(.draft)'),
34
+ ];
35
+ const place = index === undefined ? cards.length : Math.min(index, cards.length);
36
+ const picture = before ?? snapshot(board, id);
37
+ if (!picture.clones.size) {
38
+ const own = snapshot(board, id).clones;
39
+ for (const [k, v] of own) picture.clones.set(k, v);
40
+ }
41
+ const detail: KanbanMoveDetail = { card, id, from: fromCol, to, index: place };
42
+ const event = new CustomEvent('feitio-kanban-move', {
43
+ bubbles: true,
44
+ cancelable: true,
45
+ detail,
46
+ });
47
+ const done = (): void => {
48
+ syncKanban(board);
49
+ animateFrom(window, board, picture, { id, to, from });
50
+ };
51
+ if (board.dispatchEvent(event)) {
52
+ const ref = cards[place] ?? target.querySelector(':scope > .add');
53
+ target.insertBefore(card, ref);
54
+ syncKanban(board);
55
+ window.requestAnimationFrame(done);
56
+ } else {
57
+ // The page moves it (a React state renders on its own schedule): wait one frame more.
58
+ window.requestAnimationFrame(() => window.requestAnimationFrame(done));
59
+ }
60
+ return true;
61
+ }
@@ -0,0 +1,31 @@
1
+ /** Writes a text only when it changed, so a node another library owns (React) is left alone. */
2
+ function setText(el: Element | null, text: string): void {
3
+ if (el && el.textContent !== text) el.textContent = text;
4
+ }
5
+
6
+ /** Brings the counts, the dots, the empty folders and their sentence in line with the cards there. */
7
+ export function syncKanban(board: HTMLElement): void {
8
+ for (const col of board.querySelectorAll<HTMLElement>('.col')) {
9
+ const n = col.querySelectorAll(':scope > .feitio-kanban-card:not(.draft)').length;
10
+ const id = col.dataset.col ?? '';
11
+ if (col.hasAttribute('data-empty') !== (n === 0)) col.toggleAttribute('data-empty', n === 0);
12
+ setText(col.querySelector('.col-head .n'), String(n));
13
+ const empty = col.querySelector<HTMLElement>(':scope > .empty');
14
+ if (empty && empty.hidden !== n > 0) empty.hidden = n > 0;
15
+ const tab = board.querySelector<HTMLElement>(`.tabs [data-col="${id}"]`);
16
+ if (!tab) continue;
17
+ if (tab.hasAttribute('data-has') !== n > 0) tab.toggleAttribute('data-has', n > 0);
18
+ setText(tab.querySelector('.n'), String(n));
19
+ }
20
+ }
21
+
22
+ /** Shows one stage on a phone: its column, and its tab pressed. */
23
+ export function chooseStage(board: HTMLElement, id: string): void {
24
+ for (const col of board.querySelectorAll<HTMLElement>('.col')) {
25
+ const on = col.dataset.col === id;
26
+ if (col.hasAttribute('data-current') !== on) col.toggleAttribute('data-current', on);
27
+ }
28
+ for (const tab of board.querySelectorAll<HTMLElement>('.tabs [data-col]')) {
29
+ tab.setAttribute('aria-pressed', String(tab.dataset.col === id));
30
+ }
31
+ }
@@ -0,0 +1,84 @@
1
+ import { animateFrom, snapshot } from './kanban-flip.ts';
2
+ import { syncKanban } from './sync-kanban.ts';
3
+
4
+ /** What the feitio-kanban-create event says: the column, its element, and what was written. */
5
+ export interface KanbanCreateDetail {
6
+ column: string;
7
+ element: HTMLElement;
8
+ text: string;
9
+ }
10
+
11
+ let drafts = 0;
12
+
13
+ /**
14
+ * The new-card button of a column that allows it: a card with a field takes its place, with Save and
15
+ * Cancel (Esc cancels too). Saving sends feitio-kanban-create; the page adds the card it wants (the
16
+ * content is the page's), and it arrives on a spring.
17
+ */
18
+ export function wireCreate(window: Window, board: HTMLElement): void {
19
+ const doc = window.document;
20
+ board.addEventListener('click', (event) => {
21
+ const add = (event.target as Element).closest<HTMLElement>('.col > .add');
22
+ if (!add || !board.contains(add)) return;
23
+ const col = add.parentElement as HTMLElement;
24
+ const id = `${board.id || 'kanban'}-new-${++drafts}`;
25
+ const form = doc.createElement('form');
26
+ form.className = 'feitio-kanban-card draft feitio-place';
27
+ const label = doc.createElement('label');
28
+ label.htmlFor = id;
29
+ label.textContent = add.dataset.field ?? '';
30
+ const field = doc.createElement('textarea');
31
+ field.className = 'feitio-textarea';
32
+ field.id = id;
33
+ field.required = true;
34
+ field.rows = 2;
35
+ const actions = doc.createElement('div');
36
+ actions.className = 'actions';
37
+ const save = doc.createElement('button');
38
+ save.type = 'submit';
39
+ save.className = 'feitio-button';
40
+ save.dataset.variant = 'primary';
41
+ save.dataset.size = 'small';
42
+ save.textContent = add.dataset.save ?? 'Save';
43
+ const cancel = doc.createElement('button');
44
+ cancel.type = 'button';
45
+ cancel.className = 'feitio-button';
46
+ cancel.dataset.size = 'small';
47
+ cancel.textContent = add.dataset.cancel ?? 'Cancel';
48
+ actions.append(save, cancel);
49
+ form.append(label, field, actions);
50
+ add.hidden = true;
51
+ col.insertBefore(form, add);
52
+ field.focus();
53
+ const close = (): void => {
54
+ form.remove();
55
+ add.hidden = false;
56
+ add.focus();
57
+ };
58
+ cancel.addEventListener('click', close);
59
+ form.addEventListener('keydown', (e) => {
60
+ if (e.key === 'Escape') close();
61
+ });
62
+ form.addEventListener('submit', (e) => {
63
+ e.preventDefault();
64
+ const text = field.value.trim();
65
+ if (!text) return field.focus();
66
+ const before = snapshot(board);
67
+ const known = new Set(
68
+ [...col.querySelectorAll<HTMLElement>(':scope > .feitio-kanban-card')].map(
69
+ (c) => c.dataset.id,
70
+ ),
71
+ );
72
+ const detail: KanbanCreateDetail = { column: col.dataset.col ?? '', element: col, text };
73
+ board.dispatchEvent(new CustomEvent('feitio-kanban-create', { bubbles: true, detail }));
74
+ close();
75
+ window.requestAnimationFrame(() => {
76
+ syncKanban(board);
77
+ const fresh = [
78
+ ...col.querySelectorAll<HTMLElement>(':scope > .feitio-kanban-card:not(.draft)'),
79
+ ].find((c) => !known.has(c.dataset.id));
80
+ animateFrom(window, board, before, { id: fresh?.dataset.id });
81
+ });
82
+ });
83
+ });
84
+ }