Skip to content

Commit 6a7403b

Browse files
fix(print): a zh-CN record page prints dates in Chinese order, fits its line items to the sheet, and prints on white with plain values (#34422)
<!-- claude-projects-attribution: {"github_login":"os-project-manager"} --> _Requested by **Leehom** · [project thread](https://claude.ai/code/project/chan_01TenzsRopZj4jMbvjX8uz1X?thread=cmsg_01TenzsRopZj4jMbvjX8uz1XRT27U1unc64yMdYqqPWFjw)_ Part of #34228 Ruling-ref: 6922479414 (item 3 of #34228: the maintainer chose C 「规则不动,拆开报价单模板」; `avoidBreakInside` keeps its published meaning, the quote template is split instead) Clause-②: no Metadata-protocol: none This PR builds items 1, 2 and 4 of the card. Item 3 (the blank half page under `avoidBreakInside`) was measured (below) and ruled C by the maintainer (record 6922479414): `avoidBreakInside` keeps its published meaning, and a print page that may break between sections authors one `record:details` block per section. Where the rest of that ruling goes: the reply to the reporter is on #34228; the HotCRM quote template split is hotcrm#2553; the one-sentence guidance in `content/docs/ui/print-pages.mdx` and `skills/objectstack-ui/rules/pages.md` is the separate text PR #34436 (`Part of #34228`, Tier H, handed to the maintainer by the seat); a long `record:line_items` table moving whole to the next sheet is #34427, outside this ruling. ## 维护者速读 - 改了什么:中文组织打印记录页时,`format: 'short'` 的日期按语言自己的顺序显示(`2026年11月15日`);打印时明细表收进纸宽;纸面为白底,查找字段和下拉选项印成普通文字。 - 为什么改:报价单这类发给客户的文件在中文环境下日期印成 `11月 15, '26`,五列明细表超出卡片边框,背景发灰,状态印成彩色胶囊。 - 风险与回滚:日期改动在屏幕上同样生效,凡是非“月-日-年”语言(中、日、韩、德、英式英语)的 `short` 日期都会变成该语言自己的写法,英语(美式)不变;打印规则只在 `@media print` 且限定在打印页内。回滚即还原本 PR。 - 先看:`ui/packages/core/src/utils/date-display.ts`(`short` 分支的语言顺序判断)、`ui/packages/components/src/renderers/layout/page-print.ts`(新增的三组打印规则)、`ui/packages/fields/src/index.tsx`(下拉徽章上的打印钩子属性)。 - 两分钟验证:`pnpm --dir ui exec vitest run packages/core/src/utils/__tests__/date-display.shortLocaleOrder-34228.test.ts`,预期全部通过,`zh-CN` 一行断言 `2026年11月15日`。 - 需要你做的:无。第 3 项已裁 C(记录 6922479414):规则不动,由模板拆块解决;答复报告人在 #34228,HotCRM 模板拆块是 hotcrm#2553,docs/skills 各补一句由文字 PR #34436 承接,明细表整表移页另立 #34427。本 PR 由席位落地。 ## What changes **Item 1, dates.** The mechanism the dispatch named was measured first and does not hold. Headless Chromium does run with `en-US` as its default (`navigator.language` = `en-US`, Chromium 153.0.8010.12), but every date renderer passes an explicit tag (`useDisplayLocale()` never answers `undefined`), and in that same Chromium `toLocaleDateString('zh-CN', { month: 'short' })` answers `11月`. The record page and the list grid draw a date through the same `DateCellRenderer`. The shape `11月 15, '26` has one producer in the tree: `formatDate`'s `'short'` face. That face put a localized month token into a fixed English layout (`Nov 15, '26`). The quote's date fields are authored `format: 'short'` (the HotCRM quote object declares it on `quote_date` and `expiration_date` so that they always show the year). So the same face showed on screen and on paper. The fix is at that producer. The compact layout is kept only where it is the locale's own order (month → day → year, asked of `Intl.DateTimeFormat#formatToParts`, cached per tag). Every other locale gets its own date with the year, which is the default face with the year shown: `zh-CN` / `ja-JP` `2026年11月15日`, `ko-KR` `2026년 11월 15일`, `de-DE` `15. Nov. 2026`, `en-GB` `15 Nov 2026`. `en` / `en-US` stay `Nov 15, '26`, byte for byte. The change reaches every reader of `formatDate(v, 'short', { locale })` (date cells, dataset measures), on screen and on paper alike. There is no print-only formatter. **Item 2, tables fit the sheet.** On a print page, every data table except the running header/footer table gets `width: 100%`, and its cells drop the on-screen `min-width` and `white-space: nowrap` (`!important`, because the grid sets the minimum width inline). The columns then shrink and wrap to the box. The overflow was what drew the stray vertical rule: the wrapper's right border ran through the last column. **Item 4, document face.** On paper the page and its ancestors print on `#fff`. Links print in the surrounding colour with no underline; a lookup's reference is a link. A select value's badge prints as its label: no fill, transparent border, no padding, no radius, with `!important` so that it also beats a hex option colour's inline style. To give the stylesheet a stable hook, `SelectCellRenderer`'s badge carries a new inert `data-select-badge` attribute. Nothing reads it on screen. Every new rule sits inside `@media print` and names `[data-print-page]`, and a test pins that for every rule of the sheet. The optional page-level switch from the card is not built (it would add a spec key). ## Tests - `ui/packages/core/src/utils/__tests__/date-display.shortLocaleOrder-34228.test.ts`: six non-month-first locales get their own face; `en` / `en-US` controls keep `Nov 15, '26`; the `short` face equals the default face with `alwaysShowYear`. - `ui/packages/plugin-detail/src/__tests__/recordDetailsPrintFace-34228.test.tsx`: a real `record:details` on a real print page (`SchemaRenderer` → `PageRenderer`) under `LocalizationProvider` `zh-CN` draws `2026年11月15日` (control: `en` draws `Nov 15, '26`). A rule of the stylesheet the page actually mounts matches the real select badge and the real lookup anchor, with the plain-text declarations. - `ui/packages/components/src/__tests__/page-print-paper-34228.test.tsx`: the rule text; every rule is scoped to the print page; the fit selectors match a data table and its cells on a real print page and never the running-region table or its cells. - Re-judged pins: `date-formatter-residue-4272.test.ts` pinned `8月 15, '26` / `Okt 15, '26`, the face this card retires. Its expected values are now `2026年8月15日` / `15. Okt. 2026`, and the `en` must-not-change pins are untouched. One assertion in `page-print-stylesheet-11958.test.tsx` was a substring (`data-print-running]) {`) that the new fit rule also contains. It now spells the running-region rule it meant (`:has(> table[data-print-running]) {`). <details><summary>Suite readings</summary> - `pnpm --dir ui exec vitest run --maxWorkers=2 packages/core/ packages/fields/ packages/components/ packages/plugin-detail/ packages/plugin-form/` on `dd8fec17`: `Test Files 1639 passed | 3 skipped (1642)`, `Tests 19666 passed | 77 skipped (19743)`, lock `VERDICT command-exit 0`. - The commit after `dd8fec17` (`23cdb744`) changes two test files only: a module-top `import '@object-ui/react-runtime'` that `check:test-source-alias` asked for. Their local rerun and `type-check` of `@object-ui/core`, `fields`, `components` and `plugin-detail`: NOT MEASURED, reason: four tries at the shared verify lock found no free turn (exit 99) behind a dogfood build and test run. CI's `Type Check UI` and `Test UI` carry both. - Derived gate subset at `23cdb744`: 37 of 39 families ran locally and exited 0 (`check:test-source-alias` after the fix above). `pnpm check:type-check-debt`: NOT MEASURED (exit 3; its closure build got no lock turn). `pnpm --dir ui exec vitest run scripts/`: not run, CI-owned. </details> ## Acceptance notes - **Item 3, measured and reported (not built).** Harness: the real DOM of a print page (`record:details` of one section, a 5-column `record:line_items`, then a `record:details` of two sections, `pricing` and `terms`, 11 short fields) rendered through `SchemaRenderer`, the console's compiled Tailwind sheet, Chromium 153 `page.pdf`, A4 portrait with 18/15 mm margins, `avoidBreakInside: true`, and the blank space read from `pdftotext -bbox`. - Does the block fit one sheet? Yes. The two-section block is about 472 pt tall on a 740 pt printable sheet (64%). So `break-inside: avoid` keeps a block that fits whole on one sheet. That is exactly what the published text says: "keep each block whole on one sheet when it fits". - With 3 lines, page 1 is 35% blank with the key on and 4% blank with it off. With 6 lines it is 18% on and 0% off. With the key off, the 6-line page splits inside the `pricing` section, which is the split the key exists to prevent. - A per-section rule (`break-inside: avoid` on each section card of `record:details`, the block-level rule left on every other block), injected into the same page: 3 lines leaves 4% blank on page 1, with `pricing` whole on page 1 and `terms` whole on page 2. 6 lines gives 18%, the same as today, because `pricing` does not fit the space left either. - For other print pages, a per-section rule lets one `record:details` block run across two sheets between sections. The showcase invoice's `invoice` and `amounts` sections could then print on different sheets. Every block that is not `record:details` would be unchanged. - A per-section rule would change what `avoidBreakInside` declares ("on every block"). The maintainer ruled C (record 6922479414): the rule stays, and the quote template is split into one `record:details` per section instead. Measured on the split template: page 1 is 4% blank with 3 lines and 18% with 6 (dev report 6923091449 on #34228). - The `short` face change shows on screen too: a `zh-CN` user's record page and list cells for a `format: 'short'` date now read `2026年11月15日`. This is the same defect, fixed at its producer. - **Seat condition (b), red first on the screen face.** Ablation, run once (not a test file): with `ui/packages/core/src/utils/date-display.ts` swapped back to its `cfcba539` version on head `98f29438`, `date-display.shortLocaleOrder-34228` and `recordDetailsPrintFace-34228` fail 8 of 13. `zh-CN`, `zh` and `ja-JP` expect `2026年11月15日` and receive `11月 15, '26`; `ko-KR`, `de-DE` and `en-GB` fail too; the `zh-CN` print page renders `Quote Date 11月 15, '26`. The `en` and `en-US` controls stay green. With the file restored from HEAD (blob `e9d02cc1` matches, `git diff HEAD` is empty) both files pass 13/13. Dev report 6925134613 on #34228. - **Dark theme does not regress.** The console already prints dark-theme text black: the app-shell print rule (`ui/packages/app-shell/src/styles.css`, `.dark, .dark *` get `color: #000 !important` inside `@media print`) does this, not the page print stylesheet. Measured on a body field value, A4, headless Chromium with `class="dark"` on the root: with background graphics on, `cfcba539` printed black text on the dark (18,18,18) canvas, which cannot be read, and `98f29438` prints it on white; with background graphics off, both builds print black on white. Still present in both builds and not changed here: with background graphics on, the `@page` margin band prints dark (18,18,18) in dark theme (measured in a scratch harness only); filed as #34446. - Column headings wrap where they must: `Discount %` prints as two lines in the 5-column table. ## Verification Chromium 153.0.8010.12 (`/opt/pw-browsers/chromium`), the real print-page DOM of the item-3 harness (`zh-CN`, 6 lines, prices `¥1,234,567.89`, totals `¥12,345,678.90`), the console's compiled stylesheet, inside a grey app shell. Geometry is read under `emulateMedia('print')` at the A4 printable width (680 px, transitions off); the PDF is read with `pdftoppm` and `pdftotext`. <details><summary>Before (main cfcba53) / after</summary> | reading | before | after | |:---|:---|:---| | `record:line_items` table vs its 630 px box | 647 px, overflows by 17.6 px; the box's right border runs through `Total` | 628 px, inside the box | | column widths (#, Product, Quantity, Sales Price, Discount %, Total) | 25 / 160 / 104 / 122 / 104 / 131 | 40 / 184 / 71 / 122 / 79 / 131 | | body / page background | `rgb(237, 237, 242)` / `rgb(246, 246, 249)` | `rgb(255, 255, 255)` / `rgb(255, 255, 255)` | | lookup `北京黑猫科技有限公司` | `rgb(0, 123, 255)` | `rgb(2, 8, 23)` = the page's text colour | | select `已批准` badge | green fill, 1px green border, full radius, 10px padding | transparent fill and border, radius 0, padding 0, text `rgb(2, 8, 23)` | | dates in the PDF | `11月 15, '26`, `12月 15, '26` | `2026年11月15日`, `2026年12月15日` | </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01FEkQ5pNoRjsgMJKr5utN33)_ <!-- signed: repo:objectstack#1 · os-project-manager · session_01FEkQ5pNoRjsgMJKr5utN33 --> --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: objectstack-fleet[bot] <332303061+objectstack-fleet[bot]@users.noreply.github.com>
1 parent 54f8e16 commit 6a7403b

9 files changed

Lines changed: 454 additions & 12 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@object-ui/core': patch
3+
'@object-ui/fields': patch
4+
'@object-ui/components': patch
5+
---
6+
7+
fix(console): a record page printed in a zh-CN organization shows dates in Chinese order, fits its line-items table to the sheet, and prints on white with plain values
8+
9+
Clause-②: no
10+
Metadata-protocol: none
11+
12+
A print page (a record page that declares `print`), printed by `@objectstack/service-print` or by the console's Print button, now reads as a document:
13+
14+
- **Dates in the locale's own order.** A `date` field authored `format: 'short'` drew `11月 15, '26` in `zh-CN`: only the month was translated into the English compact layout `Nov 15, '26`. That compact layout is now kept only for a locale that writes a date month first, then day, then year (`en`, `en-US`). Every other locale gets its own date with the year: `2026年11月15日` in `zh-CN` and `ja`, `15. Nov. 2026` in `de`, `15 Nov 2026` in `en-GB`. This is the face `formatDate` already drew for the default style with the year shown. It applies on screen as well as on paper, and to every reader of `formatDate(value, 'short', { locale })`.
15+
- **Tables fit the printable width.** On a print page, a data table's cells drop their on-screen minimum width and single line on paper, so the columns shrink and wrap to the box. Before, a five-column `record:line_items` table on A4 portrait with 15 mm margins was 647 px wide in a 630 px box. Its last column ran past the box's border, and that border was drawn through the column. The running header and footer table is not affected.
16+
- **White page, plain values.** On paper the page and everything around it are white instead of the app's background. A link, such as a lookup's referenced record, prints in the surrounding text colour with no underline. A select value drawn as a coloured badge prints as its label with no pill. The select badge carries a new inert attribute, `data-select-badge`, for this rule. Every rule sits inside `@media print` and is scoped to the print page, so the screen is unchanged.
17+
18+
`avoidBreakInside` is unchanged: it still keeps each block whole on one sheet when the block fits.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* objectstack#34228 — the print stylesheet puts a record page on paper as a
11+
* document: a data table fits the printable width, and the page prints on
12+
* white with links and select badges as plain text.
13+
*
14+
* Measured in Chromium (A4 portrait, 15 mm side margins) before the change: a
15+
* five-column `record:line_items` grid was 647 px wide in its 630 px box, its
16+
* last column under the box's right border; the page and its ancestors
17+
* printed the app's grey; a lookup printed in the link colour and a select as
18+
* a coloured pill. The readings are in the pull request.
19+
*
20+
* Two halves here, as in `page-print-stylesheet-11958.test.tsx`: the rules'
21+
* text, every one inside `@media print` and scoped to the print page, and the
22+
* rules' selectors against a real render — the fit rule reaches a data table
23+
* on the page and never the running-region table. ⚠️ `@media print` does not
24+
* apply under happy-dom, so nothing here shows a rule taking effect on paper.
25+
*/
26+
import { describe, it, expect, afterEach } from 'vitest';
27+
import { render, cleanup } from '@testing-library/react';
28+
import React from 'react';
29+
import { SchemaRenderer } from '@object-ui/react';
30+
import '../renderers';
31+
// `PageRenderer` reaches `react-page.tsx`, which loads this dynamically: pay the load at collection.
32+
import '@object-ui/react-runtime';
33+
import {
34+
buildPagePrintStylesheet,
35+
PRINT_PAGE_ATTRIBUTE,
36+
PRINT_PLAIN_BADGE_ATTRIBUTE,
37+
PRINT_RUNNING_ATTRIBUTE,
38+
} from '../renderers/layout/page-print';
39+
40+
afterEach(() => cleanup());
41+
42+
const PAGE = `[${PRINT_PAGE_ATTRIBUTE}]`;
43+
44+
/** Each rule of the sheet as `[selector, body]`, the `@page` rule left out. */
45+
function rulesOf(css: string): Array<[string, string]> {
46+
const rules: Array<[string, string]> = [];
47+
for (const line of css.split('\n')) {
48+
const rule = /^\s*(.+?) \{ (.*) \}$/.exec(line);
49+
if (rule && !rule[1].startsWith('@')) rules.push([rule[1], rule[2]]);
50+
}
51+
return rules;
52+
}
53+
54+
describe('the print stylesheet fits a data table to the printable width (objectstack#34228)', () => {
55+
it('drops the cells’ screen minimum width and single line, and holds the table to the box', () => {
56+
const css = buildPagePrintStylesheet({});
57+
const table = `${PAGE} table:not([${PRINT_RUNNING_ATTRIBUTE}])`;
58+
expect(css).toContain(`${table} { width: 100% !important; }`);
59+
expect(css).toContain(`${table} :is(th, td) { min-width: 0 !important; white-space: normal !important;`);
60+
});
61+
62+
it('every declaration shape carries the rules, whatever `print` declares', () => {
63+
for (const print of [{}, { avoidBreakInside: true }, { repeatHeader: true, repeatFooter: true }]) {
64+
expect(buildPagePrintStylesheet(print)).toContain(`${PAGE} table:not([${PRINT_RUNNING_ATTRIBUTE}]) { width: 100% !important; }`);
65+
}
66+
});
67+
});
68+
69+
describe('the print stylesheet prints the page on white, links and select badges as plain text (objectstack#34228)', () => {
70+
it('the page and every box around it print on white', () => {
71+
expect(buildPagePrintStylesheet({})).toContain(`:has(${PAGE}), ${PAGE} { background: #fff !important; }`);
72+
});
73+
74+
it('a link prints in the colour around it, with no underline', () => {
75+
expect(buildPagePrintStylesheet({})).toContain(`${PAGE} a[href] { color: inherit !important; text-decoration: none !important; }`);
76+
});
77+
78+
it('a select badge prints as its label: no fill, no border colour, no padding, no pill', () => {
79+
const [, body] = rulesOf(buildPagePrintStylesheet({})).find(([sel]) => sel === `${PAGE} [${PRINT_PLAIN_BADGE_ATTRIBUTE}]`)!;
80+
for (const declaration of [
81+
'background: none !important',
82+
'border-color: transparent !important',
83+
'color: inherit !important',
84+
'padding: 0 !important',
85+
'border-radius: 0 !important',
86+
]) {
87+
expect(body).toContain(declaration);
88+
}
89+
});
90+
91+
it('every rule sits inside `@media print` and names the print page, so the screen and every other page are untouched', () => {
92+
const css = buildPagePrintStylesheet({ avoidBreakInside: true, repeatHeader: true });
93+
expect(css.startsWith('@media print {')).toBe(true);
94+
const rules = rulesOf(css);
95+
expect(rules.length).toBeGreaterThan(0);
96+
for (const [selector] of rules) {
97+
expect(selector, selector).toContain(PAGE);
98+
}
99+
});
100+
});
101+
102+
const page = (print: Record<string, unknown>) => ({
103+
type: 'record',
104+
name: 'invoice_print',
105+
label: 'Invoice',
106+
print,
107+
regions: [
108+
{ name: 'header', components: [{ type: 'text', value: 'ACME Ltd' }] },
109+
{
110+
name: 'main',
111+
components: [
112+
{
113+
type: 'table',
114+
columns: [{ header: 'Product', accessorKey: 'p' }, { header: 'Total', accessorKey: 't' }],
115+
data: [{ p: 'Widget', t: '12,345,678.90' }],
116+
},
117+
],
118+
},
119+
],
120+
});
121+
122+
describe('the fit rule’s selectors against a real print page (objectstack#34228)', () => {
123+
it('reach a data table on the page and its cells, and never the running-region table', () => {
124+
const declaration = { repeatHeader: true };
125+
const { container } = render(<SchemaRenderer schema={page(declaration) as never} />);
126+
const [[tableSelector], [cellSelector]] = rulesOf(buildPagePrintStylesheet(declaration)).filter(([sel]) => sel.includes('table:not('));
127+
const running = container.querySelector(`table[${PRINT_RUNNING_ATTRIBUTE}]`)!;
128+
const data = [...container.querySelectorAll('table')].find((t) => t !== running)!;
129+
expect(running).not.toBeNull();
130+
expect(data).toBeDefined();
131+
expect(data.matches(tableSelector)).toBe(true);
132+
expect(data.querySelector('td')!.matches(cellSelector)).toBe(true);
133+
expect(data.querySelector('th')!.matches(cellSelector)).toBe(true);
134+
// CONTROL — the running-region table lays out regions, and keeps its own layout.
135+
expect(running.matches(tableSelector)).toBe(false);
136+
expect(running.querySelector(':scope > thead > tr > td')!.matches(cellSelector)).toBe(false);
137+
});
138+
});

‎ui/packages/components/src/__tests__/page-print-stylesheet-11958.test.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import React from 'react';
3535
import { ComponentRegistry } from '@object-ui/core';
3636
import { SchemaRenderer, usePrintPage } from '@object-ui/react';
3737
import '../renderers';
38+
// `PageRenderer` reaches `react-page.tsx`, which loads this dynamically: pay the load at collection.
39+
import '@object-ui/react-runtime';
3840
import { buildPagePrintStylesheet, PRINT_PAGE_ATTRIBUTE, PRINT_RUNNING_ATTRIBUTE } from '../renderers/layout/page-print';
3941

4042
afterEach(() => cleanup());
@@ -84,7 +86,9 @@ describe('buildPagePrintStylesheet — one print-CSS rule per declared key (obje
8486
expect(css).toContain(`${table} > thead { display: table-header-group !important; }`);
8587
expect(css).toContain(`${table} > tfoot { display: table-footer-group !important; }`);
8688
}
87-
expect(buildPagePrintStylesheet({})).not.toContain(PRINT_RUNNING_ATTRIBUTE + ']) {');
89+
// The running region's block rule, spelled whole: the data-table fit rule
90+
// (objectstack#34228) names the attribute too, to exclude that table.
91+
expect(buildPagePrintStylesheet({})).not.toContain(`:has(> table[${PRINT_RUNNING_ATTRIBUTE}]) {`);
8892
expect(buildPagePrintStylesheet({})).not.toContain(`${table} {`);
8993
});
9094

‎ui/packages/components/src/renderers/layout/page-print.ts‎

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@
3737
* not repeat;
3838
* - `avoidBreakInside` → `break-inside: avoid` on every block of a region.
3939
*
40-
* Besides the keys, two rules every print page needs: everything on the
40+
* Besides the keys, the rules every print page needs: everything on the
4141
* document that is not the print page, an ancestor of it, or inside it is
4242
* hidden on paper (the console chrome, and the record view's own panels
43-
* around the page), and the ancestors' viewport-height clipping is released
44-
* so the page runs onto as many sheets as it needs.
43+
* around the page); the ancestors' viewport-height clipping is released so
44+
* the page runs onto as many sheets as it needs; a data table fits the
45+
* printable width; and the page prints on white, with links and select
46+
* badges as plain text (objectstack#34228).
4547
*
4648
* ⚠️ Values are WRITTEN INTO A STYLESHEET, so only the spec's own values pass:
4749
* an enum member for `paperSize` / `orientation`, a finite non-negative number
@@ -61,6 +63,13 @@ export const PRINT_PAGE_ATTRIBUTE = 'data-print-page';
6163
/** The DOM attribute on the running-region table `PageRenderer` draws for `repeatHeader` / `repeatFooter`. */
6264
export const PRINT_RUNNING_ATTRIBUTE = 'data-print-running';
6365

66+
/**
67+
* The DOM attribute on a value drawn as a coloured badge that prints as plain
68+
* text: the select cell's option badge (`SelectCellRenderer` in
69+
* `@object-ui/fields`, which spells the same literal).
70+
*/
71+
export const PRINT_PLAIN_BADGE_ATTRIBUTE = 'data-select-badge';
72+
6473
const PAPER_SIZES: ReadonlySet<string> = new Set(['A4', 'A5', 'Letter', 'Legal']);
6574
const ORIENTATIONS: ReadonlySet<string> = new Set(['portrait', 'landscape']);
6675
const MARGIN_SIDES = ['top', 'right', 'bottom', 'left'] as const;
@@ -111,6 +120,42 @@ function runningRegionRules(print: PagePrintDeclaration, page: string): string[]
111120
];
112121
}
113122

123+
/**
124+
* A data table fits the sheet's printable width (objectstack#34228). On screen
125+
* a table's cells keep a comfortable minimum width and one line, and the table
126+
* scrolls sideways inside its box when they do not fit; paper has no scroll,
127+
* so the table ran past its box: measured in Chromium on A4 portrait with
128+
* 15 mm margins, a five-column `record:line_items` grid was 647 px wide in a
129+
* 630 px box, its last column under the box's right border. On paper the
130+
* minimum widths and the single line are dropped, so the columns shrink and
131+
* wrap to the box. The running-region table is excluded: it lays out regions,
132+
* not data.
133+
*/
134+
function paperFitRules(page: string): string[] {
135+
const table = `${page} table:not([${PRINT_RUNNING_ATTRIBUTE}])`;
136+
return [
137+
`${table} { width: 100% !important; }`,
138+
`${table} :is(th, td) { min-width: 0 !important; white-space: normal !important; overflow-wrap: break-word; }`,
139+
];
140+
}
141+
142+
/**
143+
* A printed document is a white sheet with plain values (objectstack#34228):
144+
* the page and every box around it print on white, not the app's background;
145+
* a link (a lookup's reference) prints as plain text in the colour around
146+
* it; and a select value drawn as a coloured badge
147+
* ({@link PRINT_PLAIN_BADGE_ATTRIBUTE}) prints as its label in that colour,
148+
* with no pill. The `!important` beats the inline colours a hex option
149+
* colour sets on its badge.
150+
*/
151+
function paperFaceRules(page: string): string[] {
152+
return [
153+
`:has(${page}), ${page} { background: #fff !important; }`,
154+
`${page} a[href] { color: inherit !important; text-decoration: none !important; }`,
155+
`${page} [${PRINT_PLAIN_BADGE_ATTRIBUTE}] { background: none !important; border-color: transparent !important; color: inherit !important; padding: 0 !important; border-radius: 0 !important; font-size: inherit !important; font-weight: inherit !important; }`,
156+
];
157+
}
158+
114159
/**
115160
* Build the print stylesheet for a page that declares `print`. Pure: the same
116161
* declaration always yields the same text, so it is pinned as text.
@@ -135,5 +180,6 @@ export function buildPagePrintStylesheet(print: PagePrintDeclaration): string {
135180
if (print.avoidBreakInside === true) {
136181
rules.push(`${page} [data-region] > * { break-inside: avoid; }`);
137182
}
183+
rules.push(...paperFitRules(page), ...paperFaceRules(page));
138184
return `@media print {\n ${rules.join('\n ')}\n}\n`;
139185
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* objectstack#34228 — `formatDate`'s `'short'` face follows the locale's own
11+
* date order.
12+
*
13+
* The face was a fixed English layout with only the month token localized,
14+
* so a `zh-CN` record page, and the document `service-print` printed from it,
15+
* read `11月 15, '26`. The compact layout is kept where it IS the locale's
16+
* order (month → day → year); every other locale gets its own face with the
17+
* year, the default face's with the year shown.
18+
*/
19+
import { describe, it, expect } from 'vitest';
20+
import { formatDate } from '../date-display';
21+
22+
/** A date-only value: its day is the same in every zone. */
23+
const DAY = '2026-11-15';
24+
25+
describe("formatDate 'short' — the locale's date order (objectstack#34228)", () => {
26+
it.each([
27+
['zh-CN', '2026年11月15日'],
28+
['zh', '2026年11月15日'],
29+
['ja-JP', '2026年11月15日'],
30+
['ko-KR', '2026년 11월 15일'],
31+
['de-DE', '15. Nov. 2026'],
32+
['en-GB', '15 Nov 2026'],
33+
])('%s writes the date in its own order, with the year', (locale, face) => {
34+
expect(formatDate(DAY, 'short', { locale })).toBe(face);
35+
});
36+
37+
it.each([
38+
['en', "Nov 15, '26"],
39+
['en-US', "Nov 15, '26"],
40+
])('CONTROL — %s, a month-first locale, keeps the compact face', (locale, face) => {
41+
expect(formatDate(DAY, 'short', { locale })).toBe(face);
42+
});
43+
44+
it('the face is the default face with the year shown, so the two never drift apart', () => {
45+
expect(formatDate(DAY, 'short', { locale: 'zh-CN' })).toBe(formatDate(DAY, undefined, { locale: 'zh-CN', alwaysShowYear: true }));
46+
});
47+
});

‎ui/packages/core/src/utils/date-display.ts‎

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,36 @@ function inZone(timeZone: string | undefined): Intl.DateTimeFormatOptions {
154154
return timeZone ? { timeZone } : {};
155155
}
156156

157+
/** Per locale tag (`''` for the runtime default): whether it writes a date month → day → year. */
158+
const monthDayYearLocales = new Map<string, boolean>();
159+
160+
/**
161+
* Whether `locale` writes a date with the month first, then the day, then the
162+
* year (`en-US`: `Nov 15, 2026`), the order `formatDate`'s compact `'short'`
163+
* face is laid out in. `zh-CN` (`2026年11月15日`), `de` (`15. Nov. 2026`) and
164+
* `en-GB` (`15 Nov 2026`) do not. Asked of `Intl` itself, so no table of
165+
* locales is kept here. A tag `Intl` refuses answers `true`: the compact face
166+
* then runs as it always did and meets the same refusal.
167+
*/
168+
function writesMonthDayYear(locale: string | undefined): boolean {
169+
const key = locale ?? '';
170+
let answer = monthDayYearLocales.get(key);
171+
if (answer === undefined) {
172+
try {
173+
const order = new Intl.DateTimeFormat(locale, { year: 'numeric', month: 'short', day: 'numeric' })
174+
.formatToParts(new Date(2026, 10, 15))
175+
.filter((p) => p.type === 'year' || p.type === 'month' || p.type === 'day')
176+
.map((p) => p.type)
177+
.join(',');
178+
answer = order === 'month,day,year';
179+
} catch {
180+
answer = true;
181+
}
182+
monthDayYearLocales.set(key, answer);
183+
}
184+
return answer;
185+
}
186+
157187
const dayPartsFormats = new Map<string, Intl.DateTimeFormat>();
158188

159189
/** The calendar day `date` falls on in `timeZone` (the viewer's when `undefined`); `month` is 0-based. */
@@ -595,12 +625,22 @@ export function formatDate(value: string | Date | number, style?: string, option
595625
const zone = zoneFor(date);
596626

597627
if (effectiveStyle === 'short') {
598-
// Compact format for mobile: "Jan 15, '24" / "1月 15, '24".
599-
// Only the MONTH token is localized: the surrounding compact shape (day,
600-
// apostrophe + 2-digit year) is a deliberate fixed layout for narrow
601-
// cards, not a locale-derived one. The tag comes from `options.locale`
602-
// like the default branch below — hardcoding `'en-US'` here made this the
603-
// one branch that ignored a locale its caller had threaded (objectui#4272).
628+
// Compact format with the year: "Jan 15, '24". The tag comes from
629+
// `options.locale` like the default branch below — hardcoding `'en-US'`
630+
// here made this the one branch that ignored a locale its caller had
631+
// threaded (objectui#4272).
632+
//
633+
// The compact layout (day, apostrophe + 2-digit year after the month) is
634+
// the order a month-first locale writes a date in, and only there. A
635+
// locale that writes the date in another order gets its OWN face with the
636+
// year, the one the default branch below draws with the year shown
637+
// (objectstack#34228): `2026年11月15日` in `zh-CN`, `15. Nov. 2026` in
638+
// `de`. Before, only the month token was localized into the fixed layout,
639+
// so a `zh-CN` record page and its printed document read `11月 15, '26` —
640+
// an English-order date in Chinese month words.
641+
if (!writesMonthDayYear(options?.locale)) {
642+
return date.toLocaleDateString(options?.locale, { year: 'numeric', month: 'short', day: 'numeric', ...inZone(zone) });
643+
}
604644
const month = date.toLocaleDateString(options?.locale, { month: 'short', ...inZone(zone) });
605645
const { day, year } = dayIn(date, zone);
606646
return `${month} ${day}, '${String(year).slice(-2)}`;

‎ui/packages/fields/src/__tests__/date-formatter-residue-4272.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,14 @@ const OCT = new Date(2026, 9, 15, 12, 0, 0);
5353
const INSTANT = new Date(2024, 0, 5, 8, 30, 0);
5454

5555
describe("formatDate 'short' honors the threaded locale (objectui#4272)", () => {
56+
/**
57+
* The two non-English faces below were `8月 15, '26` and `Okt 15, '26` until
58+
* objectstack#34228: only the month token was localized into the English
59+
* compact layout. A locale that does not write month → day → year now gets
60+
* its own face with the year; the month is still the threaded locale's.
61+
*/
5662
it('zh renders the Chinese month, not the hardcoded English one', () => {
57-
expect(formatDate(AUG, 'short', { locale: 'zh' })).toBe("8月 15, '26");
63+
expect(formatDate(AUG, 'short', { locale: 'zh' })).toBe('2026年8月15日');
5864
});
5965

6066
/**
@@ -63,7 +69,7 @@ describe("formatDate 'short' honors the threaded locale (objectui#4272)", () =>
6369
* abbreviates it `Aug` — so this case is deliberately October.
6470
*/
6571
it('de renders the German month abbreviation', () => {
66-
expect(formatDate(OCT, 'short', { locale: 'de' })).toBe("Okt 15, '26");
72+
expect(formatDate(OCT, 'short', { locale: 'de' })).toBe('15. Okt. 2026');
6773
});
6874

6975
/**

0 commit comments

Comments
 (0)