Skip to content

Commit 0b65d03

Browse files
committed
dnd - support dirty editor drop that is empty
1 parent f3d4526 commit 0b65d03

File tree

1 file changed

+23
-28
lines changed
  • src/vs/workbench/browser

1 file changed

+23
-28
lines changed

src/vs/workbench/browser/dnd.ts

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,30 @@ export interface IDraggedResource {
3636
isExternal: boolean;
3737
}
3838

39-
export class DraggedEditorIdentifier {
39+
interface ISerializedDraggedResource {
40+
resource: string;
41+
}
4042

41-
constructor(private _identifier: IEditorIdentifier) { }
43+
export class DraggedEditorIdentifier {
4244

43-
get identifier(): IEditorIdentifier {
44-
return this._identifier;
45-
}
45+
constructor(public readonly identifier: IEditorIdentifier) { }
4646
}
4747

4848
export class DraggedEditorGroupIdentifier {
4949

5050
constructor(public readonly identifier: GroupIdentifier) { }
5151
}
5252

53-
export interface IDraggedEditor extends IDraggedResource {
54-
content?: string;
53+
interface IDraggedEditorProps {
54+
dirtyContent?: string;
5555
encoding?: string;
5656
mode?: string;
5757
options?: ITextEditorOptions;
5858
}
5959

60-
export interface ISerializedDraggedEditor {
61-
resource: string;
62-
content?: string;
63-
encoding?: string;
64-
mode?: string;
65-
options?: ITextEditorOptions;
66-
}
60+
export interface IDraggedEditor extends IDraggedResource, IDraggedEditorProps { }
61+
62+
export interface ISerializedDraggedEditor extends ISerializedDraggedResource, IDraggedEditorProps { }
6763

6864
export const CodeDataTransfers = {
6965
EDITORS: 'CodeEditors',
@@ -85,7 +81,7 @@ export function extractResources(e: DragEvent, externalOnly?: boolean): Array<ID
8581
draggedEditors.forEach(draggedEditor => {
8682
resources.push({
8783
resource: URI.parse(draggedEditor.resource),
88-
content: draggedEditor.content,
84+
dirtyContent: draggedEditor.dirtyContent,
8985
options: draggedEditor.options,
9086
encoding: draggedEditor.encoding,
9187
mode: draggedEditor.mode,
@@ -182,13 +178,12 @@ export class ResourcesDropHandler {
182178

183179
// Check for special things being dropped
184180
const isWorkspaceOpening = await this.doHandleDrop(untitledOrFileResources);
185-
186181
if (isWorkspaceOpening) {
187182
return; // return early if the drop operation resulted in this window changing to a workspace
188183
}
189184

190185
// Add external ones to recently open list unless dropped resource is a workspace
191-
const recentFiles: IRecentFile[] = untitledOrFileResources.filter(d => d.isExternal && d.resource.scheme === Schemas.file).map(d => ({ fileUri: d.resource }));
186+
const recentFiles: IRecentFile[] = untitledOrFileResources.filter(untitledOrFileResource => untitledOrFileResource.isExternal && untitledOrFileResource.resource.scheme === Schemas.file).map(d => ({ fileUri: d.resource }));
192187
if (recentFiles.length) {
193188
this.workspacesService.addRecentlyOpened(recentFiles);
194189
}
@@ -215,15 +210,15 @@ export class ResourcesDropHandler {
215210
private async doHandleDrop(untitledOrFileResources: Array<IDraggedResource | IDraggedEditor>): Promise<boolean> {
216211

217212
// Check for dirty editors being dropped
218-
const resourcesWithContent: IDraggedEditor[] = untitledOrFileResources.filter(resource => !resource.isExternal && !!(resource as IDraggedEditor).content);
219-
if (resourcesWithContent.length > 0) {
220-
await Promise.all(resourcesWithContent.map(resourceWithContent => this.handleDirtyEditorDrop(resourceWithContent)));
213+
const dirtyEditors: IDraggedEditor[] = untitledOrFileResources.filter(untitledOrFileResource => !untitledOrFileResource.isExternal && typeof (untitledOrFileResource as IDraggedEditor).dirtyContent === 'string');
214+
if (dirtyEditors.length > 0) {
215+
await Promise.all(dirtyEditors.map(dirtyEditor => this.handleDirtyEditorDrop(dirtyEditor)));
221216
return false;
222217
}
223218

224219
// Check for workspace file being dropped if we are allowed to do so
225220
if (this.options.allowWorkspaceOpen) {
226-
const externalFileOnDiskResources = untitledOrFileResources.filter(d => d.isExternal && d.resource.scheme === Schemas.file).map(d => d.resource);
221+
const externalFileOnDiskResources = untitledOrFileResources.filter(untitledOrFileResource => untitledOrFileResource.isExternal && untitledOrFileResource.resource.scheme === Schemas.file).map(d => d.resource);
227222
if (externalFileOnDiskResources.length > 0) {
228223
return this.handleWorkspaceFileDrop(externalFileOnDiskResources);
229224
}
@@ -249,9 +244,9 @@ export class ResourcesDropHandler {
249244

250245
// If the dropped editor is dirty with content we simply take that
251246
// content and turn it into a backup so that it loads the contents
252-
if (droppedDirtyEditor.content) {
247+
if (typeof droppedDirtyEditor.dirtyContent === 'string') {
253248
try {
254-
await this.backupFileService.backup(droppedDirtyEditor.resource, stringToSnapshot(droppedDirtyEditor.content));
249+
await this.backupFileService.backup(droppedDirtyEditor.resource, stringToSnapshot(droppedDirtyEditor.dirtyContent));
255250
} catch (e) {
256251
// Ignore error
257252
}
@@ -331,9 +326,9 @@ export function fillResourceDataTransfers(accessor: ServicesAccessor, resources:
331326
}
332327

333328
// Resource URLs: allows to drop multiple resources to a target in VS Code (not directories)
334-
const files = sources.filter(s => !s.isDirectory);
329+
const files = sources.filter(source => !source.isDirectory);
335330
if (files.length) {
336-
event.dataTransfer.setData(DataTransfers.RESOURCES, JSON.stringify(files.map(f => f.resource.toString())));
331+
event.dataTransfer.setData(DataTransfers.RESOURCES, JSON.stringify(files.map(file => file.resource.toString())));
337332
}
338333

339334
// Editors: enables cross window DND of tabs into the editor area
@@ -380,13 +375,13 @@ export function fillResourceDataTransfers(accessor: ServicesAccessor, resources:
380375

381376
// If the resource is dirty or untitled, send over its content
382377
// to restore dirty state. Get that from the text model directly
383-
let content: string | undefined = undefined;
378+
let dirtyContent: string | undefined = undefined;
384379
if (model?.isDirty()) {
385-
content = model.textEditorModel.getValue();
380+
dirtyContent = model.textEditorModel.getValue();
386381
}
387382

388383
// Add as dragged editor
389-
draggedEditors.push({ resource: file.resource.toString(), content, options, encoding, mode });
384+
draggedEditors.push({ resource: file.resource.toString(), dirtyContent, options, encoding, mode });
390385
});
391386

392387
if (draggedEditors.length) {

0 commit comments

Comments
 (0)