MCPcopy Create free account
hub / github.com/TruthHun/BookStack / onDrop

Function onDrop

static/mergely/lib/codemirror.js:3839–3896  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

3837 var lastDrop = 0;
3838
3839 function onDrop(e) {
3840 var cm = this;
3841 clearDragCursor(cm);
3842 if (signalDOMEvent(cm, e) || eventInWidget(cm.display, e))
3843 return;
3844 e_preventDefault(e);
3845 if (ie) lastDrop = +new Date;
3846 var pos = posFromMouse(cm, e, true), files = e.dataTransfer.files;
3847 if (!pos || cm.isReadOnly()) return;
3848 // Might be a file drop, in which case we simply extract the text
3849 // and insert it.
3850 if (files && files.length && window.FileReader && window.File) {
3851 var n = files.length, text = Array(n), read = 0;
3852 var loadFile = function(file, i) {
3853 if (cm.options.allowDropFileTypes &&
3854 indexOf(cm.options.allowDropFileTypes, file.type) == -1)
3855 return;
3856
3857 var reader = new FileReader;
3858 reader.onload = operation(cm, function() {
3859 var content = reader.result;
3860 if (/[\x00-\x08\x0e-\x1f]{2}/.test(content)) content = "";
3861 text[i] = content;
3862 if (++read == n) {
3863 pos = clipPos(cm.doc, pos);
3864 var change = {from: pos, to: pos,
3865 text: cm.doc.splitLines(text.join(cm.doc.lineSeparator())),
3866 origin: "paste"};
3867 makeChange(cm.doc, change);
3868 setSelectionReplaceHistory(cm.doc, simpleSelection(pos, changeEnd(change)));
3869 }
3870 });
3871 reader.readAsText(file);
3872 };
3873 for (var i = 0; i < n; ++i) loadFile(files[i], i);
3874 } else { // Normal drop
3875 // Don't do a replace if the drop happened inside of the selected text.
3876 if (cm.state.draggingText && cm.doc.sel.contains(pos) > -1) {
3877 cm.state.draggingText(e);
3878 // Ensure the editor is re-focused
3879 setTimeout(function() {cm.display.input.focus();}, 20);
3880 return;
3881 }
3882 try {
3883 var text = e.dataTransfer.getData("Text");
3884 if (text) {
3885 if (cm.state.draggingText && !(mac ? e.altKey : e.ctrlKey))
3886 var selected = cm.listSelections();
3887 setSelectionNoUndo(cm.doc, simpleSelection(pos, pos));
3888 if (selected) for (var i = 0; i < selected.length; ++i)
3889 replaceRange(cm.doc, "", selected[i].anchor, selected[i].head, "drag");
3890 cm.replaceSelection(text, "around", "paste");
3891 cm.display.input.focus();
3892 }
3893 }
3894 catch(e){}
3895 }
3896 }

Callers

nothing calls this directly

Calls 8

clearDragCursorFunction · 0.85
signalDOMEventFunction · 0.70
eventInWidgetFunction · 0.70
posFromMouseFunction · 0.70
loadFileFunction · 0.70
setSelectionNoUndoFunction · 0.70
simpleSelectionFunction · 0.70
replaceRangeFunction · 0.70

Tested by

no test coverage detected