@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
111 lines • 5.09 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2019 RedHat and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { __decorate } from "tslib";
import { injectable, inject, named } from 'inversify';
import { isFirefox } from './browser.js';
import { Emitter } from '../common/event.js';
import { ILogger } from '../common/logger.js';
import { MessageService } from '../common/message-service.js';
import { nls } from '../common/nls.js';
let BrowserClipboardService = class BrowserClipboardService {
constructor() {
this.onDidWriteTextEmitter = new Emitter();
this.onDidWriteText = this.onDidWriteTextEmitter.event;
}
async readText() {
let permission;
try {
permission = await this.queryPermission('clipboard-read');
}
catch (e1) {
this.logger.error('Failed checking a clipboard-read permission.', e1);
// in FireFox, Clipboard API isn't gated with the permissions
try {
return await this.getClipboardAPI().readText();
}
catch (e2) {
this.logger.error('Failed reading clipboard content.', e2);
if (isFirefox) {
this.messageService.warn(nls.localize('theia/navigator/clipboardWarnFirefox',
// eslint-disable-next-line max-len
"Clipboard API is not available. It can be enabled by '{0}' preference on '{1}' page. Then reload Theia. Note, it will allow FireFox getting full access to the system clipboard.", 'dom.events.testing.asyncClipboard', 'about:config'));
}
return '';
}
}
if (permission.state === 'denied') {
// most likely, the user intentionally denied the access
this.messageService.warn(nls.localize('theia/navigator/clipboardWarn', "Access to the clipboard is denied. Check your browser's permission."));
return '';
}
return this.getClipboardAPI().readText();
}
async writeText(value) {
let permission;
try {
permission = await this.queryPermission('clipboard-write');
}
catch (e1) {
this.logger.error('Failed checking a clipboard-write permission.', e1);
// in FireFox, Clipboard API isn't gated with the permissions
try {
await this.getClipboardAPI().writeText(value);
this.onDidWriteTextEmitter.fire(value);
return;
}
catch (e2) {
this.logger.error('Failed writing to the clipboard.', e2);
if (isFirefox) {
this.messageService.warn(nls.localize('theia/core/navigator/clipboardWarnFirefox',
// eslint-disable-next-line max-len
"Clipboard API is not available. It can be enabled by '{0}' preference on '{1}' page. Then reload Theia. Note, it will allow FireFox getting full access to the system clipboard.", 'dom.events.testing.asyncClipboard', 'about:config'));
}
return;
}
}
if (permission.state === 'denied') {
// most likely, the user intentionally denied the access
this.messageService.warn(nls.localize('theia/core/navigator/clipboardWarn', "Access to the clipboard is denied. Check your browser's permission."));
return;
}
await this.getClipboardAPI().writeText(value);
this.onDidWriteTextEmitter.fire(value);
}
async queryPermission(name) {
if ('permissions' in navigator) {
return navigator['permissions'].query({ name: name });
}
throw new Error('Permissions API unavailable');
}
getClipboardAPI() {
if ('clipboard' in navigator) {
return navigator['clipboard'];
}
throw new Error('Async Clipboard API unavailable');
}
};
__decorate([
inject(MessageService)
], BrowserClipboardService.prototype, "messageService", void 0);
__decorate([
inject(ILogger),
named('core:BrowserClipboardService')
], BrowserClipboardService.prototype, "logger", void 0);
BrowserClipboardService = __decorate([
injectable()
], BrowserClipboardService);
export { BrowserClipboardService };
//# sourceMappingURL=browser-clipboard-service.js.map