@wordpress/block-library
Version:
Block library for the WordPress editor.
102 lines (81 loc) • 2.57 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _element = require("@wordpress/element");
var _i18n = require("@wordpress/i18n");
var _components = require("@wordpress/components");
var _data = require("@wordpress/data");
var _coreData = require("@wordpress/core-data");
var _utils = require("../../utils");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
const AUTHORS_QUERY = {
who: 'authors',
per_page: -1,
_fields: 'id,name',
context: 'view'
};
function AuthorControl(_ref) {
let {
value,
onChange
} = _ref;
const authorsList = (0, _data.useSelect)(select => {
const {
getUsers
} = select(_coreData.store);
return getUsers(AUTHORS_QUERY);
}, []);
if (!authorsList) {
return null;
}
const authorsInfo = (0, _utils.getEntitiesInfo)(authorsList);
/**
* We need to normalize the value because the block operates on a
* comma(`,`) separated string value and `FormTokenFiels` needs an
* array.
*/
const normalizedValue = !value ? [] : value.toString().split(','); // Returns only the existing authors ids. This prevents the component
// from crashing in the editor, when non existing ids are provided.
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) => {
var _entitiesMappedByName;
const id = (authorValue === null || authorValue === void 0 ? void 0 : authorValue.id) || ((_entitiesMappedByName = entitiesMappedByName[authorValue]) === null || _entitiesMappedByName === void 0 ? void 0 : _entitiesMappedByName.id);
if (id) return id;
};
const onAuthorChange = newValue => {
const ids = Array.from(newValue.reduce((accumulator, author) => {
// Verify that new values point to existing entities.
const id = getIdByValue(authorsInfo.mapByName, author);
if (id) accumulator.add(id);
return accumulator;
}, new Set()));
onChange({
author: ids.join(',')
});
};
return (0, _element.createElement)(_components.FormTokenField, {
label: (0, _i18n.__)('Authors'),
value: sanitizedValue,
suggestions: authorsInfo.names,
onChange: onAuthorChange
});
}
var _default = AuthorControl;
exports.default = _default;
//# sourceMappingURL=author-control.js.map
;