Pagedown markdown script inserts image url once

岁酱吖の 提交于 2020-02-04 01:30:20

问题


I have a modified pagedown markdown markup script for inserting image url to the editor but it works only the first time.

I have explained my code with comments

 </script>
 <script type="text/javascript">
(function () {
 var converter = new Markdown.Converter();
 var help = function () { window.open('http://mywebsite.com/editing-help'); }
 var editor = new Markdown.Editor(converter);
 editor.hooks.set('insertImageDialog', function(callback) {
 setTimeout(function () 
{
        //i use bootstrap dialog to enter the url
        $('#fileModal').modal('show');  

        /*i have a button for clearing the textbox when i open 
        it the second time since when i open it the second 
        time the modal still contains what i had placed previously*/
        $("#clear").on("click", function(e) {
        e.preventDefault();
          $("#imgt").val(''); 
          $("#file").val('');
        });


        //the button that when clicked inserts the image url
        $("#insert_image_post").on("click", function(e) {
        e.preventDefault(); 

        //the image file being inserted
        if($("#imgt").val().length > 0)
        {
            var $url    =   $('input[type=text]');
            var image   =   $("#imgt").val();
            callback(image);
            $("#fileModal").modal('hide');

        }

    });


 }, 0);
return true; // tell the editor that we'll take care of getting the image url
 });

editor.run();

})();
</script>

Any one with pagedown markdown javascript... ideas to help me understand where i am going wrong?


回答1:


I managed to make it work

Its like markdown editor does not smoothly run the .on("click", function(e)... in my case. i.e.

$("#insert_image_post").on("click", function(e) {
e.preventDefault(); 

So i used theirs after going through their Markdown.Editor.js file i.e.

var thebtn = document.getElementById("insert_image_post");
thebtn.onclick = function () {

The full adjusted code below

<script>
(function () {
 var converter = new Markdown.Converter();
 var help = function () { window.open('http://stackoverflow.com/editing-help'); }
 var editor = new Markdown.Editor(converter);

 editor.hooks.set("insertImageDialog", function (callback) {

        $('#fileModal').modal('show');

            var thebtn = document.getElementById("insert_image_post");
            thebtn.onclick = function () {
                var images  =   $(".img-url").val();
                callback(images)
                 $('#fileModal').modal('hide');
            };

            var theclear = document.getElementById("clear");
            theclear.onclick = function () {

                  $("#imgt").val(''); 
                  $("#file").val('');

            };

    return true; // tell the editor that we'll take care of getting the image url
});

editor.run();

})();

</script>


来源:https://stackoverflow.com/questions/39562057/pagedown-markdown-script-inserts-image-url-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!