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

Format library: Allow explicit registration when using npm packages #11611

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,11 @@ function gutenberg_register_scripts_and_styles() {
filemtime( gutenberg_dir_path() . 'build/format-library/index.js' ),
true
);
wp_add_inline_script(
'wp-format-library',
'wp.formatLibrary.registerCoreFormatTypes();',
'after'
);
Copy link
Contributor

Choose a reason for hiding this comment

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

How can we make sure someone use the edit-post module (via npm) get the formats by default?

Can we remove the wp_enqueue_script (and style) in favor of an explicit dependency towards wp-format-library in wp-edit-post?

Copy link
Member Author

Choose a reason for hiding this comment

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

Does it mean, we would call registerCoreFormatTypes in edit-post instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe, It's not perfect because people that want to unregister these styles would need edit-post as a dependency. I'd love other opinions.

gutenberg_override_script(
'wp-nux',
gutenberg_url( 'build/nux/index.js' ),
Expand Down
38 changes: 24 additions & 14 deletions packages/format-library/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* WordPress dependencies
*/
import {
registerFormatType,
} from '@wordpress/rich-text';

/**
* Internal dependencies
*/
Expand All @@ -8,18 +15,21 @@ import { italic } from './italic';
import { link } from './link';
import { strikethrough } from './strikethrough';

/**
* WordPress dependencies
*/
import {
registerFormatType,
} from '@wordpress/rich-text';
export function getCoreFormatTypes() {
Copy link
Member

Choose a reason for hiding this comment

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

JSDoc.

Copy link
Member Author

Choose a reason for hiding this comment

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

Does it mean we are good with the proposal?

return [
bold,
code,
image,
italic,
link,
strikethrough,
].map(
( { name, ...settings } ) => [ name, settings ]
Copy link
Member

Choose a reason for hiding this comment

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

Why did we land on an array return value here.

Copy link
Member Author

Choose a reason for hiding this comment

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

The shape it returns would have to be defined, I opted for the simplicity of refactoring :)

We can make it { name, settings } or reduce everything to { [ name ]: settings, ... }. I'm fine with all of them.

);
}

[
bold,
code,
image,
italic,
link,
strikethrough,
].forEach( ( { name, ...settings } ) => registerFormatType( name, settings ) );
export function registerCoreFormatTypes() {
getCoreFormatTypes().forEach(
( params ) => registerFormatType( ...params )
);
}