import {Plugin, PluginKey} from "prosemirror-state"

import {HTMLPaste, TextPaste} from "../paste"

const key = new PluginKey('paste')
export let pastePlugin = function(options) {
    let shiftPressed = false
    return new Plugin({
        key,
        props: {
            handleKeyDown: (view, event) => {
                shiftPressed = event.shiftKey
                return false
            },
            handleDrop: (view, event) => {
                shiftPressed = event.shiftKey
                return false
            },
            transformPastedHTML: inHTML => {
                if (shiftPressed) {
                    return inHTML
                }
                let target = options.editor.currentView === options.editor.view ? 'main' : 'footnotes'
                let ph = new HTMLPaste(inHTML, target)
                return ph.getOutput()
            },
            transformPastedText: inText => {
                if (shiftPressed) {
                    return inText
                }
                let ph = new TextPaste(options.editor, inText, options.editor.currentView)
                ph.init()
                return '' // We need to analyze it asynchronously, so we always need to turn this into an empty string for now.
            }
        }
    })
}

@@ -18,16 +18,31 @@
 luginKey
+, TextSelection
 %7D from %22
@@ -435,32 +435,49 @@
 op: (view, event
+, slice, dragging
 ) =%3E %7B%0A         
@@ -525,35 +525,685 @@
             
-return fals
+if (dragging %7C%7C (slice && slice.size)) %7B%0A                    return false // Something other than en empty plain text string from outside. Handled by PM already.%0A                %7D%0A                let eventPos = view.posAtCoords(%7Bleft: event.clientX, top: event.clientY%7D)%0A                if (!eventPos) %7B%0A                    return false%0A                %7D%0A                let $mouse = view.state.doc.resolve(eventPos.pos)%0A                if (!$mouse) %7B%0A                    return false%0A                %7D%0A                let tr = view.state.tr%0A                tr.setSelection(new TextSelection($mouse))%0A                view.dispatch(tr)%0A                return tru
 e%0A          

move selection correctly on plaintext drop