Skip to content

Commit

Permalink
adapt to null return for api element helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
reonZ committed May 13, 2024
1 parent 7bcd83d commit 3ec1e3a
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 45 deletions.
3 changes: 2 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const firstSettings: { tool: string; key: string }[] = [];
function onRenderSettingsConfig(app: SettingsConfig, $html: JQuery) {
const html = htmlElement($html);
const tab = querySelector(html, `.tab[data-tab="${MODULE.id}"]`);
if (!tab) return;

for (const { tool, key } of firstSettings) {
const group = tab.querySelector(`[data-setting-id="${MODULE.id}.${key}"]`);
Expand All @@ -31,7 +32,7 @@ function onRenderSettingsConfig(app: SettingsConfig, $html: JQuery) {
const setting = game.settings.settings.get(settingId)!;

if (setting.requiresReload) {
const label = querySelector(group, "label");
const label = querySelector(group, "label")!;
label.textContent = `${label.textContent} ${localize("settings.requiresReload")}`;
}
}
Expand Down
19 changes: 13 additions & 6 deletions src/tools/actionable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,24 @@ async function actionSheetPF2eRenderInner(
const html = htmlElement($html);
const label = localize("itemSheet.label");
const group = querySelector(html, "[data-drop-zone='self-applied-effect']");
const dropZone = group.querySelector(".drop-zone.empty");
const macro = await getActionMacro(item);
if (!group) return $html;

const labelEl = querySelector(group, ":scope > label");
const hintEl = querySelector(group, ".hint");

querySelector(group, ":scope > label").innerText += ` / ${label}`;
querySelector(group, ".hint").innerText += localize("itemSheet.hint");
if (labelEl) labelEl.innerText += ` / ${label}`;
if (hintEl) hintEl.innerText += localize("itemSheet.hint");

const dropZone = group?.querySelector(".drop-zone.empty");
if (!dropZone) return $html;

const macro = await getActionMacro(item);

if (macro) {
dropZone.outerHTML = await render("dropzone", { macro });
} else {
querySelector(dropZone, ".name").innerText += ` / ${label}`;
const nameEl = querySelector(dropZone, ".name");
if (nameEl) nameEl.innerText += ` / ${label}`;
}

return $html;
Expand All @@ -187,11 +193,12 @@ function actionSheetPF2eActivateListeners(
item.system.actionType.value === "passive" ||
item.permission <= CONST.DOCUMENT_PERMISSION_LEVELS.LIMITED
) {
return $html;
return;
}

const html = htmlElement($html);
const group = querySelector(html, "[data-drop-zone='self-applied-effect']");
if (!group) return;

addListener(group, "[data-action='open-macro-sheet']", async () => {
const macro = await getActionMacro(item);
Expand Down
39 changes: 25 additions & 14 deletions src/tools/betterMerchant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
ErrorPF2e,
R,
addListener,
addListenerAll,
Expand Down Expand Up @@ -496,6 +495,7 @@ async function browserRenderInner(
html,
"section.content .tab[data-tab='equipment'] .list-buttons"
);
if (!listButtons) return $html;

html.classList.add("toolbelt-merchant");

Expand Down Expand Up @@ -611,6 +611,8 @@ function updateBrowser(selection: string[], skipAll = false) {
if (!browserTab) return;

const listButtons = querySelector(browserTab, ".list-buttons");
if (!listButtons) return;

const tab = game.pf2e.compendiumBrowser.tabs.equipment;
const selected = selection.length;
const total = tab.currentIndex.length;
Expand Down Expand Up @@ -640,7 +642,7 @@ function updateBrowser(selection: string[], skipAll = false) {
}
}

querySelector<HTMLButtonElement>(listButtons, "button").disabled = selected === 0;
querySelector<HTMLButtonElement>(listButtons, "button")!.disabled = selected === 0;

for (const checkbox of checkboxes) {
const checked = checkbox.checked;
Expand All @@ -661,12 +663,14 @@ function browserActivateListeners(
const data = getInMemory<BrowserData>(this);
if (!data?.actor?.isMerchant) return;

const actor = data.actor;
const html = htmlElement($html);
const browser = this;
const tab = browser.tabs.equipment;
const tabEl = querySelector(html, "section.content .tab[data-tab='equipment']");
const listButtons = querySelector(tabEl, ".list-buttons");
if (!listButtons) return;

const actor = data.actor;
const browser = this;
const tab = browser.tabs.equipment;

if (data.type === "pull") {
addListener(listButtons, "[data-action='add-to-merchant']", async () => {
Expand Down Expand Up @@ -707,7 +711,7 @@ function browserActivateListeners(
selection.length = 0;
}

const checkboxes = tabEl.querySelectorAll<HTMLInputElement>(".item input");
const checkboxes = tabEl!.querySelectorAll<HTMLInputElement>(".item input");
for (const checkbox of checkboxes) {
const { uuid } = elementData(checkbox);
checkbox.checked = selection.includes(uuid);
Expand Down Expand Up @@ -827,25 +831,31 @@ async function lootSheetPF2eRenderInner(
if (filter) {
const priceElement = querySelector(itemElement, ".price");

priceElement.classList.add(filter.ratio < 1 ? "cheap" : "expensive");
priceElement.dataset.tooltip = `${filter.name} (${filter.ratio})`;
if (priceElement) {
priceElement.classList.add(filter.ratio < 1 ? "cheap" : "expensive");
priceElement.dataset.tooltip = `${filter.name} (${filter.ratio})`;
}
}
}

if (infiniteAll) {
const quantityElement = querySelector(itemElement, ".quantity");
quantityElement.innerHTML = INFINITE;
if (quantityElement) quantityElement.innerHTML = INFINITE;
}
}

if (isGM && infiniteAll) {
const bulkElement = querySelector(html, ".total-bulk span");
const wealthElement = querySelector(html, ".coinage .wealth .item-name:last-child span");

wealthElement.innerHTML = INFINITE;
bulkElement.innerHTML = game.i18n.format("PF2E.Actor.Inventory.TotalBulk", {
bulk: INFINITE,
});
if (bulkElement) {
bulkElement.innerHTML = game.i18n.format("PF2E.Actor.Inventory.TotalBulk", {
bulk: INFINITE,
});
}
if (wealthElement) {
wealthElement.innerHTML = INFINITE;
}
}

return $html;
Expand All @@ -863,6 +873,7 @@ function lootSheetPF2eActivateListeners(

const html = htmlElement($html);
const betterMenu = querySelector(html, ".better-merchant");
if (!betterMenu) return;

addListener(betterMenu, "[data-action='open-equipment-tab']", () => {
openEquipmentTab({ actor, type: "pull", owned: [] });
Expand Down Expand Up @@ -955,7 +966,7 @@ class FiltersMenu extends Application {

const getItemData = (el: HTMLElement) => {
const data = elementData<{ id: string; type: ItemFilterType }>(
closest(el, "[data-id]")
closest(el, "[data-id]")!
);
const { type, id } = data;

Expand Down
3 changes: 3 additions & 0 deletions src/tools/effectsPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ function onRenderEffectsPanel(panel: EffectsPanel, $html: JQuery) {
effect.slug !== "persistent-damage"
) {
const h1 = querySelector(effectsPanel, ".effect-info > h1");
if (!h1) continue;

appendHTMLFromString(h1, editIcon);
addListener(h1, "[data-action='edit']", (event, el) =>
Expand Down Expand Up @@ -117,6 +118,8 @@ function onEditCondition(event: MouseEvent, el: HTMLElement, actor: ActorPF2e) {

function getEffect(target: HTMLElement, actor: ActorPF2e) {
const effectEl = closest(target, ".effect-item[data-item-id]");
if (!effectEl) return;

const { itemId } = elementData(effectEl);
return actor.items.get<EffectPF2e>(itemId);
}
Expand Down
40 changes: 28 additions & 12 deletions src/tools/heroActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ function characterSheetPF2eActivateListeners(this: CharacterSheetPF2e, html: HTM
const actor = this.actor;
const tab = querySelector(html, ".tab[data-tab=actions] .tab-content .tab[data-tab=encounter]");
const list = querySelector(tab, ".heroActions-list");
if (!list || !tab) return;

addListener(tab, "[data-action='hero-actions-draw']", () => drawHeroActions(actor));
addListener(tab, "[data-action='hero-actions-trade']", () => tradeHeroAction(actor, this));
Expand All @@ -187,6 +188,8 @@ function characterSheetPF2eActivateListeners(this: CharacterSheetPF2e, html: HTM
) => {
addListenerAll(list, `[data-action="${action}"]`, (event: Event, el: HTMLElement) => {
const actionEl = closest(el, "[data-uuid]");
if (!actionEl) return;

const { uuid } = elementData(actionEl);
callback(actor, uuid, actionEl);
});
Expand Down Expand Up @@ -305,7 +308,10 @@ async function onTradeRequest(packet: SocketPacket, senderId: string) {
content: await TextEditor.enrichHTML(content, { async: true }),
onRender: (html) => {
addListenerAll(html, "[data-action='description']", async (event, el) => {
const { uuid } = elementData(closest(el, ".action"));
const action = closest(el, ".action");
if (!action) return;

const { uuid } = elementData(action);
const entry = await fromUuid(uuid);
entry?.sheet.render(true);
});
Expand All @@ -324,7 +330,7 @@ async function onTradeRequest(packet: SocketPacket, senderId: string) {
packet.receiver.uuid = querySelector<HTMLInputElement>(
html,
"[name='action']:checked"
).value;
)!.value;
}

if (game.user.isGM) {
Expand Down Expand Up @@ -394,7 +400,10 @@ async function tradeHeroAction(actor: CharacterPF2e, app?: Application) {
});

addListenerAll(html, "[data-action='description']", async (event, el) => {
const { uuid } = elementData(closest(el, ".action"));
const action = closest(el, ".action");
if (!action) return;

const { uuid } = elementData(action);
const entry = await fromUuid(uuid);
entry?.sheet.render(true);
});
Expand All @@ -415,15 +424,20 @@ async function tradeHeroAction(actor: CharacterPF2e, app?: Application) {

if (html === null) return;

const targetId = querySelector<HTMLSelectElement>(html, ".header [name='target']").value;
const targetId = querySelector<HTMLSelectElement>(html, ".header [name='target']")?.value;
const target = game.actors.get<CharacterPF2e>(targetId);
if (!target) return;

const actionUuid = querySelector<HTMLInputElement>(html, ".left [name='action']:checked").value;
const actionUuid = querySelector<HTMLInputElement>(
html,
".left [name='action']:checked"
)?.value;
const targetActionUUid = html.querySelector<HTMLInputElement>(
`.right [name="action-${targetId}"]:checked`
)?.value;

if (!actionUuid || !targetActionUUid) return;

if (target.isOwner && targetActionUUid) {
const targetActions = others.find((x) => x.id === targetId)!.actions;

Expand Down Expand Up @@ -452,7 +466,7 @@ async function tradeHeroAction(actor: CharacterPF2e, app?: Application) {
},
receiver: {
id: getOwner(target)?.id ?? game.users.activeGM!.id,
cid: targetId,
cid: targetId!,
uuid: targetActionUUid,
},
});
Expand All @@ -476,7 +490,7 @@ function exchangeHeroActions(trader1: ExchangeObj, trader2: ExchangeObj, senderI

async function onExpandAction(actor: CharacterPF2e, uuid: string, actionEl: HTMLElement) {
const duration = 0.4;
const summaryEl = querySelector(actionEl, ".item-summary");
const summaryEl = querySelector(actionEl, ".item-summary")!;

if (!summaryEl.classList.contains("loaded")) {
const details = await getHeroActionDetails(uuid);
Expand Down Expand Up @@ -579,7 +593,7 @@ async function getHeroActionDetails(uuid: string) {
}

function onDiscardAction(actor: CharacterPF2e, uuid: string, el: HTMLElement) {
const parent = closest(el, ".actions-list");
const parent = closest(el, ".actions-list")!;
const toDiscard = Number(elementData(parent).discard);
const discarded = querySelectorArray(parent, ".item.discarded").map((x) => elementData(x).uuid);

Expand Down Expand Up @@ -813,6 +827,8 @@ async function giveHeroActions(actor: CharacterPF2e) {
onRender: (html) => {
addListenerAll(html, "[data-action='expand']", (event, el) => {
const actionEl = closest(el, "[data-uuid]");
if (!actionEl) return;

const { uuid } = elementData(actionEl);
onExpandAction(actor, uuid, actionEl);
});
Expand All @@ -822,11 +838,11 @@ async function giveHeroActions(actor: CharacterPF2e) {
if (!html) return;

const selected = querySelectorArray(html, "[name='action']:checked").map((el) => {
const parent = closest(el, ".action");
const parent = closest(el, ".action")!;
return elementData<{ uuid: string; name: string; key: string }>(parent);
});
const asDrawn = html.querySelector<HTMLInputElement>("[name='drawn']")?.checked;
const withMessage = querySelector<HTMLInputElement>(html, "[name='message']").checked;
const asDrawn = querySelector<HTMLInputElement>(html, "[name='drawn']")?.checked;
const withMessage = querySelector<HTMLInputElement>(html, "[name='message']")!.checked;
const tableUpdates = [];

for (const { uuid, name, key } of selected) {
Expand Down Expand Up @@ -860,7 +876,7 @@ async function removeHeroActions() {
const translate = localize.sub("removeActions");

const onRender = (html: HTMLElement) => {
const allInput = querySelector<HTMLInputElement>(html, "[name='all']");
const allInput = querySelector<HTMLInputElement>(html, "[name='all']")!;
const actorInputs = querySelectorArray<HTMLInputElement>(html, "[name='actor']");

allInput.addEventListener("change", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/mergeDamage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function onRenderChatMessage(message: ChatMessagePF2e, $html: JQuery) {
html.classList.add("merged");
}

querySelector(html, ".dice-result .dice-total").append(buttons);
querySelector(html, ".dice-result .dice-total")?.append(buttons);

addListener(buttons, "[data-action='merge-damage']", (event) => {
event.stopPropagation();
Expand Down
3 changes: 2 additions & 1 deletion src/tools/spellsSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function characterSheetPF2eActivateListeners(this: CharacterSheetPF2e, html: HTM

const spellcastingTab = getSpellcastingTab(html);
const spellsSummaryTab = querySelector(spellcastingTab, ".tab.spells-summary");
if (!spellsSummaryTab) return;

addListenerAll(
spellsSummaryTab,
Expand All @@ -111,7 +112,7 @@ function toggleSpellcastingTab(this: CharacterSheetPF2e, html: HTMLElement, navI
const isActive = querySelector(
spellcastingNav,
"[data-tab='spells-summary']"
).classList.contains("active");
)?.classList.contains("active");

const tabName = isActive ? "known-spells" : "spells-summary";
this.activateTab(tabName, { group: "spell-collections" });
Expand Down
4 changes: 3 additions & 1 deletion src/tools/stances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ function characterSheetPF2eActivateListeners(this: CharacterSheetPF2e, html: HTM
if (!actor.isOwner) return;

const encounterTab = getEncounterTab(html);
if (!encounterTab) return;

addListenerAll(encounterTab, ".pf2e-stances .stance", (event, el) => {
const uuid = elementData(el).effectUuid;
Expand Down Expand Up @@ -195,9 +196,10 @@ async function onCreateCombatant(combatant: CombatantPF2e) {
});

const effectUUID = html
? querySelector<HTMLInputElement>(html, "[name='stance']:checked").value
? querySelector<HTMLInputElement>(html, "[name='stance']:checked")?.value
: stances[0].effectUUID;

if (!effectUUID) return;
addStance(actor, effectUUID);
}

Expand Down
Loading

0 comments on commit 3ec1e3a

Please sign in to comment.