TableTower/core/sheet_file.gd
RobinFiredrake 317cac7a78 Sheets phase E: export/import, text toolbar and Lore links, Lore library and items, phone view
- E1: Export ▾ (share file with embedded pictures, print/PDF web page,
  PNG of the tab); Import a sheet file from Sheets and New sheet.
- E2: formatting toolbar (FormatMenu) in sheet text boxes; Lore links
  ([url=lore:ID]) with hover excerpt and the details panel.
- E3: Lore Index in Play and in sheets (shared LoreList); This spread |
  My library; item sheets on Lore Entries; drag entries into lists,
  slots and notes (copy, Shift keeps linked).
- E4: phone stacked view in phone order, Stacked | Canvas switch, 44 px
  touch sizes, bottom panels; sheet CLI flags split out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 17:25:09 -06:00

71 lines
2.7 KiB
GDScript

class_name SheetFile
extends RefCounted
## A share file: one sheet as JSON that someone else can import into their
## app. Pictures in picture boxes travel inside it (base64 under "pictures",
## keyed by file name), so the receiver sees them too.
## Form: {"tabletarot_sheet": 1, "sheet": SheetDoc.to_dict(), "pictures": {name: base64}}.
const FORMAT_KEY := "tabletarot_sheet"
const FORMAT_VERSION := 1
const EXTENSION := "sheet.json"
const FILTER := "*.sheet.json, *.json ; Sheet file"
## The share file's text for `doc`.
static func share_text(doc: SheetDoc) -> String:
var pictures := {}
for tab in doc.tabs:
for box: SheetBox in tab["boxes"]:
var path := _picture_path(box)
if path == "" or pictures.has(path.get_file()):
continue
var bytes := FileAccess.get_file_as_bytes(path)
if not bytes.is_empty():
pictures[path.get_file()] = Marshalls.raw_to_base64(bytes)
var sheet := doc.to_dict()
sheet.erase("from_template") # the receiver doesn't have our templates
return JSON.stringify({FORMAT_KEY: FORMAT_VERSION, "sheet": sheet, "pictures": pictures}, "\t")
## Reads share-file `text` into `store` as a new sheet (or a template) with a
## fresh id, saved, with its pictures written back. Null if it isn't one.
static func import_text(store: SheetStore, text: String, templates: bool = false) -> SheetDoc:
var parsed: Variant = JSON.parse_string(text)
if not parsed is Dictionary or not (parsed as Dictionary).get("sheet") is Dictionary:
return null
var data: Dictionary = parsed
var doc := SheetDoc.from_dict(data["sheet"])
doc.from_template = ""
var pictures: Variant = data.get("pictures", {})
var kept := {} # file name in the share file -> path in this store
if pictures is Dictionary:
for file_name: String in pictures:
var bytes := Marshalls.base64_to_raw(str(pictures[file_name]))
kept[file_name] = store.keep_picture_bytes(bytes, file_name.get_extension())
for tab in doc.tabs:
for box: SheetBox in tab["boxes"]:
var path := _picture_path(box)
if path != "":
box.value = str(kept.get(path.get_file(), ""))
doc.id = store.free_id(doc.name, templates)
if store.save(doc, templates) != OK:
return null
return doc
static func import_path(store: SheetStore, path: String, templates: bool = false) -> SheetDoc:
if not FileAccess.file_exists(path):
return null
return import_text(store, FileAccess.get_file_as_string(path), templates)
## A file name for sharing `doc` ("brin-hollowmere.sheet.json").
static func file_name(doc: SheetDoc) -> String:
var slug := LibraryStore.slugify(doc.name)
return "%s.%s" % [slug if slug != "" else "sheet", EXTENSION]
static func _picture_path(box: SheetBox) -> String:
if box.kind != SheetKinds.PICTURE or not box.value is String:
return ""
return str(box.value)