awesome-timeline-react 0.13.1 → 0.14.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/dist/index.mjs CHANGED
@@ -520,7 +520,10 @@ var DEFAULT_THEME = {
520
520
  groupChildBackground: "rgba(0,0,0,0.05)",
521
521
  groupShadowColor: "rgba(0,0,0,0.22)",
522
522
  pointerLineColor: "rgba(15,23,42,0.7)",
523
- pointerLabelColor: "#ffffff"
523
+ pointerLabelColor: "#ffffff",
524
+ linkColor: "#2563eb",
525
+ linkWidth: 2,
526
+ linkMarkerTextColor: "#ffffff"
524
527
  };
525
528
  var resolveTheme = (theme) => __spreadValues(__spreadValues({}, DEFAULT_THEME), theme);
526
529
 
@@ -714,65 +717,8 @@ import React5, {
714
717
  useSyncExternalStore
715
718
  } from "react";
716
719
 
717
- // src/canvas/frame-scheduler.ts
718
- var FrameScheduler = class {
719
- constructor(onFrame) {
720
- this.onFrame = onFrame;
721
- this.rafId = null;
722
- this.dirty = { static: false, dynamic: false };
723
- }
724
- invalidate(layer) {
725
- if (layer === "all" || layer === "static") this.dirty.static = true;
726
- if (layer === "all" || layer === "dynamic") this.dirty.dynamic = true;
727
- this.schedule();
728
- }
729
- /** Runs a pending frame immediately (tests, synchronous reads). */
730
- flush() {
731
- if (this.rafId !== null) {
732
- cancelAnimationFrame(this.rafId);
733
- this.rafId = null;
734
- this.run();
735
- }
736
- }
737
- dispose() {
738
- if (this.rafId !== null) {
739
- cancelAnimationFrame(this.rafId);
740
- this.rafId = null;
741
- }
742
- this.dirty = { static: false, dynamic: false };
743
- }
744
- schedule() {
745
- if (this.rafId !== null) return;
746
- this.rafId = requestAnimationFrame(() => {
747
- this.rafId = null;
748
- this.run();
749
- });
750
- }
751
- run() {
752
- const dirty = this.dirty;
753
- this.dirty = { static: false, dynamic: false };
754
- if (dirty.static || dirty.dynamic) this.onFrame(dirty);
755
- }
756
- };
757
-
758
- // src/core/culling.ts
759
- var cullToWindow = (events, windowStart, windowEnd) => {
760
- let lo = 0;
761
- let hi = events.length;
762
- while (lo < hi) {
763
- const mid = lo + hi >> 1;
764
- if (events[mid].startTime <= windowEnd) {
765
- lo = mid + 1;
766
- } else {
767
- hi = mid;
768
- }
769
- }
770
- const visible = [];
771
- for (let i = 0; i < lo; i++) {
772
- if (events[i].endTime >= windowStart) visible.push(events[i]);
773
- }
774
- return visible;
775
- };
720
+ // src/core/types.ts
721
+ var rectContains = (rect, x, y) => x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height;
776
722
 
777
723
  // src/core/coords.ts
778
724
  var timeToX = (time, windowStart, tick) => (time - windowStart) / tick;
@@ -822,6 +768,92 @@ var assignLanes = (rowEvents) => {
822
768
  return { laneOf, highestLane: Math.max(0, laneEnd.length - 1) };
823
769
  };
824
770
 
771
+ // src/core/links.ts
772
+ var MARKER_HEIGHT = 20;
773
+ var MARKER_PAD = 8;
774
+ var MARKER_INSET = 4;
775
+ var MARKER_GAP = 4;
776
+ var LABEL_MAX = 28;
777
+ var eventRect = (scene, offsetOf, eventId, view) => {
778
+ var _a;
779
+ const event = scene.getEvent(eventId);
780
+ if (!event) return null;
781
+ const top = offsetOf.get(event.rowId);
782
+ if (top === void 0) return null;
783
+ const g = scene.getGeometry();
784
+ const lane = (_a = scene.getLanes(event.rowId).laneOf.get(eventId)) != null ? _a : 0;
785
+ return {
786
+ x: timeToX(event.startTime, view.windowStart, view.tick),
787
+ y: top - view.scrollTop + scene.getLaneInset(event.rowId) + laneTop(lane, g),
788
+ width: timeToWidth(event.startTime, event.endTime, view.tick),
789
+ height: g.barHeight
790
+ };
791
+ };
792
+ var linkRoute = (from, to, stub = 8) => {
793
+ const ax = from.x + from.width, ay = from.y + from.height / 2;
794
+ const bx = to.x, by = to.y + to.height / 2;
795
+ if (bx - ax >= 2 * stub) return [[ax, ay], [ax + stub, ay], [ax + stub, by], [bx, by]];
796
+ const midY = Math.abs(ay - by) < 1 ? from.y + from.height + stub / 2 : (ay + by) / 2;
797
+ return [[ax, ay], [ax + stub, ay], [ax + stub, midY], [bx - stub, midY], [bx - stub, by], [bx, by]];
798
+ };
799
+ var truncate = (text) => text.length > LABEL_MAX ? `${text.slice(0, LABEL_MAX - 1)}\u2026` : text;
800
+ var linkMarkers = (scene, links, rectOf, view, measure) => {
801
+ const shown = (r) => r.y + r.height > 0 && r.y < view.height;
802
+ const wanted = /* @__PURE__ */ new Map();
803
+ for (const link of links) {
804
+ const a = rectOf(link.fromEventId), b = rectOf(link.toEventId);
805
+ if (!a || !b) continue;
806
+ for (const [hidden, id, other] of [[b, link.toEventId, a], [a, link.fromEventId, b]]) {
807
+ if (shown(hidden) || !shown(other) || wanted.has(id)) continue;
808
+ wanted.set(id, { up: hidden.y + hidden.height <= 0, x: hidden.x });
809
+ }
810
+ }
811
+ const out = [];
812
+ for (const up of [true, false]) {
813
+ const y = up ? MARKER_INSET : view.height - MARKER_HEIGHT - MARKER_INSET;
814
+ const edge = [...wanted].filter(([, w]) => w.up === up).sort((p, q) => p[1].x - q[1].x).map(([eventId, w]) => {
815
+ var _a, _b, _c;
816
+ const label = truncate(`${up ? "\u25B2" : "\u25BC"} ${(_c = (_b = (_a = scene.getEvent(eventId)) == null ? void 0 : _a.props) == null ? void 0 : _b.label) != null ? _c : eventId}`);
817
+ return { eventId, up, label, rect: { x: w.x, y, width: measure(label) + 2 * MARKER_PAD, height: MARKER_HEIGHT } };
818
+ });
819
+ let right = MARKER_INSET - MARKER_GAP;
820
+ for (const m of edge) {
821
+ m.rect.x = Math.max(m.rect.x, right + MARKER_GAP);
822
+ right = m.rect.x + m.rect.width;
823
+ }
824
+ let left = view.width - MARKER_INSET + MARKER_GAP;
825
+ for (const m of [...edge].reverse()) {
826
+ m.rect.x = Math.max(MARKER_INSET, Math.min(m.rect.x, left - MARKER_GAP - m.rect.width));
827
+ left = m.rect.x;
828
+ }
829
+ out.push(...edge);
830
+ }
831
+ return out;
832
+ };
833
+ var markerAt = (markers, x, y) => {
834
+ var _a;
835
+ return (_a = markers.find((m) => rectContains(m.rect, x, y))) != null ? _a : null;
836
+ };
837
+
838
+ // src/core/culling.ts
839
+ var cullToWindow = (events, windowStart, windowEnd) => {
840
+ let lo = 0;
841
+ let hi = events.length;
842
+ while (lo < hi) {
843
+ const mid = lo + hi >> 1;
844
+ if (events[mid].startTime <= windowEnd) {
845
+ lo = mid + 1;
846
+ } else {
847
+ hi = mid;
848
+ }
849
+ }
850
+ const visible = [];
851
+ for (let i = 0; i < lo; i++) {
852
+ if (events[i].endTime >= windowStart) visible.push(events[i]);
853
+ }
854
+ return visible;
855
+ };
856
+
825
857
  // src/core/interactions.ts
826
858
  var computeDropTimes = ({
827
859
  pointerX,
@@ -1079,7 +1111,7 @@ var paintOverlapStripes = (ctx, event, rect, rowStatics, windowStart, tick, colo
1079
1111
  ctx.restore();
1080
1112
  }
1081
1113
  };
1082
- var drawEvents = (ctx, view, scene, font, animator) => {
1114
+ var drawEvents = (ctx, view, scene, font, animator, beforeSelected) => {
1083
1115
  var _a, _b, _c, _d;
1084
1116
  if (view.tick === null) return;
1085
1117
  const [windowStart, windowEnd] = view.windowTime;
@@ -1209,9 +1241,104 @@ var drawEvents = (ctx, view, scene, font, animator) => {
1209
1241
  }
1210
1242
  }
1211
1243
  }
1244
+ beforeSelected == null ? void 0 : beforeSelected();
1212
1245
  for (const paint of deferred) paint();
1213
1246
  };
1214
1247
 
1248
+ // src/canvas/draw-links.ts
1249
+ var ARROW = 6;
1250
+ var drawLinks = (ctx, view, scene, font) => {
1251
+ var _a, _b, _c, _d, _e, _f, _g;
1252
+ if (view.tick === null || !view.links.length) return [];
1253
+ const { offsetOf } = scene.getRowOffsets();
1254
+ const at = { windowStart: view.windowTime[0], tick: view.tick, scrollTop: view.scrollTop };
1255
+ const rects = /* @__PURE__ */ new Map();
1256
+ const rectOf = (id) => {
1257
+ var _a2;
1258
+ if (!rects.has(id)) rects.set(id, eventRect(scene, offsetOf, id, at));
1259
+ return (_a2 = rects.get(id)) != null ? _a2 : null;
1260
+ };
1261
+ const { theme } = view;
1262
+ ctx.save();
1263
+ ctx.lineJoin = "round";
1264
+ for (const link of view.links) {
1265
+ const a = rectOf(link.fromEventId), b = rectOf(link.toEventId);
1266
+ if (!a || !b) continue;
1267
+ const color = (_b = (_a = link.style) == null ? void 0 : _a.stroke) != null ? _b : theme.linkColor;
1268
+ const points = linkRoute(a, b);
1269
+ ctx.strokeStyle = color;
1270
+ ctx.lineWidth = (_d = (_c = link.style) == null ? void 0 : _c.strokeWidth) != null ? _d : theme.linkWidth;
1271
+ ctx.setLineDash((_f = (_e = link.style) == null ? void 0 : _e.dash) != null ? _f : []);
1272
+ ctx.beginPath();
1273
+ points.forEach(([x, y], i) => i ? ctx.lineTo(x, y) : ctx.moveTo(x, y));
1274
+ ctx.stroke();
1275
+ const [bx, by] = points[points.length - 1];
1276
+ ctx.setLineDash([]);
1277
+ ctx.fillStyle = color;
1278
+ ctx.beginPath();
1279
+ ctx.moveTo(bx, by);
1280
+ ctx.lineTo(bx - ARROW, by - ARROW / 1.4);
1281
+ ctx.lineTo(bx - ARROW, by + ARROW / 1.4);
1282
+ ctx.closePath();
1283
+ ctx.fill();
1284
+ }
1285
+ ctx.font = (_g = theme.font) != null ? _g : font;
1286
+ ctx.textBaseline = "middle";
1287
+ ctx.textAlign = "left";
1288
+ const markers = linkMarkers(scene, view.links, rectOf, view, (text) => ctx.measureText(text).width);
1289
+ for (const m of markers) {
1290
+ const { x, y, width, height } = m.rect;
1291
+ ctx.fillStyle = theme.linkColor;
1292
+ roundedRect(ctx, x, y, width, height, height / 2);
1293
+ ctx.fill();
1294
+ ctx.fillStyle = theme.linkMarkerTextColor;
1295
+ ctx.fillText(m.label, x + 8, y + height / 2);
1296
+ }
1297
+ ctx.restore();
1298
+ return markers;
1299
+ };
1300
+
1301
+ // src/canvas/frame-scheduler.ts
1302
+ var FrameScheduler = class {
1303
+ constructor(onFrame) {
1304
+ this.onFrame = onFrame;
1305
+ this.rafId = null;
1306
+ this.dirty = { static: false, dynamic: false };
1307
+ }
1308
+ invalidate(layer) {
1309
+ if (layer === "all" || layer === "static") this.dirty.static = true;
1310
+ if (layer === "all" || layer === "dynamic") this.dirty.dynamic = true;
1311
+ this.schedule();
1312
+ }
1313
+ /** Runs a pending frame immediately (tests, synchronous reads). */
1314
+ flush() {
1315
+ if (this.rafId !== null) {
1316
+ cancelAnimationFrame(this.rafId);
1317
+ this.rafId = null;
1318
+ this.run();
1319
+ }
1320
+ }
1321
+ dispose() {
1322
+ if (this.rafId !== null) {
1323
+ cancelAnimationFrame(this.rafId);
1324
+ this.rafId = null;
1325
+ }
1326
+ this.dirty = { static: false, dynamic: false };
1327
+ }
1328
+ schedule() {
1329
+ if (this.rafId !== null) return;
1330
+ this.rafId = requestAnimationFrame(() => {
1331
+ this.rafId = null;
1332
+ this.run();
1333
+ });
1334
+ }
1335
+ run() {
1336
+ const dirty = this.dirty;
1337
+ this.dirty = { static: false, dynamic: false };
1338
+ if (dirty.static || dirty.dynamic) this.onFrame(dirty);
1339
+ }
1340
+ };
1341
+
1215
1342
  // src/canvas/renderer.ts
1216
1343
  var TimelineRenderer = class {
1217
1344
  constructor(staticCanvas, dynamicCanvas, scene) {
@@ -1232,8 +1359,10 @@ var TimelineRenderer = class {
1232
1359
  hoveredEventId: null,
1233
1360
  draggedEventId: null,
1234
1361
  resizePreview: null,
1235
- selectedEventIds: /* @__PURE__ */ new Set()
1362
+ selectedEventIds: /* @__PURE__ */ new Set(),
1363
+ links: []
1236
1364
  };
1365
+ this.linkMarkers = [];
1237
1366
  this.animator = new LayoutAnimator();
1238
1367
  this.dpr = typeof window !== "undefined" ? window.devicePixelRatio : 1;
1239
1368
  this.font = "16px sans-serif";
@@ -1249,6 +1378,10 @@ var TimelineRenderer = class {
1249
1378
  getView() {
1250
1379
  return this.view;
1251
1380
  }
1381
+ /** the link edge markers as last drawn, in viewport px */
1382
+ getLinkMarkers() {
1383
+ return this.linkMarkers;
1384
+ }
1252
1385
  setView(partial) {
1253
1386
  const sizeChanged = partial.width !== void 0 && partial.width !== this.view.width || partial.height !== void 0 && partial.height !== this.view.height;
1254
1387
  this.view = __spreadValues(__spreadValues({}, this.view), partial);
@@ -1317,7 +1450,9 @@ var TimelineRenderer = class {
1317
1450
  const ctx = this.dynamicCanvas.getContext("2d");
1318
1451
  if (ctx) {
1319
1452
  ctx.clearRect(0, 0, this.view.width, this.view.height);
1320
- drawEvents(ctx, this.view, this.scene, this.font, this.animator);
1453
+ drawEvents(ctx, this.view, this.scene, this.font, this.animator, () => {
1454
+ this.linkMarkers = drawLinks(ctx, this.view, this.scene, this.font);
1455
+ });
1321
1456
  }
1322
1457
  }
1323
1458
  if (this.animator.isAnimating()) {
@@ -1329,9 +1464,6 @@ var TimelineRenderer = class {
1329
1464
  // src/hooks/use-canvas-interactions.ts
1330
1465
  import { useCallback, useEffect as useEffect4, useMemo, useRef as useRef4 } from "react";
1331
1466
 
1332
- // src/core/types.ts
1333
- var rectContains = (rect, x, y) => x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height;
1334
-
1335
1467
  // src/core/hit-test.ts
1336
1468
  var hitTest = (scene, view, x, y) => {
1337
1469
  const { windowStart, windowEnd, tick, scrollTop } = view;
@@ -1416,6 +1548,7 @@ var useCanvasInteractions = ({
1416
1548
  onResize,
1417
1549
  onEventClick,
1418
1550
  onEventHover,
1551
+ onLinkReveal,
1419
1552
  eventsResize,
1420
1553
  panZoom,
1421
1554
  selectable,
@@ -1545,6 +1678,32 @@ var useCanvasInteractions = ({
1545
1678
  },
1546
1679
  [dynamicCanvasRef, eventsResize]
1547
1680
  );
1681
+ const markerUnder = useCallback(
1682
+ (clientX, clientY) => {
1683
+ const canvas = dynamicCanvasRef.current;
1684
+ const renderer = rendererRef.current;
1685
+ if (!canvas || !renderer) return null;
1686
+ const rect = canvas.getBoundingClientRect();
1687
+ return markerAt(renderer.getLinkMarkers(), clientX - rect.left, clientY - rect.top);
1688
+ },
1689
+ [dynamicCanvasRef, rendererRef]
1690
+ );
1691
+ const reveal = useCallback(
1692
+ (eventId) => {
1693
+ const event = scene.getEvent(eventId);
1694
+ if (!event) return;
1695
+ const body = bodyRef.current;
1696
+ const top = scene.getRowOffsets().offsetOf.get(event.rowId);
1697
+ if (top !== void 0 && body) body.scrollTop = Math.max(0, top - body.clientHeight / 3);
1698
+ setWindowTime((prev) => {
1699
+ if (event.startTime >= prev[0] && event.startTime <= prev[1]) return prev;
1700
+ const span = prev[1] - prev[0];
1701
+ return [event.startTime - span / 5, event.startTime - span / 5 + span];
1702
+ });
1703
+ onLinkReveal == null ? void 0 : onLinkReveal(eventId);
1704
+ },
1705
+ [scene, bodyRef, setWindowTime, onLinkReveal]
1706
+ );
1548
1707
  const schedulePan = useCallback(() => {
1549
1708
  if (panFrameRef.current !== null) return;
1550
1709
  panFrameRef.current = requestAnimationFrame(() => {
@@ -1634,6 +1793,17 @@ var useCanvasInteractions = ({
1634
1793
  return;
1635
1794
  }
1636
1795
  if (pointerEvent.button !== 0) return;
1796
+ const marker = markerUnder(pointerEvent.clientX, pointerEvent.clientY);
1797
+ if (marker) {
1798
+ stateRef.current = {
1799
+ mode: "marker",
1800
+ eventId: marker.eventId,
1801
+ startClientX: pointerEvent.clientX,
1802
+ startClientY: pointerEvent.clientY
1803
+ };
1804
+ canvas.setPointerCapture(pointerEvent.pointerId);
1805
+ return;
1806
+ }
1637
1807
  const hit = currentHit(pointerEvent.clientX, pointerEvent.clientY);
1638
1808
  if ((hit == null ? void 0 : hit.kind) === "event" && !((_b = hit.event.props) == null ? void 0 : _b.isLocked)) {
1639
1809
  const direction = resizeDirectionAt(hit, pointerEvent.clientX);
@@ -1702,7 +1872,8 @@ var useCanvasInteractions = ({
1702
1872
  ghostRef,
1703
1873
  rendererRef,
1704
1874
  styleGhost,
1705
- selectable
1875
+ selectable,
1876
+ markerUnder
1706
1877
  ]
1707
1878
  );
1708
1879
  const handleHover = useCallback(
@@ -1724,6 +1895,7 @@ var useCanvasInteractions = ({
1724
1895
  } else {
1725
1896
  canvas.style.cursor = "grab";
1726
1897
  }
1898
+ if (markerUnder(pointerEvent.clientX, pointerEvent.clientY)) canvas.style.cursor = "pointer";
1727
1899
  const hoveredId = (hit == null ? void 0 : hit.kind) === "event" ? hit.event.id : null;
1728
1900
  const prevId = (prevHovered == null ? void 0 : prevHovered.kind) === "event" ? prevHovered.event.id : null;
1729
1901
  if (hoveredId !== prevId) {
@@ -1757,7 +1929,8 @@ var useCanvasInteractions = ({
1757
1929
  dynamicCanvasRef,
1758
1930
  eventPromptRef,
1759
1931
  rendererRef,
1760
- onEventHover
1932
+ onEventHover,
1933
+ markerUnder
1761
1934
  ]
1762
1935
  );
1763
1936
  const resolveRowAtClientY = useCallback(
@@ -1841,6 +2014,7 @@ var useCanvasInteractions = ({
1841
2014
  }
1842
2015
  return;
1843
2016
  }
2017
+ if (state.mode === "marker") return;
1844
2018
  state.deltaPx = pointerEvent.clientX - state.startClientX;
1845
2019
  (_d = rendererRef.current) == null ? void 0 : _d.setView({
1846
2020
  resizePreview: {
@@ -1979,6 +2153,9 @@ var useCanvasInteractions = ({
1979
2153
  }
1980
2154
  } else if (state.mode === "resize") {
1981
2155
  commitResize(state);
2156
+ } else if (state.mode === "marker") {
2157
+ const moved = Math.abs(pointerEvent.clientX - state.startClientX) + Math.abs(pointerEvent.clientY - state.startClientY) > DRAG_THRESHOLD_PX;
2158
+ if (!moved) reveal(state.eventId);
1982
2159
  }
1983
2160
  },
1984
2161
  [
@@ -1991,7 +2168,8 @@ var useCanvasInteractions = ({
1991
2168
  selectable,
1992
2169
  applySelection,
1993
2170
  getSelection,
1994
- scene
2171
+ scene,
2172
+ reveal
1995
2173
  ]
1996
2174
  );
1997
2175
  const handlePointerCancel = useCallback(
@@ -2118,6 +2296,8 @@ var CanvasBoard = forwardRef2(
2118
2296
  onEventHover,
2119
2297
  eventsResize,
2120
2298
  stripeOverlap,
2299
+ links,
2300
+ onLinkReveal,
2121
2301
  timeBar,
2122
2302
  panZoom,
2123
2303
  animations,
@@ -2161,6 +2341,7 @@ var CanvasBoard = forwardRef2(
2161
2341
  drawEvent,
2162
2342
  eventsResize,
2163
2343
  stripeOverlap,
2344
+ links,
2164
2345
  timeBar
2165
2346
  });
2166
2347
  }, [
@@ -2171,6 +2352,7 @@ var CanvasBoard = forwardRef2(
2171
2352
  drawEvent,
2172
2353
  eventsResize,
2173
2354
  stripeOverlap,
2355
+ links,
2174
2356
  timeBar,
2175
2357
  animations,
2176
2358
  rendererRef
@@ -2299,6 +2481,7 @@ var CanvasBoard = forwardRef2(
2299
2481
  onResize,
2300
2482
  onEventClick,
2301
2483
  onEventHover,
2484
+ onLinkReveal,
2302
2485
  eventsResize,
2303
2486
  panZoom,
2304
2487
  selectable,
@@ -2966,6 +3149,7 @@ var SceneStore = class {
2966
3149
 
2967
3150
  // src/timeline.tsx
2968
3151
  var defaultPointerFormat = (time) => new Date(time * 1e3).toLocaleString();
3152
+ var NO_LINKS = [];
2969
3153
  var Timeline = forwardRef4(
2970
3154
  ({
2971
3155
  rows,
@@ -2997,7 +3181,9 @@ var Timeline = forwardRef4(
2997
3181
  showPointerLine = false,
2998
3182
  formatPointerTime,
2999
3183
  onPointerTime,
3000
- animations = true
3184
+ animations = true,
3185
+ links = NO_LINKS,
3186
+ onLinkReveal
3001
3187
  }, handleRef) => {
3002
3188
  const [windowTime, setWindowTime] = useState4([
3003
3189
  new Date(
@@ -3318,6 +3504,8 @@ var Timeline = forwardRef4(
3318
3504
  onEventHover: handleEventHover,
3319
3505
  eventsResize,
3320
3506
  stripeOverlap,
3507
+ links,
3508
+ onLinkReveal,
3321
3509
  timeBar,
3322
3510
  panZoom,
3323
3511
  animations: resolvedAnimations,