Skip to content

Commit

Permalink
fix: Admonitions updated in settings are no longer moved to the botto…
Browse files Browse the repository at this point in the history
…m of the list
  • Loading branch information
valentine195 committed Mar 15, 2022
1 parent e17b75a commit 54ea584
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 48 deletions.
85 changes: 43 additions & 42 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ declare module "obsidian" {
executeCommandById(id: string): void;
findCommand(id: string): Command;
};
customCss: {
getSnippetPath(file: string): string;
readCssFolders(): void;
setCssEnabledStatus(snippet: string, enabled: boolean): void;
};
plugins: {
getPluginFolder(): string;
};
Expand Down Expand Up @@ -95,11 +100,13 @@ const DEFAULT_APP_SETTINGS: AdmonitionSettings = {
useFontAwesome: true,
rpgDownloadedOnce: false,
msDocConverted: false,
useSnippet: false
useSnippet: false,
snippetPath: `custom-admonitions.${[...Array(6).keys()]
.map(() => ((16 * Math.random()) | 0).toString(16))
.join("")}`
};

export default class ObsidianAdmonition extends Plugin {
admonitions: { [admonitionType: string]: Admonition } = {};
data: AdmonitionSettings;

postprocessors: Map<string, MarkdownPostProcessor> = new Map();
Expand Down Expand Up @@ -129,16 +136,9 @@ export default class ObsidianAdmonition extends Plugin {

this.registerEditorSuggest(new AdmonitionSuggest(this));

Object.keys(this.admonitions).forEach((type) => {
const processor = this.registerMarkdownCodeBlockProcessor(
`ad-${type}`,
(src, el, ctx) => this.postprocessor(type, src, el, ctx)
);
this.postprocessors.set(type, processor);
if (this.admonitions[type].command) {
this.registerCommandsFor(this.admonitions[type]);
}
});
Object.values(this.admonitions).forEach((admonition) =>
this.registerType(admonition)
);

this.addSettingTab(new AdmonitionSetting(this.app, this));

Expand All @@ -148,10 +148,6 @@ export default class ObsidianAdmonition extends Plugin {
addIcon(COPY_ICON_NAME, COPY_ICON);
addIcon(SPIN_ICON_NAME, SPIN_ICON);

if (this.data.syntaxHighlight) {
this.turnOnSyntaxHighlighting();
}

/** Add generic commands. */
this.addCommand({
id: "collapse-admonitions",
Expand Down Expand Up @@ -201,7 +197,6 @@ export default class ObsidianAdmonition extends Plugin {
}
}
});

this.addCommand({
id: "insert-admonition",
name: "Insert Admonition",
Expand Down Expand Up @@ -240,7 +235,6 @@ ${editor.getDoc().getSelection()}
suggestor.open();
}
});

this.addCommand({
id: "insert-admonition",
name: "Insert Callout",
Expand Down Expand Up @@ -572,22 +566,19 @@ ${selection.split("\n").join("\n> ")}
return contentEl;
}

async addAdmonition(admonition: Admonition): Promise<void> {
this.data.userAdmonitions = {
...this.data.userAdmonitions,
[admonition.type]: admonition
};
this.admonitions = {
...ADMONITION_MAP,
...this.data.userAdmonitions
};

registerType(admonition: Admonition) {
/** Turn on CodeMirror syntax highlighting for this "language" */
if (this.data.syntaxHighlight) {
this.turnOnSyntaxHighlighting([admonition.type]);
}

/** Register an admonition code-block post processor for legacy support. */
if (this.postprocessors.has(admonition.type)) {
//@ts-expect-error
MarkdownPreviewRenderer.unregisterCodeBlockPostProcessor(
`ad-${admonition.type}`
);
}
this.postprocessors.set(
admonition.type,
this.registerMarkdownCodeBlockProcessor(
Expand All @@ -596,6 +587,21 @@ ${selection.split("\n").join("\n> ")}
this.postprocessor(admonition.type, src, el, ctx)
)
);
if (admonition.command) {
this.registerCommandsFor(admonition);
}
}
get admonitions() {
return { ...ADMONITION_MAP, ...this.data.userAdmonitions}
}
async addAdmonition(admonition: Admonition): Promise<void> {
this.data.userAdmonitions = {
...this.data.userAdmonitions,
[admonition.type]: admonition
};

this.registerCommandsFor(admonition);

/** Create the admonition type in CSS */
this.calloutManager.addAdmonition(admonition);

Expand Down Expand Up @@ -677,15 +683,7 @@ ${editor.getDoc().getSelection()}
}
});
}
async removeAdmonition(admonition: Admonition) {
if (this.data.userAdmonitions[admonition.type]) {
delete this.data.userAdmonitions[admonition.type];
}
this.admonitions = {
...ADMONITION_MAP,
...this.data.userAdmonitions
};

unregisterType(admonition: Admonition) {
if (this.data.syntaxHighlight) {
this.turnOffSyntaxHighlighting([admonition.type]);
}
Expand All @@ -704,6 +702,13 @@ ${editor.getDoc().getSelection()}
);
this.postprocessors.delete(admonition.type);
}
}
async removeAdmonition(admonition: Admonition) {
if (this.data.userAdmonitions[admonition.type]) {
delete this.data.userAdmonitions[admonition.type];
}

this.unregisterType(admonition);

/** Remove the admonition type in CSS */
this.calloutManager.removeAdmonition(admonition);
Expand Down Expand Up @@ -803,11 +808,7 @@ ${editor.getDoc().getSelection()}
this.data.rpgDownloadedOnce = true;
} catch (e) {}
}

this.admonitions = {
...ADMONITION_MAP,
...this.data.userAdmonitions
};

await this.saveSettings();
}

Expand Down
48 changes: 42 additions & 6 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,9 @@ export default class AdmonitionSetting extends PluginSettingTab {

async buildTypes() {
this.additionalEl.empty();
for (let a in this.plugin.data.userAdmonitions) {
const admonition = this.plugin.data.userAdmonitions[a];

for (const admonition of Object.values(
this.plugin.data.userAdmonitions
)) {
let setting = new Setting(this.additionalEl);

let admonitionElement = this.plugin.getAdmonitionElement(
Expand Down Expand Up @@ -741,8 +741,7 @@ export default class AdmonitionSetting extends PluginSettingTab {
modal.onClose = async () => {
if (modal.saved) {
const hasCommand = admonition.command;
this.plugin.removeAdmonition(admonition);
this.plugin.addAdmonition({
const modalAdmonition = {
type: modal.type,
color: modal.color,
icon: modal.icon,
Expand All @@ -751,7 +750,44 @@ export default class AdmonitionSetting extends PluginSettingTab {
injectColor: modal.injectColor,
noTitle: modal.noTitle,
copy: modal.copy
});
};

if (
modalAdmonition.type != admonition.type
) {
this.plugin.unregisterType(admonition);
this.plugin.registerType(
modalAdmonition
);

const existing: [string, Admonition][] =
Object.entries(
this.plugin.data.userAdmonitions
);

this.plugin.data.userAdmonitions =
Object.fromEntries(
existing.map(([type, def]) => {
if (
type == admonition.type
) {
return [
modalAdmonition.type,
modalAdmonition
];
}
return [type, def];
})
);
} else {
this.plugin.data.userAdmonitions[
modalAdmonition.type
] = modalAdmonition;
}

/* this.plugin.removeAdmonition(admonition);
this.plugin.addAdmonition(modalAdmonition); */

this.display();
}
};
Expand Down

0 comments on commit 54ea584

Please sign in to comment.