Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLSP-1423 Add MovementBehavior to control invalid multi element movements #397

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,18 @@ export class FeedbackMoveMouseListener extends DragAwareMouseListener {
if (!this.tracker.isTracking()) {
return [];
}
// only reset the move of invalid elements, the others will be handled by the change bounds tool itself
this.getElementsToMove(target)
.filter(element => this.tool.changeBoundsManager.isValid(element))
.forEach(element => this.elementId2startPos.delete(element.id));
const elementsToMove = this.getElementsToMove(target);
if (!this.tool.movementOptions.allElementsNeedToBeValid) {
// only reset the move of invalid elements, the others will be handled by the change bounds tool itself
elementsToMove
.filter(element => this.tool.changeBoundsManager.isValid(element))
.forEach(element => this.elementId2startPos.delete(element.id));
} else {
if (elementsToMove.every(element => this.tool.changeBoundsManager.isValid(element))) {
// do not reset any element as all are valid
this.elementId2startPos.clear();
}
}
this.dispose();
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ import {
import { FeedbackMoveMouseListener } from './change-bounds-tool-move-feedback';
import { ChangeBoundsTracker, TrackedElementResize, TrackedResize } from './change-bounds-tracker';

export interface IMovementOptions {
/** If set to true, a move with multiple elements is only performed if each individual move is valid. */
readonly allElementsNeedToBeValid: boolean;
ivy-lli marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* The change bounds tool has the license to move multiple elements or resize a single element by implementing the ChangeBounds operation.
* In contrast to Sprotty's implementation this tool only sends a `ChangeBoundsOperationAction` when an operation has finished and does not
Expand All @@ -84,6 +89,7 @@ export class ChangeBoundsTool extends BaseEditTool {
@inject(EdgeRouterRegistry) @optional() readonly edgeRouterRegistry?: EdgeRouterRegistry;
@inject(TYPES.IMovementRestrictor) @optional() readonly movementRestrictor?: IMovementRestrictor;
@inject(TYPES.IChangeBoundsManager) readonly changeBoundsManager: IChangeBoundsManager;
@inject(TYPES.IMovementOptions) @optional() readonly movementOptions: IMovementOptions = { allElementsNeedToBeValid: true };

get id(): string {
return ChangeBoundsTool.ID;
Expand Down Expand Up @@ -260,7 +266,11 @@ export class ChangeBoundsListener extends DragAwareMouseListener implements ISel
protected getElementsToMove(target: GModelElement): SelectableBoundsAware[] {
const selectedElements = getMatchingElements(target.index, isNonRoutableSelectedMovableBoundsAware);
const selectionSet: Set<BoundsAwareModelElement> = new Set(selectedElements);
return selectedElements.filter(element => this.isValidMove(element, selectionSet));
const elementsToMove = selectedElements.filter(element => this.isValidMove(element, selectionSet));
if (this.tool.movementOptions.allElementsNeedToBeValid && elementsToMove.length !== selectionSet.size) {
return [];
}
return elementsToMove;
}

protected handleMoveElementsOnServer(elementsToMove: SelectableBoundsAware[]): Operation[] {
Expand Down
1 change: 1 addition & 0 deletions packages/glsp-sprotty/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const TYPES = {
IToolFactory: Symbol('Factory<Tool>'),
ITypeHintProvider: Symbol('ITypeHintProvider'),
IMovementRestrictor: Symbol('IMovementRestrictor'),
IMovementOptions: Symbol('IMovementOptions'),
ISelectionListener: Symbol('ISelectionListener'),
/** @deprecated Use {@link TYPES.IGModelRootListener} instead */
// eslint-disable-next-line deprecation/deprecation
Expand Down
Loading