TableTower/core/snapshot_history.gd
RobinFiredrake 2661744b06 Sheets phase B: main menu, Sheets list, sheet editor
- Main menu (Spreads, Sheets, Settings, Quit) on the theme background,
  with recent spreads and sheets; logo or ☰ returns to it.
- Sheets: data model, store, templates, snapshot undo; New sheet dialog
  (blank or a template); editor with folder tabs, snap-grid canvas on the
  spread background, field palette, inspector, Play/Build, status strip.
- Field kinds: Number, Text box (BBCode, live {@name}), Heading.
- PaletteList shared by the card and field palettes; shared top bar brand.
- Keys go to the screen that shows; theme reloads keep the screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 15:53:56 -06:00

57 lines
1.5 KiB
GDScript

class_name SnapshotHistory
extends RefCounted
## Undo and redo by whole snapshots (Dictionaries), for small documents such
## as a sheet. The caller records the state before each edit (record) and
## gets back the state to show on undo or redo. Spreads use EditHistory, which
## keeps per-card strings because spreads can be large.
const MAX_STEPS := 200
var _undo: Array = [] # [{"state", "label"}]
var _redo: Array = []
func can_undo() -> bool:
return not _undo.is_empty()
func can_redo() -> bool:
return not _redo.is_empty()
func undo_label() -> String:
return str(_undo.back()["label"]) if can_undo() else ""
func redo_label() -> String:
return str(_redo.back()["label"]) if can_redo() else ""
func clear() -> void:
_undo.clear()
_redo.clear()
## Records `before`, the state just before an edit called `label`. Clears redo.
func record(before: Dictionary, label: String) -> void:
_undo.append({"state": before.duplicate(true), "label": label})
if _undo.size() > MAX_STEPS:
_undo.pop_front()
_redo.clear()
## Steps back: give the current state, get the one to show ({} if none).
func undo(current: Dictionary) -> Dictionary:
if not can_undo():
return {}
var step: Dictionary = _undo.pop_back()
_redo.append({"state": current.duplicate(true), "label": step["label"]})
return step["state"]
func redo(current: Dictionary) -> Dictionary:
if not can_redo():
return {}
var step: Dictionary = _redo.pop_back()
_undo.append({"state": current.duplicate(true), "label": step["label"]})
return step["state"]