Skip to content

Commit 41be3a1

Browse files
committed
refactor: last column fillhandler position
1 parent 3a529ef commit 41be3a1

4 files changed

Lines changed: 89 additions & 19 deletions

File tree

packages/vtable/examples/interactive/fill-handle.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,26 @@ export function createTable() {
8282
columns,
8383
widthMode: 'standard',
8484
excelOptions: {
85-
fillHandle: (col, row, table) => {
86-
// console.trace('fillHandle', col, row, table);
87-
if (col === 1) {
88-
return false;
85+
fillHandle: args => {
86+
const { selectRanges, table } = args;
87+
if (selectRanges.length === 1) {
88+
const { start, end } = selectRanges[0];
89+
console.log('fillHandle', start, end);
90+
const minCol = Math.min(start.col, end.col);
91+
const maxCol = Math.max(start.col, end.col);
92+
const minRow = Math.min(start.row, end.row);
93+
const maxRow = Math.max(start.row, end.row);
94+
//判断start到end 所有单元格有没有不能编辑的
95+
for (let col = minCol; col <= maxCol; col++) {
96+
for (let row = minRow; row <= maxRow; row++) {
97+
if (row === 2 && col === 2) {
98+
return false;
99+
}
100+
}
101+
}
102+
return true;
89103
}
90-
return true;
104+
return false;
91105
}
92106
}
93107
};

packages/vtable/src/event/event.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,17 @@ export class EventManager {
587587
}
588588
return false;
589589
}
590-
591590
checkCellFillhandle(eventArgsSet: SceneEvent, update?: boolean): boolean {
592-
if (this.table.options.excelOptions?.fillHandle) {
591+
let isFillHandle = false;
592+
if (typeof this.table.options.excelOptions?.fillHandle === 'function') {
593+
isFillHandle = this.table.options.excelOptions.fillHandle({
594+
selectRanges: this.table.stateManager.select.ranges,
595+
table: this.table
596+
});
597+
} else {
598+
isFillHandle = this.table.options.excelOptions?.fillHandle;
599+
}
600+
if (isFillHandle) {
593601
const { eventArgs } = eventArgsSet;
594602
if (eventArgs) {
595603
if (this.table.stateManager.select?.ranges?.length) {
@@ -601,11 +609,31 @@ export class EventManager {
601609
this.table.stateManager.select.ranges[this.table.stateManager.select.ranges.length - 1].start.row,
602610
this.table.stateManager.select.ranges[this.table.stateManager.select.ranges.length - 1].end.row
603611
);
604-
605-
const lastCellBound = this.table.scenegraph.highPerformanceGetCell(lastCol, lastRow).globalAABBBounds;
606-
// 计算鼠标与fillhandle矩形中心之间的距离
612+
const startCol = Math.min(
613+
this.table.stateManager.select.ranges[this.table.stateManager.select.ranges.length - 1].start.col,
614+
this.table.stateManager.select.ranges[this.table.stateManager.select.ranges.length - 1].end.col
615+
);
616+
const startRow = Math.min(
617+
this.table.stateManager.select.ranges[this.table.stateManager.select.ranges.length - 1].start.row,
618+
this.table.stateManager.select.ranges[this.table.stateManager.select.ranges.length - 1].end.row
619+
);
620+
// 计算鼠标与fillhandle矩形中心之间的距离 distanceX 和 distanceY
621+
// 考虑最后一行和最后一列的特殊情况
622+
let lastCellBound;
623+
if (lastCol < this.table.colCount - 1) {
624+
lastCellBound = this.table.scenegraph.highPerformanceGetCell(lastCol, lastRow).globalAABBBounds;
625+
} else {
626+
lastCellBound = this.table.scenegraph.highPerformanceGetCell(startCol - 1, lastRow).globalAABBBounds;
627+
}
607628
const distanceX = Math.abs(eventArgsSet.abstractPos.x - lastCellBound.x2);
629+
630+
if (lastRow < this.table.rowCount - 1) {
631+
lastCellBound = this.table.scenegraph.highPerformanceGetCell(lastCol, lastRow).globalAABBBounds;
632+
} else {
633+
lastCellBound = this.table.scenegraph.highPerformanceGetCell(lastCol, startRow - 1).globalAABBBounds;
634+
}
608635
const distanceY = Math.abs(eventArgsSet.abstractPos.y - lastCellBound.y2);
636+
609637
const squareSize = 6 * 3;
610638
// 判断鼠标是否落在fillhandle矩形内
611639
if (

packages/vtable/src/scenegraph/select/update-select-border.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ function updateComponent(
9494
computeRectCellRangeStartCol,
9595
computeRectCellRangeStartRow
9696
).globalAABBBounds;
97-
const lastCellBound = scene.highPerformanceGetCell(
98-
computeRectCellRangeEndCol,
99-
computeRectCellRangeEndRow
100-
).globalAABBBounds;
10197

10298
selectComp.rect.setAttributes({
10399
x: firstCellBound.x1 - scene.tableGroup.attribute.x, //坐标xy在下面的逻辑中会做适当调整
@@ -110,12 +106,44 @@ function updateComponent(
110106
const fillHandle = scene.table.options.excelOptions?.fillHandle;
111107
let visible = true;
112108
if (typeof fillHandle === 'function') {
113-
visible = fillHandle(endCol, endRow, scene.table);
109+
visible = fillHandle({ selectRanges: scene.table.stateManager.select.ranges, table: scene.table });
110+
}
111+
//#region 计算填充柄小方块的位置
112+
113+
let lastCellBound;
114+
//当选择区域没有到到最后一列时
115+
if (computeRectCellRangeEndCol < table.colCount - 1) {
116+
lastCellBound = scene.highPerformanceGetCell(
117+
computeRectCellRangeEndCol,
118+
computeRectCellRangeEndRow
119+
).globalAABBBounds;
120+
} else {
121+
// 最后一列
122+
lastCellBound = scene.highPerformanceGetCell(
123+
computeRectCellRangeStartCol - 1,
124+
computeRectCellRangeEndRow
125+
).globalAABBBounds;
114126
}
115-
// console.log('updateComponent', selectComp.fillhandle);
127+
const handlerX = lastCellBound.x2 - scene.tableGroup.attribute.x - 3;
128+
//当选择区域没有到到最后一行时
129+
if (computeRectCellRangeEndRow < table.rowCount - 1) {
130+
lastCellBound = scene.highPerformanceGetCell(
131+
computeRectCellRangeEndCol,
132+
computeRectCellRangeEndRow
133+
).globalAABBBounds;
134+
} else {
135+
// 最后一行
136+
lastCellBound = scene.highPerformanceGetCell(
137+
computeRectCellRangeEndCol,
138+
computeRectCellRangeStartRow - 1
139+
).globalAABBBounds;
140+
}
141+
const handlerY = lastCellBound.y2 - scene.tableGroup.attribute.y - 3;
142+
//#endregion
143+
116144
selectComp.fillhandle?.setAttributes({
117-
x: lastCellBound.x2 - scene.tableGroup.attribute.x - 3, // 调整小方块位置
118-
y: lastCellBound.y2 - scene.tableGroup.attribute.y - 3, // 调整小方块位置
145+
x: handlerX, // 调整小方块位置
146+
y: handlerY, // 调整小方块位置
119147
width: 6,
120148
height: 6,
121149
visible

packages/vtable/src/ts-types/base-table.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ export interface BaseTableConstructorOptions {
327327
/** 快捷键功能设置 */
328328
keyboardOptions?: TableKeyboardOptions;
329329
excelOptions?: {
330-
fillHandle?: boolean | ((col: number, row: number, table: BaseTableAPI) => boolean);
330+
fillHandle?: boolean | ((args: { selectRanges: CellRange[]; table: BaseTableAPI }) => boolean);
331331
};
332332
/** 事件触发相关设置 */
333333
eventOptions?: TableEventOptions;

0 commit comments

Comments
 (0)