richer editing

This commit is contained in:
2025-08-15 00:13:38 -04:00
parent 929535c987
commit 7286c9a335
3 changed files with 182 additions and 46 deletions

View File

@@ -1,6 +1,25 @@
import ICAL from "ical.js";
import type { CalendarEvent } from "@/lib/types";
function valToString(val: unknown): string | undefined {
if (!val) return undefined;
if (typeof val === "string") return val;
if ((val as any).toString) return (val as any).toString();
return undefined;
}
function valToISOString(val: unknown): string | undefined {
if (!val) return undefined;
if (typeof val === "string") {
const d = new Date(val);
return isNaN(d.getTime()) ? undefined : d.toISOString();
}
if ((val as any).toJSDate) {
return (val as any).toJSDate().toISOString();
}
return undefined;
}
export function parseICS(icsString: string): CalendarEvent[] {
const jcalData = ICAL.parse(icsString);
const comp = new ICAL.Component(jcalData);
@@ -8,14 +27,19 @@ export function parseICS(icsString: string): CalendarEvent[] {
return vevents.map((v) => {
const ev = new ICAL.Event(v);
const isAllDay = ev.startDate.isDate;
return {
id: ev.uid || crypto.randomUUID(),
title: ev.summary || "Untitled Event",
start: ev.startDate.toJSDate().toISOString().split("T")[0],
end: ev.endDate
? ev.endDate.toJSDate().toISOString().split("T")[0]
: undefined,
description: ev.description || "",
location: ev.location || "",
url: valToString(v.getFirstPropertyValue("url")),
start: ev.startDate.toJSDate().toISOString(),
end: ev.endDate ? ev.endDate.toJSDate().toISOString() : undefined,
allDay: isAllDay,
createdAt: valToISOString(v.getFirstPropertyValue("dtstamp")),
lastModified: valToISOString(v.getFirstPropertyValue("last-modified")),
};
});
}
@@ -23,19 +47,52 @@ export function parseICS(icsString: string): CalendarEvent[] {
export function generateICS(events: CalendarEvent[]): string {
const comp = new ICAL.Component(["vcalendar", [], []]);
comp.addPropertyWithValue("version", "2.0");
comp.addPropertyWithValue("prodid", "-//YourAppName//EN");
comp.addPropertyWithValue("prodid", "-//iCalPWA//EN");
events.forEach((ev) => {
const vevent = new ICAL.Component("vevent");
vevent.addPropertyWithValue("uid", ev.id);
vevent.addPropertyWithValue("summary", ev.title);
vevent.addPropertyWithValue("dtstart", ICAL.Time.fromDateString(ev.start));
if (ev.end) {
vevent.addPropertyWithValue("dtend", ICAL.Time.fromDateString(ev.end));
}
if (ev.description) {
if (ev.description)
vevent.addPropertyWithValue("description", ev.description);
if (ev.location) vevent.addPropertyWithValue("location", ev.location);
if (ev.url) vevent.addPropertyWithValue("url", ev.url);
if (ev.allDay) {
vevent.addPropertyWithValue(
"dtstart",
ICAL.Time.fromDateString(ev.start.split("T")[0]),
);
if (ev.end) {
vevent.addPropertyWithValue(
"dtend",
ICAL.Time.fromDateString(ev.end.split("T")[0]),
);
}
} else {
vevent.addPropertyWithValue(
"dtstart",
ICAL.Time.fromJSDate(new Date(ev.start)),
);
if (ev.end) {
vevent.addPropertyWithValue(
"dtend",
ICAL.Time.fromJSDate(new Date(ev.end)),
);
}
}
vevent.addPropertyWithValue(
"dtstamp",
ICAL.Time.fromJSDate(ev.createdAt ? new Date(ev.createdAt) : new Date()),
);
if (ev.lastModified) {
vevent.addPropertyWithValue(
"last-modified",
ICAL.Time.fromJSDate(new Date(ev.lastModified)),
);
}
comp.addSubcomponent(vevent);
});