Skip to content

Commit

Permalink
feat: added "Delete All Filtered" button to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Dec 21, 2021
1 parent 3d95c86 commit 0dcff35
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
20 changes: 15 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import {
addIcon,
MarkdownPostProcessorContext,
Notice,
Plugin,
Plugin
} from "obsidian";
import domtoimage from "dom-to-image";


import { BESTIARY_BY_NAME } from "./data/srd-bestiary";
import StatBlockRenderer from "./view/statblock";
import { getParamsFromSource, renderError } from "./util/util";
Expand All @@ -25,9 +24,7 @@ import StatblockSettingTab from "./settings/settings";
import "./main.css";
import { sort } from "fast-sort";
import type { Plugins } from "../../obsidian-overload";
import type {
HomebrewCreature,
} from "../../obsidian-initiative-tracker/@types";
import type { HomebrewCreature } from "../../obsidian-initiative-tracker/@types";
declare module "obsidian" {
interface App {
plugins: {
Expand Down Expand Up @@ -218,6 +215,19 @@ export default class StatBlockPlugin extends Plugin {
await this.saveMonster(newMonster);
}

async deleteMonsters(...monsters: string[]) {
for (const monster of monsters) {
if (!this.data.has(monster)) continue;
this.data.delete(monster);
this.bestiary.delete(monster);
}
await this.saveSettings();

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);
Expand Down
8 changes: 8 additions & 0 deletions src/settings/settings.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
.statblock-filter-container .search-input-container {
margin-bottom: 0;
}

.no-border-top {
border-top: 0;
}

.statblock-edit-block
.statblock-additional-container
> .additional
Expand Down
55 changes: 54 additions & 1 deletion src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default class StatblockSettingTab extends PluginSettingTab {
}
}
generateMonsters(containerEl: HTMLDivElement) {
containerEl.empty();
const additionalContainer = containerEl.createDiv(
"statblock-additional-container statblock-monsters"
);
Expand All @@ -62,10 +63,33 @@ export default class StatblockSettingTab extends PluginSettingTab {
"statblock-monster-filter"
);
const searchMonsters = new Setting(filters)
.setClass("statblock-filter-container")
.setName("Homebrew Monsters")
.addSearch((t) => {
t.setPlaceholder("Filter Monsters");
t.setPlaceholder("Search Monsters");
monsterFilter = t;
})
.addButton((b) => {
b.setIcon("trash")
.setCta()
.setTooltip("Delete Filtered Monsters")
.onClick(() => {
const modal = new ConfirmModal(
suggester.filteredItems.length,
this.plugin.app
);
modal.onClose = async () => {
if (modal.saved) {
await this.plugin.deleteMonsters(
...(suggester.filteredItems?.map(
(m) => m.item.name
) ?? [])
);
this.generateMonsters(containerEl);
}
};
modal.open();
});
});

const additional = additionalContainer.createDiv("additional");
Expand Down Expand Up @@ -583,3 +607,32 @@ class CreateStatblockModal extends Modal {
});
}
}

class ConfirmModal extends Modal {
saved: boolean = false;
constructor(public filtered: number, app: App) {
super(app);
}
onOpen() {
this.titleEl.setText("Are you sure?");
this.contentEl.createEl("p", {
text: `This will delete ${this.filtered} creatures. This cannot be undone.`
});
new Setting(this.contentEl)
.setClass("no-border-top")
.addButton((b) => {
b.setIcon("checkmark")
.setCta()
.onClick(() => {
this.saved = true;
this.close();
});
})
.addExtraButton((b) =>
b.setIcon("cross").onClick(() => {
this.saved = true;
this.close();
})
);
}
}

0 comments on commit 0dcff35

Please sign in to comment.