import "./hotbar-menu.scss"; import "./hotbar.commands"; import React, { ReactNode } from "react"; import { disposeOnUnmount, observer } from "mobx-react"; import { HotbarIcon } from "./hotbar-icon"; import { cssNames, cssVar, IClassName } from "../../utils"; import { catalogEntityRegistry } from "../../api/catalog-entity-registry"; import { HotbarItem, HotbarStore } from "../../../common/hotbar-store"; import { catalogEntityRunContext } from "../../api/catalog-entity"; import { Icon } from "../icon"; import { Badge } from "../badge"; import { CommandOverlay } from "../command-palette"; import { HotbarSwitchCommand } from "./hotbar-switch-command"; import { action, reaction } from "mobx"; interface Props { className?: IClassName; } @observer export class HotbarMenu extends React.Component { componentDidMount() { disposeOnUnmount(this, [ reaction(() => this.hotbar, () => this.createInitialCells(), { fireImmediately: true }) ]); } get hotbar() { return HotbarStore.getInstance().getActive(); } getEntity(item: HotbarItem) { const hotbar = HotbarStore.getInstance().getActive(); if (!hotbar) { return null; } return item ? catalogEntityRegistry.items.find((entity) => entity.metadata.uid === item.entity.uid) : null; } previous() { HotbarStore.getInstance().switchToPrevious(); } next() { HotbarStore.getInstance().switchToNext(); } openSelector() { CommandOverlay.open(); } @action createInitialCells() { if (this.hotbar.items.length) { return; } const element = document.querySelector(".HotbarItems"); const height = element.offsetHeight; const cellHeight = cssVar(element).get("--cellFullHeight").toString(); const cellsFit = Math.floor(height / parseInt(cellHeight)) - 1; this.hotbar.items = [...Array.from(Array(cellsFit).fill(null))]; } renderGrid() { if (!this.hotbar.items.length) return; return this.hotbar.items.map((item, index) => { const entity = this.getEntity(item); return ( {entity && ( entity.onRun(catalogEntityRunContext)} /> )} {!entity && (
HotbarStore.getInstance().removeEmptyCell(index)}>
)}
); }); } renderAddCellButton() { return ( ); } render() { const { className } = this.props; const hotbarIndex = HotbarStore.getInstance().activeHotbarIndex + 1; return (
{this.renderGrid()} {this.renderAddCellButton()}
this.previous()} />
this.openSelector()} />
this.next()} />
); } } interface HotbarCellProps { children?: ReactNode; } function HotbarCell(props: HotbarCellProps) { return (
{props.children}
); }