Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor TextPanel to improve font selection handling

konstantin-paulus3c72a3a01152
diff --git a/apps/web/src/components/sidebar-right/inspector/text.tsx b/apps/web/src/components/sidebar-right/inspector/text.tsx
index 802e78d7..3ef00c64 100644
--- a/apps/web/src/components/sidebar-right/inspector/text.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/text.tsx
@@ -95,8 +95,9 @@ export function TextPanel(props: TextPanelProps) {
const chars = useTrait(entity, Chars);
const hasSize = useHas(entity, Size);
- const fontFamily = () => style()?.fontFamily ?? 'Inter';
- const fontWeight = () => style()?.fontWeight ?? '400';
+ const [selectedFamily, setSelectedFamily] = createSignal(entity().get(TextStyle)?.fontFamily ?? 'Inter');
+ const [selectedWeight, setSelectedWeight] = createSignal(entity().get(TextStyle)?.fontWeight ?? '400');
+
const fontSize = () => style()?.fontSize ?? 16;
const leading = () => style()?.leading ?? 1;
const letterSpacing = () => style()?.letterSpacing ?? 0;
@@ -125,6 +126,7 @@ export function TextPanel(props: TextPanelProps) {
};
const handleFontFamilyChange = (family: string) => {
+ setSelectedFamily(family);
editor.editProperty(entity(), 'fontFamily', family);
if (family in WebFonts) {
@@ -133,6 +135,9 @@ export function TextPanel(props: TextPanelProps) {
};
const handleFontWeightChange = (weight: string) => {
+ if (weight === selectedWeight()) return;
+
+ setSelectedWeight(weight);
// Authored as a CSS number, which is the only spelling the prop takes
// besides the "normal"/"bold" keywords the panel does not offer.
editor.editProperty(entity(), 'fontWeight', Number(weight));
@@ -173,7 +178,7 @@ export function TextPanel(props: TextPanelProps) {
<ControlRow label="Font">
<FontDropdown
- family={fontFamily()}
+ family={selectedFamily()}
onPreview={(family) => previewFont({ fontFamily: family })}
onFamilyChange={handleFontFamilyChange}
onWeightsChange={setAvailableWeights}
@@ -183,7 +188,7 @@ export function TextPanel(props: TextPanelProps) {
<ControlRow label="Sizing" contentClass="flex gap-2 items-center">
<Select
class="flex-1 min-w-0"
- value={fontWeight()}
+ value={selectedWeight()}
onChange={v => handleFontWeightChange(v ?? '400')}
options={weightOptions().map(w => w.value)}
itemComponent={itemProps => (
@@ -197,11 +202,11 @@ export function TextPanel(props: TextPanelProps) {
>
<SelectTrigger>
<SelectValue>
- {weightOptions().find(w => w.value === fontWeight())?.label}
+ {weightOptions().find(w => w.value === selectedWeight())?.label}
</SelectValue>
</SelectTrigger>
<SelectPortal>
- <SelectContent onCloseAutoFocus={() => previewFont({ fontWeight: fontWeight() })} />
+ <SelectContent onCloseAutoFocus={() => previewFont({ fontWeight: selectedWeight() })} />
</SelectPortal>
</Select>
@@ -328,10 +333,11 @@ function FontDropdown(props: FontDropdownProps) {
return fonts.filter((f) => f.family.toLowerCase().includes(q));
});
- const handleOpenChange = () => {
+ const handleOpenChange = (isOpen: boolean) => {
setFontQuery('');
- // Whatever was hovered last goes; the selection is the only edit.
- props.onPreview(props.family);
+ // Whatever was hovered last goes: only a selection authors a family, and
+ // `family` is the authored one whether or not this picker changed it.
+ if (!isOpen) props.onPreview(props.family);
};
const handleGrantAccess = async () => {
diff --git a/packages/runtime/MIGRATION.md b/packages/runtime/MIGRATION.md
index 1fbc0a6f..b16fa3bb 100644
--- a/packages/runtime/MIGRATION.md
+++ b/packages/runtime/MIGRATION.md
@@ -87,6 +87,7 @@ Conventions worth repeating when migrating the next panel:
- `syncKeyframe` after a prop write keeps an existing track in step, since the motion system would otherwise overwrite the edit next tick. **Exception:** `width`/`height` sync *before* the write, because the document's handler is `resizeEntity`, which syncs the track itself and would mint an unsourced keyframe first. The canvas resize in `engine/input/interactions.ts` has the same gap, unfixed.
- State with no JSX spelling (`KeepAspectRatio`, `ClipsContent`, `Skew`) is written to the trait alone and does not survive a recompile. Giving any of them a prop is a separate decision; a lock snapshot would then have to come from the authored size, not from whatever `Size` holds at mount.
- Panels still on bitecs import the diamond from `components/ui/keyframe-bitecs.tsx` until they move.
+- A picker that previews on hover writes the trait alone, so while it is open the trait holds the hover and not the authored value. Keep the authored one in a signal seeded at mount (panels remount per selection) and move it only on a selection: it is what the control displays *and* what the close restores. Reading it back off the trait restores the hover instead, permanently. `AppearanceSettings`' blend mode and the text panel's family/weight both do this.
- What a `<text>` says is its children, not a prop, so it travels as its own edit end to end: `editor.editText` -> `TextEdit` -> the writer's `SourceSet.text` -> `RuntimeDocument.setText`. The document keeps the reconciler's text nodes: the first says all of it and the rest say nothing, so a project that later re-renders one of them still lands.
- A text is the one element whose box is optional (`Size` absent = sized to its glyphs), so the text panel's "Grow" toggle is `width`/`height` being authored at all. Unsetting a bound drops `Size` only once neither is left, which is why the toggle writes `width` before `height`.