devnonla-ui 0.7.1 → 0.9.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.
Files changed (61) hide show
  1. package/README.md +31 -4
  2. package/package.json +3 -3
  3. package/src/app/App.tsx +5 -1
  4. package/src/app/context.ts +5 -3
  5. package/src/button/Button.tsx +2 -2
  6. package/src/button/ButtonCopy.tsx +2 -2
  7. package/src/card/Card.tsx +81 -0
  8. package/src/chat/AgentChatbox.tsx +1 -1
  9. package/src/chat/AgentPanel.tsx +1 -1
  10. package/src/chat/message-ui/ChatError.tsx +2 -2
  11. package/src/chat/message-ui/ChatInput.tsx +6 -6
  12. package/src/chat/message-ui/ChatMarkdown.tsx +8 -7
  13. package/src/chat/message-ui/ChatMarkdownTable.tsx +1 -1
  14. package/src/chat/message-ui/ChatThinking.tsx +3 -3
  15. package/src/chat/message-ui/ChatUserMessage.tsx +3 -3
  16. package/src/chat/message-ui/MermaidBlock.tsx +76 -70
  17. package/src/chat/tool-ui/BackgroundTasksBar.tsx +3 -3
  18. package/src/chat/tool-ui/CallAgentToolUI.tsx +3 -3
  19. package/src/chat/tool-ui/ChatToolCall.tsx +4 -4
  20. package/src/chat/tool-ui/GetCurrentTimeToolUI.tsx +2 -2
  21. package/src/chat/tool-ui/ReadSkillToolUI.tsx +2 -2
  22. package/src/chat/tool-ui/RunJsToolUI.tsx +2 -2
  23. package/src/chat/tool-ui/ToolUiTrailing.tsx +3 -3
  24. package/src/chat/tool-ui/WebFetchToolUI.tsx +2 -2
  25. package/src/checkbox/Checkbox.tsx +17 -4
  26. package/src/codeblock/CodeBlock.tsx +4 -10
  27. package/src/datepicker/DatePicker.tsx +3 -3
  28. package/src/desktop/DesktopHeader.tsx +1 -1
  29. package/src/desktop/DesktopIcon.tsx +2 -2
  30. package/src/desktop/DesktopWindow.tsx +51 -29
  31. package/src/empty/Empty.tsx +8 -5
  32. package/src/form/FormItem.tsx +1 -1
  33. package/src/form-layout/FormLayout.tsx +1 -1
  34. package/src/icon/SolarIcon.tsx +39 -0
  35. package/src/icon/solar.ts +172 -0
  36. package/src/index.ts +24 -12
  37. package/src/input/EditableInput.tsx +3 -3
  38. package/src/input/Input.tsx +3 -3
  39. package/src/lib/sizes.ts +9 -6
  40. package/src/lib/surface.ts +1 -1
  41. package/src/markdown-editor/MarkdownDiff.tsx +211 -0
  42. package/src/markdown-editor/MarkdownEditor.tsx +294 -0
  43. package/src/markdown-editor/MarkdownPreview.tsx +326 -0
  44. package/src/markdown-editor/MarkdownSource.tsx +72 -0
  45. package/src/markdown-editor/blocks.ts +329 -0
  46. package/src/markdown-editor/diff.ts +296 -0
  47. package/src/menu/Menu.tsx +3 -3
  48. package/src/pagination/Pagination.tsx +8 -7
  49. package/src/scroll/OverlayScroll.tsx +2 -2
  50. package/src/segmented/Segmented.tsx +2 -2
  51. package/src/select/Select.tsx +192 -63
  52. package/src/sidebar/Sidebar.tsx +9 -8
  53. package/src/spin/Spin.tsx +17 -11
  54. package/src/styles.css +273 -107
  55. package/src/switch/Switch.tsx +17 -3
  56. package/src/table/Table.tsx +63 -28
  57. package/src/theme-toggle/ThemeToggle.tsx +33 -0
  58. package/src/theme.ts +72 -0
  59. package/src/timepicker/TimePicker.tsx +1 -1
  60. package/src/icon/FluentIcon.tsx +0 -28
  61. package/src/icon/fluent.ts +0 -75
@@ -0,0 +1,329 @@
1
+ export type MarkdownBlockKind = "text" | "code" | "mermaid";
2
+
3
+ export type MarkdownBlock = {
4
+ kind: MarkdownBlockKind;
5
+ /** Block source without the newline gap after it. */
6
+ body: string;
7
+ /** Exact characters between this block and the next, usually newlines. */
8
+ trailing: string;
9
+ };
10
+
11
+ export type MarkdownDoc = {
12
+ /** Newlines before the first block. */
13
+ leading: string;
14
+ blocks: MarkdownBlock[];
15
+ };
16
+
17
+ type Line = { text: string; nl: boolean };
18
+ type Group = { kind: MarkdownBlockKind; lineStart: number; lineEnd: number };
19
+
20
+ const FENCE = /^( {0,3})(`{3,}|~{3,})(.*)$/;
21
+
22
+ function linesOf(markdown: string): Line[] {
23
+ if (markdown.length === 0) return [];
24
+ const parts = markdown.split("\n");
25
+ const lines: Line[] = [];
26
+ for (let i = 0; i < parts.length; i++) {
27
+ const isLast = i === parts.length - 1;
28
+ if (isLast && parts[i] === "") continue;
29
+ lines.push({ text: parts[i] ?? "", nl: !isLast });
30
+ }
31
+ return lines;
32
+ }
33
+
34
+ function lineSource(line: Line): string {
35
+ return line.text + (line.nl ? "\n" : "");
36
+ }
37
+
38
+ function fenceMark(text: string): { marker: string; lang: string } | null {
39
+ const match = FENCE.exec(text);
40
+ if (!match) return null;
41
+ const info = match[3] ?? "";
42
+ const lang = info.trim().split(/\s+/)[0]?.toLowerCase() ?? "";
43
+ return { marker: match[2] ?? "", lang };
44
+ }
45
+
46
+ function isHeading(text: string): boolean {
47
+ return /^#{1,6}(?:\s|$)/.test(text);
48
+ }
49
+
50
+ function isHr(text: string): boolean {
51
+ return /^ {0,3}(?:(?:-\s*){3,}|(?:\*\s*){3,}|(?:_\s*){3,})$/.test(text);
52
+ }
53
+
54
+ const LIST_LINE = /^(\s*)([-+*]|\d{1,9}[.)])([ \t]+)(.*)$/;
55
+
56
+ function isListItemLine(text: string): boolean {
57
+ return !isHr(text) && LIST_LINE.test(text);
58
+ }
59
+
60
+ function closesFence(open: { marker: string; lang: string }, text: string): boolean {
61
+ const close = fenceMark(text);
62
+ if (!close || !open.marker) return false;
63
+ return close.marker[0] === open.marker[0] && close.marker.length >= open.marker.length && close.lang === "";
64
+ }
65
+
66
+ /** Split markdown into blocks. `joinMarkdownDoc(parseMarkdownDoc(text))` round-trips. */
67
+ export function parseMarkdownDoc(markdown: string): MarkdownDoc {
68
+ const lines = linesOf(markdown);
69
+ let i = 0;
70
+ let leading = "";
71
+ while (i < lines.length && lines[i]?.text === "") {
72
+ leading += lineSource(lines[i]!);
73
+ i++;
74
+ }
75
+
76
+ const groups: Group[] = [];
77
+ while (i < lines.length) {
78
+ const line = lines[i];
79
+ if (!line || line.text === "") {
80
+ i++;
81
+ continue;
82
+ }
83
+
84
+ const fence = fenceMark(line.text);
85
+ if (fence) {
86
+ const start = i;
87
+ i++;
88
+ while (i < lines.length) {
89
+ const closed = closesFence(fence, lines[i]?.text ?? "");
90
+ i++;
91
+ if (closed) break;
92
+ }
93
+ groups.push({ kind: fence.lang === "mermaid" ? "mermaid" : "code", lineStart: start, lineEnd: i });
94
+ continue;
95
+ }
96
+
97
+ if (isHeading(line.text) || isHr(line.text)) {
98
+ groups.push({ kind: "text", lineStart: i, lineEnd: i + 1 });
99
+ i++;
100
+ continue;
101
+ }
102
+
103
+ if (isListItemLine(line.text)) {
104
+ const start = i;
105
+ i++;
106
+ while (i < lines.length) {
107
+ const next = lines[i];
108
+ if (!next || next.text === "") break;
109
+ if (isListItemLine(next.text) || isHeading(next.text) || isHr(next.text) || fenceMark(next.text)) break;
110
+ if (!/^\s/.test(next.text)) break;
111
+ i++;
112
+ }
113
+ groups.push({ kind: "text", lineStart: start, lineEnd: i });
114
+ continue;
115
+ }
116
+
117
+ const start = i;
118
+ i++;
119
+ while (i < lines.length) {
120
+ const next = lines[i];
121
+ if (!next || next.text === "" || isHeading(next.text) || isHr(next.text) || fenceMark(next.text) || isListItemLine(next.text)) break;
122
+ i++;
123
+ }
124
+ groups.push({ kind: "text", lineStart: start, lineEnd: i });
125
+ }
126
+
127
+ const blocks: MarkdownBlock[] = groups.map((group, index) => {
128
+ const bodyLines = lines.slice(group.lineStart, group.lineEnd);
129
+ const body = bodyLines.map((entry) => entry.text).join("\n");
130
+ const nextStart = groups[index + 1]?.lineStart ?? lines.length;
131
+ const last = bodyLines[bodyLines.length - 1];
132
+ let trailing = last?.nl ? "\n" : "";
133
+ for (let cursor = group.lineEnd; cursor < nextStart; cursor++) {
134
+ const gap = lines[cursor];
135
+ if (gap) trailing += lineSource(gap);
136
+ }
137
+ return { kind: group.kind, body, trailing };
138
+ });
139
+
140
+ return { leading, blocks };
141
+ }
142
+
143
+ export function joinMarkdownDoc(doc: MarkdownDoc): string {
144
+ return doc.leading + doc.blocks.map((block) => block.body + block.trailing).join("");
145
+ }
146
+
147
+ /** Drop cleared paragraphs, headings, and list items. Code and mermaid fences stay. */
148
+ export function cleanupDoc(doc: MarkdownDoc): MarkdownDoc {
149
+ const blocks = doc.blocks.filter((block) => !(block.kind === "text" && textBlockIsEmpty(block.body)));
150
+ if (blocks.length === 0) return { leading: "", blocks: [] };
151
+ return { leading: doc.leading, blocks };
152
+ }
153
+
154
+ /** Add an empty paragraph after the current document. */
155
+ export function appendTextBlock(doc: MarkdownDoc): MarkdownDoc {
156
+ if (doc.blocks.length === 0) {
157
+ return { leading: doc.leading, blocks: [{ kind: "text", body: "", trailing: "" }] };
158
+ }
159
+ const blocks = doc.blocks.slice();
160
+ const last = blocks[blocks.length - 1]!;
161
+ let trailing = last.trailing;
162
+ if (!trailing.endsWith("\n")) trailing += "\n";
163
+ if (!trailing.endsWith("\n\n")) trailing += "\n";
164
+ blocks[blocks.length - 1] = { ...last, trailing };
165
+ blocks.push({ kind: "text", body: "", trailing: "" });
166
+ return { leading: doc.leading, blocks };
167
+ }
168
+
169
+ /** Inner code of a fence, without the opening and closing lines. */
170
+ export function readFence(body: string): { lang: string; code: string } | null {
171
+ const lines = body.split("\n");
172
+ const open = fenceMark(lines[0] ?? "");
173
+ if (!open) return null;
174
+ let end = lines.length;
175
+ if (lines.length > 1 && closesFence(open, lines[lines.length - 1] ?? "")) end = lines.length - 1;
176
+ return { lang: open.lang, code: lines.slice(1, end).join("\n") };
177
+ }
178
+
179
+ export type InlineEdit =
180
+ | { kind: "plain"; text: string }
181
+ | { kind: "heading"; level: number; gap: string; text: string }
182
+ | { kind: "list"; indent: string; marker: string; ordered: boolean; task: boolean; checked: boolean; text: string };
183
+
184
+ const HEADING_LINE = /^(#{1,6})([ \t]*)(.*)$/;
185
+ const TASK_PREFIX = /^\[([ xX])\]([ \t]*)(.*)$/;
186
+
187
+ /** Text the preview editor shows. Headings omit `#`, list items omit the marker. */
188
+ export function readInlineEdit(body: string): InlineEdit {
189
+ const nl = body.indexOf("\n");
190
+ const first = nl === -1 ? body : body.slice(0, nl);
191
+ const rest = nl === -1 ? "" : body.slice(nl + 1);
192
+ const withRest = (content: string) => (rest ? `${content}\n${rest}` : content);
193
+
194
+ const item = !isHr(first) ? LIST_LINE.exec(first) : null;
195
+ if (item) {
196
+ const indent = item[1] ?? "";
197
+ const bullet = item[2] ?? "-";
198
+ const space = item[3] ?? " ";
199
+ const after = item[4] ?? "";
200
+ const task = TASK_PREFIX.exec(after);
201
+ const ordered = /^\d/.test(bullet);
202
+ if (task) {
203
+ const content = task[3] ?? "";
204
+ const marker = `${bullet}${space}[${task[1]}]${task[2] ?? ""}`;
205
+ return {
206
+ kind: "list",
207
+ indent,
208
+ marker,
209
+ ordered,
210
+ task: true,
211
+ checked: (task[1] ?? "").toLowerCase() === "x",
212
+ text: listText(content, rest, nl !== -1, indent.length + marker.length),
213
+ };
214
+ }
215
+ const marker = `${bullet}${space}`;
216
+ return { kind: "list", indent, marker, ordered, task: false, checked: false, text: listText(after, rest, nl !== -1, indent.length + marker.length) };
217
+ }
218
+
219
+ const heading = isHeading(first) ? HEADING_LINE.exec(first) : null;
220
+ if (heading) {
221
+ const hashes = heading[1] ?? "#";
222
+ return { kind: "heading", level: hashes.length, gap: heading[2] ?? "", text: withRest(heading[3] ?? "") };
223
+ }
224
+
225
+ return { kind: "plain", text: body };
226
+ }
227
+
228
+ function listText(content: string, rest: string, hadBreak: boolean, pad: number): string {
229
+ if (!hadBreak) return content;
230
+ const lines = rest.split("\n").map((line) => {
231
+ let i = 0;
232
+ while (i < pad && (line[i] === " " || line[i] === "\t")) i += 1;
233
+ return line.slice(i);
234
+ });
235
+ return `${content}\n${lines.join("\n")}`;
236
+ }
237
+
238
+ function listSource(indent: string, marker: string, text: string): string {
239
+ const pad = `${indent}${" ".repeat(marker.length)}`;
240
+ const lines = text.split("\n");
241
+ return lines.map((line, index) => (index === 0 ? `${indent}${marker}${line}` : `${pad}${line}`)).join("\n");
242
+ }
243
+
244
+ function markerNumber(marker: string): number {
245
+ return Number(/^(\d+)/.exec(marker)?.[1] ?? "1");
246
+ }
247
+
248
+ function withMarkerNumber(marker: string, n: number): string {
249
+ return marker.replace(/^\d+/, String(n));
250
+ }
251
+
252
+ function markerDelim(marker: string): string {
253
+ return /^\d+\)/.test(marker) ? ")" : ".";
254
+ }
255
+
256
+ function nextListMarker(view: Extract<InlineEdit, { kind: "list" }>): string {
257
+ let marker = view.marker;
258
+ if (view.ordered) marker = withMarkerNumber(marker, markerNumber(marker) + 1);
259
+ if (view.task) marker = marker.replace(/\[[ xX]\]/, "[ ]");
260
+ return marker;
261
+ }
262
+
263
+ /** Put the hidden marker back in front of the edited text. */
264
+ export function writeInlineEdit(body: string, text: string): string {
265
+ const view = readInlineEdit(body);
266
+ if (view.kind === "plain") return text;
267
+ if (view.kind === "list") return listSource(view.indent, view.marker, text);
268
+ const lines = text.split("\n");
269
+ const first = lines[0] ?? "";
270
+ const tail = lines.slice(1);
271
+ const gap = view.gap.length > 0 ? view.gap : first.length > 0 ? " " : "";
272
+ return [`${"#".repeat(view.level)}${gap}${first}`, ...tail].join("\n");
273
+ }
274
+
275
+ /** Enter in a list item: keep the text before the caret, open a sibling item for the rest. */
276
+ export function splitListItem(doc: MarkdownDoc, index: number, before: string, after: string): { doc: MarkdownDoc; index: number } | null {
277
+ const block = doc.blocks[index];
278
+ if (!block || block.kind !== "text") return null;
279
+ const view = readInlineEdit(block.body);
280
+ if (view.kind !== "list") return null;
281
+
282
+ const blocks = doc.blocks.slice();
283
+ const marker = nextListMarker(view);
284
+ blocks[index] = { ...block, body: listSource(view.indent, view.marker, before) };
285
+ const at = listItemEnd(blocks, index, view.indent);
286
+ const previous = blocks[at - 1] ?? block;
287
+ const carried = previous.trailing;
288
+ blocks[at - 1] = { ...previous, trailing: "\n" };
289
+ blocks.splice(at, 0, { kind: "text", body: listSource(view.indent, marker, after), trailing: carried });
290
+ if (view.ordered) renumberOrdered(blocks, at, view.indent, markerDelim(view.marker));
291
+ return { doc: { ...doc, blocks }, index: at };
292
+ }
293
+
294
+ function listItemEnd(blocks: MarkdownBlock[], index: number, indent: string): number {
295
+ let end = index + 1;
296
+ while (end < blocks.length) {
297
+ const block = blocks[end];
298
+ if (!block || block.kind !== "text") break;
299
+ const item = readInlineEdit(block.body);
300
+ if (item.kind !== "list" || item.indent.length <= indent.length) break;
301
+ end += 1;
302
+ }
303
+ return end;
304
+ }
305
+
306
+ function renumberOrdered(blocks: MarkdownBlock[], from: number, indent: string, delim: string) {
307
+ const first = blocks[from] ? readInlineEdit(blocks[from].body) : null;
308
+ if (!first || first.kind !== "list" || !first.ordered) return;
309
+ let n = markerNumber(first.marker);
310
+ for (let i = from + 1; i < blocks.length; i++) {
311
+ const block = blocks[i];
312
+ if (!block || block.kind !== "text") break;
313
+ const item = readInlineEdit(block.body);
314
+ if (item.kind !== "list") break;
315
+ if (item.indent.length < indent.length) break;
316
+ if (item.indent.length > indent.length) continue;
317
+ if (!item.ordered || markerDelim(item.marker) !== delim) break;
318
+ n += 1;
319
+ const marker = withMarkerNumber(item.marker, n);
320
+ if (marker === item.marker) continue;
321
+ const prefix = item.indent + item.marker;
322
+ if (!block.body.startsWith(prefix)) continue;
323
+ blocks[i] = { ...block, body: item.indent + marker + block.body.slice(prefix.length) };
324
+ }
325
+ }
326
+
327
+ export function textBlockIsEmpty(body: string): boolean {
328
+ return readInlineEdit(body).text.trim() === "";
329
+ }
@@ -0,0 +1,296 @@
1
+ export type DiffSpan = { text: string; changed: boolean };
2
+
3
+ export type DiffLineKind = "equal" | "insert" | "delete" | "empty";
4
+
5
+ export type DiffLine = {
6
+ kind: DiffLineKind;
7
+ text: string;
8
+ oldNo?: number;
9
+ newNo?: number;
10
+ spans: DiffSpan[];
11
+ };
12
+
13
+ /** One aligned row. Side-by-side shows both cells; inline flattens a change hunk. */
14
+ export type DiffRow = {
15
+ left: DiffLine;
16
+ right: DiffLine;
17
+ };
18
+
19
+ export type MarkdownDiffModel = {
20
+ rows: DiffRow[];
21
+ inline: DiffLine[];
22
+ added: number;
23
+ removed: number;
24
+ };
25
+
26
+ type Op<T> = { type: "equal" | "insert" | "delete"; value: T };
27
+
28
+ const CELL_LIMIT = 1_000_000;
29
+ const TOKEN_LIMIT = 500;
30
+
31
+ function equalOp<T>(value: T): Op<T> {
32
+ return { type: "equal", value };
33
+ }
34
+
35
+ /** Longest-common-subsequence diff. Prefers a deletion when both edits keep the same LCS. */
36
+ function lcsDiff<T>(a: T[], b: T[]): Op<T>[] {
37
+ const n = a.length;
38
+ const m = b.length;
39
+ if (n === 0) return b.map((value) => ({ type: "insert", value }));
40
+ if (m === 0) return a.map((value) => ({ type: "delete", value }));
41
+
42
+ const dp: Uint32Array[] = Array.from({ length: n + 1 }, () => new Uint32Array(m + 1));
43
+ for (let i = n - 1; i >= 0; i--) {
44
+ const row = dp[i]!;
45
+ const below = dp[i + 1]!;
46
+ for (let j = m - 1; j >= 0; j--) {
47
+ if (a[i] === b[j]) row[j] = below[j + 1]! + 1;
48
+ else row[j] = Math.max(below[j]!, row[j + 1]!);
49
+ }
50
+ }
51
+
52
+ const ops: Op<T>[] = [];
53
+ let i = 0;
54
+ let j = 0;
55
+ while (i < n && j < m) {
56
+ if (a[i] === b[j]) {
57
+ ops.push({ type: "equal", value: a[i]! });
58
+ i++;
59
+ j++;
60
+ } else if (dp[i + 1]![j]! >= dp[i]![j + 1]!) {
61
+ ops.push({ type: "delete", value: a[i]! });
62
+ i++;
63
+ } else {
64
+ ops.push({ type: "insert", value: b[j]! });
65
+ j++;
66
+ }
67
+ }
68
+ while (i < n) ops.push({ type: "delete", value: a[i++]! });
69
+ while (j < m) ops.push({ type: "insert", value: b[j++]! });
70
+ return ops;
71
+ }
72
+
73
+ /** Linear fallback when the changed region is too large for the LCS table. */
74
+ function greedyDiff(a: string[], b: string[]): Op<string>[] {
75
+ const positions = new Map<string, number[]>();
76
+ for (let i = 0; i < b.length; i++) {
77
+ const list = positions.get(b[i]!) ?? [];
78
+ list.push(i);
79
+ positions.set(b[i]!, list);
80
+ }
81
+
82
+ const ops: Op<string>[] = [];
83
+ let j = 0;
84
+ for (const line of a) {
85
+ const list = positions.get(line);
86
+ if (list) {
87
+ while (list.length > 0 && list[0]! < j) list.shift();
88
+ }
89
+ const match = list && list.length > 0 ? list.shift()! : -1;
90
+ if (match < 0) {
91
+ ops.push({ type: "delete", value: line });
92
+ continue;
93
+ }
94
+ while (j < match) ops.push({ type: "insert", value: b[j++]! });
95
+ ops.push({ type: "equal", value: line });
96
+ j = match + 1;
97
+ }
98
+ while (j < b.length) ops.push({ type: "insert", value: b[j++]! });
99
+ return ops;
100
+ }
101
+
102
+ function diffLines(a: string[], b: string[]): Op<string>[] {
103
+ let start = 0;
104
+ const maxStart = Math.min(a.length, b.length);
105
+ while (start < maxStart && a[start] === b[start]) start++;
106
+
107
+ let aEnd = a.length;
108
+ let bEnd = b.length;
109
+ while (aEnd > start && bEnd > start && a[aEnd - 1] === b[bEnd - 1]) {
110
+ aEnd--;
111
+ bEnd--;
112
+ }
113
+
114
+ const midA = a.slice(start, aEnd);
115
+ const midB = b.slice(start, bEnd);
116
+ const mid = midA.length * midB.length > CELL_LIMIT ? greedyDiff(midA, midB) : lcsDiff(midA, midB);
117
+ return [...a.slice(0, start).map(equalOp), ...mid, ...a.slice(aEnd).map(equalOp)];
118
+ }
119
+
120
+ function splitDiffLines(text: string): string[] {
121
+ if (text.length === 0) return [];
122
+ const parts = text.split("\n");
123
+ if (text.endsWith("\n")) parts.pop();
124
+ return parts;
125
+ }
126
+
127
+ function plain(text: string): DiffSpan[] {
128
+ return [{ text, changed: false }];
129
+ }
130
+
131
+ function tokenize(line: string): string[] {
132
+ return line.match(/[A-Za-z0-9_]+|\s+|[^\sA-Za-z0-9_]+/g) ?? (line ? [line] : []);
133
+ }
134
+
135
+ function spansFrom(ops: Op<string>[], side: "delete" | "insert"): DiffSpan[] {
136
+ const spans: DiffSpan[] = [];
137
+ const push = (text: string, changed: boolean) => {
138
+ const last = spans[spans.length - 1];
139
+ if (last && last.changed === changed) last.text += text;
140
+ else spans.push({ text, changed });
141
+ };
142
+ for (const op of ops) {
143
+ if (op.type === "equal") push(op.value, false);
144
+ else if ((side === "delete" && op.type === "delete") || (side === "insert" && op.type === "insert")) push(op.value, true);
145
+ }
146
+ return spans;
147
+ }
148
+
149
+ function intraLine(before: string, after: string): { before: DiffSpan[]; after: DiffSpan[] } | null {
150
+ const left = tokenize(before);
151
+ const right = tokenize(after);
152
+ if (left.length === 0 || right.length === 0 || left.length > TOKEN_LIMIT || right.length > TOKEN_LIMIT) return null;
153
+ const ops = lcsDiff(left, right);
154
+ if (!ops.some((op) => op.type === "equal")) return null;
155
+ return { before: spansFrom(ops, "delete"), after: spansFrom(ops, "insert") };
156
+ }
157
+
158
+ function emptyLine(): DiffLine {
159
+ return { kind: "empty", text: "", spans: [] };
160
+ }
161
+
162
+ function rowsToInline(rows: DiffRow[]): DiffLine[] {
163
+ const lines: DiffLine[] = [];
164
+ let i = 0;
165
+ while (i < rows.length) {
166
+ const row = rows[i]!;
167
+ if (row.left.kind === "equal") {
168
+ lines.push({
169
+ kind: "equal",
170
+ text: row.left.text,
171
+ oldNo: row.left.oldNo,
172
+ newNo: row.right.newNo,
173
+ spans: plain(row.left.text),
174
+ });
175
+ i++;
176
+ continue;
177
+ }
178
+ const deleted: DiffLine[] = [];
179
+ const inserted: DiffLine[] = [];
180
+ while (i < rows.length && rows[i]!.left.kind !== "equal") {
181
+ const current = rows[i]!;
182
+ if (current.left.kind === "delete") deleted.push(current.left);
183
+ if (current.right.kind === "insert") inserted.push(current.right);
184
+ i++;
185
+ }
186
+ lines.push(...deleted, ...inserted);
187
+ }
188
+ return lines;
189
+ }
190
+
191
+ /** Line diff of two markdown versions, with word highlights inside changed lines. */
192
+ export function diffMarkdown(original: string, modified: string): MarkdownDiffModel {
193
+ const ops = diffLines(splitDiffLines(original), splitDiffLines(modified));
194
+ const rows: DiffRow[] = [];
195
+ let oldNo = 1;
196
+ let newNo = 1;
197
+ let added = 0;
198
+ let removed = 0;
199
+ let i = 0;
200
+
201
+ while (i < ops.length) {
202
+ const op = ops[i]!;
203
+ if (op.type === "equal") {
204
+ const spans = plain(op.value);
205
+ rows.push({
206
+ left: { kind: "equal", text: op.value, oldNo, spans },
207
+ right: { kind: "equal", text: op.value, newNo, spans },
208
+ });
209
+ oldNo++;
210
+ newNo++;
211
+ i++;
212
+ continue;
213
+ }
214
+
215
+ const deleted: string[] = [];
216
+ const inserted: string[] = [];
217
+ while (i < ops.length && ops[i]!.type !== "equal") {
218
+ const current = ops[i]!;
219
+ if (current.type === "delete") deleted.push(current.value);
220
+ else inserted.push(current.value);
221
+ i++;
222
+ }
223
+
224
+ const count = Math.max(deleted.length, inserted.length);
225
+ for (let k = 0; k < count; k++) {
226
+ const leftText = deleted[k];
227
+ const rightText = inserted[k];
228
+ const intra = leftText !== undefined && rightText !== undefined ? intraLine(leftText, rightText) : null;
229
+ const left: DiffLine =
230
+ leftText !== undefined ? { kind: "delete", text: leftText, oldNo: oldNo++, spans: intra?.before ?? plain(leftText) } : emptyLine();
231
+ const right: DiffLine =
232
+ rightText !== undefined ? { kind: "insert", text: rightText, newNo: newNo++, spans: intra?.after ?? plain(rightText) } : emptyLine();
233
+ if (leftText !== undefined) removed++;
234
+ if (rightText !== undefined) added++;
235
+ rows.push({ left, right });
236
+ }
237
+ }
238
+
239
+ return { rows, inline: rowsToInline(rows), added, removed };
240
+ }
241
+
242
+ export type FoldItem<T> = { type: "item"; item: T } | { type: "fold"; id: number; count: number; items: T[] };
243
+
244
+ /** Collapse long unchanged runs, keeping a few lines of context around edits. */
245
+ export function foldContext<T>(items: T[], isEqual: (item: T) => boolean, context = 3): FoldItem<T>[] {
246
+ const out: FoldItem<T>[] = [];
247
+ let i = 0;
248
+ let foldId = 0;
249
+
250
+ const pushItems = (slice: T[]) => {
251
+ for (const item of slice) out.push({ type: "item", item });
252
+ };
253
+ const pushFold = (hidden: T[]) => {
254
+ if (hidden.length === 0) return;
255
+ out.push({ type: "fold", id: foldId, count: hidden.length, items: hidden });
256
+ foldId++;
257
+ };
258
+
259
+ while (i < items.length) {
260
+ if (!isEqual(items[i]!)) {
261
+ out.push({ type: "item", item: items[i]! });
262
+ i++;
263
+ continue;
264
+ }
265
+ let j = i;
266
+ while (j < items.length && isEqual(items[j]!)) j++;
267
+ const run = items.slice(i, j);
268
+ const atStart = i === 0;
269
+ const atEnd = j === items.length;
270
+
271
+ if (atStart && atEnd) {
272
+ if (run.length > context * 2 + 1) {
273
+ pushItems(run.slice(0, context));
274
+ pushFold(run.slice(context, run.length - context));
275
+ pushItems(run.slice(run.length - context));
276
+ } else pushItems(run);
277
+ } else if (atStart) {
278
+ const keepFrom = Math.max(0, run.length - context);
279
+ pushFold(run.slice(0, keepFrom));
280
+ pushItems(run.slice(keepFrom));
281
+ } else if (atEnd) {
282
+ const keep = Math.min(context, run.length);
283
+ pushItems(run.slice(0, keep));
284
+ pushFold(run.slice(keep));
285
+ } else if (run.length > context * 2) {
286
+ pushItems(run.slice(0, context));
287
+ pushFold(run.slice(context, run.length - context));
288
+ pushItems(run.slice(run.length - context));
289
+ } else {
290
+ pushItems(run);
291
+ }
292
+ i = j;
293
+ }
294
+
295
+ return out;
296
+ }
package/src/menu/Menu.tsx CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { ButtonHTMLAttributes, ReactNode } from "react";
2
- import { FluentIcon } from "../icon/FluentIcon";
2
+ import { SolarIcon } from "../icon/SolarIcon";
3
3
  import { cn } from "../lib/cn";
4
4
  import type { PopperPlacement } from "../lib/placement";
5
5
  import { Popover } from "../popover/Popover";
@@ -48,7 +48,7 @@ export function MenuTrigger({ active, open, className, children, ...rest }: Menu
48
48
  {...rest}
49
49
  >
50
50
  <span className="min-w-0 truncate leading-5">{children}</span>
51
- <FluentIcon name="arrow-square-down-24" size={12} className={cn("shrink-0 transition-transform duration-150", open && "rotate-180")} />
51
+ <SolarIcon name="alt-arrow-down-linear" size={12} className={cn("shrink-0 transition-transform duration-150", open && "rotate-180")} />
52
52
  </button>
53
53
  );
54
54
  }
@@ -75,7 +75,7 @@ export function MenuItem({ icon, label, extra, selected, action, onClick, classN
75
75
  selected ? "font-medium text-foreground" : "font-normal text-foreground",
76
76
  )}
77
77
  >
78
- {icon ? <span className="inline-flex size-4 shrink-0 items-center justify-center [&_img]:size-4">{icon}</span> : null}
78
+ {icon ? <span className="inline-flex size-4 shrink-0 items-center justify-center [&_img]:size-4 [&_svg]:size-4">{icon}</span> : null}
79
79
  <span className="min-w-0 flex-1 truncate">{label}</span>
80
80
  {extra != null ? <span className={cn("shrink-0 text-xs tabular-nums text-muted-foreground", action && "group-hover:invisible")}>{extra}</span> : null}
81
81
  </button>