Skip to content

🎥 Make videos programmatically with React

better colors

Jonny Burger8d76ff6d678e
diff --git a/packages/studio-server/src/codemods/update-sequence-props.ts b/packages/studio-server/src/codemods/update-sequence-props.ts
index 1dc4a368af..5f504287d2 100644
--- a/packages/studio-server/src/codemods/update-sequence-props.ts
+++ b/packages/studio-server/src/codemods/update-sequence-props.ts
@@ -65,14 +65,22 @@ const snapshotTopLevelAttrs = (
const result = new Map<string, string>();
for (const a of node.attributes ?? []) {
if (a.type === 'JSXAttribute' && a.name.type === 'JSXIdentifier') {
- result.set(
- a.name.name,
- recast
- .print(a)
- .code.replace(/\s+/g, ' ')
- .replace(/,(\s*[}\]])/g, '$1')
- .trim(),
- );
+ const name = a.name.name;
+ const printed = recast
+ .print(a)
+ .code.replace(/\s+/g, ' ')
+ .replace(/,(\s*[}\]])/g, '$1')
+ .trim();
+
+ const prefix = `${name}=`;
+ let valueOnly = printed.startsWith(prefix)
+ ? printed.slice(prefix.length)
+ : printed;
+ if (valueOnly.startsWith('{') && valueOnly.endsWith('}')) {
+ valueOnly = valueOnly.slice(1, -1).trim();
+ }
+
+ result.set(name, valueOnly);
}
}
@@ -92,7 +100,7 @@ export const updateSequencePropsAst = ({
input: string;
nodePath: SequenceNodePath;
updates: SequencePropUpdate[];
- schema?: SequenceSchema;
+ schema: SequenceSchema;
}): {
serialized: string;
oldValueStrings: string[];
diff --git a/packages/studio-server/src/preview-server/routes/log-update.ts b/packages/studio-server/src/preview-server/routes/log-update.ts
index 4a54805cb6..10387f36d1 100644
--- a/packages/studio-server/src/preview-server/routes/log-update.ts
+++ b/packages/studio-server/src/preview-server/routes/log-update.ts
@@ -82,6 +82,30 @@ export type PropDelta = {
valueString: string;
};
+const formatPropDelta = ({key, valueString}: PropDelta) => {
+ if (!colorEnabled()) {
+ const dotIdx = key.indexOf('.');
+ if (dotIdx === -1) {
+ return `${key}={${valueString}}`;
+ }
+
+ const parent = key.slice(0, dotIdx);
+ const child = key.slice(dotIdx + 1);
+ return `${parent}={{${child}: ${valueString}}}`;
+ }
+
+ const dotIdx = key.indexOf('.');
+ if (dotIdx === -1) {
+ return formatSimpleProp(key, valueString);
+ }
+
+ return formatNestedProp(
+ key.slice(0, dotIdx),
+ key.slice(dotIdx + 1),
+ valueString,
+ );
+};
+
const formatSideProps = ({
removedProps,
addedProps,
@@ -91,12 +115,13 @@ const formatSideProps = ({
}) => {
const parts: string[] = [];
- for (const {valueString} of removedProps) {
- parts.push(colorEnabled() ? strikeThrough(valueString) : valueString);
+ for (const prop of removedProps) {
+ const formatted = formatPropDelta(prop);
+ parts.push(colorEnabled() ? strikeThrough(formatted) : formatted);
}
- for (const {valueString} of addedProps) {
- parts.push(valueString);
+ for (const prop of addedProps) {
+ parts.push(formatPropDelta(prop));
}
if (parts.length === 0) {
diff --git a/packages/studio-server/src/test/log-update.test.ts b/packages/studio-server/src/test/log-update.test.ts
index f6c81f137b..d4c7308747 100644
--- a/packages/studio-server/src/test/log-update.test.ts
+++ b/packages/studio-server/src/test/log-update.test.ts
@@ -119,7 +119,7 @@ test('logUpdate emits change-from-default output for discriminated union enum ch
expect(oldValueStrings[0]).toBe('"absolute-fill"');
expect(removedProps).toEqual([
- {key: 'style', valueString: 'style={{ scale: 1.74 }}'},
+ {key: 'style', valueString: '{ scale: 1.74 }'},
]);
const newValueString = JSON.stringify('none');
@@ -154,7 +154,10 @@ test('logUpdate emits change-from-default output for discriminated union enum ch
const simpleProp = (key: string, value: string) =>
`${attrName(key)}${equals('=')}${punctuation('{')}${stringValue(value)}${punctuation('}')}`;
- const expectedPropChange = `${simpleProp('layout', "'none'")}, ${strikeThrough('style={{ scale: 1.74 }}')}`;
+ const simplePropPunctuation = (key: string, value: string) =>
+ `${attrName(key)}${equals('=')}${punctuation('{')}${punctuation(value)}${punctuation('}')}`;
+
+ const expectedPropChange = `${simpleProp('layout', "'none'")}, ${strikeThrough(simplePropPunctuation('style', '{ scale: 1.74 }'))}`;
const expectedLine = `${chalk.blueBright(`src/Example.tsx:${logLine}:`)} ${expectedPropChange}`;
expect(logged).toBe(expectedLine);
@@ -172,9 +175,47 @@ test('logUpdate emits change-from-default output for discriminated union enum ch
addedProps: removedProps,
});
- const expectedUndoPropChange = `${strikeThrough(simpleProp('layout', "'none'"))}, style={{ scale: 1.74 }}`;
+ const expectedUndoPropChange = `${strikeThrough(simpleProp('layout', "'none'"))}, ${simplePropPunctuation('style', '{ scale: 1.74 }')}`;
expect(undoPropChange).toBe(expectedUndoPropChange);
} finally {
consoleSpy.mockRestore();
}
});
+
+test('Undo prop change should not nest key={key={value}} for re-added props', async () => {
+ const fixture = readFileSync(
+ path.join(__dirname, 'snapshots', 'discriminated-union-with-premount.tsx'),
+ 'utf-8',
+ );
+
+ const {removedProps} = await updateSequenceProps({
+ input: fixture,
+ nodePath: lineColumnToNodePath(fixture, 3),
+ updates: [
+ {
+ key: 'layout',
+ value: 'none',
+ defaultValue: Internals.sequenceSchema.layout.default,
+ },
+ ],
+ schema: Internals.sequenceSchema,
+ });
+
+ const premount = removedProps.find((p) => p.key === 'premountFor');
+ if (!premount) {
+ throw new Error('premountFor should have been removed');
+ }
+
+ const undoPropChange = formatPropChange({
+ key: 'layout',
+ oldValueString: "'none'",
+ newValueString: "'absolute-fill'",
+ defaultValueString: "'absolute-fill'",
+ removedProps: [],
+ addedProps: [premount],
+ });
+
+ expect(undoPropChange).not.toContain('premountFor={premountFor=');
+ expect(undoPropChange).toContain('premountFor');
+ expect(undoPropChange).toContain('25');
+});