@tristan2828/ui-foundation 4.78.0 → 4.80.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/conventions/docs/design-language.md +87 -28
- package/dist/components/app/app-shell-bottom-bar.js +31 -6
- package/dist/components/app/app-shell-nav.d.ts +58 -1
- package/dist/components/app/app-shell-nav.js +36 -3
- package/dist/components/app/app-shell.d.ts +11 -4
- package/dist/components/app/app-shell.js +40 -12
- package/dist/components/app/markdown.js +7 -2
- package/dist/components/app/milkdown-editor.js +65 -5
- package/dist/components/app/rich-text-table-grips.d.ts +5 -1
- package/dist/components/app/rich-text-table-grips.js +120 -6
- package/dist/components/app/rich-text-table.d.ts +23 -1
- package/dist/components/app/rich-text-table.js +98 -8
- package/dist/lib/markdown-highlight.js +2 -1
- package/dist/lib/markdown-merge.js +18 -6
- package/dist/lib/markdown-table-widths.d.ts +51 -0
- package/dist/lib/markdown-table-widths.js +179 -0
- package/package.json +1 -1
- package/styles/index.css +27 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// A table's column widths, kept in the Markdown as one comment after the
|
|
2
|
+
// table, naming its columns (the developer's choice, 2026-11-09):
|
|
3
|
+
//
|
|
4
|
+
// | Item | Room | Cost |
|
|
5
|
+
// | ---- | ---- | ---: |
|
|
6
|
+
// | Paint | Hall | $42 |
|
|
7
|
+
//
|
|
8
|
+
// <!-- table widths: Item=140, Cost=72 -->
|
|
9
|
+
//
|
|
10
|
+
// GFM has nowhere to put a width, and the table has to stay a table to
|
|
11
|
+
// everything else that reads it. A comment hides itself on GitHub and in
|
|
12
|
+
// other renderers, and a chat or the connector can read or change it.
|
|
13
|
+
// By name, so a column moved outside the editor keeps its width: a
|
|
14
|
+
// column named nowhere is automatic (fit to its words), a name no column
|
|
15
|
+
// has is ignored. A blank or repeated header can't be named, and is its
|
|
16
|
+
// place instead (`#3=72`). A header that isn't plain words is quoted, as
|
|
17
|
+
// JSON (`"Cost, each"=72`).
|
|
18
|
+
//
|
|
19
|
+
// It's the one piece of HTML the rich-text editor writes, and neither
|
|
20
|
+
// renderer shows it: `remarkTableWidths` takes it off the tree and puts
|
|
21
|
+
// each width on its column's cells. A comment that isn't this one, or
|
|
22
|
+
// isn't straight after a table, is any other raw HTML: shown as its
|
|
23
|
+
// characters, never lost.
|
|
24
|
+
//
|
|
25
|
+
// Widths are pixels. In the editor they sit on the cells (ProseMirror's
|
|
26
|
+
// `colwidth`), so a column moved, added or deleted there, and a row added
|
|
27
|
+
// or sorted there, keeps them right; the comment is written from the
|
|
28
|
+
// cells on each save. Styled in styles/index.css ("Tables in written
|
|
29
|
+
// text"): a phone keeps fitting columns to their words.
|
|
30
|
+
export const MIN_COLUMN_WIDTH = 56;
|
|
31
|
+
export const MAX_COLUMN_WIDTH = 1200;
|
|
32
|
+
/** The sizes a dragged edge catches on, within `COLUMN_CATCH` pixels. */
|
|
33
|
+
export const COLUMN_SIZES = [
|
|
34
|
+
{ label: 'S', width: 96 },
|
|
35
|
+
{ label: 'M', width: 160 },
|
|
36
|
+
{ label: 'L', width: 256 },
|
|
37
|
+
{ label: 'XL', width: 384 },
|
|
38
|
+
];
|
|
39
|
+
export const COLUMN_CATCH = 10;
|
|
40
|
+
/** A width within the limits, whole pixels. */
|
|
41
|
+
export function clampColumnWidth(width) {
|
|
42
|
+
return Math.min(MAX_COLUMN_WIDTH, Math.max(MIN_COLUMN_WIDTH, Math.round(width)));
|
|
43
|
+
}
|
|
44
|
+
/** A dragged width: within the limits, and on a size when it's that close to one. */
|
|
45
|
+
export function catchColumnWidth(width) {
|
|
46
|
+
const clamped = clampColumnWidth(width);
|
|
47
|
+
const size = COLUMN_SIZES.find((one) => Math.abs(one.width - clamped) <= COLUMN_CATCH);
|
|
48
|
+
return size ? { width: size.width, size: size.label } : { width: clamped, size: null };
|
|
49
|
+
}
|
|
50
|
+
const COMMENT = /^<!--\s*table widths:\s*(.*?)\s*-->\s*$/s;
|
|
51
|
+
// One `key=width`: a quoted name, a place (`#3`), or bare words.
|
|
52
|
+
const ENTRY = /\s*(?:("(?:[^"\\]|\\.)*")|#(\d+)|([^,="]+?))\s*=\s*(\d+)\s*(,|$)/y;
|
|
53
|
+
/** The entries of a widths comment, or null when it isn't one. */
|
|
54
|
+
export function parseTableWidths(comment) {
|
|
55
|
+
const body = COMMENT.exec(comment.trim())?.[1];
|
|
56
|
+
if (!body)
|
|
57
|
+
return null;
|
|
58
|
+
const entries = [];
|
|
59
|
+
ENTRY.lastIndex = 0;
|
|
60
|
+
for (;;) {
|
|
61
|
+
const at = ENTRY.lastIndex;
|
|
62
|
+
const found = ENTRY.exec(body);
|
|
63
|
+
if (!found)
|
|
64
|
+
return null;
|
|
65
|
+
const [, quoted, position, bare, width, separator] = found;
|
|
66
|
+
let name = bare;
|
|
67
|
+
if (quoted !== undefined) {
|
|
68
|
+
try {
|
|
69
|
+
name = JSON.parse(quoted);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (position !== undefined)
|
|
76
|
+
entries.push({ position: Number(position), width: Number(width) });
|
|
77
|
+
else
|
|
78
|
+
entries.push({ name: cleanHeader(name), width: Number(width) });
|
|
79
|
+
if (!separator)
|
|
80
|
+
break;
|
|
81
|
+
// A `,` with nothing after it isn't one of ours.
|
|
82
|
+
if (ENTRY.lastIndex === at || ENTRY.lastIndex >= body.length)
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
return entries;
|
|
86
|
+
}
|
|
87
|
+
const cleanHeader = (text) => text.replace(/\s+/g, ' ').trim();
|
|
88
|
+
/** Each column's width, in order: null is automatic. */
|
|
89
|
+
export function resolveTableWidths(entries, headers) {
|
|
90
|
+
const names = headers.map(cleanHeader);
|
|
91
|
+
const widths = headers.map(() => null);
|
|
92
|
+
const used = new Set();
|
|
93
|
+
for (const entry of entries) {
|
|
94
|
+
const index = 'position' in entry
|
|
95
|
+
? entry.position - 1
|
|
96
|
+
: names.findIndex((name, column) => name === entry.name && name !== '' && !used.has(column));
|
|
97
|
+
if (index < 0 || index >= headers.length)
|
|
98
|
+
continue;
|
|
99
|
+
used.add(index);
|
|
100
|
+
widths[index] = clampColumnWidth(entry.width);
|
|
101
|
+
}
|
|
102
|
+
return widths;
|
|
103
|
+
}
|
|
104
|
+
// A name that can sit bare in the comment. `--` could end the comment, and
|
|
105
|
+
// `#3` would read as a place.
|
|
106
|
+
const isBare = (name) => /^[^,="\\<>\r\n]+$/.test(name) && !name.includes('--') && !/^#\d+$/.test(name);
|
|
107
|
+
// Quoted as JSON, with what could end the comment as an escape.
|
|
108
|
+
const quoted = (name) => JSON.stringify(name).replace(/-/g, '\\u002d').replace(/>/g, '\\u003e');
|
|
109
|
+
/** The comment for these columns' widths, or null when they're all automatic. */
|
|
110
|
+
export function formatTableWidths(headers, widths) {
|
|
111
|
+
const names = headers.map(cleanHeader);
|
|
112
|
+
const entries = [];
|
|
113
|
+
names.forEach((name, column) => {
|
|
114
|
+
const width = widths[column];
|
|
115
|
+
if (width === null || width === undefined)
|
|
116
|
+
return;
|
|
117
|
+
const nameable = name !== '' && names.indexOf(name) === names.lastIndexOf(name);
|
|
118
|
+
entries.push(`${nameable ? (isBare(name) ? name : quoted(name)) : `#${column + 1}`}=${width}`);
|
|
119
|
+
});
|
|
120
|
+
return entries.length ? `<!-- table widths: ${entries.join(', ')} -->` : null;
|
|
121
|
+
}
|
|
122
|
+
// `==words==` and `=={green}words==`: text to a tree the highlights
|
|
123
|
+
// haven't been read from (the merge's), so a header's name is the same
|
|
124
|
+
// in every tree.
|
|
125
|
+
const HIGHLIGHT = /==(?:\{(?:amber|yellow|green|red)\})?(\S(?:.*?\S)?)==/g;
|
|
126
|
+
function textOf(node) {
|
|
127
|
+
if (typeof node.value === 'string')
|
|
128
|
+
return node.value;
|
|
129
|
+
return (node.children ?? []).map(textOf).join('');
|
|
130
|
+
}
|
|
131
|
+
/** A table node's header texts, as a reader sees them. */
|
|
132
|
+
export function tableHeaders(table) {
|
|
133
|
+
return (table.children?.[0]?.children ?? []).map((cell) => cleanHeader(textOf(cell).replace(HIGHLIGHT, '$1')));
|
|
134
|
+
}
|
|
135
|
+
/** The widths the block after a table gives it, or null when it gives none. */
|
|
136
|
+
export function widthsAfterTable(table, next) {
|
|
137
|
+
// The rich-text editor's commonmark preset wraps a block of HTML in a
|
|
138
|
+
// paragraph before this reads it.
|
|
139
|
+
const block = next?.type === 'paragraph' && next.children?.length === 1 ? next.children[0] : next;
|
|
140
|
+
if (block?.type !== 'html' || typeof block.value !== 'string')
|
|
141
|
+
return null;
|
|
142
|
+
const entries = parseTableWidths(block.value);
|
|
143
|
+
return entries && resolveTableWidths(entries, tableHeaders(table));
|
|
144
|
+
}
|
|
145
|
+
function putOnCells(table, widths) {
|
|
146
|
+
for (const row of table.children ?? []) {
|
|
147
|
+
;
|
|
148
|
+
(row.children ?? []).forEach((cell, column) => {
|
|
149
|
+
const width = widths[column];
|
|
150
|
+
if (width === null || width === undefined)
|
|
151
|
+
return;
|
|
152
|
+
// The editor's parser reads `columnWidth`; the read view's HTML
|
|
153
|
+
// takes `hProperties`.
|
|
154
|
+
cell.columnWidth = width;
|
|
155
|
+
cell.data = { ...cell.data, hProperties: { dataColumnWidth: String(width), style: `--column-width: ${width}px` } };
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function walk(parent) {
|
|
160
|
+
const children = parent.children;
|
|
161
|
+
if (!children)
|
|
162
|
+
return;
|
|
163
|
+
for (let index = 0; index < children.length; index++) {
|
|
164
|
+
const node = children[index];
|
|
165
|
+
if (node.type !== 'table') {
|
|
166
|
+
walk(node);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const widths = widthsAfterTable(node, children[index + 1]);
|
|
170
|
+
if (!widths)
|
|
171
|
+
continue;
|
|
172
|
+
putOnCells(node, widths);
|
|
173
|
+
children.splice(index + 1, 1);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/** Takes each widths comment off the tree and puts its widths on its table's cells. */
|
|
177
|
+
export function remarkTableWidths() {
|
|
178
|
+
return (tree) => walk(tree);
|
|
179
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tristan2828/ui-foundation",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.80.0",
|
|
4
4
|
"description": "Shared UI foundation for contract-first, database-backed React apps: shadcn primitives, DataTable/EntityForm composites, auth, the gateway error seam, design tokens, lint rules and the AI playbook.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
package/styles/index.css
CHANGED
|
@@ -360,6 +360,10 @@
|
|
|
360
360
|
- boxed: a rounded box, roomier cells, the header row on the muted
|
|
361
361
|
fill.
|
|
362
362
|
Both stripe their body rows (zebra, as DataTable's).
|
|
363
|
+
A column with a set width (src/lib/markdown-table-widths.ts) is exactly
|
|
364
|
+
that wide, its words wrapping inside it, from a phone's width up; on a
|
|
365
|
+
phone columns still fit their words. On paper the widths are only
|
|
366
|
+
preferences, so the table shrinks to the page rather than run off it.
|
|
363
367
|
Row headers (a header column: src/lib/markdown-table.ts) sit on the
|
|
364
368
|
muted fill, semibold. A coloured cell (its whole text one highlight)
|
|
365
369
|
is painted in that wash, its words' own wash dropped. Unlayered, so a
|
|
@@ -397,6 +401,16 @@
|
|
|
397
401
|
white-space: nowrap;
|
|
398
402
|
font-variant-numeric: tabular-nums;
|
|
399
403
|
}
|
|
404
|
+
@media (min-width: 768px) {
|
|
405
|
+
.prose-table :is(td, th)[data-column-width] {
|
|
406
|
+
box-sizing: border-box;
|
|
407
|
+
width: var(--column-width);
|
|
408
|
+
min-width: var(--column-width);
|
|
409
|
+
max-width: var(--column-width);
|
|
410
|
+
white-space: normal;
|
|
411
|
+
overflow-wrap: anywhere;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
400
414
|
.prose-table th:not([scope="row"], [data-row-header]) {
|
|
401
415
|
background: var(--heading-band);
|
|
402
416
|
font-weight: 600;
|
|
@@ -507,6 +521,19 @@
|
|
|
507
521
|
[data-print-notes] table {
|
|
508
522
|
border-collapse: collapse;
|
|
509
523
|
}
|
|
524
|
+
/* Paper can't scroll: the widths are what the columns would like, and
|
|
525
|
+
the table takes the page's width when they add up to more. */
|
|
526
|
+
[data-print-notes] [data-slot="table-container"] > table {
|
|
527
|
+
max-width: 100%;
|
|
528
|
+
}
|
|
529
|
+
[data-print-notes] [data-slot="table-container"] :is(td, th)[data-column-width] {
|
|
530
|
+
box-sizing: border-box;
|
|
531
|
+
width: var(--column-width);
|
|
532
|
+
min-width: 0;
|
|
533
|
+
max-width: none;
|
|
534
|
+
white-space: normal;
|
|
535
|
+
overflow-wrap: anywhere;
|
|
536
|
+
}
|
|
510
537
|
[data-print-notes] tr {
|
|
511
538
|
break-inside: avoid;
|
|
512
539
|
}
|