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

Feat: InspectorFields component #1547

Merged
merged 8 commits into from
Aug 30, 2023
Merged

Feat: InspectorFields component #1547

merged 8 commits into from
Aug 30, 2023

Conversation

theodesp
Copy link
Member

@theodesp theodesp commented Aug 24, 2023

Tasks

  • I have signed a Contributor License Agreement (CLA) with WP Engine.
  • If a code change, I have written testing instructions that the whole team & outside contributors can understand.
  • I have written and included a comprehensive changeset to properly document the changes I've made.

Description

  • Implements automatic handling of InspectorFields controls as per RFC document.
  • Unit tests added

Related Issue(s):

#1522

Testing

  1. run npm install
  2. Build the project
    npm run build -w packages/block-editor-utils
  3. cd inside the project folder:
    cd packages/block-editor-utils
  4. run npm link
  5. On your WordPress instance create a new block plugin using @wordpress/create-block
    npx @wordpress/create-block hello-world-block
  6. Inside the plugin folder run npm link @faustwp/block-editor-utils to add a reference to the package.
  7. Rewrite the genenated index.js editor script to use the registerFaustBlock helper function instead adding the hook for testing the controls
// index.js
import { addFilter } from '@wordpress/hooks';
import "./style.scss";
// import block.json
import metadata from "./block.json";

// import Pure React Component
import HelloWorld from "./HelloWorld";

// Register React Component in Gutenberg
import { registerFaustBlock } from "@faustwp/block-editor-utils";

addFilter(
  'faustBlockEditorUtils.controls',
  'faust-block-editor-utils',
  (controls) => {
    controls.color = () => <div>Another Color</div>;
    return controls;
  },
);

registerFaustBlock(HelloWorld, { blockJson: metadata });

Here HelloWorld is a simple Div Component with the editorFields config that contains a color field

// HelloWorld.js
function HelloWorld({ style, className, attributes, children, ...props }) {
	const styles = {
		...style,
		backgroundColor: attributes.bg_color,
		color: attributes.text_color,
	};
	return (
		<div
			{...props}
			style={styles}
			className={className}
			dangerouslySetInnerHTML={{ __html: attributes.message }}
		/>
	);
}

HelloWorld.config = {
	name: "HelloWorld",
        editorFields: {
		message: {
			type: "string",
			default: "",
			label: "My Message",
			location: "editor",
			control: "textarea",
		},
		bg_color: {
			location: "inspector",
			control: "color",
		},
	},
};
export default HelloWorld;
// block.json
{
	"$schema": "https://schemas.wp.org/trunk/block.json",
	"apiVersion": 2,
	"name": "create-block/hello-world-block",
	"version": "0.1.0",
	"title": "Hello World Block",
	"category": "widgets",
	"icon": "smiley",
	"description": "Example block scaffolded with Create Block tool.",
	"supports": {
		"html": false
	},
	"attributes": {
		"message": {
			"type": "string",
			"default": "Hello World"
		},
		"bg_color": { "type": "string", "default": "#000000" }
	},
	"textdomain": "hello-world-block",
	"editorScript": "file:./index.js",
	"editorStyle": "file:./index.css",
	"style": "file:./style-index.css"
}
  1. Run npm start to build the compiled js

  2. Now go into the editor and try to create a new block. You should be able to see the Edit mode component. On the sidebar you can see the example color control component that we defined for testing purposes.

Screenshot 2023-08-30 at 16 55 01

Screenshots

Documentation Changes

Dependant PRs

@changeset-bot
Copy link

changeset-bot bot commented Aug 24, 2023

⚠️ No Changeset found

Latest commit: edf934a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Aug 24, 2023

📦 Next.js Bundle Analysis for @faustwp/getting-started-example

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌

@theodesp theodesp marked this pull request as ready for review August 28, 2023 16:13
@theodesp theodesp requested a review from a team as a code owner August 28, 2023 16:13
@blakewilson
Copy link
Contributor

Hey @theodesp,

I followed your testing instructions, but I'm not seeing the same thing. It looks like there are no new editor fields when I try and edit the block:

Screenshot 2023-08-29 at 9 13 29 AM Screenshot 2023-08-29 at 9 13 35 AM

@theodesp
Copy link
Member Author

theodesp commented Aug 30, 2023

Hey @theodesp,

I followed your testing instructions, but I'm not seeing the same thing. It looks like there are no new editor fields when I try and edit the block:

Screenshot 2023-08-29 at 9 13 29 AM Screenshot 2023-08-29 at 9 13 35 AM

Are there any console errors you can share? I tried the steps on a new plugin without issues.

@blakewilson
Copy link
Contributor

@theodesp Nope, no console errors that stand out 🤔

Copy link
Contributor

@matthewguywright matthewguywright left a comment

Choose a reason for hiding this comment

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

We ran through the testing steps with Theo during a meeting.

Copy link
Contributor

@TeresaGobble TeresaGobble left a comment

Choose a reason for hiding this comment

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

The panel body features "Another Color" as expected. Still seeing the semi-colon, not sure how to remove that except for maybe with snipping it in the file InspectorFields.tsx, line 34, and putting ats-ignore line above it.

image

Edit: Fixed by Theo during pairing!

@theodesp theodesp merged commit 0fb5e9e into canary Aug 30, 2023
13 checks passed
@theodesp theodesp deleted the MERL-1144 branch August 30, 2023 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants