Skip to content

fix(pm-adapter): plumb body section direction context through resolver#3199

Open
caio-pizzol wants to merge 2 commits intomainfrom
caio/sd-2768-section-direction-context-plumbing
Open

fix(pm-adapter): plumb body section direction context through resolver#3199
caio-pizzol wants to merge 2 commits intomainfrom
caio/sd-2768-section-direction-context-plumbing

Conversation

@caio-pizzol
Copy link
Copy Markdown
Contributor

Follow-up to #3184. The resolver chain that PR landed builds `sectionContext` from `undefined` at the paragraph call site, so `directionContext.writingMode` is always `'horizontal-tb'` even when the body's `w:sectPr` declares a vertical writing-mode. ECMA-376 §17.3.1.41 specifies that paragraph `w:textDirection` inherits from the parent section when omitted; the data contract was incomplete.

What changes

  • Adds `sectionDirectionContext?: SectionDirectionContext` to `ConverterContext`.
  • Populates it once at top-level conversion from the body `sectPr` (where the inline-direction signal was already being resolved).
  • The paragraph resolver now reads `converterContext?.sectionDirectionContext` before falling back to the default.
  • New tests prove writing-mode inheritance from body section context, and that paragraph `w:textDirection` still wins as an explicit override.

What it does NOT do

Two known gaps remain, both documented inline and tracked under SD-2777:

  1. Per-paragraph `sectPr` variation. Multi-section documents where each section has its own `sectPr` with a vertical writing-mode are not yet covered; the body sectPr is the single source.
  2. Cell direction context. Paragraphs in vertical table cells will still see `writingMode: 'horizontal-tb'` until the table converter computes and propagates a `CellDirectionContext`.

Why now

No consumer currently reads `directionContext.writingMode` in production code (grep confirms). Fixing the data contract before the first consumer arrives prevents downstream code from being written against the wrong default.

Validation

  • 1,789 pm-adapter tests pass (+2 new section-inheritance tests)
  • 12,644 super-editor tests pass
  • 1,201 layout-bridge tests pass

Per ECMA §17.3.1.41, paragraph w:textDirection inherits from the parent
section when omitted. The previous call site built sectionContext from
`undefined`, so directionContext.writingMode was always 'horizontal-tb'
even when the body's w:sectPr declared a vertical writing-mode.

This wires SectionDirectionContext through ConverterContext, populated
once at top-level conversion from the body sectPr. Paragraphs that omit
their own w:textDirection now correctly inherit writing-mode.

Scope:
- Body-level sectPr only. Per-paragraph-section variation (each section
  with its own sectPr) and table-cell direction context are not yet
  plumbed through. Both gaps are documented inline and tracked under
  SD-2777 (migrate remaining direction-aware consumers).
- No consumer currently reads directionContext.writingMode in production,
  so this fixes the data contract before the first consumer arrives.

Tests:
- New: paragraph inherits body sectionDirectionContext.writingMode
- New: paragraph w:textDirection still wins as explicit override
@linear
Copy link
Copy Markdown

linear Bot commented May 7, 2026

SD-2768

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@caio-pizzol caio-pizzol marked this pull request as ready for review May 7, 2026 21:28
@caio-pizzol caio-pizzol requested a review from a team as a code owner May 7, 2026 21:28
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7ef0198d01

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/layout-engine/pm-adapter/src/internal.ts Outdated
Codex finding on PR #3199: when callers reuse one ConverterContext across
documents (toFlowBlocksMap does this), the previous `??` cache let the first
document's body sectPr resolve once and stick. A vertical doc 1 followed by
a horizontal doc 2 would have doc 2's paragraphs inherit doc 1's
writing-mode.

Fix: drop the `??` and always overwrite. The shared ConverterContext is
mutated freshly each call before children read it, so per-document
recomputation is enough. (The pre-existing `sectionDirection` field on the
line above has the same pattern but is out of scope for this PR.)

Test added: toFlowBlocksMap with two docs (vertical w:textDirection then
horizontal w:textDirection) sharing one converterContext - asserts each
doc's paragraphs get their own writingMode. Failed before the fix because
the cached vertical-rl persisted into the horizontal doc; passes after.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants