-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try: Reusable block edit locking #39950
Changes from 5 commits
1eb0057
eb392d3
19610a9
4162705
3b84542
4656245
71ebda8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,11 @@ import useBlockLock from './use-block-lock'; | |
import BlockLockModal from './modal'; | ||
|
||
export default function BlockLockMenuItem( { clientId } ) { | ||
const { canMove, canRemove, canLock } = useBlockLock( clientId, true ); | ||
const isLocked = ! canMove || ! canRemove; | ||
const { canEdit, canMove, canRemove, canLock } = useBlockLock( | ||
clientId, | ||
true | ||
); | ||
const isLocked = ! canEdit || ! canMove || ! canRemove; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we also use it as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and |
||
|
||
const [ isModalOpen, toggleModal ] = useReducer( | ||
( isActive ) => ! isActive, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,8 @@ import { | |
} from '@wordpress/components'; | ||
import { lock as lockIcon, unlock as unlockIcon } from '@wordpress/icons'; | ||
import { useInstanceId } from '@wordpress/compose'; | ||
import { useDispatch } from '@wordpress/data'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { isReusableBlock, getBlockType } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -24,7 +25,18 @@ import { store as blockEditorStore } from '../../store'; | |
|
||
export default function BlockLockModal( { clientId, onClose } ) { | ||
const [ lock, setLock ] = useState( { move: false, remove: false } ); | ||
const { canMove, canRemove } = useBlockLock( clientId, true ); | ||
const { canEdit, canMove, canRemove } = useBlockLock( clientId, true ); | ||
const { isReusable } = useSelect( | ||
( select ) => { | ||
const { getBlockName } = select( blockEditorStore ); | ||
const blockName = getBlockName( clientId ); | ||
|
||
return { | ||
isReusable: isReusableBlock( getBlockType( blockName ) ), | ||
}; | ||
}, | ||
[ clientId ] | ||
); | ||
const { updateBlockAttributes } = useDispatch( blockEditorStore ); | ||
const blockInformation = useBlockDisplayInformation( clientId ); | ||
const instanceId = useInstanceId( | ||
|
@@ -36,12 +48,12 @@ export default function BlockLockModal( { clientId, onClose } ) { | |
setLock( { | ||
move: ! canMove, | ||
remove: ! canRemove, | ||
...( isReusable ? { edit: ! canEdit } : {} ), | ||
} ); | ||
}, [ canMove, canRemove ] ); | ||
}, [ canEdit, canMove, canRemove, isReusable ] ); | ||
|
||
const isAllChecked = Object.values( lock ).every( Boolean ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be replaced with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The effect uses these values, not derived one. |
||
const isIndeterminate = | ||
Object.values( lock ).some( Boolean ) && ! isAllChecked; | ||
const isMixed = Object.values( lock ).some( Boolean ) && ! isAllChecked; | ||
|
||
return ( | ||
<Modal | ||
|
@@ -77,15 +89,41 @@ export default function BlockLockModal( { clientId, onClose } ) { | |
<span id={ instanceId }>{ __( 'Lock all' ) }</span> | ||
} | ||
checked={ isAllChecked } | ||
indeterminate={ isIndeterminate } | ||
indeterminate={ isMixed } | ||
onChange={ ( newValue ) => | ||
setLock( { | ||
move: newValue, | ||
remove: newValue, | ||
...( isReusable ? { edit: newValue } : {} ), | ||
} ) | ||
} | ||
/> | ||
<ul className="block-editor-block-lock-modal__checklist"> | ||
{ isReusable && ( | ||
<li className="block-editor-block-lock-modal__checklist-item"> | ||
<CheckboxControl | ||
label={ | ||
<> | ||
{ __( 'Restrict editing' ) } | ||
<Icon | ||
icon={ | ||
lock.edit | ||
? lockIcon | ||
: unlockIcon | ||
} | ||
/> | ||
</> | ||
} | ||
checked={ !! lock.edit } | ||
onChange={ ( edit ) => | ||
setLock( ( prevLock ) => ( { | ||
...prevLock, | ||
edit, | ||
} ) ) | ||
} | ||
/> | ||
</li> | ||
) } | ||
<li className="block-editor-block-lock-modal__checklist-item"> | ||
<CheckboxControl | ||
label={ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
canEdit
needed here? TheuseEffect
above seems to setisOverlayActive
back totrue
even if it becomesfalse
here. Or is this check meant to prevent this tiny blip ofisOverLayActive
being set to false and then right away to true?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. It's not always noticeable, but there's also no need to attach an event callback when editing is disabled.