Skip to content

Commit

Permalink
fix: fix cell position type problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Rui-Sun committed Jun 24, 2024
1 parent ff6366a commit 85133bd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 178 deletions.
171 changes: 4 additions & 167 deletions packages/vtable/src/core/BaseTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2511,67 +2511,7 @@ export abstract class BaseTable extends EventTarget implements BaseTableAPI {
* @returns
*/
getTargetColAt(absoluteX: number): ColumnInfo | null {
if (absoluteX === 0) {
return { left: 0, col: 0, right: 0, width: 0 };
}
const findBefore = (
startCol: number,
startRight: number
): {
left: number;
col: number;
right: number;
width: number;
} | null => {
let right = startRight;
for (let col = startCol; col >= 0; col--) {
const width = this.getColWidth(col);
const left = right - width;
if (Math.round(left) <= Math.round(absoluteX) && Math.round(absoluteX) < Math.round(right)) {
return {
left,
col,
right,
width
};
}
right = left;
}
return null;
};
const findAfter = (
startCol: number,
startRight: number
): {
left: number;
col: number;
right: number;
width: number;
} | null => {
let left = startRight - this.getColWidth(startCol);
const { colCount } = this.internalProps;
for (let col = startCol; col < colCount; col++) {
const width = this.getColWidth(col);
const right = left + width;
if (Math.round(left) <= Math.round(absoluteX) && Math.round(absoluteX) < Math.round(right)) {
return {
left,
col,
right,
width
};
}
left = right;
}
return null;
};
//计算这个位置处是第几行
const candCol = this.computeTargetColByX(absoluteX);
const right = this.getColsWidth(0, candCol);
if (absoluteX >= right) {
return findAfter(candCol, right);
}
return findBefore(candCol, right);
return getTargetColAt(absoluteX, this);
}
/**
* 根据y获取该位置所处行值
Expand All @@ -2580,72 +2520,7 @@ export abstract class BaseTable extends EventTarget implements BaseTableAPI {
* @returns
*/
getTargetRowAt(absoluteY: number): RowInfo | null {
if (absoluteY === 0) {
return { top: 0, row: 0, bottom: 0, height: 0 };
}

const findBefore = (
startRow: number,
startBottom: number
): {
top: number;
row: number;
bottom: number;
height: number;
} | null => {
let bottom = startBottom;
for (let row = startRow; row >= 0; row--) {
const height = this.getRowHeight(row);
const top = bottom - height;
if (Math.round(top) <= Math.round(absoluteY) && Math.round(absoluteY) < Math.round(bottom)) {
return {
top,
row,
bottom,
height
};
}
bottom = top;
}
return null;
};
const findAfter = (
startRow: number,
startBottom: number
): {
top: number;
row: number;
bottom: number;
height: number;
} | null => {
let top = startBottom - this.getRowHeight(startRow);
const { rowCount } = this.internalProps;
for (let row = startRow; row < rowCount; row++) {
const height = this.getRowHeight(row);
const bottom = top + height;
if (Math.round(top) <= Math.round(absoluteY) && Math.round(absoluteY) < Math.round(bottom)) {
return {
top,
row,
bottom,
height
};
}
top = bottom;
}
return null;
};
// const candRow = Math.min(
// Math.ceil(absoluteY / this.internalProps.defaultRowHeight),
// this.rowCount - 1
// );
//计算这个位置处是第几行
const candRow = this.computeTargetRowByY(absoluteY);
const bottom = this.getRowsHeight(0, candRow);
if (absoluteY >= bottom) {
return findAfter(candRow, bottom);
}
return findBefore(candRow, bottom);
return getTargetRowAt(absoluteY, this);
}

/**
Expand All @@ -2655,26 +2530,7 @@ export abstract class BaseTable extends EventTarget implements BaseTableAPI {
* @returns
*/
getTargetColAtConsiderRightFrozen(absoluteX: number, isConsider: boolean): ColumnInfo | null {
if (absoluteX === 0) {
return { left: 0, col: 0, right: 0, width: 0 };
}
if (
isConsider &&
absoluteX > this.tableNoFrameWidth - this.getRightFrozenColsWidth() &&
absoluteX < this.tableNoFrameWidth
) {
for (let i = 0; i < this.rightFrozenColCount; i++) {
if (absoluteX > this.tableNoFrameWidth - this.getColsWidth(this.colCount - i - 1, this.colCount - 1)) {
return {
col: this.colCount - i - 1,
left: undefined,
right: undefined,
width: undefined
};
}
}
}
return this.getTargetColAt(absoluteX);
return getTargetColAtConsiderRightFrozen(absoluteX, isConsider, this);
}

/**
Expand All @@ -2684,26 +2540,7 @@ export abstract class BaseTable extends EventTarget implements BaseTableAPI {
* @returns
*/
getTargetRowAtConsiderBottomFrozen(absoluteY: number, isConsider: boolean): RowInfo | null {
if (absoluteY === 0) {
return { top: 0, row: 0, bottom: 0, height: 0 };
}
if (
isConsider &&
absoluteY > this.tableNoFrameHeight - this.getBottomFrozenRowsHeight() &&
absoluteY < this.tableNoFrameHeight
) {
for (let i = 0; i < this.rightFrozenColCount; i++) {
if (absoluteY > this.tableNoFrameHeight - this.getRowsHeight(this.rowCount - i - 1, this.rowCount - 1)) {
return {
row: this.rowCount - i - 1,
top: undefined,
bottom: undefined,
height: undefined
};
}
}
}
return this.getTargetRowAt(absoluteY);
return getTargetRowAtConsiderBottomFrozen(absoluteY, isConsider, this);
}

/**
Expand Down
16 changes: 5 additions & 11 deletions packages/vtable/src/core/utils/get-cell-position.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CellAddressWithBound } from '../../ts-types';
import type { CellAddressWithBound, ColumnInfo, RowInfo } from '../../ts-types';
import type { BaseTableAPI } from '../../ts-types/base-table';
import { _getTargetFrozenColAt, _getTargetFrozenRowAt } from '../tableHelper';

Expand Down Expand Up @@ -82,10 +82,7 @@ export function getCellAt(absoluteX: number, absoluteY: number, _this: BaseTable
* @param absoluteX
* @returns
*/
export function getTargetColAt(
absoluteX: number,
_this: BaseTableAPI
): { col: number; left: number; right: number; width: number } | null {
export function getTargetColAt(absoluteX: number, _this: BaseTableAPI): ColumnInfo | null {
if (absoluteX === 0) {
return { left: 0, col: 0, right: 0, width: 0 };
}
Expand Down Expand Up @@ -154,10 +151,7 @@ export function getTargetColAt(
* @param absoluteX
* @returns
*/
export function getTargetRowAt(
absoluteY: number,
_this: BaseTableAPI
): { row: number; top: number; bottom: number; height: number } | null {
export function getTargetRowAt(absoluteY: number, _this: BaseTableAPI): RowInfo | null {
if (absoluteY === 0) {
return { top: 0, row: 0, bottom: 0, height: 0 };
}
Expand Down Expand Up @@ -236,7 +230,7 @@ export function getTargetColAtConsiderRightFrozen(
absoluteX: number,
isConsider: boolean,
_this: BaseTableAPI
): { col: number; left: number; right: number; width: number } | null {
): ColumnInfo | null {
if (absoluteX === 0) {
return { left: 0, col: 0, right: 0, width: 0 };
}
Expand Down Expand Up @@ -269,7 +263,7 @@ export function getTargetRowAtConsiderBottomFrozen(
absoluteY: number,
isConsider: boolean,
_this: BaseTableAPI
): { row: number; top: number; bottom: number; height: number } | null {
): RowInfo | null {
if (absoluteY === 0) {
return { top: 0, row: 0, bottom: 0, height: 0 };
}
Expand Down

0 comments on commit 85133bd

Please sign in to comment.