svelte-highlight 7.23.2 → 7.24.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 (94) hide show
  1. package/AnsiOutput.svelte +33 -6
  2. package/AnsiOutput.svelte.d.ts +8 -0
  3. package/CodeToolbar.svelte +144 -0
  4. package/CodeToolbar.svelte.d.ts +125 -0
  5. package/CodeWindow.svelte +19 -0
  6. package/CodeWindow.svelte.d.ts +10 -0
  7. package/CopyButton.svelte +107 -3
  8. package/CopyButton.svelte.d.ts +17 -1
  9. package/DiffFileList.svelte +523 -0
  10. package/DiffFileList.svelte.d.ts +94 -0
  11. package/DiffFileListItem.svelte +44 -0
  12. package/DiffMinimap.svelte +99 -0
  13. package/DiffMinimap.svelte.d.ts +17 -0
  14. package/DiffStats.svelte +43 -0
  15. package/DiffStats.svelte.d.ts +17 -0
  16. package/DiffView.svelte +1213 -0
  17. package/DiffView.svelte.d.ts +76 -0
  18. package/FileTabs.svelte +2 -5
  19. package/Highlight.svelte +158 -2
  20. package/Highlight.svelte.d.ts +50 -2
  21. package/HighlightAuto.svelte.d.ts +3 -2
  22. package/HighlightAutoLazy.svelte +112 -0
  23. package/HighlightAutoLazy.svelte.d.ts +66 -0
  24. package/HighlightDiff.svelte +162 -0
  25. package/HighlightDiff.svelte.d.ts +102 -0
  26. package/HighlightEditable.svelte +65 -756
  27. package/HighlightEditable.svelte.d.ts +15 -5
  28. package/HighlightStream.svelte +117 -218
  29. package/HighlightStream.svelte.d.ts +8 -0
  30. package/HighlightSvelte.svelte.d.ts +3 -2
  31. package/HighlightVirtual.svelte +41 -8
  32. package/HighlightVirtual.svelte.d.ts +8 -0
  33. package/LangTag.svelte +33 -4
  34. package/LangTag.svelte.d.ts +6 -2
  35. package/LineNumbers.svelte +25 -4
  36. package/LineNumbers.svelte.d.ts +8 -0
  37. package/MarkdownStream.svelte +1 -0
  38. package/README.md +735 -18
  39. package/ToolbarButton.svelte +90 -0
  40. package/ToolbarButton.svelte.d.ts +106 -0
  41. package/Typewriter.svelte +59 -10
  42. package/Typewriter.svelte.d.ts +7 -0
  43. package/WrapToggle.svelte +34 -0
  44. package/WrapToggle.svelte.d.ts +39 -0
  45. package/action.js +7 -3
  46. package/ansi.d.ts +1 -3
  47. package/auto-detect.d.ts +52 -0
  48. package/auto-detect.js +208 -0
  49. package/code-region.d.ts +7 -0
  50. package/code-region.js +20 -0
  51. package/css-highlights.js +115 -0
  52. package/css-walk.js +50 -0
  53. package/diff-controller.d.ts +120 -0
  54. package/diff-controller.js +721 -0
  55. package/diff-edits.d.ts +101 -0
  56. package/diff-edits.js +890 -0
  57. package/diff-html.d.ts +8 -0
  58. package/diff-html.js +60 -0
  59. package/diff-languages.d.ts +4 -0
  60. package/diff-languages.js +73 -0
  61. package/diff.d.ts +161 -0
  62. package/diff.js +1111 -0
  63. package/editable-commands.js +81 -0
  64. package/editable-css-paint.js +5 -3
  65. package/editable-dom-paint.js +1 -1
  66. package/editable-history.js +199 -0
  67. package/editable-line-dom.js +176 -1
  68. package/editable-painter.js +356 -0
  69. package/editable.d.ts +118 -0
  70. package/editable.js +513 -0
  71. package/engine.d.ts +1 -1
  72. package/fence.js +1 -1
  73. package/highlight-cascade.js +679 -0
  74. package/highlight-theme.js +6 -47
  75. package/incremental-tokenize.js +1 -1
  76. package/index.d.ts +39 -6
  77. package/index.js +30 -0
  78. package/languages/detect-index.d.ts +16 -0
  79. package/languages/detect-index.js +6 -0
  80. package/languages/loaders.d.ts +8 -0
  81. package/languages/loaders.js +370 -0
  82. package/package.json +50 -2
  83. package/preprocessor.js +20 -5
  84. package/search.js +2 -2
  85. package/stream-highlighter.js +198 -0
  86. package/stream.css +16 -0
  87. package/stream.d.ts +60 -0
  88. package/stream.js +283 -0
  89. package/style-registry.js +34 -3
  90. package/themes/_base-css.d.ts +1 -0
  91. package/themes/_base-css.js +1 -0
  92. package/typewriter-units.d.ts +3 -0
  93. package/typewriter-units.js +43 -0
  94. package/virtual-window.js +9 -4
@@ -0,0 +1,523 @@
1
+ <script>
2
+ /**
3
+ * @typedef {{
4
+ * key?: string,
5
+ * path: string,
6
+ * oldPath?: string,
7
+ * status?: "modified" | "added" | "deleted" | "renamed" | "binary",
8
+ * patch?: import("./diff-edits.js").FilePatch,
9
+ * before?: string,
10
+ * after?: string,
11
+ * language?: import("./languages").LanguageType<string>,
12
+ * }} DiffFile
13
+ */
14
+
15
+ /**
16
+ * Files to show, each a parsed patch (`patch`) or two texts
17
+ * (`before`/`after`).
18
+ * @type {DiffFile[]}
19
+ */
20
+ export let files = [];
21
+
22
+ /** @type {"unified" | "split"} */
23
+ export let view = "unified";
24
+
25
+ /** Unchanged lines kept around each change. */
26
+ export let context = 3;
27
+
28
+ /** Highlight changed words inside paired lines. */
29
+ export let wordDiff = true;
30
+
31
+ /** Treat lines that differ only in whitespace as unchanged. */
32
+ export let ignoreWhitespace = false;
33
+
34
+ /** Wrap long lines instead of scrolling sideways. */
35
+ export let wrap = false;
36
+
37
+ /** Show accept/reject controls per change. */
38
+ export let review = false;
39
+
40
+ /** @type {"default" | "colorblind"} */
41
+ export let palette = "default";
42
+
43
+ /**
44
+ * Tallest a file's diff grows before it scrolls on its own, in pixels.
45
+ * `null` lets every file show in full.
46
+ * @type {number | null}
47
+ */
48
+ export let maxHeight = 640;
49
+
50
+ /**
51
+ * Picks a language for a path. The default loads one by file extension
52
+ * the first time a file mounts.
53
+ * @type {(path: string) => import("./languages").LanguageType<string> | Promise<import("./languages").LanguageType<string> | undefined> | undefined}
54
+ */
55
+ export let languageFor = defaultLanguageFor;
56
+
57
+ /**
58
+ * `"windowed"` mounts files near the viewport and unmounts far ones,
59
+ * keeping their height; `"lazy"` never unmounts; `"none"` mounts every
60
+ * file up front (for printing or browser find on short lists).
61
+ * @type {"windowed" | "lazy" | "none"}
62
+ */
63
+ export let windowing = "windowed";
64
+
65
+ /** How far outside the viewport a file mounts. */
66
+ export let rootMargin = "1500px 0px";
67
+
68
+ /**
69
+ * The scrolling element, if the list isn't in the page's scroll.
70
+ * @type {Element | null}
71
+ */
72
+ export let root = null;
73
+
74
+ /** Let readers collapse a file by clicking its header. */
75
+ export let collapsible = true;
76
+
77
+ import { onMount, tick } from "svelte";
78
+ import DiffFileListItem from "./DiffFileListItem.svelte";
79
+ import { languageNameForPath } from "./diff-languages.js";
80
+ import { loadLanguage } from "./load-language.js";
81
+
82
+ /** @param {string} path */
83
+ function defaultLanguageFor(path) {
84
+ const name = languageNameForPath(path);
85
+ return name ? loadLanguage(name) : undefined;
86
+ }
87
+
88
+ // Before a file has rendered, its height is a guess from its size.
89
+ const ESTIMATED_LINE = 18;
90
+
91
+ /** @param {DiffFile} file @param {number} index */
92
+ const keyOf = (file, index) =>
93
+ file.key ?? `${index}\u0000${file.oldPath ?? file.path}\u0000${file.path}`;
94
+
95
+ /** @type {Set<string>} */
96
+ let mounted = new Set();
97
+ /** @type {Map<string, number>} */
98
+ let heights = new Map();
99
+ /** @type {Map<string, { additions: number, deletions: number, changes: number }>} */
100
+ let stats = new Map();
101
+ /** @type {Set<string>} */
102
+ let collapsed = new Set();
103
+
104
+ /** @type {HTMLElement[]} */
105
+ const sections = [];
106
+
107
+ /** @param {DiffFile} file */
108
+ function estimate(file) {
109
+ const lines = file.patch
110
+ ? file.patch.hunks.reduce((n, h) => n + h.lines.length + 1, 0)
111
+ : Math.max(
112
+ (file.before ?? "").split("\n").length,
113
+ (file.after ?? "").split("\n").length,
114
+ );
115
+ const px = Math.max(1, lines) * ESTIMATED_LINE;
116
+ return maxHeight === null ? px : Math.min(maxHeight, px);
117
+ }
118
+
119
+ /** @param {DiffFile} file */
120
+ function isEmpty(file) {
121
+ if (file.status === "binary") return true;
122
+ if (file.patch) return file.patch.hunks.length === 0;
123
+ return (file.before ?? "") === (file.after ?? "");
124
+ }
125
+
126
+ /**
127
+ * @param {DiffFile} file
128
+ * @param {string} key
129
+ * @param {typeof stats} known stats from mounted files
130
+ */
131
+ function statsOf(file, key, known) {
132
+ if (file.patch) {
133
+ return {
134
+ additions: file.patch.additions,
135
+ deletions: file.patch.deletions,
136
+ changes: file.patch.hunks.length,
137
+ };
138
+ }
139
+ return known.get(key);
140
+ }
141
+
142
+ /** @param {string} key */
143
+ function toggle(key) {
144
+ if (!collapsible) return;
145
+ const next = new Set(collapsed);
146
+ if (next.has(key)) next.delete(key);
147
+ else next.add(key);
148
+ collapsed = next;
149
+ }
150
+
151
+ /**
152
+ * @param {string} key
153
+ * @param {{ additions: number, deletions: number, changes: number }} value
154
+ */
155
+ function setStats(key, value) {
156
+ const prev = stats.get(key);
157
+ if (
158
+ prev &&
159
+ prev.additions === value.additions &&
160
+ prev.deletions === value.deletions &&
161
+ prev.changes === value.changes
162
+ ) {
163
+ return;
164
+ }
165
+ stats.set(key, value);
166
+ stats = new Map(stats);
167
+ }
168
+
169
+ /** @type {IntersectionObserver | undefined} */
170
+ let observer;
171
+
172
+ // Files above the viewport change height as they mount or unmount (an
173
+ // estimate becomes a measurement). Keep the first file in view where it
174
+ // was: remember it on scroll, and after any resize scroll by however far
175
+ // it moved. If the browser's own scroll anchoring already corrected it,
176
+ // it hasn't moved and nothing happens.
177
+ /** @type {ResizeObserver | undefined} */
178
+ let resizer;
179
+ /** @type {HTMLElement | null} */
180
+ let anchor = null;
181
+ let anchorTop = 0;
182
+ // Where our own correction left the scroll, so its scroll event doesn't
183
+ // re-anchor mid-correction.
184
+ let correctedTo = Number.NaN;
185
+
186
+ const viewTop = () => (root ? root.getBoundingClientRect().top : 0);
187
+ const scrollPosition = () => (root ? root.scrollTop : window.scrollY);
188
+
189
+ function onScroll() {
190
+ if (scrollPosition() === correctedTo) {
191
+ correctedTo = Number.NaN;
192
+ return;
193
+ }
194
+ captureAnchor();
195
+ }
196
+
197
+ /** First file whose top is at or below the top of the view. */
198
+ function captureAnchor() {
199
+ const top = viewTop();
200
+ let lo = 0;
201
+ let hi = sections.length - 1;
202
+ /** @type {HTMLElement | null} */
203
+ let found = null;
204
+ while (lo <= hi) {
205
+ const mid = (lo + hi) >> 1;
206
+ const section = sections[mid];
207
+ if (!section) break;
208
+ if (section.getBoundingClientRect().top >= top - 1) {
209
+ found = section;
210
+ hi = mid - 1;
211
+ } else {
212
+ lo = mid + 1;
213
+ }
214
+ }
215
+ anchor = found;
216
+ anchorTop = found ? found.getBoundingClientRect().top : 0;
217
+ }
218
+
219
+ function onResize() {
220
+ if (!anchor?.isConnected) return;
221
+ const drift = anchor.getBoundingClientRect().top - anchorTop;
222
+ if (Math.abs(drift) < 0.5) return;
223
+ if (root) root.scrollTop += drift;
224
+ else window.scrollBy(0, drift);
225
+ correctedTo = scrollPosition();
226
+ anchorTop = anchor.getBoundingClientRect().top;
227
+ }
228
+
229
+ /** @param {IntersectionObserverEntry[]} entries */
230
+ function onIntersect(entries) {
231
+ // Measure where the reader is before anything mounts or unmounts.
232
+ captureAnchor();
233
+ let changed = false;
234
+ for (const entry of entries) {
235
+ const el = /** @type {HTMLElement} */ (entry.target);
236
+ const key = el.dataset.key;
237
+ if (key === undefined) continue;
238
+ if (entry.isIntersecting) {
239
+ if (!mounted.has(key)) {
240
+ mounted.add(key);
241
+ changed = true;
242
+ }
243
+ } else if (windowing === "windowed" && mounted.has(key)) {
244
+ const body = el.querySelector(".shl-diff-file-body");
245
+ if (body) heights.set(key, body.getBoundingClientRect().height);
246
+ mounted.delete(key);
247
+ changed = true;
248
+ }
249
+ }
250
+ if (changed) {
251
+ mounted = new Set(mounted);
252
+ heights = new Map(heights);
253
+ }
254
+ }
255
+
256
+ /**
257
+ * @param {HTMLElement} node
258
+ * @param {string} key
259
+ */
260
+ function track(node, key) {
261
+ node.dataset.key = key;
262
+ observer?.observe(node);
263
+ resizer?.observe(node);
264
+ return {
265
+ /** @param {string} next */
266
+ update(next) {
267
+ node.dataset.key = next;
268
+ },
269
+ destroy() {
270
+ observer?.unobserve(node);
271
+ resizer?.unobserve(node);
272
+ },
273
+ };
274
+ }
275
+
276
+ $: if (windowing === "none") {
277
+ mounted = new Set(files.map(keyOf));
278
+ }
279
+
280
+ /**
281
+ * Scroll a file's header into view. The files around it mount first, so
282
+ * they have real heights before the reader sees them.
283
+ * @param {number} index
284
+ * @param {ScrollIntoViewOptions} [options]
285
+ */
286
+ export async function scrollToFile(index, options = { block: "start" }) {
287
+ if (!files[index]) return;
288
+ const next = new Set(mounted);
289
+ for (
290
+ let i = Math.max(0, index - 3);
291
+ i <= Math.min(files.length - 1, index + 3);
292
+ i++
293
+ ) {
294
+ next.add(keyOf(/** @type {DiffFile} */ (files[i]), i));
295
+ }
296
+ if (next.size !== mounted.size) {
297
+ mounted = next;
298
+ await tick();
299
+ // One frame, so the new files lay out and measure their rows.
300
+ await new Promise((resolve) => requestAnimationFrame(resolve));
301
+ }
302
+ sections[index]?.scrollIntoView(options);
303
+ captureAnchor();
304
+ }
305
+
306
+ onMount(() => {
307
+ if (typeof IntersectionObserver === "undefined") {
308
+ mounted = new Set(files.map(keyOf));
309
+ return;
310
+ }
311
+ observer = new IntersectionObserver(onIntersect, { root, rootMargin });
312
+ if (typeof ResizeObserver !== "undefined") {
313
+ resizer = new ResizeObserver(onResize);
314
+ }
315
+ const scroller = root ?? window;
316
+ scroller.addEventListener("scroll", onScroll, { passive: true });
317
+ captureAnchor();
318
+ for (const section of sections) {
319
+ if (!section) continue;
320
+ observer.observe(section);
321
+ resizer?.observe(section);
322
+ }
323
+ return () => {
324
+ observer?.disconnect();
325
+ resizer?.disconnect();
326
+ scroller.removeEventListener("scroll", onScroll);
327
+ };
328
+ });
329
+
330
+ $: ({ class: _class, ...rest } = $$restProps);
331
+
332
+ $: options = { view, context, wordDiff, ignoreWhitespace };
333
+ $: viewProps = {
334
+ wrap,
335
+ review,
336
+ palette,
337
+ style:
338
+ maxHeight === null
339
+ ? "height: auto;"
340
+ : `height: auto; max-height: ${maxHeight}px;`,
341
+ };
342
+
343
+ const statusLabel = {
344
+ modified: "Modified",
345
+ added: "Added",
346
+ deleted: "Deleted",
347
+ renamed: "Renamed",
348
+ binary: "Binary",
349
+ };
350
+ </script>
351
+
352
+ <div
353
+ {...rest}
354
+ class="shl-diff-file-list shl-diff-theme {$$restProps.class ?? ''}"
355
+ >
356
+ {#each files as file, index (keyOf(file, index))}
357
+ {@const key = keyOf(file, index)}
358
+ {@const fileStats = statsOf(file, key, stats)}
359
+ {@const isCollapsed = collapsed.has(key)}
360
+ <section
361
+ class="shl-diff-file"
362
+ class:shl-diff-file-collapsed={isCollapsed}
363
+ bind:this={sections[index]}
364
+ use:track={key}
365
+ aria-label={file.path}
366
+ >
367
+ <slot
368
+ name="header"
369
+ {file}
370
+ {index}
371
+ stats={fileStats}
372
+ collapsed={isCollapsed}
373
+ toggle={() => toggle(key)}
374
+ >
375
+ <button
376
+ type="button"
377
+ class="shl-diff-file-header"
378
+ aria-expanded={!isCollapsed}
379
+ disabled={!collapsible}
380
+ on:click={() => toggle(key)}
381
+ >
382
+ {#if collapsible}
383
+ <span class="shl-diff-file-chevron" aria-hidden="true">▾</span>
384
+ {/if}
385
+ <span class="shl-diff-file-path">
386
+ {file.status === "renamed" && file.oldPath
387
+ ? `${file.oldPath} → ${file.path}`
388
+ : file.path}
389
+ </span>
390
+ {#if file.status && file.status !== "modified"}
391
+ <span class="shl-diff-file-status">{statusLabel[file.status]}</span>
392
+ {/if}
393
+ {#if fileStats}
394
+ <span class="shl-diff-file-stats">
395
+ <span class="shl-diff-file-add">+{fileStats.additions}</span>
396
+ <span class="shl-diff-file-del">−{fileStats.deletions}</span>
397
+ </span>
398
+ {/if}
399
+ </button>
400
+ </slot>
401
+ {#if !isCollapsed}
402
+ {#if isEmpty(file)}
403
+ <p class="shl-diff-file-empty">
404
+ {file.status === "binary" ? "Binary file not shown" : "No changes"}
405
+ </p>
406
+ {:else if mounted.has(key)}
407
+ <div class="shl-diff-file-body">
408
+ <DiffFileListItem
409
+ {file}
410
+ {languageFor}
411
+ {options}
412
+ {viewProps}
413
+ onStats={(value) => setStats(key, value)}
414
+ />
415
+ </div>
416
+ {:else}
417
+ <div
418
+ class="shl-diff-file-placeholder"
419
+ style="height: {heights.get(key) ?? estimate(file)}px;"
420
+ ></div>
421
+ {/if}
422
+ {/if}
423
+ </section>
424
+ {/each}
425
+ </div>
426
+
427
+ <style>
428
+ .shl-diff-file-list {
429
+ overflow-anchor: none;
430
+ display: flex;
431
+ flex-direction: column;
432
+ gap: var(--shl-diff-file-gap, 12px);
433
+ }
434
+
435
+ .shl-diff-file {
436
+ border: 1px solid var(--shl-diff-rule, rgba(128, 128, 128, 0.25));
437
+ border-radius: var(--shl-diff-file-radius, 6px);
438
+ overflow: hidden;
439
+ }
440
+
441
+ .shl-diff-file-header {
442
+ display: flex;
443
+ align-items: center;
444
+ gap: 1ch;
445
+ width: 100%;
446
+ padding: 6px 10px;
447
+ border: 0;
448
+ border-bottom: 1px solid var(--shl-diff-rule, rgba(128, 128, 128, 0.25));
449
+ background: var(--shl-diff-file-header-background, transparent);
450
+ color: inherit;
451
+ font: inherit;
452
+ font-size: 0.85em;
453
+ text-align: left;
454
+ cursor: pointer;
455
+ }
456
+
457
+ .shl-diff-file-header:disabled {
458
+ cursor: default;
459
+ }
460
+
461
+ .shl-diff-file-collapsed .shl-diff-file-header {
462
+ border-bottom: 0;
463
+ }
464
+
465
+ .shl-diff-file-chevron {
466
+ transition: transform 0.1s;
467
+ }
468
+
469
+ .shl-diff-file-collapsed .shl-diff-file-chevron {
470
+ transform: rotate(-90deg);
471
+ }
472
+
473
+ .shl-diff-file-path {
474
+ flex: 1;
475
+ min-width: 0;
476
+ overflow: hidden;
477
+ text-overflow: ellipsis;
478
+ white-space: nowrap;
479
+ font-family: var(--shl-diff-font, ui-monospace, Menlo, Consolas, monospace);
480
+ /* overflow: hidden clips descenders under an inherited line-height: 1. */
481
+ line-height: normal;
482
+ }
483
+
484
+ .shl-diff-file-status {
485
+ padding: 0 6px;
486
+ border-radius: 3px;
487
+ border: 1px solid currentColor;
488
+ opacity: 0.7;
489
+ font-size: 0.9em;
490
+ }
491
+
492
+ .shl-diff-file-add {
493
+ color: var(--shl-diff-add-accent, #3fb950);
494
+ }
495
+
496
+ .shl-diff-file-del {
497
+ color: var(--shl-diff-del-accent, #f85149);
498
+ }
499
+
500
+ .shl-diff-file-empty {
501
+ margin: 0;
502
+ padding: 8px 10px;
503
+ opacity: 0.6;
504
+ font-size: 0.85em;
505
+ }
506
+
507
+ .shl-diff-file-placeholder {
508
+ background: var(
509
+ --shl-diff-file-placeholder,
510
+ repeating-linear-gradient(
511
+ -45deg,
512
+ transparent 0 8px,
513
+ rgba(128, 128, 128, 0.08) 8px 9px
514
+ )
515
+ );
516
+ }
517
+
518
+ @media (prefers-reduced-motion: reduce) {
519
+ .shl-diff-file-chevron {
520
+ transition: none;
521
+ }
522
+ }
523
+ </style>
@@ -0,0 +1,94 @@
1
+ import type { SvelteComponentTyped } from "svelte";
2
+ import type { HTMLAttributes } from "svelte/elements";
3
+ import type { FilePatch } from "./diff-edits";
4
+ import type { LanguageType } from "./languages";
5
+
6
+ /** One file in a `DiffFileList`: a parsed patch, or two texts. */
7
+ export interface DiffFile {
8
+ /** Stable identity across updates. @default index + paths */
9
+ key?: string;
10
+ path: string;
11
+ oldPath?: string;
12
+ status?: "modified" | "added" | "deleted" | "renamed" | "binary";
13
+ /** A file from `parsePatch`. Takes precedence over `before`/`after`. */
14
+ patch?: FilePatch;
15
+ before?: string;
16
+ after?: string;
17
+ /** Skips `languageFor` for this file. */
18
+ language?: LanguageType<string>;
19
+ }
20
+
21
+ export type DiffFileListProps = HTMLAttributes<HTMLDivElement> & {
22
+ files: DiffFile[];
23
+ /** @default "unified" */
24
+ view?: "unified" | "split";
25
+ /** @default 3 */
26
+ context?: number;
27
+ /** @default true */
28
+ wordDiff?: boolean;
29
+ /** @default false */
30
+ ignoreWhitespace?: boolean;
31
+ /** @default false */
32
+ wrap?: boolean;
33
+ /** @default false */
34
+ review?: boolean;
35
+ /** @default "default" */
36
+ palette?: "default" | "colorblind";
37
+
38
+ /**
39
+ * Tallest a file's diff grows before it scrolls on its own, in pixels.
40
+ * `null` shows every file in full.
41
+ * @default 640
42
+ */
43
+ maxHeight?: number | null;
44
+
45
+ /**
46
+ * Picks a language for a path. The default loads one by file extension
47
+ * (see `languageNameForPath`) the first time the file mounts.
48
+ */
49
+ languageFor?: (
50
+ path: string,
51
+ ) =>
52
+ | LanguageType<string>
53
+ | Promise<LanguageType<string> | undefined>
54
+ | undefined;
55
+
56
+ /**
57
+ * `"windowed"` mounts files near the viewport and unmounts far ones,
58
+ * keeping their height. `"lazy"` never unmounts. `"none"` mounts every
59
+ * file up front, for printing or browser find on short lists.
60
+ * @default "windowed"
61
+ */
62
+ windowing?: "windowed" | "lazy" | "none";
63
+
64
+ /** How far outside the viewport a file mounts. @default "1500px 0px" */
65
+ rootMargin?: string;
66
+
67
+ /** The scrolling element, if the list isn't in the page's scroll. @default null */
68
+ root?: Element | null;
69
+
70
+ /** Collapse a file by clicking its header. @default true */
71
+ collapsible?: boolean;
72
+ };
73
+
74
+ export type DiffFileListSlots = {
75
+ /** Replaces a file's header. */
76
+ header: {
77
+ file: DiffFile;
78
+ index: number;
79
+ /** From the patch, or once the file has mounted. */
80
+ stats:
81
+ { additions: number; deletions: number; changes: number } | undefined;
82
+ collapsed: boolean;
83
+ toggle: () => void;
84
+ };
85
+ };
86
+
87
+ export default class DiffFileList extends SvelteComponentTyped<
88
+ DiffFileListProps,
89
+ Record<string, never>,
90
+ DiffFileListSlots
91
+ > {
92
+ /** Scroll a file's header into view, mounting it first. */
93
+ scrollToFile(index: number, options?: ScrollIntoViewOptions): Promise<void>;
94
+ }
@@ -0,0 +1,44 @@
1
+ <script>
2
+ // One mounted file of a DiffFileList. Internal.
3
+
4
+ /** @type {import("./DiffFileList.svelte").DiffFile} */
5
+ export let file;
6
+
7
+ /** @type {(path: string) => import("./languages").LanguageType<string> | Promise<import("./languages").LanguageType<string> | undefined> | undefined} */
8
+ export let languageFor;
9
+
10
+ /** @type {Partial<import("./diff-controller.js").DiffOptions>} */
11
+ export let options = {};
12
+
13
+ /** @type {import("./DiffView.svelte").DiffViewProps} */
14
+ export let viewProps = {};
15
+
16
+ /** @type {(stats: { additions: number, deletions: number, changes: number }) => void} */
17
+ export let onStats = () => {};
18
+
19
+ import { onMount } from "svelte";
20
+ import DiffView from "./DiffView.svelte";
21
+ import { createDiffController } from "./diff-controller.js";
22
+
23
+ const diff = createDiffController(options);
24
+
25
+ $: diff.setOptions(options);
26
+ $: if (file.patch) diff.setPatch(file.patch);
27
+ else diff.update(file.before ?? "", file.after ?? "");
28
+ $: onStats($diff.stats());
29
+
30
+ onMount(() => {
31
+ let alive = true;
32
+ const pick = file.language ?? languageFor(file.path);
33
+ Promise.resolve(pick)
34
+ .then((language) => {
35
+ if (alive && language) diff.setOptions({ language });
36
+ })
37
+ .catch(() => {});
38
+ return () => {
39
+ alive = false;
40
+ };
41
+ });
42
+ </script>
43
+
44
+ <DiffView {diff} keyboard {...viewProps} />