Skip to content

Commit

Permalink
'Copy all' for context menu in output view
Browse files Browse the repository at this point in the history
This adds another option to the context menu of the output view,
which copies everything in the selected channel into the clipboard.

Closes #7912

Signed-off-by: Jan-Niklas Spangenberg <[email protected]>
  • Loading branch information
salkin-naj committed Jun 19, 2020
1 parent 8dae0ad commit 95a44a5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
60 changes: 59 additions & 1 deletion packages/output/src/browser/output-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { injectable } 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';
import { CommonCommands, quickCommand, OpenHandler, OpenerOptions } from '@theia/core/lib/browser';
import { CommonCommands, quickCommand, OpenHandler, OpenerOptions, isFirefox, KeybindingRegistry } from '@theia/core/lib/browser';
import { Command, CommandRegistry, MenuModelRegistry } from '@theia/core/lib/common';
import { AbstractViewContribution } from '@theia/core/lib/browser/shell/view-contribution';
import { OutputWidget } from './output-widget';
Expand Down Expand Up @@ -100,12 +100,18 @@ export namespace OutputCommands {
category: OUTPUT_CATEGORY
};

export const COPY_ALL: Command = {
id: 'output:copy-all',
label: 'Copy all',
category: OUTPUT_CATEGORY,
};
}

@injectable()
export class OutputContribution extends AbstractViewContribution<OutputWidget> implements OpenHandler {

readonly id: string = `${OutputWidget.ID}-opener`;
private textToCopy: string;

constructor() {
super({
Expand Down Expand Up @@ -136,13 +142,31 @@ export class OutputContribution extends AbstractViewContribution<OutputWidget> i
isVisible: widget => this.withWidget(widget, output => output.isLocked),
execute: () => this.widget.then(widget => widget.unlock())
});
registry.registerCommand(OutputCommands.COPY_ALL, {
execute: () => {
this.widget.then(widget => {
const textToCopy = widget.getText();
this.copy(textToCopy);
});
}
});
}

registerKeybindings(registry: KeybindingRegistry): void {
registry.registerKeybinding({
command: OutputCommands.COPY_ALL.id,
keybinding: 'ctrlcmd+shift+c'
});
}

registerMenus(registry: MenuModelRegistry): void {
super.registerMenus(registry);
registry.registerMenuAction(OutputContextMenu.TEXT_EDIT_GROUP, {
commandId: CommonCommands.COPY.id
});
registry.registerMenuAction(OutputContextMenu.TEXT_EDIT_GROUP, {
commandId: OutputCommands.COPY_ALL.id
});
registry.registerMenuAction(OutputContextMenu.COMMAND_GROUP, {
commandId: quickCommand.id,
label: 'Find Command...'
Expand Down Expand Up @@ -173,5 +197,39 @@ export class OutputContribution extends AbstractViewContribution<OutputWidget> i

return widget instanceof OutputWidget ? predicate(widget) : false;
}
private async clipBoardCopyIsGranted(): Promise<boolean> {
// Unfortunately Firefox doesn't support permission check `clipboard-write`, so let try to copy anyway,
if (isFirefox) {
return true;
}
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const permissions = (navigator as any).permissions;
const { state } = await permissions.query({ name: 'clipboard-write' });
if (state === 'granted') {
return true;
}
} catch (e) { }

return false;
}

private async writeToClipBoard(): Promise<void> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const clipboard = (navigator as any).clipboard;

try {
await clipboard.writeText(this.textToCopy);
} catch (e) {}
}

async copy(text: string): Promise<void> {
this.textToCopy = text;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const permissions = (navigator as any).permissions;
if (permissions && permissions.query && await this.clipBoardCopyIsGranted()) {
await this.writeToClipBoard();
}
}
}
11 changes: 11 additions & 0 deletions packages/output/src/browser/output-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@ export class OutputWidget extends BaseWidget implements StatefulWidget {
return undefined;
}

getText(): string {
const editor = this.editor;
if (editor) {
const model = editor.getControl().getModel();
if (model) {
return model.getValue();
}
}
return '';
}

}

export namespace OutputWidget {
Expand Down

0 comments on commit 95a44a5

Please sign in to comment.