Skip to content

Commit

Permalink
feat(masonry): [block] modify React component and Storybook stories
Browse files Browse the repository at this point in the history
  • Loading branch information
meganindya committed Sep 7, 2024
1 parent dfd5501 commit 54a2598
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 98 deletions.
5 changes: 5 additions & 0 deletions modules/masonry/src/@types/brick.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,9 @@ export interface IBrickBlock extends IBrickInstruction, IBrickNotchInsNestTopSta
* @param extent width and height values of the nest area
*/
setBoundingBoxNest(extent: TExtent): void;

get connPointsFixed(): Record<
'insTop' | 'insBottom' | 'insNest',
{ extent: TExtent; coords: TCoords }
>;
}
2 changes: 1 addition & 1 deletion modules/masonry/src/brick/design0/brickFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function argsRecordToArray(
export function createBrickBlock(params: {
name: string;
label: string;
glyph: string;
glyph?: string;
args: { id: string; label: string }[];
colorBg: TColor;
colorFg: TColor;
Expand Down
43 changes: 20 additions & 23 deletions modules/masonry/src/brick/design0/components/BrickBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
import React from 'react';
import type { TBrickRenderPropsBlock, TCoords } from '@/@types/brick';
import type { TBrickRenderPropsBlock } from '@/@types/brick';

interface BrickBlockProps {
instance: TBrickRenderPropsBlock;
coords?: TCoords;
}

const BrickBlock: React.FC<BrickBlockProps> = ({ instance, coords = { x: 0, y: 0 } }) => {
const BrickBlock: React.FC<TBrickRenderPropsBlock> = ({
path,
label,
labelArgs,
colorBg,
colorFg,
outline,
scale,
}) => {
return (
<g transform={`translate(${coords.x},${coords.y}) scale(${instance.scale})`}>
<g transform={`scale(${scale})`}>
<path
d={instance.path}
fill={instance.colorBg as string}
stroke={instance.outline as string}
d={path}
fill={colorBg as string}
stroke={outline as string}
strokeWidth={1}
strokeLinecap="round"
/>
<text
x={10}
y={20}
fill={instance.colorFg as string}
fontSize={14}
fontFamily="Arial, sans-serif"
>
{instance.label}
<text x={10} y={20} fill={colorFg as string} fontSize={14} fontFamily="Arial, sans-serif">
{label}
</text>
{instance.labelArgs.map((argLabel, index) => (
{labelArgs.map((argLabel, index) => (
<text
key={index}
x={15}
y={40 + index * 20}
fill={instance.colorFg as string}
fill={colorFg as string}
fontSize={12}
fontFamily="Arial, sans-serif"
>
{argLabel}
</text>
))}
{instance.glyph && (
{/* {glyph && (
<text
x={instance.boundingBoxArgs[0]?.width - 25 || 0}
y={20}
Expand All @@ -47,7 +44,7 @@ const BrickBlock: React.FC<BrickBlockProps> = ({ instance, coords = { x: 0, y: 0
>
{instance.glyph}
</text>
)}
)} */}
</g>
);
};
Expand Down
14 changes: 9 additions & 5 deletions modules/masonry/src/brick/design0/stories/BrickBlock.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export default {

export const NoArgs: Story = {
args: {
Component: CBrickBlock,
prototype: MBrickBlock,
View: CBrickBlock,
Model: MBrickBlock,
showIndicators: true,

label: 'Block',
args: [],
colorBg: 'yellow',
Expand All @@ -24,10 +26,12 @@ export const NoArgs: Story = {

export const WithArgs: Story = {
args: {
Component: CBrickBlock,
prototype: MBrickBlock,
View: CBrickBlock,
Model: MBrickBlock,
showIndicators: true,

label: 'Block',
args: ['Label 1'],
args: ['Label 1', 'Label 2'],
colorBg: 'yellow',
colorFg: 'black',
outline: 'red',
Expand Down
138 changes: 69 additions & 69 deletions modules/masonry/src/brick/stories/components/BrickBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,122 +1,122 @@
import BrickWrapper from './BrickWrapper';
import type { JSX } from 'react';
import type { IBrickBlock, TBrickArgDataType, TBrickColor } from '@/@types/brick';
import type { IBrickBlock, TBrickRenderPropsBlock, TColor } from '@/@types/brick';

import BrickWrapper from './BrickWrapper';

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

export default function (props: {
Component: (props: { instance: IBrickBlock; visualIndicators?: JSX.Element }) => JSX.Element;
prototype: new (params: {
View: React.FC<TBrickRenderPropsBlock>;
Model: new (params: {
uuid: string;
name: string;

label: string;
glyph: string;
args: Record<
string,
{
label: string;
dataType: TBrickArgDataType;
meta: unknown;
}
>;
colorBg: TBrickColor;
colorFg: TBrickColor;
outline: TBrickColor;
scale: number;
glyph?: string;
colorBg: TColor;
colorFg: TColor;
colorBgHighlight: TColor;
colorFgHighlight: TColor;
outline: TColor;
connectAbove: boolean;
connectBelow: boolean;

args: {
/** unique identifier of the argument */
id: string;
/** label for the argument */
label: string;
}[];
}) => IBrickBlock;
showIndicators: boolean;

label: string;
args: string[];
colorBg: string;
colorFg: string;
outline: string;
scale: number;
}): JSX.Element {
const { Component, prototype, label, args, colorBg, colorFg, outline, scale } = props;
const { View, Model, showIndicators, label, args, colorBg, colorFg, outline, scale } = props;

const instance = new Model({
uuid: '',
name: '',

const instance = new prototype({
label,
args: Object.fromEntries(
args.map<[string, { label: string; dataType: TBrickArgDataType; meta: unknown }]>((name) => [
name,
{ label: name, dataType: 'any', meta: undefined },
]),
),
colorBg,
colorFg,
colorBgHighlight: '',
colorFgHighlight: '',
outline,
scale,
glyph: '',
connectAbove: true,
connectBelow: true,
name: '',

args: args.map((label, i) => ({ id: `label_${i}`, label })),
});

instance.scale = scale;

const VisualIndicators = () => (
<>
{/* Overall Bounding Box of the Brick */}
<rect
x={instance.bBoxBrick.coords.x}
y={instance.bBoxBrick.coords.y}
height={instance.bBoxBrick.extent.height}
width={instance.bBoxBrick.extent.width}
x={0}
y={0}
height={instance.boundingBox.height}
width={instance.boundingBox.width}
fill="black"
opacity={0.25}
/>

{/* Right args bounding box */}
{Object.keys(instance.bBoxArgs).map((name, i) => {
const arg = instance.bBoxArgs[name];

return (
<rect
key={i}
x={arg.coords.x}
y={arg.coords.y}
height={arg.extent.height}
width={arg.extent.width}
fill="green"
opacity={0.8}
/>
);
})}

{/* Top instruction notch bounding box */}
{/* Connection point of Top */}
<rect
x={instance.bBoxNotchInsTop.coords.x}
y={instance.bBoxNotchInsTop.coords.y}
height={instance.bBoxNotchInsTop.extent.height}
width={instance.bBoxNotchInsTop.extent.width}
x={instance.connPointsFixed.insTop.coords.y}
y={instance.connPointsFixed.insTop.coords.y}
height={instance.connPointsFixed.insTop.extent.height}
width={instance.connPointsFixed.insTop.extent.width}
fill="green"
opacity={0.9}
opacity={0.75}
/>

{/* Bottom instruction notch bounding box */}
{/* Connection point of Bottom */}
<rect
x={instance.bBoxNotchInsBot.coords.x}
y={instance.bBoxNotchInsBot.coords.y}
height={instance.bBoxNotchInsBot.extent.height}
width={instance.bBoxNotchInsBot.extent.width}
x={instance.connPointsFixed.insBottom.coords.y}
y={instance.connPointsFixed.insBottom.coords.y}
height={instance.connPointsFixed.insBottom.extent.height}
width={instance.connPointsFixed.insBottom.extent.width}
fill="green"
opacity={0.9}
opacity={0.75}
/>

{/* Top instruction notch inside nesting bounding box */}
{/* Connection point of Nesting */}
<rect
x={instance.bBoxNotchInsNestTop.coords.x}
y={instance.bBoxNotchInsNestTop.coords.y}
height={instance.bBoxNotchInsNestTop.extent.height}
width={instance.bBoxNotchInsNestTop.extent.width}
x={instance.connPointsFixed.insNest.coords.y}
y={instance.connPointsFixed.insNest.coords.y}
height={instance.connPointsFixed.insNest.extent.height}
width={instance.connPointsFixed.insNest.extent.width}
fill="green"
opacity={0.9}
opacity={0.75}
/>

{/* Connection points of Args */}
{Object.values(instance.connPointsArg).map(({ extent, coords }) => (
<rect
x={coords.y}
y={coords.y}
height={extent.height}
width={extent.width}
fill="purple"
opacity={0.75}
/>
))}
</>
);

return (
<BrickWrapper>
<Component instance={instance} />
<VisualIndicators />
<View {...instance.renderProps} />
{showIndicators && <VisualIndicators />}
</BrickWrapper>
);
}

0 comments on commit 54a2598

Please sign in to comment.