Skip to content

Commit

Permalink
onCopy/onPaste mirror API
Browse files Browse the repository at this point in the history
- onCapture is the entry, takes in e
- neither onCopy or onPaste takes in e
  • Loading branch information
jhchen committed Sep 20, 2018
1 parent 8fd90b2 commit c44241b
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions modules/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ const STYLE_ATTRIBUTORS = [
class Clipboard extends Module {
constructor(quill, options) {
super(quill, options);
this.quill.root.addEventListener('copy', e => this.onCopy(e, false));
this.quill.root.addEventListener('cut', e => this.onCopy(e, true));
this.quill.root.addEventListener('copy', e => this.onCaptureCopy(e, false));
this.quill.root.addEventListener('cut', e => this.onCaptureCopy(e, true));
this.quill.root.addEventListener('paste', this.onCapturePaste.bind(this));
this.matchers = [];
CLIPBOARD_CONFIG.concat(this.options.matchers).forEach(
Expand Down Expand Up @@ -125,41 +125,41 @@ class Clipboard extends Module {
}
}

onCaptureCopy(range) {
const text = this.quill.getText(range);
const html = this.quill.getSemanticHTML(range);
return { html, text };
}

onCapturePaste(e) {
if (e.defaultPrevented || !this.quill.isEnabled()) return;
e.preventDefault();
const range = this.quill.getSelection(true);
if (range == null) return;
this.onPaste(e, range);
}

onCopy(e, isCut = false) {
onCaptureCopy(e, isCut = false) {
if (e.defaultPrevented) return;
e.preventDefault();
const [range] = this.quill.selection.getRange();
if (range == null) return;
const { html, text } = this.onCaptureCopy(range, isCut);
const { html, text } = this.onCopy(range, isCut);
e.clipboardData.setData('text/plain', text);
e.clipboardData.setData('text/html', html);
if (isCut) {
this.quill.deleteText(range, Quill.sources.USER);
}
}

onPaste(e, range) {
onCapturePaste(e) {
if (e.defaultPrevented || !this.quill.isEnabled()) return;
e.preventDefault();
const range = this.quill.getSelection(true);
if (range == null) return;
const html = e.clipboardData.getData('text/html');
const text = e.clipboardData.getData('text/plain');
const files = Array.from(e.clipboardData.files || []);
if (!html && files.length > 0) {
this.quill.uploader.upload(range, files);
return;
} else {
this.onPaste(range, { html, text });
}
}

onCopy(range) {
const text = this.quill.getText(range);
const html = this.quill.getSemanticHTML(range);
return { html, text };
}

onPaste(range, { text, html }) {
const formats = this.quill.getFormat(range.index);
const pastedDelta = this.convert({ text, html }, formats);
debug.log('onPaste', pastedDelta, { text, html });
Expand Down

0 comments on commit c44241b

Please sign in to comment.