diff --git a/src/lib/components/EventInput.svelte b/src/lib/components/EventInput.svelte index d296827..0e1b4e5 100644 --- a/src/lib/components/EventInput.svelte +++ b/src/lib/components/EventInput.svelte @@ -38,7 +38,9 @@ // Event loading state let eventIdSearch = $state(""); + let eventJsonInput = $state(""); let loadingEvent = $state(false); + let loadMethod = $state<'hex' | 'json'>('hex'); // Session storage loading let hasLoadedFromStorage = $state(false); @@ -143,6 +145,55 @@ } } + /** + * Loads an event from JSON string for editing + */ + function loadEventFromJson(): void { + if (!eventJsonInput.trim()) { + error = "Please enter event JSON."; + return; + } + + try { + const eventJson = JSON.parse(eventJsonInput.trim()); + + // Validate required fields + if (typeof eventJson.kind !== 'number') { + error = "Invalid event JSON: missing or invalid 'kind' field."; + return; + } + + if (typeof eventJson.content !== 'string') { + error = "Invalid event JSON: missing or invalid 'content' field."; + return; + } + + if (!Array.isArray(eventJson.tags)) { + error = "Invalid event JSON: missing or invalid 'tags' field."; + return; + } + + // Extract event data (drop fields that need to be regenerated) + eventData = { + kind: eventJson.kind, + content: eventJson.content, + createdAt: Math.floor(Date.now() / 1000), // Use current time + }; + + // Convert tags from NDK format to our format + tags = eventJson.tags.map((tag: string[]) => ({ + key: tag[0] || "", + values: tag.slice(1) + })); + + success = "Loaded event from JSON successfully."; + error = null; + } catch (err) { + console.error("Error parsing event JSON:", err); + error = `Failed to parse event JSON: ${err instanceof Error ? err.message : "Invalid JSON format"}`; + } + } + /** * Clears all form fields and resets to initial state */ @@ -158,6 +209,7 @@ publishedRelays = []; lastPublishedEventId = null; eventIdSearch = ""; + eventJsonInput = ""; showJsonPreview = false; } @@ -201,32 +253,83 @@
- Load an existing event to edit and publish as a replacement with your signature. -
+ + {#if loadMethod === 'hex'} + ++ Load an existing event from relays by its hex ID. +
+ {:else} + ++ Paste a complete event JSON to load it into the form. Fields like id, pubkey, created_at, and sig will be regenerated. +
+ {/if}