TableTower/core/sheet_store.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

111 lines
3.2 KiB
GDScript

class_name SheetStore
extends RefCounted
## Sheets and sheet templates as JSON files: user://sheets/<id>.json and
## user://sheet_templates/<id>.json. `root` can point elsewhere for tests.
const SHEETS := "sheets"
const TEMPLATES := "sheet_templates"
var root := "user://"
func _init(p_root: String = "user://") -> void:
root = p_root
func dir_for(templates: bool) -> String:
return root.path_join(TEMPLATES if templates else SHEETS)
func path_for(id: String, templates: bool) -> String:
return dir_for(templates).path_join(id + ".json")
## Rows for the Sheets list, newest first: {id, name, modified, from_template}.
func list(templates: bool = false) -> Array[Dictionary]:
var out: Array[Dictionary] = []
var dir := dir_for(templates)
if not DirAccess.dir_exists_absolute(dir):
return out
for file in DirAccess.get_files_at(dir):
if not file.ends_with(".json"):
continue
var path := dir.path_join(file)
var d := _read(path)
if d.is_empty():
continue
out.append({"id": file.get_basename(), "name": str(d.get("name", file.get_basename())),
"modified": FileAccess.get_modified_time(path), "from_template": str(d.get("from_template", ""))})
out.sort_custom(func(a: Dictionary, b: Dictionary) -> bool: return int(a["modified"]) > int(b["modified"]))
return out
## The sheet with `id`, or null.
func load_sheet(id: String, templates: bool = false) -> SheetDoc:
if not LibraryStore.is_valid_id(id):
return null
var d := _read(path_for(id, templates))
if d.is_empty():
return null
var doc := SheetDoc.from_dict(d)
doc.id = id
return doc
func save(doc: SheetDoc, templates: bool = false) -> Error:
if not LibraryStore.is_valid_id(doc.id):
return ERR_INVALID_PARAMETER
DirAccess.make_dir_recursive_absolute(dir_for(templates))
return BoardFileIO.write_text_atomic(path_for(doc.id, templates), JSON.stringify(doc.to_dict(), "\t"))
func delete(id: String, templates: bool = false) -> Error:
if not LibraryStore.is_valid_id(id):
return ERR_INVALID_PARAMETER
return DirAccess.remove_absolute(path_for(id, templates))
## A file id from `name` not used yet in that folder ("brin", "brin-2"…).
func free_id(name: String, templates: bool = false) -> String:
var base := LibraryStore.slugify(name)
if base == "":
base = "sheet"
var id := base
var n := 2
while FileAccess.file_exists(path_for(id, templates)):
id = "%s-%d" % [base, n]
n += 1
return id
## A new sheet, blank or copied from a template, saved and returned.
func create(name: String, template_id: String = "") -> SheetDoc:
var doc: SheetDoc = null
if template_id != "":
var tpl := load_sheet(template_id, true)
if tpl:
doc = tpl.copy()
doc.from_template = template_id
if doc == null:
doc = SheetDoc.new()
doc.name = name
doc.id = free_id(name)
save(doc)
return doc
## Saves a copy of `doc` as a template named `name`; returns its id.
func save_as_template(doc: SheetDoc, name: String) -> String:
var tpl := doc.copy()
tpl.name = name
tpl.from_template = ""
tpl.id = free_id(name, true)
save(tpl, true)
return tpl.id
func _read(path: String) -> Dictionary:
if not FileAccess.file_exists(path):
return {}
var parsed: Variant = JSON.parse_string(FileAccess.get_file_as_string(path))
return parsed if parsed is Dictionary else {}