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
199 stars 17 forks source link

Display File Content as Multi-Select #310

Closed LemurTech closed 1 month ago

LemurTech commented 1 month ago

Finding the plugin really useful, but struggling with one thing: how can I return the contents of a file (which is a simple text list of items) as an array to be acceptable by Modal Forms? Something like this, which doesn't work:

dv.io.load("Shared/Metadata/Lookups/Tags-Context.md").split('\n')

Metadata Menu can return the contents of a file as a list of items for multi-select fields. I was hoping to find something similar.

danielo515 commented 1 month ago

Habe you checked already the documentation for providing default values or how to use data view as a source for multi select ? If those docs don't help you, then I will try to look into your problem further: https://danielorodriguez.com/obsidian-modal-form/advanced-examples/#adding-additional-items-to-a-dataview-input

El vie, 2 ago 2024, 19:42, Matthew Arciniega @.***> escribió:

Finding the plugin really useful, but struggling with one thing: how can I return the contents of a file (which is a simple text list of items) as an array to be acceptable by Modal Forms? Something like this, which doesn't work:

dv.io.load("Shared/Metadata/Lookups/Tags-Context.md").split('\n')

Metadata Menu can return the contents of a file as a list of items for multi-select fields. I was hoping to find something similar.

— Reply to this email directly, view it on GitHub https://github.com/danielo515/obsidian-modal-form/issues/310, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARKJWN4PJ4OFBY5OKHXKILZPPAJHAVCNFSM6AAAAABL46NSASVHI2DSMVQWIX3LMV43ASLTON2WKOZSGQ2DKNBXGUYDEMQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

LemurTech commented 1 month ago

Yes, I have! I've tried a number of variations using AI tools, but haven't had any luck with this one. I have another form that reads defaults and writes to frontmatter values, so that part I can handle. The problem is in reading a simple list of choices from a file. dv.io.load() returns a promise, I know. I can't figure out how to extend it to wait for the file content and convert it to an array. Can forms handle this? Thanks much!

hdsateyate commented 1 month ago

Have you tried debugging in a dataviewjs codeblock with console logs to make sure the output is as you expect?

hdsateyate commented 1 month ago

Also the dataview documentation suggests you await the result of an dv.io.load call:

await dv.io.load("File")

In any case the way I've got this to work for me is to house the list in a front matter field as a list but an inline field would work as well. Then you wouldn't need to do a specific asynchronous file read operation.

LemurTech commented 1 month ago

Ok, I got it. Your comment led me back to trying a dataviewjs code block with a more elaborate version of the code that worked. I'd already tried 'await', but aparently I'd never set it apart with parentheses, which was dumb on my part. This is what works to pull in the contents of a simple text file as an array:

(await dv.io.load("<YourFilePath>.md")).split("\n");

Thanks for your help!

LemurTech commented 1 month ago

Closing, as I have now been enlightened!

danielo515 commented 1 month ago

Forgetting to await promises is a common mistake when you don't have a compiler helping you. Glad you have figured it out. Your use case is very interesting, I think it is worth adding to the advanced examples.