Skip to content

Commit

Permalink
bypass uploader if clipboardData has rich text
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Venegas committed Aug 7, 2018
1 parent ae22be5 commit f9e9394
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 33 deletions.
5 changes: 4 additions & 1 deletion modules/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ class Clipboard extends Module {
e.preventDefault();
const range = this.quill.getSelection(true);
if (range == null) return;
const hasRichText =
e.clipboardData.getData('text/html') &&
e.clipboardData.getData('text/plain');
const files = Array.from(e.clipboardData.files || []);
if (files.length > 0) {
if (!hasRichText && files.length > 0) {
this.quill.uploader.upload(range, files);
} else {
this.onPaste(e, range);
Expand Down
80 changes: 48 additions & 32 deletions test/unit/modules/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,58 @@ describe('Clipboard', function() {
this.quill.setSelection(2, 5);
});

it('paste', function(done) {
this.quill.clipboard.onCapturePaste({
clipboardData: {
getData: type => {
return type === 'text/html' ? '<strong>|</strong>' : '';
},
},
preventDefault: () => {},
describe('paste', function() {
beforeAll(function() {
this.createClipboardData = overrides => ({
clipboardData: Object.assign(
{
getData: type => {
return type === 'text/html' ? '<strong>|</strong>' : '|';
},
},
overrides,
),
preventDefault: () => {},
});
});
setTimeout(() => {
expect(this.quill.root).toEqualHTML(
'<p>01<strong>|</strong><em>7</em>8</p>',

it('pastes html data', function(done) {
this.quill.clipboard.onCapturePaste(this.createClipboardData());
setTimeout(() => {
expect(this.quill.root).toEqualHTML(
'<p>01<strong>|</strong><em>7</em>8</p>',
);
expect(this.quill.getSelection()).toEqual(new Range(3));
done();
}, 2);
});

it('pastes html data if present with file', function(done) {
const upload = spyOn(this.quill.uploader, 'upload');
this.quill.clipboard.onCapturePaste(
this.createClipboardData({
files: ['file'],
}),
);
expect(this.quill.getSelection()).toEqual(new Range(3));
done();
}, 2);
});
setTimeout(() => {
expect(upload).not.toHaveBeenCalled();
expect(this.quill.root).toEqualHTML(
'<p>01<strong>|</strong><em>7</em>8</p>',
);
expect(this.quill.getSelection()).toEqual(new Range(3));
done();
}, 2);
});

it('selection-change', function(done) {
const handler = {
change() {},
};
spyOn(handler, 'change');
this.quill.on('selection-change', handler.change);
this.quill.clipboard.onCapturePaste({
clipboardData: {
getData: () => {
return '0';
},
},
preventDefault: () => {},
it('does not fire selection-change', function(done) {
const change = jasmine.createSpy('change');
this.quill.on('selection-change', change);
this.quill.clipboard.onCapturePaste(this.createClipboardData());
setTimeout(function() {
expect(change).not.toHaveBeenCalled();
done();
}, 2);
});
setTimeout(function() {
expect(handler.change).not.toHaveBeenCalled();
done();
}, 2);
});

it('dangerouslyPasteHTML(html)', function() {
Expand Down

0 comments on commit f9e9394

Please sign in to comment.