|
| 1 | +import {MarkdownRenderChild, TFile} from 'obsidian'; |
| 2 | +import MetaBindPlugin from './main'; |
| 3 | +import {Logger} from './utils/Logger'; |
| 4 | +import {AbstractInputField} from './inputFields/AbstractInputField'; |
| 5 | +import {InputFieldFactory, InputFieldType} from './inputFields/InputFieldFactory'; |
| 6 | + |
| 7 | +export class InputFieldMarkdownRenderChild extends MarkdownRenderChild { |
| 8 | + plugin: MetaBindPlugin; |
| 9 | + metaData: any; |
| 10 | + filePath: string; |
| 11 | + uid: number; |
| 12 | + inputField: AbstractInputField; |
| 13 | + error: string; |
| 14 | + |
| 15 | + fullDeclaration: string; |
| 16 | + inputFieldType: InputFieldType; |
| 17 | + isBound: boolean; |
| 18 | + bindTargetMetadataField: string; |
| 19 | + file: TFile; |
| 20 | + |
| 21 | + limitInterval: number; |
| 22 | + intervalCounter: number; |
| 23 | + valueQueue: any[]; |
| 24 | + |
| 25 | + arguments: { name: string, value: any }[]; |
| 26 | + |
| 27 | + constructor(containerEl: HTMLElement, fullDeclaration: string, plugin: MetaBindPlugin, filePath: string, uid: number) { |
| 28 | + super(containerEl); |
| 29 | + |
| 30 | + //console.log(this, 2) |
| 31 | + |
| 32 | + this.error = ''; |
| 33 | + this.fullDeclaration = fullDeclaration; |
| 34 | + this.filePath = filePath; |
| 35 | + this.uid = uid; |
| 36 | + this.plugin = plugin; |
| 37 | + |
| 38 | + this.valueQueue = []; |
| 39 | + this.intervalCounter = 0; |
| 40 | + this.limitInterval = window.setInterval(() => this.incrementInterval(), this.plugin.settings.syncInterval); |
| 41 | + |
| 42 | + try { |
| 43 | + this.parseDeclaration(); |
| 44 | + |
| 45 | + this.inputField = InputFieldFactory.createInputField(this.inputFieldType, { |
| 46 | + inputFieldMarkdownRenderChild: this, |
| 47 | + onValueChanged: this.updateMetaData.bind(this), |
| 48 | + }); |
| 49 | + } catch (e) { |
| 50 | + this.error = e.message; |
| 51 | + } |
| 52 | + |
| 53 | + // console.log(this, 3) |
| 54 | + } |
| 55 | + |
| 56 | + parseDeclaration() { |
| 57 | + const declarationRegExp: RegExp = new RegExp(/\[.*?\]/); |
| 58 | + let declaration: string = declarationRegExp.exec(this.fullDeclaration)[0]; |
| 59 | + declaration = declaration.replace('[', '').replace(']', ''); |
| 60 | + let declarationParts: string[] = declaration.split(':'); |
| 61 | + |
| 62 | + let boundTo: string = declarationParts[1] ?? ''; |
| 63 | + this.isBound = !!boundTo; |
| 64 | + |
| 65 | + let inputFieldTypeWithArguments: string = declarationParts[0]; |
| 66 | + const inputFieldArgumentsRegExp: RegExp = new RegExp(/\(.*\)/); |
| 67 | + const inputFieldTypeString = inputFieldTypeWithArguments.replace(inputFieldArgumentsRegExp, ''); |
| 68 | + this.inputFieldType = InputFieldFactory.getInputFieldType(inputFieldTypeString); |
| 69 | + if (this.inputFieldType === InputFieldType.INVALID) { |
| 70 | + throw Error(`Invalid input field type \'${inputFieldTypeString}\'`); |
| 71 | + } |
| 72 | + |
| 73 | + this.arguments = []; |
| 74 | + let inputFieldArgumentsRegExpResult = inputFieldArgumentsRegExp.exec(inputFieldTypeWithArguments); |
| 75 | + let inputFieldArgumentsString = inputFieldArgumentsRegExpResult ? inputFieldArgumentsRegExpResult[0] : ''; |
| 76 | + if (inputFieldArgumentsString) { |
| 77 | + this.parseArguments(inputFieldArgumentsString); |
| 78 | + } |
| 79 | + |
| 80 | + if (this.isBound) { |
| 81 | + this.parseBindTarget(boundTo); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + parseArguments(inputFieldArgumentsString: string) { |
| 86 | + const inputFieldArgumentsRegExp: RegExp = new RegExp(/\(.*\)/); |
| 87 | + |
| 88 | + inputFieldArgumentsString = inputFieldArgumentsString.substring(1, inputFieldArgumentsString.length - 1); |
| 89 | + let inputFieldArguments: string[] = inputFieldArgumentsString.split(','); |
| 90 | + |
| 91 | + inputFieldArguments = inputFieldArguments.map(x => x.trim()); |
| 92 | + for (const inputFieldArgument of inputFieldArguments) { |
| 93 | + if (inputFieldArgument.startsWith('class')) { |
| 94 | + let classArgumentsString: string = inputFieldArgumentsRegExp.exec(inputFieldArgument)[0]; |
| 95 | + if (!classArgumentsString && classArgumentsString.length >= 2) { |
| 96 | + throw new Error('class needs an argument'); |
| 97 | + } |
| 98 | + classArgumentsString = classArgumentsString.substring(1, classArgumentsString.length - 1); |
| 99 | + if (!classArgumentsString) { |
| 100 | + throw new Error('class argument can not be empty'); |
| 101 | + } |
| 102 | + |
| 103 | + let inputFieldStyleArgument: { name: string, value: string } = {name: 'class', value: classArgumentsString}; |
| 104 | + |
| 105 | + this.arguments.push(inputFieldStyleArgument); |
| 106 | + } |
| 107 | + |
| 108 | + if (inputFieldArgument.startsWith('addLabels')) { |
| 109 | + this.arguments.push({name: 'labels', value: true}); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + parseBindTarget(bindTarget: string) { |
| 115 | + let bindTargetParts = bindTarget.split('#'); |
| 116 | + if (bindTargetParts.length === 1) { // same file |
| 117 | + this.bindTargetMetadataField = bindTarget; |
| 118 | + const files = this.plugin.getFilesByName(this.filePath); |
| 119 | + if (files.length === 0) { |
| 120 | + this.error = 'file not fond.'; |
| 121 | + return; |
| 122 | + } else if (files.length === 1) { |
| 123 | + this.file = files[0]; |
| 124 | + } else { |
| 125 | + throw new Error('multiple files found. please specify the file path.'); |
| 126 | + } |
| 127 | + } else if (bindTargetParts.length === 2) { |
| 128 | + this.bindTargetMetadataField = bindTargetParts[1]; |
| 129 | + const files = this.plugin.getFilesByName(bindTargetParts[0]); |
| 130 | + if (files.length === 0) { |
| 131 | + this.error = 'file not fond.'; |
| 132 | + return; |
| 133 | + } else if (files.length === 1) { |
| 134 | + this.file = files[0]; |
| 135 | + } else { |
| 136 | + throw new Error('multiple files found. please specify the file path.'); |
| 137 | + } |
| 138 | + } else { |
| 139 | + throw new Error('invalid binding.'); |
| 140 | + } |
| 141 | + this.metaData = this.plugin.getMetaDataForFile(this.file); |
| 142 | + } |
| 143 | + |
| 144 | + // use this interval to reduce writing operations |
| 145 | + async incrementInterval() { |
| 146 | + if (this.valueQueue.length > 0) { |
| 147 | + // console.log(this.valueQueue.at(-1)) |
| 148 | + await this.plugin.updateMetaData(this.bindTargetMetadataField, this.valueQueue.at(-1), this.file); |
| 149 | + this.valueQueue = []; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + async updateMetaData(value: any) { |
| 154 | + if (this.isBound) { |
| 155 | + this.valueQueue.push(value); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + updateValue(value: any) { |
| 160 | + if (value != null && this.inputField.getValue() !== value && this.valueQueue.length === 0) { |
| 161 | + Logger.logDebug(`updating input field ${this.uid} to '${value.toString()}'`); |
| 162 | + this.inputField.setValue(value); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + getInitialValue() { |
| 167 | + // console.log(this); |
| 168 | + if (this.isBound) { |
| 169 | + return this.metaData[this.bindTargetMetadataField]; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + getArgument(name: string) { |
| 174 | + return this.arguments.filter(x => x.name === name).first(); |
| 175 | + } |
| 176 | + |
| 177 | + async onload() { |
| 178 | + Logger.logDebug(this); |
| 179 | + |
| 180 | + this.metaData = await this.metaData; |
| 181 | + |
| 182 | + const container = this.containerEl.createDiv(); |
| 183 | + container.addClass('meta-bind-plugin-input-wrapper'); |
| 184 | + |
| 185 | + if (this.error) { |
| 186 | + container.innerText = ` -> Error: ${this.error}`; |
| 187 | + container.addClass('meta-bind-plugin-error'); |
| 188 | + this.containerEl.appendChild(container); |
| 189 | + return; |
| 190 | + } |
| 191 | + |
| 192 | + this.plugin.registerMarkdownInputField(this); |
| 193 | + |
| 194 | + this.inputField.render(container); |
| 195 | + |
| 196 | + const classArgument = this.getArgument('class'); |
| 197 | + if (classArgument) { |
| 198 | + this.inputField.getHtmlElement().addClass(classArgument.value); |
| 199 | + } |
| 200 | + |
| 201 | + this.containerEl.empty(); |
| 202 | + this.containerEl.appendChild(container); |
| 203 | + } |
| 204 | + |
| 205 | + onunload() { |
| 206 | + this.plugin.unregisterMarkdownInputField(this); |
| 207 | + |
| 208 | + super.onunload(); |
| 209 | + |
| 210 | + //console.log('unload', this); |
| 211 | + window.clearInterval(this.limitInterval); |
| 212 | + } |
| 213 | +} |
0 commit comments