-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Extract pattern-overrides code from the block-editor package #60887
Closed
Closed
Changes from all commits
Commits
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
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
90 changes: 90 additions & 0 deletions
90
packages/patterns/src/components/rename-block-modal-control.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,90 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { | ||
store as blockEditorStore, | ||
useBlockDisplayInformation, | ||
privateApis as blockEditorPrivateApis, | ||
} from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as patternsStore } from '../store'; | ||
import { unlock } from '../lock-unlock'; | ||
import { PATTERN_OVERRIDES_BINDING_SOURCE } from '../constants'; | ||
|
||
const { BlockRenameModal } = unlock( blockEditorPrivateApis ); | ||
|
||
function BlockRenameModalWrapper( { clientId } ) { | ||
const { metadata } = useSelect( | ||
( select ) => { | ||
const { getBlockAttributes } = select( blockEditorStore ); | ||
const attributes = getBlockAttributes( clientId ); | ||
return { | ||
metadata: attributes?.metadata, | ||
}; | ||
}, | ||
[ clientId ] | ||
); | ||
const { updateBlockAttributes } = useDispatch( blockEditorStore ); | ||
const { setRenamingBlock } = unlock( useDispatch( patternsStore ) ); | ||
const blockInformation = useBlockDisplayInformation( clientId ); | ||
const customName = metadata?.name ?? ''; | ||
const hasPatternOverrides = | ||
!! customName && | ||
!! metadata?.bindings && | ||
Object.values( metadata.bindings ).some( | ||
( binding ) => binding.source === PATTERN_OVERRIDES_BINDING_SOURCE | ||
); | ||
|
||
const closeModal = () => setRenamingBlock( null ); | ||
const onRename = ( newName ) => { | ||
// If the new value is the block's original name (e.g. `Group`) | ||
// or it is an empty string then assume the intent is to reset | ||
// the value. Therefore reset the metadata. | ||
if ( newName === blockInformation?.title || ! newName.trim() ) { | ||
newName = undefined; | ||
} | ||
|
||
updateBlockAttributes( [ clientId ], { | ||
metadata: { | ||
...metadata, | ||
name: newName, | ||
}, | ||
} ); | ||
}; | ||
|
||
return ( | ||
<BlockRenameModal | ||
blockName={ customName } | ||
originalBlockName={ blockInformation?.title } | ||
helpText={ | ||
hasPatternOverrides | ||
? __( | ||
'This block allows overrides. Changing the name can cause problems with content entered into instances of this pattern.' | ||
) | ||
: undefined | ||
} | ||
onClose={ closeModal } | ||
onSave={ onRename } | ||
/> | ||
); | ||
} | ||
|
||
// Split into a different component to minimize the store subscriptions. | ||
export default function RenameBlockModalControl() { | ||
const { renamingBlockClientId } = useSelect( | ||
( select ) => ( { | ||
renamingBlockClientId: unlock( | ||
select( patternsStore ) | ||
).getRenamingBlockClientId(), | ||
} ), | ||
[] | ||
); | ||
if ( ! renamingBlockClientId ) return null; | ||
|
||
return <BlockRenameModalWrapper clientId={ renamingBlockClientId } />; | ||
} |
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import './store'; | ||
export { store } from './store'; | ||
import { lock } from './lock-unlock'; | ||
|
||
export const privateApis = {}; | ||
lock( privateApis, { | ||
CreatePatternModal: () => null, | ||
PatternsMenuItems: () => null, | ||
RenameBlockModalControl: () => null, | ||
} ); |
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.
Since we're opening a dialog and we won't be able to access this element when the dialog is open, I don't see why we need this 🤔. I could be wrong though so could use a sanity check from @Mamaduka. 🙏
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.
You're right. The content behind the dialog become "inert" and browser should ignore them. Adding them to few menu items with modals was my mistake.