@motionscript/code 0.0.0-stage → 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 +101 -0
- package/LICENSE +201 -0
- package/NOTICE +49 -0
- package/README.md +174 -3
- package/dist/browser/THIRD_PARTY_LICENSES.txt +431 -0
- package/dist/browser/chunks/chunk-HET2RCSD.js +2 -0
- package/dist/browser/chunks/chunk-HET2RCSD.js.map +7 -0
- package/dist/browser/chunks/chunk-SYZUZB3M.js +2 -0
- package/dist/browser/chunks/chunk-SYZUZB3M.js.map +7 -0
- package/dist/browser/chunks/dist-3F5TQHM6.js +2 -0
- package/dist/browser/chunks/dist-3F5TQHM6.js.map +7 -0
- package/dist/browser/chunks/dist-3SIR23P4.js +2 -0
- package/dist/browser/chunks/dist-3SIR23P4.js.map +7 -0
- package/dist/browser/chunks/dist-AQRNACF3.js +2 -0
- package/dist/browser/chunks/dist-AQRNACF3.js.map +7 -0
- package/dist/browser/chunks/dist-B46VH2YP.js +2 -0
- package/dist/browser/chunks/dist-B46VH2YP.js.map +7 -0
- package/dist/browser/chunks/dist-FH6BHJ6A.js +7 -0
- package/dist/browser/chunks/dist-FH6BHJ6A.js.map +7 -0
- package/dist/browser/chunks/dist-FQDUBZKU.js +2 -0
- package/dist/browser/chunks/dist-FQDUBZKU.js.map +7 -0
- package/dist/browser/chunks/dist-GPNEVCJI.js +2 -0
- package/dist/browser/chunks/dist-GPNEVCJI.js.map +7 -0
- package/dist/browser/chunks/dist-LRSXSDHT.js +2 -0
- package/dist/browser/chunks/dist-LRSXSDHT.js.map +7 -0
- package/dist/browser/chunks/dist-N4CNBNHL.js +2 -0
- package/dist/browser/chunks/dist-N4CNBNHL.js.map +7 -0
- package/dist/browser/chunks/dist-OXJDTPQ6.js +2 -0
- package/dist/browser/chunks/dist-OXJDTPQ6.js.map +7 -0
- package/dist/browser/chunks/dist-SH7H4MRK.js +2 -0
- package/dist/browser/chunks/dist-SH7H4MRK.js.map +7 -0
- package/dist/browser/chunks/dist-UG3NTKWL.js +2 -0
- package/dist/browser/chunks/dist-UG3NTKWL.js.map +7 -0
- package/dist/browser/chunks/dist-W5ZXIMV7.js +2 -0
- package/dist/browser/chunks/dist-W5ZXIMV7.js.map +7 -0
- package/dist/browser/index.js +11 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/browser/manifest.json +93 -0
- package/dist/code-range.d.ts +75 -0
- package/dist/code-range.d.ts.map +1 -0
- package/dist/code-range.js +132 -0
- package/dist/code-range.js.map +1 -0
- package/dist/diff.d.ts +56 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +311 -0
- package/dist/diff.js.map +1 -0
- package/dist/engine.d.ts +15 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +17 -0
- package/dist/engine.js.map +1 -0
- package/dist/highlight.d.ts +51 -0
- package/dist/highlight.d.ts.map +1 -0
- package/dist/highlight.js +277 -0
- package/dist/highlight.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/layout.d.ts +77 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +74 -0
- package/dist/layout.js.map +1 -0
- package/dist/measure-cache.d.ts +38 -0
- package/dist/measure-cache.d.ts.map +1 -0
- package/dist/measure-cache.js +0 -0
- package/dist/measure-cache.js.map +1 -0
- package/dist/node.d.ts +287 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +667 -0
- package/dist/node.js.map +1 -0
- package/dist/nodes.d.ts +15 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +16 -0
- package/dist/nodes.js.map +1 -0
- package/dist/render.d.ts +35 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +282 -0
- package/dist/render.js.map +1 -0
- package/dist/style.d.ts +88 -0
- package/dist/style.d.ts.map +1 -0
- package/dist/style.js +207 -0
- package/dist/style.js.map +1 -0
- package/dist/tokens.d.ts +24 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +54 -0
- package/dist/tokens.js.map +1 -0
- package/dist/transitions.d.ts +133 -0
- package/dist/transitions.d.ts.map +1 -0
- package/dist/transitions.js +85 -0
- package/dist/transitions.js.map +1 -0
- package/package.json +85 -3
- package/registry.json +6 -0
- package/src/code-range.ts +165 -0
- package/src/diff.ts +351 -0
- package/src/engine.ts +21 -0
- package/src/highlight.ts +340 -0
- package/src/index.ts +3 -0
- package/src/layout.ts +157 -0
- package/src/measure-cache.ts +0 -0
- package/src/node.ts +854 -0
- package/src/nodes.ts +16 -0
- package/src/render.ts +315 -0
- package/src/style.ts +255 -0
- package/src/tokens.ts +79 -0
- package/src/transitions.ts +204 -0
package/dist/node.js
ADDED
|
@@ -0,0 +1,667 @@
|
|
|
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)
|
|
348
|
+
return;
|
|
349
|
+
for (let j = 0; j < a.length; j++)
|
|
350
|
+
if (a[j].content !== b[j].content)
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
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;
|
|
357
|
+
}
|
|
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) {
|
|
388
|
+
return null;
|
|
389
|
+
}
|
|
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)
|
|
409
|
+
continue;
|
|
410
|
+
tokens.push(makeAnim(tok.id, { keys: [0, 1], values: [fromOp, toOp] }));
|
|
411
|
+
}
|
|
412
|
+
}
|
|
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] }));
|
|
434
|
+
}
|
|
435
|
+
}
|
|
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)
|
|
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;
|
|
489
|
+
}
|
|
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
|
+
}
|
|
505
|
+
/**
|
|
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.
|
|
508
|
+
*/
|
|
509
|
+
function normalizeCodeRanges(codeRange) {
|
|
510
|
+
const maybeIterable = codeRange;
|
|
511
|
+
if (typeof maybeIterable[Symbol.iterator] === "function") {
|
|
512
|
+
return [...codeRange];
|
|
513
|
+
}
|
|
514
|
+
const stored = codeRange.list;
|
|
515
|
+
if (Array.isArray(stored))
|
|
516
|
+
return [...stored];
|
|
517
|
+
return [codeRange];
|
|
518
|
+
}
|
|
519
|
+
const grammarLoaders = new Map();
|
|
520
|
+
// One function per language: blocks sharing a grammar declare one key, and a
|
|
521
|
+
// second load function for a key already declared is ignored with a warning.
|
|
522
|
+
function grammarLoader(language) {
|
|
523
|
+
let load = grammarLoaders.get(language);
|
|
524
|
+
if (load === undefined) {
|
|
525
|
+
load = () => ensureHighlighter(undefined, [language]);
|
|
526
|
+
grammarLoaders.set(language, load);
|
|
527
|
+
}
|
|
528
|
+
return load;
|
|
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
|
+
}
|
|
667
|
+
//# sourceMappingURL=node.js.map
|