import React, { useEffect, useState } from 'react' import type { WObject } from '@/lib/wysiwyg/schema' export type PropsPanelProps = { object: WObject onChange?: (next: WObject) => void } function num(v: string, fallback: number) { const n = Number(v) return Number.isFinite(n) ? n : fallback } function isImage(o: WObject): o is Extract { return o.type === 'image' } function isText(o: WObject): o is Extract { return o.type === 'text' } function isButton(o: WObject): o is Extract { return o.type === 'button' } export function PropsPanel({ object, onChange }: PropsPanelProps) { const [model, setModel] = useState(object) useEffect(() => { setModel(object) }, [object]) const emit = (next: WObject) => { setModel(next) onChange?.(next) } const update = (patch: Partial>): void => { const next: WObject = { ...model, ...patch } emit(next) } const updateImageProps = (patch: Partial<{ src: string; alt?: string }>): void => { if (!isImage(model)) return const next: WObject = { ...model, props: { ...model.props, ...patch } } emit(next) } const updateTextProps = (patch: Partial<{ text: string }>): void => { if (!isText(model)) return const next: WObject = { ...model, props: { ...model.props, ...patch } } emit(next) } const updateButtonProps = (patch: Partial<{ label: string }>): void => { if (!isButton(model)) return const next: WObject = { ...model, props: { ...model.props, ...patch } } emit(next) } return (
{isText(model) && (
)} {isButton(model) && (
)} {isImage(model) && (
)}
) }