fantasycalendar / obsidian-fantasy-calendar

MIT License
175 stars 7 forks source link

[Question] Is it possible to create a category from the API or from the Frontmatter? #76

Closed carlonicora closed 1 year ago

carlonicora commented 1 year ago

Hello wonderful people at Fantasy Calendar,

I tried to check in the documentation (and even trying to tweak the Frontmatter), but did not manage to find an answer to this:

Is there a way of creating a new category through the Api or adding it in the Frontmatter?

So far, the only way of adding a Category I found is through the FC Settings, but I was wondering if a new one can be created programmatically as part of an Api integration.

Thanks a lot Carlo

valentine195 commented 1 year ago

There's not a direct way, but you could add it to the FC settings property directly and then call the saveCalendar method, which saves the data and refreshes the view.

carlonicora commented 1 year ago

@valentine195 Thanks a lot for your reply, really appreciated.

I will dig into it and see how I can make it work!

valentine195 commented 1 year ago

@carlonicora FYI I just added this to the Fantasy Calendar API

See here https://github.com/fantasycalendar/obsidian-fantasy-calendar#plugin-api

carlonicora commented 1 year ago

Thank you! Much appreciated!

carlonicora commented 1 year ago

Hi @valentine195

Apologies for my ignorance. I added fantasy-calendar as dev in package, but I still can't find FantasyCalendarAPI

Also, if I just use the plugin as :any and access it through app.plugins.getPlugin('fantasy-calendar') I can see all the info in the console, but when I try and get the calendars through api.getCalendars() I get an error (screenshot attached)

Screenshot 2022-10-21 at 17 33 03

valentine195 commented 1 year ago

No worries, the API is available on the window, you can just use it. No need to install Fantasy Calendar as a dependency unless you want the typings

carlonicora commented 1 year ago

Hi @valentine195

What I meant to say is that I must be doing something wrong, as in any way I access the API, I get an error.

When I write the fantasy calendar api in the console, I can see everything, but when I run window.FantasyCalendarAPI.getCalendars() (or app.plugins.getPlugin('fantasy-calendar').api.getCalendars()` I get the error above.

valentine195 commented 1 year ago

That’s weird. That method just grabs the calendars out of the plugin’s data object. What does app.plugins.getPlugin('fantasy-calendar').data look like?

carlonicora commented 1 year ago

That's the strange thing. When I output the entire API I can see the data (see screenshot above), but if I try to get the data directly I get undefined.

valentine195 commented 1 year ago

Weird. It works for me on my phone lol

Do any of the API methods work?

carlonicora commented 1 year ago

nope, and I have the latest version of Fantasy Calendar (2.3.10) installed in obsidian (and it works, as I can change the calendar from the plugin itself)

valentine195 commented 1 year ago

Well I forgot to push the release for the new category functionality lol I'll do that later today.

Still doesn't solve this issue though. Not sure what's going on there, I'll have to investigate.

carlonicora commented 1 year ago

Thanks! Much appreciated!

carlonicora commented 1 year ago

@valentine195 So, I have understood the issue with data being undefined. I call it from the onLayoutReady event in Obsidian, but it seems that the data is not yet ready. If I add a delay, then the data becomes available and I can read it.

This is because the load of the calendars information is run in the onLayoutReady, so my call reads the data before they are ready.

I need to write a wrapper that waits for the fantasy-calendars-settings-loaded to be called before allowing the system to query the calendars!

valentine195 commented 1 year ago

Ah yeah, that was going to be the first thing I did actually was move that into the initial load...

You don't need a wrapper, use the FC's onSettingsLoad method:

https://github.com/fantasycalendar/obsidian-fantasy-calendar/blob/70e6b5dd1f6a87c0ad3178c2aa9e583556d4c8fe/src/main.ts#L396

carlonicora commented 1 year ago

Hello!

Yes, I was thinking internally, to give access to Fantasy Calendar from my plugin only when FC is ready.

Thanks a lot again for the support!