Files
misode.github.io/src/app/schema/transformOutput.ts
Misode c6c52ca41a Projects (#192)
* Add file save UI and drafts project

* Fix build

* Create SearchList component as abstraction

* Add project page and file tree view

* Create Locale context

* Create Theme context

* Create Version context

* Create Title context

* Create Project context

* Store current file in project context

* Fix issues when renaming file and implement deleting

* Style improvements

* Make all project strings translatable

* Fix z-index
2022-01-18 01:02:19 +01:00

68 lines
2.0 KiB
TypeScript

import type { DataModel, Hook } from '@mcschema/core'
import { ModelPath, relativePath } from '@mcschema/core'
import type { BlockStateRegistry } from '../services'
export function getOutput(model: DataModel, blockStates: BlockStateRegistry): any {
return model.schema.hook(transformOutput, new ModelPath(model), model.data, { blockStates })
}
export type OutputProps = {
blockStates: BlockStateRegistry,
}
export const transformOutput: Hook<[any, OutputProps], any> = {
base({}, _, value) {
return value
},
choice({ switchNode }, path, value, props) {
return switchNode.hook(this, path, value, props)
},
list({ children }, path, value, props) {
if (!Array.isArray(value)) return value
const res = value.map((obj, index) =>
children.hook(this, path.push(index), obj.node, props)
)
for (const a of Object.getOwnPropertySymbols(value)) {
res[a as any] = value[a as any]
}
return res
},
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 => {
if (blockState) {
if (!Object.keys(blockState.properties ?? {}).includes(f)) return
}
res[f] = children.hook(this, path.push(f), value[f], props)
})
for (const a of Object.getOwnPropertySymbols(value)) {
res[a as any] = value[a]
}
return res
},
object({ getActiveFields }, path, value, props) {
if (value === undefined || value === null || typeof value !== 'object') {
return value
}
const res: any = {}
const activeFields = getActiveFields(path)
Object.keys(activeFields)
.filter(k => activeFields[k].enabled(path))
.forEach(f => {
const out = activeFields[f].hook(this, path.push(f), value[f], props)
if (out !== undefined && out !== null) {
res[f] = out
}
})
for (const a of Object.getOwnPropertySymbols(value)) {
res[a as any] = value[a]
}
return res
},
}