Calendarium

repository·main·Indexed 41 days ago

https://github.com/javalent/calendarium

An Obsidian plugin for crafting complex fantasy and science fiction calendars. Version 2.1.0 provides a comprehensive API for managing custom calendar systems, including date translation, event retrieval, seasonal data, and programmatic control of calendar views and stores.

Tokens
7.6K
Snippets
37
Records
45
Agent score
97%

What's inside Calendarium

  1. Manage UI state with EphemeralStore

    main

    While the CalendarStore manages the data, the EphemeralStore manages the user's current view and interface preferences. You can retrieve an ephemeral store using getEphemeralStore(id: string) from the main store.

    Key UI Controls:

    • View Mode: Control the current view using viewState (values: ViewState.Year, ViewState.Month, ViewState.Week, ViewState.Day).
    • Navigation: Use goToNext(), goToPrevious(), goToToday(), or displayDate(date: CalDate) to move the UI focus.
    • Display Toggles: Control visibility of UI elements via displayMoons, displayDayNumber, displayWeeks, hideEra, displayAbsoluteYear, displaySeasonColors, displayWeather, and interpolateColors.
    • Location: Manage the currently selected location via the location store.
  2. Access seasonal data

    main

    The CalendarAPI provides methods to interact with the calendar's seasonal system:

    • getSeasons(): Returns a list of all defined Season objects on the calendar.
    • getSeasonForDate(date: CalDate): Returns the Season applicable to the provided date, or undefined if no season is found.
    const seasons = api.getSeasons();
    const currentSeason = api.getSeasonForDate(api.getCurrentDate());
  3. Manage calendar events with EventStore

    main

    The EventStore class serves as the source of truth for events within a specific Calendar. It manages a collection of CalEvent objects, providing methods to query, insert, and remove events.

    Key Behaviors:

    • Source of Truth: It maintains an internal map of EventId -> CalEvent.
    • Cache Invalidation: When events are inserted or removed, the store automatically invalidates the internal EventCache, ensuring that date-based queries remain accurate.
    • Data Sync Warning: Events added directly to the EventStore via insertEvents are NOT automatically synced back to the underlying calendar data source.
    • File vs. Manual Events: The store distinguishes between events parsed from files and manually inserted events. This allows for bulk removal of all events associated with a specific file path.
    import { EventStore } from 'src/stores/events.store';
    
    // Initialize with a calendar object
    const eventStore = new EventStore(myCalendar);
    
    // Get all events
    const allEvents = eventStore.getEvents();
    
    // Get events for a specific date
    const eventsOnDate = eventStore.getEventsForDate(someCalDate);
  4. Retrieve a CalendarStore for a specific calendar

    main

    To interact with the state of a specific calendar (e.g., to change the current date or manage file events), use getStore or getStoreByCalendar.

    • getStore(calendarId: string): Finds a calendar by its ID and returns its associated CalendarStore.
    • getStoreByCalendar(calendar: Calendar): Returns the CalendarStore for a given Calendar object.

    Each CalendarStore manages the reactive state for that specific calendar instance.

    // Example: Getting a store by calendar ID
    const store = plugin.getStore('my-calendar-id');
    if (store) {
      // Perform store operations
    }
  5. Check event removability in EventStore

    main

    The EventStore provides utility methods to determine the nature of an event, which is important for deciding if an event can be deleted manually:

    • isFileEvent(eventId: string): Returns true if the event was added via insertEventsFromFile.
    • isRemovable(eventId: string): Returns true if the event is not a file event. This is a convenience method to check if an event can be safely removed without affecting file-based synchronization logic.
    if (eventStore.isRemovable(someEventId)) {
        eventStore.removeEvents(someEvent);
    }
  6. Manage core calendar state with CalendarStore

    main

    The CalendarStore provides methods to manipulate the underlying calendar data and navigate time. Key capabilities include:

    • Navigation: Use setCurrentDate(date: CalDate) to change the calendar's reference date, or getDaysBeforeDate(date: CalDate) to calculate offsets.
    • Data Access: Access eventStore for event management, staticStore for immutable calendar rules (months, moons, eras), and locationStore for location data.
    • Event Retrieval: Use getEventsForDate(date: CalDate) to get a Readable store of all events (including era-based events) occurring on a specific date.
    • Category Management: Use addCategory(category: CalEventCategory) to add new event categories or hasCategory(category: string) to check for existence.
  7. Modify the current date in a calendar

    main

    You can advance or decrement the current date of a calendar using the changeDay method. This method requires the Calendar object, its corresponding CalendarStore, and a change function (like incrementDay or decrementDay).

    import { incrementDay } from "./stores/calendar.store";
    
    // Advance the day for a specific calendar
    plugin.changeDay(calendar, store, incrementDay);
  8. Calendarium core schema exports

    main

    The src/schemas/index.ts file serves as the central export point for Calendarium's data structures. It aggregates and re-exports all primary schemas related to:

    • Calendar: Definitions for calendar structures and logic.
    • Events: Definitions for event data and properties.
    • Settings: Configuration and user preference schemas.
  9. Use the API class to interact with Calendarium

    main

    The API class is the primary entry point for interacting with the Calendarium plugin. It allows you to manage settings callbacks, retrieve available calendar names, and obtain a CalendarAPI instance for specific calendars to access their data and events.

    import { API } from "src/api/api";
    
    // Assuming 'plugin' is an instance of Calendarium provided by the environment
    const api = new API(plugin);
  10. Insert and remove events in EventStore

    main

    You can modify the event collection using these methods:

    • insertEvents(...events: CalEvent[]): Adds new events to the store. Note that events with EventType.Undated are ignored.
    • removeEvents(...events: CalEvent[]): Removes specific events from the store. Events with EventType.Undated are ignored.
    • insertEventsFromFile(path: string | null, ...events: CalEvent[]): Adds events and associates them with a specific file path. This is useful for tracking which events belong to which file for later bulk removal.
    • removeEventsFromFile(filePath: string): Removes all events associated with the provided filePath.
    • removeAllFileEvents(): Clears all events that were marked as file events.
    // Insert manual events
    eventStore.insertEvents(event1, event2);
    
    // Insert events linked to a file
    eventStore.insertEventsFromFile('/path/to/file.md', event3);
    
    // Remove specific events
    eventStore.removeEvents(event1);
    
    // Bulk remove by file
    eventStore.removeEventsFromFile('/path/to/file.md');
    
    // Clear all file-based events
    eventStore.removeAllFileEvents();