Skip to content

Commit ba8a385

Browse files
committed
Blocks: Refactor paragraph to be exported as a name and settings pair
1 parent 8fd0049 commit ba8a385

File tree

4 files changed

+86
-90
lines changed

4 files changed

+86
-90
lines changed

blocks/library/embed/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ function getEmbedBlockSettings( { title, icon, category = 'embed', transforms, k
239239

240240
export const name = 'core/embed';
241241

242-
export const settings = getEmbedBlockSettings( {
242+
export const settings = getEmbedBlockSettings( {
243243
title: __( 'Embed' ),
244244
icon: 'embed-generic',
245245
transforms: {

blocks/library/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
*/
44
import * as audio from './audio';
55
import * as embed from './embed';
6-
import { registerBlockType } from '../api';
6+
import * as paragraph from './paragraph';
7+
import { registerBlockType, setDefaultBlockName } from '../api';
78
import { registerShortcodeBlock } from './shortcode';
89
import { registerImageBlock } from './image';
910
import { registerGalleryBlock } from './gallery';
@@ -26,14 +27,14 @@ import { registerTextColumnsBlock } from './text-columns';
2627
import { registerVerseBlock } from './verse';
2728
import { registerVideoBlock } from './video';
2829
import { registerReusableBlock } from './block';
29-
import { registerParagraphBlock } from './paragraph';
3030

3131
export const registerCoreBlocks = () => {
3232
[
3333
audio,
3434
embed,
3535
...embed.common,
3636
...embed.others,
37+
paragraph,
3738
].forEach( ( { name, settings } ) => {
3839
registerBlockType( name, settings );
3940
} );
@@ -59,5 +60,6 @@ export const registerCoreBlocks = () => {
5960
registerVerseBlock();
6061
registerVideoBlock();
6162
registerReusableBlock();
62-
registerParagraphBlock();
63+
64+
setDefaultBlockName( paragraph.name );
6365
};

blocks/library/paragraph/index.js

+78-80
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Autocomplete, PanelBody, PanelColor, withFallbackStyles } from '@wordpr
1515
*/
1616
import './editor.scss';
1717
import './style.scss';
18-
import { registerBlockType, createBlock, setDefaultBlockName } from '../../api';
18+
import { createBlock } from '../../api';
1919
import { blockAutocompleter, userAutocompleter } from '../../autocompleters';
2020
import AlignmentToolbar from '../../alignment-toolbar';
2121
import BlockAlignmentToolbar from '../../block-alignment-toolbar';
@@ -192,97 +192,95 @@ class ParagraphBlock extends Component {
192192
}
193193
}
194194

195-
export const registerParagraphBlock = () => {
196-
registerBlockType( 'core/paragraph', {
197-
title: __( 'Paragraph' ),
195+
export const name = 'core/paragraph';
198196

199-
description: __( 'This is a simple text only block for adding a single paragraph of content.' ),
197+
export const settings = {
198+
title: __( 'Paragraph' ),
200199

201-
icon: 'editor-paragraph',
200+
description: __( 'This is a simple text only block for adding a single paragraph of content.' ),
202201

203-
category: 'common',
202+
icon: 'editor-paragraph',
204203

205-
keywords: [ __( 'text' ) ],
204+
category: 'common',
206205

207-
supports: {
208-
className: false,
209-
},
206+
keywords: [ __( 'text' ) ],
210207

211-
attributes: {
212-
content: {
213-
type: 'array',
214-
source: 'children',
215-
selector: 'p',
216-
},
217-
align: {
218-
type: 'string',
219-
},
220-
dropCap: {
221-
type: 'boolean',
222-
default: false,
223-
},
224-
placeholder: {
225-
type: 'string',
226-
},
227-
width: {
228-
type: 'string',
229-
},
230-
textColor: {
231-
type: 'string',
232-
},
233-
backgroundColor: {
234-
type: 'string',
235-
},
236-
fontSize: {
237-
type: 'number',
238-
},
239-
},
208+
supports: {
209+
className: false,
210+
},
240211

241-
transforms: {
242-
from: [
243-
{
244-
type: 'raw',
245-
isMatch: ( node ) => (
246-
node.nodeName === 'P' &&
247-
// Do not allow embedded content.
248-
! node.querySelector( 'audio, canvas, embed, iframe, img, math, object, svg, video' )
249-
),
250-
},
251-
],
212+
attributes: {
213+
content: {
214+
type: 'array',
215+
source: 'children',
216+
selector: 'p',
252217
},
253-
254-
merge( attributes, attributesToMerge ) {
255-
return {
256-
content: concatChildren( attributes.content, attributesToMerge.content ),
257-
};
218+
align: {
219+
type: 'string',
258220
},
259-
260-
getEditWrapperProps( attributes ) {
261-
const { width } = attributes;
262-
if ( [ 'wide', 'full', 'left', 'right' ].indexOf( width ) !== -1 ) {
263-
return { 'data-align': width };
264-
}
221+
dropCap: {
222+
type: 'boolean',
223+
default: false,
224+
},
225+
placeholder: {
226+
type: 'string',
227+
},
228+
width: {
229+
type: 'string',
230+
},
231+
textColor: {
232+
type: 'string',
233+
},
234+
backgroundColor: {
235+
type: 'string',
236+
},
237+
fontSize: {
238+
type: 'number',
265239
},
240+
},
266241

267-
edit: ParagraphBlock,
242+
transforms: {
243+
from: [
244+
{
245+
type: 'raw',
246+
isMatch: ( node ) => (
247+
node.nodeName === 'P' &&
248+
// Do not allow embedded content.
249+
! node.querySelector( 'audio, canvas, embed, iframe, img, math, object, svg, video' )
250+
),
251+
},
252+
],
253+
},
268254

269-
save( { attributes } ) {
270-
const { width, align, content, dropCap, backgroundColor, textColor, fontSize } = attributes;
271-
const className = classnames( {
272-
[ `align${ width }` ]: width,
273-
'has-background': backgroundColor,
274-
'has-drop-cap': dropCap,
275-
} );
276-
const styles = {
277-
backgroundColor: backgroundColor,
278-
color: textColor,
279-
fontSize: fontSize,
280-
textAlign: align,
281-
};
255+
merge( attributes, attributesToMerge ) {
256+
return {
257+
content: concatChildren( attributes.content, attributesToMerge.content ),
258+
};
259+
},
282260

283-
return <p style={ styles } className={ className ? className : undefined }>{ content }</p>;
284-
},
285-
} );
261+
getEditWrapperProps( attributes ) {
262+
const { width } = attributes;
263+
if ( [ 'wide', 'full', 'left', 'right' ].indexOf( width ) !== -1 ) {
264+
return { 'data-align': width };
265+
}
266+
},
267+
268+
edit: ParagraphBlock,
269+
270+
save( { attributes } ) {
271+
const { width, align, content, dropCap, backgroundColor, textColor, fontSize } = attributes;
272+
const className = classnames( {
273+
[ `align${ width }` ]: width,
274+
'has-background': backgroundColor,
275+
'has-drop-cap': dropCap,
276+
} );
277+
const styles = {
278+
backgroundColor: backgroundColor,
279+
color: textColor,
280+
fontSize: fontSize,
281+
textAlign: align,
282+
};
286283

287-
setDefaultBlockName( 'core/paragraph' );
284+
return <p style={ styles } className={ className ? className : undefined }>{ content }</p>;
285+
},
288286
};

blocks/library/paragraph/test/index.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
/**
22
* Internal dependencies
33
*/
4-
import { registerParagraphBlock } from '../';
4+
import { name, settings } from '../';
55
import { blockEditRender } from 'blocks/test/helpers';
66

77
describe( 'core/paragraph', () => {
8-
beforeAll( () => {
9-
registerParagraphBlock();
10-
} );
11-
128
test( 'block edit matches snapshot', () => {
13-
const wrapper = blockEditRender( 'core/paragraph' );
9+
const wrapper = blockEditRender( name, settings );
1410

1511
expect( wrapper ).toMatchSnapshot();
1612
} );

0 commit comments

Comments
 (0)