From 94f54dca5c08ef508fa2fcc2b4331c0b3d19d4a7 Mon Sep 17 00:00:00 2001 From: Abraham Nassiff Date: Sun, 4 Jun 2023 12:17:43 -0500 Subject: [PATCH] Switched text input to file upload, improved some messages --- module/mosh.js | 52 +++++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/module/mosh.js b/module/mosh.js index eb9a60b..533cc6f 100644 --- a/module/mosh.js +++ b/module/mosh.js @@ -155,8 +155,6 @@ async function createMothershipMacro(data, slot) { return false; } - - /* -------------------------------------------- */ /* Actor from JSON */ /* -------------------------------------------- */ @@ -414,7 +412,7 @@ async function createActorFromJson(jsonData) { newActor.items = items } catch (err) { console.error(err); - ui.notifications.error("Invalid JSON data"); + ui.notifications.error("Invalid JSON, make sure you exported it from the Mothership Companion App"); return; } @@ -426,32 +424,46 @@ async function createActorFromJson(jsonData) { try { await Actor.create(newActor); - ui.notifications.info("Actor created successfully"); + ui.notifications.info("Character imported successfully!"); } catch (err) { console.error(err); - ui.notifications.error("Failed to create actor"); + ui.notifications.error("Failed to import character, check console for more details."); } } let d = new Dialog({ - title: "Create Actor from JSON", - content: `

Paste your JSON data below:

`, + title: "Mothership Companion JSON Import", + content: ` +
+
+ + +
+
+ `, buttons: { - save: { - icon: '', - label: "Create", - callback: (html) => { - const jsonData = html.find('#json-data').val(); - createActorFromJson(jsonData); + upload: { + label: "Upload", + callback: (html) => { + let fileInput = html.find('#json-upload')[0]; + let file = fileInput.files[0]; + let reader = new FileReader(); + + reader.onload = (event) => { + let contents = event.target.result; + createActorFromJson(contents); + }; + + reader.onerror = (event) => { + console.error(`File could not be read: ${event.target.error}`); + }; + + reader.readAsText(file); + } } - }, - cancel: { - icon: '', - label: "Cancel" - } }, - default: "save" -}); + default: "upload" +}) d.render(true);