Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load all available compendium packs by default in the Compendium Browser #17690

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 10 additions & 27 deletions src/module/apps/compendium-browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ class CompendiumBrowser extends SvelteApplicationMixin(foundryApp.ApplicationV2)
activeTab: BrowserTab;
dataTabsList = ["action", "bestiary", "campaignFeature", "equipment", "feat", "hazard", "spell"] as const;
packLoader = new PackLoader();
settings: CompendiumBrowserSettings;
declare settings: CompendiumBrowserSettings;
tabs: BrowserTabs;
tabsArray: BrowserTab[];

constructor(options: Partial<ApplicationConfiguration> = {}) {
super(options);

this.settings = game.settings.get("pf2e", "compendiumBrowserPacks");
this.tabs = {
action: new browserTabs.Actions(this),
bestiary: new browserTabs.Bestiary(this),
Expand Down Expand Up @@ -264,7 +263,7 @@ class CompendiumBrowser extends SvelteApplicationMixin(foundryApp.ApplicationV2)
}

initCompendiumList(): void {
const settings: Omit<TabData<Record<string, PackInfo | undefined>>, "settings"> = {
const settings: TabData<Record<string, PackInfo | undefined>> = {
action: {},
bestiary: {},
campaignFeature: {},
Expand All @@ -274,20 +273,6 @@ class CompendiumBrowser extends SvelteApplicationMixin(foundryApp.ApplicationV2)
spell: {},
};

// NPCs and Hazards are all loaded by default, other packs can be set here.
const loadDefault: Record<string, boolean | undefined> = {
bestiary: true,
hazard: true,
"pf2e.actionspf2e": true,
"pf2e.familiar-abilities": true,
"pf2e.equipment-srd": true,
"pf2e.ancestryfeatures": true,
"pf2e.classfeatures": true,
"pf2e.feats-srd": true,
"pf2e.spells-srd": true,
"pf2e.kingmaker-features": true,
};

const browsableTypes = new Set([
"action",
"campaignFeature",
Expand All @@ -299,7 +284,7 @@ class CompendiumBrowser extends SvelteApplicationMixin(foundryApp.ApplicationV2)
...PHYSICAL_ITEM_TYPES,
] as const);
type BrowsableType = SetElement<typeof browsableTypes>;
const typeToTab = new Map<ItemType | "hazard" | "npc", Exclude<TabName, "settings">>([
const typeToTab = new Map<ItemType | "hazard" | "npc", TabName>([
["action", "action"],
["campaignFeature", "campaignFeature"],
["feat", "feat"],
Expand All @@ -310,6 +295,7 @@ class CompendiumBrowser extends SvelteApplicationMixin(foundryApp.ApplicationV2)
...Array.from(PHYSICAL_ITEM_TYPES).map((t): [ItemType, "equipment"] => [t, "equipment"]),
]);

const userSettings = game.settings.get("pf2e", "compendiumBrowserPacks");
for (const pack of game.packs) {
const tabNames = R.unique(
R.unique(pack.index.map((entry) => entry.type))
Expand All @@ -318,12 +304,8 @@ class CompendiumBrowser extends SvelteApplicationMixin(foundryApp.ApplicationV2)
);

for (const tabName of tabNames) {
const load =
this.settings[tabName]?.[pack.collection]?.load ??
loadDefault[tabName] ??
!!loadDefault[pack.collection];
settings[tabName]![pack.collection] = {
load,
settings[tabName][pack.collection] = {
load: userSettings[tabName]?.[pack.collection]?.load !== false,
name: pack.metadata.label,
package: pack.metadata.packageName,
};
Expand All @@ -332,9 +314,10 @@ class CompendiumBrowser extends SvelteApplicationMixin(foundryApp.ApplicationV2)

for (const tab of this.dataTabsList) {
settings[tab] = Object.fromEntries(
Object.entries(settings[tab]!).sort(([_collectionA, dataA], [_collectionB, dataB]) => {
return (dataA?.name ?? "") > (dataB?.name ?? "") ? 1 : -1;
}),
Object.entries(settings[tab]).sort(
([_collectionA, dataA], [_collectionB, dataB]) =>
dataA?.name.localeCompare(dataB?.name ?? "", game.i18n.lang) ?? 1,
),
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/module/apps/compendium-browser/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface PackInfo {
load: boolean;
name: string;
package: string;
showFullId?: boolean;
}

interface SourceInfo {
Expand All @@ -24,7 +25,7 @@ interface BrowserTabs {
type TabName = "action" | "bestiary" | "campaignFeature" | "equipment" | "feat" | "hazard" | "spell";
type ContentTabName = Exclude<TabName, "settings">;
type BrowserTab = InstanceType<(typeof browserTabs)[keyof typeof browserTabs]>;
type TabData<T> = Record<TabName, T | null>;
type TabData<T> = Record<TabName, T>;

type CommonSortByOption = "name" | "level";
type SortByOption = CommonSortByOption | "price";
Expand Down
38 changes: 26 additions & 12 deletions src/module/apps/compendium-browser/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,25 @@ class CompendiumBrowserSettingsApp extends foundryApp.HandlebarsApplicationMixin
const browser = game.pf2e.compendiumBrowser;
for (const [name, settings] of Object.entries(browser.settings)) {
if (objectHasKey(this.#tabSettings, name)) {
this.#tabSettings[name]!.settings = settings;
const duplicates = new Set<string>();
const seen = new Set<string>();
// Find multiple entries for the same module
for (const setting of Object.values(settings)) {
if (!setting || setting.package === "pf2e") continue;
if (seen.has(setting.package)) {
duplicates.add(setting.package);
continue;
}
seen.add(setting.package);
}
// Show the full pack id if a module has multiple packs in the same category
for (const setting of Object.values(settings)) {
if (!setting || setting.package === "pf2e") continue;
if (duplicates.has(setting.package)) {
setting.showFullId = true;
}
}
this.#tabSettings[name].settings = settings;
} else {
console.warn(`Unknown Compendium Browser setting "${name}"!`);
}
Expand All @@ -160,17 +178,13 @@ class CompendiumBrowserSettingsApp extends foundryApp.HandlebarsApplicationMixin
): Promise<void> {
const browser = game.pf2e.compendiumBrowser;
const settings = browser.settings;
const getFormValue = (key: string): boolean => {
const value = formData.get(key);
if (value === "true") {
return true;
}
return false;
const getCheckboxValue = (key: string): boolean => {
return formData.get(key) === "true";
};

for (const [t, packs] of Object.entries(settings) as [string, { [key: string]: PackInfo }][]) {
for (const [key, pack] of Object.entries(packs) as [string, PackInfo][]) {
pack.load = getFormValue(`${t}-${key}`);
pack.load = getCheckboxValue(`${t}-${key}`);
}
}
await game.settings.set("pf2e", "compendiumBrowserPacks", settings);
Expand All @@ -180,12 +194,12 @@ class CompendiumBrowserSettingsApp extends foundryApp.HandlebarsApplicationMixin
delete browser.packLoader.sourcesSettings.sources[key]; // just to make sure we clean up
continue;
}
source.load = getFormValue(`source-${key}`);
source.load = getCheckboxValue(`source-${key}`);
}

browser.packLoader.sourcesSettings.showEmptySources = getFormValue("show-empty-sources");
browser.packLoader.sourcesSettings.showUnknownSources = getFormValue("show-unknown-sources");
browser.packLoader.sourcesSettings.ignoreAsGM = getFormValue("ignore-as-gm");
browser.packLoader.sourcesSettings.showEmptySources = getCheckboxValue("show-empty-sources");
browser.packLoader.sourcesSettings.showUnknownSources = getCheckboxValue("show-unknown-sources");
browser.packLoader.sourcesSettings.ignoreAsGM = getCheckboxValue("ignore-as-gm");
await game.settings.set("pf2e", "compendiumBrowserSources", browser.packLoader.sourcesSettings);

await browser.resetInitializedTabs();
Expand Down
6 changes: 3 additions & 3 deletions static/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3371,10 +3371,10 @@
"DeleteAllQuestion": "Are you sure you want to reset all settings for included sources?",
"DeleteAllTitle": "Reset Settings",
"Hint": "Settings to display only entries with specified sources in the compendium browser.",
"IgnoreAsGM": "Let GMs ignore filtering and see all entries.",
"IgnoreAsGM": "Let GMs ignore these settings and see all entries.",
"Name": "Included Sources",
"ShowEmptySources": "Do not filter entries with empty sources.",
"ShowUnknownSources": "Do not filter entries with unknown sources.",
"ShowEmptySources": "Allow entries with empty sources.",
"ShowUnknownSources": "Allow entries with unknown sources.",
"SourcesListHeader": "Specified Sources"
},
"Core": {
Expand Down
10 changes: 9 additions & 1 deletion static/templates/compendium-browser/settings/pack-settings.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
<fieldset>
<legend>{{localize setting.label}}</legend>
{{#each setting.settings as |conf pack|}}
<label for="{{key}}-{{pack}}"><input type="checkbox" id="{{key}}-{{pack}}" name="{{key}}-{{pack}}" {{checked conf.load}}>{{conf.name}} ({{conf.package}})</label>
<label for="{{key}}-{{pack}}">
<input
type="checkbox"
id="{{key}}-{{pack}}"
name="{{key}}-{{pack}}"
{{checked conf.load}}
>
{{conf.name}} ({{#if conf.showFullId}}{{pack}}{{else}}{{conf.package}}{{/if}})
</label>
{{/each}}
</fieldset>
</div>
Expand Down
Loading