diff --git a/src/app/components/SourcePanel.tsx b/src/app/components/SourcePanel.tsx index d8eb012a..9c65fb4f 100644 --- a/src/app/components/SourcePanel.tsx +++ b/src/app/components/SourcePanel.tsx @@ -4,24 +4,27 @@ import { useEffect, useRef } from 'preact/hooks' import { useModel } from '../hooks' import { locale } from '../Locales' import { transformOutput } from '../schema/transformOutput' +import type { BlockStateRegistry } from '../Schemas' type SourcePanelProps = { lang: string, name: string, model: DataModel | null, + blockStates: BlockStateRegistry | null, doCopy?: number, doDownload?: number, doImport?: number, onError: (message: string) => unknown, } -export function SourcePanel({ lang, name, model, doCopy, doDownload, doImport, onError }: SourcePanelProps) { +export function SourcePanel({ lang, name, model, blockStates, doCopy, doDownload, doImport, onError }: SourcePanelProps) { const loc = locale.bind(null, lang) const source = useRef(null) const download = useRef(null) useModel(model, model => { try { - const data = model.schema.hook(transformOutput, new ModelPath(model), model.data) + const props = { blockStates: blockStates ?? {} } + const data = model.schema.hook(transformOutput, new ModelPath(model), model.data, props) source.current.value = JSON.stringify(data, null, 2) + '\n' } catch (e) { onError(`Error getting JSON output: ${e.message}`) diff --git a/src/app/components/Tree.tsx b/src/app/components/Tree.tsx index 27137f74..e2ed0e4a 100644 --- a/src/app/components/Tree.tsx +++ b/src/app/components/Tree.tsx @@ -5,24 +5,25 @@ import { useModel } from '../hooks' import { locale } from '../Locales' import { Mounter } from '../schema/Mounter' import { renderHtml } from '../schema/renderHtml' -import type { VersionId } from '../Schemas' +import type { BlockStateRegistry, VersionId } from '../Schemas' type TreePanelProps = { lang: string, - model: DataModel | null, version: VersionId, + model: DataModel | null, + blockStates: BlockStateRegistry | null, onError: (message: string) => unknown, } -export function Tree({ lang, model, version, onError }: TreePanelProps) { +export function Tree({ lang, model, version, blockStates, onError }: TreePanelProps) { const tree = useRef(null) const redraw = useRef() useEffect(() => { redraw.current = () => { - if (!model) return + if (!model || !blockStates) return try { const mounter = new Mounter() - const props = { loc: locale.bind(null, lang), version, mounter } + const props = { loc: locale.bind(null, lang), version, mounter, blockStates } const path = new ModelPath(model) const rendered = model.schema.hook(renderHtml, path, model.data, props) const category = model.schema.category(path) @@ -30,7 +31,7 @@ export function Tree({ lang, model, version, onError }: TreePanelProps) { let html = rendered[2] if (rendered[1]) { html = `
-
${rendered[1]}
+
${rendered[0]}${rendered[1]}
${rendered[2]}
` } @@ -50,7 +51,7 @@ export function Tree({ lang, model, version, onError }: TreePanelProps) { useEffect(() => { redraw.current() - }, [lang]) + }, [lang, model, blockStates]) return
} diff --git a/src/app/pages/Generator.tsx b/src/app/pages/Generator.tsx index b667dc98..2119c8c8 100644 --- a/src/app/pages/Generator.tsx +++ b/src/app/pages/Generator.tsx @@ -5,8 +5,8 @@ import { Analytics } from '../Analytics' import { Ad, Btn, BtnInput, BtnMenu, ErrorPanel, HasPreview, Octicon, PreviewPanel, SourcePanel, Tree } from '../components' import { fetchPreset } from '../DataFetcher' import { locale } from '../Locales' -import type { VersionId } from '../Schemas' -import { checkVersion, getCollections, getModel } from '../Schemas' +import type { BlockStateRegistry, VersionId } from '../Schemas' +import { checkVersion, getBlockStates, getCollections, getModel } from '../Schemas' type GeneratorProps = { lang: string, @@ -40,8 +40,11 @@ export function Generator({ lang, changeTitle, version, onChangeVersion, categor changeTitle(loc('title.generator', loc(id)), allowedVersions) const [model, setModel] = useState(null) + const [blockStates, setBlockStates] = useState(null) useEffect(() => { setModel(null) + getBlockStates(version) + .then(b => setBlockStates(b)) getModel(version, id) .then(m => setModel(m)) .catch(e => setError(e.message)) @@ -165,7 +168,7 @@ export function Generator({ lang, changeTitle, version, onChangeVersion, categor {error && } - +