@wordpress/block-editor
Version:
91 lines (90 loc) • 3.05 kB
JavaScript
// packages/block-editor/src/components/use-block-display-information/index.js
import { useSelect } from "@wordpress/data";
import {
store as blocksStore,
isReusableBlock,
isTemplatePart,
__experimentalGetBlockLabel as getBlockLabel
} from "@wordpress/blocks";
import { __ } from "@wordpress/i18n";
import { symbol } from "@wordpress/icons";
import { store as blockEditorStore } from "../../store";
function getPositionTypeLabel(attributes) {
const positionType = attributes?.style?.position?.type;
if (positionType === "sticky") {
return __("Sticky");
}
if (positionType === "fixed") {
return __("Fixed");
}
return null;
}
function useBlockDisplayInformation(clientId) {
return useSelect(
(select) => {
if (!clientId) {
return null;
}
const {
getBlockName,
getBlockAttributes,
__experimentalGetParsedPattern
} = select(blockEditorStore);
const { getBlockType, getActiveBlockVariation } = select(blocksStore);
const blockName = getBlockName(clientId);
const blockType = getBlockType(blockName);
if (!blockType) {
return null;
}
const attributes = getBlockAttributes(clientId);
const patternName = attributes?.metadata?.patternName;
if (patternName && window?.__experimentalContentOnlyPatternInsertion) {
const pattern = __experimentalGetParsedPattern(patternName);
const positionLabel2 = getPositionTypeLabel(attributes);
return {
isSynced: false,
title: __("Pattern"),
icon: symbol,
description: pattern?.description || __("A block pattern."),
anchor: attributes?.anchor,
positionLabel: positionLabel2,
positionType: attributes?.style?.position?.type,
name: pattern?.title || attributes?.metadata?.name
};
}
const match = getActiveBlockVariation(blockName, attributes);
const isSynced = isReusableBlock(blockType) || isTemplatePart(blockType);
const syncedTitle = isSynced ? getBlockLabel(blockType, attributes) : void 0;
const title = syncedTitle || blockType.title;
const positionLabel = getPositionTypeLabel(attributes);
const blockTypeInfo = {
isSynced,
title,
icon: blockType.icon,
description: blockType.description,
anchor: attributes?.anchor,
positionLabel,
positionType: attributes?.style?.position?.type,
name: attributes?.metadata?.name
};
if (!match) {
return blockTypeInfo;
}
return {
isSynced,
title: match.title || blockType.title,
icon: match.icon || blockType.icon,
description: match.description || blockType.description,
anchor: attributes?.anchor,
positionLabel,
positionType: attributes?.style?.position?.type,
name: attributes?.metadata?.name
};
},
[clientId]
);
}
export {
useBlockDisplayInformation as default
};
//# sourceMappingURL=index.js.map