Skip to content

Commit b1c8bba

Browse files
committed
Add color indicator to enum fields
1 parent 95f4363 commit b1c8bba

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/app/components/generator/McdocRenderer.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as core from '@spyglassmc/core'
2+
import { ColorFormat } from '@spyglassmc/core'
23
import type { JsonPairNode } from '@spyglassmc/json'
34
import * as json from '@spyglassmc/json'
45
import { JsonArrayNode, JsonBooleanNode, JsonNode, JsonNumberNode, JsonObjectNode, JsonStringNode } from '@spyglassmc/json'
@@ -260,13 +261,23 @@ function EnumHead({ type, optional, excludeStrings, node, ctx }: Props<Simplifie
260261
})
261262
}, [type.enumKind, value, ctx])
262263

263-
return <select value={value === undefined ? SPECIAL_UNSET : value} onInput={(e) => onChangeValue((e.target as HTMLSelectElement).value)}>
264-
{(value === undefined || optional) && <option value={SPECIAL_UNSET}>{locale('unset')}</option>}
265-
{(value !== undefined && !type.values.map(v => v.value).includes(value)) && <option value={value}>{value}</option>}
266-
{type.values.filter(v => !excludeStrings?.includes(v.value.toString())).map(value =>
267-
<option value={value.value}>{formatIdentifier(value.identifier, value.attributes)}</option>
268-
)}
269-
</select>
264+
const color = type.attributes?.find(a => a.name === 'color')?.value
265+
const colorKind = color?.kind === 'literal' && color.value.kind === 'string' ? color.value.value : undefined
266+
const nodeColor = node?.color ? (Array.isArray(node.color) ? node.color : node.color.value ) : undefined
267+
const inputColor = nodeColor ? core.ColorPresentation.fromColorFormat(ColorFormat.HexRGB, nodeColor, core.Range.create(0)).text : undefined
268+
269+
return <>
270+
<select value={value === undefined ? SPECIAL_UNSET : value} onInput={(e) => onChangeValue((e.target as HTMLSelectElement).value)}>
271+
{(value === undefined || optional) && <option value={SPECIAL_UNSET}>{locale('unset')}</option>}
272+
{(value !== undefined && !type.values.map(v => v.value).includes(value)) && <option value={value}>{value}</option>}
273+
{type.values.filter(v => !excludeStrings?.includes(v.value.toString())).map(value =>
274+
<option value={value.value}>{formatIdentifier(value.identifier, value.attributes)}</option>
275+
)}
276+
</select>
277+
{colorKind === 'named' && nodeColor && <>
278+
<input class="short-input" type="color" value={inputColor} readonly disabled />
279+
</>}
280+
</>
270281
}
271282

272283
function NumericHead({ type, node, ctx }: Props<NumericType>) {

0 commit comments

Comments
 (0)