svelte-highlight 7.17.0 → 7.18.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/AnsiOutput.svelte +2 -135
- package/HighlightStream.svelte +8 -14
- package/README.md +23 -7
- package/Typewriter.svelte +79 -56
- package/Typewriter.svelte.d.ts +10 -1
- package/ansi-color.js +201 -0
- package/editable-dom-paint.js +4 -3
- package/engine.js +61 -43
- package/index.d.ts +12 -0
- package/index.js +12 -0
- package/package.json +1 -1
- package/stream-sealed-chunks.js +44 -0
- package/text-diff.js +10 -2
- package/typewriter-easing.d.ts +14 -0
- package/typewriter-easing.js +57 -0
- package/typewriter-units.d.ts +6 -0
- package/typewriter-units.js +27 -4
package/AnsiOutput.svelte
CHANGED
|
@@ -9,147 +9,14 @@
|
|
|
9
9
|
export let autoContrast = true;
|
|
10
10
|
|
|
11
11
|
import { parseAnsi } from "./ansi.js";
|
|
12
|
-
|
|
13
|
-
// Default foreground and minimum WCAG contrast ratio against backgrounds.
|
|
14
|
-
const FOREGROUND_FALLBACK = "#d4d4d4";
|
|
15
|
-
const CONTRAST_TARGET = 4.5;
|
|
16
|
-
/** @type {[number, number, number]} */
|
|
17
|
-
const BLACK = [0, 0, 0];
|
|
18
|
-
/** @type {[number, number, number]} */
|
|
19
|
-
const WHITE = [255, 255, 255];
|
|
20
|
-
|
|
21
|
-
// 16-color xterm defaults, each overridable via `--ansi-<name>`.
|
|
22
|
-
const DEFAULTS = {
|
|
23
|
-
black: "#000000",
|
|
24
|
-
red: "#cd0000",
|
|
25
|
-
green: "#00cd00",
|
|
26
|
-
yellow: "#cdcd00",
|
|
27
|
-
blue: "#0000ee",
|
|
28
|
-
magenta: "#cd00cd",
|
|
29
|
-
cyan: "#00cdcd",
|
|
30
|
-
white: "#e5e5e5",
|
|
31
|
-
"bright-black": "#7f7f7f",
|
|
32
|
-
"bright-red": "#ff0000",
|
|
33
|
-
"bright-green": "#00ff00",
|
|
34
|
-
"bright-yellow": "#ffff00",
|
|
35
|
-
"bright-blue": "#5c5cff",
|
|
36
|
-
"bright-magenta": "#ff00ff",
|
|
37
|
-
"bright-cyan": "#00ffff",
|
|
38
|
-
"bright-white": "#ffffff",
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
// 256-color cube steps (6×6×6).
|
|
42
|
-
const CUBE = [0, 95, 135, 175, 215, 255];
|
|
43
|
-
|
|
44
|
-
/** 256-color index → hex. */
|
|
45
|
-
function indexedHex(index) {
|
|
46
|
-
if (index >= 232) {
|
|
47
|
-
const value = (index - 232) * 10 + 8;
|
|
48
|
-
const hex = value.toString(16).padStart(2, "0");
|
|
49
|
-
return `#${hex}${hex}${hex}`;
|
|
50
|
-
}
|
|
51
|
-
const n = index - 16;
|
|
52
|
-
const r = CUBE[Math.floor(n / 36) % 6];
|
|
53
|
-
const g = CUBE[Math.floor(n / 6) % 6];
|
|
54
|
-
const b = CUBE[n % 6];
|
|
55
|
-
return `#${[r, g, b].map((c) => c.toString(16).padStart(2, "0")).join("")}`;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Parsed color → CSS (named colors use `--ansi-*` vars). */
|
|
59
|
-
function cssColor(color) {
|
|
60
|
-
if ("name" in color) {
|
|
61
|
-
return `var(--ansi-${color.name}, ${DEFAULTS[color.name]})`;
|
|
62
|
-
}
|
|
63
|
-
if ("rgb" in color) {
|
|
64
|
-
return `rgb(${color.rgb[0]}, ${color.rgb[1]}, ${color.rgb[2]})`;
|
|
65
|
-
}
|
|
66
|
-
return indexedHex(color.index);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @param {string} hex A `#rrggbb` string.
|
|
71
|
-
* @returns {[number, number, number]}
|
|
72
|
-
*/
|
|
73
|
-
function hexToRgb(hex) {
|
|
74
|
-
const value = hex.replace("#", "");
|
|
75
|
-
return [
|
|
76
|
-
Number.parseInt(value.slice(0, 2), 16),
|
|
77
|
-
Number.parseInt(value.slice(2, 4), 16),
|
|
78
|
-
Number.parseInt(value.slice(4, 6), 16),
|
|
79
|
-
];
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** Named colors use the default palette (theme overrides aren't known here). */
|
|
83
|
-
function colorToRgb(color) {
|
|
84
|
-
if ("name" in color) return hexToRgb(DEFAULTS[color.name] ?? "#000000");
|
|
85
|
-
if ("rgb" in color) return color.rgb;
|
|
86
|
-
return hexToRgb(indexedHex(color.index));
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** WCAG relative luminance. */
|
|
90
|
-
function luminance([r, g, b]) {
|
|
91
|
-
const channels = [r, g, b].map((c) => {
|
|
92
|
-
const v = c / 255;
|
|
93
|
-
return v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
|
|
94
|
-
});
|
|
95
|
-
return 0.2126 * channels[0] + 0.7152 * channels[1] + 0.0722 * channels[2];
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** WCAG contrast ratio. */
|
|
99
|
-
function contrastRatio(a, b) {
|
|
100
|
-
const la = luminance(a);
|
|
101
|
-
const lb = luminance(b);
|
|
102
|
-
return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** Black or white, whichever contrasts more with `bg`. */
|
|
106
|
-
function readableForeground(bg) {
|
|
107
|
-
return contrastRatio(BLACK, bg) >= contrastRatio(WHITE, bg)
|
|
108
|
-
? "#000000"
|
|
109
|
-
: "#ffffff";
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/** Foreground CSS, with auto-contrast override when needed. */
|
|
113
|
-
function foregroundCss(segment) {
|
|
114
|
-
// Concealed text is rendered transparent (layout and copy text stay).
|
|
115
|
-
if (segment.conceal) return "transparent";
|
|
116
|
-
if (autoContrast && segment.bg) {
|
|
117
|
-
const bg = colorToRgb(segment.bg);
|
|
118
|
-
const fg = segment.fg
|
|
119
|
-
? colorToRgb(segment.fg)
|
|
120
|
-
: hexToRgb(FOREGROUND_FALLBACK);
|
|
121
|
-
if (contrastRatio(fg, bg) < CONTRAST_TARGET)
|
|
122
|
-
return readableForeground(bg);
|
|
123
|
-
}
|
|
124
|
-
return segment.fg ? cssColor(segment.fg) : undefined;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/** @param {import("./ansi").AnsiSegment} segment */
|
|
128
|
-
function classNames(segment) {
|
|
129
|
-
const names = [];
|
|
130
|
-
if (segment.bold) names.push("bold");
|
|
131
|
-
if (segment.dim) names.push("dim");
|
|
132
|
-
if (segment.italic) names.push("italic");
|
|
133
|
-
if (segment.underline) names.push("underline");
|
|
134
|
-
if (segment.strikethrough) names.push("strikethrough");
|
|
135
|
-
return names.length ? names.join(" ") : undefined;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/** @param {import("./ansi").AnsiSegment} segment */
|
|
139
|
-
function inlineStyle(segment) {
|
|
140
|
-
let style = "";
|
|
141
|
-
if (segment.bg) style += `background-color:${cssColor(segment.bg)};`;
|
|
142
|
-
const fg = foregroundCss(segment);
|
|
143
|
-
if (fg) style += `color:${fg};`;
|
|
144
|
-
return style || undefined;
|
|
145
|
-
}
|
|
12
|
+
import { classNames, inlineStyle } from "./ansi-color.js";
|
|
146
13
|
|
|
147
14
|
// Precompute class/style per segment. Parse only when `text` changes.
|
|
148
15
|
$: parsed = parseAnsi(text);
|
|
149
16
|
$: segments = parsed.map((segment) => ({
|
|
150
17
|
text: segment.text,
|
|
151
18
|
class: classNames(segment),
|
|
152
|
-
style: inlineStyle(segment),
|
|
19
|
+
style: inlineStyle(segment, autoContrast),
|
|
153
20
|
link: segment.link,
|
|
154
21
|
}));
|
|
155
22
|
</script>
|
package/HighlightStream.svelte
CHANGED
|
@@ -59,6 +59,10 @@
|
|
|
59
59
|
import { extendLines } from "./engine.js";
|
|
60
60
|
import { ensureRegistered, registry } from "./registry.js";
|
|
61
61
|
import { createCompletedHtmlBuffer } from "./stream-highlighted.js";
|
|
62
|
+
import {
|
|
63
|
+
buildSealedChunkHtml,
|
|
64
|
+
pushSealedChunk,
|
|
65
|
+
} from "./stream-sealed-chunks.js";
|
|
62
66
|
import { createTokenizedDocument } from "./tokenized-document.js";
|
|
63
67
|
import { watchLineHeight, windowRange } from "./virtual-window.js";
|
|
64
68
|
|
|
@@ -172,14 +176,10 @@
|
|
|
172
176
|
// as many full chunks as are ready.
|
|
173
177
|
function sealChunk() {
|
|
174
178
|
const chunkLines = unsealedLines.slice(0, SEAL_CHUNK_LINES);
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
domHtml += `${sep}<span class="highlight-stream-line" data-line="${i}">${chunkLines[li]}</span>`;
|
|
180
|
-
}
|
|
181
|
-
sealedChunks.push(domHtml);
|
|
182
|
-
sealedChunks = sealedChunks;
|
|
179
|
+
sealedChunks = pushSealedChunk(
|
|
180
|
+
sealedChunks,
|
|
181
|
+
buildSealedChunkHtml(chunkLines, sealedLineCount),
|
|
182
|
+
);
|
|
183
183
|
sealedLineCount += chunkLines.length;
|
|
184
184
|
unsealedLines = unsealedLines.slice(SEAL_CHUNK_LINES);
|
|
185
185
|
}
|
|
@@ -478,12 +478,6 @@
|
|
|
478
478
|
animation: highlight-stream-blink var(--caret-blink, 1s) step-end infinite;
|
|
479
479
|
}
|
|
480
480
|
|
|
481
|
-
@media (prefers-reduced-motion: reduce) {
|
|
482
|
-
.highlight-stream-caret {
|
|
483
|
-
animation: none;
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
|
|
487
481
|
@keyframes highlight-stream-blink {
|
|
488
482
|
50% {
|
|
489
483
|
opacity: 0;
|
package/README.md
CHANGED
|
@@ -1333,7 +1333,22 @@ Set `speed` (milliseconds per character) and pause with `play`. Turn `play` back
|
|
|
1333
1333
|
</Highlight>
|
|
1334
1334
|
```
|
|
1335
1335
|
|
|
1336
|
-
|
|
1336
|
+
Control the pacing curve with `easing`: a function mapping elapsed-time fraction (`0`-`1`) to revealed-fraction (`0`-`1`). It defaults to `linear`; import a named curve or pass your own. Total typing duration is always `speed` × character count, no matter the curve -- only the pacing within that duration changes.
|
|
1337
|
+
|
|
1338
|
+
```svelte
|
|
1339
|
+
<script>
|
|
1340
|
+
import Highlight, { Typewriter, easeOutCubic } from "svelte-highlight";
|
|
1341
|
+
</script>
|
|
1342
|
+
|
|
1343
|
+
<Highlight language={typescript} {code} let:highlighted>
|
|
1344
|
+
<Typewriter {highlighted} easing={easeOutCubic} />
|
|
1345
|
+
</Highlight>
|
|
1346
|
+
```
|
|
1347
|
+
|
|
1348
|
+
Named curves: `linear` (default), `easeInQuad`/`easeOutQuad`/`easeInOutQuad`, `easeInCubic`/`easeOutCubic`/`easeInOutCubic`, `easeInSine`/`easeOutSine`/`easeInOutSine`.
|
|
1349
|
+
|
|
1350
|
+
`Typewriter` doesn't gate itself on `prefers-reduced-motion` -- if you want to honor it, check `matchMedia("(prefers-reduced-motion: reduce)")` yourself and skip rendering `Typewriter` (or set a very low `speed`). Customize the caret with `--caret-width`, `--caret-height`, `--caret-gap`, `--caret-color`, and `--caret-blink`.
|
|
1351
|
+
|
|
1337
1352
|
|
|
1338
1353
|
Each tick reveals one already-rendered unit rather than re-rendering the block, so animating stays smooth up to tens of thousands of characters; past that it falls back to the coarser whole-block reveal.
|
|
1339
1354
|
|
|
@@ -1815,17 +1830,18 @@ See [Large documents](#large-documents) above.
|
|
|
1815
1830
|
|
|
1816
1831
|
#### Props
|
|
1817
1832
|
|
|
1818
|
-
| Name | Type
|
|
1819
|
-
| :---------- |
|
|
1820
|
-
| highlighted | `string`
|
|
1821
|
-
| speed | `number`
|
|
1822
|
-
| play | `boolean`
|
|
1833
|
+
| Name | Type | Default value |
|
|
1834
|
+
| :---------- | :----------------------- | :------------ |
|
|
1835
|
+
| highlighted | `string` | `""` |
|
|
1836
|
+
| speed | `number` | `30` |
|
|
1837
|
+
| play | `boolean` | `true` |
|
|
1838
|
+
| easing | `(t: number) => number` | `linear` |
|
|
1823
1839
|
|
|
1824
1840
|
`$$restProps` are forwarded to the top-level `pre` element.
|
|
1825
1841
|
|
|
1826
1842
|
#### Dispatched Events
|
|
1827
1843
|
|
|
1828
|
-
- **on:done**: fires when typing finishes
|
|
1844
|
+
- **on:done**: fires when typing finishes
|
|
1829
1845
|
|
|
1830
1846
|
```svelte
|
|
1831
1847
|
<Highlight language={typescript} {code} let:highlighted>
|
package/Typewriter.svelte
CHANGED
|
@@ -18,11 +18,23 @@
|
|
|
18
18
|
export let play = true;
|
|
19
19
|
|
|
20
20
|
import { createEventDispatcher, onMount } from "svelte";
|
|
21
|
+
import { linear } from "./typewriter-easing.js";
|
|
21
22
|
import {
|
|
23
|
+
buildUnitMarkup,
|
|
22
24
|
createTypewriterSplitter,
|
|
23
25
|
tokenizeTypewriter as tokenize,
|
|
24
26
|
} from "./typewriter-units.js";
|
|
25
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Reveal-progress curve: maps elapsed-time fraction (0-1) to
|
|
30
|
+
* revealed-fraction (0-1). Total typing duration is always
|
|
31
|
+
* `speed * <visible character count>` regardless of curve -- only the
|
|
32
|
+
* pacing within that duration changes. Import a named curve
|
|
33
|
+
* (`easeOutQuad`, `easeInOutCubic`, ...) or pass your own function.
|
|
34
|
+
* @type {(t: number) => number}
|
|
35
|
+
*/
|
|
36
|
+
export let easing = linear;
|
|
37
|
+
|
|
26
38
|
const dispatch = createEventDispatcher();
|
|
27
39
|
|
|
28
40
|
// Above this many visible units, per-unit spans (one element per char)
|
|
@@ -34,17 +46,20 @@
|
|
|
34
46
|
/** @type {boolean} */
|
|
35
47
|
let mounted = false;
|
|
36
48
|
|
|
37
|
-
/** @type {boolean} */
|
|
38
|
-
let reducedMotion = false;
|
|
39
|
-
|
|
40
49
|
/** @type {boolean} */
|
|
41
50
|
let doneFired = false;
|
|
42
51
|
|
|
43
52
|
/** Number of visible characters currently revealed. @type {number} */
|
|
44
53
|
let revealed = 0;
|
|
45
54
|
|
|
46
|
-
/** @type {
|
|
47
|
-
let
|
|
55
|
+
/** @type {number | undefined} */
|
|
56
|
+
let rafId;
|
|
57
|
+
|
|
58
|
+
/** Active ms elapsed for the current run (excludes paused time). @type {number} */
|
|
59
|
+
let elapsedMs = 0;
|
|
60
|
+
|
|
61
|
+
/** `performance.now()` at the last tick, or `undefined` right after a (re)start. @type {number | undefined} */
|
|
62
|
+
let frameTime;
|
|
48
63
|
|
|
49
64
|
/** @type {string | undefined} */
|
|
50
65
|
let prevHighlighted;
|
|
@@ -64,18 +79,6 @@
|
|
|
64
79
|
/** The unit currently marked with the caret, if any. @type {HTMLElement | undefined} */
|
|
65
80
|
let caretMark;
|
|
66
81
|
|
|
67
|
-
/** Render every unit once: tags pass through, each visible unit gets a span. */
|
|
68
|
-
function buildUnitMarkup(list) {
|
|
69
|
-
let html = "";
|
|
70
|
-
for (const unit of list) {
|
|
71
|
-
html +=
|
|
72
|
-
unit.visible === 0
|
|
73
|
-
? unit.raw
|
|
74
|
-
: `<span class="typewriter-unit typewriter-hidden">${unit.raw}</span>`;
|
|
75
|
-
}
|
|
76
|
-
return html;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
82
|
/**
|
|
80
83
|
* Bring `contentEl` in sync with `units`/`revealed` in O(1) amortized work:
|
|
81
84
|
* a fresh `units` value triggers one full (re)paint; otherwise only the
|
|
@@ -90,6 +93,15 @@
|
|
|
90
93
|
paintedUnits = units;
|
|
91
94
|
revealedInDom = 0;
|
|
92
95
|
caretMark = undefined;
|
|
96
|
+
// Resolve `currentColor` to a literal value now, while `contentEl`
|
|
97
|
+
// itself carries the theme's base foreground (nothing has recolored
|
|
98
|
+
// it yet). Stored as a custom property, it stays correct even once
|
|
99
|
+
// the caret mark lands inside a colored hljs token span, where a live
|
|
100
|
+
// `currentColor` would pick up that token's color instead.
|
|
101
|
+
contentEl.style.setProperty(
|
|
102
|
+
"--typewriter-caret-fg",
|
|
103
|
+
getComputedStyle(contentEl).color,
|
|
104
|
+
);
|
|
93
105
|
}
|
|
94
106
|
|
|
95
107
|
while (revealedInDom < revealed) {
|
|
@@ -110,9 +122,9 @@
|
|
|
110
122
|
}
|
|
111
123
|
}
|
|
112
124
|
|
|
113
|
-
function
|
|
114
|
-
|
|
115
|
-
|
|
125
|
+
function stopLoop() {
|
|
126
|
+
if (rafId !== undefined) cancelAnimationFrame(rafId);
|
|
127
|
+
rafId = undefined;
|
|
116
128
|
}
|
|
117
129
|
|
|
118
130
|
function fireDone() {
|
|
@@ -122,31 +134,50 @@
|
|
|
122
134
|
}
|
|
123
135
|
}
|
|
124
136
|
|
|
125
|
-
|
|
126
|
-
|
|
137
|
+
/**
|
|
138
|
+
* One animation frame: advance `elapsedMs` by the real time since the last
|
|
139
|
+
* frame (zero on the first frame after a start/resume, so pausing never
|
|
140
|
+
* counts the paused gap), then derive `revealed` from `easing` applied to
|
|
141
|
+
* the elapsed fraction of the total duration. Total duration is always
|
|
142
|
+
* `speed * total`, so only the pacing within it -- not its length --
|
|
143
|
+
* depends on `easing`. Clamped to `[0, total]` since a custom `easing` may
|
|
144
|
+
* overshoot outside `[0, 1]` (e.g. a "back"/"elastic" curve).
|
|
145
|
+
* @param {number} now
|
|
146
|
+
*/
|
|
147
|
+
function tick(now) {
|
|
148
|
+
if (frameTime !== undefined) elapsedMs += now - frameTime;
|
|
149
|
+
frameTime = now;
|
|
150
|
+
|
|
151
|
+
const duration = Math.max(0, speed) * total;
|
|
152
|
+
const t = duration > 0 ? Math.min(1, elapsedMs / duration) : 1;
|
|
153
|
+
const target = Math.round(easing(t) * total);
|
|
154
|
+
const next = Math.min(total, Math.max(0, target));
|
|
155
|
+
if (next > revealed) revealed = next;
|
|
156
|
+
|
|
127
157
|
if (useUnitReveal) syncUnitDom();
|
|
158
|
+
|
|
128
159
|
if (revealed >= total) {
|
|
129
|
-
|
|
160
|
+
stopLoop();
|
|
130
161
|
fireDone();
|
|
162
|
+
return;
|
|
131
163
|
}
|
|
164
|
+
rafId = requestAnimationFrame(tick);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function startLoop() {
|
|
168
|
+
frameTime = undefined;
|
|
169
|
+
rafId = requestAnimationFrame(tick);
|
|
132
170
|
}
|
|
133
171
|
|
|
134
|
-
/** Start/stop/retime the typing
|
|
172
|
+
/** Start/stop/retime the typing loop. */
|
|
135
173
|
function sync() {
|
|
136
|
-
|
|
174
|
+
stopLoop();
|
|
137
175
|
if (!mounted) return;
|
|
138
176
|
|
|
139
|
-
if (reducedMotion) {
|
|
140
|
-
// Reduced motion: show all at once.
|
|
141
|
-
if (revealed !== total) revealed = total;
|
|
142
|
-
fireDone();
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
177
|
if (useUnitReveal) syncUnitDom();
|
|
147
178
|
|
|
148
179
|
if (play && revealed < total) {
|
|
149
|
-
|
|
180
|
+
startLoop();
|
|
150
181
|
} else if (revealed >= total) {
|
|
151
182
|
fireDone();
|
|
152
183
|
}
|
|
@@ -159,20 +190,21 @@
|
|
|
159
190
|
$: splitter = createTypewriterSplitter(units, highlighted);
|
|
160
191
|
$: total = units.reduce((sum, unit) => sum + unit.visible, 0);
|
|
161
192
|
$: bigInput = total > UNIT_THRESHOLD;
|
|
162
|
-
$: useUnitReveal = mounted && !
|
|
193
|
+
$: useUnitReveal = mounted && !bigInput;
|
|
163
194
|
|
|
164
195
|
// Restart when `highlighted` changes.
|
|
165
196
|
$: if (highlighted !== prevHighlighted) {
|
|
166
197
|
prevHighlighted = highlighted;
|
|
167
198
|
doneFired = false;
|
|
168
|
-
|
|
199
|
+
elapsedMs = 0;
|
|
200
|
+
revealed = 0;
|
|
169
201
|
void useUnitReveal;
|
|
170
202
|
sync();
|
|
171
203
|
}
|
|
172
204
|
|
|
173
|
-
// Re-sync on play/speed/mount
|
|
205
|
+
// Re-sync on play/speed/easing/mount. Skip `total` (handled above).
|
|
174
206
|
$: {
|
|
175
|
-
void [play, speed,
|
|
207
|
+
void [play, speed, easing, mounted, useUnitReveal];
|
|
176
208
|
sync();
|
|
177
209
|
}
|
|
178
210
|
|
|
@@ -182,21 +214,13 @@
|
|
|
182
214
|
: mounted
|
|
183
215
|
? splitter.splitAt(revealed)
|
|
184
216
|
: { head: highlighted, tail: "" };
|
|
185
|
-
$: showCaret = mounted &&
|
|
217
|
+
$: showCaret = mounted && revealed < total;
|
|
186
218
|
|
|
187
219
|
onMount(() => {
|
|
188
220
|
mounted = true;
|
|
189
221
|
|
|
190
|
-
const query = window.matchMedia("(prefers-reduced-motion: reduce)");
|
|
191
|
-
reducedMotion = query.matches;
|
|
192
|
-
const onChange = (event) => {
|
|
193
|
-
reducedMotion = event.matches;
|
|
194
|
-
};
|
|
195
|
-
query.addEventListener?.("change", onChange);
|
|
196
|
-
|
|
197
222
|
return () => {
|
|
198
|
-
|
|
199
|
-
clearTimer();
|
|
223
|
+
stopLoop();
|
|
200
224
|
};
|
|
201
225
|
});
|
|
202
226
|
</script>
|
|
@@ -234,7 +258,13 @@
|
|
|
234
258
|
}
|
|
235
259
|
|
|
236
260
|
/* The caret rides the next-to-reveal (still hidden) unit's ::before, so it
|
|
237
|
-
never requires moving a real DOM node -- one class add/remove per tick.
|
|
261
|
+
never requires moving a real DOM node -- one class add/remove per tick.
|
|
262
|
+
That unit can land inside a still-open hljs token span (e.g. mid-
|
|
263
|
+
keyword), where `currentColor` would resolve to that token's color
|
|
264
|
+
instead of the theme's base foreground. `--typewriter-caret-fg` is set
|
|
265
|
+
on `contentEl` as a literal resolved color (not the `currentColor`
|
|
266
|
+
keyword) in `syncUnitDom`, so it survives that nesting unaffected by
|
|
267
|
+
descendant `color` overrides. */
|
|
238
268
|
:global(.typewriter-unit.typewriter-caret)::before {
|
|
239
269
|
content: "";
|
|
240
270
|
visibility: visible;
|
|
@@ -243,17 +273,10 @@
|
|
|
243
273
|
height: var(--caret-height, 1.1em);
|
|
244
274
|
margin-left: var(--caret-gap, 1px);
|
|
245
275
|
vertical-align: text-bottom;
|
|
246
|
-
background: var(--caret-color, currentColor);
|
|
276
|
+
background: var(--caret-color, var(--typewriter-caret-fg, currentColor));
|
|
247
277
|
animation: typewriter-blink var(--caret-blink, 1s) step-end infinite;
|
|
248
278
|
}
|
|
249
279
|
|
|
250
|
-
@media (prefers-reduced-motion: reduce) {
|
|
251
|
-
.typewriter-caret,
|
|
252
|
-
:global(.typewriter-unit.typewriter-caret)::before {
|
|
253
|
-
animation: none;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
280
|
@keyframes typewriter-blink {
|
|
258
281
|
50% {
|
|
259
282
|
opacity: 0;
|
package/Typewriter.svelte.d.ts
CHANGED
|
@@ -20,6 +20,15 @@ export type TypewriterProps = HTMLAttributes<HTMLPreElement> & {
|
|
|
20
20
|
*/
|
|
21
21
|
play?: boolean;
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Reveal-progress curve: maps elapsed-time fraction (0-1) to
|
|
25
|
+
* revealed-fraction (0-1). Total typing duration is always
|
|
26
|
+
* `speed * <visible character count>` regardless of curve. Import a named
|
|
27
|
+
* curve (`easeOutQuad`, `easeInOutCubic`, ...) or pass your own function.
|
|
28
|
+
* @default linear
|
|
29
|
+
*/
|
|
30
|
+
easing?: (t: number) => number;
|
|
31
|
+
|
|
23
32
|
/**
|
|
24
33
|
* Width of the blinking caret.
|
|
25
34
|
* @default "0.6em"
|
|
@@ -53,7 +62,7 @@ export type TypewriterProps = HTMLAttributes<HTMLPreElement> & {
|
|
|
53
62
|
|
|
54
63
|
export type TypewriterEvents = {
|
|
55
64
|
/**
|
|
56
|
-
* Fires when typing finishes
|
|
65
|
+
* Fires when typing finishes.
|
|
57
66
|
*/
|
|
58
67
|
done: CustomEvent<null>;
|
|
59
68
|
};
|
package/ansi-color.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure color math for AnsiOutput: SGR color to CSS, and WCAG contrast-based
|
|
3
|
+
* auto-foreground selection. Split out of the component (rather than left
|
|
4
|
+
* inline in its `<script>`) so it's directly testable and benchable without
|
|
5
|
+
* a DOM - .svelte files aren't type-checked by `tsgo`/covered by `bun test`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** @typedef {import("./ansi.d.ts").AnsiColor} AnsiColor */
|
|
9
|
+
/** @typedef {import("./ansi.d.ts").AnsiSegment} AnsiSegment */
|
|
10
|
+
|
|
11
|
+
export const FOREGROUND_FALLBACK = "#d4d4d4";
|
|
12
|
+
export const CONTRAST_TARGET = 4.5;
|
|
13
|
+
/** @type {[number, number, number]} */
|
|
14
|
+
const BLACK = [0, 0, 0];
|
|
15
|
+
/** @type {[number, number, number]} */
|
|
16
|
+
const WHITE = [255, 255, 255];
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 16-color xterm defaults, each overridable via `--ansi-<name>`.
|
|
20
|
+
* @type {Record<string, string>}
|
|
21
|
+
*/
|
|
22
|
+
export const ANSI_COLOR_DEFAULTS = {
|
|
23
|
+
black: "#000000",
|
|
24
|
+
red: "#cd0000",
|
|
25
|
+
green: "#00cd00",
|
|
26
|
+
yellow: "#cdcd00",
|
|
27
|
+
blue: "#0000ee",
|
|
28
|
+
magenta: "#cd00cd",
|
|
29
|
+
cyan: "#00cdcd",
|
|
30
|
+
white: "#e5e5e5",
|
|
31
|
+
"bright-black": "#7f7f7f",
|
|
32
|
+
"bright-red": "#ff0000",
|
|
33
|
+
"bright-green": "#00ff00",
|
|
34
|
+
"bright-yellow": "#ffff00",
|
|
35
|
+
"bright-blue": "#5c5cff",
|
|
36
|
+
"bright-magenta": "#ff00ff",
|
|
37
|
+
"bright-cyan": "#00ffff",
|
|
38
|
+
"bright-white": "#ffffff",
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// 256-color cube steps (6x6x6).
|
|
42
|
+
const CUBE = [0, 95, 135, 175, 215, 255];
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 256-color index to an RGB triple. Shared by `indexedHex` (stringifies it
|
|
46
|
+
* for CSS) and `colorToRgb` (used as-is) so the latter doesn't have to
|
|
47
|
+
* round-trip through a hex string just to parse the numbers back out.
|
|
48
|
+
* @param {number} index
|
|
49
|
+
* @returns {[number, number, number]}
|
|
50
|
+
*/
|
|
51
|
+
function indexedRgb(index) {
|
|
52
|
+
if (index >= 232) {
|
|
53
|
+
const value = (index - 232) * 10 + 8;
|
|
54
|
+
return [value, value, value];
|
|
55
|
+
}
|
|
56
|
+
const n = index - 16;
|
|
57
|
+
// n is always in [0, 215] for index in [16, 231], so these three indices
|
|
58
|
+
// are always in CUBE's bounds ([0, 5]).
|
|
59
|
+
return [
|
|
60
|
+
/** @type {number} */ (CUBE[Math.floor(n / 36) % 6]),
|
|
61
|
+
/** @type {number} */ (CUBE[Math.floor(n / 6) % 6]),
|
|
62
|
+
/** @type {number} */ (CUBE[n % 6]),
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 256-color index to hex.
|
|
68
|
+
* @param {number} index
|
|
69
|
+
* @returns {string}
|
|
70
|
+
*/
|
|
71
|
+
export function indexedHex(index) {
|
|
72
|
+
const [r, g, b] = indexedRgb(index);
|
|
73
|
+
return `#${[r, g, b].map((c) => c.toString(16).padStart(2, "0")).join("")}`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Parsed color to CSS (named colors use `--ansi-*` vars).
|
|
78
|
+
* @param {AnsiColor} color
|
|
79
|
+
* @returns {string}
|
|
80
|
+
*/
|
|
81
|
+
export function cssColor(color) {
|
|
82
|
+
if ("name" in color) {
|
|
83
|
+
return `var(--ansi-${color.name}, ${ANSI_COLOR_DEFAULTS[color.name]})`;
|
|
84
|
+
}
|
|
85
|
+
if ("rgb" in color) {
|
|
86
|
+
return `rgb(${color.rgb[0]}, ${color.rgb[1]}, ${color.rgb[2]})`;
|
|
87
|
+
}
|
|
88
|
+
return indexedHex(color.index);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @param {string} hex A `#rrggbb` string.
|
|
93
|
+
* @returns {[number, number, number]}
|
|
94
|
+
*/
|
|
95
|
+
export function hexToRgb(hex) {
|
|
96
|
+
const value = hex.replace("#", "");
|
|
97
|
+
return [
|
|
98
|
+
Number.parseInt(value.slice(0, 2), 16),
|
|
99
|
+
Number.parseInt(value.slice(2, 4), 16),
|
|
100
|
+
Number.parseInt(value.slice(4, 6), 16),
|
|
101
|
+
];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Named colors use the default palette (theme overrides aren't known here).
|
|
106
|
+
* @param {AnsiColor} color
|
|
107
|
+
* @returns {[number, number, number]}
|
|
108
|
+
*/
|
|
109
|
+
export function colorToRgb(color) {
|
|
110
|
+
if ("name" in color) {
|
|
111
|
+
return hexToRgb(ANSI_COLOR_DEFAULTS[color.name] ?? "#000000");
|
|
112
|
+
}
|
|
113
|
+
if ("rgb" in color) return color.rgb;
|
|
114
|
+
return indexedRgb(color.index);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* WCAG relative luminance.
|
|
119
|
+
* @param {[number, number, number]} rgb
|
|
120
|
+
* @returns {number}
|
|
121
|
+
*/
|
|
122
|
+
export function luminance(rgb) {
|
|
123
|
+
const toLinear = (/** @type {number} */ c) => {
|
|
124
|
+
const v = c / 255;
|
|
125
|
+
return v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
|
|
126
|
+
};
|
|
127
|
+
return (
|
|
128
|
+
0.2126 * toLinear(rgb[0]) +
|
|
129
|
+
0.7152 * toLinear(rgb[1]) +
|
|
130
|
+
0.0722 * toLinear(rgb[2])
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* WCAG contrast ratio.
|
|
136
|
+
* @param {[number, number, number]} a
|
|
137
|
+
* @param {[number, number, number]} b
|
|
138
|
+
* @returns {number}
|
|
139
|
+
*/
|
|
140
|
+
export function contrastRatio(a, b) {
|
|
141
|
+
const la = luminance(a);
|
|
142
|
+
const lb = luminance(b);
|
|
143
|
+
return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Black or white, whichever contrasts more with `bg`.
|
|
148
|
+
* @param {[number, number, number]} bg
|
|
149
|
+
* @returns {string}
|
|
150
|
+
*/
|
|
151
|
+
export function readableForeground(bg) {
|
|
152
|
+
return contrastRatio(BLACK, bg) >= contrastRatio(WHITE, bg)
|
|
153
|
+
? "#000000"
|
|
154
|
+
: "#ffffff";
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Foreground CSS, with auto-contrast override when needed.
|
|
159
|
+
* @param {AnsiSegment} segment
|
|
160
|
+
* @param {boolean} autoContrast
|
|
161
|
+
* @returns {string | undefined}
|
|
162
|
+
*/
|
|
163
|
+
export function foregroundCss(segment, autoContrast) {
|
|
164
|
+
// Concealed text is rendered transparent (layout and copy text stay).
|
|
165
|
+
if (segment.conceal) return "transparent";
|
|
166
|
+
if (autoContrast && segment.bg) {
|
|
167
|
+
const bg = colorToRgb(segment.bg);
|
|
168
|
+
const fg = segment.fg
|
|
169
|
+
? colorToRgb(segment.fg)
|
|
170
|
+
: hexToRgb(FOREGROUND_FALLBACK);
|
|
171
|
+
if (contrastRatio(fg, bg) < CONTRAST_TARGET) return readableForeground(bg);
|
|
172
|
+
}
|
|
173
|
+
return segment.fg ? cssColor(segment.fg) : undefined;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @param {AnsiSegment} segment
|
|
178
|
+
* @returns {string | undefined}
|
|
179
|
+
*/
|
|
180
|
+
export function classNames(segment) {
|
|
181
|
+
const names = [];
|
|
182
|
+
if (segment.bold) names.push("bold");
|
|
183
|
+
if (segment.dim) names.push("dim");
|
|
184
|
+
if (segment.italic) names.push("italic");
|
|
185
|
+
if (segment.underline) names.push("underline");
|
|
186
|
+
if (segment.strikethrough) names.push("strikethrough");
|
|
187
|
+
return names.length ? names.join(" ") : undefined;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* @param {AnsiSegment} segment
|
|
192
|
+
* @param {boolean} autoContrast
|
|
193
|
+
* @returns {string | undefined}
|
|
194
|
+
*/
|
|
195
|
+
export function inlineStyle(segment, autoContrast) {
|
|
196
|
+
let style = "";
|
|
197
|
+
if (segment.bg) style += `background-color:${cssColor(segment.bg)};`;
|
|
198
|
+
const fg = foregroundCss(segment, autoContrast);
|
|
199
|
+
if (fg) style += `color:${fg};`;
|
|
200
|
+
return style || undefined;
|
|
201
|
+
}
|
package/editable-dom-paint.js
CHANGED
|
@@ -133,9 +133,10 @@ export function createDomLinePainter({ registry }) {
|
|
|
133
133
|
openScopes,
|
|
134
134
|
pendingHtml,
|
|
135
135
|
);
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
136
|
+
// push, not concat: concat copies the whole (ever-growing) array on
|
|
137
|
+
// every call, which is O(lines) per completed line - push is O(1)
|
|
138
|
+
// amortized (same reasoning as HighlightStream's sealed-chunk append).
|
|
139
|
+
for (const line of result.completedLines) completedLines.push(line);
|
|
139
140
|
openScopes = result.openScopes;
|
|
140
141
|
pendingHtml = result.pendingHtml;
|
|
141
142
|
renderedEventCount = committed.length;
|
package/engine.js
CHANGED
|
@@ -61,13 +61,13 @@
|
|
|
61
61
|
* Memoized result of scanning a single rule/frame's pattern: `match` is the
|
|
62
62
|
* earliest guard-passing match at or after the position `this.pos` had when
|
|
63
63
|
* it was computed (or `null` if none exists in `this.code` as of `codeLen`
|
|
64
|
-
* characters). See `Tokenizer#
|
|
64
|
+
* characters). See `Tokenizer#nextMatch`'s cache-hit checks.
|
|
65
65
|
* @typedef {{ codeLen: number, match: RegExpExecArray | null }} MatchCache
|
|
66
66
|
*/
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
69
|
* One entry of the tokenizer's parse stack. `endCache` memoizes this
|
|
70
|
-
* frame's end-pattern scan (see `
|
|
70
|
+
* frame's end-pattern scan (see `nextMatch`); it's per-frame rather than
|
|
71
71
|
* per-state because `endSameAsBegin`'s guard depends on this frame's own
|
|
72
72
|
* `beginMatch`, and two frames can share the same state (recursive nesting).
|
|
73
73
|
* `beginPos` is this frame's begin match's position in `this.code` (see
|
|
@@ -124,14 +124,26 @@ export const TEXT = 0;
|
|
|
124
124
|
export const OPEN = 1;
|
|
125
125
|
export const CLOSE = 2;
|
|
126
126
|
|
|
127
|
-
|
|
127
|
+
const HTML_ESCAPE_RE = /[&<>"']/g;
|
|
128
|
+
/** @type {Record<string, string>} */
|
|
129
|
+
const HTML_ESCAPE_MAP = {
|
|
130
|
+
"&": "&",
|
|
131
|
+
"<": "<",
|
|
132
|
+
">": ">",
|
|
133
|
+
'"': """,
|
|
134
|
+
"'": "'",
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Five sequential `.replace()` passes (one per character) previously cost
|
|
139
|
+
* ~87% of renderHtml's time, since every TEXT event runs through this. A
|
|
140
|
+
* single regex pass, skipped entirely when there's nothing to escape, cuts
|
|
141
|
+
* that to a fraction (see bench/render.bench.ts).
|
|
142
|
+
* @param {string} value
|
|
143
|
+
*/
|
|
128
144
|
export function escapeHtml(value) {
|
|
129
|
-
return value
|
|
130
|
-
|
|
131
|
-
.replace(/</g, "<")
|
|
132
|
-
.replace(/>/g, ">")
|
|
133
|
-
.replace(/"/g, """)
|
|
134
|
-
.replace(/'/g, "'");
|
|
145
|
+
if (!HTML_ESCAPE_RE.test(value)) return value;
|
|
146
|
+
return value.replace(HTML_ESCAPE_RE, (char) => HTML_ESCAPE_MAP[char] ?? char);
|
|
135
147
|
}
|
|
136
148
|
|
|
137
149
|
/**
|
|
@@ -205,7 +217,7 @@ class Tokenizer {
|
|
|
205
217
|
this.subContinuations = Object.create(null);
|
|
206
218
|
/**
|
|
207
219
|
* Memoized begin-pattern scans, indexed by state index (see
|
|
208
|
-
* `
|
|
220
|
+
* `nextMatch`). A rule's "next match" is independent of which parent
|
|
209
221
|
* state referenced it, so this is shared across all of them rather than
|
|
210
222
|
* being per-frame like `Frame#endCache`.
|
|
211
223
|
* @type {(MatchCache | undefined)[]}
|
|
@@ -416,27 +428,17 @@ class Tokenizer {
|
|
|
416
428
|
}
|
|
417
429
|
|
|
418
430
|
/**
|
|
419
|
-
*
|
|
431
|
+
* Whether `cache` is still valid at `this.pos`: without this, every token
|
|
420
432
|
* boundary re-scans every rule's pattern from scratch. A cached match stays
|
|
421
433
|
* valid until `this.pos` passes it; a cached miss stays valid until the
|
|
422
|
-
* code grows (streaming append).
|
|
423
|
-
* so guarded rules don't allocate a fresh guard closure on every call once
|
|
424
|
-
* their match is cached.
|
|
425
|
-
* @param {RegExp} re
|
|
426
|
-
* @param {() => ((m: RegExpExecArray) => boolean) | null} guardFn
|
|
434
|
+
* code grows (streaming append).
|
|
427
435
|
* @param {MatchCache | undefined} cache
|
|
428
|
-
* @returns {MatchCache}
|
|
436
|
+
* @returns {cache is MatchCache}
|
|
429
437
|
*/
|
|
430
|
-
|
|
431
|
-
if (cache)
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
return cache;
|
|
435
|
-
}
|
|
436
|
-
return {
|
|
437
|
-
codeLen: this.code.length,
|
|
438
|
-
match: this.execValid(re, this.pos, guardFn()),
|
|
439
|
-
};
|
|
438
|
+
isCacheValid(cache) {
|
|
439
|
+
if (!cache) return false;
|
|
440
|
+
if (cache.match !== null && cache.match.index >= this.pos) return true;
|
|
441
|
+
return cache.match === null && cache.codeLen === this.code.length;
|
|
440
442
|
}
|
|
441
443
|
|
|
442
444
|
/**
|
|
@@ -547,13 +549,23 @@ class Tokenizer {
|
|
|
547
549
|
// rules[i] is in-bounds by the loop condition.
|
|
548
550
|
const ruleIdx = /** @type {number} */ (state.rules[i]);
|
|
549
551
|
const child = /** @type {CompiledState} */ (this.program.states[ruleIdx]);
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
)
|
|
556
|
-
|
|
552
|
+
let cache = this.beginCache[ruleIdx];
|
|
553
|
+
// Guard closures (this.beginGuard(child)) are only built on a cache
|
|
554
|
+
// miss - passing them in as a pre-built thunk, as this used to via a
|
|
555
|
+
// shared cachedMatch() helper, allocated one on every call regardless
|
|
556
|
+
// of hit/miss, since JS evaluates argument expressions eagerly.
|
|
557
|
+
if (!this.isCacheValid(cache)) {
|
|
558
|
+
cache = {
|
|
559
|
+
codeLen: this.code.length,
|
|
560
|
+
match: this.execValid(
|
|
561
|
+
// Every state referenced from a `rules` list has a `begin` pattern.
|
|
562
|
+
/** @type {RegExp} */ (child.beginRe),
|
|
563
|
+
this.pos,
|
|
564
|
+
this.beginGuard(child),
|
|
565
|
+
),
|
|
566
|
+
};
|
|
567
|
+
this.beginCache[ruleIdx] = cache;
|
|
568
|
+
}
|
|
557
569
|
consider(cache.match, i, "begin", ruleIdx);
|
|
558
570
|
}
|
|
559
571
|
// End candidates walk outward while `endsWithParent` chains allow.
|
|
@@ -562,15 +574,21 @@ class Tokenizer {
|
|
|
562
574
|
// d ranges over [1, frames.length - 1], always in bounds.
|
|
563
575
|
const frame = /** @type {Frame} */ (this.frames[d]);
|
|
564
576
|
if (frame.state.endRe) {
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
577
|
+
let cache = frame.endCache;
|
|
578
|
+
if (!this.isCacheValid(cache)) {
|
|
579
|
+
cache = {
|
|
580
|
+
codeLen: this.code.length,
|
|
581
|
+
match: this.execValid(
|
|
582
|
+
/** @type {RegExp} */ (frame.state.endRe),
|
|
583
|
+
this.pos,
|
|
584
|
+
frame.state.endSameAsBegin
|
|
585
|
+
? (/** @type {RegExpExecArray} */ m) =>
|
|
586
|
+
m[1] === frame.beginMatch
|
|
587
|
+
: null,
|
|
588
|
+
),
|
|
589
|
+
};
|
|
590
|
+
frame.endCache = cache;
|
|
591
|
+
}
|
|
574
592
|
consider(cache.match, pri++, "end", d);
|
|
575
593
|
}
|
|
576
594
|
if (!frame.state.endsWithParent) break;
|
package/index.d.ts
CHANGED
|
@@ -25,3 +25,15 @@ export {
|
|
|
25
25
|
} from "./scoped";
|
|
26
26
|
export { default as Typewriter } from "./Typewriter.svelte";
|
|
27
27
|
export type { ThemePalette } from "./theme";
|
|
28
|
+
export {
|
|
29
|
+
easeInCubic,
|
|
30
|
+
easeInOutCubic,
|
|
31
|
+
easeInOutQuad,
|
|
32
|
+
easeInOutSine,
|
|
33
|
+
easeInQuad,
|
|
34
|
+
easeInSine,
|
|
35
|
+
easeOutCubic,
|
|
36
|
+
easeOutQuad,
|
|
37
|
+
easeOutSine,
|
|
38
|
+
linear,
|
|
39
|
+
} from "./typewriter-easing";
|
package/index.js
CHANGED
|
@@ -21,3 +21,15 @@ export {
|
|
|
21
21
|
scopeStyle,
|
|
22
22
|
} from "./scoped.js";
|
|
23
23
|
export { default as Typewriter } from "./Typewriter.svelte";
|
|
24
|
+
export {
|
|
25
|
+
easeInCubic,
|
|
26
|
+
easeInOutCubic,
|
|
27
|
+
easeInOutQuad,
|
|
28
|
+
easeInOutSine,
|
|
29
|
+
easeInQuad,
|
|
30
|
+
easeInSine,
|
|
31
|
+
easeOutCubic,
|
|
32
|
+
easeOutQuad,
|
|
33
|
+
easeOutSine,
|
|
34
|
+
linear,
|
|
35
|
+
} from "./typewriter-easing.js";
|
package/package.json
CHANGED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sealed-chunk bookkeeping for HighlightStream's default (non-virtualized)
|
|
3
|
+
* engine: once `SEAL_CHUNK_LINES` completed lines accumulate, they're
|
|
4
|
+
* folded into one immutable HTML string and never touched again, so keyed
|
|
5
|
+
* `{#each sealedChunks}` reconciliation only ever diffs the (constant-size)
|
|
6
|
+
* unsealed tail, not the whole stream.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Joins `lines` (already-highlighted line HTML, one entry per line) into one
|
|
11
|
+
* `<span class="highlight-stream-line" data-line="i">` per line, separated
|
|
12
|
+
* by the newlines a real multi-line block needs.
|
|
13
|
+
* @param {string[]} lines
|
|
14
|
+
* @param {number} startLine Index of `lines[0]` in the overall document.
|
|
15
|
+
* @returns {string}
|
|
16
|
+
*/
|
|
17
|
+
export function buildSealedChunkHtml(lines, startLine) {
|
|
18
|
+
if (lines.length === 0) return "";
|
|
19
|
+
// Only the very first line of the whole document (index 0) skips a
|
|
20
|
+
// leading separator; every other line - including this chunk's own first
|
|
21
|
+
// line, when it isn't the document's first - gets one. Peeling that one
|
|
22
|
+
// check out of the loop avoids re-evaluating it every iteration.
|
|
23
|
+
let html = `<span class="highlight-stream-line" data-line="${startLine}">${lines[0]}</span>`;
|
|
24
|
+
for (let i = 1; i < lines.length; i++) {
|
|
25
|
+
const lineIndex = startLine + i;
|
|
26
|
+
html += `\n<span class="highlight-stream-line" data-line="${lineIndex}">${lines[i]}</span>`;
|
|
27
|
+
}
|
|
28
|
+
return startLine > 0 ? `\n${html}` : html;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Appends `chunk` to `chunks` in place and returns the same array reference
|
|
33
|
+
* (Svelte's `x = x` self-assign invalidation idiom relies on referential
|
|
34
|
+
* equality staying stable so a keyed each-block doesn't rebuild). O(1)
|
|
35
|
+
* amortized, not O(c): a concat/spread-copy here would make sealing chunk
|
|
36
|
+
* `c` of a growing stream cost O(c), i.e. O(c^2) total across the stream.
|
|
37
|
+
* @param {string[]} chunks
|
|
38
|
+
* @param {string} chunk
|
|
39
|
+
* @returns {string[]}
|
|
40
|
+
*/
|
|
41
|
+
export function pushSealedChunk(chunks, chunk) {
|
|
42
|
+
chunks.push(chunk);
|
|
43
|
+
return chunks;
|
|
44
|
+
}
|
package/text-diff.js
CHANGED
|
@@ -24,15 +24,23 @@ function isLowSurrogate(code) {
|
|
|
24
24
|
export function diffText(before, after) {
|
|
25
25
|
const minLength = Math.min(before.length, after.length);
|
|
26
26
|
|
|
27
|
+
// charCodeAt (not bracket indexing) so this loop - which walks the entire
|
|
28
|
+
// unchanged prefix on a pure-append edit, the common case while typing -
|
|
29
|
+
// doesn't allocate a 1-character string per position compared.
|
|
27
30
|
let prefix = 0;
|
|
28
|
-
while (
|
|
31
|
+
while (
|
|
32
|
+
prefix < minLength &&
|
|
33
|
+
before.charCodeAt(prefix) === after.charCodeAt(prefix)
|
|
34
|
+
)
|
|
35
|
+
prefix++;
|
|
29
36
|
if (isHighSurrogate(before.charCodeAt(prefix - 1))) prefix--;
|
|
30
37
|
|
|
31
38
|
let suffix = 0;
|
|
32
39
|
const maxSuffix = minLength - prefix;
|
|
33
40
|
while (
|
|
34
41
|
suffix < maxSuffix &&
|
|
35
|
-
before
|
|
42
|
+
before.charCodeAt(before.length - 1 - suffix) ===
|
|
43
|
+
after.charCodeAt(after.length - 1 - suffix)
|
|
36
44
|
) {
|
|
37
45
|
suffix++;
|
|
38
46
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Constant rate; `Typewriter`'s default. */
|
|
2
|
+
export declare function linear(t: number): number;
|
|
3
|
+
|
|
4
|
+
export declare function easeInQuad(t: number): number;
|
|
5
|
+
export declare function easeOutQuad(t: number): number;
|
|
6
|
+
export declare function easeInOutQuad(t: number): number;
|
|
7
|
+
|
|
8
|
+
export declare function easeInCubic(t: number): number;
|
|
9
|
+
export declare function easeOutCubic(t: number): number;
|
|
10
|
+
export declare function easeInOutCubic(t: number): number;
|
|
11
|
+
|
|
12
|
+
export declare function easeInSine(t: number): number;
|
|
13
|
+
export declare function easeOutSine(t: number): number;
|
|
14
|
+
export declare function easeInOutSine(t: number): number;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reveal-progress curves for `Typewriter`'s `easing` prop: each maps an
|
|
3
|
+
* elapsed-time fraction (0 to 1) to a revealed-fraction (0 to 1). Total
|
|
4
|
+
* typing duration is always `speed * <visible character count>` regardless
|
|
5
|
+
* of which curve is used -- only the pacing within that duration changes.
|
|
6
|
+
* Formulas from easings.net.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** @param {number} t @returns {number} */
|
|
10
|
+
export function linear(t) {
|
|
11
|
+
return t;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** @param {number} t @returns {number} */
|
|
15
|
+
export function easeInQuad(t) {
|
|
16
|
+
return t * t;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @param {number} t @returns {number} */
|
|
20
|
+
export function easeOutQuad(t) {
|
|
21
|
+
return 1 - (1 - t) * (1 - t);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @param {number} t @returns {number} */
|
|
25
|
+
export function easeInOutQuad(t) {
|
|
26
|
+
return t < 0.5 ? 2 * t * t : 1 - (-2 * t + 2) ** 2 / 2;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** @param {number} t @returns {number} */
|
|
30
|
+
export function easeInCubic(t) {
|
|
31
|
+
return t * t * t;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** @param {number} t @returns {number} */
|
|
35
|
+
export function easeOutCubic(t) {
|
|
36
|
+
return 1 - (1 - t) ** 3;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** @param {number} t @returns {number} */
|
|
40
|
+
export function easeInOutCubic(t) {
|
|
41
|
+
return t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @param {number} t @returns {number} */
|
|
45
|
+
export function easeInSine(t) {
|
|
46
|
+
return 1 - Math.cos((t * Math.PI) / 2);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @param {number} t @returns {number} */
|
|
50
|
+
export function easeOutSine(t) {
|
|
51
|
+
return Math.sin((t * Math.PI) / 2);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** @param {number} t @returns {number} */
|
|
55
|
+
export function easeInOutSine(t) {
|
|
56
|
+
return -(Math.cos(Math.PI * t) - 1) / 2;
|
|
57
|
+
}
|
package/typewriter-units.d.ts
CHANGED
|
@@ -12,6 +12,12 @@ export interface TypewriterUnit {
|
|
|
12
12
|
*/
|
|
13
13
|
export declare function tokenizeTypewriter(html: string): TypewriterUnit[];
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Renders `units` into HTML once: tags pass through unchanged, each visible
|
|
17
|
+
* unit is wrapped in a `typewriter-unit typewriter-hidden` span.
|
|
18
|
+
*/
|
|
19
|
+
export declare function buildUnitMarkup(units: TypewriterUnit[]): string;
|
|
20
|
+
|
|
15
21
|
export interface TypewriterSplitter {
|
|
16
22
|
splitAt(count: number): { head: string; tail: string };
|
|
17
23
|
}
|
package/typewriter-units.js
CHANGED
|
@@ -25,9 +25,13 @@ export function tokenizeTypewriter(html) {
|
|
|
25
25
|
let i = 0;
|
|
26
26
|
|
|
27
27
|
while (i < n) {
|
|
28
|
-
|
|
28
|
+
// charCodeAt, not html[i]: this runs once per iteration over the whole
|
|
29
|
+
// string, and bracket-indexing allocates a 1-character string for every
|
|
30
|
+
// position compared, even the (overwhelmingly common) plain-char branch
|
|
31
|
+
// below that doesn't otherwise need one.
|
|
32
|
+
const code = html.charCodeAt(i);
|
|
29
33
|
|
|
30
|
-
if (
|
|
34
|
+
if (code === 60 /* "<" */) {
|
|
31
35
|
const end = html.indexOf(">", i);
|
|
32
36
|
if (end === -1) {
|
|
33
37
|
// Unclosed tag: treat as text.
|
|
@@ -49,14 +53,14 @@ export function tokenizeTypewriter(html) {
|
|
|
49
53
|
name: match ? (match[1] ?? "") : "",
|
|
50
54
|
});
|
|
51
55
|
i = end + 1;
|
|
52
|
-
} else if (
|
|
56
|
+
} else if (code === 38 /* "&" */) {
|
|
53
57
|
// One visible char per entity.
|
|
54
58
|
const end = html.indexOf(";", i);
|
|
55
59
|
if (end !== -1 && end - i <= 10) {
|
|
56
60
|
units.push({ raw: html.slice(i, end + 1), visible: 1 });
|
|
57
61
|
i = end + 1;
|
|
58
62
|
} else {
|
|
59
|
-
units.push({ raw:
|
|
63
|
+
units.push({ raw: html.charAt(i), visible: 1 });
|
|
60
64
|
i += 1;
|
|
61
65
|
}
|
|
62
66
|
} else {
|
|
@@ -72,6 +76,25 @@ export function tokenizeTypewriter(html) {
|
|
|
72
76
|
return units;
|
|
73
77
|
}
|
|
74
78
|
|
|
79
|
+
/**
|
|
80
|
+
* Renders `units` into HTML once: tags pass through unchanged, each visible
|
|
81
|
+
* unit is wrapped in a `typewriter-unit typewriter-hidden` span so
|
|
82
|
+
* `Typewriter` can reveal them one at a time without touching the DOM tree
|
|
83
|
+
* shape again until `units` itself changes.
|
|
84
|
+
* @param {TypewriterUnit[]} units
|
|
85
|
+
* @returns {string}
|
|
86
|
+
*/
|
|
87
|
+
export function buildUnitMarkup(units) {
|
|
88
|
+
let html = "";
|
|
89
|
+
for (const unit of units) {
|
|
90
|
+
html +=
|
|
91
|
+
unit.visible === 0
|
|
92
|
+
? unit.raw
|
|
93
|
+
: `<span class="typewriter-unit typewriter-hidden">${unit.raw}</span>`;
|
|
94
|
+
}
|
|
95
|
+
return html;
|
|
96
|
+
}
|
|
97
|
+
|
|
75
98
|
/**
|
|
76
99
|
* @typedef {Object} TypewriterSplitter
|
|
77
100
|
* @property {(count: number) => { head: string; tail: string }} splitAt
|