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

Markdown block: Add Align and Spacing features #22429

Merged
merged 7 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Markdown block: Add Align and Spacing features
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/extensions/blocks/markdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ export const settings = {
},

supports: {
align: [ 'wide', 'full' ],
html: false,
spacing: {
padding: true,
},
},

edit,
Expand Down
10 changes: 8 additions & 2 deletions projects/plugins/jetpack/extensions/blocks/markdown/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { __ } from '@wordpress/i18n';
import MarkdownIt from 'markdown-it';
import { RawHTML } from '@wordpress/element';
import { __experimentalGetSpacingClassesAndStyles as getSpacingClassesAndStyles } from '@wordpress/block-editor';

/**
* Module variables
Expand All @@ -21,8 +22,13 @@ const handleLinkClick = event => {
}
};

export default ( { className, source = '' } ) => (
<RawHTML className={ className } onClick={ handleLinkClick }>
const getStyles = ( attributes = {} ) => {
const spacingProps = getSpacingClassesAndStyles( attributes );
Copy link
Member

Choose a reason for hiding this comment

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

Should we check that getSpacingClassesAndStyles exists before we use it, given that it's still marked as experimental?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we check that getSpacingClassesAndStyles exists before we use it, given that it's still marked as experimental?

Thank you for the feedback, Jeremy! That's a good point.

I have added check in the new commit: e367e3d:

const getStyles = ( attributes = {} ) => {
	const spacingProps = getSpacingClassesAndStyles?.( attributes );
	return spacingProps?.style ? spacingProps.style : {};
};

In case the getSpacingClassesAndStyles() doesn't exist, spacingProps will be undefined and getStyles() will return an empty object. Padding won't be applied and the app won't crash. This is what the user would see:

Markup on 2022-01-21 at 18:36:30

When they click the "Attempt Block Recovery" button, padding will be removed from the content, but everything else will work correctly.

The proposed code will now also check whether spacingProps.style exists.


By the way, I was doing a research on the state of __experimentalGetSpacingClassesAndStyles() before and could find the following thread: WordPress/gutenberg#35920. There are no details shared unfortunately, but the function is being used with core Button block. Also, similar getColorClassesAndStyles() function is used with the Table and Pullquote blocks.

return spacingProps.style;
};

export default ( { className, source = '', attributes } ) => (
<RawHTML className={ className } onClick={ handleLinkClick } style={ getStyles( attributes ) }>
{ source.length ? markdownConverter.render( source ) : '' }
</RawHTML>
);
6 changes: 5 additions & 1 deletion projects/plugins/jetpack/extensions/blocks/markdown/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
import MarkdownRenderer from './renderer';

export default ( { attributes, className } ) => (
<MarkdownRenderer className={ className } source={ attributes.source } />
<MarkdownRenderer
className={ className }
source={ attributes.source }
attributes={ attributes }
/>
);
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`MarkdownRenderer renders markdown to HTML as expected 1`] = `
<RawHTML
className="markdown"
onClick={[Function]}
style={Object {}}
>
&lt;h1&gt;Heading&lt;/h1&gt;
&lt;h2&gt;2nd Heading&lt;/h2&gt;
Expand Down