Skip to content
Open
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
272 changes: 84 additions & 188 deletions app/board/[boardId]/_components/selection-box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,199 +8,95 @@ import { useSelectionBounds } from "@/hooks/use-selection-bounds";

interface SelectionBoxProps {
onResizeHandlePointerDown: (corner: Side, initialBounds: XYWH) => void;
};
}

const HANDLE_WIDTH = 8;

export const SelectionBox = memo(({
onResizeHandlePointerDown,
}: SelectionBoxProps) => {
const soleLayerId = useSelf((me) =>
me.presence.selection.length === 1 ? me.presence.selection[0] : null
);
export const SelectionBox = memo(
({ onResizeHandlePointerDown }: SelectionBoxProps) => {
const soleLayerId = useSelf((me) =>
me.presence.selection.length === 1 ? me.presence.selection[0] : null
);

const isShowingHandles = useStorage((root) =>
soleLayerId && root.layers.get(soleLayerId)?.type !== LayerType.Path
);
const isShowingHandles = useStorage(
(root) =>
soleLayerId && root.layers.get(soleLayerId)?.type !== LayerType.Path
);

const bounds = useSelectionBounds();
const bounds = useSelectionBounds();

if (!bounds) {
return null;
}
if (!bounds) {
return null;
}

const rect = {
cardinal: ["nwse", "ns", "nesw", "ew", "nwse", "ns", "nesw", "ew"],
boundsX: [
bounds.x - HANDLE_WIDTH / 2,
bounds.x + bounds.width / 2 - HANDLE_WIDTH / 2,
bounds.x + bounds.width - HANDLE_WIDTH / 2,
bounds.x + bounds.width - HANDLE_WIDTH / 2,
bounds.x + bounds.width - HANDLE_WIDTH / 2,
bounds.x + bounds.width / 2 - HANDLE_WIDTH / 2,
bounds.x - HANDLE_WIDTH / 2,
bounds.x - HANDLE_WIDTH / 2,
],
boundsY: [
bounds.y - HANDLE_WIDTH / 2,
bounds.y - HANDLE_WIDTH / 2,
bounds.y - HANDLE_WIDTH / 2,
bounds.y + bounds.height / 2 - HANDLE_WIDTH / 2,
bounds.y + bounds.height - HANDLE_WIDTH / 2,
bounds.y + bounds.height - HANDLE_WIDTH / 2,
bounds.y + bounds.height - HANDLE_WIDTH / 2,
bounds.y + bounds.height / 2 - HANDLE_WIDTH / 2,
],
resizeSide: [
Side.Top + Side.Left,
Side.Top,
Side.Top + Side.Right,
Side.Right,
Side.Bottom + Side.Right,
Side.Bottom,
Side.Bottom + Side.Left,
Side.Left,
],
};

return (
<>
<rect
className="fill-transparent stroke-blue-500 stroke-1 pointer-events-none"
style={{
transform: `translate(${bounds.x}px, ${bounds.y}px)`,
}}
x={0}
y={0}
width={bounds.width}
height={bounds.height}
/>
{isShowingHandles && (
<>
<rect
className="fill-white stroke-1 stroke-blue-500"
x={0}
y={0}
style={{
cursor: "nwse-resize",
width: `${HANDLE_WIDTH}px`,
height: `${HANDLE_WIDTH}px`,
transform: `
translate(
${bounds.x - HANDLE_WIDTH / 2}px,
${bounds.y - HANDLE_WIDTH / 2}px
)
`
}}
onPointerDown={(e) => {
e.stopPropagation();
onResizeHandlePointerDown(Side.Top + Side.Left, bounds);
}}
/>
<rect
className="fill-white stroke-1 stroke-blue-500"
x={0}
y={0}
style={{
cursor: "ns-resize",
width: `${HANDLE_WIDTH}px`,
height: `${HANDLE_WIDTH}px`,
transform: `
translate(
${bounds.x + bounds.width / 2 - HANDLE_WIDTH / 2}px,
${bounds.y - HANDLE_WIDTH / 2}px
)
`
}}
onPointerDown={(e) => {
e.stopPropagation();
onResizeHandlePointerDown(Side.Top, bounds);
}}
/>
<rect
className="fill-white stroke-1 stroke-blue-500"
x={0}
y={0}
style={{
cursor: "nesw-resize",
width: `${HANDLE_WIDTH}px`,
height: `${HANDLE_WIDTH}px`,
transform: `
translate(
${bounds.x - HANDLE_WIDTH / 2 + bounds.width}px,
${bounds.y - HANDLE_WIDTH / 2}px
)`
}}
onPointerDown={(e) => {
e.stopPropagation();
onResizeHandlePointerDown(Side.Top + Side.Right, bounds);
}}
/>
<rect
className="fill-white stroke-1 stroke-blue-500"
x={0}
y={0}
style={{
cursor: "ew-resize",
width: `${HANDLE_WIDTH}px`,
height: `${HANDLE_WIDTH}px`,
transform: `
translate(
${bounds.x - HANDLE_WIDTH / 2 + bounds.width}px,
${bounds.y + bounds.height / 2 - HANDLE_WIDTH / 2}px
)`
}}
onPointerDown={(e) => {
e.stopPropagation();
onResizeHandlePointerDown(Side.Right, bounds);
}}
/>
<rect
className="fill-white stroke-1 stroke-blue-500"
x={0}
y={0}
style={{
cursor: "nwse-resize",
width: `${HANDLE_WIDTH}px`,
height: `${HANDLE_WIDTH}px`,
transform: `
translate(
${bounds.x - HANDLE_WIDTH / 2 + bounds.width}px,
${bounds.y - HANDLE_WIDTH / 2 + bounds.height}px
)`
}}
onPointerDown={(e) => {
e.stopPropagation();
onResizeHandlePointerDown(Side.Bottom + Side.Right, bounds);
}}
/>
<rect
className="fill-white stroke-1 stroke-blue-500"
x={0}
y={0}
style={{
cursor: "ns-resize",
width: `${HANDLE_WIDTH}px`,
height: `${HANDLE_WIDTH}px`,
transform: `
translate(
${bounds.x + bounds.width / 2 - HANDLE_WIDTH / 2}px,
${bounds.y - HANDLE_WIDTH / 2 + bounds.height}px
)`
}}
onPointerDown={(e) => {
e.stopPropagation();
onResizeHandlePointerDown(Side.Bottom, bounds);
}}
/>
<rect
className="fill-white stroke-1 stroke-blue-500"
x={0}
y={0}
style={{
cursor: "nesw-resize",
width: `${HANDLE_WIDTH}px`,
height: `${HANDLE_WIDTH}px`,
transform: `
translate(
${bounds.x - HANDLE_WIDTH / 2}px,
${bounds.y - HANDLE_WIDTH / 2 + bounds.height}px
)`
}}
onPointerDown={(e) => {
e.stopPropagation();
onResizeHandlePointerDown(Side.Bottom + Side.Left, bounds);
}}
/>
<rect
className="fill-white stroke-1 stroke-blue-500"
x={0}
y={0}
style={{
cursor: "ew-resize",
width: `${HANDLE_WIDTH}px`,
height: `${HANDLE_WIDTH}px`,
transform: `
translate(
${bounds.x - HANDLE_WIDTH / 2}px,
${bounds.y - HANDLE_WIDTH / 2 + bounds.height / 2}px
)`
}}
onPointerDown={(e) => {
e.stopPropagation();
onResizeHandlePointerDown(Side.Left, bounds);
}}
/>
</>
)}
</>
);
});
return (
<>
<rect
className="fill-transparent stroke-blue-500 stroke-1 pointer-events-none"
style={{
transform: `translate(${bounds.x}px, ${bounds.y}px)`,
}}
x={0}
y={0}
width={bounds.width}
height={bounds.height}
/>
{isShowingHandles &&
rect.cardinal.map((direction, i) => (
<rect
key={i}
className="fill-white stroke-1 stroke-blue-500"
x={0}
y={0}
style={{
cursor: `${direction}-resize`,
width: `${HANDLE_WIDTH}px`,
height: `${HANDLE_WIDTH}px`,
transform: `translate(${rect.boundsX[i]}px, ${rect.boundsY[i]}px)`,
}}
onPointerDown={(e) => {
e.stopPropagation();
onResizeHandlePointerDown(rect.resizeSide[i], bounds);
}}
/>
))}
</>
);
}
);

SelectionBox.displayName = "SelectionBox";
SelectionBox.displayName = "SelectionBox";