UNPKG

@eclipse-glsp/theia-integration

Version:

Glue code to integrate GLSP clients into Eclipse Theia

106 lines 4.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TheiaExportCommandHandler = void 0; /******************************************************************************** * Copyright (c) 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.0 ********************************************************************************/ const client_1 = require("@eclipse-glsp/client"); const core_1 = require("@theia/core"); const buffer_1 = require("@theia/core/lib/common/buffer"); const glsp_diagram_widget_1 = require("../../glsp-diagram-widget"); const EXPORT_FORMATS = [ { label: 'SVG', format: 'svg', extension: 'svg', filterLabel: 'SVG Image (*.svg)' }, { label: 'PNG', format: 'png', extension: 'png', filterLabel: 'PNG Image (*.png)' } ]; /** * {@link CommandHandler} for the "Export Diagram" menu command. Asks the user for the target * format up front, then opens the save dialog with a single matching filter so the resulting * URI's extension is unambiguous. Uses the request/response dispatcher path so the export * result lands here directly rather than fanning out to action handlers. */ class TheiaExportCommandHandler { constructor(shell, fileService, fileDialogService, quickInputService, messageService) { this.shell = shell; this.fileService = fileService; this.fileDialogService = fileDialogService; this.quickInputService = quickInputService; this.messageService = messageService; } async execute() { const widget = (0, glsp_diagram_widget_1.getDiagramWidget)(this.shell); if (!widget) { return; } const choice = await this.pickFormat(); if (!choice) { return; } const folder = await this.getExportFolder(widget); let file = await this.fileDialogService.showSaveDialog({ title: 'Export Diagram', filters: { [choice.filterLabel]: [choice.extension] } }, folder); if (!file) { return; } file = this.ensureExtension(file, choice.extension); try { const result = await widget.actionDispatcher.request(client_1.RequestExportAction.create(choice.format)); await this.writeResult(file, result); this.messageService.info(`Diagram exported to '${file.path.name}'`); } catch (error) { this.messageService.error(`Error exporting diagram: ${error}`); } } isEnabled() { return !!(0, glsp_diagram_widget_1.getDiagramWidget)(this.shell); } isVisible() { return true; } async pickFormat() { const items = EXPORT_FORMATS.map(choice => ({ label: choice.label, description: choice.filterLabel, choice })); const picked = await this.quickInputService.showQuickPick(items, { title: 'Export Diagram', placeholder: 'Select export format' }); return picked === null || picked === void 0 ? void 0 : picked.choice; } ensureExtension(file, extension) { const expected = '.' + extension; return file.path.ext.toLowerCase() === expected ? file : new core_1.URI(file.path.fsPath() + expected); } writeResult(file, result) { if (result.encoding === 'base64') { return this.fileService.writeFile(file, buffer_1.BinaryBuffer.wrap(this.decodeBase64(result.data))); } return this.fileService.write(file, result.data); } decodeBase64(data) { const binary = atob(data); const bytes = new Uint8Array(binary.length); for (let index = 0; index < binary.length; index++) { bytes[index] = binary.charCodeAt(index); } return bytes; } getExportFolder(widget) { return this.fileService.resolve(new core_1.URI(widget.editorContext.sourceUri)); } } exports.TheiaExportCommandHandler = TheiaExportCommandHandler; //# sourceMappingURL=theia-export-command-handler.js.map