Skip to content

Commit

Permalink
not working version, using clipboardService
Browse files Browse the repository at this point in the history
  • Loading branch information
salkin-naj committed Jun 23, 2020
1 parent 95a44a5 commit 98c5f20
Showing 1 changed file with 8 additions and 40 deletions.
48 changes: 8 additions & 40 deletions packages/output/src/browser/output-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
* 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';
import { CommonCommands, quickCommand, OpenHandler, OpenerOptions, isFirefox, KeybindingRegistry } from '@theia/core/lib/browser';
import { CommonCommands, quickCommand, OpenHandler, OpenerOptions, 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';
import { OutputContextMenu } from './output-context-menu';
import { OutputUri } from '../common/output-uri';
import { ClipboardService } from '@theia/core/src/browser/clipboard-service';

export namespace OutputCommands {

Expand Down Expand Up @@ -110,8 +111,10 @@ export namespace OutputCommands {
@injectable()
export class OutputContribution extends AbstractViewContribution<OutputWidget> implements OpenHandler {

@inject(ClipboardService)
protected readonly clipboardService: ClipboardService;

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

constructor() {
super({
Expand Down Expand Up @@ -144,9 +147,8 @@ export class OutputContribution extends AbstractViewContribution<OutputWidget> i
});
registry.registerCommand(OutputCommands.COPY_ALL, {
execute: () => {
this.widget.then(widget => {
const textToCopy = widget.getText();
this.copy(textToCopy);
this.widget.then(async widget => {
this.clipboardService.writeText(widget.getText());
});
}
});
Expand Down Expand Up @@ -197,39 +199,5 @@ 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();
}
}
}

0 comments on commit 98c5f20

Please sign in to comment.