MCPcopy Create free account
hub / github.com/OpenEndedGroup/Field2 / onDrop

Function onDrop

lib/web/CodeMirror/src/edit/drop_events.js:19–88  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

17let lastDrop = 0
18
19export function onDrop(e) {
20 let cm = this
21 clearDragCursor(cm)
22 if (signalDOMEvent(cm, e) || eventInWidget(cm.display, e))
23 return
24 e_preventDefault(e)
25 if (ie) lastDrop = +new Date
26 let pos = posFromMouse(cm, e, true), files = e.dataTransfer.files
27 if (!pos || cm.isReadOnly()) return
28 // Might be a file drop, in which case we simply extract the text
29 // and insert it.
30 if (files && files.length && window.FileReader && window.File) {
31 let n = files.length, text = Array(n), read = 0
32 const markAsReadAndPasteIfAllFilesAreRead = () => {
33 if (++read == n) {
34 operation(cm, () => {
35 pos = clipPos(cm.doc, pos)
36 let change = {from: pos, to: pos,
37 text: cm.doc.splitLines(
38 text.filter(t => t != null).join(cm.doc.lineSeparator())),
39 origin: "paste"}
40 makeChange(cm.doc, change)
41 setSelectionReplaceHistory(cm.doc, simpleSelection(clipPos(cm.doc, pos), clipPos(cm.doc, changeEnd(change))))
42 })()
43 }
44 }
45 const readTextFromFile = (file, i) => {
46 if (cm.options.allowDropFileTypes &&
47 indexOf(cm.options.allowDropFileTypes, file.type) == -1) {
48 markAsReadAndPasteIfAllFilesAreRead()
49 return
50 }
51 let reader = new FileReader
52 reader.onerror = () => markAsReadAndPasteIfAllFilesAreRead()
53 reader.onload = () => {
54 let content = reader.result
55 if (/[\x00-\x08\x0e-\x1f]{2}/.test(content)) {
56 markAsReadAndPasteIfAllFilesAreRead()
57 return
58 }
59 text[i] = content
60 markAsReadAndPasteIfAllFilesAreRead()
61 }
62 reader.readAsText(file)
63 }
64 for (let i = 0; i < files.length; i++) readTextFromFile(files[i], i)
65 } else { // Normal drop
66 // Don't do a replace if the drop happened inside of the selected text.
67 if (cm.state.draggingText && cm.doc.sel.contains(pos) > -1) {
68 cm.state.draggingText(e)
69 // Ensure the editor is re-focused
70 setTimeout(() => cm.display.input.focus(), 20)
71 return
72 }
73 try {
74 let text = e.dataTransfer.getData("Text")
75 if (text) {
76 let selected

Callers

nothing calls this directly

Calls 12

signalDOMEventFunction · 0.90
eventInWidgetFunction · 0.90
e_preventDefaultFunction · 0.90
posFromMouseFunction · 0.90
setSelectionNoUndoFunction · 0.90
simpleSelectionFunction · 0.90
replaceRangeFunction · 0.90
getDataMethod · 0.80
clearDragCursorFunction · 0.70
readTextFromFileFunction · 0.70
containsMethod · 0.45
focusMethod · 0.45

Tested by

no test coverage detected