UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

156 lines • 6.11 kB
import { __decorate } from "tslib"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; // ***************************************************************************** // Copyright (C) 2020 Alibaba Inc. and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** import { injectable, inject } from 'inversify'; import * as React from 'react'; import { ReactWidget } from '../widgets/index.js'; import { ContextMenuRenderer } from '../context-menu-renderer.js'; import { MenuModelRegistry } from '../../common/menu/index.js'; import { HoverService } from '../hover-service.js'; import { Emitter, DisposableCollection } from '../../common/index.js'; import { ContextKeyService } from '../context-key-service.js'; export const SidebarTopMenuWidgetFactory = Symbol('SidebarTopMenuWidgetFactory'); export const SidebarBottomMenuWidgetFactory = Symbol('SidebarBottomMenuWidgetFactory'); export class SidebarMenuItem { get badge() { if (this._badge <= 0) { return ''; } else if (this._badge > 99) { return '99+'; } else { return this._badge.toString(); } } ; constructor(menu) { this.onDidBadgeChangeEmitter = new Emitter(); this.onDidBadgeChange = this.onDidBadgeChangeEmitter.event; this._badge = 0; this.toDispose = new DisposableCollection(); this.menu = menu; if (menu.onDidBadgeChange) { this.toDispose.push(menu.onDidBadgeChange(value => { this._badge = value; this.onDidBadgeChangeEmitter.fire(value); })); } } dispose() { this.toDispose.dispose(); this.onDidBadgeChangeEmitter.dispose(); } } /** * The menu widget placed on the sidebar. */ let SidebarMenuWidget = class SidebarMenuWidget extends ReactWidget { constructor() { super(); /** * Flag indicating whether a context menu is open. While a context menu is open, the `preservedContext` should not be cleared. */ this.preservingContext = false; this.onMouseDown = () => { const { activeElement } = document; if (activeElement instanceof HTMLElement && !this.node.contains(activeElement)) { this.preservedContext = activeElement; } }; this.onMouseOut = () => { if (!this.preservingContext) { this.preservedContext = undefined; } }; this.onMouseEnter = (event, title) => { if (title && event.nativeEvent.currentTarget) { this.hoverService.requestHover({ content: title, target: event.currentTarget, position: 'right' }); } }; this.items = []; } addMenu(menu) { const exists = this.items.find(item => item.menu.id === menu.id); if (exists) { return; } const newItem = new SidebarMenuItem(menu); newItem.onDidBadgeChange(() => this.update()); this.items.push(newItem); this.items.sort((a, b) => a.menu.order - b.menu.order); this.update(); } removeMenu(menuId) { const index = this.items.findIndex(m => m.menu.id === menuId); if (index !== -1) { this.items[index].dispose(); this.items.splice(index, 1); this.update(); } } onClick(e, menuPath) { this.preservingContext = true; const button = e.currentTarget.getBoundingClientRect(); const menu = this.menuRegistry.getMenuNode(menuPath); this.contextMenuRenderer.render({ menuPath: menuPath, menu: menu, includeAnchorArg: false, anchor: { x: button.left + button.width, y: button.top, }, context: e.currentTarget, contextKeyService: this.contextKeyService, onHide: () => { this.preservingContext = false; if (this.preservedContext) { this.preservedContext.focus({ preventScroll: true }); this.preservedContext = undefined; } } }); } render() { return _jsx(React.Fragment, { children: this.items.map(item => this.renderItem(item)) }); } renderItem(item) { return _jsxs("div", { className: 'theia-sidebar-menu-item', onClick: e => this.onClick(e, item.menu.menuPath), onMouseDown: this.onMouseDown, onMouseEnter: e => this.onMouseEnter(e, item.menu.title), onMouseLeave: this.onMouseOut, children: [_jsx("i", { className: item.menu.iconClass }), item.badge && _jsx("div", { className: 'theia-badge-decorator-sidebar', children: item.badge })] }, item.menu.id); } }; __decorate([ inject(ContextMenuRenderer) ], SidebarMenuWidget.prototype, "contextMenuRenderer", void 0); __decorate([ inject(MenuModelRegistry) ], SidebarMenuWidget.prototype, "menuRegistry", void 0); __decorate([ inject(HoverService) ], SidebarMenuWidget.prototype, "hoverService", void 0); __decorate([ inject(ContextKeyService) ], SidebarMenuWidget.prototype, "contextKeyService", void 0); SidebarMenuWidget = __decorate([ injectable() ], SidebarMenuWidget); export { SidebarMenuWidget }; //# sourceMappingURL=sidebar-menu-widget.js.map