@eclipse-glsp/theia-integration
Version:
Glue code to integrate GLSP clients into Eclipse Theia
188 lines • 8.75 kB
JavaScript
;
/********************************************************************************
* Copyright (c) 2018-2026 TypeFox and others.
* Modifications: (c) 2019-2024 EclipseSource 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 WITH Classpath-exception-2.0
********************************************************************************/
// based on: https://github.com/eclipse-sprotty/sprotty-theia/blob/v0.12.0/src/theia/diagram-manager.ts
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GLSPDiagramManager = void 0;
exports.registerDiagramManager = registerDiagramManager;
exports.deriveDiagramManagerId = deriveDiagramManagerId;
const client_1 = require("@eclipse-glsp/client");
const browser_1 = require("@theia/core/lib/browser");
const inversify_1 = require("@theia/core/shared/inversify");
const browser_2 = require("@theia/editor/lib/browser");
const diagram_service_provider_1 = require("../diagram-service-provider");
const theia_opener_options_navigation_service_1 = require("../theia-opener-options-navigation-service");
const glsp_diagram_context_key_service_1 = require("./glsp-diagram-context-key-service");
const glsp_diagram_widget_1 = require("./glsp-diagram-widget");
function registerDiagramManager(bind, diagramManagerServiceId, bindToSelf = true) {
if (bindToSelf) {
bind(diagramManagerServiceId).toSelf().inSingletonScope();
}
bind(browser_1.FrontendApplicationContribution).toService(diagramManagerServiceId);
bind(browser_1.OpenHandler).toService(diagramManagerServiceId);
bind(browser_1.WidgetFactory).toService(diagramManagerServiceId);
}
let GLSPDiagramManager = class GLSPDiagramManager extends browser_1.WidgetOpenHandler {
constructor() {
super(...arguments);
this.widgetCount = 0;
this.registerOpenWithHandler = true;
}
init() {
super.init();
if (this.registerOpenWithHandler) {
this.openWithService.registerHandler(this);
}
}
async doOpen(widget, uri, maybeOptions) {
const options = {
mode: 'activate',
...maybeOptions
};
if (!widget.isAttached) {
this.attachWidget(widget, options);
}
if (options.mode === 'activate') {
await widget.getSvgElement();
await this.shell.activateWidget(widget.id);
}
else if (options.mode === 'reveal') {
await this.shell.revealWidget(widget.id);
}
if (this.handleNavigations(widget, options)) {
return;
}
}
attachWidget(widget, options) {
const currentEditor = this.editorManager.currentEditor;
const widgetOptions = {
area: 'main',
...(options && options.widgetOptions ? options.widgetOptions : {})
};
if (!!currentEditor && currentEditor.editor.uri.toString(true) === widget.uri.toString(true)) {
widgetOptions.ref = currentEditor;
widgetOptions.mode =
options && options.widgetOptions && options.widgetOptions.mode ? options.widgetOptions.mode : 'open-to-right';
}
this.shell.addWidget(widget, widgetOptions);
}
handleNavigations(widget, options) {
const navigations = this.diagramNavigationService.determineNavigations(widget.uri.toString(true), options);
if (navigations.length > 0) {
widget.actionDispatcher.dispatchOnceModelInitialized(...navigations);
return true;
}
return false;
}
async createWidget(options) {
if (glsp_diagram_widget_1.GLSPDiagramWidgetOptions.is(options)) {
const config = this.getDiagramConfiguration(options);
const diagramOptions = this.createDiagramOptions(options);
const diContainer = config.createContainer(diagramOptions);
const diagramWidgetFactory = this.diagramServiceProvider.getDiagramWidgetFactory(this.diagramType);
const widget = diagramWidgetFactory === null || diagramWidgetFactory === void 0 ? void 0 : diagramWidgetFactory.create(options, diContainer);
widget.listenToFocusState(this.shell);
return widget;
}
throw Error('DiagramWidgetFactory needs DiagramWidgetOptions but got ' + JSON.stringify(options));
}
createDiagramOptions(options) {
return {
clientId: this.createClientId(),
diagramType: options.diagramType,
sourceUri: options.uri,
editMode: options.editMode,
glspClientProvider: () => this.diagramServiceProvider.getGLSPClientContribution(this.contributionId).glspClient
};
}
createClientId() {
return this.diagramType + '_' + this.widgetCount++;
}
createWidgetOptions(uri, options) {
return {
diagramType: this.diagramType,
kind: 'navigatable',
uri: uri.toString(true),
iconClass: this.iconClass,
label: uri.path.base,
editMode: options && options.editMode ? options.editMode : client_1.EditMode.EDITABLE
};
}
getDiagramConfiguration(options) {
return this.diagramServiceProvider.getDiagramConfiguration(options.diagramType);
}
canHandle(uri, _options) {
for (const extension of this.fileExtensions) {
if (uri.path.toString().endsWith(extension)) {
return 1001;
}
}
return 0;
}
get id() {
return deriveDiagramManagerId(this.diagramType);
}
get iconClass() {
return (0, client_1.codiconCSSString)('type-hierarchy-sub');
}
get providerName() {
return undefined;
}
};
exports.GLSPDiagramManager = GLSPDiagramManager;
__decorate([
(0, inversify_1.inject)(theia_opener_options_navigation_service_1.TheiaOpenerOptionsNavigationService),
__metadata("design:type", theia_opener_options_navigation_service_1.TheiaOpenerOptionsNavigationService)
], GLSPDiagramManager.prototype, "diagramNavigationService", void 0);
__decorate([
(0, inversify_1.inject)(glsp_diagram_context_key_service_1.GLSPDiagramContextKeyService),
__metadata("design:type", glsp_diagram_context_key_service_1.GLSPDiagramContextKeyService)
], GLSPDiagramManager.prototype, "contextKeyService", void 0);
__decorate([
(0, inversify_1.inject)(diagram_service_provider_1.DiagramServiceProvider),
__metadata("design:type", diagram_service_provider_1.DiagramServiceProvider)
], GLSPDiagramManager.prototype, "diagramServiceProvider", void 0);
__decorate([
(0, inversify_1.inject)(browser_2.EditorManager),
__metadata("design:type", browser_2.EditorManager)
], GLSPDiagramManager.prototype, "editorManager", void 0);
__decorate([
(0, inversify_1.inject)(browser_1.OpenWithService),
__metadata("design:type", browser_1.OpenWithService)
], GLSPDiagramManager.prototype, "openWithService", void 0);
__decorate([
(0, inversify_1.postConstruct)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], GLSPDiagramManager.prototype, "init", null);
exports.GLSPDiagramManager = GLSPDiagramManager = __decorate([
(0, inversify_1.injectable)()
], GLSPDiagramManager);
function deriveDiagramManagerId(diagramType) {
return diagramType + '-diagram-manager';
}
//# sourceMappingURL=glsp-diagram-manager.js.map