@eclipse-glsp/client
Version:
A sprotty-based client for GLSP
125 lines • 6.96 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NoOverlapMovementRestrictor = void 0;
exports.createMovementRestrictionFeedback = createMovementRestrictionFeedback;
exports.removeMovementRestrictionFeedback = removeMovementRestrictionFeedback;
exports.movementRestrictionFeedback = movementRestrictionFeedback;
/********************************************************************************
* Copyright (c) 2019-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 css_feedback_1 = require("../../base/feedback/css-feedback");
const gmodel_util_1 = require("../../utils/gmodel-util");
const viewpoint_util_1 = require("../../utils/viewpoint-util");
const model_1 = require("../hints/model");
const model_2 = require("./model");
/**
* A `IMovementRestrictor` that checks for overlapping elements. Move operations
* are only valid if the element does not collide with another element/node after moving.
*/
let NoOverlapMovementRestrictor = class NoOverlapMovementRestrictor {
constructor() {
this.cssClasses = ['movement-not-allowed'];
}
validate(element, newLocation) {
if (!(element instanceof sprotty_1.GChildElement) || !(0, sprotty_1.isMoveable)(element) || !newLocation) {
return false;
}
const moveContext = this.createMoveElementContext(element, newLocation);
const elementsToValidate = Array.from(element.root.index.all()).filter(e => this.isBoundsRelevant(e, moveContext));
const valid = !elementsToValidate.some(e => this.areOverlapping(e, moveContext.elementAtNewLocation));
return valid;
}
createMoveElementContext(element, newLocation) {
const parentContainers = (0, gmodel_util_1.getParents)(element, model_1.isContainable);
const childNodes = (0, gmodel_util_1.getChildren)(element, (0, sprotty_1.toTypeGuard)(sprotty_1.GNode));
// Create a mock element at the newLocation for overlap checking
const dimensions = (0, sprotty_1.isBoundsAware)(element) ? element.bounds : { width: 1, height: 1 };
const elementAtNewLocation = Object.create(element);
elementAtNewLocation.bounds = { ...dimensions, ...newLocation };
return {
element,
elementAtNewLocation,
parentContainers,
childNodes
};
}
isBoundsRelevant(element, moveContext) {
var _a;
// Only consider GNodes that are not the element being moved (or one of its children)
if (!(element instanceof sprotty_1.GNode) ||
element.id === moveContext.element.id ||
moveContext.childNodes.some(child => child.id === element.id)) {
return false;
}
// Do not consider parent containers of the element being moved
if (moveContext.parentContainers.length > 0 && moveContext.parentContainers.some(container => container.id === element.id)) {
return false;
}
// If the element is a ghost element (node creation), don't consider overlap checks for potential parent containers
if (((_a = moveContext.element.cssClasses) === null || _a === void 0 ? void 0 : _a.includes(css_feedback_1.CSS_GHOST_ELEMENT)) &&
(0, model_1.isContainable)(element) &&
element.isContainableElement(moveContext.element.type)) {
return false;
}
return true;
}
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_2.GResizeHandle).forEach(e => elements.push(e));
}
return css_feedback_1.ModifyCSSFeedbackAction.create({ elements, add: movementRestrictor.cssClasses });
}
/**
* 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_2.GResizeHandle).forEach(e => elements.push(e));
}
return css_feedback_1.ModifyCSSFeedbackAction.create({ elements, remove: movementRestrictor.cssClasses });
}
function movementRestrictionFeedback(element, movementRestrictor, valid) {
return valid
? removeMovementRestrictionFeedback(element, movementRestrictor)
: createMovementRestrictionFeedback(element, movementRestrictor);
}
//# sourceMappingURL=movement-restrictor.js.map