Skip to content

Commit b181a1a

Browse files
Copilottarebyte
andcommitted
Improve transformation function based on code review feedback
Co-authored-by: tarebyte <564113+tarebyte@users.noreply.github.com>
1 parent 58417fc commit b181a1a

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

src/colors.js

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,19 @@ function getColors(theme) {
7373
}
7474

7575
// Transform new flat structure (e.g., "fgColor-default") to old nested structure (e.g., fg.default)
76+
// The new primitives use a flat key structure with hyphenated names, where each value is an object
77+
// with a `.value` property. This function transforms that to the old nested object structure
78+
// for backward compatibility with the existing theme.js code.
79+
//
80+
// Input format: { "fgColor-default": { value: "#1f2328", ... }, ... }
81+
// Output format: { fg: { default: "#1f2328" }, ... }
7682
function transformToNestedStructure(flatColors) {
7783
const nested = {
7884
scale: {
79-
black: '#1f2328',
80-
white: '#ffffff',
85+
// Note: black and white are not in the scale arrays in new primitives,
86+
// so we extract them from the flat structure if available, or use fallback values
87+
black: flatColors['bgColor-black']?.value || '#1f2328',
88+
white: flatColors['bgColor-white']?.value || '#ffffff',
8189
gray: [],
8290
blue: [],
8391
green: [],
@@ -174,16 +182,35 @@ function transformToNestedStructure(flatColors) {
174182
else if (prop === 'onEmphasis') nested.fg.onEmphasis = val;
175183
}
176184

185+
// Helper to check if a key should be mapped to canvas
186+
const isCanvasBgColor = (key) => {
187+
return key.startsWith('bgColor-') &&
188+
!key.startsWith('bgColor-accent-') &&
189+
!key.startsWith('bgColor-danger-') &&
190+
!key.startsWith('bgColor-attention-') &&
191+
!key.startsWith('bgColor-success-') &&
192+
!key.startsWith('bgColor-neutral-');
193+
};
194+
195+
// Helper to check if a key should be mapped to border
196+
const isBorderColor = (key) => {
197+
return key.startsWith('borderColor-') &&
198+
!key.startsWith('borderColor-accent-') &&
199+
!key.startsWith('borderColor-danger-') &&
200+
!key.startsWith('borderColor-attention-') &&
201+
!key.startsWith('borderColor-success-');
202+
};
203+
177204
// Map bgColor-* to canvas.*
178-
else if (key.startsWith('bgColor-') && !key.startsWith('bgColor-accent-') && !key.startsWith('bgColor-danger-') && !key.startsWith('bgColor-attention-') && !key.startsWith('bgColor-success-') && !key.startsWith('bgColor-neutral-')) {
205+
if (isCanvasBgColor(key)) {
179206
if (key === 'bgColor-default') nested.canvas.default = val;
180207
else if (key === 'bgColor-overlay') nested.canvas.overlay = val;
181208
else if (key === 'bgColor-inset') nested.canvas.inset = val;
182209
else if (key === 'bgColor-muted') nested.canvas.subtle = val;
183210
}
184211

185212
// Map borderColor-* to border.*
186-
else if (key.startsWith('borderColor-') && !key.startsWith('borderColor-accent-') && !key.startsWith('borderColor-danger-') && !key.startsWith('borderColor-attention-') && !key.startsWith('borderColor-success-')) {
213+
else if (isBorderColor(key)) {
187214
if (key === 'borderColor-default') nested.border.default = val;
188215
else if (key === 'borderColor-muted') nested.border.muted = val;
189216
}
@@ -241,16 +268,19 @@ function transformToNestedStructure(flatColors) {
241268
else if (ansiColor === 'cyan-bright') nested.ansi.cyanBright = val;
242269
}
243270

244-
// Map display scale colors
271+
// Map display scale colors (e.g., display-blue-scale-6)
272+
// Expected format: "display-{color}-scale-{index}"
245273
else if (key.startsWith('display-') && key.includes('-scale-')) {
246274
const parts = key.split('-');
247-
const color = parts[1]; // e.g., "blue"
248-
const index = parseInt(parts[3]); // e.g., "6"
249-
250-
if (!nested.scale[color]) {
251-
nested.scale[color] = [];
275+
if (parts.length >= 4 && parts[0] === 'display' && parts[2] === 'scale') {
276+
const color = parts[1]; // e.g., "blue"
277+
const indexStr = parts[3]; // e.g., "6"
278+
const index = parseInt(indexStr, 10);
279+
280+
if (!isNaN(index) && nested.scale[color]) {
281+
nested.scale[color][index] = val;
282+
}
252283
}
253-
nested.scale[color][index] = val;
254284
}
255285

256286
// Map codemirror activelineBg

0 commit comments

Comments
 (0)