DataviewJS + Modal Form Plugin + a Smallish Script = Auto-Create a Property-Editing Form...1 time!

Hi,

If you have the plugins for Dataview and Modal Form enabled, you should be able to copy-paste the following code into an Obsidian file with manually created Properties, click the button, and get a Modal Form to open that allows you to edit all the properties, without further ado!

However, there’s a bug and I’d love help on it.

The form only allows you to edit the “tags” or “aliases” property 1 time. Experiment by adding a tag, and then editing the form twice. You’ll see the problem. I believe the error in my code is how I’m processing “splitrvalue”, below, but I’m too tired to have ideas on how to fix it tonight, and AI isn’t being much help. So, fellow humans, any ideas? :smiley:

	const button = dv.el('button', 'Update this Record');

button.onclick = async () => {	
	const file = app.vault.getAbstractFileByPath(dv.current().file.path); 
	const metadata = app.metadataCache.getFileCache(file); 
	const frontmatter = metadata.frontmatter;
	const allPropertiesOfAllPropertiesInVault = Object.getOwnPropertyDescriptors(app.metadataCache.app.metadataTypeManager.properties);
	let fieldsForModalForm = [];
	let values = {};
	for (const [key1, value1] of Object.entries(frontmatter)) {
		for (const [key2, dict2] of Object.entries(allPropertiesOfAllPropertiesInVault)){			
			if (key1.toLowerCase() === key2){
				const type2 = dict2.value.type;
				switch(type2){
					case 'aliases':
						fieldsForModalForm.push({
							name: key1, 
							label: key1,
							description: key1,
							input: { 
								type: "multiselect",
								source: "dataview",
								query: "[]",
								allowUnknownValues: "true",
								multi_select_options: []
								}
						});
						value1 ? values[key1]=value1 : {};
						break;					 
					case 'checkbox':
						fieldsForModalForm.push({
							name: key1, 
							label: key1,
							description: key1,
							input: { type: "toggle" }
						});
						value1 ? values[key1]=value1 : {};
						break;
					case 'date':
						fieldsForModalForm.push({
							name: key1, 
							label: key1,
							description: key1,
							input: { type: "date" }
						});
						value1 ? values[key1]=value1 : {};
						break;
					case 'datetime':
						fieldsForModalForm.push({
							name: key1, 
							label: key1,
							description: key1,
							input: { type: "datetime" }
						});
						value1 ? values[key1]=value1 : {};
						break;
					case 'number':
						fieldsForModalForm.push({
							name: key1, 
							label: key1,
							description: key1,
							input: { type: "number" }
						});
						value1 ? values[key1]=value1 : {};
						break;
					case 'tags':
						fieldsForModalForm.push({
							name: key1, 
							label: key1,
							description: key1,
							input: { 
								type: "multiselect",
								source: "dataview",
								query: "[]",
								allowUnknownValues: "true",
								multi_select_options: []
								}
						});
						value1 ? values[key1]=value1 : {};
						break;
					case 'text':
						fieldsForModalForm.push({
							name: key1, 
							label: key1,
							description: key1,
							input: { type: "text", value: value1 }
						});
						value1 ? values[key1]=value1 : {};
						break;
					case 'multitext':
						fieldsForModalForm.push({
							name: key1, 
							label: key1,
							description: key1,
							input: { 
								type: "multiselect",
								source: "dataview",
								query: "[]",
								allowUnknownValues: "true",
								multi_select_options: []
								}
						});
						value1 ? values[key1]=value1 : {};
						break;
					default:
						console.log("ERROR:\t" + key1 + "\t" + value1 + "\t" + type2);
						break;
				}
			}
		}
	}

	const modalForm = app.plugins.plugins.modalforms.api;
	const result = await modalForm.openForm({fields: fieldsForModalForm}, {values: values});
	console.log(result.asDataviewProperties());
	const splitResults = result.asDataviewProperties().split("\n").forEach( (r)=>{
		let splitr = r.split(":: ");
		let splitrvalue = splitr[1];
		if( splitr[1] !== "" ){
			splitrvalue = splitr[1].replace(/"/g, '');			
			if (splitr[1] === "true"){ splitrvalue = true; }
			if (splitr[1] === "false"){ splitrvalue = false; }			
			app.fileManager.processFrontMatter(file, frontmatter => {  
				frontmatter[splitr[0]] = splitrvalue;
			});		
		}
	});	
};

dv.table(
	["Column 1","Buttons"], 
	[["Bar",button]]
);

1 Like