UNPKG

5.82 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2019 RedHat and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21 return c > 3 && r && Object.defineProperty(target, key, r), r;
22};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.BrowserClipboardService = void 0;
28const inversify_1 = require("inversify");
29const browser_1 = require("./browser");
30const logger_1 = require("../common/logger");
31const message_service_1 = require("../common/message-service");
32const nls_1 = require("../common/nls");
33let BrowserClipboardService = class BrowserClipboardService {
34 async readText() {
35 let permission;
36 try {
37 permission = await this.queryPermission('clipboard-read');
38 }
39 catch (e1) {
40 this.logger.error('Failed checking a clipboard-read permission.', e1);
41 // in FireFox, Clipboard API isn't gated with the permissions
42 try {
43 return await this.getClipboardAPI().readText();
44 }
45 catch (e2) {
46 this.logger.error('Failed reading clipboard content.', e2);
47 if (browser_1.isFirefox) {
48 this.messageService.warn(nls_1.nls.localize('theia/navigator/clipboardWarnFirefox',
49 // eslint-disable-next-line max-len
50 "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'));
51 }
52 return '';
53 }
54 }
55 if (permission.state === 'denied') {
56 // most likely, the user intentionally denied the access
57 this.messageService.warn(nls_1.nls.localize('theia/navigator/clipboardWarn', "Access to the clipboard is denied. Check your browser's permission."));
58 return '';
59 }
60 return this.getClipboardAPI().readText();
61 }
62 async writeText(value) {
63 let permission;
64 try {
65 permission = await this.queryPermission('clipboard-write');
66 }
67 catch (e1) {
68 this.logger.error('Failed checking a clipboard-write permission.', e1);
69 // in FireFox, Clipboard API isn't gated with the permissions
70 try {
71 await this.getClipboardAPI().writeText(value);
72 return;
73 }
74 catch (e2) {
75 this.logger.error('Failed writing to the clipboard.', e2);
76 if (browser_1.isFirefox) {
77 this.messageService.warn(nls_1.nls.localize('theia/core/navigator/clipboardWarnFirefox',
78 // eslint-disable-next-line max-len
79 "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'));
80 }
81 return;
82 }
83 }
84 if (permission.state === 'denied') {
85 // most likely, the user intentionally denied the access
86 this.messageService.warn(nls_1.nls.localize('theia/core/navigator/clipboardWarn', "Access to the clipboard is denied. Check your browser's permission."));
87 return;
88 }
89 return this.getClipboardAPI().writeText(value);
90 }
91 async queryPermission(name) {
92 if ('permissions' in navigator) {
93 return navigator['permissions'].query({ name: name });
94 }
95 throw new Error('Permissions API unavailable');
96 }
97 getClipboardAPI() {
98 if ('clipboard' in navigator) {
99 return navigator['clipboard'];
100 }
101 throw new Error('Async Clipboard API unavailable');
102 }
103};
104__decorate([
105 (0, inversify_1.inject)(message_service_1.MessageService),
106 __metadata("design:type", message_service_1.MessageService)
107], BrowserClipboardService.prototype, "messageService", void 0);
108__decorate([
109 (0, inversify_1.inject)(logger_1.ILogger),
110 __metadata("design:type", Object)
111], BrowserClipboardService.prototype, "logger", void 0);
112BrowserClipboardService = __decorate([
113 (0, inversify_1.injectable)()
114], BrowserClipboardService);
115exports.BrowserClipboardService = BrowserClipboardService;
116//# sourceMappingURL=browser-clipboard-service.js.map
\No newline at end of file