UNPKG

sprotty-theia

Version:

Glue code for Sprotty diagrams in a Theia IDE

192 lines 9.43 kB
"use strict"; /******************************************************************************** * Copyright (c) 2017-2018 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 ********************************************************************************/ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DiagramCommandContribution = exports.OpenInDiagramHandler = exports.isDiagramWidgetContainer = exports.DiagramCommandHandler = exports.DiagramMenuContribution = exports.DiagramMenus = exports.DiagramCommands = void 0; const sprotty_1 = require("sprotty"); const sprotty_protocol_1 = require("sprotty-protocol"); const diagram_widget_1 = require("./diagram-widget"); const inversify_1 = require("inversify"); const common_1 = require("@theia/core/lib/common"); const browser_1 = require("@theia/core/lib/browser"); const browser_2 = require("@theia/editor/lib/browser"); const diagram_manager_1 = require("./diagram-manager"); var DiagramCommands; (function (DiagramCommands) { DiagramCommands.CENTER = 'diagram:center'; DiagramCommands.FIT = 'diagram:fit'; DiagramCommands.EXPORT = 'diagram:export'; DiagramCommands.SELECT_ALL = 'diagram.selectAll'; DiagramCommands.OPEN_IN_DIAGRAM = 'diagram.open'; DiagramCommands.DELETE = 'diagram.delete'; DiagramCommands.LAYOUT = 'diagram.layout'; })(DiagramCommands = exports.DiagramCommands || (exports.DiagramCommands = {})); var DiagramMenus; (function (DiagramMenus) { DiagramMenus.DIAGRAM = common_1.MAIN_MENU_BAR.concat("3_diagram"); DiagramMenus.EDITOR_CONTEXT_DIAGRAM = browser_2.EDITOR_CONTEXT_MENU.concat("a_diagram"); })(DiagramMenus = exports.DiagramMenus || (exports.DiagramMenus = {})); let DiagramMenuContribution = /** @class */ (() => { let DiagramMenuContribution = class DiagramMenuContribution { registerMenus(registry) { registry.registerSubmenu(DiagramMenus.DIAGRAM, "Diagram"); registry.registerMenuAction(DiagramMenus.DIAGRAM, { commandId: DiagramCommands.CENTER }); registry.registerMenuAction(DiagramMenus.DIAGRAM, { commandId: DiagramCommands.FIT }); registry.registerMenuAction(DiagramMenus.DIAGRAM, { commandId: DiagramCommands.EXPORT }); registry.registerMenuAction(DiagramMenus.DIAGRAM, { commandId: DiagramCommands.LAYOUT }); registry.registerMenuAction(DiagramMenus.EDITOR_CONTEXT_DIAGRAM, { commandId: DiagramCommands.OPEN_IN_DIAGRAM }); } }; DiagramMenuContribution = __decorate([ inversify_1.injectable() ], DiagramMenuContribution); return DiagramMenuContribution; })(); exports.DiagramMenuContribution = DiagramMenuContribution; class DiagramCommandHandler { constructor(shell, doExecute) { this.shell = shell; this.doExecute = doExecute; } execute(...args) { return this.isEnabled() ? this.doExecute(this.diagramWidget) : undefined; } isEnabled() { return this.diagramWidget !== undefined; } get diagramWidget() { const widget = (this.shell.activeWidget || this.shell.currentWidget); if (widget instanceof diagram_widget_1.DiagramWidget) { return widget; } else if (isDiagramWidgetContainer(widget)) { return widget.diagramWidget; } return undefined; } } exports.DiagramCommandHandler = DiagramCommandHandler; function isDiagramWidgetContainer(widget) { return widget !== undefined && widget.diagramWidget instanceof diagram_widget_1.DiagramWidget; } exports.isDiagramWidgetContainer = isDiagramWidgetContainer; class OpenInDiagramHandler { constructor(editorManager, openerService) { this.editorManager = editorManager; this.openerService = openerService; } execute(...args) { const editor = this.editorManager.currentEditor; if (editor !== undefined) { const uri = editor.editor.uri; const openersPromise = this.openerService.getOpeners(uri); openersPromise.then(openers => { const opener = openers.find(o => o instanceof diagram_manager_1.DiagramManager); if (opener !== undefined) opener.open(uri); }); } } isEnabled() { const editor = this.editorManager.currentEditor; if (editor) { const uri = editor.editor.uri; return uri.scheme !== 'diff'; } return false; } } exports.OpenInDiagramHandler = OpenInDiagramHandler; let DiagramCommandContribution = /** @class */ (() => { let DiagramCommandContribution = class DiagramCommandContribution { registerCommands(registry) { registry.registerCommand({ id: DiagramCommands.CENTER, label: 'Center' }); registry.registerCommand({ id: DiagramCommands.FIT, label: 'Fit to screen' }); registry.registerCommand({ id: DiagramCommands.EXPORT, label: 'Export' }); registry.registerCommand({ id: DiagramCommands.LAYOUT, label: 'Layout' }); registry.registerCommand({ id: DiagramCommands.SELECT_ALL, label: 'Select all' }); registry.registerCommand({ id: DiagramCommands.OPEN_IN_DIAGRAM, label: 'Open in Diagram' }); registry.registerHandler(DiagramCommands.CENTER, new DiagramCommandHandler(this.shell, widget => widget.actionDispatcher.dispatch(sprotty_protocol_1.CenterAction.create([])))); registry.registerHandler(DiagramCommands.FIT, new DiagramCommandHandler(this.shell, widget => widget.actionDispatcher.dispatch(sprotty_protocol_1.FitToScreenAction.create([])))); registry.registerHandler(DiagramCommands.EXPORT, new DiagramCommandHandler(this.shell, widget => widget.actionDispatcher.dispatch(sprotty_1.RequestExportSvgAction.create()))); registry.registerHandler(DiagramCommands.LAYOUT, new DiagramCommandHandler(this.shell, widget => widget.actionDispatcher.dispatch(sprotty_protocol_1.LayoutAction.create()))); registry.registerHandler(DiagramCommands.SELECT_ALL, new DiagramCommandHandler(this.shell, widget => { const action = sprotty_protocol_1.SelectAllAction.create({ select: true }); widget.actionDispatcher.dispatch(action); })); registry.registerHandler(DiagramCommands.OPEN_IN_DIAGRAM, new OpenInDiagramHandler(this.editorManager, this.openerService)); registry.registerHandler(browser_1.CommonCommands.UNDO.id, new DiagramCommandHandler(this.shell, widget => widget.actionDispatcher.dispatch(sprotty_1.UndoAction.create()))); registry.registerHandler(browser_1.CommonCommands.REDO.id, new DiagramCommandHandler(this.shell, widget => widget.actionDispatcher.dispatch(sprotty_1.RedoAction.create()))); } }; __decorate([ inversify_1.inject(browser_1.ApplicationShell), __metadata("design:type", browser_1.ApplicationShell) ], DiagramCommandContribution.prototype, "shell", void 0); __decorate([ inversify_1.inject(browser_2.EditorManager), __metadata("design:type", browser_2.EditorManager) ], DiagramCommandContribution.prototype, "editorManager", void 0); __decorate([ inversify_1.inject(browser_1.OpenerService), __metadata("design:type", Object) ], DiagramCommandContribution.prototype, "openerService", void 0); DiagramCommandContribution = __decorate([ inversify_1.injectable() ], DiagramCommandContribution); return DiagramCommandContribution; })(); exports.DiagramCommandContribution = DiagramCommandContribution; //# sourceMappingURL=diagram-commands.js.map