MCPcopy
hub / github.com/pandao/editor.md / onDrop

Function onDrop

lib/codemirror/lib/codemirror.js:3709–3757  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

3707 var lastDrop = 0;
3708
3709 function onDrop(e) {
3710 var cm = this;
3711 if (signalDOMEvent(cm, e) || eventInWidget(cm.display, e))
3712 return;
3713 e_preventDefault(e);
3714 if (ie) lastDrop = +new Date;
3715 var pos = posFromMouse(cm, e, true), files = e.dataTransfer.files;
3716 if (!pos || isReadOnly(cm)) return;
3717 // Might be a file drop, in which case we simply extract the text
3718 // and insert it.
3719 if (files && files.length && window.FileReader && window.File) {
3720 var n = files.length, text = Array(n), read = 0;
3721 var loadFile = function(file, i) {
3722 var reader = new FileReader;
3723 reader.onload = operation(cm, function() {
3724 text[i] = reader.result;
3725 if (++read == n) {
3726 pos = clipPos(cm.doc, pos);
3727 var change = {from: pos, to: pos, text: splitLines(text.join("\n")), origin: "paste"};
3728 makeChange(cm.doc, change);
3729 setSelectionReplaceHistory(cm.doc, simpleSelection(pos, changeEnd(change)));
3730 }
3731 });
3732 reader.readAsText(file);
3733 };
3734 for (var i = 0; i < n; ++i) loadFile(files[i], i);
3735 } else { // Normal drop
3736 // Don't do a replace if the drop happened inside of the selected text.
3737 if (cm.state.draggingText && cm.doc.sel.contains(pos) > -1) {
3738 cm.state.draggingText(e);
3739 // Ensure the editor is re-focused
3740 setTimeout(function() {cm.display.input.focus();}, 20);
3741 return;
3742 }
3743 try {
3744 var text = e.dataTransfer.getData("Text");
3745 if (text) {
3746 if (cm.state.draggingText && !(mac ? e.metaKey : e.ctrlKey))
3747 var selected = cm.listSelections();
3748 setSelectionNoUndo(cm.doc, simpleSelection(pos, pos));
3749 if (selected) for (var i = 0; i < selected.length; ++i)
3750 replaceRange(cm.doc, "", selected[i].anchor, selected[i].head, "drag");
3751 cm.replaceSelection(text, "around", "paste");
3752 cm.display.input.focus();
3753 }
3754 }
3755 catch(e){}
3756 }
3757 }
3758
3759 function onDragStart(cm, e) {
3760 if (ie && (!cm.state.draggingText || +new Date - lastDrop < 100)) { e_stop(e); return; }

Callers

nothing calls this directly

Calls 8

signalDOMEventFunction · 0.70
eventInWidgetFunction · 0.70
posFromMouseFunction · 0.70
isReadOnlyFunction · 0.70
loadFileFunction · 0.70
setSelectionNoUndoFunction · 0.70
simpleSelectionFunction · 0.70
replaceRangeFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…