Skip to content

Visualize Your Ideas With Code

feat: display array values in inspector (#670)

Jacobe71d74c9c049
diff --git a/packages/core/src/types/BBox.ts b/packages/core/src/types/BBox.ts
index 8c7cdd26..1ff59b45 100644
--- a/packages/core/src/types/BBox.ts
+++ b/packages/core/src/types/BBox.ts
@@ -337,6 +337,10 @@ export class BBox implements Type {
return BBox.symbol;
}
+ public toString(): string {
+ return `BBox(${this.x}, ${this.y}, ${this.width}, ${this.height})`;
+ }
+
public serialize(): SerializedBBox {
return {x: this.x, y: this.y, width: this.width, height: this.height};
}
diff --git a/packages/core/src/types/Spacing.ts b/packages/core/src/types/Spacing.ts
index e6419987..52775c22 100644
--- a/packages/core/src/types/Spacing.ts
+++ b/packages/core/src/types/Spacing.ts
@@ -126,6 +126,10 @@ export class Spacing implements Type {
return Spacing.symbol;
}
+ public toString() {
+ return `Spacing(${this.top}, ${this.right}, ${this.bottom}, ${this.left})`;
+ }
+
public serialize(): SerializedSpacing {
return {
top: this.top,
diff --git a/packages/core/src/types/Vector.ts b/packages/core/src/types/Vector.ts
index 68e60550..dff2d22c 100644
--- a/packages/core/src/types/Vector.ts
+++ b/packages/core/src/types/Vector.ts
@@ -327,6 +327,10 @@ export class Vector2 implements Type {
return Vector2.symbol;
}
+ public toString() {
+ return `Vector2(${this.x}, ${this.y})`;
+ }
+
public serialize(): SerializedVector2 {
return {x: this.x, y: this.y};
}
diff --git a/packages/ui/src/components/fields/ArrayField.tsx b/packages/ui/src/components/fields/ArrayField.tsx
new file mode 100644
index 00000000..cacdf149
--- /dev/null
+++ b/packages/ui/src/components/fields/ArrayField.tsx
@@ -0,0 +1,24 @@
+import {Field, FieldSet, FieldSurface, FieldValue} from './Layout';
+import {AutoField} from './AutoField';
+
+export interface NumberFieldProps {
+ value: any[];
+}
+
+export function ArrayField({value}: NumberFieldProps) {
+ const header = (
+ <Field copy={JSON.stringify(value)}>
+ <FieldValue>{JSON.stringify(value)}</FieldValue>
+ </Field>
+ );
+
+ return value.length > 0 ? (
+ <FieldSet nested header={header}>
+ {value.map(item => (
+ <AutoField value={item} />
+ ))}
+ </FieldSet>
+ ) : (
+ <FieldSurface>{header}</FieldSurface>
+ );
+}
diff --git a/packages/ui/src/components/fields/AutoField.tsx b/packages/ui/src/components/fields/AutoField.tsx
index 07fa8dd1..e0e5b310 100644
--- a/packages/ui/src/components/fields/AutoField.tsx
+++ b/packages/ui/src/components/fields/AutoField.tsx
@@ -6,10 +6,9 @@ import {UnknownField} from './UnknownField';
import {NumberField} from './NumberField';
import {ColorField} from './ColorField';
import {SpacingField} from './SpacingField';
-import {Group, Label} from '../controls';
+import {ArrayField} from './ArrayField';
export interface AutoFieldProps {
- label: string;
value: any;
}
@@ -19,18 +18,15 @@ const TYPE_MAP: Record<symbol, FunctionComponent<{value: any}>> = {
[Spacing.symbol]: SpacingField,
};
-export function AutoField({label, value}: AutoFieldProps) {
+export function AutoField({value}: AutoFieldProps) {
let Field = UnknownField;
if (isType(value)) {
Field = TYPE_MAP[value.toSymbol()] ?? UnknownField;
} else if (typeof value === 'number') {
Field = NumberField;
+ } else if (Array.isArray(value)) {
+ Field = ArrayField;
}
- return (
- <Group>
- <Label>{label}</Label>
- <Field value={value} />
- </Group>
- );
+ return <Field value={value} />;
}
diff --git a/packages/ui/src/components/fields/Layout.module.scss b/packages/ui/src/components/fields/Layout.module.scss
index c83905c6..c1310859 100644
--- a/packages/ui/src/components/fields/Layout.module.scss
+++ b/packages/ui/src/components/fields/Layout.module.scss
@@ -1,5 +1,9 @@
.surface {
- background: var(--surface-color-light);
+ --field-color: var(--surface-color-light);
+ --field-color-hover: var(--surface-color-hover);
+ --field-color-border: var(--surface-color);
+
+ background: var(--field-color);
color: rgba(255, 255, 255, 0.6);
border-radius: 4px;
line-height: 24px;
@@ -13,6 +17,12 @@
opacity: 0.54;
pointer-events: none;
}
+
+ .surface {
+ --field-color: var(--surface-color);
+ --field-color-hover: var(--surface-color-hover);
+ --field-color-border: var(--surface-color-light);
+ }
}
.header {
@@ -20,7 +30,7 @@
align-items: center;
height: 24px;
- .open & {
+ .open > & {
color: rgba(255, 255, 255, 0.32);
}
}
@@ -28,12 +38,21 @@
.fields {
display: none;
- .open & {
+ &.nested {
+ flex-direction: column;
+ gap: 4px;
+ padding: 4px;
+ }
+
+ .open > & {
display: block;
+ &.nested {
+ display: flex;
+ }
}
- .field {
- border-top: 1px solid var(--surface-color);
+ & > .field {
+ border-top: 1px solid var(--field-color-border);
}
}
@@ -47,7 +66,7 @@
&.copy {
cursor: pointer;
&:hover {
- background-color: var(--surface-color-hover);
+ background-color: var(--field-color-hover);
}
}
@@ -57,7 +76,7 @@
left: 0;
right: 0;
text-align: center;
- background-color: var(--surface-color-hover);
+ background-color: var(--field-color-hover);
color: white;
opacity: 0;
transition: opacity 0.2s;
diff --git a/packages/ui/src/components/fields/Layout.tsx b/packages/ui/src/components/fields/Layout.tsx
index 1239b0ce..aed6eb67 100644
--- a/packages/ui/src/components/fields/Layout.tsx
+++ b/packages/ui/src/components/fields/Layout.tsx
@@ -9,8 +9,9 @@ import clsx from 'clsx';
export interface FieldSetProps {
children: ComponentChildren;
header: ComponentChildren;
+ nested?: boolean;
}
-export function FieldSet({children, header}: FieldSetProps) {
+export function FieldSet({children, header, nested}: FieldSetProps) {
const [open, setOpen] = useState(false);
return (
@@ -19,7 +20,9 @@ export function FieldSet({children, header}: FieldSetProps) {
<Toggle open={open} onToggle={setOpen} />
{header}
</div>
- <div className={styles.fields}>{children}</div>
+ <div className={clsx(styles.fields, nested && styles.nested)}>
+ {children}
+ </div>
</FieldSurface>
);
}
diff --git a/packages/ui/src/components/meta/UnknownMetaFieldView.tsx b/packages/ui/src/components/meta/UnknownMetaFieldView.tsx
index a2bb48b0..b97b643f 100644
--- a/packages/ui/src/components/meta/UnknownMetaFieldView.tsx
+++ b/packages/ui/src/components/meta/UnknownMetaFieldView.tsx
@@ -1,6 +1,7 @@
import {useSubscribableValue} from '../../hooks';
import {MetaField} from '@motion-canvas/core/lib/meta';
import {AutoField} from '../fields';
+import {Group, Label} from '../controls';
export interface UnknownMetaFieldViewProps {
field: MetaField<any>;
@@ -9,5 +10,10 @@ export interface UnknownMetaFieldViewProps {
export function UnknownMetaFieldView({field}: UnknownMetaFieldViewProps) {
const value = useSubscribableValue(field.onChanged);
- return <AutoField label={field.name} value={value} />;
+ return (
+ <Group>
+ <Label>{field.name}</Label>
+ <AutoField value={value} />
+ </Group>
+ );
}
diff --git a/packages/ui/src/components/sidebar/Properties.tsx b/packages/ui/src/components/sidebar/Properties.tsx
index b6d82581..d3ad778a 100644
--- a/packages/ui/src/components/sidebar/Properties.tsx
+++ b/packages/ui/src/components/sidebar/Properties.tsx
@@ -39,7 +39,10 @@ export function Properties() {
)}
{attributes
? Object.entries(attributes).map(([key, value]) => (
- <AutoField label={key} value={value} />
+ <Group>
+ <Label>{key}</Label>
+ <AutoField value={value} />
+ </Group>
))
: inspectable
? 'Click on a node to view its properties.'