@bridgebrew/ui 0.1.11-next.94 → 0.1.11
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/types/components/composite/diff-view/diff-view.d.ts +54 -0
- package/dist/types/components/composite/diff-view/diff.d.ts +65 -0
- package/dist/types/components/composite/diff-view/index.d.ts +4 -0
- package/dist/types/components/core/code-block/palette.d.ts +3 -0
- package/dist/types/components/layout/multi-pane/multi-pane.d.ts +4 -1
- package/dist/types/index.d.ts +2 -0
- package/package.json +1 -1
- package/src/components/composite/diff-view/diff-view.meta.yaml +65 -0
- package/src/components/composite/diff-view/diff-view.tsx +799 -0
- package/src/components/composite/diff-view/diff.ts +280 -0
- package/src/components/composite/diff-view/index.ts +4 -0
- package/src/components/core/code-block/code-block.tsx +2 -12
- package/src/components/core/code-block/palette.ts +10 -0
- package/src/components/core/resizable/use-remembered-layout.ts +95 -16
- package/src/components/layout/multi-pane/multi-pane.meta.yaml +10 -1
- package/src/components/layout/multi-pane/multi-pane.tsx +51 -5
- package/src/index.ts +18 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
// The model DiffView draws: a unified git diff parsed into files → hunks → lines, each line with its old and new numbers
|
|
2
|
+
// by its kind (a removed line has only an old one, an added line only a new one), the unchanged lines between hunks as
|
|
3
|
+
// gaps (new-side numbers, the old side a constant offset away), and the words that changed inside a removed/added pair.
|
|
4
|
+
// Pure, no React: the parse a plugin may also use (counts, paths) without drawing.
|
|
5
|
+
|
|
6
|
+
/** One line of a hunk. */
|
|
7
|
+
type DiffLine =
|
|
8
|
+
| { kind: "context"; text: string; old: number; new: number }
|
|
9
|
+
| { kind: "removed"; text: string; old: number }
|
|
10
|
+
| { kind: "added"; text: string; new: number };
|
|
11
|
+
|
|
12
|
+
/** One `@@ -oldStart,oldCount +newStart,newCount @@ section` block. */
|
|
13
|
+
interface DiffHunk {
|
|
14
|
+
oldStart: number;
|
|
15
|
+
oldCount: number;
|
|
16
|
+
newStart: number;
|
|
17
|
+
newCount: number;
|
|
18
|
+
/** What git prints after the second `@@`: the enclosing function or heading, often. */
|
|
19
|
+
section: string;
|
|
20
|
+
lines: DiffLine[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type DiffStatus = "added" | "deleted" | "renamed" | "modified";
|
|
24
|
+
|
|
25
|
+
/** One file of a diff. */
|
|
26
|
+
interface DiffFileModel {
|
|
27
|
+
/** Its path in the repository: the new one, the old one for a deletion. */
|
|
28
|
+
path: string;
|
|
29
|
+
/** Its old path, when it was renamed. */
|
|
30
|
+
oldPath?: string;
|
|
31
|
+
status: DiffStatus;
|
|
32
|
+
hunks: DiffHunk[];
|
|
33
|
+
added: number;
|
|
34
|
+
removed: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The unchanged lines between two hunks (or before the first, or after the last: `to` unknown), by their new numbers;
|
|
38
|
+
* a line's old number is its new one plus `offset`. `section`: the next hunk's. */
|
|
39
|
+
interface DiffGap {
|
|
40
|
+
from: number;
|
|
41
|
+
/** The last line of the gap; `undefined`: the end of the file, unknown until read. */
|
|
42
|
+
to: number | undefined;
|
|
43
|
+
offset: number;
|
|
44
|
+
section: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A path as git prints it: a name with a quote, a control or a non-ASCII character comes quoted, C-escaped, its bytes
|
|
48
|
+
* in octal (`"b/caf\303\251.ts"`; under `core.quotePath=false`, a non-ASCII character as is, a whole code point). */
|
|
49
|
+
const unquote = (name: string | undefined) => {
|
|
50
|
+
if (!name?.startsWith('"') || !name.endsWith('"')) return name;
|
|
51
|
+
const escapes: Record<string, number> = { a: 7, b: 8, t: 9, n: 10, v: 11, f: 12, r: 13 };
|
|
52
|
+
const bytes: number[] = [];
|
|
53
|
+
const body = name.slice(1, -1);
|
|
54
|
+
for (let at = 0; at < body.length; at++) {
|
|
55
|
+
const char = String.fromCodePoint(body.codePointAt(at)!);
|
|
56
|
+
if (char.length > 1) at++;
|
|
57
|
+
if (char !== "\\") bytes.push(...new TextEncoder().encode(char));
|
|
58
|
+
else if (/[0-7]{3}/.test(body.slice(at + 1, at + 4))) {
|
|
59
|
+
bytes.push(parseInt(body.slice(at + 1, at + 4), 8));
|
|
60
|
+
at += 3;
|
|
61
|
+
} else {
|
|
62
|
+
const next = body[++at] ?? "";
|
|
63
|
+
bytes.push(escapes[next] ?? next.charCodeAt(0));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return new TextDecoder().decode(new Uint8Array(bytes));
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** The path of a file whose diff has no `---`/`+++` lines (a binary file, a mode change): its rename's target, else its
|
|
70
|
+
* `diff --git` line's `b/` side, quoted, or unquoted and split where both sides name the same file (a space is in it). */
|
|
71
|
+
const gitLinePath = (lines: string[]) => {
|
|
72
|
+
const renamed = lines.find((line) => line.startsWith("rename to "));
|
|
73
|
+
if (renamed) return unquote(renamed.slice("rename to ".length));
|
|
74
|
+
const rest = lines[0]!.slice("diff --git ".length);
|
|
75
|
+
const quoted = /"(?:[^"\\]|\\.)*"$/.exec(rest)?.[0];
|
|
76
|
+
if (quoted) return unquote(quoted)?.replace(/^b\//, "");
|
|
77
|
+
const half = (rest.length - 1) / 2;
|
|
78
|
+
if (
|
|
79
|
+
Number.isInteger(half) &&
|
|
80
|
+
rest.startsWith("a/") &&
|
|
81
|
+
rest.slice(half + 1).startsWith("b/") &&
|
|
82
|
+
rest.slice(2, half) === rest.slice(half + 3)
|
|
83
|
+
)
|
|
84
|
+
return rest.slice(half + 3);
|
|
85
|
+
return /^a\/(.*) b\//.exec(rest)?.[1];
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const HUNK = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@ ?(.*)$/;
|
|
89
|
+
|
|
90
|
+
/** The hunks of a file's lines (from its first `@@`; anything before is skipped, a line of none of the kinds, as
|
|
91
|
+
* `` or a `… [truncated]` mark, too). */
|
|
92
|
+
const hunksOf = (lines: string[]) => {
|
|
93
|
+
const hunks: DiffHunk[] = [];
|
|
94
|
+
let hunk: DiffHunk | undefined;
|
|
95
|
+
let old = 0;
|
|
96
|
+
let neu = 0;
|
|
97
|
+
for (const line of lines) {
|
|
98
|
+
const head = HUNK.exec(line);
|
|
99
|
+
if (head) {
|
|
100
|
+
hunk = {
|
|
101
|
+
oldStart: Number(head[1]),
|
|
102
|
+
oldCount: head[2] === undefined ? 1 : Number(head[2]),
|
|
103
|
+
newStart: Number(head[3]),
|
|
104
|
+
newCount: head[4] === undefined ? 1 : Number(head[4]),
|
|
105
|
+
section: head[5] ?? "",
|
|
106
|
+
lines: [],
|
|
107
|
+
};
|
|
108
|
+
hunks.push(hunk);
|
|
109
|
+
old = hunk.oldStart;
|
|
110
|
+
neu = hunk.newStart;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (!hunk) continue;
|
|
114
|
+
const text = line.slice(1);
|
|
115
|
+
if (line[0] === "+") hunk.lines.push({ kind: "added", text, new: neu++ });
|
|
116
|
+
else if (line[0] === "-") hunk.lines.push({ kind: "removed", text, old: old++ });
|
|
117
|
+
else if (line[0] === " " || line === "")
|
|
118
|
+
hunk.lines.push({ kind: "context", text, old: old++, new: neu++ });
|
|
119
|
+
}
|
|
120
|
+
return hunks;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/** One file's chunk of a diff (from its `diff --git` line, or hunks alone: `path` names it then). */
|
|
124
|
+
const fileOf = (chunk: string, path = ""): DiffFileModel => {
|
|
125
|
+
const lines = chunk.replace(/\n$/, "").split("\n");
|
|
126
|
+
const first = lines.findIndex((line) => line.startsWith("@@"));
|
|
127
|
+
const header = first < 0 ? lines : lines.slice(0, first);
|
|
128
|
+
const side = (prefix: string) => {
|
|
129
|
+
// Git ends a name holding a space with a tab, outside its quotes: dropped before reading it.
|
|
130
|
+
const name = unquote(
|
|
131
|
+
header
|
|
132
|
+
.find((line) => line.startsWith(prefix))
|
|
133
|
+
?.slice(prefix.length)
|
|
134
|
+
.replace(/\t$/, ""),
|
|
135
|
+
);
|
|
136
|
+
return name && name !== "/dev/null" ? name.replace(/^[ab]\//, "") : undefined;
|
|
137
|
+
};
|
|
138
|
+
const hunks = hunksOf(lines);
|
|
139
|
+
const newPath = side("+++ ");
|
|
140
|
+
const oldPath = side("--- ");
|
|
141
|
+
const git = lines[0]?.startsWith("diff --git ") ? gitLinePath(lines) : undefined;
|
|
142
|
+
const renamedFrom = unquote(
|
|
143
|
+
header.find((line) => line.startsWith("rename from "))?.slice("rename from ".length),
|
|
144
|
+
);
|
|
145
|
+
const status: DiffStatus =
|
|
146
|
+
header.some((line) => line.startsWith("new file")) || header.includes("--- /dev/null")
|
|
147
|
+
? "added"
|
|
148
|
+
: header.some((line) => line.startsWith("deleted file")) || header.includes("+++ /dev/null")
|
|
149
|
+
? "deleted"
|
|
150
|
+
: renamedFrom || (oldPath && newPath && oldPath !== newPath)
|
|
151
|
+
? "renamed"
|
|
152
|
+
: "modified";
|
|
153
|
+
const lineKinds = hunks.flatMap((hunk) => hunk.lines);
|
|
154
|
+
return {
|
|
155
|
+
path: newPath ?? oldPath ?? git ?? path,
|
|
156
|
+
...(status === "renamed" && { oldPath: renamedFrom ?? oldPath }),
|
|
157
|
+
status,
|
|
158
|
+
hunks,
|
|
159
|
+
added: lineKinds.filter((line) => line.kind === "added").length,
|
|
160
|
+
removed: lineKinds.filter((line) => line.kind === "removed").length,
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/** A unified git diff, file by file. Text without a `diff --git` line is one file's hunks, named `path`. */
|
|
165
|
+
function parseDiff(diff: string, path?: string): DiffFileModel[] {
|
|
166
|
+
if (!/^diff --git /m.test(diff)) return diff.trim() ? [fileOf(diff, path)] : [];
|
|
167
|
+
return diff
|
|
168
|
+
.split(/^(?=diff --git )/m)
|
|
169
|
+
.filter((chunk) => chunk.startsWith("diff --git "))
|
|
170
|
+
.map((chunk) => fileOf(chunk));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** The lines of context git gives around a change, by default. */
|
|
174
|
+
const CONTEXT = 3;
|
|
175
|
+
|
|
176
|
+
/** The unchanged lines around `file`'s hunks: before the first, between two, and after the last (its end unknown;
|
|
177
|
+
* none when the last hunk reaches the file's end). None for a file added or deleted whole, nor where two hunks touch. */
|
|
178
|
+
function gapsOf(file: Pick<DiffFileModel, "hunks" | "status">): DiffGap[] {
|
|
179
|
+
if (file.status === "added" || file.status === "deleted" || !file.hunks.length) return [];
|
|
180
|
+
const gaps: DiffGap[] = [];
|
|
181
|
+
let next = 1;
|
|
182
|
+
let offset = 0;
|
|
183
|
+
for (const hunk of file.hunks) {
|
|
184
|
+
if (hunk.newStart > next)
|
|
185
|
+
gaps.push({
|
|
186
|
+
from: next,
|
|
187
|
+
to: hunk.newStart - 1,
|
|
188
|
+
offset: hunk.oldStart - hunk.newStart,
|
|
189
|
+
section: hunk.section,
|
|
190
|
+
});
|
|
191
|
+
next = hunk.newStart + hunk.newCount;
|
|
192
|
+
offset = hunk.oldStart + hunk.oldCount - next;
|
|
193
|
+
}
|
|
194
|
+
// Git gives each change 3 lines of context: a last hunk ending with fewer ends the file, no lines after it.
|
|
195
|
+
// ponytail: assumes git's default context (`-U3`); a diff made with less shows no fold at its end.
|
|
196
|
+
const last = file.hunks.at(-1)!.lines;
|
|
197
|
+
const trailing = last.length - 1 - last.findLastIndex((line) => line.kind !== "context");
|
|
198
|
+
if (trailing >= CONTEXT) gaps.push({ from: next, to: undefined, offset, section: "" });
|
|
199
|
+
return gaps;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** A range of characters `[start, end)`. */
|
|
203
|
+
type CharRange = readonly [number, number];
|
|
204
|
+
|
|
205
|
+
/** The words of a line: runs of letters and digits, of spaces, or one other character each. */
|
|
206
|
+
const wordsOf = (text: string) => text.match(/\w+|\s+|[^\w\s]/g) ?? [];
|
|
207
|
+
|
|
208
|
+
/** Past this many words on a side, a pair of lines is not compared word by word (the line's colour says enough). */
|
|
209
|
+
const WORDS_MAX = 400;
|
|
210
|
+
|
|
211
|
+
/** The characters that changed between `before` (a removed line) and `after` (its added counterpart), each side: the
|
|
212
|
+
* words outside their longest common sequence. Nothing when they share less than half their text
|
|
213
|
+
* (two different lines). */
|
|
214
|
+
function changedWords(before: string, after: string): [CharRange[], CharRange[]] {
|
|
215
|
+
const a = wordsOf(before);
|
|
216
|
+
const b = wordsOf(after);
|
|
217
|
+
if (a.length > WORDS_MAX || b.length > WORDS_MAX) return [[], []];
|
|
218
|
+
// The longest common subsequence's table, from the ends.
|
|
219
|
+
const table = Array.from({ length: a.length + 1 }, () => new Uint16Array(b.length + 1));
|
|
220
|
+
for (let i = a.length - 1; i >= 0; i--)
|
|
221
|
+
for (let j = b.length - 1; j >= 0; j--)
|
|
222
|
+
table[i]![j] =
|
|
223
|
+
a[i] === b[j] ? table[i + 1]![j + 1]! + 1 : Math.max(table[i + 1]![j]!, table[i]![j + 1]!);
|
|
224
|
+
const keptA = new Set<number>();
|
|
225
|
+
const keptB = new Set<number>();
|
|
226
|
+
let shared = 0;
|
|
227
|
+
for (let i = 0, j = 0; i < a.length && j < b.length;) {
|
|
228
|
+
if (a[i] === b[j]) {
|
|
229
|
+
shared += a[i]!.trim().length;
|
|
230
|
+
keptA.add(i++);
|
|
231
|
+
keptB.add(j++);
|
|
232
|
+
} else if (table[i + 1]![j]! >= table[i]![j + 1]!) i++;
|
|
233
|
+
else j++;
|
|
234
|
+
}
|
|
235
|
+
// Lines that share less than half their text are two different lines: their colour says it, no word is marked.
|
|
236
|
+
const longest = Math.max(before.replace(/\s/g, "").length, after.replace(/\s/g, "").length);
|
|
237
|
+
if (shared * 2 < longest) return [[], []];
|
|
238
|
+
const ranges = (words: string[], kept: Set<number>) => {
|
|
239
|
+
const out: [number, number][] = [];
|
|
240
|
+
let at = 0;
|
|
241
|
+
words.forEach((word, index) => {
|
|
242
|
+
const end = at + word.length;
|
|
243
|
+
if (!kept.has(index)) {
|
|
244
|
+
const last = out.at(-1);
|
|
245
|
+
if (last && last[1] === at) last[1] = end;
|
|
246
|
+
else out.push([at, end]);
|
|
247
|
+
}
|
|
248
|
+
at = end;
|
|
249
|
+
});
|
|
250
|
+
return out;
|
|
251
|
+
};
|
|
252
|
+
return [ranges(a, keptA), ranges(b, keptB)];
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** A hunk's removed lines paired with the added lines right after them (the first removed with the first added…):
|
|
256
|
+
* the pairs whose words are compared, and the rows of a split view. */
|
|
257
|
+
function pairsOf(lines: readonly DiffLine[]) {
|
|
258
|
+
const rows: { left?: DiffLine; right?: DiffLine }[] = [];
|
|
259
|
+
for (let at = 0; at < lines.length;) {
|
|
260
|
+
const line = lines[at]!;
|
|
261
|
+
if (line.kind === "context") {
|
|
262
|
+
rows.push({ left: line, right: line });
|
|
263
|
+
at++;
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
const removed: DiffLine[] = [];
|
|
267
|
+
const added: DiffLine[] = [];
|
|
268
|
+
while (lines[at]?.kind === "removed") removed.push(lines[at++]!);
|
|
269
|
+
while (lines[at]?.kind === "added") added.push(lines[at++]!);
|
|
270
|
+
for (let i = 0; i < Math.max(removed.length, added.length); i++)
|
|
271
|
+
rows.push({
|
|
272
|
+
...(removed[i] && { left: removed[i] }),
|
|
273
|
+
...(added[i] && { right: added[i] }),
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
return rows;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export { changedWords, gapsOf, pairsOf, parseDiff };
|
|
280
|
+
export type { CharRange, DiffFileModel, DiffGap, DiffHunk, DiffLine, DiffStatus };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { DiffView } from "./diff-view";
|
|
2
|
+
export type { DiffViewFile, DiffViewHandle, DiffViewProps } from "./diff-view";
|
|
3
|
+
export { changedWords, gapsOf, pairsOf, parseDiff } from "./diff";
|
|
4
|
+
export type { CharRange, DiffFileModel, DiffGap, DiffHunk, DiffLine, DiffStatus } from "./diff";
|
|
@@ -5,6 +5,7 @@ import { cn } from "../../../lib/utils";
|
|
|
5
5
|
import { Button } from "../button";
|
|
6
6
|
import { Icon } from "../icon";
|
|
7
7
|
import { ScrollArea } from "../scroll-area";
|
|
8
|
+
import { codeTokenPalette } from "./palette";
|
|
8
9
|
|
|
9
10
|
// Code to read and copy (research-070): the kit's mono font on the hover tint, its long lines scrolling sideways (the
|
|
10
11
|
// kit's ScrollArea), a Copy button in its corner. Coloured when given `tokens`: the caller highlights (Shiki in its own
|
|
@@ -27,17 +28,6 @@ interface CodeToken {
|
|
|
27
28
|
color?: string | undefined;
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
/** The `--shiki-*` variables a css-variables token names, from the theme's own colours (light, dark and skins follow):
|
|
31
|
-
* tags blue, attributes amber, keywords red, strings green, punctuation grey. */
|
|
32
|
-
const palette = [
|
|
33
|
-
"[--shiki-foreground:var(--fg)] [--shiki-color-text:var(--fg)] [--shiki-token-parameter:var(--fg)]",
|
|
34
|
-
"[--shiki-token-constant:var(--accent)] [--shiki-token-link:var(--accent)]",
|
|
35
|
-
"[--shiki-token-function:var(--warning-text)]",
|
|
36
|
-
"[--shiki-token-keyword:var(--label-bug-fg)]",
|
|
37
|
-
"[--shiki-token-string:var(--label-improvement-fg)] [--shiki-token-string-expression:var(--label-improvement-fg)]",
|
|
38
|
-
"[--shiki-token-punctuation:var(--fg-secondary)] [--shiki-token-comment:var(--fg-muted)]",
|
|
39
|
-
].join(" ");
|
|
40
|
-
|
|
41
31
|
function CodeBlock({ code, language, tokens, className, ...props }: CodeBlockProps) {
|
|
42
32
|
const [copied, setCopied] = React.useState(false);
|
|
43
33
|
const timer = React.useRef<ReturnType<typeof setTimeout>>(undefined);
|
|
@@ -55,7 +45,7 @@ function CodeBlock({ code, language, tokens, className, ...props }: CodeBlockPro
|
|
|
55
45
|
<div
|
|
56
46
|
data-slot="code-block"
|
|
57
47
|
{...(language !== undefined && { "data-language": language })}
|
|
58
|
-
className={cn("group relative rounded-dropdown bg-hover",
|
|
48
|
+
className={cn("group relative rounded-dropdown bg-hover", codeTokenPalette, className)}
|
|
59
49
|
{...props}
|
|
60
50
|
>
|
|
61
51
|
<ScrollArea orientation="horizontal">
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** The `--shiki-*` variables a css-variables token names, from the theme's own colours (light, dark and skins follow):
|
|
2
|
+
* tags blue, attributes amber, keywords red, strings green, punctuation grey. */
|
|
3
|
+
export const codeTokenPalette = [
|
|
4
|
+
"[--shiki-foreground:var(--fg)] [--shiki-color-text:var(--fg)] [--shiki-token-parameter:var(--fg)]",
|
|
5
|
+
"[--shiki-token-constant:var(--accent)] [--shiki-token-link:var(--accent)]",
|
|
6
|
+
"[--shiki-token-function:var(--warning-text)]",
|
|
7
|
+
"[--shiki-token-keyword:var(--label-bug-fg)]",
|
|
8
|
+
"[--shiki-token-string:var(--label-improvement-fg)] [--shiki-token-string-expression:var(--label-improvement-fg)]",
|
|
9
|
+
"[--shiki-token-punctuation:var(--fg-secondary)] [--shiki-token-comment:var(--fg-muted)]",
|
|
10
|
+
].join(" ");
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
// research-049: the sizes a user dragged kept per workspace in their profile, for MultiPane and SplitView. Only a
|
|
2
2
|
// gesture on the edge is kept (a layout change at mount, from a narrow window or a programmatic one is not); the kept
|
|
3
3
|
// sizes come back when the box grows again; another tab's write (or reset) is applied to the open group; a
|
|
4
|
-
// double-click forgets the workspace's sizes and brings the starting ones back.
|
|
4
|
+
// double-click forgets the workspace's sizes and brings the starting ones back. One layout per key covers every pane the
|
|
5
|
+
// group may show (MultiPane's aside while it is hidden too): a pane shown or hidden keeps the others at their widths, the
|
|
6
|
+
// unsized pane (`main`) giving or taking the room.
|
|
5
7
|
import * as React from "react";
|
|
6
8
|
import { isRecord } from "../../../lib/json.ts";
|
|
7
9
|
|
|
@@ -42,6 +44,25 @@ export function startingLayout(panes: PaneStart[], length: number): PaneLayout {
|
|
|
42
44
|
: layout;
|
|
43
45
|
}
|
|
44
46
|
|
|
47
|
+
/** `layout`'s sizes for `panes` (it may name more, or fewer): a sized pane keeps its size from `layout`, else its
|
|
48
|
+
* starting one; the unsized panes share the rest. `undefined` when a px start needs a length not known yet. */
|
|
49
|
+
function layoutFor(layout: PaneLayout, panes: PaneStart[], length: number): PaneLayout | undefined {
|
|
50
|
+
if (panes.length === 0) return layout;
|
|
51
|
+
const unsized = panes.some(({ size }) => size === undefined);
|
|
52
|
+
// No pane takes the rest (SplitView): the kept sizes as they are, when they are this group's.
|
|
53
|
+
if (
|
|
54
|
+
!unsized &&
|
|
55
|
+
panes.length === Object.keys(layout).length &&
|
|
56
|
+
panes.every(({ id }) => id in layout)
|
|
57
|
+
)
|
|
58
|
+
return layout;
|
|
59
|
+
const sizes = panes.map(({ id, size }) =>
|
|
60
|
+
size !== undefined && layout[id] !== undefined ? { id, size: `${layout[id]}%` } : { id, size },
|
|
61
|
+
);
|
|
62
|
+
if (length <= 0 && sizes.some(({ size }) => size?.trim().endsWith("px"))) return undefined;
|
|
63
|
+
return startingLayout(sizes, length);
|
|
64
|
+
}
|
|
65
|
+
|
|
45
66
|
export interface RememberedLayout {
|
|
46
67
|
/** The sizes kept for this workspace: the group's starting layout; `undefined` without a key or a workspace. */
|
|
47
68
|
defaultLayout: PaneLayout | undefined;
|
|
@@ -86,17 +107,44 @@ export function useRememberedLayout(
|
|
|
86
107
|
const startsRef = React.useRef(starts);
|
|
87
108
|
startsRef.current = starts;
|
|
88
109
|
|
|
89
|
-
const
|
|
110
|
+
const length = React.useCallback(() => {
|
|
111
|
+
const box = elementRef.current?.getBoundingClientRect();
|
|
112
|
+
return box ? (orientation === "horizontal" ? box.width : box.height) : 0;
|
|
113
|
+
}, [orientation]);
|
|
114
|
+
|
|
115
|
+
/** The sizes kept for every pane, shown or not. */
|
|
116
|
+
const stored = React.useCallback((): PaneLayout | undefined => {
|
|
90
117
|
if (storageKey === undefined || workspace === undefined) return undefined;
|
|
91
118
|
const value = profile.get("pane-sizes", storageKey, { workspace });
|
|
92
|
-
|
|
93
|
-
// Sizes kept for other panes (MultiPane's aside left out on a narrower box, a pane renamed) are not this group's.
|
|
94
|
-
const ids = startsRef.current().map(({ id }) => id);
|
|
95
|
-
const fits =
|
|
96
|
-
ids.length === 0 ||
|
|
97
|
-
(ids.length === Object.keys(value).length && ids.every((id) => id in value));
|
|
98
|
-
return fits ? value : undefined;
|
|
119
|
+
return isPaneLayout(value) ? value : undefined;
|
|
99
120
|
}, [storageKey, workspace]);
|
|
121
|
+
/** Those sizes for the panes shown now. */
|
|
122
|
+
const kept = React.useCallback((): PaneLayout | undefined => {
|
|
123
|
+
const value = stored();
|
|
124
|
+
return value && layoutFor(value, startsRef.current(), length());
|
|
125
|
+
}, [stored, length]);
|
|
126
|
+
|
|
127
|
+
/** The px the panels share when `count` of them show: the group's length less its edges (one fewer than them). */
|
|
128
|
+
const space = React.useCallback(
|
|
129
|
+
(count: number) => {
|
|
130
|
+
const edge = elementRef.current?.querySelector(':scope > [data-slot="resizable-handle"]');
|
|
131
|
+
const size = edge?.getBoundingClientRect();
|
|
132
|
+
const each = size ? (orientation === "horizontal" ? size.width : size.height) : 0;
|
|
133
|
+
return length() - each * Math.max(0, count - 1);
|
|
134
|
+
},
|
|
135
|
+
[length, orientation],
|
|
136
|
+
);
|
|
137
|
+
/** Every pane's last size shown in px (a hidden one's too), for when the panes shown change: a percentage is of the
|
|
138
|
+
* room the panels share, which an edge more or less changes. */
|
|
139
|
+
const last = React.useRef<PaneLayout>({});
|
|
140
|
+
const remember = React.useCallback(
|
|
141
|
+
(layout: PaneLayout) => {
|
|
142
|
+
const room = space(Object.keys(layout).length);
|
|
143
|
+
if (room <= 0) return;
|
|
144
|
+
for (const [id, size] of Object.entries(layout)) last.current[id] = (size / 100) * room;
|
|
145
|
+
},
|
|
146
|
+
[space],
|
|
147
|
+
);
|
|
100
148
|
|
|
101
149
|
// What the group starts from when it mounts (the library reads it then only; a group mounted again, MultiPane back
|
|
102
150
|
// from `mode="main"`, starts from what is kept now). Later changes go through `setLayout`.
|
|
@@ -105,11 +153,9 @@ export function useRememberedLayout(
|
|
|
105
153
|
const defaultLayout = React.useMemo(() => keptNow, [keptText]);
|
|
106
154
|
|
|
107
155
|
const restore = React.useCallback(() => {
|
|
108
|
-
const
|
|
109
|
-
const length = box ? (orientation === "horizontal" ? box.width : box.height) : 0;
|
|
110
|
-
const layout = startingLayout(startsRef.current(), length);
|
|
156
|
+
const layout = startingLayout(startsRef.current(), length());
|
|
111
157
|
if (Object.keys(layout).length > 0) groupRef.current?.setLayout(layout);
|
|
112
|
-
}, [groupRef,
|
|
158
|
+
}, [groupRef, length]);
|
|
113
159
|
|
|
114
160
|
/** What is shown follows what is kept (another workspace, another tab's write or reset). */
|
|
115
161
|
const shown = React.useRef<string | undefined>(JSON.stringify(defaultLayout));
|
|
@@ -123,6 +169,7 @@ export function useRememberedLayout(
|
|
|
123
169
|
const next = JSON.stringify(layout);
|
|
124
170
|
if (next === shown.current) return;
|
|
125
171
|
shown.current = next;
|
|
172
|
+
last.current = {};
|
|
126
173
|
if (layout) groupRef.current?.setLayout(layout);
|
|
127
174
|
else restore();
|
|
128
175
|
}, [storageKey, workspace, kept, groupRef, restore]);
|
|
@@ -206,24 +253,56 @@ export function useRememberedLayout(
|
|
|
206
253
|
if (held) dragged.current = true;
|
|
207
254
|
}, []);
|
|
208
255
|
|
|
256
|
+
// The panes shown changed (MultiPane's aside shown or hidden): each other pane keeps its width, the shown pane comes
|
|
257
|
+
// back at its last size; the library would take the layout it last had for these panes. Computed before the library
|
|
258
|
+
// lays the new panes out, applied in place of the layout it then reports (a setLayout before would name panes it does
|
|
259
|
+
// not hold yet).
|
|
260
|
+
const ids = starts()
|
|
261
|
+
.map(({ id }) => id)
|
|
262
|
+
.join(",");
|
|
263
|
+
const lastIds = React.useRef(ids);
|
|
264
|
+
const pending = React.useRef<PaneLayout | undefined>(undefined);
|
|
265
|
+
if (lastIds.current !== ids) {
|
|
266
|
+
if (lastIds.current !== "") {
|
|
267
|
+
const room = space(starts().length);
|
|
268
|
+
const base: PaneLayout = { ...stored() };
|
|
269
|
+
if (room > 0)
|
|
270
|
+
for (const [id, px] of Object.entries(last.current)) base[id] = (px / room) * 100;
|
|
271
|
+
pending.current = layoutFor(base, starts(), length());
|
|
272
|
+
}
|
|
273
|
+
lastIds.current = ids;
|
|
274
|
+
}
|
|
275
|
+
|
|
209
276
|
const onLayoutChanged = React.useCallback(
|
|
210
277
|
(layout: PaneLayout) => {
|
|
278
|
+
const next = pending.current;
|
|
279
|
+
if (next && Object.keys(next).sort().join() === Object.keys(layout).sort().join()) {
|
|
280
|
+
pending.current = undefined;
|
|
281
|
+
remember(next);
|
|
282
|
+
shown.current = JSON.stringify(kept());
|
|
283
|
+
groupRef.current?.setLayout(next);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
remember(layout);
|
|
211
287
|
if (storageKey === undefined || workspace === undefined) return;
|
|
212
288
|
if (!dragged.current && !key.current) return;
|
|
213
289
|
if (canKeep && !canKeep()) return;
|
|
214
290
|
// One drag, one layout: what follows its release (a programmatic setLayout) is not this drag.
|
|
215
291
|
dragged.current = false;
|
|
216
292
|
pointer.current.moved = false;
|
|
217
|
-
|
|
218
|
-
|
|
293
|
+
// A hidden pane's kept size stays (the aside's while the list is dragged).
|
|
294
|
+
const value = { ...stored(), ...layout };
|
|
295
|
+
shown.current = JSON.stringify(layoutFor(value, startsRef.current(), length()));
|
|
296
|
+
profile.set("pane-sizes", storageKey, value, { scope: "workspace", workspace });
|
|
219
297
|
},
|
|
220
|
-
[storageKey, workspace, canKeep],
|
|
298
|
+
[storageKey, workspace, canKeep, stored, length, kept, groupRef, remember],
|
|
221
299
|
);
|
|
222
300
|
|
|
223
301
|
const forget = React.useCallback(() => {
|
|
224
302
|
dragged.current = false;
|
|
225
303
|
key.current = false;
|
|
226
304
|
missed.current = false;
|
|
305
|
+
last.current = {};
|
|
227
306
|
if (storageKey !== undefined && workspace !== undefined) {
|
|
228
307
|
shown.current = JSON.stringify(undefined);
|
|
229
308
|
profile.reset("pane-sizes", storageKey, { scope: "workspace", workspace });
|
|
@@ -6,6 +6,7 @@ when_to_use:
|
|
|
6
6
|
- "A list beside its record (Contacts, Health): nav and main"
|
|
7
7
|
- "A table beside the row it opened (Orders): main and aside"
|
|
8
8
|
- "Three columns: folders | list | message, pages | document | properties"
|
|
9
|
+
- "A details panel that reads as a card beside the work (a session's branch, changes, pull request): asideVariant='floating'"
|
|
9
10
|
when_not_to_use:
|
|
10
11
|
- "A single column (a page of sections, settings): Pane alone"
|
|
11
12
|
- "Any other split of a view into rows or columns (an editor above its console, a grid of panes) — use SplitView"
|
|
@@ -51,6 +52,11 @@ props:
|
|
|
51
52
|
required: false
|
|
52
53
|
default: "true"
|
|
53
54
|
description: "When room runs out, the panes leave from the right; off for a screen that cannot open them elsewhere yet"
|
|
55
|
+
- name: asideVariant
|
|
56
|
+
type: "'pane' | 'floating'"
|
|
57
|
+
required: false
|
|
58
|
+
default: "'pane'"
|
|
59
|
+
description: "pane: the aside a full-height column after a 1px divider; floating: the aside a card (the elevated Card with its border and shadow, on the page composer's fill: the active background in a light theme, halfway to the border's in a dark one) inset 12px from the pane's edges, none toward main (on every side in mode='aside'), as tall as its content and scrolling inside it (whatever scroll says); no divider drawn, the edge still dragged"
|
|
54
60
|
composition_partners:
|
|
55
61
|
- Pane (each pane's content)
|
|
56
62
|
- Detail, EmptyState, DetailSkeleton (a record pane)
|
|
@@ -61,7 +67,10 @@ examples:
|
|
|
61
67
|
- title: "A table beside the row it opened"
|
|
62
68
|
code: |
|
|
63
69
|
<MultiPane main={<Pane aria-label="Orders"><Table>…</Table></Pane>} aside={open && <Detail>…</Detail>} />
|
|
70
|
+
- title: "A floating details panel"
|
|
71
|
+
code: |
|
|
72
|
+
<MultiPane nav={sessions} main={<Pane aria-label={session.title}>…</Pane>} aside={panelOpen && <Pane aria-label="Details">…</Pane>} asideVariant="floating" />
|
|
64
73
|
anti_patterns:
|
|
65
74
|
- "Don't define a component in a pane's prop (main={() => <Detail />}, or a component declared inside the screen's render): each render makes a new component, React mounts it again and its state goes (a draft, the scroll, the focus). Pass an element: main={<Detail />}"
|
|
66
75
|
- "Don't write a large block of JSX inside a pane's prop: put it in a variable or a small component of its own (nav={list} main={record})"
|
|
67
|
-
notes: "Sub-parts by data-slot: panes, multi-pane-alone (a pane alone, mode other than split and the first pane). Panel ids nav, main, aside (the keys of the kept sizes). Narrow under multiPaneNarrowWidth (640px) of its own box, a container query; three panes leave their aside out under multiPaneAsideWidth (800px), measured. ListDetail is its former name (list is nav, detail is main), kept for plugins built against it."
|
|
76
|
+
notes: "Sub-parts by data-slot: panes, multi-pane-alone (a pane alone, mode other than split and the first pane), multi-pane-floating and multi-pane-floating-card (the floating aside's inset and its card); data-aside-variant on the root. Panel ids nav, main, aside (the keys of the kept sizes). Narrow under multiPaneNarrowWidth (640px) of its own box, a container query; three panes leave their aside out under multiPaneAsideWidth (800px), measured. ListDetail is its former name (list is nav, detail is main), kept for plugins built against it."
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
|
|
3
3
|
import { cn } from "../../../lib/utils";
|
|
4
|
+
import { cardVariants } from "../../core/card";
|
|
4
5
|
import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from "../../core/resizable";
|
|
5
6
|
import { ScrollArea } from "../../core/scroll-area";
|
|
6
7
|
import { useRememberedLayout } from "../../core/resizable/use-remembered-layout";
|
|
@@ -10,7 +11,8 @@ import { useRememberedLayout } from "../../core/resizable/use-remembered-layout"
|
|
|
10
11
|
// on an edge back to the starting widths. When room runs out the panes leave from the right: under 800px of its own box
|
|
11
12
|
// the `aside` goes (three panes), under 640px the first pane stays alone (a container query: a window's width, not the
|
|
12
13
|
// page's); the screen opens what left elsewhere (a window; on a phone, pushed with the shell's Back). `mode`: one pane
|
|
13
|
-
// alone, a window showing only it.
|
|
14
|
+
// alone, a window showing only it. `asideVariant="floating"`: the aside a card inset from the pane's edges, as tall as its
|
|
15
|
+
// content and scrolling inside it, no line drawn between it and `main` (its edge still dragged).
|
|
14
16
|
|
|
15
17
|
type PaneName = "nav" | "main" | "aside";
|
|
16
18
|
|
|
@@ -40,6 +42,9 @@ interface MultiPaneProps extends Omit<React.ComponentProps<"div">, "children"> {
|
|
|
40
42
|
/** The panes that scroll their content (the kit's ScrollArea, as a frame's `scrolling`); the others (default) leave
|
|
41
43
|
* it to their content (a Pane's body, a Detail). */
|
|
42
44
|
scroll?: PaneName[];
|
|
45
|
+
/** `pane`: the aside a full-height column after a divider (default); `floating`: a card inset from the edges, as tall
|
|
46
|
+
* as its content, scrolling inside it, no divider drawn (the edge still dragged). */
|
|
47
|
+
asideVariant?: "pane" | "floating";
|
|
43
48
|
/** Its group's props (a customization owner's data attribute). */
|
|
44
49
|
groupProps?: Omit<React.ComponentProps<typeof ResizablePanelGroup>, "orientation" | "children">;
|
|
45
50
|
}
|
|
@@ -53,6 +58,31 @@ const firstAlone =
|
|
|
53
58
|
const firstAloneNarrow =
|
|
54
59
|
"@max-[639.98px]:[&>[data-slot=resizable-handle]]:hidden! @max-[639.98px]:[&>[data-slot=resizable-panel]:not(:first-of-type)]:hidden! @max-[639.98px]:[&>[data-slot=resizable-panel]]:min-w-0! @max-[639.98px]:[&>[data-slot=resizable-panel]]:max-w-none! @max-[639.98px]:[&>[data-slot=resizable-panel]]:flex-[1_1_0]!";
|
|
55
60
|
|
|
61
|
+
/** The floating aside: a card (the kit's elevated Card, with its border, on the page composer's fill) inset 12px from the
|
|
62
|
+
* edges, none toward `main` (`alone`: on every side), as tall as its content and scrolling inside it. */
|
|
63
|
+
function FloatingAside({ alone, children }: { alone: boolean; children: React.ReactNode }) {
|
|
64
|
+
return (
|
|
65
|
+
<div
|
|
66
|
+
data-slot="multi-pane-floating"
|
|
67
|
+
className={cn("flex min-h-0 flex-col py-3 pr-3", alone && "pl-3")}
|
|
68
|
+
>
|
|
69
|
+
<ScrollArea
|
|
70
|
+
data-slot="multi-pane-floating-card"
|
|
71
|
+
className={cn(
|
|
72
|
+
cardVariants({ variant: "elevated" }),
|
|
73
|
+
"flex max-h-full flex-col border border-border p-0",
|
|
74
|
+
// Set apart from the page as the page composer is: the active fill in a light theme, halfway to the border's
|
|
75
|
+
// in a dark one.
|
|
76
|
+
"bg-[light-dark(var(--bg-active),color-mix(in_oklab,var(--bg-active),var(--border)))]/90",
|
|
77
|
+
)}
|
|
78
|
+
viewportClassName="h-auto min-h-0 flex-1"
|
|
79
|
+
>
|
|
80
|
+
{children}
|
|
81
|
+
</ScrollArea>
|
|
82
|
+
</div>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
56
86
|
/** Its box's width, `undefined` until measured; 0 when not laid out (hidden, jsdom). */
|
|
57
87
|
function useWidth(ref: React.RefObject<HTMLDivElement | null>, watch: boolean) {
|
|
58
88
|
const [width, setWidth] = React.useState<number>();
|
|
@@ -78,12 +108,21 @@ function MultiPane({
|
|
|
78
108
|
storageKey,
|
|
79
109
|
groupProps,
|
|
80
110
|
scroll = [],
|
|
111
|
+
asideVariant = "pane",
|
|
81
112
|
className,
|
|
82
113
|
...props
|
|
83
114
|
}: MultiPaneProps) {
|
|
84
115
|
const root = React.useRef<HTMLDivElement | null>(null);
|
|
85
|
-
const
|
|
86
|
-
|
|
116
|
+
const floating = asideVariant === "floating";
|
|
117
|
+
// A floating aside scrolls inside its card, whatever `scroll` says.
|
|
118
|
+
const frame = (id: PaneName, node: React.ReactNode, alone = false) =>
|
|
119
|
+
id === "aside" && floating ? (
|
|
120
|
+
<FloatingAside alone={alone}>{node}</FloatingAside>
|
|
121
|
+
) : scroll.includes(id) ? (
|
|
122
|
+
<ScrollArea className="h-full min-h-0">{node}</ScrollArea>
|
|
123
|
+
) : (
|
|
124
|
+
node
|
|
125
|
+
);
|
|
87
126
|
const three = present(nav) && present(aside);
|
|
88
127
|
const width = useWidth(root, collapse && three && mode === "split");
|
|
89
128
|
// The aside is unmounted rather than hidden: the library sizes the panes it holds, a hidden one would skew a drag.
|
|
@@ -106,12 +145,13 @@ function MultiPane({
|
|
|
106
145
|
ref={root}
|
|
107
146
|
data-slot="multi-pane"
|
|
108
147
|
data-mode={mode}
|
|
148
|
+
data-aside-variant={asideVariant}
|
|
109
149
|
className={cn("@container relative flex min-h-0 min-w-0 flex-1 flex-col", className)}
|
|
110
150
|
{...props}
|
|
111
151
|
>
|
|
112
152
|
{alone ? (
|
|
113
153
|
<div data-slot="multi-pane-alone" className="min-h-0 flex-1 [&>*]:h-full [&>*]:min-h-0">
|
|
114
|
-
{frame(mode, mode === "nav" ? nav : mode === "aside" ? aside : main)}
|
|
154
|
+
{frame(mode, mode === "nav" ? nav : mode === "aside" ? aside : main, true)}
|
|
115
155
|
</div>
|
|
116
156
|
) : (
|
|
117
157
|
<ResizablePanelGroup
|
|
@@ -137,7 +177,13 @@ function MultiPane({
|
|
|
137
177
|
>
|
|
138
178
|
{shown.map((pane, i) => (
|
|
139
179
|
<React.Fragment key={pane.id}>
|
|
140
|
-
{i > 0 &&
|
|
180
|
+
{i > 0 && (
|
|
181
|
+
// The floating aside's edge: dragged as any other, its 1px line not drawn.
|
|
182
|
+
<ResizableHandle
|
|
183
|
+
onDoubleClick={forget}
|
|
184
|
+
className={pane.id === "aside" && floating ? "bg-transparent" : undefined}
|
|
185
|
+
/>
|
|
186
|
+
)}
|
|
141
187
|
<ResizablePanel
|
|
142
188
|
id={pane.id}
|
|
143
189
|
{...(pane.size !== undefined && { defaultSize: pane.size })}
|