Skip to content

Commit

Permalink
feat: adjust contextmenu onclick params to get element (#6174)
Browse files Browse the repository at this point in the history
  • Loading branch information
yvonneyx authored Aug 13, 2024
1 parent 7078585 commit 2d4aabb
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/g6/src/plugins/contextmenu/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { RuntimeContext } from '../../runtime/types';
import type { Element } from '../../types';
import type { IElementEvent } from '../../types/event';
import { createPluginContainer, insertDOM } from '../../utils/dom';
import type { BasePluginOptions } from '../base-plugin';
import { BasePlugin } from '../base-plugin';
import type { Item } from './util';
import { CONTEXTMENU_CSS, getContentFromItems } from './util';

/**
* <zh/> 上下文菜单配置项
*
Expand Down Expand Up @@ -42,7 +42,7 @@ export interface ContextmenuOptions extends BasePluginOptions {
*
* <en/> The callback method triggered when the menu is clicked
*/
onClick?: (value: string, target: HTMLElement) => void;
onClick?: (value: string, target: HTMLElement, current: Element) => void;
/**
* <zh/> 返回菜单的项目列表,支持 `Promise` 类型的返回值。是 `getContent` 的快捷配置
*
Expand Down Expand Up @@ -90,6 +90,8 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {

private $element!: HTMLElement;

private targetElement: Element | null = null;

constructor(context: RuntimeContext, options: ContextmenuOptions) {
super(context, Object.assign({}, Contextmenu.defaultOptions, options));

Expand Down Expand Up @@ -138,6 +140,8 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
this.$element.style.left = `${event.client.x - clientRect.left + offset[0]}px`;
this.$element.style.top = `${event.client.y - clientRect.top + offset[1]}px`;
this.$element.style.display = 'block';

this.targetElement = event.target;
}

/**
Expand All @@ -147,6 +151,7 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
*/
public hide() {
this.$element.style.display = 'none';
this.targetElement = null;
}

/**
Expand Down Expand Up @@ -218,7 +223,7 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
if (event.target instanceof HTMLElement) {
if (event.target.className.includes('g6-contextmenu-li')) {
const value = event.target.getAttribute('value') as string;
onClick?.(value, event.target);
onClick?.(value, event.target, this.targetElement!);
this.hide();
}
}
Expand Down

0 comments on commit 2d4aabb

Please sign in to comment.