Skip to content
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
79 changes: 64 additions & 15 deletions packages/s2-core/__tests__/spreadsheet/scroll-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ describe('Scroll Tests', () => {
).toBeFalsy();
});

test('should scroll horizontally when shift key is held on Windows', async () => {
test('should scroll horizontally when shift key is held and deltaY has value (Windows behavior)', async () => {
s2.setOptions({
frozenRowHeader: true,
style: {
Expand Down Expand Up @@ -723,45 +723,94 @@ describe('Scroll Tests', () => {

s2.on(S2Event.GLOBAL_SCROLL, onScroll);

// Windows 行为: shift+wheel 时系统不转换方向, deltaY 有值, deltaX 为 0
const wheelEvent = new WheelEvent('wheel', {
deltaX: 0,
deltaY: 20,
shiftKey: true,
});

Object.defineProperty(window.navigator, 'userAgent', {
value: 'Windows',
configurable: true,
writable: true,
});

canvas.dispatchEvent(wheelEvent);
await sleep(200);
expect(onScroll).toHaveBeenCalled();
});

test('should not scroll horizontally when shift key is held on macOS', async () => {
test('should scroll horizontally when shift key is held and system already converted to deltaX (cloud desktop / macOS behavior)', async () => {
s2.setOptions({
frozenRowHeader: true,
style: {
layoutWidthType: 'compact',
rowCfg: {
width: 200,
},
},
});

const onScroll = jest.fn((...args) => {
expect(args[0].rowHeaderScrollX).toBeGreaterThan(0);
expect(args[0].scrollX).toBe(0);
expect(args[0].scrollY).toBe(0);
});

s2.changeSheetSize(400, 300);
s2.render(false);

jest
.spyOn(s2.facet, 'isScrollOverTheCornerArea')
.mockImplementationOnce(() => true);
jest
.spyOn(s2.facet, 'isScrollOverTheViewport')
.mockImplementationOnce(() => true);

s2.on(S2Event.GLOBAL_SCROLL, onScroll);

// 云桌面/macOS 行为: 系统已将 shift+wheel 转换为横向滚动, deltaX 有值, deltaY 为 0
const wheelEvent = new WheelEvent('wheel', {
deltaX: 0,
deltaY: 20,
deltaX: 100,
deltaY: 0,
shiftKey: true,
});

Object.defineProperty(window.navigator, 'userAgent', {
value: 'Mac OS',
configurable: true,
writable: true,
canvas.dispatchEvent(wheelEvent);
await sleep(200);
expect(onScroll).toHaveBeenCalled();
});

test('should not convert deltaX when system already handled shift+wheel conversion', async () => {
s2.setOptions({
frozenRowHeader: true,
style: {
layoutWidthType: 'compact',
rowCfg: {
width: 200,
},
},
});

s2.changeSheetSize(400, 300);
s2.render(false);

jest
.spyOn(s2.facet, 'isScrollOverTheCornerArea')
.mockImplementationOnce(() => true);
jest
.spyOn(s2.facet, 'isScrollOverTheViewport')
.mockImplementationOnce(() => true);

// 云桌面环境: 系统已转换, deltaX=-100, deltaY=0
// 不应该将 deltaX 覆盖为 0
const wheelEvent = new WheelEvent('wheel', {
deltaX: -100,
deltaY: 0,
shiftKey: true,
});

const onScroll = jest.fn();

s2.on(S2Event.GLOBAL_SCROLL, onScroll);
canvas.dispatchEvent(wheelEvent);
await sleep(200);
expect(onScroll).not.toHaveBeenCalled();
expect(onScroll).toHaveBeenCalled();
});

it('should not change init body overscrollBehavior style when render and destroyed', () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/s2-core/src/facet/base-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import { getAdjustedRowScrollX, getAdjustedScrollOffset } from '../utils/facet';
import { getAllChildCells } from '../utils/get-all-child-cells';
import { getColsForGrid, getRowsForGrid } from '../utils/grid';
import { diffPanelIndexes, type PanelIndexes } from '../utils/indexes';
import { isMobile, isWindows } from '../utils/is-mobile';
import { isMobile } from '../utils/is-mobile';
import { CornerBBox } from './bbox/cornerBBox';
import { PanelBBox } from './bbox/panelBBox';
import {
Expand Down Expand Up @@ -986,9 +986,11 @@ export abstract class BaseFacet {
let { deltaX, deltaY, offsetX, offsetY } = event;
const { shiftKey } = event;

// Windows 环境,按住 shift 时,固定为水平方向滚动,macOS 环境默认有该行为
// 按住 shift 时,固定为水平方向滚动
// macOS 环境和部分云桌面环境,系统会自动将 shift+wheel 转换为横向滚动 (deltaX 有值, deltaY 为 0),无需手动处理
// Windows 环境下系统不会自动转换 (deltaY 有值, deltaX 为 0),需要手动将 deltaY 转为 deltaX
// see https://github.com/antvis/S2/issues/2198
if (shiftKey && isWindows()) {
if (shiftKey && deltaY !== 0 && deltaX === 0) {
offsetX = offsetX - deltaX + deltaY;
deltaX = deltaY;
offsetY -= deltaY;
Expand Down
Loading