UNPKG

@wordpress/block-editor

Version:
231 lines (230 loc) 9.2 kB
// packages/block-editor/src/components/block-lock/modal.js import { __, sprintf } from "@wordpress/i18n"; import { useEffect, useState } from "@wordpress/element"; import { Button, CheckboxControl, Flex, FlexItem, Icon, Modal, ToggleControl } from "@wordpress/components"; import { lock as lockIcon, unlock as unlockIcon } from "@wordpress/icons"; import { useDispatch, useSelect } from "@wordpress/data"; import { getBlockType } from "@wordpress/blocks"; import useBlockLock from "./use-block-lock"; import useBlockDisplayInformation from "../use-block-display-information"; import { store as blockEditorStore } from "../../store"; import { jsx, jsxs } from "react/jsx-runtime"; var ALLOWS_EDIT_LOCKING = ["core/navigation"]; function getTemplateLockValue(lock) { if (lock.remove && lock.move) { return "all"; } if (lock.remove && !lock.move) { return "insert"; } return false; } function BlockLockModal({ clientId, onClose }) { const [lock, setLock] = useState({ move: false, remove: false }); const { isEditLocked, isMoveLocked, isRemoveLocked } = useBlockLock(clientId); const { allowsEditLocking, templateLock, hasTemplateLock } = useSelect( (select) => { const { getBlockName, getBlockAttributes } = select(blockEditorStore); const blockName = getBlockName(clientId); const blockType = getBlockType(blockName); return { allowsEditLocking: ALLOWS_EDIT_LOCKING.includes(blockName), templateLock: getBlockAttributes(clientId)?.templateLock, hasTemplateLock: !!blockType?.attributes?.templateLock }; }, [clientId] ); const [applyTemplateLock, setApplyTemplateLock] = useState( !!templateLock ); const { updateBlockAttributes } = useDispatch(blockEditorStore); const blockInformation = useBlockDisplayInformation(clientId); useEffect(() => { setLock({ move: isMoveLocked, remove: isRemoveLocked, ...allowsEditLocking ? { edit: isEditLocked } : {} }); }, [isEditLocked, isMoveLocked, isRemoveLocked, allowsEditLocking]); const isAllChecked = Object.values(lock).every(Boolean); const isMixed = Object.values(lock).some(Boolean) && !isAllChecked; return /* @__PURE__ */ jsx( Modal, { title: sprintf( /* translators: %s: Name of the block. */ __("Lock %s"), blockInformation.title ), overlayClassName: "block-editor-block-lock-modal", onRequestClose: onClose, size: "small", children: /* @__PURE__ */ jsxs( "form", { onSubmit: (event) => { event.preventDefault(); updateBlockAttributes([clientId], { lock, templateLock: applyTemplateLock ? getTemplateLockValue(lock) : void 0 }); onClose(); }, children: [ /* @__PURE__ */ jsxs("fieldset", { className: "block-editor-block-lock-modal__options", children: [ /* @__PURE__ */ jsx("legend", { children: __("Select the features you want to lock") }), /* @__PURE__ */ jsx( "ul", { role: "list", className: "block-editor-block-lock-modal__checklist", children: /* @__PURE__ */ jsxs("li", { children: [ /* @__PURE__ */ jsx( CheckboxControl, { __nextHasNoMarginBottom: true, className: "block-editor-block-lock-modal__options-all", label: __("Lock all"), checked: isAllChecked, indeterminate: isMixed, onChange: (newValue) => setLock({ move: newValue, remove: newValue, ...allowsEditLocking ? { edit: newValue } : {} }) } ), /* @__PURE__ */ jsxs( "ul", { role: "list", className: "block-editor-block-lock-modal__checklist", children: [ allowsEditLocking && /* @__PURE__ */ jsxs("li", { className: "block-editor-block-lock-modal__checklist-item", children: [ /* @__PURE__ */ jsx( CheckboxControl, { __nextHasNoMarginBottom: true, label: __("Lock editing"), checked: !!lock.edit, onChange: (edit) => setLock((prevLock) => ({ ...prevLock, edit })) } ), /* @__PURE__ */ jsx( Icon, { className: "block-editor-block-lock-modal__lock-icon", icon: lock.edit ? lockIcon : unlockIcon } ) ] }), /* @__PURE__ */ jsxs("li", { className: "block-editor-block-lock-modal__checklist-item", children: [ /* @__PURE__ */ jsx( CheckboxControl, { __nextHasNoMarginBottom: true, label: __("Lock movement"), checked: lock.move, onChange: (move) => setLock((prevLock) => ({ ...prevLock, move })) } ), /* @__PURE__ */ jsx( Icon, { className: "block-editor-block-lock-modal__lock-icon", icon: lock.move ? lockIcon : unlockIcon } ) ] }), /* @__PURE__ */ jsxs("li", { className: "block-editor-block-lock-modal__checklist-item", children: [ /* @__PURE__ */ jsx( CheckboxControl, { __nextHasNoMarginBottom: true, label: __("Lock removal"), checked: lock.remove, onChange: (remove) => setLock((prevLock) => ({ ...prevLock, remove })) } ), /* @__PURE__ */ jsx( Icon, { className: "block-editor-block-lock-modal__lock-icon", icon: lock.remove ? lockIcon : unlockIcon } ) ] }) ] } ) ] }) } ), hasTemplateLock && /* @__PURE__ */ jsx( ToggleControl, { __nextHasNoMarginBottom: true, className: "block-editor-block-lock-modal__template-lock", label: __("Apply to all blocks inside"), checked: applyTemplateLock, disabled: lock.move && !lock.remove, onChange: () => setApplyTemplateLock(!applyTemplateLock) } ) ] }), /* @__PURE__ */ jsxs( Flex, { className: "block-editor-block-lock-modal__actions", justify: "flex-end", expanded: false, children: [ /* @__PURE__ */ jsx(FlexItem, { children: /* @__PURE__ */ jsx( Button, { variant: "tertiary", onClick: onClose, __next40pxDefaultSize: true, children: __("Cancel") } ) }), /* @__PURE__ */ jsx(FlexItem, { children: /* @__PURE__ */ jsx( Button, { variant: "primary", type: "submit", __next40pxDefaultSize: true, children: __("Apply") } ) }) ] } ) ] } ) } ); } export { BlockLockModal as default }; //# sourceMappingURL=modal.js.map