|
| 1 | +import { useState } from 'react'; |
| 2 | +import { useMove } from 'react-aria'; |
| 3 | +import { BrickBlock, BrickData, BrickExpression, BrickStatement } from '@/brick'; |
| 4 | +import { useBricksCoords } from './BricksCoordsStore'; |
| 5 | +import { WORKSPACES_DATA } from './data'; |
| 6 | +import type { Brick } from './data'; |
| 7 | +import { getBelowBricksIds } from './utils'; |
| 8 | + |
| 9 | +const BrickFactory = ({ brickData }: { brickData: Brick }) => { |
| 10 | + const CONTAINER_SIZE_X = 800; |
| 11 | + const CONTAINER_SIZE_Y = 700; |
| 12 | + const BRICK_HEIGHT = brickData.instance.bBoxBrick.extent.height; |
| 13 | + const BRICK_WIDTH = brickData.instance.bBoxBrick.extent.width; |
| 14 | + const { getCoords, setCoords } = useBricksCoords(); |
| 15 | + const brickCoords = getCoords(brickData.id)!; |
| 16 | + const [color, setColor] = useState(brickData.instance.colorBg as string); |
| 17 | + |
| 18 | + const clampX = (pos: number) => Math.min(Math.max(pos, 0), CONTAINER_SIZE_X - BRICK_WIDTH * 2); |
| 19 | + const clampY = (pos: number) => Math.min(Math.max(pos, 0), CONTAINER_SIZE_Y - BRICK_HEIGHT * 2); |
| 20 | + |
| 21 | + const { moveProps } = useMove({ |
| 22 | + onMoveStart(e) { |
| 23 | + console.log(`move start with pointerType = ${e.pointerType}`); |
| 24 | + setColor('white'); |
| 25 | + }, |
| 26 | + onMove(e) { |
| 27 | + const newX = brickCoords.x + e.deltaX; |
| 28 | + const newY = brickCoords.y + e.deltaY; |
| 29 | + setCoords(brickData.id, { x: clampX(newX), y: clampY(newY) }); |
| 30 | + |
| 31 | + brickData.childBricks.forEach((childBrick) => { |
| 32 | + const childBrickCoords = getCoords(childBrick)!; |
| 33 | + setCoords(childBrick, { |
| 34 | + x: childBrickCoords.x + e.deltaX, |
| 35 | + y: childBrickCoords.y + e.deltaY, |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + const belowBrickIds = getBelowBricksIds(WORKSPACES_DATA[0].data, brickData.id); |
| 40 | + belowBrickIds.forEach((belowBrickId) => { |
| 41 | + const belowBrickCoords = getCoords(belowBrickId)!; |
| 42 | + setCoords(belowBrickId, { |
| 43 | + x: belowBrickCoords.x + e.deltaX, |
| 44 | + y: belowBrickCoords.y + e.deltaY, |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + // Normally, we want to allow the user to continue |
| 49 | + // dragging outside the box such that they need to |
| 50 | + // drag back over the ball again before it moves. |
| 51 | + // This is handled below by clamping during render. |
| 52 | + // If using the keyboard, however, we need to clamp |
| 53 | + // here so that dragging outside the container and |
| 54 | + // then using the arrow keys works as expected. |
| 55 | + // if (e.pointerType === 'keyboard') { |
| 56 | + // x = clamp(x); |
| 57 | + // y = clamp(y); |
| 58 | + // } |
| 59 | + |
| 60 | + // setEvents((events) => [ |
| 61 | + // `move with pointerType = ${e.pointerType}, deltaX = ${e.deltaX}, deltaY = ${e.deltaY}`, |
| 62 | + // ...events, |
| 63 | + // ]); |
| 64 | + }, |
| 65 | + onMoveEnd(e) { |
| 66 | + console.log(`move end with pointerType = ${e.pointerType}`); |
| 67 | + setColor(brickData.instance.colorBg as string); |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + const VisualIndicators = () => ( |
| 72 | + <> |
| 73 | + {/* Right args bounding box */} |
| 74 | + {'bBoxArgs' in brickData.instance && ( |
| 75 | + <> |
| 76 | + {Object.keys(brickData.instance.bBoxArgs).map((name, i) => { |
| 77 | + if ('bBoxArgs' in brickData.instance) { |
| 78 | + const arg = brickData.instance.bBoxArgs[name]; |
| 79 | + |
| 80 | + return ( |
| 81 | + <rect |
| 82 | + key={i} |
| 83 | + x={brickCoords.x + arg?.coords.x} |
| 84 | + y={brickCoords.y + arg?.coords.y} |
| 85 | + height={arg?.extent.height} |
| 86 | + width={arg?.extent.width} |
| 87 | + fill="green" |
| 88 | + opacity={0.8} |
| 89 | + /> |
| 90 | + ); |
| 91 | + } |
| 92 | + })} |
| 93 | + </> |
| 94 | + )} |
| 95 | + |
| 96 | + {/* Top instruction notch bounding box */} |
| 97 | + {'bBoxNotchInsTop' in brickData.instance && ( |
| 98 | + <rect |
| 99 | + x={brickCoords.x + brickData.instance.bBoxNotchInsTop?.coords.x} |
| 100 | + y={brickCoords.y + brickData.instance.bBoxNotchInsTop?.coords.y} |
| 101 | + height={brickData.instance.bBoxNotchInsTop?.extent.height} |
| 102 | + width={brickData.instance.bBoxNotchInsTop?.extent.width} |
| 103 | + fill="green" |
| 104 | + opacity={0.9} |
| 105 | + /> |
| 106 | + )} |
| 107 | + |
| 108 | + {/* Bottom instruction notch bounding box */} |
| 109 | + {'bBoxNotchInsBot' in brickData.instance && ( |
| 110 | + <rect |
| 111 | + x={brickCoords.x + brickData.instance.bBoxNotchInsBot?.coords.x} |
| 112 | + y={brickCoords.y + brickData.instance.bBoxNotchInsBot?.coords.y} |
| 113 | + height={brickData.instance.bBoxNotchInsBot?.extent.height} |
| 114 | + width={brickData.instance.bBoxNotchInsBot?.extent.width} |
| 115 | + fill="green" |
| 116 | + opacity={0.9} |
| 117 | + /> |
| 118 | + )} |
| 119 | + |
| 120 | + {/* Top instruction notch inside nesting bounding box */} |
| 121 | + {'bBoxNotchInsNestTop' in brickData.instance && ( |
| 122 | + <rect |
| 123 | + x={brickCoords.x + brickData.instance.bBoxNotchInsNestTop?.coords.x} |
| 124 | + y={brickCoords.y + brickData.instance.bBoxNotchInsNestTop?.coords.y} |
| 125 | + height={brickData.instance.bBoxNotchInsNestTop?.extent.height} |
| 126 | + width={brickData.instance.bBoxNotchInsNestTop?.extent.width} |
| 127 | + fill="green" |
| 128 | + opacity={0.9} |
| 129 | + /> |
| 130 | + )} |
| 131 | + </> |
| 132 | + ); |
| 133 | + |
| 134 | + const getBrick = () => { |
| 135 | + switch (brickData.type) { |
| 136 | + case 'data': |
| 137 | + return ( |
| 138 | + <BrickData |
| 139 | + brickData={brickData} |
| 140 | + moveProps={moveProps} |
| 141 | + coords={brickCoords} |
| 142 | + color={color} |
| 143 | + /> |
| 144 | + ); |
| 145 | + case 'expression': |
| 146 | + return ( |
| 147 | + <BrickExpression |
| 148 | + brickData={brickData} |
| 149 | + moveProps={moveProps} |
| 150 | + coords={brickCoords} |
| 151 | + color={color} |
| 152 | + /> |
| 153 | + ); |
| 154 | + case 'statement': |
| 155 | + return ( |
| 156 | + <BrickStatement |
| 157 | + brickData={brickData} |
| 158 | + moveProps={moveProps} |
| 159 | + coords={brickCoords} |
| 160 | + color={color} |
| 161 | + /> |
| 162 | + ); |
| 163 | + case 'block': |
| 164 | + return ( |
| 165 | + <BrickBlock |
| 166 | + brickData={brickData} |
| 167 | + moveProps={moveProps} |
| 168 | + coords={brickCoords} |
| 169 | + color={color} |
| 170 | + /> |
| 171 | + ); |
| 172 | + default: |
| 173 | + return <></>; |
| 174 | + } |
| 175 | + }; |
| 176 | + |
| 177 | + return ( |
| 178 | + <> |
| 179 | + <VisualIndicators /> |
| 180 | + {/* {getBrick()} */} |
| 181 | + </> |
| 182 | + ); |
| 183 | +}; |
| 184 | + |
| 185 | +export default BrickFactory; |
0 commit comments