@lanfermann/ui 0.4.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.
- package/dist/components/code-block/code-block-live.d.ts +13 -1
- package/dist/components/code-block/code-block-live.js +10 -4
- package/dist/components/code-block/code-block.d.ts +1 -0
- package/dist/components/code-block/code-lines-live.d.ts +7 -0
- package/dist/components/code-block/code-lines-live.js +167 -0
- package/dist/components/code-block/code-lines-view.d.ts +1 -0
- package/dist/components/code-block/code-lines-view.js +50 -18
- package/dist/components/code-block/code-lines.d.ts +3 -0
- package/dist/components/code-block/code-tabs.d.ts +1 -0
- package/dist/components/code-block/highlight.d.ts +1 -0
- package/dist/components/code-block/line-diff.js +135 -0
- package/dist/components/grow/grow.js +7 -1
- package/package.json +1 -1
- package/src/styles/code.css +89 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { IconSvgElement } from "../icon/icon.js";
|
|
2
2
|
import { CodeKeyOptions } from "./code-keys.js";
|
|
3
|
+
import { CodeChangeMotion } from "./code-lines-live.js";
|
|
3
4
|
import { CodeLinesOptions } from "./code-lines-view.js";
|
|
4
5
|
import { ReactNode } from "react";
|
|
5
6
|
/**
|
|
@@ -29,6 +30,12 @@ export type CodeBlockLiveProps = CodeLinesOptions & CodeKeyOptions & {
|
|
|
29
30
|
fold?: number;
|
|
30
31
|
foldLabel?: (hidden: number) => string;
|
|
31
32
|
className?: string;
|
|
33
|
+
/**
|
|
34
|
+
* How what a change put into a line (a value, a comma) shows: it fades in (`fade`, the
|
|
35
|
+
* default), or also lights up in the accent for a moment (`glow`), for a change the reader
|
|
36
|
+
* should not miss.
|
|
37
|
+
*/
|
|
38
|
+
changeMotion?: CodeChangeMotion;
|
|
32
39
|
};
|
|
33
40
|
/**
|
|
34
41
|
* A `CodeBlock` for code that is put together in the browser: a configuration generated from the
|
|
@@ -41,5 +48,10 @@ export type CodeBlockLiveProps = CodeLinesOptions & CodeKeyOptions & {
|
|
|
41
48
|
* along, about a megabyte for a few lines. So: `CodeBlock` for code that is known when the page
|
|
42
49
|
* is built, `CodeBlockLive` for code that changes under the reader's hand. Mark the lines their
|
|
43
50
|
* choice put there (`marked`), so they see what it changed.
|
|
51
|
+
*
|
|
52
|
+
* When the code changes, the block shows the change: a line that joins opens like a fold (its
|
|
53
|
+
* row opens and the lines below make room while its text fades in), a line that goes fades and
|
|
54
|
+
* its row closes, and what a change put into a line arrives as new (`changeMotion`). The block's
|
|
55
|
+
* height follows, so a `Grow` around it needs nothing more.
|
|
44
56
|
*/
|
|
45
|
-
export declare function CodeBlockLive({ lines, title, icon, mark, label, prompt, fold, foldLabel, className, lineNumbers, marked, added, removed, wrap, addedLabel, removedLabel, ...keyOptions }: CodeBlockLiveProps): import("react").JSX.Element;
|
|
57
|
+
export declare function CodeBlockLive({ lines, title, icon, mark, label, prompt, fold, foldLabel, className, lineNumbers, marked, added, removed, wrap, addedLabel, removedLabel, changeMotion, ...keyOptions }: CodeBlockLiveProps): import("react").JSX.Element;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CodeBlockFrame } from "./code-block-frame.js";
|
|
2
|
-
import {
|
|
2
|
+
import { CodeLinesLive } from "./code-lines-live.js";
|
|
3
3
|
import { codeFileName, parseLines } from "./lines.js";
|
|
4
4
|
import { jsx } from "react/jsx-runtime";
|
|
5
5
|
import { FileCodeIcon } from "@hugeicons/core-free-icons";
|
|
@@ -15,8 +15,13 @@ import { FileCodeIcon } from "@hugeicons/core-free-icons";
|
|
|
15
15
|
* along, about a megabyte for a few lines. So: `CodeBlock` for code that is known when the page
|
|
16
16
|
* is built, `CodeBlockLive` for code that changes under the reader's hand. Mark the lines their
|
|
17
17
|
* choice put there (`marked`), so they see what it changed.
|
|
18
|
+
*
|
|
19
|
+
* When the code changes, the block shows the change: a line that joins opens like a fold (its
|
|
20
|
+
* row opens and the lines below make room while its text fades in), a line that goes fades and
|
|
21
|
+
* its row closes, and what a change put into a line arrives as new (`changeMotion`). The block's
|
|
22
|
+
* height follows, so a `Grow` around it needs nothing more.
|
|
18
23
|
*/
|
|
19
|
-
function CodeBlockLive({ lines, title, icon = FileCodeIcon, mark, label = "Code", prompt = false, fold, foldLabel = (hidden) => `${hidden} weitere Zeilen`, className, lineNumbers, marked, added, removed, wrap, addedLabel, removedLabel, ...keyOptions }) {
|
|
24
|
+
function CodeBlockLive({ lines, title, icon = FileCodeIcon, mark, label = "Code", prompt = false, fold, foldLabel = (hidden) => `${hidden} weitere Zeilen`, className, lineNumbers, marked, added, removed, wrap, addedLabel, removedLabel, changeMotion, ...keyOptions }) {
|
|
20
25
|
const coloured = lines.map((line) => line.map((piece) => ({
|
|
21
26
|
content: piece.text,
|
|
22
27
|
color: `var(--lf-code-${piece.role ?? "text"})`,
|
|
@@ -38,7 +43,7 @@ function CodeBlockLive({ lines, title, icon = FileCodeIcon, mark, label = "Code"
|
|
|
38
43
|
fileName: codeFileName(title),
|
|
39
44
|
wrap,
|
|
40
45
|
...keyOptions,
|
|
41
|
-
children: /* @__PURE__ */ jsx(
|
|
46
|
+
children: /* @__PURE__ */ jsx(CodeLinesLive, {
|
|
42
47
|
lines: coloured,
|
|
43
48
|
lineNumbers,
|
|
44
49
|
marked,
|
|
@@ -46,7 +51,8 @@ function CodeBlockLive({ lines, title, icon = FileCodeIcon, mark, label = "Code"
|
|
|
46
51
|
removed,
|
|
47
52
|
wrap,
|
|
48
53
|
addedLabel,
|
|
49
|
-
removedLabel
|
|
54
|
+
removedLabel,
|
|
55
|
+
changeMotion
|
|
50
56
|
})
|
|
51
57
|
});
|
|
52
58
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { IconSvgElement } from "../icon/icon.js";
|
|
2
2
|
import { CodeKeyOptions } from "./code-keys.js";
|
|
3
3
|
import { CodeLinesOptions } from "./code-lines-view.js";
|
|
4
|
+
import "./code-lines.js";
|
|
4
5
|
import { CodeLanguage } from "./highlight.js";
|
|
5
6
|
import { ReactNode } from "react";
|
|
6
7
|
export type CodeBlockProps = CodeLinesOptions & CodeKeyOptions & {
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import "./code-lines-view.js";
|
|
2
|
+
/**
|
|
3
|
+
* How the part of a line that a change put in shows (a value, a comma): it fades in (`fade`),
|
|
4
|
+
* or also lights up in the accent and lets the light go (`glow`), for a change the reader should
|
|
5
|
+
* not miss.
|
|
6
|
+
*/
|
|
7
|
+
export type CodeChangeMotion = "fade" | "glow";
|
|
@@ -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,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,
|
|
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
|
-
|
|
17
|
-
|
|
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
|
|
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
|
-
|
|
31
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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 };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
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“). */
|
|
@@ -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 };
|
|
@@ -14,6 +14,10 @@ const heightProperties = /* @__PURE__ */ new Set([
|
|
|
14
14
|
function movingInside(content) {
|
|
15
15
|
return content.getAnimations({ subtree: true }).some((animation) => animation instanceof CSSTransition && heightProperties.has(animation.transitionProperty) && animation.playState !== "finished");
|
|
16
16
|
}
|
|
17
|
+
/** Whether the box itself is on its way to a height. */
|
|
18
|
+
function heightMoving(box) {
|
|
19
|
+
return box.getAnimations().some((animation) => animation instanceof CSSTransition && animation.transitionProperty === "height" && animation.playState === "running");
|
|
20
|
+
}
|
|
17
21
|
/**
|
|
18
22
|
* A block whose height follows what it holds. When something joins it (a step of a `Trace`, a
|
|
19
23
|
* paragraph that arrives, the next tab's panel), it eases to its new height on `settle`, and
|
|
@@ -71,7 +75,9 @@ function Grow({ className, children, ref, ...props }) {
|
|
|
71
75
|
});
|
|
72
76
|
observer.observe(inner);
|
|
73
77
|
const arrived = (event) => {
|
|
74
|
-
if (event.target
|
|
78
|
+
if (event.target !== outer || event.propertyName !== "height") return;
|
|
79
|
+
if (event.type === "transitioncancel" && heightMoving(outer)) return;
|
|
80
|
+
rest();
|
|
75
81
|
};
|
|
76
82
|
outer.addEventListener("transitionend", arrived);
|
|
77
83
|
outer.addEventListener("transitioncancel", arrived);
|
package/package.json
CHANGED
package/src/styles/code.css
CHANGED
|
@@ -140,13 +140,26 @@
|
|
|
140
140
|
overflow-wrap: normal;
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
+
/*
|
|
144
|
+
* A line is a grid of one row around its body, so a live block can open the row from nothing
|
|
145
|
+
* and close it again (below). It clips only up and down: an italic's overhang stays.
|
|
146
|
+
*/
|
|
143
147
|
[data-slot="code-block-line"] {
|
|
144
148
|
position: relative;
|
|
145
|
-
|
|
149
|
+
display: grid;
|
|
150
|
+
grid-template-rows: 1fr;
|
|
151
|
+
overflow-y: clip;
|
|
146
152
|
padding-inline: var(--code-pad-x) var(--code-pad-x);
|
|
147
153
|
padding-left: calc(var(--code-pad-x) + var(--code-gutter, 0px));
|
|
148
154
|
counter-increment: line;
|
|
149
155
|
}
|
|
156
|
+
[data-slot="code-block-line-body"] {
|
|
157
|
+
min-height: 0;
|
|
158
|
+
}
|
|
159
|
+
/* An empty line still holds one line of room: a zero-width space, which is never copied. */
|
|
160
|
+
[data-slot="code-block-line-body"]:empty::before {
|
|
161
|
+
content: "\200b";
|
|
162
|
+
}
|
|
150
163
|
|
|
151
164
|
/* Numbers are read past, not read out, and never copied with the code. */
|
|
152
165
|
[data-line-numbers] > [data-slot="code-block-line"]::before {
|
|
@@ -187,6 +200,81 @@
|
|
|
187
200
|
color: var(--lf-danger);
|
|
188
201
|
}
|
|
189
202
|
|
|
203
|
+
/*
|
|
204
|
+
* Lines that change (CodeBlockLive, code-lines-live.tsx). Only once the block stands
|
|
205
|
+
* (`data-live`): what is there when it first shows does not move.
|
|
206
|
+
*
|
|
207
|
+
* A line that joins opens like a fold: its row opens from nothing on `settle` and pushes the
|
|
208
|
+
* lines below down, while its text fades in and drops into place from just above
|
|
209
|
+
* (`--motion-enter-offset`). Chosen by Justin over a flap and a drawer (DESIGN.md). The row is
|
|
210
|
+
* the one height that moves here, because the lines below have to make room (motion.md); a
|
|
211
|
+
* `Grow` around the block sees it moving and follows. A line that goes fades on `leave` while
|
|
212
|
+
* its row closes, is not counted and has no number. Every state is a transition, so a line that
|
|
213
|
+
* comes back mid-way turns round.
|
|
214
|
+
*
|
|
215
|
+
* Part way, a row of `f fr` is narrower than the line around it (f times f, against f, of the
|
|
216
|
+
* line's height: fractions under 1 share out only part of the room). The line's own height is
|
|
217
|
+
* what makes room and what is seen, as it clips; the text overflows its row into it.
|
|
218
|
+
*/
|
|
219
|
+
[data-live] > [data-slot="code-block-line"] {
|
|
220
|
+
transition: grid-template-rows var(--transition-duration-spring-settle)
|
|
221
|
+
var(--ease-spring-settle);
|
|
222
|
+
@starting-style {
|
|
223
|
+
grid-template-rows: 0fr;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
[data-live] > [data-slot="code-block-line"] > [data-slot="code-block-line-body"] {
|
|
227
|
+
transition:
|
|
228
|
+
opacity var(--transition-duration-spring-settle) var(--ease-spring-settle),
|
|
229
|
+
translate var(--transition-duration-spring-settle) var(--ease-spring-settle);
|
|
230
|
+
@starting-style {
|
|
231
|
+
opacity: 0;
|
|
232
|
+
translate: 0 calc(-1 * var(--motion-enter-offset));
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
[data-slot="code-block-line"][data-motion="leave"] {
|
|
236
|
+
grid-template-rows: 0fr;
|
|
237
|
+
counter-increment: none;
|
|
238
|
+
user-select: none;
|
|
239
|
+
&::before {
|
|
240
|
+
content: none;
|
|
241
|
+
}
|
|
242
|
+
& > [data-slot="code-block-line-body"] {
|
|
243
|
+
opacity: 0;
|
|
244
|
+
transition-duration: var(--transition-duration-spring-leave);
|
|
245
|
+
transition-timing-function: var(--ease-spring-leave);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/*
|
|
250
|
+
* What a change put into a line that stays (a value, a comma) arrives as new. Two styles, both
|
|
251
|
+
* kept (DESIGN.md): it fades in (`fade`, Einblenden), or also lights up in the accent and lets
|
|
252
|
+
* the light go (`glow`, Aufleuchten).
|
|
253
|
+
*/
|
|
254
|
+
[data-live] [data-slot="code-block-fresh"] {
|
|
255
|
+
transition:
|
|
256
|
+
opacity var(--transition-duration-spring-settle) var(--ease-spring-settle),
|
|
257
|
+
background-color 900ms ease-out;
|
|
258
|
+
@starting-style {
|
|
259
|
+
opacity: 0;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
[data-live][data-change-motion="glow"] [data-slot="code-block-fresh"] {
|
|
263
|
+
border-radius: 2px;
|
|
264
|
+
box-decoration-break: clone;
|
|
265
|
+
@starting-style {
|
|
266
|
+
background-color: color-mix(in oklab, var(--lf-accent) 28%, transparent);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/* Under reduced motion rows do not open or close, and the text does not travel (the offset is
|
|
271
|
+
zero, motion.css): it only fades. */
|
|
272
|
+
@media (prefers-reduced-motion: reduce) {
|
|
273
|
+
[data-live] > [data-slot="code-block-line"] {
|
|
274
|
+
transition: none;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
190
278
|
/* The edges fade where more code waits to the side (scroll-fade.css). The focus outline is
|
|
191
279
|
drawn by the block around it, since the mask would cut it off. */
|
|
192
280
|
[data-slot="code-block-scroll"] {
|