diff --git a/ui/desktop/src/components/ChatInput.tsx b/ui/desktop/src/components/ChatInput.tsx index d65a19280..c2d99e789 100644 --- a/ui/desktop/src/components/ChatInput.tsx +++ b/ui/desktop/src/components/ChatInput.tsx @@ -37,6 +37,7 @@ import { getNavigationShortcutText } from '../utils/keyboardShortcuts'; import { UserInput, ImageData } from '../types/message'; import { compressImageDataUrl } from '../utils/conversionUtils'; import { fetchCanonicalModelInfo } from '../utils/canonical'; +import { getTextDirection } from '../utils/textDirection'; import { defineMessages, useIntl } from '../i18n'; import TurndownService from 'turndown'; import type { NextChatExtensionDraft } from '../utils/nextChatExtensions'; @@ -258,6 +259,8 @@ export default function ChatInput({ // Derived state - chatState != Idle means we're in some form of loading state const isLoading = chatState !== ChatState.Idle; const isLoadingRef = useRef(isLoading); + + const composerDir = useMemo(() => getTextDirection(displayValue) ?? undefined, [displayValue]); const queueProcessingBlockedRef = useRef(queueProcessingBlocked); const wasLoadingRef = useRef(isLoading); const wasQueueProcessingBlockedRef = useRef(queueProcessingBlocked); @@ -1542,6 +1545,7 @@ export default function ChatInput({ data-testid="chat-input" autoFocus id="dynamic-textarea" + dir={composerDir} placeholder={isRecording ? '' : getNavigationShortcutText(intl)} value={displayValue} onChange={handleChange} diff --git a/ui/desktop/src/components/GooseMessage.tsx b/ui/desktop/src/components/GooseMessage.tsx index 0d382bf73..979d6c58b 100644 --- a/ui/desktop/src/components/GooseMessage.tsx +++ b/ui/desktop/src/components/GooseMessage.tsx @@ -18,6 +18,7 @@ import ToolCallConfirmation from './ToolCallConfirmation'; import ElicitationRequest from './ElicitationRequest'; import MessageCopyLink from './MessageCopyLink'; import MessageUsageStats from './MessageUsageStats'; +import { getTextDirection } from '../utils/textDirection'; import { cn } from '../utils'; import type { ToolRenderState } from './messageRowContext'; import { @@ -78,6 +79,7 @@ function GooseMessage({ streamingRenderCooldownMs ); const toolConfirmationContent = getToolConfirmationContent(message); + const messageDir = useMemo(() => getTextDirection(displayText) ?? undefined, [displayText]); const elicitationContent = getElicitationContent(message); const hasToolConfirmation = toolConfirmationContent !== undefined; const hasElicitation = elicitationContent !== undefined; @@ -110,7 +112,7 @@ function GooseMessage({ {(displayText.trim() || imagePaths.length > 0) && (
{displayText.trim() && ( -
+
)} diff --git a/ui/desktop/src/components/MarkdownContent.tsx b/ui/desktop/src/components/MarkdownContent.tsx index e8d924f9e..78139f42b 100644 --- a/ui/desktop/src/components/MarkdownContent.tsx +++ b/ui/desktop/src/components/MarkdownContent.tsx @@ -29,6 +29,7 @@ const customOneDarkTheme = { import { Check, Copy } from './icons'; import { wrapHTMLInCodeBlock } from '../utils/htmlSecurity'; import { BLOCKED_PROTOCOLS } from '../utils/urlSecurity'; +import { getTextDirection } from '../utils/textDirection'; import { defineMessages, useIntl } from '../i18n'; const i18n = defineMessages({ @@ -55,6 +56,83 @@ interface MarkdownContentProps { className?: string; } +// Minimal hast node shape; avoids importing @types/hast just for this plugin. +interface HastNode { + type?: string; + tagName?: string; + value?: string; + properties?: Record | null; + children?: HastNode[]; +} + +// Block-level elements that get their own computed dir so mixed-direction +// markdown (e.g. an English paragraph inside an Arabic message) resolves +// punctuation placement per block instead of inheriting the message direction. +const DIRECTIONAL_BLOCK_TAGS = new Set([ + 'p', + 'h1', + 'h2', + 'h3', + 'h4', + 'h5', + 'h6', + 'li', + 'blockquote', + 'dd', + 'dt', + 'td', + 'th', + 'figcaption', + 'caption', + 'summary', +]); + +function collectText(node: HastNode): string { + // Code and KaTeX output are language-neutral (both render LTR internally), + // so they don't vote on the direction of the prose block containing them. + if (node.type === 'element') { + if (node.tagName === 'code' || node.tagName === 'pre') return ''; + const classNames = node.properties?.className; + if (Array.isArray(classNames) && classNames.includes('katex')) return ''; + } + let text = node.type === 'text' ? (node.value ?? '') : ''; + for (const child of node.children ?? []) { + // Nested blocks other than paragraphs compute their own direction and + // don't vote on the parent, so a long sublist can't flip the list item + // containing it. Prose

children still count: loose list items and + // blockquotes hold their own text in direct

children, and their + // marker/indent side follows the parent's own direction. + if ( + child.type === 'element' && + child.tagName && + child.tagName !== 'p' && + DIRECTIONAL_BLOCK_TAGS.has(child.tagName) + ) { + continue; + } + text += collectText(child); + } + return text; +} + +function applyPerBlockDirection(node: HastNode): void { + if (node.type === 'element' && node.tagName && DIRECTIONAL_BLOCK_TAGS.has(node.tagName)) { + const direction = getTextDirection(collectText(node)); + if (direction) { + node.properties = { ...node.properties, dir: direction }; + } + } + for (const child of node.children ?? []) { + applyPerBlockDirection(child); + } +} + +function rehypePerBlockDirection(): (tree: HastNode) => void { + return (tree) => { + if (tree) applyPerBlockDirection(tree); + }; +} + // Memoized CodeBlock component to prevent re-rendering when props haven't changed const CodeBlock = memo(function CodeBlock({ language, @@ -131,7 +209,7 @@ const CodeBlock = memo(function CodeBlock({ }, [language, children]); return ( -

+