UNPKG

@theia/core

Version:

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

208 lines • 9.04 kB
"use strict"; // ***************************************************************************** // Copyright (C) 2024 STMicroelectronics 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 // ***************************************************************************** Object.defineProperty(exports, "__esModule", { value: true }); exports.ReactToolbarItemImpl = exports.RenderedToolbarItemImpl = void 0; const label_parser_1 = require("../../label-parser"); const common_1 = require("../../../common"); const widgets_1 = require("../../widgets"); const tab_bar_toolbar_1 = require("./tab-bar-toolbar"); const React = require("react"); const menu_1 = require("../../../common/menu"); /** * Class name indicating rendering of a toolbar item without an icon but instead with a text label. */ const NO_ICON_CLASS = 'no-icon'; class AbstractToolbarItemImpl { constructor(commandRegistry, contextKeyService, action) { this.commandRegistry = commandRegistry; this.contextKeyService = contextKeyService; this.action = action; } get id() { return this.action.id; } get group() { return this.action.group; } get priority() { return this.action.priority; } get onDidChange() { return this.action.onDidChange; } isVisible(widget) { if (this.action.isVisible) { return this.action.isVisible(widget); } const actionVisible = !this.action.command || this.commandRegistry.isVisible(this.action.command, widget); const contextMatches = !this.action.when || this.contextKeyService.match(this.action.when); return actionVisible && contextMatches; } isEnabled(widget) { return this.action.command ? this.commandRegistry.isEnabled(this.action.command, widget) : !!this.action.menuPath; } isToggled() { return this.action.command ? this.commandRegistry.isToggled(this.action.command) : true; } } class RenderedToolbarItemImpl extends AbstractToolbarItemImpl { constructor(commandRegistry, contextKeyService, keybindingRegistry, labelParser, action) { super(commandRegistry, contextKeyService, action); this.keybindingRegistry = keybindingRegistry; this.labelParser = labelParser; this.disposables = new common_1.DisposableCollection(); this.onDidChangeEmitter = new common_1.Emitter; this.onMouseDownEvent = (e) => { if (e.button === 0) { e.currentTarget.classList.add('active'); } }; this.onMouseUpEvent = (e) => { e.currentTarget.classList.remove('active'); }; if (action.onDidChange) { this.disposables.push(action.onDidChange(() => this.onDidChangeEmitter.fire())); } } dispose() { this.disposables.dispose(); } updateContextKeyListener(when) { var _a; const contextKeys = new Set(); (_a = this.contextKeyService.parseKeys(when)) === null || _a === void 0 ? void 0 : _a.forEach(key => contextKeys.add(key)); if (contextKeys.size > 0) { this.contextKeyListener = this.contextKeyService.onDidChange(change => { if (change.affects(contextKeys)) { this.onDidChangeEmitter.fire(); } }); } } render(widget) { return this.renderItem(widget); } getToolbarItemClassNames(widget) { const classNames = [tab_bar_toolbar_1.TabBarToolbar.Styles.TAB_BAR_TOOLBAR_ITEM]; if (this.isEnabled(widget)) { classNames.push('enabled'); } if (this.isToggled()) { classNames.push('toggled'); } return classNames; } resolveKeybindingForCommand(widget, command) { let result = ''; if (this.action.command) { const bindings = this.keybindingRegistry.getKeybindingsForCommand(this.action.command); let found = false; if (bindings && bindings.length > 0) { bindings.forEach(binding => { if (binding.when) { this.updateContextKeyListener(binding.when); } if (!found && this.keybindingRegistry.isEnabledInScope(binding, widget === null || widget === void 0 ? void 0 : widget.node)) { found = true; result = ` (${this.keybindingRegistry.acceleratorFor(binding, '+')})`; } }); } } return result; } get onDidChange() { return this.onDidChangeEmitter.event; } toMenuNode() { var _a; const action = new menu_1.ActionMenuNode({ label: this.action.tooltip, commandId: this.action.command, when: this.action.when, order: this.action.order }, this.commandRegistry, this.keybindingRegistry, this.contextKeyService); // Register a submenu for the item, if the group is in format `<submenu group>/<submenu name>/.../<item group>` const menuPath = ((_a = this.action.group) === null || _a === void 0 ? void 0 : _a.split('/')) || []; if (menuPath.length > 1) { let menu = new menu_1.GroupImpl(menuPath[0], this.action.order); menu = menu.getOrCreate(menuPath, 1, menuPath.length); menu.addNode(action); return menu; } return action; } executeCommand(e, widget) { e.preventDefault(); e.stopPropagation(); if (!this.isEnabled(widget)) { return; } if (this.action.command) { this.commandRegistry.executeCommand(this.action.command, widget); } } ; renderItem(widget) { let innerText = ''; const classNames = []; const command = this.action.command ? this.commandRegistry.getCommand(this.action.command) : undefined; // Fall back to the item ID in extremis so there is _something_ to render in the // case that there is neither an icon nor a title const itemText = this.action.text || (command === null || command === void 0 ? void 0 : command.label) || (command === null || command === void 0 ? void 0 : command.id) || this.action.id; if (itemText) { for (const labelPart of this.labelParser.parse(itemText)) { if (label_parser_1.LabelIcon.is(labelPart)) { const className = `fa fa-${labelPart.name}${labelPart.animation ? ' fa-' + labelPart.animation : ''}`; classNames.push(...className.split(' ')); } else { innerText = labelPart; } } } const iconClass = (typeof this.action.icon === 'function' && this.action.icon()) || this.action.icon || (command && command.iconClass); if (iconClass) { classNames.push(iconClass); } const tooltipText = this.action.tooltip || (command && command.label) || ''; const tooltip = `${this.labelParser.stripIcons(tooltipText)}${this.resolveKeybindingForCommand(widget, command === null || command === void 0 ? void 0 : command.id)}`; // Only present text if there is no icon if (classNames.length) { innerText = ''; } else if (innerText) { // Make room for the label text classNames.push(NO_ICON_CLASS); } // In any case, this is an action item, with or without icon. classNames.push(widgets_1.ACTION_ITEM); const toolbarItemClassNames = this.getToolbarItemClassNames(widget); return React.createElement("div", { key: this.action.id, className: toolbarItemClassNames.join(' '), onMouseDown: this.onMouseDownEvent, onMouseUp: this.onMouseUpEvent, onMouseOut: this.onMouseUpEvent }, React.createElement("div", { id: this.action.id, className: classNames.join(' '), onClick: e => this.executeCommand(e, widget), title: tooltip }, " ", innerText)); } } exports.RenderedToolbarItemImpl = RenderedToolbarItemImpl; class ReactToolbarItemImpl extends AbstractToolbarItemImpl { render(widget) { return this.action.render(widget); } } exports.ReactToolbarItemImpl = ReactToolbarItemImpl; //# sourceMappingURL=tab-toolbar-item.js.map