Skip to content

Commit da00712

Browse files
authored
fix half block dragging (#6135)
1 parent 3c6006d commit da00712

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

web/ce/store/timeline/base-timeline.store.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export interface IBaseTimelineStore {
4444
updateActiveBlockId: (blockId: string | null) => void;
4545
updateRenderView: (data: any) => void;
4646
updateAllBlocksOnChartChangeWhileDragging: (addedWidth: number) => void;
47-
getUpdatedPositionAfterDrag: (id: string, ignoreDependencies?: boolean) => IBlockUpdateDependencyData[];
47+
getUpdatedPositionAfterDrag: (
48+
id: string,
49+
shouldUpdateHalfBlock: boolean,
50+
ignoreDependencies?: boolean
51+
) => IBlockUpdateDependencyData[];
4852
updateBlockPosition: (id: string, deltaLeft: number, deltaWidth: number, ignoreDependencies?: boolean) => void;
4953
getNumberOfDaysFromPosition: (position: number | undefined) => number | undefined;
5054
setIsDragging: (isDragging: boolean) => void;
@@ -271,24 +275,30 @@ export class BaseTimeLineStore implements IBaseTimelineStore {
271275
/**
272276
* returns updates dates of blocks post drag.
273277
* @param id
278+
* @param shouldUpdateHalfBlock if is a half block then update the incomplete block only if this is true
274279
* @returns
275280
*/
276-
getUpdatedPositionAfterDrag = action((id: string) => {
281+
getUpdatedPositionAfterDrag = action((id: string, shouldUpdateHalfBlock: boolean) => {
277282
const currBlock = this.blocksMap[id];
278283

279284
if (!currBlock?.position || !this.currentViewData) return [];
280285

281-
return [
282-
{
283-
id,
284-
start_date: renderFormattedPayloadDate(
285-
getDateFromPositionOnGantt(currBlock.position.marginLeft, this.currentViewData)
286-
),
287-
target_date: renderFormattedPayloadDate(
288-
getDateFromPositionOnGantt(currBlock.position.marginLeft + currBlock.position.width, this.currentViewData, -1)
289-
),
290-
},
291-
] as IBlockUpdateDependencyData[];
286+
const updatePayload: IBlockUpdateDependencyData = { id };
287+
288+
// If shouldUpdateHalfBlock or the start date is available then update start date
289+
if (shouldUpdateHalfBlock || currBlock.start_date) {
290+
updatePayload.start_date = renderFormattedPayloadDate(
291+
getDateFromPositionOnGantt(currBlock.position.marginLeft, this.currentViewData)
292+
);
293+
}
294+
// If shouldUpdateHalfBlock or the target date is available then update target date
295+
if (shouldUpdateHalfBlock || currBlock.target_date) {
296+
updatePayload.target_date = renderFormattedPayloadDate(
297+
getDateFromPositionOnGantt(currBlock.position.marginLeft + currBlock.position.width, this.currentViewData, -1)
298+
);
299+
}
300+
301+
return [updatePayload];
292302
});
293303

294304
/**

web/core/components/gantt-chart/helpers/blockResizables/use-gantt-resizable.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { setToast } from "@plane/ui";
44
// hooks
55
import { useTimeLineChartStore } from "@/hooks/use-timeline-chart";
66
//
7-
import { SIDEBAR_WIDTH } from "../../constants";
7+
import { DEFAULT_BLOCK_WIDTH, SIDEBAR_WIDTH } from "../../constants";
88
import { IBlockUpdateDependencyData, IGanttBlock } from "../../types";
99

1010
export const useGanttResizable = (
@@ -75,10 +75,19 @@ export const useGanttResizable = (
7575
const prevWidth = parseFloat(resizableDiv.style.width.slice(0, -2));
7676
// calculate new width
7777
const marginDelta = prevMarginLeft - marginLeft;
78-
width = prevWidth + marginDelta;
78+
// If target date does not exist while dragging with left handle the revert to default width
79+
width = block.target_date ? prevWidth + marginDelta : DEFAULT_BLOCK_WIDTH;
7980
} else if (dragDirection === "right") {
8081
// calculate new width and update the initialMarginLeft using +=
8182
width = Math.round(mouseX / dayWidth) * dayWidth - marginLeft;
83+
84+
// If start date does not exist while dragging with right handle the revert to default width and adjust marginLeft accordingly
85+
if (!block.start_date) {
86+
// calculate new right and update the marginLeft to the newly calculated one
87+
const marginRight = Math.round(mouseX / dayWidth) * dayWidth;
88+
marginLeft = marginRight - DEFAULT_BLOCK_WIDTH;
89+
width = DEFAULT_BLOCK_WIDTH;
90+
}
8291
} else if (dragDirection === "move") {
8392
// calculate new marginLeft and update the initial marginLeft using -=
8493
marginLeft = Math.round((mouseX - initialPositionRef.current.offsetX) / dayWidth) * dayWidth;
@@ -105,8 +114,12 @@ export const useGanttResizable = (
105114
ganttContainerElement.removeEventListener("scroll", handleOnScroll);
106115
document.removeEventListener("mouseup", handleMouseUp);
107116

117+
// update half blocks only when the missing side of the block is directly dragged
118+
const shouldUpdateHalfBlock =
119+
(dragDirection === "left" && !block.start_date) || (dragDirection === "right" && !block.target_date);
120+
108121
try {
109-
const blockUpdates = getUpdatedPositionAfterDrag(block.id, dragDirection !== "move");
122+
const blockUpdates = getUpdatedPositionAfterDrag(block.id, shouldUpdateHalfBlock, dragDirection !== "move");
110123
updateBlockDates && updateBlockDates(blockUpdates);
111124
} catch (e) {
112125
setToast;

web/core/components/gantt-chart/views/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export const getItemPositionWidth = (chartData: ChartDataType, itemData: IGanttB
9999
// get scroll position from the number of days and width of each day
100100
scrollPosition = itemStartDate
101101
? getPositionFromDate(chartData, itemStartDate, 0)
102-
: getPositionFromDate(chartData, itemTargetDate!, -1 * DEFAULT_BLOCK_WIDTH);
102+
: getPositionFromDate(chartData, itemTargetDate!, -1 * DEFAULT_BLOCK_WIDTH + chartData.data.dayWidth);
103103

104104
if (itemStartDate && itemTargetDate) {
105105
// get width of block

0 commit comments

Comments
 (0)