@invinite-org/chartlang-adapter-kit 1.4.0 → 1.5.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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @invinite-org/chartlang-adapter-kit
2
2
 
3
+ ## 1.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 24946e4: Add a library-agnostic pan/zoom interaction layer under `src/interaction/`,
8
+ exported from the root entry. `createViewController()` holds a user x-window +
9
+ `userInteracted` flag with pure `resolveXWindow` / `zoomAt` / `panBy` / `reset`
10
+ transforms (auto-follow live data until the first gesture, then hold the held
11
+ window, clamped to the data bounds — zoom-out cannot exceed all-data).
12
+ `yRangeInWindow(candidates, win)` is the shared "auto-fit the price scale to
13
+ the visible window" helper. `attachInteraction(el, handlers)` wires
14
+ wheel→zoom / drag→pan / dblclick→reset onto a DOM element (the listener
15
+ plumbing is the only DOM-bound part; the decision cores `onWheelCore` /
16
+ `onDragCore` / `onDblCore` are pure). The four example adapters (canvas2d,
17
+ konva, uplot, echarts) consume these for consistent zoom + drag + auto-fit.
18
+
3
19
  ## 1.4.0
4
20
 
5
21
  ### Minor Changes
package/dist/index.d.ts CHANGED
@@ -10,5 +10,7 @@ export type { MockCandleSourceMode, MockCandleSourceOpts } from "./mocks/index.j
10
10
  export { BufferingAdapter, PassThroughAdapter } from "./base/index.js";
11
11
  export { decomposeDrawing, priceToY, timeToX, worldPointToPixel } from "./geometry/index.js";
12
12
  export type { DrawPrimitive, FillStyle, Point2, StrokeStyle, Viewport, } from "./geometry/index.js";
13
+ export { attachInteraction, createViewController, onDblCore, onDragCore, onWheelCore, yRangeInWindow, } from "./interaction/index.js";
14
+ export type { InteractionHandlers, ViewController, ViewControllerOpts, WindowYInput, XWindow, } from "./interaction/index.js";
13
15
  export type { Adapter, AdapterSymInfo, AlertChannel, AlertConditionEmission, AlertEmission, Capabilities, CandleEvent, DiagnosticCode, DrawingCounts, DrawingEmission, DrawingKind, InputKind, LogEmission, PlotEmission, PlotKind, PlotOverride, PlotSlotDescriptor, PlotStyle, RunnerEmissions, RuntimeDiagnostic, SymInfoField, } from "./types.js";
14
16
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AACtE,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAChF,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,YAAY,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACxE,YAAY,EAAE,cAAc,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC5F,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACpD,YAAY,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACnF,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAC7F,YAAY,EACR,aAAa,EACb,SAAS,EACT,MAAM,EACN,WAAW,EACX,QAAQ,GACX,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EACR,OAAO,EACP,cAAc,EACd,YAAY,EACZ,sBAAsB,EACtB,aAAa,EACb,YAAY,EACZ,WAAW,EACX,cAAc,EACd,aAAa,EACb,eAAe,EACf,WAAW,EACX,SAAS,EACT,WAAW,EACX,YAAY,EACZ,QAAQ,EACR,Y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,sBAAsB,EACtB,aAAa,EACb,YAAY,EACZ,WAAW,EACX,cAAc,EACd,aAAa,EACb,eAAe,EACf,WAAW,EACX,SAAS,EACT,WAAW,EACX,YAAY,EACZ,QAAQ,EACR,YAAY,EACZ,kBAAkB,EAClB,SAAS,EACT,eAAe,EACf,iBAAiB,EACjB,YAAY,GACf,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -7,4 +7,5 @@ export { decodeDrawing, validateEmission } from "./validation/index.js";
7
7
  export { mockCandleSource } from "./mocks/index.js";
8
8
  export { BufferingAdapter, PassThroughAdapter } from "./base/index.js";
9
9
  export { decomposeDrawing, priceToY, timeToX, worldPointToPixel } from "./geometry/index.js";
10
+ export { attachInteraction, createViewController, onDblCore, onDragCore, onWheelCore, yRangeInWindow, } from "./interaction/index.js";
10
11
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,+DAA+D;AAE/D,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAEtE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAExE,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpD,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC","sourcesContent":["// Copyright (c) 2026 Invinite. Licensed under the MIT License.\n// See the LICENSE file in the repo root for full license text.\n\nexport { KIND_BUCKET, bucketFor } from \"@invinite-org/chartlang-core\";\nexport type { DrawingBucket, DrawingState } from \"@invinite-org/chartlang-core\";\nexport { defineAdapter } from \"./defineAdapter.js\";\nexport type { DefineAdapterOpts } from \"./defineAdapter.js\";\nexport { PHASE_5_PLOT_KINDS, capabilities } from \"./capabilities/index.js\";\nexport { decodeDrawing, validateEmission } from \"./validation/index.js\";\nexport type { ValidationFail, ValidationOk, ValidationResult } from \"./validation/index.js\";\nexport { mockCandleSource } from \"./mocks/index.js\";\nexport type { MockCandleSourceMode, MockCandleSourceOpts } from \"./mocks/index.js\";\nexport { BufferingAdapter, PassThroughAdapter } from \"./base/index.js\";\nexport { decomposeDrawing, priceToY, timeToX, worldPointToPixel } from \"./geometry/index.js\";\nexport type {\n DrawPrimitive,\n FillStyle,\n Point2,\n StrokeStyle,\n Viewport,\n} from \"./geometry/index.js\";\nexport type {\n Adapter,\n AdapterSymInfo,\n AlertChannel,\n AlertConditionEmission,\n AlertEmission,\n Capabilities,\n CandleEvent,\n DiagnosticCode,\n DrawingCounts,\n DrawingEmission,\n DrawingKind,\n InputKind,\n LogEmission,\n PlotEmission,\n PlotKind,\n PlotOverride,\n PlotSlotDescriptor,\n PlotStyle,\n RunnerEmissions,\n RuntimeDiagnostic,\n SymInfoField,\n} from \"./types.js\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,+DAA+D;AAE/D,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAEtE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAExE,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpD,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAQ7F,OAAO,EACH,iBAAiB,EACjB,oBAAoB,EACpB,SAAS,EACT,UAAU,EACV,WAAW,EACX,cAAc,GACjB,MAAM,wBAAwB,CAAC","sourcesContent":["// Copyright (c) 2026 Invinite. Licensed under the MIT License.\n// See the LICENSE file in the repo root for full license text.\n\nexport { KIND_BUCKET, bucketFor } from \"@invinite-org/chartlang-core\";\nexport type { DrawingBucket, DrawingState } from \"@invinite-org/chartlang-core\";\nexport { defineAdapter } from \"./defineAdapter.js\";\nexport type { DefineAdapterOpts } from \"./defineAdapter.js\";\nexport { PHASE_5_PLOT_KINDS, capabilities } from \"./capabilities/index.js\";\nexport { decodeDrawing, validateEmission } from \"./validation/index.js\";\nexport type { ValidationFail, ValidationOk, ValidationResult } from \"./validation/index.js\";\nexport { mockCandleSource } from \"./mocks/index.js\";\nexport type { MockCandleSourceMode, MockCandleSourceOpts } from \"./mocks/index.js\";\nexport { BufferingAdapter, PassThroughAdapter } from \"./base/index.js\";\nexport { decomposeDrawing, priceToY, timeToX, worldPointToPixel } from \"./geometry/index.js\";\nexport type {\n DrawPrimitive,\n FillStyle,\n Point2,\n StrokeStyle,\n Viewport,\n} from \"./geometry/index.js\";\nexport {\n attachInteraction,\n createViewController,\n onDblCore,\n onDragCore,\n onWheelCore,\n yRangeInWindow,\n} from \"./interaction/index.js\";\nexport type {\n InteractionHandlers,\n ViewController,\n ViewControllerOpts,\n WindowYInput,\n XWindow,\n} from \"./interaction/index.js\";\nexport type {\n Adapter,\n AdapterSymInfo,\n AlertChannel,\n AlertConditionEmission,\n AlertEmission,\n Capabilities,\n CandleEvent,\n DiagnosticCode,\n DrawingCounts,\n DrawingEmission,\n DrawingKind,\n InputKind,\n LogEmission,\n PlotEmission,\n PlotKind,\n PlotOverride,\n PlotSlotDescriptor,\n PlotStyle,\n RunnerEmissions,\n RuntimeDiagnostic,\n SymInfoField,\n} from \"./types.js\";\n"]}
@@ -0,0 +1,90 @@
1
+ import type { ViewController } from "./viewController.js";
2
+ /**
3
+ * The per-frame projection + data context {@link attachInteraction} needs
4
+ * to translate pixel gestures into world-x transforms. The adapter supplies
5
+ * these closing over its latest overlay `Viewport`, so the math always uses
6
+ * the current frame's scale.
7
+ *
8
+ * @since 1.6
9
+ * @stable
10
+ * @example
11
+ * declare const view: import("./viewController.js").ViewController;
12
+ * const h: InteractionHandlers = {
13
+ * controller: view,
14
+ * pxToWorldX: (px) => px,
15
+ * worldXPerPx: () => 1,
16
+ * dataBounds: () => ({ xMin: 0, xMax: 100 }),
17
+ * requestRender: () => {},
18
+ * };
19
+ * void h;
20
+ */
21
+ export type InteractionHandlers = {
22
+ /** The controller whose window the gestures mutate. */
23
+ readonly controller: ViewController;
24
+ /** Map a plot-area pixel x to a world x for the current frame. */
25
+ readonly pxToWorldX: (px: number) => number;
26
+ /** World-x units per pixel for the current frame (pan delta scale). */
27
+ readonly worldXPerPx: () => number;
28
+ /** The current data x extent (grows as bars stream in). */
29
+ readonly dataBounds: () => {
30
+ readonly xMin: number;
31
+ readonly xMax: number;
32
+ };
33
+ /** Re-render the adapter after a gesture mutates the window. */
34
+ readonly requestRender: () => void;
35
+ /** Override the wheel zoom sensitivity (default {@link DEFAULT_ZOOM_STEP}). */
36
+ readonly zoomStep?: number;
37
+ };
38
+ /**
39
+ * Apply one wheel notch: zoom the controller about the world-x under the
40
+ * cursor, then request a render. Pure on `(offsetX, deltaY)` so it is unit
41
+ * tested without a DOM event.
42
+ *
43
+ * @since 1.6
44
+ * @stable
45
+ * @example
46
+ * declare const h: InteractionHandlers;
47
+ * onWheelCore(h, 50, -120); // zoom in about pixel x=50
48
+ */
49
+ export declare function onWheelCore(h: InteractionHandlers, offsetX: number, deltaY: number): void;
50
+ /**
51
+ * Apply one drag step: pan the controller by the pixel delta converted to
52
+ * world x (dragging right reveals earlier data), then request a render.
53
+ * Pure on `dxPx`.
54
+ *
55
+ * @since 1.6
56
+ * @stable
57
+ * @example
58
+ * declare const h: InteractionHandlers;
59
+ * onDragCore(h, 12); // pan left by 12 px
60
+ */
61
+ export declare function onDragCore(h: InteractionHandlers, dxPx: number): void;
62
+ /**
63
+ * Apply a double-click: reset the controller to auto-follow, then request a
64
+ * render.
65
+ *
66
+ * @since 1.6
67
+ * @stable
68
+ * @example
69
+ * declare const h: InteractionHandlers;
70
+ * onDblCore(h);
71
+ */
72
+ export declare function onDblCore(h: InteractionHandlers): void;
73
+ /**
74
+ * Wire wheel-zoom, drag-pan, and double-click-reset on `el`, driving the
75
+ * supplied {@link InteractionHandlers}. Returns a detach function that
76
+ * removes every listener (call it from the adapter's `dispose`). The
77
+ * `addEventListener` plumbing is the only DOM-bound part; the decision
78
+ * logic lives in {@link onWheelCore} / {@link onDragCore} / {@link
79
+ * onDblCore}, which are unit tested directly.
80
+ *
81
+ * @since 1.6
82
+ * @stable
83
+ * @example
84
+ * declare const el: HTMLElement;
85
+ * declare const h: InteractionHandlers;
86
+ * const detach = attachInteraction(el, h);
87
+ * detach();
88
+ */
89
+ export declare function attachInteraction(el: HTMLElement, h: InteractionHandlers): () => void;
90
+ //# sourceMappingURL=domWiring.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"domWiring.d.ts","sourceRoot":"","sources":["../../src/interaction/domWiring.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAQ1D;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAC9B,uDAAuD;IACvD,QAAQ,CAAC,UAAU,EAAE,cAAc,CAAC;IACpC,kEAAkE;IAClE,QAAQ,CAAC,UAAU,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,MAAM,CAAC;IAC5C,uEAAuE;IACvE,QAAQ,CAAC,WAAW,EAAE,MAAM,MAAM,CAAC;IACnC,2DAA2D;IAC3D,QAAQ,CAAC,UAAU,EAAE,MAAM;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IAC5E,gEAAgE;IAChE,QAAQ,CAAC,aAAa,EAAE,MAAM,IAAI,CAAC;IACnC,+EAA+E;IAC/E,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,mBAAmB,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAKzF;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,mBAAmB,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAIrE;AAED;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,mBAAmB,GAAG,IAAI,CAGtD;AAED;;;;;;;;;;;;;;;GAeG;AAEH,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,WAAW,EAAE,CAAC,EAAE,mBAAmB,GAAG,MAAM,IAAI,CAwCrF"}
@@ -0,0 +1,113 @@
1
+ // Copyright (c) 2026 Invinite. Licensed under the MIT License.
2
+ // See the LICENSE file in the repo root for full license text.
3
+ // Wheel notch → zoom factor sensitivity. A positive `deltaY` (scroll down)
4
+ // yields a factor > 1 (zoom out); negative (scroll up) yields < 1 (zoom in).
5
+ // `Math.exp` keeps both directions symmetric, so zoom-out always exists —
6
+ // the bug the native-default drag-zoom-in lacked.
7
+ const DEFAULT_ZOOM_STEP = 0.0015;
8
+ /**
9
+ * Apply one wheel notch: zoom the controller about the world-x under the
10
+ * cursor, then request a render. Pure on `(offsetX, deltaY)` so it is unit
11
+ * tested without a DOM event.
12
+ *
13
+ * @since 1.6
14
+ * @stable
15
+ * @example
16
+ * declare const h: InteractionHandlers;
17
+ * onWheelCore(h, 50, -120); // zoom in about pixel x=50
18
+ */
19
+ export function onWheelCore(h, offsetX, deltaY) {
20
+ const factor = Math.exp(deltaY * (h.zoomStep ?? DEFAULT_ZOOM_STEP));
21
+ const { xMin, xMax } = h.dataBounds();
22
+ h.controller.zoomAt(h.pxToWorldX(offsetX), factor, xMin, xMax);
23
+ h.requestRender();
24
+ }
25
+ /**
26
+ * Apply one drag step: pan the controller by the pixel delta converted to
27
+ * world x (dragging right reveals earlier data), then request a render.
28
+ * Pure on `dxPx`.
29
+ *
30
+ * @since 1.6
31
+ * @stable
32
+ * @example
33
+ * declare const h: InteractionHandlers;
34
+ * onDragCore(h, 12); // pan left by 12 px
35
+ */
36
+ export function onDragCore(h, dxPx) {
37
+ const { xMin, xMax } = h.dataBounds();
38
+ h.controller.panBy(-dxPx * h.worldXPerPx(), xMin, xMax);
39
+ h.requestRender();
40
+ }
41
+ /**
42
+ * Apply a double-click: reset the controller to auto-follow, then request a
43
+ * render.
44
+ *
45
+ * @since 1.6
46
+ * @stable
47
+ * @example
48
+ * declare const h: InteractionHandlers;
49
+ * onDblCore(h);
50
+ */
51
+ export function onDblCore(h) {
52
+ h.controller.reset();
53
+ h.requestRender();
54
+ }
55
+ /**
56
+ * Wire wheel-zoom, drag-pan, and double-click-reset on `el`, driving the
57
+ * supplied {@link InteractionHandlers}. Returns a detach function that
58
+ * removes every listener (call it from the adapter's `dispose`). The
59
+ * `addEventListener` plumbing is the only DOM-bound part; the decision
60
+ * logic lives in {@link onWheelCore} / {@link onDragCore} / {@link
61
+ * onDblCore}, which are unit tested directly.
62
+ *
63
+ * @since 1.6
64
+ * @stable
65
+ * @example
66
+ * declare const el: HTMLElement;
67
+ * declare const h: InteractionHandlers;
68
+ * const detach = attachInteraction(el, h);
69
+ * detach();
70
+ */
71
+ /* v8 ignore start -- DOM event plumbing; the pure cores above carry the coverage */
72
+ export function attachInteraction(el, h) {
73
+ let dragging = false;
74
+ let lastX = 0;
75
+ const onWheel = (e) => {
76
+ e.preventDefault();
77
+ onWheelCore(h, e.offsetX, e.deltaY);
78
+ };
79
+ const onPointerDown = (e) => {
80
+ dragging = true;
81
+ lastX = e.clientX;
82
+ el.setPointerCapture(e.pointerId);
83
+ };
84
+ const onPointerMove = (e) => {
85
+ if (!dragging)
86
+ return;
87
+ const dx = e.clientX - lastX;
88
+ lastX = e.clientX;
89
+ onDragCore(h, dx);
90
+ };
91
+ const onPointerUp = (e) => {
92
+ dragging = false;
93
+ if (el.hasPointerCapture(e.pointerId))
94
+ el.releasePointerCapture(e.pointerId);
95
+ };
96
+ const onDblClick = () => {
97
+ onDblCore(h);
98
+ };
99
+ el.addEventListener("wheel", onWheel, { passive: false });
100
+ el.addEventListener("pointerdown", onPointerDown);
101
+ el.addEventListener("pointermove", onPointerMove);
102
+ el.addEventListener("pointerup", onPointerUp);
103
+ el.addEventListener("dblclick", onDblClick);
104
+ return () => {
105
+ el.removeEventListener("wheel", onWheel);
106
+ el.removeEventListener("pointerdown", onPointerDown);
107
+ el.removeEventListener("pointermove", onPointerMove);
108
+ el.removeEventListener("pointerup", onPointerUp);
109
+ el.removeEventListener("dblclick", onDblClick);
110
+ };
111
+ }
112
+ /* v8 ignore stop */
113
+ //# sourceMappingURL=domWiring.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"domWiring.js","sourceRoot":"","sources":["../../src/interaction/domWiring.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,+DAA+D;AAI/D,2EAA2E;AAC3E,6EAA6E;AAC7E,0EAA0E;AAC1E,kDAAkD;AAClD,MAAM,iBAAiB,GAAG,MAAM,CAAC;AAoCjC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,WAAW,CAAC,CAAsB,EAAE,OAAe,EAAE,MAAc;IAC/E,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,QAAQ,IAAI,iBAAiB,CAAC,CAAC,CAAC;IACpE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC,U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U,CAAC,CAAC;IACnD,CAAC,CAAC;AACN,CAAC;AACD,oBAAoB","sourcesContent":["// Copyright (c) 2026 Invinite. Licensed under the MIT License.\n// See the LICENSE file in the repo root for full license text.\n\nimport type { ViewController } from \"./viewController.js\";\n\n// Wheel notch → zoom factor sensitivity. A positive `deltaY` (scroll down)\n// yields a factor > 1 (zoom out); negative (scroll up) yields < 1 (zoom in).\n// `Math.exp` keeps both directions symmetric, so zoom-out always exists —\n// the bug the native-default drag-zoom-in lacked.\nconst DEFAULT_ZOOM_STEP = 0.0015;\n\n/**\n * The per-frame projection + data context {@link attachInteraction} needs\n * to translate pixel gestures into world-x transforms. The adapter supplies\n * these closing over its latest overlay `Viewport`, so the math always uses\n * the current frame's scale.\n *\n * @since 1.6\n * @stable\n * @example\n * declare const view: import(\"./viewController.js\").ViewController;\n * const h: InteractionHandlers = {\n * controller: view,\n * pxToWorldX: (px) => px,\n * worldXPerPx: () => 1,\n * dataBounds: () => ({ xMin: 0, xMax: 100 }),\n * requestRender: () => {},\n * };\n * void h;\n */\nexport type InteractionHandlers = {\n /** The controller whose window the gestures mutate. */\n readonly controller: ViewController;\n /** Map a plot-area pixel x to a world x for the current frame. */\n readonly pxToWorldX: (px: number) => number;\n /** World-x units per pixel for the current frame (pan delta scale). */\n readonly worldXPerPx: () => number;\n /** The current data x extent (grows as bars stream in). */\n readonly dataBounds: () => { readonly xMin: number; readonly xMax: number };\n /** Re-render the adapter after a gesture mutates the window. */\n readonly requestRender: () => void;\n /** Override the wheel zoom sensitivity (default {@link DEFAULT_ZOOM_STEP}). */\n readonly zoomStep?: number;\n};\n\n/**\n * Apply one wheel notch: zoom the controller about the world-x under the\n * cursor, then request a render. Pure on `(offsetX, deltaY)` so it is unit\n * tested without a DOM event.\n *\n * @since 1.6\n * @stable\n * @example\n * declare const h: InteractionHandlers;\n * onWheelCore(h, 50, -120); // zoom in about pixel x=50\n */\nexport function onWheelCore(h: InteractionHandlers, offsetX: number, deltaY: number): void {\n const factor = Math.exp(deltaY * (h.zoomStep ?? DEFAULT_ZOOM_STEP));\n const { xMin, xMax } = h.dataBounds();\n h.controller.zoomAt(h.pxToWorldX(offsetX), factor, xMin, xMax);\n h.requestRender();\n}\n\n/**\n * Apply one drag step: pan the controller by the pixel delta converted to\n * world x (dragging right reveals earlier data), then request a render.\n * Pure on `dxPx`.\n *\n * @since 1.6\n * @stable\n * @example\n * declare const h: InteractionHandlers;\n * onDragCore(h, 12); // pan left by 12 px\n */\nexport function onDragCore(h: InteractionHandlers, dxPx: number): void {\n const { xMin, xMax } = h.dataBounds();\n h.controller.panBy(-dxPx * h.worldXPerPx(), xMin, xMax);\n h.requestRender();\n}\n\n/**\n * Apply a double-click: reset the controller to auto-follow, then request a\n * render.\n *\n * @since 1.6\n * @stable\n * @example\n * declare const h: InteractionHandlers;\n * onDblCore(h);\n */\nexport function onDblCore(h: InteractionHandlers): void {\n h.controller.reset();\n h.requestRender();\n}\n\n/**\n * Wire wheel-zoom, drag-pan, and double-click-reset on `el`, driving the\n * supplied {@link InteractionHandlers}. Returns a detach function that\n * removes every listener (call it from the adapter's `dispose`). The\n * `addEventListener` plumbing is the only DOM-bound part; the decision\n * logic lives in {@link onWheelCore} / {@link onDragCore} / {@link\n * onDblCore}, which are unit tested directly.\n *\n * @since 1.6\n * @stable\n * @example\n * declare const el: HTMLElement;\n * declare const h: InteractionHandlers;\n * const detach = attachInteraction(el, h);\n * detach();\n */\n/* v8 ignore start -- DOM event plumbing; the pure cores above carry the coverage */\nexport function attachInteraction(el: HTMLElement, h: InteractionHandlers): () => void {\n let dragging = false;\n let lastX = 0;\n\n const onWheel = (e: WheelEvent): void => {\n e.preventDefault();\n onWheelCore(h, e.offsetX, e.deltaY);\n };\n const onPointerDown = (e: PointerEvent): void => {\n dragging = true;\n lastX = e.clientX;\n el.setPointerCapture(e.pointerId);\n };\n const onPointerMove = (e: PointerEvent): void => {\n if (!dragging) return;\n const dx = e.clientX - lastX;\n lastX = e.clientX;\n onDragCore(h, dx);\n };\n const onPointerUp = (e: PointerEvent): void => {\n dragging = false;\n if (el.hasPointerCapture(e.pointerId)) el.releasePointerCapture(e.pointerId);\n };\n const onDblClick = (): void => {\n onDblCore(h);\n };\n\n el.addEventListener(\"wheel\", onWheel, { passive: false });\n el.addEventListener(\"pointerdown\", onPointerDown);\n el.addEventListener(\"pointermove\", onPointerMove);\n el.addEventListener(\"pointerup\", onPointerUp);\n el.addEventListener(\"dblclick\", onDblClick);\n\n return (): void => {\n el.removeEventListener(\"wheel\", onWheel);\n el.removeEventListener(\"pointerdown\", onPointerDown);\n el.removeEventListener(\"pointermove\", onPointerMove);\n el.removeEventListener(\"pointerup\", onPointerUp);\n el.removeEventListener(\"dblclick\", onDblClick);\n };\n}\n/* v8 ignore stop */\n"]}
@@ -0,0 +1,5 @@
1
+ export { createViewController, yRangeInWindow } from "./viewController.js";
2
+ export type { ViewController, ViewControllerOpts, WindowYInput, XWindow, } from "./viewController.js";
3
+ export { attachInteraction, onDblCore, onDragCore, onWheelCore } from "./domWiring.js";
4
+ export type { InteractionHandlers } from "./domWiring.js";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/interaction/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC3E,YAAY,EACR,cAAc,EACd,kBAAkB,EAClB,YAAY,EACZ,OAAO,GACV,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACvF,YAAY,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,5 @@
1
+ // Copyright (c) 2026 Invinite. Licensed under the MIT License.
2
+ // See the LICENSE file in the repo root for full license text.
3
+ export { createViewController, yRangeInWindow } from "./viewController.js";
4
+ export { attachInteraction, onDblCore, onDragCore, onWheelCore } from "./domWiring.js";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interaction/index.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,+DAA+D;AAE/D,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAO3E,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC","sourcesContent":["// Copyright (c) 2026 Invinite. Licensed under the MIT License.\n// See the LICENSE file in the repo root for full license text.\n\nexport { createViewController, yRangeInWindow } from \"./viewController.js\";\nexport type {\n ViewController,\n ViewControllerOpts,\n WindowYInput,\n XWindow,\n} from \"./viewController.js\";\nexport { attachInteraction, onDblCore, onDragCore, onWheelCore } from \"./domWiring.js\";\nexport type { InteractionHandlers } from \"./domWiring.js\";\n"]}
@@ -0,0 +1,120 @@
1
+ /**
2
+ * A visible window into the world x axis (bar times in UTC ms). The two
3
+ * self-scaled adapters (canvas2d, konva) resolve one per frame from a
4
+ * {@link ViewController} and feed `xMin`/`xMax` into their `Viewport`.
5
+ *
6
+ * @since 1.6
7
+ * @stable
8
+ * @example
9
+ * const win: XWindow = { xMin: 0, xMax: 100 };
10
+ * void win;
11
+ */
12
+ export type XWindow = {
13
+ readonly xMin: number;
14
+ readonly xMax: number;
15
+ };
16
+ /**
17
+ * Construction options for {@link createViewController}. `minSpan` is the
18
+ * smallest world-x span a zoom-in can reach (so the window never collapses
19
+ * to a point); `maxSpanFactor` is the largest span a zoom-out can reach as
20
+ * a multiple of the current data span (default `1` ⇒ cannot zoom out past
21
+ * "all data visible").
22
+ *
23
+ * @since 1.6
24
+ * @stable
25
+ * @example
26
+ * const opts: ViewControllerOpts = { minSpan: 1, maxSpanFactor: 1 };
27
+ * void opts;
28
+ */
29
+ export type ViewControllerOpts = {
30
+ readonly minSpan?: number;
31
+ readonly maxSpanFactor?: number;
32
+ };
33
+ /**
34
+ * Stateful, library-agnostic pan/zoom controller for an adapter that
35
+ * computes its own x scale every frame. It holds a user x-window plus a
36
+ * `userInteracted` flag: until the user wheels or drags, {@link
37
+ * ViewController.resolveXWindow} returns the full data range (auto-follow
38
+ * live bars); after the first interaction it returns the held window
39
+ * (re-clamped as data grows), so live frames stop snapping the view back.
40
+ *
41
+ * All transforms are pure functions of the current state + the supplied
42
+ * data bounds — there is no DOM or library coupling. The example adapters
43
+ * wire DOM events to {@link ViewController.zoomAt} / {@link
44
+ * ViewController.panBy} / {@link ViewController.reset} via {@link
45
+ * attachInteraction}.
46
+ *
47
+ * @since 1.6
48
+ * @stable
49
+ * @example
50
+ * const view = createViewController();
51
+ * view.resolveXWindow(0, 100); // { xMin: 0, xMax: 100 } (auto-follow)
52
+ * view.panBy(10, 0, 100);
53
+ * view.userInteracted; // true
54
+ */
55
+ export type ViewController = {
56
+ /** `true` once the user has zoomed or panned (auto-follow is paused). */
57
+ readonly userInteracted: boolean;
58
+ /**
59
+ * The x-window to render this frame: `[dataXMin, dataXMax]` while not
60
+ * interacted (auto-follow), else the held window clamped into the
61
+ * current data bounds.
62
+ */
63
+ resolveXWindow(dataXMin: number, dataXMax: number): XWindow;
64
+ /** Zoom by `factor` (`<1` in, `>1` out) about a world-x pivot. */
65
+ zoomAt(pivotX: number, factor: number, dataXMin: number, dataXMax: number): void;
66
+ /** Pan the window by a signed world-x delta. */
67
+ panBy(deltaWorldX: number, dataXMin: number, dataXMax: number): void;
68
+ /** Clear the held window + flag so the view auto-follows again. */
69
+ reset(): void;
70
+ };
71
+ /**
72
+ * Build a {@link ViewController}. Pass `opts` to tune the zoom-in floor
73
+ * (`minSpan`) and zoom-out ceiling (`maxSpanFactor`); both default to a
74
+ * "1 ms floor, all-data ceiling" policy.
75
+ *
76
+ * @since 1.6
77
+ * @stable
78
+ * @example
79
+ * const view = createViewController({ minSpan: 2, maxSpanFactor: 1 });
80
+ * view.zoomAt(50, 0.5, 0, 100); // zoom in 2× about x=50
81
+ * void view.resolveXWindow(0, 100);
82
+ */
83
+ export declare function createViewController(opts?: ViewControllerOpts): ViewController;
84
+ /**
85
+ * One candidate row for {@link yRangeInWindow}: a world `x` (bar time) plus
86
+ * the low / high values to fold into the y range when `x` is inside the
87
+ * window. Bars pass `{ x: time, lo: low, hi: high }`; a scalar series point
88
+ * passes `lo === hi === value`.
89
+ *
90
+ * @since 1.6
91
+ * @stable
92
+ * @example
93
+ * const c: WindowYInput = { x: 10, lo: 99, hi: 101 };
94
+ * void c;
95
+ */
96
+ export type WindowYInput = {
97
+ readonly x: number;
98
+ readonly lo: number;
99
+ readonly hi: number;
100
+ };
101
+ /**
102
+ * Fold the y range of every candidate whose `x` falls inside `win` — the
103
+ * shared "auto-fit the price scale to the visible window" helper (matching
104
+ * lightweight-charts' auto price scale). Non-finite `lo`/`hi` rows are
105
+ * skipped. Returns `undefined` when no finite in-window candidate is seen,
106
+ * so the caller keeps its own degenerate-range fallback. Horizontal lines
107
+ * (no `x`) are folded in by the caller, not here.
108
+ *
109
+ * @since 1.6
110
+ * @stable
111
+ * @example
112
+ * const r = yRangeInWindow([{ x: 5, lo: 1, hi: 3 }], { xMin: 0, xMax: 10 });
113
+ * // r === { yMin: 1, yMax: 3 }
114
+ * void r;
115
+ */
116
+ export declare function yRangeInWindow(candidates: Iterable<WindowYInput>, win: XWindow): {
117
+ readonly yMin: number;
118
+ readonly yMax: number;
119
+ } | undefined;
120
+ //# sourceMappingURL=viewController.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"viewController.d.ts","sourceRoot":"","sources":["../../src/interaction/viewController.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;GAUG;AACH,MAAM,MAAM,OAAO,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvE;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,kBAAk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ite. Licensed under the MIT License.
2
+ // See the LICENSE file in the repo root for full license text.
3
+ const DEFAULT_MIN_SPAN = 1;
4
+ const DEFAULT_MAX_SPAN_FACTOR = 1;
5
+ // Clamp a candidate window: bound its span to [minSpan, maxSpan] (maxSpan =
6
+ // dataSpan * maxSpanFactor), then shift it to sit inside [dataMin, dataMax]
7
+ // when it fits, or pin it to the full data range when it is at least as wide
8
+ // as the data. The single clamp shared by zoom / pan / resolve.
9
+ function clampWindow(win, dataMin, dataMax, minSpan, maxSpanFactor) {
10
+ const dataSpan = Math.max(0, dataMax - dataMin);
11
+ const maxSpan = dataSpan * maxSpanFactor;
12
+ let span = win.xMax - win.xMin;
13
+ if (maxSpan > 0 && span > maxSpan)
14
+ span = maxSpan;
15
+ if (span < minSpan)
16
+ span = minSpan;
17
+ const center = (win.xMin + win.xMax) / 2;
18
+ let xMin = center - span / 2;
19
+ let xMax = center + span / 2;
20
+ if (span <= dataSpan) {
21
+ if (xMin < dataMin) {
22
+ xMax += dataMin - xMin;
23
+ xMin = dataMin;
24
+ }
25
+ if (xMax > dataMax) {
26
+ xMin -= xMax - dataMax;
27
+ xMax = dataMax;
28
+ }
29
+ return { xMin, xMax };
30
+ }
31
+ return { xMin: dataMin, xMax: dataMax };
32
+ }
33
+ /**
34
+ * Build a {@link ViewController}. Pass `opts` to tune the zoom-in floor
35
+ * (`minSpan`) and zoom-out ceiling (`maxSpanFactor`); both default to a
36
+ * "1 ms floor, all-data ceiling" policy.
37
+ *
38
+ * @since 1.6
39
+ * @stable
40
+ * @example
41
+ * const view = createViewController({ minSpan: 2, maxSpanFactor: 1 });
42
+ * view.zoomAt(50, 0.5, 0, 100); // zoom in 2× about x=50
43
+ * void view.resolveXWindow(0, 100);
44
+ */
45
+ export function createViewController(opts) {
46
+ const minSpan = opts?.minSpan ?? DEFAULT_MIN_SPAN;
47
+ const maxSpanFactor = opts?.maxSpanFactor ?? DEFAULT_MAX_SPAN_FACTOR;
48
+ let held;
49
+ let interacted = false;
50
+ const base = (dataMin, dataMax) => held ?? { xMin: dataMin, xMax: dataMax };
51
+ return {
52
+ get userInteracted() {
53
+ return interacted;
54
+ },
55
+ resolveXWindow(dataMin, dataMax) {
56
+ if (!interacted || held === undefined)
57
+ return { xMin: dataMin, xMax: dataMax };
58
+ return clampWindow(held, dataMin, dataMax, minSpan, maxSpanFactor);
59
+ },
60
+ zoomAt(pivotX, factor, dataMin, dataMax) {
61
+ interacted = true;
62
+ const b = base(dataMin, dataMax);
63
+ const span = b.xMax - b.xMin;
64
+ const frac = span === 0 ? 0.5 : (pivotX - b.xMin) / span;
65
+ const newSpan = span * factor;
66
+ const xMin = pivotX - frac * newSpan;
67
+ held = clampWindow({ xMin, xMax: xMin + newSpan }, dataMin, dataMax, minSpan, maxSpanFactor);
68
+ },
69
+ panBy(deltaWorldX, dataMin, dataMax) {
70
+ interacted = true;
71
+ const b = base(dataMin, dataMax);
72
+ held = clampWindow({ xMin: b.xMin + deltaWorldX, xMax: b.xMax + deltaWorldX }, dataMin, dataMax, minSpan, maxSpanFactor);
73
+ },
74
+ reset() {
75
+ held = undefined;
76
+ interacted = false;
77
+ },
78
+ };
79
+ }
80
+ /**
81
+ * Fold the y range of every candidate whose `x` falls inside `win` — the
82
+ * shared "auto-fit the price scale to the visible window" helper (matching
83
+ * lightweight-charts' auto price scale). Non-finite `lo`/`hi` rows are
84
+ * skipped. Returns `undefined` when no finite in-window candidate is seen,
85
+ * so the caller keeps its own degenerate-range fallback. Horizontal lines
86
+ * (no `x`) are folded in by the caller, not here.
87
+ *
88
+ * @since 1.6
89
+ * @stable
90
+ * @example
91
+ * const r = yRangeInWindow([{ x: 5, lo: 1, hi: 3 }], { xMin: 0, xMax: 10 });
92
+ * // r === { yMin: 1, yMax: 3 }
93
+ * void r;
94
+ */
95
+ export function yRangeInWindow(candidates, win) {
96
+ let yMin = Number.POSITIVE_INFINITY;
97
+ let yMax = Number.NEGATIVE_INFINITY;
98
+ for (const c of candidates) {
99
+ if (c.x < win.xMin || c.x > win.xMax)
100
+ continue;
101
+ if (!Number.isFinite(c.lo) || !Number.isFinite(c.hi))
102
+ continue;
103
+ if (c.lo < yMin)
104
+ yMin = c.lo;
105
+ if (c.hi > yMax)
106
+ yMax = c.hi;
107
+ }
108
+ if (!Number.isFinite(yMin) || !Number.isFinite(yMax))
109
+ return undefined;
110
+ return { yMin, yMax };
111
+ }
112
+ //# sourceMappingURL=viewController.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"viewController.js","sourceRoot":"","sources":["../../src/interaction/viewController.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,+DAA+D;AAwE/D,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAC3B,MAAM,uBAAuB,GAAG,CAAC,CAAC;AAElC,4EAA4E;AAC5E,4EAA4E;AAC5E,6EAA6E;AAC7E,gEAAgE;AAChE,SAAS,WAAW,CAChB,GAAY,EACZ,OAAe,EACf,OAAe,EACf,OAAe,EACf,aAAqB;IAErB,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,CAAC;IAChD,MAAM,OAAO,GAAG,QAAQ,GAAG,aAAa,CAAC;IACzC,IAAI,IAAI,GAAG,GAAG,CAAC,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC;IAC/B,IAAI,OAAO,GAAG,CAAC,IAAI,IAAI,GAAG,OAAO;QAAE,IAAI,GAAG,OAAO,CAAC;IAClD,IAAI,IAAI,GAAG,OAAO;QAAE,IAAI,GAAG,OAAO,CAAC;IACnC,MAAM,MAAM,GAAG,CAAC,GAAG,CAAC,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACzC,IAAI,IAAI,GAAG,MAAM,GAAG,IAAI,GAAG,CAAC,CAAC;IAC7B,IAAI,IAAI,GAAG,MAAM,GAAG,IAAI,GAAG,CAAC,CAAC;IAC7B,IAAI,IAAI,IAAI,QAAQ,EAAE,CAAC;QACnB,IAAI,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,IAAI,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,IAAI;YAAE,SAAS;QAC/C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;YAAE,SAAS;QAC/D,IAAI,CAAC,CAAC,EAAE,GAAG,IAAI;YAAE,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC;QAC7B,IAAI,CAAC,CAAC,EAAE,GAAG,IAAI;YAAE,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC;IACjC,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,SAAS,CAAC;IACvE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;AAC1B,CAAC","sourcesContent":["// Copyright (c) 2026 Invinite. Licensed under the MIT License.\n// See the LICENSE file in the repo root for full license text.\n\n/**\n * A visible window into the world x axis (bar times in UTC ms). The two\n * self-scaled adapters (canvas2d, konva) resolve one per frame from a\n * {@link ViewController} and feed `xMin`/`xMax` into their `Viewport`.\n *\n * @since 1.6\n * @stable\n * @example\n * const win: XWindow = { xMin: 0, xMax: 100 };\n * void win;\n */\nexport type XWindow = { readonly xMin: number; readonly xMax: number };\n\n/**\n * Construction options for {@link createViewController}. `minSpan` is the\n * smallest world-x span a zoom-in can reach (so the window never collapses\n * to a point); `maxSpanFactor` is the largest span a zoom-out can reach as\n * a multiple of the current data span (default `1` ⇒ cannot zoom out past\n * \"all data visible\").\n *\n * @since 1.6\n * @stable\n * @example\n * const opts: ViewControllerOpts = { minSpan: 1, maxSpanFactor: 1 };\n * void opts;\n */\nexport type ViewControllerOpts = {\n readonly minSpan?: number;\n readonly maxSpanFactor?: number;\n};\n\n/**\n * Stateful, library-agnostic pan/zoom controller for an adapter that\n * computes its own x scale every frame. It holds a user x-window plus a\n * `userInteracted` flag: until the user wheels or drags, {@link\n * ViewController.resolveXWindow} returns the full data range (auto-follow\n * live bars); after the first interaction it returns the held window\n * (re-clamped as data grows), so live frames stop snapping the view back.\n *\n * All transforms are pure functions of the current state + the supplied\n * data bounds — there is no DOM or library coupling. The example adapters\n * wire DOM events to {@link ViewController.zoomAt} / {@link\n * ViewController.panBy} / {@link ViewController.reset} via {@link\n * attachInteraction}.\n *\n * @since 1.6\n * @stable\n * @example\n * const view = createViewController();\n * view.resolveXWindow(0, 100); // { xMin: 0, xMax: 100 } (auto-follow)\n * view.panBy(10, 0, 100);\n * view.userInteracted; // true\n */\nexport type ViewController = {\n /** `true` once the user has zoomed or panned (auto-follow is paused). */\n readonly userInteracted: boolean;\n /**\n * The x-window to render this frame: `[dataXMin, dataXMax]` while not\n * interacted (auto-follow), else the held window clamped into the\n * current data bounds.\n */\n resolveXWindow(dataXMin: number, dataXMax: number): XWindow;\n /** Zoom by `factor` (`<1` in, `>1` out) about a world-x pivot. */\n zoomAt(pivotX: number, factor: number, dataXMin: number, dataXMax: number): void;\n /** Pan the window by a signed world-x delta. */\n panBy(deltaWorldX: number, dataXMin: number, dataXMax: number): void;\n /** Clear the held window + flag so the view auto-follows again. */\n reset(): void;\n};\n\nconst DEFAULT_MIN_SPAN = 1;\nconst DEFAULT_MAX_SPAN_FACTOR = 1;\n\n// Clamp a candidate window: bound its span to [minSpan, maxSpan] (maxSpan =\n// dataSpan * maxSpanFactor), then shift it to sit inside [dataMin, dataMax]\n// when it fits, or pin it to the full data range when it is at least as wide\n// as the data. The single clamp shared by zoom / pan / resolve.\nfunction clampWindow(\n win: XWindow,\n dataMin: number,\n dataMax: number,\n minSpan: number,\n maxSpanFactor: number,\n): XWindow {\n const dataSpan = Math.max(0, dataMax - dataMin);\n const maxSpan = dataSpan * maxSpanFactor;\n let span = win.xMax - win.xMin;\n if (maxSpan > 0 && span > maxSpan) span = maxSpan;\n if (span < minSpan) span = minSpan;\n const center = (win.xMin + win.xMax) / 2;\n let xMin = center - span / 2;\n let xMax = center + span / 2;\n if (span <= dataSpan) {\n if (xMin < dataMin) {\n xMax += dataMin - xMin;\n xMin = dataMin;\n }\n if (xMax > dataMax) {\n xMin -= xMax - dataMax;\n xMax = dataMax;\n }\n return { xMin, xMax };\n }\n return { xMin: dataMin, xMax: dataMax };\n}\n\n/**\n * Build a {@link ViewController}. Pass `opts` to tune the zoom-in floor\n * (`minSpan`) and zoom-out ceiling (`maxSpanFactor`); both default to a\n * \"1 ms floor, all-data ceiling\" policy.\n *\n * @since 1.6\n * @stable\n * @example\n * const view = createViewController({ minSpan: 2, maxSpanFactor: 1 });\n * view.zoomAt(50, 0.5, 0, 100); // zoom in 2× about x=50\n * void view.resolveXWindow(0, 100);\n */\nexport function createViewController(opts?: ViewControllerOpts): ViewController {\n const minSpan = opts?.minSpan ?? DEFAULT_MIN_SPAN;\n const maxSpanFactor = opts?.maxSpanFactor ?? DEFAULT_MAX_SPAN_FACTOR;\n let held: XWindow | undefined;\n let interacted = false;\n\n const base = (dataMin: number, dataMax: number): XWindow =>\n held ?? { xMin: dataMin, xMax: dataMax };\n\n return {\n get userInteracted(): boolean {\n return interacted;\n },\n resolveXWindow(dataMin: number, dataMax: number): XWindow {\n if (!interacted || held === undefined) return { xMin: dataMin, xMax: dataMax };\n return clampWindow(held, dataMin, dataMax, minSpan, maxSpanFactor);\n },\n zoomAt(pivotX: number, factor: number, dataMin: number, dataMax: number): void {\n interacted = true;\n const b = base(dataMin, dataMax);\n const span = b.xMax - b.xMin;\n const frac = span === 0 ? 0.5 : (pivotX - b.xMin) / span;\n const newSpan = span * factor;\n const xMin = pivotX - frac * newSpan;\n held = clampWindow(\n { xMin, xMax: xMin + newSpan },\n dataMin,\n dataMax,\n minSpan,\n maxSpanFactor,\n );\n },\n panBy(deltaWorldX: number, dataMin: number, dataMax: number): void {\n interacted = true;\n const b = base(dataMin, dataMax);\n held = clampWindow(\n { xMin: b.xMin + deltaWorldX, xMax: b.xMax + deltaWorldX },\n dataMin,\n dataMax,\n minSpan,\n maxSpanFactor,\n );\n },\n reset(): void {\n held = undefined;\n interacted = false;\n },\n };\n}\n\n/**\n * One candidate row for {@link yRangeInWindow}: a world `x` (bar time) plus\n * the low / high values to fold into the y range when `x` is inside the\n * window. Bars pass `{ x: time, lo: low, hi: high }`; a scalar series point\n * passes `lo === hi === value`.\n *\n * @since 1.6\n * @stable\n * @example\n * const c: WindowYInput = { x: 10, lo: 99, hi: 101 };\n * void c;\n */\nexport type WindowYInput = { readonly x: number; readonly lo: number; readonly hi: number };\n\n/**\n * Fold the y range of every candidate whose `x` falls inside `win` — the\n * shared \"auto-fit the price scale to the visible window\" helper (matching\n * lightweight-charts' auto price scale). Non-finite `lo`/`hi` rows are\n * skipped. Returns `undefined` when no finite in-window candidate is seen,\n * so the caller keeps its own degenerate-range fallback. Horizontal lines\n * (no `x`) are folded in by the caller, not here.\n *\n * @since 1.6\n * @stable\n * @example\n * const r = yRangeInWindow([{ x: 5, lo: 1, hi: 3 }], { xMin: 0, xMax: 10 });\n * // r === { yMin: 1, yMax: 3 }\n * void r;\n */\nexport function yRangeInWindow(\n candidates: Iterable<WindowYInput>,\n win: XWindow,\n): { readonly yMin: number; readonly yMax: number } | undefined {\n let yMin = Number.POSITIVE_INFINITY;\n let yMax = Number.NEGATIVE_INFINITY;\n for (const c of candidates) {\n if (c.x < win.xMin || c.x > win.xMax) continue;\n if (!Number.isFinite(c.lo) || !Number.isFinite(c.hi)) continue;\n if (c.lo < yMin) yMin = c.lo;\n if (c.hi > yMax) yMax = c.hi;\n }\n if (!Number.isFinite(yMin) || !Number.isFinite(yMax)) return undefined;\n return { yMin, yMax };\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@invinite-org/chartlang-adapter-kit",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "SDK for writing chartlang adapters in consumer repos",