UNPKG

sprotty-vscode

Version:

Glue code to integrate Sprotty diagrams in VSCode extensions (extension part)

150 lines 7.13 kB
"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.WebviewPanelManager = 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"); /** * This class manages freestyle webview panels. In contrast to custom editor webviews or webview views, * VS Code does not prescribe a specific lifecycle to webview panels, so this service provides a means * to keep track of the opened diagram webviews. */ class WebviewPanelManager { constructor(options) { var _a; this.options = options; this.endpoints = []; this.messenger = (_a = options.messenger) !== null && _a !== void 0 ? _a : new vscode_messenger_1.Messenger(); } /** * Find the webview endpoint of a webview panel that is currently active. */ findActiveWebview() { for (const endpoint of this.endpoints) { if ((0, webview_endpoint_1.isWebviewPanel)(endpoint.webviewContainer) && endpoint.webviewContainer.active) { return endpoint; } } return undefined; } /** * Open a webview panel for the given URI. Depending on the `singleton` option, this either replaces a * previously opened diagram or creates a new panel. */ async openDiagram(uri, options = {}) { const identifier = await this.createDiagramIdentifier(uri, options.diagramType); if (!identifier) { return undefined; } let endpoint = this.options.singleton ? this.endpoints[0] : this.endpoints.find(ep => { var _a, _b; return ((_a = ep.diagramIdentifier) === null || _a === void 0 ? void 0 : _a.uri) === identifier.uri && ((_b = ep.diagramIdentifier) === null || _b === void 0 ? void 0 : _b.diagramType) === identifier.diagramType; }); if (endpoint) { if (endpoint.diagramIdentifier) { identifier.clientId = endpoint.diagramIdentifier.clientId; } endpoint.webviewContainer.title = (0, webview_utils_1.createWebviewTitle)(identifier); endpoint.reloadContent(identifier); if (options.reveal && (0, webview_endpoint_1.isWebviewPanel)(endpoint.webviewContainer)) { endpoint.webviewContainer.reveal(endpoint.webviewContainer.viewColumn, options.preserveFocus); } } else { endpoint = this.createEndpoint(identifier); endpoint.webviewContainer.onDidDispose(() => this.didCloseWebview(endpoint)); this.endpoints.push(endpoint); } return endpoint; } createEndpoint(identifier) { var _a, _b; const webviewContainer = this.createWebview(identifier); const participant = this.messenger.registerWebviewPanel(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; } /** * Create and configure a webview panel. 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. */ createWebview(identifier) { var _a; const extensionPath = this.options.extensionUri.fsPath; const title = (0, webview_utils_1.createWebviewTitle)(identifier); const diagramPanel = vscode.window.createWebviewPanel(identifier.diagramType || 'diagram', title, vscode.ViewColumn.Beside, { localResourceRoots: (_a = this.options.localResourceRoots) !== null && _a !== void 0 ? _a : [(0, webview_utils_1.createFileUri)(extensionPath, 'pack')], enableScripts: true, retainContextWhenHidden: true }); if (this.options.createWebviewHtml) { diagramPanel.webview.html = this.options.createWebviewHtml(identifier, diagramPanel); } else { const scriptUri = (0, webview_utils_1.createFileUri)(extensionPath, 'pack', 'webview.js'); diagramPanel.webview.html = (0, webview_utils_1.createWebviewHtml)(identifier, diagramPanel, { scriptUri }); } return diagramPanel; } async createDiagramIdentifier(uri, diagramType) { if (!diagramType) { diagramType = await this.getDiagramType(uri); if (!diagramType) { return undefined; } } const clientId = diagramType + '_' + WebviewPanelManager.viewCount++; return { diagramType, uri: (0, webview_utils_1.serializeUri)(uri), clientId }; } /** * Determine a diagram type from the given URI. The default implementation returns the `defaultDiagramType` option * if the URI matches the `supportedFileExtensions` or no file extensions were provided. If no default diagram type * was provided, the file extension is used instead. */ getDiagramType(uri) { const extname = (0, webview_utils_1.getExtname)(uri); if (!this.options.supportedFileExtensions || this.options.supportedFileExtensions.includes(extname)) { if (this.options.defaultDiagramType) { return this.options.defaultDiagramType; } if (extname.length >= 2) { // Fallback: use the file extension after the '.' as diagram type return extname.substring(1); } } } didCloseWebview(endpoint) { const index = this.endpoints.indexOf(endpoint); if (index >= 0) { this.endpoints.splice(index, 1); } } } exports.WebviewPanelManager = WebviewPanelManager; WebviewPanelManager.viewCount = 0; //# sourceMappingURL=webview-panel-manager.js.map