Skip to content

Commit

Permalink
chore: Code reorginization
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Apr 10, 2024
1 parent 4fa5095 commit 1a3dda4
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/calendar/ui/Events/Dots.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { getTypedContext } from "../../view";
import { getTypedContext } from "../../view.types";
import Dot from "./Dot.svelte";
import type { CalEvent, EventLike } from "src/@types";
Expand Down
2 changes: 1 addition & 1 deletion src/calendar/ui/Events/Flag.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
setNodeIcon,
} from "src/utils/icons";
import { createEventDispatcher } from "svelte";
import { getTypedContext } from "../../view";
import { getTypedContext } from "../../view.types";
import { ViewEventModal } from "../../event-modal";
import { DEFAULT_CATEGORY_COLOR } from "src/utils/constants";
import { addEventWithModal } from "src/settings/modals/event/event";
Expand Down
2 changes: 1 addition & 1 deletion src/calendar/ui/Month/Month.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { ViewState } from "src/stores/calendar.store";
import { getTypedContext } from "../../view";
import { getTypedContext } from "../../view.types";
import Weekdays from "../Week/Weekdays.svelte";
import Week from "../Week/Week.svelte";
Expand Down
11 changes: 3 additions & 8 deletions src/calendar/ui/Nav.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { ExtraButtonComponent, Menu } from "obsidian";
import { getTypedContext } from "../view";
import { getTypedContext } from "../view.types";
import { ViewState } from "src/stores/calendar.store";
import { LEFT, RIGHT, SETTINGS } from "src/utils/icons";
import CalendariumMenu from "src/utils/menu";
Expand All @@ -12,12 +12,7 @@
const ephemeral = getTypedContext("ephemeralStore");
const plugin = getTypedContext("plugin");
const store = $global;
const {
displaying,
displayingMonth,
displayingYear,
hideEra,
} = $ephemeral;
const { displaying, displayingMonth, displayingYear, hideEra } = $ephemeral;
const { currentDisplay, yearCalculator } = store;
const monthInFrame = getTypedContext("monthInFrame");
const viewState = $ephemeral.viewState;
Expand Down Expand Up @@ -74,7 +69,7 @@
menu.addItem((item) => {
item.setTitle("Go to day").onClick(() => {
new GoToDateModal(plugin, store).open();
new GoToDateModal(plugin, store, $ephemeral).open();
});
});
menu.addSeparator();
Expand Down
2 changes: 1 addition & 1 deletion src/calendar/ui/Week/Week.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { getTypedContext } from "src/calendar/view";
import { getTypedContext } from "src/calendar/view.types";
import Day from "../Day/Day.svelte";
import { get } from "svelte/store";
import type { DayOrLeapDay } from "src/schemas/calendar/timespans";
Expand Down
2 changes: 1 addition & 1 deletion src/calendar/ui/Week/Weekdays.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { getTypedContext } from "src/calendar/view";
import { getTypedContext } from "src/calendar/view.types";
import { getAbbreviation } from "src/utils/functions";
export let year: number;
Expand Down
2 changes: 1 addition & 1 deletion src/calendar/ui/Year/Year.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { getTypedContext } from "src/calendar/view";
import { getTypedContext } from "src/calendar/view.types";
import Month from "../Month/Month.svelte";
import { nanoid } from "src/utils/functions";
import { onMount } from "svelte";
Expand Down
70 changes: 70 additions & 0 deletions src/calendar/view.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { getContext } from "svelte";
import { setContext } from "svelte";
import type { AgendaView } from "./view";
import type { ViewState } from "obsidian";
import type Calendarium from "src/main";
import type {
CalendarStoreState,
CalendarStore,
EphemeralStore,
} from "src/stores/calendar.store";
import type { Writable } from "svelte/store";

export const ViewType = {
Calendarium: "CALENDARIUM",
Agenda: "CALENDARIUM_AGENDA",
} as const;

export type ViewType = (typeof ViewType)[keyof typeof ViewType];

export interface CalendariumViewStates {
[ViewType.Calendarium]: CalendarStoreState;
[ViewType.Agenda]: {
calendar: string;
parent: string;
id?: string;
};
}

interface TypedViewState<T extends ViewType> extends ViewState {
type: T;
state: CalendariumViewStates[T];
}

declare module "obsidian" {
interface WorkspaceLeaf {
setViewState<T extends ViewType>(
viewState: TypedViewState<T>,
eState?: any
): Promise<void>;
}
}

interface CalendarContext {
store: Writable<CalendarStore>;
view: ViewParent;
plugin: Calendarium;
ephemeralStore: Writable<EphemeralStore>;
full: Writable<boolean>;
monthInFrame: Writable<number | null>;
}

export interface ViewParent {
child: AgendaView | null;
id: string;
calendar: string;
store: CalendarStore | null;
}

export function setTypedContext<T extends keyof CalendarContext>(
key: T,
value: CalendarContext[T]
): void {
setContext(key, value);
}

export function getTypedContext<T extends keyof CalendarContext>(
key: T
): CalendarContext[T] {
return getContext(key);
}

0 comments on commit 1a3dda4

Please sign in to comment.