UNPKG

@eclipse-glsp/vscode-integration

Version:

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

97 lines 4.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GlspEditorProvider = void 0; /******************************************************************************** * Copyright (c) 2021-2026 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.01 ********************************************************************************/ const vscode = require("vscode"); const webview_endpoint_1 = require("./webview-endpoint"); /** * An extensible base class to create a CustomEditorProvider that takes care of * diagram initialization and custom document events. * * Webview setup needs to be implemented. */ class GlspEditorProvider { get onDidChangeCustomDocument() { return this.onDidChangeCustomDocumentEmitter.event; } constructor(glspVscodeConnector) { this.glspVscodeConnector = glspVscodeConnector; /** Used to generate continuous and unique clientIds - TODO: consider replacing this with uuid. */ this.viewCount = 0; this.ownedDocuments = new Set(); this.disposables = []; this.onDidChangeCustomDocumentEmitter = new vscode.EventEmitter(); this.disposables.push(this.onDidChangeCustomDocumentEmitter, glspVscodeConnector.onDidChangeCustomDocument(e => { if (this.ownedDocuments.has(e.document)) { this.onDidChangeCustomDocumentEmitter.fire(e); } })); } saveCustomDocument(document, _cancellation) { return this.glspVscodeConnector.saveDocument(document); } saveCustomDocumentAs(document, destination, _cancellation) { return this.glspVscodeConnector.saveDocument(document, destination); } revertCustomDocument(document, _cancellation) { return this.glspVscodeConnector.revertDocument(document, this.diagramType); } backupCustomDocument(_document, context, _cancellation) { // Basically do the bare minimum - which is nothing return Promise.resolve({ id: context.destination.toString(), delete: () => undefined }); } openCustomDocument(uri, _openContext, _token) { const document = { uri, dispose: () => { this.ownedDocuments.delete(document); } }; this.ownedDocuments.add(document); return document; } async resolveCustomEditor(document, webviewPanel, token) { // This is used to initialize GLSP for our diagram const diagramIdentifier = { diagramType: this.diagramType, uri: serializeUri(document.uri), clientId: `${this.diagramType}_${this.viewCount++}` }; const endpoint = new webview_endpoint_1.WebviewEndpoint({ diagramIdentifier, messenger: this.glspVscodeConnector.messenger, webviewPanel }); // Register document/diagram panel/model in vscode connector this.glspVscodeConnector.registerClient({ clientId: diagramIdentifier.clientId, diagramType: diagramIdentifier.diagramType, document: document, webviewEndpoint: endpoint }); this.setUpWebview(document, webviewPanel, token, diagramIdentifier.clientId); } dispose() { this.disposables.forEach(disposable => disposable.dispose()); } } exports.GlspEditorProvider = GlspEditorProvider; function serializeUri(uri) { let uriString = uri.toString(); const match = uriString.match(/file:\/\/\/([a-z])%3A/i); if (match) { uriString = 'file:///' + match[1] + ':' + uriString.substring(match[0].length); } return uriString; } //# sourceMappingURL=glsp-editor-provider.js.map