Skip to content

Commit 18e966e

Browse files
authored
fix(settings): remove ability to enter non-number in number input boxes (#880)
close #878
1 parent bd5b942 commit 18e966e

File tree

1 file changed

+53
-95
lines changed

1 file changed

+53
-95
lines changed

src/setting/settings.ts

Lines changed: 53 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -93,34 +93,18 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
9393
: "Commit and sync"
9494
} changes every X minutes. Set to 0 (default) to disable. (See below setting for further configuration!)`
9595
)
96-
.addText((text) =>
97-
text
98-
.setValue(String(plugin.settings.autoSaveInterval))
99-
.onChange(async (value) => {
100-
if (!isNaN(Number(value))) {
101-
plugin.settings.autoSaveInterval =
102-
Number(value);
103-
await plugin.saveSettings();
104-
105-
plugin.automaticsManager.reload("commit");
106-
if (plugin.settings.autoSaveInterval > 0) {
107-
new Notice(
108-
`Automatic ${commitOrSync} enabled! Every ${formatMinutes(
109-
plugin.settings.autoSaveInterval
110-
)}.`
111-
);
112-
} else if (
113-
plugin.settings.autoSaveInterval <= 0
114-
) {
115-
new Notice(
116-
`Automatic ${commitOrSync} disabled!`
117-
);
118-
}
119-
} else {
120-
new Notice("Please specify a valid number.");
121-
}
122-
})
123-
);
96+
.addText((text) => {
97+
text.inputEl.type = "number";
98+
text.setValue(String(plugin.settings.autoSaveInterval));
99+
text.setPlaceholder(
100+
String(DEFAULT_SETTINGS.autoSaveInterval)
101+
);
102+
text.onChange(async (value) => {
103+
plugin.settings.autoSaveInterval = Number(value);
104+
await plugin.saveSettings();
105+
plugin.automaticsManager.reload("commit");
106+
});
107+
});
124108

125109
setting = new Setting(containerEl)
126110
.setName(`Auto ${commitOrSync} after stopping file edits`)
@@ -171,32 +155,18 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
171155
.setDesc(
172156
"Push commits every X minutes. Set to 0 (default) to disable."
173157
)
174-
.addText((text) =>
175-
text
176-
.setValue(String(plugin.settings.autoPushInterval))
177-
.onChange(async (value) => {
178-
if (!isNaN(Number(value))) {
179-
plugin.settings.autoPushInterval =
180-
Number(value);
181-
await plugin.saveSettings();
182-
183-
plugin.automaticsManager.reload("push");
184-
if (plugin.settings.autoPushInterval > 0) {
185-
new Notice(
186-
`Automatic push enabled! Every ${formatMinutes(
187-
plugin.settings.autoPushInterval
188-
)}.`
189-
);
190-
} else if (
191-
plugin.settings.autoPushInterval <= 0
192-
) {
193-
new Notice("Automatic push disabled!");
194-
}
195-
} else {
196-
new Notice("Please specify a valid number.");
197-
}
198-
})
199-
);
158+
.addText((text) => {
159+
text.inputEl.type = "number";
160+
text.setValue(String(plugin.settings.autoPushInterval));
161+
text.setPlaceholder(
162+
String(DEFAULT_SETTINGS.autoPushInterval)
163+
);
164+
text.onChange(async (value) => {
165+
plugin.settings.autoPushInterval = Number(value);
166+
await plugin.saveSettings();
167+
plugin.automaticsManager.reload("push");
168+
});
169+
});
200170
this.mayDisableSetting(
201171
setting,
202172
!plugin.settings.differentIntervalCommitAndPush
@@ -207,32 +177,18 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
207177
.setDesc(
208178
"Pull changes every X minutes. Set to 0 (default) to disable."
209179
)
210-
.addText((text) =>
211-
text
212-
.setValue(String(plugin.settings.autoPullInterval))
213-
.onChange(async (value) => {
214-
if (!isNaN(Number(value))) {
215-
plugin.settings.autoPullInterval =
216-
Number(value);
217-
await plugin.saveSettings();
218-
219-
plugin.automaticsManager.reload("pull");
220-
if (plugin.settings.autoPullInterval > 0) {
221-
new Notice(
222-
`Automatic pull enabled! Every ${formatMinutes(
223-
plugin.settings.autoPullInterval
224-
)}.`
225-
);
226-
} else if (
227-
plugin.settings.autoPullInterval <= 0
228-
) {
229-
new Notice("Automatic pull disabled!");
230-
}
231-
} else {
232-
new Notice("Please specify a valid number.");
233-
}
234-
})
235-
);
180+
.addText((text) => {
181+
text.inputEl.type = "number";
182+
text.setValue(String(plugin.settings.autoPullInterval));
183+
text.setPlaceholder(
184+
String(DEFAULT_SETTINGS.autoPullInterval)
185+
);
186+
text.onChange(async (value) => {
187+
plugin.settings.autoPullInterval = Number(value);
188+
await plugin.saveSettings();
189+
plugin.automaticsManager.reload("pull");
190+
});
191+
});
236192

237193
new Setting(containerEl)
238194
.setName(
@@ -479,21 +435,23 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
479435
.setDesc(
480436
"Milliseconds to wait after file change before refreshing the Source Control View."
481437
)
482-
.addText((toggle) =>
483-
toggle
484-
.setValue(
485-
plugin.settings.refreshSourceControlTimer.toString()
486-
)
487-
.setPlaceholder("7000")
488-
.onChange(async (value) => {
489-
plugin.settings.refreshSourceControlTimer = Math.max(
490-
parseInt(value),
491-
500
492-
);
493-
await plugin.saveSettings();
494-
plugin.setRefreshDebouncer();
495-
})
496-
);
438+
.addText((text) => {
439+
text.inputEl.type = "number";
440+
text.setValue(
441+
String(plugin.settings.refreshSourceControlTimer)
442+
);
443+
text.setPlaceholder(
444+
String(DEFAULT_SETTINGS.refreshSourceControlTimer)
445+
);
446+
text.onChange(async (value) => {
447+
plugin.settings.refreshSourceControlTimer = Math.max(
448+
Number(value),
449+
500
450+
);
451+
await plugin.saveSettings();
452+
plugin.setRefreshDebouncer();
453+
});
454+
});
497455
new Setting(containerEl).setName("Miscellaneous").setHeading();
498456

499457
if (plugin.gitManager instanceof SimpleGit) {

0 commit comments

Comments
 (0)