awesome-timeline-react 0.11.0 → 0.13.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,
@@ -492,9 +573,20 @@ var drawTimeBar = (ctx, {
492
573
  bottomBlocks,
493
574
  font,
494
575
  borderColor = DEFAULT_THEME.timeBarBorder,
495
- textColor = DEFAULT_THEME.timeBarTextColor
576
+ textColor = DEFAULT_THEME.timeBarTextColor,
577
+ topBackground,
578
+ bottomBackground,
579
+ bottomTextColor = textColor
496
580
  }) => {
497
581
  const mid = height / 2;
582
+ if (topBackground) {
583
+ ctx.fillStyle = topBackground;
584
+ ctx.fillRect(0, 0, width, Math.round(mid));
585
+ }
586
+ if (bottomBackground) {
587
+ ctx.fillStyle = bottomBackground;
588
+ ctx.fillRect(0, Math.round(mid), width, height - Math.round(mid));
589
+ }
498
590
  ctx.strokeStyle = borderColor;
499
591
  ctx.lineWidth = 1;
500
592
  ctx.beginPath();
@@ -526,6 +618,7 @@ var drawTimeBar = (ctx, {
526
618
  block.width - inset
527
619
  );
528
620
  }
621
+ ctx.fillStyle = bottomTextColor;
529
622
  for (const block of bottomBlocks) {
530
623
  ctx.fillText(
531
624
  block.label,
@@ -547,13 +640,11 @@ var CanvasTimeBar = ({
547
640
  theme,
548
641
  timeBar
549
642
  }) => {
550
- var _a, _b, _c;
643
+ var _a;
551
644
  const canvasRef = useRef3(null);
552
645
  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
646
  useEffect3(() => {
556
- var _a2;
647
+ var _a2, _b;
557
648
  const canvas = canvasRef.current;
558
649
  if (!canvas || !contentWidth) return;
559
650
  const ctx = sizeCanvas(
@@ -564,12 +655,16 @@ var CanvasTimeBar = ({
564
655
  );
565
656
  if (!ctx) return;
566
657
  const [windowStart, windowEnd] = windowTime;
658
+ const { topRow, bottomRow } = resolveTimeBar(timeBar, windowStart, windowEnd, tick);
567
659
  drawTimeBar(ctx, {
568
660
  width: contentWidth,
569
661
  height: TIME_BAR_INNER_HEIGHT,
570
662
  font: (_a2 = theme == null ? void 0 : theme.font) != null ? _a2 : getComputedStyle(canvas).font,
571
663
  borderColor: theme == null ? void 0 : theme.timeBarBorder,
572
664
  textColor: theme == null ? void 0 : theme.timeBarTextColor,
665
+ topBackground: theme == null ? void 0 : theme.timeBarTopBackground,
666
+ bottomBackground: theme == null ? void 0 : theme.timeBarBottomBackground,
667
+ bottomTextColor: (_b = theme == null ? void 0 : theme.timeBarBottomTextColor) != null ? _b : theme == null ? void 0 : theme.timeBarTextColor,
573
668
  topBlocks: generateUnitBlocks(__spreadProps(__spreadValues({}, topRow), { windowStart, windowEnd, tick })),
574
669
  bottomBlocks: generateUnitBlocks(__spreadProps(__spreadValues({}, bottomRow), {
575
670
  windowStart,
@@ -600,7 +695,7 @@ var CanvasTimeBar = ({
600
695
  width: scrollWidth,
601
696
  height: "100%",
602
697
  boxSizing: "border-box",
603
- borderLeft: `1px solid ${(_c = header.headerBorderColor) != null ? _c : "yellow"}`
698
+ borderLeft: `1px solid ${(_a = header.headerBorderColor) != null ? _a : "yellow"}`
604
699
  }
605
700
  }
606
701
  ) : null
@@ -1183,6 +1278,28 @@ var TimelineRenderer = class {
1183
1278
  this.font = getComputedStyle(this.dynamicCanvas).font || this.font;
1184
1279
  }
1185
1280
  }
1281
+ drawGrid(ctx, tick) {
1282
+ const { width, height, windowTime, timeBar, theme } = this.view;
1283
+ if (timeBar == null ? void 0 : timeBar.auto) {
1284
+ const [windowStart, windowEnd] = windowTime;
1285
+ const { bottomRow } = resolveTimeBar(timeBar, windowStart, windowEnd, tick);
1286
+ drawBlockGridLines(ctx, {
1287
+ width,
1288
+ height,
1289
+ blocks: generateUnitBlocks(__spreadProps(__spreadValues({}, bottomRow), { windowStart, windowEnd, tick })),
1290
+ color: theme.gridColor
1291
+ });
1292
+ return;
1293
+ }
1294
+ drawGridLines(ctx, {
1295
+ width,
1296
+ height,
1297
+ cellWidth: this.view.cellWidth,
1298
+ windowStart: windowTime[0],
1299
+ tick,
1300
+ color: theme.gridColor
1301
+ });
1302
+ }
1186
1303
  drawFrame(dirty) {
1187
1304
  if (this.view.tick === null) return;
1188
1305
  this.animator.beginFrame(performance.now());
@@ -1191,14 +1308,7 @@ var TimelineRenderer = class {
1191
1308
  if (ctx) {
1192
1309
  ctx.clearRect(0, 0, this.view.width, this.view.height);
1193
1310
  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
- });
1311
+ this.drawGrid(ctx, this.view.tick);
1202
1312
  drawRowDividers(ctx, this.view, this.scene);
1203
1313
  }
1204
1314
  }
@@ -2007,6 +2117,7 @@ var CanvasBoard = forwardRef2(
2007
2117
  onEventHover,
2008
2118
  eventsResize,
2009
2119
  stripeOverlap,
2120
+ timeBar,
2010
2121
  panZoom,
2011
2122
  animations,
2012
2123
  selectable,
@@ -2048,7 +2159,8 @@ var CanvasBoard = forwardRef2(
2048
2159
  theme,
2049
2160
  drawEvent,
2050
2161
  eventsResize,
2051
- stripeOverlap
2162
+ stripeOverlap,
2163
+ timeBar
2052
2164
  });
2053
2165
  }, [
2054
2166
  windowTime,
@@ -2058,6 +2170,7 @@ var CanvasBoard = forwardRef2(
2058
2170
  drawEvent,
2059
2171
  eventsResize,
2060
2172
  stripeOverlap,
2173
+ timeBar,
2061
2174
  animations,
2062
2175
  rendererRef
2063
2176
  ]);
@@ -2986,16 +3099,17 @@ var Timeline = forwardRef4(
2986
3099
  const pointerOpts = useRef7({ showPointerLine, showTimeBar, formatPointerTime, onPointerTime });
2987
3100
  pointerOpts.current = { showPointerLine, showTimeBar, formatPointerTime, onPointerTime };
2988
3101
  const drawPointer = useCallback3(() => {
2989
- var _a, _b;
3102
+ var _a, _b, _c;
2990
3103
  const line = pointerLineRef.current;
2991
3104
  const p = pointerRef.current;
2992
3105
  const t = tickRef.current;
2993
3106
  if (!line) return;
2994
3107
  if (!p || !t) return line.hide();
3108
+ if ("x" in p && ((_a = rendererRef.current) == null ? void 0 : _a.getView().hoveredEventId)) return line.hide();
2995
3109
  const start = windowTimeRef.current[0];
2996
3110
  const time = "x" in p ? start + p.x * t : p.time;
2997
3111
  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;
3112
+ const width = (_c = (_b = contentRef.current) == null ? void 0 : _b.getBoundingClientRect().width) != null ? _c : 0;
2999
3113
  if (x < 0 || x > width) return line.hide();
3000
3114
  const { showTimeBar: labelled, formatPointerTime: format } = pointerOpts.current;
3001
3115
  line.show(x, labelled ? (format != null ? format : defaultPointerFormat)(time) : null);
@@ -3034,6 +3148,13 @@ var Timeline = forwardRef4(
3034
3148
  if (p && "x" in p && tick) (_b = (_a = pointerOpts.current).onPointerTime) == null ? void 0 : _b.call(_a, windowTime[0] + p.x * tick);
3035
3149
  drawPointer();
3036
3150
  }, [windowTime[0], windowTime[1], tick, drawPointer]);
3151
+ const onEventHoverRef = useRef7(onEventHover);
3152
+ onEventHoverRef.current = onEventHover;
3153
+ const handleEventHover = useCallback3((hover) => {
3154
+ var _a;
3155
+ drawPointer();
3156
+ (_a = onEventHoverRef.current) == null ? void 0 : _a.call(onEventHoverRef, hover);
3157
+ }, [drawPointer]);
3037
3158
  useImperativeHandle3(
3038
3159
  handleRef,
3039
3160
  () => ({
@@ -3193,9 +3314,10 @@ var Timeline = forwardRef4(
3193
3314
  onDrop,
3194
3315
  onResize,
3195
3316
  onEventClick,
3196
- onEventHover,
3317
+ onEventHover: handleEventHover,
3197
3318
  eventsResize,
3198
3319
  stripeOverlap,
3320
+ timeBar,
3199
3321
  panZoom,
3200
3322
  animations: resolvedAnimations,
3201
3323
  selectable,
@@ -3218,6 +3340,7 @@ var Timeline = forwardRef4(
3218
3340
  }
3219
3341
  );
3220
3342
  export {
3343
+ DEFAULT_TIME_BAR_LEVELS,
3221
3344
  Timeline
3222
3345
  };
3223
3346
  //# sourceMappingURL=index.mjs.map