@baron1996/klinecharts-adapter 0.1.1 → 0.2.2

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.
Files changed (35) hide show
  1. package/dist/adapter.d.ts +40 -1
  2. package/dist/adapter.d.ts.map +1 -1
  3. package/dist/adapter.js +488 -21
  4. package/dist/conversion/overlays.d.ts +3 -0
  5. package/dist/conversion/overlays.d.ts.map +1 -1
  6. package/dist/conversion/overlays.js +2 -0
  7. package/dist/conversion/panes.d.ts +3 -1
  8. package/dist/conversion/panes.d.ts.map +1 -1
  9. package/dist/conversion/panes.js +7 -1
  10. package/dist/engine.d.ts.map +1 -1
  11. package/dist/engine.js +3 -3
  12. package/dist/extensions/price-measurement.d.ts +13 -0
  13. package/dist/extensions/price-measurement.d.ts.map +1 -0
  14. package/dist/extensions/price-measurement.js +64 -0
  15. package/dist/extensions/register.d.ts.map +1 -1
  16. package/dist/extensions/register.js +2 -0
  17. package/dist/index.d.ts +2 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +2 -0
  20. package/dist/interaction/dragging.d.ts +12 -0
  21. package/dist/interaction/dragging.d.ts.map +1 -0
  22. package/dist/interaction/dragging.js +71 -0
  23. package/dist/interaction/hit-testing.d.ts +26 -0
  24. package/dist/interaction/hit-testing.d.ts.map +1 -0
  25. package/dist/interaction/hit-testing.js +86 -0
  26. package/dist/interaction/selection-arbitration.d.ts +6 -0
  27. package/dist/interaction/selection-arbitration.d.ts.map +1 -0
  28. package/dist/interaction/selection-arbitration.js +10 -0
  29. package/dist/registry/overlays.d.ts +2 -2
  30. package/dist/registry/overlays.d.ts.map +1 -1
  31. package/dist/registry/overlays.js +1 -0
  32. package/dist/version.d.ts +3 -2
  33. package/dist/version.d.ts.map +1 -1
  34. package/dist/version.js +3 -2
  35. package/package.json +2 -2
@@ -81,6 +81,7 @@ function toPoints(overlay) {
81
81
  return [structuredClone(overlay.point ?? {})];
82
82
  case 'rectangle':
83
83
  case 'arrow':
84
+ case 'priceMeasurement':
84
85
  return [structuredClone(overlay.start ?? {}), structuredClone(overlay.end ?? {})];
85
86
  }
86
87
  }
@@ -278,6 +279,7 @@ export function fromEngineOverlay(engine, source, idMap, path, pricePrecision) {
278
279
  };
279
280
  case 'rectangle':
280
281
  case 'arrow':
282
+ case 'priceMeasurement':
281
283
  return {
282
284
  ...base,
283
285
  start: readPoint(0, true, true, `${path}/start`),
@@ -1,6 +1,8 @@
1
- import type { ChartScene } from '@baron1996/kline-scene-schema';
1
+ import type { ChartScene, YAxis as SceneYAxis } from '@baron1996/kline-scene-schema';
2
2
  import type { Chart } from 'klinecharts';
3
3
  import type { EngineIdMap } from './id-map.js';
4
+ /** 原子提交 Scene 前,将单条轴映射到引擎的正式 normal/logarithm 名称。 */
5
+ export declare function overrideSceneYAxis(chart: Chart, idMap: EngineIdMap, axis: SceneYAxis, paneId: string, path: string): void;
4
6
  /** 按 Scene 顺序创建 Pane、Y 轴和指标,并核对每个映射。 */
5
7
  export declare function applyPanes(scene: ChartScene, chart: Chart, idMap: EngineIdMap): void;
6
8
  //# sourceMappingURL=panes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"panes.d.ts","sourceRoot":"","sources":["../../src/conversion/panes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAuB,MAAM,+BAA+B,CAAC;AAErF,OAAO,KAAK,EAAE,KAAK,EAAS,MAAM,aAAa,CAAC;AAEhD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AA8B/C,wCAAwC;AACxC,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,WAAW,GAAG,IAAI,CAuEpF"}
1
+ {"version":3,"file":"panes.d.ts","sourceRoot":"","sources":["../../src/conversion/panes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,KAAK,IAAI,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAErF,OAAO,KAAK,EAAE,KAAK,EAAS,MAAM,aAAa,CAAC;AAEhD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AA8B/C,qDAAqD;AACrD,wBAAgB,kBAAkB,CACjC,KAAK,EAAE,KAAK,EACZ,KAAK,EAAE,WAAW,EAClB,IAAI,EAAE,UAAU,EAChB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,GACV,IAAI,CAIN;AAED,wCAAwC;AACxC,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,WAAW,GAAG,IAAI,CAuEpF"}
@@ -5,7 +5,7 @@ function axisOverride(axis, enginePaneId, engineAxisId) {
5
5
  return {
6
6
  id: engineAxisId,
7
7
  paneId: enginePaneId,
8
- name: 'normal',
8
+ name: axis.scale === 'logarithmic' ? 'logarithm' : 'normal',
9
9
  reverse: axis.reverse,
10
10
  inside: axis.inside,
11
11
  position: axis.position,
@@ -17,6 +17,12 @@ function axisOverride(axis, enginePaneId, engineAxisId) {
17
17
  needWidget: true,
18
18
  };
19
19
  }
20
+ /** 原子提交 Scene 前,将单条轴映射到引擎的正式 normal/logarithm 名称。 */
21
+ export function overrideSceneYAxis(chart, idMap, axis, paneId, path) {
22
+ const enginePaneId = requireMappedId(idMap.paneToEngine, paneId, `${path}/paneId`, 'Pane');
23
+ const engineAxisId = requireMappedId(idMap.yAxisToEngine, axis.id, `${path}/id`, 'Y-axis');
24
+ chart.overrideYAxis(axisOverride(axis, enginePaneId, engineAxisId));
25
+ }
20
26
  /** 按 Scene 顺序创建 Pane、Y 轴和指标,并核对每个映射。 */
21
27
  export function applyPanes(scene, chart, idMap) {
22
28
  for (let paneIndex = 0; paneIndex < scene.panes.length; paneIndex++) {
@@ -1 +1 @@
1
- {"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAEhE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AASzC,MAAM,MAAM,iBAAiB,GAAG,cAAc,aAAa,CAAC,CAAC;AAE7D,MAAM,WAAW,YAAY;IAC5B,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,iBAAiB,CAAC;CACnC;AAiBD,qCAAqC;AACrC,wBAAsB,YAAY,CACjC,SAAS,EAAE,WAAW,EACtB,KAAK,EAAE,UAAU,GACf,OAAO,CAAC,YAAY,CAAC,CA+BvB"}
1
+ {"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAEhE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAUzC,MAAM,MAAM,iBAAiB,GAAG,cAAc,aAAa,CAAC,CAAC;AAE7D,MAAM,WAAW,YAAY;IAC5B,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,iBAAiB,CAAC;CACnC;AAmBD,qCAAqC;AACrC,wBAAsB,YAAY,CACjC,SAAS,EAAE,WAAW,EACtB,KAAK,EAAE,UAAU,GACf,OAAO,CAAC,YAAY,CAAC,CA+BvB"}
package/dist/engine.js CHANGED
@@ -1,13 +1,13 @@
1
1
  import { SceneError } from '@baron1996/kline-scene-schema';
2
2
  import { toKLineChartsOptions } from './conversion/chart-options.js';
3
3
  import { createStaticDataLoader } from './static-data-loader.js';
4
- import { KLINECHARTS_ENGINE_VERSION, KLINECHARTS_RUNTIME_VERSION, } from './version.js';
4
+ import { KLINECHARTS_ENGINE_VERSION, KLINECHARTS_RUNTIME_VERSION, SUPPORTED_KLINECHARTS_RUNTIME_VERSIONS, } from './version.js';
5
5
  function assertRuntimeIdentity(scene, actualEngineVersion) {
6
6
  if (scene.runtime.engine !== 'klinecharts' ||
7
7
  scene.runtime.engineVersion !== KLINECHARTS_ENGINE_VERSION ||
8
8
  actualEngineVersion !== KLINECHARTS_ENGINE_VERSION ||
9
- scene.runtime.runtimeVersion !== KLINECHARTS_RUNTIME_VERSION) {
10
- throw new SceneError('ENGINE_VERSION_MISMATCH', '/runtime', `Expected klinecharts ${KLINECHARTS_ENGINE_VERSION} and Runtime ${KLINECHARTS_RUNTIME_VERSION}; received engine ${actualEngineVersion}.`);
9
+ !SUPPORTED_KLINECHARTS_RUNTIME_VERSIONS.includes(scene.runtime.runtimeVersion)) {
10
+ throw new SceneError('ENGINE_VERSION_MISMATCH', '/runtime', `Expected klinecharts ${KLINECHARTS_ENGINE_VERSION} and a supported Runtime through ${KLINECHARTS_RUNTIME_VERSION}; received engine ${actualEngineVersion}.`);
11
11
  }
12
12
  }
13
13
  /** 按固定顺序创建并初始化唯一的 KLineCharts 引擎。 */
@@ -0,0 +1,13 @@
1
+ import type { registerOverlay } from 'klinecharts';
2
+ type KLineOverlayTemplate = Parameters<typeof registerOverlay>[0];
3
+ export interface PriceMeasurementDisplay {
4
+ readonly absoluteChange: number;
5
+ readonly percentageChange: number;
6
+ readonly label: string;
7
+ }
8
+ /** 仅派生显示值;绝对变化、百分比和 label 永不写入 Scene。 */
9
+ export declare function derivePriceMeasurementDisplay(startValue: number, endValue: number, pricePrecision: number): PriceMeasurementDisplay;
10
+ /** 量度工具只从两个引擎数据点派生显示文字,派生值不进入 Scene。 */
11
+ export declare const priceMeasurementOverlay: KLineOverlayTemplate;
12
+ export {};
13
+ //# sourceMappingURL=price-measurement.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"price-measurement.d.ts","sourceRoot":"","sources":["../../src/extensions/price-measurement.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAGnD,KAAK,oBAAoB,GAAG,UAAU,CAAC,OAAO,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;AAOlE,MAAM,WAAW,uBAAuB;IACvC,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;IAClC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACvB;AAED,yCAAyC;AACzC,wBAAgB,6BAA6B,CAC5C,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,EAChB,cAAc,EAAE,MAAM,GACpB,uBAAuB,CAsBzB;AAED,wCAAwC;AACxC,eAAO,MAAM,uBAAuB,EAAE,oBA6CrC,CAAC"}
@@ -0,0 +1,64 @@
1
+ import { SceneError } from '@baron1996/kline-scene-schema';
2
+ function signedFixed(value, precision) {
3
+ const prefix = value > 0 ? '+' : '';
4
+ return `${prefix}${value.toFixed(precision)}`;
5
+ }
6
+ /** 仅派生显示值;绝对变化、百分比和 label 永不写入 Scene。 */
7
+ export function derivePriceMeasurementDisplay(startValue, endValue, pricePrecision) {
8
+ if (!Number.isFinite(startValue) ||
9
+ !Number.isFinite(endValue) ||
10
+ startValue <= 0 ||
11
+ !Number.isInteger(pricePrecision) ||
12
+ pricePrecision < 0 ||
13
+ pricePrecision > 16) {
14
+ throw new SceneError('SCENE_SCHEMA_INVALID', '/overlays/priceMeasurement', 'Price measurement display inputs must be positive finite prices and a 0..16 precision.');
15
+ }
16
+ const absoluteChange = endValue - startValue;
17
+ const percentageChange = absoluteChange / startValue * 100;
18
+ return {
19
+ absoluteChange,
20
+ percentageChange,
21
+ label: `${signedFixed(absoluteChange, pricePrecision)} (${signedFixed(percentageChange, 2)}%)`,
22
+ };
23
+ }
24
+ /** 量度工具只从两个引擎数据点派生显示文字,派生值不进入 Scene。 */
25
+ export const priceMeasurementOverlay = {
26
+ name: 'priceMeasurement',
27
+ totalStep: 3,
28
+ needDefaultPointFigure: true,
29
+ needDefaultXAxisFigure: true,
30
+ needDefaultYAxisFigure: true,
31
+ createPointFigures: ({ chart, coordinates, overlay }) => {
32
+ const startCoordinate = coordinates[0];
33
+ const endCoordinate = coordinates[1];
34
+ const startPoint = overlay.points[0];
35
+ const endPoint = overlay.points[1];
36
+ if (startCoordinate === undefined ||
37
+ endCoordinate === undefined ||
38
+ startPoint?.value === undefined ||
39
+ endPoint?.value === undefined) {
40
+ return [];
41
+ }
42
+ const pricePrecision = chart.getSymbol()?.pricePrecision ?? 0;
43
+ const { label } = derivePriceMeasurementDisplay(startPoint.value, endPoint.value, pricePrecision);
44
+ return [
45
+ {
46
+ key: 'measurement-body',
47
+ type: 'line',
48
+ attrs: { coordinates: [startCoordinate, endCoordinate] },
49
+ },
50
+ {
51
+ key: 'measurement-label',
52
+ type: 'text',
53
+ attrs: {
54
+ x: (startCoordinate.x + endCoordinate.x) / 2 + 8,
55
+ y: (startCoordinate.y + endCoordinate.y) / 2 - 8,
56
+ text: label,
57
+ align: 'left',
58
+ baseline: 'bottom',
59
+ },
60
+ ignoreEvent: true,
61
+ },
62
+ ];
63
+ },
64
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"register.d.ts","sourceRoot":"","sources":["../../src/extensions/register.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAQnD,KAAK,oBAAoB,GAAG,UAAU,CAAC,OAAO,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;AAClE,KAAK,eAAe,GAAG,CAAC,QAAQ,EAAE,oBAAoB,KAAK,IAAI,CAAC;AAYhE,kCAAkC;AAClC,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI,CAQvE;AAED,wBAAgB,4BAA4B,IAAI,OAAO,CAEtD"}
1
+ {"version":3,"file":"register.d.ts","sourceRoot":"","sources":["../../src/extensions/register.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AASnD,KAAK,oBAAoB,GAAG,UAAU,CAAC,OAAO,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;AAClE,KAAK,eAAe,GAAG,CAAC,QAAQ,EAAE,oBAAoB,KAAK,IAAI,CAAC;AAahE,kCAAkC;AAClC,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI,CAQvE;AAED,wBAAgB,4BAA4B,IAAI,OAAO,CAEtD"}
@@ -2,8 +2,10 @@ import { arrowOverlay } from './arrow.js';
2
2
  import { calloutOverlay } from './callout.js';
3
3
  import { crossLineOverlay } from './cross-line.js';
4
4
  import { rectangleOverlay } from './rectangle.js';
5
+ import { priceMeasurementOverlay } from './price-measurement.js';
5
6
  import { textOverlay } from './text.js';
6
7
  const projectExtensions = [
8
+ priceMeasurementOverlay,
7
9
  rectangleOverlay,
8
10
  arrowOverlay,
9
11
  crossLineOverlay,
package/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  export * from './adapter.js';
2
2
  export * from './errors.js';
3
3
  export * from './extensions/register.js';
4
+ export * from './interaction/dragging.js';
5
+ export * from './interaction/hit-testing.js';
4
6
  export * from './registry/indicators.js';
5
7
  export * from './registry/overlays.js';
6
8
  export * from './static-data-loader.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,cAAc,CAAC"}
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  export * from './adapter.js';
2
2
  export * from './errors.js';
3
3
  export * from './extensions/register.js';
4
+ export * from './interaction/dragging.js';
5
+ export * from './interaction/hit-testing.js';
4
6
  export * from './registry/indicators.js';
5
7
  export * from './registry/overlays.js';
6
8
  export * from './static-data-loader.js';
@@ -0,0 +1,12 @@
1
+ import type { SceneOverlay } from '@baron1996/kline-scene-schema';
2
+ export interface DragDataPoint {
3
+ readonly dataIndex: number;
4
+ readonly value: number;
5
+ }
6
+ export interface DragTarget {
7
+ readonly target: 'anchor' | 'body';
8
+ readonly anchorIndex: number | null;
9
+ }
10
+ /** 根据冻结的绝对平移语义构造未提交候选,不修改输入 Overlay。 */
11
+ export declare function createDragCandidate(before: SceneOverlay, dragTarget: DragTarget, origin: DragDataPoint, current: DragDataPoint, timestamps: readonly number[], pricePrecision: number): SceneOverlay;
12
+ //# sourceMappingURL=dragging.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dragging.d.ts","sourceRoot":"","sources":["../../src/interaction/dragging.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAKlE,MAAM,WAAW,aAAa;IAC7B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,UAAU;IAC1B,QAAQ,CAAC,MAAM,EAAE,QAAQ,GAAG,MAAM,CAAC;IACnC,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CACpC;AAyBD,wCAAwC;AACxC,wBAAgB,mBAAmB,CAClC,MAAM,EAAE,YAAY,EACpB,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,aAAa,EACrB,OAAO,EAAE,aAAa,EACtB,UAAU,EAAE,SAAS,MAAM,EAAE,EAC7B,cAAc,EAAE,MAAM,GACpB,YAAY,CAgFd"}
@@ -0,0 +1,71 @@
1
+ import { SceneError } from '@baron1996/kline-scene-schema';
2
+ import { normalizePriceValue } from '../conversion/price.js';
3
+ function requireDataIndex(value, path) {
4
+ if (!Number.isFinite(value)) {
5
+ throw new SceneError('INVALID_REFERENCE', path, 'Drag data index must be finite.');
6
+ }
7
+ return Math.round(value);
8
+ }
9
+ function requireTimestamp(timestamps, index, path) {
10
+ const timestamp = timestamps[index];
11
+ if (timestamp === undefined) {
12
+ throw new SceneError('INVALID_REFERENCE', path, 'Drag candidate must remain on an embedded market-data bar.');
13
+ }
14
+ return timestamp;
15
+ }
16
+ /** 根据冻结的绝对平移语义构造未提交候选,不修改输入 Overlay。 */
17
+ export function createDragCandidate(before, dragTarget, origin, current, timestamps, pricePrecision) {
18
+ const deltaValue = current.value - origin.value;
19
+ if (!Number.isFinite(deltaValue)) {
20
+ throw new SceneError('SCENE_SCHEMA_INVALID', '/overlays', 'Drag price delta must be finite.');
21
+ }
22
+ if (before.type === 'horizontalStraightLine') {
23
+ const anchor = before.anchor;
24
+ if (anchor === undefined || !('value' in anchor)) {
25
+ throw new SceneError('SCENE_SCHEMA_INVALID', '/overlays/anchor', 'Missing price anchor.');
26
+ }
27
+ return {
28
+ ...structuredClone(before),
29
+ anchor: {
30
+ value: normalizePriceValue(anchor.value + deltaValue, pricePrecision, '/overlays/anchor/value'),
31
+ },
32
+ };
33
+ }
34
+ if (before.type !== 'priceMeasurement' || before.start === undefined || before.end === undefined) {
35
+ throw new SceneError('SCENE_SCHEMA_INVALID', '/overlays/type', 'Controlled M2 dragging only supports horizontalStraightLine and priceMeasurement.');
36
+ }
37
+ const candidate = structuredClone(before);
38
+ if (dragTarget.target === 'anchor') {
39
+ if (dragTarget.anchorIndex !== 0 && dragTarget.anchorIndex !== 1) {
40
+ throw new SceneError('INVALID_REFERENCE', '/overlays/anchorIndex', 'Invalid anchor index.');
41
+ }
42
+ const index = requireDataIndex(current.dataIndex, '/overlays/anchor/dataIndex');
43
+ const point = {
44
+ timestamp: requireTimestamp(timestamps, index, '/overlays/anchor/timestamp'),
45
+ value: normalizePriceValue(current.value, pricePrecision, '/overlays/anchor/value'),
46
+ };
47
+ if (dragTarget.anchorIndex === 0) {
48
+ candidate.start = point;
49
+ }
50
+ else {
51
+ candidate.end = point;
52
+ }
53
+ return candidate;
54
+ }
55
+ const startIndex = timestamps.indexOf(before.start.timestamp);
56
+ const endIndex = timestamps.indexOf(before.end.timestamp);
57
+ if (startIndex < 0 || endIndex < 0) {
58
+ throw new SceneError('INVALID_REFERENCE', '/overlays', 'priceMeasurement endpoints must reference embedded bars before dragging.');
59
+ }
60
+ const deltaIndex = requireDataIndex(current.dataIndex, '/overlays/body/dataIndex') -
61
+ requireDataIndex(origin.dataIndex, '/overlays/body/originDataIndex');
62
+ candidate.start = {
63
+ timestamp: requireTimestamp(timestamps, startIndex + deltaIndex, '/overlays/start/timestamp'),
64
+ value: normalizePriceValue(before.start.value + deltaValue, pricePrecision, '/overlays/start/value'),
65
+ };
66
+ candidate.end = {
67
+ timestamp: requireTimestamp(timestamps, endIndex + deltaIndex, '/overlays/end/timestamp'),
68
+ value: normalizePriceValue(before.end.value + deltaValue, pricePrecision, '/overlays/end/value'),
69
+ };
70
+ return candidate;
71
+ }
@@ -0,0 +1,26 @@
1
+ export declare const OVERLAY_BODY_HIT_THRESHOLD_CSS_PX = 12;
2
+ export declare const OVERLAY_ANCHOR_HIT_THRESHOLD_CSS_PX = 14;
3
+ export interface PixelCoordinate {
4
+ readonly x: number;
5
+ readonly y: number;
6
+ }
7
+ export interface OverlayPixelGeometry {
8
+ readonly overlayId: string;
9
+ readonly sceneIndex: number;
10
+ readonly zLevel: number;
11
+ readonly locked: boolean;
12
+ readonly anchors: readonly PixelCoordinate[];
13
+ readonly bodySegments: readonly (readonly [PixelCoordinate, PixelCoordinate])[];
14
+ }
15
+ export interface OverlayHitResult {
16
+ readonly overlayId: string;
17
+ readonly target: 'anchor' | 'body';
18
+ readonly anchorIndex: number | null;
19
+ readonly locked: boolean;
20
+ }
21
+ /**
22
+ * 按冻结契约在 CSS 像素坐标中执行命中测试。
23
+ * 锚点全局优先于主体;同类再按 zLevel、Scene 后序和锚点低索引排序。
24
+ */
25
+ export declare function hitTestOverlayGeometries(point: PixelCoordinate, geometries: readonly OverlayPixelGeometry[]): OverlayHitResult | null;
26
+ //# sourceMappingURL=hit-testing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hit-testing.d.ts","sourceRoot":"","sources":["../../src/interaction/hit-testing.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,iCAAiC,KAAK,CAAC;AACpD,eAAO,MAAM,mCAAmC,KAAK,CAAC;AAEtD,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,oBAAoB;IACpC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,OAAO,EAAE,SAAS,eAAe,EAAE,CAAC;IAC7C,QAAQ,CAAC,YAAY,EAAE,SAAS,CAAC,SAAS,CAAC,eAAe,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC;CAChF;AAED,MAAM,WAAW,gBAAgB;IAChC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,MAAM,EAAE,QAAQ,GAAG,MAAM,CAAC;IACnC,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;CACzB;AAmDD;;;GAGG;AACH,wBAAgB,wBAAwB,CACvC,KAAK,EAAE,eAAe,EACtB,UAAU,EAAE,SAAS,oBAAoB,EAAE,GACzC,gBAAgB,GAAG,IAAI,CAgDzB"}
@@ -0,0 +1,86 @@
1
+ export const OVERLAY_BODY_HIT_THRESHOLD_CSS_PX = 12;
2
+ export const OVERLAY_ANCHOR_HIT_THRESHOLD_CSS_PX = 14;
3
+ function coordinateDistance(left, right) {
4
+ return Math.hypot(left.x - right.x, left.y - right.y);
5
+ }
6
+ function segmentDistance(point, start, end) {
7
+ const dx = end.x - start.x;
8
+ const dy = end.y - start.y;
9
+ const lengthSquared = dx * dx + dy * dy;
10
+ if (lengthSquared === 0) {
11
+ return coordinateDistance(point, start);
12
+ }
13
+ const projection = Math.max(0, Math.min(1, ((point.x - start.x) * dx + (point.y - start.y) * dy) / lengthSquared));
14
+ return coordinateDistance(point, {
15
+ x: start.x + projection * dx,
16
+ y: start.y + projection * dy,
17
+ });
18
+ }
19
+ function compareHits(left, right) {
20
+ if (left.zLevel !== right.zLevel) {
21
+ return right.zLevel - left.zLevel;
22
+ }
23
+ if (left.sceneIndex !== right.sceneIndex) {
24
+ return right.sceneIndex - left.sceneIndex;
25
+ }
26
+ if (left.overlayId === right.overlayId &&
27
+ left.anchorIndex !== null &&
28
+ right.anchorIndex !== null &&
29
+ left.anchorIndex !== right.anchorIndex) {
30
+ return left.anchorIndex - right.anchorIndex;
31
+ }
32
+ return left.distance - right.distance;
33
+ }
34
+ /**
35
+ * 按冻结契约在 CSS 像素坐标中执行命中测试。
36
+ * 锚点全局优先于主体;同类再按 zLevel、Scene 后序和锚点低索引排序。
37
+ */
38
+ export function hitTestOverlayGeometries(point, geometries) {
39
+ const anchorHits = [];
40
+ const bodyHits = [];
41
+ for (const geometry of geometries) {
42
+ for (let anchorIndex = 0; anchorIndex < geometry.anchors.length; anchorIndex++) {
43
+ const anchor = geometry.anchors[anchorIndex];
44
+ if (anchor === undefined) {
45
+ continue;
46
+ }
47
+ const distance = coordinateDistance(point, anchor);
48
+ if (distance <= OVERLAY_ANCHOR_HIT_THRESHOLD_CSS_PX) {
49
+ anchorHits.push({
50
+ overlayId: geometry.overlayId,
51
+ target: 'anchor',
52
+ anchorIndex,
53
+ locked: geometry.locked,
54
+ distance,
55
+ sceneIndex: geometry.sceneIndex,
56
+ zLevel: geometry.zLevel,
57
+ });
58
+ }
59
+ }
60
+ let distance = Number.POSITIVE_INFINITY;
61
+ for (const [start, end] of geometry.bodySegments) {
62
+ distance = Math.min(distance, segmentDistance(point, start, end));
63
+ }
64
+ if (distance <= OVERLAY_BODY_HIT_THRESHOLD_CSS_PX) {
65
+ bodyHits.push({
66
+ overlayId: geometry.overlayId,
67
+ target: 'body',
68
+ anchorIndex: null,
69
+ locked: geometry.locked,
70
+ distance,
71
+ sceneIndex: geometry.sceneIndex,
72
+ zLevel: geometry.zLevel,
73
+ });
74
+ }
75
+ }
76
+ const winner = (anchorHits.length > 0 ? anchorHits : bodyHits).sort(compareHits)[0];
77
+ if (winner === undefined) {
78
+ return null;
79
+ }
80
+ return {
81
+ overlayId: winner.overlayId,
82
+ target: winner.target,
83
+ anchorIndex: winner.anchorIndex,
84
+ locked: winner.locked,
85
+ };
86
+ }
@@ -0,0 +1,6 @@
1
+ import { type OverlayPixelGeometry, type PixelCoordinate } from './hit-testing.js';
2
+ /**
3
+ * 仅忽略引擎旧命中区域产生的取消选择:回调坐标必须仍命中当前选中 Overlay 的规范几何。
4
+ */
5
+ export declare function shouldIgnoreStaleOverlayDeselection(selectedOverlayId: string | null, deselectedOverlayId: string, coordinate: PixelCoordinate | undefined, currentGeometries: readonly OverlayPixelGeometry[]): boolean;
6
+ //# sourceMappingURL=selection-arbitration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"selection-arbitration.d.ts","sourceRoot":"","sources":["../../src/interaction/selection-arbitration.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,oBAAoB,EACzB,KAAK,eAAe,EACpB,MAAM,kBAAkB,CAAC;AAE1B;;GAEG;AACH,wBAAgB,mCAAmC,CAClD,iBAAiB,EAAE,MAAM,GAAG,IAAI,EAChC,mBAAmB,EAAE,MAAM,EAC3B,UAAU,EAAE,eAAe,GAAG,SAAS,EACvC,iBAAiB,EAAE,SAAS,oBAAoB,EAAE,GAChD,OAAO,CAKT"}
@@ -0,0 +1,10 @@
1
+ import { hitTestOverlayGeometries, } from './hit-testing.js';
2
+ /**
3
+ * 仅忽略引擎旧命中区域产生的取消选择:回调坐标必须仍命中当前选中 Overlay 的规范几何。
4
+ */
5
+ export function shouldIgnoreStaleOverlayDeselection(selectedOverlayId, deselectedOverlayId, coordinate, currentGeometries) {
6
+ if (selectedOverlayId !== deselectedOverlayId || coordinate === undefined) {
7
+ return false;
8
+ }
9
+ return hitTestOverlayGeometries(coordinate, currentGeometries)?.overlayId === deselectedOverlayId;
10
+ }
@@ -1,6 +1,6 @@
1
1
  import type { SceneOverlay } from '@baron1996/kline-scene-schema';
2
2
  export declare const BUILT_IN_OVERLAYS: readonly ["horizontalRayLine", "horizontalSegment", "horizontalStraightLine", "verticalRayLine", "verticalSegment", "verticalStraightLine", "rayLine", "segment", "straightLine", "priceLine", "priceChannelLine", "parallelStraightLine", "fibonacciLine", "brush", "simpleAnnotation", "simpleTag"];
3
- export declare const PROJECT_OVERLAYS: readonly ["rectangle", "arrow", "crossLine", "callout", "text"];
4
- export declare const SUPPORTED_OVERLAYS: readonly ["horizontalRayLine", "horizontalSegment", "horizontalStraightLine", "verticalRayLine", "verticalSegment", "verticalStraightLine", "rayLine", "segment", "straightLine", "priceLine", "priceChannelLine", "parallelStraightLine", "fibonacciLine", "brush", "simpleAnnotation", "simpleTag", "rectangle", "arrow", "crossLine", "callout", "text"];
3
+ export declare const PROJECT_OVERLAYS: readonly ["priceMeasurement", "rectangle", "arrow", "crossLine", "callout", "text"];
4
+ export declare const SUPPORTED_OVERLAYS: readonly ["horizontalRayLine", "horizontalSegment", "horizontalStraightLine", "verticalRayLine", "verticalSegment", "verticalStraightLine", "rayLine", "segment", "straightLine", "priceLine", "priceChannelLine", "parallelStraightLine", "fibonacciLine", "brush", "simpleAnnotation", "simpleTag", "priceMeasurement", "rectangle", "arrow", "crossLine", "callout", "text"];
5
5
  export declare function isSupportedOverlay(name: string): name is SceneOverlay['type'];
6
6
  //# sourceMappingURL=overlays.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"overlays.d.ts","sourceRoot":"","sources":["../../src/registry/overlays.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAElE,eAAO,MAAM,iBAAiB,uSAiBsB,CAAC;AAErD,eAAO,MAAM,gBAAgB,iEAMuB,CAAC;AAErD,eAAO,MAAM,kBAAkB,6VAGqB,CAAC;AAIrD,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,IAAI,YAAY,CAAC,MAAM,CAAC,CAE7E"}
1
+ {"version":3,"file":"overlays.d.ts","sourceRoot":"","sources":["../../src/registry/overlays.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAElE,eAAO,MAAM,iBAAiB,uSAiBsB,CAAC;AAErD,eAAO,MAAM,gBAAgB,qFAOuB,CAAC;AAErD,eAAO,MAAM,kBAAkB,iXAGqB,CAAC;AAIrD,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,IAAI,YAAY,CAAC,MAAM,CAAC,CAE7E"}
@@ -17,6 +17,7 @@ export const BUILT_IN_OVERLAYS = [
17
17
  'simpleTag',
18
18
  ];
19
19
  export const PROJECT_OVERLAYS = [
20
+ 'priceMeasurement',
20
21
  'rectangle',
21
22
  'arrow',
22
23
  'crossLine',
package/dist/version.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  /** Adapter 包版本。 */
2
- export declare const ADAPTER_PACKAGE_VERSION: "0.1.1";
2
+ export declare const ADAPTER_PACKAGE_VERSION: "0.2.2";
3
3
  /** 唯一允许加载的 KLineCharts 引擎版本。 */
4
4
  export declare const KLINECHARTS_ENGINE_VERSION: "10.0.0";
5
5
  /** 与 ChartScene 绑定的 Runtime 协议版本。 */
6
- export declare const KLINECHARTS_RUNTIME_VERSION: "0.1.0";
6
+ export declare const KLINECHARTS_RUNTIME_VERSION: "0.2.0";
7
+ export declare const SUPPORTED_KLINECHARTS_RUNTIME_VERSIONS: readonly ["0.1.0", "0.2.0"];
7
8
  //# sourceMappingURL=version.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA,mBAAmB;AACnB,eAAO,MAAM,uBAAuB,EAAG,OAAgB,CAAC;AAExD,gCAAgC;AAChC,eAAO,MAAM,0BAA0B,EAAG,QAAiB,CAAC;AAE5D,qCAAqC;AACrC,eAAO,MAAM,2BAA2B,EAAG,OAAgB,CAAC"}
1
+ {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA,mBAAmB;AACnB,eAAO,MAAM,uBAAuB,EAAG,OAAgB,CAAC;AAExD,gCAAgC;AAChC,eAAO,MAAM,0BAA0B,EAAG,QAAiB,CAAC;AAE5D,qCAAqC;AACrC,eAAO,MAAM,2BAA2B,EAAG,OAAgB,CAAC;AAC5D,eAAO,MAAM,sCAAsC,6BAA8B,CAAC"}
package/dist/version.js CHANGED
@@ -1,6 +1,7 @@
1
1
  /** Adapter 包版本。 */
2
- export const ADAPTER_PACKAGE_VERSION = '0.1.1';
2
+ export const ADAPTER_PACKAGE_VERSION = '0.2.2';
3
3
  /** 唯一允许加载的 KLineCharts 引擎版本。 */
4
4
  export const KLINECHARTS_ENGINE_VERSION = '10.0.0';
5
5
  /** 与 ChartScene 绑定的 Runtime 协议版本。 */
6
- export const KLINECHARTS_RUNTIME_VERSION = '0.1.0';
6
+ export const KLINECHARTS_RUNTIME_VERSION = '0.2.0';
7
+ export const SUPPORTED_KLINECHARTS_RUNTIME_VERSIONS = ['0.1.0', '0.2.0'];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baron1996/klinecharts-adapter",
3
- "version": "0.1.1",
3
+ "version": "0.2.2",
4
4
  "description": "Controlled ChartScene adapter for KLineCharts.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -38,7 +38,7 @@
38
38
  "typecheck": "tsc -p tsconfig.json --noEmit"
39
39
  },
40
40
  "dependencies": {
41
- "@baron1996/kline-scene-schema": "0.1.0",
41
+ "@baron1996/kline-scene-schema": "0.2.2",
42
42
  "klinecharts": "10.0.0"
43
43
  }
44
44
  }