Repository navigation
fix: populate the table of contents on open and fix link navigation - #4448
Conversation
|
@NSO73 Pls solve this conflicts |
The @muyajs/core migration broke table-of-contents generation (see #4441): the sidebar TOC stayed empty until the first edit, and clicking a TOC entry did not scroll to the heading (the click-to-navigate behaviour requested in - Seed the TOC from the engine on file load and on tab switch. The engine emits no json-change in those paths, so listToc/toc remained empty until a document change occurred. - Resolve TOC link clicks by heading order. @muyajs/core slugs are stable per-block ids that are not stamped onto the heading DOM, so a `#slug` selector never matched; walk the headings in document order and map the slug to its index instead. Refs #2992 Refs #4441
|
Done — conflicts resolved as requested, following The only conflict was in The PR is now MERGEABLE and |
ReviewI verified both bugs against Bugs confirmed
The seeding fix (4 entry points: mount, Main concern: index mapping misaligns on nested / raw-HTML headings
If any such heading precedes the target, the DOM index is shifted relative to the Suggested fixes, most robust first:
Minor
Test coverageThe PR cites the e2e TOC smoke test "added in #4441", but #4441 is still open and not merged into VerdictThe seeding half is solid. The navigation half works for typical documents but mis-scrolls when nested or raw-HTML headings are present — I'd prefer the DOM- |
Address review on #4448: scrollToHeader resolved a slug to its index in listToc, then indexed querySelectorAll('h1..h6'). getTOC only walks top-level scrollPage children (no recursion), so headings nested in blockquotes/list items or inside raw-HTML blocks were counted by the DOM but not by getTOC, shifting every later index and scrolling to the wrong heading. Scope the DOM query to direct-child headings (:scope > h1..h6) so the DOM set matches getTOC's set by construction. Extract the resolution into a pure resolveTocHeadingElement (util/tocNavigation.ts) so it is unit testable. Also from the review: - extract a shared scrollElementIntoView(el) helper, de-duping the getBoundingClientRect + animatedScrollTo math between scrollToHeader and scrollToElement - reword the UPDATE_TOC JSDoc: it is an unconditional re-seed on load/switch, drop the inaccurate deep-equal 'subset' rationale - add toc-navigation.spec.ts covering UPDATE_TOC (seed + unconditional re-seed) and the slug->index resolution, pinning the nested / raw-HTML heading edge case (no longer relying on the unmerged #4441 e2e) Refs #4448
|
Thanks for the thorough review — both bugs and the index-mapping concern are spot on. I've pushed a follow-up commit. On the navigation mapping — I went with the top-level-scoped query rather than the DOM-id approach, and want to be transparent about why, because I think the "aligned with #4412" basis doesn't actually hold yet. I checked: Stamping the slug onto the live DOM properly would require, concretely:
That's a genuinely nice unification (live navigation + export anchors on one slug scheme), but it's a cross-package change with its own dedup/collision test surface, and it really belongs with the #4412/#4441 lineage rather than a regression fix. So this PR takes your second suggestion: scope the DOM query to direct-child headings of the container ( Is the scoped-query approach acceptable to you for this PR, with the DOM-id unification tracked as follow-up work alongside #4412/#4441? Happy to do the engine-side id approach instead if you'd rather block on it — just wanted to surface the actual cost before committing to it. Other review items addressed in the same commit:
|
Runtime verification (opening a doc and clicking a TOC entry) surfaced that the previous ':scope > hN' selector matched zero headings: the scroll container returned by getScrollContainer() is muya's root (.mu-editor), which WRAPS the scrollPage root (.mu-container) whose direct children are the top-level blocks. The headings are one level deeper than ':scope >' looked, so the click resolved to nothing and the editor never scrolled. Anchor the selector on '.mu-container > hN' so it matches the top-level headings wherever .mu-container sits under the scroll container, while still excluding nested (blockquote/list) and raw-HTML headings. Update the unit fixture to mirror the real .mu-editor > .mu-container > heading nesting so a regression to a scroller-scoped selector is caught. Verified in-app: TOC seeds with the 3 top-level headings on open, and clicking 'Top Two' scrolls it to the target offset (y=320) past the interleaved blockquote/raw-HTML headings. Refs #4448
|
Small correction + verification follow-up on the navigation fix. I verified the change by actually running the app (open a doc, click a TOC entry) and the first version of the scoped selector was wrong: Fixed by anchoring the selector on Verified in-app on a doc that interleaves a blockquote
The unit fixture in Branch is also synced with |
Catch up the fork with upstream's recent work: muya engine refactors (selection-API rework, private `_`-prefix members, paste/clipboard re-audit, complexity reduction, quick-insert refactor), desktop fixes (marktext#4448 TOC-on-open + link nav, folder-search caret reveal, screenshot paste), Turkish locale, and the configurable PlantUML server URL. Conflict resolution (22 files): - Adopted upstream's superseding refactors: paste re-audit, quick-insert buildReplacementBlock (carries text → subsumes #7), createTable keep/replace, selection-API shape, `_muya`/`_apply` renames, focus-restore (subsumes #2), UPDATE_TOC (subsumes #19). - Preserved fork-only features on top of the refactors: blank-line round-trip (preserveEmptyLines + heading recovery + fence-info `meta.info`), PDF rasterisation (now lazy-imports pdfjs-dist so the engine import stays Node-safe and DOMMatrix no longer breaks tests), `_extendSelectionAcrossBlocks`, `flushPendingChanges`, Tauri renderer bootstrap, fresh-doc caret (#15/#16), and whole-line paste (df174e6 — re-applied over upstream's paste, real-engine spec rewritten for the new selection shape). - Locales: took upstream's restructured files + re-applied the Remarks rebrand (value-only MarkText→Remarks). Regenerated pnpm-lock.yaml. Verified: muya typecheck/lint/circular clean, unit 1125, CommonMark+GFM 1347; desktop typecheck clean, unit 571. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Refs #2992, #4441
Summary
The @muyajs/core migration broke table-of-contents generation (acknowledged in #4441): the sidebar TOC stayed empty until the first edit, and clicking a TOC entry did not scroll to the heading (the click-to-navigate behaviour requested in #2992). This restores both:
json-changein those paths, solistToc/tocstayed empty until a document change occurred.#slugselector never matched. Headings are now walked in document order and the slug mapped to its index.Type of change
Test plan
Notes for reviewers
The slug→index mapping mirrors
getTOC(), which walks headings in document order. If heading ids get stamped onto the DOM later (cf. #4412 for export), the#slugpath could be restored.By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.
fix_toc-compressed.mp4