class_name SpreadFolders extends RefCounted ## Folders for spreads in the spread switcher. A folder is a path such as ## "Traveller/Worlds" ("/" puts a folder inside another). Each spread saves its ## own path (TowerBoard.folder), so a folder is listed while it holds spreads. ## A folder with no spreads is listed only if the user chose to keep it; those ## paths are a StateStore setting. Rules only, no UI. const KEPT := "kept_spread_folders" ## Trims each part and drops empty parts: " a / /b " -> "a/b". static func normalize(path: String) -> String: var parts := PackedStringArray() for part in path.split("/"): if part.strip_edges() != "": parts.append(part.strip_edges()) return "/".join(parts) static func parent_of(path: String) -> String: var i := path.rfind("/") return "" if i < 0 else path.substr(0, i) static func leaf(path: String) -> String: return path.substr(path.rfind("/") + 1) static func depth(path: String) -> int: return path.count("/") ## True when `path` is `folder` or a folder inside it. static func is_inside(path: String, folder: String) -> bool: return path == folder or path.begins_with(folder + "/") # ---------- Listing ---------- ## Every folder to show: the spreads' folders and the kept ones, with their ## parents, sorted A–Z part by part (a folder comes just before its inner ones). static func all_folders(entries: Array, kept_list: Array) -> Array[String]: var seen := {} var paths: Array = kept_list.duplicate() for entry: Dictionary in entries: paths.append(str(entry.get("folder", ""))) for p in paths: var path := normalize(str(p)) while path != "": seen[path] = true path = parent_of(path) var out: Array[String] = [] out.assign(seen.keys()) out.sort_custom(_before) return out ## The folders directly inside `parent` ("" = the top level). static func children(folders: Array[String], parent: String) -> Array[String]: var out: Array[String] = [] for f in folders: if parent_of(f) == parent: out.append(f) return out ## The spreads directly in `folder` (not in its inner folders). static func spreads_in(entries: Array, folder: String) -> Array[Dictionary]: var out: Array[Dictionary] = [] for entry: Dictionary in entries: if str(entry.get("folder", "")) == folder: out.append(entry) return out ## True when no spread is in `folder` or inside it, and no kept folder is inside it. static func is_empty(folder: String, entries: Array, kept_list: Array) -> bool: for entry: Dictionary in entries: if is_inside(str(entry.get("folder", "")), folder): return false for k in kept_list: if str(k) != folder and is_inside(str(k), folder): return false return true ## The typed path, with each part spelled like an existing folder that matches ## it without regard to case: "traveller/worlds" -> "Traveller/Worlds". static func resolve(typed: String, folders: Array[String]) -> String: var path := "" for part in normalize(typed).split("/", false): var want := part if path == "" else path + "/" + part for f in folders: if f.to_lower() == want.to_lower(): want = f break path = want return path static func _before(a: String, b: String) -> bool: var pa := a.split("/") var pb := b.split("/") for i in mini(pa.size(), pb.size()): var c := pa[i].naturalnocasecmp_to(pb[i]) if c != 0: return c < 0 return pa.size() < pb.size() # ---------- Kept (empty) folders ---------- static func kept(state: StateStore) -> Array: var v: Variant = state.get_setting(KEPT, []) if state else [] return (v as Array).duplicate() if v is Array else [] static func keep(state: StateStore, folder: String) -> void: var list := kept(state) if folder != "" and not list.has(folder): list.append(folder) state.set_setting(KEPT, list) state.save() ## Stops keeping `folder`, so it's gone once it has no spreads. static func forget(state: StateStore, folder: String) -> void: var list := kept(state) if list.has(folder): list.erase(folder) state.set_setting(KEPT, list) state.save() # ---------- Changes (saved at once) ---------- ## Puts `current` in `folder` ("" = none) and saves it. Returns the folder it ## left if that folder is empty now (so the UI can ask to keep it), else "". static func move(library: LibraryStore, current: TowerBoard, folder: String) -> String: var old := current.folder current.folder = normalize(folder) library.save(current) if old == "" or is_inside(current.folder, old): return "" return old if is_empty(old, library.list(), kept(library.state)) else "" ## Renames the last part of `folder`. The spreads and kept folders inside it ## move with it (if the new name is taken, they join that folder). `current` is ## changed in memory too. Returns the new path, or "" for a blank name. static func rename(library: LibraryStore, current: TowerBoard, folder: String, new_name: String) -> String: var part := new_name.replace("/", " ").strip_edges() if part == "" or folder == "": return "" var others: Array[String] = [] for f in all_folders(library.list(), kept(library.state)): if not is_inside(f, folder): others.append(f) var parent := parent_of(folder) var to := resolve(part if parent == "" else parent + "/" + part, others) if to == folder: return to for entry in library.list(): var path := str(entry.get("folder", "")) if not is_inside(path, folder): continue var b := current if str(entry["id"]) == current.id else library.load_collection(str(entry["id"])) if b: b.folder = to + path.substr(folder.length()) library.save(b) var list: Array = [] for k in kept(library.state): var path := str(k) path = to + path.substr(folder.length()) if is_inside(path, folder) else path if not list.has(path): list.append(path) library.state.set_setting(KEPT, list) library.state.save() return to