From 18d030c5eb22fb08cc91c12d18867769fa845534 Mon Sep 17 00:00:00 2001 From: DMKAL <83312683+tirzah2@users.noreply.github.com> Date: Wed, 7 Aug 2024 13:28:43 -0800 Subject: [PATCH 1/3] Update module.json --- module.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module.json b/module.json index 29a6f6e..bffd45b 100644 --- a/module.json +++ b/module.json @@ -26,7 +26,7 @@ "media":[ { "type":"cover", - "url":"https://github.com/tirzah2/minipaint/blob/main/images/cover.png" + "url":"https://raw.githubusercontent.com/tirzah2/minipaint/main/images/cover.png" } ], "packs": [], From dd21d7ab47a60d166b28e5ef2ac13d669691acdd Mon Sep 17 00:00:00 2001 From: DMKAL <83312683+tirzah2@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:59:24 +0100 Subject: [PATCH 2/3] Update module.json --- module.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/module.json b/module.json index bffd45b..bffd197 100644 --- a/module.json +++ b/module.json @@ -2,10 +2,10 @@ "name": "minipaint", "title": "miniPaint", "description": "miniPaint is a full drawing tool to edit tile, tokens, items, generate art with AI, generate frames and tokens and so much more", - "version": "1.5", + "version": "1.6", "author": "DMKal", "minimumCoreVersion": "11", - "compatibleCoreVersion": "11", + "compatibleCoreVersion": "12", "scripts": [ "js/minipaint.js" ], @@ -35,5 +35,5 @@ "url": "https://github.com/tirzah2/minipaint", "readme": "https://github.com/tirzah2/minipaint/README.md", "manifest": "https://github.com/tirzah2/minipaint/releases/latest/download/module.json", - "download": "https://github.com/tirzah2/minipaint/releases/download/1.5/module.zip" + "download": "https://github.com/tirzah2/minipaint/releases/download/1.6/module.zip" } From 35d0e9d505f13d70661ed05cf0e8fac411c6966b Mon Sep 17 00:00:00 2001 From: DMKAL <83312683+tirzah2@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:59:47 +0100 Subject: [PATCH 3/3] Update minipaint.js --- js/minipaint.js | 104 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 95 insertions(+), 9 deletions(-) diff --git a/js/minipaint.js b/js/minipaint.js index 0c620a7..76835a5 100644 --- a/js/minipaint.js +++ b/js/minipaint.js @@ -350,8 +350,48 @@ async function replaceTile() { } const tile = selectedTiles[0]; - const originalPath = tile.document.texture.src; - const directoryPath = originalPath.substring(0, originalPath.lastIndexOf("/")); + let originalPath = tile.document.texture.src; + let directoryPath; + + // Determine the directory path + if (originalPath) { + directoryPath = originalPath.substring(0, originalPath.lastIndexOf("/")); + + // Check if the directory is writable (user data) or not (core data) + try { + await FilePicker.browse("data", directoryPath); + } catch (error) { + console.warn("Directory is not writable, switching to miniMapNewTiles."); + directoryPath = "miniMapNewTiles"; + + // Check if the fallback directory exists, and create it if it doesn't + try { + await FilePicker.browse("data", directoryPath); + } catch (error) { + if (error.message.includes("does not exist")) { + await FilePicker.createDirectory("data", directoryPath); + } else { + ui.notifications.error("Error accessing or creating the fallback directory."); + return; + } + } + } + } else { + // If originalPath is null, set the directory to 'miniMapNewTiles' in the root + directoryPath = "miniMapNewTiles"; + + // Check if the directory exists, and create it if it doesn't + try { + await FilePicker.browse("data", directoryPath); + } catch (error) { + if (error.message.includes("does not exist")) { + await FilePicker.createDirectory("data", directoryPath); + } else { + ui.notifications.error("Error accessing or creating the directory."); + return; + } + } + } const iframe = document.getElementById('myFrame'); const minipaintCanvas = iframe.contentWindow.document.querySelector('#canvas_minipaint'); @@ -374,14 +414,14 @@ async function replaceTile() { // Create a File object from the Blob with the random filename const file = new File([blob], randomFilename, { type: blob.type }); - // Upload the image file to the same directory as the original tile image + // Upload the image file to the determined directory const response = await FilePicker.upload("data", directoryPath, file, {}); if (response.path) { // Update the tile with the new image source await tile.document.update({ "texture.src": response.path }); - //ui.notifications.info("Tile image replaced successfully."); + ui.notifications.info("Tile image replaced successfully."); } else { ui.notifications.error("Failed to upload the modified image."); } @@ -435,8 +475,48 @@ async function replaceToken() { } const token = selectedTokens[0]; - const originalPath = token.document.texture.src; - const directoryPath = originalPath.substring(0, originalPath.lastIndexOf("/")); + let originalPath = token.document.texture.src; + let directoryPath; + + // Determine the directory path + if (originalPath) { + directoryPath = originalPath.substring(0, originalPath.lastIndexOf("/")); + + // Check if the directory is writable (user data) or not (core data) + try { + await FilePicker.browse("data", directoryPath); + } catch (error) { + console.warn("Directory is not writable, switching to miniMapNewTiles."); + directoryPath = "miniMapNewTiles"; + + // Check if the fallback directory exists, and create it if it doesn't + try { + await FilePicker.browse("data", directoryPath); + } catch (error) { + if (error.message.includes("does not exist")) { + await FilePicker.createDirectory("data", directoryPath); + } else { + ui.notifications.error("Error accessing or creating the fallback directory."); + return; + } + } + } + } else { + // If originalPath is null, set the directory to 'miniMapNewTiles' in the root + directoryPath = "miniMapNewTiles"; + + // Check if the directory exists, and create it if it doesn't + try { + await FilePicker.browse("data", directoryPath); + } catch (error) { + if (error.message.includes("does not exist")) { + await FilePicker.createDirectory("data", directoryPath); + } else { + ui.notifications.error("Error accessing or creating the directory."); + return; + } + } + } const iframe = document.getElementById('myFrame'); const minipaintCanvas = iframe.contentWindow.document.querySelector('#canvas_minipaint'); @@ -459,20 +539,26 @@ async function replaceToken() { // Create a File object from the Blob with the random filename const file = new File([blob], randomFilename, { type: blob.type }); - // Upload the image file to the same directory as the original token image + // Upload the image file to the determined directory const response = await FilePicker.upload("data", directoryPath, file, {}); if (response.path) { // Update the token with the new image source await token.document.update({ "texture.src": response.path }); - // ui.notifications.info("Token image replaced successfully."); + ui.notifications.info("Token image replaced successfully."); } else { ui.notifications.error("Failed to upload the modified image."); } }, "image/png"); } +// Function to generate a random filename +function generateRandomFilename() { + return Math.random().toString(36).substring(2, 12); +} + + async function showCanvasInImagePopout() { const iframe = document.getElementById('myFrame'); @@ -1274,4 +1360,4 @@ async function applyInpaintRing() { } }, "image/png"); // Ensure the image is in PNG format -} \ No newline at end of file +}