-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathInputField.ts
More file actions
146 lines (123 loc) · 3.85 KB
/
InputField.ts
File metadata and controls
146 lines (123 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {MarkdownRenderChild, SliderComponent, TextComponent, TFile, ToggleComponent} from 'obsidian';
import MetaBindPlugin from './main';
export class InputField extends MarkdownRenderChild {
plugin: MetaBindPlugin;
metaData: any;
error: string;
declaration: string;
inputFieldType: string;
isBound: boolean;
boundMetadataField: string;
file: TFile;
limitInterval: NodeJS.Timer;
intervalCounter: number;
valueQueue: any[];
constructor(containerEl: HTMLElement, fullDeclaration: string, plugin: MetaBindPlugin, filePath: string) {
super(containerEl);
//console.log(this, 2)
this.error = '';
this.declaration = fullDeclaration;
this.plugin = plugin;
this.valueQueue = [];
this.intervalCounter = 0;
this.limitInterval = setInterval(this.incrementInterval.bind(this), 10);
const regExp = new RegExp(/\[.*?\]/);
let declaration = regExp.exec(fullDeclaration)[0];
declaration = declaration.replace('[', '').replace(']', '');
let declarationParts: string[] = declaration.split(':');
let boundTo: string = declarationParts[1] ?? '';
this.isBound = !!boundTo;
this.inputFieldType = declarationParts[0].toLowerCase();
if (this.isBound) {
let boundToParts = boundTo.split('#');
if (boundToParts.length === 1) { // same file
this.boundMetadataField = boundTo;
const files = plugin.getFilesByName(filePath);
if (files.length === 0) {
this.error = 'file not fond.';
return;
} else if (files.length === 1) {
this.file = files[0];
} else {
this.error = 'multiple files found. please specify the file path.';
return;
}
} else if (boundToParts.length === 2) {
this.boundMetadataField = boundToParts[1];
const files = plugin.getFilesByName(boundToParts[0]);
if (files.length === 0) {
this.error = 'file not fond.';
return;
} else if (files.length === 1) {
this.file = files[0];
} else {
this.error = 'multiple files found. please specify the file path.';
return;
}
} else {
this.error = 'invalid binding.';
return;
}
this.metaData = plugin.getMetaDataForFile(this.file);
}
// console.log(this, 3)
}
// use this interval to reduce writing operations
async incrementInterval() {
this.intervalCounter += 1;
if (this.intervalCounter >= 20 && this.valueQueue.length > 0) {
// console.log(this.valueQueue.at(-1))
await this.plugin.updateMetaData(this.boundMetadataField, this.valueQueue.at(-1), this.file);
this.valueQueue = [];
this.intervalCounter = 0;
}
}
async updateMetaData(value: any) {
if (this.isBound) {
this.valueQueue.push(value);
}
}
getInitialValue() {
// console.log(this);
if (this.isBound) {
return this.metaData[this.boundMetadataField];
}
}
onload() {
//console.log('load', this);
const container = this.containerEl.createDiv();
container.addClass('meta-bind-plugin-input-wrapper');
if (this.error) {
container.innerText = `Error: ${this.error}`;
container.addClass('meta-bind-plugin-error');
this.containerEl.appendChild(container);
return;
}
if (this.inputFieldType === 'toggle') {
const newEl = new ToggleComponent(container);
newEl.setValue(this.getInitialValue());
newEl.onChange(async (value) => {
await this.updateMetaData(value);
});
} else if (this.inputFieldType === 'slider') {
const newEl = new SliderComponent(container);
newEl.setValue(this.getInitialValue());
newEl.onChange(async (value) => {
await this.updateMetaData(value);
});
} else if (this.inputFieldType === 'text') {
const newEl = new TextComponent(container);
newEl.setValue(this.getInitialValue());
newEl.onChange(async (value) => {
await this.updateMetaData(value);
});
}
this.containerEl.empty();
this.containerEl.appendChild(container);
}
onunload() {
super.onunload();
//console.log('unload', this);
clearInterval(this.limitInterval);
}
}