|
| 1 | +/* global document */ |
| 2 | +import {Widget, WidgetProps} from '@deck.gl/core'; |
| 3 | +import type {Deck, PickingInfo} from '@deck.gl/core'; |
| 4 | +import {render} from 'preact'; |
| 5 | +import {SimpleMenu} from './lib/components/simple-menu'; |
| 6 | + |
| 7 | +/** The standard, modern way is to use event.button === 2, where button is the standardized property (0 = left, 1 = middle, 2 = right). */ |
| 8 | +const MOUSE_BUTTON_RIGHT = 2; |
| 9 | +/** A name for the legacy MouseEvent.which value that corresponds to the right-mouse button. In older browsers, the check is: if (event.which === 3) */ |
| 10 | +const MOUSE_WHICH_RIGHT = 3; |
| 11 | + |
| 12 | +export type ContextWidgetMenuItem = { |
| 13 | + label: string; |
| 14 | + key: string; |
| 15 | +}; |
| 16 | + |
| 17 | +export type ContextMenuWidgetProps = WidgetProps & { |
| 18 | + /** View to attach to and interact with. Required when using multiple views. */ |
| 19 | + viewId?: string | null; |
| 20 | + /** Controls visibility of the context menu */ |
| 21 | + visible?: boolean; |
| 22 | + /** Screen position at which to place the menu */ |
| 23 | + position: {x: number; y: number}; |
| 24 | + /** Items to render */ |
| 25 | + menuItems: ContextWidgetMenuItem[]; |
| 26 | + /** Provide menu items for the menu given the picked object */ |
| 27 | + getMenuItems: (info: PickingInfo, widget: ContextMenuWidget) => ContextWidgetMenuItem[] | null; |
| 28 | + /** Callback with the selected item */ |
| 29 | + onMenuItemSelected?: (key: string, pickInfo: PickingInfo | null) => void; |
| 30 | +}; |
| 31 | + |
| 32 | +export class ContextMenuWidget extends Widget<ContextMenuWidgetProps> { |
| 33 | + static defaultProps: Required<ContextMenuWidgetProps> = { |
| 34 | + ...Widget.defaultProps, |
| 35 | + viewId: null, |
| 36 | + visible: false, |
| 37 | + position: {x: 0, y: 0}, |
| 38 | + getMenuItems: undefined!, |
| 39 | + menuItems: [], |
| 40 | + // eslint-disable-next-line no-console |
| 41 | + onMenuItemSelected: (key, pickInfo) => console.log('Context menu item selected:', key, pickInfo) |
| 42 | + }; |
| 43 | + |
| 44 | + className = 'deck-widget-context-menu'; |
| 45 | + placement = 'fill' as const; |
| 46 | + |
| 47 | + pickInfo: PickingInfo | null = null; |
| 48 | + |
| 49 | + constructor(props: ContextMenuWidgetProps) { |
| 50 | + super(props, ContextMenuWidget.defaultProps); |
| 51 | + this.pickInfo = null; |
| 52 | + this.setProps(props); |
| 53 | + } |
| 54 | + |
| 55 | + onAdd({deck}: {deck: Deck<any>}): HTMLDivElement { |
| 56 | + const element = document.createElement('div'); |
| 57 | + element.classList.add('deck-widget', 'deck-widget-context-menu'); |
| 58 | + const style = { |
| 59 | + margin: '0px', |
| 60 | + top: '0px', |
| 61 | + left: '0px', |
| 62 | + position: 'absolute', |
| 63 | + pointerEvents: 'auto' |
| 64 | + }; |
| 65 | + Object.entries(style).forEach(([key, value]) => element.style.setProperty(key, value)); |
| 66 | + |
| 67 | + deck.getCanvas()?.addEventListener('click', () => this.hide()); |
| 68 | + deck.getCanvas()?.addEventListener('contextmenu', event => this.handleContextMenu(event)); |
| 69 | + return element; |
| 70 | + } |
| 71 | + |
| 72 | + onRenderHTML(rootElement: HTMLElement): void { |
| 73 | + const {visible, position, menuItems} = this.props; |
| 74 | + |
| 75 | + const ui = |
| 76 | + visible && menuItems.length ? ( |
| 77 | + <SimpleMenu |
| 78 | + menuItems={menuItems} |
| 79 | + onItemSelected={key => this.props.onMenuItemSelected(key, this.pickInfo)} |
| 80 | + position={position} |
| 81 | + style={{pointerEvents: 'auto'}} |
| 82 | + /> |
| 83 | + ) : null; |
| 84 | + render(ui, rootElement); |
| 85 | + } |
| 86 | + |
| 87 | + handleContextMenu(srcEvent: MouseEvent): boolean { |
| 88 | + if ( |
| 89 | + srcEvent && |
| 90 | + (srcEvent.button === MOUSE_BUTTON_RIGHT || srcEvent.which === MOUSE_WHICH_RIGHT) |
| 91 | + ) { |
| 92 | + this.pickInfo = |
| 93 | + this.deck?.pickObject({ |
| 94 | + x: srcEvent.clientX, |
| 95 | + y: srcEvent.clientY |
| 96 | + }) || null; |
| 97 | + const menuItems = (this.pickInfo && this.props.getMenuItems?.(this.pickInfo, this)) || []; |
| 98 | + const visible = menuItems.length > 0; |
| 99 | + this.setProps({ |
| 100 | + visible, |
| 101 | + position: {x: srcEvent.clientX, y: srcEvent.clientY}, |
| 102 | + menuItems |
| 103 | + }); |
| 104 | + this.updateHTML(); |
| 105 | + srcEvent.preventDefault(); |
| 106 | + return visible; |
| 107 | + } |
| 108 | + |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + hide(): void { |
| 113 | + this.setProps({visible: false}); |
| 114 | + } |
| 115 | +} |
0 commit comments