From 6db8ac875b2714c17b314db076ca8edfc92706e5 Mon Sep 17 00:00:00 2001 From: Martin Fleck Date: Wed, 22 Nov 2023 00:01:45 +0100 Subject: [PATCH] Add ghost elements to tool palette items through trigger actions (#65) * Add ghost elements to tool palette items through trigger actions Relates to https://github.com/eclipse-glsp/glsp/issues/1159 * PR Review: Add easier hooks for customization * Update yarn lock --------- Co-authored-by: Tobias Ortmayr --- .../handler/create-activity-node-handler.ts | 14 ++++++++----- .../common/handler/create-category-handler.ts | 12 +++++++---- .../src/common/handler/create-task-handler.ts | 12 +++++++---- .../gmodel-create-node-operation-handler.ts | 20 +++++++++++++++++-- .../operations/json-operation-handler.ts | 17 +++++++++++++++- yarn.lock | 7 ++++--- 6 files changed, 63 insertions(+), 19 deletions(-) diff --git a/examples/workflow-server/src/common/handler/create-activity-node-handler.ts b/examples/workflow-server/src/common/handler/create-activity-node-handler.ts index 6f5139a..fdbd8dd 100644 --- a/examples/workflow-server/src/common/handler/create-activity-node-handler.ts +++ b/examples/workflow-server/src/common/handler/create-activity-node-handler.ts @@ -13,7 +13,7 @@ * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ -import { CreateNodeOperation, GNode, Point } from '@eclipse-glsp/server'; +import { CreateNodeOperation, GNode, GhostElement, Point } from '@eclipse-glsp/server'; import { injectable } from 'inversify'; import { ActivityNode, ActivityNodeBuilder } from '../graph-extension'; import { ModelTypes } from '../util/model-types'; @@ -25,10 +25,14 @@ export abstract class CreateActivityNodeHandler extends CreateWorkflowNodeOperat return this.builder(relativeLocation).build(); } - protected builder(point: Point | undefined): ActivityNodeBuilder { + protected builder(point: Point = Point.ORIGIN, elementTypeId = this.elementTypeIds[0]): ActivityNodeBuilder { return ActivityNode.builder() - .position(point ?? Point.ORIGIN) - .type(this.elementTypeIds[0]) - .nodeType(ModelTypes.toNodeType(this.elementTypeIds[0])); + .position(point) + .type(elementTypeId) + .nodeType(ModelTypes.toNodeType(elementTypeId)); + } + + override createTriggerGhostElement(elementTypeId: string): GhostElement | undefined { + return { template: this.serializer.createSchema(this.builder(undefined, elementTypeId).build()) }; } } diff --git a/examples/workflow-server/src/common/handler/create-category-handler.ts b/examples/workflow-server/src/common/handler/create-category-handler.ts index aab7e69..702af1d 100644 --- a/examples/workflow-server/src/common/handler/create-category-handler.ts +++ b/examples/workflow-server/src/common/handler/create-category-handler.ts @@ -13,7 +13,7 @@ * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ -import { ArgsUtil, CreateNodeOperation, GNode, Point } from '@eclipse-glsp/server'; +import { ArgsUtil, CreateNodeOperation, GNode, GhostElement, Point } from '@eclipse-glsp/server'; import { Category, CategoryNodeBuilder } from '../graph-extension'; import { ModelTypes } from '../util/model-types'; import { CreateWorkflowNodeOperationHandler } from './create-workflow-node-operation-handler'; @@ -26,12 +26,16 @@ export class CreateCategoryHandler extends CreateWorkflowNodeOperationHandler { return this.builder(relativeLocation).build(); } - protected builder(point: Point | undefined): CategoryNodeBuilder { + protected builder(point: Point = Point.ORIGIN, elementTypeId = this.elementTypeIds[0]): CategoryNodeBuilder { return Category.builder() - .type(this.elementTypeIds[0]) - .position(point ?? Point.ORIGIN) + .type(elementTypeId) + .position(point) .name(this.label.replace(' ', '') + this.modelState.index.getAllByClass(Category).length) .addArgs(ArgsUtil.cornerRadius(5)) .children(); } + + override createTriggerGhostElement(elementTypeId: string): GhostElement | undefined { + return { template: this.serializer.createSchema(this.builder(undefined, elementTypeId).build()), dynamic: true }; + } } diff --git a/examples/workflow-server/src/common/handler/create-task-handler.ts b/examples/workflow-server/src/common/handler/create-task-handler.ts index 1ea8fb4..477885f 100644 --- a/examples/workflow-server/src/common/handler/create-task-handler.ts +++ b/examples/workflow-server/src/common/handler/create-task-handler.ts @@ -13,7 +13,7 @@ * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ -import { Point } from '@eclipse-glsp/protocol'; +import { GhostElement, Point } from '@eclipse-glsp/protocol'; import { CreateNodeOperation, GNode } from '@eclipse-glsp/server'; import { injectable } from 'inversify'; import { TaskNode, TaskNodeBuilder } from '../graph-extension'; @@ -26,12 +26,16 @@ export abstract class CreateTaskHandler extends CreateWorkflowNodeOperationHandl return this.builder(relativeLocation).build(); } - protected builder(point: Point | undefined): TaskNodeBuilder { + protected builder(point: Point = Point.ORIGIN, elementTypeId = this.elementTypeIds[0]): TaskNodeBuilder { return TaskNode.builder() .position(point ?? Point.ORIGIN) .name(this.label.replace(' ', '') + this.modelState.index.getAllByClass(TaskNode).length) - .type(this.elementTypeIds[0]) - .taskType(ModelTypes.toNodeType(this.elementTypeIds[0])) + .type(elementTypeId) + .taskType(ModelTypes.toNodeType(elementTypeId)) .children(); } + + override createTriggerGhostElement(elementTypeId: string): GhostElement | undefined { + return { template: this.serializer.createSchema(this.builder(undefined, elementTypeId).build()), dynamic: true }; + } } diff --git a/packages/server/src/common/gmodel/gmodel-create-node-operation-handler.ts b/packages/server/src/common/gmodel/gmodel-create-node-operation-handler.ts index 6c87dbb..7c4f69b 100644 --- a/packages/server/src/common/gmodel/gmodel-create-node-operation-handler.ts +++ b/packages/server/src/common/gmodel/gmodel-create-node-operation-handler.ts @@ -15,7 +15,10 @@ ********************************************************************************/ import { GModelElement, GNode } from '@eclipse-glsp/graph'; -import { CreateNodeOperation, MaybePromise, Point, SelectAction, TriggerNodeCreationAction } from '@eclipse-glsp/protocol'; +import { + Args, CreateNodeOperation, GhostElement, MaybePromise, Point, SelectAction, + TriggerNodeCreationAction +} from '@eclipse-glsp/protocol'; import { inject, injectable } from 'inversify'; import { ActionDispatcher } from '../actions/action-dispatcher'; import { Command } from '../command/command'; @@ -55,7 +58,20 @@ export abstract class GModelCreateNodeOperationHandler extends GModelOperationHa } getTriggerActions(): TriggerNodeCreationAction[] { - return this.elementTypeIds.map(typeId => TriggerNodeCreationAction.create(typeId)); + return this.elementTypeIds.map(elementTypeId => this.createTriggerNodeCreationAction(elementTypeId)); + } + + protected createTriggerNodeCreationAction(elementTypeId: string): TriggerNodeCreationAction { + return TriggerNodeCreationAction.create(elementTypeId, + { ghostElement: this.createTriggerGhostElement(elementTypeId), args: this.createTriggerArgs(elementTypeId) } ); + } + + protected createTriggerGhostElement(elementTypeId: string): GhostElement | undefined { + return undefined; + } + + protected createTriggerArgs(elementTypeId: string): Args | undefined { + return undefined; } /** diff --git a/packages/server/src/common/operations/json-operation-handler.ts b/packages/server/src/common/operations/json-operation-handler.ts index 44e1560..46c03b9 100644 --- a/packages/server/src/common/operations/json-operation-handler.ts +++ b/packages/server/src/common/operations/json-operation-handler.ts @@ -16,8 +16,10 @@ import { GModelElement } from '@eclipse-glsp/graph'; import { AnyObject, + Args, CreateEdgeOperation, CreateNodeOperation, + GhostElement, MaybePromise, Point, TriggerEdgeCreationAction, @@ -93,7 +95,20 @@ export abstract class JsonCreateNodeOperationHandler extends JsonOperationHandle abstract override createCommand(operation: CreateNodeOperation): MaybePromise; getTriggerActions(): TriggerNodeCreationAction[] { - return this.elementTypeIds.map(typeId => TriggerNodeCreationAction.create(typeId)); + return this.elementTypeIds.map(elementTypeId => this.createTriggerNodeCreationAction(elementTypeId)); + } + + protected createTriggerNodeCreationAction(elementTypeId: string): TriggerNodeCreationAction { + return TriggerNodeCreationAction.create(elementTypeId, + { ghostElement: this.createTriggerGhostElement(elementTypeId), args: this.createTriggerArgs(elementTypeId) } ); + } + + protected createTriggerGhostElement(elementTypeId: string): GhostElement | undefined { + return undefined; + } + + protected createTriggerArgs(elementTypeId: string): Args | undefined { + return undefined; } /** diff --git a/yarn.lock b/yarn.lock index 01a0592..1935c6d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -305,9 +305,9 @@ prettier-plugin-packagejson "~2.4.6" "@eclipse-glsp/protocol@next": - version "2.1.0-alpha.290" - resolved "https://registry.yarnpkg.com/@eclipse-glsp/protocol/-/protocol-2.1.0-alpha.290.tgz#0b874cb344e5b741f2a4be43a824121f2913aab6" - integrity sha512-e49InCT4rgW33wY7Y15RA5jvC34qHdMfxIE/vmxZQApOQEdmdHw7fg8CtJ4hXDVeSXOuUN0On5InaMIpg07xKQ== + version "2.1.0-next.297" + resolved "https://registry.yarnpkg.com/@eclipse-glsp/protocol/-/protocol-2.1.0-next.297.tgz#4d178d5286c44b293b58b614fd84d0a0653052a6" + integrity sha512-WmV/ZAHusldwrXFof5oF68peBo0EpmVAb0N/v7LeYaTUiwnz4eFARm/p0KxL2QlcPBuKgLvi0xgGDzDGvbeNcw== dependencies: sprotty-protocol "0.15.0-next.044bba2.13" uuid "7.0.3" @@ -7563,6 +7563,7 @@ workerpool@6.2.1: integrity sha512-ILEIE97kDZvF9Wb9f6h5aXK4swSlKGUcOEGiIYb2OOu/IrDU9iwj0fD//SsA6E5ibwJxpEvhullJY4Sl4GcpAw== "wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0: + name wrap-ansi-cjs version "7.0.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==