UNPKG

@eclipse-glsp/client

Version:

A sprotty-based client for GLSP

98 lines 5.64 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.movementRestrictionFeedback = exports.removeMovementRestrictionFeedback = exports.createMovementRestrictionFeedback = exports.NoOverlapMovementRestrictor = void 0; /******************************************************************************** * Copyright (c) 2019-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 css_feedback_1 = require("../../base/feedback/css-feedback"); const viewpoint_util_1 = require("../../utils/viewpoint-util"); const model_1 = require("./model"); /** * A `IMovementRestrictor` that checks for overlapping elements. Move operations * are only valid if the element does not collide with another element after moving. */ let NoOverlapMovementRestrictor = class NoOverlapMovementRestrictor { constructor() { this.cssClasses = ['movement-not-allowed']; } validate(element, newLocation) { if (!(0, sprotty_1.isMoveable)(element) || !newLocation) { return false; } // Create ghost element at the newLocation const dimensions = (0, sprotty_1.isBoundsAware)(element) ? element.bounds : { width: 1, height: 1 }; const ghostElement = Object.create(element); ghostElement.bounds = { ...dimensions, ...newLocation }; ghostElement.type = 'Ghost'; ghostElement.id = element.id; return !Array.from(element.root.index .all() .filter(node => node.id !== ghostElement.id && node !== ghostElement.root && node instanceof sprotty_1.GNode) .map(node => node)).some(e => this.areOverlapping(e, ghostElement)); } isBoundsRelevant(element, ghostElement) { return element.id !== ghostElement.id && element !== ghostElement.root && element instanceof sprotty_1.GNode && (0, sprotty_1.isBoundsAware)(element); } areOverlapping(element1, element2) { return sprotty_1.Bounds.overlap((0, viewpoint_util_1.toAbsoluteBounds)(element1), (0, viewpoint_util_1.toAbsoluteBounds)(element2)); } }; exports.NoOverlapMovementRestrictor = NoOverlapMovementRestrictor; exports.NoOverlapMovementRestrictor = NoOverlapMovementRestrictor = __decorate([ (0, inversify_1.injectable)() ], NoOverlapMovementRestrictor); /** * Utility function to create an action that applies the given {@link IMovementRestrictor.cssClasses} to the given element. * @param element The element on which the css classes should be applied. * @param movementRestrictor The movement restrictor whose cssClasses should be applied. * @returns The corresponding {@link ModifyCSSFeedbackAction} */ function createMovementRestrictionFeedback(element, movementRestrictor) { const elements = [element]; if (element instanceof sprotty_1.GParentElement) { element.children.filter(child => child instanceof model_1.GResizeHandle).forEach(e => elements.push(e)); } return css_feedback_1.ModifyCSSFeedbackAction.create({ elements, add: movementRestrictor.cssClasses }); } exports.createMovementRestrictionFeedback = createMovementRestrictionFeedback; /** * Utility function to create an action that removes the given {@link IMovementRestrictor.cssClasses} from the given element. * @param element The element from which the css classes should be removed. * @param movementRestrictor The movement restrictor whose cssClasses should be removed. * @returns The corresponding {@link ModifyCSSFeedbackAction} */ function removeMovementRestrictionFeedback(element, movementRestrictor) { const elements = [element]; if (element instanceof sprotty_1.GParentElement) { element.children.filter(child => child instanceof model_1.GResizeHandle).forEach(e => elements.push(e)); } return css_feedback_1.ModifyCSSFeedbackAction.create({ elements, remove: movementRestrictor.cssClasses }); } exports.removeMovementRestrictionFeedback = removeMovementRestrictionFeedback; function movementRestrictionFeedback(element, movementRestrictor, valid) { return valid ? removeMovementRestrictionFeedback(element, movementRestrictor) : createMovementRestrictionFeedback(element, movementRestrictor); } exports.movementRestrictionFeedback = movementRestrictionFeedback; //# sourceMappingURL=movement-restrictor.js.map