Skip to content

Commit

Permalink
feat: 🎸 [Timeline] Added initialDateTime prop to auto scroll
Browse files Browse the repository at this point in the history
✅ Closes: #77
  • Loading branch information
luciob committed Oct 16, 2023
1 parent b5b7b44 commit 4500178
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/KonvaTimeline/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export const HiddenResources: Story = {
},
};

export const InitialDateTime: Story = {
args: {
...Primary.args,
initialDateTime: (range.start + range.end) / 2,
},
};

export const MixedDateTimeFormats: Story = {
args: {
...Primary.args,
Expand Down
19 changes: 16 additions & 3 deletions src/timeline/TimelineContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export type TimelineProviderProps = PropsWithChildren<TimelineInput> & {
* Enables resize operation on tasks
*/
enableResize?: boolean;
/**
* Initial date time to scroll to
*/
initialDateTime?: number | string;
/**
* Callback invoked when errors are thrown
*/
Expand Down Expand Up @@ -63,6 +67,7 @@ type TimelineContextType = Required<
drawRange: InternalTimeRange;
enableDrag: boolean;
enableResize: boolean;
initialDateTime?: number;
interval: Interval;
onErrors?: (errors: KonvaTimelineError[]) => void;
onTaskClick?: (task: TaskData) => void;
Expand Down Expand Up @@ -93,6 +98,7 @@ export const TimelineProvider = ({
enableDrag = true,
enableResize = true,
hideResources = false,
initialDateTime: externalInitialDateTime,
onErrors,
onTaskClick,
onTaskChange,
Expand All @@ -103,9 +109,6 @@ export const TimelineProvider = ({
rowHeight: externalRowHeight,
theme: externalTheme = "light",
}: TimelineProviderProps) => {
// logWarn("TimelineProvider", `Debug ${debug ? "ON" : "OFF"}`);
// window.__MELFORE_KONVA_TIMELINE_DEBUG__ = debug;

const [drawRange, setDrawRange] = useState(DEFAULT_DRAW_RANGE);

useEffect(() => {
Expand All @@ -121,6 +124,15 @@ export const TimelineProvider = ({
return { start, end };
}, [externalRange]);

const initialDateTime = useMemo(() => {
let initial = !externalInitialDateTime ? DateTime.now().toMillis() : getValidTime(externalInitialDateTime);
if (initial < range.start || initial > range.end) {
return;
}

return initial;
}, [externalInitialDateTime, range]);

const validTasks = useMemo(() => validateTasks(externalTasks, range), [externalTasks, range]);

const interval = useMemo(
Expand Down Expand Up @@ -235,6 +247,7 @@ export const TimelineProvider = ({
enableDrag,
enableResize,
hideResources,
initialDateTime,
interval,
onErrors,
onTaskClick,
Expand Down
14 changes: 14 additions & 0 deletions src/timeline/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { CSSProperties, FC, useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Stage } from "react-konva";
import Konva from "konva";
import { DateTime } from "luxon";

import GridLayer from "../grid/Layer";
import ResourcesLayer from "../resources/components/Layer";
Expand All @@ -23,8 +24,11 @@ const DEFAULT_STAGE_SIZE: StageSize = { height: 0, width: 0 };
const Timeline: FC<TimelineProps> = () => {
const {
hideResources,
initialDateTime,
interval: { start: intervalStart },
columnWidth,
resourcesContentHeight,
resolution,
setDrawRange,
theme: { color: themeColor },
timeBlocks,
Expand Down Expand Up @@ -90,6 +94,16 @@ const Timeline: FC<TimelineProps> = () => {
onWindowResize();
}, [hideResources, onWindowResize]);

useEffect(() => {
if (!wrapper.current || !initialDateTime) {
return;
}

const timeStart = DateTime.fromMillis(initialDateTime);
const startOffsetInUnit = timeStart.diff(intervalStart!).as(resolution.unit);
wrapper.current.scrollTo({ left: (startOffsetInUnit * columnWidth) / resolution.sizeInUnits });
}, [columnWidth, initialDateTime, intervalStart, resolution.sizeInUnits, resolution.unit]);

const fullTimelineWidth = useMemo(() => columnWidth * timeBlocks.length, [columnWidth, timeBlocks]);

// const stageHeight = useMemo(() => size.height, [size]);
Expand Down

0 comments on commit 4500178

Please sign in to comment.