New line break handling

I recently updated to 2.1.42 from a version installed just a few months earlier (I think it may have been 2.1.38). I am disappointed to find that line break handling has been changed in an unfortunately unhelpful way.

To be precise, I am talking about using the return key whilst typing in a field on a note, whether when adding a new note or when editing a note in the browse window. Formerly, typing a return closed the current <div> and opened a new <div>, but now it simply inserts a <br>. This is a problem, because a <br> does not start a new element, but rather it is akin to a forced line break rather than a carriage break in the publishing world.

The consequence of this change is that each next paragraph is not really a new paragraph, and so block-level CSS properties no longer work as intended, such as text-indent, margin, and padding. For example, if I type three paragraphs, separating each with the return key (as we are used to doing), only the first is indented, because under this new handling the three paragraphs are a single block element.

I beg you to reconsider this change. For the moment I must be more heavy-handed with customizing the HTML for each note, and that is a big time drain for basic note entry. I recognize there are times when a forced line break rather than carriage return would be desired, but the standard implementation is to use shift-return when a forced line break is desired. Please consider implementing a more standard approach. In this regard, the previous implementation was quite superior.

In the current Anki, I am not sure how to actually type distinct paragraphs without directly editing the HTML. Is there something I am missing?

Divs cause their own problems, so I’m not sure about switching back to the old behaviour, but perhaps we could make it optional.

@hengiesel is changes to onKey() and onKeyUp() based on a flag all that would be required here?

@dae Yes. However I would like to look into a nicer / fuller solution. Give me some time to prepare a PR :slight_smile:

@garretm3
Could you show us, how you apply text-indent / margin / etc. to the block elements? Do you do it through the template CSS?

1 Like

Thank you, both of you, for considering.

@hengiesel No problem. Below is a screenshot example of a card where I make use of text-indent. In poetic sections, each poetic line is a paragraph of its own (the <div> that was automatically created with the return key in previous versions). When a single poetic line is too long to display in the column, the continuation is displayed inset (I’m sure you’ve seen these before in various types of poetic or verse settings).

In the previous versions of Anki, I could type or paste the text and type returns after each line. Then I would edit the HTML for that note and simply wrap all the parts of a card that were poetry inside a single <div class='poetry'> that I would add myself. Adding that additional div was the only HTML editing I would need to do on a per card basis. The rest is handled by the card template, notably:

.poetry {
	text-indent: -20px;
	padding-left: 20px;
}

You can separate your text into new divs in the template. Just split them by <br>:

<div id="content" class="hidden">{{Front}}</div>

<div id="output" class="poetry"></div>

<script>
   let output = document.getElementById("output")

   for (item of document.getElementById("content").innerHTML.split("<br>")) {
      let paragraph = document.createElement("div")
      paragraph.innerHTML = item
      output.appendChild(paragraph)
   }
</script>

Input

"Behold, I have created the blacksmith<br>
Who blows the coals in the fire,<br>
Who brings forth an instrument for his work;<br>
And I have created the spoiler to destroy.<br>
No weapon formed against you shall prosper<br>
And every tongue which rises against you in judgement<br>
You shall condemn.<br>
This is the heritage of the servants of the Lord,<br>
And their righteousness is from Me,"<br>
Says the Lord.

Output

image

3 Likes

Everyone has their own preferences, but I was very happy when Anki desktop started using <br> instead of <div> as <br> works much better for my decks. So my vote is to keep <br> !

Cheers, and no disrepect to the OP, just stating my preference!

1 Like

How can I modify this if I want both divs and brs displayed as <p> paragraphs?

So far, I’ve been using search+replace for the copypasted content in my Extra fields.

For <br> you’ll have to use the script, but <div> and <p> are already styled pretty much the same way, so you only need to align the margin with CSS:

div {
  margin-block-start: 1em;
  margin-block-end: 1em;
  margin-inline-start: 0px;
  margin-inline-end: 0px;
}
1 Like

Maybe a way to meet halfway would be to create <br> tags when you hit newline once, and <div> when you hit newline twice (or when you go to a new line on a blank line), the idea being that if you newline twice, it means that you want a paragraph, not just a line break.

Besides that, I would say that <br> are actually much easier to manage because they allow inserting newlines without having to think about the hierarchy of the document, and thus make it much easier to make bulk modifications with find and replace & friends.

Would you at least consider leaving shift-enter to input <div>.

Shift+Enter is already used to insert linebreaks into lists without creating a new <li>. I don’t know how feasible it would be to special-case this command for different elements.

What’s your use case that makes you favor <div> over <br>?

2 Likes

for different line height between br and block

Since I just found the <p> element has the desired behavior, this doesn’t bother me anymore.

But what if one day I need blocks, paragraphs and line-brs in one field. I still think there should be a hard way to input div. not as hard as opening the html editor.

Have you considered Search and Replace (Ctrl+Alt+F)?