const minimumZoom = 50; const maximumZoom = 250; const zoomStep = 25; function button(label, title) { const node = document.createElement("button"); node.type = "button"; node.title = title; node.setAttribute("aria-label", title); node.textContent = label; return node; } function sameTextPage(previous, next) { return previous?.width === next.width && previous.height === next.height && previous.lines.length === next.lines.length && previous.lines.every((line, index) => { const other = next.lines[index]; return line.text === other.text && line.x === other.x && line.y === other.y && line.width === other.width && line.height === other.height; }); } function centerOfPage(scroller, pages) { const bounds = scroller.getBoundingClientRect(); const x = bounds.left + scroller.clientLeft + scroller.clientWidth / 2; const y = bounds.top + scroller.clientTop + scroller.clientHeight / 2; for (const page of pages.children) { const rect = page.getBoundingClientRect(); if (rect.top > y) break; if (rect.bottom >= y && rect.width && rect.height) { return { page, x, y, u: (x - rect.left) / rect.width, v: (y - rect.top) / rect.height }; } } return null; } export function attachPreviewView(pages) { const scroller = pages.parentElement; const panel = pages.closest(".preview-panel"); pages.classList.add("texrra-preview-pages"); if (panel) pages.classList.add("texrra-preview-web"); const toolbar = document.createElement("div"); toolbar.className = "texrra-preview-controls"; toolbar.setAttribute("role", "group"); toolbar.setAttribute("aria-label", "预览工具"); const zoomOut = button("−", "缩小预览"); const zoomIn = button("+", "放大预览"); const zoomValue = document.createElement("output"); const fit = button("适合宽度", "适合预览区宽度"); toolbar.append(zoomOut, zoomValue, zoomIn, fit); if (panel) scroller.before(toolbar); else { toolbar.classList.add("texrra-preview-sticky"); scroller.prepend(toolbar); } let zoom = 100; let measure; const textByFrame = new WeakMap(); function resize() { if (zoom === 100) { pages.style.removeProperty("--texrra-zoom-width"); return; } const style = getComputedStyle(scroller); const available = scroller.clientWidth - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight); pages.style.setProperty("--texrra-zoom-width", "calc(min(" + Math.max(0, available) + "px, var(--texrra-fit-width)) * " + zoom / 100 + ")"); } function updateControls() { zoomOut.disabled = zoom === minimumZoom; zoomIn.disabled = zoom === maximumZoom; zoomValue.value = zoom + "%"; fit.setAttribute("aria-pressed", String(zoom === 100)); } function setZoom(next) { if (next === zoom || next < minimumZoom || next > maximumZoom) return; const anchor = centerOfPage(scroller, pages); zoom = next; resize(); updateControls(); if (anchor) { const rect = anchor.page.getBoundingClientRect(); scroller.scrollLeft += rect.left + rect.width * anchor.u - anchor.x; scroller.scrollTop += rect.top + rect.height * anchor.v - anchor.y; } } zoomOut.addEventListener("click", () => setZoom(zoom - zoomStep)); zoomIn.addEventListener("click", () => setZoom(zoom + zoomStep)); fit.addEventListener("click", () => setZoom(100)); new ResizeObserver(resize).observe(scroller); updateControls(); function wrap(image, page) { const frame = document.createElement("div"); frame.className = "texrra-preview-page"; frame.dataset.index = page.index; image.draggable = false; frame.append(image); return frame; } function setText(frame, page) { const previous = textByFrame.get(frame); if (sameTextPage(previous, page)) return; frame.querySelector(".texrra-preview-text")?.remove(); textByFrame.set(frame, page); if (page.lines.length === 0 || !(page.width > 0 && page.height > 0)) { frame.classList.remove("texrra-preview-has-text"); return; } if (measure === undefined) { measure = document.createElement("canvas").getContext("2d"); if (measure) measure.font = "100px Arial, sans-serif"; } const layer = document.createElement("div"); layer.className = "texrra-preview-text"; for (const line of page.lines) { if (!line.text || !(line.width > 0 && line.height > 0)) continue; const text = document.createElement("span"); // Absolutely positioned siblings don't create a line break in copied text. text.textContent = line.text + "\n"; text.dir = "auto"; text.style.left = (100 * line.x / page.width) + "%"; text.style.top = (100 * line.y / page.height) + "%"; text.style.fontSize = (100 * line.height / page.width) + "cqw"; const width = (measure?.measureText(line.text).width ?? 0) * line.height / 100; if (width > 0) text.style.transform = "scaleX(" + line.width / width + ")"; layer.append(text); } frame.classList.add("texrra-preview-has-text"); frame.append(layer); } function setTextPages(textPages) { for (const page of textPages) { const frame = pages.children[page.index]; if (frame?.dataset.index === String(page.index)) setText(frame, page); } } return { toolbar, wrap, setTextPages }; }