UNPKG

@eclipse-glsp/client

Version:

A sprotty-based client for GLSP

143 lines 6.97 kB
"use strict"; /******************************************************************************** * 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 ********************************************************************************/ 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.ResizeElementHandler = exports.ResizeElementAction = exports.ResizeType = void 0; const sprotty_1 = require("@eclipse-glsp/sprotty"); const inversify_1 = require("inversify"); const lodash_1 = require("lodash"); const editor_context_service_1 = require("../../../base/editor-context-service"); const gmodel_util_1 = require("../../../utils/gmodel-util"); const layout_utils_1 = require("../../../utils/layout-utils"); const grid_1 = require("../../grid/grid"); var ResizeType; (function (ResizeType) { ResizeType[ResizeType["Increase"] = 0] = "Increase"; ResizeType[ResizeType["Decrease"] = 1] = "Decrease"; ResizeType[ResizeType["MinSize"] = 2] = "MinSize"; })(ResizeType || (exports.ResizeType = ResizeType = {})); var ResizeElementAction; (function (ResizeElementAction) { ResizeElementAction.KIND = 'resizeElementAction'; function is(object) { return sprotty_1.Action.hasKind(object, ResizeElementAction.KIND); } ResizeElementAction.is = is; function create(elementIds, resizeType) { return { kind: ResizeElementAction.KIND, elementIds, resizeType }; } ResizeElementAction.create = create; })(ResizeElementAction || (exports.ResizeElementAction = ResizeElementAction = {})); /* * The ResizeElementHandler class is an implementation of the IActionHandler interface that handles * resizing of elements. */ let ResizeElementHandler = class ResizeElementHandler { constructor() { this.grid = grid_1.Grid.DEFAULT; } init() { this.resizeFeedback = this.feedbackDispatcher.createEmitter(); } handle(action) { if (ResizeElementAction.is(action)) { this.handleResizeElement(action); } } handleResizeElement(action) { var _a; const elements = (0, gmodel_util_1.getElements)(this.editorContextService.modelRoot.index, action.elementIds, gmodel_util_1.isSelectableAndBoundsAware); const elementAndBounds = this.computeElementAndBounds(elements, action); this.resizeFeedback.add(sprotty_1.SetBoundsAction.create(elementAndBounds)).submit(); (_a = this.debouncedChangeBounds) === null || _a === void 0 ? void 0 : _a.cancel(); this.debouncedChangeBounds = (0, lodash_1.debounce)(() => { this.resizeFeedback.dispose(); this.dispatcher.dispatchAll([sprotty_1.ChangeBoundsOperation.create(elementAndBounds)]); this.debouncedChangeBounds = undefined; }, 300); this.debouncedChangeBounds(); } computeElementAndBounds(elements, action) { const elementAndBounds = []; elements.forEach(element => { const { x, y, width: oldWidth, height: oldHeight } = element.bounds; let width = 0; let height = 0; if (action.resizeType === ResizeType.Decrease) { width = oldWidth - this.grid.x; height = oldHeight - this.grid.y; } else if (action.resizeType === ResizeType.Increase) { width = oldWidth + this.grid.x; height = oldHeight + this.grid.y; } else if (action.resizeType === ResizeType.MinSize) { width = (0, layout_utils_1.minWidth)(element); height = (0, layout_utils_1.minHeight)(element); } if (this.isValidBoundChange(element, { x, y }, { width, height })) { const resizeElement = { id: element.id, bounds: { x, y, width, height } }; elementAndBounds.push((0, gmodel_util_1.toElementAndBounds)(resizeElement)); } }); return elementAndBounds; } isValidBoundChange(element, newPosition, newSize) { return (0, layout_utils_1.isValidSize)(element, newSize) && (0, layout_utils_1.isValidMove)(element, newPosition); } }; exports.ResizeElementHandler = ResizeElementHandler; __decorate([ (0, inversify_1.inject)(editor_context_service_1.EditorContextService), __metadata("design:type", editor_context_service_1.EditorContextService) ], ResizeElementHandler.prototype, "editorContextService", void 0); __decorate([ (0, inversify_1.inject)(sprotty_1.TYPES.IActionDispatcher), __metadata("design:type", Object) ], ResizeElementHandler.prototype, "dispatcher", void 0); __decorate([ (0, inversify_1.inject)(sprotty_1.TYPES.IFeedbackActionDispatcher), __metadata("design:type", Object) ], ResizeElementHandler.prototype, "feedbackDispatcher", void 0); __decorate([ (0, inversify_1.inject)(sprotty_1.TYPES.Grid), (0, inversify_1.optional)(), __metadata("design:type", Object) ], ResizeElementHandler.prototype, "grid", void 0); __decorate([ (0, inversify_1.inject)(sprotty_1.TYPES.ISnapper), (0, inversify_1.optional)(), __metadata("design:type", Object) ], ResizeElementHandler.prototype, "snapper", void 0); __decorate([ (0, inversify_1.postConstruct)(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], ResizeElementHandler.prototype, "init", null); exports.ResizeElementHandler = ResizeElementHandler = __decorate([ (0, inversify_1.injectable)() ], ResizeElementHandler); //# sourceMappingURL=resize-handler.js.map