UNPKG

@wordpress/block-editor

Version:
51 lines (50 loc) 1.35 kB
/** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; import { MenuItem } from '@wordpress/components'; import { getBlockType, hasBlockSupport } from '@wordpress/blocks'; import { useDispatch, useSelect } from '@wordpress/data'; /** * Internal dependencies */ import { store as blockEditorStore } from '../../store'; import { jsx as _jsx } from "react/jsx-runtime"; const noop = () => {}; export default function BlockModeToggle({ clientId, onToggle = noop }) { const { blockType, mode, isCodeEditingEnabled } = useSelect(select => { const { getBlock, getBlockMode, getSettings } = select(blockEditorStore); const block = getBlock(clientId); return { mode: getBlockMode(clientId), blockType: block ? getBlockType(block.name) : null, isCodeEditingEnabled: getSettings().codeEditingEnabled }; }, [clientId]); const { toggleBlockMode } = useDispatch(blockEditorStore); if (!blockType || !hasBlockSupport(blockType, 'html', true) || !isCodeEditingEnabled) { return null; } const label = mode === 'visual' ? __('Edit as HTML') : __('Edit visually'); return /*#__PURE__*/_jsx(MenuItem, { onClick: () => { toggleBlockMode(clientId); onToggle(); }, children: label }); } //# sourceMappingURL=block-mode-toggle.js.map