UNPKG

sprotty-vscode

Version:

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

156 lines 6.97 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.SprottyEditorProvider = 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"); /** * Custom editor provider for rendering diagrams. This must be registered in the package.json with a `customEditors` contribution. * The default implementation is not able to save, revert or backup the document. If you need such functionality, implement it in * a subclass. */ class SprottyEditorProvider { get onDidChangeCustomDocument() { return this.changeEmitter.event; } constructor(options) { var _a; this.options = options; this.changeEmitter = new vscode.EventEmitter(); this.documents = []; this.messenger = (_a = options.messenger) !== null && _a !== void 0 ? _a : new vscode_messenger_1.Messenger(); } /** * Find the webview endpoint of a custom editor that is currently active. */ findActiveWebview() { for (const document of this.documents) { if (document.endpoint && (0, webview_endpoint_1.isWebviewPanel)(document.endpoint.webviewContainer) && document.endpoint.webviewContainer.active) { return document.endpoint; } } return undefined; } /** * Open a custom editor for the given URI. */ async openDiagram(uri, options) { await vscode.commands.executeCommand('vscode.openWith', uri, this.options.viewType); // We can't access the resulting webview endpoint from here. return undefined; } openCustomDocument(uri, openContext, cancelToken) { const document = { uri, dispose: () => this.disposeDocument(document) }; this.documents.push(document); return document; } disposeDocument(document) { const index = this.documents.indexOf(document); if (index >= 0) { this.documents.splice(index, 1); } } async resolveCustomEditor(document, webviewPanel, cancelToken) { const identifier = await this.createDiagramIdentifier(document); if (!identifier) { throw new Error(`Document type not supported: ${document.uri.toString(true)}`); } document.endpoint = this.createEndpoint(identifier, webviewPanel); await this.configureWebview(document, webviewPanel, cancelToken); } createEndpoint(identifier, webviewContainer) { var _a, _b; 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; } /** * Configure the given 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. */ configureWebview(document, webviewPanel, cancelToken) { var _a, _b; const extensionPath = this.options.extensionUri.fsPath; webviewPanel.webview.options = { localResourceRoots: (_a = this.options.localResourceRoots) !== null && _a !== void 0 ? _a : [(0, webview_utils_1.createFileUri)(extensionPath, 'pack')], enableScripts: true }; const identifier = (_b = document.endpoint) === null || _b === void 0 ? void 0 : _b.diagramIdentifier; if (identifier) { if (this.options.createWebviewHtml) { webviewPanel.webview.html = this.options.createWebviewHtml(identifier, webviewPanel); } else { const scriptUri = (0, webview_utils_1.createFileUri)(extensionPath, 'pack', 'webview.js'); webviewPanel.webview.html = (0, webview_utils_1.createWebviewHtml)(identifier, webviewPanel, { scriptUri }); } } } async createDiagramIdentifier(document, diagramType) { if (!diagramType) { diagramType = await this.getDiagramType(document.uri); if (!diagramType) { return undefined; } } const clientId = diagramType + '_' + SprottyEditorProvider.viewCount++; return { diagramType, uri: (0, webview_utils_1.serializeUri)(document.uri), clientId }; } /** * Determine a diagram type from the given URI. The default implementation returns the `viewType` of the custom * editor 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; } } saveCustomDocument(document, cancellation) { // Default: read-only diagram view, so no changes to save return Promise.resolve(); } revertCustomDocument(document, cancelToken) { // Default: read-only diagram view, so no changes to revert return Promise.resolve(); } saveCustomDocumentAs(document, destination, cancelToken) { return Promise.reject('Operation not supported.'); } backupCustomDocument(document, context, cancelToken) { return Promise.reject('Operation not supported.'); } } exports.SprottyEditorProvider = SprottyEditorProvider; SprottyEditorProvider.viewCount = 0; //# sourceMappingURL=sprotty-editor-provider.js.map