@wordpress/block-library
Version:
Block library for the WordPress editor.
221 lines (220 loc) • 7.92 kB
JavaScript
import { BlockControls, InspectorControls, useBlockProps } from "@wordpress/block-editor";
import { Button, ExternalLink, Placeholder, RangeControl, Spinner, TextControl, ToggleControl, ToolbarGroup, __experimentalInputControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components";
import { createInterpolateElement, useState } from "@wordpress/element";
import { grid, list, pencil, rss } from "@wordpress/icons";
import { __, _x, sprintf } from "@wordpress/i18n";
import { prependHTTPS } from "@wordpress/url";
import { useServerSideRender } from "@wordpress/server-side-render";
import { useDisabled } from "@wordpress/compose";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs";
import HtmlRenderer from "../utils/html-renderer.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/rss/edit.jsx
const DEFAULT_MIN_ITEMS = 1;
const DEFAULT_MAX_ITEMS = 20;
function RSSEdit({ attributes, setAttributes, name }) {
const [isEditing, setIsEditing] = useState(!attributes.feedURL);
const { blockLayout, columns, displayAuthor, displayDate, displayExcerpt, excerptLength, feedURL, itemsToShow, openInNewTab, rel } = attributes;
const [url, setURL] = useState(feedURL);
const [prevFeedURL, setPrevFeedURL] = useState(feedURL);
if (feedURL !== prevFeedURL) {
setPrevFeedURL(feedURL);
setURL(feedURL);
}
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
function toggleAttribute(propName) {
return () => {
const value = attributes[propName];
setAttributes({ [propName]: !value });
};
}
function onSubmitURL(event) {
event.preventDefault();
if (url) {
setAttributes({ feedURL: prependHTTPS(url) });
setIsEditing(false);
}
}
const { content, status, error } = useServerSideRender({
attributes,
skipBlockSupportAttributes: true,
block: name
});
const disabledRef = useDisabled();
const blockProps = useBlockProps({ ref: isEditing ? null : disabledRef });
const label = __("RSS URL");
if (isEditing) return /* @__PURE__ */ jsx("div", {
...blockProps,
children: /* @__PURE__ */ jsx(Placeholder, {
icon: rss,
label,
instructions: __("Display entries from any RSS or Atom feed."),
children: /* @__PURE__ */ jsxs("form", {
onSubmit: onSubmitURL,
className: "wp-block-rss__placeholder-form",
children: [/* @__PURE__ */ jsx(__experimentalInputControl, {
label,
type: "url",
hideLabelFromVision: true,
placeholder: __("Enter URL here…"),
value: url,
onChange: (value) => setURL(value),
className: "wp-block-rss__placeholder-input"
}), /* @__PURE__ */ jsx(Button, {
__next40pxDefaultSize: true,
variant: "primary",
type: "submit",
children: __("Apply")
})]
})
})
});
const toolbarControls = [
{
icon: pencil,
title: __("Edit RSS URL"),
onClick: () => setIsEditing(true)
},
{
icon: list,
title: _x("List view", "RSS block display setting"),
onClick: () => setAttributes({ blockLayout: "list" }),
isActive: blockLayout === "list"
},
{
icon: grid,
title: _x("Grid view", "RSS block display setting"),
onClick: () => setAttributes({ blockLayout: "grid" }),
isActive: blockLayout === "grid"
}
];
return /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(BlockControls, { children: /* @__PURE__ */ jsx(ToolbarGroup, { controls: toolbarControls }) }),
/* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({
itemsToShow: 5,
displayAuthor: false,
displayDate: false,
displayExcerpt: false,
excerptLength: 55,
columns: 2,
openInNewTab: false
});
},
dropdownMenuProps,
children: [
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Number of items"),
hasValue: () => itemsToShow !== 5,
onDeselect: () => setAttributes({ itemsToShow: 5 }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(RangeControl, {
label: __("Number of items"),
value: itemsToShow,
onChange: (value) => setAttributes({ itemsToShow: value }),
min: DEFAULT_MIN_ITEMS,
max: DEFAULT_MAX_ITEMS,
required: true
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Display author"),
hasValue: () => !!displayAuthor,
onDeselect: () => setAttributes({ displayAuthor: false }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Display author"),
checked: displayAuthor,
onChange: toggleAttribute("displayAuthor")
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Display date"),
hasValue: () => !!displayDate,
onDeselect: () => setAttributes({ displayDate: false }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Display date"),
checked: displayDate,
onChange: toggleAttribute("displayDate")
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Display excerpt"),
hasValue: () => !!displayExcerpt,
onDeselect: () => setAttributes({ displayExcerpt: false }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Display excerpt"),
checked: displayExcerpt,
onChange: toggleAttribute("displayExcerpt")
})
}),
displayExcerpt && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Max number of words in excerpt"),
hasValue: () => excerptLength !== 55,
onDeselect: () => setAttributes({ excerptLength: 55 }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(RangeControl, {
label: __("Max number of words in excerpt"),
value: excerptLength,
onChange: (value) => setAttributes({ excerptLength: value }),
min: 10,
max: 100,
required: true
})
}),
blockLayout === "grid" && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Columns"),
hasValue: () => columns !== 2,
onDeselect: () => setAttributes({ columns: 2 }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(RangeControl, {
label: __("Columns"),
value: columns,
onChange: (value) => setAttributes({ columns: value }),
min: 2,
max: 6,
required: true
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Open links in new tab"),
hasValue: () => !!openInNewTab,
onDeselect: () => setAttributes({ openInNewTab: false }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Open links in new tab"),
checked: openInNewTab,
onChange: (value) => setAttributes({ openInNewTab: value })
})
})
]
}) }),
/* @__PURE__ */ jsx(InspectorControls, {
group: "advanced",
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: (value) => setAttributes({ rel: value })
})
}),
status === "loading" && /* @__PURE__ */ jsx("div", {
...blockProps,
children: /* @__PURE__ */ jsx(Spinner, {})
}),
status === "error" && /* @__PURE__ */ jsx("div", {
...blockProps,
children: /* @__PURE__ */ jsx("p", { children: sprintf(__("Error: %s"), error) })
}),
status === "success" && /* @__PURE__ */ jsx(HtmlRenderer, {
wrapperProps: blockProps,
html: content
})
] });
}
export { RSSEdit as default };
//# sourceMappingURL=edit.mjs.map