-
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) · 2.18 KB
/
index.js
File metadata and controls
89 lines (78 loc) · 2.18 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
/**
* WordPress dependencies
*/
import { IconButton } from 'components';
/**
* Internal dependencies
*/
import './style.scss';
import { registerBlockType, query } from '../../api';
import Editable from '../../editable';
import BlockControls from '../../block-controls';
import BlockAlignmentToolbar from '../../block-alignment-toolbar';
const { attr, children } = query;
registerBlockType( 'core/button', {
title: wp.i18n.__( 'Button' ),
icon: 'button',
category: 'layout',
attributes: {
url: attr( 'a', 'href' ),
title: attr( 'a', 'title' ),
text: children( 'a' ),
},
getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'center' === align ) {
return { 'data-align': align };
}
},
edit( { attributes, setAttributes, focus, setFocus } ) {
const { text, url, title, align } = attributes;
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
return [
focus && (
<BlockControls key="controls">
<BlockAlignmentToolbar value={ align } onChange={ updateAlignment } />
</BlockControls>
),
<span key="button" className="blocks-button" title={ title }>
<Editable
tagName="span"
placeholder={ wp.i18n.__( 'Write label…' ) }
value={ text }
focus={ focus }
onFocus={ setFocus }
onChange={ ( value ) => setAttributes( { text: value } ) }
inline
inlineToolbar
formattingControls={ [ 'bold', 'italic', 'strikethrough' ] }
/>
{ focus &&
<form
className="editable-format-toolbar__link-modal"
onSubmit={ ( event ) => event.preventDefault() }>
<input
className="editable-format-toolbar__link-input"
type="url"
required
value={ url }
onChange={ ( event ) => setAttributes( { url: event.target.value } ) }
placeholder={ wp.i18n.__( 'Paste URL or type' ) }
/>
<IconButton icon="editor-break" type="submit" />
</form>
}
</span>,
];
},
save( { attributes } ) {
const { url, text, title, align = 'none' } = attributes;
return (
<div className={ `align${ align }` }>
<a href={ url } title={ title }>
{ text }
</a>
</div>
);
},
} );