Skip to content

Commit adf4bcd

Browse files
authored
fix(gantt): 整体拖动工作项时间,会自动增加一天 (#602)
1 parent cd28573 commit adf4bcd

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

packages/gantt/src/components/bar/bar-drag.ts

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { DragDrop, DragRef } from '@angular/cdk/drag-drop';
22
import { effect, ElementRef, Injectable, NgZone, OnDestroy, signal, WritableSignal } from '@angular/core';
33
import { animationFrameScheduler, fromEvent, interval, Subject } from 'rxjs';
44
import { takeUntil } from 'rxjs/operators';
5-
import { GanttViewType } from '../../class';
65
import { GanttItemInternal } from '../../class/item';
76
import { GanttLinkType } from '../../class/link';
87
import { GanttDomService } from '../../gantt-dom.service';
@@ -376,35 +375,13 @@ export class GanttBarDrag implements OnDestroy {
376375

377376
private barDragMove() {
378377
const currentX = this.item().refs.x + this.barDragMoveDistance + this.dragScrollDistance;
379-
const currentDate = this.ganttUpper.view.getDateByXPoint(currentX);
380-
const currentStartX = this.ganttUpper.view.getXPointByDate(currentDate);
381-
const currentEndDate = this.ganttUpper.view.getDateByXPoint(currentX + this.item().refs.width);
382-
383-
const diffs = this.ganttUpper.view.differenceByPrecisionUnit(currentEndDate, currentDate);
384-
385-
let start = currentDate;
386-
let end = currentDate.add(diffs, this.ganttUpper.view?.options?.datePrecisionUnit);
387-
388-
// 日视图特殊逻辑处理
389-
if (this.ganttUpper.view.viewType === GanttViewType.day) {
390-
const dayWidth = this.ganttUpper.view.getDayOccupancyWidth(currentDate);
391-
if (currentX > currentStartX + dayWidth / 2) {
392-
start = start.addDays(1);
393-
end = end.addDays(1);
394-
}
395-
}
396-
378+
const start = this.ganttUpper.view.getDateByXPoint(currentX);
379+
const end = this.ganttUpper.view.getEndDateByWidth(start, this.item().refs.width);
397380
if (this.dragScrolling) {
398381
const left = currentX - this.barDragMoveDistance;
399382
this.barElement.style.left = left + 'px';
400383
}
401-
402-
this.openDragBackdrop(
403-
this.barElement,
404-
this.ganttUpper.view.getDateByXPoint(currentX),
405-
this.ganttUpper.view.getDateByXPoint(currentX + this.item().refs.width)
406-
);
407-
384+
this.openDragBackdrop(this.barElement, start, end);
408385
if (!this.isStartOrEndInsideView(start, end)) {
409386
return;
410387
}

packages/gantt/src/views/day.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,30 @@ export class GanttViewDay extends GanttView {
8585
}
8686
return points;
8787
}
88+
89+
override getEndDateByWidth(start: GanttDate, width: number): GanttDate {
90+
if (!this.options.hoilday?.hideHoliday) {
91+
return super.getEndDateByWidth(start, width);
92+
}
93+
// 通过宽度计算包含多少个工作日(每个工作日的宽度是 cellWidth)
94+
const workingDaysCount = Math.round(width / this.getCellWidth());
95+
let end = start;
96+
let addedWorkingDays = 0;
97+
let attempts = 0;
98+
const maxAttempts = 365;
99+
100+
while (addedWorkingDays < workingDaysCount && attempts < maxAttempts) {
101+
if (this.getDayOccupancyWidth(end) > 0) {
102+
addedWorkingDays++;
103+
if (addedWorkingDays < workingDaysCount) {
104+
end = end.addDays(1);
105+
}
106+
} else {
107+
end = end.addDays(1);
108+
}
109+
attempts++;
110+
}
111+
112+
return end;
113+
}
88114
}

packages/gantt/src/views/view.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,17 @@ export abstract class GanttView {
268268
return this.getDayOccupancyWidth(date);
269269
}
270270
}
271+
272+
/**
273+
* 根据起始日期和宽度计算结束日期
274+
* @param start 起始日期
275+
* @param width 视觉宽度
276+
* @returns 结束日期
277+
*/
278+
getEndDateByWidth(start: GanttDate, width: number): GanttDate {
279+
const startX = this.getXPointByDate(start);
280+
// 减去偏移量避免因为 getDateRangeWidth 加了 1 秒而多算一天
281+
const endX = startX + width - 0.1;
282+
return this.getDateByXPoint(endX);
283+
}
271284
}

0 commit comments

Comments
 (0)