@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
74 lines (69 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = DisableNonPageContentBlocks;
var _data = require("@wordpress/data");
var _blockEditor = require("@wordpress/block-editor");
var _element = require("@wordpress/element");
var _hooks = require("@wordpress/hooks");
var _store = require("../../store");
var _lockUnlock = require("../../lock-unlock");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
const POST_CONTENT_BLOCK_TYPES = ['core/post-title', 'core/post-featured-image', 'core/post-content'];
/**
* Component that when rendered, makes it so that the site editor allows only
* page content to be edited.
*/
function DisableNonPageContentBlocks() {
const contentOnlyBlockTypes = (0, _element.useMemo)(() => [...(0, _hooks.applyFilters)('editor.postContentBlockTypes', POST_CONTENT_BLOCK_TYPES), 'core/template-part'], []);
// Note that there are two separate subscriptions because the result for each
// returns a new array.
const contentOnlyIds = (0, _data.useSelect)(select => {
const {
getPostBlocksByName
} = (0, _lockUnlock.unlock)(select(_store.store));
return getPostBlocksByName(contentOnlyBlockTypes);
}, [contentOnlyBlockTypes]);
const disabledIds = (0, _data.useSelect)(select => {
const {
getBlocksByName,
getBlockOrder
} = select(_blockEditor.store);
return getBlocksByName('core/template-part').flatMap(clientId => getBlockOrder(clientId));
}, []);
const registry = (0, _data.useRegistry)();
(0, _element.useEffect)(() => {
const {
setBlockEditingMode,
unsetBlockEditingMode
} = registry.dispatch(_blockEditor.store);
registry.batch(() => {
setBlockEditingMode('', 'disabled');
for (const clientId of contentOnlyIds) {
setBlockEditingMode(clientId, 'contentOnly');
}
for (const clientId of disabledIds) {
setBlockEditingMode(clientId, 'disabled');
}
});
return () => {
registry.batch(() => {
unsetBlockEditingMode('');
for (const clientId of contentOnlyIds) {
unsetBlockEditingMode(clientId);
}
for (const clientId of disabledIds) {
unsetBlockEditingMode(clientId);
}
});
};
}, [contentOnlyIds, disabledIds, registry]);
return null;
}
//# sourceMappingURL=disable-non-page-content-blocks.js.map