2025-09-08 15:56:57 -07:00
|
|
|
import { z } from "zod";
|
2025-11-10 08:43:33 +00:00
|
|
|
import { get_empty_datum_for, Presentation } from "./presentation.svelte.ts";
|
2025-09-08 15:56:57 -07:00
|
|
|
|
|
|
|
|
type Assert<_T extends true> = void;
|
|
|
|
|
|
2025-09-23 13:08:51 -07:00
|
|
|
export const all_datum_tags = [
|
2025-09-08 15:56:57 -07:00
|
|
|
"Text",
|
|
|
|
|
"Timestamp",
|
|
|
|
|
"Uuid",
|
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
|
|
// Type checking to ensure that all valid enum tags are included.
|
|
|
|
|
type _ = Assert<
|
2025-09-23 13:08:51 -07:00
|
|
|
Datum["t"] extends (typeof all_datum_tags)[number] ? true : false
|
2025-09-08 15:56:57 -07:00
|
|
|
>;
|
|
|
|
|
|
2025-09-23 13:08:51 -07:00
|
|
|
const datum_text_schema = z.object({
|
2025-09-08 15:56:57 -07:00
|
|
|
t: z.literal("Text"),
|
|
|
|
|
c: z.string().nullish().transform((x) => x ?? undefined),
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-23 13:08:51 -07:00
|
|
|
const datum_timestamp_schema = z.object({
|
2025-09-08 15:56:57 -07:00
|
|
|
t: z.literal("Timestamp"),
|
|
|
|
|
c: z.coerce.date().nullish().transform((x) => x ?? undefined),
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-23 13:08:51 -07:00
|
|
|
const datum_uuid_schema = z.object({
|
2025-09-08 15:56:57 -07:00
|
|
|
t: z.literal("Uuid"),
|
|
|
|
|
c: z.string().nullish().transform((x) => x ?? undefined),
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-23 13:08:51 -07:00
|
|
|
export const datum_schema = z.union([
|
|
|
|
|
datum_text_schema,
|
|
|
|
|
datum_timestamp_schema,
|
|
|
|
|
datum_uuid_schema,
|
2025-09-08 15:56:57 -07:00
|
|
|
]);
|
|
|
|
|
|
2025-09-23 13:08:51 -07:00
|
|
|
export type Datum = z.infer<typeof datum_schema>;
|
2025-11-10 08:43:33 +00:00
|
|
|
|
|
|
|
|
export function parse_clipboard_value(
|
|
|
|
|
presentation: Presentation,
|
|
|
|
|
input: string,
|
|
|
|
|
): Datum {
|
|
|
|
|
if (presentation.t === "Dropdown") {
|
|
|
|
|
if (
|
|
|
|
|
presentation.c.allow_custom ||
|
|
|
|
|
presentation.c.options.some(({ value }) => value === input)
|
|
|
|
|
) {
|
|
|
|
|
return { t: "Text", c: input };
|
|
|
|
|
} else {
|
|
|
|
|
return get_empty_datum_for(presentation);
|
|
|
|
|
}
|
|
|
|
|
} else if (presentation.t === "Text") {
|
|
|
|
|
return { t: "Text", c: input };
|
|
|
|
|
} else if (presentation.t === "Timestamp") {
|
|
|
|
|
// TODO: implement
|
|
|
|
|
console.warn("parsing timestamps from clipboard is not yet supported");
|
|
|
|
|
return get_empty_datum_for(presentation);
|
|
|
|
|
} else if (presentation.t === "Uuid") {
|
|
|
|
|
// TODO: implement
|
|
|
|
|
console.warn("parsing uuids from clipboard is not yet supported");
|
|
|
|
|
return get_empty_datum_for(presentation);
|
|
|
|
|
}
|
|
|
|
|
type _ = Assert<typeof presentation["t"] extends never ? true : false>;
|
|
|
|
|
throw new Error("should be unreachable");
|
|
|
|
|
}
|