feat: render math in reader popup - #1425
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds KaTeX-based LaTeX rendering to the Reader selection popup and introduces stream-safe/throttled overlay updates, aiming to render formulas once delimiters are complete and reduce UI jitter during streaming translation updates.
Changes:
- Added stream-safe LaTeX detection + rendering helpers (with fallback to escaped source on KaTeX failures).
- Reworked Reader popup UI to include a math-rendered overlay layered over the textarea and throttled overlay rendering via
requestAnimationFrame. - Added per-task popup width tracking to reduce repeated shrinking during streaming updates.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/utils/mathRenderer.ts | Adds helpers for determining whether math should render, overlay visibility state, and KaTeX rendering with fallback behavior. |
| src/modules/popup.ts | Introduces the popup math overlay/container, throttled rendering, and task-scoped width tracking to stabilize streaming UI updates. |
| src/elements/mathTextbox.ts | Refactors overlay lifecycle to reuse the overlay element and throttle rendering, using the new shouldRenderMath helper. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const idPrefix = popup?.getAttribute(`${config.addonRef}-prefix`); | ||
| const makeId = (type: string) => `${idPrefix}-${type}`; | ||
| const audiobox = popup?.querySelector( | ||
| `#${makeId("audiobox")}`, | ||
| ) as HTMLDivElement; | ||
| const translateButton = popup?.querySelector( | ||
| `#${makeId("translate")}`, | ||
| ) as HTMLDivElement; | ||
| const textContainer = popup?.querySelector( | ||
| `#${makeId("text-container")}`, | ||
| ) as HTMLDivElement; | ||
| const textarea = popup?.querySelector( | ||
| `#${makeId("text")}`, | ||
| ) as HTMLTextAreaElement; | ||
| const mathOverlay = popup?.querySelector( | ||
| `#${makeId("math-overlay")}`, | ||
| ) as HTMLDivElement; | ||
| const addToNoteButton = popup?.querySelector( | ||
| `#${makeId("addtonote")}`, | ||
| ) as HTMLDivElement; | ||
|
|
||
| const updateHidden = (elem: HTMLElement, hidden: boolean) => { | ||
| if (hidden) { | ||
| elem.style.display = "none"; | ||
| } else { | ||
| elem.style.removeProperty("display"); | ||
| } | ||
| }; | ||
|
|
||
| if (!enablePopup) { | ||
| cancelPopupMathOverlayRender(mathOverlay); | ||
| mathOverlay.innerHTML = ""; | ||
| updateHidden(audiobox, true); | ||
| updateHidden(translateButton, true); | ||
| updateHidden(textContainer, true); | ||
| updateHidden(textarea, true); | ||
| updateHidden(mathOverlay, true); | ||
| updateHidden(addToNoteButton, true); |
| function syncPopupTextContainer( | ||
| container: HTMLDivElement, | ||
| textarea: HTMLTextAreaElement, | ||
| ): void { | ||
| container.hidden = textarea.hidden; | ||
| container.style.width = textarea.style.width; | ||
| container.style.height = textarea.style.height; | ||
| } |
There was a problem hiding this comment.
@copilot apply changes based on this feedback. Please use ResizeObserver.
| const popupMathOverlayFrames = new WeakMap<HTMLDivElement, number>(); | ||
| const popupTaskMaxWidths = new Map<string, number>(); |
|
@Golden-Pigeon npm run lintand push the results? Thanks! |
1 similar comment
|
@Golden-Pigeon npm run lintand push the results? Thanks! |
c741e8b to
81787cc
Compare
|
@Golden-Pigeon npm run lintand push the results? Thanks! |
|
@copilot apply changes based on the comments in this thread |
|
@Golden-Pigeon npm run lintand push the results? Thanks! |
|
Updated the Reader popup math rendering behavior to fix the height mismatch issue. Changes included:
With this, the popup still uses the textarea measurement for plain/raw text, but switches to rendered-content measurement when the math overlay |
|
thanks. this will be pended due to planned refactor of the plugin |
6760690 to
44f081e
Compare
|
This feature is good. I'm looking forward to this feature update! |
Summary
This PR adds LaTeX formula rendering support to the Reader selection popup, addressing #1076 (comment).
Changes
$...$,$$...$$,\(...\), and\[...\].Known Issues
The Reader popup height may not fit the translated text height exactly.(fixed in latest commits)