@adrienhobbs/candlekit 0.2.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.
package/dist/index.js CHANGED
@@ -327,6 +327,8 @@ function ChartComponent({
327
327
  const mouseDownPosRef = useRef(null);
328
328
  const heightRef = useRef(height);
329
329
  heightRef.current = height;
330
+ const lineEditEnabledRef = useRef(false);
331
+ lineEditEnabledRef.current = Boolean(onAddLine || onClearAllLines);
330
332
  useEffect(() => {
331
333
  if (!chartContainerRef.current) return;
332
334
  chartContainerRef.current.style.position = "relative";
@@ -416,6 +418,7 @@ function ChartComponent({
416
418
  const resizeObserver = new ResizeObserver(handleResize);
417
419
  resizeObserver.observe(chartContainerRef.current);
418
420
  const handleContextMenu = (e) => {
421
+ if (!lineEditEnabledRef.current) return;
419
422
  e.preventDefault();
420
423
  e.stopPropagation();
421
424
  if (!candlestickSeriesRef.current) {
@@ -527,16 +530,37 @@ function ChartComponent({
527
530
  if (!chartRef.current || !focusTradeId) return;
528
531
  const trade = trades.find((t) => t.id === focusTradeId);
529
532
  if (!trade) return;
530
- const entrySec = trade.entryTime / 1e3;
531
- const exitSec = trade.exitTime / 1e3;
532
- const sorted = [...bars].sort((a, b) => a.timestamp - b.timestamp);
533
- const stepSec = sorted.length > 1 ? (sorted[sorted.length - 1].timestamp - sorted[0].timestamp) / 1e3 / (sorted.length - 1) : 300;
534
- const pad = Math.max(exitSec - entrySec, stepSec * 12);
533
+ const seen = /* @__PURE__ */ new Set();
534
+ const series = [];
535
+ for (const b of [...bars].sort((a, b2) => a.timestamp - b2.timestamp)) {
536
+ if (seen.has(b.timestamp)) continue;
537
+ seen.add(b.timestamp);
538
+ series.push(b.timestamp);
539
+ }
540
+ if (series.length === 0) return;
541
+ const nearestIdx = (ms) => {
542
+ let lo = 0;
543
+ let hi = series.length - 1;
544
+ let idx = series.length - 1;
545
+ while (lo <= hi) {
546
+ const mid = lo + hi >> 1;
547
+ if (series[mid] >= ms) {
548
+ idx = mid;
549
+ hi = mid - 1;
550
+ } else {
551
+ lo = mid + 1;
552
+ }
553
+ }
554
+ return idx;
555
+ };
556
+ const entryIdx = nearestIdx(trade.entryTime);
557
+ const exitIdx = Math.max(entryIdx, nearestIdx(trade.exitTime));
558
+ const PAD = 15;
535
559
  const raf = requestAnimationFrame(() => {
536
560
  try {
537
- chartRef.current?.timeScale().setVisibleRange({
538
- from: entrySec - pad,
539
- to: exitSec + pad
561
+ chartRef.current?.timeScale().setVisibleLogicalRange({
562
+ from: entryIdx - PAD,
563
+ to: exitIdx + PAD
540
564
  });
541
565
  } catch {
542
566
  }
@@ -878,7 +902,7 @@ function ChartComponent({
878
902
  }
879
903
  )
880
904
  ] }),
881
- chartContainerRef.current && lines.map((line) => {
905
+ onDeleteLine && chartContainerRef.current && lines.map((line) => {
882
906
  const pos = linePositions.get(line.id);
883
907
  if (!pos) return null;
884
908
  return createPortal(