@celestia-island/hikari 0.55.454 → 0.55.455
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/dist/types/composables/useHostPromotion.d.ts +24 -0
- package/dist/types/composables/useOverviewRuler.d.ts +77 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +1 -1
- package/src/composables/useHostPromotion.ts +58 -0
- package/src/composables/useOverlayScrollbar.ts +14 -21
- package/src/composables/useOverviewRuler.behavior.test.ts +324 -0
- package/src/composables/useOverviewRuler.contract.test.ts +76 -0
- package/src/composables/useOverviewRuler.ts +395 -0
- package/src/index.ts +9 -0
- package/src/styles/index.scss +1 -0
- package/src/styles/overview-ruler.scss +87 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reference-counted host-position promotion, shared by every composable
|
|
3
|
+
* that needs a positioned positioning context (the overlay scrollbar and
|
|
4
|
+
* the overview ruler). A module-private WeakMap per composable is the
|
|
5
|
+
* exact accident this exists to prevent: ruler A's detach stripping the
|
|
6
|
+
* positioning out from under a still-live scrollbar B on the same host.
|
|
7
|
+
*
|
|
8
|
+
* Contract:
|
|
9
|
+
* - `promoteHost(host)` counts the caller and returns its release;
|
|
10
|
+
* - the FIRST caller on a static/empty-position host sets
|
|
11
|
+
* `position: relative`; every later caller (whatever the computed
|
|
12
|
+
* position now is) is still counted;
|
|
13
|
+
* - the LAST release restores the PRE-ATTACH inline position — unless a
|
|
14
|
+
* consumer re-styled the host after attach (their style wins; the
|
|
15
|
+
* saved value stays dead).
|
|
16
|
+
*/
|
|
17
|
+
export interface PromoteHostHandle {
|
|
18
|
+
release(): void;
|
|
19
|
+
/** Whether THIS call applied the promotion (count reached 1 on a
|
|
20
|
+
* static/empty host). Consumers use it to gate companion markers that
|
|
21
|
+
* only make sense when the promotion actually happened. */
|
|
22
|
+
promoted: boolean;
|
|
23
|
+
}
|
|
24
|
+
export declare function promoteHost(host: HTMLElement): PromoteHostHandle;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useOverviewRuler — a standalone minimap strip ("竖向瀑布流") that any
|
|
3
|
+
* scrollable element can attach, the same way attachOverlayScrollbars
|
|
4
|
+
* attaches the scrollbar. It renders a proportional density map of the
|
|
5
|
+
* viewport's content (one band per top-level block, sized by its share of
|
|
6
|
+
* the content extent) plus a cursor marking the visible window; dragging
|
|
7
|
+
* scrolls the viewport, clicking jumps.
|
|
8
|
+
*
|
|
9
|
+
* Design direction (2026-11-21 user ruling): the minimap is a STANDALONE
|
|
10
|
+
* attachment — not a mode of the scrollbar — and it is the same primitive
|
|
11
|
+
* a waterfall layout needs: a vertical (or horizontal) strip of
|
|
12
|
+
* proportional bands with a draggable cursor. Direction and cursor
|
|
13
|
+
* behaviour are options, not separate components.
|
|
14
|
+
*
|
|
15
|
+
* Two cursor modes:
|
|
16
|
+
* - "jump" (default): pointer down anywhere on the strip scrolls so the
|
|
17
|
+
* content window under the pointer becomes the visible window, then
|
|
18
|
+
* keeps dragging (双向可拖动 — both the cursor and a strip click steer
|
|
19
|
+
* the scroll);
|
|
20
|
+
* - "thumb": only the cursor box is draggable; strip clicks are inert.
|
|
21
|
+
*
|
|
22
|
+
* Sampling: bands are harvested from `bandSelector` matches inside the
|
|
23
|
+
* viewport (default: the viewport's DIRECT element children). Nested
|
|
24
|
+
* matches are dropped when a match contains another (a parent band's
|
|
25
|
+
* extent already contains its children's — counting both would paint the
|
|
26
|
+
* same content twice). Caller-driven content (virtualized rows, a canvas
|
|
27
|
+
* terminal) should pass a `bandSelector` for whatever stands in for lines,
|
|
28
|
+
* or drive `setBands` directly with fractions.
|
|
29
|
+
*
|
|
30
|
+
* The strip sits at z-index 9 — UNDER the overlay scrollbar's interactive
|
|
31
|
+
* track (z-index 10, see theme/scrollbar.scss) so a host that attaches
|
|
32
|
+
* both keeps fine scrolling on the track while the ruler carries the map.
|
|
33
|
+
*/
|
|
34
|
+
export type OverviewRulerAxis = "vertical" | "horizontal";
|
|
35
|
+
export type OverviewRulerCursorMode = "jump" | "thumb";
|
|
36
|
+
export interface OverviewRulerBand {
|
|
37
|
+
/** Band start as a fraction of the content extent, 0..1. */
|
|
38
|
+
start: number;
|
|
39
|
+
/** Band length as a fraction of the content extent, 0..1. */
|
|
40
|
+
length: number;
|
|
41
|
+
/** Extra class for the band (e.g. severity coloring for log lines). */
|
|
42
|
+
modifier?: string;
|
|
43
|
+
}
|
|
44
|
+
export interface OverviewRulerOptions {
|
|
45
|
+
/** Strip direction. Default "vertical" (the waterfall). */
|
|
46
|
+
axis?: OverviewRulerAxis;
|
|
47
|
+
/** Strip thickness in CSS px. Default 48 (vertical) / 36 (horizontal). */
|
|
48
|
+
thickness?: number;
|
|
49
|
+
/** Which edge of the host the strip docks to. Default "end"
|
|
50
|
+
* (right for vertical, bottom for horizontal). */
|
|
51
|
+
side?: "start" | "end";
|
|
52
|
+
/** Selector harvested INSIDE the viewport for band sources. Matches
|
|
53
|
+
* contained within another match are dropped (the parent band already
|
|
54
|
+
* covers them). Default: the viewport's DIRECT element children. */
|
|
55
|
+
bandSelector?: string;
|
|
56
|
+
/** Cursor behaviour: "jump" (default) = strip pointer down jumps then
|
|
57
|
+
* drags; "thumb" = only the cursor box is draggable. */
|
|
58
|
+
cursorDrag?: OverviewRulerCursorMode;
|
|
59
|
+
/** Element the strip is appended to — its positioning context. Same
|
|
60
|
+
* contract as OverlayScrollbarOptions.host: a box wrapping exactly the
|
|
61
|
+
* viewport. Defaults to the viewport's DOM parent. */
|
|
62
|
+
host?: HTMLElement;
|
|
63
|
+
/** Content-resample cadence in ms (bands, not the cursor — the cursor
|
|
64
|
+
* tracks scroll and resize live). Default 250. */
|
|
65
|
+
resampleMs?: number;
|
|
66
|
+
}
|
|
67
|
+
export interface OverviewRulerHandle {
|
|
68
|
+
/** Resample the bands and reposition the cursor. */
|
|
69
|
+
update(): void;
|
|
70
|
+
/** Replace the sampled bands with caller fractions (virtualized or
|
|
71
|
+
* canvas content). An empty list clears the waterfall. */
|
|
72
|
+
setBands(bands: OverviewRulerBand[]): void;
|
|
73
|
+
/** Remove the strip and every listener/observer; restore any inline
|
|
74
|
+
* positioning the promotion applied. Safe to call twice. */
|
|
75
|
+
detach(): void;
|
|
76
|
+
}
|
|
77
|
+
export declare function attachOverviewRuler(viewport: HTMLElement, opts?: OverviewRulerOptions): OverviewRulerHandle;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -550,3 +550,4 @@ export type { VirtualPointerSinks, UseVirtualPointer, } from "./composables/useV
|
|
|
550
550
|
export { useRemoteTouchGestures } from "./composables/useRemoteTouchGestures";
|
|
551
551
|
export type { TouchGestureHandlers } from "./composables/useRemoteTouchGestures";
|
|
552
552
|
export { useOrientationLock } from "./composables/useOrientationLock";
|
|
553
|
+
export { attachOverviewRuler, type OverviewRulerAxis, type OverviewRulerBand, type OverviewRulerCursorMode, type OverviewRulerHandle, type OverviewRulerOptions, } from "./composables/useOverviewRuler";
|
package/package.json
CHANGED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reference-counted host-position promotion, shared by every composable
|
|
3
|
+
* that needs a positioned positioning context (the overlay scrollbar and
|
|
4
|
+
* the overview ruler). A module-private WeakMap per composable is the
|
|
5
|
+
* exact accident this exists to prevent: ruler A's detach stripping the
|
|
6
|
+
* positioning out from under a still-live scrollbar B on the same host.
|
|
7
|
+
*
|
|
8
|
+
* Contract:
|
|
9
|
+
* - `promoteHost(host)` counts the caller and returns its release;
|
|
10
|
+
* - the FIRST caller on a static/empty-position host sets
|
|
11
|
+
* `position: relative`; every later caller (whatever the computed
|
|
12
|
+
* position now is) is still counted;
|
|
13
|
+
* - the LAST release restores the PRE-ATTACH inline position — unless a
|
|
14
|
+
* consumer re-styled the host after attach (their style wins; the
|
|
15
|
+
* saved value stays dead).
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const promotedParents = new WeakMap<HTMLElement, { count: number; saved: string }>();
|
|
19
|
+
|
|
20
|
+
export interface PromoteHostHandle {
|
|
21
|
+
release(): void;
|
|
22
|
+
/** Whether THIS call applied the promotion (count reached 1 on a
|
|
23
|
+
* static/empty host). Consumers use it to gate companion markers that
|
|
24
|
+
* only make sense when the promotion actually happened. */
|
|
25
|
+
promoted: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function promoteHost(host: HTMLElement): PromoteHostHandle {
|
|
29
|
+
let entry = promotedParents.get(host);
|
|
30
|
+
if (!entry) {
|
|
31
|
+
entry = { count: 0, saved: host.style.position };
|
|
32
|
+
promotedParents.set(host, entry);
|
|
33
|
+
}
|
|
34
|
+
entry.count += 1;
|
|
35
|
+
const computedPosition = getComputedStyle(host).position;
|
|
36
|
+
const promoted = entry.count === 1 && (computedPosition === "static" || computedPosition === "");
|
|
37
|
+
if (promoted) {
|
|
38
|
+
host.style.position = "relative";
|
|
39
|
+
}
|
|
40
|
+
let released = false;
|
|
41
|
+
return {
|
|
42
|
+
promoted,
|
|
43
|
+
release(): void {
|
|
44
|
+
if (released) return;
|
|
45
|
+
released = true;
|
|
46
|
+
const current = promotedParents.get(host);
|
|
47
|
+
if (!current || current !== entry) return;
|
|
48
|
+
entry.count -= 1;
|
|
49
|
+
if (entry.count <= 0) {
|
|
50
|
+
// Only undo OUR promotion: if a consumer re-positioned the host
|
|
51
|
+
// after attach, their inline style wins and the saved value stays
|
|
52
|
+
// dead (presence-gated, per R4b).
|
|
53
|
+
if (host.style.position === "relative") host.style.position = entry.saved;
|
|
54
|
+
promotedParents.delete(host);
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
// `detach()` on close/unmount so no DOM or listener leaks.
|
|
25
25
|
|
|
26
26
|
import { drawnScale } from "./layoutGeometry";
|
|
27
|
+
import { promoteHost } from "./useHostPromotion";
|
|
27
28
|
import { scheduleCronAfter, type CronHandle } from "../runtime/cronBus";
|
|
28
29
|
import { scheduleFrame, type AnimationHandle } from "../runtime/animationBus";
|
|
29
30
|
|
|
@@ -240,7 +241,7 @@ const POSITION_GUARD_ATTR = "data-hk-overlay-scrollbar-positioned";
|
|
|
240
241
|
* (e.g. sibling code panes reconciled independently); the inline
|
|
241
242
|
* `position: relative` must survive until the LAST handle detaches, so
|
|
242
243
|
* promotion is reference-counted. */
|
|
243
|
-
|
|
244
|
+
|
|
244
245
|
|
|
245
246
|
/** Read the scroll geometry of `viewport` for one axis. */
|
|
246
247
|
function axisMetrics(viewport: HTMLElement, horizontal: boolean) {
|
|
@@ -296,25 +297,21 @@ export function attachOverlayScrollbars(
|
|
|
296
297
|
// viewport's parent. A static parent would anchor them to the
|
|
297
298
|
// nearest positioned ancestor instead — promote it to `relative`
|
|
298
299
|
// and remember the previous inline value for restore on detach —
|
|
299
|
-
// reference-counted per parent (see
|
|
300
|
+
// reference-counted per parent (see useHostPromotion) so a sibling
|
|
300
301
|
// handle's detach cannot strip the positioning out from under the
|
|
301
302
|
// tracks that remain.
|
|
302
303
|
// (Real browsers always report "static" for unpositioned elements;
|
|
303
304
|
// happy-dom reports "" — both mean "no positioning context".)
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
305
|
+
// Positioning guard: the tracks are position:absolute inside the
|
|
306
|
+
// viewport's parent. A static parent would anchor them to the nearest
|
|
307
|
+
// positioned ancestor — promote it to `relative` through the shared
|
|
308
|
+
// reference-counted helper so a sibling composable's detach (the
|
|
309
|
+
// overview ruler attaches to the same hosts) cannot strip the
|
|
310
|
+
// positioning out from under the tracks that remain.
|
|
311
|
+
const promotion = promoteHost(parent);
|
|
312
|
+
if (promotion.promoted) {
|
|
313
|
+
parent.setAttribute(POSITION_GUARD_ATTR, "true");
|
|
311
314
|
}
|
|
312
|
-
// Count EVERY handle on a promoted parent — including handles whose
|
|
313
|
-
// attach sees the parent already `relative` (computed styles reflect
|
|
314
|
-
// the first handle's inline promotion). Skipping those would let the
|
|
315
|
-
// first detach strip the positioning out from under surviving tracks.
|
|
316
|
-
const entry = promotedParents.get(parent);
|
|
317
|
-
if (entry) entry.count++;
|
|
318
315
|
|
|
319
316
|
const states: AxisState[] = [];
|
|
320
317
|
if (wantV) states.push(makeAxisState(false));
|
|
@@ -688,13 +685,9 @@ export function attachOverlayScrollbars(
|
|
|
688
685
|
state.hideTimer?.disconnect();
|
|
689
686
|
state.track.remove();
|
|
690
687
|
}
|
|
691
|
-
|
|
692
|
-
if (
|
|
693
|
-
// Last handle on this parent is gone — restore its inline
|
|
694
|
-
// positioning exactly as the guard found it.
|
|
695
|
-
parent.style.position = entry.saved;
|
|
688
|
+
promotion.release();
|
|
689
|
+
if (parent.style.position !== "relative") {
|
|
696
690
|
parent.removeAttribute(POSITION_GUARD_ATTR);
|
|
697
|
-
promotedParents.delete(parent);
|
|
698
691
|
}
|
|
699
692
|
},
|
|
700
693
|
};
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Behavioural tests for the overview ruler: real mounts with STUBBED
|
|
3
|
+
* geometry (happy-dom reports no layout, so clientHeight/scrollHeight and
|
|
4
|
+
* getBoundingClientRect are seeded per test — the engine math then runs
|
|
5
|
+
* for real). Pins the R2/R3 geometry findings:
|
|
6
|
+
* - the cursor edge travels progress × (1 − length) — the overlay
|
|
7
|
+
* scrollbar's own mapping — and lands at 100% at max scroll (S1-1);
|
|
8
|
+
* - thumb drags are 1:1 with the cursor's own travel (S1-2);
|
|
9
|
+
* - attach renders the first frame immediately (S2-A);
|
|
10
|
+
* - the host promotion survives a sibling detach and restores on the
|
|
11
|
+
* last one (S2-1);
|
|
12
|
+
* - Clear semantics: setBands([]) empties the waterfall (S2-B tail).
|
|
13
|
+
*/
|
|
14
|
+
import { describe, expect, it, beforeEach, afterEach } from "vitest";
|
|
15
|
+
import { attachOverviewRuler, type OverviewRulerHandle } from "./useOverviewRuler";
|
|
16
|
+
|
|
17
|
+
interface Rect {
|
|
18
|
+
left: number;
|
|
19
|
+
top: number;
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function stubRect(el: HTMLElement, rect: Rect): void {
|
|
25
|
+
Object.defineProperty(el, "getBoundingClientRect", {
|
|
26
|
+
configurable: true,
|
|
27
|
+
value: () => ({
|
|
28
|
+
...rect,
|
|
29
|
+
right: rect.left + rect.width,
|
|
30
|
+
bottom: rect.top + rect.height,
|
|
31
|
+
x: rect.left,
|
|
32
|
+
y: rect.top,
|
|
33
|
+
toJSON() {
|
|
34
|
+
return {};
|
|
35
|
+
},
|
|
36
|
+
}),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function seedScroll(
|
|
41
|
+
sc: HTMLElement,
|
|
42
|
+
dims: { extent: number; visible: number },
|
|
43
|
+
): void {
|
|
44
|
+
Object.defineProperty(sc, "scrollHeight", {
|
|
45
|
+
configurable: true,
|
|
46
|
+
value: dims.extent,
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(sc, "clientHeight", {
|
|
49
|
+
configurable: true,
|
|
50
|
+
value: dims.visible,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function makeScroller(children: number): { host: HTMLElement; sc: HTMLElement } {
|
|
55
|
+
const host = document.createElement("div");
|
|
56
|
+
const sc = document.createElement("div");
|
|
57
|
+
for (let i = 1; i <= children; i += 1) {
|
|
58
|
+
const d = document.createElement("div");
|
|
59
|
+
d.textContent = `row ${i}`;
|
|
60
|
+
sc.appendChild(d);
|
|
61
|
+
}
|
|
62
|
+
host.appendChild(sc);
|
|
63
|
+
document.body.appendChild(host);
|
|
64
|
+
return { host, sc };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Strip geometry used across these tests: 48px wide, 400px tall, docked
|
|
68
|
+
* at the host origin (the scroller rect is stubbed to the same box). */
|
|
69
|
+
const STRIP: Rect = { left: 0, top: 0, width: 48, height: 400 };
|
|
70
|
+
|
|
71
|
+
describe("attachOverviewRuler (behaviour)", () => {
|
|
72
|
+
let cleanup: (() => void) | null = null;
|
|
73
|
+
|
|
74
|
+
beforeEach(() => {
|
|
75
|
+
cleanup = null;
|
|
76
|
+
});
|
|
77
|
+
afterEach(() => {
|
|
78
|
+
cleanup?.();
|
|
79
|
+
cleanup = null;
|
|
80
|
+
document.body.textContent = "";
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("renders the first frame on attach (cursor styled immediately)", () => {
|
|
84
|
+
const { host, sc } = makeScroller(30);
|
|
85
|
+
stubRect(sc, { left: 0, top: 0, width: 300, height: 300 });
|
|
86
|
+
seedScroll(sc, { extent: 3000, visible: 300 });
|
|
87
|
+
const handle = attachOverviewRuler(sc, { host });
|
|
88
|
+
cleanup = () => handle.detach();
|
|
89
|
+
const cursor = host.querySelector<HTMLElement>(".hk-overview-ruler-cursor")!;
|
|
90
|
+
// length = 300/3000 = 10%; progress = 0 → top = 0%
|
|
91
|
+
expect(cursor.style.top).toBe("0%");
|
|
92
|
+
expect(cursor.style.height).toBe("10%");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("jump: a strip click at fraction f scrolls to f × maxScroll", () => {
|
|
96
|
+
const { host, sc } = makeScroller(30);
|
|
97
|
+
stubRect(sc, { left: 0, top: 0, width: 300, height: 300 });
|
|
98
|
+
seedScroll(sc, { extent: 3000, visible: 300 }); // maxScroll 2700
|
|
99
|
+
const handle = attachOverviewRuler(sc, { host });
|
|
100
|
+
cleanup = () => handle.detach();
|
|
101
|
+
const strip = host.querySelector<HTMLElement>(".hk-overview-ruler")!;
|
|
102
|
+
stubRect(strip, STRIP);
|
|
103
|
+
// click at fraction 0.5 down the strip
|
|
104
|
+
const y = STRIP.top + STRIP.height * 0.5;
|
|
105
|
+
strip.dispatchEvent(
|
|
106
|
+
new PointerEvent("pointerdown", {
|
|
107
|
+
bubbles: true,
|
|
108
|
+
cancelable: true,
|
|
109
|
+
clientX: STRIP.left + STRIP.width / 2,
|
|
110
|
+
clientY: y,
|
|
111
|
+
pointerId: 1,
|
|
112
|
+
pointerType: "mouse",
|
|
113
|
+
button: 0,
|
|
114
|
+
isPrimary: true,
|
|
115
|
+
}),
|
|
116
|
+
);
|
|
117
|
+
strip.dispatchEvent(
|
|
118
|
+
new PointerEvent("pointerup", {
|
|
119
|
+
bubbles: true,
|
|
120
|
+
cancelable: true,
|
|
121
|
+
clientX: STRIP.left + STRIP.width / 2,
|
|
122
|
+
clientY: y,
|
|
123
|
+
pointerId: 1,
|
|
124
|
+
pointerType: "mouse",
|
|
125
|
+
}),
|
|
126
|
+
);
|
|
127
|
+
expect(sc.scrollTop).toBe(1350); // 0.5 × 2700
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("cursor lands exactly at the strip end when fully scrolled (S1-1)", () => {
|
|
131
|
+
const { host, sc } = makeScroller(30);
|
|
132
|
+
stubRect(sc, { left: 0, top: 0, width: 300, height: 300 });
|
|
133
|
+
seedScroll(sc, { extent: 3000, visible: 300 });
|
|
134
|
+
const handle = attachOverviewRuler(sc, { host });
|
|
135
|
+
cleanup = () => handle.detach();
|
|
136
|
+
sc.scrollTop = 2700; // maxScroll
|
|
137
|
+
sc.dispatchEvent(new Event("scroll"));
|
|
138
|
+
const cursor = host.querySelector<HTMLElement>(".hk-overview-ruler-cursor")!;
|
|
139
|
+
// progress = 1 → top = (1 − 0.1) = 90%; the bottom edge lands at 100%
|
|
140
|
+
expect(cursor.style.top).toBe("90%");
|
|
141
|
+
expect(cursor.style.height).toBe("10%");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("thumb drag is 1:1 with the cursor's own travel (S1-2)", () => {
|
|
145
|
+
const { host, sc } = makeScroller(30);
|
|
146
|
+
stubRect(sc, { left: 0, top: 0, width: 300, height: 300 });
|
|
147
|
+
seedScroll(sc, { extent: 3000, visible: 300 }); // length 0.1, maxScroll 2700
|
|
148
|
+
const handle = attachOverviewRuler(sc, { host, cursorDrag: "thumb" });
|
|
149
|
+
cleanup = () => handle.detach();
|
|
150
|
+
const strip = host.querySelector<HTMLElement>(".hk-overview-ruler")!;
|
|
151
|
+
stubRect(strip, STRIP);
|
|
152
|
+
const cursor = host.querySelector<HTMLElement>(".hk-overview-ruler-cursor")!;
|
|
153
|
+
stubRect(cursor, { left: 0, top: 0, width: 48, height: 40 }); // 10% of 400
|
|
154
|
+
// cursor travel = 400 − 40 = 360px ↔ maxScroll 2700: a 180px drag is
|
|
155
|
+
// HALF the document (1350) — exactly 1:1 with the cursor box.
|
|
156
|
+
// Pointerdown MUST hit the cursor: thumb mode ignores strip presses
|
|
157
|
+
// (dispatching on the strip would take the inert early return and the
|
|
158
|
+
// assertion would pass vacuously — the R4 finding).
|
|
159
|
+
const startY = STRIP.top + 20; // inside the 40px cursor at top 0%
|
|
160
|
+
cursor.dispatchEvent(
|
|
161
|
+
new PointerEvent("pointerdown", {
|
|
162
|
+
bubbles: true,
|
|
163
|
+
cancelable: true,
|
|
164
|
+
clientX: 24,
|
|
165
|
+
clientY: startY,
|
|
166
|
+
pointerId: 1,
|
|
167
|
+
pointerType: "mouse",
|
|
168
|
+
button: 0,
|
|
169
|
+
isPrimary: true,
|
|
170
|
+
}),
|
|
171
|
+
);
|
|
172
|
+
window.dispatchEvent(
|
|
173
|
+
new PointerEvent("pointermove", {
|
|
174
|
+
bubbles: true,
|
|
175
|
+
cancelable: true,
|
|
176
|
+
clientX: 24,
|
|
177
|
+
clientY: startY + 180,
|
|
178
|
+
pointerId: 1,
|
|
179
|
+
pointerType: "mouse",
|
|
180
|
+
}),
|
|
181
|
+
);
|
|
182
|
+
window.dispatchEvent(
|
|
183
|
+
new PointerEvent("pointerup", {
|
|
184
|
+
bubbles: true,
|
|
185
|
+
cancelable: true,
|
|
186
|
+
clientX: 24,
|
|
187
|
+
clientY: startY + 180,
|
|
188
|
+
pointerId: 1,
|
|
189
|
+
pointerType: "mouse",
|
|
190
|
+
}),
|
|
191
|
+
);
|
|
192
|
+
expect(sc.scrollTop).toBe(1350);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("thumb-mode strip clicks are inert (thumb restricts dragging)", () => {
|
|
196
|
+
const { host, sc } = makeScroller(30);
|
|
197
|
+
stubRect(sc, { left: 0, top: 0, width: 300, height: 300 });
|
|
198
|
+
seedScroll(sc, { extent: 3000, visible: 300 });
|
|
199
|
+
const handle = attachOverviewRuler(sc, { host, cursorDrag: "thumb" });
|
|
200
|
+
cleanup = () => handle.detach();
|
|
201
|
+
const strip = host.querySelector<HTMLElement>(".hk-overview-ruler")!;
|
|
202
|
+
stubRect(strip, STRIP);
|
|
203
|
+
// click OUTSIDE the cursor (fraction 0.5 down the strip)
|
|
204
|
+
strip.dispatchEvent(
|
|
205
|
+
new PointerEvent("pointerdown", {
|
|
206
|
+
bubbles: true,
|
|
207
|
+
cancelable: true,
|
|
208
|
+
clientX: 24,
|
|
209
|
+
clientY: STRIP.top + STRIP.height * 0.5,
|
|
210
|
+
pointerId: 1,
|
|
211
|
+
pointerType: "mouse",
|
|
212
|
+
button: 0,
|
|
213
|
+
isPrimary: true,
|
|
214
|
+
}),
|
|
215
|
+
);
|
|
216
|
+
expect(sc.scrollTop).toBe(0);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("setBands replaces the waterfall; empty clears it (S2-B tail)", () => {
|
|
220
|
+
const { host, sc } = makeScroller(10);
|
|
221
|
+
stubRect(sc, { left: 0, top: 0, width: 300, height: 300 });
|
|
222
|
+
seedScroll(sc, { extent: 500, visible: 300 });
|
|
223
|
+
const handle = attachOverviewRuler(sc, { host });
|
|
224
|
+
cleanup = () => handle.detach();
|
|
225
|
+
handle.setBands([
|
|
226
|
+
{ start: 0.1, length: 0.2, modifier: "hk-overview-band-error" },
|
|
227
|
+
{ start: 0.5, length: 0.1, modifier: "hk-overview-band-warn" },
|
|
228
|
+
]);
|
|
229
|
+
const bands = host.querySelectorAll<HTMLElement>(".hk-overview-ruler-band");
|
|
230
|
+
expect(bands.length).toBe(2);
|
|
231
|
+
expect(bands[0].classList.contains("hk-overview-band-error")).toBe(true);
|
|
232
|
+
expect(bands[1].classList.contains("hk-overview-band-warn")).toBe(true);
|
|
233
|
+
expect(bands[0].style.top).toBe("10%");
|
|
234
|
+
handle.setBands([]);
|
|
235
|
+
expect(host.querySelectorAll(".hk-overview-ruler-band").length).toBe(0);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("horizontal RTL: the cursor reads |scrollLeft| and jumps write positive (S2-1 guard)", () => {
|
|
239
|
+
const { host, sc } = makeScroller(30);
|
|
240
|
+
stubRect(sc, { left: 0, top: 0, width: 300, height: 300 });
|
|
241
|
+
// HORIZONTAL axis: the extent reads scrollWidth/clientWidth, not the
|
|
242
|
+
// vertical pair the vertical tests seed.
|
|
243
|
+
seedScroll(sc, { extent: 3000, visible: 300 });
|
|
244
|
+
Object.defineProperty(sc, "scrollWidth", { configurable: true, value: 3000 });
|
|
245
|
+
Object.defineProperty(sc, "clientWidth", { configurable: true, value: 300 });
|
|
246
|
+
// Chromium RTL: direction rtl, scrollLeft positive (0 → +maxScroll).
|
|
247
|
+
const orig = window.getComputedStyle;
|
|
248
|
+
// @ts-expect-error test stub — a minimal plain object: the composable
|
|
249
|
+
// only reads `.direction` on this path.
|
|
250
|
+
window.getComputedStyle = (el: Element) =>
|
|
251
|
+
el === sc ? { direction: "rtl" } : orig(el);
|
|
252
|
+
const handle = attachOverviewRuler(sc, { host, axis: "horizontal" });
|
|
253
|
+
cleanup = () => {
|
|
254
|
+
handle.detach();
|
|
255
|
+
window.getComputedStyle = orig;
|
|
256
|
+
};
|
|
257
|
+
// half-scroll under the LEGACY-negative convention (Firefox RTL:
|
|
258
|
+
// scrollLeft −1350) — the Math.abs read makes the cursor
|
|
259
|
+
// convention-proof: 45% under both the legacy and Chromium forms.
|
|
260
|
+
Object.defineProperty(sc, "scrollLeft", {
|
|
261
|
+
configurable: true,
|
|
262
|
+
value: -1350,
|
|
263
|
+
writable: true,
|
|
264
|
+
});
|
|
265
|
+
sc.dispatchEvent(new Event("scroll"));
|
|
266
|
+
const cursor = host.querySelector<HTMLElement>(".hk-overview-ruler-cursor")!;
|
|
267
|
+
expect(cursor.style.left).toBe("45%");
|
|
268
|
+
expect(cursor.style.width).toBe("10%");
|
|
269
|
+
// jump at fraction 0.5 writes the POSITIVE value (Chromium semantics)
|
|
270
|
+
const strip = host.querySelector<HTMLElement>(".hk-overview-ruler")!;
|
|
271
|
+
stubRect(strip, { left: 0, top: 0, width: 400, height: 48 });
|
|
272
|
+
const y = 24;
|
|
273
|
+
strip.dispatchEvent(
|
|
274
|
+
new PointerEvent("pointerdown", {
|
|
275
|
+
bubbles: true,
|
|
276
|
+
cancelable: true,
|
|
277
|
+
clientX: 200,
|
|
278
|
+
clientY: y,
|
|
279
|
+
pointerId: 1,
|
|
280
|
+
pointerType: "mouse",
|
|
281
|
+
button: 0,
|
|
282
|
+
isPrimary: true,
|
|
283
|
+
}),
|
|
284
|
+
);
|
|
285
|
+
expect(sc.scrollLeft).toBe(1350);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("host promotion is reference-counted and restores on the last detach (S2-1)", () => {
|
|
289
|
+
const { host, sc } = makeScroller(5);
|
|
290
|
+
stubRect(sc, { left: 0, top: 0, width: 300, height: 300 });
|
|
291
|
+
seedScroll(sc, { extent: 250, visible: 300 });
|
|
292
|
+
const a = attachOverviewRuler(sc, { host });
|
|
293
|
+
const b = attachOverviewRuler(sc, { host });
|
|
294
|
+
cleanup = () => {
|
|
295
|
+
a.detach();
|
|
296
|
+
b.detach();
|
|
297
|
+
};
|
|
298
|
+
// happy-dom computed position is static → the promotion applied
|
|
299
|
+
expect(host.style.position).toBe("relative");
|
|
300
|
+
a.detach();
|
|
301
|
+
// ruler B still mounted — the promotion must survive A's detach
|
|
302
|
+
expect(host.style.position).toBe("relative");
|
|
303
|
+
expect(host.querySelector(".hk-overview-ruler")).toBeTruthy();
|
|
304
|
+
b.detach();
|
|
305
|
+
// the LAST detach restores the saved inline position
|
|
306
|
+
expect(host.style.position).toBe("");
|
|
307
|
+
expect(host.querySelector(".hk-overview-ruler")).toBeNull();
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("detach is idempotent and a no-host viewport gets a no-op handle", () => {
|
|
311
|
+
const { sc } = makeScroller(3);
|
|
312
|
+
// Detach sc from any parent: no host resolvable
|
|
313
|
+
const orphan = document.createElement("div");
|
|
314
|
+
orphan.appendChild(sc);
|
|
315
|
+
const handle = attachOverviewRuler(sc);
|
|
316
|
+
expect(() => {
|
|
317
|
+
handle.update();
|
|
318
|
+
handle.setBands([{ start: 0, length: 1 }]);
|
|
319
|
+
handle.detach();
|
|
320
|
+
handle.detach();
|
|
321
|
+
}).not.toThrow();
|
|
322
|
+
void orphan;
|
|
323
|
+
});
|
|
324
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Source contract for the standalone overview ruler ("竖向瀑布流" minimap,
|
|
3
|
+
* 2026-11-21 user direction): a STANDALONE attachment any scrollable
|
|
4
|
+
* element can host — not a mode of the scrollbar — with customizable
|
|
5
|
+
* direction and cursor behaviour.
|
|
6
|
+
*
|
|
7
|
+
* Pinned here (structure):
|
|
8
|
+
* - exported from the barrel (usable by npm consumers, same as
|
|
9
|
+
* attachOverlayScrollbars);
|
|
10
|
+
* - the two cursor modes exist and the default is "jump" (strip click
|
|
11
|
+
* jumps then drags — 双向可拖动);
|
|
12
|
+
* - direction is a first-class option ("vertical" default, "horizontal"
|
|
13
|
+
* supported) with per-side placement;
|
|
14
|
+
* - the no-host viewport renders a no-op handle (same contract as
|
|
15
|
+
* attachOverlayScrollbars);
|
|
16
|
+
* - content resampling is RECURRING (scheduleEvery — scheduleCronAfter
|
|
17
|
+
* is a one-shot and would kill the waterfall after one fire);
|
|
18
|
+
* - a ResizeObserver keeps the cursor honest across viewport resizes.
|
|
19
|
+
*
|
|
20
|
+
* BEHAVIOUR lives in useOverviewRuler.behavior.test.ts (real mounts:
|
|
21
|
+
* strip/bands/cursor presence, jump, thumb 1:1 drag, clear, detach).
|
|
22
|
+
*/
|
|
23
|
+
import { describe, expect, it } from "vitest";
|
|
24
|
+
import { readFileSync } from "node:fs";
|
|
25
|
+
import { dirname, join } from "node:path";
|
|
26
|
+
import { fileURLToPath } from "node:url";
|
|
27
|
+
|
|
28
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
29
|
+
const composable = readFileSync(join(here, "useOverviewRuler.ts"), "utf-8");
|
|
30
|
+
const barrel = readFileSync(join(here, "../index.ts"), "utf-8");
|
|
31
|
+
const scss = readFileSync(join(here, "../styles/overview-ruler.scss"), "utf-8");
|
|
32
|
+
|
|
33
|
+
describe("useOverviewRuler contract", () => {
|
|
34
|
+
it("is exported from the barrel for npm consumers", () => {
|
|
35
|
+
expect(barrel).toMatch(/attachOverviewRuler/);
|
|
36
|
+
expect(barrel).toMatch(/OverviewRulerOptions|OverviewRulerHandle/);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("cursor modes: jump is the default; thumb restricts dragging to the cursor", () => {
|
|
40
|
+
expect(composable).toMatch(/cursorDrag \?\? "jump"/);
|
|
41
|
+
expect(composable).toMatch(/OverviewRulerCursorMode = "jump" \| "thumb"/);
|
|
42
|
+
// Target identity, not composedPath — happy-dom's composedPath is empty
|
|
43
|
+
// for manually dispatched events, and the cursor box has no children.
|
|
44
|
+
expect(composable).toContain("if (event.target !== cursor) return;");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("direction is a first-class option with per-side placement", () => {
|
|
48
|
+
expect(composable).toMatch(/axis \?\? "vertical"/);
|
|
49
|
+
expect(scss.match(/\[data-axis="vertical"\]/g)?.length).toBeGreaterThanOrEqual(1);
|
|
50
|
+
expect(scss.match(/\[data-axis="horizontal"\]/g)?.length).toBeGreaterThanOrEqual(1);
|
|
51
|
+
expect(scss).toContain('data-side="end"');
|
|
52
|
+
expect(scss).toContain('data-side="start"');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("no-host viewport renders a no-op handle", () => {
|
|
56
|
+
expect(composable).toContain(
|
|
57
|
+
"return { update() {}, setBands() {}, detach() {} };",
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("resampling is recurring and a RO keeps the cursor live", () => {
|
|
62
|
+
expect(composable).toContain("scheduleEvery(");
|
|
63
|
+
expect(composable).not.toContain("scheduleCronAfter(");
|
|
64
|
+
expect(composable).toContain("new ResizeObserver(");
|
|
65
|
+
expect(composable).toContain("observer.observe(viewport)");
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("strip/cursor/bands chrome exists with the placement contract", () => {
|
|
69
|
+
expect(scss).toContain(".hk-overview-ruler {");
|
|
70
|
+
expect(scss).toContain(".hk-overview-ruler-cursor {");
|
|
71
|
+
expect(scss).toContain(".hk-overview-ruler-band {");
|
|
72
|
+
expect(scss).toContain("touch-action: none;");
|
|
73
|
+
expect(scss).toContain(".hk-overview-ruler[data-axis=\"vertical\"] .hk-overview-ruler-cursor");
|
|
74
|
+
expect(scss).toContain(".hk-overview-ruler[data-axis=\"horizontal\"] .hk-overview-ruler-cursor");
|
|
75
|
+
});
|
|
76
|
+
});
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import { laidOffsetWithin } from "./layoutGeometry";
|
|
2
|
+
import { promoteHost } from "./useHostPromotion";
|
|
3
|
+
import { scheduleEvery } from "../runtime/animationBus";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* useOverviewRuler — a standalone minimap strip ("竖向瀑布流") that any
|
|
7
|
+
* scrollable element can attach, the same way attachOverlayScrollbars
|
|
8
|
+
* attaches the scrollbar. It renders a proportional density map of the
|
|
9
|
+
* viewport's content (one band per top-level block, sized by its share of
|
|
10
|
+
* the content extent) plus a cursor marking the visible window; dragging
|
|
11
|
+
* scrolls the viewport, clicking jumps.
|
|
12
|
+
*
|
|
13
|
+
* Design direction (2026-11-21 user ruling): the minimap is a STANDALONE
|
|
14
|
+
* attachment — not a mode of the scrollbar — and it is the same primitive
|
|
15
|
+
* a waterfall layout needs: a vertical (or horizontal) strip of
|
|
16
|
+
* proportional bands with a draggable cursor. Direction and cursor
|
|
17
|
+
* behaviour are options, not separate components.
|
|
18
|
+
*
|
|
19
|
+
* Two cursor modes:
|
|
20
|
+
* - "jump" (default): pointer down anywhere on the strip scrolls so the
|
|
21
|
+
* content window under the pointer becomes the visible window, then
|
|
22
|
+
* keeps dragging (双向可拖动 — both the cursor and a strip click steer
|
|
23
|
+
* the scroll);
|
|
24
|
+
* - "thumb": only the cursor box is draggable; strip clicks are inert.
|
|
25
|
+
*
|
|
26
|
+
* Sampling: bands are harvested from `bandSelector` matches inside the
|
|
27
|
+
* viewport (default: the viewport's DIRECT element children). Nested
|
|
28
|
+
* matches are dropped when a match contains another (a parent band's
|
|
29
|
+
* extent already contains its children's — counting both would paint the
|
|
30
|
+
* same content twice). Caller-driven content (virtualized rows, a canvas
|
|
31
|
+
* terminal) should pass a `bandSelector` for whatever stands in for lines,
|
|
32
|
+
* or drive `setBands` directly with fractions.
|
|
33
|
+
*
|
|
34
|
+
* The strip sits at z-index 9 — UNDER the overlay scrollbar's interactive
|
|
35
|
+
* track (z-index 10, see theme/scrollbar.scss) so a host that attaches
|
|
36
|
+
* both keeps fine scrolling on the track while the ruler carries the map.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
export type OverviewRulerAxis = "vertical" | "horizontal";
|
|
40
|
+
export type OverviewRulerCursorMode = "jump" | "thumb";
|
|
41
|
+
|
|
42
|
+
export interface OverviewRulerBand {
|
|
43
|
+
/** Band start as a fraction of the content extent, 0..1. */
|
|
44
|
+
start: number;
|
|
45
|
+
/** Band length as a fraction of the content extent, 0..1. */
|
|
46
|
+
length: number;
|
|
47
|
+
/** Extra class for the band (e.g. severity coloring for log lines). */
|
|
48
|
+
modifier?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface OverviewRulerOptions {
|
|
52
|
+
/** Strip direction. Default "vertical" (the waterfall). */
|
|
53
|
+
axis?: OverviewRulerAxis;
|
|
54
|
+
/** Strip thickness in CSS px. Default 48 (vertical) / 36 (horizontal). */
|
|
55
|
+
thickness?: number;
|
|
56
|
+
/** Which edge of the host the strip docks to. Default "end"
|
|
57
|
+
* (right for vertical, bottom for horizontal). */
|
|
58
|
+
side?: "start" | "end";
|
|
59
|
+
/** Selector harvested INSIDE the viewport for band sources. Matches
|
|
60
|
+
* contained within another match are dropped (the parent band already
|
|
61
|
+
* covers them). Default: the viewport's DIRECT element children. */
|
|
62
|
+
bandSelector?: string;
|
|
63
|
+
/** Cursor behaviour: "jump" (default) = strip pointer down jumps then
|
|
64
|
+
* drags; "thumb" = only the cursor box is draggable. */
|
|
65
|
+
cursorDrag?: OverviewRulerCursorMode;
|
|
66
|
+
/** Element the strip is appended to — its positioning context. Same
|
|
67
|
+
* contract as OverlayScrollbarOptions.host: a box wrapping exactly the
|
|
68
|
+
* viewport. Defaults to the viewport's DOM parent. */
|
|
69
|
+
host?: HTMLElement;
|
|
70
|
+
/** Content-resample cadence in ms (bands, not the cursor — the cursor
|
|
71
|
+
* tracks scroll and resize live). Default 250. */
|
|
72
|
+
resampleMs?: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface OverviewRulerHandle {
|
|
76
|
+
/** Resample the bands and reposition the cursor. */
|
|
77
|
+
update(): void;
|
|
78
|
+
/** Replace the sampled bands with caller fractions (virtualized or
|
|
79
|
+
* canvas content). An empty list clears the waterfall. */
|
|
80
|
+
setBands(bands: OverviewRulerBand[]): void;
|
|
81
|
+
/** Remove the strip and every listener/observer; restore any inline
|
|
82
|
+
* positioning the promotion applied. Safe to call twice. */
|
|
83
|
+
detach(): void;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Clamp to [0, 1]; a band outside the content extent is caller noise and
|
|
87
|
+
* would render outside the strip. */
|
|
88
|
+
function clamp01(n: number): number {
|
|
89
|
+
if (!Number.isFinite(n)) return 0;
|
|
90
|
+
return n < 0 ? 0 : n > 1 ? 1 : n;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function attachOverviewRuler(
|
|
94
|
+
viewport: HTMLElement,
|
|
95
|
+
opts: OverviewRulerOptions = {},
|
|
96
|
+
): OverviewRulerHandle {
|
|
97
|
+
const axis = opts.axis ?? "vertical";
|
|
98
|
+
const horizontal = axis === "horizontal";
|
|
99
|
+
const thickness = Math.max(
|
|
100
|
+
16,
|
|
101
|
+
Math.round(opts.thickness ?? (horizontal ? 36 : 48)),
|
|
102
|
+
);
|
|
103
|
+
const cursorDrag = opts.cursorDrag ?? "jump";
|
|
104
|
+
const resampleMs = Math.max(50, opts.resampleMs ?? 250);
|
|
105
|
+
const parent = opts.host ?? viewport.parentElement;
|
|
106
|
+
if (!parent) {
|
|
107
|
+
return { update() {}, setBands() {}, detach() {} };
|
|
108
|
+
}
|
|
109
|
+
const host: HTMLElement = parent;
|
|
110
|
+
|
|
111
|
+
// ── Promotion (shared, reference-counted — see useHostPromotion) ──────
|
|
112
|
+
const promotion = promoteHost(host);
|
|
113
|
+
|
|
114
|
+
// ── Chrome ─────────────────────────────────────────────────────────────
|
|
115
|
+
const strip = document.createElement("div");
|
|
116
|
+
strip.className = "hk-overview-ruler";
|
|
117
|
+
strip.dataset.axis = axis;
|
|
118
|
+
strip.dataset.side = opts.side ?? "end";
|
|
119
|
+
strip.style.setProperty("--hk-overview-ruler-thickness", `${thickness}px`);
|
|
120
|
+
strip.setAttribute("aria-hidden", "true");
|
|
121
|
+
|
|
122
|
+
const cursor = document.createElement("div");
|
|
123
|
+
cursor.className = "hk-overview-ruler-cursor";
|
|
124
|
+
strip.appendChild(cursor);
|
|
125
|
+
|
|
126
|
+
const bandsHost = document.createElement("div");
|
|
127
|
+
bandsHost.className = "hk-overview-ruler-bands";
|
|
128
|
+
strip.appendChild(bandsHost);
|
|
129
|
+
|
|
130
|
+
host.appendChild(strip);
|
|
131
|
+
|
|
132
|
+
// ── Geometry ───────────────────────────────────────────────────────────
|
|
133
|
+
const scrollCoord = (): number => (horizontal ? viewport.scrollLeft : viewport.scrollTop);
|
|
134
|
+
const setScrollCoord = (v: number) => {
|
|
135
|
+
if (horizontal) viewport.scrollLeft = v;
|
|
136
|
+
else viewport.scrollTop = v;
|
|
137
|
+
};
|
|
138
|
+
/** Horizontal RTL: Chromium/WebKitGTK report scrollLeft rising 0 →
|
|
139
|
+
* +maxScroll with 0 at the visual END; legacy Firefox descends 0 →
|
|
140
|
+
* −maxScroll. |scrollLeft| is the position under BOTH conventions, so
|
|
141
|
+
* the cursor reads correctly everywhere; the positive write targets
|
|
142
|
+
* Chromium semantics (the dominant engine — the overlay scrollbar's
|
|
143
|
+
* thumb uses |progress| for the same reason). Legacy-Firefox
|
|
144
|
+
* horizontal-RTL WRITES degrade to a no-op: documented limitation. */
|
|
145
|
+
const rtlHorizontal = horizontal
|
|
146
|
+
&& typeof getComputedStyle === "function"
|
|
147
|
+
&& getComputedStyle(viewport).direction === "rtl";
|
|
148
|
+
/** The scroll position in the ruler's positive-fraction domain. */
|
|
149
|
+
const readScroll = (): number => (rtlHorizontal ? Math.abs(scrollCoord()) : scrollCoord());
|
|
150
|
+
const writeScroll = (v: number): void => setScrollCoord(rtlHorizontal ? Math.abs(v) : v);
|
|
151
|
+
const contentExtent = (): number =>
|
|
152
|
+
horizontal
|
|
153
|
+
? Math.max(viewport.scrollWidth, viewport.clientWidth)
|
|
154
|
+
: Math.max(viewport.scrollHeight, viewport.clientHeight);
|
|
155
|
+
const visibleExtent = (): number =>
|
|
156
|
+
horizontal ? viewport.clientWidth : viewport.clientHeight;
|
|
157
|
+
|
|
158
|
+
/** The visible window mapped onto the strip: the cursor spans
|
|
159
|
+
* visible/extent of the strip, and its EDGE travels progress times the
|
|
160
|
+
* strip's remaining travel — the same mapping the overlay scrollbar's
|
|
161
|
+
* thumb uses (offset = progress × (track − thumb)), so the cursor
|
|
162
|
+
* always covers the visible window and lands exactly at the strip's
|
|
163
|
+
* end when fully scrolled. */
|
|
164
|
+
function windowFractions(): { start: number; length: number } {
|
|
165
|
+
const extent = contentExtent();
|
|
166
|
+
const visible = visibleExtent();
|
|
167
|
+
const maxScroll = Math.max(extent - visible, 0);
|
|
168
|
+
const length = Math.min(visible / extent, 1);
|
|
169
|
+
const progress = maxScroll > 0 ? clamp01(readScroll() / maxScroll) : 0;
|
|
170
|
+
return { start: progress * (1 - length), length };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Pointer fraction (0..1 down the strip) → the scroll position whose
|
|
174
|
+
* visible window START lands at that fraction of the travel, clamped.
|
|
175
|
+
* Jump-mode scrubbing follows the pointer proportionally: re-targeting
|
|
176
|
+
* the same fraction is idempotent (the same input yields the same
|
|
177
|
+
* scroll), so no drag state double-applies. */
|
|
178
|
+
function scrollToFraction(fraction: number) {
|
|
179
|
+
const extent = contentExtent();
|
|
180
|
+
const visible = visibleExtent();
|
|
181
|
+
const maxScroll = Math.max(extent - visible, 0);
|
|
182
|
+
writeScroll(clamp01(fraction) * maxScroll);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Strip geometry along the drag axis, for pointer→fraction math. */
|
|
186
|
+
function stripExtent(): number {
|
|
187
|
+
const rect = strip.getBoundingClientRect();
|
|
188
|
+
return horizontal ? rect.width : rect.height;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function pointerFraction(event: PointerEvent): number {
|
|
192
|
+
const rect = strip.getBoundingClientRect();
|
|
193
|
+
const coord = horizontal ? event.clientX - rect.left : event.clientY - rect.top;
|
|
194
|
+
const extent = stripExtent();
|
|
195
|
+
return extent > 0 ? Math.min(Math.max(coord / extent, 0), 1) : 0;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// ── Rendering ──────────────────────────────────────────────────────────
|
|
199
|
+
function renderCursor() {
|
|
200
|
+
const { start, length } = windowFractions();
|
|
201
|
+
const pos = `${Number((clamp01(start) * 100).toFixed(4))}%`;
|
|
202
|
+
const len = `${Number((Math.max(clamp01(length), 0.002) * 100).toFixed(4))}%`;
|
|
203
|
+
if (axis === "vertical") {
|
|
204
|
+
cursor.style.top = pos;
|
|
205
|
+
cursor.style.height = len;
|
|
206
|
+
} else {
|
|
207
|
+
cursor.style.left = pos;
|
|
208
|
+
cursor.style.width = len;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
let manualBands: OverviewRulerBand[] | null = null;
|
|
213
|
+
|
|
214
|
+
function sampleDomBands(): OverviewRulerBand[] {
|
|
215
|
+
const extent = contentExtent();
|
|
216
|
+
if (extent <= 0) return [];
|
|
217
|
+
let nodes: HTMLElement[] = opts.bandSelector
|
|
218
|
+
? (Array.from(viewport.querySelectorAll<HTMLElement>(opts.bandSelector)) as HTMLElement[])
|
|
219
|
+
: (viewport.children as HTMLCollectionOf<HTMLElement> as unknown as HTMLElement[]);
|
|
220
|
+
nodes = Array.from(nodes);
|
|
221
|
+
// Drop matches contained within another match: a parent band's extent
|
|
222
|
+
// already covers its children's, and counting both paints content twice.
|
|
223
|
+
nodes = nodes.filter(
|
|
224
|
+
(n) => !nodes.some((other) => other !== n && other.contains(n)),
|
|
225
|
+
);
|
|
226
|
+
const bands: OverviewRulerBand[] = [];
|
|
227
|
+
for (const node of nodes) {
|
|
228
|
+
const laid = laidOffsetWithin(node, viewport);
|
|
229
|
+
if (!Number.isFinite(laid.x) || !Number.isFinite(laid.y)) continue;
|
|
230
|
+
// Horizontal RTL mirrors band placement too (S2-R4-5): laid.x grows
|
|
231
|
+
// visually leftward under RTL, so the band's start anchors at the
|
|
232
|
+
// FAR edge minus its own extent.
|
|
233
|
+
const laidCoord = rtlHorizontal
|
|
234
|
+
? extent - laid.x - node.offsetWidth
|
|
235
|
+
: horizontal
|
|
236
|
+
? laid.x
|
|
237
|
+
: laid.y;
|
|
238
|
+
const start = laidCoord / extent;
|
|
239
|
+
const length = (horizontal ? node.offsetWidth : node.offsetHeight) / extent;
|
|
240
|
+
if (length <= 0) continue;
|
|
241
|
+
bands.push({ start, length });
|
|
242
|
+
}
|
|
243
|
+
return bands;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function renderBands() {
|
|
247
|
+
const bands = manualBands ?? sampleDomBands();
|
|
248
|
+
bandsHost.textContent = "";
|
|
249
|
+
for (const band of bands) {
|
|
250
|
+
const el = document.createElement("div");
|
|
251
|
+
el.className = "hk-overview-ruler-band" + (band.modifier ? ` ${band.modifier}` : "");
|
|
252
|
+
const pos = `${Number((clamp01(band.start) * 100).toFixed(4))}%`;
|
|
253
|
+
const len = `${Number((Math.max(clamp01(band.length), 0.002) * 100).toFixed(4))}%`;
|
|
254
|
+
if (axis === "vertical") {
|
|
255
|
+
el.style.top = pos;
|
|
256
|
+
el.style.height = len;
|
|
257
|
+
} else {
|
|
258
|
+
el.style.left = pos;
|
|
259
|
+
el.style.width = len;
|
|
260
|
+
}
|
|
261
|
+
bandsHost.appendChild(el);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function update() {
|
|
266
|
+
renderBands();
|
|
267
|
+
renderCursor();
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// ── Pointer steering ───────────────────────────────────────────────────
|
|
271
|
+
let dragging = false;
|
|
272
|
+
let dragMode: OverviewRulerCursorMode = cursorDrag;
|
|
273
|
+
let dragStartClient = 0;
|
|
274
|
+
let dragStartScroll = 0;
|
|
275
|
+
/** The cursor's own travel along the strip in px (strip − cursor box):
|
|
276
|
+
* the denominator that makes thumb-drag 1:1 with the pointer — moving
|
|
277
|
+
* the pointer the full cursor travel scrolls the full document. */
|
|
278
|
+
let cursorTravelPx = 1;
|
|
279
|
+
|
|
280
|
+
function beginDrag(event: PointerEvent) {
|
|
281
|
+
dragging = true;
|
|
282
|
+
dragMode = cursorDrag;
|
|
283
|
+
dragStartClient = horizontal ? event.clientX : event.clientY;
|
|
284
|
+
dragStartScroll = readScroll();
|
|
285
|
+
const extent = contentExtent();
|
|
286
|
+
const visible = visibleExtent();
|
|
287
|
+
const length = Math.min(visible / extent, 1);
|
|
288
|
+
cursorTravelPx = Math.max((1 - length) * stripExtent(), 1);
|
|
289
|
+
strip.setPointerCapture(event.pointerId);
|
|
290
|
+
event.preventDefault();
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function onPointerDown(event: PointerEvent) {
|
|
294
|
+
if (event.button !== 0 || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey)
|
|
295
|
+
return;
|
|
296
|
+
const fraction = pointerFraction(event);
|
|
297
|
+
if (cursorDrag === "thumb") {
|
|
298
|
+
if (event.target !== cursor) return;
|
|
299
|
+
} else {
|
|
300
|
+
scrollToFraction(fraction);
|
|
301
|
+
}
|
|
302
|
+
beginDrag(event);
|
|
303
|
+
// Move/up ride the WINDOW (not the strip): setPointerCapture keeps the
|
|
304
|
+
// events flowing through the strip when it works, and the window
|
|
305
|
+
// bindings are the fallback everywhere capture is unavailable.
|
|
306
|
+
window.addEventListener("pointermove", onPointerMove);
|
|
307
|
+
window.addEventListener("pointerup", onPointerUp);
|
|
308
|
+
window.addEventListener("pointercancel", onPointerUp);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function onPointerMove(event: PointerEvent) {
|
|
312
|
+
if (!dragging) return;
|
|
313
|
+
if (dragMode === "thumb") {
|
|
314
|
+
const deltaPx = (horizontal ? event.clientX : event.clientY) - dragStartClient;
|
|
315
|
+
const maxScroll = Math.max(contentExtent() - visibleExtent(), 0);
|
|
316
|
+
writeScroll(dragStartScroll + (deltaPx / cursorTravelPx) * maxScroll);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
scrollToFraction(pointerFraction(event));
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function onPointerUp(event: PointerEvent) {
|
|
323
|
+
if (!dragging) return;
|
|
324
|
+
dragging = false;
|
|
325
|
+
try {
|
|
326
|
+
strip.releasePointerCapture(event.pointerId);
|
|
327
|
+
} catch {
|
|
328
|
+
// already released by the browser on pointer cancel
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
strip.addEventListener("pointerdown", onPointerDown);
|
|
333
|
+
|
|
334
|
+
// ── Live cursor: scroll + viewport resize ─────────────────────────────
|
|
335
|
+
const onViewportScroll = () => renderCursor();
|
|
336
|
+
viewport.addEventListener("scroll", onViewportScroll, { passive: true });
|
|
337
|
+
|
|
338
|
+
let observer: ResizeObserver | null = null;
|
|
339
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
340
|
+
observer = new ResizeObserver(() => {
|
|
341
|
+
renderCursor();
|
|
342
|
+
renderBands();
|
|
343
|
+
});
|
|
344
|
+
observer.observe(viewport);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// ── Content resample (bands only; the cursor tracks live) ─────────────
|
|
348
|
+
const sampler = scheduleEvery(
|
|
349
|
+
() => {
|
|
350
|
+
if (!manualBands) renderBands();
|
|
351
|
+
},
|
|
352
|
+
resampleMs,
|
|
353
|
+
);
|
|
354
|
+
|
|
355
|
+
// First frame now — the sampler's first tick is resampleMs away, and a
|
|
356
|
+
// bare background strip for a quarter second reads as a flash.
|
|
357
|
+
renderBands();
|
|
358
|
+
renderCursor();
|
|
359
|
+
|
|
360
|
+
function endDrag(event: PointerEvent) {
|
|
361
|
+
if (!dragging) return;
|
|
362
|
+
dragging = false;
|
|
363
|
+
window.removeEventListener("pointermove", onPointerMove);
|
|
364
|
+
window.removeEventListener("pointerup", onPointerUp);
|
|
365
|
+
window.removeEventListener("pointercancel", onPointerUp);
|
|
366
|
+
try {
|
|
367
|
+
strip.releasePointerCapture(event.pointerId);
|
|
368
|
+
} catch {
|
|
369
|
+
// already released by the browser on pointer cancel
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function detach() {
|
|
374
|
+
viewport.removeEventListener("scroll", onViewportScroll);
|
|
375
|
+
window.removeEventListener("pointermove", onPointerMove);
|
|
376
|
+
window.removeEventListener("pointerup", onPointerUp);
|
|
377
|
+
window.removeEventListener("pointercancel", onPointerUp);
|
|
378
|
+
strip.removeEventListener("pointerdown", onPointerDown);
|
|
379
|
+
promotion.release();
|
|
380
|
+
sampler.disconnect();
|
|
381
|
+
observer?.disconnect();
|
|
382
|
+
observer = null;
|
|
383
|
+
strip.remove();
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
return {
|
|
387
|
+
update,
|
|
388
|
+
setBands(bands: OverviewRulerBand[]) {
|
|
389
|
+
manualBands = bands;
|
|
390
|
+
renderBands();
|
|
391
|
+
renderCursor();
|
|
392
|
+
},
|
|
393
|
+
detach,
|
|
394
|
+
};
|
|
395
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1243,3 +1243,12 @@ export type {
|
|
|
1243
1243
|
export { useRemoteTouchGestures } from "./composables/useRemoteTouchGestures";
|
|
1244
1244
|
export type { TouchGestureHandlers } from "./composables/useRemoteTouchGestures";
|
|
1245
1245
|
export { useOrientationLock } from "./composables/useOrientationLock";
|
|
1246
|
+
|
|
1247
|
+
export {
|
|
1248
|
+
attachOverviewRuler,
|
|
1249
|
+
type OverviewRulerAxis,
|
|
1250
|
+
type OverviewRulerBand,
|
|
1251
|
+
type OverviewRulerCursorMode,
|
|
1252
|
+
type OverviewRulerHandle,
|
|
1253
|
+
type OverviewRulerOptions,
|
|
1254
|
+
} from "./composables/useOverviewRuler";
|
package/src/styles/index.scss
CHANGED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// The standalone minimap strip ("竖向瀑布流"): one band per content block,
|
|
2
|
+
// sized by its share of the content extent, plus the draggable cursor that
|
|
3
|
+
// marks the visible window. Attached by composables/useOverviewRuler.ts —
|
|
4
|
+
// nothing renders without it, so consumers that never attach keep exactly
|
|
5
|
+
// the chrome they had.
|
|
6
|
+
|
|
7
|
+
.hk-overview-ruler {
|
|
8
|
+
position: absolute;
|
|
9
|
+
z-index: 9;
|
|
10
|
+
background: color-mix(in srgb, var(--hi-color-surface, rgba(255, 255, 255, 0.95)) 60%, transparent);
|
|
11
|
+
border-radius: var(--radius-sm, 4px);
|
|
12
|
+
overflow: hidden;
|
|
13
|
+
touch-action: none;
|
|
14
|
+
|
|
15
|
+
&[data-axis="vertical"][data-side="end"] {
|
|
16
|
+
top: 4px;
|
|
17
|
+
bottom: 4px;
|
|
18
|
+
right: 4px;
|
|
19
|
+
width: var(--hk-overview-ruler-thickness, 48px);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
&[data-axis="vertical"][data-side="start"] {
|
|
23
|
+
top: 4px;
|
|
24
|
+
bottom: 4px;
|
|
25
|
+
left: 4px;
|
|
26
|
+
width: var(--hk-overview-ruler-thickness, 48px);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
&[data-axis="horizontal"][data-side="end"] {
|
|
30
|
+
left: 4px;
|
|
31
|
+
right: 4px;
|
|
32
|
+
bottom: 4px;
|
|
33
|
+
height: var(--hk-overview-ruler-thickness, 36px);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
&[data-axis="horizontal"][data-side="start"] {
|
|
37
|
+
top: 4px;
|
|
38
|
+
left: 4px;
|
|
39
|
+
right: 4px;
|
|
40
|
+
height: var(--hk-overview-ruler-thickness, 36px);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.hk-overview-ruler-bands {
|
|
45
|
+
position: absolute;
|
|
46
|
+
inset: 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
.hk-overview-ruler-band {
|
|
50
|
+
position: absolute;
|
|
51
|
+
background: color-mix(in srgb, var(--hi-color-primary, #7aa2f7) 28%, transparent);
|
|
52
|
+
border-radius: 1px;
|
|
53
|
+
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Severity modifiers for log-style waterfalls. The `modifier` option is a
|
|
57
|
+
// caller-owned styling hook — these defaults exist so the documented use
|
|
58
|
+
// ("severity coloring for log lines") works out of the box; unknown
|
|
59
|
+
// modifiers stay unstyled by design.
|
|
60
|
+
.hk-overview-ruler-band.hk-overview-band-error {
|
|
61
|
+
background: color-mix(in srgb, var(--hi-color-danger, #a62937) 55%, transparent);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
.hk-overview-ruler-band.hk-overview-band-warn {
|
|
65
|
+
background: color-mix(in srgb, var(--hi-color-warning, #894800) 55%, transparent);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.hk-overview-ruler-cursor {
|
|
69
|
+
position: absolute;
|
|
70
|
+
background: color-mix(in srgb, var(--hi-color-primary, #7aa2f7) 24%, transparent);
|
|
71
|
+
outline: 1px solid color-mix(in srgb, var(--hi-color-primary, #7aa2f7) 55%, transparent);
|
|
72
|
+
border-radius: 2px;
|
|
73
|
+
transition: none;
|
|
74
|
+
cursor: var(--hk-overview-ruler-cursor-cursor, grab);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// The cursor's inline styles place it on the drag axis; the cross axis
|
|
78
|
+
// stretches to the strip so the box reads as one rail-wide window.
|
|
79
|
+
.hk-overview-ruler[data-axis="vertical"] .hk-overview-ruler-cursor {
|
|
80
|
+
left: 0;
|
|
81
|
+
right: 0;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.hk-overview-ruler[data-axis="horizontal"] .hk-overview-ruler-cursor {
|
|
85
|
+
top: 0;
|
|
86
|
+
bottom: 0;
|
|
87
|
+
}
|