/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { $ } from '../../../base/browser/dom.js'; export class BrowserClipboardService { constructor() { this.mapTextToType = new Map(); // unsupported in web (only in-memory) this.findText = ''; // unsupported in web (only in-memory) } writeText(text, type) { return __awaiter(this, void 0, void 0, function* () { // With type: only in-memory is supported if (type) { this.mapTextToType.set(type, text); return; } // Guard access to navigator.clipboard with try/catch // as we have seen DOMExceptions in certain browsers // due to security policies. try { return yield navigator.clipboard.writeText(text); } catch (error) { console.error(error); } // Fallback to textarea and execCommand solution const activeElement = document.activeElement; const textArea = document.body.appendChild($('textarea', { 'aria-hidden': true })); textArea.style.height = '1px'; textArea.style.width = '1px'; textArea.style.position = 'absolute'; textArea.value = text; textArea.focus(); textArea.select(); document.execCommand('copy'); if (activeElement instanceof HTMLElement) { activeElement.focus(); } document.body.removeChild(textArea); return; }); } readText(type) { return __awaiter(this, void 0, void 0, function* () { // With type: only in-memory is supported if (type) { return this.mapTextToType.get(type) || ''; } // Guard access to navigator.clipboard with try/catch // as we have seen DOMExceptions in certain browsers // due to security policies. try { return yield navigator.clipboard.readText(); } catch (error) { console.error(error); return ''; } }); } readFindText() { return __awaiter(this, void 0, void 0, function* () { return this.findText; }); } writeFindText(text) { return __awaiter(this, void 0, void 0, function* () { this.findText = text; }); } }