Skip to content
74 changes: 43 additions & 31 deletions spec/pane-element-spec.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
const PaneContainer = require('../src/pane-container');

describe('PaneElement', function() {
let [paneElement, container, containerElement, pane] = [];

beforeEach(function() {
spyOn(atom.applicationDelegate, 'open');

container = new PaneContainer({
location: 'center',
config: atom.config,
confirm: atom.confirm.bind(atom),
viewRegistry: atom.views,
applicationDelegate: atom.applicationDelegate
});
containerElement = container.getElement();
container = atom.workspace.getActivePaneContainer();
containerElement = container.paneContainer.getElement();
pane = container.getActivePane();
paneElement = pane.getElement();
});
Expand Down Expand Up @@ -277,9 +269,9 @@ describe('PaneElement', function() {
}));

describe('drag and drop', function() {
const buildDragEvent = function(type, files) {
const buildDragEvent = function(type, items) {
const dataTransfer = {
files,
items,
data: {},
setData(key, value) {
this.data[key] = value;
Expand All @@ -294,26 +286,46 @@ describe('PaneElement', function() {
return event;
};

describe('when a file is dragged to the pane', () =>
it('opens it', function() {
const event = buildDragEvent('drop', [
{ path: '/fake1' },
{ path: '/fake2' }
]);
paneElement.dispatchEvent(event);
expect(atom.applicationDelegate.open.callCount).toBe(1);
expect(atom.applicationDelegate.open.argsForCall[0][0]).toEqual({
pathsToOpen: ['/fake1', '/fake2'],
here: true
});
}));
describe('when the pane is in the center workspace', () => {
describe('when a file is dragged to the pane', () =>
it('opens it', function() {
const event = buildDragEvent('drop', [
{ kind: 'file', getAsFile: () => ({ path: '/fake1' }) },
{ kind: 'file', getAsFile: () => ({ path: '/fake2' }) }
]);
paneElement.dispatchEvent(event);
expect(atom.applicationDelegate.open.callCount).toBe(1);
expect(atom.applicationDelegate.open.argsForCall[0][0]).toEqual({
pathsToOpen: ['/fake1', '/fake2'],
here: true
});
}));

describe('when a non-file is dragged to the pane', () =>
it('does nothing', function() {
const event = buildDragEvent('drop', []);
paneElement.dispatchEvent(event);
expect(atom.applicationDelegate.open).not.toHaveBeenCalled();
}));
});

describe('when a non-file is dragged to the pane', () =>
it('does nothing', function() {
const event = buildDragEvent('drop', []);
paneElement.dispatchEvent(event);
expect(atom.applicationDelegate.open).not.toHaveBeenCalled();
}));
describe('when the pane is not in the center workspace', () => {
beforeEach(() => {
pane = atom.workspace.getLeftDock().getActivePane();
paneElement = pane.getElement();
});

describe('when a drag event occurs', () => {
it('does nothing', () => {
const event = buildDragEvent('drop', [
{ kind: 'file', getAsFile: () => ({ path: '/fake1' }) },
{ kind: 'file', getAsFile: () => ({ path: '/fake2' }) }
]);
paneElement.dispatchEvent(event);
expect(atom.applicationDelegate.open).not.toHaveBeenCalled();
});
});
});
});

describe('resize', () =>
Expand Down
38 changes: 31 additions & 7 deletions src/pane-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,39 @@ class PaneElement extends HTMLElement {
}
};
const handleDragOver = event => {
event.preventDefault();
event.stopPropagation();
const items = Array.from(event.dataTransfer.items).filter(
item => item.kind === 'file'
);
// TextEditors are only allowed in the center workspace, so make sure this pane is in the center
if (
items.length > 0 &&
atom.workspace
.getCenter()
.getPanes()
.includes(this.getModel())
) {
event.preventDefault();
event.stopPropagation();
}
};
const handleDrop = event => {
event.preventDefault();
event.stopPropagation();
this.getModel().activate();
const pathsToOpen = [...event.dataTransfer.files].map(file => file.path);
if (pathsToOpen.length > 0) {
const items = Array.from(event.dataTransfer.items).filter(
item => item.kind === 'file'
);
// TextEditors are only allowed in the center workspace, so make sure this pane is in the center
if (
items.length > 0 &&
atom.workspace
.getCenter()
.getPanes()
.includes(this.getModel())
) {
event.preventDefault();
event.stopPropagation();
this.getModel().activate();

const files = items.map(item => item.getAsFile());
const pathsToOpen = files.map(file => file.path);
this.applicationDelegate.open({ pathsToOpen, here: true });
}
};
Expand Down
17 changes: 0 additions & 17 deletions src/window-event-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ module.exports = class WindowEventHandler {
'keydown',
this.handleDocumentKeyEvent
);
this.addEventListener(this.document, 'drop', this.handleDocumentDrop);
this.addEventListener(
this.document,
'dragover',
this.handleDocumentDragover
);
this.addEventListener(
this.document,
'contextmenu',
Expand Down Expand Up @@ -154,17 +148,6 @@ module.exports = class WindowEventHandler {
event.stopImmediatePropagation();
}

handleDrop(event) {
event.preventDefault();
event.stopPropagation();
}

handleDragover(event) {
event.preventDefault();
event.stopPropagation();
event.dataTransfer.dropEffect = 'none';
}

eachTabIndexedElement(callback) {
for (let element of this.document.querySelectorAll('[tabindex]')) {
if (element.disabled) {
Expand Down