Skip to content

Commit 4627443

Browse files
committed
Fix breaking bc of multiple files with same name
1 parent e96db5a commit 4627443

File tree

4 files changed

+97
-30
lines changed

4 files changed

+97
-30
lines changed

src/InputField.ts

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,84 @@ export class InputField extends MarkdownRenderChild {
1212
boundMetadataField: string;
1313
file: TFile;
1414

15+
limitInterval: NodeJS.Timer;
16+
intervalCounter: number;
17+
valueQueue: any[];
18+
1519
constructor(containerEl: HTMLElement, fullDeclaration: string, plugin: MetaBindPlugin, filePath: string) {
1620
super(containerEl);
1721

1822
//console.log(this, 2)
1923

2024
this.error = '';
25+
this.declaration = fullDeclaration;
26+
this.plugin = plugin;
27+
28+
this.valueQueue = [];
29+
this.intervalCounter = 0;
30+
this.limitInterval = setInterval(this.incrementInterval.bind(this), 10);
2131

2232
const regExp = new RegExp(/\[.*?\]/);
2333
let declaration = regExp.exec(fullDeclaration)[0];
2434
declaration = declaration.replace('[', '').replace(']', '');
25-
2635
let declarationParts: string[] = declaration.split(':');
2736
let boundTo: string = declarationParts[1] ?? '';
37+
2838
this.isBound = !!boundTo;
39+
this.inputFieldType = declarationParts[0].toLowerCase();
40+
2941
if (this.isBound) {
3042
let boundToParts = boundTo.split('#');
3143
if (boundToParts.length === 1) { // same file
3244
this.boundMetadataField = boundTo;
33-
this.file = plugin.getFileByName(filePath);
45+
const files = plugin.getFilesByName(filePath);
46+
if (files.length === 0) {
47+
this.error = 'file not fond.';
48+
return;
49+
} else if (files.length === 1) {
50+
this.file = files[0];
51+
} else {
52+
this.error = 'multiple files found. please specify the file path.';
53+
return;
54+
}
3455
} else if (boundToParts.length === 2) {
3556
this.boundMetadataField = boundToParts[1];
36-
this.file = plugin.getFileByName(boundToParts[0]);
57+
const files = plugin.getFilesByName(boundToParts[0]);
58+
if (files.length === 0) {
59+
this.error = 'file not fond.';
60+
return;
61+
} else if (files.length === 1) {
62+
this.file = files[0];
63+
} else {
64+
this.error = 'multiple files found. please specify the file path.';
65+
return;
66+
}
3767
} else {
38-
this.error = 'invalid binding';
68+
this.error = 'invalid binding.';
69+
return;
3970
}
40-
4171
this.metaData = plugin.getMetaDataForFile(this.file);
4272
}
4373

44-
this.declaration = fullDeclaration;
45-
this.inputFieldType = declarationParts[0].toLowerCase();
46-
this.plugin = plugin;
47-
4874
// console.log(this, 3)
4975
}
5076

77+
// use this interval to reduce writing operations
78+
async incrementInterval() {
79+
this.intervalCounter += 1;
80+
81+
if (this.intervalCounter >= 20 && this.valueQueue.length > 0) {
82+
// console.log(this.valueQueue.at(-1))
83+
await this.plugin.updateMetaData(this.boundMetadataField, this.valueQueue.at(-1), this.file);
84+
this.valueQueue = [];
85+
this.intervalCounter = 0;
86+
}
87+
88+
}
89+
5190
async updateMetaData(value: any) {
5291
if (this.isBound) {
53-
await this.plugin.updateMetaData(this.boundMetadataField, value, this.file);
92+
this.valueQueue.push(value);
5493
}
5594
}
5695

@@ -61,14 +100,15 @@ export class InputField extends MarkdownRenderChild {
61100
}
62101

63102
onload() {
64-
//console.log(this, 1)
103+
console.log('load', this);
65104

66105
const container = this.containerEl.createDiv();
67106
container.addClass('meta-bind-plugin-input-wrapper');
68107

69108
if (this.error) {
70-
container.innerText = ` \`Error ${this.error}\``;
71-
this.containerEl.replaceWith(container);
109+
container.innerText = `Error: ${this.error}`;
110+
container.addClass('meta-bind-plugin-error');
111+
this.containerEl.appendChild(container);
72112
return;
73113
}
74114

@@ -92,6 +132,14 @@ export class InputField extends MarkdownRenderChild {
92132
});
93133
}
94134

95-
this.containerEl.replaceWith(container);
135+
this.containerEl.empty();
136+
this.containerEl.appendChild(container);
137+
}
138+
139+
onunload() {
140+
super.onunload();
141+
142+
console.log('unload', this);
143+
clearInterval(this.limitInterval);
96144
}
97145
}

src/Utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ export function getFileName(path: string) {
22
return path.split('/').at(-1);
33
}
44

5+
export function isPath(path: string) {
6+
return path.split('/').length > 1;
7+
}
8+
59
export function removeFileEnding(fileName: string) {
610
const fileNameParts = fileName.split('.');
711
if (fileNameParts.length === 1) {

src/main.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Notice, Plugin, stringifyYaml, TFile} from 'obsidian';
22
import {DEFAULT_SETTINGS, MetaBindPluginSettings, MetaBindSettingTab} from './settings/Settings';
33
import {InputField} from './InputField';
4-
import {getFileName, removeFileEnding} from './Utils';
4+
import {getFileName, isPath, removeFileEnding} from './Utils';
55

66
export default class MetaBindPlugin extends Plugin {
77
settings: MetaBindPluginSettings;
@@ -16,14 +16,14 @@ export default class MetaBindPlugin extends Plugin {
1616

1717

1818
this.registerMarkdownPostProcessor((element, context) => {
19-
const codeblocks = element.querySelectorAll('code');
20-
for (let index = 0; index < codeblocks.length; index++) {
21-
const codeblock = codeblocks.item(index);
22-
const text = codeblock.innerText;
23-
const isEmoji = text.startsWith('INPUT[') && text.endsWith(']');
19+
const codeBlocks = element.querySelectorAll('code');
20+
for (let index = 0; index < codeBlocks.length; index++) {
21+
const codeBlock = codeBlocks.item(index);
22+
const text = codeBlock.innerText;
23+
const isInputField = text.startsWith('INPUT[') && text.endsWith(']');
2424
// console.log(context.sourcePath);
25-
if (isEmoji) {
26-
context.addChild(new InputField(codeblock, text, this, context.sourcePath));
25+
if (isInputField) {
26+
context.addChild(new InputField(codeBlock, text, this, context.sourcePath));
2727
}
2828
}
2929
});
@@ -40,8 +40,10 @@ export default class MetaBindPlugin extends Plugin {
4040
}
4141

4242
async updateMetaData(key: string, value: any, file: TFile) {
43+
// console.log('update', key, value);
44+
4345
if (!file) {
44-
console.log('file not found');
46+
console.log('no file');
4547
return;
4648
}
4749

@@ -60,23 +62,32 @@ export default class MetaBindPlugin extends Plugin {
6062
await this.app.vault.modify(file, fileContent);
6163
}
6264

63-
getFileByName(name: string): TFile {
65+
getFilesByName(name: string): TFile[] {
6466
// console.log(getFileName(removeFileEnding(name)))
65-
const files = this.app.vault.getFiles();
66-
for (const file of files) {
67-
// console.log(getFileName(removeFileEnding(file.name)));
68-
if (getFileName(removeFileEnding(file.name)) === getFileName(removeFileEnding(name))) {
69-
return file;
67+
const allFiles = this.app.vault.getFiles();
68+
const files: TFile[] = [];
69+
for (const file of allFiles) {
70+
// console.log(removeFileEnding(file.path));
71+
if (isPath(name)) {
72+
if (removeFileEnding(file.path) === removeFileEnding(name)) {
73+
files.push(file);
74+
}
75+
} else {
76+
if (getFileName(removeFileEnding(file.name)) === getFileName(removeFileEnding(name))) {
77+
files.push(file);
78+
}
7079
}
7180
}
7281

73-
return null;
82+
return files;
7483
}
7584

7685
getMetaDataForFile(file: TFile): any {
7786
let metadata: any;
7887
try {
7988
metadata = this.app.metadataCache.getFileCache(file).frontmatter;
89+
metadata = JSON.parse(JSON.stringify(metadata)); // deep copy
90+
// console.log(metadata);
8091
} catch (e) {
8192
new Notice('Waring: ' + e.toString());
8293
return;

styles.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
.meta-bind-plugin-input-wrapper .checkbox-container {
66
vertical-align: text-bottom;
77
}
8+
9+
.meta-bind-plugin-error {
10+
color: red;
11+
}

0 commit comments

Comments
 (0)