Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): 使用客户端的位置来计算调整大小的偏移量 #2273

Merged
merged 3 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/s2-core/__tests__/spreadsheet/table-sheet-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,15 @@ describe('TableSheet normal spec', () => {

await sleep(30);

let columnNodes = s2.getColumnNodes();
let lastColumnCell = columnNodes[columnNodes.length - 1]
.belongsCell as ColCell;
const startCellWidth = lastColumnCell.getMeta().width;

const { x, width, top } = s2.getCanvasElement().getBoundingClientRect();
s2.getCanvasElement().dispatchEvent(
new MouseEvent('mousedown', {
clientX: x + width - 1,
clientX: x + width,
clientY: top + 25,
}),
);
Expand All @@ -207,11 +212,11 @@ describe('TableSheet normal spec', () => {

await sleep(300);

const columnNodes = s2.getColumnNodes();
const lastColumnCell = columnNodes[columnNodes.length - 1]
.belongsCell as ColCell;
columnNodes = s2.getColumnNodes();
lastColumnCell = columnNodes[columnNodes.length - 1].belongsCell as ColCell;
const endCellWidth = lastColumnCell.getMeta().width;

expect(lastColumnCell.getMeta().width).toBe(199);
expect(endCellWidth - startCellWidth).toBe(100);
});

test('should render link shape', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,16 @@ describe('Interaction Row Column Resize Tests', () => {
{
offsetX: 10,
offsetY: 20,
clientX: 10,
clientY: 20,
},
resizeInfo,
);

expect(s2.store.get('resized')).toBeFalsy();
expect(rowColumnResizeInstance.resizeStartPosition).toStrictEqual({
offsetX: 10,
clientX: 10,
});
expect(getStartGuideLine().attr('path')).toStrictEqual([
['M', 2, 2],
Expand Down Expand Up @@ -312,12 +315,15 @@ describe('Interaction Row Column Resize Tests', () => {
{
offsetX: 10,
offsetY: 20,
clientX: 10,
clientY: 20,
},
resizeInfo,
);

expect(rowColumnResizeInstance.resizeStartPosition).toStrictEqual({
offsetY: 20,
clientY: 20,
});
expect(getStartGuideLine().attr('path')).toStrictEqual([
['M', 2, 2],
Expand Down Expand Up @@ -605,6 +611,8 @@ describe('Interaction Row Column Resize Tests', () => {
{
offsetX: 10,
offsetY: 20,
clientX: 10,
clientY: 20,
},
resizeInfo,
);
Expand All @@ -614,6 +622,8 @@ describe('Interaction Row Column Resize Tests', () => {
{
offsetX: 20,
offsetY: 20,
clientX: 20,
clientY: 20,
},
resizeInfo,
);
Expand Down
2 changes: 2 additions & 0 deletions packages/s2-core/src/common/interface/resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export interface ResizeGuideLinePosition {
export interface ResizePosition {
offsetX?: number;
offsetY?: number;
clientX?: number;
clientY?: number;
}

export interface ResizeDetail {
Expand Down
12 changes: 7 additions & 5 deletions packages/s2-core/src/interaction/row-column-resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import {
InterceptType,
MIN_CELL_HEIGHT,
MIN_CELL_WIDTH,
ResizeAreaEffect,
ResizeDirectionType,
ResizeType,
RESIZE_END_GUIDE_LINE_ID,
RESIZE_MASK_ID,
RESIZE_START_GUIDE_LINE_ID,
ResizeAreaEffect,
ResizeDirectionType,
ResizeType,
S2Event,
} from '../common/constant';
import type {
Expand Down Expand Up @@ -137,6 +137,7 @@ export class RowColumnResize extends BaseEvent implements BaseEventImplement {
['L', offsetX + width, guideLineMaxHeight],
]);
this.resizeStartPosition.offsetX = event.offsetX;
this.resizeStartPosition.clientX = event.clientX;
return;
}

Expand All @@ -149,6 +150,7 @@ export class RowColumnResize extends BaseEvent implements BaseEventImplement {
['L', guideLineMaxWidth, offsetY + height],
]);
this.resizeStartPosition.offsetY = event.offsetY;
this.resizeStartPosition.clientY = event.clientY;
}

private bindMouseDown() {
Expand Down Expand Up @@ -425,7 +427,7 @@ export class RowColumnResize extends BaseEvent implements BaseEventImplement {
guideLineStart: ResizeGuideLinePath,
guideLineEnd: ResizeGuideLinePath,
) {
let offsetX = originalEvent.offsetX - this.resizeStartPosition.offsetX;
let offsetX = originalEvent.clientX - this.resizeStartPosition.clientX;
if (resizeInfo.width + offsetX < MIN_CELL_WIDTH) {
// 禁止拖到最小宽度
offsetX = -(resizeInfo.width - MIN_CELL_WIDTH);
Expand All @@ -447,7 +449,7 @@ export class RowColumnResize extends BaseEvent implements BaseEventImplement {
guideLineStart: ResizeGuideLinePath,
guideLineEnd: ResizeGuideLinePath,
) {
let offsetY = originalEvent.offsetY - this.resizeStartPosition.offsetY;
let offsetY = originalEvent.clientY - this.resizeStartPosition.clientY;

if (resizeInfo.height + offsetY < MIN_CELL_HEIGHT) {
offsetY = -(resizeInfo.height - MIN_CELL_HEIGHT);
Expand Down