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

feat: custom collapse behaviour #357

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Admonition {
injectColor?: boolean;
noTitle: boolean;
copy?: boolean;
collapseType: "open" | "closed" | "none";
}

export interface NestedAdmonition {
Expand Down
3 changes: 3 additions & 0 deletions src/lang/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export default {
"Show Copy Button": "Show Copy Button",
"A copy button will be added to the admonition.":
"A copy button will be added to the admonition.",
"Default Collapsible": "Default Collapsible",
"This will be the default collapsible state for this admonition type.":
"This will be the default collapsible state for this admonition type.",
"Parse Titles as Markdown": "Parse Titles as Markdown",
"Admonition Titles will be rendered as markdown.":
"Admonition Titles will be rendered as markdown."
Expand Down
9 changes: 9 additions & 0 deletions src/modal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
SearchComponent,
Setting,
TextComponent,
DropdownComponent,
ValueComponent,
renderMatches,
setIcon
} from "obsidian";
Expand Down Expand Up @@ -117,6 +119,10 @@ export class InsertAdmonitionModal extends Modal {
this.type.slice(1).toLowerCase();
}
titleInput.setValue(this.title);
if(this.plugin.admonitions[this.type].collapseType){
this.collapse = this.plugin.admonitions[this.type].collapseType;
collapseDropdown.setValue(this.collapse);
}
} else {
new Notice("No admonition type by that name exists.");
t.inputEl.value = "";
Expand Down Expand Up @@ -172,9 +178,12 @@ export class InsertAdmonitionModal extends Modal {
}
});
});

let collapseDropdown: DropdownComponent;

const collapseSetting = new Setting(contentEl);
collapseSetting.setName("Make Collapsible").addDropdown((d) => {
collapseDropdown = d;
d.addOption("open", "Open");
d.addOption("closed", "Closed");
d.addOption("none", "None");
Expand Down
27 changes: 25 additions & 2 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ export default class AdmonitionSetting extends PluginSettingTab {
title: modal.title,
injectColor: modal.injectColor,
noTitle: modal.noTitle,
copy: modal.copy
copy: modal.copy,
collapseType: modal.collapseType
};
this.plugin.addAdmonition(admonition);

Expand Down Expand Up @@ -965,7 +966,8 @@ export default class AdmonitionSetting extends PluginSettingTab {
title: modal.title,
injectColor: modal.injectColor,
noTitle: modal.noTitle,
copy: modal.copy
copy: modal.copy,
collapseType: modal.collapseType
};

if (
Expand Down Expand Up @@ -1037,6 +1039,7 @@ class SettingsModal extends Modal {
admonitionPreviewParent: HTMLElement;
admonitionPreview: HTMLElement;
copy: boolean;
collapseType: "open" | "closed" | "none" = "none";
originalType: string;
editing = false;
constructor(public plugin: ObsidianAdmonition, admonition?: Admonition) {
Expand All @@ -1051,6 +1054,7 @@ class SettingsModal extends Modal {
this.injectColor = admonition.injectColor ?? this.injectColor;
this.noTitle = admonition.noTitle ?? false;
this.copy = admonition.copy ?? this.plugin.data.copyButton;
this.collapseType = admonition.collapseType ?? this.collapseType;
}
}

Expand Down Expand Up @@ -1177,6 +1181,25 @@ class SettingsModal extends Modal {
.addToggle((t) => {
t.setValue(this.copy).onChange((v) => (this.copy = v));
});
new Setting(settingDiv)
.setName(t("Default Collapsible"))
.setDesc(
createFragment((e) => {
e.createSpan({
text: t("This will be the default collapsible state for this admonition type.")
});
})
)
.addDropdown((d) => {
d.addOption("open", "Open");
d.addOption("closed", "Closed");
d.addOption("none", "None");
d.setValue(this.collapseType);
d.onChange((v: "open" | "closed" | "none") => {
this.collapseType = v;
});
});


const input = createEl("input", {
attr: {
Expand Down