UNPKG

@eclipse-glsp/client

Version:

A sprotty-based client for GLSP

206 lines 9.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SResizeHandle = exports.removeResizeHandles = exports.addResizeHandles = exports.GResizeHandle = exports.isBoundsAwareMoveable = exports.ResizeHandleLocation = exports.isResizable = exports.resizeFeature = 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 css_feedback_1 = require("../../base/feedback/css-feedback"); exports.resizeFeature = Symbol('resizeFeature'); function isResizable(element) { return (0, sprotty_1.isBoundsAware)(element) && (0, sprotty_1.isSelectable)(element) && element instanceof sprotty_1.GParentElement && element.hasFeature(exports.resizeFeature); } exports.isResizable = isResizable; // eslint-disable-next-line no-shadow var ResizeHandleLocation; (function (ResizeHandleLocation) { ResizeHandleLocation["TopLeft"] = "top-left"; ResizeHandleLocation["Top"] = "top"; ResizeHandleLocation["TopRight"] = "top-right"; ResizeHandleLocation["Right"] = "right"; ResizeHandleLocation["BottomRight"] = "bottom-right"; ResizeHandleLocation["Bottom"] = "bottom"; ResizeHandleLocation["BottomLeft"] = "bottom-left"; ResizeHandleLocation["Left"] = "left"; })(ResizeHandleLocation || (exports.ResizeHandleLocation = ResizeHandleLocation = {})); (function (ResizeHandleLocation) { ResizeHandleLocation.CORNERS = [ ResizeHandleLocation.TopLeft, ResizeHandleLocation.TopRight, ResizeHandleLocation.BottomRight, ResizeHandleLocation.BottomLeft ]; ResizeHandleLocation.CROSS = [ ResizeHandleLocation.Top, ResizeHandleLocation.Right, ResizeHandleLocation.Bottom, ResizeHandleLocation.Left ]; ResizeHandleLocation.ALL = [...ResizeHandleLocation.CORNERS, ...ResizeHandleLocation.CROSS]; function opposite(location) { switch (location) { case ResizeHandleLocation.TopLeft: return ResizeHandleLocation.BottomRight; case ResizeHandleLocation.Top: return ResizeHandleLocation.Bottom; case ResizeHandleLocation.TopRight: return ResizeHandleLocation.BottomLeft; case ResizeHandleLocation.Right: return ResizeHandleLocation.Left; case ResizeHandleLocation.BottomRight: return ResizeHandleLocation.TopLeft; case ResizeHandleLocation.Bottom: return ResizeHandleLocation.Top; case ResizeHandleLocation.BottomLeft: return ResizeHandleLocation.TopRight; case ResizeHandleLocation.Left: return ResizeHandleLocation.Right; } } ResizeHandleLocation.opposite = opposite; function direction(location) { switch (location) { case ResizeHandleLocation.TopLeft: return [sprotty_1.Direction.Up, sprotty_1.Direction.Left]; case ResizeHandleLocation.Top: return [sprotty_1.Direction.Up]; case ResizeHandleLocation.TopRight: return [sprotty_1.Direction.Up, sprotty_1.Direction.Right]; case ResizeHandleLocation.Right: return [sprotty_1.Direction.Right]; case ResizeHandleLocation.BottomRight: return [sprotty_1.Direction.Down, sprotty_1.Direction.Right]; case ResizeHandleLocation.Bottom: return [sprotty_1.Direction.Down]; case ResizeHandleLocation.BottomLeft: return [sprotty_1.Direction.Down, sprotty_1.Direction.Left]; case ResizeHandleLocation.Left: return [sprotty_1.Direction.Left]; } } ResizeHandleLocation.direction = direction; })(ResizeHandleLocation || (exports.ResizeHandleLocation = ResizeHandleLocation = {})); function isBoundsAwareMoveable(element) { return (0, sprotty_1.isMoveable)(element) && (0, sprotty_1.isBoundsAware)(element); } exports.isBoundsAwareMoveable = isBoundsAwareMoveable; class GResizeHandle extends sprotty_1.GChildElement { constructor(location, type = GResizeHandle.TYPE, hoverFeedback = false) { super(); this.location = location; this.type = type; this.hoverFeedback = hoverFeedback; } hasFeature(feature) { return feature === sprotty_1.hoverFeedbackFeature; } isNwResize() { return this.location === ResizeHandleLocation.TopLeft; } isNResize() { return this.location === ResizeHandleLocation.Top; } isNeResize() { return this.location === ResizeHandleLocation.TopRight; } isEResize() { return this.location === ResizeHandleLocation.Right; } isSeResize() { return this.location === ResizeHandleLocation.BottomRight; } isSResize() { return this.location === ResizeHandleLocation.Bottom; } isSwResize() { return this.location === ResizeHandleLocation.BottomLeft; } isWResize() { return this.location === ResizeHandleLocation.Left; } isNwSeResize() { return this.isNwResize() || this.isSeResize(); } isNeSwResize() { return this.isNeResize() || this.isSwResize(); } static getHandlePosition(first, second) { const bounds = GResizeHandle.is(first) ? first.parent.bounds : first instanceof sprotty_1.GModelElement ? first.bounds : first; const location = GResizeHandle.is(first) ? first.location : second; switch (location) { case ResizeHandleLocation.TopLeft: return sprotty_1.Bounds.topLeft(bounds); case ResizeHandleLocation.Top: return sprotty_1.Bounds.topCenter(bounds); case ResizeHandleLocation.TopRight: return sprotty_1.Bounds.topRight(bounds); case ResizeHandleLocation.Right: return sprotty_1.Bounds.middleRight(bounds); case ResizeHandleLocation.BottomRight: return sprotty_1.Bounds.bottomRight(bounds); case ResizeHandleLocation.Bottom: return sprotty_1.Bounds.bottomCenter(bounds); case ResizeHandleLocation.BottomLeft: return sprotty_1.Bounds.bottomLeft(bounds); case ResizeHandleLocation.Left: return sprotty_1.Bounds.middleLeft(bounds); } } static getCursorCss(handle) { switch (handle.location) { case ResizeHandleLocation.TopLeft: return css_feedback_1.CursorCSS.RESIZE_NW; case ResizeHandleLocation.Top: return css_feedback_1.CursorCSS.RESIZE_N; case ResizeHandleLocation.TopRight: return css_feedback_1.CursorCSS.RESIZE_NE; case ResizeHandleLocation.Right: return css_feedback_1.CursorCSS.RESIZE_E; case ResizeHandleLocation.BottomRight: return css_feedback_1.CursorCSS.RESIZE_SE; case ResizeHandleLocation.Bottom: return css_feedback_1.CursorCSS.RESIZE_S; case ResizeHandleLocation.BottomLeft: return css_feedback_1.CursorCSS.RESIZE_SW; case ResizeHandleLocation.Left: return css_feedback_1.CursorCSS.RESIZE_W; } } static is(handle) { return typeof handle === 'object' && !!handle && 'type' in handle && handle.type === GResizeHandle.TYPE; } } exports.GResizeHandle = GResizeHandle; exports.SResizeHandle = GResizeHandle; GResizeHandle.TYPE = 'resize-handle'; function addResizeHandles(element, locations = ResizeHandleLocation.CORNERS) { for (const location of ResizeHandleLocation.ALL) { const existing = element.children.find(child => child instanceof GResizeHandle && child.location === location); if (locations.includes(location) && !existing) { // add missing handle element.add(new GResizeHandle(location)); } else if (!locations.includes(location) && existing) { // remove existing handle element.remove(existing); } } } exports.addResizeHandles = addResizeHandles; function removeResizeHandles(element) { element.removeAll(child => child instanceof GResizeHandle); } exports.removeResizeHandles = removeResizeHandles; //# sourceMappingURL=model.js.map