awesome-timeline-react 0.10.0 → 0.11.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
@@ -52,7 +52,7 @@ __export(index_exports, {
52
52
  module.exports = __toCommonJS(index_exports);
53
53
 
54
54
  // src/timeline.tsx
55
- var import_react7 = __toESM(require("react"));
55
+ var import_react8 = __toESM(require("react"));
56
56
 
57
57
  // src/canvas/animator.ts
58
58
  var DEFAULT_LAYOUT_DURATION_MS = 200;
@@ -129,34 +129,40 @@ var LayoutAnimator = class {
129
129
 
130
130
  // src/hooks/use-resize-observer.ts
131
131
  var import_react = require("react");
132
+ var createWidthObserver = (getWindow, onScale) => {
133
+ let prevWidth = 0;
134
+ return new ResizeObserver((entries) => {
135
+ var _a;
136
+ for (const entry of entries) {
137
+ const width = (_a = entry.borderBoxSize) == null ? void 0 : _a[0].inlineSize;
138
+ if (typeof width === "number" && width !== prevWidth) {
139
+ prevWidth = width;
140
+ const [start, end] = getWindow();
141
+ const windowDuration = end - start;
142
+ onScale(
143
+ windowDuration / entry.contentRect.width,
144
+ entry.contentRect.width / (windowDuration / 3600)
145
+ );
146
+ }
147
+ }
148
+ });
149
+ };
132
150
  var useResizeObserver = ({
133
- windowTime,
151
+ windowTimeRef,
134
152
  setTick,
135
153
  setCellWidth,
136
154
  contentRef
137
155
  }) => {
138
- const prevWidthRef = (0, import_react.useRef)(0);
139
- const resizeObserver = (0, import_react.useMemo)(() => {
140
- return new ResizeObserver((entries) => {
141
- var _a;
142
- for (const entry of entries) {
143
- const width = (_a = entry.borderBoxSize) == null ? void 0 : _a[0].inlineSize;
144
- if (typeof width === "number" && width !== prevWidthRef.current) {
145
- prevWidthRef.current = width;
146
- const windowDuration = windowTime[1] - windowTime[0];
147
- const numberOfHourBlocks = windowDuration / 3600;
148
- setTick(windowDuration / entry.contentRect.width);
149
- setCellWidth(entry.contentRect.width / numberOfHourBlocks);
150
- }
151
- }
152
- });
153
- }, [windowTime]);
154
156
  (0, import_react.useEffect)(() => {
157
+ const observer = createWidthObserver(() => windowTimeRef.current, (tick, cellWidth) => {
158
+ setTick(tick);
159
+ setCellWidth(cellWidth);
160
+ });
155
161
  if (contentRef.current) {
156
- resizeObserver.observe(contentRef.current, { box: "border-box" });
162
+ observer.observe(contentRef.current, { box: "border-box" });
157
163
  }
158
- return () => resizeObserver.disconnect();
159
- }, [resizeObserver]);
164
+ return () => observer.disconnect();
165
+ }, [windowTimeRef, setTick, setCellWidth, contentRef]);
160
166
  };
161
167
  var use_resize_observer_default = useResizeObserver;
162
168
 
@@ -469,7 +475,9 @@ var DEFAULT_THEME = {
469
475
  barRadius: 5,
470
476
  minHitWidth: 0,
471
477
  groupChildBackground: "rgba(0,0,0,0.05)",
472
- groupShadowColor: "rgba(0,0,0,0.22)"
478
+ groupShadowColor: "rgba(0,0,0,0.22)",
479
+ pointerLineColor: "rgba(15,23,42,0.7)",
480
+ pointerLabelColor: "#ffffff"
473
481
  };
474
482
  var resolveTheme = (theme) => __spreadValues(__spreadValues({}, DEFAULT_THEME), theme);
475
483
 
@@ -549,7 +557,8 @@ var drawTimeBar = (ctx, {
549
557
  };
550
558
 
551
559
  // src/components/canvas-time-bar.tsx
552
- var TIME_BAR_INNER_HEIGHT = 48;
560
+ var TIME_BAR_HEIGHT = 50;
561
+ var TIME_BAR_INNER_HEIGHT = TIME_BAR_HEIGHT - 2;
553
562
  var CanvasTimeBar = ({
554
563
  windowTime,
555
564
  tick,
@@ -2358,6 +2367,43 @@ var CanvasBoard = (0, import_react6.forwardRef)(
2358
2367
  );
2359
2368
  var canvas_board_default = CanvasBoard;
2360
2369
 
2370
+ // src/components/pointer-line.tsx
2371
+ var import_react7 = __toESM(require("react"));
2372
+ var PointerLine = (0, import_react7.forwardRef)(({ contentRef, mainRef, top, color, labelColor, font }, ref) => {
2373
+ const lineRef = (0, import_react7.useRef)(null);
2374
+ const labelRef = (0, import_react7.useRef)(null);
2375
+ (0, import_react7.useImperativeHandle)(ref, () => ({
2376
+ show: (x, label) => {
2377
+ var _a, _b;
2378
+ const line = lineRef.current, tag = labelRef.current;
2379
+ const content = contentRef.current, main = mainRef.current;
2380
+ if (!line || !tag || !content || !main) return;
2381
+ const box = content.getBoundingClientRect();
2382
+ const frame = main.getBoundingClientRect();
2383
+ const left = box.left - frame.left;
2384
+ const bottom = Math.min(box.bottom, (_b = (_a = content.parentElement) == null ? void 0 : _a.getBoundingClientRect().bottom) != null ? _b : box.bottom);
2385
+ line.style.height = `${Math.max(bottom - frame.top - top, 0)}px`;
2386
+ line.style.display = "block";
2387
+ line.style.transform = `translateX(${Math.round(left + x)}px)`;
2388
+ if (label === null) {
2389
+ tag.style.display = "none";
2390
+ return;
2391
+ }
2392
+ tag.textContent = label;
2393
+ tag.style.display = "block";
2394
+ const w = tag.offsetWidth;
2395
+ const at = Math.min(Math.max(x - w / 2, 0), Math.max(box.width - w, 0));
2396
+ tag.style.transform = `translateX(${Math.round(left + at)}px)`;
2397
+ },
2398
+ hide: () => {
2399
+ if (lineRef.current) lineRef.current.style.display = "none";
2400
+ if (labelRef.current) labelRef.current.style.display = "none";
2401
+ }
2402
+ }), [contentRef, mainRef, top]);
2403
+ return /* @__PURE__ */ import_react7.default.createElement(import_react7.default.Fragment, null, /* @__PURE__ */ import_react7.default.createElement("div", { ref: lineRef, className: "pointer-line", style: { top, borderLeftColor: color } }), /* @__PURE__ */ import_react7.default.createElement("div", { ref: labelRef, className: "pointer-label", style: { background: color, color: labelColor, font } }));
2404
+ });
2405
+ var pointer_line_default = PointerLine;
2406
+
2361
2407
  // src/core/scene.ts
2362
2408
  var binaryInsertIndex = (events, event) => {
2363
2409
  let lo = 0;
@@ -2817,7 +2863,8 @@ var SceneStore = class {
2817
2863
  };
2818
2864
 
2819
2865
  // src/timeline.tsx
2820
- var Timeline = (0, import_react7.forwardRef)(
2866
+ var defaultPointerFormat = (time) => new Date(time * 1e3).toLocaleString();
2867
+ var Timeline = (0, import_react8.forwardRef)(
2821
2868
  ({
2822
2869
  rows,
2823
2870
  events,
@@ -2845,9 +2892,12 @@ var Timeline = (0, import_react7.forwardRef)(
2845
2892
  panZoom,
2846
2893
  timeBar,
2847
2894
  showTimeBar = true,
2895
+ showPointerLine = false,
2896
+ formatPointerTime,
2897
+ onPointerTime,
2848
2898
  animations = true
2849
2899
  }, handleRef) => {
2850
- const [windowTime, setWindowTime] = (0, import_react7.useState)([
2900
+ const [windowTime, setWindowTime] = (0, import_react8.useState)([
2851
2901
  new Date(
2852
2902
  startDate.getFullYear(),
2853
2903
  startDate.getMonth(),
@@ -2865,25 +2915,25 @@ var Timeline = (0, import_react7.forwardRef)(
2865
2915
  0
2866
2916
  ).getTime() / 1e3
2867
2917
  ]);
2868
- const [cellWidth, setCellWidth] = (0, import_react7.useState)(0);
2869
- const contentRef = (0, import_react7.useRef)(null);
2870
- const mainRef = (0, import_react7.useRef)(null);
2871
- const bodyRef = (0, import_react7.useRef)(null);
2872
- const [tick, setTick] = (0, import_react7.useState)(null);
2873
- const [scrollWidth, setScrollWidth] = (0, import_react7.useState)(0);
2874
- const eventPromptRef = (0, import_react7.useRef)(null);
2875
- const rendererRef = (0, import_react7.useRef)(null);
2876
- const sceneRef = (0, import_react7.useRef)(null);
2918
+ const [cellWidth, setCellWidth] = (0, import_react8.useState)(0);
2919
+ const contentRef = (0, import_react8.useRef)(null);
2920
+ const mainRef = (0, import_react8.useRef)(null);
2921
+ const bodyRef = (0, import_react8.useRef)(null);
2922
+ const [tick, setTick] = (0, import_react8.useState)(null);
2923
+ const [scrollWidth, setScrollWidth] = (0, import_react8.useState)(0);
2924
+ const eventPromptRef = (0, import_react8.useRef)(null);
2925
+ const rendererRef = (0, import_react8.useRef)(null);
2926
+ const sceneRef = (0, import_react8.useRef)(null);
2877
2927
  if (sceneRef.current === null) {
2878
2928
  sceneRef.current = new SceneStore();
2879
2929
  }
2880
2930
  const scene = sceneRef.current;
2881
- const selectionRef = (0, import_react7.useRef)(
2931
+ const selectionRef = (0, import_react8.useRef)(
2882
2932
  new Set(defaultSelectedEventIds != null ? defaultSelectedEventIds : [])
2883
2933
  );
2884
- const onSelectionChangeRef = (0, import_react7.useRef)(onSelectionChange);
2934
+ const onSelectionChangeRef = (0, import_react8.useRef)(onSelectionChange);
2885
2935
  onSelectionChangeRef.current = onSelectionChange;
2886
- const applySelection = (0, import_react7.useCallback)(
2936
+ const applySelection = (0, import_react8.useCallback)(
2887
2937
  (ids) => {
2888
2938
  var _a, _b;
2889
2939
  const expanded = [];
@@ -2903,13 +2953,13 @@ var Timeline = (0, import_react7.forwardRef)(
2903
2953
  },
2904
2954
  [scene]
2905
2955
  );
2906
- const getSelection = (0, import_react7.useCallback)(() => [...selectionRef.current], []);
2907
- (0, import_react7.useEffect)(() => {
2956
+ const getSelection = (0, import_react8.useCallback)(() => [...selectionRef.current], []);
2957
+ (0, import_react8.useEffect)(() => {
2908
2958
  var _a;
2909
2959
  scene.setRows(rows);
2910
2960
  (_a = rendererRef.current) == null ? void 0 : _a.animator.reset();
2911
2961
  }, [scene, rows]);
2912
- (0, import_react7.useEffect)(() => {
2962
+ (0, import_react8.useEffect)(() => {
2913
2963
  var _a, _b, _c;
2914
2964
  scene.setEvents([...events].sort(sort_events_default));
2915
2965
  (_a = rendererRef.current) == null ? void 0 : _a.animator.reset();
@@ -2922,26 +2972,81 @@ var Timeline = (0, import_react7.forwardRef)(
2922
2972
  (_c = onSelectionChangeRef.current) == null ? void 0 : _c.call(onSelectionChangeRef, survivors);
2923
2973
  }
2924
2974
  }, [scene, events]);
2925
- (0, import_react7.useEffect)(() => {
2975
+ (0, import_react8.useEffect)(() => {
2926
2976
  scene.setStaticEvents(staticEvents ? [...staticEvents] : []);
2927
2977
  }, [scene, staticEvents]);
2928
- const seededCollapseRef = (0, import_react7.useRef)(false);
2929
- (0, import_react7.useEffect)(() => {
2978
+ const seededCollapseRef = (0, import_react8.useRef)(false);
2979
+ (0, import_react8.useEffect)(() => {
2930
2980
  if (seededCollapseRef.current) return;
2931
2981
  seededCollapseRef.current = true;
2932
2982
  for (const id of defaultCollapsedRowIds != null ? defaultCollapsedRowIds : []) {
2933
2983
  scene.setCollapsed(id, true);
2934
2984
  }
2935
2985
  }, [scene, defaultCollapsedRowIds]);
2936
- const windowTimeRef = (0, import_react7.useRef)(windowTime);
2986
+ const windowTimeRef = (0, import_react8.useRef)(windowTime);
2937
2987
  windowTimeRef.current = windowTime;
2938
- const onWindowChangeRef = (0, import_react7.useRef)(onWindowChange);
2988
+ const onWindowChangeRef = (0, import_react8.useRef)(onWindowChange);
2939
2989
  onWindowChangeRef.current = onWindowChange;
2940
- (0, import_react7.useEffect)(() => {
2990
+ (0, import_react8.useEffect)(() => {
2941
2991
  var _a;
2942
2992
  (_a = onWindowChangeRef.current) == null ? void 0 : _a.call(onWindowChangeRef, { startTime: windowTime[0], endTime: windowTime[1] });
2943
2993
  }, [windowTime[0], windowTime[1]]);
2944
- (0, import_react7.useImperativeHandle)(
2994
+ const tickRef = (0, import_react8.useRef)(tick);
2995
+ tickRef.current = tick;
2996
+ const pointerLineRef = (0, import_react8.useRef)(null);
2997
+ const pointerRef = (0, import_react8.useRef)(null);
2998
+ const pointerOpts = (0, import_react8.useRef)({ showPointerLine, showTimeBar, formatPointerTime, onPointerTime });
2999
+ pointerOpts.current = { showPointerLine, showTimeBar, formatPointerTime, onPointerTime };
3000
+ const drawPointer = (0, import_react8.useCallback)(() => {
3001
+ var _a, _b;
3002
+ const line = pointerLineRef.current;
3003
+ const p = pointerRef.current;
3004
+ const t = tickRef.current;
3005
+ if (!line) return;
3006
+ if (!p || !t) return line.hide();
3007
+ const start = windowTimeRef.current[0];
3008
+ const time = "x" in p ? start + p.x * t : p.time;
3009
+ 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;
3011
+ if (x < 0 || x > width) return line.hide();
3012
+ const { showTimeBar: labelled, formatPointerTime: format } = pointerOpts.current;
3013
+ line.show(x, labelled ? (format != null ? format : defaultPointerFormat)(time) : null);
3014
+ }, []);
3015
+ (0, import_react8.useEffect)(() => {
3016
+ const column = contentRef.current;
3017
+ if (!column) return;
3018
+ const move = (e) => {
3019
+ var _a, _b;
3020
+ const t = tickRef.current;
3021
+ if (!t) return;
3022
+ const x = e.clientX - column.getBoundingClientRect().left;
3023
+ (_b = (_a = pointerOpts.current).onPointerTime) == null ? void 0 : _b.call(_a, windowTimeRef.current[0] + x * t);
3024
+ if (!pointerOpts.current.showPointerLine) return;
3025
+ pointerRef.current = { x };
3026
+ drawPointer();
3027
+ };
3028
+ const leave = () => {
3029
+ var _a, _b;
3030
+ (_b = (_a = pointerOpts.current).onPointerTime) == null ? void 0 : _b.call(_a, null);
3031
+ if (pointerRef.current && "x" in pointerRef.current) {
3032
+ pointerRef.current = null;
3033
+ drawPointer();
3034
+ }
3035
+ };
3036
+ column.addEventListener("pointermove", move);
3037
+ column.addEventListener("pointerleave", leave);
3038
+ return () => {
3039
+ column.removeEventListener("pointermove", move);
3040
+ column.removeEventListener("pointerleave", leave);
3041
+ };
3042
+ }, [drawPointer]);
3043
+ (0, import_react8.useEffect)(() => {
3044
+ var _a, _b;
3045
+ const p = pointerRef.current;
3046
+ if (p && "x" in p && tick) (_b = (_a = pointerOpts.current).onPointerTime) == null ? void 0 : _b.call(_a, windowTime[0] + p.x * tick);
3047
+ drawPointer();
3048
+ }, [windowTime[0], windowTime[1], tick, drawPointer]);
3049
+ (0, import_react8.useImperativeHandle)(
2945
3050
  handleRef,
2946
3051
  () => ({
2947
3052
  setEvents: (next) => scene.setEvents([...next].sort(sort_events_default)),
@@ -2959,6 +3064,7 @@ var Timeline = (0, import_react7.forwardRef)(
2959
3064
  // the scale follows the window, or a zoom applied from outside draws and pans at the old one
2960
3065
  setWindow: (startTime, endTime) => {
2961
3066
  var _a;
3067
+ windowTimeRef.current = [startTime, endTime];
2962
3068
  setWindowTime([startTime, endTime]);
2963
3069
  const width = (_a = contentRef.current) == null ? void 0 : _a.getBoundingClientRect().width;
2964
3070
  if (width && endTime > startTime) {
@@ -2970,6 +3076,10 @@ var Timeline = (0, import_react7.forwardRef)(
2970
3076
  startTime: windowTimeRef.current[0],
2971
3077
  endTime: windowTimeRef.current[1]
2972
3078
  }),
3079
+ setPointerTime: (time) => {
3080
+ pointerRef.current = time === null ? null : { time };
3081
+ drawPointer();
3082
+ },
2973
3083
  setSelection: (ids) => applySelection(ids),
2974
3084
  getSelection,
2975
3085
  toggleRow: (rowId) => scene.toggleCollapsed(rowId),
@@ -2979,9 +3089,9 @@ var Timeline = (0, import_react7.forwardRef)(
2979
3089
  return (_a = rendererRef.current) == null ? void 0 : _a.invalidate("all");
2980
3090
  }
2981
3091
  }),
2982
- [scene, applySelection, getSelection]
3092
+ [scene, applySelection, getSelection, drawPointer]
2983
3093
  );
2984
- (0, import_react7.useEffect)(() => {
3094
+ (0, import_react8.useEffect)(() => {
2985
3095
  if (contentRef.current) {
2986
3096
  const windowDuration = windowTime[1] - windowTime[0];
2987
3097
  const numberOfHourBlocks = windowDuration / 3600;
@@ -3009,8 +3119,8 @@ var Timeline = (0, import_react7.forwardRef)(
3009
3119
  0,
3010
3120
  0
3011
3121
  ).getTime() / 1e3;
3012
- const lastFitRef = (0, import_react7.useRef)([fitStart, fitEnd]);
3013
- (0, import_react7.useEffect)(() => {
3122
+ const lastFitRef = (0, import_react8.useRef)([fitStart, fitEnd]);
3123
+ (0, import_react8.useEffect)(() => {
3014
3124
  var _a;
3015
3125
  if (lastFitRef.current[0] === fitStart && lastFitRef.current[1] === fitEnd) {
3016
3126
  return;
@@ -3024,23 +3134,23 @@ var Timeline = (0, import_react7.forwardRef)(
3024
3134
  setCellWidth(width / (windowDuration / 3600));
3025
3135
  }
3026
3136
  }, [fitStart, fitEnd]);
3027
- (0, import_react7.useEffect)(() => {
3137
+ (0, import_react8.useEffect)(() => {
3028
3138
  if (bodyRef.current) {
3029
3139
  setScrollWidth(
3030
3140
  bodyRef.current.getBoundingClientRect().width - bodyRef.current.scrollWidth
3031
3141
  );
3032
3142
  }
3033
3143
  }, []);
3034
- use_resize_observer_default({ contentRef, setCellWidth, setTick, windowTime });
3035
- const resolvedTheme = (0, import_react7.useMemo)(() => resolveTheme(theme), [theme]);
3036
- const geometry = (0, import_react7.useMemo)(
3144
+ use_resize_observer_default({ contentRef, setCellWidth, setTick, windowTimeRef });
3145
+ const resolvedTheme = (0, import_react8.useMemo)(() => resolveTheme(theme), [theme]);
3146
+ const geometry = (0, import_react8.useMemo)(
3037
3147
  () => geometryFromTheme(resolvedTheme),
3038
3148
  [resolvedTheme]
3039
3149
  );
3040
- (0, import_react7.useEffect)(() => {
3150
+ (0, import_react8.useEffect)(() => {
3041
3151
  scene.setGeometry(geometry);
3042
3152
  }, [scene, geometry]);
3043
- const resolvedAnimations = (0, import_react7.useMemo)(() => {
3153
+ const resolvedAnimations = (0, import_react8.useMemo)(() => {
3044
3154
  var _a, _b;
3045
3155
  if (animations === false) return { layoutMs: 0, fadeMs: 0 };
3046
3156
  if (animations === true) {
@@ -3054,8 +3164,8 @@ var Timeline = (0, import_react7.forwardRef)(
3054
3164
  fadeMs: (_b = animations.fadeMs) != null ? _b : DEFAULT_FADE_DURATION_MS
3055
3165
  };
3056
3166
  }, [animations]);
3057
- const eventPrompt = (0, import_react7.useMemo)(
3058
- () => /* @__PURE__ */ import_react7.default.createElement(
3167
+ const eventPrompt = (0, import_react8.useMemo)(
3168
+ () => /* @__PURE__ */ import_react8.default.createElement(
3059
3169
  event_prompt_default,
3060
3170
  {
3061
3171
  ref: eventPromptRef,
@@ -3064,7 +3174,7 @@ var Timeline = (0, import_react7.forwardRef)(
3064
3174
  ),
3065
3175
  [eventPromptTemplate]
3066
3176
  );
3067
- return /* @__PURE__ */ import_react7.default.createElement("div", { className: "main-wrapper", ref: mainRef }, showRTIndicator && /* @__PURE__ */ import_react7.default.createElement(rt_indicator_default, { tick, windowTime }), showTimeBar && /* @__PURE__ */ import_react7.default.createElement(
3177
+ return /* @__PURE__ */ import_react8.default.createElement("div", { className: "main-wrapper", ref: mainRef }, showRTIndicator && /* @__PURE__ */ import_react8.default.createElement(rt_indicator_default, { tick, windowTime }), showTimeBar && /* @__PURE__ */ import_react8.default.createElement(
3068
3178
  canvas_time_bar_default,
3069
3179
  {
3070
3180
  windowTime,
@@ -3074,7 +3184,7 @@ var Timeline = (0, import_react7.forwardRef)(
3074
3184
  theme: resolvedTheme,
3075
3185
  timeBar
3076
3186
  }
3077
- ), /* @__PURE__ */ import_react7.default.createElement("div", { className: "body-wrapper", ref: bodyRef, style: { borderColor: resolvedTheme.borderColor } }, /* @__PURE__ */ import_react7.default.createElement(
3187
+ ), /* @__PURE__ */ import_react8.default.createElement("div", { className: "body-wrapper", ref: bodyRef, style: { borderColor: resolvedTheme.borderColor } }, /* @__PURE__ */ import_react8.default.createElement(
3078
3188
  canvas_board_default,
3079
3189
  {
3080
3190
  scene,
@@ -3105,7 +3215,18 @@ var Timeline = (0, import_react7.forwardRef)(
3105
3215
  getSelection,
3106
3216
  ref: contentRef
3107
3217
  }
3108
- ), showEventPrompt && eventPrompt));
3218
+ ), showEventPrompt && eventPrompt), /* @__PURE__ */ import_react8.default.createElement(
3219
+ pointer_line_default,
3220
+ {
3221
+ ref: pointerLineRef,
3222
+ contentRef,
3223
+ mainRef,
3224
+ top: showTimeBar ? TIME_BAR_HEIGHT : 0,
3225
+ color: resolvedTheme.pointerLineColor,
3226
+ labelColor: resolvedTheme.pointerLabelColor,
3227
+ font: resolvedTheme.font
3228
+ }
3229
+ ));
3109
3230
  }
3110
3231
  );
3111
3232
  // Annotate the CommonJS export names for ESM import in node: