-
Notifications
You must be signed in to change notification settings - Fork 139
fix: align rtl toolbar state and alignment writes with ooxml bidi #3244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
66fe33d
fix: align rtl toolbar state and alignment writes with ooxml bidi
60da67d
test(rtl-alignment): cover helper roundtrip + toolbar writer + doc-ap…
caio-pizzol c93db70
test(rtl-alignment): cover section-bidi-only edge case (ECMA-376 §17.…
caio-pizzol 2880cba
test(rtl-alignment): cover headings, tables, multi-paragraph selectio…
caio-pizzol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
packages/super-editor/src/editors/v1/core/helpers/paragraph-alignment.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| * Maps display alignment (UI-facing physical left/right/center/justify) to | ||
| * stored OOXML paragraph justification, honoring RTL paragraph direction. | ||
| * | ||
| * @param {'left' | 'center' | 'right' | 'justify'} alignment | ||
| * @param {boolean} isRtl | ||
| * @returns {'left' | 'center' | 'right' | 'both'} | ||
| */ | ||
| export function mapDisplayAlignmentToStoredJustification(alignment, isRtl) { | ||
| if (alignment === 'justify') return 'both'; | ||
| if (!isRtl) return alignment; | ||
| if (alignment === 'left') return 'right'; | ||
| if (alignment === 'right') return 'left'; | ||
| return alignment; | ||
| } | ||
|
|
||
| /** | ||
| * Maps stored OOXML paragraph justification to display alignment, honoring | ||
| * RTL paragraph direction. When justification is absent, returns the | ||
| * visual default by direction. | ||
| * | ||
| * @param {string | null | undefined} justification | ||
| * @param {boolean} isRtl | ||
| * @returns {'left' | 'center' | 'right' | 'justify'} | ||
| */ | ||
| export function mapStoredJustificationToDisplayAlignment(justification, isRtl) { | ||
| if (!justification) return isRtl ? 'right' : 'left'; | ||
| if (justification === 'both') return 'justify'; | ||
| if (!isRtl) return /** @type {'left' | 'center' | 'right' | 'justify'} */ (justification); | ||
| if (justification === 'left') return 'right'; | ||
| if (justification === 'right') return 'left'; | ||
| return /** @type {'left' | 'center' | 'right' | 'justify'} */ (justification); | ||
| } | ||
105 changes: 105 additions & 0 deletions
105
packages/super-editor/src/editors/v1/core/helpers/paragraph-alignment.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { | ||
| mapDisplayAlignmentToStoredJustification, | ||
| mapStoredJustificationToDisplayAlignment, | ||
| } from './paragraph-alignment.js'; | ||
|
|
||
| // SD-3094: per ECMA-376 §17.3.1.13, `w:jc="left"` is the leading edge of the | ||
| // paragraph, not the physical left. In RTL paragraphs (`w:bidi`), leading is | ||
| // the right side. The two helpers below own the display ↔ stored translation | ||
| // so the editor can keep its UI in physical terms while the model stays in the | ||
| // spec's logical terms. | ||
|
|
||
| describe('mapDisplayAlignmentToStoredJustification', () => { | ||
| describe('LTR paragraphs (isRtl=false)', () => { | ||
| it('passes left/right/center through unchanged', () => { | ||
| expect(mapDisplayAlignmentToStoredJustification('left', false)).toBe('left'); | ||
| expect(mapDisplayAlignmentToStoredJustification('right', false)).toBe('right'); | ||
| expect(mapDisplayAlignmentToStoredJustification('center', false)).toBe('center'); | ||
| }); | ||
|
|
||
| it('maps justify to OOXML both', () => { | ||
| expect(mapDisplayAlignmentToStoredJustification('justify', false)).toBe('both'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('RTL paragraphs (isRtl=true)', () => { | ||
| it('mirrors left to right', () => { | ||
| expect(mapDisplayAlignmentToStoredJustification('left', true)).toBe('right'); | ||
| }); | ||
|
|
||
| it('mirrors right to left', () => { | ||
| expect(mapDisplayAlignmentToStoredJustification('right', true)).toBe('left'); | ||
| }); | ||
|
|
||
| it('keeps center unchanged', () => { | ||
| expect(mapDisplayAlignmentToStoredJustification('center', true)).toBe('center'); | ||
| }); | ||
|
|
||
| it('maps justify to OOXML both (no direction-flip)', () => { | ||
| expect(mapDisplayAlignmentToStoredJustification('justify', true)).toBe('both'); | ||
| }); | ||
| }); | ||
|
|
||
| it('justify always maps to both regardless of direction', () => { | ||
| expect(mapDisplayAlignmentToStoredJustification('justify', false)).toBe('both'); | ||
| expect(mapDisplayAlignmentToStoredJustification('justify', true)).toBe('both'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('mapStoredJustificationToDisplayAlignment', () => { | ||
| describe('LTR paragraphs (isRtl=false)', () => { | ||
| it('passes left/right/center through unchanged', () => { | ||
| expect(mapStoredJustificationToDisplayAlignment('left', false)).toBe('left'); | ||
| expect(mapStoredJustificationToDisplayAlignment('right', false)).toBe('right'); | ||
| expect(mapStoredJustificationToDisplayAlignment('center', false)).toBe('center'); | ||
| }); | ||
|
|
||
| it('maps both to justify', () => { | ||
| expect(mapStoredJustificationToDisplayAlignment('both', false)).toBe('justify'); | ||
| }); | ||
|
|
||
| it('defaults to left when justification is absent', () => { | ||
| expect(mapStoredJustificationToDisplayAlignment(null, false)).toBe('left'); | ||
| expect(mapStoredJustificationToDisplayAlignment(undefined, false)).toBe('left'); | ||
| expect(mapStoredJustificationToDisplayAlignment('', false)).toBe('left'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('RTL paragraphs (isRtl=true)', () => { | ||
| it('mirrors stored left to display right', () => { | ||
| expect(mapStoredJustificationToDisplayAlignment('left', true)).toBe('right'); | ||
| }); | ||
|
|
||
| it('mirrors stored right to display left', () => { | ||
| expect(mapStoredJustificationToDisplayAlignment('right', true)).toBe('left'); | ||
| }); | ||
|
|
||
| it('keeps center unchanged', () => { | ||
| expect(mapStoredJustificationToDisplayAlignment('center', true)).toBe('center'); | ||
| }); | ||
|
|
||
| it('maps both to justify (no direction-flip)', () => { | ||
| expect(mapStoredJustificationToDisplayAlignment('both', true)).toBe('justify'); | ||
| }); | ||
|
|
||
| it('defaults to right when justification is absent', () => { | ||
| expect(mapStoredJustificationToDisplayAlignment(null, true)).toBe('right'); | ||
| expect(mapStoredJustificationToDisplayAlignment(undefined, true)).toBe('right'); | ||
| expect(mapStoredJustificationToDisplayAlignment('', true)).toBe('right'); | ||
| }); | ||
| }); | ||
|
|
||
| // Roundtrip: writing then reading must return the original display value. | ||
| describe('roundtrip display → stored → display', () => { | ||
| for (const isRtl of [false, true]) { | ||
| for (const display of /** @type {const} */ (['left', 'center', 'right', 'justify'])) { | ||
| it(`${display} on ${isRtl ? 'RTL' : 'LTR'} survives a write+read cycle`, () => { | ||
| const stored = mapDisplayAlignmentToStoredJustification(display, isRtl); | ||
| const readBack = mapStoredJustificationToDisplayAlignment(stored, isRtl); | ||
| expect(readBack).toBe(display); | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.