Skip to content

Commit

Permalink
Add color indicator to enum fields
Browse files Browse the repository at this point in the history
  • Loading branch information
misode committed Nov 27, 2024
1 parent 95f4363 commit b1c8bba
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/app/components/generator/McdocRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as core from '@spyglassmc/core'
import { ColorFormat } from '@spyglassmc/core'
import type { JsonPairNode } from '@spyglassmc/json'
import * as json from '@spyglassmc/json'
import { JsonArrayNode, JsonBooleanNode, JsonNode, JsonNumberNode, JsonObjectNode, JsonStringNode } from '@spyglassmc/json'
Expand Down Expand Up @@ -260,13 +261,23 @@ function EnumHead({ type, optional, excludeStrings, node, ctx }: Props<Simplifie
})
}, [type.enumKind, value, ctx])

return <select value={value === undefined ? SPECIAL_UNSET : value} onInput={(e) => onChangeValue((e.target as HTMLSelectElement).value)}>
{(value === undefined || optional) && <option value={SPECIAL_UNSET}>{locale('unset')}</option>}
{(value !== undefined && !type.values.map(v => v.value).includes(value)) && <option value={value}>{value}</option>}
{type.values.filter(v => !excludeStrings?.includes(v.value.toString())).map(value =>
<option value={value.value}>{formatIdentifier(value.identifier, value.attributes)}</option>
)}
</select>
const color = type.attributes?.find(a => a.name === 'color')?.value
const colorKind = color?.kind === 'literal' && color.value.kind === 'string' ? color.value.value : undefined
const nodeColor = node?.color ? (Array.isArray(node.color) ? node.color : node.color.value ) : undefined
const inputColor = nodeColor ? core.ColorPresentation.fromColorFormat(ColorFormat.HexRGB, nodeColor, core.Range.create(0)).text : undefined

return <>
<select value={value === undefined ? SPECIAL_UNSET : value} onInput={(e) => onChangeValue((e.target as HTMLSelectElement).value)}>
{(value === undefined || optional) && <option value={SPECIAL_UNSET}>{locale('unset')}</option>}
{(value !== undefined && !type.values.map(v => v.value).includes(value)) && <option value={value}>{value}</option>}
{type.values.filter(v => !excludeStrings?.includes(v.value.toString())).map(value =>
<option value={value.value}>{formatIdentifier(value.identifier, value.attributes)}</option>
)}
</select>
{colorKind === 'named' && nodeColor && <>
<input class="short-input" type="color" value={inputColor} readonly disabled />
</>}
</>
}

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

0 comments on commit b1c8bba

Please sign in to comment.