UNPKG

@wordpress/block-library

Version:
275 lines (274 loc) 8.54 kB
import { Fragment, jsx, jsxs } from "react/jsx-runtime"; import clsx from "clsx"; import { __experimentalToggleGroupControl as ToggleGroupControl, __experimentalToggleGroupControlOption as ToggleGroupControlOption, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, ToggleControl, SelectControl } from "@wordpress/components"; import { InspectorControls, RichText, BlockControls, AlignmentToolbar, useBlockProps, useBlockEditingMode } from "@wordpress/block-editor"; import { __, _x } from "@wordpress/i18n"; import { useSelect } from "@wordpress/data"; import { store as coreStore } from "@wordpress/core-data"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks"; function PostNavigationLinkEdit({ context: { postType }, attributes: { type, label, showTitle, textAlign, linkLabel, arrow, taxonomy }, setAttributes }) { const blockEditingMode = useBlockEditingMode(); const showControls = blockEditingMode === "default"; const isNext = type === "next"; let placeholder = isNext ? __("Next") : __("Previous"); const arrowMap = { none: "", arrow: isNext ? "\u2192" : "\u2190", chevron: isNext ? "\xBB" : "\xAB" }; const displayArrow = arrowMap[arrow]; if (showTitle) { placeholder = isNext ? ( /* translators: Label before for next and previous post. There is a space after the colon. */ __("Next: ") ) : ( /* translators: Label before for next and previous post. There is a space after the colon. */ __("Previous: ") ); } const ariaLabel = isNext ? __("Next post") : __("Previous post"); const blockProps = useBlockProps({ className: clsx({ [`has-text-align-${textAlign}`]: textAlign }) }); const taxonomies = useSelect( (select) => { const { getTaxonomies } = select(coreStore); const filteredTaxonomies = getTaxonomies({ type: postType, per_page: -1 }); return filteredTaxonomies; }, [postType] ); const getTaxonomyOptions = () => { const selectOption = { label: __("Unfiltered"), value: "" }; const taxonomyOptions = (taxonomies ?? []).filter(({ visibility }) => !!visibility?.publicly_queryable).map((item) => { return { value: item.slug, label: item.name }; }); return [selectOption, ...taxonomyOptions]; }; const dropdownMenuProps = useToolsPanelDropdownMenuProps(); return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs( ToolsPanel, { label: __("Settings"), resetAll: () => { setAttributes({ showTitle: false, linkLabel: false, arrow: "none" }); }, dropdownMenuProps, children: [ /* @__PURE__ */ jsx( ToolsPanelItem, { label: __("Display the title as a link"), isShownByDefault: true, hasValue: () => showTitle, onDeselect: () => setAttributes({ showTitle: false }), children: /* @__PURE__ */ jsx( ToggleControl, { __nextHasNoMarginBottom: true, label: __("Display the title as a link"), help: __( "If you have entered a custom label, it will be prepended before the title." ), checked: !!showTitle, onChange: () => setAttributes({ showTitle: !showTitle }) } ) } ), showTitle && /* @__PURE__ */ jsx( ToolsPanelItem, { label: __( "Include the label as part of the link" ), isShownByDefault: true, hasValue: () => !!linkLabel, onDeselect: () => setAttributes({ linkLabel: false }), children: /* @__PURE__ */ jsx( ToggleControl, { __nextHasNoMarginBottom: true, label: __( "Include the label as part of the link" ), checked: !!linkLabel, onChange: () => setAttributes({ linkLabel: !linkLabel }) } ) } ), /* @__PURE__ */ jsx( ToolsPanelItem, { label: __("Arrow"), isShownByDefault: true, hasValue: () => arrow !== "none", onDeselect: () => setAttributes({ arrow: "none" }), children: /* @__PURE__ */ jsxs( ToggleGroupControl, { __next40pxDefaultSize: true, __nextHasNoMarginBottom: true, label: __("Arrow"), value: arrow, onChange: (value) => { setAttributes({ arrow: value }); }, help: __( "A decorative arrow for the next and previous link." ), isBlock: true, children: [ /* @__PURE__ */ jsx( ToggleGroupControlOption, { value: "none", label: _x( "None", "Arrow option for Next/Previous link" ) } ), /* @__PURE__ */ jsx( ToggleGroupControlOption, { value: "arrow", label: _x( "Arrow", "Arrow option for Next/Previous link" ) } ), /* @__PURE__ */ jsx( ToggleGroupControlOption, { value: "chevron", label: _x( "Chevron", "Arrow option for Next/Previous link" ) } ) ] } ) } ) ] } ) }), /* @__PURE__ */ jsx(InspectorControls, { group: "advanced", children: /* @__PURE__ */ jsx( SelectControl, { __next40pxDefaultSize: true, __nextHasNoMarginBottom: true, label: __("Filter by taxonomy"), value: taxonomy, options: getTaxonomyOptions(), onChange: (value) => setAttributes({ taxonomy: value }), help: __( "Only link to posts that have the same taxonomy terms as the current post. For example the same tags or categories." ) } ) }), showControls && /* @__PURE__ */ jsx(BlockControls, { children: /* @__PURE__ */ jsx( AlignmentToolbar, { value: textAlign, onChange: (nextAlign) => { setAttributes({ textAlign: nextAlign }); } } ) }), /* @__PURE__ */ jsxs("div", { ...blockProps, children: [ !isNext && displayArrow && /* @__PURE__ */ jsx( "span", { className: `wp-block-post-navigation-link__arrow-previous is-arrow-${arrow}`, children: displayArrow } ), /* @__PURE__ */ jsx( RichText, { tagName: "a", identifier: "label", "aria-label": ariaLabel, placeholder, value: label, withoutInteractiveFormatting: true, onChange: (newLabel) => setAttributes({ label: newLabel }) } ), showTitle && /* @__PURE__ */ jsx( "a", { href: "#post-navigation-pseudo-link", onClick: (event) => event.preventDefault(), children: __("An example title") } ), isNext && displayArrow && /* @__PURE__ */ jsx( "span", { className: `wp-block-post-navigation-link__arrow-next is-arrow-${arrow}`, "aria-hidden": true, children: displayArrow } ) ] }) ] }); } export { PostNavigationLinkEdit as default }; //# sourceMappingURL=edit.js.map