1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
import { GridComponent } from '@/components/grid/GridComponent';
import { ColorSwatch } from '@/components/toolbar/ColorSwatch';
import { Toolbar } from '@/components/toolbar/Toolbar';
import { ToolbarItem } from '@/components/toolbar/ToolbarItem';
import { SaveModal } from '@/components/SaveModal';
import type { AnsiTermColor, Grid, GridCell } from '@/types/grid';
import { gridToAnsi } from '@/utils/grid';
import { saveArt } from '@/utils/storage';
import { type IZipper, ListZipper } from '@emprespresso/pengueno';
import { useCallback, useState } from 'react';
import { flushSync } from 'react-dom';
export interface ChooseArtProps {
grid: Grid;
onGoHome?: () => void;
}
// Gruvbox theme colors converted to ANSI RGB values
const defaultColors: AnsiTermColor[] = [
{ foreground: { r: 5, g: 5, b: 5 }, background: null }, // bright7 #ebdbb2
{ foreground: { r: 5, g: 1, b: 1 }, background: null }, // regular1 #cc241d
{ foreground: { r: 4, g: 4, b: 1 }, background: null }, // regular2 #98971a
{ foreground: { r: 5, g: 4, b: 1 }, background: null }, // regular3 #d79921
{ foreground: { r: 2, g: 3, b: 4 }, background: null }, // regular4 #458588
{ foreground: { r: 4, g: 2, b: 3 }, background: null }, // regular5 #b16286
{ foreground: { r: 3, g: 4, b: 3 }, background: null }, // regular6 #689d6a
{ foreground: { r: 4, g: 4, b: 3 }, background: null }, // regular7 #a89984
];
export const Paint: React.FC<ChooseArtProps> = ({ grid, onGoHome }) => {
const [showSaveModal, setShowSaveModal] = useState(false);
const [selectedColor, setSelectedColor] = useState<AnsiTermColor>({
foreground: { r: 5, g: 5, b: 5 },
background: null,
});
const [history, setHistory] = useState<IZipper<Grid>>(
ListZipper.from([grid]),
);
const [isDragging, setIsDragging] = useState(false);
const [workingGrid, setWorkingGrid] = useState<Grid>(grid);
const handleDragStart = useCallback(() => {
setIsDragging(true);
// Don't reset workingGrid here - let it accumulate changes
}, []);
const handleDragEnd = useCallback(() => {
if (isDragging) {
// Commit the working grid to history as a single atomic operation
setHistory((currentHistory) => {
return currentHistory.prepend(workingGrid).previous().get();
});
setIsDragging(false);
}
}, [isDragging, workingGrid]);
const cellInteractionCallback = useCallback(
(cell: GridCell) => {
flushSync(() => {
setWorkingGrid((currentGrid) => {
const newGrid = currentGrid.map((row) => [...row]);
newGrid[cell.y][cell.x] = { ...cell, color: selectedColor };
return newGrid;
});
});
},
[selectedColor],
);
const handleSave = (name: string) => {
const currentGrid = history.read().get();
saveArt(name, currentGrid);
};
const currentGrid = workingGrid;
const ansiOutput = gridToAnsi(currentGrid);
return (
<div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
{showSaveModal && (
<SaveModal
ansiOutput={ansiOutput}
onSave={handleSave}
onClose={() => setShowSaveModal(false)}
/>
)}
<Toolbar>
<ToolbarItem
renderContent={(onClose) => (
<ColorSwatch
onSelect={setSelectedColor}
onClose={onClose}
defaultColors={defaultColors}
></ColorSwatch>
)}
>
🎨
</ToolbarItem>
<ToolbarItem
disabled={
!history
.previous()
.flatMap((it) => it.read())
.present()
}
onClick={() => {
setHistory((history) => {
const newHistory = history.previous().get();
setWorkingGrid(newHistory.read().get());
return newHistory;
});
}}
>
↷
</ToolbarItem>
<ToolbarItem
disabled={
!history
.next()
.flatMap((it) => it.read())
.present()
}
onClick={() => {
setHistory((history) => {
const newHistory = history.next().get();
setWorkingGrid(newHistory.read().get());
return newHistory;
});
}}
>
↶
</ToolbarItem>
<ToolbarItem onClick={() => setShowSaveModal(true)}>
💾
</ToolbarItem>
<ToolbarItem onClick={onGoHome}>
🏠
</ToolbarItem>
</Toolbar>
<GridComponent
grid={currentGrid}
onCellInteract={cellInteractionCallback}
onDragStart={handleDragStart}
onDragEnd={handleDragEnd}
/>
</div>
);
};
|