@slotform/react-ui 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +44 -2
- package/dist/density.d.ts +4 -0
- package/dist/density.d.ts.map +1 -0
- package/dist/density.js +8 -0
- package/dist/density.js.map +1 -0
- package/dist/index.d.ts +400 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +388 -0
- package/dist/index.js.map +1 -0
- package/dist/node/canvas.d.ts +3 -0
- package/dist/node/canvas.d.ts.map +1 -0
- package/dist/node/canvas.js +13 -0
- package/dist/node/canvas.js.map +1 -0
- package/dist/node/codegen.d.ts +6 -0
- package/dist/node/codegen.d.ts.map +1 -0
- package/dist/node/codegen.js +70 -0
- package/dist/node/codegen.js.map +1 -0
- package/dist/node/css-escape.d.ts +8 -0
- package/dist/node/css-escape.d.ts.map +1 -0
- package/dist/node/css-escape.js +12 -0
- package/dist/node/css-escape.js.map +1 -0
- package/dist/node/fonts.d.ts +6 -0
- package/dist/node/fonts.d.ts.map +1 -0
- package/dist/node/fonts.js +219 -0
- package/dist/node/fonts.js.map +1 -0
- package/dist/node/lint.d.ts +3 -0
- package/dist/node/lint.d.ts.map +1 -0
- package/dist/node/lint.js +23 -0
- package/dist/node/lint.js.map +1 -0
- package/dist/node/mode.d.ts +9 -0
- package/dist/node/mode.d.ts.map +1 -0
- package/dist/node/mode.js +38 -0
- package/dist/node/mode.js.map +1 -0
- package/dist/node/theme.d.ts +13 -0
- package/dist/node/theme.d.ts.map +1 -0
- package/dist/node/theme.js +160 -0
- package/dist/node/theme.js.map +1 -0
- package/dist/types.d.ts +250 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +80 -0
- package/dist/types.js.map +1 -0
- package/guide/acts.md +61 -0
- package/guide/audio.md +77 -0
- package/guide/blocks.md +82 -0
- package/guide/canvas-choosing.md +34 -0
- package/guide/canvas-editing.md +36 -0
- package/guide/canvas-layout.md +46 -0
- package/guide/canvas-opening.md +47 -0
- package/guide/canvas-overview.md +24 -0
- package/guide/canvas-states.md +37 -0
- package/guide/canvas-view.md +46 -0
- package/guide/canvas.md +94 -0
- package/guide/containers.md +60 -0
- package/guide/content.md +40 -0
- package/guide/controls.md +89 -0
- package/guide/density.md +48 -0
- package/guide/dock.md +42 -0
- package/guide/drag.md +38 -0
- package/guide/figures.md +25 -0
- package/guide/forms.md +82 -0
- package/guide/pane-bar.md +11 -0
- package/guide/pane.md +50 -0
- package/guide/reference.md +74 -0
- package/guide/rules.md +109 -0
- package/guide/section.md +29 -0
- package/guide/shell.md +26 -0
- package/guide/shots.md +37 -0
- package/guide/sidebar.md +30 -0
- package/guide/table.md +119 -0
- package/lint/no-arbitrary-value.grit +9 -0
- package/lint/no-class-on-component.grit +12 -0
- package/lint/no-density.grit +8 -0
- package/lint/no-host-look.grit +38 -0
- package/lint/no-img.grit +10 -0
- package/lint/no-navigate-act.grit +22 -0
- package/lint/no-raw-style.grit +8 -0
- package/package.json +150 -3
- package/src/density.ts +9 -0
- package/src/index.ts +503 -0
- package/src/node/canvas.ts +14 -0
- package/src/node/codegen.ts +100 -0
- package/src/node/css-escape.ts +27 -0
- package/src/node/fonts.ts +260 -0
- package/src/node/lint.ts +25 -0
- package/src/node/mode.ts +64 -0
- package/src/node/theme.ts +232 -0
- package/src/types.ts +120 -0
- package/src/ui/atoms/avatar/base.tsx +42 -0
- package/src/ui/atoms/button/glyph.tsx +244 -0
- package/src/ui/atoms/button/link.tsx +81 -0
- package/src/ui/atoms/button/look.ts +32 -0
- package/src/ui/atoms/button/reason.tsx +69 -0
- package/src/ui/atoms/chip/base.tsx +85 -0
- package/src/ui/atoms/count/base.tsx +27 -0
- package/src/ui/atoms/icon/index.tsx +51 -0
- package/src/ui/atoms/message/waiting.tsx +19 -0
- package/src/ui/atoms/prose/painted.tsx +35 -0
- package/src/ui/atoms/row/badges.tsx +226 -0
- package/src/ui/atoms/row/leading.tsx +166 -0
- package/src/ui/atoms/row/lock.tsx +17 -0
- package/src/ui/atoms/row/marks.tsx +79 -0
- package/src/ui/atoms/section/card.tsx +27 -0
- package/src/ui/atoms/spinner/index.tsx +42 -0
- package/src/ui/atoms/status/base.tsx +51 -0
- package/src/ui/atoms/status/change.tsx +29 -0
- package/src/ui/atoms/status/dot.tsx +45 -0
- package/src/ui/atoms/switch/checkbox.tsx +121 -0
- package/src/ui/atoms/turn.tsx +28 -0
- package/src/ui/components/action-bar/base.tsx +276 -0
- package/src/ui/components/action-bar/index.tsx +20 -0
- package/src/ui/components/avatar/index.tsx +21 -0
- package/src/ui/components/banner/index.tsx +207 -0
- package/src/ui/components/banner/pending.tsx +109 -0
- package/src/ui/components/bar-chart/index.tsx +701 -0
- package/src/ui/components/button/base.tsx +214 -0
- package/src/ui/components/button/face.tsx +84 -0
- package/src/ui/components/button/index.tsx +206 -0
- package/src/ui/components/canvas/connection.tsx +26 -0
- package/src/ui/components/canvas/edges.tsx +116 -0
- package/src/ui/components/canvas/elk.ts +260 -0
- package/src/ui/components/canvas/floor.ts +173 -0
- package/src/ui/components/canvas/geometry.ts +772 -0
- package/src/ui/components/canvas/ground.tsx +97 -0
- package/src/ui/components/canvas/group.tsx +156 -0
- package/src/ui/components/canvas/hit.ts +40 -0
- package/src/ui/components/canvas/index.tsx +497 -0
- package/src/ui/components/canvas/label.tsx +53 -0
- package/src/ui/components/canvas/layout.ts +543 -0
- package/src/ui/components/canvas/lift.ts +272 -0
- package/src/ui/components/canvas/look.ts +129 -0
- package/src/ui/components/canvas/node.tsx +689 -0
- package/src/ui/components/canvas/open.ts +243 -0
- package/src/ui/components/canvas/opening.ts +240 -0
- package/src/ui/components/canvas/view.ts +621 -0
- package/src/ui/components/canvas/viewport.ts +417 -0
- package/src/ui/components/canvas/zoom.tsx +113 -0
- package/src/ui/components/card/index.tsx +20 -0
- package/src/ui/components/chip/index.tsx +24 -0
- package/src/ui/components/code/frame.ts +2 -0
- package/src/ui/components/code/index.tsx +234 -0
- package/src/ui/components/columns/index.tsx +51 -0
- package/src/ui/components/count/index.tsx +17 -0
- package/src/ui/components/diff/index.tsx +249 -0
- package/src/ui/components/diff/word.tsx +104 -0
- package/src/ui/components/dock/index.tsx +262 -0
- package/src/ui/components/file-row/index.tsx +213 -0
- package/src/ui/components/form/base.tsx +280 -0
- package/src/ui/components/form/derive.tsx +269 -0
- package/src/ui/components/form/index.tsx +49 -0
- package/src/ui/components/form/saved.tsx +39 -0
- package/src/ui/components/form-field/index.tsx +658 -0
- package/src/ui/components/gallery/index.tsx +83 -0
- package/src/ui/components/gate/index.tsx +183 -0
- package/src/ui/components/image/base.tsx +245 -0
- package/src/ui/components/image/index.tsx +58 -0
- package/src/ui/components/input/base.tsx +246 -0
- package/src/ui/components/input/code.tsx +108 -0
- package/src/ui/components/input/drop.ts +37 -0
- package/src/ui/components/input/file.tsx +154 -0
- package/src/ui/components/input/index.tsx +132 -0
- package/src/ui/components/input/prose.tsx +107 -0
- package/src/ui/components/input/refusal.tsx +30 -0
- package/src/ui/components/item-header/base.tsx +410 -0
- package/src/ui/components/menu/base.tsx +319 -0
- package/src/ui/components/message/attachments.tsx +101 -0
- package/src/ui/components/message/base.tsx +266 -0
- package/src/ui/components/message-input/index.tsx +377 -0
- package/src/ui/components/missing/base.tsx +45 -0
- package/src/ui/components/pane/head.tsx +399 -0
- package/src/ui/components/pane/index.tsx +325 -0
- package/src/ui/components/picker/base.tsx +1551 -0
- package/src/ui/components/picker/index.tsx +224 -0
- package/src/ui/components/picker/list.tsx +423 -0
- package/src/ui/components/picker/segments.tsx +183 -0
- package/src/ui/components/prose/index.tsx +259 -0
- package/src/ui/components/qr-code/index.tsx +65 -0
- package/src/ui/components/row/acts.tsx +126 -0
- package/src/ui/components/row/fact.tsx +54 -0
- package/src/ui/components/row/index.tsx +1370 -0
- package/src/ui/components/row/progress.tsx +38 -0
- package/src/ui/components/row/status.tsx +54 -0
- package/src/ui/components/row/title.ts +7 -0
- package/src/ui/components/rules/index.tsx +290 -0
- package/src/ui/components/section/band.tsx +39 -0
- package/src/ui/components/section/head.tsx +267 -0
- package/src/ui/components/section/index.tsx +409 -0
- package/src/ui/components/section/rows.tsx +356 -0
- package/src/ui/components/sheet/base.tsx +272 -0
- package/src/ui/components/sheet/confirm.tsx +79 -0
- package/src/ui/components/sheet/docked.tsx +218 -0
- package/src/ui/components/sheet/form.tsx +76 -0
- package/src/ui/components/sheet/head.tsx +120 -0
- package/src/ui/components/sheet/index.tsx +45 -0
- package/src/ui/components/sheet/layer.tsx +168 -0
- package/src/ui/components/sheet/outlet.tsx +58 -0
- package/src/ui/components/sheet/task.tsx +41 -0
- package/src/ui/components/shell/cut.tsx +116 -0
- package/src/ui/components/shell/drag.tsx +45 -0
- package/src/ui/components/shell/host.tsx +71 -0
- package/src/ui/components/shell/index.tsx +737 -0
- package/src/ui/components/shell/panes.tsx +76 -0
- package/src/ui/components/shell/preview.ts +230 -0
- package/src/ui/components/shell/rail.tsx +163 -0
- package/src/ui/components/shell/row.ts +11 -0
- package/src/ui/components/shell/switcher.tsx +143 -0
- package/src/ui/components/shell/tree.tsx +252 -0
- package/src/ui/components/slider/base.tsx +139 -0
- package/src/ui/components/slider/index.tsx +57 -0
- package/src/ui/components/stages/index.tsx +198 -0
- package/src/ui/components/stats/count-links.tsx +47 -0
- package/src/ui/components/stats/index.tsx +203 -0
- package/src/ui/components/stats/meter.tsx +213 -0
- package/src/ui/components/stats/parts.ts +10 -0
- package/src/ui/components/step-count/index.tsx +45 -0
- package/src/ui/components/switch/base.tsx +86 -0
- package/src/ui/components/switch/index.tsx +38 -0
- package/src/ui/components/table/columns.tsx +131 -0
- package/src/ui/components/table/index.tsx +2215 -0
- package/src/ui/components/text/index.tsx +37 -0
- package/src/ui/components/thread/index.tsx +503 -0
- package/src/ui/components/thread/latest.tsx +59 -0
- package/src/ui/components/toast/index.tsx +113 -0
- package/src/ui/components/toast/layer.tsx +26 -0
- package/src/ui/components/toolbar/filters.tsx +161 -0
- package/src/ui/components/toolbar/index.tsx +125 -0
- package/src/ui/components/waveform/index.tsx +205 -0
- package/src/ui/globals.css +427 -0
- package/src/ui/lib/act.ts +60 -0
- package/src/ui/lib/age.ts +49 -0
- package/src/ui/lib/bound.ts +16 -0
- package/src/ui/lib/canvas-layout.ts +11 -0
- package/src/ui/lib/caret.ts +9 -0
- package/src/ui/lib/chip-lines.ts +3 -0
- package/src/ui/lib/closed.ts +8 -0
- package/src/ui/lib/code.tsx +184 -0
- package/src/ui/lib/columns.ts +39 -0
- package/src/ui/lib/commit.ts +24 -0
- package/src/ui/lib/container.ts +112 -0
- package/src/ui/lib/copy.ts +29 -0
- package/src/ui/lib/drag.ts +575 -0
- package/src/ui/lib/empty.tsx +85 -0
- package/src/ui/lib/field.ts +39 -0
- package/src/ui/lib/focus.ts +175 -0
- package/src/ui/lib/folds.ts +67 -0
- package/src/ui/lib/follow.ts +220 -0
- package/src/ui/lib/form.ts +19 -0
- package/src/ui/lib/guards.ts +38 -0
- package/src/ui/lib/heading.ts +14 -0
- package/src/ui/lib/ink.ts +26 -0
- package/src/ui/lib/lang.ts +7 -0
- package/src/ui/lib/leave.ts +14 -0
- package/src/ui/lib/lockup.tsx +145 -0
- package/src/ui/lib/measure.ts +22 -0
- package/src/ui/lib/media.ts +63 -0
- package/src/ui/lib/modality.ts +69 -0
- package/src/ui/lib/moment.ts +13 -0
- package/src/ui/lib/more.tsx +76 -0
- package/src/ui/lib/navigate.ts +210 -0
- package/src/ui/lib/noop.ts +3 -0
- package/src/ui/lib/not-found.tsx +40 -0
- package/src/ui/lib/paint.tsx +55 -0
- package/src/ui/lib/parts-line.tsx +54 -0
- package/src/ui/lib/picked.ts +18 -0
- package/src/ui/lib/player-store.ts +38 -0
- package/src/ui/lib/player.ts +139 -0
- package/src/ui/lib/portal.ts +12 -0
- package/src/ui/lib/read.tsx +269 -0
- package/src/ui/lib/region.tsx +49 -0
- package/src/ui/lib/reorder.ts +154 -0
- package/src/ui/lib/rest.ts +16 -0
- package/src/ui/lib/saved.ts +29 -0
- package/src/ui/lib/scramble.tsx +147 -0
- package/src/ui/lib/scrolls.ts +70 -0
- package/src/ui/lib/style.ts +6 -0
- package/src/ui/lib/swap.tsx +107 -0
- package/src/ui/lib/toast.ts +39 -0
- package/src/ui/lib/walk-classes.ts +16 -0
- package/src/ui/lib/window.tsx +376 -0
- package/src/ui/worker.d.ts +4 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import type { CanvasPoint } from "@slotform/ui-core/descriptors";
|
|
2
|
+
import { DRAG_DELAY, DRAG_DELAY_SLOP } from "@slotform/ui-core/drag";
|
|
3
|
+
import {
|
|
4
|
+
type MouseEvent,
|
|
5
|
+
type PointerEvent,
|
|
6
|
+
useEffect,
|
|
7
|
+
useLayoutEffect,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
} from "react";
|
|
11
|
+
import type { Viewport } from "./viewport.ts";
|
|
12
|
+
|
|
13
|
+
// Where a held node stands with the pointer at `point` (client pixels):
|
|
14
|
+
// `grab` (flow pixels) from the node's corner to where it was taken, under
|
|
15
|
+
// the pointer as the view stands now, so a pan under a still pointer carries
|
|
16
|
+
// the node with it.
|
|
17
|
+
export function heldAt(
|
|
18
|
+
viewport: Viewport,
|
|
19
|
+
point: CanvasPoint,
|
|
20
|
+
grab: CanvasPoint,
|
|
21
|
+
): CanvasPoint {
|
|
22
|
+
const at = viewport.screenToFlow(point);
|
|
23
|
+
return { x: at.x - grab.x, y: at.y - grab.y };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface Lift {
|
|
27
|
+
// A finger holds the node, which follows it.
|
|
28
|
+
lifted: boolean;
|
|
29
|
+
// Spread on the node's element, beside a touch finger's own handlers.
|
|
30
|
+
bind: {
|
|
31
|
+
onPointerDown: (event: PointerEvent<HTMLElement>) => void;
|
|
32
|
+
onPointerMove: (event: PointerEvent<HTMLElement>) => void;
|
|
33
|
+
onPointerUp: (event: PointerEvent<HTMLElement>) => void;
|
|
34
|
+
onPointerCancel: (event: PointerEvent<HTMLElement>) => void;
|
|
35
|
+
onClickCapture: (event: MouseEvent) => void;
|
|
36
|
+
onContextMenu: (event: MouseEvent) => void;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface Press {
|
|
41
|
+
pointer: number;
|
|
42
|
+
target: HTMLElement;
|
|
43
|
+
from: CanvasPoint;
|
|
44
|
+
// Where the finger is, until the node lifts.
|
|
45
|
+
now: CanvasPoint;
|
|
46
|
+
// The finger strayed past the slop, or a second one landed: a pan or a pinch
|
|
47
|
+
// is d3-zoom's, and the click that follows it selects nothing.
|
|
48
|
+
moved: boolean;
|
|
49
|
+
timer: number | undefined;
|
|
50
|
+
// The event time at which the press has held long enough to lift.
|
|
51
|
+
due: number;
|
|
52
|
+
// Set when the node lifts: where the finger took it (`heldAt`).
|
|
53
|
+
hold: { grab: CanvasPoint } | null;
|
|
54
|
+
// The node's latest position while held.
|
|
55
|
+
point: CanvasPoint;
|
|
56
|
+
// The node dropped (a second finger landed) while this finger is still down.
|
|
57
|
+
dropped: boolean;
|
|
58
|
+
other: (event: globalThis.PointerEvent) => void;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// d3-zoom reads its filter once, at a gesture's `touchstart`, and has no cancel:
|
|
62
|
+
// the finger that lifts a node is already in a gesture. A lift starves that
|
|
63
|
+
// gesture instead, by stopping the touch events before the region's listener
|
|
64
|
+
// (a `document` capture listener runs first) until the lifting finger ends. A
|
|
65
|
+
// `touchend` is never stopped, so the gesture closes itself.
|
|
66
|
+
const starve = (event: Event) => event.stopPropagation();
|
|
67
|
+
const STARVED = ["touchmove", "touchstart"] as const;
|
|
68
|
+
|
|
69
|
+
function starving(on: boolean) {
|
|
70
|
+
for (const type of STARVED) {
|
|
71
|
+
if (on) document.addEventListener(type, starve, true);
|
|
72
|
+
else document.removeEventListener(type, starve, true);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function unhook(state: Press) {
|
|
77
|
+
window.clearTimeout(state.timer);
|
|
78
|
+
document.removeEventListener("pointerdown", state.other, true);
|
|
79
|
+
if (state.hold) starving(false);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export const client = (event: {
|
|
83
|
+
clientX: number;
|
|
84
|
+
clientY: number;
|
|
85
|
+
}): CanvasPoint => ({
|
|
86
|
+
x: event.clientX,
|
|
87
|
+
y: event.clientY,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// A long press on a node lifts it, and the finger then carries it. A finger
|
|
91
|
+
// that moves first, or a second one, leaves the pan or the pinch to d3-zoom.
|
|
92
|
+
// Only a touch or a pen takes part: a mouse drags at once, in the node's own
|
|
93
|
+
// handlers. With `enabled` false the hook only keeps a pan's click from
|
|
94
|
+
// selecting.
|
|
95
|
+
export function useLift(args: {
|
|
96
|
+
id: string;
|
|
97
|
+
enabled: boolean;
|
|
98
|
+
viewport: Viewport;
|
|
99
|
+
// The canvas's one writer of a held position, `null` clearing it.
|
|
100
|
+
at: (id: string, point: CanvasPoint | null) => void;
|
|
101
|
+
// A release: reports the position, then clears it.
|
|
102
|
+
drop: (id: string, point: CanvasPoint) => void;
|
|
103
|
+
// Where the node stands now.
|
|
104
|
+
place: () => CanvasPoint;
|
|
105
|
+
}): Lift {
|
|
106
|
+
const [lifted, setLifted] = useState(false);
|
|
107
|
+
const press = useRef<Press | null>(null);
|
|
108
|
+
const armed = useRef(false);
|
|
109
|
+
const latest = useRef(args);
|
|
110
|
+
useLayoutEffect(() => {
|
|
111
|
+
latest.current = args;
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// The hold ends: the view stops panning at the pane's edge for it.
|
|
115
|
+
const unhold = () => {
|
|
116
|
+
latest.current.viewport.edge(null);
|
|
117
|
+
setLifted(false);
|
|
118
|
+
};
|
|
119
|
+
const finish = (state: Press) => {
|
|
120
|
+
unhook(state);
|
|
121
|
+
press.current = null;
|
|
122
|
+
unhold();
|
|
123
|
+
};
|
|
124
|
+
// A node held by a finger drops where it is, once.
|
|
125
|
+
const settle = (state: Press, point: CanvasPoint) => {
|
|
126
|
+
if (!state.hold || state.dropped) return;
|
|
127
|
+
state.dropped = true;
|
|
128
|
+
latest.current.drop(latest.current.id, point);
|
|
129
|
+
unhold();
|
|
130
|
+
};
|
|
131
|
+
// The held node follows the finger at `point`.
|
|
132
|
+
const follow = (state: Press, point: CanvasPoint) => {
|
|
133
|
+
if (!state.hold || state.dropped) return;
|
|
134
|
+
const { id, at, viewport } = latest.current;
|
|
135
|
+
state.point = heldAt(viewport, point, state.hold.grab);
|
|
136
|
+
at(id, state.point);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// A page slower than the press lifts the node on its timer before it handles
|
|
140
|
+
// an event the finger made earlier, and an event's `timeStamp` is the
|
|
141
|
+
// input's, not the handler's: the node goes back as it stood.
|
|
142
|
+
const unlift = (state: Press) => {
|
|
143
|
+
if (!state.hold || state.dropped) return;
|
|
144
|
+
state.target.releasePointerCapture(state.pointer);
|
|
145
|
+
starving(false);
|
|
146
|
+
latest.current.at(latest.current.id, null);
|
|
147
|
+
state.hold = null;
|
|
148
|
+
unhold();
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const down = (event: PointerEvent<HTMLElement>) => {
|
|
152
|
+
if (event.pointerType === "mouse" || event.button !== 0 || press.current)
|
|
153
|
+
return;
|
|
154
|
+
armed.current = false;
|
|
155
|
+
const state: Press = {
|
|
156
|
+
pointer: event.pointerId,
|
|
157
|
+
target: event.currentTarget,
|
|
158
|
+
from: client(event),
|
|
159
|
+
now: client(event),
|
|
160
|
+
moved: false,
|
|
161
|
+
timer: undefined,
|
|
162
|
+
// A node lifts after the drag long press: docs/architecture/plugins/react-ui.md, "Canvas".
|
|
163
|
+
due: event.timeStamp + DRAG_DELAY,
|
|
164
|
+
hold: null,
|
|
165
|
+
point: client(event),
|
|
166
|
+
dropped: false,
|
|
167
|
+
// A second finger anywhere ends the press, or drops the node.
|
|
168
|
+
other: (landing) => {
|
|
169
|
+
if (landing.pointerId === state.pointer) return;
|
|
170
|
+
if (landing.timeStamp < state.due) unlift(state);
|
|
171
|
+
state.moved = true;
|
|
172
|
+
window.clearTimeout(state.timer);
|
|
173
|
+
settle(state, state.point);
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
press.current = state;
|
|
177
|
+
if (!latest.current.enabled) return;
|
|
178
|
+
document.addEventListener("pointerdown", state.other, true);
|
|
179
|
+
state.timer = window.setTimeout(() => {
|
|
180
|
+
const { id, at, place, viewport } = latest.current;
|
|
181
|
+
const start = place();
|
|
182
|
+
const taken = viewport.screenToFlow(state.now);
|
|
183
|
+
state.hold = { grab: { x: taken.x - start.x, y: taken.y - start.y } };
|
|
184
|
+
state.point = start;
|
|
185
|
+
state.target.setPointerCapture(state.pointer);
|
|
186
|
+
starving(true);
|
|
187
|
+
at(id, start);
|
|
188
|
+
setLifted(true);
|
|
189
|
+
}, DRAG_DELAY);
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const move = (event: PointerEvent<HTMLElement>) => {
|
|
193
|
+
const state = press.current;
|
|
194
|
+
if (!state || event.pointerId !== state.pointer) return;
|
|
195
|
+
const point = client(event);
|
|
196
|
+
if (
|
|
197
|
+
event.timeStamp < state.due &&
|
|
198
|
+
Math.hypot(point.x - state.from.x, point.y - state.from.y) >
|
|
199
|
+
DRAG_DELAY_SLOP
|
|
200
|
+
)
|
|
201
|
+
unlift(state);
|
|
202
|
+
if (state.hold) {
|
|
203
|
+
if (state.dropped) return;
|
|
204
|
+
follow(state, point);
|
|
205
|
+
latest.current.viewport.edge({
|
|
206
|
+
point,
|
|
207
|
+
follow: (again) => follow(state, again),
|
|
208
|
+
});
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
state.now = point;
|
|
212
|
+
if (
|
|
213
|
+
Math.hypot(point.x - state.from.x, point.y - state.from.y) >
|
|
214
|
+
DRAG_DELAY_SLOP
|
|
215
|
+
) {
|
|
216
|
+
state.moved = true;
|
|
217
|
+
window.clearTimeout(state.timer);
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const up = (event: PointerEvent<HTMLElement>) => {
|
|
222
|
+
const state = press.current;
|
|
223
|
+
if (!state || event.pointerId !== state.pointer) return;
|
|
224
|
+
if (state.hold && !state.dropped)
|
|
225
|
+
settle(
|
|
226
|
+
state,
|
|
227
|
+
heldAt(latest.current.viewport, client(event), state.hold.grab),
|
|
228
|
+
);
|
|
229
|
+
armed.current = state.moved || state.hold !== null;
|
|
230
|
+
finish(state);
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
// A cancelled hold gives the node back where it stood, reporting nothing.
|
|
234
|
+
const cancel = (event: PointerEvent<HTMLElement>) => {
|
|
235
|
+
const state = press.current;
|
|
236
|
+
if (!state || event.pointerId !== state.pointer) return;
|
|
237
|
+
if (state.hold && !state.dropped)
|
|
238
|
+
latest.current.at(latest.current.id, null);
|
|
239
|
+
armed.current = true;
|
|
240
|
+
finish(state);
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
useEffect(
|
|
244
|
+
() => () => {
|
|
245
|
+
const state = press.current;
|
|
246
|
+
if (state) unhook(state);
|
|
247
|
+
},
|
|
248
|
+
[],
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
return {
|
|
252
|
+
lifted,
|
|
253
|
+
bind: {
|
|
254
|
+
onPointerDown: down,
|
|
255
|
+
onPointerMove: move,
|
|
256
|
+
onPointerUp: up,
|
|
257
|
+
onPointerCancel: cancel,
|
|
258
|
+
// The click that follows a lift or a pan selects nothing. A key's click
|
|
259
|
+
// (`detail` 0) is never one.
|
|
260
|
+
onClickCapture: (event) => {
|
|
261
|
+
if (!armed.current || event.detail === 0) return;
|
|
262
|
+
armed.current = false;
|
|
263
|
+
event.stopPropagation();
|
|
264
|
+
event.preventDefault();
|
|
265
|
+
},
|
|
266
|
+
// A long press raises the context menu on some platforms.
|
|
267
|
+
onContextMenu: (event) => {
|
|
268
|
+
if (press.current) event.preventDefault();
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CanvasEdge,
|
|
3
|
+
CanvasNode,
|
|
4
|
+
CanvasPath,
|
|
5
|
+
CanvasRole,
|
|
6
|
+
} from "@slotform/ui-core/descriptors";
|
|
7
|
+
|
|
8
|
+
type Surface =
|
|
9
|
+
| "plain"
|
|
10
|
+
| "strong"
|
|
11
|
+
| "accent"
|
|
12
|
+
| "attention"
|
|
13
|
+
| "muted"
|
|
14
|
+
| "off"
|
|
15
|
+
| "dimmed";
|
|
16
|
+
|
|
17
|
+
// A selectable part's hover edge and its keyboard focus ring: a node's card or
|
|
18
|
+
// glyph, a group's head.
|
|
19
|
+
export const HOVERED = "hover:border-edge-hover";
|
|
20
|
+
export const FOCUSED =
|
|
21
|
+
"in-data-[modality=keyboard]:focus-visible:outline-2 focus-visible:outline-ring";
|
|
22
|
+
|
|
23
|
+
// The tone each role draws at rest.
|
|
24
|
+
const ROLE_SURFACE: Record<CanvasRole, Surface> = {
|
|
25
|
+
step: "plain",
|
|
26
|
+
start: "accent",
|
|
27
|
+
decision: "strong",
|
|
28
|
+
wait: "attention",
|
|
29
|
+
call: "plain",
|
|
30
|
+
end: "muted",
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// What a node draws, decided here and only spelled by `NodeView`.
|
|
34
|
+
export interface NodeLook {
|
|
35
|
+
// The `CANVAS_NODE` state: the outline's colour.
|
|
36
|
+
state: "rest" | "selected" | "warning";
|
|
37
|
+
// The `CANVAS_NODE_TEXT` tone of every part. A warning's words take the rest
|
|
38
|
+
// ink: status colour belongs to a mark. An end's words are quiet as an off
|
|
39
|
+
// node's.
|
|
40
|
+
tone: "rest" | "off" | "dimmed";
|
|
41
|
+
// What the node is: its shape.
|
|
42
|
+
role: CanvasRole;
|
|
43
|
+
// The `CANVAS_NODE` tone: the role's ground and edge at rest, and the quiet
|
|
44
|
+
// an off node and a node off the path draw over any role.
|
|
45
|
+
surface: Surface;
|
|
46
|
+
// What the line region shows.
|
|
47
|
+
shows: "line" | "off" | "warning";
|
|
48
|
+
// Whether the line under the title is led by the node's `status`.
|
|
49
|
+
status: boolean;
|
|
50
|
+
// Whether the trailing column draws the danger mark. It outlives selection,
|
|
51
|
+
// which takes the outline from the warning's border.
|
|
52
|
+
warning: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// A node is on the path when there is no path, when the path lists it, or when
|
|
56
|
+
// it is where the path stands.
|
|
57
|
+
function onPath(id: string, path: CanvasPath | undefined): boolean {
|
|
58
|
+
return !path || path.nodes.includes(id) || id === path.at;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// An off node ignores its warning, and a node off the path shows none.
|
|
62
|
+
function warns(
|
|
63
|
+
node: Pick<CanvasNode, "id" | "off" | "warning">,
|
|
64
|
+
path: CanvasPath | undefined,
|
|
65
|
+
): boolean {
|
|
66
|
+
return Boolean(node.warning) && !node.off && onPath(node.id, path);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function boxState(
|
|
70
|
+
node: Pick<CanvasNode, "id" | "off" | "warning">,
|
|
71
|
+
selected: string | undefined,
|
|
72
|
+
path: CanvasPath | undefined,
|
|
73
|
+
): NodeLook["state"] {
|
|
74
|
+
if (node.id === selected || node.id === path?.at) return "selected";
|
|
75
|
+
return warns(node, path) ? "warning" : "rest";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function nodeTone(
|
|
79
|
+
node: Pick<CanvasNode, "id" | "off" | "role">,
|
|
80
|
+
path: CanvasPath | undefined,
|
|
81
|
+
): NodeLook["tone"] {
|
|
82
|
+
if (!onPath(node.id, path)) return "dimmed";
|
|
83
|
+
return node.off || node.role === "end" ? "off" : "rest";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function nodeSurface(
|
|
87
|
+
node: Pick<CanvasNode, "id" | "off" | "role">,
|
|
88
|
+
path: CanvasPath | undefined,
|
|
89
|
+
): Surface {
|
|
90
|
+
if (!onPath(node.id, path)) return "dimmed";
|
|
91
|
+
return node.off ? "off" : ROLE_SURFACE[node.role ?? "step"];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function lineShows(
|
|
95
|
+
node: Pick<CanvasNode, "off" | "warning">,
|
|
96
|
+
): NodeLook["shows"] {
|
|
97
|
+
if (node.off) return "off";
|
|
98
|
+
return node.warning ? "warning" : "line";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function nodeLook(
|
|
102
|
+
node: Pick<CanvasNode, "id" | "off" | "warning" | "status" | "role">,
|
|
103
|
+
selected: string | undefined,
|
|
104
|
+
path?: CanvasPath,
|
|
105
|
+
): NodeLook {
|
|
106
|
+
return {
|
|
107
|
+
state: boxState(node, selected, path),
|
|
108
|
+
tone: nodeTone(node, path),
|
|
109
|
+
role: node.role ?? "step",
|
|
110
|
+
surface: nodeSurface(node, path),
|
|
111
|
+
shows: lineShows(node),
|
|
112
|
+
status: Boolean(node.status) && onPath(node.id, path),
|
|
113
|
+
warning: warns(node, path),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export type EdgeTone = "rest" | "dimmed";
|
|
118
|
+
|
|
119
|
+
// An edge is on the path by its own id only, never by its ends; an edge to an
|
|
120
|
+
// off node dims with it. An end that is not a node is never off.
|
|
121
|
+
export function edgeLook(
|
|
122
|
+
edge: Pick<CanvasEdge, "id" | "from" | "to">,
|
|
123
|
+
nodes: ReadonlyMap<string, Pick<CanvasNode, "off">>,
|
|
124
|
+
path: CanvasPath | undefined,
|
|
125
|
+
): EdgeTone {
|
|
126
|
+
if (path && !path.edges.includes(edge.id)) return "dimmed";
|
|
127
|
+
if (nodes.get(edge.from)?.off || nodes.get(edge.to)?.off) return "dimmed";
|
|
128
|
+
return "rest";
|
|
129
|
+
}
|