From 2cbc2170734d7faed89073c859343a66bb3a5153 Mon Sep 17 00:00:00 2001 From: Jan-Niklas Spangenberg Date: Fri, 19 Jun 2020 12:18:28 +0200 Subject: [PATCH] 'Copy all' for context menu in output view This adds another option to the context menu of the output view, which copies everything in the selected channel into the clipboard, using the ClipboardService. Closes #7912 Signed-off-by: Jan-Niklas Spangenberg --- .../output/src/browser/output-contribution.ts | 22 +++++++++++++++++-- packages/output/src/browser/output-widget.tsx | 11 ++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/output/src/browser/output-contribution.ts b/packages/output/src/browser/output-contribution.ts index 0041d6acf246b..57aab1c0aead4 100644 --- a/packages/output/src/browser/output-contribution.ts +++ b/packages/output/src/browser/output-contribution.ts @@ -14,7 +14,7 @@ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ -import { injectable } from 'inversify'; +import { injectable, inject } from 'inversify'; import URI from '@theia/core/lib/common/uri'; import { Widget } from '@theia/core/lib/browser/widgets/widget'; import { MaybePromise } from '@theia/core/lib/common/types'; @@ -24,6 +24,7 @@ import { AbstractViewContribution } from '@theia/core/lib/browser/shell/view-con import { OutputWidget } from './output-widget'; import { OutputContextMenu } from './output-context-menu'; import { OutputUri } from '../common/output-uri'; +import { ClipboardService } from '@theia/core/lib/browser/clipboard-service'; export namespace OutputCommands { @@ -100,11 +101,17 @@ export namespace OutputCommands { category: OUTPUT_CATEGORY }; + export const COPY_ALL: Command = { + id: 'output:copy-all', + }; } @injectable() export class OutputContribution extends AbstractViewContribution implements OpenHandler { + @inject(ClipboardService) + protected readonly clipboardService: ClipboardService; + readonly id: string = `${OutputWidget.ID}-opener`; constructor() { @@ -136,6 +143,14 @@ export class OutputContribution extends AbstractViewContribution i isVisible: widget => this.withWidget(widget, output => output.isLocked), execute: () => this.widget.then(widget => widget.unlock()) }); + registry.registerCommand(OutputCommands.COPY_ALL, { + execute: () => { + const textToCopy = this.tryGetWidget()?.getText(); + if (textToCopy) { + this.clipboardService.writeText(textToCopy); + } + } + }); } registerMenus(registry: MenuModelRegistry): void { @@ -143,6 +158,10 @@ export class OutputContribution extends AbstractViewContribution i registry.registerMenuAction(OutputContextMenu.TEXT_EDIT_GROUP, { commandId: CommonCommands.COPY.id }); + registry.registerMenuAction(OutputContextMenu.TEXT_EDIT_GROUP, { + commandId: OutputCommands.COPY_ALL.id, + label: 'Copy All' + }); registry.registerMenuAction(OutputContextMenu.COMMAND_GROUP, { commandId: quickCommand.id, label: 'Find Command...' @@ -173,5 +192,4 @@ export class OutputContribution extends AbstractViewContribution i return widget instanceof OutputWidget ? predicate(widget) : false; } - } diff --git a/packages/output/src/browser/output-widget.tsx b/packages/output/src/browser/output-widget.tsx index 0d137c3bd3dcd..470a224d5c61c 100644 --- a/packages/output/src/browser/output-widget.tsx +++ b/packages/output/src/browser/output-widget.tsx @@ -220,6 +220,17 @@ export class OutputWidget extends BaseWidget implements StatefulWidget { return undefined; } + getText(): string | undefined { + const editor = this.editor; + if (editor) { + const model = editor.getControl().getModel(); + if (model) { + return model.getValue(); + } + } + return undefined; + } + } export namespace OutputWidget {