Ensure editor content is saved before using it (#1923)

This commit is contained in:
Veronica Berglyd Olsen
2024-06-11 22:12:03 +02:00
committed by GitHub
3 changed files with 19 additions and 7 deletions
+5 -6
View File
@@ -32,10 +32,8 @@ from enum import Enum
from time import time from time import time
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from PyQt5.QtCore import QPoint, QTimer, Qt, pyqtSignal, pyqtSlot from PyQt5.QtCore import QPoint, Qt, QTimer, pyqtSignal, pyqtSlot
from PyQt5.QtGui import ( from PyQt5.QtGui import QDragEnterEvent, QDragMoveEvent, QDropEvent, QIcon, QMouseEvent, QPalette
QDragEnterEvent, QDragMoveEvent, QDropEvent, QIcon, QMouseEvent, QPalette
)
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QAbstractItemView, QAction, QDialog, QFrame, QHBoxLayout, QHeaderView, QAbstractItemView, QAction, QDialog, QFrame, QHBoxLayout, QHeaderView,
QLabel, QMenu, QShortcut, QSizePolicy, QTreeWidget, QTreeWidgetItem, QLabel, QMenu, QShortcut, QSizePolicy, QTreeWidget, QTreeWidgetItem,
@@ -44,14 +42,14 @@ from PyQt5.QtWidgets import (
from novelwriter import CONFIG, SHARED from novelwriter import CONFIG, SHARED
from novelwriter.common import minmax from novelwriter.common import minmax
from novelwriter.constants import nwHeaders, nwUnicode, trConst, nwLabels from novelwriter.constants import nwHeaders, nwLabels, nwUnicode, trConst
from novelwriter.core.coretools import DocDuplicator, DocMerger, DocSplitter from novelwriter.core.coretools import DocDuplicator, DocMerger, DocSplitter
from novelwriter.core.item import NWItem from novelwriter.core.item import NWItem
from novelwriter.dialogs.docmerge import GuiDocMerge from novelwriter.dialogs.docmerge import GuiDocMerge
from novelwriter.dialogs.docsplit import GuiDocSplit from novelwriter.dialogs.docsplit import GuiDocSplit
from novelwriter.dialogs.editlabel import GuiEditLabel from novelwriter.dialogs.editlabel import GuiEditLabel
from novelwriter.dialogs.projectsettings import GuiProjectSettings from novelwriter.dialogs.projectsettings import GuiProjectSettings
from novelwriter.enum import nwDocMode, nwItemType, nwItemClass, nwItemLayout from novelwriter.enum import nwDocMode, nwItemClass, nwItemLayout, nwItemType
from novelwriter.extensions.modified import NIconToolButton from novelwriter.extensions.modified import NIconToolButton
from novelwriter.gui.theme import STYLES_MIN_TOOLBUTTON from novelwriter.gui.theme import STYLES_MIN_TOOLBUTTON
from novelwriter.types import QtAlignLeft, QtAlignRight, QtMouseLeft, QtMouseMiddle, QtUserRole from novelwriter.types import QtAlignLeft, QtAlignRight, QtMouseLeft, QtMouseMiddle, QtUserRole
@@ -1830,6 +1828,7 @@ class _TreeContextMenu(QMenu):
def _itemHeader(self) -> None: def _itemHeader(self) -> None:
"""Check if there is a header that can be used for rename.""" """Check if there is a header that can be used for rename."""
SHARED.ensureEditorSaved(self._handle)
if hItem := SHARED.project.index.getItemHeading(self._handle, "T0001"): if hItem := SHARED.project.index.getItemHeading(self._handle, "T0001"):
action = self.addAction(self.tr("Rename to Heading")) action = self.addAction(self.tr("Rename to Heading"))
action.triggered.connect( action.triggered.connect(
+13
View File
@@ -204,6 +204,19 @@ class SharedData(QObject):
self._resetIdleTimer() self._resetIdleTimer()
return return
def ensureEditorSaved(self, tHandle: str | None) -> None:
"""Ensure that the editor content is saved. Optionally, only if
it is a specific handle.
"""
docEditor = self.mainGui.docEditor
if (
self.hasProject and docEditor.docHandle
and (tHandle is None or tHandle == docEditor.docHandle)
):
logger.debug("Saving editor document before action")
docEditor.saveText()
return
def updateSpellCheckLanguage(self, reload: bool = False) -> None: def updateSpellCheckLanguage(self, reload: bool = False) -> None:
"""Update the active spell check language from settings.""" """Update the active spell check language from settings."""
from novelwriter import CONFIG from novelwriter import CONFIG
+1 -1
View File
@@ -343,7 +343,7 @@ class GuiManuscript(NDialog):
return return
# Make sure editor content is saved before we start # Make sure editor content is saved before we start
SHARED.mainGui.saveDocument() SHARED.ensureEditorSaved(None)
docBuild = NWBuildDocument(SHARED.project, build) docBuild = NWBuildDocument(SHARED.project, build)
docBuild.setPreviewMode(True) docBuild.setPreviewMode(True)