Skip to content

Commit f3389dc

Browse files
vanceingallsclaude
andcommitted
feat(studio): flatten multi-field Text, retiring TextSection fallback
FlatTextSection's multi-field branch (textFields.length > 1) now renders FlatTextLayerList (Task 5) + the existing single-field FlatTextFieldEditor for the active field, tracked via new local activeFieldKey state that resyncs (useEffect) when the active field disappears from props. This retires the legacy TextSection delegation entirely for that case; the TextSection import is removed from propertyPanelFlatTextSection.tsx since nothing else in the file referenced it. Also updates propertyPanelSections.test.tsx and PropertyPanel.test.tsx, which exercised/documented the old multi-field-falls-back-to-legacy- TextSection behavior in comments and test titles — reworded to describe the new flat path (assertions were already compatible and still pass). Flag for reviewer: hideOwnHeading on the legacy TextSection component (propertyPanelSections.tsx) was added in an earlier plan specifically for this now-removed call site. It has no remaining consumer after this task lands (PropertyPanel.tsx's legacy caller doesn't pass it). Left in place per brief instruction — not deleting unilaterally, since that's a scope decision for whoever reviews this task. Co-Authored-By: Claude Sonnet 5 <[email protected]>
1 parent e5da895 commit f3389dc

4 files changed

Lines changed: 215 additions & 29 deletions

File tree

‎packages/studio/src/components/editor/PropertyPanel.test.tsx‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ function nonTextElement() {
8181
};
8282
}
8383

84-
// Bug 2 fixture: 2+ text fields, which routes FlatTextSection to the legacy
85-
// multi-field <TextSection> fallback — must not double-render the "Text"
86-
// heading (FlatGroup's own heading + TextSection's internal Section heading).
84+
// Bug 2 fixture: 2+ text fields, which routes FlatTextSection to its own
85+
// flat multi-field layer list (FlatTextLayerList + FlatTextFieldEditor) —
86+
// must not double-render the "Text" heading (FlatGroup's own heading; this
87+
// component never renders one of its own).
8788
function multiFieldTextElement() {
8889
const base = baseElement();
8990
return {
@@ -312,10 +313,11 @@ describe("PropertyPanel — STUDIO_FLAT_INSPECTOR_ENABLED on", () => {
312313
const { host, root } = await renderPanel(true, multiFieldTextElement());
313314
// The FlatGroup's own "Text" heading is the only one that should exist —
314315
// the legacy TextSection's internal Section heading (data-panel-section
315-
// ="text") must be suppressed when it's used as the flat fallback.
316+
// ="text") must never appear, since the flat multi-field path no longer
317+
// delegates to that component at all.
316318
expect(host.querySelector('[data-flat-group-open="true"]')).not.toBeNull();
317319
expect(host.querySelector('[data-panel-section="text"]')).toBeNull();
318-
// Content from the legacy multi-field fallback must still render.
320+
// Content from the flat multi-field layer list must render.
319321
expect(host.textContent).toContain("Text layers");
320322
act(() => root.unmount());
321323
},

‎packages/studio/src/components/editor/propertyPanelFlatTextSection.test.tsx‎

Lines changed: 166 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// @vitest-environment happy-dom
22

3-
import React, { act } from "react";
3+
import React, { act, useState } from "react";
44
import { createRoot } from "react-dom/client";
55
import { afterEach, describe, expect, it, vi } from "vitest";
6-
import { FlatTextLayerList } from "./propertyPanelFlatTextSection";
6+
import { FlatTextLayerList, FlatTextSection } from "./propertyPanelFlatTextSection";
7+
import type { DomEditSelection, DomEditTextField } from "./domEditingTypes";
78

89
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
910

@@ -86,3 +87,166 @@ describe("FlatTextLayerList", () => {
8687
act(() => root.unmount());
8788
});
8889
});
90+
91+
function makeMultiFieldElement(): DomEditSelection {
92+
return {
93+
element: document.createElement("div"),
94+
id: "multi",
95+
selector: ".multi",
96+
label: "Multi",
97+
tagName: "div",
98+
sourceFile: "index.html",
99+
compositionPath: "index.html",
100+
isCompositionHost: false,
101+
isInsideLockedComposition: false,
102+
boundingBox: { x: 0, y: 0, width: 100, height: 100 },
103+
textContent: "Headline Subhead",
104+
dataAttributes: {},
105+
inlineStyles: {},
106+
computedStyles: {},
107+
textFields: [
108+
{
109+
key: "a",
110+
label: "Text",
111+
value: "Headline",
112+
tagName: "div",
113+
attributes: [],
114+
inlineStyles: {},
115+
computedStyles: {},
116+
source: "self",
117+
},
118+
{
119+
key: "b",
120+
label: "Text",
121+
value: "Subhead",
122+
tagName: "span",
123+
attributes: [],
124+
inlineStyles: {},
125+
computedStyles: {},
126+
source: "self",
127+
},
128+
],
129+
capabilities: {
130+
canSelect: true,
131+
canEditStyles: true,
132+
canCrop: true,
133+
canMove: true,
134+
canResize: true,
135+
canApplyManualOffset: true,
136+
canApplyManualSize: true,
137+
canApplyManualRotation: true,
138+
},
139+
} as DomEditSelection;
140+
}
141+
142+
describe("FlatTextSection — multi-field", () => {
143+
it("shows the layer list, switches the active field's rows on selection, and has no doubled heading (this component never renders its own heading — the parent FlatGroup does)", () => {
144+
const host = document.createElement("div");
145+
document.body.append(host);
146+
const root = createRoot(host);
147+
act(() => {
148+
root.render(
149+
<FlatTextSection
150+
element={makeMultiFieldElement()}
151+
styles={{}}
152+
fontAssets={[]}
153+
onSetText={vi.fn()}
154+
onSetTextFieldStyle={vi.fn()}
155+
onAddTextField={vi.fn()}
156+
onRemoveTextField={vi.fn()}
157+
/>,
158+
);
159+
});
160+
expect(host.textContent).toContain("Headline");
161+
expect(host.textContent).toContain("Subhead");
162+
// Active field's editor rows are visible (Font/Weight/etc. from FlatTextFieldEditor).
163+
expect(host.textContent).toContain("Weight");
164+
// Exactly one "Text layers" micro-label — this component doesn't duplicate its own list.
165+
const layerLabels = Array.from(host.querySelectorAll("div")).filter(
166+
(el) => el.textContent === "Text layers",
167+
);
168+
expect(layerLabels.length).toBeLessThanOrEqual(1);
169+
170+
const rows = host.querySelectorAll('[data-flat-text-layer-row="true"]');
171+
act(() => rows[1].dispatchEvent(new MouseEvent("click", { bubbles: true })));
172+
expect(host.textContent).toContain("Subhead");
173+
act(() => root.unmount());
174+
});
175+
176+
it("wires onAdd/onRemove end-to-end: async onAddTextField switches the active field once it appears in props, and the resync effect falls back to the first field when the active one disappears", async () => {
177+
let addResolved = false;
178+
179+
function Harness() {
180+
const [fields, setFields] = useState<DomEditTextField[]>(makeMultiFieldElement().textFields);
181+
const element: DomEditSelection = { ...makeMultiFieldElement(), textFields: fields };
182+
return (
183+
<FlatTextSection
184+
element={element}
185+
styles={{}}
186+
fontAssets={[]}
187+
onSetText={vi.fn()}
188+
onSetTextFieldStyle={vi.fn()}
189+
onAddTextField={() =>
190+
Promise.resolve().then(() => {
191+
addResolved = true;
192+
setFields((prev) => [
193+
...prev,
194+
{
195+
key: "c",
196+
label: "Text",
197+
value: "Third",
198+
tagName: "div",
199+
attributes: [],
200+
inlineStyles: {},
201+
computedStyles: {},
202+
source: "self",
203+
},
204+
]);
205+
return "c";
206+
})
207+
}
208+
onRemoveTextField={(fieldKey: string) =>
209+
setFields((prev) => prev.filter((field) => field.key !== fieldKey))
210+
}
211+
/>
212+
);
213+
}
214+
215+
const host = document.createElement("div");
216+
document.body.append(host);
217+
const root = createRoot(host);
218+
act(() => {
219+
root.render(<Harness />);
220+
});
221+
222+
let rows = host.querySelectorAll('[data-flat-text-layer-row="true"]');
223+
expect(rows).toHaveLength(2);
224+
expect((rows[0] as HTMLElement).getAttribute("data-active")).toBe("true");
225+
226+
const addButton = host.querySelector<HTMLButtonElement>('[data-flat-text-layer-add="true"]');
227+
await act(async () => {
228+
addButton?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
229+
await Promise.resolve();
230+
await Promise.resolve();
231+
});
232+
expect(addResolved).toBe(true);
233+
rows = host.querySelectorAll('[data-flat-text-layer-row="true"]');
234+
expect(rows).toHaveLength(3);
235+
expect((rows[2] as HTMLElement).getAttribute("data-active")).toBe("true");
236+
237+
// Remove the active field ("c") through the wired onRemoveTextField — the
238+
// resync useEffect must fall back to the first remaining field ("a")
239+
// since "c" no longer exists in element.textFields.
240+
const removeButtons = host.querySelectorAll<HTMLButtonElement>(
241+
'[data-flat-text-layer-remove="true"]',
242+
);
243+
act(() => {
244+
removeButtons[2].dispatchEvent(new MouseEvent("click", { bubbles: true }));
245+
});
246+
rows = host.querySelectorAll('[data-flat-text-layer-row="true"]');
247+
expect(rows).toHaveLength(2);
248+
expect((rows[0] as HTMLElement).getAttribute("data-active")).toBe("true");
249+
250+
act(() => root.unmount());
251+
});
252+
});

‎packages/studio/src/components/editor/propertyPanelFlatTextSection.tsx‎

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect, useState } from "react";
12
import { Plus, X } from "../../icons/SystemIcons";
23
import { isTextEditableSelection, type DomEditSelection } from "./domEditing";
34
import type { ImportedFontAsset } from "./fontAssets";
@@ -16,7 +17,6 @@ import {
1617
getTextFieldColor,
1718
getTextStyleValue,
1819
TextAreaField,
19-
TextSection,
2020
WEIGHT_LABELS,
2121
} from "./propertyPanelSections";
2222

@@ -201,27 +201,47 @@ export function FlatTextSection({
201201
onAddTextField: (afterFieldKey?: string) => string | Promise<string | null> | null;
202202
onRemoveTextField: (fieldKey: string) => void;
203203
}) {
204+
const [activeFieldKey, setActiveFieldKey] = useState<string | null>(
205+
element.textFields[0]?.key ?? null,
206+
);
207+
208+
useEffect(() => {
209+
const nextFields = element.textFields;
210+
setActiveFieldKey((current) => {
211+
if (current && nextFields.some((field) => field.key === current)) return current;
212+
return nextFields[0]?.key ?? null;
213+
});
214+
}, [element.id, element.selector, element.textFields]);
215+
204216
if (!isTextEditableSelection(element)) return null;
205217
const textFields = element.textFields;
206-
const activeField = textFields[0];
218+
const activeField = textFields.find((field) => field.key === activeFieldKey) ?? textFields[0];
207219
if (!activeField) return null;
208220

209221
if (textFields.length > 1) {
210-
// The parent FlatGroup (PropertyPanelFlat) already renders a "Text"
211-
// heading around this section — suppress TextSection's own internal
212-
// heading so the flat panel doesn't show "Text" twice in a row.
213222
return (
214-
<TextSection
215-
element={element}
216-
styles={styles}
217-
fontAssets={fontAssets}
218-
onImportFonts={onImportFonts}
219-
onSetText={onSetText}
220-
onSetTextFieldStyle={onSetTextFieldStyle}
221-
onAddTextField={onAddTextField}
222-
onRemoveTextField={onRemoveTextField}
223-
hideOwnHeading
224-
/>
223+
<div className="space-y-1.5">
224+
<FlatTextLayerList
225+
fields={textFields}
226+
activeFieldKey={activeField.key}
227+
styles={styles}
228+
onSelect={setActiveFieldKey}
229+
onAdd={() =>
230+
void Promise.resolve(onAddTextField(activeField.key)).then((nextKey) => {
231+
if (nextKey) setActiveFieldKey(nextKey);
232+
})
233+
}
234+
onRemove={onRemoveTextField}
235+
/>
236+
<FlatTextFieldEditor
237+
field={activeField}
238+
styles={styles}
239+
fontAssets={fontAssets}
240+
onImportFonts={onImportFonts}
241+
onSetText={onSetText}
242+
onSetTextFieldStyle={onSetTextFieldStyle}
243+
/>
244+
</div>
225245
);
226246
}
227247

‎packages/studio/src/components/editor/propertyPanelSections.test.tsx‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe("FlatTextSection", () => {
133133
act(() => root.unmount());
134134
});
135135

136-
it("suppresses TextSection's own heading when falling back for a multi-field element", () => {
136+
it("renders the flat layer list (not the legacy TextSection) for a multi-field element", () => {
137137
const { host, root } = renderSection({
138138
textFields: [
139139
makeElement().textFields[0],
@@ -149,12 +149,12 @@ describe("FlatTextSection", () => {
149149
},
150150
],
151151
});
152-
// TextSection's own Section wrapper (data-panel-section="text") must not
153-
// render here — the caller (PropertyPanelFlat's FlatGroup) already shows
154-
// a "Text" heading, so a second one from the legacy component would be a
155-
// doubled heading.
152+
// Legacy TextSection's own Section wrapper (data-panel-section="text")
153+
// must never render here — multi-field elements now go through the flat
154+
// FlatTextLayerList + FlatTextFieldEditor path end to end, not a
155+
// delegation to the legacy component.
156156
expect(host.querySelector('[data-panel-section="text"]')).toBeNull();
157-
// The multi-field fallback's own content must still render.
157+
// The flat layer list's own content must render.
158158
expect(host.textContent).toContain("Text layers");
159159
act(() => root.unmount());
160160
});

0 commit comments

Comments
 (0)