From 896b6fcb396291368159b545395b397b4795b6c6 Mon Sep 17 00:00:00 2001 From: mikael-lovqvists-claude-agent Date: Sat, 21 Mar 2026 00:15:37 +0000 Subject: [PATCH] Fix template formatters: expose c.fields by name not ID c.fields was keyed by generated field IDs, so c.fields?.resistance was always undefined. Now fields are remapped by name before being passed to formatters, so the documented API works as expected. Co-Authored-By: Claude Sonnet 4.6 --- public/app.mjs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/public/app.mjs b/public/app.mjs index 54fb259..596726e 100644 --- a/public/app.mjs +++ b/public/app.mjs @@ -90,10 +90,23 @@ function compile_templates() { } } +// Build a version of the component where c.fields is keyed by field name +// instead of field ID, so formatters can use c.fields?.resistance etc. +function named_fields_comp(comp) { + const fields = {}; + for (const [fid, val] of Object.entries(comp.fields ?? {})) { + const def = field_by_id(fid); + if (def) { fields[def.name] = val; } + } + return { ...comp, fields }; +} + function component_display_name(comp) { + if (!compiled_formatters.length) return comp.name; + const c = named_fields_comp(comp); for (const { fn } of compiled_formatters) { try { - const result = fn(comp); + const result = fn(c); if (result != null && result !== '') return String(result); } catch (_) { // formatter threw — skip it @@ -609,7 +622,7 @@ function update_tmpl_preview() { try { // eslint-disable-next-line no-new-func const fn = new Function('c', `"use strict"; return (${formatter_str})(c);`); - const result = fn(sample); + const result = fn(named_fields_comp(sample)); preview_el.textContent = result != null ? String(result) : `null — falls back to "${sample.name}"`; } catch (err) { preview_el.textContent = `Error: ${err.message}`;