refactor: add affixes to variable names in selection context

This commit is contained in:
trafficlunar 2025-02-07 17:10:09 +00:00
parent 634b6c24cb
commit df016ddf74
6 changed files with 28 additions and 24 deletions

View file

@ -31,12 +31,7 @@ PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.NEAREST;
function Canvas() {
const { stageSize, canvasSize, blocks, coords, scale, version, setStageSize, setBlocks, setCoords, setScale } = useContext(CanvasContext);
const {
coords: selectionCoords,
layerBlocks: selectionLayerBlocks,
setCoords: setSelectionCoords,
setLayerBlocks: setSelectionLayerBlocks,
} = useContext(SelectionContext);
const { selectionCoords, selectionLayerBlocks, setSelectionCoords, setSelectionLayerBlocks } = useContext(SelectionContext);
const { settings } = useContext(SettingsContext);
const { missingTexture } = useContext(TexturesContext);
const { isDark } = useContext(ThemeContext);

View file

@ -10,13 +10,13 @@ import { Button } from "@/components/ui/button";
function SelectionBar() {
const { blocks, setBlocks } = useContext(CanvasContext);
const { layerBlocks, setLayerBlocks } = useContext(SelectionContext);
const { selectionLayerBlocks, setSelectionLayerBlocks } = useContext(SelectionContext);
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
setIsVisible(layerBlocks.length !== 0);
}, [layerBlocks]);
setIsVisible(selectionLayerBlocks.length !== 0);
}, [selectionLayerBlocks]);
return (
<div
@ -25,11 +25,11 @@ function SelectionBar() {
`}
>
{/* todo: place back blocks removed */}
<Button variant="ghost" className="w-8 h-8" onClick={() => setLayerBlocks([])}>
<Button variant="ghost" className="w-8 h-8" onClick={() => setSelectionLayerBlocks([])}>
<XIcon />
</Button>
<span className="mx-2 text-[0.85rem]">Confirm selection?</span>
<Button variant="ghost" className="w-8 h-8" onClick={() => selection.confirm(blocks, layerBlocks, setBlocks, setLayerBlocks)}>
<Button variant="ghost" className="w-8 h-8" onClick={() => selection.confirm(blocks, selectionLayerBlocks, setBlocks, setSelectionLayerBlocks)}>
<CheckIcon />
</Button>
</div>

View file

@ -10,7 +10,7 @@ import { MenubarContent, MenubarItem, MenubarMenu, MenubarSeparator, MenubarShor
function EditMenu() {
const { blocks, setBlocks } = useContext(CanvasContext);
const { coords: selectionCoords, setCoords: setSelectionCoords, setLayerBlocks: setSelectionLayerBlocks } = useContext(SelectionContext);
const { selectionCoords, setSelectionCoords, setSelectionLayerBlocks } = useContext(SelectionContext);
const { setTool } = useContext(ToolContext);
const cut = () => {

View file

@ -9,7 +9,7 @@ import * as selection from "@/utils/selection";
function SelectMenu() {
const { canvasSize } = useContext(CanvasContext);
const { coords: selectionCoords, setCoords: setSelectionCoords } = useContext(SelectionContext);
const { selectionCoords, setSelectionCoords } = useContext(SelectionContext);
// Add every block within the canvas size to the temporary array
const selectAll = () => {

View file

@ -14,7 +14,7 @@ import * as selection from "@/utils/selection";
function Replace() {
const { version, setBlocks } = useContext(CanvasContext);
const { coords: selectionCoords } = useContext(SelectionContext);
const { selectionCoords } = useContext(SelectionContext);
const { selectedBlock, tool, setTool } = useContext(ToolContext);
const { missingTexture } = useContext(TexturesContext);

View file

@ -1,10 +1,11 @@
import { createContext, ReactNode, useState } from "react";
interface Context {
coords: CoordinateArray;
layerBlocks: Block[];
setCoords: React.Dispatch<React.SetStateAction<CoordinateArray>>;
setLayerBlocks: React.Dispatch<React.SetStateAction<Block[]>>;
selectionCoords: CoordinateArray;
selectionLayerBlocks: Block[];
setSelectionCoords: React.Dispatch<React.SetStateAction<CoordinateArray>>;
setSelectionLayerBlocks: React.Dispatch<React.SetStateAction<Block[]>>;
isInSelection: (x: number, y: number) => boolean;
}
interface Props {
@ -14,16 +15,24 @@ interface Props {
export const SelectionContext = createContext<Context>({} as Context);
export const SelectionProvider = ({ children }: Props) => {
const [coords, setCoords] = useState<CoordinateArray>([]);
const [layerBlocks, setLayerBlocks] = useState<Block[]>([]);
const [selectionCoords, setSelectionCoords] = useState<CoordinateArray>([]);
const [selectionLayerBlocks, setSelectionLayerBlocks] = useState<Block[]>([]);
const isInSelection = (x: number, y: number): boolean => {
if (selectionCoords.length !== 0) {
return selectionCoords.some(([x2, y2]) => x2 === x && y2 === y);
}
return true;
};
return (
<SelectionContext.Provider
value={{
coords,
layerBlocks,
setCoords,
setLayerBlocks,
selectionCoords,
selectionLayerBlocks,
setSelectionCoords,
setSelectionLayerBlocks,
isInSelection,
}}
>
{children}