Code
The preview follows a Vim editing sequence. v starts a characterwise selection,
l extends it, :%s replaces the regex match, and zc and zo close and open the
summarize fold. Change replacement or try effect = "morph" in the source.
Give inline source a lang and pass it as code={source} or as a literal XML
body. Use src="file.py" to load a file relative to the scene source.
Fixed dimensions let you scroll or wrap long lines.
A Code defaults to monospace. Text defaults to sans-serif.
Both accept a list of cursors. Lines and columns start at 1. A "bar" cursor
marks an insertion point. A "block" cursor covers the next character by
default. Set color, width, or radius to customize either shape.
<Text cursors={[
TextCursor(1, 1, shape="block", color=Color("#62a0ea66")),
TextCursor(1, 6, width=2pt, radius=1pt),
]}>Ready</Text>
Patch and edit
CodeStyle, CodeTheme and CodeSpan name the records returned by their
same-named constructors. Text.edit and Code.edit default to 0s.
Folding and unfolding default to 100ms. Other timed methods default to 300ms.
Set ease to change the pacing of a
timed edit. It defaults to "spring".
view.edit(text?: String, font?: Font, animation: EditAnimation = "morph"): Animation
code.patch(patch?: String, src?: String, animation: EditAnimation = "morph",
file?: String, range?: CodeRange): Animation
selection.edit(code?: String, src?: String, animation: EditAnimation = "morph",
file?: String): Animation
Use edit(text, font=...) on a Text or Code to replace its
content on the timeline. Use edit(font=...) to change the font and keep the
current text. For text driven by a signal, bind the font to a signal to change
it over time. A Code highlights the replacement in its current
language. Use selection.edit(code=...) to replace a source range, or pass a
unified diff string to code.patch(patch=...). Either method accepts a
replacement or patch file through src=... in place of inline content.
Specify file when the diff contains changes for multiple files.
typekeeps the shared prefix of a changed run, backspaces to the first difference, then types the replacement.morphmoves surviving text and fades the characters that enter or leave.
EditAnimation is "type" | "morph". Timed methods take an Easing such as "linear" or "smooth".
Select a range
code.range(name?: String, level?: Number, line?: Number, end_line?: Number,
column?: Number, character?: Number, length?: Number,
regex?: String, occurrence?: Number, within?: CodeRange): CodeRange
code.ranges(name?: String, level?: Number, line?: Number, end_line?: Number,
column?: Number, character?: Number, length?: Number,
regex?: String, within?: CodeRange): List<CodeRange>
range needs a selector and a unique match. ranges returns all matches,
possibly empty; occurrence is only available on range.
| Selector | Meaning |
|---|---|
name="WorkQueue.push" | Qualified declaration name; a unique bare name also works. |
level=2 | Syntax depth. |
line=24, end_line=28 | Source line interval. |
line=24, column=8, length=5 | Text within a source line. |
character=120, length=8 | Text at a global character offset. |
regex="return [0-9]+", occurrence=2 | A regex match; use raw strings for regex braces. |
within=code.range(name="WorkQueue") | Restrict another selector to this interval. |
Lines, columns, characters and occurrences start at 1. The end_line selector
includes that line; returned CodeRange endpoints are exclusive.
Bound ranges belong to one view/revision. Query again after a patch; inline
chains resolve when their animation is placed.
Fold and style
let alternate: "fade" | "morph" = "fade"
let view = <Code lang="python" width={86%} h-114 font_size={12pt}
line_numbers={true} scope_indicators={true}
x_align={Align.Center} y_align={Align.Center}>
def total(x, y):
result = x + y
return result
print(total(2, 3))
</Code>
let scope = view.range(name="total")
view
wait(500ms)
scope.fold(duration=300ms, ease="linear")
wait(400ms)
scope.unfold(duration=300ms, ease="linear")
wait(500ms)
scope.fold(animation=alternate, duration=300ms, ease="linear")
wait(400ms)
scope.unfold(animation=alternate, duration=300ms, ease="linear")
wait(500ms)
The first fold and unfold use "collapse" and "expand" by default. The body’s
visible height changes as the lines below move. The second pair uses "fade"
to crossfade the layouts. Try alternate = "morph" to move surviving text and
fade the changed pieces.
selection.fold(animation: "collapse" | "morph" | "fade" = "collapse"): Animation
selection.unfold(animation: "expand" | "morph" | "fade" = "expand"): Animation
selection.select(): Animation
selection.scroll_to(align: "nearest" | "start" | "center" | "end" = "nearest"): Animation
selection.style(style: CodeStyle): Animation
code.select(ranges?: List<CodeRange>): Animation
code.style(spans?: List<CodeSpan>, theme?: CodeTheme,
line_numbers?: Bool, scope_indicators?: Bool): Animation
Folds affect one scope independently. A line or regex selection chooses its
innermost containing scope. The same animation options work with
code.fold(range=selection) and code.unfold(range=selection).
CodeStyle accepts fill, background, opacity, bold, italic, underline and
strike; CodeTheme applies named capture rules, and CodeSpan overrides
explicit source ranges. Later spans win. code.select() clears selection;
code.style(spans=[]) clears span overrides.
Set selection on CodeTheme to control the highlight color and alpha,
and selection_radius to control its corner radius. Use
code.style(theme=..., duration=...) to animate either change while a range
is selected. Set inactive_opacity=1 to keep the surrounding code at full opacity.
Line numbers and scope indicators default to hidden. Animate them with
code.style(line_numbers=true, scope_indicators=true). Scope chevrons rotate
with folding. A signal-owned property must be changed through its input,
instead of a second style/patch writer.
Bundled languages: Ebi, Python, Rust, JavaScript, TypeScript, TSX, C/C++, JSON, Bash, TOML, YAML, SQL and plain text. Invalid displayed code remains visible with syntax diagnostics.