@wordpress/edit-post
Version:
Edit Post module for WordPress.
401 lines (340 loc) • 11.7 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getEditorMode = getEditorMode;
exports.getPreferences = getPreferences;
exports.getPreference = getPreference;
exports.isPublishSidebarOpened = isPublishSidebarOpened;
exports.isEditorPanelRemoved = isEditorPanelRemoved;
exports.isEditorPanelEnabled = isEditorPanelEnabled;
exports.isEditorPanelOpened = isEditorPanelOpened;
exports.isModalActive = isModalActive;
exports.isFeatureActive = isFeatureActive;
exports.isMetaBoxLocationVisible = isMetaBoxLocationVisible;
exports.isMetaBoxLocationActive = isMetaBoxLocationActive;
exports.getMetaBoxesPerLocation = getMetaBoxesPerLocation;
exports.hasMetaBoxes = hasMetaBoxes;
exports.isSavingMetaBoxes = isSavingMetaBoxes;
exports.__experimentalGetPreviewDeviceType = __experimentalGetPreviewDeviceType;
exports.isInserterOpened = isInserterOpened;
exports.__experimentalGetInsertionPoint = __experimentalGetInsertionPoint;
exports.isEditingTemplate = isEditingTemplate;
exports.getEditedPostTemplate = exports.getAllMetaBoxes = exports.getActiveMetaBoxLocations = exports.isPluginItemPinned = exports.getActiveGeneralSidebarName = exports.isPluginSidebarOpened = exports.isEditorSidebarOpened = void 0;
var _rememo = _interopRequireDefault(require("rememo"));
var _lodash = require("lodash");
var _data = require("@wordpress/data");
var _interface = require("@wordpress/interface");
var _coreData = require("@wordpress/core-data");
var _editor = require("@wordpress/editor");
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
/**
* Returns the current editing mode.
*
* @param {Object} state Global application state.
*
* @return {string} Editing mode.
*/
function getEditorMode(state) {
return getPreference(state, 'editorMode', 'visual');
}
/**
* Returns true if the editor sidebar is opened.
*
* @param {Object} state Global application state
*
* @return {boolean} Whether the editor sidebar is opened.
*/
const isEditorSidebarOpened = (0, _data.createRegistrySelector)(select => () => {
const activeGeneralSidebar = select(_interface.store).getActiveComplementaryArea('core/edit-post');
return (0, _lodash.includes)(['edit-post/document', 'edit-post/block'], activeGeneralSidebar);
});
/**
* Returns true if the plugin sidebar is opened.
*
* @param {Object} state Global application state
* @return {boolean} Whether the plugin sidebar is opened.
*/
exports.isEditorSidebarOpened = isEditorSidebarOpened;
const isPluginSidebarOpened = (0, _data.createRegistrySelector)(select => () => {
const activeGeneralSidebar = select(_interface.store).getActiveComplementaryArea('core/edit-post');
return !!activeGeneralSidebar && !(0, _lodash.includes)(['edit-post/document', 'edit-post/block'], activeGeneralSidebar);
});
/**
* Returns the current active general sidebar name, or null if there is no
* general sidebar active. The active general sidebar is a unique name to
* identify either an editor or plugin sidebar.
*
* Examples:
*
* - `edit-post/document`
* - `my-plugin/insert-image-sidebar`
*
* @param {Object} state Global application state.
*
* @return {?string} Active general sidebar name.
*/
exports.isPluginSidebarOpened = isPluginSidebarOpened;
const getActiveGeneralSidebarName = (0, _data.createRegistrySelector)(select => () => {
return select(_interface.store).getActiveComplementaryArea('core/edit-post');
});
/**
* Returns the preferences (these preferences are persisted locally).
*
* @param {Object} state Global application state.
*
* @return {Object} Preferences Object.
*/
exports.getActiveGeneralSidebarName = getActiveGeneralSidebarName;
function getPreferences(state) {
return state.preferences;
}
/**
*
* @param {Object} state Global application state.
* @param {string} preferenceKey Preference Key.
* @param {*} defaultValue Default Value.
*
* @return {*} Preference Value.
*/
function getPreference(state, preferenceKey, defaultValue) {
const preferences = getPreferences(state);
const value = preferences[preferenceKey];
return value === undefined ? defaultValue : value;
}
/**
* Returns true if the publish sidebar is opened.
*
* @param {Object} state Global application state
*
* @return {boolean} Whether the publish sidebar is open.
*/
function isPublishSidebarOpened(state) {
return state.publishSidebarActive;
}
/**
* Returns true if the given panel was programmatically removed, or false otherwise.
* All panels are not removed by default.
*
* @param {Object} state Global application state.
* @param {string} panelName A string that identifies the panel.
*
* @return {boolean} Whether or not the panel is removed.
*/
function isEditorPanelRemoved(state, panelName) {
return (0, _lodash.includes)(state.removedPanels, panelName);
}
/**
* Returns true if the given panel is enabled, or false otherwise. Panels are
* enabled by default.
*
* @param {Object} state Global application state.
* @param {string} panelName A string that identifies the panel.
*
* @return {boolean} Whether or not the panel is enabled.
*/
function isEditorPanelEnabled(state, panelName) {
const panels = getPreference(state, 'panels');
return !isEditorPanelRemoved(state, panelName) && (0, _lodash.get)(panels, [panelName, 'enabled'], true);
}
/**
* Returns true if the given panel is open, or false otherwise. Panels are
* closed by default.
*
* @param {Object} state Global application state.
* @param {string} panelName A string that identifies the panel.
*
* @return {boolean} Whether or not the panel is open.
*/
function isEditorPanelOpened(state, panelName) {
const panels = getPreference(state, 'panels');
return (0, _lodash.get)(panels, [panelName]) === true || (0, _lodash.get)(panels, [panelName, 'opened']) === true;
}
/**
* Returns true if a modal is active, or false otherwise.
*
* @param {Object} state Global application state.
* @param {string} modalName A string that uniquely identifies the modal.
*
* @return {boolean} Whether the modal is active.
*/
function isModalActive(state, modalName) {
return state.activeModal === modalName;
}
/**
* Returns whether the given feature is enabled or not.
*
* @param {Object} state Global application state.
* @param {string} feature Feature slug.
*
* @return {boolean} Is active.
*/
function isFeatureActive(state, feature) {
return (0, _lodash.get)(state.preferences.features, [feature], false);
}
/**
* Returns true if the plugin item is pinned to the header.
* When the value is not set it defaults to true.
*
* @param {Object} state Global application state.
* @param {string} pluginName Plugin item name.
*
* @return {boolean} Whether the plugin item is pinned.
*/
const isPluginItemPinned = (0, _data.createRegistrySelector)(select => pluginName => {
return select(_interface.store).isItemPinned('core/edit-post', pluginName);
});
/**
* Returns an array of active meta box locations.
*
* @param {Object} state Post editor state.
*
* @return {string[]} Active meta box locations.
*/
exports.isPluginItemPinned = isPluginItemPinned;
const getActiveMetaBoxLocations = (0, _rememo.default)(state => {
return Object.keys(state.metaBoxes.locations).filter(location => isMetaBoxLocationActive(state, location));
}, state => [state.metaBoxes.locations]);
/**
* Returns true if a metabox location is active and visible
*
* @param {Object} state Post editor state.
* @param {string} location Meta box location to test.
*
* @return {boolean} Whether the meta box location is active and visible.
*/
exports.getActiveMetaBoxLocations = getActiveMetaBoxLocations;
function isMetaBoxLocationVisible(state, location) {
return isMetaBoxLocationActive(state, location) && (0, _lodash.some)(getMetaBoxesPerLocation(state, location), ({
id
}) => {
return isEditorPanelEnabled(state, `meta-box-${id}`);
});
}
/**
* Returns true if there is an active meta box in the given location, or false
* otherwise.
*
* @param {Object} state Post editor state.
* @param {string} location Meta box location to test.
*
* @return {boolean} Whether the meta box location is active.
*/
function isMetaBoxLocationActive(state, location) {
const metaBoxes = getMetaBoxesPerLocation(state, location);
return !!metaBoxes && metaBoxes.length !== 0;
}
/**
* Returns the list of all the available meta boxes for a given location.
*
* @param {Object} state Global application state.
* @param {string} location Meta box location to test.
*
* @return {?Array} List of meta boxes.
*/
function getMetaBoxesPerLocation(state, location) {
return state.metaBoxes.locations[location];
}
/**
* Returns the list of all the available meta boxes.
*
* @param {Object} state Global application state.
*
* @return {Array} List of meta boxes.
*/
const getAllMetaBoxes = (0, _rememo.default)(state => {
return (0, _lodash.flatten)((0, _lodash.values)(state.metaBoxes.locations));
}, state => [state.metaBoxes.locations]);
/**
* Returns true if the post is using Meta Boxes
*
* @param {Object} state Global application state
*
* @return {boolean} Whether there are metaboxes or not.
*/
exports.getAllMetaBoxes = getAllMetaBoxes;
function hasMetaBoxes(state) {
return getActiveMetaBoxLocations(state).length > 0;
}
/**
* Returns true if the Meta Boxes are being saved.
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether the metaboxes are being saved.
*/
function isSavingMetaBoxes(state) {
return state.metaBoxes.isSaving;
}
/**
* Returns the current editing canvas device type.
*
* @param {Object} state Global application state.
*
* @return {string} Device type.
*/
function __experimentalGetPreviewDeviceType(state) {
return state.deviceType;
}
/**
* Returns true if the inserter is opened.
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether the inserter is opened.
*/
function isInserterOpened(state) {
return !!state.blockInserterPanel;
}
/**
* Get the insertion point for the inserter.
*
* @param {Object} state Global application state.
*
* @return {Object} The root client ID and index to insert at.
*/
function __experimentalGetInsertionPoint(state) {
const {
rootClientId,
insertionIndex
} = state.blockInserterPanel;
return {
rootClientId,
insertionIndex
};
}
/**
* Returns true if the template editing mode is enabled.
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether we're editing the template.
*/
function isEditingTemplate(state) {
return state.isEditingTemplate;
}
/**
* Retrieves the template of the currently edited post.
*
* @return {Object?} Post Template.
*/
const getEditedPostTemplate = (0, _data.createRegistrySelector)(select => () => {
const currentTemplate = select(_editor.store).getEditedPostAttribute('template');
if (currentTemplate) {
var _select$getEntityReco;
return (_select$getEntityReco = select(_coreData.store).getEntityRecords('postType', 'wp_template')) === null || _select$getEntityReco === void 0 ? void 0 : _select$getEntityReco.find(template => template.slug === currentTemplate);
}
const post = select(_editor.store).getCurrentPost();
if (post.link && post.status !== 'auto-draft') {
return select(_coreData.store).__experimentalGetTemplateForLink(post.link);
}
return null;
});
exports.getEditedPostTemplate = getEditedPostTemplate;
//# sourceMappingURL=selectors.js.map