Repository navigation
Cover block: add media editor modal #79258
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import clsx from 'clsx'; | |
| */ | ||
| import { useEntityProp, store as coreStore } from '@wordpress/core-data'; | ||
| import { | ||
| useCallback, | ||
| useEffect, | ||
| useLayoutEffect, | ||
| useMemo, | ||
|
|
@@ -24,6 +25,7 @@ import { | |
| __experimentalUseGradient, | ||
| store as blockEditorStore, | ||
| useBlockEditingMode, | ||
| privateApis as blockEditorPrivateApis, | ||
| } from '@wordpress/block-editor'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { useSelect, useDispatch } from '@wordpress/data'; | ||
|
|
@@ -55,6 +57,9 @@ import { | |
| } from './color-utils'; | ||
| import { DEFAULT_MEDIA_SIZE_SLUG } from '../constants'; | ||
| import { getBackgroundEmbedHtml } from '../embed-video-utils'; | ||
| import { unlock } from '../../lock-unlock'; | ||
|
|
||
| const { openMediaEditorModalKey } = unlock( blockEditorPrivateApis ); | ||
|
|
||
| function getInnerBlocksTemplate( attributes ) { | ||
| return [ | ||
|
|
@@ -123,6 +128,11 @@ function CoverEdit( { | |
| postId | ||
| ); | ||
| const { getSettings } = useSelect( blockEditorStore ); | ||
| const openMediaEditorModal = useSelect( | ||
| ( select ) => | ||
| select( blockEditorStore ).getSettings()[ openMediaEditorModalKey ], | ||
| [] | ||
| ); | ||
|
|
||
| const { __unstableMarkNextChangeAsNotPersistent } = | ||
| useDispatch( blockEditorStore ); | ||
|
|
@@ -490,6 +500,7 @@ function CoverEdit( { | |
| ); | ||
|
|
||
| const mediaElement = useRef(); | ||
| const editMediaButtonRef = useRef(); | ||
| const currentSettings = { | ||
| isVideoBackground, | ||
| isImageBackground, | ||
|
|
@@ -500,6 +511,78 @@ function CoverEdit( { | |
| overlayColor, | ||
| }; | ||
|
|
||
| const openCoverMediaEditorModal = useCallback( () => { | ||
| if ( ! id || ! openMediaEditorModal ) { | ||
| return; | ||
| } | ||
|
|
||
| openMediaEditorModal( { | ||
| id, | ||
| onClose: () => { | ||
| editMediaButtonRef.current?.focus(); | ||
| }, | ||
| onUpdate: async ( { id: newId, url: newUrl } ) => { | ||
| if ( typeof newId !== 'number' ) { | ||
| return; | ||
| } | ||
|
|
||
| const nextAttributes = { | ||
| id: newId, | ||
| backgroundType: IMAGE_BACKGROUND_TYPE, | ||
| ...( newUrl ? { url: newUrl } : {} ), | ||
| ...( newId !== id | ||
| ? { sizeSlug: DEFAULT_MEDIA_SIZE_SLUG } | ||
| : {} ), | ||
| }; | ||
|
|
||
| if ( newUrl ) { | ||
|
Member
Author
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. I kept the change targeted to the new media editor update path rather than refactoring the existing Cover media-selection logic. I tried the latter first, but wasn't confident about it affecting existing flows. Maybe a follow up to unify, but it'd be a behavioral refactor so I'm not sure I'd want to make this PR more complicated. There's a fair bit going on, e.g., overlay color, isDark, isUserOverlayColor, undo persistence, and sometimes dim ratio so any shared helper could trigger unintended side effects.
Contributor
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. Keeping it contained to this flow sounds wise, then 👍 |
||
| const averageBackgroundColor = | ||
| await getMediaColor( newUrl ); | ||
|
|
||
| // Read latest values after await to avoid stale closures. | ||
| const { | ||
| attributes: currentAttrs, | ||
| overlayColor: currentOverlay, | ||
| } = propsRef.current; | ||
|
|
||
| let newOverlayColor = currentOverlay.color; | ||
| if ( ! currentAttrs.isUserOverlayColor ) { | ||
| newOverlayColor = averageBackgroundColor; | ||
| setOverlayColor( newOverlayColor ); | ||
|
|
||
| // Make undo revert the next setAttributes and the previous setOverlayColor. | ||
| __unstableMarkNextChangeAsNotPersistent(); | ||
| } | ||
|
|
||
| nextAttributes.isDark = compositeIsDark( | ||
| currentAttrs.dimRatio, | ||
| newOverlayColor, | ||
| averageBackgroundColor | ||
| ); | ||
| nextAttributes.isUserOverlayColor = | ||
| currentAttrs.isUserOverlayColor || false; | ||
| } | ||
|
|
||
| setAttributes( nextAttributes ); | ||
| }, | ||
| } ); | ||
| }, [ | ||
| id, | ||
| openMediaEditorModal, | ||
| setAttributes, | ||
| setOverlayColor, | ||
| __unstableMarkNextChangeAsNotPersistent, | ||
| ] ); | ||
|
|
||
| const showEditMediaButton = | ||
| hasNonContentControls && | ||
| ! useFeaturedImage && | ||
| isImageBackground && | ||
| !! id && | ||
| !! url && | ||
| ! isUploadingMedia && | ||
| !! openMediaEditorModal; | ||
|
|
||
| const toggleUseFeaturedImage = async () => { | ||
| const newUseFeaturedImage = ! useFeaturedImage; | ||
|
|
||
|
|
@@ -556,6 +639,9 @@ function CoverEdit( { | |
| toggleUseFeaturedImage={ toggleUseFeaturedImage } | ||
| onClearMedia={ onClearMedia } | ||
| blockEditingMode={ blockEditingMode } | ||
| onEditMedia={ openCoverMediaEditorModal } | ||
| editMediaButtonRef={ editMediaButtonRef } | ||
| showEditMediaButton={ showEditMediaButton } | ||
| /> | ||
| ); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.