UNPKG

@wordpress/edit-post

Version:
176 lines (150 loc) 6.06 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _element = require("@wordpress/element"); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _lodash = require("lodash"); var _blocks = require("@wordpress/blocks"); var _data = require("@wordpress/data"); var _editor = require("@wordpress/editor"); var _components = require("@wordpress/components"); var _preventEventDiscovery = _interopRequireDefault(require("./prevent-event-discovery")); var _layout = _interopRequireDefault(require("./components/layout")); var _editorInitialization = _interopRequireDefault(require("./components/editor-initialization")); var _editPostSettings = _interopRequireDefault(require("./components/edit-post-settings")); var _store = require("./store"); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ function Editor({ postId, postType, settings, initialEdits, onError, ...props }) { const { hasFixedToolbar, focusMode, hasReducedUI, hasThemeStyles, post, preferredStyleVariations, hiddenBlockTypes, blockTypes, __experimentalLocalAutosaveInterval, keepCaretInsideBlock, isTemplateMode, template } = (0, _data.useSelect)(select => { var _getPostType$viewable, _getPostType; const { isFeatureActive, getPreference, __experimentalGetPreviewDeviceType, isEditingTemplate, getEditedPostTemplate } = select(_store.store); const { getEntityRecord, getPostType, getEntityRecords } = select('core'); const { getEditorSettings } = select('core/editor'); const { getBlockTypes } = select(_blocks.store); const isTemplate = ['wp_template', 'wp_template_part'].includes(postType); // Ideally the initializeEditor function should be called using the ID of the REST endpoint. // to avoid the special case. let postObject; if (isTemplate) { const posts = getEntityRecords('postType', postType, { wp_id: postId }); postObject = posts === null || posts === void 0 ? void 0 : posts[0]; } else { postObject = getEntityRecord('postType', postType, postId); } const supportsTemplateMode = getEditorSettings().supportsTemplateMode; const isViewable = (_getPostType$viewable = (_getPostType = getPostType(postType)) === null || _getPostType === void 0 ? void 0 : _getPostType.viewable) !== null && _getPostType$viewable !== void 0 ? _getPostType$viewable : false; return { hasFixedToolbar: isFeatureActive('fixedToolbar') || __experimentalGetPreviewDeviceType() !== 'Desktop', focusMode: isFeatureActive('focusMode'), hasReducedUI: isFeatureActive('reducedUI'), hasThemeStyles: isFeatureActive('themeStyles'), preferredStyleVariations: getPreference('preferredStyleVariations'), hiddenBlockTypes: getPreference('hiddenBlockTypes'), blockTypes: getBlockTypes(), __experimentalLocalAutosaveInterval: getPreference('localAutosaveInterval'), keepCaretInsideBlock: isFeatureActive('keepCaretInsideBlock'), isTemplateMode: isEditingTemplate(), template: supportsTemplateMode && isViewable ? getEditedPostTemplate() : null, post: postObject }; }); const { updatePreferredStyleVariations, setIsInserterOpened } = (0, _data.useDispatch)(_store.store); const editorSettings = (0, _element.useMemo)(() => { const result = { ...(0, _lodash.omit)(settings, ['styles']), __experimentalPreferredStyleVariations: { value: preferredStyleVariations, onChange: updatePreferredStyleVariations }, hasFixedToolbar, focusMode, hasReducedUI, __experimentalLocalAutosaveInterval, // This is marked as experimental to give time for the quick inserter to mature. __experimentalSetIsInserterOpened: setIsInserterOpened, keepCaretInsideBlock }; // Omit hidden block types if exists and non-empty. if ((0, _lodash.size)(hiddenBlockTypes) > 0) { // Defer to passed setting for `allowedBlockTypes` if provided as // anything other than `true` (where `true` is equivalent to allow // all block types). const defaultAllowedBlockTypes = true === settings.allowedBlockTypes ? (0, _lodash.map)(blockTypes, 'name') : settings.allowedBlockTypes || []; result.allowedBlockTypes = (0, _lodash.without)(defaultAllowedBlockTypes, ...hiddenBlockTypes); } return result; }, [settings, hasFixedToolbar, focusMode, hasReducedUI, hiddenBlockTypes, blockTypes, preferredStyleVariations, __experimentalLocalAutosaveInterval, setIsInserterOpened, updatePreferredStyleVariations, keepCaretInsideBlock]); const styles = (0, _element.useMemo)(() => { return hasThemeStyles ? settings.styles : []; }, [settings, hasThemeStyles]); if (!post) { return null; } return (0, _element.createElement)(_element.StrictMode, null, (0, _element.createElement)(_editPostSettings.default.Provider, { value: settings }, (0, _element.createElement)(_components.SlotFillProvider, null, (0, _element.createElement)(_editor.EditorProvider, (0, _extends2.default)({ settings: editorSettings, post: post, initialEdits: initialEdits, useSubRegistry: false, __unstableTemplate: isTemplateMode ? template : undefined }, props), (0, _element.createElement)(_editor.ErrorBoundary, { onError: onError }, (0, _element.createElement)(_editorInitialization.default, { postId: postId }), (0, _element.createElement)(_layout.default, { styles: styles }), (0, _element.createElement)(_components.KeyboardShortcuts, { shortcuts: _preventEventDiscovery.default })), (0, _element.createElement)(_editor.PostLockedModal, null))))); } var _default = Editor; exports.default = _default; //# sourceMappingURL=editor.js.map