@@ -178,6 +178,13 @@ const FILL_COLORS_DARK = [
178178
179179const FILL_COLORS_LENGTH = 20 ;
180180
181+ const MAX_UNDO = 15 ; // WARN: tweak later if needed (should be ok for now)
182+
183+ let undoStates : ImageData [ ] = [ ] ;
184+
185+ let saveMS = performance . now ( ) ;
186+ let undoMS = performance . now ( ) ;
187+
181188let prevMS = performance . now ( ) ;
182189let latestColorChangeMS = performance . now ( ) ;
183190
@@ -1278,6 +1285,24 @@ export function animateGraph(
12781285 annotationSecondLastPos = mousePos ;
12791286 annotationLastPos = mousePos ;
12801287
1288+ const curMS = performance . now ( ) ;
1289+
1290+ if ( curMS - saveMS >= 250 ) {
1291+ const savedState = ctxAnnotation . getImageData (
1292+ 0 ,
1293+ 0 ,
1294+ canvasWidth ,
1295+ canvasHeight ,
1296+ ) ;
1297+ undoStates . push ( savedState ) ;
1298+
1299+ if ( undoStates . length > MAX_UNDO ) {
1300+ undoStates . shift ( ) ;
1301+ }
1302+
1303+ saveMS = curMS ;
1304+ }
1305+
12811306 if ( settings . drawMode === "pen" ) {
12821307 inAnnotation = true ;
12831308 inErase = false ;
@@ -1317,6 +1342,33 @@ export function animateGraph(
13171342 inErase = false ;
13181343 } ) ;
13191344
1345+ const undo = ( ) => {
1346+ if ( undoStates . length === 0 ) return ;
1347+
1348+ const lastState = undoStates . pop ( ) ;
1349+
1350+ ctxAnnotation . putImageData ( lastState as ImageData , 0 , 0 ) ;
1351+ } ;
1352+
1353+ window . addEventListener ( "keydown" , ( event : KeyboardEvent ) => {
1354+ const isCtrl = event . ctrlKey || event . metaKey ;
1355+ const isZ = event . key . toLowerCase ( ) === "z" ;
1356+ const inInput = document . activeElement instanceof HTMLTextAreaElement ;
1357+
1358+ if ( isCtrl && isZ ) {
1359+ if ( inInput ) return ;
1360+
1361+ event . preventDefault ( ) ;
1362+ const curMS = performance . now ( ) ;
1363+
1364+ if ( curMS - undoMS <= 100 ) return ;
1365+
1366+ undo ( ) ;
1367+
1368+ undoMS = curMS ;
1369+ }
1370+ } ) ;
1371+
13201372 canvas . addEventListener ( "pointerdown" , ( event ) => {
13211373 event . preventDefault ( ) ;
13221374
0 commit comments