Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

💄 fix(canvas-runtime): fix card text flow, table overflow, donut legend and page width (#21592)

Innei8698734ec915
diff --git a/packages/canvas-runtime/src/base.css b/packages/canvas-runtime/src/base.css
index 7ac17d9e90e..b3fb1adb894 100644
--- a/packages/canvas-runtime/src/base.css
+++ b/packages/canvas-runtime/src/base.css
@@ -3,9 +3,17 @@
color-scheme: light dark;
}
+ /* The host owns the side gutter and the centered reading width. Models write
+ `body { margin: 0; padding: 4px 2px }`, which left content touching the edge and a
+ published page stretched across the whole window; a layered !important outranks
+ unlayered page CSS. */
body {
+ box-sizing: border-box;
+ max-width: 1024px;
margin: 0;
+ margin-inline: auto !important;
padding: 24px;
+ padding-inline: 24px !important;
font-family: var(--lobe-font-family);
font-size: var(--lobe-font-size);
line-height: var(--lobe-line-height);
@@ -124,6 +132,33 @@
display: contents;
}
+ [data-lobe-canvas-card-body] > [data-lobe-canvas-flow] > :first-child {
+ margin-block-start: 0;
+ }
+
+ [data-lobe-canvas-card-body] > [data-lobe-canvas-flow] > :last-child {
+ margin-block-end: 0;
+ }
+
+ /* macOS overlay scrollbars stay hidden until scrolled, so a wide table or diagram read
+ as clipped. Styling the scrollbar switches Chromium to an always-visible one. */
+ :is(lobe-table, lobe-mermaid, lobe-code, pre) ::-webkit-scrollbar,
+ :is([data-lobe-canvas-mermaid], pre)::-webkit-scrollbar {
+ width: 8px;
+ height: 8px;
+ }
+
+ :is(lobe-table, lobe-mermaid, lobe-code, pre) ::-webkit-scrollbar-thumb,
+ :is([data-lobe-canvas-mermaid], pre)::-webkit-scrollbar-thumb {
+ border-radius: 4px;
+ background: var(--lobe-color-fill);
+ }
+
+ :is(lobe-table, lobe-mermaid, lobe-code, pre) ::-webkit-scrollbar-track,
+ :is([data-lobe-canvas-mermaid], pre)::-webkit-scrollbar-track {
+ background: transparent;
+ }
+
[hidden] {
display: none !important;
}
diff --git a/packages/canvas-runtime/src/format.test.ts b/packages/canvas-runtime/src/format.test.ts
index 36c11756425..0d8d77c40e2 100644
--- a/packages/canvas-runtime/src/format.test.ts
+++ b/packages/canvas-runtime/src/format.test.ts
@@ -13,6 +13,12 @@ describe('formatValue', () => {
expect(formatValue(120, undefined, 'ms')).toBe('120 ms');
expect(formatValue('n/a')).toBe('n/a');
});
+
+ it('attaches symbol units without a space', () => {
+ expect(formatValue(40, undefined, '%')).toBe('40%');
+ expect(formatValue(3, undefined, '‰')).toBe('3‰');
+ expect(formatValue(12, undefined, '万')).toBe('12 万');
+ });
});
describe('heatmapLevel', () => {
diff --git a/packages/canvas-runtime/src/format.ts b/packages/canvas-runtime/src/format.ts
index 8cf05257fc5..ac345c19bde 100644
--- a/packages/canvas-runtime/src/format.ts
+++ b/packages/canvas-runtime/src/format.ts
@@ -1,6 +1,7 @@
const compact = new Intl.NumberFormat(undefined, { maximumFractionDigits: 1, notation: 'compact' });
const number = new Intl.NumberFormat(undefined, { maximumFractionDigits: 2 });
const percent = new Intl.NumberFormat(undefined, { maximumFractionDigits: 1, style: 'percent' });
+const TIGHT_UNIT = /^[%‰°′″]/;
export const formatValue = (value: unknown, format?: unknown, unit?: unknown): string => {
if (typeof value !== 'number') return value === undefined || value === null ? '' : String(value);
@@ -24,7 +25,8 @@ export const formatValue = (value: unknown, format?: unknown, unit?: unknown): s
}
}
- return typeof unit === 'string' && unit ? `${text} ${unit}` : text;
+ if (typeof unit !== 'string' || !unit) return text;
+ return TIGHT_UNIT.test(unit) ? `${text}${unit}` : `${text} ${unit}`;
};
export const heatmapLevel = (count: number, max: number) =>
diff --git a/packages/canvas-runtime/src/views/base.tsx b/packages/canvas-runtime/src/views/base.tsx
index 97ebd7a0077..590e59d2ecb 100644
--- a/packages/canvas-runtime/src/views/base.tsx
+++ b/packages/canvas-runtime/src/views/base.tsx
@@ -29,6 +29,7 @@ import { memo, type ReactNode, useState } from 'react';
import type { CanvasView, CanvasViewContext } from '../element';
import { formatValue } from '../format';
import type { CanvasChildItem } from '../props';
+import { toTableColumns } from './tableColumns';
const Slot = ({ slot }: Pick<CanvasViewContext, 'slot'>) => (
<span data-lobe-canvas-slot="" ref={slot} />
@@ -104,7 +105,10 @@ const card = (context: CanvasViewContext): ReactNode => {
return (
<Block gap={12} padding={props.padding ?? 16} variant={props.variant ?? 'outlined'}>
{props.title && <Text strong>{props.title}</Text>}
- <Slot slot={context.slot} />
+ {/* Block is a flex column: slotted text and <strong> would each become a flex item */}
+ <div data-lobe-canvas-card-body="">
+ <span data-lobe-canvas-flow="" data-lobe-canvas-slot="" ref={context.slot} />
+ </div>
</Block>
);
};
@@ -247,23 +251,18 @@ export const baseViews: Record<string, CanvasView> = {
</Flexbox>
),
'lobe-table': ({ props }) => {
- const columns = (Array.isArray(props.columns) ? props.columns : []).map((column: any) => ({
- align: column.align,
- dataIndex: column.key,
- key: column.key,
- render: (value: unknown) => formatValue(value, column.format),
- title: column.title,
- }));
const data = (Array.isArray(props.data) ? props.data : []).map((row: any, index: number) => ({
__key: String(index),
...row,
}));
+ const columns = toTableColumns(Array.isArray(props.columns) ? props.columns : [], data);
return (
<Table
columns={columns}
dataSource={data}
pagination={false}
rowKey={'__key'}
+ scroll={{ x: true }}
size={'middle'}
variant={'outlined'}
/>
diff --git a/packages/canvas-runtime/src/views/charts.tsx b/packages/canvas-runtime/src/views/charts.tsx
index dac9a52b95d..8715e5d148b 100644
--- a/packages/canvas-runtime/src/views/charts.tsx
+++ b/packages/canvas-runtime/src/views/charts.tsx
@@ -5,6 +5,7 @@ import {
DonutChart,
FunnelChart,
Heatmaps,
+ Legend,
LineChart,
RadarChart,
ScatterChart,
@@ -13,11 +14,12 @@ import {
SparkLineChart,
Tracker,
} from '@lobehub/charts';
-import { useTheme } from '@lobehub/ui';
+import { Flexbox, useTheme } from '@lobehub/ui';
import { memo } from 'react';
import type { CanvasView } from '../element';
import { formatValue, heatmapLevel } from '../format';
+import { donutLegendCategories, showsDonutTotal } from './donut';
type Props = Record<string, any>;
@@ -77,16 +79,24 @@ const Scatter = memo<{ props: Props }>(({ props }) => (
/>
));
-const Donut = memo<{ props: Props }>(({ props }) => (
- <DonutChart
- category={props.category}
- colors={usePaletteColors(props.colors)}
- data={asArray(props.data)}
- index={props.index}
- valueFormatter={(value: number) => formatValue(value, props['value-format'], props.unit)}
- variant={props.variant ?? 'donut'}
- />
-));
+const Donut = memo<{ props: Props }>(({ props }) => {
+ const colors = usePaletteColors(props.colors);
+ const data = asArray(props.data);
+ return (
+ <Flexbox align={'center'} gap={12}>
+ <DonutChart
+ category={props.category}
+ colors={colors}
+ data={data}
+ index={props.index}
+ showLabel={showsDonutTotal(props)}
+ valueFormatter={(value: number) => formatValue(value, props['value-format'], props.unit)}
+ variant={props.variant ?? 'donut'}
+ />
+ <Legend categories={donutLegendCategories(data, props.index)} colors={colors} />
+ </Flexbox>
+ );
+});
const Spark = memo<{ props: Props }>(({ props }) => {
const colors = usePaletteColors(props.colors);
diff --git a/packages/canvas-runtime/src/views/donut.test.ts b/packages/canvas-runtime/src/views/donut.test.ts
new file mode 100644
index 00000000000..a6e327a745f
--- /dev/null
+++ b/packages/canvas-runtime/src/views/donut.test.ts
@@ -0,0 +1,23 @@
+import { describe, expect, it } from 'vitest';
+
+import { donutLegendCategories, showsDonutTotal } from './donut';
+
+describe('showsDonutTotal', () => {
+ it('hides the center total for shares of a whole', () => {
+ expect(showsDonutTotal({ unit: '%' })).toBe(false);
+ expect(showsDonutTotal({ 'value-format': 'percent' })).toBe(false);
+ expect(showsDonutTotal({ unit: '元' })).toBe(true);
+ });
+});
+
+describe('donutLegendCategories', () => {
+ it('names every slice in data order', () => {
+ const rows = [
+ { region: '华东', share: 45 },
+ { region: '华南', share: 30 },
+ { region: '华北', share: 25 },
+ ];
+ expect(donutLegendCategories(rows, 'region')).toEqual(['华东', '华南', '华北']);
+ expect(donutLegendCategories(rows, undefined)).toEqual([]);
+ });
+});
diff --git a/packages/canvas-runtime/src/views/donut.ts b/packages/canvas-runtime/src/views/donut.ts
new file mode 100644
index 00000000000..f0929784f46
--- /dev/null
+++ b/packages/canvas-runtime/src/views/donut.ts
@@ -0,0 +1,8 @@
+type Props = Record<string, any>;
+
+// The center label is the sum of the slices, which is always "100%" for shares
+export const showsDonutTotal = (props: Props) =>
+ props.unit !== '%' && props['value-format'] !== 'percent';
+
+export const donutLegendCategories = (rows: Props[], index: unknown) =>
+ typeof index === 'string' ? rows.map((row) => String(row[index] ?? '')) : [];
diff --git a/packages/canvas-runtime/src/views/tableColumns.test.ts b/packages/canvas-runtime/src/views/tableColumns.test.ts
new file mode 100644
index 00000000000..0d6e89479a3
--- /dev/null
+++ b/packages/canvas-runtime/src/views/tableColumns.test.ts
@@ -0,0 +1,44 @@
+import { describe, expect, it } from 'vitest';
+
+import { isShortText, textColumns, toTableColumns } from './tableColumns';
+
+describe('textColumns', () => {
+ it('counts CJK and full-width characters as two columns', () => {
+ expect(textColumns('王五')).toBe(4);
+ expect(textColumns('2026-10-05')).toBe(10);
+ expect(textColumns('第 3 天')).toBe(7);
+ expect(textColumns('(2 小时)')).toBe(10);
+ });
+});
+
+describe('isShortText', () => {
+ it('treats labels up to six CJK characters as short', () => {
+ expect(isShortText('多人实时协作')).toBe(true);
+ expect(isShortText('团队工作区:文档 + 数据库')).toBe(false);
+ });
+});
+
+describe('toTableColumns', () => {
+ const columns = toTableColumns(
+ [
+ { key: 'dim', title: '维度' },
+ { key: 'note', title: '说明' },
+ ],
+ [
+ { dim: '产品定位', note: '团队工作区:文档 + 数据库 + 任务 + AI,适合需要协作的团队' },
+ { dim: '多人实时协作', note: '原生' },
+ ],
+ );
+ const [label, note] = columns;
+
+ it('keeps a column of short CJK labels on one line', () => {
+ expect(label.onCell?.()).toEqual({ style: { whiteSpace: 'nowrap' } });
+ expect(label.onHeaderCell?.()).toEqual({ style: { whiteSpace: 'nowrap' } });
+ expect(label.minWidth).toBeUndefined();
+ });
+
+ it('lets a column with long text wrap, but not below a readable width', () => {
+ expect(note.onCell).toBeUndefined();
+ expect(note.minWidth).toBeGreaterThanOrEqual(120);
+ });
+});
diff --git a/packages/canvas-runtime/src/views/tableColumns.ts b/packages/canvas-runtime/src/views/tableColumns.ts
new file mode 100644
index 00000000000..ffddf214fa1
--- /dev/null
+++ b/packages/canvas-runtime/src/views/tableColumns.ts
@@ -0,0 +1,50 @@
+import { formatValue } from '../format';
+
+const WIDE_RANGES: [number, number][] = [
+ [0x11_00, 0x11_5f],
+ [0x2e_80, 0xa4_cf],
+ [0xac_00, 0xd7_a3],
+ [0xf9_00, 0xfa_ff],
+ [0xfe_30, 0xfe_4f],
+ [0xff_00, 0xff_60],
+ [0xff_e0, 0xff_e6],
+];
+
+const isWideChar = (code: number) => WIDE_RANGES.some(([from, to]) => code >= from && code <= to);
+
+export const textColumns = (text: string) => {
+ let width = 0;
+ for (const char of text) width += isWideChar(char.codePointAt(0)!) ? 2 : 1;
+ return width;
+};
+
+// CJK can break between any two characters, so an auto-layout table squeezes a column of
+// short labels down to one character per line. Text up to this width stays on one line.
+const SHORT_TEXT_COLUMNS = 12;
+const WRAPPING_COLUMN_MIN_WIDTH = 140;
+const NO_WRAP = { style: { whiteSpace: 'nowrap' } } as const;
+
+export const isShortText = (text: string) => textColumns(text) <= SHORT_TEXT_COLUMNS;
+
+export interface CanvasTableColumn {
+ align?: 'left' | 'right';
+ format?: string;
+ key: string;
+ title?: string;
+}
+
+export const toTableColumns = (columns: CanvasTableColumn[], rows: Record<string, unknown>[]) =>
+ columns.map((column) => {
+ const render = (value: unknown) => formatValue(value, column.format);
+ const cellsFit = rows.every((row) => isShortText(render(row[column.key])));
+ return {
+ align: column.align,
+ dataIndex: column.key,
+ key: column.key,
+ minWidth: cellsFit ? undefined : WRAPPING_COLUMN_MIN_WIDTH,
+ onCell: cellsFit ? () => NO_WRAP : undefined,
+ onHeaderCell: isShortText(String(column.title ?? '')) ? () => NO_WRAP : undefined,
+ render,
+ title: column.title,
+ };
+ });
diff --git a/packages/canvas-runtime/vite.config.ts b/packages/canvas-runtime/vite.config.ts
index 4d38eb18071..912abda165c 100644
--- a/packages/canvas-runtime/vite.config.ts
+++ b/packages/canvas-runtime/vite.config.ts
@@ -29,6 +29,9 @@ export default defineConfig(({ mode }) => {
},
},
define: { 'process.env.NODE_ENV': '"production"' },
+ // The dev server compiles this config inside a NODE_ENV=development process, which
+ // makes oxc emit jsxDEV; the production React selected by `define` has no jsxDEV.
+ oxc: { jsx: { development: false } },
plugins: [react(), ...lobeUiImports()],
};
});