@eclipse-glsp/theia-integration
Version:
Glue code to integrate GLSP clients into Eclipse Theia
136 lines • 6.51 kB
JavaScript
;
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.TheiaGLSPSelectionForwarder = exports.GlspSelectionDataService = exports.GlspSelection = void 0;
/********************************************************************************
* Copyright (c) 2018-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/sprotty/theia-sprotty-selection-forwarder.ts
const client_1 = require("@eclipse-glsp/client");
const core_1 = require("@theia/core");
const inversify_1 = require("@theia/core/shared/inversify");
var GlspSelection;
(function (GlspSelection) {
function is(object) {
return (client_1.AnyObject.is(object) &&
(0, client_1.hasArrayProp)(object, 'selectedElementsIDs') &&
(0, client_1.hasStringProp)(object, 'widgetId') &&
(0, client_1.hasStringProp)(object, 'sourceUri', true) &&
(0, client_1.hasObjectProp)(object, 'additionalSelectionData', true));
}
GlspSelection.is = is;
})(GlspSelection || (exports.GlspSelection = GlspSelection = {}));
/**
* Optional service that can be implemented to provide additional {@link GlspSelectionData} for
* the {@link TheiaGLSPSelectionForwarder}
*/
let GlspSelectionDataService = class GlspSelectionDataService {
};
exports.GlspSelectionDataService = GlspSelectionDataService;
exports.GlspSelectionDataService = GlspSelectionDataService = __decorate([
(0, inversify_1.injectable)()
], GlspSelectionDataService);
/**
* Reacts to diagram selection changes and forwards the corresponding {@link GlspSelection}
* to Theia`s {@link SelectionService}
*
* (bound in Diagram child DI container)
*/
let TheiaGLSPSelectionForwarder = class TheiaGLSPSelectionForwarder {
constructor() {
this.toDispose = new client_1.DisposableCollection();
}
init() {
this.toDispose.push(this.editorContext.onFocusChanged(event => {
if (event.hasFocus) {
// restore selection from the global scope to the diagram
this.selectionChanged(this.editorContext.modelRoot, (0, client_1.pluck)(this.editorContext.selectedElements, 'id'));
}
}));
}
dispose() {
this.toDispose.dispose();
}
async getSourceUri() {
if (!this.sourceUri) {
const modelSource = await this.modelSourceProvider();
if (modelSource instanceof client_1.GLSPModelSource) {
this.sourceUri = modelSource.sourceUri;
}
}
return this.sourceUri;
}
selectionChanged(root, selectedElements) {
this.handleSelectionChanged(root, selectedElements);
}
async handleSelectionChanged(root, selectedElementsIDs) {
var _a, _b;
const sourceUri = await this.getSourceUri();
const additionalSelectionData = (_b = (await ((_a = this.selectionDataService) === null || _a === void 0 ? void 0 : _a.getSelectionData(root, selectedElementsIDs)))) !== null && _b !== void 0 ? _b : undefined;
const glspSelection = {
selectedElementsIDs,
additionalSelectionData,
widgetId: this.viewerOptions.baseDiv,
sourceUri: sourceUri
};
this.theiaSelectionService.selection = glspSelection;
}
};
exports.TheiaGLSPSelectionForwarder = TheiaGLSPSelectionForwarder;
__decorate([
(0, inversify_1.inject)(GlspSelectionDataService),
(0, inversify_1.optional)(),
__metadata("design:type", GlspSelectionDataService)
], TheiaGLSPSelectionForwarder.prototype, "selectionDataService", void 0);
__decorate([
(0, inversify_1.inject)(client_1.TYPES.ViewerOptions),
__metadata("design:type", Object)
], TheiaGLSPSelectionForwarder.prototype, "viewerOptions", void 0);
__decorate([
(0, inversify_1.inject)(core_1.SelectionService),
__metadata("design:type", core_1.SelectionService)
], TheiaGLSPSelectionForwarder.prototype, "theiaSelectionService", void 0);
__decorate([
(0, inversify_1.inject)(client_1.TYPES.ModelSourceProvider),
__metadata("design:type", Function)
], TheiaGLSPSelectionForwarder.prototype, "modelSourceProvider", void 0);
__decorate([
(0, inversify_1.inject)(client_1.EditorContextService),
__metadata("design:type", client_1.EditorContextService)
], TheiaGLSPSelectionForwarder.prototype, "editorContext", void 0);
__decorate([
(0, inversify_1.postConstruct)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TheiaGLSPSelectionForwarder.prototype, "init", null);
__decorate([
(0, inversify_1.preDestroy)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TheiaGLSPSelectionForwarder.prototype, "dispose", null);
exports.TheiaGLSPSelectionForwarder = TheiaGLSPSelectionForwarder = __decorate([
(0, inversify_1.injectable)()
], TheiaGLSPSelectionForwarder);
//# sourceMappingURL=theia-glsp-selection-forwarder.js.map