import { useState, useEffect } from '@wordpress/element'; import { Flex, FlexItem, Modal, TextControl, Button, ComboboxControl, FormTokenField, } from '@wordpress/components'; import Accordion from './Accordion'; import GoogleVariableFontSettings from './GoogleVariableFontSettings'; import { __, sprintf } from '@wordpress/i18n'; type FontOption = { label: string; value: string; }; type ExtendedComboboxControlProps = React.ComponentProps & { placeholder?: string; }; const ExtendedComboboxControl = (props: ExtendedComboboxControlProps) => ( ); type Props = { values:GoogleFontItem[]; defaults: { [key: string]: string }; fonts: FontOption[]; fontVariants: GoogleFontConfig[]; title: string; description?: string; onChange: (newValues:GoogleFontItem[]) => void } export default function GoogleFontSettings({ values, defaults, fonts, fontVariants,title, description, onChange }: Props) { // console.log(values); const [items, setItems] = useState(values || [{type: 'static', name: '', category: '', variants:[], italic: '', optical_size: ''}]); const [searchFonts, setSearchFonts] = useState(fonts); const [isDeleteMsgOpen, setDeleteMsgOpen] = useState(-1); // console.log(fonts); // Sync `values` changes to local state useEffect(() => { // 選択可能なフォント. const availableFonts = fonts.filter(({ label }) => items.every((item) => item.name !== label) ); setSearchFonts(availableFonts); // フォント名が無い場合は無視. onChange(items.filter(item => item?.name)); }, [items]); // console.log(searchFonts); // フォントのVariantsをデータから取得. const getAllVariantsForFont = (fontName: string|null|undefined): string[] => { if (!fontName) return []; return fontVariants.find((data) => data.name === fontName)?.variants || []; } const getFontType = (fontName: string|null|undefined): string => { if (!fontName) return ''; return fontVariants.find((data) => data.name === fontName)?.type || ''; } // アイテム追加. const addItem = () => { // フォント名を指定していない場合は追加できないようにする. if (items.some((item) => item.name === '')) { alert(__('Please select a font', 'apura')); return; } setItems([ ...items, { type: 'static', name: '', category: '', variants: [], italic: '', optical_size: '', width: '', } ] ); }; // アイテム削除. const removeItem = (index: number) => { setItems(items.filter((_, i) => i !== index)); setDeleteMsgOpen(-1); }; const updateFont = (fontName: string | null | undefined, index: number) => { setItems((prev) => prev.map((item, i) => { return i === index ? { type: getFontType(fontName), name: fontName || '', category: fontVariants.find((data) => data.name === fontName)?.category || '', variants: getAllVariantsForFont(fontName), italic: fontVariants.find((data) => data.name === fontName)?.italic || '', optical_size: fontVariants.find((data) => data.name === fontName)?.optical_size || '', width: fontVariants.find((data) => data.name === fontName)?.width?.default || '', } : item }) ); }; // Variants選択. const updateStaticFontVariants = (tokens: string[], fontName: string, index: number) => { setItems((prev) => prev.map((item, i) => { if (i === index) { const variants = tokens.filter((t) => getAllVariantsForFont(fontName).includes(t)); return { ...item, variants }; } return item; }) ); }; const updateVariableFontVariants = (data: GoogleFontItem, index: number) => { setItems((prev) => prev.map((item, i) => { if (i === index) { return { ...item, ...data }; } return item; }) ); }; const openDeleteModal = (index: number) => setDeleteMsgOpen(index); // 削除時のダイアログメッセージ閉じる const closeDeleteModal = () => setDeleteMsgOpen(-1); return (
{title}
{description &&

{description}

} {items?.length > 0 && items.map((item, index) => ( updateFont(value, index)} options={searchFonts} allowReset={false} placeholder={__('Search for...', 'apura' )} /> {}} /> {item.type === 'static' ? ( updateStaticFontVariants(tokens as string[], item.name, index)} suggestions={getAllVariantsForFont(item?.name)} value={item?.variants} /> ) : ( { // console.log(newValues); updateVariableFontVariants(newValues, index); }} /> )} {isDeleteMsgOpen === index && (

{__('If this font is used anywhere on the site, it will be unset.', 'apura')}

)}
))}
); }