UNPKG

@wordpress/editor

Version:
81 lines (69 loc) 2.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = EntityRecordItem; var _element = require("@wordpress/element"); var _components = require("@wordpress/components"); var _i18n = require("@wordpress/i18n"); var _data = require("@wordpress/data"); var _blockEditor = require("@wordpress/block-editor"); /** * WordPress dependencies */ function EntityRecordItem({ record, checked, onChange, closePanel }) { const { name, kind, title, key } = record; const parentBlockId = (0, _data.useSelect)(select => { var _blocks$; // Get entity's blocks. const { blocks = [] } = select('core').getEditedEntityRecord(kind, name, key); // Get parents of the entity's first block. const parents = select(_blockEditor.store).getBlockParents((_blocks$ = blocks[0]) === null || _blocks$ === void 0 ? void 0 : _blocks$.clientId); // Return closest parent block's clientId. return parents[parents.length - 1]; }, []); // Handle templates that might use default descriptive titles const entityRecordTitle = (0, _data.useSelect)(select => { if ('postType' !== kind || 'wp_template' !== name) { return title; } const template = select('core').getEditedEntityRecord(kind, name, key); return select('core/editor').__experimentalGetTemplateInfo(template).title; }, [name, kind, title, key]); const isSelected = (0, _data.useSelect)(select => { const selectedBlockId = select(_blockEditor.store).getSelectedBlockClientId(); return selectedBlockId === parentBlockId; }, [parentBlockId]); const isSelectedText = isSelected ? (0, _i18n.__)('Selected') : (0, _i18n.__)('Select'); const { selectBlock } = (0, _data.useDispatch)(_blockEditor.store); const selectParentBlock = (0, _element.useCallback)(() => selectBlock(parentBlockId), [parentBlockId]); const selectAndDismiss = (0, _element.useCallback)(() => { selectBlock(parentBlockId); closePanel(); }, [parentBlockId]); return (0, _element.createElement)(_components.PanelRow, null, (0, _element.createElement)(_components.CheckboxControl, { label: (0, _element.createElement)("strong", null, entityRecordTitle || (0, _i18n.__)('Untitled')), checked: checked, onChange: onChange }), parentBlockId ? (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(_components.Button, { onClick: selectParentBlock, className: "entities-saved-states__find-entity", disabled: isSelected }, isSelectedText), (0, _element.createElement)(_components.Button, { onClick: selectAndDismiss, className: "entities-saved-states__find-entity-small", disabled: isSelected }, isSelectedText)) : null); } //# sourceMappingURL=entity-record-item.js.map