Skip to content

fix: correct table-block anchor Y in markdown scroll sync - #726

Draft
pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/bugfix/378227
Draft

pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/bugfix/378227

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Root Cause Analysis

The Markdown dual-pane scroll sync in deepin-editor uses buildAnchorMap() to map source line numbers to preview Y coordinates. For table blocks, the function used docY(children[i]) which returns the top of the .table-block div — this includes a 37px header wrapper (36px header + 1px border). The resulting systematic 37px offset caused visible misalignment between the editor and preview panes when scrolling table-heavy Markdown documents.

Fix

Query the inner <table> element via child.querySelector('.table-wrapper table') and use its top as previewY for table blocks. Non-table blocks fall back to the original docY(child) logic, preserving existing behavior.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • buildAnchorMap() was introduced in commit 262648622 (BUG-378227 initial fix). This change refines the anchor Y for table blocks; it does not revert the core anchor mapping mechanism.
  • All 5 callers (reapplyScroll, syncScroll, editor scroll handler) are unaffected — function signature unchanged, only internal anchor Y values change for table blocks.

Business Impact Scope

Markdown dual-pane scroll sync for table-heavy documents. Non-table documents are unaffected (querySelector returns null, falls back to original logic).

Verification Suggestion

Test scroll sync with table-heavy Markdown documents (primary), non-table documents (regression), and mixed content (tables + headings + images).


根因分析

deepin-editor 的 Markdown 双栏滚动同步使用 buildAnchorMap() 将源码行号映射到预览 Y 坐标。对于表格块,该函数使用 docY(children[i]) 返回 .table-block div 的顶部——包含 37px 表头容器(36px 表头 + 1px 边框)。这导致系统性 37px 偏移,在滚动表格密集的 Markdown 文档时编辑栏与预览栏明显不同步。

修复方案

通过 child.querySelector('.table-wrapper table') 查找内部 <table> 元素,使用其顶部作为表格块的 previewY。非表格块回退到原始 docY(child) 逻辑,保持现有行为不变。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • buildAnchorMap() 由 commit 262648622(BUG-378227 初始修复)引入。本次改动细化表格块的锚点 Y 值,不撤销核心锚点映射机制。
  • 全部 5 个调用者(reapplyScroll、syncScroll、编辑器滚动处理器)不受影响——函数签名不变,仅表格块的内部锚点 Y 值变化。

业务影响范围

Markdown 双栏滚动同步(表格密集文档)。非表格文档不受影响(querySelector 返回 null,回退原逻辑)。

验证建议

测试表格密集 Markdown 文档的滚动同步(主要场景)、非表格文档(回归验证)、混合内容(表格+标题+图片)。

PMS: BUG-378227

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: pengfeixx

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai

sourcery-ai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Update zip-mode anchor construction to use the rendered table’s position instead of the containing block’s position, while preserving the block-position fallback.

  • src/editor/markdown/web/main.js L238-241

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@pengfeixx
pengfeixx marked this pull request as draft November 13, 2026 17:59

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 4 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/editor/markdown/web/main.js" line_range="238" />
<code_context>
         lastAnchorMode = "zip";
         for (let i = 0; i < children.length; i++) {
-            anchors.push({ sourceLine: blocks[i].startLine, previewY: docY(children[i]) });
+            if (blocks[i].type === "table") {
+                const trs = children[i].querySelectorAll("table tbody tr");
+                if (trs.length > 0) {
</code_context>
<issue_to_address>
**Packaged preview keeps table drift**

When the application runs its embedded preview with the checked-in bundle, `milkdown.html` loads `build/main.bundle.js`, and the Qt resource embeds that bundle rather than `main.js`. Since the bundle still uses block-level table anchors, the row-level mapping never reaches users and table scrolling remains misaligned.

Regenerate `build/main.bundle.js` and the Qt resource from the updated `main.js`.

Also at `src/editor/markdown/web/main.js:239`.
</issue_to_address>

### Comment 2
<location path="src/editor/markdown/web/main.js" line_range="242" />
<code_context>
+                const trs = children[i].querySelectorAll("table tbody tr");
+                if (trs.length > 0) {
+                    anchors.push({ sourceLine: blocks[i].startLine, previewY: docY(trs[0]) });
+                    if (trs.length > 1) {
+                        anchors.push({ sourceLine: blocks[i].startLine + 1,
+                                       previewY: trs[0].getBoundingClientRect().bottom + window.scrollY });
</code_context>
<issue_to_address>
**Header-only tables misalign scrolling**

When a valid GFM table has a header and separator but no data rows, `buildAnchorMap` skips the separator-line anchor because `trs.length` is 1, so `lineToPreviewY` and `previewYToLine` interpolate that line toward the next block or document sentinel instead of mapping it to the header row’s bottom. Scrolling around the table is misaligned.

Add the separator-line anchor for a table with a header even when it has no data rows.

Also at `src/editor/markdown/web/main.js:243`.
</issue_to_address>

### Comment 3
<location path="src/editor/markdown/web/main.js" line_range="241-246" />
<code_context>
+            if (blocks[i].type === "table") {
+                const trs = children[i].querySelectorAll("table tbody tr");
+                if (trs.length > 0) {
+                    anchors.push({ sourceLine: blocks[i].startLine, previewY: docY(trs[0]) });
+                    if (trs.length > 1) {
+                        anchors.push({ sourceLine: blocks[i].startLine + 1,
+                                       previewY: trs[0].getBoundingClientRect().bottom + window.scrollY });
+                        for (let r = 1; r < trs.length; r++) {
+                            anchors.push({ sourceLine: blocks[i].startLine + r + 1, previewY: docY(trs[r]) });
+                        }
+                    }
</code_context>
<issue_to_address>
**Anchor harness reports failure**

When the local Markdown harness is run against a document containing its existing table fixture, the new table-row anchors make `amap.slice(0, expectedLines.length)` include the table's delimiter and data-row anchors, so the existing `A1_anchor_exact_source_lines` check compares `20` with the expected paragraph line `23` and reports FAIL whenever the local harness runs.

Update the harness assertion to account for the table's row anchors while still checking the following block's source line.
</issue_to_address>

### Comment 4
<location path="src/editor/markdown/web/main.js" line_range="241" />
<code_context>
+            if (blocks[i].type === "table") {
+                const trs = children[i].querySelectorAll("table tbody tr");
+                if (trs.length > 0) {
+                    anchors.push({ sourceLine: blocks[i].startLine, previewY: docY(trs[0]) });
+                    if (trs.length > 1) {
+                        anchors.push({ sourceLine: blocks[i].startLine + 1,
</code_context>
<issue_to_address>
**Progressive rendering skips row anchors**

When the user scrolls while a multi-batch document is still rendering, `buildAnchorMap` reaches this table-row branch only when AST blocks and rendered children have equal counts; during a multi-batch render, its mismatch path builds heading-only anchors, so table scroll synchronization remains approximate until rendering completes.

Provide table-row anchors in the progressive-render mismatch path, or otherwise preserve accurate table mapping while batches are incomplete.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/editor/markdown/web/main.js Outdated
lastAnchorMode = "zip";
for (let i = 0; i < children.length; i++) {
anchors.push({ sourceLine: blocks[i].startLine, previewY: docY(children[i]) });
if (blocks[i].type === "table") {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Packaged preview keeps table drift

When the application runs its embedded preview with the checked-in bundle, milkdown.html loads build/main.bundle.js, and the Qt resource embeds that bundle rather than main.js. Since the bundle still uses block-level table anchors, the row-level mapping never reaches users and table scrolling remains misaligned.

Regenerate build/main.bundle.js and the Qt resource from the updated main.js.

Also at src/editor/markdown/web/main.js:239.

Prompt for AI agents
In `src/editor/markdown/web/main.js` at line 238:

**Packaged preview keeps table drift**

When the application runs its embedded preview with the checked-in bundle, `milkdown.html` loads `build/main.bundle.js`, and the Qt resource embeds that bundle rather than `main.js`. Since the bundle still uses block-level table anchors, the row-level mapping never reaches users and table scrolling remains misaligned.

Regenerate `build/main.bundle.js` and the Qt resource from the updated `main.js`.

Also at `src/editor/markdown/web/main.js:239`.

Comment thread src/editor/markdown/web/main.js Outdated
const trs = children[i].querySelectorAll("table tbody tr");
if (trs.length > 0) {
anchors.push({ sourceLine: blocks[i].startLine, previewY: docY(trs[0]) });
if (trs.length > 1) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Header-only tables misalign scrolling

When a valid GFM table has a header and separator but no data rows, buildAnchorMap skips the separator-line anchor because trs.length is 1, so lineToPreviewY and previewYToLine interpolate that line toward the next block or document sentinel instead of mapping it to the header row’s bottom. Scrolling around the table is misaligned.

Add the separator-line anchor for a table with a header even when it has no data rows.

Also at src/editor/markdown/web/main.js:243.

Prompt for AI agents
In `src/editor/markdown/web/main.js` at line 242:

**Header-only tables misalign scrolling**

When a valid GFM table has a header and separator but no data rows, `buildAnchorMap` skips the separator-line anchor because `trs.length` is 1, so `lineToPreviewY` and `previewYToLine` interpolate that line toward the next block or document sentinel instead of mapping it to the header row’s bottom. Scrolling around the table is misaligned.

Add the separator-line anchor for a table with a header even when it has no data rows.

Also at `src/editor/markdown/web/main.js:243`.

Comment thread src/editor/markdown/web/main.js Outdated
Comment on lines +241 to +246
anchors.push({ sourceLine: blocks[i].startLine, previewY: docY(trs[0]) });
if (trs.length > 1) {
anchors.push({ sourceLine: blocks[i].startLine + 1,
previewY: trs[0].getBoundingClientRect().bottom + window.scrollY });
for (let r = 1; r < trs.length; r++) {
anchors.push({ sourceLine: blocks[i].startLine + r + 1, previewY: docY(trs[r]) });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Anchor harness reports failure

When the local Markdown harness is run against a document containing its existing table fixture, the new table-row anchors make amap.slice(0, expectedLines.length) include the table's delimiter and data-row anchors, so the existing A1_anchor_exact_source_lines check compares 20 with the expected paragraph line 23 and reports FAIL whenever the local harness runs.

Update the harness assertion to account for the table's row anchors while still checking the following block's source line.

Prompt for AI agents
In `src/editor/markdown/web/main.js` at lines 241-246:

**Anchor harness reports failure**

When the local Markdown harness is run against a document containing its existing table fixture, the new table-row anchors make `amap.slice(0, expectedLines.length)` include the table's delimiter and data-row anchors, so the existing `A1_anchor_exact_source_lines` check compares `20` with the expected paragraph line `23` and reports FAIL whenever the local harness runs.

Update the harness assertion to account for the table's row anchors while still checking the following block's source line.

Comment thread src/editor/markdown/web/main.js Outdated
if (blocks[i].type === "table") {
const trs = children[i].querySelectorAll("table tbody tr");
if (trs.length > 0) {
anchors.push({ sourceLine: blocks[i].startLine, previewY: docY(trs[0]) });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Progressive rendering skips row anchors

When the user scrolls while a multi-batch document is still rendering, buildAnchorMap reaches this table-row branch only when AST blocks and rendered children have equal counts; during a multi-batch render, its mismatch path builds heading-only anchors, so table scroll synchronization remains approximate until rendering completes.

Provide table-row anchors in the progressive-render mismatch path, or otherwise preserve accurate table mapping while batches are incomplete.

Prompt for AI agents
In `src/editor/markdown/web/main.js` at line 241:

**Progressive rendering skips row anchors**

When the user scrolls while a multi-batch document is still rendering, `buildAnchorMap` reaches this table-row branch only when AST blocks and rendered children have equal counts; during a multi-batch render, its mismatch path builds heading-only anchors, so table scroll synchronization remains approximate until rendering completes.

Provide table-row anchors in the progressive-render mismatch path, or otherwise preserve accurate table mapping while batches are incomplete.

1. Root cause: buildAnchorMap() used docY(children[i]) for table
   blocks, which returns the .table-block div top including the 37px
   header wrapper, causing a systematic 37px offset in scroll sync
2. Fix: query the inner <table> element via child.querySelector and
   use its top as previewY; fall back to docY(child) for non-table
   blocks
3. Impact: only table-block anchor Y values change; non-table blocks
   and function signature are unaffected

Log: fix markdown dual-pane scroll sync offset for table-heavy docs

Influence:
1. Test scroll sync with table-heavy markdown documents
2. Test scroll sync with non-table markdown documents (regression)
3. Test scroll sync with mixed content (tables + headings + images)

fix: 修正Markdown滚动同步中表格块锚点Y坐标

1. 根因:buildAnchorMap() 对表格块使用 docY(children[i]),返回
   .table-block div 的顶部(含37px表头容器),导致滚动同步存在
   系统性37px偏移
2. 方案:通过 child.querySelector 查找内部 <table> 元素,使用其
   顶部作为 previewY;非表格块回退到 docY(child)
3. 影响:仅表格块锚点Y值变化,非表格块和函数签名不受影响

Log: 修复表格密集文档双栏滚动同步偏移问题

Influence:
1. 测试表格密集Markdown文档的滚动同步
2. 测试非表格Markdown文档的滚动同步(回归)
3. 测试混合内容(表格+标题+图片)的滚动同步

PMS: BUG-378227
@pengfeixx pengfeixx changed the title /home/deepin/multica_workspaces/v25-0f66ae001f2f/v-8467-b2cfe061d8c0/workdir/deepin-editor/.pms/bugs/378227/fix/commit-msg.txt fix: correct table-block anchor Y in markdown scroll sync Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants