diff --git a/archive/number-state-input.tsx b/archive/number-state-input.tsx new file mode 100644 index 0000000..2da20e8 --- /dev/null +++ b/archive/number-state-input.tsx @@ -0,0 +1,32 @@ +interface StateInputProps { + state: PrimitiveAtom; + className?: string; +} + +const NumberStateInput: FC> = ( + props: StateInputProps +) => { + const { state, className } = props; + const [number, setNumber] = useAtom(state); + + const handleChange = useCallback( + (e: ChangeEvent) => { + const re = /^\d*$/; + const v = e.target.value; + if (v === '') { + setNumber(null); + } else if (re.test(v)) { + setNumber(parseInt(v)); + } + }, + [setNumber] + ); + + return ( + + ); +}; diff --git a/src/atoms.ts b/src/atoms.ts index e727f00..9775cb2 100644 --- a/src/atoms.ts +++ b/src/atoms.ts @@ -16,6 +16,3 @@ type DrawMode = 'line' | 'trash' | 'grid'; export const lineColorState = atom(COLORS[0]!); export const drawModeState = atom('line'); - -export const drawGridWidthState = atom(4); -export const drawGridHeightState = atom(4); diff --git a/src/components/grid/grid-config.tsx b/src/components/grid/grid-config.tsx index 5791120..3b3c6b9 100644 --- a/src/components/grid/grid-config.tsx +++ b/src/components/grid/grid-config.tsx @@ -1,12 +1,6 @@ import { ChangeEvent, FC, useCallback } from 'react'; -import { PrimitiveAtom, useAtom, WritableAtom } from 'jotai'; -import { - COLORS, - drawGridHeightState, - drawGridWidthState, - drawModeState, - lineColorState, -} from '../../atoms'; +import { PrimitiveAtom, useAtom } from 'jotai'; +import { COLORS, drawModeState, lineColorState } from '../../atoms'; import './grid-config.scss'; @@ -46,16 +40,14 @@ const GridIcon = ( - - - - - - + + + + + + ); @@ -84,39 +76,6 @@ const ColorOption: FC = (props: ColorOptionProps) => { ); }; -interface StateInputProps { - state: PrimitiveAtom; - className?: string; -} - -const NumberStateInput: FC> = ( - props: StateInputProps -) => { - const { state, className } = props; - const [number, setNumber] = useAtom(state); - - const handleChange = useCallback( - (e: ChangeEvent) => { - const re = /^\d*$/; - const v = e.target.value; - if (v === '') { - setNumber(null); - } else if (re.test(v)) { - setNumber(parseInt(v)); - } - }, - [setNumber] - ); - - return ( - - ); -}; - const ColorConfig: FC = () => (
{COLORS.map((color) => ( @@ -130,8 +89,7 @@ const ModeConfig: FC = () => { const lineClassName = drawMode === 'line' ? 'option active' : 'option'; const trashClassName = drawMode === 'trash' ? 'option active' : 'option'; - const gridClassName = - drawMode === 'grid' ? 'option active grid' : 'option grid'; + const gridClassName = drawMode === 'grid' ? 'option active' : 'option'; return ( <>
@@ -147,22 +105,11 @@ const ModeConfig: FC = () => { > {TrashIcon}
-
-
setDrawMode('grid')} > {GridIcon} - - x -
diff --git a/src/components/grid/grid.tsx b/src/components/grid/grid.tsx index 97d67a0..df5e1ff 100644 --- a/src/components/grid/grid.tsx +++ b/src/components/grid/grid.tsx @@ -181,7 +181,14 @@ const Grid: FC = () => { return; } - const point = snapToGrid(mousePoint, GAP); + let point; + if (userDrawMode === 'grid') { + // snap to the thick grid + point = snapToGrid(mousePoint, GAP * 6); + } else { + // snap to the thin grid for line / trash + point = snapToGrid(mousePoint, GAP); + } if (lodash.isEqual(userStartPoint, point)) { setUserStartPoint(null); @@ -249,6 +256,12 @@ const Grid: FC = () => { // ctrl+z/ctrl+y for undo/redo const onUndo = useCallback(() => { + if (!lodash.isNull(userStartPoint)) { + // if the user has a start point, just cancel the pending line + setUserStartPoint(null); + return; + } + const action = userActions[userActions.length - 1]; if (!action) return; @@ -264,7 +277,7 @@ const Grid: FC = () => { } setUserActions(userActions.slice(0, -1)); setUserRedoActions([...userRedoActions, action]); - }, [userLines, userActions, userRedoActions]); + }, [userLines, userActions, userRedoActions, userStartPoint]); const onRedo = useCallback(() => { const action = userRedoActions[userRedoActions.length - 1]; @@ -341,6 +354,7 @@ const Grid: FC = () => { strokeWidth="2" /> ); + // TODO: grid mode } else { console.assert(userDrawMode === 'trash'); return (