clipboardService.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  6. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  7. return new (P || (P = Promise))(function (resolve, reject) {
  8. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  9. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  10. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  11. step((generator = generator.apply(thisArg, _arguments || [])).next());
  12. });
  13. };
  14. import { $ } from '../../../base/browser/dom.js';
  15. export class BrowserClipboardService {
  16. constructor() {
  17. this.mapTextToType = new Map(); // unsupported in web (only in-memory)
  18. this.findText = ''; // unsupported in web (only in-memory)
  19. }
  20. writeText(text, type) {
  21. return __awaiter(this, void 0, void 0, function* () {
  22. // With type: only in-memory is supported
  23. if (type) {
  24. this.mapTextToType.set(type, text);
  25. return;
  26. }
  27. // Guard access to navigator.clipboard with try/catch
  28. // as we have seen DOMExceptions in certain browsers
  29. // due to security policies.
  30. try {
  31. return yield navigator.clipboard.writeText(text);
  32. }
  33. catch (error) {
  34. console.error(error);
  35. }
  36. // Fallback to textarea and execCommand solution
  37. const activeElement = document.activeElement;
  38. const textArea = document.body.appendChild($('textarea', { 'aria-hidden': true }));
  39. textArea.style.height = '1px';
  40. textArea.style.width = '1px';
  41. textArea.style.position = 'absolute';
  42. textArea.value = text;
  43. textArea.focus();
  44. textArea.select();
  45. document.execCommand('copy');
  46. if (activeElement instanceof HTMLElement) {
  47. activeElement.focus();
  48. }
  49. document.body.removeChild(textArea);
  50. return;
  51. });
  52. }
  53. readText(type) {
  54. return __awaiter(this, void 0, void 0, function* () {
  55. // With type: only in-memory is supported
  56. if (type) {
  57. return this.mapTextToType.get(type) || '';
  58. }
  59. // Guard access to navigator.clipboard with try/catch
  60. // as we have seen DOMExceptions in certain browsers
  61. // due to security policies.
  62. try {
  63. return yield navigator.clipboard.readText();
  64. }
  65. catch (error) {
  66. console.error(error);
  67. return '';
  68. }
  69. });
  70. }
  71. readFindText() {
  72. return __awaiter(this, void 0, void 0, function* () {
  73. return this.findText;
  74. });
  75. }
  76. writeFindText(text) {
  77. return __awaiter(this, void 0, void 0, function* () {
  78. this.findText = text;
  79. });
  80. }
  81. }