Search code examples
javascriptpagedown

When embedding PageDown editor in my webpage, how I can I stop it from doing conversion in real time?


I noticed from the docs that PageDown offers a refreshPreview method. I'd like to turn off real-time editing and use this method to only do the conversion when the user presses a preview button.

I tried having the onPreviewRefresh hook return false, but it still updates on every change event.

Here is my code:

$(document).ready(function() {
  var converter = Markdown.getSanitizingConverter();
  Markdown.Extra.init(converter, {table_class: "table table-striped"});
  var editor = new Markdown.Editor(converter);

  editor.hooks.chain("onPreviewRefresh", function () {
    console.debug("the preview has been updated");
  });

  editor.run();

  (function() {
    $('#wmd-preview').hide();

    $('button.preview').click(function() {
      editor.refreshPreview();
      $('#wmd-editor').hide();
      $('#wmd-preview').show();
    });

    $('button.edit').click(function() {
      $('#wmd-preview').hide();
      $('#wmd-editor').show();
    });
  })();
});

Update:

Commenting out the following line (line 909 here) will actually accomplish what I want:

//timeout = setTimeout(makePreviewHtml, delay);

However, I'd be worried about unintended side effects, so I'd prefer a more surgical approach -- preferably one that doesn't require modifying the editor source code.


Solution

  • The update I added to my question actually led me to an answer.

    Apparently, the startType var here in the PreviewManager function was provided expressly for this purpose. Changing it from delayed to manual has the desired effect (hopefully without unforeseen side-effects).

    I'd feel more reassured if the setting had been offered as a Markdown.Editor configuration option.