editor.md: drag, cut, copy, paste, upload pictures, file plug-ins

Keywords: Javascript PHP github JSON

editor.md is a Markdown editor with beautiful interface and powerful functions.

But there is a lack of cutting and dragging upload in the aspect of image and file upload. There are so many shortcomings in each aspect. Here is a simple implementation. There is not much nonsense, just code directly.

uploadImg.js

function initPasteDragImg(Editor){
    var doc = document.getElementById(Editor.id)
    doc.addEventListener('paste', function (event) {
        var items = (event.clipboardData || window.clipboardData).items;
        var file = null;
        if (items && items.length) {
            // Search clipboard items
            for (var i = 0; i < items.length; i++) {
                if (items[i].type.indexOf('image') !== -1) {
                    file = items[i].getAsFile();
                    break;
                }
            }
        } else {
            console.log("Current browser does not support");
            return;
        }
        if (!file) {
            console.log("Paste content not picture");
            return;
        }
        uploadImg(file,Editor);
    });

    var dashboard = document.getElementById(Editor.id)
    dashboard.addEventListener("dragover", function (e) {
        e.preventDefault()
        e.stopPropagation()
    })
    dashboard.addEventListener("dragenter", function (e) {
        e.preventDefault()
        e.stopPropagation()
    })
    dashboard.addEventListener("drop", function (e) {
        e.preventDefault()
        e.stopPropagation()
     var files = this.files || e.dataTransfer.files;
     uploadImg(files[0],Editor);
     })
}
function uploadImg(file,Editor){
    var formData = new FormData();
    var fileName=new Date().getTime()+"."+file.name.split(".").pop();
    formData.append('editormd-image-file', file, fileName);

    $.ajax({
        url: Editor.settings.imageUploadURL,
        type: 'post',
        data: formData,
        processData: false,
        contentType: false,
        dataType: 'json',
        success: function (msg) {

            var success=msg['success'];
            if(success==1){
                var url=msg["url"];
                if(/\.(png|jpg|jpeg|gif|bmp|ico)$/.test(url)){
                    Editor.insertValue("![picture alt]("+msg["url"]+" ''picture title'')");
                }else{
                    Editor.insertValue("[Download attachments]("+msg["url"]+")");
                }     
            }else{
                console.log(msg);
                alert("Upload failure");
            }
        }
    });


}

Usage method

1. Import uploadImg.js into the page

<script src="uploadImg.js" type="text/javascript"></script>

2. The editor.md configuration enables the image upload function and initializes the plug-in in the onload event.

var testEditor = editormd("test-editormd", {
                        width: "90%",
                        height: 740,
                        path : '../lib/',
                        theme : "dark",
                        previewTheme : "dark",
                        editorTheme : "pastel-on-dark",
                        codeFold : true,
                        saveHTMLToTextarea : true,    // Save HTML to Textarea
                        searchReplace : true,

                        imageUpload : true, //Must
                        imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                        imageUploadURL : "./php/upload.php", //Must
                        onload : function() {
                            initPasteDragImg(this); //Must

                        }
});

3. Copy and paste, drag and drop the file, and add the suffix supported by the upload service.

The official upload code for PHP is provided. / editor.md/blob/master/examples/php/upload.php

In this way, it supports the function of dragging, copying, posting and uploading of txt and PDF.

4 GIthub : https://github.com/mifunc/edi... php demo is provided, which is ready to be used out of the box

Posted by busnut on Wed, 23 Oct 2019 10:55:38 -0700