@tai42/studio-sdk 0.12.0 → 0.14.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/dist/components/code-block.d.ts.map +1 -1
- package/dist/components/code-block.js +26 -13
- package/dist/components/code-block.js.map +1 -1
- package/dist/components/components.css +20 -0
- package/dist/components/copy-field.d.ts.map +1 -1
- package/dist/components/copy-field.js +8 -73
- package/dist/components/copy-field.js.map +1 -1
- package/dist/components/date-range-picker.d.ts +73 -0
- package/dist/components/date-range-picker.d.ts.map +1 -0
- package/dist/components/date-range-picker.js +146 -0
- package/dist/components/date-range-picker.js.map +1 -0
- package/dist/components/dialog.d.ts +19 -1
- package/dist/components/dialog.d.ts.map +1 -1
- package/dist/components/dialog.js +16 -13
- package/dist/components/dialog.js.map +1 -1
- package/dist/components/dirty-guard.d.ts +28 -0
- package/dist/components/dirty-guard.d.ts.map +1 -0
- package/dist/components/dirty-guard.js +93 -0
- package/dist/components/dirty-guard.js.map +1 -0
- package/dist/components/extension-picker.d.ts.map +1 -1
- package/dist/components/extension-picker.js +7 -0
- package/dist/components/extension-picker.js.map +1 -1
- package/dist/components/form-dialog.d.ts +45 -0
- package/dist/components/form-dialog.d.ts.map +1 -0
- package/dist/components/form-dialog.js +71 -0
- package/dist/components/form-dialog.js.map +1 -0
- package/dist/components/json-tree.d.ts +7 -1
- package/dist/components/json-tree.d.ts.map +1 -1
- package/dist/components/json-tree.js +300 -13
- package/dist/components/json-tree.js.map +1 -1
- package/dist/components/markdown.d.ts +60 -0
- package/dist/components/markdown.d.ts.map +1 -0
- package/dist/components/markdown.js +435 -0
- package/dist/components/markdown.js.map +1 -0
- package/dist/components/tabs.d.ts +3 -1
- package/dist/components/tabs.d.ts.map +1 -1
- package/dist/components/tabs.js +2 -2
- package/dist/components/tabs.js.map +1 -1
- package/dist/hooks/useApi.d.ts +56 -28
- package/dist/hooks/useApi.d.ts.map +1 -1
- package/dist/hooks/useClipboardCopy.d.ts +26 -0
- package/dist/hooks/useClipboardCopy.d.ts.map +1 -0
- package/dist/hooks/useClipboardCopy.js +72 -0
- package/dist/hooks/useClipboardCopy.js.map +1 -0
- package/dist/index.d.ts +9 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/navigation/context.d.ts +21 -1
- package/dist/navigation/context.d.ts.map +1 -1
- package/dist/navigation/context.js +229 -7
- package/dist/navigation/context.js.map +1 -1
- package/dist/navigation/index.d.ts +2 -2
- package/dist/navigation/index.d.ts.map +1 -1
- package/dist/navigation/index.js +1 -1
- package/dist/navigation/index.js.map +1 -1
- package/dist/navigation/types.d.ts +11 -0
- package/dist/navigation/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/components/code-block.tsx +72 -9
- package/src/components/components.css +20 -0
- package/src/components/copy-field.tsx +25 -81
- package/src/components/date-range-picker.tsx +256 -0
- package/src/components/dialog.tsx +59 -18
- package/src/components/dirty-guard.tsx +153 -0
- package/src/components/extension-picker.tsx +12 -0
- package/src/components/form-dialog.tsx +133 -0
- package/src/components/json-tree.tsx +477 -42
- package/src/components/markdown.tsx +552 -0
- package/src/components/tabs.tsx +10 -1
- package/src/hooks/useClipboardCopy.ts +105 -0
- package/src/index.ts +19 -0
- package/src/navigation/context.tsx +277 -7
- package/src/navigation/index.ts +3 -0
- package/src/navigation/types.ts +12 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `Markdown` — a safe renderer for the markdown agents emit, on the design
|
|
3
|
+
* system's prose ground (`tai-prose`). Supports a deliberate subset (headings,
|
|
4
|
+
* emphasis, lists, blockquotes, thematic breaks, links, inline and fenced code),
|
|
5
|
+
* rendered entirely through React elements and text children.
|
|
6
|
+
*
|
|
7
|
+
* SAFETY: never an HTML sink — no `dangerouslySetInnerHTML`, no raw-HTML
|
|
8
|
+
* passthrough, no network. A link is admitted only when `safeHttpUrl` accepts an
|
|
9
|
+
* absolute `http(s)` URL; every other spelling renders as plain link text.
|
|
10
|
+
* `` collapses to its `alt` text, so no source URL is fetched. Fenced
|
|
11
|
+
* code goes to `CodeBlock`, itself text-only.
|
|
12
|
+
*/
|
|
13
|
+
import { useMemo, type ReactNode } from 'react';
|
|
14
|
+
|
|
15
|
+
import { CodeBlock } from './code-block';
|
|
16
|
+
import { safeHttpUrl } from './primitives';
|
|
17
|
+
|
|
18
|
+
/** The heading levels ATX syntax can express, `#` through `######`. */
|
|
19
|
+
export type HeadingLevel = 1 | 2 | 3 | 4 | 5 | 6;
|
|
20
|
+
|
|
21
|
+
/** A parsed block. Inline markup inside `text` is resolved at render time. */
|
|
22
|
+
export type MarkdownBlock =
|
|
23
|
+
| { readonly type: 'heading'; readonly level: HeadingLevel; readonly text: string }
|
|
24
|
+
| { readonly type: 'paragraph'; readonly text: string }
|
|
25
|
+
| { readonly type: 'code'; readonly language: string | undefined; readonly code: string }
|
|
26
|
+
| {
|
|
27
|
+
readonly type: 'list';
|
|
28
|
+
readonly ordered: boolean;
|
|
29
|
+
readonly items: readonly string[];
|
|
30
|
+
/** The first item's number on an ordered list, so the render can offset `<ol start>`. */
|
|
31
|
+
readonly start?: number;
|
|
32
|
+
}
|
|
33
|
+
| { readonly type: 'blockquote'; readonly children: readonly MarkdownBlock[] }
|
|
34
|
+
| { readonly type: 'thematicBreak' };
|
|
35
|
+
|
|
36
|
+
export interface MarkdownProps {
|
|
37
|
+
/** The markdown source. Rendered as an escaped-by-construction React tree. */
|
|
38
|
+
readonly markdown: string;
|
|
39
|
+
/** Appended to `tai-prose` so a caller can position the block without losing its paint. */
|
|
40
|
+
readonly className?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// -- block grammar -----------------------------------------------------------
|
|
44
|
+
|
|
45
|
+
const BLANK = /^[ \t]*$/;
|
|
46
|
+
/** A fence opener: three-or-more backticks or tildes, then an info string. */
|
|
47
|
+
const FENCE_OPEN = /^[ \t]{0,3}(`{3,}|~{3,})[ \t]*(.*)$/;
|
|
48
|
+
/**
|
|
49
|
+
* An ATX heading: 1–6 hashes, a required space, then text. A closing hash run is
|
|
50
|
+
* stripped only when whitespace separates it from the content, so `# C#` keeps
|
|
51
|
+
* its literal `C#`.
|
|
52
|
+
*/
|
|
53
|
+
const HEADING = /^[ \t]{0,3}(#{1,6})[ \t]+(.*?)(?:[ \t]+#+)?[ \t]*$/;
|
|
54
|
+
/** A thematic break: three-or-more `-`, `*` or `_`, spaces allowed between. */
|
|
55
|
+
const THEMATIC_BREAK = /^[ \t]{0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/;
|
|
56
|
+
/** A blockquote line: a `>` marker and its (optionally space-prefixed) content. */
|
|
57
|
+
const BLOCKQUOTE = /^[ \t]{0,3}>[ \t]?(.*)$/;
|
|
58
|
+
/** An unordered list item: a `-`, `*` or `+` bullet, a space, then content. */
|
|
59
|
+
const UNORDERED_ITEM = /^[ \t]*[-*+][ \t]+(.*)$/;
|
|
60
|
+
/** An ordered list item: a start number, a `.` or `)` delimiter, a space, then content. */
|
|
61
|
+
const ORDERED_ITEM = /^[ \t]*(\d{1,9})[.)][ \t]+(.*)$/;
|
|
62
|
+
|
|
63
|
+
/** True when a line starts a block that interrupts an open paragraph. */
|
|
64
|
+
function startsBlock(line: string): boolean {
|
|
65
|
+
return (
|
|
66
|
+
FENCE_OPEN.test(line) ||
|
|
67
|
+
HEADING.test(line) ||
|
|
68
|
+
THEMATIC_BREAK.test(line) ||
|
|
69
|
+
BLOCKQUOTE.test(line) ||
|
|
70
|
+
UNORDERED_ITEM.test(line) ||
|
|
71
|
+
ORDERED_ITEM.test(line)
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The `closer` for an open fence: the same marker character, at least as long as
|
|
77
|
+
* the opener, alone on its line. A source that never closes its fence takes the
|
|
78
|
+
* rest of the document as code — the same run-to-end rule CommonMark applies —
|
|
79
|
+
* rather than silently dropping the block.
|
|
80
|
+
*/
|
|
81
|
+
function fenceCloser(marker: string): RegExp {
|
|
82
|
+
const char = marker.startsWith('`') ? '`' : '~';
|
|
83
|
+
return new RegExp(`^[ \\t]{0,3}${char}{${String(marker.length)},}[ \\t]*$`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Folds markdown source into an ordered list of blocks. Pure and total: any
|
|
88
|
+
* string parses, and text that matches no block construct becomes a paragraph
|
|
89
|
+
* rather than being discarded.
|
|
90
|
+
*/
|
|
91
|
+
export function parseMarkdown(markdown: string): MarkdownBlock[] {
|
|
92
|
+
const lines = markdown.replace(/\r\n?/g, '\n').split('\n');
|
|
93
|
+
return parseBlocks(lines);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function parseBlocks(lines: readonly string[]): MarkdownBlock[] {
|
|
97
|
+
const blocks: MarkdownBlock[] = [];
|
|
98
|
+
let i = 0;
|
|
99
|
+
|
|
100
|
+
while (i < lines.length) {
|
|
101
|
+
const line = lines[i] ?? '';
|
|
102
|
+
|
|
103
|
+
if (BLANK.test(line)) {
|
|
104
|
+
i += 1;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const fence = FENCE_OPEN.exec(line);
|
|
109
|
+
if (fence !== null) {
|
|
110
|
+
const closer = fenceCloser(fence[1] ?? '');
|
|
111
|
+
const info = (fence[2] ?? '').trim();
|
|
112
|
+
const body: string[] = [];
|
|
113
|
+
i += 1;
|
|
114
|
+
for (; i < lines.length; i += 1) {
|
|
115
|
+
const next = lines[i] ?? '';
|
|
116
|
+
if (closer.test(next)) break;
|
|
117
|
+
body.push(next);
|
|
118
|
+
}
|
|
119
|
+
// Step past the closing fence when one is present; at end-of-document
|
|
120
|
+
// there is nothing to step past.
|
|
121
|
+
if (i < lines.length) i += 1;
|
|
122
|
+
blocks.push({
|
|
123
|
+
type: 'code',
|
|
124
|
+
language: info.length > 0 ? info : undefined,
|
|
125
|
+
code: body.join('\n'),
|
|
126
|
+
});
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const heading = HEADING.exec(line);
|
|
131
|
+
if (heading !== null) {
|
|
132
|
+
blocks.push({
|
|
133
|
+
type: 'heading',
|
|
134
|
+
level: (heading[1] ?? '').length as HeadingLevel,
|
|
135
|
+
text: heading[2] ?? '',
|
|
136
|
+
});
|
|
137
|
+
i += 1;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (THEMATIC_BREAK.test(line)) {
|
|
142
|
+
blocks.push({ type: 'thematicBreak' });
|
|
143
|
+
i += 1;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (BLOCKQUOTE.test(line)) {
|
|
148
|
+
const inner: string[] = [];
|
|
149
|
+
for (; i < lines.length; i += 1) {
|
|
150
|
+
const quoted = BLOCKQUOTE.exec(lines[i] ?? '');
|
|
151
|
+
if (quoted === null) break;
|
|
152
|
+
inner.push(quoted[1] ?? '');
|
|
153
|
+
}
|
|
154
|
+
blocks.push({ type: 'blockquote', children: parseBlocks(inner) });
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const unordered = UNORDERED_ITEM.exec(line);
|
|
159
|
+
const ordered = unordered === null ? ORDERED_ITEM.exec(line) : null;
|
|
160
|
+
if (unordered !== null || ordered !== null) {
|
|
161
|
+
const pattern = ordered !== null ? ORDERED_ITEM : UNORDERED_ITEM;
|
|
162
|
+
// An ordered item's content is its second group, behind the start number; an
|
|
163
|
+
// unordered item's content is its first.
|
|
164
|
+
const textGroup = ordered !== null ? 2 : 1;
|
|
165
|
+
const items: string[] = [];
|
|
166
|
+
for (; i < lines.length; i += 1) {
|
|
167
|
+
const item = pattern.exec(lines[i] ?? '');
|
|
168
|
+
if (item === null) break;
|
|
169
|
+
items.push(item[textGroup] ?? '');
|
|
170
|
+
}
|
|
171
|
+
if (ordered !== null) {
|
|
172
|
+
blocks.push({ type: 'list', ordered: true, items, start: Number(ordered[1] ?? '1') });
|
|
173
|
+
} else {
|
|
174
|
+
blocks.push({ type: 'list', ordered: false, items });
|
|
175
|
+
}
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// A paragraph: this line and the following lines up to a blank line or the
|
|
180
|
+
// start of another block. Soft line breaks are kept as newlines.
|
|
181
|
+
const paragraph: string[] = [line];
|
|
182
|
+
i += 1;
|
|
183
|
+
for (; i < lines.length; i += 1) {
|
|
184
|
+
const next = lines[i] ?? '';
|
|
185
|
+
if (BLANK.test(next) || startsBlock(next)) break;
|
|
186
|
+
paragraph.push(next);
|
|
187
|
+
}
|
|
188
|
+
blocks.push({ type: 'paragraph', text: paragraph.join('\n') });
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return blocks;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// -- inline grammar ----------------------------------------------------------
|
|
195
|
+
|
|
196
|
+
// Single-character predicates, so the `\S` and `\w` classes decide flanking
|
|
197
|
+
// exactly — a non-breaking space is whitespace, an underscore a word character —
|
|
198
|
+
// rather than an open-coded space test getting those edges wrong.
|
|
199
|
+
const NON_SPACE = /\S/;
|
|
200
|
+
const WORD = /\w/;
|
|
201
|
+
|
|
202
|
+
/** Inline code: a run of backticks and its literal, unparsed content, on one line. */
|
|
203
|
+
const CODE_SPAN = /`([^`\n]+)`/g;
|
|
204
|
+
|
|
205
|
+
// Emphasis runs carry CommonMark flanking: the opening marker must be followed by
|
|
206
|
+
// a non-space and the closing marker preceded by a non-space, so spaced prose like
|
|
207
|
+
// `w * h * d` is left literal. An `_` marker is additionally barred from touching a
|
|
208
|
+
// word character on its outer side, so intraword underscores (`user_id_field`,
|
|
209
|
+
// `a__proto__b`) never open or close a run; `*` keeps working everywhere.
|
|
210
|
+
|
|
211
|
+
/** A resolved emphasis or strong run: where it sits and the text between its markers. */
|
|
212
|
+
interface EmphasisSpan {
|
|
213
|
+
readonly index: number;
|
|
214
|
+
readonly length: number;
|
|
215
|
+
readonly content: string;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The earliest run delimited by `marker` (`**`, `__`, `*` or `_`) at or after
|
|
220
|
+
* `from`. Walks once, holding one pending opener per line and closing it at the
|
|
221
|
+
* first marker a non-space away (and, for `_`, not abutting a word character on
|
|
222
|
+
* its outer edges). One pending opener per line reproduces the leftmost lazy
|
|
223
|
+
* match, quirks and all (`***a***` → `***a**`, `**a*` → none); a newline drops the
|
|
224
|
+
* opener. O(1) per character.
|
|
225
|
+
*/
|
|
226
|
+
function findEmphasisSpan(text: string, from: number, marker: string): EmphasisSpan | null {
|
|
227
|
+
const m = marker.length;
|
|
228
|
+
const mark = marker[0];
|
|
229
|
+
const underscore = mark === '_';
|
|
230
|
+
let opener = -1;
|
|
231
|
+
for (let j = from; j + m <= text.length; j += 1) {
|
|
232
|
+
const ch = text[j];
|
|
233
|
+
if (ch === '\n') {
|
|
234
|
+
opener = -1;
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
if (ch !== mark || (m === 2 && text[j + 1] !== mark)) continue;
|
|
238
|
+
// A closer: first marker >=1 body char away, preceded by a non-space (and,
|
|
239
|
+
// for `_`, not followed by a word character).
|
|
240
|
+
if (
|
|
241
|
+
opener !== -1 &&
|
|
242
|
+
j >= opener + m + 1 &&
|
|
243
|
+
NON_SPACE.test(text[j - 1] ?? '') &&
|
|
244
|
+
(!underscore || j + m >= text.length || !WORD.test(text[j + m] ?? ''))
|
|
245
|
+
) {
|
|
246
|
+
return { index: opener, length: j + m - opener, content: text.slice(opener + m, j) };
|
|
247
|
+
}
|
|
248
|
+
// The opener: first marker followed by a non-space (and, for `_`, not abutting
|
|
249
|
+
// a word character on its left); only the first is kept.
|
|
250
|
+
if (
|
|
251
|
+
opener === -1 &&
|
|
252
|
+
j + m < text.length &&
|
|
253
|
+
NON_SPACE.test(text[j + m] ?? '') &&
|
|
254
|
+
(!underscore || j === 0 || !WORD.test(text[j - 1] ?? ''))
|
|
255
|
+
) {
|
|
256
|
+
opener = j;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** The outcome of a `scanDestination` sweep: the `)` it found, and where it halted. */
|
|
263
|
+
interface DestinationScan {
|
|
264
|
+
/** The closing `)` index, or `-1` when the run is empty or unterminated. */
|
|
265
|
+
readonly end: number;
|
|
266
|
+
/**
|
|
267
|
+
* Where the walk halted — the `)` on success, else the whitespace or end-of-text
|
|
268
|
+
* that ended a failure. On a failure that advanced, `[start, stop)` provably
|
|
269
|
+
* holds no `)`, which the caller memoizes as a dead span.
|
|
270
|
+
*/
|
|
271
|
+
readonly stop: number;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The destination run of a bracketed construct: one-or-more non-`)`, non-space
|
|
276
|
+
* characters bounded by `)`. Forward-only cursor. See {@link DestinationScan} for
|
|
277
|
+
* `end`/`stop`.
|
|
278
|
+
*/
|
|
279
|
+
function scanDestination(text: string, start: number): DestinationScan {
|
|
280
|
+
let k = start;
|
|
281
|
+
while (k < text.length && text[k] !== ')' && NON_SPACE.test(text[k] ?? '')) k += 1;
|
|
282
|
+
if (k > start && text[k] === ')') return { end: k, stop: k };
|
|
283
|
+
// A failure halts at `k`: the whitespace that stopped the walk, or `text.length`
|
|
284
|
+
// when the run reached the end. Either way `[start, k)` holds no `)`.
|
|
285
|
+
return { end: -1, stop: k };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** A resolved link or image: its label/alt `content` and its destination `extra`. */
|
|
289
|
+
interface BracketedSpan {
|
|
290
|
+
readonly index: number;
|
|
291
|
+
readonly length: number;
|
|
292
|
+
readonly content: string;
|
|
293
|
+
readonly extra: string;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* The earliest `[label](dest)` (or, when `image`, ``) at or after
|
|
298
|
+
* `from`. A label runs from `[` to the first `]`; a link needs a non-empty label,
|
|
299
|
+
* an image admits an empty one. Linear: each `[` candidate reuses a forward-only
|
|
300
|
+
* `]` pointer and a destination memoized on that `]`, and `deadUntil` records a
|
|
301
|
+
* proven no-`)` span so later candidates starting inside it skip the scan.
|
|
302
|
+
*/
|
|
303
|
+
function findBracketed(text: string, from: number, image: boolean): BracketedSpan | null {
|
|
304
|
+
let close = -1; // first `]` at an index the current candidate can still use
|
|
305
|
+
let destKey = -1; // the `]` the memoized destination belongs to
|
|
306
|
+
let destEnd = -1; // closing `)` for `destKey`, or -1
|
|
307
|
+
// Exclusive index up to which a scan proved no `)` exists; a destination start
|
|
308
|
+
// below it skips the scan. A start at or past it must still scan. Call-local.
|
|
309
|
+
let deadUntil = -1;
|
|
310
|
+
|
|
311
|
+
let bracket = text.indexOf('[', from);
|
|
312
|
+
while (bracket !== -1) {
|
|
313
|
+
// An image opens at the `!` immediately before its `[`.
|
|
314
|
+
const start = image ? bracket - 1 : bracket;
|
|
315
|
+
if (!image || (start >= from && text[start] === '!')) {
|
|
316
|
+
if (close < bracket + 1) close = text.indexOf(']', bracket + 1);
|
|
317
|
+
if (close === -1) return null;
|
|
318
|
+
const labelOk = image || close > bracket + 1;
|
|
319
|
+
if (labelOk && text[close + 1] === '(') {
|
|
320
|
+
if (destKey !== close) {
|
|
321
|
+
destKey = close;
|
|
322
|
+
const destStart = close + 2;
|
|
323
|
+
if (deadUntil !== -1 && destStart < deadUntil) {
|
|
324
|
+
// A destination inside a proven no-`)` span halts where that scan did.
|
|
325
|
+
destEnd = -1;
|
|
326
|
+
} else {
|
|
327
|
+
const scan = scanDestination(text, destStart);
|
|
328
|
+
destEnd = scan.end;
|
|
329
|
+
if (scan.end === -1 && scan.stop > destStart) deadUntil = scan.stop;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
if (destEnd !== -1) {
|
|
333
|
+
return {
|
|
334
|
+
index: start,
|
|
335
|
+
length: destEnd + 1 - start,
|
|
336
|
+
content: text.slice(bracket + 1, close),
|
|
337
|
+
extra: text.slice(close + 2, destEnd),
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
bracket = text.indexOf('[', bracket + 1);
|
|
343
|
+
}
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
type InlineKind = 'code' | 'image' | 'link' | 'strong' | 'emphasis';
|
|
348
|
+
|
|
349
|
+
/** An inline construct located at an absolute `index`, ready to render. */
|
|
350
|
+
interface InlineFound {
|
|
351
|
+
readonly kind: InlineKind;
|
|
352
|
+
readonly index: number;
|
|
353
|
+
readonly length: number;
|
|
354
|
+
/** Inline code text, image alt, link label, or emphasis/strong body. */
|
|
355
|
+
readonly content: string;
|
|
356
|
+
/** A link's or image's destination. */
|
|
357
|
+
readonly extra?: string;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** Inline code at or after `from`; the shared cursor is set immediately before the scan. */
|
|
361
|
+
function findCode(text: string, from: number): InlineFound | null {
|
|
362
|
+
CODE_SPAN.lastIndex = from;
|
|
363
|
+
const match = CODE_SPAN.exec(text);
|
|
364
|
+
if (match === null) return null;
|
|
365
|
+
return { kind: 'code', index: match.index, length: match[0].length, content: match[1] ?? '' };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
interface InlineRule {
|
|
369
|
+
readonly kind: InlineKind;
|
|
370
|
+
readonly find: (text: string, from: number) => InlineFound | null;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Tags an emphasis span with its element kind, or passes `null` through. */
|
|
374
|
+
function asFound(kind: InlineKind, span: EmphasisSpan | null): InlineFound | null {
|
|
375
|
+
return span === null
|
|
376
|
+
? null
|
|
377
|
+
: { kind, index: span.index, length: span.length, content: span.content };
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Inline constructs in tie-break order at a shared position: code, image, link,
|
|
382
|
+
* strong, emphasis — so `**` reads as one strong span, not two emphasis markers.
|
|
383
|
+
* Strong and emphasis are split per marker (`**`/`__`, `*`/`_`) so each marker
|
|
384
|
+
* caches its own exhaustion and the pass stays linear; star precedes underscore.
|
|
385
|
+
*/
|
|
386
|
+
const INLINE_RULES: readonly InlineRule[] = [
|
|
387
|
+
{ kind: 'code', find: findCode },
|
|
388
|
+
{
|
|
389
|
+
kind: 'image',
|
|
390
|
+
find: (text, from) => {
|
|
391
|
+
const span = findBracketed(text, from, true);
|
|
392
|
+
return span === null ? null : { kind: 'image', ...span };
|
|
393
|
+
},
|
|
394
|
+
},
|
|
395
|
+
{
|
|
396
|
+
kind: 'link',
|
|
397
|
+
find: (text, from) => {
|
|
398
|
+
const span = findBracketed(text, from, false);
|
|
399
|
+
return span === null ? null : { kind: 'link', ...span };
|
|
400
|
+
},
|
|
401
|
+
},
|
|
402
|
+
{ kind: 'strong', find: (text, from) => asFound('strong', findEmphasisSpan(text, from, '**')) },
|
|
403
|
+
{ kind: 'strong', find: (text, from) => asFound('strong', findEmphasisSpan(text, from, '__')) },
|
|
404
|
+
{
|
|
405
|
+
kind: 'emphasis',
|
|
406
|
+
find: (text, from) => asFound('emphasis', findEmphasisSpan(text, from, '*')),
|
|
407
|
+
},
|
|
408
|
+
{
|
|
409
|
+
kind: 'emphasis',
|
|
410
|
+
find: (text, from) => asFound('emphasis', findEmphasisSpan(text, from, '_')),
|
|
411
|
+
},
|
|
412
|
+
];
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Resolves inline markup to React nodes. A cursor walks the text once; each rule
|
|
416
|
+
* caches its earliest match at or after the cursor and is rescanned only when the
|
|
417
|
+
* cursor passes that match, so the rules' rescans cover disjoint forward ranges
|
|
418
|
+
* and the pass is linear. Plain runs are pushed as text children that React
|
|
419
|
+
* escapes; a link is admitted only when `safeHttpUrl` accepts it, and an image
|
|
420
|
+
* contributes its `alt` text alone.
|
|
421
|
+
*/
|
|
422
|
+
function renderInline(text: string, keyBase: string): ReactNode[] {
|
|
423
|
+
const nodes: ReactNode[] = [];
|
|
424
|
+
// Per rule: `undefined` not yet scanned, `null` no match through the end
|
|
425
|
+
// (final), otherwise the pending match to reuse until the cursor passes it.
|
|
426
|
+
const cache: (InlineFound | null | undefined)[] = INLINE_RULES.map(() => undefined);
|
|
427
|
+
let pos = 0;
|
|
428
|
+
|
|
429
|
+
while (pos < text.length) {
|
|
430
|
+
let best: InlineFound | null = null;
|
|
431
|
+
for (const [r, rule] of INLINE_RULES.entries()) {
|
|
432
|
+
let hit = cache[r];
|
|
433
|
+
if (hit === undefined || (hit !== null && hit.index < pos)) {
|
|
434
|
+
hit = rule.find(text, pos);
|
|
435
|
+
cache[r] = hit;
|
|
436
|
+
}
|
|
437
|
+
if (hit !== null && (best === null || hit.index < best.index)) {
|
|
438
|
+
best = hit;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (best === null) {
|
|
442
|
+
nodes.push(text.slice(pos));
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
if (best.index > pos) {
|
|
446
|
+
nodes.push(text.slice(pos, best.index));
|
|
447
|
+
}
|
|
448
|
+
nodes.push(renderInlineMatch(best, `${keyBase}-${String(best.index)}`));
|
|
449
|
+
pos = best.index + best.length;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
return nodes;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Test-only entry point to the inline scanner. Resolves inline markup to React
|
|
457
|
+
* nodes exactly as `renderInline` does, but without mounting or serializing a
|
|
458
|
+
* DOM, so a test can time the scan itself in isolation from render cost. Not part
|
|
459
|
+
* of the public API.
|
|
460
|
+
*/
|
|
461
|
+
export function scanInline(text: string): ReactNode[] {
|
|
462
|
+
return renderInline(text, 'k');
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function renderInlineMatch(found: InlineFound, key: string): ReactNode {
|
|
466
|
+
switch (found.kind) {
|
|
467
|
+
case 'code':
|
|
468
|
+
return <code key={key}>{found.content}</code>;
|
|
469
|
+
case 'image':
|
|
470
|
+
// Images are not rendered: the alt text is all that survives, and no URL
|
|
471
|
+
// is fetched.
|
|
472
|
+
return found.content;
|
|
473
|
+
case 'link': {
|
|
474
|
+
const url = safeHttpUrl(found.extra ?? '');
|
|
475
|
+
const children = renderInline(found.content, `${key}-t`);
|
|
476
|
+
if (url === undefined) {
|
|
477
|
+
// A destination that is not an absolute http(s) URL never becomes a live
|
|
478
|
+
// anchor: the display text is rendered as plain text.
|
|
479
|
+
return <span key={key}>{children}</span>;
|
|
480
|
+
}
|
|
481
|
+
return (
|
|
482
|
+
<a key={key} href={url} target="_blank" rel="noopener noreferrer external">
|
|
483
|
+
{children}
|
|
484
|
+
</a>
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
case 'strong':
|
|
488
|
+
return <strong key={key}>{renderInline(found.content, `${key}-t`)}</strong>;
|
|
489
|
+
case 'emphasis':
|
|
490
|
+
return <em key={key}>{renderInline(found.content, `${key}-t`)}</em>;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// -- block rendering ---------------------------------------------------------
|
|
495
|
+
|
|
496
|
+
/** The intrinsic element each heading level renders, keyed for a valid JSX tag. */
|
|
497
|
+
const HEADING_TAG: Record<HeadingLevel, 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'> = {
|
|
498
|
+
1: 'h1',
|
|
499
|
+
2: 'h2',
|
|
500
|
+
3: 'h3',
|
|
501
|
+
4: 'h4',
|
|
502
|
+
5: 'h5',
|
|
503
|
+
6: 'h6',
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
function renderBlock(block: MarkdownBlock, key: string): ReactNode {
|
|
507
|
+
switch (block.type) {
|
|
508
|
+
case 'heading': {
|
|
509
|
+
const Tag = HEADING_TAG[block.level];
|
|
510
|
+
return <Tag key={key}>{renderInline(block.text, key)}</Tag>;
|
|
511
|
+
}
|
|
512
|
+
case 'paragraph':
|
|
513
|
+
return <p key={key}>{renderInline(block.text, key)}</p>;
|
|
514
|
+
case 'code':
|
|
515
|
+
return <CodeBlock key={key} code={block.code} language={block.language} />;
|
|
516
|
+
case 'list': {
|
|
517
|
+
const items = block.items.map((item, index) => {
|
|
518
|
+
const itemKey = `${key}-${String(index)}`;
|
|
519
|
+
return <li key={itemKey}>{renderInline(item, itemKey)}</li>;
|
|
520
|
+
});
|
|
521
|
+
if (block.ordered) {
|
|
522
|
+
// A list that starts at 1 needs no `start`; any other first number is carried
|
|
523
|
+
// through so the marker sequence matches the source.
|
|
524
|
+
const start = block.start !== undefined && block.start !== 1 ? block.start : undefined;
|
|
525
|
+
return (
|
|
526
|
+
<ol key={key} start={start}>
|
|
527
|
+
{items}
|
|
528
|
+
</ol>
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
return <ul key={key}>{items}</ul>;
|
|
532
|
+
}
|
|
533
|
+
case 'blockquote':
|
|
534
|
+
return (
|
|
535
|
+
<blockquote key={key}>
|
|
536
|
+
{block.children.map((child, index) => renderBlock(child, `${key}-${String(index)}`))}
|
|
537
|
+
</blockquote>
|
|
538
|
+
);
|
|
539
|
+
case 'thematicBreak':
|
|
540
|
+
return <hr key={key} />;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
export function Markdown({ markdown, className }: MarkdownProps) {
|
|
545
|
+
const blocks = useMemo(() => parseMarkdown(markdown), [markdown]);
|
|
546
|
+
const proseClass = className === undefined ? 'tai-prose' : `tai-prose ${className}`;
|
|
547
|
+
return (
|
|
548
|
+
<div className={proseClass}>
|
|
549
|
+
{blocks.map((block, index) => renderBlock(block, `b${String(index)}`))}
|
|
550
|
+
</div>
|
|
551
|
+
);
|
|
552
|
+
}
|
package/src/components/tabs.tsx
CHANGED
|
@@ -19,15 +19,24 @@ export interface TabsProps {
|
|
|
19
19
|
readonly value?: string;
|
|
20
20
|
readonly defaultValue?: string;
|
|
21
21
|
readonly onValueChange?: (value: string) => void;
|
|
22
|
+
/** `'manual'` commits on Enter/Space/click only — arrows just move focus. */
|
|
23
|
+
readonly activationMode?: 'automatic' | 'manual';
|
|
22
24
|
}
|
|
23
25
|
|
|
24
|
-
export function Tabs({
|
|
26
|
+
export function Tabs({
|
|
27
|
+
items,
|
|
28
|
+
value,
|
|
29
|
+
defaultValue,
|
|
30
|
+
onValueChange,
|
|
31
|
+
activationMode = 'automatic',
|
|
32
|
+
}: TabsProps) {
|
|
25
33
|
const firstValue = items[0]?.value;
|
|
26
34
|
return (
|
|
27
35
|
<RadixTabs.Root
|
|
28
36
|
value={value}
|
|
29
37
|
defaultValue={defaultValue ?? firstValue}
|
|
30
38
|
onValueChange={onValueChange}
|
|
39
|
+
activationMode={activationMode}
|
|
31
40
|
>
|
|
32
41
|
<RadixTabs.List className="tai-tablist">
|
|
33
42
|
{items.map((item) => (
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `useClipboardCopy` — the shared clipboard-write interaction: reads the clipboard the
|
|
3
|
+
* browser actually offers, writes the value, and holds a transient "copied" window
|
|
4
|
+
* (confirmed face + polite announcement) reverting after {@link COPIED_RESET_MS}. A
|
|
5
|
+
* refused write, or a value that throws while being produced, surfaces as a
|
|
6
|
+
* caller-worded error string rather than a throw, so the control shows a visible alert.
|
|
7
|
+
*
|
|
8
|
+
* Only the WORDING is per control, supplied via {@link ClipboardCopyMessages}.
|
|
9
|
+
*/
|
|
10
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
11
|
+
|
|
12
|
+
/** How long a copy control shows its confirmed state before reverting. */
|
|
13
|
+
export const COPIED_RESET_MS = 2000;
|
|
14
|
+
|
|
15
|
+
/** A copy control's confirmed face; whichever face shows is also its accessible name. */
|
|
16
|
+
export const COPIED_LABEL = 'Copied';
|
|
17
|
+
|
|
18
|
+
/** Announced once on a successful copy, worded so it is never read as the label. */
|
|
19
|
+
const COPIED_ANNOUNCEMENT = 'Copied to clipboard';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* `lib.dom` declares `navigator.clipboard` as always present, but the platform
|
|
23
|
+
* exposes it only in a SECURE CONTEXT — over plain http it is not there at all.
|
|
24
|
+
* Reading the navigator through this shape admits the absence the DOM types deny,
|
|
25
|
+
* so the guard below is a real check rather than a cast around one.
|
|
26
|
+
*/
|
|
27
|
+
interface MaybeClipboard {
|
|
28
|
+
readonly clipboard?: Clipboard;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The clipboard this browser actually offers, or `undefined` when it offers none. */
|
|
32
|
+
function clipboardOf(host: MaybeClipboard): Clipboard | undefined {
|
|
33
|
+
return host.clipboard;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ClipboardCopyMessages {
|
|
37
|
+
/** Shown when the browser offers no clipboard at all (any non-secure context). */
|
|
38
|
+
readonly noClipboard: string;
|
|
39
|
+
/** Shown when the write, or producing the value, is offered and refused. */
|
|
40
|
+
readonly writeFailed: (reason: unknown) => string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface ClipboardCopy {
|
|
44
|
+
/** Whether the confirmed window is currently open. */
|
|
45
|
+
readonly copied: boolean;
|
|
46
|
+
/** The last failure's message, or `undefined` while the last copy stands. */
|
|
47
|
+
readonly error: string | undefined;
|
|
48
|
+
/** The polite live-region text: the announcement while confirmed, else empty. */
|
|
49
|
+
readonly announcement: string;
|
|
50
|
+
/**
|
|
51
|
+
* Writes the produced text to the clipboard, returning whether it landed. The
|
|
52
|
+
* value is produced lazily so a serialization that throws is reported through
|
|
53
|
+
* {@link ClipboardCopyMessages.writeFailed}, exactly as a refused write is.
|
|
54
|
+
*/
|
|
55
|
+
readonly copy: (produce: () => string) => Promise<boolean>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function useClipboardCopy(messages: ClipboardCopyMessages): ClipboardCopy {
|
|
59
|
+
const [copied, setCopied] = useState(false);
|
|
60
|
+
const [error, setError] = useState<string | undefined>(undefined);
|
|
61
|
+
const resetTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
62
|
+
// The write is async, so a copy can be in flight when the control unmounts (a dialog
|
|
63
|
+
// holding it closes on the same click); checked before the resolution touches state.
|
|
64
|
+
// Clearing the timer alone can't cover it — at unmount no timer exists yet.
|
|
65
|
+
const mounted = useRef(true);
|
|
66
|
+
// Wording is read at write time, not closed over, so the callback stays stable even
|
|
67
|
+
// when the caller passes fresh message objects.
|
|
68
|
+
const messagesRef = useRef(messages);
|
|
69
|
+
messagesRef.current = messages;
|
|
70
|
+
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
mounted.current = true;
|
|
73
|
+
return () => {
|
|
74
|
+
mounted.current = false;
|
|
75
|
+
if (resetTimer.current !== null) clearTimeout(resetTimer.current);
|
|
76
|
+
};
|
|
77
|
+
}, []);
|
|
78
|
+
|
|
79
|
+
const copy = useCallback(async (produce: () => string): Promise<boolean> => {
|
|
80
|
+
const clipboard = clipboardOf(navigator);
|
|
81
|
+
if (clipboard === undefined) {
|
|
82
|
+
setError(messagesRef.current.noClipboard);
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
// Clear any prior failure at the START of a new attempt, before awaiting the
|
|
86
|
+
// write, so a stale alert does not linger through an in-flight retry.
|
|
87
|
+
setError(undefined);
|
|
88
|
+
try {
|
|
89
|
+
const text = produce();
|
|
90
|
+
await clipboard.writeText(text);
|
|
91
|
+
if (!mounted.current) return true;
|
|
92
|
+
setCopied(true);
|
|
93
|
+
if (resetTimer.current !== null) clearTimeout(resetTimer.current);
|
|
94
|
+
resetTimer.current = setTimeout(() => {
|
|
95
|
+
if (mounted.current) setCopied(false);
|
|
96
|
+
}, COPIED_RESET_MS);
|
|
97
|
+
return true;
|
|
98
|
+
} catch (reason: unknown) {
|
|
99
|
+
if (mounted.current) setError(messagesRef.current.writeFailed(reason));
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
}, []);
|
|
103
|
+
|
|
104
|
+
return { copied, error, announcement: copied ? COPIED_ANNOUNCEMENT : '', copy };
|
|
105
|
+
}
|