Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): expand compact Markdown tables (#1459)

Kmnyf6bccda894d0
diff --git a/apps/mobile/src/__tests__/messageContentLayout.test.ts b/apps/mobile/src/__tests__/messageContentLayout.test.ts
index aba9137df4..33f5d68816 100644
--- a/apps/mobile/src/__tests__/messageContentLayout.test.ts
+++ b/apps/mobile/src/__tests__/messageContentLayout.test.ts
@@ -20,6 +20,7 @@ describe('messageContentLayout', () => {
markdownBodyGap: 12,
markdownListGap: 6,
markdownListMarkerWidth: 20,
+ markdownTableAvailableWidth: 280,
markdownTableCellMinWidth: 96,
mediaGap: 6,
mediaPlaceholderMinHeight: 84,
@@ -36,6 +37,7 @@ describe('messageContentLayout', () => {
fileChipMaxWidth: 228,
imagePreviewHeight: 96,
imagePreviewWidth: 148,
+ markdownTableAvailableWidth: 329,
markdownTableCellMinWidth: 112,
mediaPreviewWidth: 160,
toolResultMaxLines: 8,
diff --git a/apps/mobile/src/__tests__/messageTableLayout.test.ts b/apps/mobile/src/__tests__/messageTableLayout.test.ts
index 844abf5548..048fa9fdd2 100644
--- a/apps/mobile/src/__tests__/messageTableLayout.test.ts
+++ b/apps/mobile/src/__tests__/messageTableLayout.test.ts
@@ -34,6 +34,60 @@ describe('messageTableLayout', () => {
expect(widths).toHaveLength(2);
});
+
+ it('expands a compact table to the available message width', () => {
+ const widths = buildMobileMarkdownTableColumnWidths({
+ header: inlineCells(['年份', 'GDP']),
+ rows: [
+ row('r1', ['2025', '30.77 万亿美元']),
+ row('r2', ['2024', '29.30 万亿美元']),
+ ],
+ availableWidth: 329,
+ minWidth: 112,
+ });
+
+ expect(widths.reduce((total, width) => total + width, 0)).toBe(329);
+ expect(widths.every((width) => width >= 112)).toBe(true);
+ });
+
+ it('keeps wide tables wider than the viewport for horizontal scrolling', () => {
+ const widths = buildMobileMarkdownTableColumnWidths({
+ header: inlineCells(['第一列', '第二列', '第三列']),
+ rows: [row('r1', [
+ '一段明显超过单列最大宽度的内容',
+ '另一段明显超过单列最大宽度的内容',
+ '第三段明显超过单列最大宽度的内容',
+ ])],
+ availableWidth: 329,
+ minWidth: 112,
+ });
+
+ expect(widths.reduce((total, width) => total + width, 0)).toBeGreaterThan(329);
+ });
+
+ it('fills wide mobile layouts beyond the intrinsic column width cap', () => {
+ const widths = buildMobileMarkdownTableColumnWidths({
+ header: inlineCells(['年份', 'GDP']),
+ rows: [row('r1', ['2025', '30.77 万亿美元'])],
+ availableWidth: 768,
+ minWidth: 112,
+ });
+
+ expect(widths.reduce((total, width) => total + width, 0)).toBe(768);
+ expect(widths.some((width) => width > 220)).toBe(true);
+ });
+
+ it('never expands past a fractional available width', () => {
+ const availableWidth = 329.5;
+ const widths = buildMobileMarkdownTableColumnWidths({
+ header: inlineCells(['年份', 'GDP']),
+ rows: [row('r1', ['2025', '30.77 万亿美元'])],
+ availableWidth,
+ minWidth: 112,
+ });
+
+ expect(widths.reduce((total, width) => total + width, 0)).toBeLessThanOrEqual(availableWidth);
+ });
});
function row(key: string, cells: string[]): MobileMarkdownTableRow {
diff --git a/apps/mobile/src/session/MessageRenderer.tsx b/apps/mobile/src/session/MessageRenderer.tsx
index bca93debb9..c7265c62c2 100644
--- a/apps/mobile/src/session/MessageRenderer.tsx
+++ b/apps/mobile/src/session/MessageRenderer.tsx
@@ -3587,6 +3587,7 @@ function MarkdownBody({
const columnWidths = buildMobileMarkdownTableColumnWidths({
header: block.header,
rows: block.rows,
+ availableWidth: layout.markdownTableAvailableWidth,
minWidth: layout.markdownTableCellMinWidth,
});
return (
diff --git a/apps/mobile/src/session/messageContentLayout.ts b/apps/mobile/src/session/messageContentLayout.ts
index a012a51308..4a57f16cc0 100644
--- a/apps/mobile/src/session/messageContentLayout.ts
+++ b/apps/mobile/src/session/messageContentLayout.ts
@@ -20,6 +20,7 @@ export interface MessageContentLayout {
markdownBodyGap: number;
markdownListGap: number;
markdownListMarkerWidth: number;
+ markdownTableAvailableWidth: number;
markdownTableCellMinWidth: number;
mediaGap: number;
mediaPlaceholderMinHeight: number;
@@ -59,6 +60,7 @@ export function buildMessageContentLayout(input: MessageContentLayoutInput): Mes
markdownBodyGap: compact ? 12 : 14,
markdownListGap: compact ? 6 : 8,
markdownListMarkerWidth: compact ? 20 : 24,
+ markdownTableAvailableWidth: contentWidth,
markdownTableCellMinWidth: compact ? 96 : 112,
mediaGap: compact ? 6 : 8,
mediaPlaceholderMinHeight: compact ? 84 : 90,
diff --git a/apps/mobile/src/session/messageTableLayout.ts b/apps/mobile/src/session/messageTableLayout.ts
index 26ea6c787b..8ef8f4c1e3 100644
--- a/apps/mobile/src/session/messageTableLayout.ts
+++ b/apps/mobile/src/session/messageTableLayout.ts
@@ -3,6 +3,8 @@ import type { MobileMarkdownInline, MobileMarkdownTableRow } from '@/session/mes
export interface MobileMarkdownTableColumnWidthInput {
header: readonly MobileMarkdownInline[][];
rows: readonly MobileMarkdownTableRow[];
+ /** 表格所在消息正文的可用宽度;短表会扩展到这里,宽表继续横向滚动。 */
+ availableWidth?: number;
minWidth: number;
maxWidth?: number;
}
@@ -16,6 +18,7 @@ const DEFAULT_IMAGE_INLINE_WIDTH = 150;
export function buildMobileMarkdownTableColumnWidths({
header,
rows,
+ availableWidth,
minWidth,
maxWidth = DEFAULT_MAX_COLUMN_WIDTH,
}: MobileMarkdownTableColumnWidthInput): number[] {
@@ -24,7 +27,8 @@ export function buildMobileMarkdownTableColumnWidths({
...rows.map((row) => row.cells.length),
);
if (columnCount <= 0) return [];
- return Array.from({ length: columnCount }, (_, columnIndex) => {
+ const normalizedMaxWidth = Math.max(minWidth, maxWidth);
+ const widths = Array.from({ length: columnCount }, (_, columnIndex) => {
const contentWidth = Math.max(
estimateInlineWidth(header[columnIndex] ?? []),
...rows.map((row) => estimateInlineWidth(row.cells[columnIndex] ?? [])),
@@ -32,9 +36,28 @@ export function buildMobileMarkdownTableColumnWidths({
return clampWidth(
Math.ceil(contentWidth + CELL_HORIZONTAL_PADDING),
minWidth,
- Math.max(minWidth, maxWidth),
+ normalizedMaxWidth,
);
});
+ return expandColumnWidthsToAvailableSpace(widths, availableWidth);
+}
+
+/**
+ * 内容较短时把正文里的剩余空间平均分给仍可扩展的列,避免表格缩在左侧;
+ * 内容已经更宽时保持估算宽度,由外层横向 ScrollView 承担溢出。
+ */
+function expandColumnWidthsToAvailableSpace(
+ widths: readonly number[],
+ availableWidth: number | undefined,
+): number[] {
+ if (availableWidth === undefined || !Number.isFinite(availableWidth) || availableWidth <= 0) {
+ return [...widths];
+ }
+ const remaining = Math.max(0, Math.floor(availableWidth) - sum(widths));
+ if (remaining <= 0) return [...widths];
+ const share = Math.floor(remaining / widths.length);
+ const remainder = remaining % widths.length;
+ return widths.map((width, index) => width + share + (index < remainder ? 1 : 0));
}
function estimateInlineWidth(inlines: readonly MobileMarkdownInline[]): number {
@@ -66,3 +89,7 @@ function isWideGlyph(char: string): boolean {
function clampWidth(value: number, min: number, max: number): number {
return Math.max(min, Math.min(max, value));
}
+
+function sum(values: readonly number[]): number {
+ return values.reduce((total, value) => total + value, 0);
+}