Skip to content

Commit

Permalink
build: Support custom mobile Demo editor setup configuration (#54957)
Browse files Browse the repository at this point in the history
* build: Support custom mobile Demo editor setup configuration

Applying optional setup configuration files allows developers to modify
the Demo editor environment via Hooks, e.g. to set custom `initialHtml`
for the editor.

* test: Disable local Demo editor configuration for tests

The `require.context` function is undefined in the test environment, as
it is a Metro-specific capability. The most straightforward way to avoid
test failures is to disable this code that relates to the local
development workflow.

* docs: Document Demo editor customization

Contributors will likely never discover or benefit from this
customization ability if it is not documented.
  • Loading branch information
dcalhoun authored Oct 5, 2023
1 parent ae5761b commit fcb1079
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ test/storybook-playwright/specs/__snapshots__
test/storybook-playwright/specs/*-snapshots/**
test/gutenberg-test-themes/twentytwentyone
test/gutenberg-test-themes/twentytwentythree
packages/react-native-editor/src/setup-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,40 @@ npm run native ios -- -- --simulator="iPhone Xs Max"

To see a list of all of your available iOS devices, use `xcrun simctl list devices`.

### Customizing the Demo Editor

By default, the Demo editor renders most of the supported core blocks. This is helpful to showcase the editor's capabilities, but can be distracting when focusing on a specific block or feature. One can customize the editor's intial state by leveraging the `native.block_editor_props` hook in a `packages/react-native-editor/src/setup-local.js` file.

<details><summary>Example setup-local.js</summary>

```js
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';

export default () => {
addFilter(
'native.block_editor_props',
'core/react-native-editor',
( props ) => {
return {
...props,
initialHtml,
};
}
);
};

const initialHtml = `
<!-- wp:heading -->
<h2 class="wp-block-heading">Just a Heading</h2>
<!-- /wp:heading -->
`;
```

</details>

### Troubleshooting

If the Android emulator doesn't start correctly, or compiling fails with `Could not initialize class org.codehaus.groovy.runtime.InvokerHelper` or similar, it may help to double check the set up of your development environment against the latest requirements in [React Native's documentation](https://reactnative.dev/docs/environment-setup). With Android Studio, for example, you will need to configure the `ANDROID_HOME` environment variable and ensure that your version of JDK matches the latest requirements.
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-editor/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
inlineRequires: false,
},
} ),
unstable_allowRequireContext: true, // Used for optional setup configuration.
},
server: {
enhanceMiddleware: ( middleware ) => ( req, res, next ) => {
Expand Down
6 changes: 6 additions & 0 deletions packages/react-native-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ const registerGutenberg = ( {
// Initialize editor
this.editorComponent = setup();

// Apply optional setup configuration, enabling modification via hooks.
if ( typeof require.context === 'function' ) {
const req = require.context( './', false, /setup-local\.js$/ );
req.keys().forEach( ( key ) => req( key ).default() );
}

// Dispatch pre-render hooks.
doAction( 'native.pre-render', parentProps );

Expand Down

1 comment on commit fcb1079

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in fcb1079.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6422140937
📝 Reported issues:

Please sign in to comment.