Skip to content

Commit 3db3496

Browse files
committed
cleanup and reduced number of settings validations
1 parent 68ec513 commit 3db3496

12 files changed

+171
-297
lines changed

src/api/APIManager.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {APIModel} from './APIModel';
22
import {MediaTypeModel} from '../models/MediaTypeModel';
3-
import {debugLog} from '../utils/Utils';
43

54
export class APIManager {
65
apis: APIModel[];
@@ -10,7 +9,7 @@ export class APIManager {
109
}
1110

1211
async query(query: string, apisToQuery: string[]): Promise<MediaTypeModel[]> {
13-
debugLog(`MDB | api manager queried with "${query}"`);
12+
console.debug(`MDB | api manager queried with "${query}"`);
1413

1514
let res: MediaTypeModel[] = [];
1615

src/main.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Notice, parseYaml, Plugin, stringifyYaml, TFile, TFolder} from 'obsidian
22
import {getDefaultSettings, MediaDbPluginSettings, MediaDbSettingTab} from './settings/Settings';
33
import {APIManager} from './api/APIManager';
44
import {MediaTypeModel} from './models/MediaTypeModel';
5-
import {dateTimeToString, debugLog, markdownTable, replaceIllegalFileNameCharactersInString, UserCancelError, UserSkipError} from './utils/Utils';
5+
import {dateTimeToString, markdownTable, replaceIllegalFileNameCharactersInString, UserCancelError, UserSkipError} from './utils/Utils';
66
import {OMDbAPI} from './api/apis/OMDbAPI';
77
import {MediaDbAdvancedSearchModal} from './modals/MediaDbAdvancedSearchModal';
88
import {MediaDbSearchResultModal} from './modals/MediaDbSearchResultModal';
@@ -44,12 +44,8 @@ export default class MediaDbPlugin extends Plugin {
4444
// register the settings tab
4545
this.addSettingTab(new MediaDbSettingTab(this.app, this));
4646

47-
// TESTING
48-
// this.settings.propertyMappingModels = getDefaultSettings(this).propertyMappingModels;
49-
5047
this.mediaTypeManager.updateTemplates(this.settings);
5148

52-
5349
// add icon to the left ribbon
5450
const ribbonIconEl = this.addRibbonIcon('database', 'Add new Media DB entry', (evt: MouseEvent) =>
5551
this.createEntryWithAdvancedSearchModal(),
@@ -136,7 +132,6 @@ export default class MediaDbPlugin extends Plugin {
136132

137133
selectModal.close();
138134

139-
debugLog(results);
140135
if (results) {
141136
await this.createMediaDbNotes(results);
142137
}
@@ -162,7 +157,6 @@ export default class MediaDbPlugin extends Plugin {
162157

163158
idSearchModal.close();
164159

165-
debugLog(result);
166160
if (result) {
167161
await this.createMediaDbNoteFromModel(result);
168162
}
@@ -189,7 +183,7 @@ export default class MediaDbPlugin extends Plugin {
189183

190184
async createMediaDbNoteFromModel(mediaTypeModel: MediaTypeModel, attachFile?: TFile): Promise<void> {
191185
try {
192-
console.log('MDB | Creating new note...');
186+
console.debug('MDB | creating new note');
193187

194188
let fileContent = await this.generateMediaDbNoteContents(mediaTypeModel, attachFile);
195189

@@ -216,13 +210,7 @@ export default class MediaDbPlugin extends Plugin {
216210
return {fileMetadata: fileMetadata, fileContent: fileContent};
217211
}
218212

219-
let attachFileMetadata: any = this.app.metadataCache.getFileCache(fileToAttach).frontmatter;
220-
if (attachFileMetadata) {
221-
attachFileMetadata = JSON.parse(JSON.stringify(attachFileMetadata)); // deep copy
222-
delete attachFileMetadata.position;
223-
} else {
224-
attachFileMetadata = {};
225-
}
213+
let attachFileMetadata: any = this.getMetadataFromFileCache(fileToAttach);
226214
fileMetadata = Object.assign(attachFileMetadata, fileMetadata);
227215

228216
let attachFileContent: string = await this.app.vault.read(fileToAttach);
@@ -241,7 +229,7 @@ export default class MediaDbPlugin extends Plugin {
241229
let templateMetadata: any = this.getMetaDataFromFileContent(template);
242230
fileMetadata = Object.assign(templateMetadata, fileMetadata);
243231

244-
const regExp = new RegExp('^(---)\\n[\\s\\S]*\\n---');
232+
const regExp = new RegExp(this.frontMatterRexExpPattern);
245233
const attachFileContent = template.replace(regExp, '');
246234
fileContent += attachFileContent;
247235

@@ -251,7 +239,7 @@ export default class MediaDbPlugin extends Plugin {
251239
getMetaDataFromFileContent(fileContent: string): any {
252240
let metadata: any;
253241

254-
const regExp = new RegExp('^(---)\\n[\\s\\S]*\\n---');
242+
const regExp = new RegExp(this.frontMatterRexExpPattern);
255243
const frontMatterRegExpResult = regExp.exec(fileContent);
256244
if (!frontMatterRegExpResult) {
257245
return {};
@@ -269,6 +257,19 @@ export default class MediaDbPlugin extends Plugin {
269257
metadata = {};
270258
}
271259

260+
console.debug(`MDB | metadata read from file content`, metadata);
261+
262+
return metadata;
263+
}
264+
265+
getMetadataFromFileCache(file: TFile) {
266+
let metadata: any = this.app.metadataCache.getFileCache(file).frontmatter;
267+
if (metadata) {
268+
metadata = Object.assign({}, metadata); // copy
269+
delete metadata.position;
270+
} else {
271+
metadata = {};
272+
}
272273
return metadata;
273274
}
274275

@@ -297,12 +298,13 @@ export default class MediaDbPlugin extends Plugin {
297298

298299
// create the file
299300
const targetFile = await this.app.vault.create(filePath, fileContent);
301+
console.debug(`MDB | created new file at ${filePath}`);
300302

301303
// open newly crated file
302304
if (openFile) {
303305
const activeLeaf = this.app.workspace.getUnpinnedLeaf();
304306
if (!activeLeaf) {
305-
console.warn('MDB | no active leaf, not opening media db note');
307+
console.warn('MDB | no active leaf, not opening newly created note');
306308
return;
307309
}
308310
await activeLeaf.openFile(targetFile, {state: {mode: 'source'}});
@@ -319,12 +321,10 @@ export default class MediaDbPlugin extends Plugin {
319321
throw new Error('MDB | there is no active note');
320322
}
321323

322-
let metadata: any = this.app.metadataCache.getFileCache(activeFile).frontmatter;
323-
metadata = JSON.parse(JSON.stringify(metadata)); // deep copy
324-
delete metadata.position; // remove unnecessary data from the FrontMatterCache
324+
let metadata: any = this.getMetadataFromFileCache(activeFile);
325325
metadata = this.modelPropertyMapper.convertObjectBack(metadata);
326326

327-
debugLog(metadata);
327+
console.debug(`MDB | read metadata`, metadata);
328328

329329
if (!metadata?.type || !metadata?.dataSource || !metadata?.id) {
330330
throw new Error('MDB | active note is not a Media DB entry or is missing metadata');
@@ -339,7 +339,8 @@ export default class MediaDbPlugin extends Plugin {
339339

340340
newMediaTypeModel = Object.assign(oldMediaTypeModel, newMediaTypeModel.getWithOutUserData());
341341

342-
console.log('MDB | deleting old entry');
342+
// deletion not happening anymore why is this log statement still here
343+
console.debug('MDB | deleting old entry');
343344
await this.createMediaDbNoteFromModel(newMediaTypeModel, activeFile);
344345
}
345346

@@ -361,7 +362,7 @@ export default class MediaDbPlugin extends Plugin {
361362
continue;
362363
}
363364

364-
let metadata: any = this.app.metadataCache.getFileCache(file).frontmatter;
365+
let metadata: any = this.getMetadataFromFileCache(file);
365366

366367
let title = metadata[titleFieldName];
367368
if (!title) {
@@ -525,7 +526,6 @@ export default class MediaDbPlugin extends Plugin {
525526

526527
async saveSettings() {
527528
this.mediaTypeManager.updateTemplates(this.settings);
528-
//this.modelPropertyMapper.updateConversionRules(this.settings);
529529

530530
await this.saveData(this.settings);
531531
}

src/modals/MediaDbAdvancedSearchModal.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import {ButtonComponent, Component, Modal, Notice, Setting, TextComponent, ToggleComponent} from 'obsidian';
1+
import {ButtonComponent, Modal, Notice, Setting, TextComponent, ToggleComponent} from 'obsidian';
22
import {MediaTypeModel} from '../models/MediaTypeModel';
3-
import {debugLog} from '../utils/Utils';
43
import MediaDbPlugin from '../main';
54

65
export class MediaDbAdvancedSearchModal extends Modal {
@@ -36,9 +35,6 @@ export class MediaDbAdvancedSearchModal extends Modal {
3635
}
3736

3837
async search(): Promise<MediaTypeModel[]> {
39-
40-
debugLog(this.selectedApis);
41-
4238
if (!this.query || this.query.length < 3) {
4339
new Notice('MDB | Query to short');
4440
return;
@@ -78,7 +74,7 @@ export class MediaDbAdvancedSearchModal extends Modal {
7874
contentEl.createDiv({cls: 'media-db-plugin-spacer'});
7975
contentEl.createEl('h3', {text: 'APIs to search'});
8076

81-
const apiToggleComponents: Component[] = [];
77+
// const apiToggleComponents: Component[] = [];
8278
for (const api of this.plugin.apiManager.apis) {
8379
const apiToggleListElementWrapper = contentEl.createEl('div', {cls: 'media-db-plugin-list-wrapper'});
8480

src/modals/MediaDbIdSearchModal.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {ButtonComponent, DropdownComponent, Modal, Notice, Setting, TextComponent} from 'obsidian';
22
import {MediaTypeModel} from '../models/MediaTypeModel';
3-
import {debugLog} from '../utils/Utils';
43
import MediaDbPlugin from '../main';
54

65
export class MediaDbIdSearchModal extends Modal {
@@ -33,9 +32,6 @@ export class MediaDbIdSearchModal extends Modal {
3332
}
3433

3534
async search(): Promise<MediaTypeModel> {
36-
37-
debugLog(this.selectedApi);
38-
3935
if (!this.query) {
4036
new Notice('MDB | no Id entered');
4137
return;

src/models/MediaTypeModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export abstract class MediaTypeModel {
1313
userData: object;
1414

1515

16-
constructor() {
16+
protected constructor() {
1717
this.type = undefined;
1818
this.subType = undefined;
1919
this.title = undefined;

src/models/MusicReleaseModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class MusicReleaseModel extends MediaTypeModel {
4545
}
4646

4747
getSummary(): string {
48-
var summary = this.title + ' (' + this.year + ')';
48+
let summary = this.title + ' (' + this.year + ')';
4949
if (this.artists.length > 0)
5050
summary += ' - ' + this.artists.join(', ');
5151
return summary;

src/settings/PropertyMapping.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export class PropertyMappingModel {
1919
}
2020

2121
validate(): { res: boolean, err?: Error } {
22+
console.debug(`MDB | validated property mappings for ${this.type}`);
23+
2224
// check properties
2325
for (const property of this.properties) {
2426
const propertyValidation = property.validate();
@@ -89,28 +91,6 @@ export class PropertyMapping {
8991
this.newProperty = newProperty;
9092
this.mapping = mapping;
9193
this.locked = locked ?? false;
92-
93-
/*
94-
const conversionRuleParts = conversionRule.split('->');
95-
if (conversionRuleParts.length !== 2) {
96-
throw Error(`Conversion rule "${conversionRule}" may only have exactly one "->"`);
97-
}
98-
99-
let property = conversionRuleParts[0].trim();
100-
let newProperty = conversionRuleParts[1].trim();
101-
102-
if (!property || !containsOnlyLettersAndUnderscores(property)) {
103-
throw Error(`Error in conversion rule "${conversionRule}": property may not be empty and only contain letters and underscores.`);
104-
}
105-
106-
if (!newProperty || !containsOnlyLettersAndUnderscores(newProperty)) {
107-
throw Error(`Error in conversion rule "${conversionRule}": new property may not be empty and only contain letters and underscores.`);
108-
}
109-
110-
this.property = property;
111-
this.newProperty = newProperty;
112-
113-
*/
11494
}
11595

11696
validate(): { res: boolean, err?: Error } {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<script lang="ts">
2+
import {PropertyMappingModel, PropertyMappingOption, propertyMappingOptions} from './PropertyMapping';
3+
import {capitalizeFirstLetter} from '../utils/Utils';
4+
import Icon from './Icon.svelte';
5+
6+
export let model: PropertyMappingModel;
7+
export let save: (model: PropertyMappingModel) => void;
8+
9+
let validationResult: { res: boolean, err?: Error };
10+
11+
$: modelChanged(model);
12+
13+
function modelChanged(model: PropertyMappingModel) {
14+
validationResult = model.validate();
15+
}
16+
</script>
17+
18+
<style>
19+
20+
</style>
21+
22+
<div class="media-db-plugin-property-mappings-model-container">
23+
<div class="setting-item-name">{capitalizeFirstLetter(model.type)}</div>
24+
<div class="media-db-plugin-property-mappings-container">
25+
{ #each model.properties as property }
26+
<div class="media-db-plugin-property-mapping-element">
27+
<div class="media-db-plugin-property-mapping-element-property-name-wrapper">
28+
<pre
29+
class="media-db-plugin-property-mapping-element-property-name"><code>{property.property}</code></pre>
30+
</div>
31+
{ #if property.locked }
32+
<div class="media-db-plugin-property-binding-text">
33+
property can not be remapped
34+
</div>
35+
{ :else }
36+
<select class="dropdown" bind:value={property.mapping}>
37+
{#each propertyMappingOptions as remappingOption}
38+
<option value={remappingOption}>
39+
{remappingOption}
40+
</option>
41+
{/each}
42+
</select>
43+
44+
{ #if property.mapping === PropertyMappingOption.Map }
45+
<Icon iconName="arrow-right"/>
46+
<div class="media-db-plugin-property-mapping-to">
47+
<input type="text" spellcheck="false" bind:value="{property.newProperty}">
48+
</div>
49+
{ /if }
50+
{ /if }
51+
</div>
52+
{ /each }
53+
</div>
54+
{ #if !validationResult?.res }
55+
<div class="media-db-plugin-property-mapping-validation">
56+
{validationResult?.err?.message}
57+
</div>
58+
{ /if }
59+
<button
60+
class="media-db-plugin-property-mappings-save-button {validationResult?.res ? 'mod-cta' : 'mod-muted'}"
61+
on:click={() => { if(model.validate().res) save(model) }}>Save
62+
</button>
63+
</div>

0 commit comments

Comments
 (0)