@@ -136,52 +136,94 @@ class CssSnippetStoreModal extends Modal {
136
136
}
137
137
}
138
138
139
+ async uninstall ( name : string ) {
140
+ const vault = this . app . vault ;
141
+ const snippetFolderPath = '.obsidian/snippets' ;
142
+ const fileName = name + '.css' ;
143
+ const fullPath = `${ snippetFolderPath } /${ fileName } ` ;
144
+
145
+ try {
146
+ // Check if the file exists
147
+ if ( await vault . adapter . exists ( fullPath ) ) {
148
+ await vault . adapter . remove ( fullPath ) ;
149
+ new Notice ( `Snippet "${ fileName } " deleted.` ) ;
150
+ } else {
151
+ new Notice ( `Snippet "${ fileName } " does not exist.` ) ;
152
+ }
153
+ } catch ( err ) {
154
+ console . error ( 'Failed to delete snippet:' , err ) ;
155
+ new Notice ( 'Failed to delete snippet. See console for details.' ) ;
156
+ }
157
+ }
158
+
159
+ async checkSnippetExists ( name : string ) : Promise < boolean > {
160
+ const vault = this . app . vault ;
161
+ const snippetFolderPath = '.obsidian/snippets' ;
162
+ const fileName = name + '.css' ;
163
+ const fullPath = `${ snippetFolderPath } /${ fileName } ` ;
164
+ return await vault . adapter . exists ( fullPath ) ;
165
+ }
166
+
139
167
onOpen ( ) {
140
168
const { contentEl } = this ;
141
169
contentEl . createEl ( 'h1' , { text : 'CSS Snippet Store' } ) ;
142
170
const grid = contentEl . createEl ( 'div' , { cls : 'snippet-store-grid' } ) ;
143
171
144
- this . snippets . forEach ( snippet => {
172
+ this . snippets . forEach ( snippet => {
145
173
const card = grid . createDiv ( { cls : 'community-item' } ) ;
146
174
147
175
card . createEl ( 'div' , { text : snippet . name , cls : 'community-item-name' } ) ;
148
176
card . createEl ( 'div' , { text : `By ${ snippet . author } ` , cls : 'community-item-author' } ) ;
149
177
card . createEl ( 'div' , { text : snippet . description , cls : 'community-desc' } ) ;
150
178
151
- const buttonWrapper = card . createEl ( 'div' , { cls : 'snippet-store-button-wrapper' } ) ;
152
-
153
- const button = buttonWrapper . createEl ( 'button' , { text : 'Install' , cls : 'mod-cta' } ) ;
154
-
155
- // Attach event listener
156
- button . addEventListener ( 'click' , async ( ) => {
157
- const url = "https://raw.githubusercontent.com/" + snippet . repo + "/refs/heads/main/" + snippet . folder + "/snippet.css"
158
- try {
159
- if ( navigator . onLine ) {
160
- const response = await fetch ( url ) ;
161
- const code = await response . text ( ) ;
162
- await this . install ( snippet . id , code ) ;
163
- } else {
164
- new Notice ( `No Internet connection...` ) ;
165
- return ;
166
- }
167
- } catch ( error ) {
168
- console . error ( error ) ;
169
- new Notice ( `Error: ${ error . message } ` ) ;
179
+ const buttonWrapper = card . createEl ( 'div' , { cls : 'snippet-store-button-wrapper' } ) ;
180
+
181
+ const button = buttonWrapper . createEl ( 'button' , { cls : 'mod-cta' } ) ;
182
+
183
+ // Check if snippet already exists before updating the button text
184
+ this . checkSnippetExists ( snippet . id ) . then ( ( exists ) => {
185
+ if ( exists ) {
186
+ button . textContent = 'Delete' ;
187
+ button . className = 'mod-danger' ; // Optionally change the class to indicate danger
188
+
189
+ // Delete snippet logic
190
+ button . addEventListener ( 'click' , async ( ) => {
191
+ await this . uninstall ( snippet . id ) ;
192
+ // Optionally, reload the modal to update the button text after deletion
193
+ this . close ( ) ;
194
+ this . open ( ) ;
195
+ } ) ;
196
+ } else {
197
+ button . textContent = 'Install' ;
198
+ button . className = 'mod-cta' ;
199
+
200
+ // Install snippet logic
201
+ button . addEventListener ( 'click' , async ( ) => {
202
+ const url = "https://raw.githubusercontent.com/" + snippet . repo + "/refs/heads/main/" + snippet . folder + "/snippet.css"
203
+ try {
204
+ if ( navigator . onLine ) {
205
+ const response = await fetch ( url ) ;
206
+ const code = await response . text ( ) ;
207
+ await this . install ( snippet . id , code ) ;
208
+ // Optionally, reload the modal to update the button text after installation
209
+ this . close ( ) ;
210
+ this . open ( ) ;
211
+ } else {
212
+ new Notice ( `No Internet connection...` ) ;
213
+ return ;
214
+ }
215
+ } catch ( error ) {
216
+ console . error ( error ) ;
217
+ new Notice ( `Error: ${ error . message } ` ) ;
218
+ }
219
+ } ) ;
170
220
}
171
221
} ) ;
172
-
173
- if ( snippet . source ) {
174
- const sourceBtn = buttonWrapper . createEl ( 'a' , {
175
- href : snippet . source ,
176
- cls : 'snippet-link'
177
- } ) ;
178
- sourceBtn . createEl ( 'button' , { text : 'Source' , cls : '' } ) ;
179
- }
180
222
} ) ;
181
223
}
182
224
183
225
onClose ( ) {
184
226
const { contentEl } = this ;
185
227
contentEl . empty ( ) ;
186
228
}
187
- }
229
+ }
0 commit comments