mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-25 08:06:51 +00:00
Improve rendering and validating block states
This commit is contained in:
@@ -1,31 +1,40 @@
|
||||
import type { Hook } from '@mcschema/core'
|
||||
import { relativePath } from '@mcschema/core'
|
||||
import type { BlockStateRegistry } from '../Schemas'
|
||||
|
||||
export const transformOutput: Hook<[any], any> = {
|
||||
export type OutputProps = {
|
||||
blockStates: BlockStateRegistry,
|
||||
}
|
||||
export const transformOutput: Hook<[any, OutputProps], any> = {
|
||||
base({}, _, value) {
|
||||
return value
|
||||
},
|
||||
|
||||
choice({ switchNode }, path, value) {
|
||||
return switchNode.hook(this, path, value)
|
||||
choice({ switchNode }, path, value, props) {
|
||||
return switchNode.hook(this, path, value, props)
|
||||
},
|
||||
|
||||
list({ children }, path, value) {
|
||||
list({ children }, path, value, props) {
|
||||
if (!Array.isArray(value)) return value
|
||||
return value.map((obj, index) =>
|
||||
children.hook(this, path.push(index), obj)
|
||||
children.hook(this, path.push(index), obj, props)
|
||||
)
|
||||
},
|
||||
|
||||
map({ children }, path, value) {
|
||||
map({ children, config }, path, value, props) {
|
||||
if (value === undefined) return undefined
|
||||
const blockState = config.validation?.validator === 'block_state_map'? props.blockStates?.[relativePath(path, config.validation.params.id).get()] : null
|
||||
const res: any = {}
|
||||
Object.keys(value).forEach(f =>
|
||||
res[f] = children.hook(this, path.push(f), value[f])
|
||||
)
|
||||
Object.keys(value).forEach(f => {
|
||||
if (blockState) {
|
||||
if (!Object.keys(blockState.properties).includes(f)) return
|
||||
}
|
||||
res[f] = children.hook(this, path.push(f), value[f], props)
|
||||
})
|
||||
return res
|
||||
},
|
||||
|
||||
object({ getActiveFields }, path, value) {
|
||||
object({ getActiveFields }, path, value, props) {
|
||||
if (value === undefined || value === null || typeof value !== 'object') {
|
||||
return value
|
||||
}
|
||||
@@ -34,7 +43,7 @@ export const transformOutput: Hook<[any], any> = {
|
||||
Object.keys(activeFields)
|
||||
.filter(k => activeFields[k].enabled(path))
|
||||
.forEach(f => {
|
||||
res[f] = activeFields[f].hook(this, path.push(f), value[f])
|
||||
res[f] = activeFields[f].hook(this, path.push(f), value[f], props)
|
||||
})
|
||||
return res
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user