@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
86 lines (83 loc) • 2.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _i18n = require("@wordpress/i18n");
var _components = require("@wordpress/components");
var _data = require("@wordpress/data");
var _keyboardShortcuts = require("@wordpress/keyboard-shortcuts");
var _store = require("../../store");
var _jsxRuntime = require("react/jsx-runtime");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
/**
* Set of available mode options.
*
* @type {Array}
*/const MODES = [{
value: 'visual',
label: (0, _i18n.__)('Visual editor')
}, {
value: 'text',
label: (0, _i18n.__)('Code editor')
}];
function ModeSwitcher() {
const {
shortcut,
isRichEditingEnabled,
isCodeEditingEnabled,
mode
} = (0, _data.useSelect)(select => ({
shortcut: select(_keyboardShortcuts.store).getShortcutRepresentation('core/editor/toggle-mode'),
isRichEditingEnabled: select(_store.store).getEditorSettings().richEditingEnabled,
isCodeEditingEnabled: select(_store.store).getEditorSettings().codeEditingEnabled,
mode: select(_store.store).getEditorMode()
}), []);
const {
switchEditorMode
} = (0, _data.useDispatch)(_store.store);
let selectedMode = mode;
if (!isRichEditingEnabled && mode === 'visual') {
selectedMode = 'text';
}
if (!isCodeEditingEnabled && mode === 'text') {
selectedMode = 'visual';
}
const choices = MODES.map(choice => {
if (!isCodeEditingEnabled && choice.value === 'text') {
choice = {
...choice,
disabled: true
};
}
if (!isRichEditingEnabled && choice.value === 'visual') {
choice = {
...choice,
disabled: true,
info: (0, _i18n.__)('You can enable the visual editor in your profile settings.')
};
}
if (choice.value !== selectedMode && !choice.disabled) {
return {
...choice,
shortcut
};
}
return choice;
});
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuGroup, {
label: (0, _i18n.__)('Editor'),
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItemsChoice, {
choices: choices,
value: selectedMode,
onSelect: switchEditorMode
})
});
}
var _default = exports.default = ModeSwitcher;
//# sourceMappingURL=index.js.map