@eclipse-glsp/client
Version:
A sprotty-based client for GLSP
131 lines • 5.57 kB
JavaScript
;
/********************************************************************************
* 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
********************************************************************************/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAbsolutePosition = getAbsolutePosition;
exports.getAbsolutePositionByPoint = getAbsolutePositionByPoint;
exports.getViewportBounds = getViewportBounds;
exports.toAbsoluteBounds = toAbsoluteBounds;
exports.toAbsolutePosition = toAbsolutePosition;
exports.toAbsoluteSize = toAbsoluteSize;
exports.absoluteToParent = absoluteToParent;
exports.absoluteToLocal = absoluteToLocal;
exports.outsideOfViewport = outsideOfViewport;
const sprotty_1 = require("@eclipse-glsp/sprotty");
/**
* Return the position corresponding to this mouse event (Browser coordinates)
* in the diagram coordinates system (i.e. relative to the Diagram's 0;0 point)
*
* This functions takes into account the following transformations:
* - Location of the Diagram Canvas inside of the browser's page
* - Current viewport Scroll and Zoom
*
* @param target
* An element from the diagram
* @param mouseEvent
* A mouseEvent
*/
function getAbsolutePosition(target, mouseEvent) {
return getAbsolutePositionByPoint(target, { x: mouseEvent.pageX, y: mouseEvent.pageY });
}
function getAbsolutePositionByPoint(target, point) {
let position = sprotty_1.Point.subtract(point, target.root.canvasBounds);
const viewport = (0, sprotty_1.findParentByFeature)(target, sprotty_1.isViewport);
if (viewport) {
const zoom = viewport.zoom;
const zoomedScroll = sprotty_1.Point.multiplyScalar(viewport.scroll, zoom);
position = sprotty_1.Point.add(position, zoomedScroll);
position = sprotty_1.Point.divideScalar(position, zoom);
}
return position;
}
function getViewportBounds(target, bounds) {
const topLeft = getAbsolutePositionByPoint(target, sprotty_1.Bounds.topLeft(bounds));
const bottomRight = getAbsolutePositionByPoint(target, sprotty_1.Bounds.bottomRight(bounds));
return sprotty_1.Bounds.from(topLeft, bottomRight);
}
/**
* Translates the bounds of the diagram element (local coordinates) into the diagram coordinates system
* (i.e. relative to the Diagram's 0;0 point)
*
* @param target A bounds-aware element from the diagram
*/
function toAbsoluteBounds(element) {
const location = (0, sprotty_1.isAlignable)(element) ? element.alignment : sprotty_1.Point.ORIGIN;
const x = location.x;
const y = location.y;
const width = element.bounds.width;
const height = element.bounds.height;
return (0, sprotty_1.translateBounds)({ x, y, width, height }, element, element.root);
}
/**
* Translates the position of the diagram element (local coordinates) into the diagram coordinates system
* (i.e. relative to the Diagram's 0;0 point)
*
* @param target A bounds-aware element from the diagram
*/
function toAbsolutePosition(target) {
return toAbsoluteBounds(target);
}
/**
* Translates the size of the diagram element (local coordinates) into the diagram coordinates system
* (i.e. relative to the Diagram's 0;0 point)
*
* @param target A bounds-aware element from the diagram
*/
function toAbsoluteSize(target) {
return toAbsoluteBounds(target);
}
/**
* Convert a point, specified in absolute coordinates, to a point relative
* to the parent of the specified child element.
*
* @param element the child element
* @param absolutePoint a point in absolute coordinates
* @returns the equivalent point, relative to the element's parent coordinates
*/
function absoluteToParent(element, absolutePoint) {
if ((0, sprotty_1.isBoundsAware)(element.parent)) {
return absoluteToLocal(element.parent, absolutePoint);
}
// If the parent is not bounds-aware, assume it's at 0; 0 and proceed
return absoluteToLocal(element, absolutePoint);
}
/**
* Convert a point, specified in absolute coordinates, to a point relative
* to the specified element.
*
* @param element the element
* @param absolutePoint a point in absolute coordinates
* @returns the equivalent point, relative to the element's coordinates
*/
function absoluteToLocal(element, absolutePoint) {
const absoluteElementBounds = toAbsoluteBounds(element);
return sprotty_1.Point.subtract(absolutePoint, absoluteElementBounds);
}
/**
* Returns `true` if `point` is outside of the `viewport`.
* @param point The point to check.
* @param viewport The viewport.
* @returns `true` if `point` is outside, `false` otherwise.
*/
function outsideOfViewport(point, viewport) {
return (point.x < viewport.scroll.x ||
point.x > viewport.scroll.x + viewport.canvasBounds.width / viewport.zoom ||
point.y < viewport.scroll.y ||
point.y > viewport.scroll.y + viewport.canvasBounds.height / viewport.zoom);
}
//# sourceMappingURL=viewpoint-util.js.map