@wordpress/interface
Version:
Interface module for WordPress. The package contains shared functionality across the modern JavaScript-based WordPress screens.
226 lines (213 loc) • 6.36 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.closeModal = closeModal;
exports.enableComplementaryArea = exports.disableComplementaryArea = void 0;
exports.openModal = openModal;
exports.setDefaultComplementaryArea = exports.pinItem = void 0;
exports.setFeatureDefaults = setFeatureDefaults;
exports.setFeatureValue = setFeatureValue;
exports.toggleFeature = toggleFeature;
exports.unpinItem = void 0;
var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
var _preferences = require("@wordpress/preferences");
var _deprecated2 = require("./deprecated");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
/**
* Set a default complementary area.
*
* @param {string} scope Complementary area scope.
* @param {string} area Area identifier.
*
* @return {Object} Action object.
*/
const setDefaultComplementaryArea = (scope, area) => {
scope = (0, _deprecated2.normalizeComplementaryAreaScope)(scope);
area = (0, _deprecated2.normalizeComplementaryAreaName)(scope, area);
return {
type: 'SET_DEFAULT_COMPLEMENTARY_AREA',
scope,
area
};
};
/**
* Enable the complementary area.
*
* @param {string} scope Complementary area scope.
* @param {string} area Area identifier.
*/
exports.setDefaultComplementaryArea = setDefaultComplementaryArea;
const enableComplementaryArea = (scope, area) => ({
registry,
dispatch
}) => {
// Return early if there's no area.
if (!area) {
return;
}
scope = (0, _deprecated2.normalizeComplementaryAreaScope)(scope);
area = (0, _deprecated2.normalizeComplementaryAreaName)(scope, area);
const isComplementaryAreaVisible = registry.select(_preferences.store).get(scope, 'isComplementaryAreaVisible');
if (!isComplementaryAreaVisible) {
registry.dispatch(_preferences.store).set(scope, 'isComplementaryAreaVisible', true);
}
dispatch({
type: 'ENABLE_COMPLEMENTARY_AREA',
scope,
area
});
};
/**
* Disable the complementary area.
*
* @param {string} scope Complementary area scope.
*/
exports.enableComplementaryArea = enableComplementaryArea;
const disableComplementaryArea = scope => ({
registry
}) => {
scope = (0, _deprecated2.normalizeComplementaryAreaScope)(scope);
const isComplementaryAreaVisible = registry.select(_preferences.store).get(scope, 'isComplementaryAreaVisible');
if (isComplementaryAreaVisible) {
registry.dispatch(_preferences.store).set(scope, 'isComplementaryAreaVisible', false);
}
};
/**
* Pins an item.
*
* @param {string} scope Item scope.
* @param {string} item Item identifier.
*
* @return {Object} Action object.
*/
exports.disableComplementaryArea = disableComplementaryArea;
const pinItem = (scope, item) => ({
registry
}) => {
// Return early if there's no item.
if (!item) {
return;
}
scope = (0, _deprecated2.normalizeComplementaryAreaScope)(scope);
item = (0, _deprecated2.normalizeComplementaryAreaName)(scope, item);
const pinnedItems = registry.select(_preferences.store).get(scope, 'pinnedItems');
// The item is already pinned, there's nothing to do.
if (pinnedItems?.[item] === true) {
return;
}
registry.dispatch(_preferences.store).set(scope, 'pinnedItems', {
...pinnedItems,
[item]: true
});
};
/**
* Unpins an item.
*
* @param {string} scope Item scope.
* @param {string} item Item identifier.
*/
exports.pinItem = pinItem;
const unpinItem = (scope, item) => ({
registry
}) => {
// Return early if there's no item.
if (!item) {
return;
}
scope = (0, _deprecated2.normalizeComplementaryAreaScope)(scope);
item = (0, _deprecated2.normalizeComplementaryAreaName)(scope, item);
const pinnedItems = registry.select(_preferences.store).get(scope, 'pinnedItems');
registry.dispatch(_preferences.store).set(scope, 'pinnedItems', {
...pinnedItems,
[item]: false
});
};
/**
* Returns an action object used in signalling that a feature should be toggled.
*
* @param {string} scope The feature scope (e.g. core/edit-post).
* @param {string} featureName The feature name.
*/
exports.unpinItem = unpinItem;
function toggleFeature(scope, featureName) {
return function ({
registry
}) {
(0, _deprecated.default)(`dispatch( 'core/interface' ).toggleFeature`, {
since: '6.0',
alternative: `dispatch( 'core/preferences' ).toggle`
});
registry.dispatch(_preferences.store).toggle(scope, featureName);
};
}
/**
* Returns an action object used in signalling that a feature should be set to
* a true or false value
*
* @param {string} scope The feature scope (e.g. core/edit-post).
* @param {string} featureName The feature name.
* @param {boolean} value The value to set.
*
* @return {Object} Action object.
*/
function setFeatureValue(scope, featureName, value) {
return function ({
registry
}) {
(0, _deprecated.default)(`dispatch( 'core/interface' ).setFeatureValue`, {
since: '6.0',
alternative: `dispatch( 'core/preferences' ).set`
});
registry.dispatch(_preferences.store).set(scope, featureName, !!value);
};
}
/**
* Returns an action object used in signalling that defaults should be set for features.
*
* @param {string} scope The feature scope (e.g. core/edit-post).
* @param {Object<string, boolean>} defaults A key/value map of feature names to values.
*
* @return {Object} Action object.
*/
function setFeatureDefaults(scope, defaults) {
return function ({
registry
}) {
(0, _deprecated.default)(`dispatch( 'core/interface' ).setFeatureDefaults`, {
since: '6.0',
alternative: `dispatch( 'core/preferences' ).setDefaults`
});
registry.dispatch(_preferences.store).setDefaults(scope, defaults);
};
}
/**
* Returns an action object used in signalling that the user opened a modal.
*
* @param {string} name A string that uniquely identifies the modal.
*
* @return {Object} Action object.
*/
function openModal(name) {
return {
type: 'OPEN_MODAL',
name
};
}
/**
* Returns an action object signalling that the user closed a modal.
*
* @return {Object} Action object.
*/
function closeModal() {
return {
type: 'CLOSE_MODAL'
};
}
//# sourceMappingURL=actions.js.map