Skip to content

Commit d0bdbbb

Browse files
committed
Merge branch 'test'
2 parents bfcddee + 383e10b commit d0bdbbb

File tree

4 files changed

+38
-19
lines changed

4 files changed

+38
-19
lines changed

exampleVault/.obsidian/plugins/obsidian-meta-bind-plugin/main.js

Lines changed: 16 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exampleVault/examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: test title test test
44
completed: false
55
toggle1: false
66
slider1: 7
7-
slider2: 6
7+
slider2: 4
88
text1: Test text
99
text_area1: Test test
1010
date1: 2022-05-28

exampleVault/other note.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
title: asdasds sdf sdsdlkfnosdijn sdaskdjasbokbfosdasdasdasd asd asdasda f
2+
title: asdasds sdf sasdfnosdijn sdaskdjasbokbfosdasdasdasd asd asdasda f
33
select: option b
44
multi-select:
55
- option b
66
- option c
7-
date: Wednesday, September 19th 2029
7+
date: Monday, June 4th 2029
88
time: 19:20
99
---

src/main.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {parseYaml, Plugin, stringifyYaml, TFile} from 'obsidian';
1+
import {CachedMetadata, FrontMatterCache, parseYaml, Plugin, stringifyYaml, TFile} from 'obsidian';
22
import {DEFAULT_SETTINGS, MetaBindPluginSettings, MetaBindSettingTab} from './settings/Settings';
33
import {InputFieldMarkdownRenderChild, InputFieldMarkdownRenderChildType} from './InputFieldMarkdownRenderChild';
44
import {getFileName, isPath, removeFileEnding} from './utils/Utils';
@@ -64,11 +64,9 @@ export default class MetaBindPlugin extends Plugin {
6464
}
6565
});
6666

67-
this.registerEvent(this.app.vault.on('modify', async abstractFile => {
68-
if (abstractFile instanceof TFile) {
69-
await this.updateMarkdownInputFieldsOnFileChange(abstractFile);
70-
}
71-
}));
67+
this.app.metadataCache.on('changed', async (file: TFile, data: string, cache: CachedMetadata) => {
68+
await this.updateMarkdownInputFieldsOnMetadataCacheChange(file, cache);
69+
});
7270

7371
this.addSettingTab(new MetaBindSettingTab(this.app, this));
7472
}
@@ -87,7 +85,7 @@ export default class MetaBindPlugin extends Plugin {
8785
this.activeMarkdownInputFields = this.activeMarkdownInputFields.filter(x => x.uid !== inputFieldMarkdownRenderChild.uid);
8886
}
8987

90-
async updateMarkdownInputFieldsOnFileChange(file: TFile): Promise<void> {
88+
async updateMarkdownInputFieldsOnMetadataCacheChange(file: TFile, cache: CachedMetadata): Promise<void> {
9189
let metadata: any = undefined;
9290

9391
for (const activeMarkdownInputField of this.activeMarkdownInputFields) {
@@ -97,7 +95,7 @@ export default class MetaBindPlugin extends Plugin {
9795

9896
if (activeMarkdownInputField.bindTargetFile.path === file.path) {
9997
if (metadata === undefined) {
100-
metadata = await this.getMetaDataForFile(file);
98+
metadata = await this.getMetadataFromFileCache(cache.frontmatter);
10199
}
102100
activeMarkdownInputField.updateValue(metadata[activeMarkdownInputField.bindTargetMetadataField]);
103101
}
@@ -192,6 +190,19 @@ export default class MetaBindPlugin extends Plugin {
192190
return metadata;
193191
}
194192

193+
getMetadataFromFileCache(cache: FrontMatterCache | undefined) {
194+
let metadata = cache as object;
195+
196+
if (metadata) {
197+
metadata = Object.assign({}, metadata); // copy
198+
// @ts-ignore
199+
delete metadata.position;
200+
} else {
201+
metadata = {};
202+
}
203+
return metadata;
204+
}
205+
195206
async loadSettings(): Promise<void> {
196207
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
197208
}

0 commit comments

Comments
 (0)