Skip to content

Commit

Permalink
#299 add custom hook for keyboard displacements
Browse files Browse the repository at this point in the history
  • Loading branch information
oleojake committed Aug 30, 2024
1 parent 6c524b1 commit 90087a8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/pods/canvas/canvas.pod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { EditableComponent } from '@/common/components/inline-edit';
import { useSnapIn } from './use-snapin.hook';
import { ShapeType } from '@/core/model';
import { useDropImageFromDesktop } from './use-drop-image-from-desktop';
import { useKeyboardDisplacement } from './use-keyboard-displacement';

export const CanvasPod = () => {
const [isTransfomerBeingDragged, setIsTransfomerBeingDragged] =
Expand Down Expand Up @@ -86,6 +87,8 @@ export const CanvasPod = () => {
updateShapePosition(id, { x, y });
};

useKeyboardDisplacement();

{
/* TODO: add other animation for isDraggerOver */
}
Expand Down
51 changes: 51 additions & 0 deletions src/pods/canvas/use-keyboard-displacement.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useEffect } from 'react';
import { useCanvasContext } from '@/core/providers';

export const useKeyboardDisplacement = () => {
const { selectionInfo, updateShapePosition } = useCanvasContext();

useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
event.preventDefault();

const selectedShapeData = selectionInfo.getSelectedShapeData();
if (selectedShapeData) {
const step = event.shiftKey ? 25 : 2; // If shift is pressed, move faster
switch (event.key) {
case 'ArrowUp':
updateShapePosition(selectionInfo.selectedShapeId, {
x: selectedShapeData.x,
y: selectedShapeData.y - step,
});
break;
case 'ArrowDown':
updateShapePosition(selectionInfo.selectedShapeId, {
x: selectedShapeData.x,
y: selectedShapeData.y + step,
});
break;
case 'ArrowLeft':
updateShapePosition(selectionInfo.selectedShapeId, {
x: selectedShapeData.x - step,
y: selectedShapeData.y,
});
break;
case 'ArrowRight':
updateShapePosition(selectionInfo.selectedShapeId, {
x: selectedShapeData.x + step,
y: selectedShapeData.y,
});
break;
default:
break;
}
}
};

window.addEventListener('keydown', handleKeyDown);

return () => {
window.removeEventListener('keydown', handleKeyDown);
};
}, [selectionInfo.selectedShapeId, selectionInfo.getSelectedShapeData]);
};

0 comments on commit 90087a8

Please sign in to comment.