-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
176 lines (160 loc) · 4.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
/**
* External dependencies
*/
import { isObject } from 'lodash';
/**
* WordPress dependencies
*/
import { __, sprintf } from 'i18n';
import { concatChildren } from 'element';
import { Toolbar } from 'components';
/**
* Internal dependencies
*/
import './style.scss';
import { registerBlockType, createBlock, query } from '../../api';
import Editable from '../../editable';
import BlockControls from '../../block-controls';
import InspectorControls from '../../inspector-controls';
import AlignmentToolbar from '../../alignment-toolbar';
import BlockDescription from '../../block-description';
const { children, prop } = query;
registerBlockType( 'core/heading', {
title: __( 'Heading' ),
icon: 'heading',
category: 'common',
className: false,
attributes: {
content: children( 'h1,h2,h3,h4,h5,h6' ),
nodeName: prop( 'h1,h2,h3,h4,h5,h6', 'nodeName' ),
},
transforms: {
from: [
{
type: 'block',
blocks: [ 'core/text' ],
transform: ( { content, ...attrs } ) => {
const isMultiParagraph = Array.isArray( content ) && isObject( content[ 0 ] ) && content[ 0 ].type === 'p';
if ( isMultiParagraph ) {
const headingContent = isObject( content[ 0 ] ) && content[ 0 ].type === 'p'
? content[ 0 ].props.children
: content[ 0 ];
const heading = createBlock( 'core/heading', {
content: headingContent,
} );
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,
} );
},
},
{
type: 'raw',
matcher: ( node ) => /H\d/.test( node.nodeName ),
attributes: {
content: children( 'h1,h2,h3,h4,h5,h6' ),
nodeName: prop( 'h1,h2,h3,h4,h5,h6', 'nodeName' ),
},
},
],
to: [
{
type: 'block',
blocks: [ 'core/text' ],
transform: ( { content } ) => {
return createBlock( 'core/text', {
content,
} );
},
},
],
},
merge( attributes, attributesToMerge ) {
return {
content: concatChildren( attributes.content, attributesToMerge.content ),
};
},
edit( { attributes, setAttributes, focus, setFocus, mergeBlocks, insertBlocksAfter } ) {
const { align, content, nodeName = 'H2' } = attributes;
return [
focus && (
<BlockControls
key="controls"
controls={
'234'.split( '' ).map( ( level ) => ( {
icon: 'heading',
title: sprintf( __( 'Heading %s' ), level ),
isActive: 'H' + level === nodeName,
onClick: () => setAttributes( { nodeName: 'H' + level } ),
subscript: level,
} ) )
}
/>
),
focus && (
<InspectorControls key="inspector">
<BlockDescription>
<p>{ __( 'Search engines use the headings to index the structure and content of your web pages.' ) }</p>
</BlockDescription>
<h3>{ __( 'Heading Settings' ) }</h3>
<p>{ __( 'Size' ) }</p>
<Toolbar
controls={
'123456'.split( '' ).map( ( level ) => ( {
icon: 'heading',
title: sprintf( __( 'Heading %s' ), level ),
isActive: 'H' + level === nodeName,
onClick: () => setAttributes( { nodeName: 'H' + level } ),
subscript: level,
} ) )
}
/>
<p>{ __( 'Text Alignment' ) }</p>
<AlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
/>
</InspectorControls>
),
<Editable
key="editable"
tagName={ nodeName.toLowerCase() }
value={ content }
focus={ focus }
onFocus={ setFocus }
onChange={ ( value ) => setAttributes( { content: value } ) }
onMerge={ mergeBlocks }
onSplit={ ( before, after, ...blocks ) => {
setAttributes( { content: before } );
insertBlocksAfter( [
...blocks,
createBlock( 'core/text', { content: after } ),
] );
} }
style={ { textAlign: align } }
placeholder={ __( 'Write heading…' ) }
/>,
];
},
save( { attributes } ) {
const { align, nodeName = 'H2', content } = attributes;
const Tag = nodeName.toLowerCase();
return (
<Tag style={ { textAlign: align } } >
{ content }
</Tag>
);
},
} );