@motionscript/code 0.1.0-alpha.0 → 0.1.0-alpha.10
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/CHANGELOG.md +96 -0
- package/NOTICE +49 -0
- package/README.md +148 -50
- package/dist/browser/THIRD_PARTY_LICENSES.txt +431 -0
- package/dist/browser/index.js +10 -7
- package/dist/browser/index.js.map +3 -3
- package/dist/browser/manifest.json +85 -3
- package/dist/code-range.d.ts +7 -1
- package/dist/code-range.d.ts.map +1 -1
- package/dist/code-range.js +9 -1
- package/dist/code-range.js.map +1 -1
- package/dist/diff.d.ts +14 -0
- package/dist/diff.d.ts.map +1 -1
- package/dist/diff.js +134 -2
- package/dist/diff.js.map +1 -1
- package/dist/engine.d.ts +2 -2
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +4 -4
- package/dist/engine.js.map +1 -1
- package/dist/highlight.d.ts +10 -2
- package/dist/highlight.d.ts.map +1 -1
- package/dist/highlight.js +40 -6
- package/dist/highlight.js.map +1 -1
- package/dist/layout.d.ts +1 -1
- package/dist/layout.d.ts.map +1 -1
- package/dist/layout.js.map +1 -1
- package/dist/measure-cache.d.ts +2 -2
- package/dist/measure-cache.d.ts.map +1 -1
- package/dist/measure-cache.js +0 -0
- package/dist/measure-cache.js.map +1 -1
- package/dist/node.d.ts +275 -281
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +634 -700
- package/dist/node.js.map +1 -1
- package/dist/nodes.d.ts +6 -9
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +7 -9
- package/dist/nodes.js.map +1 -1
- package/dist/render.d.ts +10 -3
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +60 -1
- package/dist/render.js.map +1 -1
- package/dist/style.d.ts +3 -3
- package/dist/style.d.ts.map +1 -1
- package/dist/style.js +10 -6
- package/dist/style.js.map +1 -1
- package/dist/tokens.d.ts.map +1 -1
- package/dist/tokens.js +5 -1
- package/dist/tokens.js.map +1 -1
- package/dist/transitions.d.ts +19 -1
- package/dist/transitions.d.ts.map +1 -1
- package/dist/transitions.js.map +1 -1
- package/package.json +7 -4
- package/src/code-range.ts +10 -1
- package/src/diff.ts +137 -2
- package/src/engine.ts +5 -5
- package/src/highlight.ts +54 -5
- package/src/layout.ts +1 -1
- package/src/measure-cache.ts +0 -0
- package/src/node.ts +767 -747
- package/src/nodes.ts +8 -10
- package/src/render.ts +71 -3
- package/src/style.ts +11 -7
- package/src/tokens.ts +5 -1
- package/src/transitions.ts +21 -1
package/dist/node.js
CHANGED
|
@@ -1,722 +1,519 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
1
|
+
import { Graphics2D, Paint, Types, component2d, lerpNumber, } from "@motionscript/sdk";
|
|
2
|
+
import { DefaultHighlightStyle, TokenAdvanceCache, canHighlight, charOffsetsToRange, defaultCodeDiff, defaultCodePhases, drawCode, ensureHighlighter, layoutCode, makeAnim, metricsSignature, moveProgress, normalizeSource, rangeToCharOffsets, resolveTokenStates, tokenizeCodeToIdLines, } from "./engine.js";
|
|
3
|
+
/** A row band's paint and corner radius when `highlightRows` is given neither. */
|
|
4
|
+
const ROW_BAND_COLOR = "rgba(255, 255, 255, 0.12)";
|
|
5
|
+
const ROW_BAND_RADIUS = 6;
|
|
6
|
+
/** Each `Code` component's strategies and edit cache, by the node class it generated. */
|
|
7
|
+
const kits = new WeakMap();
|
|
8
|
+
const NO_FOCUS = { dim: null, ids: new Set(), fade: null };
|
|
9
|
+
const NO_BANDING = { bands: [], fade: null };
|
|
10
|
+
/** A private entry: commands write it, documents and calls never see it. */
|
|
11
|
+
function own(initial) {
|
|
12
|
+
return Types.custom(initial, { private: true });
|
|
13
|
+
}
|
|
14
|
+
const TARGET = Types.custom(undefined, { kind: "selector" });
|
|
15
|
+
const TEXT = Types.text("", { multiline: true });
|
|
16
|
+
/**
|
|
17
|
+
* A `Code` whose edits are paired and timed by `strategies`. {@link Code} is
|
|
18
|
+
* the one with the defaults; a project wanting another hands its own to the
|
|
19
|
+
* engine in its place (the key is the same).
|
|
20
|
+
*
|
|
21
|
+
* A listing and nothing else: no paint slots, and no box behind the tokens. A
|
|
22
|
+
* listing set on a card is a `Code` inside a hugging `Rect`.
|
|
23
|
+
*/
|
|
24
|
+
export function defineCode(strategies = {}) {
|
|
25
|
+
const kit = {
|
|
26
|
+
diff: strategies.diffStrategy ?? defaultCodeDiff,
|
|
27
|
+
phases: strategies.phaseStrategy ?? defaultCodePhases,
|
|
28
|
+
edits: new WeakMap(),
|
|
29
|
+
};
|
|
30
|
+
const act = (timeline, frame) => (frame ? timeline.animate(frame) : timeline.wait(0));
|
|
31
|
+
const component = component2d({
|
|
32
|
+
key: "code",
|
|
33
|
+
state: {
|
|
34
|
+
code: Types.custom("", { lerp: blendSource }),
|
|
35
|
+
language: "typescript",
|
|
36
|
+
fontFamily: "Fira Mono",
|
|
37
|
+
/** A built-in or registered theme name (e.g. `'github-dark'`), or a `CodeHighlightStyle` object. */
|
|
38
|
+
theme: Types.custom(DefaultHighlightStyle.name),
|
|
39
|
+
fontSize: 16,
|
|
40
|
+
lineHeight: 1.6,
|
|
41
|
+
// Extra space after every glyph, in px: folded into the advance a token is measured at.
|
|
42
|
+
letterSpacing: 1.1,
|
|
43
|
+
showLineNumbers: false,
|
|
44
|
+
// In space-widths, so it scales with fontSize.
|
|
45
|
+
lineNumberGap: 2,
|
|
46
|
+
listing: own(null),
|
|
47
|
+
edit: own(null),
|
|
48
|
+
focus: own(NO_FOCUS),
|
|
49
|
+
banding: own(NO_BANDING),
|
|
50
|
+
},
|
|
51
|
+
defaults: { width: "hug", height: "hug" },
|
|
52
|
+
allowedChildren: [],
|
|
53
|
+
// The grammar gates layout: how many tokens there are, and so where each sits, depends on it.
|
|
54
|
+
load: (loader, state) => {
|
|
55
|
+
loader.font(state.fontFamily);
|
|
56
|
+
loader.task(`shiki:lang:${state.language}`, grammarLoader(state.language), { before: "layout" });
|
|
57
|
+
},
|
|
58
|
+
commands: {
|
|
59
|
+
/** Append `code` to the end of the listing. */
|
|
60
|
+
append: {
|
|
61
|
+
args: { code: TEXT },
|
|
62
|
+
run: (timeline, state) => {
|
|
63
|
+
const seen = current(state, kit);
|
|
64
|
+
return act(timeline, editFrame(kit, seen, seen.source + timeline.args.code, basisOf(state)));
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
/** Insert `code` above the listing. */
|
|
68
|
+
prepend: {
|
|
69
|
+
args: { code: TEXT },
|
|
70
|
+
run: (timeline, state) => {
|
|
71
|
+
const seen = current(state, kit);
|
|
72
|
+
return act(timeline, editFrame(kit, seen, timeline.args.code + seen.source, basisOf(state)));
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
/** Insert `code` before the 1-indexed `[line, col]`, clamped into the listing. */
|
|
76
|
+
insert: {
|
|
77
|
+
args: {
|
|
78
|
+
position: Types.custom(undefined, {
|
|
79
|
+
kind: "point",
|
|
80
|
+
properties: {
|
|
81
|
+
line: { kind: "number", default: 1, min: 1, step: 1 },
|
|
82
|
+
col: { kind: "number", default: 1, min: 1, step: 1 },
|
|
83
|
+
},
|
|
84
|
+
}),
|
|
85
|
+
code: TEXT,
|
|
86
|
+
},
|
|
87
|
+
run: (timeline, state) => {
|
|
88
|
+
const seen = current(state, kit);
|
|
89
|
+
const offset = offsetAt(seen.lines, timeline.args.position);
|
|
90
|
+
const next = seen.source.slice(0, offset) + timeline.args.code + seen.source.slice(offset);
|
|
91
|
+
return act(timeline, editFrame(kit, seen, next, basisOf(state)));
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
/**
|
|
95
|
+
* Erase the code in `target`. A range of whole lines takes their line
|
|
96
|
+
* breaks with it, so the rows below close up.
|
|
97
|
+
*/
|
|
98
|
+
remove: {
|
|
99
|
+
args: { target: TARGET },
|
|
100
|
+
run: (timeline, state) => act(timeline, eraseFrame(kit, state, timeline.args.target)),
|
|
101
|
+
},
|
|
102
|
+
replace: {
|
|
103
|
+
args: { target: TARGET, replacement: TEXT },
|
|
104
|
+
run: (timeline, state) => {
|
|
105
|
+
const seen = current(state, kit);
|
|
106
|
+
const { start, end } = rangeToCharOffsets(timeline.args.target, lineLengths(seen.lines));
|
|
107
|
+
const next = seen.source.slice(0, start) + timeline.args.replacement + seen.source.slice(end);
|
|
108
|
+
return act(timeline, editFrame(kit, seen, next, basisOf(state)));
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
/**
|
|
112
|
+
* Dim everything outside `target` (one range or several) to `dim`.
|
|
113
|
+
* Persistent: a second highlight cross-fades from this one.
|
|
114
|
+
*/
|
|
115
|
+
highlight: {
|
|
116
|
+
args: {
|
|
117
|
+
target: TARGET,
|
|
118
|
+
dim: Types.number(0.4, { min: 0, max: 1, step: 0.01, scale: 100, unit: "%" }),
|
|
119
|
+
},
|
|
120
|
+
run: (timeline, state) => act(timeline, highlightFrame(kit, state, timeline.args.target, timeline.args.dim)),
|
|
121
|
+
},
|
|
122
|
+
/** Fade every dimmed token back to full and clear the highlight. */
|
|
123
|
+
resetHighlight: (timeline, state) => act(timeline, resetHighlightFrame(kit, state)),
|
|
124
|
+
/**
|
|
125
|
+
* Lay a band of `color` behind every line a range touches. A second
|
|
126
|
+
* call slides the bands onto their new rows, paired in order; a band
|
|
127
|
+
* keeps to its lines through an edit.
|
|
128
|
+
*/
|
|
129
|
+
highlightRows: {
|
|
130
|
+
args: {
|
|
131
|
+
target: TARGET,
|
|
132
|
+
color: Types.color(ROW_BAND_COLOR),
|
|
133
|
+
radius: Types.number(ROW_BAND_RADIUS, { min: 0, step: 1 }),
|
|
134
|
+
},
|
|
135
|
+
run: (timeline, state) => {
|
|
136
|
+
const seen = current(state, kit);
|
|
137
|
+
const bands = rowBandsIn(seen.lines, normalizeCodeRanges(timeline.args.target), timeline.args.color, Math.max(0, timeline.args.radius ?? ROW_BAND_RADIUS));
|
|
138
|
+
return act(timeline, rowsFrame(seen, state.banding.bands, bands));
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
/** Fade every row band out and clear them. */
|
|
142
|
+
resetRowHighlight: (timeline, state) => act(timeline, rowsFrame(current(state, kit), state.banding.bands, [])),
|
|
143
|
+
},
|
|
144
|
+
measure: (measurer, state) => measureListing(measurer, state, kit),
|
|
145
|
+
// Refuses the ambient text style: every column is measured against this face, so an
|
|
146
|
+
// inherited family or weight would shape glyphs the geometry was never measured for.
|
|
147
|
+
render: (ctx, state) => ctx.textStyle(null, () => drawListing(ctx, state, kit)),
|
|
148
|
+
});
|
|
149
|
+
kits.set(component.node, kit);
|
|
150
|
+
return component;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* A syntax-highlighted listing whose edits animate token by token: each edit
|
|
154
|
+
* is diffed against what is on screen, so a token that survives travels to
|
|
155
|
+
* its new column and only what changed fades.
|
|
156
|
+
*/
|
|
157
|
+
export const Code = defineCode();
|
|
158
|
+
/** What a listing shows now: its source, its token rows, and the edit in flight, if any. */
|
|
159
|
+
export function listingOf(code) {
|
|
160
|
+
const kit = kits.get(code.constructor);
|
|
161
|
+
if (!kit)
|
|
162
|
+
throw new TypeError("listingOf takes a node a `Code` component built.");
|
|
163
|
+
const view = shown(code, kit);
|
|
164
|
+
return { source: view.source, lines: view.lines, edit: editOf(view) };
|
|
165
|
+
}
|
|
166
|
+
/** Every range matching the literal `text` in `source`, in order and not overlapping. Multi-line matches are found too. */
|
|
167
|
+
export function findAllRanges(source, text) {
|
|
168
|
+
const ranges = [];
|
|
169
|
+
if (!text)
|
|
170
|
+
return ranges;
|
|
171
|
+
const joined = normalizeSource(sourceText(source));
|
|
172
|
+
const lineLens = joined.split("\n").map((line) => line.length);
|
|
173
|
+
let from = 0;
|
|
174
|
+
while (true) {
|
|
175
|
+
const idx = joined.indexOf(text, from);
|
|
176
|
+
if (idx === -1)
|
|
177
|
+
break;
|
|
178
|
+
ranges.push(charOffsetsToRange(idx, idx + text.length, lineLens));
|
|
179
|
+
from = idx + Math.max(1, text.length);
|
|
180
|
+
}
|
|
181
|
+
return ranges;
|
|
182
|
+
}
|
|
183
|
+
/** The `index`th range matching `text`, or `null`. */
|
|
184
|
+
export function findRangeAt(source, text, index) {
|
|
185
|
+
return findAllRanges(source, text)[index] ?? null;
|
|
186
|
+
}
|
|
187
|
+
/** The first range matching `text`, or `null`. */
|
|
188
|
+
export function findFirstRange(source, text) {
|
|
189
|
+
return findRangeAt(source, text, 0);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* A `to({ code })` mid-flight comes first: it wrote `code` after any edit
|
|
193
|
+
* command did. Then an edit command in flight, then the settled listing.
|
|
194
|
+
*/
|
|
195
|
+
function shown(s, kit) {
|
|
196
|
+
const code = s.code;
|
|
197
|
+
if (typeof code !== "string") {
|
|
198
|
+
const { transition } = edited(settled(s, code.from, kit), code.to, basisOf(s), kit);
|
|
199
|
+
return { source: code.to, lines: transition.to, transition, progress: code.t };
|
|
200
|
+
}
|
|
201
|
+
if (s.edit)
|
|
202
|
+
return { source: normalizeSource(code), lines: s.edit.transition.to, transition: s.edit.transition, progress: s.edit.progress };
|
|
203
|
+
const listing = settled(s, normalizeSource(code), kit);
|
|
204
|
+
return { source: listing.source, lines: listing.lines, transition: null, progress: 1 };
|
|
205
|
+
}
|
|
206
|
+
function editOf(view) {
|
|
207
|
+
return view.transition ? { ...view.transition, progress: view.progress } : null;
|
|
208
|
+
}
|
|
209
|
+
/** The listing a command starts from. */
|
|
210
|
+
function current(s, kit) {
|
|
211
|
+
return settled(s, normalizeSource(sourceText(s.code)), kit);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* The structure `source` settles on. Until a listing command has acted, the
|
|
215
|
+
* props are all there is; after one, it is the listing that command left,
|
|
216
|
+
* recoloured keeping its ids when only the theme moved, and diffed onward
|
|
217
|
+
* when the source has (a `to({ code })` that landed, a binding).
|
|
218
|
+
*/
|
|
219
|
+
function settled(s, source, kit) {
|
|
220
|
+
const basis = basisOf(s);
|
|
221
|
+
const pinned = s.listing;
|
|
222
|
+
if (!pinned)
|
|
223
|
+
return derived(source, basis);
|
|
224
|
+
if (pinned.source !== source)
|
|
225
|
+
return edited(pinned, source, basis, kit).listing;
|
|
226
|
+
if (sameBasis(pinned.basis, basis))
|
|
227
|
+
return pinned;
|
|
228
|
+
if (pinned.basis.language === basis.language && pinned.basis.highlighted === basis.highlighted)
|
|
229
|
+
return recoloured(pinned, basis);
|
|
230
|
+
return derived(source, basis);
|
|
231
|
+
}
|
|
232
|
+
function basisOf(s) {
|
|
233
|
+
return { language: s.language, theme: s.theme, highlighted: canHighlight(s.language, s.theme) };
|
|
234
|
+
}
|
|
235
|
+
function sameBasis(a, b) {
|
|
236
|
+
return a.language === b.language && a.theme === b.theme && a.highlighted === b.highlighted;
|
|
237
|
+
}
|
|
238
|
+
/** The source `code` names: where a `to({ code })` is heading, mid-tween. */
|
|
239
|
+
function sourceText(code) {
|
|
240
|
+
return typeof code === "string" ? code : code.to;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Mid-tween `code` holds both sources, so the frame can draw the edit between
|
|
244
|
+
* them; at either end it is a source again.
|
|
245
|
+
*/
|
|
246
|
+
function blendSource(from, to, t) {
|
|
247
|
+
const leaving = sourceText(from);
|
|
248
|
+
const arriving = sourceText(to);
|
|
249
|
+
const a = normalizeSource(leaving);
|
|
250
|
+
const b = normalizeSource(arriving);
|
|
251
|
+
// Nothing to edit: it snaps at the end, as any text does.
|
|
252
|
+
if (a === b)
|
|
253
|
+
return t >= 1 ? arriving : leaving;
|
|
254
|
+
if (t <= 0)
|
|
255
|
+
return a;
|
|
256
|
+
if (t >= 1)
|
|
257
|
+
return b;
|
|
258
|
+
return { from: a, to: b, t };
|
|
259
|
+
}
|
|
260
|
+
const DERIVED = new Map();
|
|
261
|
+
/** Sources kept tokenized per basis, oldest dropped first; a bound `code` can name any number of them. */
|
|
262
|
+
const DERIVED_LISTINGS = 256;
|
|
263
|
+
/** The props' own listing, the same object (and ids) each time it is asked for again. */
|
|
264
|
+
function derived(source, basis) {
|
|
265
|
+
return recall(DERIVED, basisKey(basis), source, DERIVED_LISTINGS, () => ({ source, lines: tokenize(source, basis), basis }));
|
|
266
|
+
}
|
|
267
|
+
const RECOLOURED = new WeakMap();
|
|
268
|
+
/** `pinned` in another theme: the same text split the same way keeps the ids a highlight or a band names. */
|
|
269
|
+
function recoloured(pinned, basis) {
|
|
270
|
+
let shelf = RECOLOURED.get(pinned.lines);
|
|
271
|
+
if (!shelf)
|
|
272
|
+
RECOLOURED.set(pinned.lines, shelf = new Map());
|
|
273
|
+
return recall(shelf, basisKey(basis), pinned.source, 4, () => {
|
|
274
|
+
const lines = tokenize(pinned.source, basis);
|
|
275
|
+
adoptIds(pinned.lines, lines);
|
|
276
|
+
return { source: pinned.source, lines, basis };
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
function edited(from, next, basis, kit) {
|
|
280
|
+
let shelf = kit.edits.get(from.lines);
|
|
281
|
+
if (!shelf)
|
|
282
|
+
kit.edits.set(from.lines, shelf = new Map());
|
|
283
|
+
return recall(shelf, basisKey(basis), next, 16, () => buildEdit(kit, from.lines, next, basis));
|
|
284
|
+
}
|
|
285
|
+
function buildEdit(kit, from, next, basis) {
|
|
286
|
+
const edit = kit.diff(from, tokenize(next, basis));
|
|
287
|
+
const transition = {
|
|
288
|
+
kind: "structural",
|
|
289
|
+
from,
|
|
290
|
+
to: edit.lines,
|
|
291
|
+
removedIds: edit.removedIds,
|
|
292
|
+
addedIds: edit.addedIds,
|
|
293
|
+
enteringLineIds: edit.newLineIds,
|
|
294
|
+
fromColorById: edit.fromColorById,
|
|
295
|
+
phases: kit.phases(edit.removedIds.size > 0, edit.addedIds.size > 0),
|
|
296
|
+
progress: 0,
|
|
297
|
+
layoutKey: null,
|
|
298
|
+
fromLayout: null,
|
|
299
|
+
toLayout: null,
|
|
300
|
+
};
|
|
301
|
+
return { transition, listing: { source: next, lines: edit.lines, basis } };
|
|
302
|
+
}
|
|
303
|
+
function tokenize(source, basis) {
|
|
304
|
+
// Best effort outside the asset pipeline (a test, a headless measure): on the timeline `load` has the grammar in first.
|
|
305
|
+
if (!basis.highlighted)
|
|
306
|
+
ensureHighlighter(undefined, [basis.language]).catch(() => { });
|
|
307
|
+
return tokenizeCodeToIdLines(source, basis.language, basis.theme);
|
|
308
|
+
}
|
|
309
|
+
const styleIds = new WeakMap();
|
|
310
|
+
let nextStyleId = 1;
|
|
311
|
+
function basisKey(basis) {
|
|
312
|
+
let theme;
|
|
313
|
+
if (typeof basis.theme === "string")
|
|
314
|
+
theme = `name:${basis.theme}`;
|
|
315
|
+
else {
|
|
316
|
+
let id = styleIds.get(basis.theme);
|
|
317
|
+
if (id === undefined)
|
|
318
|
+
styleIds.set(basis.theme, id = nextStyleId++);
|
|
319
|
+
theme = `style:${id}`;
|
|
320
|
+
}
|
|
321
|
+
return `${basis.language}\n${basis.highlighted ? 1 : 0}\n${theme}`;
|
|
322
|
+
}
|
|
323
|
+
/** Memoized by basis, then by source; a basis keeps `limit` sources, least recently asked for dropped first. */
|
|
324
|
+
function recall(shelf, basis, source, limit, make) {
|
|
325
|
+
let row = shelf.get(basis);
|
|
326
|
+
if (!row)
|
|
327
|
+
shelf.set(basis, row = new Map());
|
|
328
|
+
const hit = row.get(source);
|
|
329
|
+
if (hit !== undefined) {
|
|
330
|
+
row.delete(source);
|
|
331
|
+
row.set(source, hit);
|
|
332
|
+
return hit;
|
|
333
|
+
}
|
|
334
|
+
const made = make();
|
|
335
|
+
row.set(source, made);
|
|
336
|
+
if (row.size > limit)
|
|
337
|
+
row.delete(row.keys().next().value);
|
|
338
|
+
return made;
|
|
339
|
+
}
|
|
340
|
+
/** Carry `from`'s line and token ids onto `to` when the two spell the same tokens. */
|
|
341
|
+
function adoptIds(from, to) {
|
|
342
|
+
if (from.length !== to.length)
|
|
343
|
+
return;
|
|
344
|
+
for (let i = 0; i < from.length; i++) {
|
|
345
|
+
const a = from[i].tokens;
|
|
346
|
+
const b = to[i].tokens;
|
|
347
|
+
if (a.length !== b.length)
|
|
71
348
|
return;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
this.layoutCacheKey = null;
|
|
349
|
+
for (let j = 0; j < a.length; j++)
|
|
350
|
+
if (a[j].content !== b[j].content)
|
|
351
|
+
return;
|
|
76
352
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
* Both belong to *layout*: token x positions are measured against the face,
|
|
82
|
-
* and how many tokens there are depends on the grammar. Declared here, the
|
|
83
|
-
* grammar goes on the timeline as an ordinary asset: `AssetManager.loadAt`
|
|
84
|
-
* waits for it before the frame is laid out, so the first measurement is the
|
|
85
|
-
* right one. Nothing is freed on eviction — parsers are cheap to keep
|
|
86
|
-
* resident. The loader touches no node: `renderContent` re-tokenizes once
|
|
87
|
-
* the grammar is there.
|
|
88
|
-
*/
|
|
89
|
-
declareAssets(assets) {
|
|
90
|
-
super.declareAssets(assets);
|
|
91
|
-
assets.font(this.fontFamily);
|
|
92
|
-
assets.loader(`shiki:lang:${this.language}`, grammarLoader(this.language), { gate: "layout" });
|
|
353
|
+
for (let i = 0; i < from.length; i++) {
|
|
354
|
+
to[i].id = from[i].id;
|
|
355
|
+
for (let j = 0; j < from[i].tokens.length; j++)
|
|
356
|
+
to[i].tokens[j].id = from[i].tokens[j].id;
|
|
93
357
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Each command writes the listing it acted on, at every time it is asked for,
|
|
361
|
+
* so the ids it captured are the ones drawn; before it starts the timeline has
|
|
362
|
+
* put back the props, and the listing is derived from them again.
|
|
363
|
+
*/
|
|
364
|
+
function editFrame(kit, seen, source, basis) {
|
|
365
|
+
const next = normalizeSource(source);
|
|
366
|
+
if (next === seen.source)
|
|
367
|
+
return null;
|
|
368
|
+
const { transition, listing } = buildEdit(kit, seen.lines, next, basis);
|
|
369
|
+
// Three phases (see `phaseStrategy`): what leaves fades out, what stays reflows, then what is new arrives.
|
|
370
|
+
return (t) => (t <= 0 ? { code: seen.source, listing: seen, edit: null }
|
|
371
|
+
: t >= 1 ? { code: next, listing, edit: null }
|
|
372
|
+
: { code: next, listing, edit: { transition, progress: t } });
|
|
373
|
+
}
|
|
374
|
+
function eraseFrame(kit, state, target) {
|
|
375
|
+
const seen = current(state, kit);
|
|
376
|
+
const source = seen.source;
|
|
377
|
+
let { start, end } = rangeToCharOffsets(target, lineLengths(seen.lines));
|
|
378
|
+
// `lines(2)` resolves to the characters of line 2: the row goes too, as an editor would close the gap. That is also what makes a blank row removable.
|
|
379
|
+
const atLineStart = start === 0 || source[start - 1] === "\n";
|
|
380
|
+
const atLineEnd = end === source.length || source[end] === "\n";
|
|
381
|
+
if (atLineStart && atLineEnd) {
|
|
382
|
+
if (end < source.length)
|
|
383
|
+
end += 1;
|
|
384
|
+
else if (start > 0)
|
|
385
|
+
start -= 1;
|
|
386
|
+
}
|
|
387
|
+
else if (end <= start) {
|
|
113
388
|
return null;
|
|
114
389
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
* and cached on the transition rather than rebuilt per frame.
|
|
135
|
-
*/
|
|
136
|
-
frameLayout(scope) {
|
|
137
|
-
const m = this.metrics();
|
|
138
|
-
const edit = this.activeEdit();
|
|
139
|
-
if (!edit) {
|
|
140
|
-
const layout = this.settledLayout(m, scope);
|
|
141
|
-
return { from: layout, to: layout, edit: null };
|
|
142
|
-
}
|
|
143
|
-
const key = `${metricsSignature(m)}|${this.advanceCache.signature(m.fontSize, m.fontFamily)}`;
|
|
144
|
-
if (edit.layoutKey !== key || !edit.fromLayout || !edit.toLayout) {
|
|
145
|
-
edit.fromLayout = layoutCode(edit.from, m, this.advanceCache, scope);
|
|
146
|
-
edit.toLayout = layoutCode(edit.to, m, this.advanceCache, scope);
|
|
147
|
-
edit.layoutKey = key;
|
|
148
|
-
}
|
|
149
|
-
return { from: edit.fromLayout, to: edit.toLayout, edit };
|
|
150
|
-
}
|
|
151
|
-
measure(constraints, scope) {
|
|
152
|
-
this.advanceCache.sync(this.advanceCache.signature(this.fontSize, this.fontFamily));
|
|
153
|
-
const wm = this.width;
|
|
154
|
-
const hm = this.height;
|
|
155
|
-
const { from, to, edit } = this.frameLayout(scope);
|
|
156
|
-
const t = moveProgress(edit);
|
|
157
|
-
const innerW = edit ? lerpNumber(from.innerW, to.innerW, t) : to.innerW;
|
|
158
|
-
const innerH = edit ? lerpNumber(from.innerH, to.innerH, t) : to.innerH;
|
|
159
|
-
const resolvedW = typeof wm === "number"
|
|
160
|
-
? wm
|
|
161
|
-
: wm === "hug"
|
|
162
|
-
? innerW + this.padding.left + this.padding.right
|
|
163
|
-
: constraints.maxWidth ?? 0;
|
|
164
|
-
const resolvedH = typeof hm === "number"
|
|
165
|
-
? hm
|
|
166
|
-
: hm === "hug"
|
|
167
|
-
? innerH + this.padding.top + this.padding.bottom
|
|
168
|
-
: constraints.maxHeight ?? 0;
|
|
169
|
-
return { width: resolvedW, height: resolvedH };
|
|
170
|
-
}
|
|
171
|
-
renderContent(ctx) {
|
|
172
|
-
// Refuse the ambient `<DefaultTextStyle>` / theme typography defaults for
|
|
173
|
-
// everything drawn inside this node.
|
|
174
|
-
//
|
|
175
|
-
// Those defaults describe the *document's* prose — a display face, a
|
|
176
|
-
// heading weight, a paragraph line-height — and a code block is not prose.
|
|
177
|
-
// Its own props are the vocabulary: `fontFamily` defaults to a monospaced
|
|
178
|
-
// face because column alignment depends on it, `letterSpacing` and
|
|
179
|
-
// `lineHeight` are tuned against that face, and every token is laid out at
|
|
180
|
-
// an x measured from those exact values. A scene-wide serif family or a
|
|
181
|
-
// 700 weight arriving through the draw scope would shape glyphs the
|
|
182
|
-
// geometry was never measured for, and the block would come apart
|
|
183
|
-
// column-by-column rather than merely look different.
|
|
184
|
-
ctx.pushTextStyle(null);
|
|
185
|
-
try {
|
|
186
|
-
// Keep retrying until the language+theme have actually loaded, so a frame
|
|
187
|
-
// that rendered as plain text upgrades to full highlighting the moment the
|
|
188
|
-
// asset loader resolves. Never mid-edit: re-tokenizing mints fresh ids,
|
|
189
|
-
// and an in-flight transition is keyed by the ones it captured.
|
|
190
|
-
if (!this.tokenized && !this.activeEdit() && canHighlight(this.language, this.theme)) {
|
|
191
|
-
this.tokenize();
|
|
192
|
-
}
|
|
193
|
-
super.renderContent(ctx);
|
|
194
|
-
}
|
|
195
|
-
finally {
|
|
196
|
-
ctx.popTextStyle();
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
renderSelf(ctx) {
|
|
200
|
-
this.drawSelf(ctx);
|
|
201
|
-
}
|
|
202
|
-
/** A listing holds a listing — `append`/`insert`/`erase` are how it grows. */
|
|
203
|
-
acceptsChild(_child) {
|
|
204
|
-
return false;
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* The node's laid-out rect as a clip outline, so `clip` and any backdrop
|
|
208
|
-
* effect are confined to the block rather than to nothing — a listing set on
|
|
209
|
-
* a card is exactly where a backdrop blur is asked for.
|
|
210
|
-
*/
|
|
211
|
-
clipSelf() {
|
|
212
|
-
return new Clip().rect({
|
|
213
|
-
width: this.layoutBounds.width,
|
|
214
|
-
height: this.layoutBounds.height,
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* A listing is grabbed by its **text**, line by ragged line, rather than by
|
|
219
|
-
* the rectangle the lines were laid out in.
|
|
220
|
-
*
|
|
221
|
-
* The default narrows a node's grab region to the outline it declares, and
|
|
222
|
-
* this node's outline is a box it no longer paints anything into. Code is
|
|
223
|
-
* mostly whitespace: a ten-line snippet with one long line is a rectangle
|
|
224
|
-
* that is largely empty, and in an editor every one of those empty pixels
|
|
225
|
-
* swallows a click meant for whatever sits behind or beneath it. Short of the
|
|
226
|
-
* text itself there is nothing there to have clicked.
|
|
227
|
-
*
|
|
228
|
-
* So the region is the union of the lines' own extents — each a line-height
|
|
229
|
-
* slot as wide as that line's ink — plus the gutter where line numbers are
|
|
230
|
-
* showing, since those are drawn glyphs too.
|
|
231
|
-
*
|
|
232
|
-
* Answered from the **cached** layout, which is what the last frame drew
|
|
233
|
-
* from, because a hit test arrives with no measurement scope and text cannot
|
|
234
|
-
* be measured without one. Where there is no usable cache — before the first
|
|
235
|
-
* frame, or mid-edit, when the geometry is an interpolation of two structures
|
|
236
|
-
* held on the transition — the box is the honest answer rather than a
|
|
237
|
-
* silhouette measured against stale metrics.
|
|
238
|
-
*/
|
|
239
|
-
hitTestSelf(local, tolerance) {
|
|
240
|
-
const layout = this.hitLayout();
|
|
241
|
-
if (!layout)
|
|
242
|
-
return super.hitTestSelf(local, tolerance);
|
|
243
|
-
// Never *wider* than the box: `hug` sizes to the ink, but a fixed width
|
|
244
|
-
// narrower than the longest line would otherwise be hit outside itself.
|
|
245
|
-
if (!super.hitTestSelf(local, tolerance))
|
|
246
|
-
return false;
|
|
247
|
-
const half = (this.fontSize * this.lineHeight) / 2;
|
|
248
|
-
// Line numbers sit in the gutter, to the left of every line's own start,
|
|
249
|
-
// so a numbered blank line is still grabbable — by its number.
|
|
250
|
-
const gutter = this.showLineNumbers ? layout.gutter : 0;
|
|
251
|
-
const left = layout.startX - gutter - tolerance;
|
|
252
|
-
for (let i = 0; i < layout.lineY.length; i++) {
|
|
253
|
-
const width = (layout.lineW[i] ?? 0) + gutter;
|
|
254
|
-
if (width <= 0)
|
|
255
|
-
continue;
|
|
256
|
-
if (Math.abs(local.y - layout.lineY[i]) > half + tolerance)
|
|
390
|
+
if (end <= start)
|
|
391
|
+
return null;
|
|
392
|
+
return editFrame(kit, seen, source.slice(0, start) + source.slice(end), basisOf(state));
|
|
393
|
+
}
|
|
394
|
+
function highlightFrame(kit, state, target, dim) {
|
|
395
|
+
const seen = current(state, kit);
|
|
396
|
+
const toDim = dim ?? 0.4;
|
|
397
|
+
const matchIds = tokenIdsInRanges(seen.lines, normalizeCodeRanges(target));
|
|
398
|
+
if (matchIds.size === 0)
|
|
399
|
+
return null;
|
|
400
|
+
const previous = state.focus;
|
|
401
|
+
const fromDim = previous.dim ?? 1;
|
|
402
|
+
const hadPrevious = previous.ids.size > 0;
|
|
403
|
+
const tokens = [];
|
|
404
|
+
for (const line of seen.lines) {
|
|
405
|
+
for (const tok of line.tokens) {
|
|
406
|
+
const fromOp = !hadPrevious || previous.ids.has(tok.id) ? 1 : fromDim;
|
|
407
|
+
const toOp = matchIds.has(tok.id) ? 1 : toDim;
|
|
408
|
+
if (fromOp === toOp)
|
|
257
409
|
continue;
|
|
258
|
-
|
|
259
|
-
return true;
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
return false;
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* The layout a hit test may be answered from: the settled cache, and only
|
|
266
|
-
* while it still matches the node's current metrics.
|
|
267
|
-
*
|
|
268
|
-
* The key is the one {@link settledLayout} writes minus its scope-dependent
|
|
269
|
-
* half — every input to it (`metricsSignature`, the advance cache's own
|
|
270
|
-
* signature) is readable without a measurer, which is precisely what makes
|
|
271
|
-
* the check possible here.
|
|
272
|
-
*/
|
|
273
|
-
hitLayout() {
|
|
274
|
-
if (!this.layoutCache || this.activeEdit())
|
|
275
|
-
return null;
|
|
276
|
-
const m = this.metrics();
|
|
277
|
-
const key = `${this.structureVersion}|${metricsSignature(m)}|${this.advanceCache.signature(m.fontSize, m.fontFamily)}`;
|
|
278
|
-
return this.layoutCacheKey === key ? this.layoutCache : null;
|
|
279
|
-
}
|
|
280
|
-
// ── Editing commands ────────────────────────────────────────────────────
|
|
281
|
-
/**
|
|
282
|
-
* Append `code` to the end of the listing.
|
|
283
|
-
*
|
|
284
|
-
* Stated as *what the source becomes*; {@link editTo} works out the rest —
|
|
285
|
-
* which is why appending a line that closes a block re-highlights the lines
|
|
286
|
-
* above it, instead of colouring the new text as its own program.
|
|
287
|
-
*/
|
|
288
|
-
append(args) {
|
|
289
|
-
const { code } = args.data;
|
|
290
|
-
return this.editTo(this.joinedSource() + code, args.duration, args.easing);
|
|
291
|
-
}
|
|
292
|
-
/** Insert `code` above the listing. */
|
|
293
|
-
prepend(args) {
|
|
294
|
-
const { code } = args.data;
|
|
295
|
-
return this.editTo(code + this.joinedSource(), args.duration, args.easing);
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Insert `code` at the given (line, col). Both are 1-indexed; col is the
|
|
299
|
-
* column BEFORE which the new content is inserted (col=1 means start of
|
|
300
|
-
* line). If `code` contains newlines, new lines are created in the middle
|
|
301
|
-
* of the existing line.
|
|
302
|
-
*/
|
|
303
|
-
insert(args) {
|
|
304
|
-
const { position, code } = args.data;
|
|
305
|
-
const source = this.joinedSource();
|
|
306
|
-
const offset = this.offsetAt(position);
|
|
307
|
-
return this.editTo(source.slice(0, offset) + code + source.slice(offset), args.duration, args.easing);
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* Erase the code in `codeRange`.
|
|
311
|
-
*
|
|
312
|
-
* Named `erase` rather than `remove` because a `Code` is also a node, and
|
|
313
|
-
* `Node.remove(child)` takes a child out of the tree. Two methods spelled
|
|
314
|
-
* the same on one object, doing entirely different things, is worse than one
|
|
315
|
-
* of them having a slightly less obvious name.
|
|
316
|
-
*
|
|
317
|
-
* A range that covers whole lines takes their line breaks with it, so the
|
|
318
|
-
* rows below close up; a range inside a line takes only the characters, and
|
|
319
|
-
* the rest of the line reflows around the hole.
|
|
320
|
-
*/
|
|
321
|
-
erase(args) {
|
|
322
|
-
const { target: codeRange } = args.data;
|
|
323
|
-
const { duration, easing } = args;
|
|
324
|
-
const source = this.joinedSource();
|
|
325
|
-
let { start, end } = rangeToCharOffsets(codeRange, this.lineLengths());
|
|
326
|
-
// `lines(2)` resolves to the *characters* of line 2, not to the row —
|
|
327
|
-
// taking those alone would leave an empty line behind where an editor
|
|
328
|
-
// would have closed the gap. This is also what makes a blank row
|
|
329
|
-
// removable at all: it has no characters, so its range is empty, and
|
|
330
|
-
// "delete nothing" is not what `lines(2)` was asking for.
|
|
331
|
-
const atLineStart = start === 0 || source[start - 1] === "\n";
|
|
332
|
-
const atLineEnd = end === source.length || source[end] === "\n";
|
|
333
|
-
if (atLineStart && atLineEnd) {
|
|
334
|
-
if (end < source.length)
|
|
335
|
-
end += 1;
|
|
336
|
-
else if (start > 0)
|
|
337
|
-
start -= 1;
|
|
338
|
-
}
|
|
339
|
-
else if (end <= start) {
|
|
340
|
-
return driveCommand(duration, () => { });
|
|
341
|
-
}
|
|
342
|
-
if (end <= start)
|
|
343
|
-
return driveCommand(duration, () => { });
|
|
344
|
-
return this.editTo(source.slice(0, start) + source.slice(end), duration, easing);
|
|
345
|
-
}
|
|
346
|
-
/** Replace the code in `codeRange` with `replacement`. */
|
|
347
|
-
replace(args) {
|
|
348
|
-
const { target: codeRange, replacement } = args.data;
|
|
349
|
-
const source = this.joinedSource();
|
|
350
|
-
const { start, end } = rangeToCharOffsets(codeRange, this.lineLengths());
|
|
351
|
-
return this.editTo(source.slice(0, start) + replacement + source.slice(end), args.duration, args.easing);
|
|
352
|
-
}
|
|
353
|
-
/**
|
|
354
|
-
* Highlight a range of code — or several at once (e.g.
|
|
355
|
-
* `CodeRanges.lines(2).word(5, 1, 3)`) — highlighted as one set: tokens
|
|
356
|
-
* within any of the ranges stay at opacity 1, tokens outside dim to
|
|
357
|
-
* `dim`. Persistent — call resetHighlight() to undo, or call highlight()
|
|
358
|
-
* again with a different range to cross-fade.
|
|
359
|
-
*/
|
|
360
|
-
highlight(args) {
|
|
361
|
-
const { target: codeRange, dim } = args.data;
|
|
362
|
-
const { duration, easing } = args;
|
|
363
|
-
const toDim = dim ?? 0.4;
|
|
364
|
-
const matchIds = this.tokenIdsInRanges(normalizeCodeRanges(codeRange));
|
|
365
|
-
if (matchIds.size === 0)
|
|
366
|
-
return driveCommand(duration, () => { });
|
|
367
|
-
const fromDim = this.highlightDimOpacity ?? 1;
|
|
368
|
-
const hadPrevious = this.highlightedIds.size > 0;
|
|
369
|
-
const previousIds = this.highlightedIds;
|
|
370
|
-
const animTokens = [];
|
|
371
|
-
for (const line of this.tokenLines) {
|
|
372
|
-
for (const tok of line.tokens) {
|
|
373
|
-
const wasHighlighted = !hadPrevious || previousIds.has(tok.id);
|
|
374
|
-
const isHighlighted = matchIds.has(tok.id);
|
|
375
|
-
const fromOp = wasHighlighted ? 1 : fromDim;
|
|
376
|
-
const toOp = isHighlighted ? 1 : toDim;
|
|
377
|
-
if (fromOp === toOp)
|
|
378
|
-
continue;
|
|
379
|
-
animTokens.push(makeAnim(tok.id, { keys: [0, 1], values: [fromOp, toOp] }));
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
// Which tokens are dimmed is state the *next* highlight reads to know
|
|
383
|
-
// what it is cross-fading from, so it is committed at the end and put
|
|
384
|
-
// back below it — a `finally` could only ever have run forwards.
|
|
385
|
-
return this.runDim(animTokens, duration, easing, (done) => {
|
|
386
|
-
this.highlightDimOpacity = done ? toDim : (hadPrevious ? fromDim : null);
|
|
387
|
-
this.highlightedIds = done ? matchIds : previousIds;
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Fade all dimmed tokens back to opacity 1 and clear the persistent
|
|
392
|
-
* highlight state.
|
|
393
|
-
*/
|
|
394
|
-
resetHighlight(args) {
|
|
395
|
-
const { duration, easing } = args;
|
|
396
|
-
if (this.highlightDimOpacity === null)
|
|
397
|
-
return driveCommand(duration, () => { });
|
|
398
|
-
const fromDim = this.highlightDimOpacity;
|
|
399
|
-
const previousIds = this.highlightedIds;
|
|
400
|
-
const animTokens = [];
|
|
401
|
-
for (const line of this.tokenLines) {
|
|
402
|
-
for (const tok of line.tokens) {
|
|
403
|
-
const fromOp = previousIds.has(tok.id) ? 1 : fromDim;
|
|
404
|
-
if (fromOp === 1)
|
|
405
|
-
continue;
|
|
406
|
-
animTokens.push(makeAnim(tok.id, { keys: [0, 1], values: [fromOp, 1] }));
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
return this.runDim(animTokens, duration, easing, (done) => {
|
|
410
|
-
this.highlightDimOpacity = done ? null : fromDim;
|
|
411
|
-
this.highlightedIds = done ? new Set() : previousIds;
|
|
412
|
-
});
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
|
-
* Animate the listing to a new source.
|
|
416
|
-
*
|
|
417
|
-
* The engine behind every editing command, and behind `to({ code })`. Every
|
|
418
|
-
* edit is expressed as the source it produces, tokenized, and then *diffed*
|
|
419
|
-
* against what is on screen — so a token that survived the edit keeps its
|
|
420
|
-
* identity and simply travels to its new column, and only what genuinely
|
|
421
|
-
* changed is faded.
|
|
422
|
-
*
|
|
423
|
-
* The result runs in three phases (see `phaseStrategy`): what is leaving
|
|
424
|
-
* fades out, what stays reflows, and only then does what is new arrive.
|
|
425
|
-
* Playing them together is what made a replacement read as a cross-dissolve
|
|
426
|
-
* of two unrelated listings rather than as an edit being made.
|
|
427
|
-
*/
|
|
428
|
-
editTo(next, duration, easing) {
|
|
429
|
-
const from = this.tokenLines;
|
|
430
|
-
const fromCode = this.joinedSource();
|
|
431
|
-
if (next === fromCode)
|
|
432
|
-
return driveCommand(duration, () => { });
|
|
433
|
-
const edit = this.diffStrategy(from, tokenizeCodeToIdLines(next, this.language, this.theme));
|
|
434
|
-
const transition = {
|
|
435
|
-
kind: "structural",
|
|
436
|
-
from,
|
|
437
|
-
to: edit.lines,
|
|
438
|
-
removedIds: edit.removedIds,
|
|
439
|
-
addedIds: edit.addedIds,
|
|
440
|
-
enteringLineIds: edit.newLineIds,
|
|
441
|
-
fromColorById: edit.fromColorById,
|
|
442
|
-
phases: this.phaseStrategy(edit.removedIds.size > 0, edit.addedIds.size > 0),
|
|
443
|
-
progress: 0,
|
|
444
|
-
layoutKey: null,
|
|
445
|
-
fromLayout: null,
|
|
446
|
-
toLayout: null,
|
|
447
|
-
};
|
|
448
|
-
// A tokenize triggered by the grammar landing mid-edit would mint fresh
|
|
449
|
-
// ids underneath this transition, and the transition is keyed by the ones
|
|
450
|
-
// it captured — so the retry in `onRender` is gated on there being no edit
|
|
451
|
-
// in flight, which means this flag has to be honest now rather than once
|
|
452
|
-
// the edit settles.
|
|
453
|
-
this.tokenized = canHighlight(this.language, this.theme);
|
|
454
|
-
return driveCommand(duration, (t) => {
|
|
455
|
-
const running = t > 0 && t < 1;
|
|
456
|
-
const index = this.transitions.indexOf(transition);
|
|
457
|
-
if (running && index < 0)
|
|
458
|
-
this.transitions.push(transition);
|
|
459
|
-
else if (!running && index >= 0)
|
|
460
|
-
this.transitions.splice(index, 1);
|
|
461
|
-
transition.progress = easing ? easing(t) : t;
|
|
462
|
-
// The settle, in both directions: asked for a time before the edit
|
|
463
|
-
// starts, the listing is what preceded it; at or past the end, the
|
|
464
|
-
// result. A `finally` could only ever have run forwards.
|
|
465
|
-
if (t <= 0) {
|
|
466
|
-
this.setLines(from);
|
|
467
|
-
this._writeProp("code", fromCode);
|
|
468
|
-
}
|
|
469
|
-
else {
|
|
470
|
-
this.setLines(edit.lines);
|
|
471
|
-
this._writeProp("code", next);
|
|
472
|
-
}
|
|
473
|
-
});
|
|
474
|
-
}
|
|
475
|
-
/**
|
|
476
|
-
* Put a dim transition on the stack, drive its progress, take it off and
|
|
477
|
-
* commit at the end.
|
|
478
|
-
*
|
|
479
|
-
* A {@link Command} rather than a generator, which is what makes a listing
|
|
480
|
-
* scrubbable: membership is a function of `t` (on the stack for `0 < t < 1`,
|
|
481
|
-
* off it outside), `settle(done)` replaces the `finally` and takes *which
|
|
482
|
-
* way*, and `progress` is assigned from `t` rather than accumulated.
|
|
483
|
-
*/
|
|
484
|
-
runDim(tokens, duration, easing, settle) {
|
|
485
|
-
const transition = { kind: "dim", tokens, progress: 0 };
|
|
486
|
-
return driveCommand(duration, (t) => {
|
|
487
|
-
const running = t > 0 && t < 1;
|
|
488
|
-
const index = this.transitions.indexOf(transition);
|
|
489
|
-
if (running && index < 0)
|
|
490
|
-
this.transitions.push(transition);
|
|
491
|
-
else if (!running && index >= 0)
|
|
492
|
-
this.transitions.splice(index, 1);
|
|
493
|
-
transition.progress = easing ? easing(t) : t;
|
|
494
|
-
settle?.(t >= 1);
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
/**
|
|
498
|
-
* Route a tweened `code` through the diff engine.
|
|
499
|
-
*
|
|
500
|
-
* `to({ code })` is the form an author reaches for first — it is how every
|
|
501
|
-
* other prop is animated — and a bare string prop would otherwise land in the
|
|
502
|
-
* generic tween's discrete-snap bucket and simply cut at the end. Intercepted
|
|
503
|
-
* here it becomes the same three-phase edit the named commands produce, while
|
|
504
|
-
* the rest of the step's props tween alongside it untouched.
|
|
505
|
-
*/
|
|
506
|
-
_prepareStep(to, duration, easing) {
|
|
507
|
-
const next = to.code;
|
|
508
|
-
if (typeof next !== "string" || next === this.joinedSource()) {
|
|
509
|
-
return super._prepareStep(to, duration, easing);
|
|
410
|
+
tokens.push(makeAnim(tok.id, { keys: [0, 1], values: [fromOp, toOp] }));
|
|
510
411
|
}
|
|
511
|
-
const rest = { ...to };
|
|
512
|
-
delete rest.code;
|
|
513
|
-
// Prepared before the edit, so a step that also moves `fontSize` has
|
|
514
|
-
// snapshotted its own start value against the listing that is still on
|
|
515
|
-
// screen.
|
|
516
|
-
const others = super._prepareStep(rest, duration, easing);
|
|
517
|
-
const edit = this.editTo(next, duration, easing)._stepper();
|
|
518
|
-
return {
|
|
519
|
-
seek: (elapsed) => {
|
|
520
|
-
others.seek(elapsed);
|
|
521
|
-
edit.seek(elapsed);
|
|
522
|
-
},
|
|
523
|
-
advance: (dt) => {
|
|
524
|
-
const othersDone = others.advance(dt);
|
|
525
|
-
const editDone = edit.advance(dt);
|
|
526
|
-
return othersDone && editDone;
|
|
527
|
-
},
|
|
528
|
-
};
|
|
529
412
|
}
|
|
530
|
-
//
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
413
|
+
// Which tokens are dimmed is what the next highlight cross-fades from, so it is committed at the end and put back below it.
|
|
414
|
+
const before = { dim: hadPrevious ? fromDim : null, ids: previous.ids, fade: null };
|
|
415
|
+
const done = { dim: toDim, ids: matchIds, fade: null };
|
|
416
|
+
return (t) => ({
|
|
417
|
+
listing: seen,
|
|
418
|
+
focus: t <= 0 ? before : t >= 1 ? done : { ...before, fade: { kind: "dim", tokens, progress: t } },
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
function resetHighlightFrame(kit, state) {
|
|
422
|
+
const previous = state.focus;
|
|
423
|
+
if (previous.dim === null)
|
|
424
|
+
return null;
|
|
425
|
+
const seen = current(state, kit);
|
|
426
|
+
const fromDim = previous.dim;
|
|
427
|
+
const tokens = [];
|
|
428
|
+
for (const line of seen.lines) {
|
|
429
|
+
for (const tok of line.tokens) {
|
|
430
|
+
const fromOp = previous.ids.has(tok.id) ? 1 : fromDim;
|
|
431
|
+
if (fromOp === 1)
|
|
432
|
+
continue;
|
|
433
|
+
tokens.push(makeAnim(tok.id, { keys: [0, 1], values: [fromOp, 1] }));
|
|
548
434
|
}
|
|
549
|
-
return ranges;
|
|
550
|
-
}
|
|
551
|
-
/** Find the `index`th range matching `text`. Returns null if not found. */
|
|
552
|
-
findRangeAt(text, index) {
|
|
553
|
-
const all = this.findAllRanges(text);
|
|
554
|
-
return all[index] ?? null;
|
|
555
435
|
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
if (offsets.length === 0)
|
|
593
|
-
return result;
|
|
594
|
-
// Walk tokens with running offsets in the joined string, testing each
|
|
595
|
-
// token against every requested range's offsets.
|
|
596
|
-
let off = 0;
|
|
597
|
-
for (let li = 0; li < this.tokenLines.length; li++) {
|
|
598
|
-
const line = this.tokenLines[li];
|
|
599
|
-
for (const tok of line.tokens) {
|
|
600
|
-
const tStart = off;
|
|
601
|
-
const tEnd = off + tok.content.length;
|
|
602
|
-
if (offsets.some(({ start, end }) => tEnd > start && tStart < end))
|
|
603
|
-
result.add(tok.id);
|
|
604
|
-
off = tEnd;
|
|
605
|
-
}
|
|
606
|
-
if (li < this.tokenLines.length - 1)
|
|
607
|
-
off += 1; // newline
|
|
608
|
-
}
|
|
436
|
+
const before = { dim: fromDim, ids: previous.ids, fade: null };
|
|
437
|
+
return (t) => ({
|
|
438
|
+
listing: seen,
|
|
439
|
+
focus: t >= 1 ? NO_FOCUS : t <= 0 ? before : { ...before, fade: { kind: "dim", tokens, progress: t } },
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
function rowsFrame(seen, from, to) {
|
|
443
|
+
if (from.length === 0 && to.length === 0)
|
|
444
|
+
return null;
|
|
445
|
+
return (t) => ({
|
|
446
|
+
listing: seen,
|
|
447
|
+
banding: t >= 1 ? { bands: to, fade: null }
|
|
448
|
+
: t <= 0 ? { bands: from, fade: null }
|
|
449
|
+
: { bands: from, fade: { kind: "rows", from, to, progress: t } },
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
function lineLengths(lines) {
|
|
453
|
+
return lines.map((line) => line.tokens.reduce((acc, t) => acc + t.content.length, 0));
|
|
454
|
+
}
|
|
455
|
+
/** Character offset of a 1-indexed (line, col), clamped into the document. */
|
|
456
|
+
function offsetAt(lines, position) {
|
|
457
|
+
const lens = lineLengths(lines);
|
|
458
|
+
if (lens.length === 0)
|
|
459
|
+
return 0;
|
|
460
|
+
const [rawLine, rawCol] = position;
|
|
461
|
+
const li = Math.max(0, Math.min(lens.length - 1, rawLine - 1));
|
|
462
|
+
let offset = 0;
|
|
463
|
+
for (let k = 0; k < li; k++)
|
|
464
|
+
offset += lens[k] + 1;
|
|
465
|
+
return offset + Math.max(0, Math.min(lens[li], rawCol - 1));
|
|
466
|
+
}
|
|
467
|
+
/** The ids of every token any of `codeRanges` overlaps, partly or wholly. */
|
|
468
|
+
function tokenIdsInRanges(lines, codeRanges) {
|
|
469
|
+
const result = new Set();
|
|
470
|
+
const lineLens = lineLengths(lines);
|
|
471
|
+
if (lineLens.length === 0)
|
|
609
472
|
return result;
|
|
473
|
+
const offsets = codeRanges
|
|
474
|
+
.map((r) => rangeToCharOffsets(r, lineLens))
|
|
475
|
+
.filter(({ start, end }) => end > start);
|
|
476
|
+
if (offsets.length === 0)
|
|
477
|
+
return result;
|
|
478
|
+
let off = 0;
|
|
479
|
+
for (let li = 0; li < lines.length; li++) {
|
|
480
|
+
for (const tok of lines[li].tokens) {
|
|
481
|
+
const tStart = off;
|
|
482
|
+
const tEnd = off + tok.content.length;
|
|
483
|
+
if (offsets.some(({ start, end }) => tEnd > start && tStart < end))
|
|
484
|
+
result.add(tok.id);
|
|
485
|
+
off = tEnd;
|
|
486
|
+
}
|
|
487
|
+
if (li < lines.length - 1)
|
|
488
|
+
off += 1;
|
|
610
489
|
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
tokenStates: resolveTokenStates(this.tokenLines, this.transitions, this.highlightDimOpacity, this.highlightedIds),
|
|
627
|
-
highlightActive: this.highlightDimOpacity !== null || this.transitions.some(tr => tr.kind === "dim"),
|
|
628
|
-
});
|
|
629
|
-
}
|
|
630
|
-
};
|
|
631
|
-
__decorate([
|
|
632
|
-
property({ default: "" })
|
|
633
|
-
], Code.prototype, "code", void 0);
|
|
634
|
-
__decorate([
|
|
635
|
-
property({ default: "typescript" })
|
|
636
|
-
], Code.prototype, "language", void 0);
|
|
637
|
-
__decorate([
|
|
638
|
-
property({ default: "Fira Mono" })
|
|
639
|
-
], Code.prototype, "fontFamily", void 0);
|
|
640
|
-
__decorate([
|
|
641
|
-
property({ default: DefaultHighlightStyle.name })
|
|
642
|
-
], Code.prototype, "theme", void 0);
|
|
643
|
-
__decorate([
|
|
644
|
-
property({ default: 16 })
|
|
645
|
-
], Code.prototype, "fontSize", void 0);
|
|
646
|
-
__decorate([
|
|
647
|
-
property({ default: 1.6 })
|
|
648
|
-
], Code.prototype, "lineHeight", void 0);
|
|
649
|
-
__decorate([
|
|
650
|
-
property({ default: 1.1 })
|
|
651
|
-
], Code.prototype, "letterSpacing", void 0);
|
|
652
|
-
__decorate([
|
|
653
|
-
property({ default: false })
|
|
654
|
-
], Code.prototype, "showLineNumbers", void 0);
|
|
655
|
-
__decorate([
|
|
656
|
-
property({ default: 2 })
|
|
657
|
-
], Code.prototype, "lineNumberGap", void 0);
|
|
658
|
-
__decorate([
|
|
659
|
-
property({ default: 0, mapper: insetsOps.resolve, tween: insetsOps.lerp })
|
|
660
|
-
], Code.prototype, "padding", void 0);
|
|
661
|
-
__decorate([
|
|
662
|
-
command({ args: [{ key: "code", kind: "text", multiline: true }] })
|
|
663
|
-
], Code.prototype, "append", null);
|
|
664
|
-
__decorate([
|
|
665
|
-
command({ args: [{ key: "code", kind: "text", multiline: true }] })
|
|
666
|
-
], Code.prototype, "prepend", null);
|
|
667
|
-
__decorate([
|
|
668
|
-
command({
|
|
669
|
-
args: [
|
|
670
|
-
// Two controls, one argument: the method takes a `[line, col]`
|
|
671
|
-
// tuple, so the pair a host draws is declared *inside* the value it
|
|
672
|
-
// composes into rather than flattened beside it.
|
|
673
|
-
{
|
|
674
|
-
key: "position", kind: "point", properties: {
|
|
675
|
-
line: { kind: "number", default: 1, min: 1, step: 1 },
|
|
676
|
-
col: { kind: "number", default: 1, min: 1, step: 1 },
|
|
677
|
-
},
|
|
678
|
-
},
|
|
679
|
-
{ key: "code", kind: "text", default: "", multiline: true },
|
|
680
|
-
],
|
|
681
|
-
})
|
|
682
|
-
], Code.prototype, "insert", null);
|
|
683
|
-
__decorate([
|
|
684
|
-
command({ key: "remove", args: [{ key: "target", kind: "selector" }] })
|
|
685
|
-
], Code.prototype, "erase", null);
|
|
686
|
-
__decorate([
|
|
687
|
-
command({ args: [{ key: "target", kind: "selector" }, { key: "replacement", kind: "text", default: "", multiline: true }] })
|
|
688
|
-
], Code.prototype, "replace", null);
|
|
689
|
-
__decorate([
|
|
690
|
-
command({ args: [{ key: "target", kind: "selector" }, { key: "dim", kind: "number", default: 0.4, min: 0, max: 1, step: 0.01, scale: 100, unit: "%" }] })
|
|
691
|
-
], Code.prototype, "highlight", null);
|
|
692
|
-
__decorate([
|
|
693
|
-
command()
|
|
694
|
-
], Code.prototype, "resetHighlight", null);
|
|
695
|
-
Code = __decorate([
|
|
696
|
-
node({
|
|
697
|
-
key: "code",
|
|
698
|
-
parentKey: "node",
|
|
699
|
-
forkable: true,
|
|
700
|
-
layout: {
|
|
701
|
-
defaultWidthMode: "hug",
|
|
702
|
-
defaultHeightMode: "hug",
|
|
703
|
-
acceptsChildren: false,
|
|
704
|
-
},
|
|
705
|
-
})
|
|
706
|
-
], Code);
|
|
707
|
-
export { Code };
|
|
490
|
+
return result;
|
|
491
|
+
}
|
|
492
|
+
/** One band per range, over every line it touches; a range past the end of the listing gives none. */
|
|
493
|
+
function rowBandsIn(lines, codeRanges, color, radius) {
|
|
494
|
+
const count = lines.length;
|
|
495
|
+
const bands = [];
|
|
496
|
+
for (const range of codeRanges) {
|
|
497
|
+
const first = Math.max(1, Math.round(range.startLine ?? 1));
|
|
498
|
+
const last = Math.min(count, range.endLine == null || range.endLine === Infinity ? count : Math.round(range.endLine));
|
|
499
|
+
if (first > last)
|
|
500
|
+
continue;
|
|
501
|
+
bands.push({ startId: lines[first - 1].id, endId: lines[last - 1].id, color, radius });
|
|
502
|
+
}
|
|
503
|
+
return bands;
|
|
504
|
+
}
|
|
708
505
|
/**
|
|
709
|
-
*
|
|
710
|
-
*
|
|
711
|
-
* `CodeRangeChain`/plain array of ranges spreads as-is. `CodeRange` and
|
|
712
|
-
* `CodeRangeChain` are distinguished by iterability, not `instanceof` — a
|
|
713
|
-
* bare array of `CodeRange`s (no chain) works the same way.
|
|
506
|
+
* A single range (a plain object) as a one-element array; a chain or an array
|
|
507
|
+
* of ranges as itself; the `{ list }` a chain serialises as, as that chain.
|
|
714
508
|
*/
|
|
715
509
|
function normalizeCodeRanges(codeRange) {
|
|
716
510
|
const maybeIterable = codeRange;
|
|
717
511
|
if (typeof maybeIterable[Symbol.iterator] === "function") {
|
|
718
512
|
return [...codeRange];
|
|
719
513
|
}
|
|
514
|
+
const stored = codeRange.list;
|
|
515
|
+
if (Array.isArray(stored))
|
|
516
|
+
return [...stored];
|
|
720
517
|
return [codeRange];
|
|
721
518
|
}
|
|
722
519
|
const grammarLoaders = new Map();
|
|
@@ -730,4 +527,141 @@ function grammarLoader(language) {
|
|
|
730
527
|
}
|
|
731
528
|
return load;
|
|
732
529
|
}
|
|
530
|
+
/** By the node's state view, one object per node that `measure` and `render` are both handed. */
|
|
531
|
+
const measuredBy = new WeakMap();
|
|
532
|
+
function measuredOf(state) {
|
|
533
|
+
let found = measuredBy.get(state);
|
|
534
|
+
if (!found)
|
|
535
|
+
measuredBy.set(state, found = { advances: new TokenAdvanceCache(), measurer: null, scope: null, settled: null, edit: null });
|
|
536
|
+
return found;
|
|
537
|
+
}
|
|
538
|
+
function metricsOf(s) {
|
|
539
|
+
return {
|
|
540
|
+
fontSize: s.fontSize,
|
|
541
|
+
fontFamily: s.fontFamily,
|
|
542
|
+
lineHeight: s.lineHeight,
|
|
543
|
+
letterSpacing: s.letterSpacing,
|
|
544
|
+
padding: s.padding,
|
|
545
|
+
showLineNumbers: s.showLineNumbers,
|
|
546
|
+
lineNumberGap: s.lineNumberGap,
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* The one or two layouts a frame is drawn from: a settled frame has one, a
|
|
551
|
+
* frame inside an edit two (where every token was, and where it is going).
|
|
552
|
+
* `null` for a node never measured, which has nothing to measure them with.
|
|
553
|
+
*/
|
|
554
|
+
function layoutsOf(measured, view, m) {
|
|
555
|
+
const signature = measured.advances.signature(m.fontSize, m.fontFamily);
|
|
556
|
+
measured.advances.sync(signature);
|
|
557
|
+
const key = `${metricsSignature(m)}|${signature}`;
|
|
558
|
+
const scope = measured.scope;
|
|
559
|
+
if (!view.transition) {
|
|
560
|
+
const hit = measured.settled;
|
|
561
|
+
if (hit && hit.lines === view.lines && hit.key === key)
|
|
562
|
+
return { from: hit.layout, to: hit.layout };
|
|
563
|
+
if (!scope)
|
|
564
|
+
return null;
|
|
565
|
+
const layout = layoutCode(view.lines, m, measured.advances, scope);
|
|
566
|
+
measured.settled = { lines: view.lines, key, layout };
|
|
567
|
+
return { from: layout, to: layout };
|
|
568
|
+
}
|
|
569
|
+
const hit = measured.edit;
|
|
570
|
+
if (hit && hit.transition === view.transition && hit.key === key)
|
|
571
|
+
return hit;
|
|
572
|
+
if (!scope)
|
|
573
|
+
return null;
|
|
574
|
+
const both = {
|
|
575
|
+
transition: view.transition,
|
|
576
|
+
key,
|
|
577
|
+
from: layoutCode(view.transition.from, m, measured.advances, scope),
|
|
578
|
+
to: layoutCode(view.transition.to, m, measured.advances, scope),
|
|
579
|
+
};
|
|
580
|
+
measured.edit = both;
|
|
581
|
+
return both;
|
|
582
|
+
}
|
|
583
|
+
function measureListing(measurer, s, kit) {
|
|
584
|
+
const measured = measuredOf(s);
|
|
585
|
+
if (measured.measurer !== measurer) {
|
|
586
|
+
measured.measurer = measurer;
|
|
587
|
+
measured.scope = {
|
|
588
|
+
measureText: (text, fontSize, fontFamily, fontWeight, letterSpacing) => measurer.text(text, { fontFamily, fontSize, fontWeight, letterSpacing }),
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
const view = shown(s, kit);
|
|
592
|
+
const m = metricsOf(s);
|
|
593
|
+
const layouts = layoutsOf(measured, view, m);
|
|
594
|
+
if (!layouts)
|
|
595
|
+
return { width: 0, height: 0 };
|
|
596
|
+
const t = moveProgress(view.transition ? { phases: view.transition.phases, progress: view.progress } : null);
|
|
597
|
+
const innerW = view.transition ? lerpNumber(layouts.from.innerW, layouts.to.innerW, t) : layouts.to.innerW;
|
|
598
|
+
const innerH = view.transition ? lerpNumber(layouts.from.innerH, layouts.to.innerH, t) : layouts.to.innerH;
|
|
599
|
+
return { width: innerW + m.padding.left + m.padding.right, height: innerH + m.padding.top + m.padding.bottom };
|
|
600
|
+
}
|
|
601
|
+
const NO_PAINT = { fill: [], stroke: [], shadow: [], overlay: [], start: 0, end: 1, clip: false };
|
|
602
|
+
const NOTHING = () => { };
|
|
603
|
+
function drawListing(ctx, s, kit) {
|
|
604
|
+
const measured = measuredOf(s);
|
|
605
|
+
const view = shown(s, kit);
|
|
606
|
+
const m = metricsOf(s);
|
|
607
|
+
const layouts = layoutsOf(measured, view, m);
|
|
608
|
+
// Paints nothing: it declares the outline picking and backdrop effects read.
|
|
609
|
+
Paint.draw(ctx, NO_PAINT, null, { clipShape: outlineOf(view, layouts, m, ctx.size), children: NOTHING });
|
|
610
|
+
const scope = measured.scope;
|
|
611
|
+
if (!layouts || !scope)
|
|
612
|
+
return;
|
|
613
|
+
const { focus, banding } = s;
|
|
614
|
+
drawCode({ draw: (graphics) => ctx.draw(graphics), measureText: scope.measureText }, {
|
|
615
|
+
tokenLines: view.lines,
|
|
616
|
+
metrics: m,
|
|
617
|
+
advanceCache: measured.advances,
|
|
618
|
+
from: layouts.from,
|
|
619
|
+
to: layouts.to,
|
|
620
|
+
edit: editOf(view),
|
|
621
|
+
tokenStates: resolveTokenStates(view.lines, focus.fade ? [focus.fade] : [], focus.dim, focus.ids),
|
|
622
|
+
highlightActive: focus.dim !== null || focus.fade !== null,
|
|
623
|
+
rowBands: banding.bands,
|
|
624
|
+
rowsEdit: banding.fade,
|
|
625
|
+
boxW: ctx.size.width,
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* A listing is grabbed by its text, line by ragged line: each line a slot as
|
|
630
|
+
* wide as its ink, plus the gutter where numbers show, inside the box. Code is
|
|
631
|
+
* mostly whitespace, and its empty corners would swallow clicks meant for what
|
|
632
|
+
* is behind it. Mid-edit, or before it is laid out, the box.
|
|
633
|
+
*/
|
|
634
|
+
function outlineOf(view, layouts, m, size) {
|
|
635
|
+
const box = { x: 0, y: 0, width: size.width, height: size.height };
|
|
636
|
+
if (view.transition || !layouts)
|
|
637
|
+
return new Graphics2D().rect(box);
|
|
638
|
+
const layout = layouts.to;
|
|
639
|
+
const half = (m.fontSize * m.lineHeight) / 2;
|
|
640
|
+
const gutter = m.showLineNumbers ? layout.gutter : 0;
|
|
641
|
+
const left = Math.max(layout.startX - gutter, -size.width / 2);
|
|
642
|
+
const outline = new Graphics2D();
|
|
643
|
+
let lines = 0;
|
|
644
|
+
for (let i = 0; i < layout.lineY.length; i++) {
|
|
645
|
+
const ink = layout.lineW[i] ?? 0;
|
|
646
|
+
if (ink + gutter <= 0)
|
|
647
|
+
continue;
|
|
648
|
+
const right = Math.min(layout.startX + ink, size.width / 2);
|
|
649
|
+
const y = layout.lineY[i];
|
|
650
|
+
const top = Math.min(y + half, size.height / 2);
|
|
651
|
+
const bottom = Math.max(y - half, -size.height / 2);
|
|
652
|
+
if (right < left || top < bottom)
|
|
653
|
+
continue;
|
|
654
|
+
// The row's own centre where the box leaves it whole, so a whole row is tested at its own centre.
|
|
655
|
+
const whole = top === y + half && bottom === y - half;
|
|
656
|
+
outline.rect({
|
|
657
|
+
x: (left + right) / 2,
|
|
658
|
+
y: whole ? y : (top + bottom) / 2,
|
|
659
|
+
width: right - left,
|
|
660
|
+
height: whole ? 2 * half : top - bottom,
|
|
661
|
+
});
|
|
662
|
+
lines++;
|
|
663
|
+
}
|
|
664
|
+
// No line has ink: the box with itself cut away, so nothing is grabbed (an empty outline would fall back to the box).
|
|
665
|
+
return lines > 0 ? outline : new Graphics2D().rect(box).rect(box).cut();
|
|
666
|
+
}
|
|
733
667
|
//# sourceMappingURL=node.js.map
|