-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GLSP-960: Improve Diagram container configuration (#51)
* GLSP-960: Improve Diagram container configuration - Use vscode-messenger protocol for webview-extension communication - Refactor webview communication code into `WebviewEndpoint` class - Introduce a `WebviewGLSPClient` that forwards RPC calls to the GLSP client running in the host extension - Refactor and update codebase to conform to changes from GLSP-960 Part of eclipse-glsp/glsp/issues/960 Requires eclipse-glsp/glsp-client#282
- Loading branch information
Showing
25 changed files
with
1,339 additions
and
1,010 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/vscode-integration-webview/src/default-modules.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2023 EclipseSource and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { ModuleConfiguration } from '@eclipse-glsp/client'; | ||
import { vscodeCopyPasteModule } from './features/copyPaste/copy-paste-module'; | ||
import { vscodeDefaultModule } from './features/default/default-module'; | ||
import { vscodeExportModule } from './features/export/export-module'; | ||
import { vscodeNavigationModule } from './features/navigation/navigation-module'; | ||
|
||
export const VSCODE_DEFAULT_MODULES = [vscodeDefaultModule, vscodeCopyPasteModule, vscodeExportModule, vscodeNavigationModule] as const; | ||
|
||
export const VSCODE_DEFAULT_MODULE_CONFIG: ModuleConfiguration = { | ||
add: [...VSCODE_DEFAULT_MODULES] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/vscode-integration-webview/src/features/copyPaste/copy-paste-module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2023 EclipseSource and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { | ||
FeatureModule, | ||
ICopyPasteHandler, | ||
RequestClipboardDataAction, | ||
SetClipboardDataAction, | ||
TYPES, | ||
bindAsService, | ||
copyPasteModule | ||
} from '@eclipse-glsp/client'; | ||
import { ExtensionActionKind } from '../default/extension-action-handler'; | ||
import { CopyPasteHandlerProvider, CopyPasteStartup } from './copy-paste-startup'; | ||
|
||
export const vscodeCopyPasteModule = new FeatureModule( | ||
bind => { | ||
bind(CopyPasteHandlerProvider).toProvider( | ||
ctx => () => new Promise<ICopyPasteHandler>(resolve => resolve(ctx.container.get<ICopyPasteHandler>(TYPES.ICopyPasteHandler))) | ||
); | ||
bindAsService(bind, TYPES.IDiagramStartup, CopyPasteStartup); | ||
bind(ExtensionActionKind).toConstantValue(RequestClipboardDataAction.KIND); | ||
bind(ExtensionActionKind).toConstantValue(SetClipboardDataAction.KIND); | ||
}, | ||
{ requires: copyPasteModule } | ||
); |
50 changes: 50 additions & 0 deletions
50
packages/vscode-integration-webview/src/features/copyPaste/copy-paste-startup.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2023 EclipseSource and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { ICopyPasteHandler, IDiagramStartup, MaybePromise } from '@eclipse-glsp/client'; | ||
import { inject, injectable } from 'inversify'; | ||
|
||
export const CopyPasteHandlerProvider = Symbol('CopyPasteHandlerProvider'); | ||
/** | ||
* Service identifier and type definition to bind the {@link ICopyPasteHandler} to an provider. | ||
* This enables asynchronous injections and can be used to bypass circular dependency issues. | ||
*/ | ||
export type CopyPasteHandlerProvider = () => Promise<ICopyPasteHandler>; | ||
|
||
/** | ||
* Startup service to hook up the copy&paste event handler | ||
*/ | ||
@injectable() | ||
export class CopyPasteStartup implements IDiagramStartup { | ||
@inject(CopyPasteHandlerProvider) | ||
protected copyPasteHandlerProvider: CopyPasteHandlerProvider; | ||
|
||
preInitialize(): MaybePromise<void> { | ||
this.copyPasteHandlerProvider().then(copyPasteHandler => { | ||
document.addEventListener('copy', (e: ClipboardEvent) => { | ||
copyPasteHandler.handleCopy(e); | ||
}); | ||
|
||
document.addEventListener('cut', (e: ClipboardEvent) => { | ||
copyPasteHandler.handleCut(e); | ||
}); | ||
|
||
document.addEventListener('paste', (e: ClipboardEvent) => { | ||
copyPasteHandler.handlePaste(e); | ||
}); | ||
}); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/vscode-integration-webview/src/features/default/default-module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2023 EclipseSource and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { FeatureModule, GLSPModelSource, TYPES, bindAsService, defaultModule } from '@eclipse-glsp/client'; | ||
import { GLSPDiagramWidget, GLSPDiagramWidgetFactory } from '../../glsp-diagram-widget'; | ||
import { HostExtensionActionHandler } from './extension-action-handler'; | ||
import { VsCodeGLSPModelSource } from './vscode-glsp-modelsource'; | ||
|
||
export const vscodeDefaultModule = new FeatureModule( | ||
(bind, _unbind, _isBound, rebind) => { | ||
bind(GLSPDiagramWidget).toSelf().inSingletonScope(); | ||
bind(GLSPDiagramWidgetFactory).toFactory(context => () => context.container.get<GLSPDiagramWidget>(GLSPDiagramWidget)); | ||
bind(VsCodeGLSPModelSource).toSelf().inSingletonScope(); | ||
rebind(GLSPModelSource).toService(VsCodeGLSPModelSource); | ||
bindAsService(bind, TYPES.IActionHandlerInitializer, HostExtensionActionHandler); | ||
}, | ||
{ requires: defaultModule } | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/vscode-integration-webview/src/features/default/vscode-glsp-modelsource.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2023 EclipseSource and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
import { Action, ActionMessage, GLSPModelSource, ServerAction } from '@eclipse-glsp/client'; | ||
import { injectable } from 'inversify'; | ||
|
||
/** | ||
* A helper interface that allows the webview to mark actions that have been received directly from the vscode extension | ||
* (i.e. they are not forwarded to the GLSP Server). | ||
*/ | ||
export interface ExtensionAction extends Action { | ||
__localDispatch: true; | ||
} | ||
|
||
export namespace ExtensionAction { | ||
export function is(object: unknown): object is ExtensionAction { | ||
return Action.is(object) && '__localDispatch' in object && object.__localDispatch === true; | ||
} | ||
|
||
/** | ||
* Mark the given action as {@link ServerAction} by attaching the "_receivedFromServer" property | ||
* @param action The action that should be marked as server action | ||
*/ | ||
export function mark(action: Action): void { | ||
(action as ExtensionAction).__localDispatch = true; | ||
} | ||
} | ||
|
||
/** | ||
* Customization of the default {@link GLSPModelSource} for the vscode integration. | ||
* Also takes locally dispatched actions (i.e. actions that are originating from or intended for the host extension) into consideration. | ||
*/ | ||
@injectable() | ||
export class VsCodeGLSPModelSource extends GLSPModelSource { | ||
protected override messageReceived(message: ActionMessage): void { | ||
if (this.clientId !== message.clientId) { | ||
return; | ||
} | ||
this.checkMessageOrigin(message); | ||
const action = message.action; | ||
this.logger.log(this, 'receiving', action); | ||
this.actionDispatcher.dispatch(action); | ||
} | ||
|
||
protected checkMessageOrigin(message: ActionMessage): void { | ||
const isLocalDispatch = (message as any)['__localDispatch'] ?? false; | ||
if (!isLocalDispatch) { | ||
ServerAction.mark(message.action); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.