Skip to content

Commit 60d2192

Browse files
author
Benjamin Pasero
authored
editors - have a editor drop service (fix microsoft#99739) (microsoft#100464)
1 parent fc4b55e commit 60d2192

5 files changed

Lines changed: 60 additions & 36 deletions

File tree

src/vs/workbench/browser/parts/editor/editorDropTarget.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,12 @@ class DropOverlay extends Themable {
530530
}
531531
}
532532

533-
export interface EditorDropTargetDelegate {
534-
groupContainsPredicate?(groupView: IEditorGroupView): boolean;
533+
export interface IEditorDropTargetDelegate {
534+
535+
/**
536+
* A helper to figure out if the drop target contains the provided group.
537+
*/
538+
containsGroup?(groupView: IEditorGroupView): boolean;
535539
}
536540

537541
export class EditorDropTarget extends Themable {
@@ -546,7 +550,7 @@ export class EditorDropTarget extends Themable {
546550
constructor(
547551
private accessor: IEditorGroupsAccessor,
548552
private container: HTMLElement,
549-
private readonly delegate: EditorDropTargetDelegate,
553+
private readonly delegate: IEditorDropTargetDelegate,
550554
@IThemeService themeService: IThemeService,
551555
@IInstantiationService private readonly instantiationService: IInstantiationService
552556
) {
@@ -620,7 +624,8 @@ export class EditorDropTarget extends Themable {
620624

621625
private findTargetGroupView(child: HTMLElement): IEditorGroupView | undefined {
622626
const groups = this.accessor.groups;
623-
return groups.find(groupView => isAncestor(child, groupView.element) || this.delegate.groupContainsPredicate?.(groupView));
627+
628+
return groups.find(groupView => isAncestor(child, groupView.element) || this.delegate.containsGroup?.(groupView));
624629
}
625630

626631
private updateContainer(isDraggedOver: boolean): void {

src/vs/workbench/browser/parts/editor/editorPart.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import { IConfigurationService, IConfigurationChangeEvent } from 'vs/platform/co
2121
import { IDisposable, dispose, toDisposable, DisposableStore } from 'vs/base/common/lifecycle';
2222
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
2323
import { ISerializedEditorGroup, isSerializedEditorGroup } from 'vs/workbench/common/editor/editorGroup';
24-
import { EditorDropTarget, EditorDropTargetDelegate } from 'vs/workbench/browser/parts/editor/editorDropTarget';
24+
import { EditorDropTarget, IEditorDropTargetDelegate } from 'vs/workbench/browser/parts/editor/editorDropTarget';
25+
import { IEditorDropService } from 'vs/workbench/services/editor/browser/editorDropService';
2526
import { Color } from 'vs/base/common/color';
2627
import { CenteredViewLayout } from 'vs/base/browser/ui/centered/centeredViewLayout';
2728
import { onUnexpectedError } from 'vs/base/common/errors';
@@ -780,6 +781,14 @@ export class EditorPart extends Part implements IEditorGroupsService, IEditorGro
780781

781782
//#endregion
782783

784+
//#region IEditorDropService
785+
786+
createEditorDropTarget(container: HTMLElement, delegate: IEditorDropTargetDelegate): IDisposable {
787+
return this.instantiationService.createInstance(EditorDropTarget, this, container, delegate);
788+
}
789+
790+
//#endregion
791+
783792
//#region Part
784793

785794
// TODO @sbatten @joao find something better to prevent editor taking over #79897
@@ -820,7 +829,7 @@ export class EditorPart extends Part implements IEditorGroupsService, IEditorGro
820829
this.centeredLayoutWidget = this._register(new CenteredViewLayout(this.container, this.gridWidgetView, this.globalMemento[EditorPart.EDITOR_PART_CENTERED_VIEW_STORAGE_KEY]));
821830

822831
// Drop support
823-
this._register(this.createEditorDropTarget(this.container, {}));
832+
this._register(this.createEditorDropTarget(this.container, Object.create(null)));
824833

825834
// No drop in the editor
826835
const overlay = document.createElement('div');
@@ -1107,6 +1116,12 @@ export class EditorPart extends Part implements IEditorGroupsService, IEditorGro
11071116
super.saveState();
11081117
}
11091118

1119+
toJSON(): object {
1120+
return {
1121+
type: Parts.EDITOR_PART
1122+
};
1123+
}
1124+
11101125
dispose(): void {
11111126

11121127
// Forward to all groups
@@ -1122,20 +1137,7 @@ export class EditorPart extends Part implements IEditorGroupsService, IEditorGro
11221137
}
11231138

11241139
//#endregion
1125-
1126-
toJSON(): object {
1127-
return {
1128-
type: Parts.EDITOR_PART
1129-
};
1130-
}
1131-
1132-
//#region TODO@matt this should move into some kind of service
1133-
1134-
createEditorDropTarget(container: HTMLElement, delegate: EditorDropTargetDelegate): IDisposable {
1135-
return this.instantiationService.createInstance(EditorDropTarget, this, container, delegate);
1136-
}
1137-
1138-
//#endregion
11391140
}
11401141

11411142
registerSingleton(IEditorGroupsService, EditorPart);
1143+
registerSingleton(IEditorDropService, EditorPart);

src/vs/workbench/contrib/notebook/browser/notebookEditor.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { NotebookEditorInput } from 'vs/workbench/contrib/notebook/browser/noteb
1717
import { INotebookEditorViewState, NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
1818
import { IEditorGroup, IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
1919
import { NotebookEditorWidget, NotebookEditorOptions } from 'vs/workbench/contrib/notebook/browser/notebookEditorWidget';
20-
import { EditorPart } from 'vs/workbench/browser/parts/editor/editorPart';
20+
import { IEditorDropService } from 'vs/workbench/services/editor/browser/editorDropService';
2121
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
2222
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
2323
import { IEditorOptions, ITextEditorOptions } from 'vs/platform/editor/common/editor';
@@ -49,12 +49,13 @@ export class NotebookEditor extends BaseEditor {
4949
@IInstantiationService private readonly instantiationService: IInstantiationService,
5050
@IStorageService storageService: IStorageService,
5151
@IEditorService private readonly _editorService: IEditorService,
52-
@IEditorGroupsService private readonly _editorGroupService: IEditorGroupsService,
52+
@IEditorGroupsService editorGroupService: IEditorGroupsService,
53+
@IEditorDropService private readonly _editorDropService: IEditorDropService,
5354
@INotificationService private readonly _notificationService: INotificationService,
5455
@INotebookEditorWidgetService private readonly _notebookWidgetService: INotebookEditorWidgetService,
5556
) {
5657
super(NotebookEditor.ID, telemetryService, themeService, storageService);
57-
this._editorMemento = this.getEditorMemento<INotebookEditorViewState>(_editorGroupService, NOTEBOOK_EDITOR_VIEW_STATE_PREFERENCE_KEY);
58+
this._editorMemento = this.getEditorMemento<INotebookEditorViewState>(editorGroupService, NOTEBOOK_EDITOR_VIEW_STATE_PREFERENCE_KEY);
5859
}
5960

6061
set viewModel(newModel: NotebookViewModel | undefined) {
@@ -160,11 +161,9 @@ export class NotebookEditor extends BaseEditor {
160161
await this._widget.value!.setOptions(options instanceof NotebookEditorOptions ? options : undefined);
161162
this._widgetDisposableStore.add(this._widget.value!.onDidFocus(() => this._onDidFocusWidget.fire()));
162163

163-
if (this._editorGroupService instanceof EditorPart) {
164-
this._widgetDisposableStore.add(this._editorGroupService.createEditorDropTarget(this._widget.value!.getDomNode(), {
165-
groupContainsPredicate: (group) => this.group?.id === group.group.id
166-
}));
167-
}
164+
this._widgetDisposableStore.add(this._editorDropService.createEditorDropTarget(this._widget.value!.getDomNode(), {
165+
containsGroup: (group) => this.group?.id === group.group.id
166+
}));
168167
}
169168

170169
clearInput(): void {

src/vs/workbench/contrib/webview/browser/webviewEditor.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import { IStorageService } from 'vs/platform/storage/common/storage';
1212
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
1313
import { IThemeService } from 'vs/platform/theme/common/themeService';
1414
import { BaseEditor } from 'vs/workbench/browser/parts/editor/baseEditor';
15-
import { EditorPart } from 'vs/workbench/browser/parts/editor/editorPart';
15+
import { IEditorDropService } from 'vs/workbench/services/editor/browser/editorDropService';
1616
import { EditorInput, EditorOptions } from 'vs/workbench/common/editor';
1717
import { WebviewOverlay } from 'vs/workbench/contrib/webview/browser/webview';
1818
import { WebviewInput } from 'vs/workbench/contrib/webview/browser/webviewEditorInput';
19-
import { IEditorGroup, IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
19+
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
2020
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
2121
import { IHostService } from 'vs/workbench/services/host/browser/host';
2222

@@ -39,7 +39,7 @@ export class WebviewEditor extends BaseEditor {
3939
@IThemeService themeService: IThemeService,
4040
@IStorageService storageService: IStorageService,
4141
@IEditorService private readonly _editorService: IEditorService,
42-
@IEditorGroupsService private readonly _editorGroupsService: IEditorGroupsService,
42+
@IEditorDropService private readonly _editorDropService: IEditorDropService,
4343
@IHostService private readonly _hostService: IHostService,
4444
) {
4545
super(WebviewEditor.ID, telemetryService, themeService, storageService);
@@ -146,11 +146,9 @@ export class WebviewEditor extends BaseEditor {
146146
this._webviewVisibleDisposables.clear();
147147

148148
// Webviews are not part of the normal editor dom, so we have to register our own drag and drop handler on them.
149-
if (this._editorGroupsService instanceof EditorPart) {
150-
this._webviewVisibleDisposables.add(this._editorGroupsService.createEditorDropTarget(input.webview.container, {
151-
groupContainsPredicate: (group) => this.group?.id === group.group.id
152-
}));
153-
}
149+
this._webviewVisibleDisposables.add(this._editorDropService.createEditorDropTarget(input.webview.container, {
150+
containsGroup: (group) => this.group?.id === group.group.id
151+
}));
154152

155153
this._webviewVisibleDisposables.add(DOM.addDisposableListener(window, DOM.EventType.DRAG_START, () => {
156154
this.webview?.windowDidDragStart();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
6+
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
7+
import { IDisposable } from 'vs/base/common/lifecycle';
8+
import { IEditorDropTargetDelegate } from 'vs/workbench/browser/parts/editor/editorDropTarget';
9+
10+
export const IEditorDropService = createDecorator<IEditorDropService>('editorDropService');
11+
12+
export interface IEditorDropService {
13+
14+
readonly _serviceBrand: undefined;
15+
16+
/**
17+
* Allows to register a drag and drop target for editors.
18+
*/
19+
createEditorDropTarget(container: HTMLElement, delegate: IEditorDropTargetDelegate): IDisposable;
20+
}

0 commit comments

Comments
 (0)