Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #63 from ckeditor/t/ckeditor5-upload/92
Browse files Browse the repository at this point in the history
Fix: The DOM `drop` event will not bubble up if the `clipboardInput` event was handled. Closes ckeditor/ckeditor5-upload#92.
  • Loading branch information
Reinmar authored Mar 28, 2019
2 parents 258f198 + 1e96b4e commit 5d14697
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/clipboardobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import DomEventObserver from '@ckeditor/ckeditor5-engine/src/view/observer/domeventobserver';
import EventInfo from '@ckeditor/ckeditor5-utils/src/eventinfo';
import DataTransfer from './datatransfer';

/**
Expand Down Expand Up @@ -45,10 +46,19 @@ export default class ClipboardObserver extends DomEventObserver {

const targetRanges = data.dropRange ? [ data.dropRange ] : Array.from( viewDocument.selection.getRanges() );

viewDocument.fire( 'clipboardInput', {
const eventInfo = new EventInfo( viewDocument, 'clipboardInput' );

viewDocument.fire( eventInfo, {
dataTransfer: data.dataTransfer,
targetRanges
} );

// If CKEditor handled the input, do not bubble the original event any further.
// This helps external integrations recognize that fact and act accordingly.
// https://github.com/ckeditor/ckeditor5-upload/issues/92
if ( eventInfo.stop.called ) {
data.stopPropagation();
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ describe( 'Clipboard feature', () => {

viewDocument.fire( 'paste', {
dataTransfer: dataTransferMock,
stopPropagation() {},
preventDefault() {}
} );

Expand Down Expand Up @@ -286,6 +287,7 @@ describe( 'Clipboard feature', () => {

viewDocument.fire( 'paste', {
dataTransfer: dataTransferMock,
stopPropagation() {},
preventDefault() {}
} );

Expand Down
31 changes: 30 additions & 1 deletion tests/clipboardobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import DataTransfer from '../src/datatransfer';
import createViewRoot from '@ckeditor/ckeditor5-engine/tests/view/_utils/createroot';

describe( 'ClipboardObserver', () => {
let view, doc, writer, observer, root, el, range, eventSpy, preventDefaultSpy;
let view, doc, writer, observer, root, el, range, eventSpy, preventDefaultSpy, stopPropagationSpy;

beforeEach( () => {
view = new View();
Expand All @@ -34,6 +34,7 @@ describe( 'ClipboardObserver', () => {

eventSpy = sinon.spy();
preventDefaultSpy = sinon.spy();
stopPropagationSpy = sinon.spy();
} );

it( 'should define domEventType', () => {
Expand Down Expand Up @@ -224,6 +225,34 @@ describe( 'ClipboardObserver', () => {

expect( sinon.assert.callOrder( normalPrioritySpy, eventSpy ) );
} );

// https://github.com/ckeditor/ckeditor5-upload/issues/92
it( 'should stop propagation of the original event if handled by the editor', () => {
const dataTransfer = new DataTransfer( mockDomDataTransfer() );

doc.fire( 'drop', {
dataTransfer,
preventDefault: preventDefaultSpy,
stopPropagation: stopPropagationSpy,
dropRange: range
} );

sinon.assert.notCalled( stopPropagationSpy );

doc.on( 'clipboardInput', evt => {
// E.g. some feature handled the input.
evt.stop();
} );

doc.fire( 'drop', {
dataTransfer,
preventDefault: preventDefaultSpy,
stopPropagation: stopPropagationSpy,
dropRange: range
} );

sinon.assert.calledOnce( stopPropagationSpy );
} );
} );

describe( 'dragover event', () => {
Expand Down

0 comments on commit 5d14697

Please sign in to comment.