mirror of
https://github.com/trafficlunar/blockmatic.git
synced 2026-06-28 06:34:13 +00:00
refactor: move selection util script into selection context
This commit is contained in:
parent
4687330f02
commit
a80119c011
6 changed files with 22 additions and 38 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import { createContext, ReactNode, useState } from "react";
|
||||
import { createContext, ReactNode, useContext, useState } from "react";
|
||||
import { CanvasContext } from "./Canvas";
|
||||
|
||||
interface Context {
|
||||
selectionCoords: CoordinateArray;
|
||||
|
|
@ -6,6 +7,7 @@ interface Context {
|
|||
setSelectionCoords: React.Dispatch<React.SetStateAction<CoordinateArray>>;
|
||||
setSelectionLayerBlocks: React.Dispatch<React.SetStateAction<Block[]>>;
|
||||
isInSelection: (x: number, y: number) => boolean;
|
||||
confirmSelection: () => void;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
|
|
@ -15,6 +17,8 @@ interface Props {
|
|||
export const SelectionContext = createContext<Context>({} as Context);
|
||||
|
||||
export const SelectionProvider = ({ children }: Props) => {
|
||||
const { blocks, setBlocks } = useContext(CanvasContext);
|
||||
|
||||
const [selectionCoords, setSelectionCoords] = useState<CoordinateArray>([]);
|
||||
const [selectionLayerBlocks, setSelectionLayerBlocks] = useState<Block[]>([]);
|
||||
|
||||
|
|
@ -25,6 +29,14 @@ export const SelectionProvider = ({ children }: Props) => {
|
|||
return true;
|
||||
};
|
||||
|
||||
const confirmSelection = () => {
|
||||
const combinedBlocks = [...blocks, ...selectionLayerBlocks];
|
||||
const uniqueBlocks = Array.from(new Map(combinedBlocks.map((block) => [`${block.x},${block.y}`, block])).values());
|
||||
|
||||
setBlocks(uniqueBlocks);
|
||||
setSelectionLayerBlocks([]);
|
||||
};
|
||||
|
||||
return (
|
||||
<SelectionContext.Provider
|
||||
value={{
|
||||
|
|
@ -33,6 +45,7 @@ export const SelectionProvider = ({ children }: Props) => {
|
|||
setSelectionCoords,
|
||||
setSelectionLayerBlocks,
|
||||
isInSelection,
|
||||
confirmSelection,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue