Skip to content

Commit

Permalink
Add generic args property to all operations (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
tortmayr authored Dec 18, 2023
1 parent 8691584 commit f286b9d
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 25 deletions.
11 changes: 9 additions & 2 deletions packages/protocol/src/action-protocol/base-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
********************************************************************************/
import * as sprotty from 'sprotty-protocol/lib/actions';
import { AnyObject, TypeGuard, hasArrayProp, hasStringProp } from '../utils/type-util';
import { Args } from './types';

/**
* An action is a declarative description of a behavior that shall be invoked by the receiver upon receipt of the action.
Expand Down Expand Up @@ -185,6 +186,11 @@ export interface Operation extends Action {
* Discriminator property to make operations distinguishable from plain {@link Action}s.
*/
isOperation: true;

/**
* Optional custom arguments.
*/
args?: Args;
}

export namespace Operation {
Expand Down Expand Up @@ -223,11 +229,12 @@ export namespace CompoundOperation {
return Operation.hasKind(object, KIND) && hasArrayProp(object, 'operationList');
}

export function create(operationList: Operation[]): CompoundOperation {
export function create(operationList: Operation[], options: { args?: Args } = {}): CompoundOperation {
return {
kind: KIND,
isOperation: true,
operationList
operationList,
...options
};
}
}
9 changes: 5 additions & 4 deletions packages/protocol/src/action-protocol/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { hasObjectProp } from '../utils/type-util';
import { Action, Operation, RequestAction, ResponseAction } from './base-protocol';
import { EditorContext } from './types';
import { Args, EditorContext } from './types';

/**
* Requests the clipboard data for the current editor context, i.e., the selected elements, in a clipboard-compatible format.
Expand Down Expand Up @@ -96,11 +96,12 @@ export namespace CutOperation {
return Operation.hasKind(object, KIND) && hasObjectProp(object, 'editorContext');
}

export function create(editorContext: EditorContext): CutOperation {
export function create(editorContext: EditorContext, options: { args?: Args } = {}): CutOperation {
return {
kind: KIND,
isOperation: true,
editorContext
editorContext,
...options
};
}
}
Expand Down Expand Up @@ -129,7 +130,7 @@ export namespace PasteOperation {
return Operation.hasKind(object, KIND) && hasObjectProp(object, 'clipboardData') && hasObjectProp(object, 'editorContext');
}

export function create(options: { editorContext: EditorContext; clipboardData: ClipboardData }): PasteOperation {
export function create(options: { editorContext: EditorContext; clipboardData: ClipboardData; args?: Args }): PasteOperation {
return {
kind: KIND,
isOperation: true,
Expand Down
10 changes: 3 additions & 7 deletions packages/protocol/src/action-protocol/edge-modification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ export interface ReconnectEdgeOperation extends Operation {
* The (new) target element of the edge.
*/
targetElementId: string;

/*
* Additional arguments for custom behavior.
*/
args?: Args;
}

export namespace ReconnectEdgeOperation {
Expand Down Expand Up @@ -95,11 +90,12 @@ export namespace ChangeRoutingPointsOperation {
return Operation.hasKind(object, KIND) && hasArrayProp(object, 'newRoutingPoints');
}

export function create(newRoutingPoints: ElementAndRoutingPoints[]): ChangeRoutingPointsOperation {
export function create(newRoutingPoints: ElementAndRoutingPoints[], options: { args?: Args } = {}): ChangeRoutingPointsOperation {
return {
kind: KIND,
isOperation: true,
newRoutingPoints
newRoutingPoints,
...options
};
}
}
9 changes: 3 additions & 6 deletions packages/protocol/src/action-protocol/element-creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ export interface CreateOperation extends Operation {
* The type of the element that should be created.
*/
elementTypeId: string;
/**
* Optional additional arguments for the server to execute the create operation.
*/
args?: Args;
}

export namespace CreateOperation {
Expand Down Expand Up @@ -148,11 +144,12 @@ export namespace DeleteElementOperation {
return Operation.hasKind(object, KIND) && hasArrayProp(object, 'elementIds');
}

export function create(elementIds: string[]): DeleteElementOperation {
export function create(elementIds: string[], options: { args?: Args } = {}): DeleteElementOperation {
return {
kind: KIND,
isOperation: true,
elementIds
elementIds,
...options
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export namespace ApplyLabelEditOperation {
return Operation.hasKind(object, KIND) && hasStringProp(object, 'labelId') && hasStringProp(object, 'text');
}

export function create(options: { labelId: string; text: string }): ApplyLabelEditOperation {
export function create(options: { labelId: string; text: string; args?: Args }): ApplyLabelEditOperation {
return {
kind: KIND,
isOperation: true,
Expand Down
7 changes: 4 additions & 3 deletions packages/protocol/src/action-protocol/model-layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as sprotty from 'sprotty-protocol/lib/actions';
import { GModelRootSchema } from '..';
import { hasArrayProp, hasObjectProp } from '../utils/type-util';
import { Action, Operation, RequestAction, ResponseAction } from './base-protocol';
import { ElementAndAlignment, ElementAndBounds, ElementAndRoutingPoints } from './types';
import { Args, ElementAndAlignment, ElementAndBounds, ElementAndRoutingPoints } from './types';

/**
* Sent from the server to the client to request bounds for the given model. The model is rendered invisibly so the bounds can
Expand Down Expand Up @@ -127,11 +127,12 @@ export namespace LayoutOperation {
return Action.hasKind(object, KIND) && hasArrayProp(object, 'elementIds');
}

export function create(elementIds?: string[]): LayoutOperation {
export function create(elementIds?: string[], options: { args?: Args } = {}): LayoutOperation {
return {
kind: KIND,
isOperation: true,
elementIds
elementIds,
...options
};
}
}
9 changes: 7 additions & 2 deletions packages/protocol/src/action-protocol/node-modification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { Point } from 'sprotty-protocol';
import { hasArrayProp, hasStringProp } from '../utils/type-util';
import { Operation } from './base-protocol';
import { ElementAndBounds } from './types';
import { Args, ElementAndBounds } from './types';

/**
* Triggers the position or size change of elements. This action concerns only the element's graphical size and position.
Expand Down Expand Up @@ -80,7 +80,12 @@ export namespace ChangeContainerOperation {
return Operation.hasKind(object, KIND) && hasStringProp(object, 'elementId') && hasStringProp(object, 'targetContainerId');
}

export function create(options: { elementId: string; targetContainerId: string; location?: Point }): ChangeContainerOperation {
export function create(options: {
elementId: string;
targetContainerId: string;
location?: Point;
args?: Args;
}): ChangeContainerOperation {
return {
kind: KIND,
isOperation: true,
Expand Down

0 comments on commit f286b9d

Please sign in to comment.