fix: protect editor content and harden document exports
Protect pending edits on window close, keep save-as paths and drafts consistent, copy relative image resources, reject oversized table imports without truncation, and preserve independent preview scrolling during refresh. Archive rendered MHTML resources with portable references and embedded local fonts. Preserve existing outputs on export cancellation or failure. Convert editor match positions and filter directory results in linear passes. Include the previously completed editor image/table/history improvements, undoable replacement, export snapshots, shared page settings, and cancellable PDF/Word export workflow. Update documentation and version to 0.3.1. Validation: 142 pytest cases passed; GUI smoke, improvements integration, and offline MHTML/scroll reliability smoke checks passed.
This commit is contained in:
+17
-9
@@ -18,12 +18,14 @@ class SearchMatch:
|
||||
class SearchIndex:
|
||||
def __init__(self, entries: list[DocumentEntry] | None = None) -> None:
|
||||
self._records: list[tuple[DocumentEntry, str, str]] = []
|
||||
self._records_by_path: dict[Path, tuple[DocumentEntry, str, str]] = {}
|
||||
self._body_cache: dict[Path, tuple[int, int, str]] = {}
|
||||
if entries:
|
||||
self.rebuild(entries)
|
||||
|
||||
def rebuild(self, entries: list[DocumentEntry]) -> None:
|
||||
self._records = [(entry, entry.title, entry.relative_path) for entry in entries]
|
||||
self._records_by_path = {record[0].path: record for record in self._records}
|
||||
current_paths = {entry.path for entry in entries}
|
||||
self._body_cache = {path: value for path, value in self._body_cache.items() if path in current_paths}
|
||||
|
||||
@@ -32,20 +34,21 @@ class SearchIndex:
|
||||
if not normalized:
|
||||
return True
|
||||
|
||||
for indexed_entry, title, relative_path in self._records:
|
||||
if indexed_entry.path == entry.path:
|
||||
if normalized in self._haystack(title, relative_path, "").casefold():
|
||||
return True
|
||||
return normalized in self._body_for(indexed_entry).casefold()
|
||||
if normalized in self._haystack(entry.title, entry.relative_path, "").casefold():
|
||||
entry, title, relative_path = self._records_by_path.get(entry.path, (entry, entry.title, entry.relative_path))
|
||||
if normalized in self._haystack(title, relative_path, "").casefold():
|
||||
return True
|
||||
return normalized in self._body_for(entry).casefold()
|
||||
|
||||
def search(self, query: str, limit: int = 80) -> list[SearchMatch]:
|
||||
return self.filter(query, limit, all_paths=False)[1]
|
||||
|
||||
def filter(self, query: str, limit: int = 80, *, all_paths: bool = True) -> tuple[set[Path], list[SearchMatch]]:
|
||||
"""Find tree visibility and bounded result details without scanning twice."""
|
||||
normalized = query.strip().casefold()
|
||||
if not normalized:
|
||||
return []
|
||||
return ({entry.path for entry, _, _ in self._records} if all_paths else set()), []
|
||||
|
||||
visible: set[Path] = set()
|
||||
matches: list[SearchMatch] = []
|
||||
for entry, title, relative_path in self._records:
|
||||
result = _match_details(title, relative_path, "", normalized)
|
||||
@@ -54,6 +57,11 @@ class SearchIndex:
|
||||
if result is None:
|
||||
continue
|
||||
|
||||
visible.add(entry.path)
|
||||
if len(matches) >= limit:
|
||||
if not all_paths:
|
||||
break
|
||||
continue
|
||||
snippet, match_count, match_kind = result
|
||||
matches.append(
|
||||
SearchMatch(
|
||||
@@ -63,9 +71,9 @@ class SearchIndex:
|
||||
match_kind=match_kind,
|
||||
)
|
||||
)
|
||||
if len(matches) >= limit:
|
||||
if not all_paths and len(matches) >= limit:
|
||||
break
|
||||
return matches
|
||||
return visible, matches
|
||||
|
||||
def _haystack(self, title: str, relative_path: str, body: str) -> str:
|
||||
return f"{title}\n{relative_path}\n{body}"
|
||||
|
||||
Reference in New Issue
Block a user