@eclipse-glsp/vscode-integration-webview 0.9.0-next.2e7eec38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2020-2021 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
+ import {
17
+ DiagramServer,
18
+ EnableToolPaletteAction,
19
+ InitializeClientSessionAction,
20
+ RequestModelAction,
21
+ RequestTypeHintsAction
22
+ } from '@eclipse-glsp/client';
23
+ import { injectable } from 'inversify';
24
+ import { VscodeDiagramWidget } from 'sprotty-vscode-webview';
25
+
26
+ @injectable()
27
+ export abstract class GLSPVscodeDiagramWidget extends VscodeDiagramWidget {
28
+ protected initializeSprotty(): void {
29
+ if (this.modelSource instanceof DiagramServer) {
30
+ this.modelSource.clientId = this.diagramIdentifier.clientId;
31
+ }
32
+ this.actionDispatcher.dispatch(new InitializeClientSessionAction(this.diagramIdentifier.clientId));
33
+ this.actionDispatcher.dispatch(new RequestModelAction({
34
+ sourceUri: decodeURI(this.diagramIdentifier.uri),
35
+ diagramType: this.diagramIdentifier.diagramType
36
+ }));
37
+
38
+ this.actionDispatcher.dispatch(new RequestTypeHintsAction(this.diagramIdentifier.diagramType));
39
+ this.actionDispatcher.dispatch(new EnableToolPaletteAction());
40
+ }
41
+ }
42
+
43
+ export function decodeURI(uri: string): string {
44
+ return decodeURIComponent(uri.replace(/\+/g, ' '));
45
+ }
@@ -0,0 +1,109 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2020-2021 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
+ import {
17
+ Action,
18
+ ActionHandlerRegistry,
19
+ ActionMessage,
20
+ ComputedBoundsAction,
21
+ DeleteElementOperation,
22
+ GLSP_TYPES,
23
+ isDeleteElementOperation,
24
+ isSetEditModeAction,
25
+ registerDefaultGLSPServerActions,
26
+ SetEditModeAction,
27
+ ICopyPasteHandler
28
+ } from '@eclipse-glsp/client';
29
+ import { SelectionService } from '@eclipse-glsp/client/lib/features/select/selection-service';
30
+ import { inject } from 'inversify';
31
+ import { isActionMessage, VscodeDiagramServer } from 'sprotty-vscode-webview';
32
+
33
+ export const receivedFromServerProperty = '__receivedFromServer';
34
+ export const localDispatchProperty = '__localDispatch';
35
+
36
+ export class GLSPVscodeDiagramServer extends VscodeDiagramServer {
37
+ @inject(GLSP_TYPES.SelectionService) protected selectionService: SelectionService;
38
+ @inject(GLSP_TYPES.ICopyPasteHandler) protected copyPasteHandler: ICopyPasteHandler;
39
+
40
+ initialize(registry: ActionHandlerRegistry): void {
41
+ registerDefaultGLSPServerActions(registry, this);
42
+ this.clientId = this.viewerOptions.baseDiv;
43
+ window.addEventListener('message', message => {
44
+ if ('data' in message && isActionMessage(message.data)) {
45
+ this.messageReceived(message.data);
46
+ }
47
+ });
48
+
49
+ window.addEventListener('copy', (e: ClipboardEvent) => {
50
+ this.copyPasteHandler.handleCopy(e);
51
+ });
52
+
53
+ window.addEventListener('cut', (e: ClipboardEvent) => {
54
+ this.copyPasteHandler.handleCut(e);
55
+ });
56
+
57
+ window.addEventListener('paste', (e: ClipboardEvent) => {
58
+ this.copyPasteHandler.handlePaste(e);
59
+ });
60
+ }
61
+
62
+ handleLocally(action: Action): boolean {
63
+ if (isSetEditModeAction(action)) {
64
+ return this.handleSetEditModeAction(action);
65
+ }
66
+ if (isDeleteElementOperation(action)) {
67
+ return this.handleDeleteElementOperation(action);
68
+ }
69
+ return super.handleLocally(action);
70
+ }
71
+
72
+ protected messageReceived(data: any): void {
73
+ const object = typeof (data) === 'string' ? JSON.parse(data) : data;
74
+ if (isActionMessage(object) && object.action) {
75
+ if (!object.clientId || object.clientId === this.clientId) {
76
+ this.checkMessageOrigin(object);
77
+ this.logger.log(this, 'receiving', object);
78
+ this.actionDispatcher.dispatch(object.action).then(() => {
79
+ this.storeNewModel(object.action);
80
+ });
81
+ }
82
+ } else {
83
+ this.logger.error(this, 'received data is not an action message', object);
84
+ }
85
+ }
86
+
87
+ protected checkMessageOrigin(message: ActionMessage): void {
88
+ const isLocalDispatch = (message as any)[localDispatchProperty] || false;
89
+ if (!isLocalDispatch) {
90
+ (message.action as any)[receivedFromServerProperty] = true;
91
+ }
92
+ }
93
+
94
+ protected handleComputedBounds(action: ComputedBoundsAction): boolean {
95
+ return true;
96
+ }
97
+
98
+ protected handleDeleteElementOperation(operation: DeleteElementOperation): boolean {
99
+ if (operation.elementIds.length === 0) {
100
+ operation.elementIds.push(...this.selectionService.getSelectedElementIDs());
101
+ }
102
+ return true;
103
+ }
104
+
105
+ protected handleSetEditModeAction(action: SetEditModeAction): boolean {
106
+ return (action as any)[receivedFromServerProperty] !== true;
107
+ }
108
+
109
+ }
package/src/index.ts ADDED
@@ -0,0 +1,18 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2020 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
+ export * from './glsp-starter';
18
+ export * from './glsp-vscode-diagramserver';