Refactor datapath logic to be seamless
This commit is contained in:
		
							parent
							
								
									47686bf761
								
							
						
					
					
						commit
						f5b414b6b0
					
				| @ -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 | ||||
|         }); | ||||
|     } | ||||
| } | ||||
| @ -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); | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -42,7 +42,7 @@ | ||||
|         <h3>Gear</h3> | ||||
|         <ul> | ||||
|             {{#each actor.system.gear}} | ||||
|                 <li><input type="checkbox" class="hench-checkbox-toggle-field" data-is-array="true" data-field-path="system.fixedGear" data-index="{{@index}}" data-sub-path="marked" {{#if this.marked}}checked{{/if}}/> {{this.description}}</li> | ||||
|                 <li><input type="checkbox" class="hench-checkbox-toggle-field" data-field-path="system.fixedGear[{{@index}}].marked" {{#if this.marked}}checked{{/if}}/> {{this.description}}</li> | ||||
|             {{/each}} | ||||
|         </ul> | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 walcutt
						walcutt