forked from slab/quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipboard.js
38 lines (33 loc) · 1.15 KB
/
clipboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Delta from 'rich-text/lib/delta';
import Emitter from '../core/emitter';
import Module from '../core/module';
class Clipboard extends Module {
constructor(quill, options) {
super(quill, options);
this.quill.root.addEventListener('paste', this.onPaste.bind(this));
this.container = this.quill.addContainer('ql-clipboard');
}
onPaste() {
let range = this.quill.getSelection();
if (range == null) return;
let updateDelta = new Delta().retain(range.index).delete(range.length);
this.container.setAttribute('contenteditable', true);
this.container.focus();
setTimeout(() => {
let pasteDelta = this.options.sanitize(this.container);
this.container.innerHTML = '';
let lengthAdded = pasteDelta.length();
if (lengthAdded > 0) {
this.quill.updateContents(updateDelta.concat(pasteDelta), Emitter.sources.USER);
}
this.quill.setSelection(range.index + lengthAdded, Emitter.sources.SILENT);
this.quill.selection.scrollIntoView();
}, 0);
}
}
Clipboard.DEFAULTS = {
sanitize: function(container) {
return new Delta().insert(container.innerText);
}
};
export default Clipboard;