@motionscript/code 0.1.0-alpha.8 → 0.1.0-alpha.9
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 +2 -0
- package/LICENSE +201 -201
- package/NOTICE +49 -49
- package/README.md +175 -176
- package/dist/browser/index.js.map +1 -1
- package/dist/code-range.js.map +1 -1
- package/dist/diff.js.map +1 -1
- package/dist/engine.js +3 -3
- package/dist/engine.js.map +1 -1
- package/dist/highlight.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/layout.js.map +1 -1
- package/dist/node.js.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/dist/render.js.map +1 -1
- package/dist/style.js.map +1 -1
- package/dist/tokens.js.map +1 -1
- package/dist/transitions.js.map +1 -1
- package/package.json +4 -4
- package/registry.json +6 -6
- package/src/code-range.ts +165 -165
- package/src/diff.ts +351 -351
- package/src/engine.ts +21 -21
- package/src/highlight.ts +340 -340
- package/src/index.ts +3 -3
- package/src/layout.ts +157 -157
- package/src/node.ts +854 -854
- package/src/nodes.ts +16 -16
- package/src/render.ts +315 -315
- package/src/style.ts +255 -255
- package/src/tokens.ts +79 -79
- package/src/transitions.ts +204 -204
package/src/render.ts
CHANGED
|
@@ -1,315 +1,315 @@
|
|
|
1
|
-
import { RenderContext2D, Graphics2D, NormalizedColor, parseColor, lerpNumber } from "@motionscript/sdk";
|
|
2
|
-
import type { CodeLayout, CodeMetrics } from "./layout";
|
|
3
|
-
import type { IdLine } from "./tokens";
|
|
4
|
-
import { windowProgress, moveProgress } from "./transitions";
|
|
5
|
-
import type { TokenState, StructuralTransition, RowBand, RowsTransition } from "./transitions";
|
|
6
|
-
import type { TokenAdvanceCache } from "./measure-cache";
|
|
7
|
-
|
|
8
|
-
/** Colour of a token the grammar had no opinion about. */
|
|
9
|
-
const DEFAULT_TOKEN_COLOR: NormalizedColor = [0.82, 0.84, 0.86, 1];
|
|
10
|
-
/** Gutter line-number colour: a muted version of the standard text colour. */
|
|
11
|
-
const LINE_NUMBER_COLOR: NormalizedColor = [0.45, 0.5, 0.55, 1];
|
|
12
|
-
|
|
13
|
-
// `parseColor` walks a CSS string; a listing asks for the same handful of theme
|
|
14
|
-
// colours on every token of every frame, so the parse is memoized. Module-level,
|
|
15
|
-
// because the answer depends only on the string.
|
|
16
|
-
const colorCache = new Map<string, NormalizedColor>();
|
|
17
|
-
function tokenColor(css: string | undefined): NormalizedColor {
|
|
18
|
-
if (!css) return DEFAULT_TOKEN_COLOR;
|
|
19
|
-
let hit = colorCache.get(css);
|
|
20
|
-
if (!hit) {
|
|
21
|
-
hit = parseColor(css);
|
|
22
|
-
colorCache.set(css, hit);
|
|
23
|
-
}
|
|
24
|
-
return hit;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function lerpColor(from: NormalizedColor, to: NormalizedColor, t: number): NormalizedColor {
|
|
28
|
-
if (t <= 0) return from;
|
|
29
|
-
if (t >= 1) return to;
|
|
30
|
-
return [
|
|
31
|
-
from[0] + (to[0] - from[0]) * t,
|
|
32
|
-
from[1] + (to[1] - from[1]) * t,
|
|
33
|
-
from[2] + (to[2] - from[2]) * t,
|
|
34
|
-
from[3] + (to[3] - from[3]) * t,
|
|
35
|
-
];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Opacity multiplier for a line's number under the active highlight. The
|
|
39
|
-
// number stays bright only when the WHOLE line is highlighted, so we take the
|
|
40
|
-
// min token opacity on the line. Returns 1 (no dimming) when no highlight is
|
|
41
|
-
// active, so an edit's entries don't drag the number down.
|
|
42
|
-
function lineHighlightOpacity(line: IdLine, stateById: Map<number, TokenState>, highlightActive: boolean): number {
|
|
43
|
-
if (!highlightActive) return 1;
|
|
44
|
-
let min = 1;
|
|
45
|
-
for (const tok of line.tokens) {
|
|
46
|
-
if (tok.content.length === 0) continue;
|
|
47
|
-
const op = stateById.get(tok.id)?.opacity ?? 1;
|
|
48
|
-
if (op < min) min = op;
|
|
49
|
-
}
|
|
50
|
-
return min;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** Everything a `Code` node's own instance state supplies to draw one frame. */
|
|
54
|
-
export interface DrawCodeState {
|
|
55
|
-
tokenLines: IdLine[];
|
|
56
|
-
metrics: CodeMetrics;
|
|
57
|
-
advanceCache: TokenAdvanceCache;
|
|
58
|
-
/** The one or two layouts this frame draws from — see `frameLayout` in `layout.ts`. */
|
|
59
|
-
from: CodeLayout;
|
|
60
|
-
to: CodeLayout;
|
|
61
|
-
edit: StructuralTransition | null;
|
|
62
|
-
/** Per-token opacity from `resolveTokenStates` (persistent highlight + any active dim transition). */
|
|
63
|
-
tokenStates: Map<number, TokenState>;
|
|
64
|
-
/** Whether a highlight (persistent dim, or a highlight()/resetHighlight() cross-fade) is currently engaged. */
|
|
65
|
-
highlightActive: boolean;
|
|
66
|
-
/** The settled row bands, and the change between two sets of them in flight, if any. */
|
|
67
|
-
rowBands?: RowBand[];
|
|
68
|
-
rowsEdit?: RowsTransition | null;
|
|
69
|
-
/** The node's laid-out width, which a band spans when it is wider than the block. */
|
|
70
|
-
boxW?: number;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** What a frame is drawn onto: somewhere to draw, and the face's measurements. A render context is one. */
|
|
74
|
-
export type CodeCanvas = Pick<RenderContext2D, "draw" | "measureText">;
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Draw one frame of a code listing: the settled structure, or — mid
|
|
78
|
-
* structural edit — the two structures it is interpolating between (what
|
|
79
|
-
* stays reflows, what is leaving fades out where it was, what is arriving
|
|
80
|
-
* fades/slides in where it lands). Pure over `state`; touches nothing but
|
|
81
|
-
* `draw`.
|
|
82
|
-
*/
|
|
83
|
-
export function drawCode(draw: CodeCanvas, state: DrawCodeState): void {
|
|
84
|
-
const { tokenLines, metrics, advanceCache, from, to, edit, tokenStates: dim } = state;
|
|
85
|
-
|
|
86
|
-
const progress = edit ? edit.progress : 1;
|
|
87
|
-
const pOut = edit ? windowProgress(edit.phases.out, progress) : 1;
|
|
88
|
-
const pMove = moveProgress(edit);
|
|
89
|
-
const pIn = edit ? windowProgress(edit.phases.in, progress) : 1;
|
|
90
|
-
|
|
91
|
-
const blockW = edit ? lerpNumber(from.blockW, to.blockW, pMove) : to.blockW;
|
|
92
|
-
const gutter = edit ? lerpNumber(from.gutter, to.gutter, pMove) : to.gutter;
|
|
93
|
-
const gutterGap = to.gutterGap || from.gutterGap;
|
|
94
|
-
// Right edge of the line-number column: one gap to the left of where the
|
|
95
|
-
// code text begins.
|
|
96
|
-
const numberRight = -blockW / 2 + metrics.padding.left + gutter - gutterGap;
|
|
97
|
-
|
|
98
|
-
drawRowBands(draw, state, Math.max(blockW, state.boxW ?? 0), pMove);
|
|
99
|
-
|
|
100
|
-
const drawNumber = (label: string, y: number, opacity: number): void => {
|
|
101
|
-
if (opacity <= 0) return;
|
|
102
|
-
const labelW = advanceCache.advance(draw, label, metrics.fontSize, metrics.fontFamily, 0);
|
|
103
|
-
draw.draw(new Graphics2D()
|
|
104
|
-
.text({
|
|
105
|
-
text: label,
|
|
106
|
-
fontSize: metrics.fontSize,
|
|
107
|
-
fontFamily: metrics.fontFamily,
|
|
108
|
-
lineHeight: metrics.lineHeight,
|
|
109
|
-
x: numberRight - labelW / 2,
|
|
110
|
-
y,
|
|
111
|
-
textAlign: 'left',
|
|
112
|
-
})
|
|
113
|
-
.fill([{ type: "solid", color: LINE_NUMBER_COLOR, opacity }]));
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const drawToken = (text: string, x: number, y: number, color: NormalizedColor, opacity: number): void => {
|
|
117
|
-
if (opacity <= 0 || text.length === 0) return;
|
|
118
|
-
const width = advanceCache.advance(draw, text, metrics.fontSize, metrics.fontFamily, metrics.letterSpacing);
|
|
119
|
-
draw.draw(new Graphics2D()
|
|
120
|
-
.text({
|
|
121
|
-
text,
|
|
122
|
-
fontSize: metrics.fontSize,
|
|
123
|
-
fontFamily: metrics.fontFamily,
|
|
124
|
-
lineHeight: metrics.lineHeight,
|
|
125
|
-
letterSpacing: metrics.letterSpacing,
|
|
126
|
-
// The renderer centres a single-token block on the (x, y) it is
|
|
127
|
-
// given, so every token is anchored at the centre of its cell
|
|
128
|
-
// rather than at its left edge. Passing lineHeight makes the
|
|
129
|
-
// block's height deterministic (fontSize × lineHeight) rather
|
|
130
|
-
// than the font's natural metrics, so the vertical centre
|
|
131
|
-
// lands exactly on the slot centre.
|
|
132
|
-
x: x + width / 2,
|
|
133
|
-
y,
|
|
134
|
-
textAlign: 'left',
|
|
135
|
-
})
|
|
136
|
-
.fill([{ type: "solid", color, opacity }]));
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
// What survived the edit, and what it brought with it — drawn from the
|
|
140
|
-
// structure the edit lands on.
|
|
141
|
-
for (let i = 0; i < tokenLines.length; i++) {
|
|
142
|
-
const line = tokenLines[i];
|
|
143
|
-
const toIndex = to.lineIndex.get(line.id);
|
|
144
|
-
if (toIndex === undefined) continue;
|
|
145
|
-
const toY = to.lineY[toIndex];
|
|
146
|
-
|
|
147
|
-
const entering = edit?.enteringLineIds.has(line.id) ?? false;
|
|
148
|
-
let rowY = toY;
|
|
149
|
-
let rowAlpha = 1;
|
|
150
|
-
if (edit && entering) {
|
|
151
|
-
// A wholly new row fades as one piece, in the place it lands — the
|
|
152
|
-
// line arrives, it does not travel to get there, and it is not a
|
|
153
|
-
// spray of glyphs each finding its own way in. See
|
|
154
|
-
// `StructuralTransition.enteringLineIds`.
|
|
155
|
-
rowAlpha = pIn;
|
|
156
|
-
} else if (edit) {
|
|
157
|
-
const fromIndex = from.lineIndex.get(line.id);
|
|
158
|
-
if (fromIndex !== undefined) rowY = lerpNumber(from.lineY[fromIndex], toY, pMove);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
for (const token of line.tokens) {
|
|
162
|
-
const box = to.tokens.get(token.id);
|
|
163
|
-
if (!box) continue;
|
|
164
|
-
|
|
165
|
-
let x = box.x;
|
|
166
|
-
let alpha = rowAlpha;
|
|
167
|
-
let color = tokenColor(token.color);
|
|
168
|
-
|
|
169
|
-
if (edit && !entering) {
|
|
170
|
-
if (edit.addedIds.has(token.id)) {
|
|
171
|
-
// Spliced into a row that already existed: the reflow
|
|
172
|
-
// already opened the space, so this only has to appear.
|
|
173
|
-
alpha = pIn;
|
|
174
|
-
} else {
|
|
175
|
-
const was = from.tokens.get(token.id);
|
|
176
|
-
if (was) x = lerpNumber(was.x, box.x, pMove);
|
|
177
|
-
if (edit.fromColorById.has(token.id)) {
|
|
178
|
-
color = lerpColor(tokenColor(edit.fromColorById.get(token.id)), color, pMove);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
drawToken(token.content, x, rowY, color, alpha * (dim.get(token.id)?.opacity ?? 1));
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// What the edit takes away, drawn from the structure it is leaving — it
|
|
188
|
-
// has no place in the new one, which is the whole reason it is fading.
|
|
189
|
-
if (edit && pOut < 1) {
|
|
190
|
-
for (let i = 0; i < edit.from.length; i++) {
|
|
191
|
-
const line = edit.from[i];
|
|
192
|
-
const fromIndex = from.lineIndex.get(line.id);
|
|
193
|
-
if (fromIndex === undefined) continue;
|
|
194
|
-
const toIndex = to.lineIndex.get(line.id);
|
|
195
|
-
// A row that survived the edit is still moving, so its outgoing
|
|
196
|
-
// tokens travel with it; a deleted row stays where it was and
|
|
197
|
-
// dissolves while the rows below close over it.
|
|
198
|
-
const y = toIndex === undefined
|
|
199
|
-
? from.lineY[fromIndex]
|
|
200
|
-
: lerpNumber(from.lineY[fromIndex], to.lineY[toIndex], pMove);
|
|
201
|
-
|
|
202
|
-
for (const token of line.tokens) {
|
|
203
|
-
if (!edit.removedIds.has(token.id)) continue;
|
|
204
|
-
const box = from.tokens.get(token.id);
|
|
205
|
-
if (!box) continue;
|
|
206
|
-
drawToken(token.content, box.x, y, tokenColor(token.color), 1 - pOut);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (metrics.showLineNumbers) drawLineNumbers(state, drawNumber, pMove, pIn, pOut);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/** A band's top and bottom edge this frame, or `null` when neither of its rows is in the listing any more. */
|
|
215
|
-
function bandEdges(band: RowBand, state: DrawCodeState, pMove: number): { top: number; bottom: number } | null {
|
|
216
|
-
const { from, to, metrics } = state;
|
|
217
|
-
const half = (metrics.fontSize * metrics.lineHeight) / 2;
|
|
218
|
-
const centre = (id: number): number | null => {
|
|
219
|
-
const toIndex = to.lineIndex.get(id);
|
|
220
|
-
const fromIndex = from.lineIndex.get(id);
|
|
221
|
-
if (toIndex !== undefined && fromIndex !== undefined) return lerpNumber(from.lineY[fromIndex], to.lineY[toIndex], pMove);
|
|
222
|
-
if (toIndex !== undefined) return to.lineY[toIndex];
|
|
223
|
-
return fromIndex === undefined ? null : from.lineY[fromIndex];
|
|
224
|
-
};
|
|
225
|
-
const first = centre(band.startId);
|
|
226
|
-
const last = centre(band.endId);
|
|
227
|
-
if (first === null && last === null) return null;
|
|
228
|
-
return { top: (first ?? last as number) + half, bottom: (last ?? first as number) - half };
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Draw the row bands, under the text and across the whole block. A band rides
|
|
233
|
-
* its rows through a structural edit; between two sets, bands paired by position
|
|
234
|
-
* slide and recolour while an unpaired one fades.
|
|
235
|
-
*/
|
|
236
|
-
function drawRowBands(draw: CodeCanvas, state: DrawCodeState, width: number, pMove: number): void {
|
|
237
|
-
const { rowBands = [], rowsEdit = null } = state;
|
|
238
|
-
const paint = (top: number, bottom: number, color: NormalizedColor, radius: number, opacity: number): void => {
|
|
239
|
-
if (opacity <= 0 || top <= bottom) return;
|
|
240
|
-
draw.draw(new Graphics2D()
|
|
241
|
-
.rect({ x: 0, y: (top + bottom) / 2, width, height: top - bottom, cornerRadius: radius })
|
|
242
|
-
.fill([{ type: "solid", color, opacity }]));
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
if (!rowsEdit) {
|
|
246
|
-
for (const band of rowBands) {
|
|
247
|
-
const edges = bandEdges(band, state, pMove);
|
|
248
|
-
if (edges) paint(edges.top, edges.bottom, band.color, band.radius, 1);
|
|
249
|
-
}
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
const p = rowsEdit.progress;
|
|
254
|
-
for (let i = 0; i < Math.max(rowsEdit.from.length, rowsEdit.to.length); i++) {
|
|
255
|
-
const was = rowsEdit.from[i];
|
|
256
|
-
const next = rowsEdit.to[i];
|
|
257
|
-
const a = was ? bandEdges(was, state, pMove) : null;
|
|
258
|
-
const b = next ? bandEdges(next, state, pMove) : null;
|
|
259
|
-
if (a && b) {
|
|
260
|
-
paint(
|
|
261
|
-
lerpNumber(a.top, b.top, p), lerpNumber(a.bottom, b.bottom, p),
|
|
262
|
-
lerpColor(was.color, next.color, p), lerpNumber(was.radius, next.radius, p), 1,
|
|
263
|
-
);
|
|
264
|
-
} else if (a) {
|
|
265
|
-
paint(a.top, a.bottom, was.color, was.radius, 1 - p);
|
|
266
|
-
} else if (b) {
|
|
267
|
-
paint(b.top, b.bottom, next.color, next.radius, p);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* Draw the gutter — one number per *slot*, not one per row.
|
|
274
|
-
*
|
|
275
|
-
* That distinction is the whole of it. A line number is an ordinal of the
|
|
276
|
-
* block: "3" means the third line, and it is there for exactly as long as the
|
|
277
|
-
* listing has a third line. Drawing it alongside the row it labels — the
|
|
278
|
-
* obvious thing, since that is where it appears — tied it to the row's identity
|
|
279
|
-
* instead, so a line rewritten past the point the diff could pair it took its
|
|
280
|
-
* number down with it and brought an identical one back up. The number blinked
|
|
281
|
-
* although it had never gone away, and every substantially rewritten line did
|
|
282
|
-
* it at once.
|
|
283
|
-
*
|
|
284
|
-
* Numbering the slots means only the numbers at the end move at all: those the
|
|
285
|
-
* block grew to reach fade in, those it shrank past fade out, and every number
|
|
286
|
-
* both structures have simply travels to its new row height. Which is also the
|
|
287
|
-
* honest reading of the edit — inserting a line in the middle does not make a
|
|
288
|
-
* new number appear there, it makes the block one line longer.
|
|
289
|
-
*/
|
|
290
|
-
function drawLineNumbers(
|
|
291
|
-
state: DrawCodeState,
|
|
292
|
-
drawNumber: (label: string, y: number, opacity: number) => void,
|
|
293
|
-
pMove: number,
|
|
294
|
-
pIn: number,
|
|
295
|
-
pOut: number,
|
|
296
|
-
): void {
|
|
297
|
-
const { tokenLines, from, to, edit, tokenStates: dim, highlightActive } = state;
|
|
298
|
-
|
|
299
|
-
const toCount = tokenLines.length;
|
|
300
|
-
const fromCount = edit ? edit.from.length : toCount;
|
|
301
|
-
|
|
302
|
-
for (let i = 0; i < Math.max(fromCount, toCount); i++) {
|
|
303
|
-
const inFrom = i < fromCount;
|
|
304
|
-
const inTo = i < toCount;
|
|
305
|
-
// Highlight dimming follows whatever content occupies the slot, which
|
|
306
|
-
// is the new listing wherever there is one.
|
|
307
|
-
const line = inTo ? tokenLines[i] : edit?.from[i];
|
|
308
|
-
const dimming = line ? lineHighlightOpacity(line, dim, highlightActive) : 1;
|
|
309
|
-
|
|
310
|
-
if (!edit) drawNumber(String(i + 1), to.lineY[i], dimming);
|
|
311
|
-
else if (inFrom && inTo) drawNumber(String(i + 1), lerpNumber(from.lineY[i], to.lineY[i], pMove), dimming);
|
|
312
|
-
else if (inTo) drawNumber(String(i + 1), to.lineY[i], pIn * dimming);
|
|
313
|
-
else drawNumber(String(i + 1), from.lineY[i], (1 - pOut) * dimming);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
1
|
+
import { RenderContext2D, Graphics2D, NormalizedColor, parseColor, lerpNumber } from "@motionscript/sdk";
|
|
2
|
+
import type { CodeLayout, CodeMetrics } from "./layout";
|
|
3
|
+
import type { IdLine } from "./tokens";
|
|
4
|
+
import { windowProgress, moveProgress } from "./transitions";
|
|
5
|
+
import type { TokenState, StructuralTransition, RowBand, RowsTransition } from "./transitions";
|
|
6
|
+
import type { TokenAdvanceCache } from "./measure-cache";
|
|
7
|
+
|
|
8
|
+
/** Colour of a token the grammar had no opinion about. */
|
|
9
|
+
const DEFAULT_TOKEN_COLOR: NormalizedColor = [0.82, 0.84, 0.86, 1];
|
|
10
|
+
/** Gutter line-number colour: a muted version of the standard text colour. */
|
|
11
|
+
const LINE_NUMBER_COLOR: NormalizedColor = [0.45, 0.5, 0.55, 1];
|
|
12
|
+
|
|
13
|
+
// `parseColor` walks a CSS string; a listing asks for the same handful of theme
|
|
14
|
+
// colours on every token of every frame, so the parse is memoized. Module-level,
|
|
15
|
+
// because the answer depends only on the string.
|
|
16
|
+
const colorCache = new Map<string, NormalizedColor>();
|
|
17
|
+
function tokenColor(css: string | undefined): NormalizedColor {
|
|
18
|
+
if (!css) return DEFAULT_TOKEN_COLOR;
|
|
19
|
+
let hit = colorCache.get(css);
|
|
20
|
+
if (!hit) {
|
|
21
|
+
hit = parseColor(css);
|
|
22
|
+
colorCache.set(css, hit);
|
|
23
|
+
}
|
|
24
|
+
return hit;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function lerpColor(from: NormalizedColor, to: NormalizedColor, t: number): NormalizedColor {
|
|
28
|
+
if (t <= 0) return from;
|
|
29
|
+
if (t >= 1) return to;
|
|
30
|
+
return [
|
|
31
|
+
from[0] + (to[0] - from[0]) * t,
|
|
32
|
+
from[1] + (to[1] - from[1]) * t,
|
|
33
|
+
from[2] + (to[2] - from[2]) * t,
|
|
34
|
+
from[3] + (to[3] - from[3]) * t,
|
|
35
|
+
];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Opacity multiplier for a line's number under the active highlight. The
|
|
39
|
+
// number stays bright only when the WHOLE line is highlighted, so we take the
|
|
40
|
+
// min token opacity on the line. Returns 1 (no dimming) when no highlight is
|
|
41
|
+
// active, so an edit's entries don't drag the number down.
|
|
42
|
+
function lineHighlightOpacity(line: IdLine, stateById: Map<number, TokenState>, highlightActive: boolean): number {
|
|
43
|
+
if (!highlightActive) return 1;
|
|
44
|
+
let min = 1;
|
|
45
|
+
for (const tok of line.tokens) {
|
|
46
|
+
if (tok.content.length === 0) continue;
|
|
47
|
+
const op = stateById.get(tok.id)?.opacity ?? 1;
|
|
48
|
+
if (op < min) min = op;
|
|
49
|
+
}
|
|
50
|
+
return min;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Everything a `Code` node's own instance state supplies to draw one frame. */
|
|
54
|
+
export interface DrawCodeState {
|
|
55
|
+
tokenLines: IdLine[];
|
|
56
|
+
metrics: CodeMetrics;
|
|
57
|
+
advanceCache: TokenAdvanceCache;
|
|
58
|
+
/** The one or two layouts this frame draws from — see `frameLayout` in `layout.ts`. */
|
|
59
|
+
from: CodeLayout;
|
|
60
|
+
to: CodeLayout;
|
|
61
|
+
edit: StructuralTransition | null;
|
|
62
|
+
/** Per-token opacity from `resolveTokenStates` (persistent highlight + any active dim transition). */
|
|
63
|
+
tokenStates: Map<number, TokenState>;
|
|
64
|
+
/** Whether a highlight (persistent dim, or a highlight()/resetHighlight() cross-fade) is currently engaged. */
|
|
65
|
+
highlightActive: boolean;
|
|
66
|
+
/** The settled row bands, and the change between two sets of them in flight, if any. */
|
|
67
|
+
rowBands?: RowBand[];
|
|
68
|
+
rowsEdit?: RowsTransition | null;
|
|
69
|
+
/** The node's laid-out width, which a band spans when it is wider than the block. */
|
|
70
|
+
boxW?: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** What a frame is drawn onto: somewhere to draw, and the face's measurements. A render context is one. */
|
|
74
|
+
export type CodeCanvas = Pick<RenderContext2D, "draw" | "measureText">;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Draw one frame of a code listing: the settled structure, or — mid
|
|
78
|
+
* structural edit — the two structures it is interpolating between (what
|
|
79
|
+
* stays reflows, what is leaving fades out where it was, what is arriving
|
|
80
|
+
* fades/slides in where it lands). Pure over `state`; touches nothing but
|
|
81
|
+
* `draw`.
|
|
82
|
+
*/
|
|
83
|
+
export function drawCode(draw: CodeCanvas, state: DrawCodeState): void {
|
|
84
|
+
const { tokenLines, metrics, advanceCache, from, to, edit, tokenStates: dim } = state;
|
|
85
|
+
|
|
86
|
+
const progress = edit ? edit.progress : 1;
|
|
87
|
+
const pOut = edit ? windowProgress(edit.phases.out, progress) : 1;
|
|
88
|
+
const pMove = moveProgress(edit);
|
|
89
|
+
const pIn = edit ? windowProgress(edit.phases.in, progress) : 1;
|
|
90
|
+
|
|
91
|
+
const blockW = edit ? lerpNumber(from.blockW, to.blockW, pMove) : to.blockW;
|
|
92
|
+
const gutter = edit ? lerpNumber(from.gutter, to.gutter, pMove) : to.gutter;
|
|
93
|
+
const gutterGap = to.gutterGap || from.gutterGap;
|
|
94
|
+
// Right edge of the line-number column: one gap to the left of where the
|
|
95
|
+
// code text begins.
|
|
96
|
+
const numberRight = -blockW / 2 + metrics.padding.left + gutter - gutterGap;
|
|
97
|
+
|
|
98
|
+
drawRowBands(draw, state, Math.max(blockW, state.boxW ?? 0), pMove);
|
|
99
|
+
|
|
100
|
+
const drawNumber = (label: string, y: number, opacity: number): void => {
|
|
101
|
+
if (opacity <= 0) return;
|
|
102
|
+
const labelW = advanceCache.advance(draw, label, metrics.fontSize, metrics.fontFamily, 0);
|
|
103
|
+
draw.draw(new Graphics2D()
|
|
104
|
+
.text({
|
|
105
|
+
text: label,
|
|
106
|
+
fontSize: metrics.fontSize,
|
|
107
|
+
fontFamily: metrics.fontFamily,
|
|
108
|
+
lineHeight: metrics.lineHeight,
|
|
109
|
+
x: numberRight - labelW / 2,
|
|
110
|
+
y,
|
|
111
|
+
textAlign: 'left',
|
|
112
|
+
})
|
|
113
|
+
.fill([{ type: "solid", color: LINE_NUMBER_COLOR, opacity }]));
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const drawToken = (text: string, x: number, y: number, color: NormalizedColor, opacity: number): void => {
|
|
117
|
+
if (opacity <= 0 || text.length === 0) return;
|
|
118
|
+
const width = advanceCache.advance(draw, text, metrics.fontSize, metrics.fontFamily, metrics.letterSpacing);
|
|
119
|
+
draw.draw(new Graphics2D()
|
|
120
|
+
.text({
|
|
121
|
+
text,
|
|
122
|
+
fontSize: metrics.fontSize,
|
|
123
|
+
fontFamily: metrics.fontFamily,
|
|
124
|
+
lineHeight: metrics.lineHeight,
|
|
125
|
+
letterSpacing: metrics.letterSpacing,
|
|
126
|
+
// The renderer centres a single-token block on the (x, y) it is
|
|
127
|
+
// given, so every token is anchored at the centre of its cell
|
|
128
|
+
// rather than at its left edge. Passing lineHeight makes the
|
|
129
|
+
// block's height deterministic (fontSize × lineHeight) rather
|
|
130
|
+
// than the font's natural metrics, so the vertical centre
|
|
131
|
+
// lands exactly on the slot centre.
|
|
132
|
+
x: x + width / 2,
|
|
133
|
+
y,
|
|
134
|
+
textAlign: 'left',
|
|
135
|
+
})
|
|
136
|
+
.fill([{ type: "solid", color, opacity }]));
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// What survived the edit, and what it brought with it — drawn from the
|
|
140
|
+
// structure the edit lands on.
|
|
141
|
+
for (let i = 0; i < tokenLines.length; i++) {
|
|
142
|
+
const line = tokenLines[i];
|
|
143
|
+
const toIndex = to.lineIndex.get(line.id);
|
|
144
|
+
if (toIndex === undefined) continue;
|
|
145
|
+
const toY = to.lineY[toIndex];
|
|
146
|
+
|
|
147
|
+
const entering = edit?.enteringLineIds.has(line.id) ?? false;
|
|
148
|
+
let rowY = toY;
|
|
149
|
+
let rowAlpha = 1;
|
|
150
|
+
if (edit && entering) {
|
|
151
|
+
// A wholly new row fades as one piece, in the place it lands — the
|
|
152
|
+
// line arrives, it does not travel to get there, and it is not a
|
|
153
|
+
// spray of glyphs each finding its own way in. See
|
|
154
|
+
// `StructuralTransition.enteringLineIds`.
|
|
155
|
+
rowAlpha = pIn;
|
|
156
|
+
} else if (edit) {
|
|
157
|
+
const fromIndex = from.lineIndex.get(line.id);
|
|
158
|
+
if (fromIndex !== undefined) rowY = lerpNumber(from.lineY[fromIndex], toY, pMove);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
for (const token of line.tokens) {
|
|
162
|
+
const box = to.tokens.get(token.id);
|
|
163
|
+
if (!box) continue;
|
|
164
|
+
|
|
165
|
+
let x = box.x;
|
|
166
|
+
let alpha = rowAlpha;
|
|
167
|
+
let color = tokenColor(token.color);
|
|
168
|
+
|
|
169
|
+
if (edit && !entering) {
|
|
170
|
+
if (edit.addedIds.has(token.id)) {
|
|
171
|
+
// Spliced into a row that already existed: the reflow
|
|
172
|
+
// already opened the space, so this only has to appear.
|
|
173
|
+
alpha = pIn;
|
|
174
|
+
} else {
|
|
175
|
+
const was = from.tokens.get(token.id);
|
|
176
|
+
if (was) x = lerpNumber(was.x, box.x, pMove);
|
|
177
|
+
if (edit.fromColorById.has(token.id)) {
|
|
178
|
+
color = lerpColor(tokenColor(edit.fromColorById.get(token.id)), color, pMove);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
drawToken(token.content, x, rowY, color, alpha * (dim.get(token.id)?.opacity ?? 1));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// What the edit takes away, drawn from the structure it is leaving — it
|
|
188
|
+
// has no place in the new one, which is the whole reason it is fading.
|
|
189
|
+
if (edit && pOut < 1) {
|
|
190
|
+
for (let i = 0; i < edit.from.length; i++) {
|
|
191
|
+
const line = edit.from[i];
|
|
192
|
+
const fromIndex = from.lineIndex.get(line.id);
|
|
193
|
+
if (fromIndex === undefined) continue;
|
|
194
|
+
const toIndex = to.lineIndex.get(line.id);
|
|
195
|
+
// A row that survived the edit is still moving, so its outgoing
|
|
196
|
+
// tokens travel with it; a deleted row stays where it was and
|
|
197
|
+
// dissolves while the rows below close over it.
|
|
198
|
+
const y = toIndex === undefined
|
|
199
|
+
? from.lineY[fromIndex]
|
|
200
|
+
: lerpNumber(from.lineY[fromIndex], to.lineY[toIndex], pMove);
|
|
201
|
+
|
|
202
|
+
for (const token of line.tokens) {
|
|
203
|
+
if (!edit.removedIds.has(token.id)) continue;
|
|
204
|
+
const box = from.tokens.get(token.id);
|
|
205
|
+
if (!box) continue;
|
|
206
|
+
drawToken(token.content, box.x, y, tokenColor(token.color), 1 - pOut);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (metrics.showLineNumbers) drawLineNumbers(state, drawNumber, pMove, pIn, pOut);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** A band's top and bottom edge this frame, or `null` when neither of its rows is in the listing any more. */
|
|
215
|
+
function bandEdges(band: RowBand, state: DrawCodeState, pMove: number): { top: number; bottom: number } | null {
|
|
216
|
+
const { from, to, metrics } = state;
|
|
217
|
+
const half = (metrics.fontSize * metrics.lineHeight) / 2;
|
|
218
|
+
const centre = (id: number): number | null => {
|
|
219
|
+
const toIndex = to.lineIndex.get(id);
|
|
220
|
+
const fromIndex = from.lineIndex.get(id);
|
|
221
|
+
if (toIndex !== undefined && fromIndex !== undefined) return lerpNumber(from.lineY[fromIndex], to.lineY[toIndex], pMove);
|
|
222
|
+
if (toIndex !== undefined) return to.lineY[toIndex];
|
|
223
|
+
return fromIndex === undefined ? null : from.lineY[fromIndex];
|
|
224
|
+
};
|
|
225
|
+
const first = centre(band.startId);
|
|
226
|
+
const last = centre(band.endId);
|
|
227
|
+
if (first === null && last === null) return null;
|
|
228
|
+
return { top: (first ?? last as number) + half, bottom: (last ?? first as number) - half };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Draw the row bands, under the text and across the whole block. A band rides
|
|
233
|
+
* its rows through a structural edit; between two sets, bands paired by position
|
|
234
|
+
* slide and recolour while an unpaired one fades.
|
|
235
|
+
*/
|
|
236
|
+
function drawRowBands(draw: CodeCanvas, state: DrawCodeState, width: number, pMove: number): void {
|
|
237
|
+
const { rowBands = [], rowsEdit = null } = state;
|
|
238
|
+
const paint = (top: number, bottom: number, color: NormalizedColor, radius: number, opacity: number): void => {
|
|
239
|
+
if (opacity <= 0 || top <= bottom) return;
|
|
240
|
+
draw.draw(new Graphics2D()
|
|
241
|
+
.rect({ x: 0, y: (top + bottom) / 2, width, height: top - bottom, cornerRadius: radius })
|
|
242
|
+
.fill([{ type: "solid", color, opacity }]));
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
if (!rowsEdit) {
|
|
246
|
+
for (const band of rowBands) {
|
|
247
|
+
const edges = bandEdges(band, state, pMove);
|
|
248
|
+
if (edges) paint(edges.top, edges.bottom, band.color, band.radius, 1);
|
|
249
|
+
}
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const p = rowsEdit.progress;
|
|
254
|
+
for (let i = 0; i < Math.max(rowsEdit.from.length, rowsEdit.to.length); i++) {
|
|
255
|
+
const was = rowsEdit.from[i];
|
|
256
|
+
const next = rowsEdit.to[i];
|
|
257
|
+
const a = was ? bandEdges(was, state, pMove) : null;
|
|
258
|
+
const b = next ? bandEdges(next, state, pMove) : null;
|
|
259
|
+
if (a && b) {
|
|
260
|
+
paint(
|
|
261
|
+
lerpNumber(a.top, b.top, p), lerpNumber(a.bottom, b.bottom, p),
|
|
262
|
+
lerpColor(was.color, next.color, p), lerpNumber(was.radius, next.radius, p), 1,
|
|
263
|
+
);
|
|
264
|
+
} else if (a) {
|
|
265
|
+
paint(a.top, a.bottom, was.color, was.radius, 1 - p);
|
|
266
|
+
} else if (b) {
|
|
267
|
+
paint(b.top, b.bottom, next.color, next.radius, p);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Draw the gutter — one number per *slot*, not one per row.
|
|
274
|
+
*
|
|
275
|
+
* That distinction is the whole of it. A line number is an ordinal of the
|
|
276
|
+
* block: "3" means the third line, and it is there for exactly as long as the
|
|
277
|
+
* listing has a third line. Drawing it alongside the row it labels — the
|
|
278
|
+
* obvious thing, since that is where it appears — tied it to the row's identity
|
|
279
|
+
* instead, so a line rewritten past the point the diff could pair it took its
|
|
280
|
+
* number down with it and brought an identical one back up. The number blinked
|
|
281
|
+
* although it had never gone away, and every substantially rewritten line did
|
|
282
|
+
* it at once.
|
|
283
|
+
*
|
|
284
|
+
* Numbering the slots means only the numbers at the end move at all: those the
|
|
285
|
+
* block grew to reach fade in, those it shrank past fade out, and every number
|
|
286
|
+
* both structures have simply travels to its new row height. Which is also the
|
|
287
|
+
* honest reading of the edit — inserting a line in the middle does not make a
|
|
288
|
+
* new number appear there, it makes the block one line longer.
|
|
289
|
+
*/
|
|
290
|
+
function drawLineNumbers(
|
|
291
|
+
state: DrawCodeState,
|
|
292
|
+
drawNumber: (label: string, y: number, opacity: number) => void,
|
|
293
|
+
pMove: number,
|
|
294
|
+
pIn: number,
|
|
295
|
+
pOut: number,
|
|
296
|
+
): void {
|
|
297
|
+
const { tokenLines, from, to, edit, tokenStates: dim, highlightActive } = state;
|
|
298
|
+
|
|
299
|
+
const toCount = tokenLines.length;
|
|
300
|
+
const fromCount = edit ? edit.from.length : toCount;
|
|
301
|
+
|
|
302
|
+
for (let i = 0; i < Math.max(fromCount, toCount); i++) {
|
|
303
|
+
const inFrom = i < fromCount;
|
|
304
|
+
const inTo = i < toCount;
|
|
305
|
+
// Highlight dimming follows whatever content occupies the slot, which
|
|
306
|
+
// is the new listing wherever there is one.
|
|
307
|
+
const line = inTo ? tokenLines[i] : edit?.from[i];
|
|
308
|
+
const dimming = line ? lineHighlightOpacity(line, dim, highlightActive) : 1;
|
|
309
|
+
|
|
310
|
+
if (!edit) drawNumber(String(i + 1), to.lineY[i], dimming);
|
|
311
|
+
else if (inFrom && inTo) drawNumber(String(i + 1), lerpNumber(from.lineY[i], to.lineY[i], pMove), dimming);
|
|
312
|
+
else if (inTo) drawNumber(String(i + 1), to.lineY[i], pIn * dimming);
|
|
313
|
+
else drawNumber(String(i + 1), from.lineY[i], (1 - pOut) * dimming);
|
|
314
|
+
}
|
|
315
|
+
}
|