~linuxgoose/bocpress

ref: d8759d9a75c5243da4460519a6aac5d5a944795d bocpress/main/templates/assets/drag-and-drop-upload.js -rw-r--r-- 2.8 KiB
d8759d9aJordan Robinson add blog setting for number of items to return in feed 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// setup for drag and drop uploading
document.getElementById('js-show').style.display = 'inline';
document.getElementById('js-status').style.color = '#f00';

// get body element, used for drag and drop onto it
var bodyElem = document.querySelector('textarea[name="body"]');

// prevent default drag and drop behaviours
[
    'drag',
    'dragstart',
    'dragend',
    'dragover',
    'dragenter',
    'dragleave',
    'drop',
].forEach(function (event) {
    bodyElem.addEventListener(event, function (e) {
        e.preventDefault();
        e.stopPropagation();
    });
});

function injectImageMarkdown(textInputElem, imageName, imageURL) {
    // build markdown image code
    var markdownImageCode = '![' + imageName + '](' + imageURL + ')';

    // inject markdown image code in cursor position
    if (textInputElem.selectionStart || textInputElem.selectionStart == '0') {
        var startPos = textInputElem.selectionStart;
        var endPos = textInputElem.selectionEnd;
        textInputElem.value = textInputElem.value.substring(0, startPos)
            + markdownImageCode
            + '\n'
            + textInputElem.value.substring(endPos, textInputElem.value.length);

        // set cursor location to after markdownImageCode +1 for the new line
        textInputElem.selectionEnd = endPos + markdownImageCode.length + 1;
    } else {
        // there is no cursor, just append
        textInputElem.value += markdownImageCode;
    }
}

bodyElem.addEventListener('drop', function (e) {
    // only upload one file at a time
    if (e.dataTransfer.files.length === 1) {

        // prepare form data
        var formData = new FormData();
        var name = e.dataTransfer.files[0].name;
        formData.append("file", e.dataTransfer.files[0]);

        // upload request
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function alertContents() {
            if (xhr.readyState === XMLHttpRequest.DONE) {
                if (xhr.status === 200) {
                    // success, inject markdown snippet
                    injectImageMarkdown(bodyElem, name, xhr.responseURL);
                } else {
                    alert('Image could not be uploaded. ' + xhr.responseText);
                }

                // re-enable textarea
                bodyElem.disabled = false;

                // update status message
                document.getElementById('js-status').innerText = '';
            } else {
                // this branch runs first
                // uplading, so disable textarea and show status message
                bodyElem.disabled = true;
                document.getElementById('js-status').innerText = 'UPLOADING...';
            }
        };

        xhr.open('POST', '/images/?raw=true');
        xhr.setRequestHeader('X-CSRFToken', '{{ csrf_token }}');
        xhr.send(formData);
    }
});