sprotty-vscode
Version:
Glue code to integrate Sprotty diagrams in VSCode extensions (extension part)
145 lines • 6.49 kB
JavaScript
"use strict";
/********************************************************************************
* Copyright (c) 2022 TypeFox 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
********************************************************************************/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SprottyViewProvider = void 0;
const vscode = require("vscode");
const vscode_messenger_1 = require("vscode-messenger");
const webview_endpoint_1 = require("./webview-endpoint");
const webview_utils_1 = require("./webview-utils");
/**
* View provider for rendering diagrams. This must be registered in the package.json with a `views` contribution.
*/
class SprottyViewProvider {
constructor(options) {
var _a;
this.options = options;
this.clientId = options.viewType + '_' + SprottyViewProvider.viewCount++;
this.messenger = (_a = options.messenger) !== null && _a !== void 0 ? _a : new vscode_messenger_1.Messenger();
}
/**
* Returns the webview endpoint of the created view if it is already opened.
*/
findActiveWebview() {
return this.endpoint;
}
/**
* Open the given URI in the view if it is already opened. There is currently no way to open the view
* programmatically, so this method shows an error message if the view is not opened. The message can
* be suppressed with the `quiet` option.
*/
async openDiagram(uri, options = {}) {
const identifier = await this.createDiagramIdentifier(uri, options.diagramType);
if (!identifier) {
return undefined;
}
const endpoint = this.endpoint;
if (endpoint) {
endpoint.reloadContent(identifier);
if (options.reveal && (0, webview_endpoint_1.isWebviewView)(endpoint.webviewContainer)) {
endpoint.webviewContainer.show(options.preserveFocus);
}
}
else {
vscode.commands.executeCommand(`${identifier.diagramType}.focus`);
}
return endpoint;
}
async resolveWebviewView(webviewView, context, cancelToken) {
if (this.endpoint) {
console.warn('Warning: Sprotty webview-view is already resolved.');
}
let identifier;
if (this.options.openActiveEditor) {
const editor = vscode.window.activeTextEditor;
if (editor) {
identifier = await this.createDiagramIdentifier(editor.document.uri);
}
}
const endpoint = this.createEndpoint(webviewView, identifier);
this.configureWebview(webviewView, endpoint, cancelToken);
this.endpoint = endpoint;
webviewView.onDidDispose(() => this.didCloseWebview(endpoint));
}
createEndpoint(webviewContainer, identifier) {
var _a, _b;
const participant = this.messenger.registerWebviewView(webviewContainer);
const endpoint = new webview_endpoint_1.WebviewEndpoint({
webviewContainer,
messenger: this.messenger,
messageParticipant: participant,
identifier
});
(_b = (_a = this.options).configureEndpoint) === null || _b === void 0 ? void 0 : _b.call(_a, endpoint);
return endpoint;
}
/**
* Configure the given webview view. The default implementation sets `localResourceRoots` to the `pack` subfolder of the extension host
* and `scriptUri` to `pack/webview.js`. Please configure your bundler to generate such an output file from the Sprotty webview frontend.
* In case you need to use different settings or change the HTML content, you can override this functionality in a subclass.
*/
configureWebview(webviewView, endpoint, cancelToken) {
var _a;
const extensionPath = this.options.extensionUri.fsPath;
webviewView.webview.options = {
localResourceRoots: (_a = this.options.localResourceRoots) !== null && _a !== void 0 ? _a : [(0, webview_utils_1.createFileUri)(extensionPath, 'pack')],
enableScripts: true
};
let identifier = endpoint.diagramIdentifier;
if (!identifier) {
// Create a preliminary diagram identifier to fill the webview's HTML content
identifier = { clientId: this.clientId, diagramType: this.options.viewType, uri: '' };
}
if (this.options.createWebviewHtml) {
webviewView.webview.html = this.options.createWebviewHtml(identifier, webviewView);
}
else {
const scriptUri = (0, webview_utils_1.createFileUri)(extensionPath, 'pack', 'webview.js');
webviewView.webview.html = (0, webview_utils_1.createWebviewHtml)(identifier, webviewView, { scriptUri });
}
}
async createDiagramIdentifier(uri, diagramType) {
if (!diagramType) {
diagramType = await this.getDiagramType(uri);
if (!diagramType) {
return undefined;
}
}
return {
diagramType,
uri: (0, webview_utils_1.serializeUri)(uri),
clientId: this.clientId
};
}
/**
* Determine a diagram type from the given URI. The default implementation returns the `viewType` of the view
* if the URI matches the `supportedFileExtensions` or no file extensions were provided.
*/
getDiagramType(uri) {
const extname = (0, webview_utils_1.getExtname)(uri);
if (!this.options.supportedFileExtensions || this.options.supportedFileExtensions.includes(extname)) {
return this.options.viewType;
}
}
didCloseWebview(endpoint) {
if (this.endpoint === endpoint) {
this.endpoint = undefined;
}
}
}
exports.SprottyViewProvider = SprottyViewProvider;
SprottyViewProvider.viewCount = 0;
//# sourceMappingURL=sprotty-view-provider.js.map