fix: use useRef() for oldTool instead of useState()

This commit is contained in:
trafficlunar 2025-01-18 21:54:35 +00:00
parent dcf4cea0c4
commit 2037ad8722

View file

@ -42,8 +42,8 @@ function Canvas() {
const [dragging, setDragging] = useState(false); const [dragging, setDragging] = useState(false);
const [holdingAlt, setHoldingAlt] = useState(false); const [holdingAlt, setHoldingAlt] = useState(false);
const [oldTool, setOldTool] = useState<Tool>("hand");
const selectionBoxBoundsRef = useRef<BoundingBox>(); const selectionBoxBoundsRef = useRef<BoundingBox>();
const oldToolRef = useRef<Tool>();
const visibleArea = useMemo(() => { const visibleArea = useMemo(() => {
const blockSize = 16 * scale; const blockSize = 16 * scale;
@ -302,7 +302,7 @@ function Canvas() {
switch (e.key) { switch (e.key) {
case " ": // Space case " ": // Space
setDragging(true); setDragging(true);
setOldTool(tool); oldToolRef.current = tool;
setTool("hand"); setTool("hand");
setCssCursor("grabbing"); setCssCursor("grabbing");
break; break;
@ -344,9 +344,10 @@ function Canvas() {
const onKeyUp = (e: KeyboardEvent) => { const onKeyUp = (e: KeyboardEvent) => {
switch (e.key) { switch (e.key) {
case " ": // Space case " ": // Space
if (!oldToolRef.current) return;
setDragging(false); setDragging(false);
setCssCursor("grab"); setCssCursor("grab");
setTool(oldTool); setTool(oldToolRef.current);
break; break;
case "Alt": case "Alt":
setHoldingAlt(false); setHoldingAlt(false);