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

feat(): drop:before event #7442

Merged
merged 4 commits into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion src/mixins/canvas_events.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
this._onDragOver = this._onDragOver.bind(this);
this._onDragEnter = this._simpleEventHandler.bind(this, 'dragenter');
this._onDragLeave = this._simpleEventHandler.bind(this, 'dragleave');
this._onDrop = this._simpleEventHandler.bind(this, 'drop');
this._onDrop = this._onDrop.bind(this);
this.eventsBound = true;
},

Expand Down Expand Up @@ -218,6 +218,18 @@
this._fireEnterLeaveEvents(target, e);
},

/**
* `drop:before` is a an event that allow you to schedule logic
* before the `drop` event. Prefer `drop` event always, but if you need
* to run some drop-disabling logic on an event, since there is no way
* to handle event handlers ordering, use `drop:before`
* @param {Event} e
*/
_onDrop: function (e) {
this._simpleEventHandler('drop:before', e);
return this._simpleEventHandler('drop', e);
},

/**
* @private
* @param {Event} e Event object fired on mousedown
Expand Down
19 changes: 17 additions & 2 deletions test/unit/canvas_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@
});
});

['DragEnter', 'DragLeave', 'DragOver', 'Drop'].forEach(function(eventType) {
['DragEnter', 'DragLeave', 'DragOver'].forEach(function(eventType) {
QUnit.test('Fabric event fired - ' + eventType, function(assert) {
var eventName = eventType.toLowerCase();
var counter = 0;
Expand All @@ -474,8 +474,23 @@
});
});

QUnit.test('Fabric event fired - Drop', function (assert) {
var eventNames = ['drop:before', 'drop'];
var c = new fabric.Canvas();
var fired = [];
eventNames.forEach(function (eventName) {
c.on(eventName, function () {
fired.push(eventName);
});
});
var event = fabric.document.createEvent('HTMLEvents');
event.initEvent('drop', true, true);
c.upperCanvasEl.dispatchEvent(event);
assert.deepEqual(fired, eventNames, 'bad drop event fired');
});

['DragEnter', 'DragLeave', 'DragOver', 'Drop'].forEach(function(eventType) {
QUnit.test('_simpleEventHandler fires on object and canvas' + eventType, function(assert) {
QUnit.test('_simpleEventHandler fires on object and canvas - ' + eventType, function(assert) {
var eventName = eventType.toLowerCase();
var counter = 0;
var target;
Expand Down