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:
2026-10-29 18:22:42 +08:00
parent fac5cbb011
commit 8dae91c2ca
26 changed files with 2722 additions and 385 deletions
+36 -4
View File
@@ -1,4 +1,5 @@
document.addEventListener("DOMContentLoaded", () => {
window.mdhelperRenderReady = false;
const CODE_LINE_NUMBER_LIMIT = 240;
const CODE_COLLAPSE_LIMIT = 18;
const CODE_AUTO_COLLAPSE_LIMIT = 420;
@@ -56,8 +57,7 @@ document.addEventListener("DOMContentLoaded", () => {
return;
}
if (engine === "mathjax" && window.MathJax && window.MathJax.typesetPromise) {
window.MathJax.typesetPromise([reader]).catch(() => {});
return;
return window.MathJax.typesetPromise([reader]).catch(() => {});
}
if (engine === "katex" && window.renderMathInElement) {
window.renderMathInElement(reader, {
@@ -72,10 +72,12 @@ document.addEventListener("DOMContentLoaded", () => {
}
};
renderMermaidDiagrams();
const diagramsReady = renderMermaidDiagrams();
window.mdhelperSyncMathFontSize = syncMathFontSize;
syncMathFontSize();
renderMath();
Promise.allSettled([diagramsReady, renderMath()]).then(() => {
window.mdhelperRenderReady = true;
});
document.querySelectorAll(".code-block").forEach((block) => {
const code = block.querySelector("pre code");
@@ -445,6 +447,36 @@ document.addEventListener("DOMContentLoaded", () => {
if (window.qt && window.qt.webChannelTransport) {
new QWebChannel(window.qt.webChannelTransport, (channel) => {
bridge = channel.objects.mdhelperBridge || null;
const editor = channel.objects.mdhelperEditor;
if (editor) {
// Only editor previews track user scrolling. Reader windows remain idle.
let intentUntil = 0, pending = null;
const intent = () => { intentUntil = performance.now() + 1200; };
document.addEventListener("wheel", intent, {passive: true});
document.addEventListener("pointerdown", intent, {passive: true});
document.addEventListener("keydown", intent);
window.addEventListener("scroll", () => {
if (pending || performance.now() > intentUntil ||
performance.now() < (window.mdhelperProgrammaticScrollUntil || 0)) return;
pending = setTimeout(() => {
pending = null;
const blocks = [...document.querySelectorAll('[data-source-line]')];
let target = null, best = -Infinity;
for (const element of blocks) {
const rect = element.getBoundingClientRect();
if (rect.height > 0 && rect.top <= 14 && rect.bottom > 12 && rect.top >= best) {
best = rect.top; target = element;
}
}
if (!target) target = blocks.find(e => e.getBoundingClientRect().top >= 0);
if (!target) return;
const rect = target.getBoundingClientRect();
const start = Number(target.dataset.sourceLine), end = Number(target.dataset.sourceEnd);
const fraction = Math.max(0, Math.min(0.999, (12 - rect.top) / Math.max(1, rect.height)));
editor.sourceLine(start + fraction * Math.max(1, end - start));
}, 50);
}, {passive: true});
}
});
}
});