namespace-ee / react-calendar-timeline

A modern and responsive react timeline component.
MIT License
1.91k stars 613 forks source link

Scroll problem with onTimeChange - Limiting visible range view #872

Open Hugofv opened 1 year ago

Hugofv commented 1 year ago

I have a problem that I want to limite visible range view using the follow code:

` const onTimeChange = ( visibleTimeStart, visibleTimeEnd, updateScrollCanvas ) => { const minDate = filter.startDate.clone().startOf("day").valueOf(); const maxDate = filter.endDate.clone().endOf("day").valueOf();

if (visibleTimeStart < minDate && visibleTimeEnd > maxDate) {
  updateScrollCanvas(minDate, maxDate);
} else if (visibleTimeStart < minDate) {
  updateScrollCanvas(
    minDate,
    minDate + (visibleTimeEnd - visibleTimeStart)
  );
} else if (visibleTimeEnd > maxDate) {
  updateScrollCanvas(
    maxDate - (visibleTimeEnd - visibleTimeStart),
    maxDate
  );
} else {
  updateScrollCanvas(visibleTimeStart, visibleTimeEnd);
}

}; `

The results is bellow:

Anyone to help in this bug. I suggest pass the start and end datetime to limite the range view.

julhernandezv commented 1 year ago

Any workaround?, I have the same issue

rezgar commented 1 year ago

Here's a workaround based on the internal calculations:

onTimeChange={(visibleTimeStart, visibleTimeEnd, updateScrollCanvas) => {
    let zoom = visibleTimeEnd - visibleTimeStart; // buffer - 1 (1 is visible area) divided by 2 (2 is the buffer split on the right and left of the timeline)

    const minTimeAdjusted = minTime + zoom * (buffer - 1) / 2; // Hack to prevent scroll past the minTime
    const maxTimeAdjusted = maxTime - zoom * (buffer - 1) / 2; // Hack to prevent scroll past the maxTime

     if (visibleTimeStart < minTimeAdjusted && visibleTimeEnd > maxTimeAdjusted) {
         updateScrollCanvas(minTimeAdjusted, maxTimeAdjusted, true)
     } else if (visibleTimeStart < minTimeAdjusted) {
         updateScrollCanvas(minTimeAdjusted, minTimeAdjusted + zoom, true)
     } else if (visibleTimeEnd > maxTimeAdjusted) {
         updateScrollCanvas(maxTimeAdjusted - zoom, maxTimeAdjusted, true)
     } else {
         updateScrollCanvas(visibleTimeStart, visibleTimeEnd)
     }
}}
drugoi commented 1 year ago

Hey, @rezgar, what is the buffer in your workaround?

matbiernat commented 1 year ago

Well, how about fixed header then? @rezgar answer resolves the body issue but header is still not moving at all.