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.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,
@@ -567,11 +649,9 @@ var CanvasTimeBar = ({
567
649
  theme,
568
650
  timeBar
569
651
  }) => {
570
- var _a, _b, _c;
652
+ var _a;
571
653
  const canvasRef = (0, import_react4.useRef)(null);
572
654
  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
655
  (0, import_react4.useEffect)(() => {
576
656
  var _a2;
577
657
  const canvas = canvasRef.current;
@@ -584,6 +664,7 @@ var CanvasTimeBar = ({
584
664
  );
585
665
  if (!ctx) return;
586
666
  const [windowStart, windowEnd] = windowTime;
667
+ const { topRow, bottomRow } = resolveTimeBar(timeBar, windowStart, windowEnd, tick);
587
668
  drawTimeBar(ctx, {
588
669
  width: contentWidth,
589
670
  height: TIME_BAR_INNER_HEIGHT,
@@ -620,7 +701,7 @@ var CanvasTimeBar = ({
620
701
  width: scrollWidth,
621
702
  height: "100%",
622
703
  boxSizing: "border-box",
623
- borderLeft: `1px solid ${(_c = header.headerBorderColor) != null ? _c : "yellow"}`
704
+ borderLeft: `1px solid ${(_a = header.headerBorderColor) != null ? _a : "yellow"}`
624
705
  }
625
706
  }
626
707
  ) : null
@@ -1195,6 +1276,28 @@ var TimelineRenderer = class {
1195
1276
  this.font = getComputedStyle(this.dynamicCanvas).font || this.font;
1196
1277
  }
1197
1278
  }
1279
+ drawGrid(ctx, tick) {
1280
+ const { width, height, windowTime, timeBar, theme } = this.view;
1281
+ if (timeBar == null ? void 0 : timeBar.auto) {
1282
+ const [windowStart, windowEnd] = windowTime;
1283
+ const { bottomRow } = resolveTimeBar(timeBar, windowStart, windowEnd, tick);
1284
+ drawBlockGridLines(ctx, {
1285
+ width,
1286
+ height,
1287
+ blocks: generateUnitBlocks(__spreadProps(__spreadValues({}, bottomRow), { windowStart, windowEnd, tick })),
1288
+ color: theme.gridColor
1289
+ });
1290
+ return;
1291
+ }
1292
+ drawGridLines(ctx, {
1293
+ width,
1294
+ height,
1295
+ cellWidth: this.view.cellWidth,
1296
+ windowStart: windowTime[0],
1297
+ tick,
1298
+ color: theme.gridColor
1299
+ });
1300
+ }
1198
1301
  drawFrame(dirty) {
1199
1302
  if (this.view.tick === null) return;
1200
1303
  this.animator.beginFrame(performance.now());
@@ -1203,14 +1306,7 @@ var TimelineRenderer = class {
1203
1306
  if (ctx) {
1204
1307
  ctx.clearRect(0, 0, this.view.width, this.view.height);
1205
1308
  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
- });
1309
+ this.drawGrid(ctx, this.view.tick);
1214
1310
  drawRowDividers(ctx, this.view, this.scene);
1215
1311
  }
1216
1312
  }
@@ -2019,6 +2115,7 @@ var CanvasBoard = (0, import_react6.forwardRef)(
2019
2115
  onEventHover,
2020
2116
  eventsResize,
2021
2117
  stripeOverlap,
2118
+ timeBar,
2022
2119
  panZoom,
2023
2120
  animations,
2024
2121
  selectable,
@@ -2060,7 +2157,8 @@ var CanvasBoard = (0, import_react6.forwardRef)(
2060
2157
  theme,
2061
2158
  drawEvent,
2062
2159
  eventsResize,
2063
- stripeOverlap
2160
+ stripeOverlap,
2161
+ timeBar
2064
2162
  });
2065
2163
  }, [
2066
2164
  windowTime,
@@ -2070,6 +2168,7 @@ var CanvasBoard = (0, import_react6.forwardRef)(
2070
2168
  drawEvent,
2071
2169
  eventsResize,
2072
2170
  stripeOverlap,
2171
+ timeBar,
2073
2172
  animations,
2074
2173
  rendererRef
2075
2174
  ]);
@@ -2998,16 +3097,17 @@ var Timeline = (0, import_react8.forwardRef)(
2998
3097
  const pointerOpts = (0, import_react8.useRef)({ showPointerLine, showTimeBar, formatPointerTime, onPointerTime });
2999
3098
  pointerOpts.current = { showPointerLine, showTimeBar, formatPointerTime, onPointerTime };
3000
3099
  const drawPointer = (0, import_react8.useCallback)(() => {
3001
- var _a, _b;
3100
+ var _a, _b, _c;
3002
3101
  const line = pointerLineRef.current;
3003
3102
  const p = pointerRef.current;
3004
3103
  const t = tickRef.current;
3005
3104
  if (!line) return;
3006
3105
  if (!p || !t) return line.hide();
3106
+ if ("x" in p && ((_a = rendererRef.current) == null ? void 0 : _a.getView().hoveredEventId)) return line.hide();
3007
3107
  const start = windowTimeRef.current[0];
3008
3108
  const time = "x" in p ? start + p.x * t : p.time;
3009
3109
  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;
3110
+ const width = (_c = (_b = contentRef.current) == null ? void 0 : _b.getBoundingClientRect().width) != null ? _c : 0;
3011
3111
  if (x < 0 || x > width) return line.hide();
3012
3112
  const { showTimeBar: labelled, formatPointerTime: format } = pointerOpts.current;
3013
3113
  line.show(x, labelled ? (format != null ? format : defaultPointerFormat)(time) : null);
@@ -3046,6 +3146,13 @@ var Timeline = (0, import_react8.forwardRef)(
3046
3146
  if (p && "x" in p && tick) (_b = (_a = pointerOpts.current).onPointerTime) == null ? void 0 : _b.call(_a, windowTime[0] + p.x * tick);
3047
3147
  drawPointer();
3048
3148
  }, [windowTime[0], windowTime[1], tick, drawPointer]);
3149
+ const onEventHoverRef = (0, import_react8.useRef)(onEventHover);
3150
+ onEventHoverRef.current = onEventHover;
3151
+ const handleEventHover = (0, import_react8.useCallback)((hover) => {
3152
+ var _a;
3153
+ drawPointer();
3154
+ (_a = onEventHoverRef.current) == null ? void 0 : _a.call(onEventHoverRef, hover);
3155
+ }, [drawPointer]);
3049
3156
  (0, import_react8.useImperativeHandle)(
3050
3157
  handleRef,
3051
3158
  () => ({
@@ -3205,9 +3312,10 @@ var Timeline = (0, import_react8.forwardRef)(
3205
3312
  onDrop,
3206
3313
  onResize,
3207
3314
  onEventClick,
3208
- onEventHover,
3315
+ onEventHover: handleEventHover,
3209
3316
  eventsResize,
3210
3317
  stripeOverlap,
3318
+ timeBar,
3211
3319
  panZoom,
3212
3320
  animations: resolvedAnimations,
3213
3321
  selectable,
@@ -3231,6 +3339,7 @@ var Timeline = (0, import_react8.forwardRef)(
3231
3339
  );
3232
3340
  // Annotate the CommonJS export names for ESM import in node:
3233
3341
  0 && (module.exports = {
3342
+ DEFAULT_TIME_BAR_LEVELS,
3234
3343
  Timeline
3235
3344
  });
3236
3345
  //# sourceMappingURL=index.js.map