Skip to content

Commit

Permalink
feat(tools): add option to keep images when parsing items
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGiddyLimit committed Jan 8, 2023
1 parent 849348d commit b1c3be5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
41 changes: 37 additions & 4 deletions tool/public/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class ConverterUi {
static _iptFile = null;
static _iptText = null;
static _cbKeepImg = null;
static _btnConvert = null;
static _btnCopy = null;
static _outText = null;
Expand Down Expand Up @@ -60,12 +61,22 @@ class ConverterUi {
}

static _doConvert_ () {
this._outText.value = JSON.stringify(Converter.getConverted(JSON.parse(this._iptText.value)), null, "\t");
this._outText.value = JSON.stringify(
Converter.getConverted(
JSON.parse(this._iptText.value),
{
isKeepImg: this._cbKeepImg.checked,
},
),
null,
"\t",
);
}

static init () {
this._iptFile = document.getElementById("ipt-file");
this._iptText = document.getElementById("ipt-text");
this._cbKeepImg = document.getElementById("cb-keep-img");
this._btnConvert = document.getElementById("btn-convert");
this._btnCopy = document.getElementById("btn-copy");
this._outText = document.getElementById("out-text");
Expand All @@ -92,24 +103,32 @@ class ConverterUtil {
}

class Converter {
static getConverted (json) {
static getConverted (json, {isKeepImg = false} = {}) {
if (json instanceof Array) {
const [, ...rest] = arguments;
return json
.sort((a, b) => (a.flags?.srd5e?.page || "").localeCompare(b.flags?.srd5e?.page || "")
|| a.type.localeCompare(b.type)
|| a.name.localeCompare(b.name, {sensitivity: "base"}))
.map(it => this.getConverted(it));
.map(it => this.getConverted(it, ...rest));
}

const effects = EffectConverter.getEffects(json);
const flags = FlagConverter.getFlags(json);

return {
const out = {
name: json.name,
source: this._getSource(json),
effects,
flags,
};

if (isKeepImg) {
const img = ImgConverter.getImg(json);
if (img) out.img = img;
}

return out;
}

static _getSource (json) {
Expand All @@ -133,6 +152,7 @@ class FlagConverter {
switch (k) {
// region Discard these
case "srd5e":
case "plutonium":
case "core":
case "favtab": // https://foundryvtt.com/packages/favtab
case "magicitems": // https://foundryvtt.com/packages/magicitems
Expand Down Expand Up @@ -316,4 +336,17 @@ class EffectConverter {
}
}

class ImgConverter {
static _IGNORED_IMGS = new Set([
"icons/svg/mystery-man.svg",
"icons/svg/item-bag.svg",
"icons/svg/aura.svg",
]);

static getImg (json) {
if (json.img && !this._IGNORED_IMGS.has(json.img)) return json.img;
return null;
}
}

window.addEventListener("load", () => ConverterUi.init());
11 changes: 9 additions & 2 deletions tool/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
.min-h-0 {
min-height: 0 !important;
}

.w-80p {
width: 80px !important;
}
Expand All @@ -31,7 +31,14 @@
<textarea class="form-control h-100 min-h-0 lh-1 font-monospace" id="ipt-text"></textarea>
</div>
<div class="col-6 h-100 d-flex flex-column">
<div class="mb-3">
<div class="d-flex align-items-center mb-3">
<label class="me-2">
<span>Keep <code>img</code>s</span>
<input type="checkbox" id="cb-keep-img">
</label>

<div class="vr me-2"></div>

<button type="button" class="btn btn-primary btn-sm me-1 w-80p" id="btn-convert">Convert</button>
<button type="button" class="btn btn-primary btn-sm me-1 w-80p" id="btn-copy">Copy</button>
</div>
Expand Down

0 comments on commit b1c3be5

Please sign in to comment.