-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1eb0057
Introduce canEditBlock selector
Mamaduka eb392d3
Update BlockContentOverlay
Mamaduka 19610a9
Update UI
Mamaduka 4162705
Fix indeterminate state and e2e tests
Mamaduka 3b84542
Update list view
Mamaduka 4656245
Update selector
Mamaduka 71ebda8
Provide 'isLocked' value via hook
Mamaduka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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={ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.