import React = require("react"); import { ContextMenuItem, ContextMenuProps, OriginalMenuProps } from "./ContextMenuItem"; import { observable, action, computed } from "mobx"; import { observer } from "mobx-react"; import "./ContextMenu.scss"; import { library } from '@fortawesome/fontawesome-svg-core'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faSearch, faCircle } from '@fortawesome/free-solid-svg-icons'; library.add(faSearch); library.add(faCircle); @observer export class ContextMenu extends React.Component { static Instance: ContextMenu; @observable private _items: Array = []; @observable private _pageX: number = 0; @observable private _pageY: number = 0; @observable private _display: boolean = false; @observable private _searchString: string = ""; // afaik displaymenu can be called before all the items are added to the menu, so can't determine in displayMenu what the height of the menu will be @observable private _yRelativeToTop: boolean = true; @observable selectedIndex = -1; private _searchRef = React.createRef(); private ref: React.RefObject; constructor(props: Readonly<{}>) { super(props); this.ref = React.createRef(); ContextMenu.Instance = this; } @action clearItems() { this._items = []; } @action addItem(item: ContextMenuProps) { if (this._items.indexOf(item) === -1) { this._items.push(item); } } getItems() { return this._items; } @action displayMenu(x: number, y: number) { //maxX and maxY will change if the UI/font size changes, but will work for any amount //of items added to the menu let maxX = window.innerWidth - 150; let maxY = window.innerHeight - ((this._items.length + 1/*for search box*/) * 34 + 30); this._pageX = x > maxX ? maxX : x; this._pageY = y > maxY ? maxY : y; this._searchString = ""; this._display = true; if (this._searchRef.current) { this._searchRef.current.focus(); } } @action closeMenu = () => { this.clearItems(); this._display = false; } @computed get filteredItems(): (OriginalMenuProps | string[])[] { const searchString = this._searchString.toLowerCase().split(" "); const matches = (descriptions: string[]): boolean => { return searchString.every(s => descriptions.some(desc => desc.toLowerCase().includes(s))); }; const flattenItems = (items: ContextMenuProps[], groupFunc: (groupName: any) => string[]) => { let eles: (OriginalMenuProps | string[])[] = []; const leaves: OriginalMenuProps[] = []; for (const item of items) { const description = item.description; const path = groupFunc(description); if ("subitems" in item) { const children = flattenItems(item.subitems, name => [...groupFunc(description), name]); if (children.length || matches(path)) { eles.push(path); eles = eles.concat(children); } } else { if (!matches(path)) { continue; } leaves.push(item); } } eles = [...leaves, ...eles]; return eles; }; return flattenItems(this._items, name => [name]); } @computed get flatItems(): OriginalMenuProps[] { return this.filteredItems.filter(item => !Array.isArray(item)) as OriginalMenuProps[]; } @computed get filteredViews() { const createGroupHeader = (contents: any) => { return (
{contents}
); }; const createItem = (item: ContextMenuProps, selected: boolean) => ; let itemIndex = 0; return this.filteredItems.map(value => { if (Array.isArray(value)) { return createGroupHeader(value.join(" -> ")); } else { return createItem(value, itemIndex++ === this.selectedIndex); } }); } @computed get menuItems() { if (!this._searchString) { return this._items.map(item => ); } return this.filteredViews; } render() { if (!this._display) { return null; } let style = this._yRelativeToTop ? { left: this._pageX, top: this._pageY } : { left: this._pageX, bottom: this._pageY }; return (
{this.menuItems}
); } @action onKeyDown = (e: React.KeyboardEvent) => { if (e.key === "ArrowDown") { if (this.selectedIndex < this.flatItems.length - 1) { this.selectedIndex++; } e.preventDefault(); } else if (e.key === "ArrowUp") { if (this.selectedIndex > 0) { this.selectedIndex--; } e.preventDefault(); } else if (e.key === "Enter") { const item = this.flatItems[this.selectedIndex]; item.event(); this.closeMenu(); } } @action onChange = (e: React.ChangeEvent) => { this._searchString = e.target.value; if (!this._searchString) { this.selectedIndex = -1; } else { if (this.selectedIndex === -1) { this.selectedIndex = 0; } else { this.selectedIndex = Math.min(this.flatItems.length - 1, this.selectedIndex); } } } }