hallo icon indicating copy to clipboard operation
hallo copied to clipboard

Select Paragraph (<p>) by default

Open mm-rtin opened this issue 11 years ago • 6 comments

When not in paragraph mode hitting enter inserts empty divs to create spaces in the markdown.

mm-rtin avatar Mar 30 '13 01:03 mm-rtin

I have the same issue. It happens in chrome (maybe all webkit browsers? In firefox it's ok though.

Oh, and it happens when the editable div is empty as well.

janverhulst avatar Jul 04 '13 15:07 janverhulst

+1. Has anyone worked out how to do this yet?

davecranwell avatar Feb 11 '15 15:02 davecranwell

As far as I can tell this could be done with a plugin like this very basic one:

(function() {
  (function(jQuery) {
    return jQuery.widget("IKS.hallorequireparagraphs", {
      options: {
        blockElements: ["p", "h1", "h2", "h3", "h4", "h5"]        
      },
      cleanupContentClone: function(el) {
        // if the editable element contains no block-level elements, wrap the whole thing in a P
        if(!jQuery(this.options.blockElements.toString(), el).length){
          el.wrapInner('<p></p>');
        }
      }
    });
  })(jQuery);
}).call(this);

davecranwell avatar Feb 12 '15 16:02 davecranwell

@davecranwell That doesn't seem to work all the time, e.g. if there's some existing content before adding that plugin, and in some other edge cases. The following wraps each text node one by one instead:

jQuery.widget('IKS.hallorequireparagraphs', {
  cleanupContentClone: function(el) {
    // wrap all immediate text node children in a paragraph
    el.contents().each(function(){
        if(this.nodeType == 3){
            $(this).wrap('<p/>');
        }
    });
  }
});

balazs-endresz avatar Mar 16 '15 10:03 balazs-endresz

That isn't actually how I did it eventually: https://github.com/torchbox/wagtail/pull/999/files

That solution may be just as broken though.

davecranwell avatar Mar 16 '15 10:03 davecranwell

Thanks, I didn't see that one. And I've just noticed that my version has some weird issues too: sometimes it starts adding a new paragraph after each keystroke.

balazs-endresz avatar Mar 16 '15 10:03 balazs-endresz