dataTransfer.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 { distinct } from './arrays.js';
  15. import { Iterable } from './iterator.js';
  16. import { generateUuid } from './uuid.js';
  17. export function createStringDataTransferItem(stringOrPromise) {
  18. return {
  19. id: generateUuid(),
  20. asString: () => __awaiter(this, void 0, void 0, function* () { return stringOrPromise; }),
  21. asFile: () => undefined,
  22. value: typeof stringOrPromise === 'string' ? stringOrPromise : undefined,
  23. };
  24. }
  25. export function createFileDataTransferItem(fileName, uri, data) {
  26. return {
  27. id: generateUuid(),
  28. asString: () => __awaiter(this, void 0, void 0, function* () { return ''; }),
  29. asFile: () => ({ name: fileName, uri, data }),
  30. value: undefined,
  31. };
  32. }
  33. export class VSDataTransfer {
  34. constructor() {
  35. this._entries = new Map();
  36. }
  37. /**
  38. * Get the total number of entries in this data transfer.
  39. */
  40. get size() {
  41. let size = 0;
  42. this.forEach(() => size++);
  43. return size;
  44. }
  45. /**
  46. * Check if this data transfer contains data for `mimeType`.
  47. *
  48. * This uses exact matching and does not support wildcards.
  49. */
  50. has(mimeType) {
  51. return this._entries.has(this.toKey(mimeType));
  52. }
  53. /**
  54. * Check if this data transfer contains data matching `mimeTypeGlob`.
  55. *
  56. * This allows matching for wildcards, such as `image/*`.
  57. *
  58. * Use the special `files` mime type to match any file in the data transfer.
  59. */
  60. matches(mimeTypeGlob) {
  61. // Exact match
  62. if (this.has(mimeTypeGlob)) {
  63. return true;
  64. }
  65. // Special `files` mime type matches any file
  66. if (mimeTypeGlob.toLowerCase() === 'files') {
  67. return Iterable.some(this.values(), item => item.asFile());
  68. }
  69. // Anything glob
  70. if (mimeTypeGlob === '*/*') {
  71. return this._entries.size > 0;
  72. }
  73. // Wildcard, such as `image/*`
  74. const wildcard = this.toKey(mimeTypeGlob).match(/^([a-z]+)\/([a-z]+|\*)$/i);
  75. if (!wildcard) {
  76. return false;
  77. }
  78. const [_, type, subtype] = wildcard;
  79. if (subtype === '*') {
  80. return Iterable.some(this._entries.keys(), key => key.startsWith(type + '/'));
  81. }
  82. return false;
  83. }
  84. /**
  85. * Retrieve the first entry for `mimeType`.
  86. *
  87. * Note that if want to find all entries for a given mime type, use {@link VSDataTransfer.entries} instead.
  88. */
  89. get(mimeType) {
  90. var _a;
  91. return (_a = this._entries.get(this.toKey(mimeType))) === null || _a === void 0 ? void 0 : _a[0];
  92. }
  93. /**
  94. * Add a new entry to this data transfer.
  95. *
  96. * This does not replace existing entries for `mimeType`.
  97. */
  98. append(mimeType, value) {
  99. const existing = this._entries.get(mimeType);
  100. if (existing) {
  101. existing.push(value);
  102. }
  103. else {
  104. this._entries.set(this.toKey(mimeType), [value]);
  105. }
  106. }
  107. /**
  108. * Set the entry for a given mime type.
  109. *
  110. * This replaces all existing entries for `mimeType`.
  111. */
  112. replace(mimeType, value) {
  113. this._entries.set(this.toKey(mimeType), [value]);
  114. }
  115. /**
  116. * Remove all entries for `mimeType`.
  117. */
  118. delete(mimeType) {
  119. this._entries.delete(this.toKey(mimeType));
  120. }
  121. /**
  122. * Iterate over all `[mime, item]` pairs in this data transfer.
  123. *
  124. * There may be multiple entries for each mime type.
  125. */
  126. *entries() {
  127. for (const [mine, items] of this._entries.entries()) {
  128. for (const item of items) {
  129. yield [mine, item];
  130. }
  131. }
  132. }
  133. /**
  134. * Iterate over all items in this data transfer.
  135. *
  136. * There may be multiple entries for each mime type.
  137. */
  138. values() {
  139. return Array.from(this._entries.values()).flat();
  140. }
  141. /**
  142. * Call `f` for each item and mime in the data transfer.
  143. *
  144. * There may be multiple entries for each mime type.
  145. */
  146. forEach(f) {
  147. for (const [mime, item] of this.entries()) {
  148. f(item, mime);
  149. }
  150. }
  151. toKey(mimeType) {
  152. return mimeType.toLowerCase();
  153. }
  154. }
  155. export const UriList = Object.freeze({
  156. // http://amundsen.com/hypermedia/urilist/
  157. create: (entries) => {
  158. return distinct(entries.map(x => x.toString())).join('\r\n');
  159. },
  160. split: (str) => {
  161. return str.split('\r\n');
  162. },
  163. parse: (str) => {
  164. return UriList.split(str).filter(value => !value.startsWith('#'));
  165. }
  166. });