@eclipse-glsp/client
Version:
A sprotty-based client for GLSP
227 lines • 9.92 kB
JavaScript
"use strict";
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.EditorContextService = void 0;
/********************************************************************************
* Copyright (c) 2020-2025 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
********************************************************************************/
const sprotty_1 = require("@eclipse-glsp/sprotty");
const inversify_1 = require("inversify");
const focus_tracker_1 = require("./focus/focus-tracker");
const selection_service_1 = require("./selection-service");
/**
* The `EditorContextService` is a central injectable component that gives read-only access to
* certain aspects of the diagram, such as the currently selected elements, the model root,
* the edit mode, the latest position of the mouse in the diagram.
*
* It has been introduced for two main reasons:
* 1. to simplify accessing the model root and the current selection from components that are
* not commands,
* 2. to conveniently create an EditorContext, which is a context object sent as part of several
* actions to the server to describe the current state of the editor (selection, last mouse
* position, etc.).
*/
let EditorContextService = class EditorContextService {
constructor() {
this.onEditModeChangedEmitter = new sprotty_1.Emitter();
this.onDirtyStateChangedEmitter = new sprotty_1.Emitter();
this.toDispose = new sprotty_1.DisposableCollection();
}
/**
* Event that is fired when the edit mode of the diagram changes i.e. after a {@link SetEditModeAction} has been handled.
*/
get onEditModeChanged() {
return this.onEditModeChangedEmitter.event;
}
/**
* Event that is fired when the dirty state of the diagram changes i.e. after a {@link SetDirtyStateAction} has been handled.
*/
get onDirtyStateChanged() {
return this.onDirtyStateChangedEmitter.event;
}
/**
* Event that is fired when the model root of the diagram changes i.e. after the `CommandStack` has processed a model update.
*/
get onModelRootChanged() {
return this.modelChangeService.onModelRootChanged;
}
/**
* Event that is fired when the focus state of the diagram changes i.e. after a {@link FocusStateChangedAction} has been handled
* by the {@link FocusTracker}.
*/
get onFocusChanged() {
return this.focusTracker.onFocusChanged;
}
/**
* Event that is fired when the selection of the diagram changes i.e. a selection change has been handled
* by the {@link SelectionService}.
*/
get onSelectionChanged() {
return this.selectionService.onSelectionChanged;
}
/**
* Event that is fired when the viewport of the diagram changes i.e. after the `CommandStack` has processed a viewport update.
* By default, this event is only fired if the viewport was changed via a `SetViewportCommand` or `BoundsAwareViewportCommand`
*/
get onViewportChanged() {
return this.modelChangeService.onViewportChanged;
}
initialize() {
var _a;
this._editMode = (_a = this.diagramOptions.editMode) !== null && _a !== void 0 ? _a : sprotty_1.EditMode.EDITABLE;
this.toDispose.push(this.onEditModeChangedEmitter, this.onDirtyStateChangedEmitter);
}
dispose() {
this.toDispose.dispose();
}
preLoadDiagram() {
this.lazyInjector.getAll(sprotty_1.TYPES.IGModelRootListener).forEach(listener => {
this.onModelRootChanged(event => listener.modelRootChanged(event));
});
this.lazyInjector.getAll(sprotty_1.TYPES.IEditModeListener).forEach(listener => {
this.onEditModeChanged(event => listener.editModeChanged(event.newValue, event.oldValue));
});
}
get(args) {
return {
selectedElementIds: Array.from(this.selectionService.getSelectedElementIDs()),
lastMousePosition: this.mousePositionTracker.lastPositionOnDiagram,
args
};
}
getWithSelection(selectedElementIds, args) {
return {
selectedElementIds,
lastMousePosition: this.mousePositionTracker.lastPositionOnDiagram,
args
};
}
handle(action) {
if (sprotty_1.SetEditModeAction.is(action)) {
this.handleSetEditModeAction(action);
}
else if (sprotty_1.SetDirtyStateAction.is(action)) {
this.handleSetDirtyStateAction(action);
}
}
handleSetEditModeAction(action) {
const oldValue = this._editMode;
this._editMode = action.editMode;
this.onEditModeChangedEmitter.fire({ newValue: this.editMode, oldValue });
}
handleSetDirtyStateAction(action) {
if (action.isDirty !== this._isDirty) {
this._isDirty = action.isDirty;
this.onDirtyStateChangedEmitter.fire(action);
}
}
get sourceUri() {
return this.diagramOptions.sourceUri;
}
get editMode() {
return this._editMode;
}
get diagramType() {
return this.diagramOptions.diagramType;
}
get clientId() {
return this.diagramOptions.clientId;
}
get modelRoot() {
if (!this.modelChangeService.currentRoot) {
throw new Error('Model root not available yet');
}
return this.modelChangeService.currentRoot;
}
get viewport() {
return this.modelRoot ? (0, sprotty_1.findParentByFeature)(this.modelRoot, sprotty_1.isViewport) : undefined;
}
get viewportData() {
var _a, _b;
const viewport = this.viewport;
// default values aligned with GetViewportCommand
return {
scroll: (_a = viewport === null || viewport === void 0 ? void 0 : viewport.scroll) !== null && _a !== void 0 ? _a : sprotty_1.Point.ORIGIN,
zoom: (_b = viewport === null || viewport === void 0 ? void 0 : viewport.zoom) !== null && _b !== void 0 ? _b : 1
};
}
get canvasBounds() {
var _a, _b;
// default value aligned with the initialization of canvasBounds in GModelRoot
return (_b = (_a = this.modelRoot) === null || _a === void 0 ? void 0 : _a.canvasBounds) !== null && _b !== void 0 ? _b : sprotty_1.Bounds.EMPTY;
}
get selectedElements() {
return this.selectionService.getSelectedElements();
}
get isReadonly() {
return this.editMode === sprotty_1.EditMode.READONLY;
}
get isDirty() {
return this._isDirty;
}
};
exports.EditorContextService = EditorContextService;
__decorate([
(0, inversify_1.inject)(selection_service_1.SelectionService),
__metadata("design:type", selection_service_1.SelectionService)
], EditorContextService.prototype, "selectionService", void 0);
__decorate([
(0, inversify_1.inject)(sprotty_1.TYPES.IModelChangeService),
__metadata("design:type", Object)
], EditorContextService.prototype, "modelChangeService", void 0);
__decorate([
(0, inversify_1.inject)(sprotty_1.MousePositionTracker),
__metadata("design:type", sprotty_1.MousePositionTracker)
], EditorContextService.prototype, "mousePositionTracker", void 0);
__decorate([
(0, inversify_1.inject)(sprotty_1.LazyInjector),
__metadata("design:type", Object)
], EditorContextService.prototype, "lazyInjector", void 0);
__decorate([
(0, inversify_1.inject)(sprotty_1.TYPES.IDiagramOptions),
__metadata("design:type", Object)
], EditorContextService.prototype, "diagramOptions", void 0);
__decorate([
(0, inversify_1.inject)(sprotty_1.TYPES.IActionDispatcher),
__metadata("design:type", Object)
], EditorContextService.prototype, "actionDispatcher", void 0);
__decorate([
(0, inversify_1.inject)(focus_tracker_1.FocusTracker),
__metadata("design:type", focus_tracker_1.FocusTracker)
], EditorContextService.prototype, "focusTracker", void 0);
__decorate([
(0, inversify_1.postConstruct)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], EditorContextService.prototype, "initialize", null);
__decorate([
(0, inversify_1.preDestroy)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], EditorContextService.prototype, "dispose", null);
exports.EditorContextService = EditorContextService = __decorate([
(0, inversify_1.injectable)()
], EditorContextService);
//# sourceMappingURL=editor-context-service.js.map