Skip to content

Commit 1f1f7d4

Browse files
LUUUAN晓峦
andauthored
fix: shift+wheel 横向滚动在云桌面环境下失效 (#3353)
云桌面系统已自动将 shift+wheel 转换为横向滚动(deltaX有值, deltaY为0), 但原逻辑依赖 isWindows() UA 判断,会错误地将有效的 deltaX 覆盖为 0。 改为基于实际事件值判断:仅当 deltaY 有值且 deltaX 为 0 时才手动转换, 兼容所有平台和云桌面环境。 Closes: #2198 Co-authored-by: 晓峦 <luanwendi.lwd@alibaba-inc.com>
1 parent 799cda5 commit 1f1f7d4

2 files changed

Lines changed: 69 additions & 18 deletions

File tree

‎packages/s2-core/__tests__/spreadsheet/scroll-spec.ts‎

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ describe('Scroll Tests', () => {
694694
).toBeFalsy();
695695
});
696696

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

724724
s2.on(S2Event.GLOBAL_SCROLL, onScroll);
725725

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

732-
Object.defineProperty(window.navigator, 'userAgent', {
733-
value: 'Windows',
734-
configurable: true,
735-
writable: true,
736-
});
737-
738733
canvas.dispatchEvent(wheelEvent);
739734
await sleep(200);
740735
expect(onScroll).toHaveBeenCalled();
741736
});
742737

743-
test('should not scroll horizontally when shift key is held on macOS', async () => {
738+
test('should scroll horizontally when shift key is held and system already converted to deltaX (cloud desktop / macOS behavior)', async () => {
739+
s2.setOptions({
740+
frozenRowHeader: true,
741+
style: {
742+
layoutWidthType: 'compact',
743+
rowCfg: {
744+
width: 200,
745+
},
746+
},
747+
});
748+
744749
const onScroll = jest.fn((...args) => {
745750
expect(args[0].rowHeaderScrollX).toBeGreaterThan(0);
746751
expect(args[0].scrollX).toBe(0);
747752
expect(args[0].scrollY).toBe(0);
748753
});
749754

755+
s2.changeSheetSize(400, 300);
756+
s2.render(false);
757+
758+
jest
759+
.spyOn(s2.facet, 'isScrollOverTheCornerArea')
760+
.mockImplementationOnce(() => true);
761+
jest
762+
.spyOn(s2.facet, 'isScrollOverTheViewport')
763+
.mockImplementationOnce(() => true);
764+
765+
s2.on(S2Event.GLOBAL_SCROLL, onScroll);
766+
767+
// 云桌面/macOS 行为: 系统已将 shift+wheel 转换为横向滚动, deltaX 有值, deltaY 为 0
750768
const wheelEvent = new WheelEvent('wheel', {
751-
deltaX: 0,
752-
deltaY: 20,
769+
deltaX: 100,
770+
deltaY: 0,
753771
shiftKey: true,
754772
});
755773

756-
Object.defineProperty(window.navigator, 'userAgent', {
757-
value: 'Mac OS',
758-
configurable: true,
759-
writable: true,
774+
canvas.dispatchEvent(wheelEvent);
775+
await sleep(200);
776+
expect(onScroll).toHaveBeenCalled();
777+
});
778+
779+
test('should not convert deltaX when system already handled shift+wheel conversion', async () => {
780+
s2.setOptions({
781+
frozenRowHeader: true,
782+
style: {
783+
layoutWidthType: 'compact',
784+
rowCfg: {
785+
width: 200,
786+
},
787+
},
760788
});
761789

790+
s2.changeSheetSize(400, 300);
791+
s2.render(false);
792+
793+
jest
794+
.spyOn(s2.facet, 'isScrollOverTheCornerArea')
795+
.mockImplementationOnce(() => true);
796+
jest
797+
.spyOn(s2.facet, 'isScrollOverTheViewport')
798+
.mockImplementationOnce(() => true);
799+
800+
// 云桌面环境: 系统已转换, deltaX=-100, deltaY=0
801+
// 不应该将 deltaX 覆盖为 0
802+
const wheelEvent = new WheelEvent('wheel', {
803+
deltaX: -100,
804+
deltaY: 0,
805+
shiftKey: true,
806+
});
807+
808+
const onScroll = jest.fn();
809+
810+
s2.on(S2Event.GLOBAL_SCROLL, onScroll);
762811
canvas.dispatchEvent(wheelEvent);
763812
await sleep(200);
764-
expect(onScroll).not.toHaveBeenCalled();
813+
expect(onScroll).toHaveBeenCalled();
765814
});
766815

767816
it('should not change init body overscrollBehavior style when render and destroyed', () => {

‎packages/s2-core/src/facet/base-facet.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import { getAdjustedRowScrollX, getAdjustedScrollOffset } from '../utils/facet';
5555
import { getAllChildCells } from '../utils/get-all-child-cells';
5656
import { getColsForGrid, getRowsForGrid } from '../utils/grid';
5757
import { diffPanelIndexes, type PanelIndexes } from '../utils/indexes';
58-
import { isMobile, isWindows } from '../utils/is-mobile';
58+
import { isMobile } from '../utils/is-mobile';
5959
import { CornerBBox } from './bbox/cornerBBox';
6060
import { PanelBBox } from './bbox/panelBBox';
6161
import {
@@ -986,9 +986,11 @@ export abstract class BaseFacet {
986986
let { deltaX, deltaY, offsetX, offsetY } = event;
987987
const { shiftKey } = event;
988988

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

0 commit comments

Comments
 (0)