1- import { PluginSettingTab , Setting , Notice } from 'obsidian'
1+ import { PluginSettingTab , Setting , Notice , TFolder } from 'obsidian'
22import * as AnkiConnect from './anki'
33
44const defaultDescs = {
@@ -9,7 +9,8 @@ const defaultDescs = {
99 "Add Context" : "Append 'context' for the card, in the form of path > heading > heading etc, to the field specified in the table." ,
1010 "CurlyCloze" : "Convert {cloze deletions} -> {{c1::cloze deletions}} on note types that have a 'Cloze' in their name." ,
1111 "CurlyCloze - Highlights to Clozes" : "Convert ==highlights== -> {highlights} to be processed by CurlyCloze." ,
12- "ID Comments" : "Wrap note IDs in a HTML comment."
12+ "ID Comments" : "Wrap note IDs in a HTML comment." ,
13+ "Add Obsidian Tags" : "Interpret #tags in the fields of a note as Anki tags, removing them from the note text in Anki."
1314}
1415
1516export class SettingsTab extends PluginSettingTab {
@@ -94,13 +95,11 @@ export class SettingsTab extends PluginSettingTab {
9495 context_field . controlEl . className += " anki-center"
9596 }
9697
97- setup_table ( ) {
98+ create_collapsible ( name : string ) {
9899 let { containerEl} = this ;
99- const plugin = ( this as any ) . plugin
100- containerEl . createEl ( 'h3' , { text : 'Note type settings' } )
101100 let div = containerEl . createEl ( 'div' , { cls : "collapsible-item" } )
102101 div . innerHTML = `
103- <div class="collapsible-item-self"><div class="collapsible-item-collapse collapse-icon anki-rotated"><svg viewBox="0 0 100 100" width="8" height="8" class="right-triangle"><path fill="currentColor" stroke="currentColor" d="M94.9,20.8c-1.4-2.5-4.1-4.1-7.1-4.1H12.2c-3,0-5.7,1.6-7.1,4.1c-1.3,2.4-1.2,5.2,0.2,7.6L43.1,88c1.5,2.3,4,3.7,6.9,3.7 s5.4-1.4,6.9-3.7l37.8-59.6C96.1,26,96.2,23.2,94.9,20.8L94.9,20.8z"></path></svg></div><div class="collapsible-item-inner"></div><header >Note Type Table </header></div>
102+ <div class="collapsible-item-self"><div class="collapsible-item-collapse collapse-icon anki-rotated"><svg viewBox="0 0 100 100" width="8" height="8" class="right-triangle"><path fill="currentColor" stroke="currentColor" d="M94.9,20.8c-1.4-2.5-4.1-4.1-7.1-4.1H12.2c-3,0-5.7,1.6-7.1,4.1c-1.3,2.4-1.2,5.2,0.2,7.6L43.1,88c1.5,2.3,4,3.7,6.9,3.7 s5.4-1.4,6.9-3.7l37.8-59.6C96.1,26,96.2,23.2,94.9,20.8L94.9,20.8z"></path></svg></div><div class="collapsible-item-inner"></div><header> ${ name } </header></div>
104103 `
105104 div . addEventListener ( 'click' , function ( ) {
106105 this . classList . toggle ( "active" )
@@ -113,6 +112,13 @@ export class SettingsTab extends PluginSettingTab {
113112 content . style . display = "block"
114113 }
115114 } )
115+ }
116+
117+ setup_note_table ( ) {
118+ let { containerEl} = this ;
119+ const plugin = ( this as any ) . plugin
120+ containerEl . createEl ( 'h3' , { text : 'Note type settings' } )
121+ this . create_collapsible ( "Note Type Table" )
116122 let note_type_table = containerEl . createEl ( 'table' , { cls : "anki-settings-table" } )
117123 let head = note_type_table . createTHead ( )
118124 let header_row = head . insertRow ( )
@@ -176,6 +182,10 @@ export class SettingsTab extends PluginSettingTab {
176182 if ( ! ( plugin . settings [ "Defaults" ] . hasOwnProperty ( "CurlyCloze - Highlights to Clozes" ) ) ) {
177183 plugin . settings [ "Defaults" ] [ "CurlyCloze - Highlights to Clozes" ] = false
178184 }
185+ // To account for new add obsidian tags
186+ if ( ! ( plugin . settings [ "Defaults" ] . hasOwnProperty ( "Add Obsidian Tags" ) ) ) {
187+ plugin . settings [ "Defaults" ] [ "Add Obsidian Tags" ] = false
188+ }
179189 for ( let key of Object . keys ( plugin . settings [ "Defaults" ] ) ) {
180190 // To account for removal of regex setting
181191 if ( key === "Regex" ) {
@@ -230,12 +240,97 @@ export class SettingsTab extends PluginSettingTab {
230240 }
231241 }
232242
243+ get_folders ( ) : TFolder [ ] {
244+ const app = ( this as any ) . plugin . app
245+ let folder_list : TFolder [ ] = [ app . vault . getRoot ( ) ]
246+ for ( let folder of folder_list ) {
247+ let filtered_list : TFolder [ ] = folder . children . filter ( ( element ) => element . hasOwnProperty ( "children" ) ) as TFolder [ ]
248+ folder_list . push ( ...filtered_list )
249+ }
250+ return folder_list . slice ( 1 ) //Removes initial vault folder
251+ }
252+
253+ setup_folder_deck ( folder : TFolder , row_cells : HTMLCollection ) {
254+ const plugin = ( this as any ) . plugin
255+ let folder_decks = plugin . settings . FOLDER_DECKS
256+ if ( ! ( folder_decks . hasOwnProperty ( folder . path ) ) ) {
257+ folder_decks [ folder . path ] = ""
258+ }
259+ let folder_deck = new Setting ( row_cells [ 1 ] as HTMLElement )
260+ . addText (
261+ text => text . setValue ( folder_decks [ folder . path ] )
262+ . onChange ( ( value ) => {
263+ plugin . settings . FOLDER_DECKS [ folder . path ] = value
264+ plugin . saveAllData ( )
265+ } )
266+ )
267+ folder_deck . settingEl = row_cells [ 1 ] as HTMLElement
268+ folder_deck . infoEl . remove ( )
269+ folder_deck . controlEl . className += " anki-center"
270+ }
271+
272+ setup_folder_tag ( folder : TFolder , row_cells : HTMLCollection ) {
273+ const plugin = ( this as any ) . plugin
274+ let folder_tags = plugin . settings . FOLDER_TAGS
275+ if ( ! ( folder_tags . hasOwnProperty ( folder . path ) ) ) {
276+ folder_tags [ folder . path ] = ""
277+ }
278+ let folder_tag = new Setting ( row_cells [ 2 ] as HTMLElement )
279+ . addText (
280+ text => text . setValue ( folder_tags [ folder . path ] )
281+ . onChange ( ( value ) => {
282+ plugin . settings . FOLDER_TAGS [ folder . path ] = value
283+ plugin . saveAllData ( )
284+ } )
285+ )
286+ folder_tag . settingEl = row_cells [ 2 ] as HTMLElement
287+ folder_tag . infoEl . remove ( )
288+ folder_tag . controlEl . className += " anki-center"
289+ }
290+
291+ setup_folder_table ( ) {
292+ let { containerEl} = this ;
293+ const plugin = ( this as any ) . plugin
294+ const folder_list = this . get_folders ( )
295+ containerEl . createEl ( 'h3' , { text : 'Folder settings' } )
296+ this . create_collapsible ( "Folder Table" )
297+ let folder_table = containerEl . createEl ( 'table' , { cls : "anki-settings-table" } )
298+ let head = folder_table . createTHead ( )
299+ let header_row = head . insertRow ( )
300+ for ( let header of [ "Folder" , "Folder Deck" , "Folder Tags" ] ) {
301+ let th = document . createElement ( "th" )
302+ th . appendChild ( document . createTextNode ( header ) )
303+ header_row . appendChild ( th )
304+ }
305+ let main_body = folder_table . createTBody ( )
306+ if ( ! ( plugin . settings . hasOwnProperty ( "FOLDER_DECKS" ) ) ) {
307+ plugin . settings . FOLDER_DECKS = { }
308+ }
309+ if ( ! ( plugin . settings . hasOwnProperty ( "FOLDER_TAGS" ) ) ) {
310+ plugin . settings . FOLDER_TAGS = { }
311+ }
312+ for ( let folder of folder_list ) {
313+ let row = main_body . insertRow ( )
314+
315+ row . insertCell ( )
316+ row . insertCell ( )
317+ row . insertCell ( )
318+
319+ let row_cells = row . children
320+
321+ row_cells [ 0 ] . innerHTML = folder . path
322+ this . setup_folder_deck ( folder , row_cells )
323+ this . setup_folder_tag ( folder , row_cells )
324+ }
325+
326+ }
327+
233328 setup_buttons ( ) {
234329 let { containerEl} = this
235330 const plugin = ( this as any ) . plugin
236331 let action_buttons = containerEl . createEl ( 'h3' , { text : 'Actions' } )
237332 new Setting ( action_buttons )
238- . setName ( "Regenerate Table" )
333+ . setName ( "Regenerate Note Type Table" )
239334 . setDesc ( "Connect to Anki to regenerate the table with new note types, or get rid of deleted note types." )
240335 . addButton (
241336 button => {
@@ -304,7 +399,8 @@ export class SettingsTab extends PluginSettingTab {
304399 containerEl . empty ( )
305400 containerEl . createEl ( 'h2' , { text : 'Obsidian_to_Anki settings' } )
306401 containerEl . createEl ( 'a' , { text : 'For more information check the wiki' , href : "https://github.com/Pseudonium/Obsidian_to_Anki/wiki" } )
307- this . setup_table ( )
402+ this . setup_note_table ( )
403+ this . setup_folder_table ( )
308404 this . setup_syntax ( )
309405 this . setup_defaults ( )
310406 this . setup_buttons ( )
0 commit comments