Skip to content

Commit

Permalink
Merge pull request #12 from sharkmu/main
Browse files Browse the repository at this point in the history
Improve error handling for not a round number in settings. Issue: #11
  • Loading branch information
Deca committed Dec 29, 2023
2 parents 710dcb3 + b5dfdcb commit e8b25e4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/etc/ribbonIcon.ts
Expand Up @@ -26,4 +26,4 @@ export class RibbonIcon {
this.plugin.utils.formatDocument(editor);
});
};
}
}
82 changes: 71 additions & 11 deletions src/settings/settingTab.ts
Expand Up @@ -8,6 +8,9 @@ export class MainPluginSettingTab extends PluginSettingTab {
private invalidNumberMessage =
"Please enter a valid number.\nIt should be at least 0.";

private notRoundNumberMessage =
"Please enter a valid number.\nIt should be a round number.";

constructor(app: App, plugin: FormattoPlugin) {
super(app, plugin);
this.plugin = plugin;
Expand All @@ -25,6 +28,12 @@ export class MainPluginSettingTab extends PluginSettingTab {
) {
new Notice(text);
}
if (
value !== "" &&
parseInt(value) % 1 === 0
) {
new Notice(text);
}
},
1000,
true
Expand All @@ -44,13 +53,19 @@ export class MainPluginSettingTab extends PluginSettingTab {
this.plugin.settings.headingGaps.beforeTopLevelHeadings
)
.onChange(async (value) => {
if (
value !== "" &&
parseInt(value) % 1 === 0
) {
debounceMsg(this.notRoundNumberMessage, value);
}
if (
value !== "" &&
(isNaN(parseInt(value)) || parseInt(value) < 0)
) {
debounceMsg(this.invalidNumberMessage, value);
}

this.plugin.settings.headingGaps.beforeTopLevelHeadings =
value;
await this.plugin.saveSettings();
Expand All @@ -68,13 +83,19 @@ export class MainPluginSettingTab extends PluginSettingTab {
this.plugin.settings.headingGaps.beforeFirstSubHeading
)
.onChange(async (value) => {
if (
value !== "" &&
parseInt(value) % 1 === 0
) {
debounceMsg(this.notRoundNumberMessage, value);
}
if (
value !== "" &&
(isNaN(parseInt(value)) || parseInt(value) < 0)
) {
debounceMsg(this.invalidNumberMessage, value);
}

this.plugin.settings.headingGaps.beforeFirstSubHeading =
value;
await this.plugin.saveSettings();
Expand All @@ -92,13 +113,19 @@ export class MainPluginSettingTab extends PluginSettingTab {
this.plugin.settings.headingGaps.beforeSubHeadings
)
.onChange(async (value) => {
if (
value !== "" &&
parseInt(value) % 1 === 0
) {
debounceMsg(this.notRoundNumberMessage, value);
}
if (
value !== "" &&
(isNaN(parseInt(value)) || parseInt(value) < 0)
) {
debounceMsg(this.invalidNumberMessage, value);
}

this.plugin.settings.headingGaps.beforeSubHeadings =
value;
await this.plugin.saveSettings();
Expand All @@ -117,14 +144,21 @@ export class MainPluginSettingTab extends PluginSettingTab {
.setPlaceholder("2")
.setValue(this.plugin.settings.otherGaps.afterProperties)
.onChange(async (value) => {
if (
value !== "" &&
parseInt(value) % 1 === 0
) {
debounceMsg(this.notRoundNumberMessage, value);
}
if (
value !== "" &&
(isNaN(parseInt(value)) || parseInt(value) < 0)
) {
debounceMsg(this.invalidNumberMessage, value);
}

this.plugin.settings.otherGaps.afterProperties = value;

this.plugin.settings.otherGaps.afterProperties =
value;
await this.plugin.saveSettings();
})
);
Expand All @@ -138,14 +172,21 @@ export class MainPluginSettingTab extends PluginSettingTab {
.setPlaceholder("0")
.setValue(this.plugin.settings.otherGaps.beforeContents)
.onChange(async (value) => {
if (
value !== "" &&
parseInt(value) % 1 === 0
) {
debounceMsg(this.notRoundNumberMessage, value);
}
if (
value !== "" &&
(isNaN(parseInt(value)) || parseInt(value) < 0)
) {
debounceMsg(this.invalidNumberMessage, value);
}

this.plugin.settings.otherGaps.beforeContents = value;

this.plugin.settings.otherGaps.beforeContents =
value;
await this.plugin.saveSettings();
})
);
Expand All @@ -160,13 +201,19 @@ export class MainPluginSettingTab extends PluginSettingTab {
.beforeContentsAfterCodeBlocks
)
.onChange(async (value) => {
if (
value !== "" &&
parseInt(value) % 1 === 0
) {
debounceMsg(this.notRoundNumberMessage, value);
}
if (
value !== "" &&
(isNaN(parseInt(value)) || parseInt(value) < 0)
) {
debounceMsg(this.invalidNumberMessage, value);
}

this.plugin.settings.otherGaps.beforeContentsAfterCodeBlocks =
value;
await this.plugin.saveSettings();
Expand All @@ -180,14 +227,21 @@ export class MainPluginSettingTab extends PluginSettingTab {
.setPlaceholder("1")
.setValue(this.plugin.settings.otherGaps.beforeCodeBlocks)
.onChange(async (value) => {
if (
value !== "" &&
parseInt(value) % 1 === 0
) {
debounceMsg(this.notRoundNumberMessage, value);
}
if (
value !== "" &&
(isNaN(parseInt(value)) || parseInt(value) < 0)
) {
debounceMsg(this.invalidNumberMessage, value);
}

this.plugin.settings.otherGaps.beforeCodeBlocks = value;

this.plugin.settings.otherGaps.beforeCodeBlocks =
value;
await this.plugin.saveSettings();
})
);
Expand All @@ -204,13 +258,19 @@ export class MainPluginSettingTab extends PluginSettingTab {
.beforeCodeBlocksAfterHeadings
)
.onChange(async (value) => {
if (
value !== "" &&
parseInt(value) % 1 === 0
) {
debounceMsg(this.notRoundNumberMessage, value);
}
if (
value !== "" &&
(isNaN(parseInt(value)) || parseInt(value) < 0)
) {
debounceMsg(this.invalidNumberMessage, value);
}

this.plugin.settings.otherGaps.beforeCodeBlocksAfterHeadings =
value;
await this.plugin.saveSettings();
Expand Down

0 comments on commit e8b25e4

Please sign in to comment.