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

Blank if null with result.asString ? #129

Closed juusan closed 9 months ago

juusan commented 10 months ago

Hello !

What a wonderful plugin ! Thanks for working hard on it.

Do you know if there is a way to output nothing if we submit no value into the prompt ? Here is an exemple :

const modalForm = app.plugins.plugins.modalforms.api; const result = await modalForm.openForm('climateLogForm'); tR += result.asString('AC_probe_T_avg :: {{AC_probe_T_avg}}') + '\n';

With this snippet, if I enter 25 it renders like : AC_probe_T_avg :: 25 But if I let the prompt empty and submit it, it renders AC_probe_T_avg :: {{AC_probe_T_avg}} I would like to render it just like AC_probe_T_avg ::

Is there any way to do it ? Thank you so much !

danielo515 commented 10 months ago

Thank you for your kind words! And for using it Sadly, there is no way yet. I've been thinking a lot about streamlining the usage of the result and the API, and this specific case was already in my mind.

As a workaround, you can currently do this:

const modalForm = app.plugins.plugins.modalforms.api;
const result = await modalForm.openForm('climateLogForm');
const data = result.getData()
if (data.AC_probe_T_avg){
    tR += `AC_probe_T_avg :: ${AC_probe_T_avg}\n`;
}

How would it look like something like this (this is the idea I have in mind)

const modalForm = app.plugins.plugins.modalforms.api;
const result = await modalForm.openForm('climateLogForm');
tR += result.map('AC_probe_T_avg', (value) => `AC_probe_T_avg :: ${value}`)

Here, if the value of AC_probe_T_avg is undefined, then the map will return an empty string, but if the value exists, then it will use your function to transform it to whatever you want