-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
108 lines (94 loc) · 2.25 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
/**
* Internal dependencies
*/
import './style.scss';
import { registerBlock, createBlock, query } from '../../api';
import Editable from '../../editable';
const { children, prop } = query;
registerBlock( 'core/heading', {
title: wp.i18n.__( 'Heading' ),
icon: 'heading',
category: 'common',
attributes: {
content: children( 'h1,h2,h3,h4,h5,h6' ),
nodeName: prop( 'h1,h2,h3,h4,h5,h6', 'nodeName' )
},
controls: [
...'123456'.split( '' ).map( ( level ) => ( {
icon: 'heading',
title: wp.i18n.sprintf( wp.i18n.__( 'Heading %s' ), level ),
isActive: ( { nodeName } ) => 'H' + level === nodeName,
onClick( attributes, setAttributes ) {
setAttributes( { nodeName: 'H' + level } );
},
subscript: level
} ) )
],
transforms: {
from: [
{
type: 'block',
blocks: [ 'core/text' ],
transform: ( { content, ...attrs } ) => {
if ( Array.isArray( content ) ) {
const heading = createBlock( 'core/heading', {
content: content[ 0 ].props.children
} );
const blocks = [ heading ];
const remainingContent = content.slice( 1 );
if ( remainingContent.length ) {
const text = createBlock( 'core/text', {
...attrs,
content: remainingContent
} );
blocks.push( text );
}
return blocks;
}
return createBlock( 'core/heading', {
content
} );
}
}
],
to: [
{
type: 'block',
blocks: [ 'core/text' ],
transform: ( { content } ) => {
return createBlock( 'core/text', {
content
} );
}
}
]
},
merge( attributes, attributesToMerge ) {
return {
content: wp.element.concatChildren( attributes.content, attributesToMerge.content )
};
},
edit( { attributes, setAttributes, focus, setFocus, mergeWithPrevious } ) {
const { content, nodeName = 'H2' } = attributes;
return (
<Editable
tagName={ nodeName.toLowerCase() }
value={ content }
focus={ focus }
onFocus={ setFocus }
onChange={ ( value ) => setAttributes( { content: value } ) }
onMerge={ mergeWithPrevious }
inline
/>
);
},
save( { attributes } ) {
const { nodeName = 'H2', content } = attributes;
const Tag = nodeName.toLowerCase();
return (
<Tag>
{ content }
</Tag>
);
},
} );