Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(playground): dummy workspace inside playground #361

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions modules/code-builder/playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { createRoot } from 'react-dom/client';
import { createBrowserRouter, Navigate, RouterProvider } from 'react-router-dom';

import PageCollision from './pages/Collision';
import WorkSpace from './pages/WorkSpace';

const router = createBrowserRouter([
{
path: '/collision',
element: <PageCollision />,
},
{
path: '/workspace',
element: <WorkSpace />,
},
{
path: '/',
element: <Navigate to="/collision" />,
Expand Down
213 changes: 213 additions & 0 deletions modules/code-builder/playground/pages/WorkSpace/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
import {
ModelBrickBlock,
ModelBrickData,
ModelBrickExpression,
ModelBrickStatement,
} from '@/brick';
import type { TBrickType, TBrickCoords, TBrickArgDataType } from '@/@types/brick';

type InstanceMap = {
data: ModelBrickData;
expression: ModelBrickExpression;
statement: ModelBrickStatement;
block: ModelBrickBlock;
};

export type Brick = {
id: string;
type: TBrickType;
instance: InstanceMap[TBrickType];
coords: TBrickCoords;
children?: Brick[];
};

export const WORKSPACES_DATA: { id: string; data: Brick[] }[] = [
{
id: 'workspace1',
data: [
{
id: '1',
type: 'block',
instance: new ModelBrickBlock({
label: 'Block',
args: Object.fromEntries(
[].map<
[string, { label: string; dataType: TBrickArgDataType; meta: unknown }]
>((name) => [name, { label: name, dataType: 'any', meta: undefined }]),
),
colorBg: 'yellow',
colorFg: 'black',
outline: 'red',
scale: 2,
glyph: '',
connectAbove: true,
connectBelow: true,
name: '',
nestLengthY: 125,
}),
coords: { x: 50, y: 50 },
children: [
{
id: '2',
type: 'statement',
instance: new ModelBrickStatement({
label: 'Statement',
args: Object.fromEntries(
[].map<
[
string,
{
label: string;
dataType: TBrickArgDataType;
meta: unknown;
},
]
>((name) => [
name,
{ label: name, dataType: 'any', meta: undefined },
]),
),
colorBg: 'lightblue',
colorFg: 'black',
outline: 'blue',
scale: 2,
glyph: '',
connectAbove: true,
connectBelow: true,
name: '',
}),
coords: { x: 68, y: 92 },
},
{
id: '3',
type: 'statement',
instance: new ModelBrickStatement({
label: 'Statement',
args: Object.fromEntries(
[].map<
[
string,
{
label: string;
dataType: TBrickArgDataType;
meta: unknown;
},
]
>((name) => [
name,
{ label: name, dataType: 'any', meta: undefined },
]),
),
colorBg: 'lightgreen',
colorFg: 'black',
outline: 'green',
scale: 2,
glyph: '',
connectAbove: true,
connectBelow: true,
name: '',
}),
coords: { x: 68, y: 134 },
},
{
id: '4',
type: 'block',
instance: new ModelBrickBlock({
label: 'Block',
args: Object.fromEntries(
[].map<
[
string,
{
label: string;
dataType: TBrickArgDataType;
meta: unknown;
},
]
>((name) => [
name,
{ label: name, dataType: 'any', meta: undefined },
]),
),
colorBg: 'orange',
colorFg: 'black',
outline: 'grey',
scale: 2,
glyph: '',
connectAbove: true,
connectBelow: true,
name: '',
nestLengthY: 17,
}),
coords: { x: 68, y: 176 },
children: [
{
id: '5',
type: 'statement',
instance: new ModelBrickStatement({
label: 'Statement',
args: Object.fromEntries(
[].map<
[
string,
{
label: string;
dataType: TBrickArgDataType;
meta: unknown;
},
]
>((name) => [
name,
{ label: name, dataType: 'any', meta: undefined },
]),
),
colorBg: 'lightpink',
colorFg: 'black',
outline: 'deeppink',

scale: 2,
glyph: '',
connectAbove: true,
connectBelow: true,
name: '',
}),
coords: { x: 86, y: 218 },
},
],
},
{
id: '6',
type: 'statement',
instance: new ModelBrickStatement({
label: 'Statement',
args: Object.fromEntries(
[].map<
[
string,
{
label: string;
dataType: TBrickArgDataType;
meta: unknown;
},
]
>((name) => [
name,
{ label: name, dataType: 'any', meta: undefined },
]),
),
colorBg: 'lightgreen',
colorFg: 'black',
outline: 'green',
scale: 2,
glyph: '',
connectAbove: true,
connectBelow: true,
name: '',
}),
coords: { x: 68, y: 302 },
},
],
},
],
},
];
65 changes: 65 additions & 0 deletions modules/code-builder/playground/pages/WorkSpace/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
BrickBlock,
BrickData,
BrickExpression,
BrickStatement,
ModelBrickBlock,
ModelBrickData,
ModelBrickExpression,
ModelBrickStatement,
} from '@/brick';
import { WORKSPACES_DATA } from './data';
import type { TBrickCoords, TBrickType } from '@/@types/brick';
import type { Brick } from './data';

function getBrick(
type: TBrickType,
instance: ModelBrickBlock | ModelBrickData | ModelBrickExpression | ModelBrickStatement,
coords: TBrickCoords,
) {
switch (type) {
case 'data':
return <BrickData instance={instance as ModelBrickData} coords={coords} />;
case 'expression':
return <BrickExpression instance={instance as ModelBrickExpression} coords={coords} />;
case 'statement':
return <BrickStatement instance={instance as ModelBrickStatement} coords={coords} />;
case 'block':
return <BrickBlock instance={instance as ModelBrickBlock} coords={coords} />;
default:
return <></>;
}
}

function RenderBricks({ brickData }: { brickData: Brick }) {
return (
<>
{getBrick(brickData.type, brickData.instance, brickData.coords)}
{brickData.children &&
brickData.children?.length > 0 &&
brickData.children.map((child) => <RenderBricks key={child.id} brickData={child} />)}
</>
);
}

function WorkSpace() {
return (
<div>
{WORKSPACES_DATA.map((workspace) => (
<svg
version="1.1"
xmlns="http://www.w3.org/2000/svg"
key={workspace.id}
height="500px"
width="500px"
>
{workspace.data.map((brick) => {
return <RenderBricks key={brick.id} brickData={brick} />;
})}
</svg>
))}
</div>
);
}

export default WorkSpace;
3 changes: 2 additions & 1 deletion modules/code-builder/src/brick/design0/BrickBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default class BrickBlock extends BrickModelBlock {
scale: number;
connectAbove: boolean;
connectBelow: boolean;
nestLengthY?: number;
}) {
super(params);
const argsKeys = Object.keys(this._args);
Expand All @@ -41,7 +42,7 @@ export default class BrickBlock extends BrickModelBlock {
hasNotchInsTop: this._connectAbove,
hasNotchInsBot: this._connectBelow,
scale: this._scale,
nestLengthY: 30,
nestLengthY: params.nestLengthY ?? 17,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious where the number 17 comes from?

innerLengthX: 100,
argHeights: Array.from({ length: argsKeys.length }, () => 17),
});
Expand Down
14 changes: 9 additions & 5 deletions modules/code-builder/src/brick/design0/components/BrickBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import type { JSX } from 'react';
import type { IBrickBlock } from '@/@types/brick';
import type { IBrickBlock, TBrickCoords } from '@/@types/brick';

// -------------------------------------------------------------------------------------------------

export default function (props: { instance: IBrickBlock }): JSX.Element {
const { instance } = props;

export default function ({
instance,
coords = { x: 0, y: 0 },
}: {
instance: IBrickBlock;
coords?: TBrickCoords;
}): JSX.Element {
return (
<g transform={`scale(${instance.scale})`}>
<g transform={`translate(${coords?.x},${coords?.y}) scale(${instance.scale})`}>
<path
d={instance.SVGpath}
style={{
Expand Down
14 changes: 9 additions & 5 deletions modules/code-builder/src/brick/design0/components/BrickData.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import type { JSX } from 'react';
import type { IBrickData } from '@/@types/brick';

export default function (props: { instance: IBrickData }): JSX.Element {
const { instance } = props;
import type { IBrickData, TBrickCoords } from '@/@types/brick';

export default function ({
instance,
coords = { x: 0, y: 0 },
}: {
instance: IBrickData;
coords?: TBrickCoords;
}): JSX.Element {
return (
<g transform={`scale(${instance.scale})`}>
<g transform={`translate(${coords?.x},${coords?.y}) scale(${instance.scale})`}>
<path
d={instance.SVGpath}
style={{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import type { JSX } from 'react';
import type { IBrickExpression } from '@/@types/brick';
import type { IBrickExpression, TBrickCoords } from '@/@types/brick';

// -------------------------------------------------------------------------------------------------

export default function (props: { instance: IBrickExpression }): JSX.Element {
const { instance } = props;

export default function ({
instance,
coords = { x: 0, y: 0 },
}: {
instance: IBrickExpression;
coords?: TBrickCoords;
}): JSX.Element {
return (
<g transform={`scale(${instance.scale})`}>
<g transform={`translate(${coords?.x},${coords?.y}) scale(${instance.scale})`}>
<path
d={instance.SVGpath}
style={{
Expand Down
Loading