@wordpress/global-styles-ui
Version:
Global Styles UI components for WordPress.
343 lines (342 loc) • 17 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let _wordpress_ui = require("@wordpress/ui");
let _wordpress_components = require("@wordpress/components");
let _wordpress_core_data = require("@wordpress/core-data");
let _wordpress_data = require("@wordpress/data");
let _wordpress_element = require("@wordpress/element");
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_icons = require("@wordpress/icons");
let __context_tsx = require("./context.cjs");
let __font_card_tsx = require("./font-card.cjs");
__font_card_tsx = __toESM(__font_card_tsx);
let __library_font_variant_tsx = require("./library-font-variant.cjs");
__library_font_variant_tsx = __toESM(__library_font_variant_tsx);
let __utils_sort_font_faces_ts = require("./utils/sort-font-faces.cjs");
let __utils_index_ts = require("./utils/index.cjs");
let ___hooks_ts = require("../hooks.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/global-styles-ui/src/font-library/installed-fonts.tsx
/**
* Comparison key for font families. Sorts families by slug and faces
* by style+weight so order differences don't produce false positives.
*
* @param fontFamilies Font families record keyed by source (e.g. "theme", "custom").
*/
function getFontFamiliesKey(fontFamilies) {
if (!fontFamilies) return "";
const normalized = {};
for (const source of Object.keys(fontFamilies).sort()) normalized[source] = (fontFamilies[source] ?? []).map((family) => ({
slug: family.slug,
fontFace: (family.fontFace ?? []).map((face) => `${face.fontStyle}-${face.fontWeight}`).sort()
})).sort((a, b) => a.slug.localeCompare(b.slug));
return JSON.stringify(normalized);
}
function InstalledFonts() {
const { baseCustomFonts, libraryFontSelected, handleSetLibraryFontSelected, uninstallFontFamily, isResolvingLibrary, isInstalling, saveFontFamilies, getFontFacesActivated } = (0, _wordpress_element.useContext)(__context_tsx.FontLibraryContext);
const [fontFamilies, setFontFamilies] = (0, ___hooks_ts.useSetting)("typography.fontFamilies");
const [lastSelectedFontSlug, setLastSelectedFontSlug] = (0, _wordpress_element.useState)(void 0);
const [isConfirmDeleteOpen, setIsConfirmDeleteOpen] = (0, _wordpress_element.useState)(false);
const [notice, setNotice] = (0, _wordpress_element.useState)(null);
const [baseFontFamilies] = (0, ___hooks_ts.useSetting)("typography.fontFamilies", void 0, "base");
const globalStylesId = (0, _wordpress_data.useSelect)((select) => {
const { __experimentalGetCurrentGlobalStylesId } = select(_wordpress_core_data.store);
return __experimentalGetCurrentGlobalStylesId();
}, []);
const globalStyles = (0, _wordpress_core_data.useEntityRecord)("root", "globalStyles", globalStylesId ?? 0, { enabled: globalStylesId !== void 0 });
const editedFontFamilies = globalStyles?.edits?.settings?.typography?.fontFamilies;
const savedFontFamilies = globalStyles?.record?.settings?.typography?.fontFamilies;
const fontFamiliesHasChanges = (0, _wordpress_element.useMemo)(() => {
if (editedFontFamilies === void 0) return false;
return getFontFamiliesKey(editedFontFamilies) !== getFontFamiliesKey(savedFontFamilies);
}, [editedFontFamilies, savedFontFamilies]);
const themeFonts = fontFamilies?.theme ? fontFamilies.theme.map((f) => (0, __utils_index_ts.setUIValuesNeeded)(f, { source: "theme" })).sort((a, b) => a.name.localeCompare(b.name)) : [];
const themeFontsSlugs = new Set(themeFonts.map((f) => f.slug));
const baseThemeFonts = baseFontFamilies?.theme ? themeFonts.concat(baseFontFamilies.theme.filter((f) => !themeFontsSlugs.has(f.slug)).map((f) => (0, __utils_index_ts.setUIValuesNeeded)(f, { source: "theme" })).sort((a, b) => a.name.localeCompare(b.name))) : [];
const customFontFamilyId = libraryFontSelected?.source === "custom" && libraryFontSelected?.id;
const canUserDelete = (0, _wordpress_data.useSelect)((select) => {
const { canUser } = select(_wordpress_core_data.store);
return customFontFamilyId && canUser("delete", {
kind: "postType",
name: "wp_font_family",
id: customFontFamilyId
});
}, [customFontFamilyId]);
const shouldDisplayDeleteButton = !!libraryFontSelected && libraryFontSelected?.source !== "theme" && canUserDelete;
const handleUninstallClick = () => {
setIsConfirmDeleteOpen(true);
};
const handleUpdate = async () => {
setNotice(null);
try {
await saveFontFamilies(fontFamilies);
setNotice({
type: "success",
message: (0, _wordpress_i18n.__)("Font family updated successfully.")
});
} catch (error) {
setNotice({
type: "error",
message: (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("There was an error updating the font family. %s"), error.message)
});
}
};
const getFontFacesToDisplay = (font) => {
if (!font) return [];
if (!font.fontFace || !font.fontFace.length) return [{
fontFamily: font.fontFamily,
fontStyle: "normal",
fontWeight: "400"
}];
return (0, __utils_sort_font_faces_ts.sortFontFaces)(font.fontFace);
};
const getFontCardVariantsText = (font) => {
const variantsInstalled = font?.fontFace && (font?.fontFace?.length ?? 0) > 0 ? font.fontFace.length : 1;
const variantsActive = getFontFacesActivated(font.slug, font.source).length;
return (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("%1$d of %2$d active"), variantsActive, variantsInstalled);
};
(0, _wordpress_element.useEffect)(() => {
handleSetLibraryFontSelected(libraryFontSelected);
}, []);
const activeFontsCount = libraryFontSelected ? getFontFacesActivated(libraryFontSelected.slug, libraryFontSelected.source).length : 0;
const selectedFontsCount = libraryFontSelected?.fontFace?.length ?? (libraryFontSelected?.fontFamily ? 1 : 0);
const isIndeterminate = activeFontsCount > 0 && activeFontsCount !== selectedFontsCount;
const isSelectAllChecked = activeFontsCount === selectedFontsCount;
const toggleSelectAll = () => {
if (!libraryFontSelected || !libraryFontSelected?.source) return;
const initialFonts = fontFamilies?.[libraryFontSelected.source]?.filter((f) => f.slug !== libraryFontSelected.slug) ?? [];
const newFonts = isSelectAllChecked ? initialFonts : [...initialFonts, libraryFontSelected];
setFontFamilies({
...fontFamilies,
[libraryFontSelected.source]: newFonts
});
if (libraryFontSelected.fontFace) libraryFontSelected.fontFace.forEach((face) => {
if (isSelectAllChecked) (0, __utils_index_ts.unloadFontFaceInBrowser)(face, "all");
else {
const displaySrc = (0, __utils_index_ts.getDisplaySrcFromFontFace)(face?.src ?? "");
if (displaySrc) (0, __utils_index_ts.loadFontFaceInBrowser)(face, displaySrc, "all");
}
});
};
const hasFonts = baseThemeFonts.length > 0 || baseCustomFonts.length > 0;
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
className: "font-library__tabpanel-layout",
children: [isResolvingLibrary && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: "font-library__loading",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Progress.Root, {
value: null,
"aria-label": (0, _wordpress_i18n.__)("Loading fonts"),
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Progress.Track, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Progress.Indicator, {}) })
})
}), !isResolvingLibrary && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.Navigator, {
initialPath: libraryFontSelected ? "/fontFamily" : "/",
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Navigator.Screen, {
path: "/",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.__experimentalVStack, {
spacing: "8",
children: [
notice && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Notice, {
status: notice.type,
onRemove: () => setNotice(null),
children: notice.message
}),
!hasFonts && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalText, {
as: "p",
children: (0, _wordpress_i18n.__)("No fonts installed.")
}),
baseThemeFonts.length > 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.__experimentalVStack, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("h2", {
className: "font-library__fonts-title",
children: (0, _wordpress_i18n._x)("Theme", "font source")
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ul", {
role: "list",
className: "font-library__fonts-list",
children: baseThemeFonts.map((font) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("li", {
className: "font-library__fonts-list-item",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__font_card_tsx.default, {
font,
navigatorPath: "/fontFamily",
variantsText: getFontCardVariantsText(font),
shouldFocus: font.slug === lastSelectedFontSlug,
onClick: () => {
setNotice(null);
handleSetLibraryFontSelected(font);
}
})
}, font.slug))
})] }),
baseCustomFonts.length > 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.__experimentalVStack, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("h2", {
className: "font-library__fonts-title",
children: (0, _wordpress_i18n._x)("Custom", "font source")
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ul", {
role: "list",
className: "font-library__fonts-list",
children: baseCustomFonts.map((font) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("li", {
className: "font-library__fonts-list-item",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__font_card_tsx.default, {
font,
navigatorPath: "/fontFamily",
variantsText: getFontCardVariantsText(font),
shouldFocus: font.slug === lastSelectedFontSlug,
onClick: () => {
setNotice(null);
handleSetLibraryFontSelected(font);
}
})
}, font.slug))
})] })
]
})
}), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.Navigator.Screen, {
path: "/fontFamily",
children: [
libraryFontSelected && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ConfirmDeleteDialog, {
font: libraryFontSelected,
isOpen: isConfirmDeleteOpen,
setIsOpen: setIsConfirmDeleteOpen,
setNotice,
uninstallFontFamily,
handleSetLibraryFontSelected
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.Flex, {
justify: "flex-start",
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Navigator.BackButton, {
icon: (0, _wordpress_i18n.isRTL)() ? _wordpress_icons.chevronRight : _wordpress_icons.chevronLeft,
size: "small",
onClick: () => {
setLastSelectedFontSlug(libraryFontSelected?.slug);
handleSetLibraryFontSelected(void 0);
setNotice(null);
},
label: (0, _wordpress_i18n.__)("Back")
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalHeading, {
level: 2,
size: 13,
className: "global-styles-ui-header",
children: libraryFontSelected?.name
})]
}),
notice && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalSpacer, { margin: 1 }),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Notice, {
status: notice.type,
onRemove: () => setNotice(null),
children: notice.message
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalSpacer, { margin: 1 })
] }),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalSpacer, { margin: 4 }),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalText, { children: (0, _wordpress_i18n.__)("Choose font variants. Keep in mind that too many variants could make your site slower.") }),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalSpacer, { margin: 4 }),
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.__experimentalVStack, {
spacing: 0,
children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.CheckboxControl, {
className: "font-library__select-all",
label: (0, _wordpress_i18n.__)("Select all"),
checked: isSelectAllChecked,
onChange: toggleSelectAll,
indeterminate: isIndeterminate
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalSpacer, { margin: 8 }),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("ul", {
role: "list",
className: "font-library__fonts-list",
children: libraryFontSelected && getFontFacesToDisplay(libraryFontSelected).map((face, i) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("li", {
className: "font-library__fonts-list-item",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__library_font_variant_tsx.default, {
font: libraryFontSelected,
face
}, `face${i}`)
}, `face${i}`))
})
]
})
]
})]
}), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.__experimentalHStack, {
justify: "flex-end",
className: "font-library__footer",
children: [
isInstalling && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Progress.Root, {
value: null,
"aria-label": (0, _wordpress_i18n.__)("Installing fonts"),
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Progress.Track, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Progress.Indicator, {}) })
}),
shouldDisplayDeleteButton && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Button, {
__next40pxDefaultSize: true,
isDestructive: true,
variant: "tertiary",
onClick: handleUninstallClick,
children: (0, _wordpress_i18n.__)("Delete")
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Button, {
__next40pxDefaultSize: true,
variant: "primary",
onClick: handleUpdate,
disabled: !fontFamiliesHasChanges,
accessibleWhenDisabled: true,
children: (0, _wordpress_i18n.__)("Update")
})
]
})] })]
});
}
function ConfirmDeleteDialog({ font, isOpen, setIsOpen, setNotice, uninstallFontFamily, handleSetLibraryFontSelected }) {
const navigator = (0, _wordpress_components.useNavigator)();
const handleConfirmUninstall = async () => {
setNotice(null);
setIsOpen(false);
try {
await uninstallFontFamily(font);
navigator.goBack();
handleSetLibraryFontSelected(void 0);
setNotice({
type: "success",
message: (0, _wordpress_i18n.__)("Font family uninstalled successfully.")
});
} catch (error) {
setNotice({
type: "error",
message: (0, _wordpress_i18n.__)("There was an error uninstalling the font family.") + error.message
});
}
};
const handleCancelUninstall = () => {
setIsOpen(false);
};
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalConfirmDialog, {
isOpen,
cancelButtonText: (0, _wordpress_i18n.__)("Cancel"),
confirmButtonText: (0, _wordpress_i18n.__)("Delete"),
onCancel: handleCancelUninstall,
onConfirm: handleConfirmUninstall,
size: "medium",
children: font && (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Are you sure you want to delete \"%s\" font and all its variants and assets?"), font.name)
});
}
exports.default = InstalledFonts;
//# sourceMappingURL=installed-fonts.cjs.map