class_name SheetLoreLinks extends RefCounted ## Links to Lore Entries (BookPage) in sheet text. The link is plain BBCode: ## [url=lore:ENTRY_ID]shown words[/url]. RichText.decorate draws it in the ## reference colours; hovering shows the entry's name and the start of its ## text (DetailsPopup "lore:"); a click in a sheet opens it in the side panel ## (SheetDetailPanel.open_lore). ## ## Which Lore Index: two levels. The open spread's (Main.data.book) and the ## shared "My library" (LoreLibrary, kept by SheetsScreen). A link to a ## library entry says so: [url=lore:lib:ID]; a plain lore:ID is the spread's. ## entries() and find() are the one place that decides. const META := "lore:" ## The level mark in a link ref for a "My library" entry. const LIBRARY := "lib:" ## Longest hover excerpt, in characters (as for list entries). const EXCERPT := 140 ## The shared "My library" level, or null before the screens are built. static func library(m: Main) -> LoreLibrary: if m == null or m.screens == null or m.screens.sheets == null: return null return m.screens.sheets.library ## Every Lore Entry a sheet can link to: the spread's, then My library's. static func entries(m: Main) -> Array[BookPage]: var out: Array[BookPage] = [] if m != null and m.data != null: out.assign(m.data.book) var shared := library(m) if shared: out.append_array(shared.pages) return out ## What a link to `page` holds after "lore:": its id, or "lib:" + id for a ## My library entry. static func ref(m: Main, page: BookPage) -> String: var shared := library(m) return LIBRARY + page.id if shared and shared.pages.has(page) else page.id ## The Lore Entry a link ref points at, or null when it is gone. static func find(m: Main, link_ref: String) -> BookPage: if link_ref.begins_with(LIBRARY): var shared := library(m) return shared.get_page(link_ref.substr(LIBRARY.length())) if shared else null return m.data.get_page(link_ref) if m != null and m.data != null else null ## The entries whose name, text or tags match every word of `query`. static func search(m: Main, query: String) -> Array[BookPage]: var out: Array[BookPage] = [] var words := query.strip_edges().to_lower().split(" ", false) for page in entries(m): var hay := " ".join([page.label(), Formatter.strip_tags(page.text), " ".join(page.tags)]).to_lower() if Array(words).all(func(word: String) -> bool: return hay.contains(word)): out.append(page) return out ## The opening tag of a link to `page`. static func open_tag(m: Main, page: BookPage) -> String: return tag_for(ref(m, page)) ## The opening tag of a link with ref `link_ref` ("ID" or "lib:ID"). static func tag_for(link_ref: String) -> String: return "[url=%s%s]" % [META, link_ref] ## The words a link shows when nothing was selected: the entry's name. static func shown_name(page: BookPage) -> String: return RichText.plain(Formatter.strip_tags(page.label()).strip_edges()) ## The entry's text and details without formatting, cut with "…". static func excerpt(page: BookPage) -> String: return RulesView.cut(SheetItems.plain_text(page).replace("\n", " "), EXCERPT) ## Fills the hover pop-up for the link with ref `link_ref`. static func show_hover(m: Main, owner: Control, link_ref: String) -> void: var page := find(m, link_ref) if page == null: m.details().show_for(owner, "A missing Lore Entry", "This Lore Entry was deleted, or it is in another spread's Lore Index.") return var body := RichText.plain(excerpt(page)) m.details().show_for(owner, page.label(), body if body != "" else "[i]No text yet[/i]", "Lore Entry · click to read it all")