Skip to content

Commit

Permalink
Merge master into branch 'horz-scale-as-numbers'
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-korobko committed Aug 15, 2023
2 parents 16890a9 + 981ca1b commit 9222b83
Show file tree
Hide file tree
Showing 16 changed files with 803 additions and 9 deletions.
8 changes: 4 additions & 4 deletions .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ module.exports = [
{
name: 'CJS',
path: 'dist/lightweight-charts.production.cjs',
limit: '46.94 KB',
limit: '47.07 KB',
},
{
name: 'ESM',
path: 'dist/lightweight-charts.production.mjs',
limit: '46.86 KB',
limit: '47.01 KB',
},
{
name: 'Standalone-ESM',
path: 'dist/lightweight-charts.standalone.production.mjs',
limit: '48.56 KB',
limit: '48.70 KB',
},
{
name: 'Standalone',
path: 'dist/lightweight-charts.standalone.production.js',
limit: '48.61 KB',
limit: '48.74 KB',
},
];
19 changes: 19 additions & 0 deletions src/api/chart-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export class ChartApi<HorzScaleItem> implements IChartApiBase<HorzScaleItem>, Da
private readonly _seriesMapReversed: Map<Series<SeriesType>, SeriesApi<SeriesType, HorzScaleItem>> = new Map();

private readonly _clickedDelegate: Delegate<MouseEventParams<HorzScaleItem>> = new Delegate();
private readonly _dblClickedDelegate: Delegate<MouseEventParams<HorzScaleItem>> = new Delegate();
private readonly _crosshairMovedDelegate: Delegate<MouseEventParams<HorzScaleItem>> = new Delegate();

private readonly _timeScaleApi: TimeScaleApi<HorzScaleItem>;
Expand All @@ -143,6 +144,14 @@ export class ChartApi<HorzScaleItem> implements IChartApiBase<HorzScaleItem>, Da
},
this
);
this._chartWidget.dblClicked().subscribe(
(paramSupplier: MouseEventParamsImplSupplier) => {
if (this._dblClickedDelegate.hasListeners()) {
this._dblClickedDelegate.fire(this._convertMouseParams(paramSupplier()));
}
},
this
);
this._chartWidget.crosshairMoved().subscribe(
(paramSupplier: MouseEventParamsImplSupplier) => {
if (this._crosshairMovedDelegate.hasListeners()) {
Expand All @@ -158,6 +167,7 @@ export class ChartApi<HorzScaleItem> implements IChartApiBase<HorzScaleItem>, Da

public remove(): void {
this._chartWidget.clicked().unsubscribeAll(this);
this._chartWidget.dblClicked().unsubscribeAll(this);
this._chartWidget.crosshairMoved().unsubscribeAll(this);

this._timeScaleApi.destroy();
Expand All @@ -167,6 +177,7 @@ export class ChartApi<HorzScaleItem> implements IChartApiBase<HorzScaleItem>, Da
this._seriesMapReversed.clear();

this._clickedDelegate.destroy();
this._dblClickedDelegate.destroy();
this._crosshairMovedDelegate.destroy();
this._dataLayer.destroy();
}
Expand Down Expand Up @@ -265,6 +276,14 @@ export class ChartApi<HorzScaleItem> implements IChartApiBase<HorzScaleItem>, Da
this._crosshairMovedDelegate.unsubscribe(handler);
}

public subscribeDblClick(handler: MouseEventHandler<HorzScaleItem>): void {
this._dblClickedDelegate.subscribe(handler);
}

public unsubscribeDblClick(handler: MouseEventHandler<HorzScaleItem>): void {
this._dblClickedDelegate.unsubscribe(handler);
}

public priceScale(priceScaleId: string): IPriceScaleApi {
return new PriceScaleApi(this._chartWidget, priceScaleId);
}
Expand Down
30 changes: 30 additions & 0 deletions src/api/ichart-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,36 @@ export interface IChartApiBase<HorzScaleItem = Time> {
*/
unsubscribeClick(handler: MouseEventHandler<HorzScaleItem>): void;

/**
* Subscribe to the chart double-click event.
*
* @param handler - Handler to be called on mouse double-click.
* @example
* ```js
* function myDblClickHandler(param) {
* if (!param.point) {
* return;
* }
*
* console.log(`Double Click at ${param.point.x}, ${param.point.y}. The time is ${param.time}.`);
* }
*
* chart.subscribeDblClick(myDblClickHandler);
* ```
*/
subscribeDblClick(handler: MouseEventHandler<HorzScaleItem>): void;

/**
* Unsubscribe a handler that was previously subscribed using {@link subscribeDblClick}.
*
* @param handler - Previously subscribed handler
* @example
* ```js
* chart.unsubscribeDblClick(myDblClickHandler);
* ```
*/
unsubscribeDblClick(handler: MouseEventHandler<HorzScaleItem>): void;

/**
* Subscribe to the crosshair move event.
*
Expand Down
17 changes: 17 additions & 0 deletions src/gui/chart-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class ChartWidget<HorzScaleItem> implements IDestroyable, IChartWidgetBas
private _invalidateMask: InvalidateMask | null = null;
private _drawPlanned: boolean = false;
private _clicked: Delegate<MouseEventParamsImplSupplier> = new Delegate();
private _dblClicked: Delegate<MouseEventParamsImplSupplier> = new Delegate();
private _crosshairMoved: Delegate<MouseEventParamsImplSupplier> = new Delegate();
private _onWheelBound: (event: WheelEvent) => void;
private _observer: ResizeObserver | null = null;
Expand Down Expand Up @@ -163,6 +164,7 @@ export class ChartWidget<HorzScaleItem> implements IDestroyable, IChartWidgetBas
for (const paneWidget of this._paneWidgets) {
this._tableElement.removeChild(paneWidget.getElement());
paneWidget.clicked().unsubscribeAll(this);
paneWidget.dblClicked().unsubscribeAll(this);
paneWidget.destroy();
}
this._paneWidgets = [];
Expand All @@ -180,6 +182,7 @@ export class ChartWidget<HorzScaleItem> implements IDestroyable, IChartWidgetBas

this._crosshairMoved.destroy();
this._clicked.destroy();
this._dblClicked.destroy();

this._uninstallObserver();
}
Expand Down Expand Up @@ -246,6 +249,10 @@ export class ChartWidget<HorzScaleItem> implements IDestroyable, IChartWidgetBas
return this._clicked;
}

public dblClicked(): ISubscription<MouseEventParamsImplSupplier> {
return this._dblClicked;
}

public crosshairMoved(): ISubscription<MouseEventParamsImplSupplier> {
return this._crosshairMoved;
}
Expand Down Expand Up @@ -696,6 +703,7 @@ export class ChartWidget<HorzScaleItem> implements IDestroyable, IChartWidgetBas
const paneWidget = ensureDefined(this._paneWidgets.pop());
this._tableElement.removeChild(paneWidget.getElement());
paneWidget.clicked().unsubscribeAll(this);
paneWidget.dblClicked().unsubscribeAll(this);
paneWidget.destroy();

// const paneSeparator = this._paneSeparators.pop();
Expand All @@ -708,6 +716,7 @@ export class ChartWidget<HorzScaleItem> implements IDestroyable, IChartWidgetBas
for (let i = actualPaneWidgetsCount; i < targetPaneWidgetsCount; i++) {
const paneWidget = new PaneWidget(this, panes[i]);
paneWidget.clicked().subscribe(this._onPaneWidgetClicked.bind(this), this);
paneWidget.dblClicked().subscribe(this._onPaneWidgetDblClicked.bind(this), this);

this._paneWidgets.push(paneWidget);

Expand Down Expand Up @@ -789,6 +798,14 @@ export class ChartWidget<HorzScaleItem> implements IDestroyable, IChartWidgetBas
this._clicked.fire(() => this._getMouseEventParamsImpl(time, point, event));
}

private _onPaneWidgetDblClicked(
time: TimePointIndex | null,
point: Point | null,
event: TouchMouseEventData
): void {
this._dblClicked.fire(() => this._getMouseEventParamsImpl(time, point, event));
}

private _onPaneWidgetCrosshairMoved(
time: TimePointIndex | null,
point: Point | null,
Expand Down
24 changes: 22 additions & 2 deletions src/gui/pane-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class PaneWidget implements IDestroyable, MouseEventHandlers {
private _startScrollingPos: StartScrollPosition | null = null;
private _isScrolling: boolean = false;
private _clicked: Delegate<TimePointIndex | null, Point, TouchMouseEventData> = new Delegate();
private _dblClicked: Delegate<TimePointIndex | null, Point, TouchMouseEventData> = new Delegate();
private _prevPinchScale: number = 0;
private _longTap: boolean = false;
private _startTrackPoint: Point | null = null;
Expand Down Expand Up @@ -266,6 +267,17 @@ export class PaneWidget implements IDestroyable, MouseEventHandlers {
this._fireClickedDelegate(event);
}

public mouseDoubleClickEvent(event: MouseEventHandlerMouseEvent | MouseEventHandlerTouchEvent): void {
if (this._state === null) {
return;
}
this._fireMouseClickDelegate(this._dblClicked, event);
}

public doubleTapEvent(event: MouseEventHandlerTouchEvent): void {
this.mouseDoubleClickEvent(event);
}

public pressedMouseMoveEvent(event: MouseEventHandlerMouseEvent): void {
this._onMouseEvent();
this._pressedMouseTouchMoveEvent(event);
Expand Down Expand Up @@ -313,6 +325,10 @@ export class PaneWidget implements IDestroyable, MouseEventHandlers {
return this._clicked;
}

public dblClicked(): ISubscription<TimePointIndex | null, Point, TouchMouseEventData> {
return this._dblClicked;
}

public pinchStartEvent(): void {
this._prevPinchScale = 1;
this._model().stopTimeScaleAnimation();
Expand Down Expand Up @@ -501,10 +517,14 @@ export class PaneWidget implements IDestroyable, MouseEventHandlers {
}

private _fireClickedDelegate(event: MouseEventHandlerEventBase): void {
this._fireMouseClickDelegate(this._clicked, event);
}

private _fireMouseClickDelegate(delegate: Delegate<TimePointIndex | null, Point, TouchMouseEventData>, event: MouseEventHandlerEventBase): void {
const x = event.localX;
const y = event.localY;
if (this._clicked.hasListeners()) {
this._clicked.fire(this._model().timeScale().coordinateToIndex(x), { x, y }, event);
if (delegate.hasListeners()) {
delegate.fire(this._model().timeScale().coordinateToIndex(x), { x, y }, event);
}
}

Expand Down
20 changes: 18 additions & 2 deletions src/model/price-range-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ import { isNumber } from '../helpers/strict-type-checks';

import { PriceRange } from './series-options';

function computeFiniteResult(
method: (...values: number[]) => number,
valueOne: number,
valueTwo: number,
fallback: number
): number {
const firstFinite = Number.isFinite(valueOne);
const secondFinite = Number.isFinite(valueTwo);

if (firstFinite && secondFinite) {
return method(valueOne, valueTwo);
}

return !firstFinite && !secondFinite ? fallback : (firstFinite ? valueOne : valueTwo);
}

export class PriceRangeImpl {
private _minValue: number;
private _maxValue!: number;
Expand Down Expand Up @@ -43,8 +59,8 @@ export class PriceRangeImpl {
return this;
}
return new PriceRangeImpl(
Math.min(this.minValue(), anotherRange.minValue()),
Math.max(this.maxValue(), anotherRange.maxValue())
computeFiniteResult(Math.min, this.minValue(), anotherRange.minValue(), -Infinity),
computeFiniteResult(Math.max, this.maxValue(), anotherRange.maxValue(), Infinity)
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function runTestCase(container) {
const chartOptions = {
rightPriceScale: {
borderVisible: false,
mode: 2,
},
layout: {
textColor: 'black',
background: { type: 'solid', color: 'white' },
},
};
const chart = (window.chart = LightweightCharts.createChart(
container,
chartOptions
));

/*
* We expect the blue series to NOT be visible
* and the red series to BE visible
*/

const series1 = chart.addLineSeries({
color: '#2962FF',
});
series1.setData([
{ time: 1522033200, value: 0 },
{ time: 1529895600, value: -3 },
{ time: 1537758000, value: 3 },
]);
const series2 = chart.addLineSeries({
color: '#FF2962',
});
series2.setData([
{ time: 1522033200, value: 1 },
{ time: 1529895600, value: -1 },
{ time: 1537758000, value: 2 },
]);
chart.timeScale().fitContent();
}
5 changes: 4 additions & 1 deletion tests/e2e/helpers/perform-interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
doVerticalDrag,
} from './mouse-drag-actions';
import { doMouseScroll } from './mouse-scroll-actions';
import { pageTimeout } from './page-timeout';
import { doLongTouch, doPinchZoomTouch, doSwipeTouch } from './touch-actions';
import { doZoomInZoomOut } from './zoom-action';

Expand Down Expand Up @@ -84,7 +85,9 @@ async function performAction(
await target.click({ button: 'left' });
break;
case 'doubleClick':
await target.click({ button: 'left', clickCount: 2 });
await target.click({ button: 'left' });
await pageTimeout(page, 200);
await target.click({ button: 'left' });
break;
case 'outsideClick':
{
Expand Down
Loading

0 comments on commit 9222b83

Please sign in to comment.