-
Notifications
You must be signed in to change notification settings - Fork 52
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 generate images in the Media Inserter #535
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
28ff330
Initial work done on testing out the new registerInserterMediaCategor…
dkotter 695a69e
Merge branch 'develop' into feature/inserter-media-category
dkotter d6d05ed
Remove code that is no longer needed. Refactor our JS a bit to only l…
dkotter fd6f437
Remove hardcoded API response
dkotter 30d14bd
Update script name
dkotter 62299ca
Fix lint issue
dkotter d220238
Modify our inserter code a bit to use apiFetch
dkotter ff29908
Refactor how we register the media category by copying what Jetpack d…
dkotter 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Some code here was copied from Jetpack's implementation of the inserter media category. | ||
* See https://github.com/Automattic/jetpack/pull/31914 | ||
*/ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import { dispatch, select, subscribe } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
|
||
const { classifaiDalleData } = window; | ||
|
||
const isInserterOpened = () => | ||
select( 'core/edit-post' )?.isInserterOpened() || | ||
select( 'core/edit-site' )?.isInserterOpened() || | ||
select( 'core/edit-widgets' )?.isInserterOpened?.(); | ||
|
||
const waitFor = async ( selector ) => | ||
new Promise( ( resolve ) => { | ||
const unsubscribe = subscribe( () => { | ||
if ( selector() ) { | ||
unsubscribe(); | ||
resolve(); | ||
} | ||
} ); | ||
} ); | ||
|
||
waitFor( isInserterOpened ).then( () => | ||
dispatch( 'core/block-editor' )?.registerInserterMediaCategory?.( | ||
registerGenerateImageMediaCategory() | ||
) | ||
); | ||
|
||
const registerGenerateImageMediaCategory = () => ( { | ||
name: 'classifai-generate-image', | ||
labels: { | ||
name: classifaiDalleData.tabText, | ||
search_items: __( 'Enter a prompt', 'classifai' ), | ||
}, | ||
mediaType: 'image', | ||
fetch: async ( { search = '' } ) => { | ||
if ( ! search ) { | ||
return []; | ||
} | ||
|
||
const images = await apiFetch( { | ||
path: addQueryArgs( classifaiDalleData.endpoint, { | ||
prompt: search, | ||
format: 'b64_json', | ||
} ), | ||
method: 'GET', | ||
} ) | ||
.then( ( response ) => | ||
response.map( ( item ) => ( { | ||
title: search, | ||
url: `data:image/png;base64,${ item.url }`, | ||
previewUrl: `data:image/png;base64,${ item.url }`, | ||
id: undefined, | ||
alt: search, | ||
caption: classifaiDalleData.caption, | ||
} ) ) | ||
) | ||
.catch( () => [] ); | ||
|
||
return images; | ||
}, | ||
isExternalResource: true, | ||
} ); |
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.
I think this needs to be debounced a little, it looks like it makes multiple requests while typing in the prompt.
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.
I mentioned that in the PR description that if someone doesn't type super fast, it will make multiple requests. I'm struggling with figuring out how to add debouncing here, since debouncing already happens on the WordPress side (see https://github.com/WordPress/gutenberg/blob/trunk/packages/block-editor/src/components/inserter/hooks/use-debounced-input.js). Everything I've tried to add another layer of debouncing isn't working. Either I'm not sure how to make this work (very likely) or this may not be possible since the only thing we have control over is the actual
fetch
function, not when that function gets called.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.
As an example, if I add debounce around our fetch function:
and leave everything else the same, it does properly debounce the requests but it does not properly render the results. It seems to always be one request behind. So if I enter an image prompt and wait for a second or two (for the debounce to kick in), I do see my request being made but no results are rendered. If I then change the image prompt, the previous results render immediately and then once the wait period is up, a new request is made (but the results from that aren't rendered unless I make changes to the prompt again).
Again, this may very well be something I'm doing wrong but could also be some limitations around how Core is doing the rendering here.
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.
Sorry Darin, I missed this in the description.
As the feature didn't make it in to WP 6.3, is it worth adding a note that it requires the Gutenberg plugin/WP 6.4 or later to the changelog entry?
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.
I'm surprised this didn't make it into WP 6.3 as this changed was added to Gutenberg in min-June. I think this is probably worth holding off on until this comes to WordPress itself, at least that's my opinion.
I also think it's worth discussing a bit more around how requests are made here. Since image generation requests are expensive, it's not great that you'll almost certainly end up making multiple requests as you enter an image prompt. I'm wondering if it's worth opening an issue on the Gutenberg side to see if we can get this feature modified a bit? I would love to be able to toggle between search happening as you type (the way it works now) or the ability to only search if someone clicks a submit button (which would solve our needs). Or if we can filter the default debounce value to make that higher, that also would help.
Not sure if either of those are changes they'll deem worth adding but may be a good conversation to start.