-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
file-dragging.js
90 lines (73 loc) · 3.28 KB
/
file-dragging.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
(function () {
'use strict';
var CLASS_DRAG_OVER = 'medium-editor-dragover';
function clearClassNames(element) {
var editable = MediumEditor.util.getContainerEditorElement(element),
existing = Array.prototype.slice.call(editable.parentElement.querySelectorAll('.' + CLASS_DRAG_OVER));
existing.forEach(function (el) {
el.classList.remove(CLASS_DRAG_OVER);
});
}
var FileDragging = MediumEditor.Extension.extend({
name: 'fileDragging',
allowedTypes: ['image'],
init: function () {
MediumEditor.Extension.prototype.init.apply(this, arguments);
this.subscribe('editableDrag', this.handleDrag.bind(this));
this.subscribe('editableDrop', this.handleDrop.bind(this));
},
handleDrag: function (event) {
event.preventDefault();
event.dataTransfer.dropEffect = 'copy';
var target = event.target.classList ? event.target : event.target.parentElement;
// Ensure the class gets removed from anything that had it before
clearClassNames(target);
if (event.type === 'dragover') {
target.classList.add(CLASS_DRAG_OVER);
}
},
handleDrop: function (event) {
// Prevent file from opening in the current window
event.preventDefault();
event.stopPropagation();
// Select the dropping target, and set the selection to the end of the target
// https://github.com/yabwe/medium-editor/issues/980
this.base.selectElement(event.target);
var selection = this.base.exportSelection();
selection.start = selection.end;
this.base.importSelection(selection);
// IE9 does not support the File API, so prevent file from opening in the window
// but also don't try to actually get the file
if (event.dataTransfer.files) {
Array.prototype.slice.call(event.dataTransfer.files).forEach(function (file) {
if (this.isAllowedFile(file)) {
if (file.type.match('image')) {
this.insertImageFile(file);
}
}
}, this);
}
// Make sure we remove our class from everything
clearClassNames(event.target);
},
isAllowedFile: function (file) {
return this.allowedTypes.some(function (fileType) {
return !!file.type.match(fileType);
});
},
insertImageFile: function (file) {
if (typeof FileReader !== 'function') {
return;
}
var fileReader = new FileReader();
fileReader.readAsDataURL(file);
// attach the onload event handler, makes it easier to listen in with jasmine
fileReader.addEventListener('load', function (e) {
var addImageElement = this.document.createElement('img');
addImageElement.src = e.target.result;
MediumEditor.util.insertHTMLCommand(this.document, addImageElement.outerHTML);
}.bind(this));
}
});
MediumEditor.extensions.fileDragging = FileDragging;
}());