Skip to content

Set DragData when directory is dragged - #243656

Merged
Matt Bierner (mjbvz) merged 11 commits into
microsoft:mainfrom
mkhuzaima:md-directory-link
Mar 28, 2025
Merged

Set DragData when directory is dragged#243656
Matt Bierner (mjbvz) merged 11 commits into
microsoft:mainfrom
mkhuzaima:md-directory-link

Conversation

@mkhuzaima

Copy link
Copy Markdown
Contributor

Fixes #241007

Bug

drag-directory-issue.webm

After Fix

drag-directory-fix.webm

Previously, it was not set for directory
@TedVu

Copy link
Copy Markdown

Couldn't reproduce on the latest version.

@mkhuzaima

Copy link
Copy Markdown
Contributor Author

Couldn't reproduce on the latest version.

Are you using ubuntu ?

@TedVu

Copy link
Copy Markdown

I'm using Windows 11, what's the directory structure ? I created a folder and dragged it to an md file, it appeared fine.

@mkhuzaima

Copy link
Copy Markdown
Contributor Author

Ted Vu (@TedVu) The issue occurs on Ubuntu but works fine on Windows. When dragging a directory into a Markdown file, it inserts the absolute path instead of the relative path and does not generate a link.

Comment thread src/vs/workbench/browser/dnd.ts Outdated
const { selection, uri } = extractSelection(resourceOrEditor);
editor = { resource: uri, options: selection ? { selection } : undefined };
} else if (!resourceOrEditor.isDirectory) {
} else {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of the code below assumes that resource will be a file, not a directory

Instead I think this check should happen down where we set the Mimes.uriList data. That logic should be updated to also include directories in the url-list

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will update accordingly.

Can you describe some scenarios, through which I can test the change?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should work the same as with the PR but using a more correct data-transfer type instead of the editors type. That could potentially result in VS Code trying to open a folder as an editor, while url-list should work for any type of file or folder

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. 👍
Thank you for your prompt reply.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed the code in detail and found that the flow is identical for both files and directories. To ensure that the if (draggedEditors.length) condition is met (which is required for setting the MIME type), we need to add the editor to the draggedEditors list.

Additionally, textFileModel is undefined for both files and directories:

const textFileModel = textFileService.files.get(resource);
if (textFileModel) {

The most reasonable approach seems to be adding a separate condition for directories, like this:

} else if (!resourceOrEditor.isDirectory) {
	editor = {
		resource: resourceOrEditor.resource,
		options: { selection: resourceOrEditor.selection }
	};
} else if (resourceOrEditor.isDirectory) {
	editor = {
		resource: resourceOrEditor.resource,
		options: { selection: undefined } // or resourceOrEditor.selection
	};
}

Since this is my first contribution to this repo, I’d appreciate any guidance on whether this approach aligns with the expected changes or if there are additional considerations I should address.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be easier to add a new draggedDirectories variable to go with with draggedEditors. A directory should never end up in draggedEditors

Then update the code where we build the uriListEntries to loop through both the draggedEditors and draggedDirectories

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will update the code soon.

auto-merge was automatically disabled March 20, 2025 22:24

Head branch was pushed to by a user without write access


// Text: allows to paste into text-capable areas
const lineDelimiter = isWindows ? '\r\n' : '\n';
event.dataTransfer.setData(DataTransfers.TEXT, fileSystemResources.map(({ resource }) => labelService.getUriLabel(resource, { noPrefix: true })).join(lineDelimiter));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to write both files and dirs in this case

It looks like maybe a few too many lines got touch in this change. Maybe instead of adding draggedDirectories and draggedFiles, you can make the fix clearer by focusing on the part below where we built the url-list. You can use use fileSystemResources to get the directories and combine those with the dragged editors. Something like:

if (draggedEditors.length) {
   event.dataTransfer.setData(CodeDataTransfers.EDITORS, stringify(draggedEditors));
}

const allDraggedResources = [
    ...draggedEditors.map(editor => resource),
    ...fileSystemResources.filter(({ isDirectory }) => isDirectory).map(dir => dir.resource)
]
if (allDraggedResources.length) {
    // set url-list
}

@mkhuzaima
Muhammad Khuzaima Umair (mkhuzaima) marked this pull request as draft March 21, 2025 00:27

@mjbvz Matt Bierner (mjbvz) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor note but otherwise looks good. Thanks for following up!

Comment thread src/vs/workbench/browser/dnd.ts Outdated

const draggedDirectories: URI[] = [
...fileSystemResources.filter(({ isDirectory }) => isDirectory).map(({ resource }) => resource),
];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since filter creates a copy of the array, you can simplify this to: const draggedDirectories = fileSystemResources .filter(...) without using spread

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Comment thread src/vs/workbench/browser/dnd.ts Outdated
const draggedDirectories: URI[] = fileSystemResources.filter(({ isDirectory }) => isDirectory).map(({ resource }) => resource);

if (draggedEditors.length || draggedDirectories.length) {
event.dataTransfer.setData(CodeDataTransfers.EDITORS, stringify([...draggedEditors, ...draggedDirectories]));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't set directories here, it does not solve the bug.

@mkhuzaima
Muhammad Khuzaima Umair (mkhuzaima) marked this pull request as ready for review March 21, 2025 11:40
@mkhuzaima

Copy link
Copy Markdown
Contributor Author

Matt Bierner (@mjbvz) I have updated the PR based on the requested changes. Please review it when you have time.
I appreciate your prompt feedback and guidance.

@mjbvz

Copy link
Copy Markdown
Contributor

Muhammad Khuzaima Umair (@mkhuzaima) I think there was a bug with how resources was set. Please test out the change I fixed and let me know if it works for you too

@mjbvz
Matt Bierner (mjbvz) merged commit cc0b0c1 into microsoft:main Mar 28, 2025
@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators May 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown support: issue with dragging folder into editor to create a link on Linux edition of VSCode

4 participants