UNPKG

@wordpress/edit-post

Version:
68 lines (60 loc) 1.62 kB
import { createElement } from "@wordpress/element"; /** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; import { MenuItemsChoice, MenuGroup } from '@wordpress/components'; import { useSelect, useDispatch } from '@wordpress/data'; import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts'; /** * Internal dependencies */ import { store as editPostStore } from '../../../store'; /** * Set of available mode options. * * @type {Array} */ const MODES = [{ value: 'visual', label: __('Visual editor') }, { value: 'text', label: __('Code editor') }]; function ModeSwitcher() { const { shortcut, isRichEditingEnabled, isCodeEditingEnabled, mode } = useSelect(select => ({ shortcut: select(keyboardShortcutsStore).getShortcutRepresentation('core/edit-post/toggle-mode'), isRichEditingEnabled: select('core/editor').getEditorSettings().richEditingEnabled, isCodeEditingEnabled: select('core/editor').getEditorSettings().codeEditingEnabled, mode: select(editPostStore).getEditorMode() }), []); const { switchEditorMode } = useDispatch(editPostStore); if (!isRichEditingEnabled || !isCodeEditingEnabled) { return null; } const choices = MODES.map(choice => { if (choice.value !== mode) { return { ...choice, shortcut }; } return choice; }); return createElement(MenuGroup, { label: __('Editor') }, createElement(MenuItemsChoice, { choices: choices, value: mode, onSelect: switchEditorMode })); } export default ModeSwitcher; //# sourceMappingURL=index.js.map