-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
184 lines (171 loc) · 5.21 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
177
178
179
180
181
182
183
184
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Button, Dashicon, Placeholder, Toolbar } from '@wordpress/components';
import { Component } from '@wordpress/element';
/**
* Internal dependencies
*/
import './style.scss';
import { registerBlockType } from '../../api';
import MediaUploadButton from '../../media-upload-button';
import Editable from '../../editable';
import BlockControls from '../../block-controls';
import BlockAlignmentToolbar from '../../block-alignment-toolbar';
import InspectorControls from '../../inspector-controls';
import BlockDescription from '../../block-description';
registerBlockType( 'core/audio', {
title: __( 'Audio' ),
icon: 'format-audio',
category: 'common',
attributes: {
src: {
type: 'string',
source: 'attribute',
selector: 'audio',
attribute: 'src',
},
align: {
type: 'string',
},
caption: {
type: 'array',
source: 'children',
selector: 'figcaption',
},
},
getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align || 'full' === align ) {
return { 'data-align': align };
}
},
edit: class extends Component {
constructor( { className } ) {
super( ...arguments );
// edit component has its own src in the state so it can be edited
// without setting the actual value outside of the edit UI
this.state = {
editing: ! this.props.attributes.src,
src: this.props.attributes.src,
className,
};
}
render() {
const { align, caption } = this.props.attributes;
const { setAttributes, focus, setFocus } = this.props;
const { editing, className, src } = this.state;
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
const switchToEditing = () => {
this.setState( { editing: true } );
};
const onSelectAudio = ( media ) => {
if ( media && media.url ) {
// sets the block's attribure and updates the edit component from the
// selected media, then switches off the editing UI
setAttributes( { src: media.url } );
this.setState( { src: media.url, editing: false } );
}
};
const onSelectUrl = ( event ) => {
event.preventDefault();
if ( src ) {
// set the block's src from the edit component's state, and switch off the editing UI
setAttributes( { src } );
this.setState( { editing: false } );
}
return false;
};
const controls = focus && [
<BlockControls key="controls">
<BlockAlignmentToolbar
value={ align }
onChange={ updateAlignment }
/>
<Toolbar>
<Button
buttonProps={ {
className: 'components-icon-button components-toolbar__control',
'aria-label': __( 'Edit audio' ),
} }
type="audio"
onClick={ switchToEditing }
>
<Dashicon icon="edit" />
</Button>
</Toolbar>
</BlockControls>,
<InspectorControls key="inspector">
<BlockDescription>
<p>{ __( 'The Audio block allows you to embed audio files and play them back using a simple player.' ) }</p>
</BlockDescription>
</InspectorControls>,
];
const focusCaption = ( focusValue ) => setFocus( { editable: 'caption', ...focusValue } );
if ( editing ) {
return [
controls,
<Placeholder
key="placeholder"
icon="media-audio"
label={ __( 'Audio' ) }
instructions={ __( 'Select an audio file from your library, or upload a new one:' ) }
className={ className }>
<form onSubmit={ onSelectUrl }>
<input
type="url"
className="components-placeholder__input"
placeholder={ __( 'Enter URL of audio file here…' ) }
onChange={ event => this.setState( { src: event.target.value } ) }
value={ src || '' } />
<Button
isLarge
type="submit">
{ __( 'Use URL' ) }
</Button>
</form>
<MediaUploadButton
buttonProps={ { isLarge: true } }
onSelect={ onSelectAudio }
type="audio"
>
{ __( 'Insert from Media Library' ) }
</MediaUploadButton>
</Placeholder>,
];
}
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
return [
controls,
<figure key="audio" className={ className }>
<audio controls="controls" src={ src } />
{ ( ( caption && caption.length ) || !! focus ) && (
<Editable
tagName="figcaption"
placeholder={ __( 'Write caption…' ) }
value={ caption }
focus={ focus && focus.editable === 'caption' ? focus : undefined }
onFocus={ focusCaption }
onChange={ ( value ) => setAttributes( { caption: value } ) }
inlineToolbar
/>
) }
</figure>,
];
/* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
}
},
save( { attributes } ) {
const { align, src, caption } = attributes;
return (
<figure className={ align ? `align${ align }` : null }>
<audio controls="controls" src={ src } />
{ caption && caption.length > 0 && <figcaption><Editable.Value value={ caption } /></figcaption> }
</figure>
);
},
} );