@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
71 lines (65 loc) • 2.56 kB
JavaScript
import { createElement, Fragment } from "@wordpress/element";
/**
* WordPress dependencies
*/
import { CheckboxControl, Button, PanelRow } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { useCallback } from '@wordpress/element';
import { store as blockEditorStore } from '@wordpress/block-editor';
export default function EntityRecordItem({
record,
checked,
onChange,
closePanel
}) {
const {
name,
kind,
title,
key
} = record;
const parentBlockId = 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(blockEditorStore).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 = 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 = useSelect(select => {
const selectedBlockId = select(blockEditorStore).getSelectedBlockClientId();
return selectedBlockId === parentBlockId;
}, [parentBlockId]);
const isSelectedText = isSelected ? __('Selected') : __('Select');
const {
selectBlock
} = useDispatch(blockEditorStore);
const selectParentBlock = useCallback(() => selectBlock(parentBlockId), [parentBlockId]);
const selectAndDismiss = useCallback(() => {
selectBlock(parentBlockId);
closePanel();
}, [parentBlockId]);
return createElement(PanelRow, null, createElement(CheckboxControl, {
label: createElement("strong", null, entityRecordTitle || __('Untitled')),
checked: checked,
onChange: onChange
}), parentBlockId ? createElement(Fragment, null, createElement(Button, {
onClick: selectParentBlock,
className: "entities-saved-states__find-entity",
disabled: isSelected
}, isSelectedText), createElement(Button, {
onClick: selectAndDismiss,
className: "entities-saved-states__find-entity-small",
disabled: isSelected
}, isSelectedText)) : null);
}
//# sourceMappingURL=entity-record-item.js.map