petehouston / laravel-tinymce-simple-imageupload

Simple image upload for TinyMCE in Laravel.
69 stars 25 forks source link

Deletion of image #6

Open rambo666 opened 6 years ago

rambo666 commented 6 years ago

The package works like a charm. But there is an issue if it could be resolved I hope many people will be using this function. After I uploaded the Image on tinymce texteditor it gets uploaded to the server, However If I delete the image from the text editor the uploaded images stays on my server. Could you add a way to resolve this issue? There is a workaround using Javascipt where I can take the deleted image name on deletion of image and then remove it from the sever, However using javascript on the client side means people can delete any image if they know the name.

ihtisham007 commented 2 years ago

Javascript code for deletion tinymce image by using ajax tinymce.init({ ... setup: function (editor) { editor.on('init change', function () { editor.save();

    });
    editor.on('KeyDown',function (e){
        if ((e.keyCode == 8 || e.keyCode == 46) && editor.selection) { //for Backspace key and delete key pressed
            var selectedNode = editor.selection.getNode();
            const Imgaes = walkDom(selectedNode);
            for(let i=0;i<Imgaes.length;i++){
                    let getFileName = Imgaes[i].src;

                    getFileName = getFileName.split('/');
                    getFileName = getFileName[getFileName.length - 1]
                    PostImgDeletion(getFileName)
            }
        }
    }
});

function walkDom(start_element) { var arr=[]; // we can gather elements here var loop=function(element) { do{ // we can do something with element if(element.nodeName=="IMG") // do not include text nodes only images Nodes arr.push(element); if(element.hasChildNodes()) loop(element.firstChild); } while(element=element.nextSibling); } loop(start_element); //loop(start_elem.firstChild); // do not include siblings of start element return arr; }