From 264e298e25b1c9123d1604b2fba893d2f562f508 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Thu, 17 Jun 2021 12:09:18 +1000 Subject: [PATCH] Add token default size from import file name --- src/helpers/token.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/helpers/token.js b/src/helpers/token.js index cb88ec5..d5eefc4 100644 --- a/src/helpers/token.js +++ b/src/helpers/token.js @@ -39,7 +39,24 @@ export async function createTokenFromFile(file, userId) { return Promise.reject(); } let name = "Unknown Token"; + let defaultSize = 1; if (file.name) { + if (file.name.matchAll) { + // Match against a regex to find the grid size in the file name + // e.g. Cave 22x23 will return [["22x22", "22", "x", "23"]] + const sizeMatches = [...file.name.matchAll(/(\d+) ?(x|X) ?(\d+)/g)]; + for (let match of sizeMatches) { + const matchX = parseInt(match[1]); + const matchY = parseInt(match[3]); + if ( + !isNaN(matchX) && + !isNaN(matchY) && + matchX < 256 // Add check to test match isn't resolution + ) { + defaultSize = matchX; + } + } + } // Remove file extension name = file.name.replace(/\.[^/.]+$/, ""); // Removed grid size expression @@ -79,6 +96,7 @@ export async function createTokenFromFile(file, userId) { const token = { name, + defaultSize, thumbnail: thumbnail.id, file: fileAsset.id, id: uuid(), @@ -86,7 +104,6 @@ export async function createTokenFromFile(file, userId) { created: Date.now(), lastModified: Date.now(), owner: userId, - defaultSize: 1, defaultCategory: "character", defaultLabel: "", hideInSidebar: false,