1
+ import { isUndefined } from "@/helpers" ;
1
2
import type GithubStarsPlugin from "@/main" ;
3
+ import { confirm } from "@/modals" ;
2
4
import {
3
5
type App ,
4
6
type Debouncer ,
7
9
debounce ,
8
10
normalizePath ,
9
11
} from "obsidian" ;
10
- import { isUndefined } from "./helpers" ;
11
12
12
13
export interface PluginSettings {
13
14
pageSize : number ;
@@ -47,6 +48,48 @@ export class SettingsTab extends PluginSettingTab {
47
48
this . initSettings ( ) ;
48
49
}
49
50
51
+ async validateSettings ( settings : Partial < PluginSettings > ) {
52
+ const validSettings : Partial < PluginSettings > = { } ;
53
+ if (
54
+ ! isUndefined ( settings . accessToken ) &&
55
+ this . plugin . settings . accessToken !== settings . accessToken
56
+ ) {
57
+ validSettings . accessToken = settings . accessToken ;
58
+ }
59
+
60
+ if (
61
+ ! isUndefined ( settings . pageSize ) &&
62
+ this . plugin . settings . pageSize !== settings . pageSize
63
+ ) {
64
+ validSettings . pageSize = settings . pageSize ;
65
+ if ( validSettings . pageSize < 1 ) {
66
+ validSettings . pageSize = 1 ;
67
+ }
68
+ if ( validSettings . pageSize > 100 ) {
69
+ validSettings . pageSize = 100 ;
70
+ }
71
+ }
72
+
73
+ if (
74
+ ! isUndefined ( settings . destinationFolder ) &&
75
+ this . plugin . settings . destinationFolder !==
76
+ settings . destinationFolder
77
+ ) {
78
+ const isRenameConfirmed = await confirm ( {
79
+ app : this . app ,
80
+ title : "Rename destination folder?" ,
81
+ message : `Destination folder will be renamed from <pre>${ this . plugin . settings . destinationFolder } </pre> to <pre>${ settings . destinationFolder } </pre>` ,
82
+ okButtonText : "Yes" ,
83
+ cancelButtonText : "No" ,
84
+ } ) ;
85
+ if ( isRenameConfirmed ) {
86
+ validSettings . destinationFolder = settings . destinationFolder ;
87
+ }
88
+ }
89
+
90
+ return validSettings ;
91
+ }
92
+
50
93
async updateSettings ( newSettings : Partial < PluginSettings > ) {
51
94
if ( isUndefined ( this . debouncedUpdateSettings ) ) {
52
95
this . debouncedUpdateSettings = debounce (
@@ -56,7 +99,8 @@ export class SettingsTab extends PluginSettingTab {
56
99
this . pageSizeSetting ?. setDisabled ( true ) ;
57
100
this . destinationFolderSetting ?. setDisabled ( true ) ;
58
101
59
- await this . plugin . updateSettings ( settings ) ;
102
+ const validSettings = await this . validateSettings ( settings ) ;
103
+ await this . plugin . updateSettings ( validSettings ) ;
60
104
61
105
this . accessTokenSetting ?. setDisabled ( false ) ;
62
106
this . pageSizeSetting ?. setDisabled ( false ) ;
0 commit comments