Skip to content
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

[RNMobile] Add "Set as Featured Image" Button to Image Block (Android Only) #28854

Closed
wants to merge 72 commits into from
Closed
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
a4a52fe
Add "set as featured image" button to image block
SiobhyB Feb 8, 2021
4040201
Add "setFeaturedImage" function in bridge code
Feb 11, 2021
6bcfbd9
Add setFeaturedImage function to MainApplication
Feb 12, 2021
0cff0d7
Add "OnSetFeaturedImageListener" listener
Feb 12, 2021
dbec57d
Update edit.native.js with small syntax correction
Feb 12, 2021
d66bd01
Set up "OnSetFeaturedImageListener" in WPAndroidGlue.java
Feb 12, 2021
d1ed155
Passing featured image ID from native (Android) to Gutenberg
Mar 3, 2021
6a57885
Merge branch 'trunk' into update/featured-image-button-in-image-block…
SiobhyB Mar 3, 2021
444f3e3
Restore missing curly brace
Mar 3, 2021
2cbe570
Updates to featured-banner.native.js
Mar 5, 2021
073fa92
Attempts to select featured image using getEditedPostAttribute()
Mar 15, 2021
64359b1
Update featured-banner.native.js
Mar 15, 2021
48f8ea1
Add featured banner to image/index.native.js
Mar 15, 2021
f114ade
Update featured image banner and button styles
Mar 15, 2021
46ba7e8
Create getFeaturedImageId function and cleanup
Mar 16, 2021
b8f05f4
Tidy up edit.native.js
Mar 16, 2021
c7d00f3
Update AndroidGlueCode
Mar 18, 2021
84a8850
retrigger checks
Mar 22, 2021
eb78dc4
Update setup.js
Mar 22, 2021
bed517e
retrigger checks
Mar 22, 2021
64b1c8b
Update function name from "featuredImageIdChange" to "featuredImageId…
Mar 23, 2021
d8c0824
Rename "featuredImageIdNotifier" to "onRequestFeaturedImageId"
Mar 23, 2021
32f134d
Create OnSetFeaturedImageListener
Mar 23, 2021
75de59f
Dismiss bottomsheet when "setFeaturedImage" is called
Mar 23, 2021
67369d3
Introduce "onRemoveFeatured" button and tidying up names
Mar 29, 2021
c9aba0a
Updates to function names
Mar 29, 2021
a9d051f
Convert FeaturedBanner to Badge component
Mar 30, 2021
a92525b
Update edit.native.js to correct merge conflict
Mar 30, 2021
54a5f14
Correct merge conflict with GutenbergBridgeJS2Parent.java
Mar 30, 2021
e99999c
retrigger checks
Mar 30, 2021
be31d63
retrigger checks
Mar 30, 2021
9795874
retrigger checks
Mar 30, 2021
2284524
retrigger checks
Mar 30, 2021
2736f33
Update edit.native.js to fix merge conflict
Mar 31, 2021
e9ebc69
Update edit.native.js to check if an image is featured uppn changes
Mar 31, 2021
2eb1979
Update styles.native.scss to add border to the top of featured button
Apr 1, 2021
a33b8b9
Update edit.native.js
Apr 1, 2021
0cdeadb
Introduce "featuredMedia" prop for use when editor loads
Apr 6, 2021
737627d
Fetch ID of post's initial featured image
Apr 7, 2021
9e524ea
Merge branch 'trunk' into update/featured-image-button-in-image-block…
SiobhyB Apr 8, 2021
6d53341
Updated featured state when image is replaced within block
Apr 8, 2021
cc00dc2
Remove checkIfFeaturedImage function
Apr 8, 2021
4dc6cef
Fix merge conflict
Apr 8, 2021
b335086
Fix merge conflict
Apr 8, 2021
3cbe30a
Add "setFeaturedImage" and "featuredImageIdCurrent" functions to Swif…
Apr 10, 2021
39303e4
Simplify this.state.isFeaturedImage by converting to a boolean inside…
Apr 10, 2021
0c69a62
Comment out iOS-specific changes to the bridge
Apr 12, 2021
cc4aa8d
Add iOS functions to bridge
Apr 12, 2021
d71d067
Flag featured image when editor mounts and comment out iOS methods fr…
Apr 12, 2021
28f19af
Comment out a bridge function for iOS (missing from previous commit).
Apr 12, 2021
262291f
Add set featured image functionality behind dev flag
Apr 13, 2021
a17e6e9
Move "set as featured" button behind devOnly and androidOnly flag
Apr 13, 2021
1c4b2ca
Calling "__DEV__" directly instead of defining a separate variable
Apr 13, 2021
ba7c283
Update changelog.md
Apr 13, 2021
206fede
Merge branch 'trunk' into update/featured-image-button-in-image-block…
SiobhyB Apr 13, 2021
1f844b6
Merge branch 'trunk' into update/featured-image-button-in-image-block…
SiobhyB Apr 20, 2021
9761c04
Merge branch 'trunk' into update/featured-image-button-in-image-block…
SiobhyB Apr 20, 2021
85844e4
Remove "subscribeFeaturedImageIdCurrent" functionality
Apr 20, 2021
1f61eb8
Remove featuredImageId check in componentDidMount()
Apr 20, 2021
35fa071
Remove isFeaturedImage functionality from image/index.native.js
Apr 20, 2021
590040b
Remove iOS functions from bridge
Apr 20, 2021
619f4f7
Remove android.util.Log import
Apr 20, 2021
dd6a63f
Remove redundant space
Apr 20, 2021
35f7023
Update CHANGELOG.md
Apr 20, 2021
2ed72bf
Tweak androidOnly const
Apr 20, 2021
c74e737
Merge branch 'trunk' into update/featured-image-button-in-image-block…
Apr 23, 2021
68ef75e
Refactor 'set as featured' button into its own function
Apr 27, 2021
fbe83c2
Merge branch 'update/featured-image-button-in-image-block-settings' o…
Apr 27, 2021
e3ba262
Merge branch 'trunk' into update/featured-image-button-in-image-block…
SiobhyB Apr 27, 2021
9caa86a
Fix syntax issue with onSetFeatured function
Apr 27, 2021
fe2c108
Merge remote-tracking branch 'upstream/trunk' into update/featured-im…
Apr 27, 2021
89a8c6c
Merge branch 'WordPress:trunk' into update/featured-image-button-in-i…
SiobhyB May 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion packages/block-library/src/image/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
requestImageFailedRetryDialog,
requestImageUploadCancelDialog,
requestImageFullscreenPreview,
setFeaturedImage,
getFeaturedImageId,
subscribeFeaturedImageIdChange,
} from '@wordpress/react-native-bridge';
import {
CycleSelectControl,
Expand All @@ -25,6 +28,7 @@ import {
Image,
WIDE_ALIGNMENTS,
LinkSettingsNavigation,
BottomSheet,
} from '@wordpress/components';
import {
BlockCaption,
Expand Down Expand Up @@ -69,6 +73,7 @@ export class ImageEdit extends Component {

this.state = {
isCaptionSelected: false,
isFeaturedImage: false,
};

this.finishMediaUploadWithSuccess = this.finishMediaUploadWithSuccess.bind(
Expand All @@ -82,12 +87,15 @@ export class ImageEdit extends Component {
this
);
this.updateMediaProgress = this.updateMediaProgress.bind( this );
this.featuredImageIdChange = this.featuredImageIdChange.bind( this );
this.updateAlt = this.updateAlt.bind( this );
this.updateImageURL = this.updateImageURL.bind( this );
this.onSetLinkDestination = this.onSetLinkDestination.bind( this );
this.onSetNewTab = this.onSetNewTab.bind( this );
this.onSetSizeSlug = this.onSetSizeSlug.bind( this );
this.onImagePressed = this.onImagePressed.bind( this );
this.onSetFeatured = this.onSetFeatured.bind( this );
this.onGetFeatured = this.onGetFeatured.bind( this );
this.onFocusCaption = this.onFocusCaption.bind( this );
this.updateAlignment = this.updateAlignment.bind( this );
this.accessibilityLabelCreator = this.accessibilityLabelCreator.bind(
Expand Down Expand Up @@ -150,6 +158,12 @@ export class ImageEdit extends Component {
) {
mediaUploadSync();
}

if ( attributes.id ) {
this.onGetFeatured();
}

this.addFeaturedImageIdListener();
}

componentWillUnmount() {
Expand All @@ -163,13 +177,52 @@ export class ImageEdit extends Component {
this.props.attributes.id
);
}

this.removeFeaturedImageIdListener();
}

componentDidUpdate( previousProps ) {
if ( ! previousProps.image && this.props.image ) {
const { image, attributes } = this.props;
const url = getUrlForSlug( image, attributes ) || image.source_url;
this.props.setAttributes( { url } );
if ( attributes.id ) {
this.onGetFeatured();
}
}
}

featuredImageIdChange( payload ) {
const { attributes } = this.props;

const featuredImageId = payload.featuredImageId;

if ( featuredImageId === attributes.id ) {
this.setState( {
isFeaturedImage: true,
} );
} else {
this.setState( {
isFeaturedImage: false,
} );
}
}

addFeaturedImageIdListener() {
//if we already have a subscription not worth doing it again
if ( this.subscriptionParentFeaturedImageIdChange ) {
return;
}
this.subscriptionParentFeaturedImageIdChange = subscribeFeaturedImageIdChange(
( payload ) => {
this.featuredImageIdChange( payload );
}
);
}

removeFeaturedImageIdListener() {
if ( this.subscriptionParentFeaturedImageIdChange ) {
this.subscriptionParentFeaturedImageIdChange.remove();
}
}

Expand Down Expand Up @@ -270,6 +323,16 @@ export class ImageEdit extends Component {
} );
}

onSetFeatured() {
const { attributes } = this.props;
setFeaturedImage( attributes.id );
}

onGetFeatured() {
const { attributes } = this.props;
getFeaturedImageId( attributes.id );
}

onSetLinkDestination( href ) {
this.props.setAttributes( {
linkDestination: LINK_DESTINATION_CUSTOM,
Expand Down Expand Up @@ -398,7 +461,7 @@ export class ImageEdit extends Component {
}

render() {
const { isCaptionSelected } = this.state;
const { isCaptionSelected, isFeaturedImage } = this.state;
const { attributes, isSelected, image, clientId } = this.props;
const { align, url, alt, id, sizeSlug, className } = attributes;

Expand Down Expand Up @@ -450,6 +513,15 @@ export class ImageEdit extends Component {
<PanelBody title={ __( 'Link Settings' ) }>
{ this.getLinkSettings( true ) }
</PanelBody>
<PanelBody>
{ ! isFeaturedImage && (
<BottomSheet.Cell
label={ __( 'Set as Featured Image ' ) }
labelStyle={ styles.setFeaturedButton }
onPress={ this.onSetFeatured }
/>
) }
</PanelBody>
</InspectorControls>
);

Expand Down Expand Up @@ -487,6 +559,7 @@ export class ImageEdit extends Component {
{ isSelected && getMediaOptions() }
{ ! this.state.isCaptionSelected &&
getToolbarEditButton( openMediaOptions ) }
{ this.state.isFeaturedImage }
<MediaUploadProgress
coverUrl={ url }
mediaId={ id }
Expand All @@ -512,6 +585,7 @@ export class ImageEdit extends Component {
isSelected={
isSelected && ! isCaptionSelected
}
isFeaturedImage={ isFeaturedImage }
isUploadFailed={ isUploadFailed }
isUploadInProgress={
isUploadInProgress
Expand Down
9 changes: 9 additions & 0 deletions packages/block-library/src/image/styles.native.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@
padding-right: 0;
padding-bottom: $grid-unit;
}

.setFeaturedButton {
text-align: center;
color: $blue-50;
}

.setFeaturedButtonDark {
color: $blue-30;
}
27 changes: 27 additions & 0 deletions packages/components/src/mobile/image/featured-banner.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* External dependencies
*/
import { View, Text } from 'react-native';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
// import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import styles from './style.scss';

export class FeaturedBanner extends Component {
render() {
return (
<View style={ styles.featuredBannerContainer }>
<Text style={ styles.featuredBanner }>Featured</Text>
</View>
);
}
}

export default FeaturedBanner;
4 changes: 4 additions & 0 deletions packages/components/src/mobile/image/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getImageWithFocalPointStyles } from './utils';
import styles from './style.scss';
import SvgIconRetry from './icon-retry';
import ImageEditingButton from './image-editing-button';
import FeaturedBanner from './featured-banner';

const ICON_TYPE = {
PLACEHOLDER: 'placeholder',
Expand All @@ -36,6 +37,7 @@ const ImageComponent = ( {
height: imageHeight,
highlightSelected = true,
isSelected,
isFeaturedImage,
isUploadFailed,
isUploadInProgress,
mediaPickerOptions,
Expand Down Expand Up @@ -241,6 +243,8 @@ const ImageComponent = ( {
) }
</View>

{ isFeaturedImage && <FeaturedBanner /> }

{ editButton && isSelected && ! isUploadInProgress && (
<ImageEditingButton
onSelectMediaUploadOption={ onSelectMediaUploadOption }
Expand Down
17 changes: 17 additions & 0 deletions packages/components/src/mobile/image/style.native.scss
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@
right: 5px;
}

.featuredBannerContainer {
padding: 30px;
position: absolute;
top: 0;
left: 0;
z-index: 2;
}

.featuredBanner {
position: absolute;
padding: 10px;
top: 5px;
left: 5px;
color: #fff;
background-color: $gray-dark;
}

.iconCustomise {
fill: #fff;
position: absolute;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ interface MediaSaveEventEmitter {
void onReplaceMediaFilesEditedBlock(final String mediaFiles, final String blockId);
}

interface FeaturedImageEmitter {
void featuredImageIdNotifier(int mediaId);
}

interface ReplaceUnsupportedBlockCallback {
void replaceUnsupportedBlock(String content, String blockId);
}
Expand Down Expand Up @@ -141,6 +145,10 @@ public static GutenbergUserEvent getEnum(String eventName) {

void requestImageUploadCancel(int mediaId);

void setFeaturedImage(int mediaId);

void getFeaturedImageId(int mediaId);

void editorDidEmitLog(String message, LogLevel logLevel);

void editorDidAutosave();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public class RNReactNativeGutenbergBridgeModule extends ReactContextBaseJavaModu
private static final String MAP_KEY_REPLACE_BLOCK_HTML = "html";
private static final String MAP_KEY_REPLACE_BLOCK_BLOCK_ID = "clientId";

public static final String MAP_KEY_FEATURED_IMAGE_ID = "featuredImageId";

private boolean mIsDarkMode;

public RNReactNativeGutenbergBridgeModule(ReactApplicationContext reactContext,
Expand Down Expand Up @@ -226,6 +228,16 @@ public void requestImageUploadCancel(final int mediaId) {
mGutenbergBridgeJS2Parent.requestImageUploadCancel(mediaId);
}

@ReactMethod
public void setFeaturedImage(final int mediaId) {
mGutenbergBridgeJS2Parent.setFeaturedImage(mediaId);
}

@ReactMethod
public void getFeaturedImageId(final int mediaId) {
mGutenbergBridgeJS2Parent.getFeaturedImageId(mediaId);
}

@ReactMethod
public void requestImageFullscreenPreview(String mediaUrl) {
mGutenbergBridgeJS2Parent.requestImageFullscreenPreview(mediaUrl);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.wordpress.mobile.WPAndroidGlue;

import android.util.Log;
import android.util.Pair;

import androidx.annotation.Nullable;
Expand All @@ -10,6 +11,7 @@

import org.wordpress.mobile.ReactNativeGutenbergBridge.GutenbergBridgeJS2Parent.MediaUploadEventEmitter;
import org.wordpress.mobile.ReactNativeGutenbergBridge.GutenbergBridgeJS2Parent.MediaSaveEventEmitter;
import org.wordpress.mobile.ReactNativeGutenbergBridge.GutenbergBridgeJS2Parent.FeaturedImageEmitter;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
Expand All @@ -18,8 +20,9 @@
import static org.wordpress.mobile.ReactNativeGutenbergBridge.RNReactNativeGutenbergBridgeModule.MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_NEW_ID;
import static org.wordpress.mobile.ReactNativeGutenbergBridge.RNReactNativeGutenbergBridgeModule.MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_URL;
import static org.wordpress.mobile.ReactNativeGutenbergBridge.RNReactNativeGutenbergBridgeModule.MAP_KEY_MEDIA_FINAL_SAVE_RESULT_SUCCESS_VALUE;
import static org.wordpress.mobile.ReactNativeGutenbergBridge.RNReactNativeGutenbergBridgeModule.MAP_KEY_FEATURED_IMAGE_ID;

public class DeferredEventEmitter implements MediaUploadEventEmitter, MediaSaveEventEmitter {
public class DeferredEventEmitter implements MediaUploadEventEmitter, MediaSaveEventEmitter, FeaturedImageEmitter {
public interface JSEventEmitter {
void emitToJS(String eventName, @Nullable WritableMap data);
}
Expand All @@ -40,6 +43,8 @@ public interface JSEventEmitter {
private static final String EVENT_NAME_MEDIA_SAVE = "mediaSave";
private static final String EVENT_NAME_MEDIA_REPLACE_BLOCK = "replaceBlock";

private static final String EVENT_FEATURED_IMAGE_ID = "featuredImageIdChange";
SiobhyB marked this conversation as resolved.
Show resolved Hide resolved

private static final String MAP_KEY_MEDIA_FILE_STATE = "state";
private static final String MAP_KEY_MEDIA_FILE_MEDIA_ACTION_PROGRESS = "progress";
private static final String MAP_KEY_MEDIA_FILE_MEDIA_SERVER_ID = "mediaServerId";
Expand Down Expand Up @@ -205,6 +210,12 @@ public void onMediaCollectionSaveResult(String firstMediaIdInCollection, boolean
}
}

public void featuredImageIdNotifier(int mediaId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to my naming comment above, what do you think about renaming to featuredImageIdChanged() or better:onFeaturedImageIdChange to make it read like an event handler? The original featuredImageIdNotifier makes me wonder what gets notified... like, is it the user that receives a notification when this function is called?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading more code where this function is used, I see that it's also called when the JS side just asks for an update of the featured image id. So, the function's purpose is not only to notify the JS side that the id has changed, which means that the event name (defined here) is not actually accurate 🤔.

So, if I understand things properly, the event is all in all just trying to pass the the media id of the current featured image, right, whether it was recently updated or not? If so, what do you think about naming the event with EVENT_FEATURED_IMAGE_ID_CURRENT = "featuredImageIdCurrent"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original featuredImageIdNotifier makes me wonder what gets notified... like, is it the user that receives a notification when this function is called?

This is useful feedback, thank you @hypest! I looked through other similar functions and renamed featuredImageIdNotifier to onRequestFeaturedImageId in order to hopefully be clearer of the function's purpose. Let me know if you think this is still unclear!

So, if I understand things properly, the event is all in all just trying to pass the the media id of the current featured image, right, whether it was recently updated or not? If so, what do you think about naming the event with EVENT_FEATURED_IMAGE_ID_CURRENT = "featuredImageIdCurrent"?

That makes sense! The purpose changed as the code evolved (it was originally only passing the id when the image changed) which is why I think the naming became a bit messy/confusing. This is a good lesson to keep on top and iterating on names. Thank you! I've updated this to "featuredImageIdCurrent" now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked through other similar functions and renamed featuredImageIdNotifier to onRequestFeaturedImageId in order to hopefully be clearer of the function's purpose. Let me know if you think this is still unclear!

Aha.

Still trying to grok what the code is trying to do, please bear with me :). There are two rather different code paths that use this function:

  1. The JS side asks the native side to provide the featured image id (getter named function here). The native side then calls onRequestFeaturedImageId only if the media id provided matches the featured image id on file. By the way, looks like it's not a getter after all since the response to the original getFeaturedImageId is conditional. Looks to me more like a "check for something", not a plain getter. If so, would it make sense to rename getFeaturedImageId -> checkIfFeaturedImage?
  2. The native side sends the id when the featured image is set.

From flow No2 above, and the function implementation itself (native side,

featuredImageIdCurrent( payload ) {
const { attributes } = this.props;
const featuredImageId = payload.featuredImageId;
if ( featuredImageId === attributes.id ) {
this.setState( {
isFeaturedImage: true,
} );
} else {
this.setState( {
isFeaturedImage: false,
} );
}
}
) I understand that onRequestFeaturedImageId just sends the current featured image id to the JS side so, may I propose renaming it to sendToJSFeaturedImageId?

Then, flow No1 shows to me we need to better name and document why onGetFeaturedImageId doesn't always call sendToJSFeaturedImageId.
Does this all make sense? Sorry if I'm a bit confused on this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hypest, thank you again for your help here!

By the way, looks like it's not a getter after all since the response to the original getFeaturedImageId is conditional. Looks to me more like a "check for something", not a plain getter. If so, would it make sense to rename getFeaturedImageId -> checkIfFeaturedImage?

Then, flow No1 shows to me we need to better name and document why onGetFeaturedImageId doesn't always call sendToJSFeaturedImageId.

It's right that the getFeaturedImageId function is only checking whether an image is featured, I've changed its name to checkIfFeaturedImage for the time being.

To further clarify: The checkIfFeaturedImage function is called within componentWillUnmount() on the JS side. It's currently responsible for flagging which image is featured when the editor is first loaded.

The background for this is that I was unable to get getEditedPostAttribute to work to get the post's initial featured image (discussed on project thread and in project's Slack channel).

I've spent a bit of time with this but am not sure if there's a more elegant way to achieve the same effect, but it's entirely likely I've been looking at the code too long! I'd be interested to hear whether you think this approach makes sense, with those extra details, or if I should work on this a bit more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been able to get this working to grab the post's initial featured image ID now and have made use of setAttributes to get this working in ComponentDidUpdate here. How does this seem? Looking forward to any feedback or ideas on this one. :)

Copy link
Contributor

@hypest hypest Apr 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this seem?

Yeah, I think we're on a good route here. We chatted a bit over Zoom, to share my thoughts on how the ImageEdit component just needs to listen to the store updates and compare the featured image id it gets from there with the id it keeps in the block attributes, and use those to compute a "isFeaturedImage" boolean inside the render() function.

Then, it becomes a matter of figuring out how to also do the update of the id in the store, for which we can use the editEntityRecord() action creator (acquiring it via the withDispach. But, that part (to update the featured image id value when the image changes) can be done in a separate PR I think.

Let me know what you think!

Copy link
Contributor Author

@SiobhyB SiobhyB Apr 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you again for this! I've spent a bit of time today trying to process this and have a few questions:

  • What are the overall benefits to moving the setFeaturedImageState() functionality inside the render() function vs. the current approach?
  • setFeaturedImageState() is currently called in componentDidMount() for when the editor first loads and then componentDidUpdate() for when an image is replaced directly within the image block (using the Edit icon to the upper right when the block is selected). Will moving the functionality for setting state from setFeaturedImageState() to render() resolve the need to do that? Is that one of the benefits?
  • It seems like I'll need to figure out how to use editEntityRecord() to grab the post's current featured image ID before I can move the functionality for setting state from setFeaturedImageState(). Is that right?

Sorry for my fuzziness on this one, just want to make sure I'm clear before moving ahead!

Copy link
Contributor Author

@SiobhyB SiobhyB Apr 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems all I had to do was ask those questions and then play around a little more to figure out the answers. :) I've been able to convert the functionality previously included in setFeaturedImageState to an isFeaturedImage boolean. I can see now that the answer to my second question is "yes" and the code is a lot cleaner as a result. 🎉 I've committed those changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to see the simplification of isFeaturedImage!

setting an isFeaturedImage state

Just a quick comment on the terminology used in the commit message: now that isFeaturedImage is just a helper local const in render(), referring to it as "state" can be misleading, mainly because within React, "state" has a special and specific meaning. I'd recommend referring to it as "local variable/const" to avoid confusion with React's use.

WritableMap writableMap = new WritableNativeMap();
writableMap.putInt(MAP_KEY_FEATURED_IMAGE_ID, mediaId);
queueActionToJS(EVENT_FEATURED_IMAGE_ID, writableMap);
}

@Override public void onReplaceMediaFilesEditedBlock(String mediaFiles, String blockId) {
WritableMap writableMap = new WritableNativeMap();
writableMap.putString(MAP_KEY_REPLACE_BLOCK_HTML, mediaFiles);
Expand Down
Loading