import { Identifier, ItemStack } from 'deepslate' import type { ComponentChild, ComponentChildren } from 'preact' import { useEffect, useRef, useState } from 'preact/hooks' import { clamp, safeJsonParse } from '../../Utils.js' import { ItemDisplay } from '../ItemDisplay.jsx' import { TextComponent } from '../TextComponent.jsx' import type { PreviewProps } from './index.js' export const DialogPreview = ({ docAndNode }: PreviewProps) => { const overlay = useRef(null) const text = docAndNode.doc.getText() const dialog = safeJsonParse(text) ?? {} const type = dialog.type?.replace(/^minecraft:/, '') const footerHeight = type === 'multi_action_input_form' ? 5 : 33 useEffect(() => { function resizeHandler() { if (!overlay.current) return const width = Math.floor(overlay.current.clientWidth) overlay.current.style.setProperty('--dialog-px', `${width/400}px`) } resizeHandler() window.addEventListener('resize', resizeHandler) return () => window.removeEventListener('resize', resizeHandler) }, [overlay]) return <>
} function DialogTitle({ title }: { title: any }) { // TODO: add warning button tooltip return
} function DialogBody({ body }: { body: any }) { if (!body) { body = [] } else if (!Array.isArray(body)) { body = [body] } return <> {body?.map((b: any) => { const type = b.type?.replace(/^minecraft:/, '') if (type === 'plain_message') { return
} if (type == 'item') { // TODO: add item components const item = new ItemStack(Identifier.parse(b.item?.id ?? 'air'), b.show_decorations ? (b.item?.count ?? 1) : 1) return
{b.description &&
}
} return <> })} } function DialogContent({ dialog }: { dialog: any }) { const type = dialog.type?.replace(/^minecraft:/, '') if (type === 'dialog_list') { let dialogs = [] if (Array.isArray(dialog.dialogs)) { dialogs = dialog.dialogs } else if (typeof dialog.dialogs === 'string') { if (dialog.dialogs.startsWith('#')) { dialogs = ['dialog_1', 'dialog_2', 'dialog_3'] } else { dialogs = [dialog.dialogs] } } return {dialogs.map((d: any) => { let text = Identifier.parse(d).path.replaceAll('/', ' ').replaceAll('_', ' ') text = text.charAt(0).toUpperCase() + text.substring(1) return