dataTransfer.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. export function createStringDataTransferItem(stringOrPromise) {
  15. return {
  16. asString: () => __awaiter(this, void 0, void 0, function* () { return stringOrPromise; }),
  17. asFile: () => undefined,
  18. value: typeof stringOrPromise === 'string' ? stringOrPromise : undefined,
  19. };
  20. }
  21. export function createFileDataTransferItem(fileName, uri, data) {
  22. return {
  23. asString: () => __awaiter(this, void 0, void 0, function* () { return ''; }),
  24. asFile: () => ({ name: fileName, uri, data }),
  25. value: undefined,
  26. };
  27. }
  28. export class VSDataTransfer {
  29. constructor() {
  30. this._entries = new Map();
  31. }
  32. get size() {
  33. return this._entries.size;
  34. }
  35. has(mimeType) {
  36. return this._entries.has(this.toKey(mimeType));
  37. }
  38. get(mimeType) {
  39. var _a;
  40. return (_a = this._entries.get(this.toKey(mimeType))) === null || _a === void 0 ? void 0 : _a[0];
  41. }
  42. append(mimeType, value) {
  43. const existing = this._entries.get(mimeType);
  44. if (existing) {
  45. existing.push(value);
  46. }
  47. else {
  48. this._entries.set(this.toKey(mimeType), [value]);
  49. }
  50. }
  51. replace(mimeType, value) {
  52. this._entries.set(this.toKey(mimeType), [value]);
  53. }
  54. delete(mimeType) {
  55. this._entries.delete(this.toKey(mimeType));
  56. }
  57. *entries() {
  58. for (const [mine, items] of this._entries.entries()) {
  59. for (const item of items) {
  60. yield [mine, item];
  61. }
  62. }
  63. }
  64. values() {
  65. return Array.from(this._entries.values()).flat();
  66. }
  67. forEach(f) {
  68. for (const [mime, item] of this.entries()) {
  69. f(item, mime);
  70. }
  71. }
  72. toKey(mimeType) {
  73. return mimeType.toLowerCase();
  74. }
  75. }