feat(前端界面): 优化按比例缩放
build.yaml / build (push) Successful in 33s

This commit is contained in:
2026-07-09 14:22:17 +08:00
parent 89577ea72f
commit 7ff1a4269a
+32
View File
@@ -2009,6 +2009,7 @@ function applyFit(updateStatus = true) {
} }
function setScale(nextScale, updateStatus = true, fitMode = "free") { function setScale(nextScale, updateStatus = true, fitMode = "free") {
const anchor = updateStatus && fitMode === "free" ? scaleAnchor() : null;
const scale = Math.min(4, Math.max(0.2, nextScale)); const scale = Math.min(4, Math.max(0.2, nextScale));
const scaleChanged = Math.abs(scale - state.scale) > 0.001; const scaleChanged = Math.abs(scale - state.scale) > 0.001;
state.fitMode = fitMode; state.fitMode = fitMode;
@@ -2018,6 +2019,9 @@ function setScale(nextScale, updateStatus = true, fitMode = "free") {
layoutPages(); layoutPages();
} }
updateFitSpace(); updateFitSpace();
if (scaleChanged) {
restoreScaleAnchor(anchor);
}
el.zoomLabel.textContent = `${Math.round(state.scale * 100)}%`; el.zoomLabel.textContent = `${Math.round(state.scale * 100)}%`;
if (updateStatus && state.doc) { if (updateStatus && state.doc) {
setStatus(`${state.pageIndex + 1} / ${state.doc.pageCount}`); setStatus(`${state.pageIndex + 1} / ${state.doc.pageCount}`);
@@ -2025,6 +2029,34 @@ function setScale(nextScale, updateStatus = true, fitMode = "free") {
updateControls(); updateControls();
} }
function scaleAnchor() {
const shell = pageShellFromView() || pageShell(state.pageIndex);
if (!shell) {
return null;
}
const viewerRect = el.viewerPanel.getBoundingClientRect();
const shellRect = shell.getBoundingClientRect();
return {
index: Number.parseInt(shell.dataset.pageIndex, 10),
x: (viewerRect.left + viewerRect.width / 2 - shellRect.left) / Math.max(1, shellRect.width),
y: (viewerRect.top + viewerRect.height * 0.45 - shellRect.top) / Math.max(1, shellRect.height),
};
}
function restoreScaleAnchor(anchor) {
if (!anchor) {
return;
}
const shell = pageShell(anchor.index);
if (!shell) {
return;
}
const viewerRect = el.viewerPanel.getBoundingClientRect();
const shellRect = shell.getBoundingClientRect();
el.viewerPanel.scrollLeft += shellRect.left + shellRect.width * anchor.x - viewerRect.left - viewerRect.width / 2;
el.viewerPanel.scrollTop += shellRect.top + shellRect.height * anchor.y - viewerRect.top - viewerRect.height * 0.45;
}
function currentPageInfo() { function currentPageInfo() {
return state.doc?.pages?.[state.pageIndex] || null; return state.doc?.pages?.[state.pageIndex] || null;
} }