-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
82 lines (74 loc) · 2.07 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
/**
* Internal dependencies
*/
import './style.scss';
import { registerBlock, query as hpq } from 'api';
import Editable from 'components/editable';
const { children, query, attr } = hpq;
registerBlock( 'core/quote', {
title: wp.i18n.__( 'Quote' ),
icon: 'format-quote',
category: 'common',
attributes: {
value: query( 'blockquote > p', children() ),
citation: children( 'footer' ),
style: node => {
const value = attr( 'blockquote', 'class' )( node );
const match = value.match( /\bblocks-quote-style-(\d+)\b/ );
return match ? +match[ 1 ] : null;
}
},
controls: [ 1, 2 ].map( ( variation ) => ( {
icon: 'format-quote',
title: wp.i18n.sprintf( wp.i18n.__( 'Quote style %d' ), variation ),
isActive: ( { style = 1 } ) => +style === +variation,
onClick( attributes, setAttributes ) {
setAttributes( { style: variation } );
},
subscript: variation
} ) ),
edit( { attributes, setAttributes, focus, setFocus } ) {
const { value, citation } = attributes;
const style = +attributes.style || 1;
return (
<blockquote className={ `blocks-quote blocks-quote-style-${ style }` }>
<Editable
value={ value }
onChange={
( nextValue ) => setAttributes( {
value: nextValue
} )
}
focus={ focus && focus.editable === 'value' ? focus : null }
onFocus={ () => setFocus( { editable: 'value' } ) }
/>
{ ( citation || !! focus ) && (
<footer>
<Editable
value={ citation }
onChange={
( nextCitation ) => setAttributes( {
citation: nextCitation
} )
}
focus={ focus && focus.editable === 'citation' ? focus : null }
onFocus={ () => setFocus( { editable: 'citation' } ) }
/>
</footer>
) }
</blockquote>
);
},
save( attributes ) {
const { value, citation } = attributes;
const style = +attributes.style || 1;
return (
<blockquote className={ `blocks-quote-style-${ style }` }>
{ value && value.map( ( paragraph, i ) => (
<p key={ i }>{ paragraph }</p>
) ) }
<footer>{ citation }</footer>
</blockquote>
);
}
} );