import Select from "react-select"; import { __ } from "@wordpress/i18n"; import { Button, Popover } from "@wordpress/components"; import { useEffect, useState } from "@wordpress/element"; import SPRangeControl from "../rangeControl/rangeControl.js"; import ComponentsTopSection from "../componentsTopControl/ComponentsTopSection.js"; import { EditIcon } from "./svgIcon.js"; import "./editor.scss"; import SelectField from "../selectField/selectField.js"; import { fontWeightMap, getFontWeightList, textStylesOptions } from "./utility.js"; import InputControl from "../inputControl/inputControl.js"; import { useDeviceType } from "../../controls/controls.js"; const fetchFonts = async () => { try { const response = await fetch( "https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyCJIzfKoHlACqsmK1zDzl-OAsgtJPk8BtI" ); if (response.status === 200) { return response.json(); } } catch (error) { // eslint-disable-next-line no-console console.error("Error fetching Google Fonts:", error); } }; const TypographyNew = ({ setAttributes, attributes, fontSizeDefault = { unit: "px", value: 16 }, spacingDefaultValue = { unit: "px", value: 0 }, lineDefaultValue = { unit: "px", value: 0 }, typographyLabel = "Typography", }) => { const [allFonts, setAllFonts] = useState([]); const [fontLists, setFontLists] = useState([]); const [lineHeightUpdateKey, setLineHeightUpdateKey] = useState(0); const { family, familyKey, fontSize, fontSizeKey, lineHeight, lineHeightKey, fontSpacing, fontSpacingKey, wordSpacing, wordSpacingKey, } = attributes; const deviceType = useDeviceType(); const [isVisible, setIsVisible] = useState(false); const toggleVisible = () => { setIsVisible((state) => !state); }; useEffect(() => { if (allFonts.length === 0) { fetchFonts().then((data) => { const fonts = data.items.map((item) => { return { label: item.family, value: item.family, font: { family: item.family, variants: item.variants }, }; }); setAllFonts(fonts); setFontLists(fonts.filter((font, i) => i < 50 && font)); }); } }, []); const fontSearch = (inputValue) => { const googleFonts = allFonts .filter((font) => { return font.label.toLowerCase().includes(inputValue.toLowerCase()); }) .filter((font, i) => i < 30 && font); setFontLists(googleFonts); }; const fontWCheck = Array( "100italic", "300italic", "italic", "400italic", "500italic", "600italic", "700italic", "800italic", "900italic" ); const fontW = family.googleFont?.variants?.map((value) => { if (fontWCheck.includes(value) || "100" === value) { return ""; } if ("regular" === value) { value = "400"; } return { label: value, value: value }; }); let fontWeightList = []; if (fontW) { fontWeightList = fontW.filter(function (item, pos) { return "" !== item; }); } useEffect(() => { const clickOutSite = (e) => { const target = e.target.closest(".sp-smart-post-typography-fonts"); const buttonTarget = e.target.closest(".sp-smart-post-typography-btn button"); const familyTarget = e.target.closest(".css-1nmdiq5-menu"); if (!target && isVisible && !buttonTarget && !familyTarget) { setIsVisible(false); } }; window.addEventListener("click", clickOutSite); return () => window.removeEventListener("click", clickOutSite); }); // default and active family options. const defaultFamilyOption = { label: "Default", value: "Default", font: { family: "Default", variants: ["300", "400", "500", "600", "700", "800"], }, }; const activeFontFamily = family.typography.family === "" ? defaultFamilyOption : { label: family.googleFont?.family, value: family.googleFont?.family, font: family.googleFont, }; const allFamilyList = [defaultFamilyOption, ...fontLists]; const isAvailableOnList = allFamilyList?.find((font) => font.value === activeFontFamily.value); const fontFamilySelectOptions = isAvailableOnList ? allFamilyList : [defaultFamilyOption, activeFontFamily, ...fontLists]; // font family. const { fontWeight, style } = family.typography; const onChangeFontStyle = (fontStyle) => { const arrayOfStyles = fontWeightMap[fontStyle]?.split(" "); const style = fontWeightMap[fontStyle].includes("italic") ? "italic" : ""; const fontWeight = arrayOfStyles[arrayOfStyles?.length - 1]; setAttributes({ [familyKey]: { ...family, typography: { ...family.typography, fontWeight: fontWeight, style: style, }, }, }); }; const onChangeLineHeight = (value) => { setAttributes({ [lineHeightKey]: { ...lineHeight, device: { ...lineHeight?.device, [deviceType]: value, }, unit: { ...lineHeight.unit, [deviceType]: "px", }, }, }); }; const onChangeFontSize = (newValue) => { setAttributes({ [fontSizeKey]: { ...fontSize, device: { ...fontSize?.device, [deviceType]: newValue.value, }, }, }); if (fontSize?.unit?.[deviceType] === "px") { const dynamicLineHeight = parseInt(newValue.value * 1.2); onChangeLineHeight(dynamicLineHeight); setLineHeightUpdateKey(dynamicLineHeight); } }; const onChangeTextStyles = (key, value) => { const newValue = family?.typography[key] === value ? "" : value; setAttributes({ [familyKey]: { ...family, typography: { ...family.typography, [key]: newValue, }, }, }); }; return ( <>

{typographyLabel}

{isVisible && (

Typography

Font Family