From 23205c514e2678a70054bfbf5bec7d0e3e8c7001 Mon Sep 17 00:00:00 2001 From: John Perkins Date: Thu, 28 May 2026 16:40:25 -0400 Subject: [PATCH] Fix FieldColorNumber crash when no palette is configured makeSwatches() was called with an undefined colour list whenever a colour field had no `colours` field option AND the target defined no runtime.palette, throwing "Cannot read properties of undefined (reading 'map')". Because this happens during flyout show() block-inflation, the exception aborts show() before layout_ runs, leaving every block in that category unpositioned (piled at the flyout origin). Older pxt tolerated a missing palette; restore that by falling back to a default palette when neither field colours nor runtime.palette is set. Co-Authored-By: Claude Opus 4.8 (1M context) --- pxtblocks/fields/field_colour.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pxtblocks/fields/field_colour.ts b/pxtblocks/fields/field_colour.ts index 1b525d076984..7cf46ca1be15 100644 --- a/pxtblocks/fields/field_colour.ts +++ b/pxtblocks/fields/field_colour.ts @@ -17,6 +17,19 @@ import { FieldCustom, FieldCustomOptions } from './field_utils'; */ export type FieldColourValueMode = "hex" | "rgb" | "index"; +/** + * Fallback palette used when a colour field is created without explicit + * `colours` field options and the target defines no `runtime.palette`. Older + * pxt tolerated a missing palette; without this guard makeSwatches() receives + * `undefined` and throws, which aborts the whole flyout layout (every block in + * the category is left unpositioned). This is the canonical MakeCode palette. + */ +const DEFAULT_COLOUR_PALETTE: string[] = [ + "#000000", "#ffffff", "#ff2121", "#ff93c4", "#ff8135", "#fff609", + "#249ca3", "#78dc52", "#003fad", "#87f2ff", "#8e2ec4", "#a4839f", + "#5c406c", "#e5cdc4", "#91463d", "#000000" +]; + export interface FieldColourNumberOptions extends FieldCustomOptions { colours?: string; // parsed as a JSON array columns?: string; // parsed as a number @@ -64,6 +77,11 @@ export class FieldColorNumber extends FieldGridDropdown implements FieldCustom { titles[0] = lf("transparent"); } } + if (!allColoursCSSFormat) { + // No field-level colours and no target palette: fall back so + // makeSwatches doesn't throw and break the flyout layout. + allColoursCSSFormat = DEFAULT_COLOUR_PALETTE.slice(); + } super(makeSwatches(valueMode, allColoursCSSFormat, titles), opt_validator, { primaryColour: "white", borderColour: "#dadce0",