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.js CHANGED
@@ -47,6 +47,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
47
47
  // src/index.ts
48
48
  var index_exports = {};
49
49
  __export(index_exports, {
50
+ DEFAULT_TIME_BAR_LEVELS: () => DEFAULT_TIME_BAR_LEVELS,
50
51
  Timeline: () => Timeline
51
52
  });
52
53
  module.exports = __toCommonJS(index_exports);
@@ -327,6 +328,15 @@ var hourLabel = (hour) => `${pad2(hour)}:00`;
327
328
  var dayLabel = (date) => `${get_week_day_name_default(date.getDay())} ${date.getDate()} ${get_month_name_default(
328
329
  date.getMonth()
329
330
  )}`;
331
+ var isoWeek = (date) => {
332
+ const thursday = new Date(
333
+ date.getFullYear(),
334
+ date.getMonth(),
335
+ date.getDate() + 3 - (date.getDay() + 6) % 7
336
+ );
337
+ const jan1 = new Date(thursday.getFullYear(), 0, 1);
338
+ return 1 + Math.floor(Math.round((thursday.getTime() - jan1.getTime()) / 864e5) / 7);
339
+ };
330
340
  var defaultFormat = (unit) => {
331
341
  if (typeof unit === "object") {
332
342
  return (d) => `${pad2(d.getHours())}:${pad2(d.getMinutes())}`;
@@ -336,6 +346,8 @@ var defaultFormat = (unit) => {
336
346
  return (d) => hourLabel(d.getHours());
337
347
  case "month":
338
348
  return (d) => `${get_month_name_default(d.getMonth())} ${d.getFullYear()}`;
349
+ case "year":
350
+ return (d) => `${d.getFullYear()}`;
339
351
  case "day":
340
352
  case "week":
341
353
  default:
@@ -362,6 +374,8 @@ var floorToUnit = (date, unit, anchorSec) => {
362
374
  }
363
375
  case "month":
364
376
  return new Date(y, mo, 1);
377
+ case "year":
378
+ return new Date(y, 0, 1);
365
379
  }
366
380
  };
367
381
  var nextBoundary = (blockStart, unit) => {
@@ -380,8 +394,36 @@ var nextBoundary = (blockStart, unit) => {
380
394
  return new Date(y, mo, d + 7);
381
395
  case "month":
382
396
  return new Date(y, mo + 1, 1);
397
+ case "year":
398
+ return new Date(y + 1, 0, 1);
383
399
  }
384
400
  };
401
+ var blockSpans = (unit, windowStart, windowEnd, limit = 1e5) => {
402
+ const spans = [];
403
+ if (typeof unit === "object" && "boundaries" in unit) {
404
+ const edges = unit.boundaries(windowStart, windowEnd);
405
+ for (let i = 0; i + 1 < edges.length; i++) {
406
+ const [start, end] = [edges[i], edges[i + 1]];
407
+ if (end <= windowStart || start >= windowEnd || end <= start) continue;
408
+ if (spans.length >= limit) return null;
409
+ spans.push([start, end]);
410
+ }
411
+ return spans;
412
+ }
413
+ const day = new Date(windowStart * 1e3);
414
+ const anchorSec = new Date(day.getFullYear(), day.getMonth(), day.getDate()).getTime() / 1e3;
415
+ let cursorSec = windowStart;
416
+ while (cursorSec < windowEnd) {
417
+ const blockStart = floorToUnit(new Date(cursorSec * 1e3), unit, anchorSec);
418
+ const startSec = blockStart.getTime() / 1e3;
419
+ const endSec = nextBoundary(blockStart, unit).getTime() / 1e3;
420
+ if (endSec <= startSec) break;
421
+ if (spans.length >= limit) return null;
422
+ spans.push([startSec, endSec]);
423
+ cursorSec = endSec;
424
+ }
425
+ return spans;
426
+ };
385
427
  var generateUnitBlocks = ({
386
428
  unit,
387
429
  format,
@@ -389,33 +431,55 @@ var generateUnitBlocks = ({
389
431
  windowEnd,
390
432
  tick
391
433
  }) => {
392
- const blocks = [];
434
+ var _a;
435
+ if (tick === null || tick <= 0 || windowEnd <= windowStart) return [];
393
436
  const resolvedUnit = unit != null ? unit : "hour";
394
- if (tick === null || tick <= 0 || windowEnd <= windowStart) return blocks;
395
437
  const label = format != null ? format : defaultFormat(resolvedUnit);
396
- const anchorSec = new Date(
397
- new Date(windowStart * 1e3).getFullYear(),
398
- new Date(windowStart * 1e3).getMonth(),
399
- new Date(windowStart * 1e3).getDate()
400
- ).getTime() / 1e3;
401
- let cursorSec = windowStart;
402
- let guard = 0;
403
- while (cursorSec < windowEnd && guard++ < 1e5) {
404
- const blockStart = floorToUnit(new Date(cursorSec * 1e3), resolvedUnit, anchorSec);
405
- const blockEnd = nextBoundary(blockStart, resolvedUnit);
406
- const startSec = blockStart.getTime() / 1e3;
407
- const endSec = blockEnd.getTime() / 1e3;
408
- if (endSec <= startSec) break;
409
- const visibleStart = Math.max(startSec, windowStart);
410
- const visibleEnd = Math.min(endSec, windowEnd);
411
- blocks.push({
412
- label: label(blockStart, blockEnd),
413
- x: (visibleStart - windowStart) / tick,
414
- width: (visibleEnd - visibleStart) / tick
415
- });
416
- cursorSec = endSec;
438
+ return ((_a = blockSpans(resolvedUnit, windowStart, windowEnd)) != null ? _a : []).map(
439
+ ([startSec, endSec]) => {
440
+ const visibleStart = Math.max(startSec, windowStart);
441
+ const visibleEnd = Math.min(endSec, windowEnd);
442
+ return {
443
+ label: label(new Date(startSec * 1e3), new Date(endSec * 1e3)),
444
+ x: (visibleStart - windowStart) / tick,
445
+ width: (visibleEnd - visibleStart) / tick
446
+ };
447
+ }
448
+ );
449
+ };
450
+ var shortDay = (d) => `${get_week_day_name_default(d.getDay())} ${d.getDate()}`;
451
+ var DEFAULT_TIME_BAR_LEVELS = [
452
+ { topRow: { unit: "day" }, bottomRow: { unit: "hour" } },
453
+ { topRow: { unit: "day" }, bottomRow: { unit: { stepSeconds: 6 * 3600 } } },
454
+ {
455
+ topRow: {
456
+ unit: "week",
457
+ format: (d) => `W${isoWeek(d)} \xB7 ${d.getDate()} ${get_month_name_default(d.getMonth())}`
458
+ },
459
+ bottomRow: { unit: "day", format: shortDay }
460
+ },
461
+ { topRow: { unit: "month" }, bottomRow: { unit: "week", format: (d) => `W${isoWeek(d)}` } },
462
+ { topRow: { unit: "year" }, bottomRow: { unit: "month", format: (d) => get_month_name_default(d.getMonth()) } }
463
+ ];
464
+ var DEFAULT_MIN_BLOCK_WIDTH = 40;
465
+ var resolveTimeBar = (timeBar, windowStart, windowEnd, tick) => {
466
+ var _a, _b, _c;
467
+ if (!(timeBar == null ? void 0 : timeBar.auto)) {
468
+ return {
469
+ topRow: (_a = timeBar == null ? void 0 : timeBar.topRow) != null ? _a : { unit: "day" },
470
+ bottomRow: (_b = timeBar == null ? void 0 : timeBar.bottomRow) != null ? _b : { unit: "hour" }
471
+ };
417
472
  }
418
- return blocks;
473
+ const { levels = DEFAULT_TIME_BAR_LEVELS, minBlockWidth = DEFAULT_MIN_BLOCK_WIDTH } = timeBar.auto === true ? {} : timeBar.auto;
474
+ const levelsOrDefault = levels.length > 0 ? levels : DEFAULT_TIME_BAR_LEVELS;
475
+ if (tick === null || tick <= 0 || windowEnd <= windowStart) return levelsOrDefault[0];
476
+ const minSecs = minBlockWidth * tick;
477
+ const limit = Math.ceil((windowEnd - windowStart) / minSecs) + 2;
478
+ return (_c = levelsOrDefault.find((level) => {
479
+ var _a2;
480
+ const spans = blockSpans((_a2 = level.bottomRow.unit) != null ? _a2 : "hour", windowStart, windowEnd, limit);
481
+ return spans !== null && spans.every(([start, end]) => end - start >= minSecs);
482
+ })) != null ? _c : levelsOrDefault[levelsOrDefault.length - 1];
419
483
  };
420
484
 
421
485
  // src/canvas/dpr.ts
@@ -505,6 +569,24 @@ var drawGridLines = (ctx, {
505
569
  }
506
570
  ctx.stroke();
507
571
  };
572
+ var drawBlockGridLines = (ctx, {
573
+ width,
574
+ height,
575
+ blocks,
576
+ color = DEFAULT_THEME.gridColor
577
+ }) => {
578
+ ctx.strokeStyle = color;
579
+ ctx.lineWidth = 1;
580
+ ctx.beginPath();
581
+ for (let i = 0; i < blocks.length - 1; i++) {
582
+ const x = blocks[i].x + blocks[i].width;
583
+ if (x < 1 || width - x < 1) continue;
584
+ const crisp = Math.round(x) - 0.5;
585
+ ctx.moveTo(crisp, 0);
586
+ ctx.lineTo(crisp, height);
587
+ }
588
+ ctx.stroke();
589
+ };
508
590
  var drawTimeBar = (ctx, {
509
591
  width,
510
592
  height,
@@ -512,9 +594,20 @@ var drawTimeBar = (ctx, {
512
594
  bottomBlocks,
513
595
  font,
514
596
  borderColor = DEFAULT_THEME.timeBarBorder,
515
- textColor = DEFAULT_THEME.timeBarTextColor
597
+ textColor = DEFAULT_THEME.timeBarTextColor,
598
+ topBackground,
599
+ bottomBackground,
600
+ bottomTextColor = textColor
516
601
  }) => {
517
602
  const mid = height / 2;
603
+ if (topBackground) {
604
+ ctx.fillStyle = topBackground;
605
+ ctx.fillRect(0, 0, width, Math.round(mid));
606
+ }
607
+ if (bottomBackground) {
608
+ ctx.fillStyle = bottomBackground;
609
+ ctx.fillRect(0, Math.round(mid), width, height - Math.round(mid));
610
+ }
518
611
  ctx.strokeStyle = borderColor;
519
612
  ctx.lineWidth = 1;
520
613
  ctx.beginPath();
@@ -546,6 +639,7 @@ var drawTimeBar = (ctx, {
546
639
  block.width - inset
547
640
  );
548
641
  }
642
+ ctx.fillStyle = bottomTextColor;
549
643
  for (const block of bottomBlocks) {
550
644
  ctx.fillText(
551
645
  block.label,
@@ -567,13 +661,11 @@ var CanvasTimeBar = ({
567
661
  theme,
568
662
  timeBar
569
663
  }) => {
570
- var _a, _b, _c;
664
+ var _a;
571
665
  const canvasRef = (0, import_react4.useRef)(null);
572
666
  const header = theme != null ? theme : DEFAULT_THEME;
573
- const topRow = (_a = timeBar == null ? void 0 : timeBar.topRow) != null ? _a : { unit: "day" };
574
- const bottomRow = (_b = timeBar == null ? void 0 : timeBar.bottomRow) != null ? _b : { unit: "hour" };
575
667
  (0, import_react4.useEffect)(() => {
576
- var _a2;
668
+ var _a2, _b;
577
669
  const canvas = canvasRef.current;
578
670
  if (!canvas || !contentWidth) return;
579
671
  const ctx = sizeCanvas(
@@ -584,12 +676,16 @@ var CanvasTimeBar = ({
584
676
  );
585
677
  if (!ctx) return;
586
678
  const [windowStart, windowEnd] = windowTime;
679
+ const { topRow, bottomRow } = resolveTimeBar(timeBar, windowStart, windowEnd, tick);
587
680
  drawTimeBar(ctx, {
588
681
  width: contentWidth,
589
682
  height: TIME_BAR_INNER_HEIGHT,
590
683
  font: (_a2 = theme == null ? void 0 : theme.font) != null ? _a2 : getComputedStyle(canvas).font,
591
684
  borderColor: theme == null ? void 0 : theme.timeBarBorder,
592
685
  textColor: theme == null ? void 0 : theme.timeBarTextColor,
686
+ topBackground: theme == null ? void 0 : theme.timeBarTopBackground,
687
+ bottomBackground: theme == null ? void 0 : theme.timeBarBottomBackground,
688
+ bottomTextColor: (_b = theme == null ? void 0 : theme.timeBarBottomTextColor) != null ? _b : theme == null ? void 0 : theme.timeBarTextColor,
593
689
  topBlocks: generateUnitBlocks(__spreadProps(__spreadValues({}, topRow), { windowStart, windowEnd, tick })),
594
690
  bottomBlocks: generateUnitBlocks(__spreadProps(__spreadValues({}, bottomRow), {
595
691
  windowStart,
@@ -620,7 +716,7 @@ var CanvasTimeBar = ({
620
716
  width: scrollWidth,
621
717
  height: "100%",
622
718
  boxSizing: "border-box",
623
- borderLeft: `1px solid ${(_c = header.headerBorderColor) != null ? _c : "yellow"}`
719
+ borderLeft: `1px solid ${(_a = header.headerBorderColor) != null ? _a : "yellow"}`
624
720
  }
625
721
  }
626
722
  ) : null
@@ -1195,6 +1291,28 @@ var TimelineRenderer = class {
1195
1291
  this.font = getComputedStyle(this.dynamicCanvas).font || this.font;
1196
1292
  }
1197
1293
  }
1294
+ drawGrid(ctx, tick) {
1295
+ const { width, height, windowTime, timeBar, theme } = this.view;
1296
+ if (timeBar == null ? void 0 : timeBar.auto) {
1297
+ const [windowStart, windowEnd] = windowTime;
1298
+ const { bottomRow } = resolveTimeBar(timeBar, windowStart, windowEnd, tick);
1299
+ drawBlockGridLines(ctx, {
1300
+ width,
1301
+ height,
1302
+ blocks: generateUnitBlocks(__spreadProps(__spreadValues({}, bottomRow), { windowStart, windowEnd, tick })),
1303
+ color: theme.gridColor
1304
+ });
1305
+ return;
1306
+ }
1307
+ drawGridLines(ctx, {
1308
+ width,
1309
+ height,
1310
+ cellWidth: this.view.cellWidth,
1311
+ windowStart: windowTime[0],
1312
+ tick,
1313
+ color: theme.gridColor
1314
+ });
1315
+ }
1198
1316
  drawFrame(dirty) {
1199
1317
  if (this.view.tick === null) return;
1200
1318
  this.animator.beginFrame(performance.now());
@@ -1203,14 +1321,7 @@ var TimelineRenderer = class {
1203
1321
  if (ctx) {
1204
1322
  ctx.clearRect(0, 0, this.view.width, this.view.height);
1205
1323
  drawGroupShading(ctx, this.view, this.scene);
1206
- drawGridLines(ctx, {
1207
- width: this.view.width,
1208
- height: this.view.height,
1209
- cellWidth: this.view.cellWidth,
1210
- windowStart: this.view.windowTime[0],
1211
- tick: this.view.tick,
1212
- color: this.view.theme.gridColor
1213
- });
1324
+ this.drawGrid(ctx, this.view.tick);
1214
1325
  drawRowDividers(ctx, this.view, this.scene);
1215
1326
  }
1216
1327
  }
@@ -2019,6 +2130,7 @@ var CanvasBoard = (0, import_react6.forwardRef)(
2019
2130
  onEventHover,
2020
2131
  eventsResize,
2021
2132
  stripeOverlap,
2133
+ timeBar,
2022
2134
  panZoom,
2023
2135
  animations,
2024
2136
  selectable,
@@ -2060,7 +2172,8 @@ var CanvasBoard = (0, import_react6.forwardRef)(
2060
2172
  theme,
2061
2173
  drawEvent,
2062
2174
  eventsResize,
2063
- stripeOverlap
2175
+ stripeOverlap,
2176
+ timeBar
2064
2177
  });
2065
2178
  }, [
2066
2179
  windowTime,
@@ -2070,6 +2183,7 @@ var CanvasBoard = (0, import_react6.forwardRef)(
2070
2183
  drawEvent,
2071
2184
  eventsResize,
2072
2185
  stripeOverlap,
2186
+ timeBar,
2073
2187
  animations,
2074
2188
  rendererRef
2075
2189
  ]);
@@ -2998,16 +3112,17 @@ var Timeline = (0, import_react8.forwardRef)(
2998
3112
  const pointerOpts = (0, import_react8.useRef)({ showPointerLine, showTimeBar, formatPointerTime, onPointerTime });
2999
3113
  pointerOpts.current = { showPointerLine, showTimeBar, formatPointerTime, onPointerTime };
3000
3114
  const drawPointer = (0, import_react8.useCallback)(() => {
3001
- var _a, _b;
3115
+ var _a, _b, _c;
3002
3116
  const line = pointerLineRef.current;
3003
3117
  const p = pointerRef.current;
3004
3118
  const t = tickRef.current;
3005
3119
  if (!line) return;
3006
3120
  if (!p || !t) return line.hide();
3121
+ if ("x" in p && ((_a = rendererRef.current) == null ? void 0 : _a.getView().hoveredEventId)) return line.hide();
3007
3122
  const start = windowTimeRef.current[0];
3008
3123
  const time = "x" in p ? start + p.x * t : p.time;
3009
3124
  const x = "x" in p ? p.x : (p.time - start) / t;
3010
- const width = (_b = (_a = contentRef.current) == null ? void 0 : _a.getBoundingClientRect().width) != null ? _b : 0;
3125
+ const width = (_c = (_b = contentRef.current) == null ? void 0 : _b.getBoundingClientRect().width) != null ? _c : 0;
3011
3126
  if (x < 0 || x > width) return line.hide();
3012
3127
  const { showTimeBar: labelled, formatPointerTime: format } = pointerOpts.current;
3013
3128
  line.show(x, labelled ? (format != null ? format : defaultPointerFormat)(time) : null);
@@ -3046,6 +3161,13 @@ var Timeline = (0, import_react8.forwardRef)(
3046
3161
  if (p && "x" in p && tick) (_b = (_a = pointerOpts.current).onPointerTime) == null ? void 0 : _b.call(_a, windowTime[0] + p.x * tick);
3047
3162
  drawPointer();
3048
3163
  }, [windowTime[0], windowTime[1], tick, drawPointer]);
3164
+ const onEventHoverRef = (0, import_react8.useRef)(onEventHover);
3165
+ onEventHoverRef.current = onEventHover;
3166
+ const handleEventHover = (0, import_react8.useCallback)((hover) => {
3167
+ var _a;
3168
+ drawPointer();
3169
+ (_a = onEventHoverRef.current) == null ? void 0 : _a.call(onEventHoverRef, hover);
3170
+ }, [drawPointer]);
3049
3171
  (0, import_react8.useImperativeHandle)(
3050
3172
  handleRef,
3051
3173
  () => ({
@@ -3205,9 +3327,10 @@ var Timeline = (0, import_react8.forwardRef)(
3205
3327
  onDrop,
3206
3328
  onResize,
3207
3329
  onEventClick,
3208
- onEventHover,
3330
+ onEventHover: handleEventHover,
3209
3331
  eventsResize,
3210
3332
  stripeOverlap,
3333
+ timeBar,
3211
3334
  panZoom,
3212
3335
  animations: resolvedAnimations,
3213
3336
  selectable,
@@ -3231,6 +3354,7 @@ var Timeline = (0, import_react8.forwardRef)(
3231
3354
  );
3232
3355
  // Annotate the CommonJS export names for ESM import in node:
3233
3356
  0 && (module.exports = {
3357
+ DEFAULT_TIME_BAR_LEVELS,
3234
3358
  Timeline
3235
3359
  });
3236
3360
  //# sourceMappingURL=index.js.map