diff --git a/nw/constants/enum.py b/nw/constants/enum.py index 67ba0c5e..9d47581e 100644 --- a/nw/constants/enum.py +++ b/nw/constants/enum.py @@ -76,6 +76,7 @@ class nwDocAction(Enum): BLOCK_H3 = 20 BLOCK_H4 = 21 BLOCK_COM = 22 + BLOCK_TXT = 23 # END Enum nwDocAction diff --git a/nw/gui/elements/doceditor.py b/nw/gui/elements/doceditor.py index 61bbd672..1f878898 100644 --- a/nw/gui/elements/doceditor.py +++ b/nw/gui/elements/doceditor.py @@ -450,6 +450,8 @@ class GuiDocEditor(QTextEdit): self._formatBlock(nwDocAction.BLOCK_H4) elif theAction == nwDocAction.BLOCK_COM: self._formatBlock(nwDocAction.BLOCK_COM) + elif theAction == nwDocAction.BLOCK_TXT: + self._formatBlock(nwDocAction.BLOCK_TXT) else: logger.debug("Unknown or unsupported document action %s" % str(theAction)) return False @@ -784,9 +786,75 @@ class GuiDocEditor(QTextEdit): theCursor = self.textCursor() theBlock = theCursor.block() - if theBlock.isValid(): - theText = theBlock.text() - print(theText) + if not theBlock.isValid(): + logger.debug("Invalid block selected for action %s" % str(docAction)) + return + + theText = theBlock.text() + if len(theText.strip()) == 0: + logger.debug("Empty block selected for action %s" % str(docAction)) + return + + # Remove existing format first, if any + if theText.startswith("@"): + logger.error("Cannot apply block format to keyword/value line") + return + elif theText.startswith("%"): + newText = theText[1:] + cOffset = 1 + elif theText.startswith("# "): + newText = theText[2:] + cOffset = 2 + elif theText.startswith("## "): + newText = theText[3:] + cOffset = 3 + elif theText.startswith("### "): + newText = theText[4:] + cOffset = 4 + elif theText.startswith("#### "): + newText = theText[5:] + cOffset = 5 + else: + newText = theText + cOffset = 0 + + # Apply new format + if docAction == nwDocAction.BLOCK_COM: + theText = "%"+newText + cOffset -= 1 + elif docAction == nwDocAction.BLOCK_H1: + theText = "# "+newText + cOffset -= 2 + elif docAction == nwDocAction.BLOCK_H2: + theText = "## "+newText + cOffset -= 3 + elif docAction == nwDocAction.BLOCK_H3: + theText = "### "+newText + cOffset -= 4 + elif docAction == nwDocAction.BLOCK_H4: + theText = "#### "+newText + cOffset -= 5 + elif docAction == nwDocAction.BLOCK_TXT: + theText = newText + cOffset -= 0 + else: + logger.error("Unknown or unsupported block format requested: %s" % str(docAction)) + return + + # Replace the block text + theCursor.beginEditBlock() + posO = theCursor.position() + theCursor.select(QTextCursor.BlockUnderCursor) + posS = theCursor.selectionStart() + theCursor.removeSelectedText() + theCursor.setPosition(posS) + if posS > 0: + theCursor.insertBlock() + theCursor.insertText(theText) + if posO - cOffset >= 0: + theCursor.setPosition(posO - cOffset) + theCursor.endEditBlock() + self.setTextCursor(theCursor) return diff --git a/nw/gui/mainmenu.py b/nw/gui/mainmenu.py index 39c918f1..679dbacc 100644 --- a/nw/gui/mainmenu.py +++ b/nw/gui/mainmenu.py @@ -618,6 +618,13 @@ class GuiMainMenu(QMenuBar): self.aFmtComment.triggered.connect(lambda: self._docAction(nwDocAction.BLOCK_COM)) self.fmtMenu.addAction(self.aFmtComment) + # Format > Remove Format + self.aFmtNoFormat = QAction("Remove Format", self) + self.aFmtNoFormat.setStatusTip("Strips block format") + self.aFmtNoFormat.setShortcuts(["Ctrl+0","Ctrl+Shift+/"]) + self.aFmtNoFormat.triggered.connect(lambda: self._docAction(nwDocAction.BLOCK_TXT)) + self.fmtMenu.addAction(self.aFmtNoFormat) + return def _buildToolsMenu(self):