Skip to content

Commit

Permalink
Fix double undo bug (closes hackclub#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
kognise committed Apr 11, 2023
1 parent 2b78fc9 commit 39350a7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/components/subeditors/bitmap-editor-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export type TempGrid = (PaletteItem | null)[][]
export type Vector = { x: number, y: number }
export const makeTempGrid = (): TempGrid => new Array(16).fill(0).map(() => new Array(16).fill(null))
export const cloneGrid = <T>(grid: T[][]): T[][] => grid.map(row => row.slice())
export const gridsEq = <T>(a: T[][], b: T[][]): boolean => a.every((row, y) => row.every((cell, x) => cell === b[y]![x]!))
export const gridClamp = (v: Vector): Vector => ({
x: Math.max(0, Math.min(15, v.x)),
y: Math.max(0, Math.min(15, v.y))
Expand Down
14 changes: 11 additions & 3 deletions src/components/subeditors/bitmap-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { EditorProps } from '../../lib/state'
import type { IconType } from 'react-icons'
import { signal, useSignal, useSignalEffect } from '@preact/signals'
import { palette, type PaletteItem, rgbaToHex, transparentBgUrl, transparent } from '../../lib/engine/1-base/palette'
import { drawingTools, gridsEq, makeTempGrid, mirrorGrid, TempGrid, transformTools, Vector } from './bitmap-editor-tools'
import { drawingTools, makeTempGrid, mirrorGrid, TempGrid, transformTools, Vector } from './bitmap-editor-tools'
import { useEffect, useRef } from 'preact/hooks'
import tinykeys from 'tinykeys'
import { IoArrowRedo, IoArrowUndo, IoImage, IoTrash } from 'react-icons/io5'
Expand Down Expand Up @@ -106,10 +106,12 @@ export default function BitmapEditor(props: EditorProps) {

// Sync text changes with pixel grid
useSignalEffect(() => {
const curGrid = moment.peek().pixelGrid
const newGrid = textToPixelGrid(props.text.value)
if (!gridsEq(newGrid, moment.peek().pixelGrid)) {

if (pixelGridToText(curGrid) !== pixelGridToText(newGrid)) {
moment.value = {
pixelGrid: textToPixelGrid(props.text.value),
pixelGrid: newGrid,
previous: moment.peek(),
next: null
}
Expand Down Expand Up @@ -296,6 +298,12 @@ export default function BitmapEditor(props: EditorProps) {
disabled={!moment.value.previous}
onActivate={() => {
if (!moment.value.previous) return
console.log('cur:')
console.log(pixelGridToText(moment.value.pixelGrid))
console.log('prev:')
console.log(pixelGridToText(moment.value.previous.pixelGrid))
console.log()

moment.value.previous.next = moment.value // We only need to populate this when traversing history
moment.value = moment.value.previous
}}
Expand Down

0 comments on commit 39350a7

Please sign in to comment.