@wordpress/block-library
Version:
Block library for the WordPress editor.
90 lines (89 loc) • 3.82 kB
JavaScript
import { useSelect } from "@wordpress/data";
import { store, useEntityProp } from "@wordpress/core-data";
import { __ } from "@wordpress/i18n";
import { BlockControls, HeadingLevelDropdown, InspectorControls, RichText, useBlockEditingMode, useBlockProps } from "@wordpress/block-editor";
import { ToggleControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components";
import { decodeEntities } from "@wordpress/html-entities";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs";
import useDeprecatedTextAlign from "../utils/deprecated-text-align-attributes.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/site-title/edit.jsx
function SiteTitleEdit(props) {
useDeprecatedTextAlign(props);
const { attributes, setAttributes } = props;
const { level, levelOptions, isLink, linkTarget, fitText } = attributes;
const canUserEdit = useSelect((select) => select(store).canUser("update", {
kind: "root",
name: "site"
}), []);
const [title, setSiteTitle] = useEntityProp("root", canUserEdit ? "site" : "__unstableBase", canUserEdit ? "title" : "name", void 0);
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const blockEditingMode = useBlockEditingMode();
const TagName = level === 0 ? "p" : `h${level}`;
const blockProps = useBlockProps({ className: !canUserEdit && !title && "wp-block-site-title__placeholder" });
const siteTitleContent = canUserEdit ? /* @__PURE__ */ jsx(TagName, {
...blockProps,
children: /* @__PURE__ */ jsx(RichText, {
tagName: isLink ? "a" : "span",
href: isLink ? "#site-title-pseudo-link" : void 0,
"aria-label": __("Site title text"),
placeholder: __("Write site title…"),
value: title,
onChange: (newTitle) => setSiteTitle(newTitle.trim()),
allowedFormats: [],
disableLineBreaks: true,
style: { whiteSpace: fitText ? "inherit" : "pre-wrap" }
})
}) : /* @__PURE__ */ jsx(TagName, {
...blockProps,
children: isLink ? /* @__PURE__ */ jsx("a", {
href: "#site-title-pseudo-link",
onClick: (event) => event.preventDefault(),
children: decodeEntities(title) || __("Site Title placeholder")
}) : /* @__PURE__ */ jsx("span", { children: decodeEntities(title) || __("Site Title placeholder") })
});
return /* @__PURE__ */ jsxs(Fragment, { children: [
blockEditingMode === "default" && /* @__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({
isLink: true,
linkTarget: "_self"
});
},
dropdownMenuProps,
children: [/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !isLink,
label: __("Make title link to home"),
onDeselect: () => setAttributes({ isLink: true }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Make title link to home"),
onChange: () => setAttributes({ isLink: !isLink }),
checked: isLink
})
}), isLink && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => linkTarget !== "_self",
label: __("Open in new tab"),
onDeselect: () => setAttributes({ linkTarget: "_self" }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Open in new tab"),
onChange: (value) => setAttributes({ linkTarget: value ? "_blank" : "_self" }),
checked: linkTarget === "_blank"
})
})]
}) }),
siteTitleContent
] });
}
export { SiteTitleEdit as default };
//# sourceMappingURL=edit.mjs.map