awesome-timeline-react 0.11.0 → 0.12.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
@@ -307,6 +307,15 @@ var hourLabel = (hour) => `${pad2(hour)}:00`;
307
307
  var dayLabel = (date) => `${get_week_day_name_default(date.getDay())} ${date.getDate()} ${get_month_name_default(
308
308
  date.getMonth()
309
309
  )}`;
310
+ var isoWeek = (date) => {
311
+ const thursday = new Date(
312
+ date.getFullYear(),
313
+ date.getMonth(),
314
+ date.getDate() + 3 - (date.getDay() + 6) % 7
315
+ );
316
+ const jan1 = new Date(thursday.getFullYear(), 0, 1);
317
+ return 1 + Math.floor(Math.round((thursday.getTime() - jan1.getTime()) / 864e5) / 7);
318
+ };
310
319
  var defaultFormat = (unit) => {
311
320
  if (typeof unit === "object") {
312
321
  return (d) => `${pad2(d.getHours())}:${pad2(d.getMinutes())}`;
@@ -316,6 +325,8 @@ var defaultFormat = (unit) => {
316
325
  return (d) => hourLabel(d.getHours());
317
326
  case "month":
318
327
  return (d) => `${get_month_name_default(d.getMonth())} ${d.getFullYear()}`;
328
+ case "year":
329
+ return (d) => `${d.getFullYear()}`;
319
330
  case "day":
320
331
  case "week":
321
332
  default:
@@ -342,6 +353,8 @@ var floorToUnit = (date, unit, anchorSec) => {
342
353
  }
343
354
  case "month":
344
355
  return new Date(y, mo, 1);
356
+ case "year":
357
+ return new Date(y, 0, 1);
345
358
  }
346
359
  };
347
360
  var nextBoundary = (blockStart, unit) => {
@@ -360,8 +373,36 @@ var nextBoundary = (blockStart, unit) => {
360
373
  return new Date(y, mo, d + 7);
361
374
  case "month":
362
375
  return new Date(y, mo + 1, 1);
376
+ case "year":
377
+ return new Date(y + 1, 0, 1);
363
378
  }
364
379
  };
380
+ var blockSpans = (unit, windowStart, windowEnd, limit = 1e5) => {
381
+ const spans = [];
382
+ if (typeof unit === "object" && "boundaries" in unit) {
383
+ const edges = unit.boundaries(windowStart, windowEnd);
384
+ for (let i = 0; i + 1 < edges.length; i++) {
385
+ const [start, end] = [edges[i], edges[i + 1]];
386
+ if (end <= windowStart || start >= windowEnd || end <= start) continue;
387
+ if (spans.length >= limit) return null;
388
+ spans.push([start, end]);
389
+ }
390
+ return spans;
391
+ }
392
+ const day = new Date(windowStart * 1e3);
393
+ const anchorSec = new Date(day.getFullYear(), day.getMonth(), day.getDate()).getTime() / 1e3;
394
+ let cursorSec = windowStart;
395
+ while (cursorSec < windowEnd) {
396
+ const blockStart = floorToUnit(new Date(cursorSec * 1e3), unit, anchorSec);
397
+ const startSec = blockStart.getTime() / 1e3;
398
+ const endSec = nextBoundary(blockStart, unit).getTime() / 1e3;
399
+ if (endSec <= startSec) break;
400
+ if (spans.length >= limit) return null;
401
+ spans.push([startSec, endSec]);
402
+ cursorSec = endSec;
403
+ }
404
+ return spans;
405
+ };
365
406
  var generateUnitBlocks = ({
366
407
  unit,
367
408
  format,
@@ -369,33 +410,55 @@ var generateUnitBlocks = ({
369
410
  windowEnd,
370
411
  tick
371
412
  }) => {
372
- const blocks = [];
413
+ var _a;
414
+ if (tick === null || tick <= 0 || windowEnd <= windowStart) return [];
373
415
  const resolvedUnit = unit != null ? unit : "hour";
374
- if (tick === null || tick <= 0 || windowEnd <= windowStart) return blocks;
375
416
  const label = format != null ? format : defaultFormat(resolvedUnit);
376
- const anchorSec = new Date(
377
- new Date(windowStart * 1e3).getFullYear(),
378
- new Date(windowStart * 1e3).getMonth(),
379
- new Date(windowStart * 1e3).getDate()
380
- ).getTime() / 1e3;
381
- let cursorSec = windowStart;
382
- let guard = 0;
383
- while (cursorSec < windowEnd && guard++ < 1e5) {
384
- const blockStart = floorToUnit(new Date(cursorSec * 1e3), resolvedUnit, anchorSec);
385
- const blockEnd = nextBoundary(blockStart, resolvedUnit);
386
- const startSec = blockStart.getTime() / 1e3;
387
- const endSec = blockEnd.getTime() / 1e3;
388
- if (endSec <= startSec) break;
389
- const visibleStart = Math.max(startSec, windowStart);
390
- const visibleEnd = Math.min(endSec, windowEnd);
391
- blocks.push({
392
- label: label(blockStart, blockEnd),
393
- x: (visibleStart - windowStart) / tick,
394
- width: (visibleEnd - visibleStart) / tick
395
- });
396
- cursorSec = endSec;
417
+ return ((_a = blockSpans(resolvedUnit, windowStart, windowEnd)) != null ? _a : []).map(
418
+ ([startSec, endSec]) => {
419
+ const visibleStart = Math.max(startSec, windowStart);
420
+ const visibleEnd = Math.min(endSec, windowEnd);
421
+ return {
422
+ label: label(new Date(startSec * 1e3), new Date(endSec * 1e3)),
423
+ x: (visibleStart - windowStart) / tick,
424
+ width: (visibleEnd - visibleStart) / tick
425
+ };
426
+ }
427
+ );
428
+ };
429
+ var shortDay = (d) => `${get_week_day_name_default(d.getDay())} ${d.getDate()}`;
430
+ var DEFAULT_TIME_BAR_LEVELS = [
431
+ { topRow: { unit: "day" }, bottomRow: { unit: "hour" } },
432
+ { topRow: { unit: "day" }, bottomRow: { unit: { stepSeconds: 6 * 3600 } } },
433
+ {
434
+ topRow: {
435
+ unit: "week",
436
+ format: (d) => `W${isoWeek(d)} \xB7 ${d.getDate()} ${get_month_name_default(d.getMonth())}`
437
+ },
438
+ bottomRow: { unit: "day", format: shortDay }
439
+ },
440
+ { topRow: { unit: "month" }, bottomRow: { unit: "week", format: (d) => `W${isoWeek(d)}` } },
441
+ { topRow: { unit: "year" }, bottomRow: { unit: "month", format: (d) => get_month_name_default(d.getMonth()) } }
442
+ ];
443
+ var DEFAULT_MIN_BLOCK_WIDTH = 40;
444
+ var resolveTimeBar = (timeBar, windowStart, windowEnd, tick) => {
445
+ var _a, _b, _c;
446
+ if (!(timeBar == null ? void 0 : timeBar.auto)) {
447
+ return {
448
+ topRow: (_a = timeBar == null ? void 0 : timeBar.topRow) != null ? _a : { unit: "day" },
449
+ bottomRow: (_b = timeBar == null ? void 0 : timeBar.bottomRow) != null ? _b : { unit: "hour" }
450
+ };
397
451
  }
398
- return blocks;
452
+ const { levels = DEFAULT_TIME_BAR_LEVELS, minBlockWidth = DEFAULT_MIN_BLOCK_WIDTH } = timeBar.auto === true ? {} : timeBar.auto;
453
+ const levelsOrDefault = levels.length > 0 ? levels : DEFAULT_TIME_BAR_LEVELS;
454
+ if (tick === null || tick <= 0 || windowEnd <= windowStart) return levelsOrDefault[0];
455
+ const minSecs = minBlockWidth * tick;
456
+ const limit = Math.ceil((windowEnd - windowStart) / minSecs) + 2;
457
+ return (_c = levelsOrDefault.find((level) => {
458
+ var _a2;
459
+ const spans = blockSpans((_a2 = level.bottomRow.unit) != null ? _a2 : "hour", windowStart, windowEnd, limit);
460
+ return spans !== null && spans.every(([start, end]) => end - start >= minSecs);
461
+ })) != null ? _c : levelsOrDefault[levelsOrDefault.length - 1];
399
462
  };
400
463
 
401
464
  // src/canvas/dpr.ts
@@ -485,6 +548,24 @@ var drawGridLines = (ctx, {
485
548
  }
486
549
  ctx.stroke();
487
550
  };
551
+ var drawBlockGridLines = (ctx, {
552
+ width,
553
+ height,
554
+ blocks,
555
+ color = DEFAULT_THEME.gridColor
556
+ }) => {
557
+ ctx.strokeStyle = color;
558
+ ctx.lineWidth = 1;
559
+ ctx.beginPath();
560
+ for (let i = 0; i < blocks.length - 1; i++) {
561
+ const x = blocks[i].x + blocks[i].width;
562
+ if (x < 1 || width - x < 1) continue;
563
+ const crisp = Math.round(x) - 0.5;
564
+ ctx.moveTo(crisp, 0);
565
+ ctx.lineTo(crisp, height);
566
+ }
567
+ ctx.stroke();
568
+ };
488
569
  var drawTimeBar = (ctx, {
489
570
  width,
490
571
  height,
@@ -547,11 +628,9 @@ var CanvasTimeBar = ({
547
628
  theme,
548
629
  timeBar
549
630
  }) => {
550
- var _a, _b, _c;
631
+ var _a;
551
632
  const canvasRef = useRef3(null);
552
633
  const header = theme != null ? theme : DEFAULT_THEME;
553
- const topRow = (_a = timeBar == null ? void 0 : timeBar.topRow) != null ? _a : { unit: "day" };
554
- const bottomRow = (_b = timeBar == null ? void 0 : timeBar.bottomRow) != null ? _b : { unit: "hour" };
555
634
  useEffect3(() => {
556
635
  var _a2;
557
636
  const canvas = canvasRef.current;
@@ -564,6 +643,7 @@ var CanvasTimeBar = ({
564
643
  );
565
644
  if (!ctx) return;
566
645
  const [windowStart, windowEnd] = windowTime;
646
+ const { topRow, bottomRow } = resolveTimeBar(timeBar, windowStart, windowEnd, tick);
567
647
  drawTimeBar(ctx, {
568
648
  width: contentWidth,
569
649
  height: TIME_BAR_INNER_HEIGHT,
@@ -600,7 +680,7 @@ var CanvasTimeBar = ({
600
680
  width: scrollWidth,
601
681
  height: "100%",
602
682
  boxSizing: "border-box",
603
- borderLeft: `1px solid ${(_c = header.headerBorderColor) != null ? _c : "yellow"}`
683
+ borderLeft: `1px solid ${(_a = header.headerBorderColor) != null ? _a : "yellow"}`
604
684
  }
605
685
  }
606
686
  ) : null
@@ -1183,6 +1263,28 @@ var TimelineRenderer = class {
1183
1263
  this.font = getComputedStyle(this.dynamicCanvas).font || this.font;
1184
1264
  }
1185
1265
  }
1266
+ drawGrid(ctx, tick) {
1267
+ const { width, height, windowTime, timeBar, theme } = this.view;
1268
+ if (timeBar == null ? void 0 : timeBar.auto) {
1269
+ const [windowStart, windowEnd] = windowTime;
1270
+ const { bottomRow } = resolveTimeBar(timeBar, windowStart, windowEnd, tick);
1271
+ drawBlockGridLines(ctx, {
1272
+ width,
1273
+ height,
1274
+ blocks: generateUnitBlocks(__spreadProps(__spreadValues({}, bottomRow), { windowStart, windowEnd, tick })),
1275
+ color: theme.gridColor
1276
+ });
1277
+ return;
1278
+ }
1279
+ drawGridLines(ctx, {
1280
+ width,
1281
+ height,
1282
+ cellWidth: this.view.cellWidth,
1283
+ windowStart: windowTime[0],
1284
+ tick,
1285
+ color: theme.gridColor
1286
+ });
1287
+ }
1186
1288
  drawFrame(dirty) {
1187
1289
  if (this.view.tick === null) return;
1188
1290
  this.animator.beginFrame(performance.now());
@@ -1191,14 +1293,7 @@ var TimelineRenderer = class {
1191
1293
  if (ctx) {
1192
1294
  ctx.clearRect(0, 0, this.view.width, this.view.height);
1193
1295
  drawGroupShading(ctx, this.view, this.scene);
1194
- drawGridLines(ctx, {
1195
- width: this.view.width,
1196
- height: this.view.height,
1197
- cellWidth: this.view.cellWidth,
1198
- windowStart: this.view.windowTime[0],
1199
- tick: this.view.tick,
1200
- color: this.view.theme.gridColor
1201
- });
1296
+ this.drawGrid(ctx, this.view.tick);
1202
1297
  drawRowDividers(ctx, this.view, this.scene);
1203
1298
  }
1204
1299
  }
@@ -2007,6 +2102,7 @@ var CanvasBoard = forwardRef2(
2007
2102
  onEventHover,
2008
2103
  eventsResize,
2009
2104
  stripeOverlap,
2105
+ timeBar,
2010
2106
  panZoom,
2011
2107
  animations,
2012
2108
  selectable,
@@ -2048,7 +2144,8 @@ var CanvasBoard = forwardRef2(
2048
2144
  theme,
2049
2145
  drawEvent,
2050
2146
  eventsResize,
2051
- stripeOverlap
2147
+ stripeOverlap,
2148
+ timeBar
2052
2149
  });
2053
2150
  }, [
2054
2151
  windowTime,
@@ -2058,6 +2155,7 @@ var CanvasBoard = forwardRef2(
2058
2155
  drawEvent,
2059
2156
  eventsResize,
2060
2157
  stripeOverlap,
2158
+ timeBar,
2061
2159
  animations,
2062
2160
  rendererRef
2063
2161
  ]);
@@ -2986,16 +3084,17 @@ var Timeline = forwardRef4(
2986
3084
  const pointerOpts = useRef7({ showPointerLine, showTimeBar, formatPointerTime, onPointerTime });
2987
3085
  pointerOpts.current = { showPointerLine, showTimeBar, formatPointerTime, onPointerTime };
2988
3086
  const drawPointer = useCallback3(() => {
2989
- var _a, _b;
3087
+ var _a, _b, _c;
2990
3088
  const line = pointerLineRef.current;
2991
3089
  const p = pointerRef.current;
2992
3090
  const t = tickRef.current;
2993
3091
  if (!line) return;
2994
3092
  if (!p || !t) return line.hide();
3093
+ if ("x" in p && ((_a = rendererRef.current) == null ? void 0 : _a.getView().hoveredEventId)) return line.hide();
2995
3094
  const start = windowTimeRef.current[0];
2996
3095
  const time = "x" in p ? start + p.x * t : p.time;
2997
3096
  const x = "x" in p ? p.x : (p.time - start) / t;
2998
- const width = (_b = (_a = contentRef.current) == null ? void 0 : _a.getBoundingClientRect().width) != null ? _b : 0;
3097
+ const width = (_c = (_b = contentRef.current) == null ? void 0 : _b.getBoundingClientRect().width) != null ? _c : 0;
2999
3098
  if (x < 0 || x > width) return line.hide();
3000
3099
  const { showTimeBar: labelled, formatPointerTime: format } = pointerOpts.current;
3001
3100
  line.show(x, labelled ? (format != null ? format : defaultPointerFormat)(time) : null);
@@ -3034,6 +3133,13 @@ var Timeline = forwardRef4(
3034
3133
  if (p && "x" in p && tick) (_b = (_a = pointerOpts.current).onPointerTime) == null ? void 0 : _b.call(_a, windowTime[0] + p.x * tick);
3035
3134
  drawPointer();
3036
3135
  }, [windowTime[0], windowTime[1], tick, drawPointer]);
3136
+ const onEventHoverRef = useRef7(onEventHover);
3137
+ onEventHoverRef.current = onEventHover;
3138
+ const handleEventHover = useCallback3((hover) => {
3139
+ var _a;
3140
+ drawPointer();
3141
+ (_a = onEventHoverRef.current) == null ? void 0 : _a.call(onEventHoverRef, hover);
3142
+ }, [drawPointer]);
3037
3143
  useImperativeHandle3(
3038
3144
  handleRef,
3039
3145
  () => ({
@@ -3193,9 +3299,10 @@ var Timeline = forwardRef4(
3193
3299
  onDrop,
3194
3300
  onResize,
3195
3301
  onEventClick,
3196
- onEventHover,
3302
+ onEventHover: handleEventHover,
3197
3303
  eventsResize,
3198
3304
  stripeOverlap,
3305
+ timeBar,
3199
3306
  panZoom,
3200
3307
  animations: resolvedAnimations,
3201
3308
  selectable,
@@ -3218,6 +3325,7 @@ var Timeline = forwardRef4(
3218
3325
  }
3219
3326
  );
3220
3327
  export {
3328
+ DEFAULT_TIME_BAR_LEVELS,
3221
3329
  Timeline
3222
3330
  };
3223
3331
  //# sourceMappingURL=index.mjs.map