@eclipse-glsp/client
Version:
A sprotty-based client for GLSP
152 lines • 7.31 kB
JavaScript
;
/********************************************************************************
* 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.MoveElementHandler = 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 viewpoint_util_1 = require("../../utils/viewpoint-util");
const move_element_action_1 = require("./move-element-action");
/**
* Action handler for moving elements.
*/
let MoveElementHandler = class MoveElementHandler {
init() {
this.moveFeedback = this.feedbackDispatcher.createEmitter();
}
handle(action) {
if (move_element_action_1.MoveElementRelativeAction.is(action)) {
this.handleMoveElement(action);
}
}
handleMoveElement(action) {
const modelRoot = this.editorContextService.modelRoot;
const viewport = (0, sprotty_1.findParentByFeature)(modelRoot, sprotty_1.isViewport);
if (!viewport) {
return;
}
const viewportActions = [];
const elementMoves = [];
const elements = (0, gmodel_util_1.getElements)(modelRoot.index, action.elementIds, gmodel_util_1.isSelectableAndBoundsAware);
for (const element of elements) {
const newPosition = this.getTargetBounds(element, action);
elementMoves.push({
elementId: element.id,
fromPosition: {
x: element.bounds.x,
y: element.bounds.y
},
toPosition: newPosition
});
const topLeftCorner = newPosition;
const bottomRightCorner = sprotty_1.Point.add(newPosition, { x: element.bounds.width, y: element.bounds.height });
if ((0, viewpoint_util_1.outsideOfViewport)(topLeftCorner, viewport) || (0, viewpoint_util_1.outsideOfViewport)(bottomRightCorner, viewport)) {
viewportActions.push(sprotty_1.MoveViewportAction.create({ moveX: action.moveX, moveY: action.moveY }));
}
}
this.dispatcher.dispatchAll(viewportActions);
const moveAction = sprotty_1.MoveAction.create(elementMoves, { animate: false });
this.moveFeedback.add(moveAction).submit();
this.scheduleChangeBounds(this.toElementAndBounds(elementMoves));
}
getTargetBounds(element, action) {
let position = sprotty_1.Point.add(element.bounds, { x: action.moveX, y: action.moveY });
if (this.snapper && action.snap) {
position = this.snapper.snap(position, element);
}
if (!(0, layout_utils_1.isValidMove)(element, position, this.movementRestrictor)) {
// reset to position before the move, if not valid
position = { x: element.bounds.x, y: element.bounds.y };
}
return position;
}
scheduleChangeBounds(elementAndBounds) {
var _a;
(_a = this.debouncedChangeBounds) === null || _a === void 0 ? void 0 : _a.cancel();
this.debouncedChangeBounds = (0, lodash_1.debounce)(() => {
this.moveFeedback.dispose();
this.dispatcher.dispatchAll([sprotty_1.ChangeBoundsOperation.create(elementAndBounds)]);
this.debouncedChangeBounds = undefined;
}, 300);
this.debouncedChangeBounds();
}
toElementAndBounds(elementMoves) {
const elementBounds = [];
for (const elementMove of elementMoves) {
const element = this.editorContextService.modelRoot.index.getById(elementMove.elementId);
if (element && (0, sprotty_1.isBoundsAware)(element)) {
elementBounds.push({
elementId: elementMove.elementId,
newSize: {
height: element.bounds.height,
width: element.bounds.width
},
newPosition: {
x: elementMove.toPosition.x,
y: elementMove.toPosition.y
}
});
}
}
return elementBounds;
}
};
exports.MoveElementHandler = MoveElementHandler;
__decorate([
(0, inversify_1.inject)(editor_context_service_1.EditorContextService),
__metadata("design:type", editor_context_service_1.EditorContextService)
], MoveElementHandler.prototype, "editorContextService", void 0);
__decorate([
(0, inversify_1.inject)(sprotty_1.TYPES.IActionDispatcher),
__metadata("design:type", Object)
], MoveElementHandler.prototype, "dispatcher", void 0);
__decorate([
(0, inversify_1.inject)(sprotty_1.TYPES.IFeedbackActionDispatcher),
__metadata("design:type", Object)
], MoveElementHandler.prototype, "feedbackDispatcher", void 0);
__decorate([
(0, inversify_1.inject)(sprotty_1.TYPES.ISnapper),
(0, inversify_1.optional)(),
__metadata("design:type", Object)
], MoveElementHandler.prototype, "snapper", void 0);
__decorate([
(0, inversify_1.inject)(sprotty_1.TYPES.IMovementRestrictor),
(0, inversify_1.optional)(),
__metadata("design:type", Object)
], MoveElementHandler.prototype, "movementRestrictor", void 0);
__decorate([
(0, inversify_1.postConstruct)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], MoveElementHandler.prototype, "init", null);
exports.MoveElementHandler = MoveElementHandler = __decorate([
(0, inversify_1.injectable)()
], MoveElementHandler);
//# sourceMappingURL=move-element-handler.js.map