Skip to content

Commit

Permalink
feat: fix demo
Browse files Browse the repository at this point in the history
  • Loading branch information
prevwong committed Jan 6, 2024
1 parent e68b0fc commit f65b2fd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
28 changes: 28 additions & 0 deletions packages/types/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,31 @@ export const clone = (value: any, options?: Partial<CloneOpts>) => {

return value;
};

export const toJSON = (value: any) => {
const x = JSON.parse(JSON.stringify(value));

return x;
};

export const fromJSON = (value: any) => {
if (Array.isArray(value)) {
return value.map((item) => fromJSON(item));
}

if (isObjectLiteral(value)) {
if (value['type'] && !!Schema.get(value['type'])) {
return Schema.fromJSON(value);
}

return Object.keys(value).reduce(
(accum, key) => ({
...accum,
[key]: fromJSON(value[key]),
}),
{}
);
}

return value;
};
8 changes: 4 additions & 4 deletions site/components/editor-layout/AddFrameModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ const getInitialComponentProps = (
if (component instanceof t.RekaComponent) {
const props: Record<string, t.Expression> = component.props.reduce(
(accum, prop) => {
const value = t.fromJSON(existingProps[prop.name]);
return {
...accum,
[prop.name]:
existingProps[prop.name] ?? prop.init ?? t.literal({ value: '' }),
[prop.name]: value ?? prop.init ?? t.literal({ value: '' }),
};
},
{}
);

if (existingProps['children']) {
props['children'] = existingProps['children'];
props['children'] = t.Schema.fromJSON(existingProps['children']);
}

return props;
Expand Down Expand Up @@ -117,7 +117,7 @@ export const AddFrameModal = (props: AddFrameModalProps) => {
return;
}

existingFrame.props = componentProps;
existingFrame.props = t.toJSON(componentProps);
});

editor.activeComponentEditor?.setActiveFrame(frameName);
Expand Down
3 changes: 1 addition & 2 deletions site/constants/encoded-dummy-program.ts

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions site/extensions/UserFrameExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as t from '@rekajs/types';
export type UserFrame = {
id: string;
name: string;
props?: string;
props?: Record<string, any>;
width?: string;
height?: string;
};
Expand All @@ -22,7 +22,7 @@ export const UserFrameExtensionFactory = () => {
{
id: 'Test Feature',
name: 'Feature',
props: JSON.stringify({
props: t.toJSON({
title: t.literal({ value: 'Test Feature' }),
description: t.literal({
value: 'An interesting feature description',
Expand All @@ -40,7 +40,7 @@ export const UserFrameExtensionFactory = () => {
{
id: 'Basic Card',
name: 'Card',
props: JSON.stringify({
props: t.toJSON({
name: t.literal({ value: 'Dummy Card' }),
description: t.literal({ value: 'Dummy description for card' }),
}),
Expand All @@ -50,7 +50,7 @@ export const UserFrameExtensionFactory = () => {
{
id: 'Demo Modal',
name: 'Modal',
props: JSON.stringify({
props: t.toJSON({
title: t.literal({ value: 'My Modal' }),
isOpen: t.literal({ value: true }),
children: t.tagTemplate({
Expand All @@ -65,7 +65,7 @@ export const UserFrameExtensionFactory = () => {
{
id: 'Primary Button',
name: 'Button',
props: JSON.stringify({
props: t.toJSON({
text: t.literal({ value: 'Click me' }),
}),
width: '300px',
Expand All @@ -74,7 +74,7 @@ export const UserFrameExtensionFactory = () => {
{
id: 'Button with Icon',
name: 'Button',
props: JSON.stringify({
props: t.toJSON({
text: t.literal({ value: 'Icon' }),
icon: t.literal({ value: 'ArrowRightIcon' }),
}),
Expand All @@ -84,7 +84,7 @@ export const UserFrameExtensionFactory = () => {
{
id: 'Basic text input',
name: 'Input',
props: JSON.stringify({
props: t.toJSON({
text: t.literal({ value: 'Hello!' }),
}),
},
Expand Down Expand Up @@ -121,7 +121,7 @@ export const UserFrameExtensionFactory = () => {
return;
}

stateFrame.setProps(props);
stateFrame.setProps(t.fromJSON(props));
});
}
);
Expand Down Expand Up @@ -149,7 +149,7 @@ export const UserFrameExtensionFactory = () => {
id: currentFrame.id,
component: {
name: currentFrame.name,
props: currentFrame.props ? JSON.parse(currentFrame.props) : {},
props: t.fromJSON(currentFrame.props),
},
syncImmediately: false,
});
Expand Down

0 comments on commit f65b2fd

Please sign in to comment.