@eclipse-glsp/client
Version:
A sprotty-based client for GLSP
191 lines • 8.5 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.ZoomHandler = exports.MoveViewportHandler = exports.RestoreViewportHandler = void 0;
/********************************************************************************
* Copyright (c) 2023-2024 Business Informatics Group (TU Wien) 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 editor_context_service_1 = require("../../base/editor-context-service");
const focus_tracker_1 = require("../../base/focus/focus-tracker");
const tool_1 = require("../../base/tool-manager/tool");
const gmodel_util_1 = require("../../utils/gmodel-util");
const actions_1 = require("../accessibility/actions");
/**
* Focuses the graph on different actions.
*/
let RestoreViewportHandler = class RestoreViewportHandler {
handle(action) {
if (tool_1.EnableDefaultToolsAction.is(action) || (actions_1.FocusDomAction.is(action) && action.id === 'graph')) {
this.focusGraph();
}
}
get graphSelector() {
const rootId = CSS.escape(this.domHelper.createUniqueDOMElementId(this.editorContext.modelRoot));
return `#${rootId}`;
}
async postRequestModel() {
await this.waitForElement(this.graphSelector);
this.focusGraph();
}
focusGraph() {
var _a;
if (this.focusTracker.hasFocus) {
const container = (_a = this.focusTracker.diagramElement) === null || _a === void 0 ? void 0 : _a.querySelector(this.graphSelector);
container === null || container === void 0 ? void 0 : container.focus();
}
}
waitForElement(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver(mutations => {
if (document.querySelector(selector)) {
observer.disconnect();
resolve(document.querySelector(selector));
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
};
exports.RestoreViewportHandler = RestoreViewportHandler;
__decorate([
(0, inversify_1.inject)(sprotty_1.TYPES.DOMHelper),
__metadata("design:type", sprotty_1.DOMHelper)
], RestoreViewportHandler.prototype, "domHelper", void 0);
__decorate([
(0, inversify_1.inject)(focus_tracker_1.FocusTracker),
__metadata("design:type", focus_tracker_1.FocusTracker)
], RestoreViewportHandler.prototype, "focusTracker", void 0);
__decorate([
(0, inversify_1.inject)(editor_context_service_1.EditorContextService),
__metadata("design:type", editor_context_service_1.EditorContextService)
], RestoreViewportHandler.prototype, "editorContext", void 0);
exports.RestoreViewportHandler = RestoreViewportHandler = __decorate([
(0, inversify_1.injectable)()
], RestoreViewportHandler);
/**
* Handles moving the viewport.
*/
let MoveViewportHandler = class MoveViewportHandler {
handle(action) {
return this.handleMoveViewport(action);
}
handleMoveViewport(action) {
const viewport = (0, sprotty_1.findParentByFeature)(this.editorContextService.modelRoot, sprotty_1.isViewport);
if (!viewport) {
return;
}
const newViewport = {
scroll: {
x: viewport.scroll.x + action.moveX,
y: viewport.scroll.y + action.moveY
},
zoom: viewport.zoom
};
return sprotty_1.SetViewportAction.create(viewport.id, newViewport, { animate: false });
}
};
exports.MoveViewportHandler = MoveViewportHandler;
__decorate([
(0, inversify_1.inject)(editor_context_service_1.EditorContextService),
__metadata("design:type", editor_context_service_1.EditorContextService)
], MoveViewportHandler.prototype, "editorContextService", void 0);
exports.MoveViewportHandler = MoveViewportHandler = __decorate([
(0, inversify_1.injectable)()
], MoveViewportHandler);
/*
* Handles zooming in and out of the viewport.
*/
let ZoomHandler = class ZoomHandler {
handle(action) {
if (action.elementIds) {
return this.handleZoomElement(action.elementIds, action.zoomFactor);
}
else {
return this.handleZoomViewport(action.zoomFactor);
}
}
handleZoomViewport(zoomFactor) {
const viewport = (0, sprotty_1.findParentByFeature)(this.editorContextService.modelRoot, sprotty_1.isViewport);
if (!viewport) {
return;
}
const newZoom = viewport.zoom * zoomFactor;
const newViewport = {
scroll: viewport.scroll,
zoom: newZoom
};
return sprotty_1.SetViewportAction.create(viewport.id, newViewport, { animate: false });
}
handleZoomElement(elementIds, zoomFactor) {
const viewport = (0, sprotty_1.findParentByFeature)(this.editorContextService.modelRoot, sprotty_1.isViewport);
if (!viewport) {
return;
}
const elements = (0, gmodel_util_1.getElements)(this.editorContextService.modelRoot.index, elementIds, gmodel_util_1.isSelectableAndBoundsAware);
const center = this.getCenter(viewport, elements);
const newZoom = viewport.zoom * zoomFactor;
const newViewport = {
scroll: {
x: center.x - (0.5 * viewport.canvasBounds.width) / newZoom,
y: center.y - (0.5 * viewport.canvasBounds.height) / newZoom
},
zoom: newZoom
};
return sprotty_1.SetViewportAction.create(viewport.id, newViewport, { animate: false });
}
getCenter(viewport, selectedElements) {
// Get bounds of elements based on the viewport
const allBounds = selectedElements.map(e => this.boundsInViewport(viewport, e, e.bounds));
const mergedBounds = allBounds.reduce((b0, b1) => sprotty_1.Bounds.combine(b0, b1));
return sprotty_1.Bounds.center(mergedBounds);
}
boundsInViewport(viewport, element, bounds) {
if (element instanceof sprotty_1.GChildElement && element.parent !== viewport) {
return this.boundsInViewport(viewport, element.parent, element.parent.localToParent(bounds));
}
else {
return bounds;
}
}
};
exports.ZoomHandler = ZoomHandler;
__decorate([
(0, inversify_1.inject)(editor_context_service_1.EditorContextService),
__metadata("design:type", editor_context_service_1.EditorContextService)
], ZoomHandler.prototype, "editorContextService", void 0);
__decorate([
(0, inversify_1.inject)(sprotty_1.TYPES.IActionDispatcher),
__metadata("design:type", Object)
], ZoomHandler.prototype, "actionDispatcher", void 0);
exports.ZoomHandler = ZoomHandler = __decorate([
(0, inversify_1.injectable)()
], ZoomHandler);
//# sourceMappingURL=viewport-handler.js.map