-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (78 loc) · 1.75 KB
/
index.js
File metadata and controls
89 lines (78 loc) · 1.75 KB
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
/**
* Internal dependencies
*/
import { registerBlock, query } from 'api';
import Editable from 'components/editable';
const { html, prop } = query;
registerBlock( 'core/heading', {
title: wp.i18n.__( 'Heading' ),
icon: 'heading',
category: 'common',
attributes: {
content: html( 'h1,h2,h3,h4,h5,h6' ),
tag: prop( 'h1,h2,h3,h4,h5,h6', 'nodeName' ),
align: prop( 'h1,h2,h3,h4,h5,h6', 'style.textAlign' )
},
controls: [
...'123456'.split( '' ).map( ( level ) => ( {
icon: 'heading',
title: wp.i18n.sprintf( wp.i18n.__( 'Heading %s' ), level ),
isActive: ( { tag } ) => 'H' + level === tag,
onClick( attributes, setAttributes ) {
setAttributes( { tag: 'H' + level } );
},
level
} ) )
],
edit( { attributes, setAttributes } ) {
const { content, tag, align } = attributes;
return (
<Editable
tagName={ tag }
value={ content }
onChange={ ( value ) => setAttributes( { content: value } ) }
style={ align ? { textAlign: align } : null }
/>
);
},
save( { attributes } ) {
const { align, tag: Tag, content } = attributes;
return (
<Tag
style={ align ? { textAlign: align } : null }
dangerouslySetInnerHTML={ { __html: content } } />
);
},
transforms: {
from: [
{
type: 'block',
blocks: [ 'core/text' ],
transform: ( { content, align } ) => {
if ( Array.isArray( content ) ) {
// TODO this appears to always be true?
// TODO reject the switch if more than one paragraph
content = content[ 0 ];
}
return {
tag: 'H2',
content,
align
};
}
}
],
to: [
{
type: 'block',
blocks: [ 'core/text' ],
transform: ( { content, align } ) => {
return {
content,
align
};
}
}
]
}
} );