Skip to content

Commit

Permalink
fix: Fixes incorrect error being displayed when using an image as an …
Browse files Browse the repository at this point in the history
…icon (close #311)
  • Loading branch information
valentine195 committed Sep 14, 2023
1 parent 22f32d4 commit 099e84c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 47 deletions.
88 changes: 46 additions & 42 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,45 +863,45 @@ export default class AdmonitionSetting extends PluginSettingTab {
// });
// })
// )
// .addButton((b) => {
// b.setButtonText("Generate");
// b.onClick((evt) => {
// const admonition_icons: {
// [admonition_type: string]: {
// icon: string;
// color: string;
// };
// } = {};
//
// for (let key in this.plugin.admonitions) {
// const value = this.plugin.admonitions[key];
//
// admonition_icons[key] = {
// icon:
// this.plugin.iconManager.getIconNode(value.icon)
// ?.outerHTML ?? "",
// color: value.color
// };
// }
//
// const js = CONTENT.replace(
// /ADMONITION_ICON_MAP\s?=\s?\{\}/,
// "ADMONITION_ICON_MAP=" +
// JSON.stringify(admonition_icons)
// );
// const file = new Blob([js], {
// type: "text/javascript"
// });
// const link = createEl("a", {
// href: URL.createObjectURL(file),
// attr: {
// download: "publish.admonition.js"
// }
// });
// link.click();
// link.detach();
// });
// });
// .addButton((b) => {
// b.setButtonText("Generate");
// b.onClick((evt) => {
// const admonition_icons: {
// [admonition_type: string]: {
// icon: string;
// color: string;
// };
// } = {};
//
// for (let key in this.plugin.admonitions) {
// const value = this.plugin.admonitions[key];
//
// admonition_icons[key] = {
// icon:
// this.plugin.iconManager.getIconNode(value.icon)
// ?.outerHTML ?? "",
// color: value.color
// };
// }
//
// const js = CONTENT.replace(
// /ADMONITION_ICON_MAP\s?=\s?\{\}/,
// "ADMONITION_ICON_MAP=" +
// JSON.stringify(admonition_icons)
// );
// const file = new Blob([js], {
// type: "text/javascript"
// });
// const link = createEl("a", {
// href: URL.createObjectURL(file),
// attr: {
// download: "publish.admonition.js"
// }
// });
// link.click();
// link.detach();
// });
// });
}

buildTypes() {
Expand Down Expand Up @@ -1190,7 +1190,7 @@ class SettingsModal extends Modal {
const validate = async () => {
const v = text.inputEl.value;
const valid = AdmonitionValidator.validateIcon(
v,
{ name: v },
this.plugin
);
if (valid.success == false) {
Expand Down Expand Up @@ -1239,7 +1239,7 @@ class SettingsModal extends Modal {
const image = files[0];
const reader = new FileReader();
reader.onloadend = (evt) => {
var image = new Image();
const image = new Image();
image.onload = () => {
try {
// Resize the image
Expand Down Expand Up @@ -1289,10 +1289,14 @@ class SettingsModal extends Modal {
b.setTooltip(t("Save"))
.setIcon("checkmark")
.onClick(async () => {
const icon = { ...this.icon };
if (iconText.inputEl.value?.length) {
icon.name = iconText.inputEl.value;
}
const valid = AdmonitionValidator.validate(
this.plugin,
typeText.inputEl.value,
iconText.inputEl.value,
icon,
this.originalType
);
if (valid.success == false) {
Expand Down
15 changes: 10 additions & 5 deletions src/util/validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Admonition } from "src/@types";
import { Admonition, AdmonitionIconDefinition } from "src/@types";
import { t } from "src/lang/helpers";
import ObsidianAdmonition from "src/main";

Expand Down Expand Up @@ -104,7 +104,7 @@ export class AdmonitionValidator {
static validate(
plugin: ObsidianAdmonition,
type: string,
icon: string,
icon: AdmonitionIconDefinition,
oldType?: string
): Result {
const validType = AdmonitionValidator.validateType(
Expand Down Expand Up @@ -155,17 +155,22 @@ export class AdmonitionValidator {
return { success: true };
}
static validateIcon(
definition: string,
definition: AdmonitionIconDefinition,
plugin: ObsidianAdmonition
): Result {
if (!definition.length) {
if (definition.type === "image") {
return {
success: true
};
}
if (!definition.name?.length) {
return {
success: false,
message: t("Icon cannot be empty."),
failed: "icon"
};
}
const icon = plugin.iconManager.getIconType(definition);
const icon = plugin.iconManager.getIconType(definition.name);
if (!icon) {
return {
success: false,
Expand Down

0 comments on commit 099e84c

Please sign in to comment.