views:

577

answers:

1

i'm trying wmd editor over ajax. here there is the bugged code

wdm code is based on openlibrary fork on github

it work very good without ajax.

but when i try to display editor over ajax form it doesn't load.

non ajax version produce this html:

<div id="wmd-container">
      <div id="wmd-button-bar"></div>
      <div id="wmd-button-bar-0" class="wmd-button-bar"><ul class="wmd-button-row"><li style="background-position: 0px 0px;" title="Strong &lt;strong&gt; Ctrl+B" class="wmd-button wmd-bold-button"></li><li style="background-position: -20px 0px;" title="Emphasis &lt;em&gt; Ctrl+I" class="wmd-button wmd-italic-button"></li><li class="wmd-spacer"></li><li style="background-position: -40px 0px;" title="Hyperlink &lt;a&gt; Ctrl+L" class="wmd-button wmd-link-button"></li><li style="background-position: -60px 0px;" title="Blockquote &lt;blockquote&gt; Ctrl+Q" class="wmd-button wmd-quote-button"></li><li style="background-position: -80px 0px;" title="Code Sample &lt;pre&gt;&lt;code&gt; Ctrl+K" class="wmd-button wmd-code-button"></li><li style="background-position: -100px 0px;" title="Image &lt;img&gt; Ctrl+G" class="wmd-button wmd-image-button"></li><li class="wmd-spacer"></li><li style="background-position: -120px 0px;" title="Numbered List &lt;ol&gt; Ctrl+O" class="wmd-button wmd-olist-button"></li><li style="background-position: -140px 0px;" title="Bulleted List &lt;ul&gt; Ctrl+U" class="wmd-button wmd-ulist-button"></li><li style="background-position: -160px 0px;" title="Heading &lt;h1&gt;/&lt;h2&gt; Ctrl+H" class="wmd-button wmd-heading-button"></li><li style="background-position: -180px 0px;" title="Horizontal Rule &lt;hr&gt; Ctrl+R" class="wmd-button wmd-hr-button"></li><li class="wmd-spacer"></li><li style="background-position: -200px -20px;" title="Undo - Ctrl+Z" class="wmd-button wmd-undo-button"></li><li style="background-position: -220px -20px;" title="Redo - Ctrl+Shift+Z" class="wmd-button wmd-redo-button"></li><li style="background-position: -240px 0px;" class="wmd-button wmd-help-button"><a title="WMD website" target="_blank" href="http://wmd-editor.com/"&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id="wmd-button-bar-2" class="wmd-button-bar"></div><div id="wmd-button-bar-4" class="wmd-button-bar"></div><textarea id="wmd-input" class="resizable" name="post-text" cols="92" rows="15" tabindex="101"></textarea><div id="wmd-preview-4" class="wmd-preview"></div><div id="wmd-preview-2" class="wmd-preview"></div><div id="wmd-preview-0" class="wmd-preview"></div>
      </div>

with ajax form:

 <div id="wmd-container">
        <div id="wmd-button-bar"></div>
        <div id="wmd-button-bar-1" class="wmd-button-bar"></div><div id="wmd-button-bar-3" class="wmd-button-bar"></div><textarea id="wmd-input" class="resizable" name="post-text" cols="92" rows="15" tabindex="101"></textarea><div id="wmd-preview-3" class="wmd-preview"></div><div id="wmd-preview-1" class="wmd-preview"></div>

        </div>

any help?

+1  A: 

I'm using the version where can be used for multiple textareas. The control is initiated by calling the following:

setup_wmd({
  "input": "user_about",
  "button_bar": "user_about-button-bar",
  "preview": "user_about-preview"
});

The problem id that the editor is loaded using the event listener on "load", so its only loaded when the page is fully loaded. When the ajax form loads, this event does not fire as the page is already loaded.'

Solution

In the method util.startEditor replace the line:

util.addEvent(top, "load", loadListener);

with something like this:

if (ajaxForm) {
  loadListener(); //this loads the editor immediately
} else {
  util.addEvent(top, "load", loadListener);  
}

You can add this line at the top in the changes section

var ajaxForm = wmd_options.ajaxForm || false

So you can call this method to load the editor immediately:

setup_wmd({
  "input": "user_about",
  "button_bar": "user_about-button-bar",
  "preview": "user_about-preview",
  "ajaxForm": true
});

So you can now call this method after the ajax request loads to load the editor.

jarrold