Update template title when creating file from template

This commit is contained in:
Veronica Berglyd Olsen
2025-07-17 15:31:03 +02:00
parent 133d3c7151
commit 20709145c8
2 changed files with 10 additions and 2 deletions
+9 -1
View File
@@ -224,7 +224,7 @@ class NWProject:
return True return True
def copyFileContent(self, tHandle: str, sHandle: str) -> bool: def copyFileContent(self, tHandle: str, sHandle: str, newTitle: str | None = None) -> bool:
"""Copy content to a new document after it is created. This """Copy content to a new document after it is created. This
will not run if the file exists and is not empty. will not run if the file exists and is not empty.
""" """
@@ -239,6 +239,14 @@ class NWProject:
logger.debug("Populating '%s' with text from '%s'", tHandle, sHandle) logger.debug("Populating '%s' with text from '%s'", tHandle, sHandle)
text = self._storage.getDocumentText(sHandle) text = self._storage.getDocumentText(sHandle)
if (
newTitle and (lines := text.split("\n")) and lines
and lines[0].startswith(("# ", "## ", "### ", "#### ", "#! ", "##! ", "###! "))
):
prefix, _, _ = lines[0].partition(" ")
lines[0] = f"{prefix} {newTitle}"
text = "\n".join(lines)
self._storage.getDocument(tHandle).writeDocument(text) self._storage.getDocument(tHandle).writeDocument(text)
sItem.setLayout(tItem.itemLayout) sItem.setLayout(tItem.itemLayout)
self._index.reIndexHandle(tHandle) self._index.reIndexHandle(tHandle)
+1 -1
View File
@@ -667,7 +667,7 @@ class GuiProjectTree(QTreeView):
if itemType == nwItemType.FILE: if itemType == nwItemType.FILE:
if tHandle := SHARED.project.newFile(newLabel, sHandle, sPos): if tHandle := SHARED.project.newFile(newLabel, sHandle, sPos):
if copyDoc: if copyDoc:
SHARED.project.copyFileContent(tHandle, copyDoc) SHARED.project.copyFileContent(tHandle, copyDoc, newLabel)
elif hLevel > 0: elif hLevel > 0:
SHARED.project.writeNewFile(tHandle, hLevel, not nNote) SHARED.project.writeNewFile(tHandle, hLevel, not nNote)
SHARED.project.index.reIndexHandle(tHandle) SHARED.project.index.reIndexHandle(tHandle)