summaryrefslogtreecommitdiff
path: root/src/pages
diff options
context:
space:
mode:
authorElizabeth Hunt <me@liz.coffee>2025-10-04 18:36:10 -0700
committerElizabeth Hunt <me@liz.coffee>2025-10-04 18:36:10 -0700
commit35add63ec4dce39710095f17abd86777de9e5b49 (patch)
tree1afdf952f310e09a663e85541474efdc95155a73 /src/pages
parent507c972ecafeceaf4f8962ad881f8fb50c9b86c1 (diff)
downloadansicolor-35add63ec4dce39710095f17abd86777de9e5b49.tar.gz
ansicolor-35add63ec4dce39710095f17abd86777de9e5b49.zip
Working history state
Diffstat (limited to 'src/pages')
-rw-r--r--src/pages/Paint.tsx65
1 files changed, 60 insertions, 5 deletions
diff --git a/src/pages/Paint.tsx b/src/pages/Paint.tsx
index 5284b26..136a347 100644
--- a/src/pages/Paint.tsx
+++ b/src/pages/Paint.tsx
@@ -1,11 +1,66 @@
-import { useState } from 'react';
+import { GridComponent } from '@/components/grid/GridComponent';
+import type { AnsiTermColor, Grid, GridCell } from '@/types/grid';
+import {
+ type IZipper,
+ ListZipper,
+} from '@emprespresso/pengueno';
+import { useCallback, useState } from 'react';
export interface ChooseArtProps {
- art: string;
+ grid: Grid;
}
-export const Paint: React.FC<ChooseArtProps> = ({ art: initArt }) => {
- const [art, setArt] = useState<string>(initArt);
+export const Paint: React.FC<ChooseArtProps> = ({ grid }) => {
+ const [selectedColor, setSelectedColor] = useState<AnsiTermColor>({
+ foreground: { r: 5, g: 5, b: 5 },
+ background: null,
+ });
+ const [history, setHistory] = useState<IZipper<Grid>>(
+ ListZipper.from([grid]),
+ );
- return <div>{art}</div>;
+ const cellInteractionCallback = useCallback(
+ (cell: GridCell) => {
+ setHistory((currentHistory) => {
+ const currentGrid = currentHistory.read().get();
+ const newGrid = currentGrid.map((row) => [...row]); // Deep copy for current state
+ newGrid[cell.y][cell.x] = { ...cell, color: selectedColor };
+ return currentHistory.prepend(newGrid).previous().get();
+ });
+ },
+ [selectedColor],
+ );
+
+ return (
+ <div>
+ <GridComponent
+ grid={history.read().get()}
+ onCellInteract={cellInteractionCallback}
+ />
+ <button
+ disabled={
+ !history
+ .next()
+ .flatMap((it) => it.read())
+ .present()
+ }
+ onClick={() => setHistory((history) => history.next().get())}
+ >
+ Undo
+ </button>
+ <button
+ disabled={
+ !history
+ .previous()
+ .flatMap((it) => it.read())
+ .present()
+ }
+ onClick={() =>
+ setHistory((history) => history.previous().get())
+ }
+ >
+ Redo
+ </button>
+ </div>
+ );
};