Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dragging files in browser #14756

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions packages/core/src/browser/shell/application-shell.ts
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ import {
} from '@phosphor/widgets';
import { Message } from '@phosphor/messaging';
import { IDragEvent } from '@phosphor/dragdrop';
import { RecursivePartial, Event as CommonEvent, DisposableCollection, Disposable, environment, isObject } from '../../common';
import { RecursivePartial, Event as CommonEvent, DisposableCollection, Disposable, environment, isObject, UntitledResourceResolver, UNTITLED_SCHEME } from '../../common';
import { animationFrame } from '../browser';
import { Saveable, SaveableWidget, SaveOptions } from '../saveable';
import { StatusBarImpl, StatusBarEntry, StatusBarAlignment } from '../status-bar/status-bar';
@@ -232,6 +232,9 @@ export class ApplicationShell extends Widget {
@inject(OpenerService)
protected readonly openerService: OpenerService;

@inject(UntitledResourceResolver)
protected readonly untitledResourceResolver: UntitledResourceResolver;

protected readonly onDidAddWidgetEmitter = new Emitter<Widget>();
readonly onDidAddWidget = this.onDidAddWidgetEmitter.event;
protected fireDidAddWidget(widget: Widget): void {
@@ -572,10 +575,28 @@ export class ApplicationShell extends Widget {
uris.forEach(openUri);
} else if (event.dataTransfer.files?.length > 0) {
// the files were dragged from the outside the workspace
Array.from(event.dataTransfer.files).forEach(file => {
if (file.path) {
const fileUri = URI.fromFilePath(file.path);
openUri(fileUri);
Array.from(event.dataTransfer.files).forEach(async file => {
if (environment.electron.is()) {
if (file.path) {
const fileUri = URI.fromFilePath(file.path);
openUri(fileUri);
}
} else {
const fileContent = await file.text();
const fileName = file.name;
const uri = new URI(`${UNTITLED_SCHEME}:/${fileName}`);
// Only create a new untitled resource if it doesn't already exist.
// VS Code does the same thing, and there's not really a better solution,
// since we want to keep the original name of the file,
// but also to prevent duplicates of the same file.
if (!this.untitledResourceResolver.has(uri)) {
const untitledResource = await this.untitledResourceResolver.createUntitledResource(
fileContent,
undefined,
new URI(`${UNTITLED_SCHEME}:/${fileName}`)
);
openUri(untitledResource.uri);
}
}
});
}
Loading