javascriptjquerytwitter-bootstrapmarkdownwmd

markdowndeep image upload


I'm working with MarkDownDeep. It has a similar "insert image" function like SO does.

I'd like to extend this function to use a bootstrap modal that would allow the user to upload their own images or at least get a typeahead working so that a user could pick something uploaded from another page.

I've tried to use a callback function in replace of the the prompt below but it doesn't insert str into the textbox like the original function.

pub.cmd_img = function (ctx) {
    ctx.TrimSelection();
    if (!ctx.CheckSimpleSelection())
        return false;

    imagePopUp(function(results) {

        $("#" + ctx.m_textarea.id).focus();

        var url = results;

        if (url === null)
            return false;

        var alttext = ctx.getSelectedText();
        if (alttext.length == 0) {
            alttext = "Image Text";
        }

        var str = "![" + alttext + "](" + url + ")";

        ctx.ReplaceSelection(str);
        ctx.m_selectionStart += 2;
        ctx.m_selectionEnd = ctx.m_selectionStart + alttext.length;
        return true;
    });
    return false;

uploadImageUrl is a holding variable because at the moment I'm using an iframe inside the modal so the user can upload, the iframe then sets parent.uploadImageUrl

uploadImageUrl = "baz"; 
function imagePopUp(callback) {
    $('#imageUpload').modal('show');

    $('#confirmTrue').click(function () {
        $('#imageUpload').modal('hide');

        if (callback) callback(uploadImageUrl);
    });
}

Original

 pub.cmd_img = function (ctx) {
        ctx.TrimSelection();
        if (!ctx.CheckSimpleSelection())
            return false;

        var url = prompt("Enter the image URL"); //need to change what this does

        if (url === null)
            return false;

        var alttext = ctx.getSelectedText();
        if (alttext.length == 0) {
            alttext = "Image Text";
        }

        var str = "![" + alttext + "](" + url + ")";

        ctx.ReplaceSelection(str);
        ctx.m_selectionStart += 2;
        ctx.m_selectionEnd = ctx.m_selectionStart + alttext.length;
        return true;
    };

You can see my none-working fiddle


Solution

  • This should do the trick:

    pub.cmd_img = function(ctx){
    
       ctx.TrimSelection();
       if (!ctx.CheckSimpleSelection())
          return false;
    
       //call popup
       imagePopUp(function(results){
    
          $("#" + ctx.m_textarea.id).focus();
    
          var url = results;
          if (url === null)
             return false;
    
          var alttext = ctx.getSelectedText();
          if (alttext.length == 0){
             alttext = "Image Text";
          }
    
          var str = "![" + alttext + "](" + url + ")";
          var editor = $(ctx.m_textarea).data("mdd");
    
          editor.cmd_img_core = function(state){
             state.ReplaceSelection(str);
             state.m_selectionStart += 2;
             state.m_selectionEnd = state.m_selectionStart + alttext.length;
             return true;
          };
    
          editor.InvokeCommand("img_core");
          delete editor.cmd_img_core;
       });             
       return false;
    };