UNPKG

@eclipse-glsp/client

Version:

A sprotty-based client for GLSP

214 lines 9.2 kB
"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-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 ********************************************************************************/ 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(); /** * Event that is fired when the model root of the diagram changes i.e. after the `CommandStack` has processed a model update. */ this.onModelRootChangedEmitter = 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; } get onModelRootChanged() { return this.onModelRootChangedEmitter.event; } /** * 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; } 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); } 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)); }); } dispose() { this.toDispose.dispose(); } get(args) { return { selectedElementIds: Array.from(this.selectionService.getSelectedElementIDs()), lastMousePosition: this.mousePositionTracker.lastPositionOnDiagram, args }; } getWithSelection(selectedElementIds, args) { return { selectedElementIds, lastMousePosition: this.mousePositionTracker.lastPositionOnDiagram, args }; } /** * Notifies the service about a model root change. This method should not be called * directly. It is called by the `CommandStack` after a model update has been processed. * @throws an error if the notifier is not a `CommandStack` * @param root the new model root * @param notifier the object that triggered the model root change */ notifyModelRootChanged(root, notifier) { if (!(notifier instanceof sprotty_1.CommandStack)) { throw new Error('Invalid model root change notification. Notifier is not an instance of `CommandStack`.'); } this._modelRoot = root; this.onModelRootChangedEmitter.fire(root); } 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._modelRoot) { throw new Error('Model root not available yet'); } return this._modelRoot; } 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.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