UNPKG

@wordpress/block-library

Version:
131 lines (130 loc) • 5.3 kB
import { BlockControls, HeadingLevelDropdown, InspectorControls, PlainText, useBlockEditingMode, useBlockProps } from "@wordpress/block-editor"; import { ExternalLink, TextControl, ToggleControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components"; import { __ } from "@wordpress/i18n"; import { store, useEntityProp } from "@wordpress/core-data"; import { useSelect } from "@wordpress/data"; import { createInterpolateElement } from "@wordpress/element"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/post-title/edit.jsx function PostTitleEdit({ attributes: { level, levelOptions, isLink, rel, linkTarget, placeholder, fitText }, setAttributes, context: { postType, postId, queryId } }) { const TagName = level === 0 ? "p" : `h${level}`; const isDescendentOfQueryLoop = Number.isFinite(queryId); const userCanEdit = useSelect((select) => { /** * useCanEditEntity may trigger an OPTIONS request to the REST API * via the canUser resolver. However, when the Post Title is a * descendant of a Query Loop block, the title cannot be edited. In * order to avoid these unnecessary requests, we call the hook * without the proper data, resulting in returning early without * making them. */ if (isDescendentOfQueryLoop) return false; return select(store).canUser("update", { kind: "postType", name: postType, id: postId }); }, [ isDescendentOfQueryLoop, postType, postId ]); const [rawTitle = "", setTitle, fullTitle] = useEntityProp("postType", postType, "title", postId); const [link] = useEntityProp("postType", postType, "link", postId); const blockProps = useBlockProps(); const blockEditingMode = useBlockEditingMode(); const dropdownMenuProps = useToolsPanelDropdownMenuProps(); let titleElement = /* @__PURE__ */ jsx(TagName, { ...blockProps, children: placeholder || __("Title") }); if (postType && postId) titleElement = userCanEdit ? /* @__PURE__ */ jsx(PlainText, { tagName: TagName, placeholder: __("(no title)"), value: rawTitle, onChange: setTitle, __experimentalVersion: 2, disableLineBreaks: true, ...blockProps }) : /* @__PURE__ */ jsx(TagName, { ...blockProps, dangerouslySetInnerHTML: { __html: fullTitle?.rendered || __("(no title)") } }); if (isLink && postType && postId) titleElement = userCanEdit ? /* @__PURE__ */ jsx(TagName, { ...blockProps, children: /* @__PURE__ */ jsx(PlainText, { tagName: "a", href: link, target: linkTarget, rel, placeholder: !rawTitle.length ? __("(no title)") : null, value: rawTitle, onChange: setTitle, __experimentalVersion: 2, disableLineBreaks: true, style: { whiteSpace: fitText ? "inherit" : "pre-wrap" } }) }) : /* @__PURE__ */ jsx(TagName, { ...blockProps, children: /* @__PURE__ */ jsx("a", { href: link, target: linkTarget, rel, onClick: (event) => event.preventDefault(), dangerouslySetInnerHTML: { __html: fullTitle?.rendered || __("(no title)") } }) }); return /* @__PURE__ */ jsxs(Fragment, { children: [blockEditingMode === "default" && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(BlockControls, { group: "block", children: /* @__PURE__ */ jsx(HeadingLevelDropdown, { value: level, options: levelOptions, onChange: (newLevel) => setAttributes({ level: newLevel }) }) }), /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, { label: __("Settings"), resetAll: () => { setAttributes({ rel: "", linkTarget: "_self", isLink: false }); }, dropdownMenuProps, children: [/* @__PURE__ */ jsx(__experimentalToolsPanelItem, { label: __("Make title a link"), isShownByDefault: true, hasValue: () => isLink, onDeselect: () => setAttributes({ isLink: false }), children: /* @__PURE__ */ jsx(ToggleControl, { label: __("Make title a link"), onChange: () => setAttributes({ isLink: !isLink }), checked: isLink }) }), isLink && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(__experimentalToolsPanelItem, { label: __("Open in new tab"), isShownByDefault: true, hasValue: () => linkTarget === "_blank", onDeselect: () => setAttributes({ linkTarget: "_self" }), children: /* @__PURE__ */ jsx(ToggleControl, { label: __("Open in new tab"), onChange: (value) => setAttributes({ linkTarget: value ? "_blank" : "_self" }), checked: linkTarget === "_blank" }) }), /* @__PURE__ */ jsx(__experimentalToolsPanelItem, { label: __("Link relation"), isShownByDefault: true, hasValue: () => !!rel, onDeselect: () => setAttributes({ rel: "" }), children: /* @__PURE__ */ jsx(TextControl, { label: __("Link relation"), help: createInterpolateElement(__("The <a>Link Relation</a> attribute defines the relationship between a linked resource and the current document."), { a: /* @__PURE__ */ jsx(ExternalLink, { href: "https://developer.mozilla.org/docs/Web/HTML/Attributes/rel" }) }), value: rel, onChange: (newRel) => setAttributes({ rel: newRel }) }) })] })] }) })] }), titleElement] }); } export { PostTitleEdit as default }; //# sourceMappingURL=edit.mjs.map