-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
106 lines (92 loc) · 2.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* External dependencies
*/
import { isString } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from 'i18n';
/**
* Internal dependencies
*/
import './style.scss';
import './block.scss';
import { registerBlockType, query as hpq } from '../../api';
import Editable from '../../editable';
import BlockControls from '../../block-controls';
import BlockAlignmentToolbar from '../../block-alignment-toolbar';
const { children, query, node } = hpq;
registerBlockType( 'core/pullquote', {
title: __( 'Pullquote' ),
icon: 'format-quote',
category: 'formatting',
attributes: {
value: query( 'blockquote > p', node() ),
citation: children( 'footer' ),
},
getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align || 'full' === align ) {
return { 'data-align': align };
}
},
edit( { attributes, setAttributes, focus, setFocus, className, settings } ) {
const { value, citation, align } = attributes;
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
return [
focus && (
<BlockControls key="controls">
<BlockAlignmentToolbar
value={ align }
onChange={ updateAlignment }
wideControlsEnabled={ settings.wideImages }
/>
</BlockControls>
),
<blockquote key="quote" className={ className }>
<Editable
multiline="p"
value={ value }
onChange={
( nextValue ) => setAttributes( {
value: nextValue,
} )
}
placeholder={ __( 'Write quote…' ) }
focus={ focus && focus.editable === 'value' ? focus : null }
onFocus={ ( props ) => setFocus( { ...props, editable: 'value' } ) }
className="blocks-pullquote__content"
/>
{ ( citation || !! focus ) && (
<Editable
tagName="footer"
value={ citation }
placeholder={ __( 'Write caption…' ) }
onChange={
( nextCitation ) => setAttributes( {
citation: nextCitation,
} )
}
focus={ focus && focus.editable === 'citation' ? focus : null }
onFocus={ ( props ) => setFocus( { ...props, editable: 'citation' } ) }
/>
) }
</blockquote>,
];
},
save( { attributes } ) {
const { value, citation, align = 'none' } = attributes;
return (
<blockquote className={ `align${ align }` }>
{ value && value.map( ( paragraph, i ) => (
<p key={ i }>
{ isString( paragraph ) ? paragraph : paragraph.props.children }
</p>
) ) }
{ citation && citation.length > 0 && (
<footer>{ citation }</footer>
) }
</blockquote>
);
},
} );