@lanfermann/ui 0.3.0 → 0.5.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 (81) hide show
  1. package/dist/components/anchor-heading/anchor-heading.d.ts +1 -1
  2. package/dist/components/breadcrumbs/breadcrumbs.js +5 -10
  3. package/dist/components/button/button.d.ts +3 -0
  4. package/dist/components/button/button.js +14 -4
  5. package/dist/components/button-group/button-group-context.js +14 -0
  6. package/dist/components/button-group/button-group-variants.d.ts +14 -0
  7. package/dist/components/button-group/button-group-variants.js +93 -0
  8. package/dist/components/button-group/button-group.d.ts +36 -0
  9. package/dist/components/button-group/button-group.js +46 -0
  10. package/dist/components/code-block/code-block-frame.js +22 -6
  11. package/dist/components/code-block/code-block-live.d.ts +15 -3
  12. package/dist/components/code-block/code-block-live.js +14 -6
  13. package/dist/components/code-block/code-block.d.ts +8 -3
  14. package/dist/components/code-block/code-block.js +9 -3
  15. package/dist/components/code-block/code-keys.d.ts +24 -0
  16. package/dist/components/code-block/code-keys.js +107 -0
  17. package/dist/components/code-block/code-lines-live.d.ts +7 -0
  18. package/dist/components/code-block/code-lines-live.js +167 -0
  19. package/dist/components/code-block/code-lines-view.d.ts +1 -0
  20. package/dist/components/code-block/code-lines-view.js +50 -18
  21. package/dist/components/code-block/code-lines.d.ts +3 -0
  22. package/dist/components/code-block/code-scroll.js +2 -1
  23. package/dist/components/code-block/code-tabs-frame.js +12 -5
  24. package/dist/components/code-block/code-tabs.d.ts +10 -4
  25. package/dist/components/code-block/code-tabs.js +9 -6
  26. package/dist/components/code-block/highlight.d.ts +1 -0
  27. package/dist/components/code-block/line-diff.js +135 -0
  28. package/dist/components/code-block/lines.js +23 -1
  29. package/dist/components/copy-page/copy-page.d.ts +4 -4
  30. package/dist/components/copy-page/copy-page.js +16 -28
  31. package/dist/components/docs-layout/docs-layout.d.ts +1 -1
  32. package/dist/components/docs-layout/docs-layout.js +3 -3
  33. package/dist/components/docs-layout/docs-nav.d.ts +51 -0
  34. package/dist/components/docs-layout/docs-nav.js +60 -0
  35. package/dist/components/grow/grow.d.ts +20 -0
  36. package/dist/components/grow/grow.js +104 -0
  37. package/dist/components/heading/heading.d.ts +4 -0
  38. package/dist/components/heading/heading.js +8 -4
  39. package/dist/components/hover-card/hover-card.d.ts +24 -0
  40. package/dist/components/hover-card/hover-card.js +51 -0
  41. package/dist/components/pager/pager.js +19 -24
  42. package/dist/components/scroll-fade/scroll-fade.d.ts +29 -0
  43. package/dist/components/scroll-fade/scroll-fade.js +40 -0
  44. package/dist/components/scroll-fade/use-scroll-edges.d.ts +13 -0
  45. package/dist/{internal/scroll-edges.js → components/scroll-fade/use-scroll-edges.js} +10 -7
  46. package/dist/components/section/section.d.ts +2 -1
  47. package/dist/components/section/section.js +2 -1
  48. package/dist/components/segmented-control/segmented-control.d.ts +7 -1
  49. package/dist/components/segmented-control/segmented-control.js +22 -11
  50. package/dist/components/select/select.d.ts +4 -1
  51. package/dist/components/select/select.js +15 -3
  52. package/dist/components/shimmer/shimmer.d.ts +21 -0
  53. package/dist/components/shimmer/shimmer.js +24 -0
  54. package/dist/components/side-nav/side-nav.js +11 -20
  55. package/dist/components/site-header/site-header.d.ts +1 -1
  56. package/dist/components/site-header/site-header.js +6 -3
  57. package/dist/components/slider/slider.d.ts +3 -1
  58. package/dist/components/slider/slider.js +45 -19
  59. package/dist/components/table/table.js +2 -1
  60. package/dist/components/tabs/tabs.js +3 -2
  61. package/dist/components/theme-switch/theme-switch.d.ts +47 -0
  62. package/dist/components/theme-switch/theme-switch.js +102 -0
  63. package/dist/components/trace/trace.d.ts +71 -0
  64. package/dist/components/trace/trace.js +201 -0
  65. package/dist/index.d.ts +11 -2
  66. package/dist/index.js +10 -2
  67. package/dist/internal/render-as.js +24 -0
  68. package/dist/internal/rule.js +5 -4
  69. package/package.json +1 -1
  70. package/src/styles/button-group.css +57 -0
  71. package/src/styles/code.css +102 -38
  72. package/src/styles/marks.css +10 -0
  73. package/src/styles/motion.css +4 -3
  74. package/src/styles/scroll-fade.css +26 -20
  75. package/src/styles/shimmer.css +68 -0
  76. package/src/styles/slider.css +57 -19
  77. package/src/styles.css +2 -1
  78. package/dist/components/page-header/page-header.d.ts +0 -30
  79. package/dist/components/page-header/page-header.js +0 -49
  80. package/dist/internal/scroll-fade.js +0 -24
  81. package/src/styles/tabs.css +0 -39
@@ -0,0 +1,167 @@
1
+ "use client";
2
+ import { useReducedMotion } from "../../internal/reduced-motion.js";
3
+ import { CodeRows } from "./code-lines-view.js";
4
+ import { changedRange, diffLines } from "./line-diff.js";
5
+ import { lineKinds } from "./lines.js";
6
+ import { jsx } from "react/jsx-runtime";
7
+ import { useCallback, useEffect, useRef, useState } from "react";
8
+ // src/components/code-block/code-lines-live.tsx
9
+ /** How long a closing line is kept at most: past the longest transition, in ms. */
10
+ const LEAVE_AT_MOST = 1500;
11
+ const textOf = (tokens) => tokens.map((token) => token.content).join("");
12
+ function signatureOf(lines, kinds) {
13
+ return `${lines.map((line) => line.map((token) => `${token.content}\u0001${token.color ?? ""}`).join("")).join("\n")}\u0000${kinds.join(",")}`;
14
+ }
15
+ function firstRows(lines, kinds, signature) {
16
+ return {
17
+ signature,
18
+ lines: lines.map((tokens, id) => ({
19
+ id,
20
+ text: textOf(tokens),
21
+ tokens,
22
+ kind: kinds[id]
23
+ })),
24
+ leaving: [],
25
+ fresh: /* @__PURE__ */ new Map(),
26
+ next: lines.length
27
+ };
28
+ }
29
+ /**
30
+ * The rows after the code changed: a line that goes on keeps its id, a new one gets a new id,
31
+ * and a gone one stays behind the row it followed until it has closed. A line that is on its way
32
+ * out and comes back where it stood takes its id back, so its row turns round instead of
33
+ * opening anew. Under reduced motion nothing is kept on its way out.
34
+ */
35
+ function nextRows(rows, lines, kinds, signature, still) {
36
+ const texts = lines.map(textOf);
37
+ const { from, removed } = diffLines(rows.lines.map((line) => line.text), texts);
38
+ let next = rows.next;
39
+ const fresh = /* @__PURE__ */ new Map();
40
+ const leaving = still ? [] : [...rows.leaving];
41
+ const ids = [];
42
+ texts.forEach((text, index) => {
43
+ const before = rows.lines[from[index]];
44
+ if (before) {
45
+ ids.push(before.id);
46
+ const range = before.text === text ? null : changedRange(before.text, text);
47
+ if (range) fresh.set(before.id, {
48
+ range,
49
+ stamp: next++
50
+ });
51
+ return;
52
+ }
53
+ const after = index === 0 ? -1 : ids[index - 1];
54
+ const back = leaving.findIndex((line) => line.text === text && line.after === after);
55
+ if (back === -1) ids.push(next++);
56
+ else ids.push(...leaving.splice(back, 1).map((line) => line.id));
57
+ });
58
+ if (!still) for (const index of removed) leaving.push({
59
+ ...rows.lines[index],
60
+ after: index === 0 ? -1 : rows.lines[index - 1].id
61
+ });
62
+ return {
63
+ signature,
64
+ lines: texts.map((text, index) => ({
65
+ id: ids[index],
66
+ text,
67
+ tokens: lines[index],
68
+ kind: kinds[index]
69
+ })),
70
+ leaving,
71
+ fresh,
72
+ next
73
+ };
74
+ }
75
+ /** The rows in the order they stand: each closing line right after the row it followed. */
76
+ function arrange({ lines, leaving, fresh }) {
77
+ const rows = [];
78
+ const closingAfter = (id) => {
79
+ for (const line of leaving) {
80
+ if (line.after !== id) continue;
81
+ rows.push({
82
+ key: `r${line.id}`,
83
+ tokens: line.tokens,
84
+ kind: line.kind,
85
+ leaving: true
86
+ });
87
+ closingAfter(line.id);
88
+ }
89
+ };
90
+ closingAfter(-1);
91
+ for (const line of lines) {
92
+ rows.push({
93
+ key: `r${line.id}`,
94
+ tokens: line.tokens,
95
+ kind: line.kind,
96
+ fresh: fresh.get(line.id)
97
+ });
98
+ closingAfter(line.id);
99
+ }
100
+ return rows;
101
+ }
102
+ /**
103
+ * The lines of a `CodeBlockLive`: `CodeLinesView` for code that changes under the reader's hand.
104
+ * When the code changes, the lines are matched to the ones before (line-diff.ts). A new line
105
+ * opens like a fold: its row opens and pushes the lines below down while its text fades in. A
106
+ * gone line fades and its row closes, and the part of a line that changed shows as new
107
+ * (code.css). What stands when the block first shows does not move, and neither does a block
108
+ * that has just mounted (the panel of another tab).
109
+ */
110
+ function CodeLinesLive({ lines, marked, added, removed, changeMotion = "fade", ...options }) {
111
+ const kinds = lineKinds(lines.length, {
112
+ marked,
113
+ added,
114
+ removed
115
+ });
116
+ const signature = signatureOf(lines, kinds);
117
+ const still = useReducedMotion();
118
+ const [rows, setRows] = useState(() => firstRows(lines, kinds, signature));
119
+ if (rows.signature !== signature) setRows(nextRows(rows, lines, kinds, signature, still));
120
+ const [live, setLive] = useState(false);
121
+ useEffect(() => {
122
+ const frame = requestAnimationFrame(() => setLive(true));
123
+ return () => cancelAnimationFrame(frame);
124
+ }, []);
125
+ /** A closing line has closed: it goes, and what stood behind it stands behind its place. */
126
+ const drop = useCallback((id) => {
127
+ setRows((current) => {
128
+ const gone = current.leaving.find((line) => line.id === id);
129
+ if (!gone) return current;
130
+ return {
131
+ ...current,
132
+ leaving: current.leaving.filter((line) => line !== gone).map((line) => line.after === id ? {
133
+ ...line,
134
+ after: gone.after
135
+ } : line)
136
+ };
137
+ });
138
+ }, []);
139
+ const code = useRef(null);
140
+ useEffect(() => {
141
+ const element = code.current;
142
+ if (!element) return;
143
+ const closed = (event) => {
144
+ const line = event.target;
145
+ if (event.propertyName !== "grid-template-rows" || line.dataset.motion !== "leave") return;
146
+ drop(Number(line.dataset.row?.slice(1)));
147
+ };
148
+ element.addEventListener("transitionend", closed);
149
+ return () => element.removeEventListener("transitionend", closed);
150
+ }, [drop]);
151
+ useEffect(() => {
152
+ if (rows.leaving.length === 0) return;
153
+ const timer = setTimeout(() => setRows((current) => ({
154
+ ...current,
155
+ leaving: []
156
+ })), LEAVE_AT_MOST);
157
+ return () => clearTimeout(timer);
158
+ }, [rows.leaving]);
159
+ return /* @__PURE__ */ jsx(CodeRows, {
160
+ ref: code,
161
+ rows: arrange(rows),
162
+ "data-live": live ? "" : void 0,
163
+ "data-change-motion": changeMotion,
164
+ ...options
165
+ });
166
+ }
167
+ export { CodeLinesLive };
@@ -1,4 +1,5 @@
1
1
  import { LineSpec } from "./lines.js";
2
+ import "react";
2
3
  export type CodeLinesOptions = {
3
4
  /** Numbers before each line. For code someone will talk about by line; off for snippets. */
4
5
  lineNumbers?: boolean;
@@ -1,3 +1,4 @@
1
+ import { splitRuns } from "./line-diff.js";
1
2
  import { lineKinds } from "./lines.js";
2
3
  import { jsx } from "react/jsx-runtime";
3
4
  // src/components/code-block/code-lines-view.tsx
@@ -7,42 +8,73 @@ import { jsx } from "react/jsx-runtime";
7
8
  * `CodeBlockLive`) is not its business, and no highlighter may be imported here: this is the
8
9
  * half that may travel to the browser.
9
10
  */
10
- function CodeLinesView({ lines, lineNumbers = false, marked, added, removed, wrap = false, addedLabel = "hinzugefügt", removedLabel = "entfernt" }) {
11
+ function CodeLinesView({ lines, marked, added, removed, ...options }) {
11
12
  const kinds = lineKinds(lines.length, {
12
13
  marked,
13
14
  added,
14
15
  removed
15
16
  });
16
- const changes = kinds.some((kind) => kind === "added" || kind === "removed");
17
- const digits = lineNumbers ? String(lines.length).length : 0;
17
+ return /* @__PURE__ */ jsx(CodeRows, {
18
+ rows: lines.map((tokens, index) => ({
19
+ key: index,
20
+ tokens,
21
+ kind: kinds[index]
22
+ })),
23
+ ...options
24
+ });
25
+ }
26
+ /**
27
+ * The `<code>` itself, from rows that may carry an identity, a line on its way out and the part
28
+ * a change put in. `CodeLinesView` draws code that stands; `CodeLinesLive` code that changes.
29
+ *
30
+ * A line is a grid of one row around its body, so the row can open from nothing and close to
31
+ * nothing (`grid-template-rows`, code.css) while the lines below make room.
32
+ */
33
+ function CodeRows({ rows, lineNumbers = false, wrap = false, addedLabel = "hinzugefügt", removedLabel = "entfernt", style, ...props }) {
34
+ const changes = rows.some((row) => row.kind === "added" || row.kind === "removed");
35
+ const count = rows.filter((row) => !row.leaving).length;
36
+ const digits = lineNumbers ? String(count).length : 0;
18
37
  const numberRoom = lineNumbers ? `${digits}ch + 1.25rem` : "0px";
19
38
  const signRoom = changes ? "1ch + 0.75rem" : "0px";
20
- const style = {
39
+ const measures = {
21
40
  "--code-digits": `${digits}ch`,
22
41
  "--code-sign-at": `calc(${numberRoom})`,
23
- "--code-gutter": `calc(${numberRoom} + ${signRoom})`
42
+ "--code-gutter": `calc(${numberRoom} + ${signRoom})`,
43
+ ...style
24
44
  };
25
45
  return /* @__PURE__ */ jsx("code", {
26
46
  "data-slot": "code-block-code",
27
47
  "data-line-numbers": lineNumbers ? "" : void 0,
28
48
  "data-wrap": wrap ? "" : void 0,
29
- style,
30
- children: lines.map((tokens, index) => {
31
- const kind = kinds[index];
49
+ style: measures,
50
+ ...props,
51
+ children: rows.map(({ key, tokens, kind, leaving, fresh }) => {
52
+ const runs = tokens.filter((token) => token.content !== "");
53
+ const pieces = fresh ? splitRuns(runs, fresh.range) : runs;
32
54
  return /* @__PURE__ */ jsx("span", {
33
55
  "data-slot": "code-block-line",
34
56
  "data-kind": kind,
57
+ "data-motion": leaving ? "leave" : void 0,
58
+ "data-row": leaving ? key : void 0,
35
59
  "data-label": kind === "added" ? addedLabel : kind === "removed" ? removedLabel : void 0,
36
- children: tokens.map((token, at) => /* @__PURE__ */ jsx("span", {
37
- style: {
38
- color: token.color,
39
- fontStyle: token.italic ? "italic" : void 0,
40
- fontWeight: token.bold ? 600 : void 0
41
- },
42
- children: token.content
43
- }, at))
44
- }, index);
60
+ "aria-hidden": leaving || void 0,
61
+ children: /* @__PURE__ */ jsx("span", {
62
+ "data-slot": "code-block-line-body",
63
+ children: pieces.map((piece, at) => {
64
+ const isFresh = "fresh" in piece && piece.fresh;
65
+ return /* @__PURE__ */ jsx("span", {
66
+ "data-slot": isFresh ? "code-block-fresh" : void 0,
67
+ style: {
68
+ color: piece.color,
69
+ fontStyle: piece.italic ? "italic" : void 0,
70
+ fontWeight: piece.bold ? 600 : void 0
71
+ },
72
+ children: piece.content
73
+ }, isFresh ? `fresh-${fresh?.stamp}-${at}` : at);
74
+ })
75
+ })
76
+ }, key);
45
77
  })
46
78
  });
47
79
  }
48
- export { CodeLinesView };
80
+ export { CodeLinesView, CodeRows };
@@ -0,0 +1,3 @@
1
+ import { CodeLinesOptions } from "./code-lines-view.js";
2
+ import "./highlight.js";
3
+ export type { CodeLinesOptions };
@@ -1,5 +1,5 @@
1
1
  "use client";
2
- import { useScrollEdges } from "../../internal/scroll-edges.js";
2
+ import { useScrollEdges } from "../scroll-fade/use-scroll-edges.js";
3
3
  import { cn } from "../../lib/cn.js";
4
4
  import { jsx } from "react/jsx-runtime";
5
5
  import { useRef } from "react";
@@ -15,6 +15,7 @@ function CodeScroll({ label, codeRef, className, preClassName, children }) {
15
15
  return /* @__PURE__ */ jsx("div", {
16
16
  ref,
17
17
  "data-slot": "code-block-scroll",
18
+ "data-scroll-fade": "x",
18
19
  role: "group",
19
20
  "aria-label": label,
20
21
  tabIndex: 0,
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
  import { cn } from "../../lib/cn.js";
3
- import { CodeCopy } from "./code-copy.js";
3
+ import { CodeKeys } from "./code-keys.js";
4
4
  import { CodeScroll } from "./code-scroll.js";
5
5
  import { codeHead, codeRoot } from "./code-surface.js";
6
6
  import { readChoice, subscribeChoices, writeChoice } from "./code-tabs-store.js";
@@ -11,9 +11,10 @@ import { Tabs } from "@base-ui/react/tabs";
11
11
  /**
12
12
  * The client half of CodeTabs: a row of tabs in the header, each with its mark, and one soft
13
13
  * highlight that glides to the chosen tab (`glide`, placed by Base UI's measure and moved by
14
- * `translate`). The copy button takes the code that is shown.
14
+ * `translate`). The keys at the end of the header take the code that is shown.
15
15
  */
16
- function CodeTabsFrame({ children, tabs, group, defaultValue, label, className, ...labels }) {
16
+ function CodeTabsFrame({ children, tabs, group, defaultValue, label, download, wrap = false, wrapToggle = false, className, ...keyOptions }) {
17
+ const [wrapped, setWrapped] = useState(wrap);
17
18
  const names = tabs.map((tab) => tab.label);
18
19
  const [own, setOwn] = useState(defaultValue ?? names[0]);
19
20
  const shared = useSyncExternalStore(subscribeChoices, () => group ? readChoice(group) : void 0, () => void 0);
@@ -36,6 +37,7 @@ function CodeTabsFrame({ children, tabs, group, defaultValue, label, className,
36
37
  setOwn(next);
37
38
  if (group) writeChoice(group, next);
38
39
  },
40
+ "data-wrap": wrapToggle ? wrapped ? "on" : "off" : void 0,
39
41
  className: cn(codeRoot, className),
40
42
  children: [/* @__PURE__ */ jsxs("div", {
41
43
  "data-slot": "code-tabs-header",
@@ -56,10 +58,15 @@ function CodeTabsFrame({ children, tabs, group, defaultValue, label, className,
56
58
  "data-slot": "code-tabs-indicator",
57
59
  className: cn("absolute top-(--active-tab-top) left-0 -z-10 h-(--active-tab-height) w-(--active-tab-width) translate-x-(--active-tab-left) rounded-sm bg-hover", "forced-colors:outline forced-colors:-outline-offset-1", placed && "transition-[translate,width] spring-glide")
58
60
  })]
59
- }), /* @__PURE__ */ jsx(CodeCopy, {
61
+ }), /* @__PURE__ */ jsx(CodeKeys, {
60
62
  text: tabs[index]?.copyText ?? "",
61
63
  codeRef: activeCode,
62
- ...labels
64
+ fileName: tabs[index]?.fileName,
65
+ download,
66
+ wrapToggle,
67
+ wrapped,
68
+ onWrappedChange: setWrapped,
69
+ ...keyOptions
63
70
  })]
64
71
  }), panels.map((panel, at) => /* @__PURE__ */ jsx(Tabs.Panel, {
65
72
  value: names[at],
@@ -1,13 +1,16 @@
1
- import { CodeCopyLabels } from "./code-copy.js";
1
+ import { CodeKeyOptions } from "./code-keys.js";
2
2
  import { CodeLinesOptions } from "./code-lines-view.js";
3
+ import "./code-lines.js";
3
4
  import { CodeLanguage } from "./highlight.js";
4
5
  export type CodeTab = CodeLinesOptions & {
5
6
  /** The tab: a noun of one or two words, often the tool's own name („bun“, „npm“). */
6
7
  label: string;
7
8
  code: string;
8
9
  language?: CodeLanguage;
10
+ /** The file this version is saved as when the block has a download key. */
11
+ fileName?: string;
9
12
  };
10
- export type CodeTabsProps = CodeCopyLabels & {
13
+ export type CodeTabsProps = Omit<CodeKeyOptions, "download"> & {
11
14
  items: CodeTab[];
12
15
  /**
13
16
  * Blocks with the same group share the reader's choice: picking „bun“ in one shows bun in all
@@ -18,11 +21,14 @@ export type CodeTabsProps = CodeCopyLabels & {
18
21
  defaultValue?: string;
19
22
  /** The name of the tab row, and of each code region after it. */
20
23
  label?: string;
24
+ /** Adds a key that saves the version that is shown, as its tab's `fileName`. */
25
+ download?: boolean;
21
26
  className?: string;
22
27
  };
23
28
  /**
24
29
  * One block, several versions of the same code: the install command for each package manager,
25
30
  * or a component and its stylesheet. The tabs sit in the header with their marks; the copy
26
- * button copies the version that is shown.
31
+ * button copies the version that is shown, and so do the download and the line wrap when the
32
+ * block has them (see `CodeBlock`).
27
33
  */
28
- export declare function CodeTabs({ items, group, defaultValue, label, className, ...copyLabels }: CodeTabsProps): import("react").JSX.Element;
34
+ export declare function CodeTabs({ items, group, defaultValue, label, className, ...keyOptions }: CodeTabsProps): import("react").JSX.Element;
@@ -1,25 +1,28 @@
1
1
  import { CodeLines } from "./code-lines.js";
2
2
  import { CodeTabsFrame } from "./code-tabs-frame.js";
3
- import { copyText } from "./lines.js";
3
+ import { codeFileName, copyText } from "./lines.js";
4
4
  import { jsx } from "react/jsx-runtime";
5
5
  // src/components/code-block/code-tabs.tsx
6
6
  /**
7
7
  * One block, several versions of the same code: the install command for each package manager,
8
8
  * or a component and its stylesheet. The tabs sit in the header with their marks; the copy
9
- * button copies the version that is shown.
9
+ * button copies the version that is shown, and so do the download and the line wrap when the
10
+ * block has them (see `CodeBlock`).
10
11
  */
11
- function CodeTabs({ items, group, defaultValue, label = "Code", className, ...copyLabels }) {
12
+ function CodeTabs({ items, group, defaultValue, label = "Code", className, ...keyOptions }) {
12
13
  return /* @__PURE__ */ jsx(CodeTabsFrame, {
13
14
  tabs: items.map((item) => ({
14
15
  label: item.label,
15
- copyText: copyText(item.code, item.removed)
16
+ copyText: copyText(item.code, item.removed),
17
+ fileName: item.fileName ?? codeFileName(void 0, item.language)
16
18
  })),
17
19
  group,
18
20
  defaultValue,
19
21
  label,
22
+ wrap: items.every((item) => item.wrap),
20
23
  className,
21
- ...copyLabels,
22
- children: items.map(({ label: name, code, language = "text", ...lines }) => /* @__PURE__ */ jsx(CodeLines, {
24
+ ...keyOptions,
25
+ children: items.map(({ label: name, code, language = "text", fileName: _, ...lines }) => /* @__PURE__ */ jsx(CodeLines, {
23
26
  code,
24
27
  language,
25
28
  ...lines
@@ -1,3 +1,4 @@
1
+ import "./code-lines-view.js";
1
2
  /** The languages a block can colour, by the names people write, and how many lines it may hold. */
2
3
  declare const languages: {
3
4
  readonly tsx: "tsx";
@@ -0,0 +1,135 @@
1
+ // src/components/code-block/line-diff.ts
2
+ /** Above this many cells the table of a full comparison costs more than a frame. */
3
+ const MAX_CELLS = 25e4;
4
+ /** A line as it is compared: a comma or spaces at its end do not make it another line. */
5
+ function shape(line) {
6
+ return line.trimEnd().replace(/,$/, "");
7
+ }
8
+ /**
9
+ * What a line sets, if it sets something: its text up to the first `:` or `=`, indent included
10
+ * (` "SEVDESK_ALLOW"`, `SEVDESK_ALLOW`, ` -e SEVDESK_ALLOW`). Two lines that set the same thing
11
+ * are one line whose value changed.
12
+ */
13
+ function setting(line) {
14
+ return /^(\s*[^:=\s][^:=]*?)\s*[:=]/.exec(line)?.[1];
15
+ }
16
+ /** Whether two lines that differ are still the same line, changed where it stands. */
17
+ function changedInPlace(before, after) {
18
+ const key = setting(before);
19
+ if (key !== void 0) return key === setting(after);
20
+ if (before.trim() === "" || after.trim() === "") return false;
21
+ let shared = 0;
22
+ while (shared < before.length && before[shared] === after[shared]) shared++;
23
+ return shared * 2 >= Math.max(before.length, after.length);
24
+ }
25
+ /**
26
+ * The pairs of a longest common subsequence of `a[aFrom..aTo)` and `b[bFrom..bTo)` under `same`,
27
+ * in order. Quadratic, so it is only used on code of a page's size.
28
+ */
29
+ function commonPairs(a, b, same, [aFrom, aTo], [bFrom, bTo]) {
30
+ const rows = aTo - aFrom;
31
+ const cols = bTo - bFrom;
32
+ if (rows === 0 || cols === 0) return [];
33
+ const width = cols + 1;
34
+ const lengths = new Uint32Array((rows + 1) * width);
35
+ for (let i = rows - 1; i >= 0; i--) for (let j = cols - 1; j >= 0; j--) lengths[i * width + j] = same(a[aFrom + i], b[bFrom + j]) ? lengths[(i + 1) * width + j + 1] + 1 : Math.max(lengths[(i + 1) * width + j], lengths[i * width + j + 1]);
36
+ const pairs = [];
37
+ let i = 0;
38
+ let j = 0;
39
+ while (i < rows && j < cols) if (same(a[aFrom + i], b[bFrom + j])) {
40
+ pairs.push([aFrom + i, bFrom + j]);
41
+ i++;
42
+ j++;
43
+ } else if (lengths[(i + 1) * width + j] >= lengths[i * width + j + 1]) i++;
44
+ else j++;
45
+ return pairs;
46
+ }
47
+ /**
48
+ * Which lines of `after` continue lines of `before`, and which of `before` are gone.
49
+ *
50
+ * Lines are first matched by their text, a trailing comma aside (a line that gains one when a
51
+ * line is added below it stays itself). Between two matches, a line that sets the same thing as
52
+ * one before it (`SEVDESK_ALLOW=read` and `SEVDESK_ALLOW=read,draft`) is that line, changed in
53
+ * place; everything else there is new or gone. Code too long to compare is matched line by line
54
+ * from both ends.
55
+ */
56
+ function diffLines(before, after) {
57
+ const from = Array.from({ length: after.length }, () => -1);
58
+ const kept = /* @__PURE__ */ new Set();
59
+ const pair = ([i, j]) => {
60
+ from[j] = i;
61
+ kept.add(i);
62
+ };
63
+ if (before.length * after.length > MAX_CELLS) {
64
+ let start = 0;
65
+ while (start < before.length && start < after.length && before[start] === after[start]) {
66
+ pair([start, start]);
67
+ start++;
68
+ }
69
+ let end = 0;
70
+ while (end < before.length - start && end < after.length - start && before[before.length - 1 - end] === after[after.length - 1 - end]) {
71
+ pair([before.length - 1 - end, after.length - 1 - end]);
72
+ end++;
73
+ }
74
+ } else {
75
+ const a = before.map(shape);
76
+ const b = after.map(shape);
77
+ const matches = commonPairs(a, b, (x, y) => x === y, [0, a.length], [0, b.length]);
78
+ let [i0, j0] = [0, 0];
79
+ for (const [i, j] of [...matches, [a.length, b.length]]) {
80
+ for (const changed of commonPairs(before, after, changedInPlace, [i0, i], [j0, j])) pair(changed);
81
+ if (i < a.length) pair([i, j]);
82
+ [i0, j0] = [i + 1, j + 1];
83
+ }
84
+ }
85
+ return {
86
+ from,
87
+ removed: before.map((_, index) => index).filter((index) => !kept.has(index))
88
+ };
89
+ }
90
+ /**
91
+ * The part of `after` that is not in `before`: what lies between their common start and common
92
+ * end, as `[start, end)` in `after`. Null when nothing was added (the same text, or only a
93
+ * deletion).
94
+ */
95
+ function changedRange(before, after) {
96
+ const most = Math.min(before.length, after.length);
97
+ let start = 0;
98
+ while (start < most && before[start] === after[start]) start++;
99
+ let end = 0;
100
+ while (end < most - start && before[before.length - 1 - end] === after[after.length - 1 - end]) end++;
101
+ return start < after.length - end ? [start, after.length - end] : null;
102
+ }
103
+ /**
104
+ * The runs of a line, cut where `range` starts and ends, with the runs inside it flagged
105
+ * `fresh`. A run keeps its colour on both sides of a cut.
106
+ */
107
+ function splitRuns(runs, [start, end]) {
108
+ const pieces = [];
109
+ let at = 0;
110
+ for (const run of runs) {
111
+ const runEnd = at + run.content.length;
112
+ const cuts = [
113
+ at,
114
+ Math.min(Math.max(start, at), runEnd),
115
+ Math.min(Math.max(end, at), runEnd),
116
+ runEnd
117
+ ];
118
+ for (let k = 0; k < 3; k++) {
119
+ const [from, to] = [cuts[k], cuts[k + 1]];
120
+ if (to <= from) continue;
121
+ const content = run.content.slice(from - at, to - at);
122
+ pieces.push(k === 1 ? {
123
+ ...run,
124
+ content,
125
+ fresh: true
126
+ } : {
127
+ ...run,
128
+ content
129
+ });
130
+ }
131
+ at = runEnd;
132
+ }
133
+ return pieces;
134
+ }
135
+ export { changedRange, diffLines, splitRuns };
@@ -37,4 +37,26 @@ function lineKinds(count, { marked, added, removed }) {
37
37
  ];
38
38
  return Array.from({ length: count }, (_, i) => r.has(i + 1) ? "removed" : a.has(i + 1) ? "added" : m.has(i + 1) ? "marked" : void 0);
39
39
  }
40
- export { copyText, lineKinds, parseLines, trimCode };
40
+ /**
41
+ * The file a block of code is saved as when nothing names it: the last part of its title if that
42
+ * is a file's name ("app/api/route.ts" gives "route.ts"), or `code` with the ending of its
43
+ * language.
44
+ */
45
+ function codeFileName(title, language = "text") {
46
+ const name = title?.split(/[/\\]/).pop();
47
+ if (name && /^\S*\.[A-Za-z0-9]+$/.test(name)) return name;
48
+ return `code.${{
49
+ text: "txt",
50
+ shell: "sh",
51
+ bash: "sh",
52
+ zsh: "sh",
53
+ typescript: "ts",
54
+ javascript: "js",
55
+ markdown: "md",
56
+ python: "py",
57
+ powershell: "ps1",
58
+ dotenv: "env",
59
+ yml: "yaml"
60
+ }[language] ?? language}`;
61
+ }
62
+ export { codeFileName, copyText, lineKinds, parseLines, trimCode };
@@ -1,5 +1,5 @@
1
1
  import { ComponentProps } from "react";
2
- export type CopyPageProps = Omit<ComponentProps<"div">, "children"> & {
2
+ export type CopyPageProps = Omit<ComponentProps<"div">, "children" | "aria-label" | "aria-labelledby"> & {
3
3
  /** The page as Markdown, on this site: what is copied, and what "view" opens. */
4
4
  href: string;
5
5
  /** The button, and the first entry of the menu: the thing, then the action ("Seite kopieren"). */
@@ -24,10 +24,10 @@ export type CopyPageProps = Omit<ComponentProps<"div">, "children"> & {
24
24
  /**
25
25
  * Hands a docs page on as Markdown: one raised fitting split in two, the copy and the menu. The
26
26
  * copy fetches the page's Markdown and puts it on the clipboard, for a model or an agent; the
27
- * menu repeats it with a line on what it is, and opens the Markdown in a new tab. Put it in the
28
- * page header, at the end of the row above the title.
27
+ * menu repeats it with a line on what it is, and opens the Markdown in a new tab. Put it at the
28
+ * end of the row above the page's title.
29
29
  *
30
- * It is one part with two keys, so it moves as one: under the pointer the whole fitting lifts,
30
+ * It is a `ButtonGroup`, named by its `label`: one part with two keys, so it moves as one: under the pointer the whole fitting lifts,
31
31
  * and the key under it takes the hover wash. Pressed, the fitting goes down and drops its
32
32
  * ambient shadow, and only the pressed key takes the inner shade, so it rocks on that key and the
33
33
  * other keeps its gloss. A copy is confirmed in the key, as in a code block: the copy symbol