From 5b200616076b8c1a924e0899d87bbc41927a58be Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Fri, 10 Feb 2023 20:01:46 +0100 Subject: [PATCH 1/2] Only insert line break for block formats if the block already has text (#1349, #1350) --- novelwriter/gui/doceditor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/novelwriter/gui/doceditor.py b/novelwriter/gui/doceditor.py index 99104c00..87c9e7cc 100644 --- a/novelwriter/gui/doceditor.py +++ b/novelwriter/gui/doceditor.py @@ -1750,6 +1750,7 @@ class GuiDocEditor(QTextEdit): # Remove existing format first, if any theText = theBlock.text() + hasText = len(theText) > 0 if theText.startswith("@"): logger.error("Cannot apply block format to keyword/value line") return False @@ -1855,7 +1856,7 @@ class GuiDocEditor(QTextEdit): posS = theCursor.selectionStart() theCursor.removeSelectedText() theCursor.setPosition(posS) - if posS > 0: + if posS > 0 and hasText: theCursor.insertBlock() theCursor.insertText(theText) if posO - cOffset >= 0: From aa3fe961589477f97d74a833cc7635d7a38760b4 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Fri, 10 Feb 2023 20:07:52 +0100 Subject: [PATCH 2/2] Add some comments to explain what's going on in the block formatting --- novelwriter/gui/doceditor.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/novelwriter/gui/doceditor.py b/novelwriter/gui/doceditor.py index 87c9e7cc..361b236e 100644 --- a/novelwriter/gui/doceditor.py +++ b/novelwriter/gui/doceditor.py @@ -1856,11 +1856,17 @@ class GuiDocEditor(QTextEdit): posS = theCursor.selectionStart() theCursor.removeSelectedText() theCursor.setPosition(posS) + if posS > 0 and hasText: + # If the block already had text, we must insert a new block + # first before we can add back the text to it. theCursor.insertBlock() + theCursor.insertText(theText) + if posO - cOffset >= 0: theCursor.setPosition(posO - cOffset) + theCursor.endEditBlock() self.setTextCursor(theCursor)