Skip to content

Commit dce82b4

Browse files
authored
Merge pull request #3597 from VisActor/3582-feature-fillHandle-function
3582 feature fill handle function
2 parents fd40523 + 41be3a1 commit dce82b4

5 files changed

Lines changed: 107 additions & 14 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "refactor: fillHandle function #3582\n\n",
5+
"type": "none",
6+
"packageName": "@visactor/vtable"
7+
}
8+
],
9+
"packageName": "@visactor/vtable",
10+
"email": "892739385@qq.com"
11+
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,27 @@ export function createTable() {
8282
columns,
8383
widthMode: 'standard',
8484
excelOptions: {
85-
fillHandle: true
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;
103+
}
104+
return false;
105+
}
86106
}
87107
};
88108
const tableInstance = new VTable.ListTable(document.getElementById(CONTAINER_ID), option);

packages/vtable/src/event/event.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,17 @@ export class EventManager {
530530
}
531531
return false;
532532
}
533-
534533
checkCellFillhandle(eventArgsSet: SceneEvent, update?: boolean): boolean {
535-
if (this.table.options.excelOptions?.fillHandle) {
534+
let isFillHandle = false;
535+
if (typeof this.table.options.excelOptions?.fillHandle === 'function') {
536+
isFillHandle = this.table.options.excelOptions.fillHandle({
537+
selectRanges: this.table.stateManager.select.ranges,
538+
table: this.table
539+
});
540+
} else {
541+
isFillHandle = this.table.options.excelOptions?.fillHandle;
542+
}
543+
if (isFillHandle) {
536544
const { eventArgs } = eventArgsSet;
537545
if (eventArgs) {
538546
if (this.table.stateManager.select?.ranges?.length) {
@@ -544,11 +552,31 @@ export class EventManager {
544552
this.table.stateManager.select.ranges[this.table.stateManager.select.ranges.length - 1].start.row,
545553
this.table.stateManager.select.ranges[this.table.stateManager.select.ranges.length - 1].end.row
546554
);
547-
548-
const lastCellBound = this.table.scenegraph.highPerformanceGetCell(lastCol, lastRow).globalAABBBounds;
549-
// 计算鼠标与fillhandle矩形中心之间的距离
555+
const startCol = Math.min(
556+
this.table.stateManager.select.ranges[this.table.stateManager.select.ranges.length - 1].start.col,
557+
this.table.stateManager.select.ranges[this.table.stateManager.select.ranges.length - 1].end.col
558+
);
559+
const startRow = Math.min(
560+
this.table.stateManager.select.ranges[this.table.stateManager.select.ranges.length - 1].start.row,
561+
this.table.stateManager.select.ranges[this.table.stateManager.select.ranges.length - 1].end.row
562+
);
563+
// 计算鼠标与fillhandle矩形中心之间的距离 distanceX 和 distanceY
564+
// 考虑最后一行和最后一列的特殊情况
565+
let lastCellBound;
566+
if (lastCol < this.table.colCount - 1) {
567+
lastCellBound = this.table.scenegraph.highPerformanceGetCell(lastCol, lastRow).globalAABBBounds;
568+
} else {
569+
lastCellBound = this.table.scenegraph.highPerformanceGetCell(startCol - 1, lastRow).globalAABBBounds;
570+
}
550571
const distanceX = Math.abs(eventArgsSet.abstractPos.x - lastCellBound.x2);
572+
573+
if (lastRow < this.table.rowCount - 1) {
574+
lastCellBound = this.table.scenegraph.highPerformanceGetCell(lastCol, lastRow).globalAABBBounds;
575+
} else {
576+
lastCellBound = this.table.scenegraph.highPerformanceGetCell(lastCol, startRow - 1).globalAABBBounds;
577+
}
551578
const distanceY = Math.abs(eventArgsSet.abstractPos.y - lastCellBound.y2);
579+
552580
const squareSize = 6 * 3;
553581
// 判断鼠标是否落在fillhandle矩形内
554582
if (

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

Lines changed: 41 additions & 7 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在下面的逻辑中会做适当调整
@@ -107,12 +103,50 @@ function updateComponent(
107103
visible: true
108104
});
109105
if (selectComp.fillhandle) {
106+
const fillHandle = scene.table.options.excelOptions?.fillHandle;
107+
let visible = true;
108+
if (typeof fillHandle === 'function') {
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;
126+
}
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+
110144
selectComp.fillhandle?.setAttributes({
111-
x: lastCellBound.x2 - scene.tableGroup.attribute.x - 3, // 调整小方块位置
112-
y: lastCellBound.y2 - scene.tableGroup.attribute.y - 3, // 调整小方块位置
145+
x: handlerX, // 调整小方块位置
146+
y: handlerY, // 调整小方块位置
113147
width: 6,
114148
height: 6,
115-
visible: true
149+
visible
116150
});
117151
}
118152

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;
330+
fillHandle?: boolean | ((args: { selectRanges: CellRange[]; table: BaseTableAPI }) => boolean);
331331
};
332332
/** 事件触发相关设置 */
333333
eventOptions?: TableEventOptions;

0 commit comments

Comments
 (0)