UNPKG

@wordpress/block-editor

Version:
110 lines (108 loc) 3.94 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-editor/src/components/block-visibility/menu-item.js var menu_item_exports = {}; __export(menu_item_exports, { default: () => BlockVisibilityMenuItem }); module.exports = __toCommonJS(menu_item_exports); var import_i18n = require("@wordpress/i18n"); var import_components = require("@wordpress/components"); var import_icons = require("@wordpress/icons"); var import_data = require("@wordpress/data"); var import_notices = require("@wordpress/notices"); var import_keyboard_shortcuts = require("@wordpress/keyboard-shortcuts"); var import_utils = require("../../hooks/utils.cjs"); var import_store = require("../../store/index.cjs"); var import_jsx_runtime = require("react/jsx-runtime"); function BlockVisibilityMenuItem({ clientIds }) { const { updateBlockAttributes } = (0, import_data.useDispatch)(import_store.store); const { createSuccessNotice } = (0, import_data.useDispatch)(import_notices.store); const blocks = (0, import_data.useSelect)( (select) => { return select(import_store.store).getBlocksByClientId(clientIds); }, [clientIds] ); const listViewShortcut = (0, import_data.useSelect)((select) => { return select(import_keyboard_shortcuts.store).getShortcutRepresentation( "core/editor/toggle-list-view" ); }, []); const hasHiddenBlock = blocks.some( (block) => block.attributes.metadata?.blockVisibility === false ); const toggleBlockVisibility = () => { const isHiding = !hasHiddenBlock; const attributesByClientId = Object.fromEntries( blocks?.map(({ clientId, attributes }) => [ clientId, { metadata: (0, import_utils.cleanEmptyObject)({ ...attributes?.metadata, blockVisibility: isHiding ? false : void 0 }) } ]) ); updateBlockAttributes(clientIds, attributesByClientId, { uniqueByBlock: true }); if (isHiding) { if (blocks.length > 1) { createSuccessNotice( (0, import_i18n.sprintf)( // translators: %s: The shortcut key to access the List View. (0, import_i18n.__)( "Blocks hidden. You can access them via the List View (%s)." ), listViewShortcut ), { id: "block-visibility-hidden", type: "snackbar" } ); } else { createSuccessNotice( (0, import_i18n.sprintf)( // translators: %s: The shortcut key to access the List View. (0, import_i18n.__)( "Block hidden. You can access it via the List View (%s)." ), listViewShortcut ), { id: "block-visibility-hidden", type: "snackbar" } ); } } }; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.MenuItem, { icon: hasHiddenBlock ? import_icons.seen : import_icons.unseen, onClick: toggleBlockVisibility, children: hasHiddenBlock ? (0, import_i18n.__)("Show") : (0, import_i18n.__)("Hide") } ); } //# sourceMappingURL=menu-item.cjs.map