UNPKG

@wordpress/block-editor

Version:
169 lines (167 loc) 6.06 kB
"use strict"; 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/text-align.js var text_align_exports = {}; __export(text_align_exports, { TEXT_ALIGN_SUPPORT_KEY: () => TEXT_ALIGN_SUPPORT_KEY, addAssignedTextAlign: () => addAssignedTextAlign, default: () => text_align_default, getValidTextAlignments: () => getValidTextAlignments }); module.exports = __toCommonJS(text_align_exports); var import_clsx = __toESM(require("clsx")); var import_i18n = require("@wordpress/i18n"); var import_blocks = require("@wordpress/blocks"); var import_icons = require("@wordpress/icons"); var import_components = require("../components"); var import_block_editing_mode = require("../components/block-editing-mode"); var import_utils = require("./utils"); var import_typography = require("./typography"); var import_jsx_runtime = require("react/jsx-runtime"); var TEXT_ALIGN_SUPPORT_KEY = "typography.textAlign"; var TEXT_ALIGNMENT_OPTIONS = [ { icon: import_icons.alignLeft, title: (0, import_i18n.__)("Align text left"), align: "left" }, { icon: import_icons.alignCenter, title: (0, import_i18n.__)("Align text center"), align: "center" }, { icon: import_icons.alignRight, title: (0, import_i18n.__)("Align text right"), align: "right" } ]; var VALID_TEXT_ALIGNMENTS = ["left", "center", "right"]; var NO_TEXT_ALIGNMENTS = []; function getValidTextAlignments(blockTextAlign) { if (Array.isArray(blockTextAlign)) { return VALID_TEXT_ALIGNMENTS.filter( (textAlign) => blockTextAlign.includes(textAlign) ); } return blockTextAlign === true ? VALID_TEXT_ALIGNMENTS : NO_TEXT_ALIGNMENTS; } function BlockEditTextAlignmentToolbarControlsPure({ style, name: blockName, setAttributes }) { const settings = (0, import_utils.useBlockSettings)(blockName); const hasTextAlignControl = settings?.typography?.textAlign; const blockEditingMode = (0, import_block_editing_mode.useBlockEditingMode)(); if (!hasTextAlignControl || blockEditingMode !== "default") { return null; } const validTextAlignments = getValidTextAlignments( (0, import_blocks.getBlockSupport)(blockName, TEXT_ALIGN_SUPPORT_KEY) ); if (!validTextAlignments.length) { return null; } const textAlignmentControls = TEXT_ALIGNMENT_OPTIONS.filter( (control) => validTextAlignments.includes(control.align) ); const onChange = (newTextAlignValue) => { const newStyle = { ...style, typography: { ...style?.typography, textAlign: newTextAlignValue } }; setAttributes({ style: (0, import_utils.cleanEmptyObject)(newStyle) }); }; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.BlockControls, { group: "block", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.AlignmentControl, { value: style?.typography?.textAlign, onChange, alignmentControls: textAlignmentControls } ) }); } var text_align_default = { edit: BlockEditTextAlignmentToolbarControlsPure, useBlockProps, addSaveProps: addAssignedTextAlign, attributeKeys: ["style"], hasSupport(name) { return (0, import_blocks.hasBlockSupport)(name, TEXT_ALIGN_SUPPORT_KEY, false); } }; function useBlockProps({ name, style }) { if (!style?.typography?.textAlign) { return null; } const validTextAlignments = getValidTextAlignments( (0, import_blocks.getBlockSupport)(name, TEXT_ALIGN_SUPPORT_KEY) ); if (!validTextAlignments.length) { return null; } if ((0, import_utils.shouldSkipSerialization)(name, import_typography.TYPOGRAPHY_SUPPORT_KEY, "textAlign")) { return null; } const textAlign = style.typography.textAlign; const className = (0, import_clsx.default)({ [`has-text-align-${textAlign}`]: textAlign }); return { className }; } function addAssignedTextAlign(props, blockType, attributes) { if (!attributes?.style?.typography?.textAlign) { return props; } const { textAlign } = attributes.style.typography; const blockTextAlign = (0, import_blocks.getBlockSupport)(blockType, TEXT_ALIGN_SUPPORT_KEY); const isTextAlignValid = getValidTextAlignments(blockTextAlign).includes(textAlign); if (isTextAlignValid && !(0, import_utils.shouldSkipSerialization)( blockType, import_typography.TYPOGRAPHY_SUPPORT_KEY, "textAlign" )) { props.className = (0, import_clsx.default)( `has-text-align-${textAlign}`, props.className ); } return props; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { TEXT_ALIGN_SUPPORT_KEY, addAssignedTextAlign, getValidTextAlignments }); //# sourceMappingURL=text-align.js.map