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

Add VBE editor injector #86123

Merged
merged 22 commits into from
Jan 15, 2024
Merged

Add VBE editor injector #86123

merged 22 commits into from
Jan 15, 2024

Conversation

alshakero
Copy link
Member

@alshakero alshakero commented Jan 8, 2024

Changes

This PR aims to create a standalone bundle that attaches the Gutenberg editor to a textarea and makes it support blocks. By standalone, I mean a library that exposes global and doesn't have any dependencies.

Testing steps

Regression testing

  1. Make sure the editor works in Calypso by heading to /comment and embedding a block or two. If one embed works, all will work, don't sweat it.

Testing in Verbum

  1. Navigate to packages/verbum-block-editor.
  2. Run yarn dev --sync.
  3. In Verbum https://github.com/Automattic/verbum/pull/128, checkout use/packaged-editor and run npm run start:sync.
  4. Sandbox widgets.wp.com.
  5. Sandbox a site.
  6. Visit a blogpost page on that site.
  7. The editor should and should work.
  8. To verify it's the new editor, verify that embeds don't work.
Old description

## Proposed Changes

This PR aims to create a standalone bundle that attaches the Gutenberg editor to a textarea and makes it support blocks. By standalone, I mean a library that exposes global and doesn't have any dependencies.

The goal is to be able to publish it to NPM, then import it in here and use it.

Issue

For some reason, the editor is not being rendered correctly. It's a bit hard to describe the issue, but if you follow the steps, and take a quick look at the code, you'll see it right away!

Reproduction steps

  1. please checkout the branch of this PR.
  2. cd into packages/verbum-block-editor
  3. run yarn build.
  4. In the dist file, add the following HTML file.
  5. Serve the dist folder using serve or whatever you like (npm i serve --global && serve).
  6. Visit the served page, and in the console, run verbumBlockEditor.attachGutenberg(editor, console.log)
  7. The rendering won’t work and you’ll get a white page.
  8. Expected result is that you’d get a Gutenberg editor to replace the textarea.

I suspect it's a JSX issue.

Related thread: p1704811519896619-slack-C7YPUHBB2

<!doctype html>
<html lang="en">
	<head>
		<meta name="description" content="Webpage description goes here" />
		<meta charset="utf-8" />
		<title>Change_me</title>
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<meta name="author" content="" />

		<link rel="stylesheet" href="main.css" />
	</head>

	<body>
		<div class="container">
			<textarea style="width: 500px; height: 300px" id="editor"></textarea>
		</div>

		<script type="module" src="./main.min.js"></script>
	</body>
</html>

Copy link

github-actions bot commented Jan 8, 2024

@matticbot
Copy link
Contributor

matticbot commented Jan 8, 2024

Here is how your PR affects size of JS and CSS bundles shipped to the user's browser:

Async-loaded Components (~66 bytes added 📈 [gzipped])

name                             parsed_size           gzip_size
async-load-comment-block-editor       +206 B  (+0.0%)      +66 B  (+0.0%)

React components that are loaded lazily, when a certain part of UI is displayed for the first time.

Legend

What is parsed and gzip size?

Parsed Size: Uncompressed size of the JS and CSS files. This much code needs to be parsed and stored in memory.
Gzip Size: Compressed size of the JS and CSS files. This much data needs to be downloaded over network.

Generated by performance advisor bot at iscalypsofastyet.com.

@alshakero alshakero requested a review from a team January 9, 2024 17:54
@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Jan 9, 2024
@alshakero
Copy link
Member Author

@Automattic/team-calypso would really appreciate your help 🙏🏼

Copy link
Member

@tyxla tyxla left a comment

Choose a reason for hiding this comment

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

Haven't dug into this too deep, but at first glance this looks like a good candidate to be implemented as a Calypso app. I'd also advise you to borrow the build config from one of the working apps (they use @automattic/calypso-apps-builder AFAIK), since they're all independently loaded outside of Calypso as standalone apps. This will likely solve whatever build issue you've been having.

@alshakero
Copy link
Member Author

I'll check that out! Maybe we don't even need to publish on npm.

@jsnajdr
Copy link
Member

jsnajdr commented Jan 10, 2024

I think JSX is OK here, I suspect something with the Gutenberg components (BlockTools, BlockCanvas, BlockEditorProvider, SlotFillProvider, ...) are not put together correctly -- wrong nesting, some props are missing etc.

Is there a situation where exactly the same code, built and bundled differently, works as expected? I'd like to compare the working and non-working versions side by side.

@alshakero
Copy link
Member Author

You're kinda right. I was sure it's a build issue because it's working perfectly in https://wordpress.com/comment. To see it

  1. Go to a blogpost page with GB flag on, like https://jardasn.blog/2023/01/11/prefer-jest-real-timers-when-testing-with-react-testing-library/?verbum_gutenberg=1.
  2. Comment something.
  3. Head to https://wordpress.com/comment.
  4. You'll see a working editor that uses this exact code.

But we now realized that removing the children of BlockCanvas fixed some of the issue. I can't make sense of these two pieces of information.

@alshakero
Copy link
Member Author

Removing renderAppender prop from BlockList fixes the issue 🌵

@jsnajdr
Copy link
Member

jsnajdr commented Jan 10, 2024

The Verbum package in this PR is build with @wordpress/block-editor version 12.6.0. But the version on WP.com is built from the Automattic/verbum repo, where the lockfile specifies 12.5.0 for this package.

There were some framework changes between these two versions (12.5.0 is from Nov 29, 12.6.0 from Dec 13), for example #56996 by @youknowriad that integrates <BlockTools> into <BlockCanvas>. That leads to BlockTools being rendered twice. But that doesn't break the editor yet.

For some reason, it's the <BlockList renderAppender={ false } /> that causes trouble, although I don't know yet why. But I think it's the version mismatch and the changes that are happening between the versions that are to blame here.

@jsnajdr
Copy link
Member

jsnajdr commented Jan 10, 2024

Regarding renderAppender: it seems that here the editor just does exactly what it's told to do :) When rendering <BlockList renderAppender={ false } />, no appender will be rendered, and because there are no blocks, no blocks will be rendered either. You'll see a white screen. Only when some block (or appender) is added and selected, the content and its UI will appear.

The situation with the Automattic/verbum version is a bit different: if the initial content is empty, it will default it to [createBlock('core/paragraph')]. See the useStateWithHistory call at the top level Editor component and notice how the two versions of the editor are different in this aspect! This default block will get selected and will make the UI appear. That's the entire difference I believe.

@alshakero alshakero marked this pull request as ready for review January 10, 2024 15:34
@alshakero alshakero requested a review from a team as a code owner January 10, 2024 15:34
@renancarvalho
Copy link
Contributor

renancarvalho commented Jan 10, 2024

While testing this, I noticed that some styles are broken, but the stylesheet itself looks ok. I also noticed that BlockTools is being rendered twice, I believe this is causing some CSS issues.

image

The content being rendered from BlockToolbar is also a bit different from what we had in Verbum initially, it gets more CSS classes I believe by default and might also be breaking styling.

There is also a version difference between Verbum and Calypso, I will try to downgrade Calypso and see if it works better

@@ -0,0 +1 @@
.cache
Copy link
Member

Choose a reason for hiding this comment

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

Let's add a /packages/*/.cache record to the top-level .gitignore instead. There's already an identical one for apps/... there.

Copy link
Contributor

Choose a reason for hiding this comment

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

Applied the suggestion 👍

@matticbot
Copy link
Contributor

This PR modifies the release build for the following Calypso Apps:

For info about this notification, see here: PCYsg-OT6-p2

  • wpcom-block-editor

To test WordPress.com changes, run install-plugin.sh $pluginSlug vbe/add-injector on your sandbox.

@alshakero alshakero merged commit 7e1da2b into trunk Jan 15, 2024
11 checks passed
@alshakero alshakero deleted the vbe/add-injector branch January 15, 2024 16:53
@github-actions github-actions bot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Jan 15, 2024
TimBroddin pushed a commit that referenced this pull request Jan 18, 2024
Co-authored-by: escapemanuele <escapemanuele@gmail.com>
Co-authored-by: Renan <renansscarvalho@gmail.com>
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.

6 participants