-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
221 lines (209 loc) · 5.36 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* External dependencies
*/
import { isString, isObject } from 'lodash';
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { Toolbar } from '@wordpress/components';
/**
* Internal dependencies
*/
import './style.scss';
import { registerBlockType, createBlock, source } from '../../api';
import AlignmentToolbar from '../../alignment-toolbar';
import BlockControls from '../../block-controls';
import Editable from '../../editable';
const { children, node: element, query } = source;
registerBlockType( 'core/quote', {
title: __( 'Quote' ),
icon: 'format-quote',
category: 'common',
attributes: {
value: {
type: 'array',
source: query( 'blockquote > p', element() ),
default: [],
},
citation: {
type: 'array',
source: children( 'footer' ),
},
align: {
type: 'string',
},
style: {
type: 'number',
default: 1,
},
},
transforms: {
from: [
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( { content } ) => {
return createBlock( 'core/quote', {
value: [
<p key="1">{ content }</p>,
],
} );
},
},
{
type: 'block',
blocks: [ 'core/heading' ],
transform: ( { content } ) => {
return createBlock( 'core/quote', {
value: [
<p key="1">{ content }</p>,
],
} );
},
},
{
type: 'pattern',
regExp: /^>\s/,
transform: ( { content } ) => {
return createBlock( 'core/quote', {
value: [
<p key="1">{ content }</p>,
],
} );
},
},
{
type: 'raw',
isMatch: ( node ) => node.nodeName === 'BLOCKQUOTE',
},
],
to: [
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( { value, citation, ...attrs } ) => {
const textElement = value[ 0 ];
if ( ! textElement ) {
return createBlock( 'core/paragraph', {
content: citation,
} );
}
const textContent = isString( textElement ) ? textElement : textElement.props.children;
if ( Array.isArray( value ) || citation ) {
const text = createBlock( 'core/paragraph', {
content: textContent,
} );
const quote = createBlock( 'core/quote', {
...attrs,
citation,
value: Array.isArray( value ) ? value.slice( 1 ) : '',
} );
return [ text, quote ];
}
return createBlock( 'core/paragraph', {
content: textContent,
} );
},
},
{
type: 'block',
blocks: [ 'core/heading' ],
transform: ( { value, citation, ...attrs } ) => {
const isMultiParagraph = Array.isArray( value ) && isObject( value[ 0 ] ) && value[ 0 ].type === 'p';
const headingElement = isMultiParagraph ? value[ 0 ] : value;
const headingContent = isObject( headingElement ) && value[ 0 ].type === 'p'
? headingElement.props.children
: headingElement;
if ( isMultiParagraph || citation ) {
const heading = createBlock( 'core/heading', {
content: headingContent,
} );
const quote = createBlock( 'core/quote', {
...attrs,
citation,
value: Array.isArray( value ) ? value.slice( 1 ) : '',
} );
return [ heading, quote ];
}
return createBlock( 'core/heading', {
content: headingContent,
} );
},
},
],
},
edit( { attributes, setAttributes, focus, setFocus, mergeBlocks, className } ) {
const { align, value, citation, style } = attributes;
const focusedEditable = focus ? focus.editable || 'value' : null;
return [
focus && (
<BlockControls key="controls">
<Toolbar controls={ [ 1, 2 ].map( ( variation ) => ( {
icon: 1 === variation ? 'format-quote' : 'testimonial',
title: sprintf( __( 'Quote style %d' ), variation ),
isActive: Number( style ) === variation,
onClick() {
setAttributes( { style: variation } );
},
} ) ) } />
<AlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
/>
</BlockControls>
),
<blockquote
key="quote"
className={ `${ className } blocks-quote-style-${ style }` }
>
<Editable
multiline="p"
value={ value }
onChange={
( nextValue ) => setAttributes( {
value: nextValue,
} )
}
focus={ focusedEditable === 'value' ? focus : null }
onFocus={ ( props ) => setFocus( { ...props, editable: 'value' } ) }
onMerge={ mergeBlocks }
style={ { textAlign: align } }
placeholder={ __( 'Write quote…' ) }
/>
{ ( ( citation && citation.length > 0 ) || !! focus ) && (
<Editable
tagName="footer"
value={ citation }
placeholder={ __( 'Write citation…' ) }
onChange={
( nextCitation ) => setAttributes( {
citation: nextCitation,
} )
}
focus={ focusedEditable === 'citation' ? focus : null }
onFocus={ ( props ) => setFocus( { ...props, editable: 'citation' } ) }
/>
) }
</blockquote>,
];
},
save( { attributes } ) {
const { align, value, citation, style } = attributes;
return (
<blockquote
className={ `blocks-quote-style-${ style }` }
style={ { textAlign: align ? align : null } }
>
{ value.map( ( paragraph, i ) => (
<p key={ i }>{ paragraph.props.children }</p>
) ) }
{ citation && citation.length > 0 && (
<footer>{ citation }</footer>
) }
</blockquote>
);
},
} );