Skip to content

Commit

Permalink
fix: enable/disable all snippets from modals
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Feb 27, 2023
1 parent af69aee commit 5bb288a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
26 changes: 24 additions & 2 deletions plugin/modals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {App, ButtonComponent, FuzzySuggestModal, Modal, Notice, Setting} from "obsidian";
import {App, ButtonComponent, FuzzySuggestModal, Modal, Notice, Setting, ExtraButtonComponent} from "obsidian";
import {GroupSnippet, GroupSnippetsSettings, Snippets} from "./interface";
import GroupSnippetsPlugins from "./main";
import i18next from "i18next";
Expand All @@ -19,6 +19,9 @@ export class GroupSnippetsEdit extends Modal {
const {contentEl} = this;
contentEl.empty();
contentEl.createEl("h2", {text: i18next.t("modals.edit.title", {snippet: this.result.name}) as string});
const icon = this.result.active ? "check-in-circle" : "cross-in-box";
const desc = this.result.active ? i18next.t("settings.everything.enable") as string : i18next.t("settings.everything.disable") as string;

new Setting(contentEl)
.setClass("group-snippets-modal-title")
.addButton((button: ButtonComponent) => {
Expand All @@ -31,7 +34,25 @@ export class GroupSnippetsEdit extends Modal {
this.onOpen();
}).open();
});
});
})
.addButton((btn: ButtonComponent) => {
btn
.setButtonText(desc)
.onClick(async () => {
if (this.result.active) {
this.result.active = false;
this.result.snippets.forEach(snippet => {
snippet.enabled = true;
});
} else {
this.result.active = true;
this.result.snippets.forEach(snippet => {
snippet.enabled = false;
});
}
this.onOpen();
});
})
for (const snippet of this.result.snippets) {
new Setting(contentEl)
.setClass("group-snippets-modal-snippet")
Expand Down Expand Up @@ -108,6 +129,7 @@ export class GroupSnippetsModal extends FuzzySuggestModal<Snippets> {

getItemText(item: Snippets): string {
return item.snippetName;

}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
24 changes: 2 additions & 22 deletions plugin/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ export class GroupSnippetsSettingsTabs extends PluginSettingTab {
});

for (const snippets of this.plugin.settings.groups) {
const icon = snippets.active ? "check-in-circle" : "cross-in-box";
const desc = snippets.active ? i18next.t("settings.everything.enable") as string : i18next.t("settings.everything.disable") as string;

const groupName = snippets.name;
new Setting(containerEl)
.setClass("group-options")
Expand Down Expand Up @@ -107,26 +106,7 @@ export class GroupSnippetsSettingsTabs extends PluginSettingTab {
await this.plugin.saveSettings();
});
})
.addExtraButton((btn: ExtraButtonComponent) => {
btn
.setIcon(icon)
.setTooltip(desc)
.onClick(async () => {
if (snippets.active) {
snippets.active = false;
snippets.snippets.forEach(snippet => {
snippet.enabled = true;
});
} else {
snippets.active = true;
snippets.snippets.forEach(snippet => {
snippet.enabled = false;
});
}
await this.plugin.saveSettings();
this.display();
});
})

.addExtraButton((btn: ExtraButtonComponent) => {
btn
.setTooltip(i18next.t("commands.name", {name: groupName}) as string)
Expand Down

0 comments on commit 5bb288a

Please sign in to comment.