@wordpress/block-library
Version:
Block library for the WordPress editor.
50 lines (49 loc) • 1.66 kB
JavaScript
import { __ } from "@wordpress/i18n";
import { FormTokenField } from "@wordpress/components";
import { useSelect } from "@wordpress/data";
import { store } from "@wordpress/core-data";
import { getEntitiesInfo } from "../../utils.mjs";
import { jsx } from "react/jsx-runtime";
// packages/block-library/src/query/edit/inspector-controls/author-control.jsx
const AUTHORS_QUERY = {
who: "authors",
per_page: -1,
_fields: "id,name",
context: "view"
};
function AuthorControl({ value, onChange }) {
const authorsList = useSelect((select) => {
const { getUsers } = select(store);
return getUsers(AUTHORS_QUERY);
}, []);
if (!authorsList) return null;
const authorsInfo = getEntitiesInfo(authorsList);
const sanitizedValue = (!value ? [] : value.toString().split(",")).reduce((accumulator, authorId) => {
const author = authorsInfo.mapById[authorId];
if (author) accumulator.push({
id: authorId,
value: author.name
});
return accumulator;
}, []);
const getIdByValue = (entitiesMappedByName, authorValue) => {
const id = authorValue?.id || entitiesMappedByName[authorValue]?.id;
if (id) return id;
};
const onAuthorChange = (newValue) => {
onChange({ author: Array.from(newValue.reduce((accumulator, author) => {
const id = getIdByValue(authorsInfo.mapByName, author);
if (id) accumulator.add(id);
return accumulator;
}, /* @__PURE__ */ new Set())).join(",") });
};
return /* @__PURE__ */ jsx(FormTokenField, {
label: __("Authors"),
value: sanitizedValue,
suggestions: authorsInfo.names,
onChange: onAuthorChange,
help: ""
});
}
export { AuthorControl as default };
//# sourceMappingURL=author-control.mjs.map