mirror of
https://github.com/broekema41/obsidian-vcf-contacts.git
synced 2026-07-22 05:42:58 +00:00
Refs #17 a logcal change to allow for testabillity of the new features separately
This commit is contained in:
parent
dbe12e89b3
commit
87082885cc
3 changed files with 23 additions and 12 deletions
|
|
@ -1,9 +1,10 @@
|
|||
import { TFile } from "obsidian";
|
||||
import { updateFrontMatter } from "src/contacts";
|
||||
import { getApp } from "src/context/sharedAppContext";
|
||||
import { getSettings } from "src/context/sharedSettingsContext";
|
||||
|
||||
export async function addDefaultFields(file: TFile) {
|
||||
import { VCardForObsidianRecord } from "./shared/vcard";
|
||||
|
||||
export async function addDefaultFields(file: TFile):Promise<VCardForObsidianRecord> {
|
||||
const defaultFieldKeys = getSettings().createFieldsKeys
|
||||
const { metadataCache } = getApp();
|
||||
|
||||
|
|
@ -12,18 +13,12 @@ export async function addDefaultFields(file: TFile) {
|
|||
throw new Error('No frontmatter found.');
|
||||
}
|
||||
const record: Record<string, any> = { ...frontMatter };
|
||||
|
||||
let changed = false;
|
||||
|
||||
for (const key of defaultFieldKeys) {
|
||||
if (!(key in record)) {
|
||||
record[key] = "";
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!changed) return;
|
||||
|
||||
await updateFrontMatter(file, record);
|
||||
return record;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import myScrollTo from "src/util/myScrollTo";
|
|||
|
||||
import { ContactsPluginSettings } from './settings/settings.d';
|
||||
import {vcard} from "./contacts/vcard";
|
||||
import {updateFrontMatter} from "./contacts";
|
||||
|
||||
export default class ContactsPlugin extends Plugin {
|
||||
settings: ContactsPluginSettings;
|
||||
|
|
@ -48,13 +49,14 @@ export default class ContactsPlugin extends Plugin {
|
|||
this.addCommand({
|
||||
id: "contacts-apply-default-fields",
|
||||
name: "Apply Default Fields to Current File",
|
||||
callback: () => {
|
||||
callback: async () => {
|
||||
const file = this.app.workspace.getActiveFile();
|
||||
if (!file) {
|
||||
new Notice("No active file.");
|
||||
return;
|
||||
}
|
||||
vcard.addDefaultFields(file)
|
||||
const records = await vcard.addDefaultFields(file);
|
||||
updateFrontMatter(file, records)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -313,5 +313,19 @@ describe('vcard tostring', () => {
|
|||
|
||||
|
||||
describe('vcard addDefaultFields', () => {
|
||||
// TODO: write some tests for this.
|
||||
it('should add missing default fields while preserving existing values', async () => {
|
||||
const result = await vcard.addDefaultFields({ basename: 'base.frontmatter' } as TFile);
|
||||
|
||||
expect(result['N.GN']).toBe('Liam');
|
||||
expect(result[ 'URL[WORK]']).toBe('https://storycraft.ie/liam');
|
||||
expect(result['N.PREFIX']).toBe('');
|
||||
expect(result['N.MN']).toBe('');
|
||||
expect(result['N.SUFFIX']).toBe('');
|
||||
expect(result['TEL[HOME]']).toBe('');
|
||||
expect(result['TEL[WORK]']).toBe('');
|
||||
expect(result['EMAIL[HOME]']).toBe('liam.green@lucky.ie');
|
||||
expect(result['EMAIL[WORK]']).toBe('liam@storycraft.ie');
|
||||
expect(result['ROLE']).toBe('');
|
||||
expect(result['CATEGORIES']).toBe('Writing, Mythology');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue