Skip to content

Commit

Permalink
feat: adds admonition exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Jan 27, 2023
1 parent 4ccd105 commit 0897e08
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 2 deletions.
66 changes: 66 additions & 0 deletions src/modal/export.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Modal, Setting } from "obsidian";
import ObsidianAdmonition from "src/main";

export default class Export extends Modal {
constructor(public plugin: ObsidianAdmonition) {
super(app);
}
admonitionDefinitions = Object.values(this.plugin.data.userAdmonitions);

admonitionNames = Object.keys(this.plugin.data.userAdmonitions);

selectedAdmonitions = [...this.admonitionNames];

export = false;

onOpen() {
this.titleEl.setText("Export Admonitions");
this.containerEl.addClasses([
"admonition-settings",
"admonition-modal",
"admonition-export-modal"
]);
new Setting(this.contentEl).addButton((b) =>
b.setButtonText("Export Selected").onClick(() => {
this.export = true;
this.close();
})
);
let toggleEl: HTMLDivElement;
new Setting(this.contentEl)
.addButton((b) =>
b
.setButtonText("Select All")
.setCta()
.onClick(() => {
this.selectedAdmonitions = [...this.admonitionNames];
this.generateToggles(toggleEl);
})
)
.addButton((b) =>
b.setButtonText("Deselect All").onClick(() => {
this.selectedAdmonitions = [];
this.generateToggles(toggleEl);
})
);
toggleEl = this.contentEl.createDiv("additional");
this.generateToggles(toggleEl);
}

generateToggles(toggleEl: HTMLDivElement) {
toggleEl.empty();
for (const name of this.admonitionNames) {
new Setting(toggleEl).setName(name).addToggle((t) => {
t.setValue(this.selectedAdmonitions.includes(name)).onChange(
(v) => {
if (v) {
this.selectedAdmonitions.push(name);
} else {
this.selectedAdmonitions.remove(name);
}
}
);
});
}
}
}
54 changes: 52 additions & 2 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import ObsidianAdmonition from "./main";
import { confirmWithModal } from "./modal/confirm";
import { DownloadableIconPack, DownloadableIcons } from "./icons/packs";
import { AdmonitionValidator } from "./util/validator";
import Export from "./modal/export";

/** Taken from https://stackoverflow.com/questions/34849001/check-if-css-selector-is-valid/42149818 */
const isSelectorValid = ((dummyElement) => (selector: string) => {
Expand Down Expand Up @@ -93,6 +94,40 @@ export default class AdmonitionSetting extends PluginSettingTab {
);
}

new Setting(admonitionEl)
.setName("Export Custom Types as JSON")
.setDesc(
"Choose custom types to export as a JSON file that you can then share with other users."
)
.addButton((b) =>
b
.setButtonText("Download All")
.setCta()
.onClick(() => {
const admonitions = Object.values(
this.plugin.data.userAdmonitions
);
this.download(admonitions);
})
)
.addButton((b) =>
b.setButtonText("Select & Download").onClick(() => {
const modal = new Export(this.plugin);
modal.onClose = () => {
if (!modal.export) return;
const admonitions = Object.values(
this.plugin.data.userAdmonitions
);
this.download(
admonitions.filter((a) =>
modal.selectedAdmonitions.includes(a.type)
)
);
};
modal.open();
})
);

new Setting(admonitionEl)
.setName("Use CSS Snippet for Custom Callouts")
.setDesc(
Expand Down Expand Up @@ -307,7 +342,21 @@ export default class AdmonitionSetting extends PluginSettingTab {
}
});
}

download(admonitions: Admonition[]) {
if (!admonitions.length) {
new Notice("At least one admonition must be chosen to export.");
return;
}
const link = createEl("a");
const file = new Blob([JSON.stringify(admonitions)], {
type: "json"
});
const url = URL.createObjectURL(file);
link.href = url;
link.download = `admonitions.json`;
link.click();
URL.revokeObjectURL(url);
}
buildAdmonitions(containerEl: HTMLDetailsElement) {
containerEl.empty();
containerEl.ontoggle = () => {
Expand Down Expand Up @@ -462,7 +511,8 @@ export default class AdmonitionSetting extends PluginSettingTab {

let selected: DownloadableIconPack;
const possibilities = Object.entries(DownloadableIcons).filter(
([icon]) => !this.plugin.data.icons.includes(icon)
([icon]) =>
!this.plugin.data.icons.includes(icon as DownloadableIconPack)
);
new Setting(containerEl)
.setName("Load Additional Icons")
Expand Down

0 comments on commit 0897e08

Please sign in to comment.