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.js CHANGED
@@ -541,7 +541,10 @@ var DEFAULT_THEME = {
541
541
  groupChildBackground: "rgba(0,0,0,0.05)",
542
542
  groupShadowColor: "rgba(0,0,0,0.22)",
543
543
  pointerLineColor: "rgba(15,23,42,0.7)",
544
- pointerLabelColor: "#ffffff"
544
+ pointerLabelColor: "#ffffff",
545
+ linkColor: "#2563eb",
546
+ linkWidth: 2,
547
+ linkMarkerTextColor: "#ffffff"
545
548
  };
546
549
  var resolveTheme = (theme) => __spreadValues(__spreadValues({}, DEFAULT_THEME), theme);
547
550
 
@@ -727,65 +730,8 @@ var canvas_time_bar_default = CanvasTimeBar;
727
730
  // src/components/canvas-board.tsx
728
731
  var import_react6 = __toESM(require("react"));
729
732
 
730
- // src/canvas/frame-scheduler.ts
731
- var FrameScheduler = class {
732
- constructor(onFrame) {
733
- this.onFrame = onFrame;
734
- this.rafId = null;
735
- this.dirty = { static: false, dynamic: false };
736
- }
737
- invalidate(layer) {
738
- if (layer === "all" || layer === "static") this.dirty.static = true;
739
- if (layer === "all" || layer === "dynamic") this.dirty.dynamic = true;
740
- this.schedule();
741
- }
742
- /** Runs a pending frame immediately (tests, synchronous reads). */
743
- flush() {
744
- if (this.rafId !== null) {
745
- cancelAnimationFrame(this.rafId);
746
- this.rafId = null;
747
- this.run();
748
- }
749
- }
750
- dispose() {
751
- if (this.rafId !== null) {
752
- cancelAnimationFrame(this.rafId);
753
- this.rafId = null;
754
- }
755
- this.dirty = { static: false, dynamic: false };
756
- }
757
- schedule() {
758
- if (this.rafId !== null) return;
759
- this.rafId = requestAnimationFrame(() => {
760
- this.rafId = null;
761
- this.run();
762
- });
763
- }
764
- run() {
765
- const dirty = this.dirty;
766
- this.dirty = { static: false, dynamic: false };
767
- if (dirty.static || dirty.dynamic) this.onFrame(dirty);
768
- }
769
- };
770
-
771
- // src/core/culling.ts
772
- var cullToWindow = (events, windowStart, windowEnd) => {
773
- let lo = 0;
774
- let hi = events.length;
775
- while (lo < hi) {
776
- const mid = lo + hi >> 1;
777
- if (events[mid].startTime <= windowEnd) {
778
- lo = mid + 1;
779
- } else {
780
- hi = mid;
781
- }
782
- }
783
- const visible = [];
784
- for (let i = 0; i < lo; i++) {
785
- if (events[i].endTime >= windowStart) visible.push(events[i]);
786
- }
787
- return visible;
788
- };
733
+ // src/core/types.ts
734
+ var rectContains = (rect, x, y) => x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height;
789
735
 
790
736
  // src/core/coords.ts
791
737
  var timeToX = (time, windowStart, tick) => (time - windowStart) / tick;
@@ -835,6 +781,92 @@ var assignLanes = (rowEvents) => {
835
781
  return { laneOf, highestLane: Math.max(0, laneEnd.length - 1) };
836
782
  };
837
783
 
784
+ // src/core/links.ts
785
+ var MARKER_HEIGHT = 20;
786
+ var MARKER_PAD = 8;
787
+ var MARKER_INSET = 4;
788
+ var MARKER_GAP = 4;
789
+ var LABEL_MAX = 28;
790
+ var eventRect = (scene, offsetOf, eventId, view) => {
791
+ var _a;
792
+ const event = scene.getEvent(eventId);
793
+ if (!event) return null;
794
+ const top = offsetOf.get(event.rowId);
795
+ if (top === void 0) return null;
796
+ const g = scene.getGeometry();
797
+ const lane = (_a = scene.getLanes(event.rowId).laneOf.get(eventId)) != null ? _a : 0;
798
+ return {
799
+ x: timeToX(event.startTime, view.windowStart, view.tick),
800
+ y: top - view.scrollTop + scene.getLaneInset(event.rowId) + laneTop(lane, g),
801
+ width: timeToWidth(event.startTime, event.endTime, view.tick),
802
+ height: g.barHeight
803
+ };
804
+ };
805
+ var linkRoute = (from, to, stub = 8) => {
806
+ const ax = from.x + from.width, ay = from.y + from.height / 2;
807
+ const bx = to.x, by = to.y + to.height / 2;
808
+ if (bx - ax >= 2 * stub) return [[ax, ay], [ax + stub, ay], [ax + stub, by], [bx, by]];
809
+ const midY = Math.abs(ay - by) < 1 ? from.y + from.height + stub / 2 : (ay + by) / 2;
810
+ return [[ax, ay], [ax + stub, ay], [ax + stub, midY], [bx - stub, midY], [bx - stub, by], [bx, by]];
811
+ };
812
+ var truncate = (text) => text.length > LABEL_MAX ? `${text.slice(0, LABEL_MAX - 1)}\u2026` : text;
813
+ var linkMarkers = (scene, links, rectOf, view, measure) => {
814
+ const shown = (r) => r.y + r.height > 0 && r.y < view.height;
815
+ const wanted = /* @__PURE__ */ new Map();
816
+ for (const link of links) {
817
+ const a = rectOf(link.fromEventId), b = rectOf(link.toEventId);
818
+ if (!a || !b) continue;
819
+ for (const [hidden, id, other] of [[b, link.toEventId, a], [a, link.fromEventId, b]]) {
820
+ if (shown(hidden) || !shown(other) || wanted.has(id)) continue;
821
+ wanted.set(id, { up: hidden.y + hidden.height <= 0, x: hidden.x });
822
+ }
823
+ }
824
+ const out = [];
825
+ for (const up of [true, false]) {
826
+ const y = up ? MARKER_INSET : view.height - MARKER_HEIGHT - MARKER_INSET;
827
+ const edge = [...wanted].filter(([, w]) => w.up === up).sort((p, q) => p[1].x - q[1].x).map(([eventId, w]) => {
828
+ var _a, _b, _c;
829
+ 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}`);
830
+ return { eventId, up, label, rect: { x: w.x, y, width: measure(label) + 2 * MARKER_PAD, height: MARKER_HEIGHT } };
831
+ });
832
+ let right = MARKER_INSET - MARKER_GAP;
833
+ for (const m of edge) {
834
+ m.rect.x = Math.max(m.rect.x, right + MARKER_GAP);
835
+ right = m.rect.x + m.rect.width;
836
+ }
837
+ let left = view.width - MARKER_INSET + MARKER_GAP;
838
+ for (const m of [...edge].reverse()) {
839
+ m.rect.x = Math.max(MARKER_INSET, Math.min(m.rect.x, left - MARKER_GAP - m.rect.width));
840
+ left = m.rect.x;
841
+ }
842
+ out.push(...edge);
843
+ }
844
+ return out;
845
+ };
846
+ var markerAt = (markers, x, y) => {
847
+ var _a;
848
+ return (_a = markers.find((m) => rectContains(m.rect, x, y))) != null ? _a : null;
849
+ };
850
+
851
+ // src/core/culling.ts
852
+ var cullToWindow = (events, windowStart, windowEnd) => {
853
+ let lo = 0;
854
+ let hi = events.length;
855
+ while (lo < hi) {
856
+ const mid = lo + hi >> 1;
857
+ if (events[mid].startTime <= windowEnd) {
858
+ lo = mid + 1;
859
+ } else {
860
+ hi = mid;
861
+ }
862
+ }
863
+ const visible = [];
864
+ for (let i = 0; i < lo; i++) {
865
+ if (events[i].endTime >= windowStart) visible.push(events[i]);
866
+ }
867
+ return visible;
868
+ };
869
+
838
870
  // src/core/interactions.ts
839
871
  var computeDropTimes = ({
840
872
  pointerX,
@@ -1092,7 +1124,7 @@ var paintOverlapStripes = (ctx, event, rect, rowStatics, windowStart, tick, colo
1092
1124
  ctx.restore();
1093
1125
  }
1094
1126
  };
1095
- var drawEvents = (ctx, view, scene, font, animator) => {
1127
+ var drawEvents = (ctx, view, scene, font, animator, beforeSelected) => {
1096
1128
  var _a, _b, _c, _d;
1097
1129
  if (view.tick === null) return;
1098
1130
  const [windowStart, windowEnd] = view.windowTime;
@@ -1222,9 +1254,104 @@ var drawEvents = (ctx, view, scene, font, animator) => {
1222
1254
  }
1223
1255
  }
1224
1256
  }
1257
+ beforeSelected == null ? void 0 : beforeSelected();
1225
1258
  for (const paint of deferred) paint();
1226
1259
  };
1227
1260
 
1261
+ // src/canvas/draw-links.ts
1262
+ var ARROW = 6;
1263
+ var drawLinks = (ctx, view, scene, font) => {
1264
+ var _a, _b, _c, _d, _e, _f, _g;
1265
+ if (view.tick === null || !view.links.length) return [];
1266
+ const { offsetOf } = scene.getRowOffsets();
1267
+ const at = { windowStart: view.windowTime[0], tick: view.tick, scrollTop: view.scrollTop };
1268
+ const rects = /* @__PURE__ */ new Map();
1269
+ const rectOf = (id) => {
1270
+ var _a2;
1271
+ if (!rects.has(id)) rects.set(id, eventRect(scene, offsetOf, id, at));
1272
+ return (_a2 = rects.get(id)) != null ? _a2 : null;
1273
+ };
1274
+ const { theme } = view;
1275
+ ctx.save();
1276
+ ctx.lineJoin = "round";
1277
+ for (const link of view.links) {
1278
+ const a = rectOf(link.fromEventId), b = rectOf(link.toEventId);
1279
+ if (!a || !b) continue;
1280
+ const color = (_b = (_a = link.style) == null ? void 0 : _a.stroke) != null ? _b : theme.linkColor;
1281
+ const points = linkRoute(a, b);
1282
+ ctx.strokeStyle = color;
1283
+ ctx.lineWidth = (_d = (_c = link.style) == null ? void 0 : _c.strokeWidth) != null ? _d : theme.linkWidth;
1284
+ ctx.setLineDash((_f = (_e = link.style) == null ? void 0 : _e.dash) != null ? _f : []);
1285
+ ctx.beginPath();
1286
+ points.forEach(([x, y], i) => i ? ctx.lineTo(x, y) : ctx.moveTo(x, y));
1287
+ ctx.stroke();
1288
+ const [bx, by] = points[points.length - 1];
1289
+ ctx.setLineDash([]);
1290
+ ctx.fillStyle = color;
1291
+ ctx.beginPath();
1292
+ ctx.moveTo(bx, by);
1293
+ ctx.lineTo(bx - ARROW, by - ARROW / 1.4);
1294
+ ctx.lineTo(bx - ARROW, by + ARROW / 1.4);
1295
+ ctx.closePath();
1296
+ ctx.fill();
1297
+ }
1298
+ ctx.font = (_g = theme.font) != null ? _g : font;
1299
+ ctx.textBaseline = "middle";
1300
+ ctx.textAlign = "left";
1301
+ const markers = linkMarkers(scene, view.links, rectOf, view, (text) => ctx.measureText(text).width);
1302
+ for (const m of markers) {
1303
+ const { x, y, width, height } = m.rect;
1304
+ ctx.fillStyle = theme.linkColor;
1305
+ roundedRect(ctx, x, y, width, height, height / 2);
1306
+ ctx.fill();
1307
+ ctx.fillStyle = theme.linkMarkerTextColor;
1308
+ ctx.fillText(m.label, x + 8, y + height / 2);
1309
+ }
1310
+ ctx.restore();
1311
+ return markers;
1312
+ };
1313
+
1314
+ // src/canvas/frame-scheduler.ts
1315
+ var FrameScheduler = class {
1316
+ constructor(onFrame) {
1317
+ this.onFrame = onFrame;
1318
+ this.rafId = null;
1319
+ this.dirty = { static: false, dynamic: false };
1320
+ }
1321
+ invalidate(layer) {
1322
+ if (layer === "all" || layer === "static") this.dirty.static = true;
1323
+ if (layer === "all" || layer === "dynamic") this.dirty.dynamic = true;
1324
+ this.schedule();
1325
+ }
1326
+ /** Runs a pending frame immediately (tests, synchronous reads). */
1327
+ flush() {
1328
+ if (this.rafId !== null) {
1329
+ cancelAnimationFrame(this.rafId);
1330
+ this.rafId = null;
1331
+ this.run();
1332
+ }
1333
+ }
1334
+ dispose() {
1335
+ if (this.rafId !== null) {
1336
+ cancelAnimationFrame(this.rafId);
1337
+ this.rafId = null;
1338
+ }
1339
+ this.dirty = { static: false, dynamic: false };
1340
+ }
1341
+ schedule() {
1342
+ if (this.rafId !== null) return;
1343
+ this.rafId = requestAnimationFrame(() => {
1344
+ this.rafId = null;
1345
+ this.run();
1346
+ });
1347
+ }
1348
+ run() {
1349
+ const dirty = this.dirty;
1350
+ this.dirty = { static: false, dynamic: false };
1351
+ if (dirty.static || dirty.dynamic) this.onFrame(dirty);
1352
+ }
1353
+ };
1354
+
1228
1355
  // src/canvas/renderer.ts
1229
1356
  var TimelineRenderer = class {
1230
1357
  constructor(staticCanvas, dynamicCanvas, scene) {
@@ -1245,8 +1372,10 @@ var TimelineRenderer = class {
1245
1372
  hoveredEventId: null,
1246
1373
  draggedEventId: null,
1247
1374
  resizePreview: null,
1248
- selectedEventIds: /* @__PURE__ */ new Set()
1375
+ selectedEventIds: /* @__PURE__ */ new Set(),
1376
+ links: []
1249
1377
  };
1378
+ this.linkMarkers = [];
1250
1379
  this.animator = new LayoutAnimator();
1251
1380
  this.dpr = typeof window !== "undefined" ? window.devicePixelRatio : 1;
1252
1381
  this.font = "16px sans-serif";
@@ -1262,6 +1391,10 @@ var TimelineRenderer = class {
1262
1391
  getView() {
1263
1392
  return this.view;
1264
1393
  }
1394
+ /** the link edge markers as last drawn, in viewport px */
1395
+ getLinkMarkers() {
1396
+ return this.linkMarkers;
1397
+ }
1265
1398
  setView(partial) {
1266
1399
  const sizeChanged = partial.width !== void 0 && partial.width !== this.view.width || partial.height !== void 0 && partial.height !== this.view.height;
1267
1400
  this.view = __spreadValues(__spreadValues({}, this.view), partial);
@@ -1330,7 +1463,9 @@ var TimelineRenderer = class {
1330
1463
  const ctx = this.dynamicCanvas.getContext("2d");
1331
1464
  if (ctx) {
1332
1465
  ctx.clearRect(0, 0, this.view.width, this.view.height);
1333
- drawEvents(ctx, this.view, this.scene, this.font, this.animator);
1466
+ drawEvents(ctx, this.view, this.scene, this.font, this.animator, () => {
1467
+ this.linkMarkers = drawLinks(ctx, this.view, this.scene, this.font);
1468
+ });
1334
1469
  }
1335
1470
  }
1336
1471
  if (this.animator.isAnimating()) {
@@ -1342,9 +1477,6 @@ var TimelineRenderer = class {
1342
1477
  // src/hooks/use-canvas-interactions.ts
1343
1478
  var import_react5 = require("react");
1344
1479
 
1345
- // src/core/types.ts
1346
- var rectContains = (rect, x, y) => x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height;
1347
-
1348
1480
  // src/core/hit-test.ts
1349
1481
  var hitTest = (scene, view, x, y) => {
1350
1482
  const { windowStart, windowEnd, tick, scrollTop } = view;
@@ -1429,6 +1561,7 @@ var useCanvasInteractions = ({
1429
1561
  onResize,
1430
1562
  onEventClick,
1431
1563
  onEventHover,
1564
+ onLinkReveal,
1432
1565
  eventsResize,
1433
1566
  panZoom,
1434
1567
  selectable,
@@ -1558,6 +1691,32 @@ var useCanvasInteractions = ({
1558
1691
  },
1559
1692
  [dynamicCanvasRef, eventsResize]
1560
1693
  );
1694
+ const markerUnder = (0, import_react5.useCallback)(
1695
+ (clientX, clientY) => {
1696
+ const canvas = dynamicCanvasRef.current;
1697
+ const renderer = rendererRef.current;
1698
+ if (!canvas || !renderer) return null;
1699
+ const rect = canvas.getBoundingClientRect();
1700
+ return markerAt(renderer.getLinkMarkers(), clientX - rect.left, clientY - rect.top);
1701
+ },
1702
+ [dynamicCanvasRef, rendererRef]
1703
+ );
1704
+ const reveal = (0, import_react5.useCallback)(
1705
+ (eventId) => {
1706
+ const event = scene.getEvent(eventId);
1707
+ if (!event) return;
1708
+ const body = bodyRef.current;
1709
+ const top = scene.getRowOffsets().offsetOf.get(event.rowId);
1710
+ if (top !== void 0 && body) body.scrollTop = Math.max(0, top - body.clientHeight / 3);
1711
+ setWindowTime((prev) => {
1712
+ if (event.startTime >= prev[0] && event.startTime <= prev[1]) return prev;
1713
+ const span = prev[1] - prev[0];
1714
+ return [event.startTime - span / 5, event.startTime - span / 5 + span];
1715
+ });
1716
+ onLinkReveal == null ? void 0 : onLinkReveal(eventId);
1717
+ },
1718
+ [scene, bodyRef, setWindowTime, onLinkReveal]
1719
+ );
1561
1720
  const schedulePan = (0, import_react5.useCallback)(() => {
1562
1721
  if (panFrameRef.current !== null) return;
1563
1722
  panFrameRef.current = requestAnimationFrame(() => {
@@ -1647,6 +1806,17 @@ var useCanvasInteractions = ({
1647
1806
  return;
1648
1807
  }
1649
1808
  if (pointerEvent.button !== 0) return;
1809
+ const marker = markerUnder(pointerEvent.clientX, pointerEvent.clientY);
1810
+ if (marker) {
1811
+ stateRef.current = {
1812
+ mode: "marker",
1813
+ eventId: marker.eventId,
1814
+ startClientX: pointerEvent.clientX,
1815
+ startClientY: pointerEvent.clientY
1816
+ };
1817
+ canvas.setPointerCapture(pointerEvent.pointerId);
1818
+ return;
1819
+ }
1650
1820
  const hit = currentHit(pointerEvent.clientX, pointerEvent.clientY);
1651
1821
  if ((hit == null ? void 0 : hit.kind) === "event" && !((_b = hit.event.props) == null ? void 0 : _b.isLocked)) {
1652
1822
  const direction = resizeDirectionAt(hit, pointerEvent.clientX);
@@ -1715,7 +1885,8 @@ var useCanvasInteractions = ({
1715
1885
  ghostRef,
1716
1886
  rendererRef,
1717
1887
  styleGhost,
1718
- selectable
1888
+ selectable,
1889
+ markerUnder
1719
1890
  ]
1720
1891
  );
1721
1892
  const handleHover = (0, import_react5.useCallback)(
@@ -1737,6 +1908,7 @@ var useCanvasInteractions = ({
1737
1908
  } else {
1738
1909
  canvas.style.cursor = "grab";
1739
1910
  }
1911
+ if (markerUnder(pointerEvent.clientX, pointerEvent.clientY)) canvas.style.cursor = "pointer";
1740
1912
  const hoveredId = (hit == null ? void 0 : hit.kind) === "event" ? hit.event.id : null;
1741
1913
  const prevId = (prevHovered == null ? void 0 : prevHovered.kind) === "event" ? prevHovered.event.id : null;
1742
1914
  if (hoveredId !== prevId) {
@@ -1770,7 +1942,8 @@ var useCanvasInteractions = ({
1770
1942
  dynamicCanvasRef,
1771
1943
  eventPromptRef,
1772
1944
  rendererRef,
1773
- onEventHover
1945
+ onEventHover,
1946
+ markerUnder
1774
1947
  ]
1775
1948
  );
1776
1949
  const resolveRowAtClientY = (0, import_react5.useCallback)(
@@ -1854,6 +2027,7 @@ var useCanvasInteractions = ({
1854
2027
  }
1855
2028
  return;
1856
2029
  }
2030
+ if (state.mode === "marker") return;
1857
2031
  state.deltaPx = pointerEvent.clientX - state.startClientX;
1858
2032
  (_d = rendererRef.current) == null ? void 0 : _d.setView({
1859
2033
  resizePreview: {
@@ -1992,6 +2166,9 @@ var useCanvasInteractions = ({
1992
2166
  }
1993
2167
  } else if (state.mode === "resize") {
1994
2168
  commitResize(state);
2169
+ } else if (state.mode === "marker") {
2170
+ const moved = Math.abs(pointerEvent.clientX - state.startClientX) + Math.abs(pointerEvent.clientY - state.startClientY) > DRAG_THRESHOLD_PX;
2171
+ if (!moved) reveal(state.eventId);
1995
2172
  }
1996
2173
  },
1997
2174
  [
@@ -2004,7 +2181,8 @@ var useCanvasInteractions = ({
2004
2181
  selectable,
2005
2182
  applySelection,
2006
2183
  getSelection,
2007
- scene
2184
+ scene,
2185
+ reveal
2008
2186
  ]
2009
2187
  );
2010
2188
  const handlePointerCancel = (0, import_react5.useCallback)(
@@ -2131,6 +2309,8 @@ var CanvasBoard = (0, import_react6.forwardRef)(
2131
2309
  onEventHover,
2132
2310
  eventsResize,
2133
2311
  stripeOverlap,
2312
+ links,
2313
+ onLinkReveal,
2134
2314
  timeBar,
2135
2315
  panZoom,
2136
2316
  animations,
@@ -2174,6 +2354,7 @@ var CanvasBoard = (0, import_react6.forwardRef)(
2174
2354
  drawEvent,
2175
2355
  eventsResize,
2176
2356
  stripeOverlap,
2357
+ links,
2177
2358
  timeBar
2178
2359
  });
2179
2360
  }, [
@@ -2184,6 +2365,7 @@ var CanvasBoard = (0, import_react6.forwardRef)(
2184
2365
  drawEvent,
2185
2366
  eventsResize,
2186
2367
  stripeOverlap,
2368
+ links,
2187
2369
  timeBar,
2188
2370
  animations,
2189
2371
  rendererRef
@@ -2312,6 +2494,7 @@ var CanvasBoard = (0, import_react6.forwardRef)(
2312
2494
  onResize,
2313
2495
  onEventClick,
2314
2496
  onEventHover,
2497
+ onLinkReveal,
2315
2498
  eventsResize,
2316
2499
  panZoom,
2317
2500
  selectable,
@@ -2979,6 +3162,7 @@ var SceneStore = class {
2979
3162
 
2980
3163
  // src/timeline.tsx
2981
3164
  var defaultPointerFormat = (time) => new Date(time * 1e3).toLocaleString();
3165
+ var NO_LINKS = [];
2982
3166
  var Timeline = (0, import_react8.forwardRef)(
2983
3167
  ({
2984
3168
  rows,
@@ -3010,7 +3194,9 @@ var Timeline = (0, import_react8.forwardRef)(
3010
3194
  showPointerLine = false,
3011
3195
  formatPointerTime,
3012
3196
  onPointerTime,
3013
- animations = true
3197
+ animations = true,
3198
+ links = NO_LINKS,
3199
+ onLinkReveal
3014
3200
  }, handleRef) => {
3015
3201
  const [windowTime, setWindowTime] = (0, import_react8.useState)([
3016
3202
  new Date(
@@ -3331,6 +3517,8 @@ var Timeline = (0, import_react8.forwardRef)(
3331
3517
  onEventHover: handleEventHover,
3332
3518
  eventsResize,
3333
3519
  stripeOverlap,
3520
+ links,
3521
+ onLinkReveal,
3334
3522
  timeBar,
3335
3523
  panZoom,
3336
3524
  animations: resolvedAnimations,