MCPcopy Create free account
hub / github.com/witheve/Eve / onDrop

Function onDrop

src/codemirror.js:3861–3918  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

3859 var lastDrop = 0;
3860
3861 function onDrop(e) {
3862 var cm = this;
3863 clearDragCursor(cm);
3864 if (signalDOMEvent(cm, e) || eventInWidget(cm.display, e))
3865 return;
3866 e_preventDefault(e);
3867 if (ie) lastDrop = +new Date;
3868 var pos = posFromMouse(cm, e, true), files = e.dataTransfer.files;
3869 if (!pos || cm.isReadOnly()) return;
3870 // Might be a file drop, in which case we simply extract the text
3871 // and insert it.
3872 if (files && files.length && window.FileReader && window.File) {
3873 var n = files.length, text = Array(n), read = 0;
3874 var loadFile = function(file, i) {
3875 if (cm.options.allowDropFileTypes &&
3876 indexOf(cm.options.allowDropFileTypes, file.type) == -1)
3877 return;
3878
3879 var reader = new FileReader;
3880 reader.onload = operation(cm, function() {
3881 var content = reader.result;
3882 if (/[\x00-\x08\x0e-\x1f]{2}/.test(content)) content = "";
3883 text[i] = content;
3884 if (++read == n) {
3885 pos = clipPos(cm.doc, pos);
3886 var change = {from: pos, to: pos,
3887 text: cm.doc.splitLines(text.join(cm.doc.lineSeparator())),
3888 origin: "paste"};
3889 makeChange(cm.doc, change);
3890 setSelectionReplaceHistory(cm.doc, simpleSelection(pos, changeEnd(change)));
3891 }
3892 });
3893 reader.readAsText(file);
3894 };
3895 for (var i = 0; i < n; ++i) loadFile(files[i], i);
3896 } else { // Normal drop
3897 // Don't do a replace if the drop happened inside of the selected text.
3898 if (cm.state.draggingText && cm.doc.sel.contains(pos) > -1) {
3899 cm.state.draggingText(e);
3900 // Ensure the editor is re-focused
3901 setTimeout(function() {cm.display.input.focus();}, 20);
3902 return;
3903 }
3904 try {
3905 var text = e.dataTransfer.getData("Text");
3906 if (text) {
3907 if (cm.state.draggingText && !cm.state.draggingText.copy)
3908 var selected = cm.listSelections();
3909 setSelectionNoUndo(cm.doc, simpleSelection(pos, pos));
3910 if (selected) for (var i = 0; i < selected.length; ++i)
3911 replaceRange(cm.doc, "", selected[i].anchor, selected[i].head, "drag");
3912 cm.replaceSelection(text, "around", "paste");
3913 cm.display.input.focus();
3914 }
3915 }
3916 catch(e){}
3917 }
3918 }

Callers

nothing calls this directly

Calls 12

clearDragCursorFunction · 0.85
signalDOMEventFunction · 0.85
eventInWidgetFunction · 0.85
posFromMouseFunction · 0.85
loadFileFunction · 0.85
setSelectionNoUndoFunction · 0.85
simpleSelectionFunction · 0.85
replaceRangeFunction · 0.85
containsMethod · 0.80
focusMethod · 0.80
listSelectionsMethod · 0.80
replaceSelectionMethod · 0.80

Tested by

no test coverage detected