| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- /*---------------------------------------------------------------------------------------------
- * Copyright (c) Microsoft Corporation. All rights reserved.
- * Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
- import { DataTransfers } from '../../../base/browser/dnd.js';
- import { parse } from '../../../base/common/marshalling.js';
- import { URI } from '../../../base/common/uri.js';
- import { extractSelection } from '../../opener/common/opener.js';
- import { Registry } from '../../registry/common/platform.js';
- //#region Editor / Resources DND
- export const CodeDataTransfers = {
- EDITORS: 'CodeEditors',
- FILES: 'CodeFiles'
- };
- export function extractEditorsDropData(e) {
- var _a;
- const editors = [];
- if (e.dataTransfer && e.dataTransfer.types.length > 0) {
- // Data Transfer: Code Editors
- const rawEditorsData = e.dataTransfer.getData(CodeDataTransfers.EDITORS);
- if (rawEditorsData) {
- try {
- editors.push(...parse(rawEditorsData));
- }
- catch (error) {
- // Invalid transfer
- }
- }
- // Data Transfer: Resources
- else {
- try {
- const rawResourcesData = e.dataTransfer.getData(DataTransfers.RESOURCES);
- editors.push(...createDraggedEditorInputFromRawResourcesData(rawResourcesData));
- }
- catch (error) {
- // Invalid transfer
- }
- }
- // Check for native file transfer
- if ((_a = e.dataTransfer) === null || _a === void 0 ? void 0 : _a.files) {
- for (let i = 0; i < e.dataTransfer.files.length; i++) {
- const file = e.dataTransfer.files[i];
- if (file && file.path /* Electron only */) {
- try {
- editors.push({ resource: URI.file(file.path), isExternal: true, allowWorkspaceOpen: true });
- }
- catch (error) {
- // Invalid URI
- }
- }
- }
- }
- // Check for CodeFiles transfer
- const rawCodeFiles = e.dataTransfer.getData(CodeDataTransfers.FILES);
- if (rawCodeFiles) {
- try {
- const codeFiles = JSON.parse(rawCodeFiles);
- for (const codeFile of codeFiles) {
- editors.push({ resource: URI.file(codeFile), isExternal: true, allowWorkspaceOpen: true });
- }
- }
- catch (error) {
- // Invalid transfer
- }
- }
- // Workbench contributions
- const contributions = Registry.as(Extensions.DragAndDropContribution).getAll();
- for (const contribution of contributions) {
- const data = e.dataTransfer.getData(contribution.dataFormatKey);
- if (data) {
- try {
- editors.push(...contribution.getEditorInputs(data));
- }
- catch (error) {
- // Invalid transfer
- }
- }
- }
- }
- return editors;
- }
- export function createDraggedEditorInputFromRawResourcesData(rawResourcesData) {
- const editors = [];
- if (rawResourcesData) {
- const resourcesRaw = JSON.parse(rawResourcesData);
- for (const resourceRaw of resourcesRaw) {
- if (resourceRaw.indexOf(':') > 0) { // mitigate https://github.com/microsoft/vscode/issues/124946
- const { selection, uri } = extractSelection(URI.parse(resourceRaw));
- editors.push({ resource: uri, options: { selection } });
- }
- }
- }
- return editors;
- }
- class DragAndDropContributionRegistry {
- constructor() {
- this._contributions = new Map();
- }
- getAll() {
- return this._contributions.values();
- }
- }
- export const Extensions = {
- DragAndDropContribution: 'workbench.contributions.dragAndDrop'
- };
- Registry.add(Extensions.DragAndDropContribution, new DragAndDropContributionRegistry());
- //#endregion
|