11import { Notice , parseYaml , Plugin , stringifyYaml , TFile , TFolder } from 'obsidian' ;
2- import { DEFAULT_SETTINGS , MediaDbPluginSettings , MediaDbSettingTab } from './settings/Settings' ;
2+ import { getDefaultSettings , MediaDbPluginSettings , MediaDbSettingTab } from './settings/Settings' ;
33import { APIManager } from './api/APIManager' ;
44import { MediaTypeModel } from './models/MediaTypeModel' ;
55import { dateTimeToString , debugLog , markdownTable , replaceIllegalFileNameCharactersInString , UserCancelError , UserSkipError } from './utils/Utils' ;
@@ -16,6 +16,7 @@ import {BoardGameGeekAPI} from './api/apis/BoardGameGeekAPI';
1616import { PropertyMapper } from './settings/PropertyMapper' ;
1717import { YAMLConverter } from './utils/YAMLConverter' ;
1818import { MediaDbFolderImportModal } from './modals/MediaDbFolderImportModal' ;
19+ import { PropertyMapping , PropertyMappingModel } from './settings/PropertyMapping' ;
1920
2021export default class MediaDbPlugin extends Plugin {
2122 settings : MediaDbPluginSettings ;
@@ -26,14 +27,6 @@ export default class MediaDbPlugin extends Plugin {
2627 frontMatterRexExpPattern : string = '^(---)\\n[\\s\\S]*?\\n---' ;
2728
2829 async onload ( ) {
29- await this . loadSettings ( ) ;
30- // register the settings tab
31- this . addSettingTab ( new MediaDbSettingTab ( this . app , this ) ) ;
32-
33- // TESTING
34- this . settings . propertyMappings = DEFAULT_SETTINGS . propertyMappings ;
35-
36-
3730 this . apiManager = new APIManager ( ) ;
3831 // register APIs
3932 this . apiManager . registerAPI ( new OMDbAPI ( this ) ) ;
@@ -44,9 +37,18 @@ export default class MediaDbPlugin extends Plugin {
4437 this . apiManager . registerAPI ( new BoardGameGeekAPI ( this ) ) ;
4538 // this.apiManager.registerAPI(new LocGovAPI(this)); // TODO: parse data
4639
47- this . mediaTypeManager = new MediaTypeManager ( this . settings ) ;
40+ this . mediaTypeManager = new MediaTypeManager ( ) ;
4841 this . modelPropertyMapper = new PropertyMapper ( this ) ;
4942
43+ await this . loadSettings ( ) ;
44+ // register the settings tab
45+ this . addSettingTab ( new MediaDbSettingTab ( this . app , this ) ) ;
46+
47+ // TESTING
48+ // this.settings.propertyMappingModels = getDefaultSettings(this).propertyMappingModels;
49+
50+ this . mediaTypeManager . updateTemplates ( this . settings ) ;
51+
5052
5153 // add icon to the left ribbon
5254 const ribbonIconEl = this . addRibbonIcon ( 'database' , 'Add new Media DB entry' , ( evt : MouseEvent ) =>
@@ -491,8 +493,37 @@ export default class MediaDbPlugin extends Plugin {
491493 }
492494
493495 async loadSettings ( ) {
494- console . log ( DEFAULT_SETTINGS ) ;
495- this . settings = Object . assign ( { } , DEFAULT_SETTINGS , await this . loadData ( ) ) ;
496+ // console.log(DEFAULT_SETTINGS);
497+ const diskSettings : MediaDbPluginSettings = await this . loadData ( ) ;
498+ const defaultSettings : MediaDbPluginSettings = getDefaultSettings ( this ) ;
499+ const loadedSettings : MediaDbPluginSettings = Object . assign ( { } , defaultSettings , diskSettings ) ;
500+
501+ // migrate the settings loaded from the disk to match the structure of the default settings
502+ let newPropertyMappings : PropertyMappingModel [ ] = [ ] ;
503+ for ( const defaultPropertyMappingModel of defaultSettings . propertyMappingModels ) {
504+ let newPropertyMappingModel : PropertyMappingModel = loadedSettings . propertyMappingModels . find ( x => x . type === defaultPropertyMappingModel . type ) ;
505+ if ( newPropertyMappingModel === undefined ) { // if the propertyMappingModel exists in the default settings but not the loaded settings, add it
506+ newPropertyMappings . push ( defaultPropertyMappingModel ) ;
507+ } else { // if the propertyMappingModel also exists in the loaded settings, add it from there
508+ let newProperties : PropertyMapping [ ] = [ ] ;
509+
510+ for ( const defaultProperty of defaultPropertyMappingModel . properties ) {
511+ let newProperty = newPropertyMappingModel . properties . find ( x => x . property === defaultProperty . property ) ;
512+ if ( newProperty === undefined ) {
513+ newProperties . push ( defaultProperty ) ;
514+ } else {
515+ newProperties . push ( newProperty ) ;
516+ }
517+ }
518+
519+ newPropertyMappingModel . properties = newProperties ;
520+
521+ newPropertyMappings . push ( newPropertyMappingModel ) ;
522+ }
523+ }
524+ loadedSettings . propertyMappingModels = newPropertyMappings ;
525+
526+ this . settings = loadedSettings ;
496527 }
497528
498529 async saveSettings ( ) {
0 commit comments