bergie / hallo

Simple rich text editor (contentEditable) for jQuery UI
http://hallojs.org/
MIT License
2.43k stars 316 forks source link

Select Paragraph (<p>) by default #157

Open mm-rtin opened 11 years ago

mm-rtin commented 11 years ago

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

janverhulst commented 11 years ago

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.

davecranwell commented 9 years ago

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

davecranwell commented 9 years ago

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);
balazs-endresz commented 9 years ago

@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/>');
        }
    });
  }
});
davecranwell commented 9 years ago

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.

balazs-endresz commented 9 years ago

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.