Skip to content

Commit 9d94ba7

Browse files
authored
GLSP-1586: Refactor MenuItem API (#116)
* GLSP-1586: Refactor MenuItem API - Adjust context menu item provider to new `ServerMenuItem` API - Extend workflow context menu provider - Add a hidden item for testing - Add a `Readonly` mode toggle entry Part of eclipse-glsp/glsp#1586 GLSP-1587: - Add missing action handler for `SetEditModeAction` - Align base `CommandPaletteActionProvider` with java-server implementation (do not return empty for readonly by default) Fixes eclipse-glsp/glsp#1587 Requires eclipse-glsp/glsp-client#450 * Update yarn.lock
1 parent 9a0879e commit 9d94ba7

File tree

6 files changed

+65
-19
lines changed

6 files changed

+65
-19
lines changed

examples/workflow-server/src/common/provider/workflow-context-menu-item-provider.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (c) 2022-2023 STMicroelectronics and others.
2+
* Copyright (c) 2022-2025 STMicroelectronics and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -13,7 +13,7 @@
1313
*
1414
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
1515
********************************************************************************/
16-
import { Args, ContextMenuItemProvider, CreateNodeOperation, MenuItem, ModelState, Point } from '@eclipse-glsp/server';
16+
import { Args, ContextMenuItemProvider, CreateNodeOperation, MenuItem, ModelState, Point, SetEditModeAction } from '@eclipse-glsp/server';
1717
import { inject, injectable } from 'inversify';
1818
import { GridSnapper } from '../handler/grid-snapper';
1919
import { ModelTypes } from '../util/model-types';
@@ -24,8 +24,15 @@ export class WorkflowContextMenuItemProvider extends ContextMenuItemProvider {
2424
protected modelState: ModelState;
2525

2626
getItems(selectedElementIds: string[], position: Point, args?: Args): MenuItem[] {
27+
const editModeMenu: MenuItem = {
28+
id: 'editMode',
29+
label: 'Readonly Mode',
30+
isToggled: this.modelState.isReadonly,
31+
actions: [this.modelState.isReadonly ? SetEditModeAction.create('editable') : SetEditModeAction.create('readonly')]
32+
};
33+
2734
if (this.modelState.isReadonly || selectedElementIds.length !== 0) {
28-
return [];
35+
return [editModeMenu];
2936
}
3037
const snappedPosition = GridSnapper.snap(position);
3138
const newAutTask: MenuItem = {
@@ -38,14 +45,22 @@ export class WorkflowContextMenuItemProvider extends ContextMenuItemProvider {
3845
label: 'Manual Task',
3946
actions: [CreateNodeOperation.create(ModelTypes.MANUAL_TASK, { location: snappedPosition })]
4047
};
48+
49+
const hiddenItem: MenuItem = {
50+
id: 'hiddenItem',
51+
label: 'Should be hidden',
52+
actions: [],
53+
isVisible: false
54+
};
4155
const newChildMenu: MenuItem = {
4256
id: 'new',
4357
label: 'New',
4458
actions: [],
45-
children: [newAutTask, newManTask],
59+
children: [newAutTask, newManTask, hiddenItem],
4660
icon: 'add',
4761
group: '0_new'
4862
};
49-
return [newChildMenu];
63+
64+
return [newChildMenu, editModeMenu];
5065
}
5166
}

packages/server/src/common/di/diagram-module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (c) 2022-2023 STMicroelectronics and others.
2+
* Copyright (c) 2022-2025 STMicroelectronics and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -44,6 +44,7 @@ import { ModelState } from '../features/model/model-state';
4444
import { ModelSubmissionHandler } from '../features/model/model-submission-handler';
4545
import { RequestModelActionHandler } from '../features/model/request-model-action-handler';
4646
import { SaveModelActionHandler } from '../features/model/save-model-action-handler';
47+
import { SetEditModeActionHandler } from '../features/model/set-edit-mode-action-handler';
4748
import { SourceModelStorage } from '../features/model/source-model-storage';
4849
import { NavigationTargetProvider } from '../features/navigation/navigation-target-provider';
4950
import {
@@ -214,6 +215,7 @@ export abstract class DiagramModule extends GLSPModule {
214215
binding.add(RequestNavigationTargetsActionHandler);
215216
binding.add(ResolveNavigationTargetsActionHandler);
216217
binding.add(SaveModelActionHandler);
218+
binding.add(SetEditModeActionHandler);
217219
binding.add(UndoRedoActionHandler);
218220
binding.add(ComputedBoundsActionHandler);
219221
}

packages/server/src/common/features/contextactions/command-palette-action-provider.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (c) 2022-2023 STMicroelectronics and others.
2+
* Copyright (c) 2022-2025 STMicroelectronics and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -64,11 +64,6 @@ export abstract class CommandPaletteActionProvider implements ContextActionsProv
6464
* @returns A list of {@link LabeledAction}s for a given {@link EditorContext}.
6565
*/
6666
getActions(editorContext: EditorContext): LabeledAction[] {
67-
const actions: LabeledAction[] = [];
68-
if (this.modelState.isReadonly) {
69-
return actions;
70-
}
71-
7267
const selectedIds = editorContext.selectedElementIds;
7368
const position = editorContext.lastMousePosition ? editorContext.lastMousePosition : { x: 0, y: 0 };
7469
const selectedElements = this.modelState.index.getAll(selectedIds);

packages/server/src/common/features/contextactions/context-menu-item-provider.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (c) 2022-2023 STMicroelectronics and others.
2+
* Copyright (c) 2022-2025 STMicroelectronics and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -18,7 +18,7 @@ import { injectable } from 'inversify';
1818
import { ContextActionsProvider } from './context-actions-provider';
1919

2020
/**
21-
* A {@link ContextActionsProvider} for {@link MenuItem}s.
21+
* A {@link ContextActionsProvider} for {@link ServerMenuItem}s.
2222
*/
2323
@injectable()
2424
export abstract class ContextMenuItemProvider implements ContextActionsProvider {
@@ -30,13 +30,14 @@ export abstract class ContextMenuItemProvider implements ContextActionsProvider
3030
}
3131

3232
/**
33-
* Returns a list of {@link MenuItem}s for a given list of selected elements at a certain mouse position.
33+
* Returns a list of {@link ServerMenuItem}s for a given list of selected elements at a certain mouse position.
3434
*
3535
* @param selectedElementIds The list of currently selected elementIds.
3636
* @param position The current mouse position.
3737
* @param args Additional arguments.
38-
* @returns A list of {@link MenuItem}s for a given list of selected elements at a certain mouse position.
38+
* @returns A list of {@link ServerMenuItem}s for a given list of selected elements at a certain mouse position.
3939
*/
40+
4041
abstract getItems(selectedElementIds: string[], position: Point, args?: Args): MenuItem[];
4142

4243
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 EclipseSource and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the Eclipse
10+
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
11+
* with the GNU Classpath Exception which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
********************************************************************************/
16+
17+
import { Action, SetEditModeAction } from '@eclipse-glsp/protocol';
18+
import { inject, injectable } from 'inversify';
19+
import { ActionHandler } from '../../actions/action-handler';
20+
import { ModelState } from './model-state';
21+
22+
@injectable()
23+
export class SetEditModeActionHandler implements ActionHandler {
24+
actionKinds = [SetEditModeAction.KIND];
25+
26+
@inject(ModelState)
27+
protected modelState: ModelState;
28+
29+
async execute(action: SetEditModeAction): Promise<Action[]> {
30+
this.modelState.editMode = action.editMode;
31+
return [];
32+
}
33+
}

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@
301301
prettier-plugin-packagejson "~2.4.6"
302302

303303
"@eclipse-glsp/protocol@next":
304-
version "2.6.0-next.1"
305-
resolved "https://registry.yarnpkg.com/@eclipse-glsp/protocol/-/protocol-2.6.0-next.1.tgz#adc43f3f7a3cc0c17d12970b473521df5dbb6d66"
306-
integrity sha512-pRcQAJl8JIXaC8O4UKlgAsJDsHhRWLP6Sn0phjI+34uEuYtWz/Ps6deHKbPPsbaeLd9ljZv7DB4KuXnL3nYWjQ==
304+
version "2.6.0-next.6"
305+
resolved "https://registry.yarnpkg.com/@eclipse-glsp/protocol/-/protocol-2.6.0-next.6.tgz#7437694ed1e4f5ca716ba657b98af46c90abbc9a"
306+
integrity sha512-uswJrgPNb7wwRCiqyoNOkTCqdM2S+3avXuh4/vDKxyo7w05yLkblp1foEInfYIbRmqTod05K4fAtUWEkxJcg5Q==
307307
dependencies:
308308
sprotty-protocol "1.4.0"
309309
uuid "~10.0.0"

0 commit comments

Comments
 (0)