Use p tag instead of div for regular block content
Currently regular blocks are a <div> with newlines added as a <br> (unless you convert a line to another type and back). It would allow more flexible styling (and arguably be more semantic) to default to a <p> tag with enter adding a new tag, and shift+enter adding a <br>. This could be added as an opt-in for backwards compatibility.
see https://github.com/basecamp/trix/issues/117
I don't think <p> will ever be the default block element in Trix. It's a popular request though and I would like to allow them via configuration more easily. Currently, you can set Trix.config.blockAttributes.default.tagName = "p" in your app, which works fine, but doesn't create a new <p> when you press return. https://github.com/basecamp/trix/pull/187 addresses that as does this (now stale) branch I started.
One potential issue with using <p> is Trix's image attachments, which are rendered in a <figure> element. <p> only permits phrasing content so placing a <figure> in one would be invalid.
For what it's worth, every word processing program (by default, at least) I've tried has the same newline behavior as Trix. Press return, cursor moves to the next line.
It would actually be quite handy when one has to sit down and play around with Nodes in the editor upon manipulation.
Even if one is not doing so, i would still like to keep the elements in a p tag.
Thanks for the quick response! Do images need to be allowed inline, or could they be added in their own block?
As far as word processors go, I think if you turn on hidden characters, when you press return you get a paragraph mark (equivalent of </p><p>) — if your formatting has space after/before paragraphs you'll get a larger break than the normal line spacing — whereas if you press shift+return you get a newline (equivalent of <br />).
Is #187 likely to be merged soon?
In any case, keep up the great work, and thanks for an awesome library! :)
I have to agree with @Truffula that semantically, Enter is a new Paragraph, while Shift+Enter is a Linebreak - and that is what br tag stands for. Copy pasting text from Basecamp to Word clearly shows the mess.
I don't think Images are allowed inline presently anyway, in Basecamp they certainly do not behave like that. So it should not be difficult to simply close the <p> tag and open <figure> whenever user inserts an image.
This change would make Trix also more accessible for screenreaders (a blind person could navigate - jump through paragraphs, which is currently impossible).
As a teacher and using Basecamp for classroom, I need to be considerate of visually impaired students, and while presently I do not have any, I am part of the research team trying to bridge the gap in educational tools and accessibility of information for impaired students.
@javan Except WordPress :(
I'm trailing trix to use in our application for authors to write books. We'd ideally like to use paragraph elements as the primary wrapper for text content. There may be another way than changing the default block element — we'd like to keep div's as they are as a kind of section break.
Ideally, a structure like this:
div
-- p
-- figure
-- p
div
-- p
What I'm thinking, as a rough idea, is that PieceView#createStringNodes appends <p> nodes instead of just text nodes. This should not have the same problem as setting the default block element to <p> and conflict with <figure> elements as its handled separately by PieceView.
I'm going to dive deeper on this to assess what all the moving parts are before starting a fork with that approach. Is there any initial advice for those of you with deeper understanding — known potential conflicts or issues?
@javan since this will be a requirement for my particular implementation of trix, and I suspect may be beneficial for others, is there any guidance on how we could best go about this?
My initial approach wouldn't have been all that successful. After more thought it comes down to how Trix.BlockView#createNodes renders out to a Trix.TextView that then collates nodes for pieces. For paragraphs to work, and to keep pieces as they are, an intermediary step to split pieces by \n\n to form paragraphs (retaining the pieces attributes) and \n for <br /> is roughly the new approach I am considering. However, I am unsure how well this will go down with the HTMLParser.
Something roughly flowing like: BlockView#createNodes -> ParagraphGroupView? -> TextView -> PieceView
But in this approach I would like to respect that the option to use paragraphs is optional. So thats potentially another added detail.
I'll give it a go in a fork branch. One way of learning more about trix's ins, outs and what-have-yous :)
Here's a small potential change that would allow configuring the default block to use <p> more naturally:
diff --git a/src/trix/models/block.coffee b/src/trix/models/block.coffee
index f426bc3..8fb3f9d 100644
--- a/src/trix/models/block.coffee
+++ b/src/trix/models/block.coffee
@@ -96,7 +96,7 @@ class Trix.Block extends Trix.Object
getBlockConfig(@getLastAttribute())?.terminal
breaksOnReturn: ->
- getBlockConfig(@getLastAttribute())?.breakOnReturn
+ getBlockConfig(@getLastAttribute() ? "default").breakOnReturn
findLineBreakInDirectionFromPosition: (direction, position) ->
string = @toString()
diff --git a/src/trix/models/line_break_insertion.coffee b/src/trix/models/line_break_insertion.coffee
index 27b0657..93e45eb 100644
--- a/src/trix/models/line_break_insertion.coffee
+++ b/src/trix/models/line_break_insertion.coffee
@@ -15,7 +15,7 @@ class Trix.LineBreakInsertion
if @block.hasAttributes() and @block.isListItem() and not @block.isEmpty()
@startLocation.offset isnt 0
else
- @breaksOnReturn and @nextCharacter isnt "\n"
+ @breaksOnReturn unless @shouldBreakFormattedBlock()
shouldBreakFormattedBlock: ->
@block.hasAttributes() and not @block.isListItem() and
And then in your app:
Trix.config.blockAttributes.default.tagName = "p"
Trix.config.blockAttributes.default.breakOnReturn = true
Let me know what you think.
@javan this seams to work perfectly and it doesn’t break any tests. Is there any reason why this can’t be merged as a configureable option? I’d happily work on fixing any issues, but your solution seams perfect. 👌
@javan I’ve tested your suggested changes quite throughly and everything appears to work very well. I’m running it live in production with paragraph tags and I’ve had no problems so far.
If I write some tests and submit a PR, would you consider merging this in? None of the original tests break if you don’t configure Trix to use paragraphs.
@joeldrapper, that'd be great! Especially adding a test or two.
I would love to see this implemented as well.
@svennerberg sorry for the delay on this! For now, you can use @javan’s implementation by changing a couple of lines.
@joeldrapper It works like a charm! Thanks!
@joeldrapper @javan After some further testing of this approach I experienced some unwanted side effects. When adding an image it's duplicated each time I save the document and then read it back from the database. So at first, there's 1 image then 2 then 4 and so on.
I don't understand exactly why this is happening but the problem disappears when I don't use:
Trix.config.blockAttributes.default.tagName = "p"
Trix.config.blockAttributes.default.breakOnReturn = true
I’ve been using it in production with attachments disabled, so thanks for letting me know. I’ll look into it.
@joeldrapper any update on this?
I'm struggling to follow this one. Is this merged into the code then? As when I do:
Trix.config.blockAttributes.default.tagName = "p"
Trix.config.blockAttributes.default.breakOnReturn = true
I'm getting <br> tags inside the <p> tags. Is that correct behaviour?
e.g.
<p><!--block-->Paragraph1<br><br>Paragraph2</p>
Where I was expecting more.
<p><!--block-->Paragraph1</p><p><!--block-->Paragraph2</p>
This issue has been automatically marked as stale after 90 days of inactivity. It will be closed if no further activity occurs.