Skip to content

Commit

Permalink
add wildcard for filenames
Browse files Browse the repository at this point in the history
closes #25
  • Loading branch information
ardittristan committed Nov 5, 2021
1 parent e8239de commit 2286a6d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Patch Notes

## Version 1.1.0

- Add option for wildcarding images

## Version 1.0.3

- Put traits in html list
Expand Down
18 changes: 18 additions & 0 deletions css/imagesettings.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@

#npcGen-image-location-settings input[type="text"] {
min-width: 100px;
height: 28px;
}

#npcGen-image-location-settings input.npcGenRegexInput {
margin-left: 4px;
}

#npcGen-image-location-settings button.file-picker {
order: 0;
margin-left: 0;
}

#npcGen-image-location-settings input.npcGenRegexInput::placeholder {
opacity: 0;
}

#npcGen-image-location-settings input.npcGenTextInput:not(:placeholder-shown) ~ input.npcGenRegexInput::placeholder {
opacity: 1;
}

#npcGen-image-location-settings .grid-container > div:nth-of-type(6n),
Expand Down
3 changes: 2 additions & 1 deletion lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@
"npcGen.exportLabel": "Öffnen",
"npcGen.exportHint": "Kopiere oder füge alle NPC Generator Einstellungen ein",
"npcGen.roleSpecificImages": "Rollenspezifische Bilder",
"npcGen.roleSpecificImagesHint": "Erlaubt dir rollenspezifische Bildpfade einzustellen, falls aktiviert."
"npcGen.roleSpecificImagesHint": "Erlaubt dir rollenspezifische Bildpfade einzustellen, falls aktiviert.",
"npcGen.default": "Voreinstellung"
}
3 changes: 2 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@
"npcGen.exportLabel": "Open",
"npcGen.exportHint": "Copy or paste all NPC Generator settings",
"npcGen.roleSpecificImages": "Role specific images",
"npcGen.roleSpecificImagesHint": "Allows to supply role specific image paths if enabled"
"npcGen.roleSpecificImagesHint": "Allows to supply role specific image paths if enabled",
"npcGen.default": "Default"
}
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"authors": [],
"url": "https://github.com/ardittristan/VTTNPCGen",
"flags": {},
"version": "1.0.3",
"version": "1.1.0",
"minimumCoreVersion": "0.8.0",
"compatibleCoreVersion": "0.8.9",
"scripts": [],
Expand Down
26 changes: 21 additions & 5 deletions modules/applications/NPCGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,22 +565,29 @@ export default class NPCGenerator extends FormApplication {
let path = locations?.[game.settings.get("npcgen", "roleSpecificImages") && locations?.[race + gender + Class]?.length > 0 ? race + gender + Class : race + gender];
if (!path || path.length === 0) return defaultReturn;

let regex =
locations?.[
game.settings.get("npcgen", "roleSpecificImages") && locations?.[race + gender + Class + "Regex"]?.length > 0
? race + gender + Class + "Regex"
: race + gender + "Regex"
];

/** @type {String[]} */
let iconList = [];

try {
let fileObject = await FilePicker.browse("public", path);

if (fileObject.target && fileObject.target !== ".") {
iconList = iconList.concat(await this._getIcons(fileObject, "public"));
iconList = iconList.concat(await this._getIcons(fileObject, "public", regex));
}
} catch {}

try {
let fileObject = await FilePicker.browse("data", path);

if (fileObject.target && fileObject.target !== ".") {
iconList = iconList.concat(await this._getIcons(fileObject, "data"));
iconList = iconList.concat(await this._getIcons(fileObject, "data", regex));
}
} catch {}

Expand All @@ -594,15 +601,20 @@ export default class NPCGenerator extends FormApplication {
/**
* @param {Object} fileObject
* @param {String} source
* @param {String?} regex
*/
async _getIcons(fileObject, source) {
async _getIcons(fileObject, source, regex) {
/** @type {String[]} */
let iconList = [];

/** @type {String[]} */
let files = fileObject?.files;
if (Array.isArray(files)) {
iconList = iconList.concat(files);
if (regex?.length) {
iconList = iconList.concat(files.filter((file) => new RegExp(regex).test(file)));
} else {
iconList = iconList.concat(files);
}
}

/** @type {String[]} */
Expand All @@ -612,7 +624,11 @@ export default class NPCGenerator extends FormApplication {
let newFileObject = await FilePicker.browse(source, folderPath);
let folderContent = await this._getIcons(newFileObject, source);

iconList = iconList.concat(folderContent);
if (regex?.length) {
iconList = iconList.concat(folderContent.filter((file) => new RegExp(regex).test(file)));
} else {
iconList = iconList.concat(folderContent);
}
});
}

Expand Down
24 changes: 21 additions & 3 deletions templates/imageSettings.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

</div>
<div class="th">
All
{{localize npcGen.default}}
</div>
<div class="th-container">
{{#each classes}}
Expand All @@ -28,6 +28,15 @@
id="{{concat ../this this}}"
name="{{concat ../this this}}"
value="{{getProperty @root.settings (concat ../this this)}}"
placeholder="&nbsp;"
/>
<input
type="text"
class="npcGenRegexInput"
id="{{concat ../this this 'Regex'}}"
name="{{concat ../this this 'Regex'}}"
value="{{getProperty @root.settings (concat ../this this 'Regex')}}"
placeholder="/.*/"
/>
</div>
<div class="classSpecific">
Expand All @@ -41,6 +50,15 @@
id="{{concat ../../this ../this this}}"
name="{{concat ../../this ../this this}}"
value="{{getProperty @root.settings (concat ../../this ../this this)}}"
placeholder="&nbsp;"
/>
<input
type="text"
class="npcGenRegexInput"
id="{{concat ../../this ../this this 'Regex'}}"
name="{{concat ../../this ../this this 'Regex'}}"
value="{{getProperty @root.settings (concat ../../this ../this this 'Regex')}}"
placeholder="/.*/"
/>
</div>
{{/each}}
Expand All @@ -50,7 +68,7 @@
{{/each}}
</div>
<div class="button-container">
<button type="submit">Save</button>
<button id="cancelButton" type="button">Cancel</button>
<button type="submit">{{localize "npcGen.save"}}</button>
<button id="cancelButton" type="button">{{localize "npcGen.cancel"}}</button>
</div>
</form>

0 comments on commit 2286a6d

Please sign in to comment.