@celestia-island/hikari 0.55.424 → 0.55.425
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/package.json +1 -1
- package/src/components/HkSlideConfirm.scss +137 -0
- package/src/components/HkSlideConfirm.test.tsx +418 -0
- package/src/components/HkSlideConfirm.tsx +439 -0
- package/src/components/slideConfirmPath.test.ts +179 -0
- package/src/components/slideConfirmPath.ts +192 -0
- package/src/i18n/locales/ar/components.json +3 -1
- package/src/i18n/locales/de/components.json +3 -1
- package/src/i18n/locales/en/components.json +3 -1
- package/src/i18n/locales/es/components.json +3 -1
- package/src/i18n/locales/fr/components.json +3 -1
- package/src/i18n/locales/ja/components.json +3 -1
- package/src/i18n/locales/ko/components.json +3 -1
- package/src/i18n/locales/pt/components.json +3 -1
- package/src/i18n/locales/ru/components.json +3 -1
- package/src/i18n/locales/zh-Hans/components.json +3 -1
- package/src/i18n/locales/zh-Hant/components.json +3 -1
- package/src/index.ts +12 -0
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import {
|
|
2
|
+
computed,
|
|
3
|
+
defineComponent,
|
|
4
|
+
onBeforeUnmount,
|
|
5
|
+
onMounted,
|
|
6
|
+
ref,
|
|
7
|
+
watch,
|
|
8
|
+
type PropType,
|
|
9
|
+
} from "vue";
|
|
10
|
+
|
|
11
|
+
import { useI18n } from "../i18n/context";
|
|
12
|
+
import {
|
|
13
|
+
generateSlidePath,
|
|
14
|
+
slidePathLength,
|
|
15
|
+
slidePixelPoints,
|
|
16
|
+
slidePointAtT,
|
|
17
|
+
slideConfirmCorridor,
|
|
18
|
+
slideSegmentDistance,
|
|
19
|
+
type SlidePath,
|
|
20
|
+
type SlidePoint,
|
|
21
|
+
} from "./slideConfirmPath";
|
|
22
|
+
import "./HkSlideConfirm.scss";
|
|
23
|
+
|
|
24
|
+
/** Drag progress at which the confirmation fires (the lane's very end;
|
|
25
|
+
* a hair of tolerance for float rounding along the final run). */
|
|
26
|
+
const COMPLETE_T = 0.995;
|
|
27
|
+
|
|
28
|
+
/** The lane keeps a knob-radius margin from the track edges. */
|
|
29
|
+
const TRACK_PAD = 24;
|
|
30
|
+
|
|
31
|
+
/** Pointer corridor: the pure module's constant — HALF A LANE SPACING
|
|
32
|
+
* minus a small slack over the inner height (the tests pin it against
|
|
33
|
+
* the half-spacing bound; see slideConfirmPath.ts). */
|
|
34
|
+
const corridorRadius = slideConfirmCorridor;
|
|
35
|
+
|
|
36
|
+
/** Follow-walk resolution: the lane is sampled at this many arc-length
|
|
37
|
+
* steps per advance scan (fine enough that a corner is never chorded). */
|
|
38
|
+
const SCAN_STEPS = 240;
|
|
39
|
+
|
|
40
|
+
interface TrackBox {
|
|
41
|
+
width: number;
|
|
42
|
+
height: number;
|
|
43
|
+
left: number;
|
|
44
|
+
top: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* HkSlideConfirm — the fat slide-to-confirm: a three-standard-row block
|
|
49
|
+
* carrying an orthogonal lane with RANDOM right-angle turns; the user
|
|
50
|
+
* grabs the knob and drags it along the lane from the left edge to the
|
|
51
|
+
* right edge. Releasing before the end — or dragging off the lane's
|
|
52
|
+
* corridor — snaps the knob back to the start at once. Reaching the end
|
|
53
|
+
* emits `confirm` once.
|
|
54
|
+
*
|
|
55
|
+
* 2026-11-19 user direction (the workspace delete confirmation): the
|
|
56
|
+
* straight slide-to-confirm family is everywhere; this one is the
|
|
57
|
+
* captcha-shaped cousin — a traced route, not a straight pull — as a
|
|
58
|
+
* general business component. The route is random per mount (captcha
|
|
59
|
+
* property); a `seed` prop pins it for tests and snapshots.
|
|
60
|
+
*
|
|
61
|
+
* Mechanics:
|
|
62
|
+
* · the lane is generated by `generateSlidePath` (≥3 random 90° turns
|
|
63
|
+
* through three lanes; pure module, jsdom-tested separately);
|
|
64
|
+
* · following = monotonic arc-length advance: the knob jumps to the
|
|
65
|
+
* furthest lane point within the pointer corridor; a pointer that
|
|
66
|
+
* leaves the corridor (corner-cutting) is a failed attempt;
|
|
67
|
+
* · release / pointercancel / corridor exit before the end → the knob
|
|
68
|
+
* snaps back to the start (transitioned; reduced-motion snaps at once);
|
|
69
|
+
* · keyboard: the knob is a slider handle — arrows walk the lane
|
|
70
|
+
* (Right/Up advance, Left/Down retreat, Home resets), reaching the end
|
|
71
|
+
* fires `confirm`; AT users get the same confirmation without a
|
|
72
|
+
* pointer.
|
|
73
|
+
*
|
|
74
|
+
* The component carries zero transport and zero business semantics: hosts
|
|
75
|
+
* render it inside their own confirmation surface and react to `confirm`.
|
|
76
|
+
*/
|
|
77
|
+
export default defineComponent({
|
|
78
|
+
name: "HkSlideConfirm",
|
|
79
|
+
props: {
|
|
80
|
+
/** Accessible name / the instruction inside the track while idle.
|
|
81
|
+
* Defaults to the localized "Hold the knob and drag along the lane
|
|
82
|
+
* to the right edge". */
|
|
83
|
+
label: { type: String, default: undefined },
|
|
84
|
+
/** Text flashed in the track after the lane is completed. */
|
|
85
|
+
successLabel: { type: String, default: undefined },
|
|
86
|
+
disabled: { type: Boolean, default: false },
|
|
87
|
+
/** Pins the random route (tests); absent = fresh random route per
|
|
88
|
+
* mount. A change regenerates the lane and resets progress. */
|
|
89
|
+
seed: { type: Number, default: undefined },
|
|
90
|
+
},
|
|
91
|
+
emits: {
|
|
92
|
+
/** Fired ONCE when the knob completes the lane. The component resets
|
|
93
|
+
* on regeneration/mount only — the host closes its surface. */
|
|
94
|
+
confirm: () => true,
|
|
95
|
+
},
|
|
96
|
+
setup(props, { emit }) {
|
|
97
|
+
const { t } = useI18n();
|
|
98
|
+
|
|
99
|
+
const rootRef = ref<HTMLElement | null>(null);
|
|
100
|
+
/** Knob element — the pointer-capture and slider-handle host. */
|
|
101
|
+
const knobRef = ref<HTMLElement | null>(null);
|
|
102
|
+
|
|
103
|
+
const path = ref<SlidePath>(generateSlidePath((props.seed ?? (Math.random() * 0xffffffff)) >>> 0));
|
|
104
|
+
|
|
105
|
+
/** Arc-length progress 0..1 along the lane. */
|
|
106
|
+
const progress = ref(0);
|
|
107
|
+
type SlideStatus = "idle" | "dragging" | "snapping" | "done";
|
|
108
|
+
const status = ref<SlideStatus>("idle");
|
|
109
|
+
|
|
110
|
+
const dragging = () => status.value === "dragging";
|
|
111
|
+
|
|
112
|
+
/** A new seed (or a fresh mount) lays a fresh route and resets the
|
|
113
|
+
* gesture. Declared after the state refs it writes. */
|
|
114
|
+
const regenerate = () => {
|
|
115
|
+
path.value = generateSlidePath((props.seed ?? (Math.random() * 0xffffffff)) >>> 0);
|
|
116
|
+
progress.value = 0;
|
|
117
|
+
status.value = "idle";
|
|
118
|
+
};
|
|
119
|
+
watch(() => props.seed, regenerate);
|
|
120
|
+
|
|
121
|
+
/** The measured track box — reactive so the lane and knob re-map when
|
|
122
|
+
* it lands (mount) or moves (resize). jsdom measures 0×0 until a
|
|
123
|
+
* test injects a rect. */
|
|
124
|
+
const box = ref<TrackBox>({ width: 0, height: 0, left: 0, top: 0 });
|
|
125
|
+
function measure() {
|
|
126
|
+
const rect = rootRef.value?.getBoundingClientRect();
|
|
127
|
+
if (rect && rect.width > 0) {
|
|
128
|
+
box.value = { width: rect.width, height: rect.height, left: rect.left, top: rect.top };
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// ResizeObserver (the 42-sibling house pattern): a track mounted
|
|
132
|
+
// before layout (hidden/collapsed container) measures 0×0 — the
|
|
133
|
+
// observer re-measures the moment the box exists, instead of leaving
|
|
134
|
+
// a knob with no visible route until the first grab.
|
|
135
|
+
let resizeObserver: ResizeObserver | null = null;
|
|
136
|
+
onMounted(() => {
|
|
137
|
+
measure();
|
|
138
|
+
resizeObserver = new ResizeObserver(() => {
|
|
139
|
+
box.value = { width: 0, height: 0, left: 0, top: 0 };
|
|
140
|
+
measure();
|
|
141
|
+
});
|
|
142
|
+
if (rootRef.value) resizeObserver.observe(rootRef.value);
|
|
143
|
+
});
|
|
144
|
+
onBeforeUnmount(() => {
|
|
145
|
+
resizeObserver?.disconnect();
|
|
146
|
+
if (snapTimer) clearTimeout(snapTimer);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
function pixelPolyline(): SlidePoint[] {
|
|
150
|
+
const b = box.value;
|
|
151
|
+
return slidePixelPoints(
|
|
152
|
+
path.value.points,
|
|
153
|
+
Math.max(0, b.width - TRACK_PAD * 2),
|
|
154
|
+
Math.max(0, b.height - TRACK_PAD * 2),
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Knob center in TRACK-LOCAL px (the transform target). */
|
|
159
|
+
const knobPos = computed<SlidePoint>(() => slidePointAtT(pixelPolyline(), progress.value));
|
|
160
|
+
|
|
161
|
+
/** Lane polyline in px (SVG points space; the svg is positioned at
|
|
162
|
+
* TRACK_PAD inset with a matching viewBox). */
|
|
163
|
+
const laneBox = computed(() => {
|
|
164
|
+
const b = box.value;
|
|
165
|
+
const w = Math.max(0, b.width - TRACK_PAD * 2);
|
|
166
|
+
const h = Math.max(0, b.height - TRACK_PAD * 2);
|
|
167
|
+
return { w, h };
|
|
168
|
+
});
|
|
169
|
+
const laneSvgPoints = computed(() => {
|
|
170
|
+
const { w, h } = laneBox.value;
|
|
171
|
+
return slidePixelPoints(path.value.points, w, h)
|
|
172
|
+
.map((p) => `${p.x.toFixed(1)},${p.y.toFixed(1)}`)
|
|
173
|
+
.join(" ");
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
let snapTimer: ReturnType<typeof setTimeout> | null = null;
|
|
177
|
+
function clearSnapTimer() {
|
|
178
|
+
if (snapTimer) {
|
|
179
|
+
clearTimeout(snapTimer);
|
|
180
|
+
snapTimer = null;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function snapBack() {
|
|
185
|
+
status.value = "snapping";
|
|
186
|
+
progress.value = 0;
|
|
187
|
+
clearSnapTimer();
|
|
188
|
+
// The transition (SCSS) carries the visual return; the state resets
|
|
189
|
+
// after it so the next grab starts clean.
|
|
190
|
+
snapTimer = setTimeout(() => {
|
|
191
|
+
status.value = "idle";
|
|
192
|
+
}, 260);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function complete() {
|
|
196
|
+
if (status.value === "done") return;
|
|
197
|
+
status.value = "done";
|
|
198
|
+
// A stale snap-back timer must never tear the done state down (it
|
|
199
|
+
// would resurrect the control and allow a second emit).
|
|
200
|
+
clearSnapTimer();
|
|
201
|
+
emit("confirm");
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** The pointer offset from the knob CENTER at grab time — subtracted
|
|
205
|
+
* from every move so the knob CENTER (not the raw pointer) is what
|
|
206
|
+
* traces the lane. Without this, grabbing the knob's edge offsets
|
|
207
|
+
* the whole trace by that edge and lets a straight drag ride a lane
|
|
208
|
+
* it never turned onto. */
|
|
209
|
+
let grabOffset: SlidePoint = { x: 0, y: 0 };
|
|
210
|
+
/** The pointer's last swept position in the lane frame — each move
|
|
211
|
+
* sweeps a SEGMENT from here to the new position, and lane samples
|
|
212
|
+
* count as traced only if that segment passes within the corridor
|
|
213
|
+
* (a fast drag sweeps a line, not a point). */
|
|
214
|
+
let lastTrace: SlidePoint | null = null;
|
|
215
|
+
|
|
216
|
+
function onKnobPointerdown(e: PointerEvent) {
|
|
217
|
+
// The house drag-family guard: only the primary button traces.
|
|
218
|
+
if (e.button !== 0) return;
|
|
219
|
+
if (props.disabled || status.value === "done") return;
|
|
220
|
+
// A regrab inside the snap-back window is a FRESH gesture: cancel
|
|
221
|
+
// the pending timer (an uncancelled one would flip the status to
|
|
222
|
+
// idle mid-gesture — freezing the drag — or tear down a done state).
|
|
223
|
+
clearSnapTimer();
|
|
224
|
+
try {
|
|
225
|
+
knobRef.value?.setPointerCapture?.(e.pointerId);
|
|
226
|
+
} catch {
|
|
227
|
+
// jsdom/test environments have no capture — the move/up wiring
|
|
228
|
+
// below is on the knob itself and still fires.
|
|
229
|
+
}
|
|
230
|
+
box.value = { width: 0, height: 0, left: 0, top: 0 }; // re-measure per gesture
|
|
231
|
+
measure();
|
|
232
|
+
const b = box.value;
|
|
233
|
+
const inner = b.height - TRACK_PAD * 2;
|
|
234
|
+
if (inner < 24) return; // no meaningful three-lane band
|
|
235
|
+
const start = slidePointAtT(pixelPolyline(), progress.value);
|
|
236
|
+
grabOffset = {
|
|
237
|
+
x: e.clientX - b.left - TRACK_PAD - start.x,
|
|
238
|
+
y: e.clientY - b.top - TRACK_PAD - start.y,
|
|
239
|
+
};
|
|
240
|
+
lastTrace = { ...start };
|
|
241
|
+
status.value = "dragging";
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** One traced event (real, or a coalesced sub-sample): the pointer
|
|
245
|
+
* moved to a new position — sweep the segment, advance. */
|
|
246
|
+
function traceEvent(e: PointerEvent) {
|
|
247
|
+
// `disabled` may flip mid-gesture (a host gating on an async state):
|
|
248
|
+
// the gesture then ends like a refusal instead of tracking a dead
|
|
249
|
+
// control and emitting confirm.
|
|
250
|
+
if (props.disabled) {
|
|
251
|
+
if (status.value === "dragging") snapBack();
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (status.value !== "dragging") return;
|
|
255
|
+
const b = box.value;
|
|
256
|
+
const inner = b.height - TRACK_PAD * 2;
|
|
257
|
+
if (inner < 24) return;
|
|
258
|
+
const pixel = pixelPolyline();
|
|
259
|
+
const total = slidePathLength(pixel);
|
|
260
|
+
if (total <= 0) return;
|
|
261
|
+
// The pointer arrives in TRACK-LOCAL px; the lane lives in the
|
|
262
|
+
// TRACK_PAD-inset frame — compare in the lane's own frame, minus
|
|
263
|
+
// the grab offset (the knob CENTER is the traced point).
|
|
264
|
+
const tx = e.clientX - b.left - TRACK_PAD - grabOffset.x;
|
|
265
|
+
const ty = e.clientY - b.top - TRACK_PAD - grabOffset.y;
|
|
266
|
+
const sweepFrom = lastTrace ?? { x: tx, y: ty };
|
|
267
|
+
const tolerance = corridorRadius(inner);
|
|
268
|
+
|
|
269
|
+
// Sweep-segment walk: advance from the current progress while each
|
|
270
|
+
// successive lane sample stays within the corridor of the SEGMENT
|
|
271
|
+
// the pointer swept since the last event. A fast honest drag sweeps
|
|
272
|
+
// a line along the lane and every sample under it counts; a corner
|
|
273
|
+
// stops the walk (the sweep line never reaches the jumped lane), so
|
|
274
|
+
// progress requires actual steering — and a straight pull held one
|
|
275
|
+
// lane off can never chord through a turn.
|
|
276
|
+
const from = progress.value;
|
|
277
|
+
let advanced: number | null = null;
|
|
278
|
+
for (let i = 0; i <= SCAN_STEPS; i++) {
|
|
279
|
+
const t = (from * SCAN_STEPS + i) / SCAN_STEPS;
|
|
280
|
+
if (t > 1) break;
|
|
281
|
+
const p = slidePointAtT(pixel, t);
|
|
282
|
+
if (slideSegmentDistance(p, sweepFrom, { x: tx, y: ty }) <= tolerance) {
|
|
283
|
+
advanced = t;
|
|
284
|
+
} else {
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
lastTrace = { x: tx, y: ty };
|
|
289
|
+
if (advanced === null) {
|
|
290
|
+
// The sweep never touched the lane: the pointer no longer
|
|
291
|
+
// represents the knob.
|
|
292
|
+
snapBack();
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
// Commit the advance, then completion (the knob lands at the lane's
|
|
296
|
+
// very end; Vue renders the final aria-valuenow asynchronously).
|
|
297
|
+
progress.value = advanced;
|
|
298
|
+
if (advanced >= COMPLETE_T) {
|
|
299
|
+
complete();
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
// Runaway check: the walk stopped at a gap AND the pointer ended
|
|
303
|
+
// more than one corridor past where the knob ended up — the pointer
|
|
304
|
+
// has left the lane for good (corridor exit). The bound is 2× the
|
|
305
|
+
// corridor: the walk legitimately leaves the knob one corridor
|
|
306
|
+
// AHEAD of the pointer, so a 1× bound would turn ordinary aim error
|
|
307
|
+
// into a refusal (the R4 probe: ±3px lateral kills the gesture).
|
|
308
|
+
const knobPoint = slidePointAtT(pixel, advanced);
|
|
309
|
+
if (Math.hypot(tx - knobPoint.x, ty - knobPoint.y) > tolerance * 2) {
|
|
310
|
+
snapBack();
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function onKnobPointermove(e: PointerEvent) {
|
|
316
|
+
if (status.value !== "dragging") return;
|
|
317
|
+
// Sub-divide with the browser's own coalesced sub-samples: a fast
|
|
318
|
+
// drag's coalesced frames each carry small pointer steps, so the
|
|
319
|
+
// walk traces through corners honestly. CAVEAT: Chromium removes
|
|
320
|
+
// getCoalescedEvents in NON-SECURE contexts — a plain-HTTP page
|
|
321
|
+
// keeps the single-event ceiling below (synthetic dispatchers too):
|
|
322
|
+
// one event straddling a corner by more than 2√2·corridor aborts.
|
|
323
|
+
const coalesced = (
|
|
324
|
+
e as PointerEvent & { getCoalescedEvents?: () => PointerEvent[] }
|
|
325
|
+
).getCoalescedEvents?.();
|
|
326
|
+
if (coalesced && coalesced.length > 0) {
|
|
327
|
+
for (const sub of coalesced) traceEvent(sub);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
traceEvent(e);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function onKnobPointerup() {
|
|
334
|
+
if (status.value !== "dragging") return;
|
|
335
|
+
if (progress.value < COMPLETE_T) snapBack();
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** Keyboard walk step: one lane-step ≈ 1/24 of the lane (each arrow
|
|
339
|
+
* press makes visible progress; ~24 presses traverse the full lane,
|
|
340
|
+
* and the turns come for free from the arc-length parameter). */
|
|
341
|
+
const KEY_STEP = 1 / 24;
|
|
342
|
+
function onKnobKeydown(e: KeyboardEvent) {
|
|
343
|
+
if (props.disabled || status.value === "done") return;
|
|
344
|
+
let handled = true;
|
|
345
|
+
switch (e.key) {
|
|
346
|
+
case "ArrowRight":
|
|
347
|
+
case "ArrowUp":
|
|
348
|
+
progress.value = Math.min(1, progress.value + KEY_STEP);
|
|
349
|
+
break;
|
|
350
|
+
case "ArrowLeft":
|
|
351
|
+
case "ArrowDown":
|
|
352
|
+
progress.value = Math.max(0, progress.value - KEY_STEP);
|
|
353
|
+
break;
|
|
354
|
+
case "Home":
|
|
355
|
+
progress.value = 0;
|
|
356
|
+
break;
|
|
357
|
+
case "End":
|
|
358
|
+
progress.value = 1;
|
|
359
|
+
break;
|
|
360
|
+
default:
|
|
361
|
+
handled = false;
|
|
362
|
+
}
|
|
363
|
+
if (handled) {
|
|
364
|
+
e.preventDefault();
|
|
365
|
+
if (progress.value >= COMPLETE_T) complete();
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const idleLabel = computed(
|
|
370
|
+
() => props.label ?? t("hikari::slideConfirm.drag", "Hold the knob and drag along the lane to the right edge"),
|
|
371
|
+
);
|
|
372
|
+
const doneLabel = computed(
|
|
373
|
+
() => props.successLabel ?? t("hikari::slideConfirm.success", "Confirmed"),
|
|
374
|
+
);
|
|
375
|
+
|
|
376
|
+
return () => {
|
|
377
|
+
const done = status.value === "done";
|
|
378
|
+
const { w, h } = laneBox.value;
|
|
379
|
+
const pos = knobPos.value;
|
|
380
|
+
return (
|
|
381
|
+
<div
|
|
382
|
+
class={{
|
|
383
|
+
"hk-slide-confirm": true,
|
|
384
|
+
"is-dragging": status.value === "dragging",
|
|
385
|
+
"is-done": done,
|
|
386
|
+
"is-disabled": props.disabled,
|
|
387
|
+
}}
|
|
388
|
+
ref={rootRef}
|
|
389
|
+
>
|
|
390
|
+
{/* The lane: px-space polyline inside a TRACK_PAD-inset svg —
|
|
391
|
+
the knob rides the same coordinate frame. */}
|
|
392
|
+
<svg
|
|
393
|
+
class="hk-slide-confirm-lane"
|
|
394
|
+
aria-hidden="true"
|
|
395
|
+
style={{ inset: `${TRACK_PAD}px` }}
|
|
396
|
+
width={w}
|
|
397
|
+
height={h}
|
|
398
|
+
viewBox={`0 0 ${w} ${h}`}
|
|
399
|
+
>
|
|
400
|
+
<polyline points={laneSvgPoints.value} class={{ "hk-slide-confirm-lane-path": true, "is-done": done }} />
|
|
401
|
+
</svg>
|
|
402
|
+
{!done ? (
|
|
403
|
+
<span class="hk-slide-confirm-label" aria-hidden="true">
|
|
404
|
+
{idleLabel.value}
|
|
405
|
+
</span>
|
|
406
|
+
) : (
|
|
407
|
+
<span class="hk-slide-confirm-done-label" aria-hidden="true">
|
|
408
|
+
{doneLabel.value}
|
|
409
|
+
</span>
|
|
410
|
+
)}
|
|
411
|
+
<div
|
|
412
|
+
ref={knobRef}
|
|
413
|
+
class={{ "hk-slide-confirm-knob": true, "is-done": done }}
|
|
414
|
+
role="slider"
|
|
415
|
+
tabindex={props.disabled ? -1 : 0}
|
|
416
|
+
aria-disabled={props.disabled || done}
|
|
417
|
+
aria-label={done ? doneLabel.value : idleLabel.value}
|
|
418
|
+
aria-valuemin={0}
|
|
419
|
+
aria-valuemax={100}
|
|
420
|
+
aria-valuenow={Math.round(progress.value * 100)}
|
|
421
|
+
aria-valuetext={done ? doneLabel.value : `${Math.round(progress.value * 100)}%`}
|
|
422
|
+
style={{
|
|
423
|
+
// The lane frame is inset by TRACK_PAD; the knob (absolutely
|
|
424
|
+
// parked at the track's corner) rides it from there.
|
|
425
|
+
transform: `translate(${(TRACK_PAD + pos.x).toFixed(1)}px, ${(TRACK_PAD + pos.y).toFixed(1)}px)`,
|
|
426
|
+
}}
|
|
427
|
+
onPointerdown={onKnobPointerdown}
|
|
428
|
+
onPointermove={onKnobPointermove}
|
|
429
|
+
onPointerup={onKnobPointerup}
|
|
430
|
+
onPointercancel={onKnobPointerup}
|
|
431
|
+
onKeydown={onKnobKeydown}
|
|
432
|
+
>
|
|
433
|
+
<span class="hk-slide-confirm-knob-grip" aria-hidden="true" />
|
|
434
|
+
</div>
|
|
435
|
+
</div>
|
|
436
|
+
);
|
|
437
|
+
};
|
|
438
|
+
},
|
|
439
|
+
});
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// slideConfirmPath — the pure geometry half of HkSlideConfirm. These
|
|
2
|
+
// pins carry the SHAPE contract: ≥3 random right-angle turns through
|
|
3
|
+
// three lanes, monotonic left→right, seeded determinism (the captcha
|
|
4
|
+
// property: a seed reproduces the exact route), and the arc-length math
|
|
5
|
+
// the knob's follow/snap mechanics ride on.
|
|
6
|
+
import { describe, expect, it } from "vitest";
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
generateSlidePath,
|
|
10
|
+
slideConfirmCorridor,
|
|
11
|
+
slideLaneY,
|
|
12
|
+
slidePathLength,
|
|
13
|
+
slidePixelPoints,
|
|
14
|
+
slidePointAtT,
|
|
15
|
+
slideSegmentDistance,
|
|
16
|
+
} from "./slideConfirmPath";
|
|
17
|
+
|
|
18
|
+
describe("generateSlidePath", () => {
|
|
19
|
+
it("a seed reproduces the exact route (the captcha property)", () => {
|
|
20
|
+
const a = generateSlidePath(20261119);
|
|
21
|
+
const b = generateSlidePath(20261119);
|
|
22
|
+
expect(a).toEqual(b);
|
|
23
|
+
const c = generateSlidePath(777);
|
|
24
|
+
expect(c).not.toEqual(a);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("carries at least three right-angle turns (the fat-confirm contract)", () => {
|
|
28
|
+
for (const seed of [1, 2, 3, 42, 999, 20261119, 7, 8, 9, 10]) {
|
|
29
|
+
const { points, turns } = generateSlidePath(seed);
|
|
30
|
+
// A turn = a vertical jump: consecutive points sharing x but not y.
|
|
31
|
+
let jumps = 0;
|
|
32
|
+
for (let i = 1; i < points.length; i++) {
|
|
33
|
+
if (points[i]!.x === points[i - 1]!.x && points[i]!.y !== points[i - 1]!.y) jumps++;
|
|
34
|
+
}
|
|
35
|
+
expect(jumps, `seed ${seed}`).toBe(turns);
|
|
36
|
+
expect(turns, `seed ${seed}`).toBeGreaterThanOrEqual(3);
|
|
37
|
+
expect(turns, `seed ${seed}`).toBeLessThanOrEqual(5);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("walks left to right monotonically and lands on the edges", () => {
|
|
42
|
+
for (const seed of [1, 42, 20261119]) {
|
|
43
|
+
const { points } = generateSlidePath(seed);
|
|
44
|
+
expect(points[0]!.x).toBe(0);
|
|
45
|
+
expect(points[points.length - 1]!.x).toBe(1);
|
|
46
|
+
for (let i = 1; i < points.length; i++) {
|
|
47
|
+
expect(points[i]!.x, `seed ${seed} step ${i}`).toBeGreaterThanOrEqual(points[i - 1]!.x);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("never overshoots the band: every turn column stays in [lo, hi]", () => {
|
|
53
|
+
// The min-gap floor clamps nothing — a squeezed draw used to push
|
|
54
|
+
// turn columns past x=1 and run the closing segment BACKWARDS.
|
|
55
|
+
for (let seed = 1; seed <= 2000; seed++) {
|
|
56
|
+
const { points } = generateSlidePath(seed);
|
|
57
|
+
let prevX = -1;
|
|
58
|
+
for (const p of points) {
|
|
59
|
+
expect(p.x, `seed ${seed}`).toBeGreaterThanOrEqual(prevX);
|
|
60
|
+
expect(p.x, `seed ${seed}`).toBeLessThanOrEqual(1);
|
|
61
|
+
prevX = p.x;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("turn columns keep the minimum spacing (grabbable horizontal runs)", () => {
|
|
67
|
+
for (let seed = 1; seed <= 2000; seed++) {
|
|
68
|
+
const { points } = generateSlidePath(seed);
|
|
69
|
+
const cols = points.filter((p) => p.x > 0 && p.x < 1).map((p) => p.x);
|
|
70
|
+
const uniq = [...new Set(cols)];
|
|
71
|
+
// Spacing between distinct turn columns stays at or above the
|
|
72
|
+
// floor (0.12); a squeeze collapses to the even fallback, which is
|
|
73
|
+
// spaced (hi - lo) / turns ≥ 0.12 for 3..5 turns in [0.14, 0.86].
|
|
74
|
+
for (let i = 1; i < uniq.length; i++) {
|
|
75
|
+
expect(uniq[i]! - uniq[i - 1]!, `seed ${seed}`).toBeGreaterThanOrEqual(0.12 - 1e-9);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("stays on the three lanes and never repeats a lane across a turn", () => {
|
|
81
|
+
for (const seed of [1, 42, 20261119]) {
|
|
82
|
+
const { points } = generateSlidePath(seed);
|
|
83
|
+
const laneYs = [slideLaneY(0), slideLaneY(1), slideLaneY(2)];
|
|
84
|
+
let lastLane = -1;
|
|
85
|
+
for (const p of points) {
|
|
86
|
+
expect(laneYs).toContain(p.y);
|
|
87
|
+
}
|
|
88
|
+
for (let i = 0; i < points.length; i++) {
|
|
89
|
+
const lane = laneYs.indexOf(points[i]!.y);
|
|
90
|
+
if (lane !== lastLane) {
|
|
91
|
+
// A lane change is a vertical jump at a turn column — the new
|
|
92
|
+
// lane must differ from the old one (never a zero-length turn).
|
|
93
|
+
if (i > 0 && points[i]!.x === points[i - 1]!.x) {
|
|
94
|
+
expect(lane, `seed ${seed} step ${i}`).not.toBe(lastLane);
|
|
95
|
+
}
|
|
96
|
+
lastLane = lane;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe("slideConfirmCorridor — the constant is the defense", () => {
|
|
104
|
+
it("stays BELOW half the lane spacing (the anti-straight-pull bound)", () => {
|
|
105
|
+
// The corridor is the ONLY thing stopping a straight pull from riding
|
|
106
|
+
// one lane's corridor across routes that revisit adjacent lanes — at
|
|
107
|
+
// +4 slack (the R3 shape) the radius passed half the lane spacing and
|
|
108
|
+
// 62/600 seeds completed with a straight pull. This pin bites the
|
|
109
|
+
// constant directly: the runaway check masks behavior pins.
|
|
110
|
+
for (let inner = 24; inner <= 240; inner += 8) {
|
|
111
|
+
const spacing = inner / 3;
|
|
112
|
+
expect(slideConfirmCorridor(inner), `inner ${inner}`).toBeLessThan(spacing / 2);
|
|
113
|
+
expect(slideConfirmCorridor(inner), `inner ${inner}`).toBeGreaterThan(0);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("the default geometry reads 10px (inner/6 − 2 at 72)", () => {
|
|
118
|
+
expect(slideConfirmCorridor(72)).toBe(10);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("pixel mapping and arc-length", () => {
|
|
123
|
+
// The COMPONENT's real lane frame: a 300×120 track minus the 24px
|
|
124
|
+
// TRACK_PAD on each side = 252×72, corridor = slideConfirmCorridor(72)
|
|
125
|
+
// = 10px (half the 24px lane spacing minus slack — the constant has its
|
|
126
|
+
// own describe block above). Pinning the raw math in THIS frame is what
|
|
127
|
+
// makes the component's corridor constant measurable (a 300×120 frame
|
|
128
|
+
// would silently measure a different physical quantity than the
|
|
129
|
+
// shipped code).
|
|
130
|
+
const path = generateSlidePath(20261119);
|
|
131
|
+
const pixel = slidePixelPoints(path.points, 252, 72);
|
|
132
|
+
|
|
133
|
+
it("maps grid fractions to the measured box", () => {
|
|
134
|
+
expect(pixel[0]!.x).toBe(0);
|
|
135
|
+
// The lane fraction maps into the SAME frame the component draws in
|
|
136
|
+
// (252×72 here): a lane fraction 0.5 → the box's mid-height 36.
|
|
137
|
+
expect(pixel[0]!.y).toBeCloseTo(path.points[0]!.y * 72, 5);
|
|
138
|
+
const last = pixel[pixel.length - 1]!;
|
|
139
|
+
expect(last.x).toBe(252);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("slideSegmentDistance measures point-to-segment honestly", () => {
|
|
143
|
+
const a = { x: 0, y: 0 };
|
|
144
|
+
const b = { x: 100, y: 0 };
|
|
145
|
+
// On the segment (between the endpoints): the perpendicular foot.
|
|
146
|
+
expect(slideSegmentDistance({ x: 50, y: 12 }, a, b)).toBe(12);
|
|
147
|
+
// Before/after the segment: the endpoint distance (clamped foot).
|
|
148
|
+
expect(slideSegmentDistance({ x: -30, y: 0 }, a, b)).toBe(30);
|
|
149
|
+
expect(slideSegmentDistance({ x: 150, y: 0 }, a, b)).toBe(50);
|
|
150
|
+
// A zero-length segment degrades to the point distance.
|
|
151
|
+
expect(slideSegmentDistance({ x: 3, y: 4 }, a, a)).toBe(5);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("pointAtT walks the endpoints and keeps arc length honest", () => {
|
|
155
|
+
const start = slidePointAtT(pixel, 0);
|
|
156
|
+
expect(start.x).toBeCloseTo(pixel[0]!.x, 5);
|
|
157
|
+
expect(start.y).toBeCloseTo(pixel[0]!.y, 5);
|
|
158
|
+
|
|
159
|
+
const end = slidePointAtT(pixel, 1);
|
|
160
|
+
const last = pixel[pixel.length - 1]!;
|
|
161
|
+
expect(end.x).toBeCloseTo(last.x, 5);
|
|
162
|
+
expect(end.y).toBeCloseTo(last.y, 5);
|
|
163
|
+
|
|
164
|
+
// The length of a fine polyline walk equals the total length — the
|
|
165
|
+
// arc-length parameterization is consistent.
|
|
166
|
+
const steps = 4000;
|
|
167
|
+
let walked = 0;
|
|
168
|
+
let prev = slidePointAtT(pixel, 0);
|
|
169
|
+
for (let i = 1; i <= steps; i++) {
|
|
170
|
+
const p = slidePointAtT(pixel, i / steps);
|
|
171
|
+
walked += Math.hypot(p.x - prev.x, p.y - prev.y);
|
|
172
|
+
prev = p;
|
|
173
|
+
}
|
|
174
|
+
// 4000 linear-t steps chord the corners slightly — the walked sum
|
|
175
|
+
// must converge to the total length within a fraction of a percent.
|
|
176
|
+
expect(Math.abs(walked - slidePathLength(pixel))).toBeLessThan(slidePathLength(pixel) * 0.001);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
});
|