@wordpress/block-library
Version:
Block library for the WordPress editor.
72 lines (71 loc) • 2.05 kB
JavaScript
// packages/block-library/src/query/edit/inspector-controls/author-control.js
import { __ } from "@wordpress/i18n";
import { FormTokenField } from "@wordpress/components";
import { useSelect } from "@wordpress/data";
import { store as coreStore } from "@wordpress/core-data";
import { getEntitiesInfo } from "../../utils";
import { jsx } from "react/jsx-runtime";
var AUTHORS_QUERY = {
who: "authors",
per_page: -1,
_fields: "id,name",
context: "view"
};
function AuthorControl({ value, onChange }) {
const authorsList = useSelect((select) => {
const { getUsers } = select(coreStore);
return getUsers(AUTHORS_QUERY);
}, []);
if (!authorsList) {
return null;
}
const authorsInfo = getEntitiesInfo(authorsList);
const normalizedValue = !value ? [] : value.toString().split(",");
const sanitizedValue = normalizedValue.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) => {
const ids = Array.from(
newValue.reduce((accumulator, author) => {
const id = getIdByValue(authorsInfo.mapByName, author);
if (id) {
accumulator.add(id);
}
return accumulator;
}, /* @__PURE__ */ new Set())
);
onChange({ author: ids.join(",") });
};
return /* @__PURE__ */ jsx(
FormTokenField,
{
label: __("Authors"),
value: sanitizedValue,
suggestions: authorsInfo.names,
onChange: onAuthorChange,
__experimentalShowHowTo: false,
__next40pxDefaultSize: true
}
);
}
var author_control_default = AuthorControl;
export {
author_control_default as default
};
//# sourceMappingURL=author-control.js.map