@wordpress/block-editor
Version:
207 lines (205 loc) • 6.87 kB
JavaScript
;
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 __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/block-editor/src/hooks/font-size.js
var font_size_exports = {};
__export(font_size_exports, {
FONT_SIZE_SUPPORT_KEY: () => FONT_SIZE_SUPPORT_KEY,
FontSizeEdit: () => FontSizeEdit,
default: () => font_size_default,
useIsFontSizeDisabled: () => useIsFontSizeDisabled
});
module.exports = __toCommonJS(font_size_exports);
var import_hooks = require("@wordpress/hooks");
var import_blocks = require("@wordpress/blocks");
var import_token_list = __toESM(require("@wordpress/token-list"));
var import_global_styles_engine = require("@wordpress/global-styles-engine");
var import_font_sizes = require("../components/font-sizes/index.cjs");
var import_typography = require("./typography.cjs");
var import_utils = require("./utils.cjs");
var import_use_settings = require("../components/use-settings/index.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
var FONT_SIZE_SUPPORT_KEY = "typography.fontSize";
function addAttributes(settings) {
if (!(0, import_blocks.hasBlockSupport)(settings, FONT_SIZE_SUPPORT_KEY)) {
return settings;
}
if (!settings.attributes.fontSize) {
Object.assign(settings.attributes, {
fontSize: {
type: "string"
}
});
}
return settings;
}
function addSaveProps(props, blockNameOrType, attributes) {
if (!(0, import_blocks.hasBlockSupport)(blockNameOrType, FONT_SIZE_SUPPORT_KEY)) {
return props;
}
if ((0, import_utils.shouldSkipSerialization)(
blockNameOrType,
import_typography.TYPOGRAPHY_SUPPORT_KEY,
"fontSize"
)) {
return props;
}
const classes = new import_token_list.default(props.className);
classes.add((0, import_font_sizes.getFontSizeClass)(attributes.fontSize));
const newClassName = classes.value;
props.className = newClassName ? newClassName : void 0;
return props;
}
function FontSizeEdit(props) {
const {
attributes: { fontSize, style },
setAttributes
} = props;
const [fontSizes] = (0, import_use_settings.useSettings)("typography.fontSizes");
const onChange = (value, selectedItem) => {
const fontSizeSlug = selectedItem?.slug || (0, import_font_sizes.getFontSizeObjectByValue)(fontSizes, value).slug;
setAttributes({
style: (0, import_utils.cleanEmptyObject)({
...style,
typography: {
...style?.typography,
fontSize: fontSizeSlug ? void 0 : value
}
}),
fontSize: fontSizeSlug
});
};
const fontSizeObject = (0, import_font_sizes.getFontSize)(
fontSizes,
fontSize,
style?.typography?.fontSize
);
const fontSizeValue = fontSizeObject?.size || style?.typography?.fontSize || fontSize;
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_font_sizes.FontSizePicker,
{
onChange,
value: fontSize || fontSizeValue,
valueMode: fontSize ? "slug" : "literal",
withReset: false,
withSlider: true,
size: "__unstable-large"
}
);
}
function useIsFontSizeDisabled({ name: blockName } = {}) {
const [fontSizes] = (0, import_use_settings.useSettings)("typography.fontSizes");
const hasFontSizes = !!fontSizes?.length;
return !(0, import_blocks.hasBlockSupport)(blockName, FONT_SIZE_SUPPORT_KEY) || !hasFontSizes;
}
function useBlockProps({ name, fontSize, style }) {
const [fontSizes, fluidTypographySettings, layoutSettings] = (0, import_use_settings.useSettings)(
"typography.fontSizes",
"typography.fluid",
"layout"
);
if (!(0, import_blocks.hasBlockSupport)(name, FONT_SIZE_SUPPORT_KEY) || (0, import_utils.shouldSkipSerialization)(name, import_typography.TYPOGRAPHY_SUPPORT_KEY, "fontSize") || !fontSize && !style?.typography?.fontSize) {
return;
}
let props;
if (style?.typography?.fontSize) {
props = {
style: {
fontSize: (0, import_global_styles_engine.getTypographyFontSizeValue)(
{ size: style.typography.fontSize },
{
typography: {
fluid: fluidTypographySettings
},
layout: layoutSettings
}
)
}
};
}
if (fontSize) {
props = {
style: {
fontSize: (0, import_font_sizes.getFontSize)(
fontSizes,
fontSize,
style?.typography?.fontSize
).size
}
};
}
if (!props) {
return;
}
return addSaveProps(props, name, { fontSize });
}
var font_size_default = {
useBlockProps,
addSaveProps,
attributeKeys: ["fontSize", "style"],
hasSupport(name) {
return (0, import_blocks.hasBlockSupport)(name, FONT_SIZE_SUPPORT_KEY);
}
};
var MIGRATION_PATHS = {
fontSize: [["fontSize"], ["style", "typography", "fontSize"]]
};
function addTransforms(result, source, index, results) {
const destinationBlockType = result.name;
const activeSupports = {
fontSize: (0, import_blocks.hasBlockSupport)(
destinationBlockType,
FONT_SIZE_SUPPORT_KEY
)
};
return (0, import_utils.transformStyles)(
activeSupports,
MIGRATION_PATHS,
result,
source,
index,
results
);
}
(0, import_hooks.addFilter)(
"blocks.registerBlockType",
"core/font/addAttribute",
addAttributes
);
(0, import_hooks.addFilter)(
"blocks.switchToBlockType.transformedBlock",
"core/font-size/addTransforms",
addTransforms
);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
FONT_SIZE_SUPPORT_KEY,
FontSizeEdit,
useIsFontSizeDisabled
});
//# sourceMappingURL=font-size.cjs.map