-
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
Add ability to prevent editing blocks using useBlockEditingMode() #50643
Merged
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5f3c1b2
Add ability to prevent editing blocks using useBlockEditingMode()
noisysocks bcf6949
Make useBlockEditingMode use context
noisysocks 6ab13d7
Remove rootBlockEditingMode setting
noisysocks 6426fca
Fix private createRegistrySelector selectors
noisysocks bd2f478
Consolidate templateLock=contentOnly logic into getBlockEditingMode
noisysocks f00dc25
Hide disabled blocks from List View
noisysocks c7e128c
Hide disabled blocks from breadcrumbs
noisysocks 2a9b9e5
Add doc comments
noisysocks 692cd76
Add unit tests
noisysocks 8162afe
Use @typedef to document mode param
noisysocks 52c4df0
Merge remote-tracking branch 'origin/trunk' into try/block-editing-mode
noisysocks 3d437a4
Restore packages/components/package.json from trunk
noisysocks 25f3e58
Restore packages/block-library/src/post-title/edit.js from trunk
noisysocks f1e1a11
Move BlockListBlockContext out of block.js so that it exists on mobil…
noisysocks 79a2bdf
DRY up blockEditingMode check
noisysocks 758b2de
Fix typo
noisysocks 7892260
Remove unnecessary comment
noisysocks 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
71 changes: 71 additions & 0 deletions
71
packages/block-editor/src/components/block-editing-mode/index.js
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { useContext, useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../../store'; | ||
import { unlock } from '../../lock-unlock'; | ||
import { BlockListBlockContext } from '../block-list/block-list-block-context'; | ||
|
||
/** | ||
* @typedef {'disabled'|'contentOnly'|'default'} BlockEditingMode | ||
*/ | ||
|
||
/** | ||
* Allows a block to restrict the user interface that is displayed for editing | ||
* that block and its inner blocks. | ||
* | ||
* @example | ||
* ```js | ||
* function MyBlock( { attributes, setAttributes } ) { | ||
* useBlockEditingMode( 'disabled' ); | ||
* return <div { ...useBlockProps() }></div>; | ||
* } | ||
* ``` | ||
* | ||
* `mode` can be one of three options: | ||
* | ||
* - `'disabled'`: Prevents editing the block entirely, i.e. it cannot be | ||
* selected. | ||
* - `'contentOnly'`: Hides all non-content UI, e.g. auxiliary controls in the | ||
* toolbar, the block movers, block settings. | ||
* - `'default'`: Allows editing the block as normal. | ||
* | ||
* The mode is inherited by all of the block's inner blocks, unless they have | ||
* their own mode. | ||
* | ||
* If called outside of a block context, the mode is applied to all blocks. | ||
* | ||
* @param {?BlockEditingMode} mode The editing mode to apply. If undefined, the | ||
* current editing mode is not changed. | ||
* | ||
* @return {BlockEditingMode} The current editing mode. | ||
*/ | ||
export function useBlockEditingMode( mode ) { | ||
ramonjd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const { clientId = '' } = useContext( BlockListBlockContext ) ?? {}; | ||
const blockEditingMode = useSelect( | ||
( select ) => | ||
unlock( select( blockEditorStore ) ).getBlockEditingMode( | ||
clientId | ||
), | ||
[ clientId ] | ||
); | ||
const { setBlockEditingMode, unsetBlockEditingMode } = unlock( | ||
useDispatch( blockEditorStore ) | ||
); | ||
useEffect( () => { | ||
if ( mode ) { | ||
setBlockEditingMode( clientId, mode ); | ||
} | ||
return () => { | ||
if ( mode ) { | ||
unsetBlockEditingMode( clientId ); | ||
} | ||
}; | ||
}, [ clientId, mode ] ); | ||
return blockEditingMode; | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/block-editor/src/components/block-list/block-list-block-context.js
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext } from '@wordpress/element'; | ||
|
||
export const BlockListBlockContext = createContext( null ); | ||
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. Pulling this out to its own file so that it's available for use on both web and mobile (React native). Hopefully that fixes mobile tests. |
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
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
Oops, something went wrong.
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.
Same here.