Skip to content

Commit

Permalink
add copy/paste delta directly
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Jul 24, 2018
1 parent 7a2adbf commit 756f0c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
23 changes: 15 additions & 8 deletions modules/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,27 @@ class Clipboard extends Module {

onCopy(e, range) {
const text = this.quill.getText(range);
const delta = this.quill.getContents(range);
const html = this.quill.getSemanticHTML(range);
e.clipboardData.setData('text/plain', text);
e.clipboardData.setData('text/html', html);
e.clipboardData.setData('text/delta', JSON.stringify(delta));
}

onPaste(e, range) {
const html = e.clipboardData.getData('text/html');
const text = e.clipboardData.getData('text/plain');
const pastedDelta = this.convert({ text, html });
debug.log('onPaste', pastedDelta, { text, html });
const delta = new Delta()
.retain(range.index)
.delete(range.length)
.concat(pastedDelta);
const json = e.clipboardData.getData('text/delta');
let delta = new Delta().retain(range.index).delete(range.length);
if (json) {
const directDelta = new Delta(JSON.parse(json));
debug.log('onPaste direct', directDelta);
delta = delta.concat(directDelta);
} else {
const html = e.clipboardData.getData('text/html');
const text = e.clipboardData.getData('text/plain');
const pastedDelta = this.convert({ text, html });
debug.log('onPaste converted', pastedDelta, { text, html });
delta = delta.concat(pastedDelta);
}
this.quill.updateContents(delta, Quill.sources.USER);
// range.length contributes to delta.length()
this.quill.setSelection(
Expand Down
4 changes: 2 additions & 2 deletions test/unit/modules/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ describe('Clipboard', function() {
it('paste', function(done) {
this.quill.clipboard.onCapturePaste({
clipboardData: {
getData: () => {
return '<strong>|</strong>';
getData: type => {
return type === 'text/html' ? '<strong>|</strong>' : '';
},
},
preventDefault: () => {},
Expand Down

0 comments on commit 756f0c3

Please sign in to comment.