diff --git a/module/helpers/object-helper.mjs b/module/helpers/object-helper.mjs index 4feb96b..409954d 100644 --- a/module/helpers/object-helper.mjs +++ b/module/helpers/object-helper.mjs @@ -19,6 +19,10 @@ export function copyAndMutateAtPath(obj, fieldPath, val) { } function mutateAtPath(obj, fieldPath, val) { + if(fieldPath === "") { + return val; + } + const pathSequence = fieldPath.split('.'); let pointer = obj; @@ -34,4 +38,55 @@ function mutateAtPath(obj, fieldPath, val) { export function deepCopy(obj) { return structuredClone(obj); +} + +export function getDataPathFromString(dataPathString) { + const arraySplit = dataPathString.indexOf('['); + + const isArray = arraySplit > 0; + + if(isArray) { + const preTokens = dataPathString.split('['); + const postTokens = preTokens[1].split(']'); + + const prefix = preTokens[0]; + const postfixRaw = postTokens[1]; + const postfix = postfixRaw.indexOf('.') === 0 ? postfixRaw.slice(1) : postfixRaw; + const index = parseInt(postTokens[0]); + + return { + path: prefix, + isArray: true, + index: index, + subPath: postfix, + }; + } else { + return { + path: dataPathString, + isArray: false, + index: 0, + subPath: null, + }; + } +} + +export function updateField(actor, dataPathString, value) { + const dataPath = getDataPathFromString(dataPathString); + + console.log(`Converted ${dataPathString} to:`); + console.log(dataPath); + + if(dataPath.isArray) { + const initial = getValueAtPath(actor, dataPath.path); + const copy = initial.map(e => deepCopy(e)); + copy[dataPath.index] = copyAndMutateAtPath(initial[dataPath.index], dataPath.subPath, value); + + actor.update({ + [dataPath.path]: copy, + }); + } else { + actor.update({ + [dataPath.path]: value + }); + } } \ No newline at end of file diff --git a/module/sheets/hench-debug.mjs b/module/sheets/hench-debug.mjs index 08149d2..a270018 100644 --- a/module/sheets/hench-debug.mjs +++ b/module/sheets/hench-debug.mjs @@ -1,5 +1,5 @@ import { playbookKeys, validatePlaybookKey, getPlaybookMutation } from "../playbooks.mjs"; -import { getValueAtPath, copyAndMutateAtPath, deepCopy } from "../helpers/object-helper.mjs"; +import { getValueAtPath, copyAndMutateAtPath, deepCopy, updateField } from "../helpers/object-helper.mjs"; export class HenchDebugSheet extends ActorSheet { /** @override */ @@ -28,25 +28,10 @@ export class HenchDebugSheet extends ActorSheet { // boolean checkboxes html.find('.hench-checkbox-toggle-field').on('change', (event) => { const element = event.currentTarget; - - const isArrayPath = element.dataset.isArray; - const fieldPath = element.dataset.fieldPath; - const index = element.dataset.index; - const subPath = element.dataset.subPath; - + const path = element.dataset.fieldPath; const value = element.checked; - if(isArrayPath) { - const array = getValueAtPath(this.actor, fieldPath); - - const copy = array.map(e => deepCopy(e)); - - copy[index] = copyAndMutateAtPath(array[index], subPath, value); - - this.actor.update({[fieldPath]: copy}); - } else { - this.actor.update({[fieldPath]: value}); - } + updateField(this.actor, path, value); }); } diff --git a/templates/hench-debug.hbs b/templates/hench-debug.hbs index 8fae5f9..a4416bd 100644 --- a/templates/hench-debug.hbs +++ b/templates/hench-debug.hbs @@ -42,7 +42,7 @@

Gear