refactor: unify document sessions close policy and resource parsing

This commit is contained in:
2026-10-29 19:18:19 +08:00
parent de362536f5
commit f27482857a
11 changed files with 519 additions and 305 deletions
+28 -9
View File
@@ -1,4 +1,5 @@
from __future__ import annotations
from .document_session import DocumentSession
import os
import json
@@ -277,8 +278,8 @@ class MarkdownEditorDialog(QDialog):
self.embedded = embedded
if embedded:
self.setWindowFlag(Qt.WindowType.Widget, True)
self.path = path
self.save_target_path = path
self.session = DocumentSession(path.resolve(), source if original_source is None else original_source, file_signature)
self._save_as_target: Path | None = None
self.renderer = renderer
self.theme = theme
self.font_scale = font_scale
@@ -287,7 +288,6 @@ class MarkdownEditorDialog(QDialog):
self.document_width_scale = document_width_scale
self.document_style_css = document_style_css
self.math_engine = math_engine
self.file_signature = file_signature
self.draft_callback = draft_callback
self._preview_dirty = True
self._preview_scroll_y = 0
@@ -297,7 +297,6 @@ class MarkdownEditorDialog(QDialog):
self._current_match_index = -1
self.setWindowTitle(f"编辑 Markdown - {path.name}")
self.setMinimumSize(1120, 720)
self._original_source = source if original_source is None else original_source
layout = QVBoxLayout(self)
layout.setContentsMargins(12, 12, 12, 12)
@@ -440,11 +439,31 @@ class MarkdownEditorDialog(QDialog):
self._rebuild_find_matches()
self._apply_view_mode()
@property
def path(self) -> Path:
return self.session.path
@property
def file_signature(self):
return self.session.signature
@property
def save_target_path(self) -> Path:
return self._save_as_target or self.session.path
@save_target_path.setter
def save_target_path(self, path: Path) -> None:
self._save_as_target = path
def stop_background_work(self) -> None:
self.draft_save_timer.stop()
self.preview_refresh_timer.stop()
def source_text(self) -> str:
return self.editor.toPlainText()
def has_unsaved_changes(self) -> bool:
return self.source_text() != self._original_source
return self.session.is_modified(self.source_text())
def apply_theme(self, dark: bool) -> None:
self.editor.apply_theme(dark)
@@ -1008,17 +1027,17 @@ class MarkdownEditorDialog(QDialog):
self.save_requested.emit(self.save_target_path, self.source_text(), self.file_signature)
def mark_saved(self, path: Path, source: str, file_signature: tuple[int, int] | None) -> None:
self.path = path
self.save_target_path = path
self.file_signature = file_signature
self.session.mark_saved(path, source, file_signature)
self._save_as_target = None
if source != self.source_text():
self.replace_source_undoable(source)
self._original_source = source
self.draft_save_timer.stop()
self._preview_dirty = True
self._update_dirty_title()
def confirm_close(self) -> bool:
if self.embedded and callable(getattr(self, "close_confirmation", None)):
return self.close_confirmation([self])
if not self.has_unsaved_changes():
return True
choice = QMessageBox.question(