forked from GitHubMirrors/silverbullet-icalendar
All checks were successful
Build SilverBullet Plug / build (push) Successful in 25s
108 lines
3.6 KiB
TypeScript
108 lines
3.6 KiB
TypeScript
import { clientStore, config, datastore, editor, index } from "@silverbulletmd/silverbullet/syscalls";
|
|
import { convertIcsCalendar } from "https://esm.sh/ts-ics@2.4.0";
|
|
|
|
const VERSION = "0.3.16";
|
|
const CACHE_KEY = "icalendar:lastSync";
|
|
|
|
const TIMEZONE_OFFSETS: Record<string, number> = {
|
|
"GMT Standard Time": 0,
|
|
"W. Europe Standard Time": 1,
|
|
"Central Europe Standard Time": 1,
|
|
"Romance Standard Time": 1,
|
|
"Central European Standard Time": 1,
|
|
"Eastern Standard Time": -5,
|
|
"Central Standard Time": -6,
|
|
"Mountain Standard Time": -7,
|
|
"Pacific Standard Time": -8,
|
|
"UTC": 0,
|
|
"None": 0
|
|
};
|
|
|
|
async function getSources(): Promise<{ sources: any[], tzShift: number }> {
|
|
try {
|
|
const rawConfig = await config.get("icalendar", { sources: [] });
|
|
const sources = rawConfig.sources || [];
|
|
const tzShift = rawConfig.tzShift || 0;
|
|
return { sources, tzShift };
|
|
} catch (e) {
|
|
return { sources: [], tzShift: 0 };
|
|
}
|
|
}
|
|
|
|
async function fetchAndParseCalendar(source: any, hourShift = 0): Promise<any[]> {
|
|
const response = await fetch(source.url);
|
|
if (!response.ok) return [];
|
|
const text = await response.text();
|
|
const calendar = convertIcsCalendar(undefined, text);
|
|
if (!calendar.events) return [];
|
|
|
|
const events: any[] = [];
|
|
for (const icsEvent of calendar.events) {
|
|
const obj = icsEvent.start;
|
|
let wallTimeStr = "";
|
|
if (obj.local && typeof obj.local.date === "string") wallTimeStr = obj.local.date;
|
|
else if (typeof obj.date === "string") wallTimeStr = obj.date;
|
|
if (!wallTimeStr) continue;
|
|
|
|
const baseDate = new Date(wallTimeStr.replace("Z", "") + "Z");
|
|
const tzName = obj.local?.timezone || obj.timezone || "UTC";
|
|
const sourceOffset = TIMEZONE_OFFSETS[tzName] ?? 0;
|
|
const utcMillis = baseDate.getTime() - (sourceOffset * 3600000);
|
|
const finalDate = new Date(utcMillis + (hourShift * 3600000));
|
|
|
|
const pad = (n: number) => String(n).padStart(2, "0");
|
|
const localIso = finalDate.getFullYear() + "-" + pad(finalDate.getMonth() + 1) + "-" + pad(finalDate.getDate()) + "T" + pad(finalDate.getHours()) + ":" + pad(finalDate.getMinutes()) + ":" + pad(finalDate.getSeconds());
|
|
|
|
events.push({
|
|
...icsEvent,
|
|
start: localIso,
|
|
tag: "ical-event",
|
|
sourceName: source.name
|
|
});
|
|
}
|
|
return events;
|
|
}
|
|
|
|
export async function syncCalendars() {
|
|
try {
|
|
const { sources, tzShift } = await getSources();
|
|
if (sources.length === 0) return;
|
|
await editor.flashNotification("Syncing calendars...", "info");
|
|
const allEvents: any[] = [];
|
|
for (const source of sources) {
|
|
try {
|
|
const events = await fetchAndParseCalendar(source, tzShift);
|
|
allEvents.push(...events);
|
|
} catch (err) {
|
|
console.error(`Failed to sync ${source.name}:`, err);
|
|
}
|
|
}
|
|
await index.indexObjects("$icalendar", allEvents);
|
|
await editor.flashNotification(`Synced ${allEvents.length} events`, "info");
|
|
} catch (err) {
|
|
console.error("Sync failed:", err);
|
|
}
|
|
}
|
|
|
|
export async function forceSync() {
|
|
await clientStore.del(CACHE_KEY);
|
|
await syncCalendars();
|
|
}
|
|
|
|
export async function clearCache() {
|
|
if (!await editor.confirm("Clear all calendar events?")) return;
|
|
const pageKeys = await datastore.query({ prefix: ["ridx", "$icalendar"] });
|
|
const allKeys: any[] = [];
|
|
for (const { key } of pageKeys) {
|
|
allKeys.push(key);
|
|
allKeys.push(["idx", ...key.slice(2), "$icalendar"]);
|
|
}
|
|
if (allKeys.length > 0) await datastore.batchDel(allKeys);
|
|
await clientStore.del(CACHE_KEY);
|
|
await editor.flashNotification("Calendar index cleared", "info");
|
|
}
|
|
|
|
export async function showVersion() {
|
|
await editor.flashNotification(`iCalendar Plug ${VERSION}`, "info");
|
|
}
|