Skip to content

Commit

Permalink
Export localization function (#7)
Browse files Browse the repository at this point in the history
* Export localization functions

calendar-ui will no longer localize the window.moment object automatically. You'll need to call configureGlobalMomentLocale

* v0.3.0
  • Loading branch information
liamcain authored Jan 25, 2021
1 parent 810527b commit 9a989bb
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 57 deletions.
12 changes: 7 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Moment } from "moment";
import type { Locale, Moment } from "moment";
import { SvelteComponentTyped } from "svelte";

export type ILocaleOverride = "system-default" | string;
Expand Down Expand Up @@ -32,10 +32,7 @@ export interface ICalendarSource {
export class Calendar extends SvelteComponentTyped<{
// Settings
showWeekNums: boolean;

// Localization
localeOverride: ILocaleOverride;
weekStart: IWeekStartOption;
localeData?: Locale;

// Event Handlers
onHoverDay?: (date: Moment, targetEl: EventTarget) => void;
Expand All @@ -53,3 +50,8 @@ export class Calendar extends SvelteComponentTyped<{
today?: Moment;
displayedMonth?: Moment;
}> {}

export function configureGlobalMomentLocale(
localeOverride: ILocaleOverride,
weekStart: IWeekStartOption
): string;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-calendar-ui",
"version": "0.2.4",
"version": "0.3.0",
"description": "Calendar UI that powers obsidian-calendar-plugin",
"author": "liamcain",
"main": "dist/index.js",
Expand Down
10 changes: 4 additions & 6 deletions src/components/Calendar.svelte
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<svelte:options immutable />

<script lang="ts">
import type { Moment } from "moment";
import type { Locale, Moment } from "moment";
import Day from "./Day.svelte";
import Nav from "./Nav.svelte";
import WeekNum from "./WeekNum.svelte";
import { getDailyMetadata, getWeeklyMetadata } from "../metadata";
import type { ICalendarSource, IMonth } from "../types";
import { getDaysOfWeek, getMonth, isWeekend } from "../utils";
import type { ILocaleOverride, IWeekStartOption } from "../localization";
// Localization
export let weekStart: IWeekStartOption = "locale";
export let localeOverride: ILocaleOverride;
export let localeData: Locale;
// Settings
export let showWeekNums: boolean = false;
Expand Down Expand Up @@ -45,8 +43,8 @@
let month: IMonth;
let daysOfWeek: string[];
$: month = getMonth(displayedMonth, weekStart, localeOverride);
$: daysOfWeek = getDaysOfWeek(today, localeOverride);
$: month = getMonth(displayedMonth, localeData);
$: daysOfWeek = getDaysOfWeek(today, localeData);
// Exports
export function incrementDisplayedMonth() {
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type moment from "moment";
import type { App } from "obsidian";

import Calendar from "./components/Calendar.svelte";
import type { ICalendarSource, IDot, IDayMetadata } from "./types";
import type { App } from "obsidian";
import type moment from "moment";

declare global {
interface Window {
Expand All @@ -12,3 +13,4 @@ declare global {

export { Calendar };
export type { ICalendarSource, IDot, IDayMetadata };
export { configureGlobalMomentLocale } from "./localization";
55 changes: 29 additions & 26 deletions src/localization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,39 @@ const weekdays = [
"saturday",
];

function overrideGlobalMomentWeekStart(weekStart: IWeekStartOption): void {
const { moment } = window;
const currentLocale = moment.locale();

// Save the initial locale weekspec so that we can restore
// it when toggling between the different options in settings.
if (!window._bundledLocaleWeekSpec) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
window._bundledLocaleWeekSpec = (<any>moment.localeData())._week;
}

if (weekStart === "locale") {
moment.updateLocale(currentLocale, {
week: window._bundledLocaleWeekSpec,
});
} else {
moment.updateLocale(currentLocale, {
week: {
dow: weekdays.indexOf(weekStart) || 0,
},
});
}
}

/**
* Sets the locale used by the calendar. This allows the calendar to
* default to the user's locale (e.g. Start Week on Sunday/Monday/Friday)
*
* @param localeOverride locale string (e.g. "en-US")
*/
export function configureMomentLocale(
localeOverride: ILocaleOverride = "system-default"
export function configureGlobalMomentLocale(
localeOverride: ILocaleOverride = "system-default",
weekStart: IWeekStartOption = "locale"
): string {
const obsidianLang = localStorage.getItem("language") || "en";
const systemLang = navigator.language?.toLowerCase();
Expand All @@ -78,29 +103,7 @@ export function configureMomentLocale(
`[Calendar] Trying to switch Moment.js global locale to ${momentLocale}, got ${currentLocale}`
);

return currentLocale;
}

export function overrideMomentWeekStart(weekStart: IWeekStartOption): void {
const { moment } = window;
const currentLocale = moment.locale();

// Save the initial locale weekspec so that we can restore
// it when toggling between the different options in settings.
if (!window._bundledLocaleWeekSpec) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
window._bundledLocaleWeekSpec = (<any>moment.localeData())._week;
}
overrideGlobalMomentWeekStart(weekStart);

if (weekStart === "locale") {
moment.updateLocale(currentLocale, {
week: window._bundledLocaleWeekSpec,
});
} else {
moment.updateLocale(currentLocale, {
week: {
dow: weekdays.indexOf(weekStart) || 0,
},
});
}
return currentLocale;
}
19 changes: 2 additions & 17 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import type { Moment } from "moment";
import * as os from "os";

import {
configureMomentLocale,
overrideMomentWeekStart,
ILocaleOverride,
IWeekStartOption,
} from "./localization";
import type { IMonth, IWeek } from "./types";

function isMacOS() {
Expand All @@ -33,17 +27,8 @@ export function getStartOfWeek(days: Moment[]): Moment {
* Generate a 2D array of daily information to power
* the calendar view.
*/
export function getMonth(
displayedMonth: Moment,
weekStart: IWeekStartOption,
localeOverride?: ILocaleOverride
): IMonth {
// These functions mutate the global window.moment object.
// Call them here to make sure the calendar view stays in
// sync with settings.
const locale = configureMomentLocale(localeOverride);
overrideMomentWeekStart(weekStart);

export function getMonth(displayedMonth: Moment, ..._args: unknown[]): IMonth {
const locale = window.moment().locale();
const month = [];
let week: IWeek;

Expand Down

0 comments on commit 9a989bb

Please sign in to comment.