1a99d4a3a2223730b2dd24dd54073550865f3ae557aab99e908763fcc6079f10ce0960a755cb2bed31c78701ede944821e3c332e9935291a9759190b22492b 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. import { DataTransfers } from '../../../base/browser/dnd.js';
  6. import { parse } from '../../../base/common/marshalling.js';
  7. import { URI } from '../../../base/common/uri.js';
  8. import { extractSelection } from '../../opener/common/opener.js';
  9. import { Registry } from '../../registry/common/platform.js';
  10. //#region Editor / Resources DND
  11. export const CodeDataTransfers = {
  12. EDITORS: 'CodeEditors',
  13. FILES: 'CodeFiles'
  14. };
  15. export function extractEditorsDropData(e) {
  16. var _a;
  17. const editors = [];
  18. if (e.dataTransfer && e.dataTransfer.types.length > 0) {
  19. // Data Transfer: Code Editors
  20. const rawEditorsData = e.dataTransfer.getData(CodeDataTransfers.EDITORS);
  21. if (rawEditorsData) {
  22. try {
  23. editors.push(...parse(rawEditorsData));
  24. }
  25. catch (error) {
  26. // Invalid transfer
  27. }
  28. }
  29. // Data Transfer: Resources
  30. else {
  31. try {
  32. const rawResourcesData = e.dataTransfer.getData(DataTransfers.RESOURCES);
  33. editors.push(...createDraggedEditorInputFromRawResourcesData(rawResourcesData));
  34. }
  35. catch (error) {
  36. // Invalid transfer
  37. }
  38. }
  39. // Check for native file transfer
  40. if ((_a = e.dataTransfer) === null || _a === void 0 ? void 0 : _a.files) {
  41. for (let i = 0; i < e.dataTransfer.files.length; i++) {
  42. const file = e.dataTransfer.files[i];
  43. if (file && file.path /* Electron only */) {
  44. try {
  45. editors.push({ resource: URI.file(file.path), isExternal: true, allowWorkspaceOpen: true });
  46. }
  47. catch (error) {
  48. // Invalid URI
  49. }
  50. }
  51. }
  52. }
  53. // Check for CodeFiles transfer
  54. const rawCodeFiles = e.dataTransfer.getData(CodeDataTransfers.FILES);
  55. if (rawCodeFiles) {
  56. try {
  57. const codeFiles = JSON.parse(rawCodeFiles);
  58. for (const codeFile of codeFiles) {
  59. editors.push({ resource: URI.file(codeFile), isExternal: true, allowWorkspaceOpen: true });
  60. }
  61. }
  62. catch (error) {
  63. // Invalid transfer
  64. }
  65. }
  66. // Workbench contributions
  67. const contributions = Registry.as(Extensions.DragAndDropContribution).getAll();
  68. for (const contribution of contributions) {
  69. const data = e.dataTransfer.getData(contribution.dataFormatKey);
  70. if (data) {
  71. try {
  72. editors.push(...contribution.getEditorInputs(data));
  73. }
  74. catch (error) {
  75. // Invalid transfer
  76. }
  77. }
  78. }
  79. }
  80. return editors;
  81. }
  82. export function createDraggedEditorInputFromRawResourcesData(rawResourcesData) {
  83. const editors = [];
  84. if (rawResourcesData) {
  85. const resourcesRaw = JSON.parse(rawResourcesData);
  86. for (const resourceRaw of resourcesRaw) {
  87. if (resourceRaw.indexOf(':') > 0) { // mitigate https://github.com/microsoft/vscode/issues/124946
  88. const { selection, uri } = extractSelection(URI.parse(resourceRaw));
  89. editors.push({ resource: uri, options: { selection } });
  90. }
  91. }
  92. }
  93. return editors;
  94. }
  95. class DragAndDropContributionRegistry {
  96. constructor() {
  97. this._contributions = new Map();
  98. }
  99. getAll() {
  100. return this._contributions.values();
  101. }
  102. }
  103. export const Extensions = {
  104. DragAndDropContribution: 'workbench.contributions.dragAndDrop'
  105. };
  106. Registry.add(Extensions.DragAndDropContribution, new DragAndDropContributionRegistry());
  107. //#endregion