An open source design system that's fully customizable and agent ready
TypeScript
fix(richtext): give every block the surface's direction instead of guessing it per block (#7001)
Cindy Zhangdae1f00de284
diff --git a/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts b/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.tsindex e3ac1fde91..d396551352 100644--- a/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts+++ b/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts@@ -732,6 +732,91 @@ for (const [viewport, colorMode] of [});}+// spec:AST-061 FR6: every block takes the direction of the surface around+// it, whatever its first strong character, on both surfaces.+for (const direction of ['ltr', 'rtl'] as const) {+ test(`blocks take the provider direction (${direction})`, async ({page}) => {+ const errors = await openStory(+ page,+ STORY.sideBySide,+ DESKTOP,+ `colorMode:light;direction:${direction}`,+ );+ await waitForDocument(page, MARKDOWN);+ await waitForDocument(page, RICH_TEXT);+ for (const surface of [MARKDOWN, RICH_TEXT]) {+ const geometry = await surfaceGeometry(page, surface);+ const otherDirection = geometry.blocks+ .filter(block => block.direction !== direction)+ .map(block => block.key);+ expect(otherDirection, surface).toEqual([]);+ // Inside blocks too: table rows and cells, list items, and paragraphs.+ const nested = await page.evaluate(+ ({selector, expected}) =>+ [+ ...document.querySelectorAll(+ `${selector} [data-parity-body] :is(tr, th, td, li, p)`,+ ),+ ]+ .filter(element => getComputedStyle(element).direction !== expected)+ .map(element => element.tagName),+ {selector: surface, expected: direction},+ );+ expect(nested, `${surface} nested`).toEqual([]);+ }+ expect(errors).toEqual([]);+ });++ // Mixed English and Hebrew cells take the document's direction, so each+ // cell's text starts at the document's start edge, whatever its script.+ test(`table cells take the provider direction with mixed scripts (${direction})`, async ({+ page,+ }) => {+ await page.setViewportSize(DESKTOP);+ await page.goto(+ `${storybook.origin}/iframe.html?id=lab-richtexteditor--markdown-serializers&viewMode=story&globals=astryxTheme:neutral;colorMode:light;direction:${direction}`,+ {waitUntil: 'load'},+ );+ await page+ .locator('textarea')+ .fill(+ '| Name | \u05e9\u05dd |\n| --- | :---: |\n| Ada | \u05e2\u05d3\u05d4 |\n| \u05e9\u05dc\u05d5\u05dd **bold** | Hello |',+ );+ await expect(page.locator('table:visible')).toHaveCount(2);+ const cells = await page.evaluate(() =>+ [...document.querySelectorAll('[data-lexical-editor] :is(th, td)')].map(+ cell => {+ const style = getComputedStyle(cell);+ const paragraph = cell.querySelector('p') ?? cell;+ const range = document.createRange();+ range.selectNodeContents(paragraph);+ const text = range.getBoundingClientRect();+ const box = cell.getBoundingClientRect();+ return {+ column: (cell as HTMLTableCellElement).cellIndex,+ direction: style.direction,+ paragraphDirection: getComputedStyle(paragraph).direction,+ startGap:+ style.direction === 'rtl'+ ? box.right - parseFloat(style.paddingRight) - text.right+ : text.left - box.left - parseFloat(style.paddingLeft),+ };+ },+ ),+ );+ // Editor and view, three rows of two cells each.+ expect(cells).toHaveLength(12);+ for (const cell of cells) {+ expect(cell.direction).toBe(direction);+ expect(cell.paragraphDirection).toBe(direction);+ if (cell.column === 0) {+ // The first column is start-aligned: its text touches the start edge.+ expect(Math.abs(cell.startGap)).toBeLessThanOrEqual(1);+ }+ }+ });+}+test('overlay: both layers start together and the drawn editor is inert', async ({page,}) => {diff --git a/packages/richtext/src/editorTheme.ts b/packages/richtext/src/editorTheme.tsindex 11667c263a..17856ab043 100644--- a/packages/richtext/src/editorTheme.ts+++ b/packages/richtext/src/editorTheme.ts@@ -48,6 +48,15 @@ const NESTED_LIST_SPACE = `calc(${spacingVars['--spacing-2']})`;const RULE_SPACE = `calc(${spacingVars['--spacing-6']})`;const editorTheme = stylex.create({+ // Lexical marks every top-level block `dir="auto"`, which picks each+ // block's direction from its first strong character: a Hebrew paragraph in+ // an English document flips to the right, an English one in an Arabic+ // document to the left. Blocks take the direction of the surface around+ // them instead, as core Markdown's do, and bidi inside a block still+ // follows the Unicode algorithm (spec:AST-061 FR6).+ providerDirection: {+ direction: 'inherit',+ },paragraph: {// The first block's leading margin would stack with the input inset, so// the first line aligns with TextArea and the empty-editor placeholder.@@ -364,35 +373,46 @@ const editorTheme = stylex.create({* class-name strings, which `stylex.props(...).className` yields.*/export function sharedEditorTheme(): EditorThemeClasses {- const ulClass = stylex.props(editorTheme.ul).className ?? '';- const olClass = stylex.props(editorTheme.ol).className ?? '';- const ulDepth2Class =- stylex.props(editorTheme.ul, editorTheme.ulNested, editorTheme.ulDepth2)- .className ?? '';- const ulDepth3Class =- stylex.props(editorTheme.ul, editorTheme.ulNested, editorTheme.ulDepth3)- .className ?? '';- const olDepth2Class =- stylex.props(editorTheme.ol, editorTheme.olNested, editorTheme.olDepth2)- .className ?? '';- const olDepth3Class =- stylex.props(editorTheme.ol, editorTheme.olNested, editorTheme.olDepth3)- .className ?? '';+ // A class for a block Lexical may mark `dir="auto"`.+ const block = (...styles: Array<stylex.StyleXStyles>): string =>+ stylex.props(...styles, editorTheme.providerDirection).className ?? '';+ const ulClass = block(editorTheme.ul);+ const olClass = block(editorTheme.ol);+ const ulDepth2Class = block(+ editorTheme.ul,+ editorTheme.ulNested,+ editorTheme.ulDepth2,+ );+ const ulDepth3Class = block(+ editorTheme.ul,+ editorTheme.ulNested,+ editorTheme.ulDepth3,+ );+ const olDepth2Class = block(+ editorTheme.ol,+ editorTheme.olNested,+ editorTheme.olDepth2,+ );+ const olDepth3Class = block(+ editorTheme.ol,+ editorTheme.olNested,+ editorTheme.olDepth3,+ );return {- paragraph: stylex.props(editorTheme.paragraph).className,+ paragraph: block(editorTheme.paragraph),heading: {- h1: stylex.props(editorTheme.h1).className,- h2: stylex.props(editorTheme.h2).className,- h3: stylex.props(editorTheme.h3).className,- h4: stylex.props(editorTheme.h4).className,- h5: stylex.props(editorTheme.h5).className,- h6: stylex.props(editorTheme.h6).className,- },- quote: stylex.props(editorTheme.quote).className,+ h1: block(editorTheme.h1),+ h2: block(editorTheme.h2),+ h3: block(editorTheme.h3),+ h4: block(editorTheme.h4),+ h5: block(editorTheme.h5),+ h6: block(editorTheme.h6),+ },+ quote: block(editorTheme.quote),list: {ul: ulClass,ol: olClass,- checklist: stylex.props(editorTheme.ul, editorTheme.checklist).className,+ checklist: block(editorTheme.ul, editorTheme.checklist),listitem: stylex.props(editorTheme.listItem).className,nested: {listitem: stylex.props(editorTheme.listItem).className,@@ -410,14 +430,15 @@ export function sharedEditorTheme(): EditorThemeClasses {strikethrough: stylex.props(editorTheme.textStrikethrough).className,code: stylex.props(editorTheme.textCode).className,},- code: stylex.props(editorTheme.code).className,+ code: block(editorTheme.code),hr: stylex.props(editorTheme.hr).className,hrSelected: stylex.props(editorTheme.hrSelected).className,- table: stylex.props(editorTheme.table).className,- tableRow: stylex.props(editorTheme.tableRow).className,- tableCell: stylex.props(editorTheme.tableCell).className,- tableCellHeader: stylex.props(editorTheme.tableCellHeader).className,- tableScrollableWrapper: stylex.props(editorTheme.tableScrollableWrapper)- .className,+ table: block(editorTheme.table),+ // Lexical stamps rows and cells `dir="auto"` too, which would set a cell+ // of English text left-to-right inside a right-to-left document.+ tableRow: block(editorTheme.tableRow),+ tableCell: block(editorTheme.tableCell),+ tableCellHeader: block(editorTheme.tableCellHeader),+ tableScrollableWrapper: block(editorTheme.tableScrollableWrapper),};}