Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
- Added filter input to homebrew monsters
  • Loading branch information
valentine195 committed Jun 3, 2021
1 parent da6c7c9 commit 9c34f16
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 12 deletions.
2 changes: 1 addition & 1 deletion @types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export declare abstract class StatblockMonsterPlugin extends Plugin {
el: HTMLElement,
ctx: MarkdownPostProcessorContext
): Promise<void>;
get sorted(): string[];
get sorted(): Monster[];
abstract saveMonster(monster: Monster): Promise<void>;
abstract saveMonsters(monsters: Monster[]): Promise<void>;
abstract deleteMonster(monster: string): Promise<void>;
Expand Down
18 changes: 13 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ export default class StatBlockPlugin
{
data: Map<string, Monster>;
bestiary: Map<string, Monster>;
private _sorted: string[] = [];
private _sorted: Monster[] = [];
get sorted() {
if (!this._sorted.length)
this._sorted = sort<string>(Array.from(this.data.keys())).asc();
this._sorted = sort<Monster>(Array.from(this.data.values())).asc(
(m) => m.name
);
return this._sorted;
}
async onload() {
Expand Down Expand Up @@ -65,22 +67,28 @@ export default class StatBlockPlugin
this.bestiary.set(monster.name, monster);
await this.saveData(this._transformData(this.data));
if (sortFields)
this._sorted = sort<string>(Array.from(this.data.keys())).asc();
this._sorted = sort<Monster>(
Array.from(this.data.values())
).asc((m) => m.name);
}
}
async saveMonsters(monsters: Monster[]) {
for (let monster of monsters) {
await this.saveMonster(monster, false);
}
this._sorted = sort<string>(Array.from(this.data.keys())).asc();
this._sorted = sort<Monster>(Array.from(this.data.values())).asc(
(m) => m.name
);
}

async deleteMonster(monster: string) {
if (!this.data.has(monster)) return;
this.data.delete(monster);
this.bestiary.delete(monster);
await this.saveData(this._transformData(this.data));
this._sorted = sort<string>(Array.from(this.data.keys())).asc();
this._sorted = sort<Monster>(Array.from(this.data.values())).asc(
(m) => m.name
);
}

private _transformData(data: Map<string, Monster>): any[] {
Expand Down
53 changes: 47 additions & 6 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { StatblockMonsterPlugin } from "@types";
import { App, Notice, PluginSettingTab, Setting } from "obsidian";
import {
App,
Notice,
PluginSettingTab,
Setting,
TextComponent
} from "obsidian";
import { DnDAppFilesImporter } from "src/importers/DnDAppFilesImporter";
import { MonsterSuggester } from "src/util/suggester";

export default class StatblockSettingTab extends PluginSettingTab {
constructor(app: App, private plugin: StatblockMonsterPlugin) {
Expand Down Expand Up @@ -65,9 +72,13 @@ export default class StatblockSettingTab extends PluginSettingTab {
const additionalContainer = containerEl.createDiv(
"statblock-additional-container"
);
new Setting(additionalContainer)
let monsterFilter: TextComponent;
const homebrewMonsters = new Setting(additionalContainer)
.setName("Homebrew Monsters")
.setDesc("Manage saved homebrew monsters.");
.addText((t) => {
t.setPlaceholder("Filter Monsters");
monsterFilter = t;
});
const additional = additionalContainer.createDiv("additional");
if (!this.plugin.data.size) {
additional
Expand All @@ -81,8 +92,38 @@ export default class StatblockSettingTab extends PluginSettingTab {
});
return;
}
//TODO: MEMOIZE THE SORT
for (let monster of this.plugin.sorted) {

let suggester = new MonsterSuggester(
this.plugin,
monsterFilter,
additional,
this.plugin.sorted
);

homebrewMonsters.setDesc(
`Manage homebrew monsters. Currently: ${
suggester.getItems().length
} monsters.`
);

suggester.onRemoveItem = async (monster) => {
try {
await this.plugin.deleteMonster(monster.name);
} catch (e) {
new Notice(
`There was an error deleting the monster:${
`\n\n` + e.message
}`
);
}
this.display();
};
suggester.onInputChanged = () =>
homebrewMonsters.setDesc(
`Manage homebrew monsters. Currently: ${suggester.filteredItems.length} monsters.`
);

/* for (let monster of this.plugin.sorted) {
let setting = new Setting(additional)
.setName(monster)
.setDesc(this.plugin.data.get(monster).source ?? "");
Expand All @@ -103,7 +144,7 @@ export default class StatblockSettingTab extends PluginSettingTab {
this.display();
});
});
}
} */
} catch (e) {
new Notice(
"There was an error displaying the settings tab for 5e Statblocks."
Expand Down
Loading

0 comments on commit 9c34f16

Please sign in to comment.