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

[Feature request] Return File extension of file selected by using Select Option #235

Open dvdmtw98 opened 5 months ago

dvdmtw98 commented 5 months ago

Is your feature request related to a problem? Please describe. I want to use Modal Form to have a user select a image which will then be embedded in the file. I have used the Select Field to limit the folders from which user can select the images. The image files show up in the drop down but the captured value does not contain the files extension because of this I cannot use the file name directly to create a image embed.

Describe the solution you'd like The Result Object could have a new property called extension which has the extension of the file that was selected by the user. A properly that has the filename along with the extension will also be fine.

Additional context

Since Modal Forms does not return extensions I have to use a hacky solution to be able to embed image.

const modalForm = app.plugins.plugins.modalforms.api;
const result = await modalForm.openForm('book-form');

const imageExtensions = [".jpg", ".jpeg", ".png", ".webp"]
const imageName = result.getValue('cover-image').value;
let imageFilename = "";

for (let extension of imageExtensions) {
    let match = tp.file.find_tfile(imageName + extension);
    if (match) {
        imageFilename = imageName + extension;
        break;
    }
}

![[<% imageFilename %>|300]]
danielo515 commented 5 months ago

This is a use-case I didn't envisioned at first, but it makes sense. I wanted to enhance the file input output for some time now, this is just another improvement to add to the list. Maybe a dedicated image input will make sense, but that could be confusing because I think people will think it could be used to import images...

dvdmtw98 commented 5 months ago

If a new input for images is added then I feel a additional input type will have to be created for documents like PDF as well since they can also be embedded.

One way to implement it could be to ask the user to define the type of files that will be returned by the input field. So when user selects type as images the field will only list the images. When user defines the type as documents files like PDF will be returned.

Either way as long as the extension is returned in the ResultSet I have no problem. Maybe the path to the file could also be added as a property which could be useful for certain workflows.