UNPKG

@theia/core

Version:

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

152 lines • 5.71 kB
import { __decorate } from "tslib"; import { jsx as _jsx } from "react/jsx-runtime"; import { injectable, inject, postConstruct } from 'inversify'; import { ReactRenderer } from '../widgets/index.js'; import { BreadcrumbsService } from './breadcrumbs-service.js'; import { BreadcrumbRenderer } from './breadcrumb-renderer.js'; import { PerfectScrollbar } from '../widgets/perfect-scrollbar.js'; import { Emitter } from '../../common/index.js'; import { CorePreferences } from '../../common/core-preferences.js'; import { Styles } from './breadcrumbs-constants.js'; import { LabelProvider } from '../label-provider.js'; let BreadcrumbsRenderer = class BreadcrumbsRenderer extends ReactRenderer { constructor() { super(...arguments); this.onDidChangeActiveStateEmitter = new Emitter(); this.breadcrumbs = []; this.refreshCancellationMarker = { canceled: true }; this.togglePopup = (breadcrumb, event) => { event.stopPropagation(); event.preventDefault(); let openPopup = true; if (this.popup?.isOpen) { this.popup.dispose(); // There is a popup open. If the popup is the popup that belongs to the currently clicked breadcrumb // just close the popup. If another breadcrumb was clicked, open the new popup immediately. openPopup = this.popup.breadcrumbId !== breadcrumb.id; } else { this.popup = undefined; } if (openPopup) { const { currentTarget } = event; const breadcrumbElement = currentTarget.closest(`.${Styles.BREADCRUMB_ITEM}`); if (breadcrumbElement) { const { left: x, bottom: y } = breadcrumbElement.getBoundingClientRect(); this.breadcrumbsService.openPopup(breadcrumb, { x, y }).then(popup => { this.popup = popup; }); } } }; } get onDidChangeActiveState() { return this.onDidChangeActiveStateEmitter.event; } get active() { return !!this.breadcrumbs.length; } get breadCrumbsContainer() { return this.host.firstElementChild ?? undefined; } init() { this.toDispose.push(this.onDidChangeActiveStateEmitter); this.toDispose.push(this.breadcrumbsService.onDidChangeBreadcrumbs(uri => { if (this.uri?.isEqual(uri)) { this.refresh(this.uri); } })); this.toDispose.push(this.corePreferences.onPreferenceChanged(change => { if (change.preferenceName === 'breadcrumbs.enabled') { this.refresh(this.uri); } })); this.toDispose.push(this.labelProvider.onDidChange(() => this.refresh(this.uri))); } dispose() { super.dispose(); this.toDispose.dispose(); if (this.popup) { this.popup.dispose(); } if (this.scrollbar) { this.scrollbar.destroy(); this.scrollbar = undefined; } } async refresh(uri) { this.uri = uri; this.refreshCancellationMarker.canceled = true; const currentCallCanceled = { canceled: false }; this.refreshCancellationMarker = currentCallCanceled; let breadcrumbs; if (uri && this.corePreferences['breadcrumbs.enabled']) { breadcrumbs = await this.breadcrumbsService.getBreadcrumbs(uri); } else { breadcrumbs = []; } if (currentCallCanceled.canceled) { return; } const wasActive = this.active; this.breadcrumbs = breadcrumbs; const isActive = this.active; if (wasActive !== isActive) { this.onDidChangeActiveStateEmitter.fire(isActive); } this.update(); } update() { this.render(); if (!this.scrollbar) { this.createScrollbar(); } else { this.scrollbar.update(); } this.scrollToEnd(); } createScrollbar() { const { breadCrumbsContainer } = this; if (breadCrumbsContainer) { this.scrollbar = new PerfectScrollbar(breadCrumbsContainer, { handlers: ['drag-thumb', 'keyboard', 'wheel', 'touch'], useBothWheelAxes: true, scrollXMarginOffset: 4, suppressScrollY: true }); } } scrollToEnd() { const { breadCrumbsContainer } = this; if (breadCrumbsContainer) { breadCrumbsContainer.scrollLeft = breadCrumbsContainer.scrollWidth; } } doRender() { return _jsx("ul", { className: Styles.BREADCRUMBS, children: this.renderBreadcrumbs() }); } renderBreadcrumbs() { return this.breadcrumbs.map(breadcrumb => this.breadcrumbRenderer.render(breadcrumb, this.togglePopup)); } }; __decorate([ inject(BreadcrumbsService) ], BreadcrumbsRenderer.prototype, "breadcrumbsService", void 0); __decorate([ inject(BreadcrumbRenderer) ], BreadcrumbsRenderer.prototype, "breadcrumbRenderer", void 0); __decorate([ inject(CorePreferences) ], BreadcrumbsRenderer.prototype, "corePreferences", void 0); __decorate([ inject(LabelProvider) ], BreadcrumbsRenderer.prototype, "labelProvider", void 0); __decorate([ postConstruct() ], BreadcrumbsRenderer.prototype, "init", null); BreadcrumbsRenderer = __decorate([ injectable() ], BreadcrumbsRenderer); export { BreadcrumbsRenderer }; export const BreadcrumbsRendererFactory = Symbol('BreadcrumbsRendererFactory'); //# sourceMappingURL=breadcrumbs-renderer.js.map