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: 修复表格右键事件无法触发 close #2687 #2690

Merged
merged 1 commit into from
Apr 30, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
sleep,
} from 'tests/util/helpers';
import { Renderer } from '@antv/g-canvas';
import { GEventType, GuiIcon } from '@/common';
import { GuiIcon } from '@/common';
import type { EmitterType } from '@/common/interface/emitter';
import {
CellType,
Expand Down Expand Up @@ -79,10 +79,7 @@ describe('Interaction Event Controller Tests', () => {
};

const expectEvents =
(
eventType: OriginEventType | GEventType,
customEvent?: (evt: FederatedEvent) => void,
) =>
(eventType: OriginEventType, customEvent?: (evt: FederatedEvent) => void) =>
(options: { eventNames: (keyof EmitterType)[]; type: CellType }) => {
const { eventNames, type } = options;

Expand Down Expand Up @@ -176,7 +173,7 @@ describe('Interaction Event Controller Tests', () => {
OriginEventType.POINTER_MOVE,
OriginEventType.POINTER_UP,
OriginEventType.MOUSE_OUT,
GEventType.RIGHT_MOUSE_UP,
OriginEventType.RIGHT_DOWN,
OriginEventType.CLICK,
OriginEventType.TOUCH_START,
];
Expand Down Expand Up @@ -388,7 +385,7 @@ describe('Interaction Event Controller Tests', () => {
},
])(
'should emit context menu event for %s',
expectEvents(GEventType.RIGHT_MOUSE_UP),
expectEvents(OriginEventType.RIGHT_DOWN),
);

test('should emit global context menu event', () => {
Expand All @@ -398,7 +395,7 @@ describe('Interaction Event Controller Tests', () => {

const evt = createFederatedPointerEvent(
spreadsheet,
GEventType.RIGHT_MOUSE_UP,
OriginEventType.RIGHT_DOWN,
);

evt.client.x = MOCK_CLIENT.x;
Expand Down
3 changes: 0 additions & 3 deletions packages/s2-core/src/common/constant/events/g.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/s2-core/src/common/constant/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './interaction';
export * from './origin';
export * from './basic';
export * from './g';
5 changes: 5 additions & 0 deletions packages/s2-core/src/common/constant/events/origin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @see https://g.antv.antgroup.com/api/event/intro
*/
export enum OriginEventType {
MOUSE_DOWN = 'mousedown',
MOUSE_MOVE = 'mousemove',
Expand All @@ -19,4 +22,6 @@ export enum OriginEventType {
POINTER_CANCEL = 'pointercancel',
POINTER_ENTER = 'pointerenter',
POINTER_OVER = 'pointerover',
RIGHT_DOWN = 'rightdown',
RIGHT_UP = 'rightup',
}
9 changes: 6 additions & 3 deletions packages/s2-core/src/interaction/event-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { each, get, hasIn, isEmpty, isNil } from 'lodash';
import { GuiIcon } from '../common';
import {
CellType,
GEventType,
InteractionKeyboardKey,
InterceptType,
OriginEventType,
Expand Down Expand Up @@ -83,7 +82,11 @@ export class EventController {
this.addCanvasEvent(OriginEventType.POINTER_MOVE, this.onCanvasMousemove);
this.addCanvasEvent(OriginEventType.MOUSE_OUT, this.onCanvasMouseout);
this.addCanvasEvent(OriginEventType.POINTER_UP, this.onCanvasMouseup);
this.addCanvasEvent(GEventType.RIGHT_MOUSE_UP, this.onCanvasContextMenu);
/**
* 如果监听 G Canvas, 右键对应的是 rightup/rightdown 事件, 如需禁用右键菜单 (preventDefault), 需要监听 DOM
* https://g.antv.antgroup.com/api/event/faq#%E7%A6%81%E7%94%A8%E5%8F%B3%E9%94%AE%E8%8F%9C%E5%8D%95
*/
this.addCanvasEvent(OriginEventType.RIGHT_DOWN, this.onCanvasContextMenu);

// spreadsheet events
this.addS2Event(S2Event.GLOBAL_ACTION_ICON_CLICK, () => {
Expand Down Expand Up @@ -291,7 +294,7 @@ export class EventController {
return false;
}

private isResizeArea(event: CanvasEvent) {
private isResizeArea(event: CanvasEvent | MouseEvent) {
const appendInfo = getAppendInfo(event.target as DisplayObject);

return appendInfo?.isResizeArea;
Expand Down
4 changes: 4 additions & 0 deletions packages/s2-react/playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,10 @@ function MainLayout() {
onRowCellAllCollapsed={logHandler(
'onRowCellAllCollapsed',
)}
onContextMenu={logHandler('onContextMenu')}
onDataCellContextMenu={logHandler(
'onDataCellContextMenu',
)}
/>
</React.StrictMode>
)}
Expand Down
4 changes: 2 additions & 2 deletions s2-site/docs/api/components/sheet-component.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ tag: Updated
| onCopied | 复制事件 | (data: CopyableList) => void | | |
| onActionIconHover | 行头操作 icon 悬停事件 | (event: CanvasEvent) => void | | |
| onActionIconClick | 行头操作 icon 点击事件 | (event: CanvasEvent) => void | | |
| onContextMenu | 右键单元格单击事件 | (data: [TargetCellInfo](#targetcellinfo)) => void | | |
| onContextMenu | 右键单元格单击事件 ([禁用右键菜单不生效?](/manual/faq#%E7%A6%81%E7%94%A8%E5%8F%B3%E9%94%AE%E8%8F%9C%E5%8D%95%E4%B8%8D%E7%94%9F%E6%95%88)) | (data: [TargetCellInfo](#targetcellinfo)) => void | | |
| onMouseHover | 表格鼠标悬停事件 | (event: CanvasEvent) => void | | |
| onMouseUp | 表格鼠标松开事件 | (event: CanvasEvent) => void | | |
| onSelected | 单元格选中事件 | (cells: ( [Cell](/docs/api/basic-class/base-cell)[] ) => void | | |
Expand Down Expand Up @@ -216,7 +216,7 @@ type SheetComponentOptions = S2Options<
| copied | 复制事件 | (data: CopyableList) => void | | |
| actionIconHover | 行头操作 icon 悬停事件 | (event: CanvasEvent) => void | | |
| actionIconClick | 行头操作 icon 点击事件 | (event: CanvasEvent) => void | | |
| contextMenu | 右键单元格单击事件 | (data: [TargetCellInfo](#targetcellinfo)) => void | | |
| contextMenu | 右键单元格单击事件 ([禁用右键菜单不生效?](/manual/faq#%E7%A6%81%E7%94%A8%E5%8F%B3%E9%94%AE%E8%8F%9C%E5%8D%95%E4%B8%8D%E7%94%9F%E6%95%88)) | (data: [TargetCellInfo](#targetcellinfo)) => void | | |
| mouseHover | 表格鼠标悬停事件 | (event: CanvasEvent) => void | | |
| mouseUp | 表格鼠标松开事件 | (event: CanvasEvent) => void | | |
| selected | 单元格选中事件 | ( cells: ([Cell](/docs/api/basic-class/base-cell)[] ) => void | | |
Expand Down
2 changes: 1 addition & 1 deletion s2-site/docs/api/general/S2Event.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ s2.on(S2Event.ROW_CELL_CLICK, (event) => {
| 复制 | `S2Event.GLOBAL_COPIED` | 对选中的单元格复制 |
| 鼠标松开 | `S2Event.GLOBAL_MOUSE_UP` | 图表区域鼠标松开 |
| 点击 | `S2Event.GLOBAL_CLICK` | 图表区域点击 |
| 右键 | `S2Event.GLOBAL_CONTEXT_MENU` | 图表区域按下右键 |
| 右键 | `S2Event.GLOBAL_CONTEXT_MENU` | 图表区域按下右键 ([禁用右键菜单不生效?](/manual/faq#%E7%A6%81%E7%94%A8%E5%8F%B3%E9%94%AE%E8%8F%9C%E5%8D%95%E4%B8%8D%E7%94%9F%E6%95%88)) |
| 选中 | `S2Event.GLOBAL_SELECTED` | 选中单元格时,如:刷选,多选,单选 |
| 悬停 | `S2Event.GLOBAL_HOVER` | 鼠标悬停在单元格 |
| 重置 | `S2Event.GLOBAL_RESET` | 点击空白处,按下 Esc 键 重置交互样式时 |
Expand Down
2 changes: 1 addition & 1 deletion s2-site/docs/manual/advanced/interaction/custom.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import { TableSheet } from '@antv/s2';

class ContextMenuInteraction extends BaseEvent {
bindEvents() {
this.spreadsheet.on(S2Event.GLOBAL_CONTEXT_MENU, (event) => {
this.spreadsheet.getCanvasElement().addEventListener('contextmenu', (event) => {
// 禁止弹出右键菜单
event.preventDefault();
});
Expand Down
18 changes: 18 additions & 0 deletions s2-site/docs/manual/faq.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,24 @@ const s2Options = {
}
```

### 禁用右键菜单不生效?

表格内部监听 `G``rightup/rightdown` 事件,而不是 `<canvas/>` DOM 的 `contextmenu` 事件,因此默认行为**并不是弹出右键系统菜单**,所以 `event.preventDefault()` 无效:

```ts | pure
// ❌ 无效写法
s2.on(S2Event.GLOBAL_CONTEXT_MENU, (event) => {
event.preventDefault();
})

// ✅ 正确写法
s2.getCanvasElement().addEventListener('contextmenu', (event) => {
event.preventDefault();
});
```

[了解更多](https://g.antv.antgroup.com/api/event/faq#%E7%A6%81%E7%94%A8%E5%8F%B3%E9%94%AE%E8%8F%9C%E5%8D%95)

### S2 有对应的 `Vue` 或者 `Angular` 版本吗?如何获取新版本发布通知?

<embed src="@/docs/common/packages.zh.md"></embed>
Expand Down
37 changes: 37 additions & 0 deletions s2-site/examples/interaction/custom/demo/disable-context-menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { PivotSheet, BaseEvent, S2Options } from '@antv/s2';
import '@antv/s2/dist/style.min.css';

class DisableContextMenuInteraction extends BaseEvent {
bindEvents() {
this.spreadsheet
.getCanvasElement()
.addEventListener('contextmenu', (event) => {
event.preventDefault();
});
}
}

fetch(
'https://gw.alipayobjects.com/os/bmw-prod/2a5dbbc8-d0a7-4d02-b7c9-34f6ca63cff6.json',
)
.then((res) => res.json())
.then(async (dataCfg) => {
const container = document.getElementById('container');

const s2Options: S2Options = {
width: 600,
height: 480,
interaction: {
customInteractions: [
{
key: 'DisableContextMenuInteraction',
interaction: DisableContextMenuInteraction,
},
],
},
};

const s2 = new PivotSheet(container, dataCfg, s2Options);

await s2.render();
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,7 @@ class HiddenInteraction extends BaseEvent {
}
}

class ContextMenuInteraction extends BaseEvent {
bindEvents() {
// 禁止弹出右键菜单
this.spreadsheet.on(S2Event.GLOBAL_CONTEXT_MENU, (event) => {
event?.preventDefault?.();
console.log('右键', event);
});
}
}

fetch(
'https://render.alipay.com/p/yuyan/180020010001215413/s2/basic.json',
)
fetch('https://render.alipay.com/p/yuyan/180020010001215413/s2/basic.json')
.then((res) => res.json())
.then(async (data) => {
const container = document.getElementById('container');
Expand Down Expand Up @@ -87,10 +75,6 @@ fetch(
key: 'HiddenInteraction',
interaction: HiddenInteraction,
},
{
key: 'ContextMenuInteraction',
interaction: ContextMenuInteraction,
},
],
},
};
Expand Down
9 changes: 9 additions & 0 deletions s2-site/examples/interaction/custom/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
"en": "Show tooltip when hover row or column"
},
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/c%24ICO1qvb/Kapture%2525202021-12-03%252520at%25252016.30.11.gif"
},
{
"filename": "disable-context-menu.ts",
"title": {
"zh": "禁用右键菜单",
"en": "Disable context menu"
},
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*i57MR6OphToAAAAAAAAAAAAADmJ7AQ/original",
"new": true
}
]
}
Loading