Skip to content

Commit

Permalink
small rework
Browse files Browse the repository at this point in the history
  • Loading branch information
salkin-naj committed Jun 25, 2020
1 parent aea93de commit 8285f46
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 45 deletions.
50 changes: 7 additions & 43 deletions 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, 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';
import { CommonCommands, quickCommand, OpenHandler, OpenerOptions, isFirefox } from '@theia/core/lib/browser';
import { CommonCommands, quickCommand, OpenHandler, OpenerOptions } 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 @@ -115,7 +115,6 @@ export class OutputContribution extends AbstractViewContribution<OutputWidget> i
protected readonly clipboardService: ClipboardService;

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

constructor() {
super({
Expand Down Expand Up @@ -147,12 +146,12 @@ export class OutputContribution extends AbstractViewContribution<OutputWidget> i
execute: () => this.widget.then(widget => widget.unlock())
});
registry.registerCommand(OutputCommands.COPY_ALL, {
execute: () => this.widget.then(widget => {
this.withWidget(widget, output => {
this.clipboardService.writeText(output.getText());
return true;
});
})
execute: () => {
const textToCopy = this.tryGetWidget()?.getText();
if (textToCopy) {
this.clipboardService.writeText(textToCopy);
}
}
});
}

Expand Down Expand Up @@ -194,39 +193,4 @@ 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();
}
}
}
4 changes: 2 additions & 2 deletions packages/output/src/browser/output-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,15 @@ export class OutputWidget extends BaseWidget implements StatefulWidget {
return undefined;
}

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

}
Expand Down

0 comments on commit 8285f46

Please sign in to comment.