danielo515 / obsidian-modal-form

Define forms for filling data that you will be able to open from anywhere you can run JS
https://danielorodriguez.com/obsidian-modal-form/
MIT License
187 stars 14 forks source link

Inline Forms - Either add functionality to create more complex inline forms, or create documentation with more complex examples? Btw, thank you for MF! #234

Open ekbl1 opened 5 months ago

ekbl1 commented 5 months ago

Is your feature request related to a problem? Please describe. Hi, I am running the code below (scroll to the bottom, plz) in a dataviewJS block inside of a file with some properties, trying to open an MF with the properties of my file populated in it.

Describe the solution you'd like

Additional context

const button = dv.el('button', 'Click me');

button.onclick = async () => {
    //const tp = app.plugins.plugins["templater-obsidian"].templater.current_functions_object;
    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.types);
    let fieldsForModalForm = [];
    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':
                        //console.log("aliases:\t" + key1 + "\t" + value1 + "\t" + type2);
                        fieldsForModalForm.push({
                            name: key1, 
                            label: key1,
                            description: key1,
                            input: { type: "aliases",
                                     value: value1
                                    }
                        });
                        break;                   
                    case 'checkbox':
                        //console.log("checkbox:\t" + key1 + "\t" + value1 + "\t" + type2);
                        fieldsForModalForm.push({
                            name: key1, 
                            label: key1,
                            description: key1,
                            input: { type: "toggle",
                                     value: value1
                                    }
                        });
                        break;
                    case 'date':
                        //console.log("date:\t" + key1 + "\t" + value1 + "\t" + type2);
                        fieldsForModalForm.push({
                            name: key1, 
                            label: key1,
                            description: key1,
                            input: { type: "date",
                                     value: value1
                                    }
                        });
                        break;
                    case 'datetime':
                        //console.log("datetime:\t" + key1 + "\t" + value1 + "\t" + type2);
                        fieldsForModalForm.push({
                            name: key1, 
                            label: key1,
                            description: key1,
                            input: { type: "datetime",
                                     value: value1
                                    }
                        });
                        break;
                    case 'number':
                        //console.log("number:\t" + key1 + "\t" + value1 + "\t" + type2);
                        fieldsForModalForm.push({
                            name: key1, 
                            label: key1,
                            description: key1,
                            input: { type: "number",
                                     value: value1
                                    }
                        });
                        break;
                    case 'tags':
                        //console.log("tags:\t" + key1 + "\t" + value1 + "\t" + type2);
                        fieldsForModalForm.push({
                            name: key1, 
                            label: key1,
                            description: key1,
                            input: { type: "tags",
                                     value: value1
                                    }
                        });
                        break;
                    case 'text':
                        //console.log("text:\t" + key1 + "\t" + value1 + "\t" + type2);
                        fieldsForModalForm.push({
                            name: key1, 
                            label: key1,
                            description: key1,
                            input: { type: "text",
                                     value: value1
                                    }
                        });
                        break;
                    case 'multitext':
                        //console.log("multitext:\t" + key1 + "\t" + value1 + "\t" + type2);
                        fieldsForModalForm.push({
                            name: key1, 
                            label: key1,
                            description: key1,
                            input: { type: "multitext",
                                     value: value1
                                    }
                        });
                        break;
                    default:
                        console.log("ERROR:\t" + key1 + "\t" + value1 + "\t" + type2);
                        break;
                }
            }
        }
    }
    console.log(fieldsForModalForm);
    const modalForm = app.plugins.plugins.modalforms.api;
    const result = await modalForm.openForm({
        title: file.title,
        fields: fieldsForModalForm
    });
};

dv.table(
    ["Column 1","Buttons"], 
    [["Bar",button]]
);
danielo515 commented 5 months ago

Something you can do to validate your format is to console log the whole object, and then go to the import form which will give you detailed errors of which fields are wrong and why.

Screenshot 2024-03-11 at 16 39 00

Make sure to log it as JSON or the importer will not be able to validate it:

console.log(JSON.stringify({
        title: file.title,
        fields: fieldsForModalForm
    }))