Skip to content

Commit

Permalink
feat: Enables the "End Year" setting for eras
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Apr 7, 2024
1 parent 33579de commit 9f5048b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 22 deletions.
45 changes: 38 additions & 7 deletions src/stores/month.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,44 @@ export class MonthStore {
this.firstDay,
this.leapDays,
this.staticStore.staticData,
this.eras,
this.staticStore.eras,
],
([weekdays, days, firstDay, leapDays, data]) => {
([weekdays, days, firstDay, leapDays, data, eras, allEras]) => {
let weekArray: (DayOrLeapDay | null)[][] = [];
let daysAdded = 0;
let intercals = 0;
while (daysAdded < days) {
//Initialize the day array.
//The first week of the month should include negative numbers for the prior month.
//If the year before this was ended early due to an era and I am the first month, I should not
//show negatives.
let showNegatives = true;
if (!weekArray.length && get(this.index) === 0) {
for (let i = allEras.length - 1; i >= 0; i--) {
const era = allEras[i];

if (era.isStartingEra) break;
if (!era.endsYear) continue;
if (era.date.year !== this.year.year - 1) continue;

/** Normalize the year to consider off the last era that ended the year. */
showNegatives = false;
break;
}
}

let dayArray: (DayOrLeapDay | null)[] =
weekArray.length === 0
? [...Array(firstDay).keys()].reverse().map((k) => {
return {
type: "day",
number: -1 * k,
name: null,
id: nanoid(3),
};
return showNegatives
? {
type: "day",
number: -1 * k,
name: null,
id: nanoid(3),
}
: null;
})
: [];

Expand Down Expand Up @@ -210,6 +231,16 @@ export class MonthStore {
) {
break;
}
let shouldEndEra = eras.find(
(era) =>
era.endsYear &&
era.date.year === this.year.year &&
era.date.month === get(this.index)
);
if (shouldEndEra && shouldEndEra.date.day === daysAdded) {
weekArray.push(dayArray);
return weekArray;
}
}
weekArray.push(dayArray);
}
Expand Down
52 changes: 37 additions & 15 deletions src/stores/years.store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { derived, get } from "svelte/store";
import type { StaticStore } from "./calendar.store";
import type { CalEvent } from "src/@types";
import type { CalDate, CalEvent } from "src/@types";
import { sortEventList, wrap } from "../utils/functions";
import { MonthStore } from "./month.store";
import type { Month, Week, LeapDay, Era } from "src/schemas/calendar/timespans";
import {
type Month,
type Week,
type LeapDay,
type Era,
TimeSpanType,
} from "src/schemas/calendar/timespans";

/* export type YearStore = ReturnType<typeof createYearStore>; */
export type YearCalculatorCache = Map<number, YearStore>;
Expand All @@ -22,18 +28,10 @@ export class YearStoreCache {
export class YearStore {
monthCache = new Map<number, MonthStore>();
constructor(public year: number, public staticStore: StaticStore) {}
months = derived(this.staticStore.months, (months) => {
return months.filter(
(m) =>
!m.interval || (this.year - (m.offset ?? 0)) % m.interval == 0
);
});
eras = derived(this.staticStore.eras, (eras) => {
const sorted = sortEventList(eras);

const list: Era[] = [];
for (let i = sorted.length - 1; i >= 0; i--) {
const era = sorted[i];
for (let i = eras.length - 1; i >= 0; i--) {
const era = eras[i];
if (era.isStartingEra) {
if (!list.length) list.push(era);
} else if (era.date.year <= this.year) {
Expand All @@ -45,6 +43,18 @@ export class YearStore {
}
return list;
});
months = derived([this.staticStore.months, this.eras], ([months, eras]) => {
let end = eras.find(
(era) => era.endsYear && era.date.year === this.year
);
if (end) {
months = months.slice(0, (end.date as CalDate).month + 1);
}
return months.filter(
(m) =>
!m.interval || (this.year - (m.offset ?? 0)) % m.interval == 0
);
});
daysBefore = derived(
[this.months, this.staticStore.leapDays],
([months, leapDays]) => {
Expand All @@ -54,13 +64,25 @@ export class YearStore {
firstDay = derived(
[
this.staticStore.staticConfiguration,
this.months,
this.staticStore.months,
this.staticStore.weekdays,
this.staticStore.leapDays,
this.staticStore.eras,
],
([config, months, weekdays, leapDays]) => {
([config, months, weekdays, leapDays, eras]) => {
let year = this.year;

//find the
for (let i = eras.length - 1; i >= 0; i--) {
const era = eras[i];
if (era.isStartingEra) break;
if (!era.endsYear) continue;
if (era.date.year >= this.year) continue;
/** Normalize the year to consider off the last era that ended the year. */
year = this.year - era.date.year;
}
return getFirstDayOfYear(
this.year,
year,
months,
weekdays,
leapDays,
Expand Down

0 comments on commit 9f5048b

Please sign in to comment.