Skip to content

Instantly share code, notes, and snippets.

@rafbm
Forked from mikelehen/firepad-drag-drop-images.js
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafbm/bc2695a764858519a800 to your computer and use it in GitHub Desktop.
Save rafbm/bc2695a764858519a800 to your computer and use it in GitHub Desktop.
codeMirror.setOption('onDragEvent', function(cm, e) {
// Move the cursor as they drag.
var pos = codeMirror.coordsChar({left: e.x, top: e.y });
codeMirror.setCursor(pos);
codeMirror.focus();
var isImageDrop = e.type == 'drop' && e.dataTransfer.files && e.dataTransfer.files.length > 0 && e.dataTransfer.files[0].type && e.dataTransfer.files[0].type.indexOf('image/') > -1;
if (!isImageDrop) return;
event.preventDefault();
var reader = new FileReader();
reader.onload = function(event) {
var img = new Image();
img.onload = function () {
firepad.insertEntity('img', {
'src' : event.target.result,
'width' : this.width,
'height' : this.height
});
};
img.src = event.target.result;
};
reader.readAsDataURL(event.dataTransfer.files[0]);
return true;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment