@wordpress/block-library
Version:
Block library for the WordPress editor.
55 lines (54 loc) • 1.96 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_components = require("@wordpress/components");
let _wordpress_data = require("@wordpress/data");
let _wordpress_core_data = require("@wordpress/core-data");
let ______utils_js = require("../../utils.cjs");
let react_jsx_runtime = require("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 = (0, _wordpress_data.useSelect)((select) => {
const { getUsers } = select(_wordpress_core_data.store);
return getUsers(AUTHORS_QUERY);
}, []);
if (!authorsList) return null;
const authorsInfo = (0, ______utils_js.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__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.FormTokenField, {
label: (0, _wordpress_i18n.__)("Authors"),
value: sanitizedValue,
suggestions: authorsInfo.names,
onChange: onAuthorChange,
help: ""
});
}
exports.default = AuthorControl;
//# sourceMappingURL=author-control.cjs.map