|
| 1 | +// ***************************************************************************** |
| 2 | +// Copyright (C) 2024 TypeFox and others. |
| 3 | +// |
| 4 | +// This program and the accompanying materials are made available under the |
| 5 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | +// http://www.eclipse.org/legal/epl-2.0. |
| 7 | +// |
| 8 | +// This Source Code may also be made available under the following Secondary |
| 9 | +// Licenses when the conditions for such availability set forth in the Eclipse |
| 10 | +// Public License v. 2.0 are satisfied: GNU General Public License, version 2 |
| 11 | +// with the GNU Classpath Exception which is available at |
| 12 | +// https://www.gnu.org/software/classpath/license.html. |
| 13 | +// |
| 14 | +// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 |
| 15 | +// ***************************************************************************** |
| 16 | + |
| 17 | +import { Command, CommandContribution, CommandRegistry } from '@theia/core'; |
| 18 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 19 | +import { NotebookEditorWidgetService } from '../service/notebook-editor-widget-service'; |
| 20 | +import { CellOutput, CellUri } from '../../common'; |
| 21 | +import { NotebookCellModel } from '../view-model/notebook-cell-model'; |
| 22 | +import { EditorManager } from '@theia/editor/lib/browser'; |
| 23 | + |
| 24 | +export namespace NotebookOutputCommands { |
| 25 | + export const ENABLE_SCROLLING = Command.toDefaultLocalizedCommand({ |
| 26 | + id: 'cellOutput.enableScrolling', |
| 27 | + }); |
| 28 | + |
| 29 | + export const OPEN_LARGE_OUTPUT = Command.toDefaultLocalizedCommand({ |
| 30 | + id: 'workbench.action.openLargeOutput', |
| 31 | + label: 'Open Large Output' |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +@injectable() |
| 36 | +export class NotebookOutputActionContribution implements CommandContribution { |
| 37 | + |
| 38 | + @inject(NotebookEditorWidgetService) |
| 39 | + protected readonly notebookEditorService: NotebookEditorWidgetService; |
| 40 | + |
| 41 | + @inject(EditorManager) |
| 42 | + protected readonly editorManager: EditorManager; |
| 43 | + |
| 44 | + registerCommands(commands: CommandRegistry): void { |
| 45 | + commands.registerCommand(NotebookOutputCommands.ENABLE_SCROLLING, { |
| 46 | + execute: outputId => { |
| 47 | + const [cell, output] = this.findOutputAndCell(outputId) ?? []; |
| 48 | + if (cell && output?.metadata) { |
| 49 | + output.metadata['scrollable'] = true; |
| 50 | + cell.restartOutputRenderer(output.outputId); |
| 51 | + } |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + commands.registerCommand(NotebookOutputCommands.OPEN_LARGE_OUTPUT, { |
| 56 | + execute: outputId => { |
| 57 | + const [cell, output] = this.findOutputAndCell(outputId) ?? []; |
| 58 | + if (cell && output) { |
| 59 | + this.editorManager.open(CellUri.generateCellOutputUri(CellUri.parse(cell.uri)!.notebook, output.outputId)); |
| 60 | + } |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + protected findOutputAndCell(output: string): [NotebookCellModel, CellOutput] | undefined { |
| 66 | + const model = this.notebookEditorService.focusedEditor?.model; |
| 67 | + if (!model) { |
| 68 | + return undefined; |
| 69 | + } |
| 70 | + |
| 71 | + const outputId = output.slice(0, output.lastIndexOf('-')); |
| 72 | + |
| 73 | + for (const cell of model.cells) { |
| 74 | + for (const outputModel of cell.outputs) { |
| 75 | + if (outputModel.outputId === outputId) { |
| 76 | + return [cell, outputModel]; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments