-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
305 lines (280 loc) · 7.3 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* External dependencies
*/
import { castArray, get, isString, isEmpty, omit } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Fragment } from '@wordpress/element';
import { createBlock, getPhrasingContentSchema } from '@wordpress/blocks';
import {
BlockControls,
AlignmentToolbar,
RichText,
} from '@wordpress/editor';
/**
* Internal dependencies
*/
import './style.scss';
import './editor.scss';
import './theme.scss';
const toRichTextValue = ( value ) => value.map( ( ( subValue ) => subValue.children ) );
const fromRichTextValue = ( value ) => value.map( ( subValue ) => ( {
children: subValue,
} ) );
const blockAttributes = {
value: {
type: 'array',
source: 'query',
selector: 'blockquote > p',
query: {
children: {
source: 'node',
},
},
default: [],
},
citation: {
type: 'array',
source: 'children',
selector: 'cite',
},
align: {
type: 'string',
},
};
export const name = 'core/quote';
export const settings = {
title: __( 'Quote' ),
description: __( 'Maybe someone else said it better -- add some quoted text.' ),
icon: 'format-quote',
category: 'common',
attributes: blockAttributes,
styles: [
{ name: 'default', label: __( 'Regular' ), isDefault: true },
{ name: 'large', label: __( 'Large' ) },
],
transforms: {
from: [
{
type: 'block',
isMultiBlock: true,
blocks: [ 'core/paragraph' ],
transform: ( attributes ) => {
const items = attributes.map( ( { content } ) => content );
const hasItems = ! items.every( isEmpty );
return createBlock( 'core/quote', {
value: hasItems ?
items.map( ( content, index ) => ( { children: <p key={ index }>{ content }</p> } ) ) :
[],
} );
},
},
{
type: 'block',
blocks: [ 'core/heading' ],
transform: ( { content } ) => {
return createBlock( 'core/quote', {
value: [
{ children: <p key="1">{ content }</p> },
],
} );
},
},
{
type: 'pattern',
regExp: /^>\s/,
transform: ( { content } ) => {
return createBlock( 'core/quote', {
value: [
{ children: <p key="1">{ content }</p> },
],
} );
},
},
{
type: 'raw',
selector: 'blockquote',
schema: {
blockquote: {
children: {
p: {
children: getPhrasingContentSchema(),
},
},
},
},
},
],
to: [
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( { value, citation } ) => {
// transforming an empty quote
if ( ( ! value || ! value.length ) && ! citation ) {
return createBlock( 'core/paragraph' );
}
// transforming a quote with content
return ( value || [] ).map( ( item ) => createBlock( 'core/paragraph', {
content: [ get( item, [ 'children', 'props', 'children' ], '' ) ],
} ) ).concat( citation ? createBlock( 'core/paragraph', {
content: citation,
} ) : [] );
},
},
{
type: 'block',
blocks: [ 'core/heading' ],
transform: ( { value, citation, ...attrs } ) => {
// if no text content exist just transform the quote into an heading block
// using citation as the content, it may be empty creating an empty heading block.
if ( ( ! value || ! value.length ) ) {
return createBlock( 'core/heading', {
content: citation,
} );
}
const firstValue = get( value, [ 0, 'children' ] );
const headingContent = castArray( isString( firstValue ) ?
firstValue :
get( firstValue, [ 'props', 'children' ], '' )
);
// if the quote content just contains a paragraph and no citation exist
// convert the quote content into and heading block.
if ( ! citation && value.length === 1 ) {
return createBlock( 'core/heading', {
content: headingContent,
} );
}
// In the normal case convert the first paragraph of quote into an heading
// and create a new quote block equal tl what we had excluding the first paragraph
const heading = createBlock( 'core/heading', {
content: headingContent,
} );
const quote = createBlock( 'core/quote', {
...attrs,
citation,
value: value.slice( 1 ),
} );
return [ heading, quote ];
},
},
],
},
edit( { attributes, setAttributes, isSelected, mergeBlocks, onReplace, className } ) {
const { align, value, citation } = attributes;
return (
<Fragment>
<BlockControls>
<AlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
/>
</BlockControls>
<blockquote className={ className } style={ { textAlign: align } }>
<RichText
multiline="p"
value={ toRichTextValue( value ) }
onChange={
( nextValue ) => setAttributes( {
value: fromRichTextValue( nextValue ),
} )
}
onMerge={ mergeBlocks }
onRemove={ ( forward ) => {
const hasEmptyCitation = ! citation || citation.length === 0;
if ( ! forward && hasEmptyCitation ) {
onReplace( [] );
}
} }
/* translators: the text of the quotation */
placeholder={ __( 'Write quote…' ) }
/>
{ ( ( citation && citation.length > 0 ) || isSelected ) && (
<RichText
tagName="cite"
value={ citation }
onChange={
( nextCitation ) => setAttributes( {
citation: nextCitation,
} )
}
/* translators: the individual or entity quoted */
placeholder={ __( 'Write citation…' ) }
/>
) }
</blockquote>
</Fragment>
);
},
save( { attributes } ) {
const { align, value, citation } = attributes;
return (
<blockquote style={ { textAlign: align ? align : null } }>
<RichText.Content value={ toRichTextValue( value ) } />
{ citation && citation.length > 0 && <RichText.Content tagName="cite" value={ citation } /> }
</blockquote>
);
},
deprecated: [
{
attributes: {
...blockAttributes,
style: {
type: 'number',
default: 1,
},
},
migrate( attributes ) {
if ( attributes.style === 2 ) {
return {
...omit( attributes, [ 'style' ] ),
className: attributes.className ? attributes.className + ' is-style-large' : 'is-style-large',
};
}
return attributes;
},
save( { attributes } ) {
const { align, value, citation, style } = attributes;
return (
<blockquote
className={ style === 2 ? 'is-large' : '' }
style={ { textAlign: align ? align : null } }
>
<RichText.Content value={ toRichTextValue( value ) } />
{ citation && citation.length > 0 && <RichText.Content tagName="cite" value={ citation } /> }
</blockquote>
);
},
},
{
attributes: {
...blockAttributes,
citation: {
type: 'array',
source: 'children',
selector: 'footer',
},
style: {
type: 'number',
default: 1,
},
},
save( { attributes } ) {
const { align, value, citation, style } = attributes;
return (
<blockquote
className={ `blocks-quote-style-${ style }` }
style={ { textAlign: align ? align : null } }
>
<RichText.Content value={ toRichTextValue( value ) } />
{ citation && citation.length > 0 && <RichText.Content tagName="footer" value={ citation } /> }
</blockquote>
);
},
},
],
};