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

Fix #1098 - editablePaste not triggering on keyboard paste #1099

Merged
merged 4 commits into from
May 27, 2016
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
66 changes: 66 additions & 0 deletions spec/paste.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,30 @@ describe('Pasting content', function () {

});

it('should trigger editablePaste', function () {
var editorEl = this.el,
editor = this.newMediumEditor('.editor', {
paste: {
forcePlainText: false,
cleanPastedHTML: true
}
}),
spy = jasmine.createSpy('handler');

editor.subscribe('editablePaste', spy);

// move caret to editor
editorEl.innerHTML = '<span id="editor-inner">&nbsp</span>';

selectElementContentsAndFire(editorEl);

expect(spy).not.toHaveBeenCalled();
var evt = prepareEvent(editorEl, 'paste');
firePreparedEvent(evt, editorEl, 'paste');
jasmine.clock().tick(1);
expect(spy).toHaveBeenCalledWith({ currentTarget: this.el, target: this.el }, this.el);
});

it('should filter multi-line rich-text pastes when "insertHTML" command is not supported', function () {
var editor = this.newMediumEditor('.editor', {
paste: {
Expand Down Expand Up @@ -287,6 +311,48 @@ describe('Pasting content', function () {
expect(pasteHandler.handlePasteBinPaste).toHaveBeenCalledWith(evt);
});

it('should fire editablePaste event when pasting', function () {
var editor = this.newMediumEditor('.editor', {
paste: {
forcePlainText: false,
cleanPastedHTML: true
}
}),
spy = jasmine.createSpy('handler');

editor.subscribe('editablePaste', spy);

selectElementContentsAndFire(editor.elements[0].firstChild);
expect(spy).not.toHaveBeenCalled();

fireEvent(this.el, 'keydown', {
keyCode: MediumEditor.util.keyCode.V,
ctrlKey: true,
metaKey: true
});

var contentEditables = document.body.querySelectorAll('[contentEditable=true]');
expect(contentEditables.length).toBe(2);

var evt = {
type: 'paste',
defaultPrevented: false,
preventDefault: function () {},
clipboardData: {
types: ['text/plain', 'text/html'],
getData: function () {
// do we need to return different results for the different types? text/plain, text/html
return 'pasted content';
}
}
},
pasteExtension = editor.getExtensionByName('paste');

pasteExtension.handlePasteBinPaste(evt);
jasmine.clock().tick(1);
expect(spy).toHaveBeenCalledWith({ currentTarget: editor.elements[0], target: editor.elements[0] }, editor.elements[0]);
});

it('should do nothing if default was prevented on paste event of the paste-bin', function () {
var editor = this.newMediumEditor('.editor', {
paste: {
Expand Down
2 changes: 1 addition & 1 deletion src/js/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@
},

handlePaste: function (event) {
this.triggerCustomEvent('editablePaste', event, event.currentTarget);
this.triggerCustomEvent('editablePaste', { currentTarget: event.currentTarget, target: event.target }, event.currentTarget);
},

handleKeydown: function (event) {
Expand Down
12 changes: 12 additions & 0 deletions src/js/extensions/paste.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@
event.preventDefault();
this.removePasteBin();
this.doPaste(pastedHTML, pastedPlain, editable);

// The event handling code listens for paste on the editable element
// in order to trigger the editablePaste event. Since this paste event
// is happening on the pastebin, the event handling code never knows about it
// So, we have to trigger editablePaste manually
this.trigger('editablePaste', { currentTarget: editable, target: editable }, editable);
return;
}

Expand All @@ -241,6 +247,12 @@

// Handle the paste with the html from the paste bin
this.doPaste(pastedHTML, pastedPlain, editable);

// The event handling code listens for paste on the editable element
// in order to trigger the editablePaste event. Since this paste event
// is happening on the pastebin, the event handling code never knows about it
// So, we have to trigger editablePaste manually
this.trigger('editablePaste', { currentTarget: editable, target: editable }, editable);
}.bind(this), 0);
},

Expand Down