-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
175 lines (151 loc) · 4.33 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
/**
* External Dependencies
*/
import { pick } from 'lodash';
/**
* WordPress dependencies
*/
import { parseWithAttributeSchema } from '@wordpress/blocks';
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
// Getter for the sake of unit tests.
const getGalleryDetailsMediaFrame = () => {
/**
* Custom gallery details frame.
*
* @link https://github.com/xwp/wp-core-media-widgets/blob/905edbccfc2a623b73a93dac803c5335519d7837/wp-admin/js/widgets/media-gallery-widget.js
* @class GalleryDetailsMediaFrame
* @constructor
*/
return wp.media.view.MediaFrame.Post.extend( {
/**
* Create the default states.
*
* @return {void}
*/
createStates: function createStates() {
this.states.add( [
new wp.media.controller.Library( {
id: 'gallery',
title: wp.media.view.l10n.createGalleryTitle,
priority: 40,
toolbar: 'main-gallery',
filterable: 'uploaded',
multiple: 'add',
editable: false,
library: wp.media.query( _.defaults( {
type: 'image',
}, this.options.library ) ),
} ),
new wp.media.controller.GalleryEdit( {
library: this.options.selection,
editing: this.options.editing,
menu: 'gallery',
displaySettings: false,
} ),
new wp.media.controller.GalleryAdd(),
] );
},
} );
};
// the media library image object contains numerous attributes
// we only need this set to display the image in the library
const slimImageObject = ( img ) => {
const attrSet = [ 'sizes', 'mime', 'type', 'subtype', 'id', 'url', 'alt', 'link', 'caption' ];
return pick( img, attrSet );
};
class MediaUpload extends Component {
constructor( { multiple = false, type, gallery = false, title = __( 'Select or Upload Media' ), modalClass } ) {
super( ...arguments );
this.openModal = this.openModal.bind( this );
this.onSelect = this.onSelect.bind( this );
this.onUpdate = this.onUpdate.bind( this );
this.onOpen = this.onOpen.bind( this );
this.processMediaCaption = this.processMediaCaption.bind( this );
const frameConfig = {
title,
button: {
text: __( 'Select' ),
},
multiple,
selection: new wp.media.model.Selection( [] ),
};
if ( !! type ) {
frameConfig.library = { type };
}
if ( gallery ) {
const GalleryDetailsMediaFrame = getGalleryDetailsMediaFrame();
this.frame = new GalleryDetailsMediaFrame( {
frame: 'select',
mimeType: type,
state: 'gallery',
} );
wp.media.frame = this.frame;
} else {
this.frame = wp.media( frameConfig );
}
if ( modalClass ) {
this.frame.$el.addClass( modalClass );
}
// When an image is selected in the media frame...
this.frame.on( 'select', this.onSelect );
this.frame.on( 'update', this.onUpdate );
this.frame.on( 'open', this.onOpen );
}
componentWillUnmount() {
this.frame.remove();
}
onUpdate( selections ) {
const { onSelect, multiple = false } = this.props;
const state = this.frame.state();
const selectedImages = selections || state.get( 'selection' );
if ( ! selectedImages || ! selectedImages.models.length ) {
return;
}
if ( multiple ) {
onSelect( selectedImages.models.map( ( model ) => this.processMediaCaption( slimImageObject( model.toJSON() ) ) ) );
} else {
onSelect( this.processMediaCaption( slimImageObject( ( selectedImages.models[ 0 ].toJSON() ) ) ) );
}
}
onSelect() {
const { onSelect, multiple = false } = this.props;
// Get media attachment details from the frame state
const attachment = this.frame.state().get( 'selection' ).toJSON();
onSelect(
multiple ?
attachment.map( this.processMediaCaption ) :
this.processMediaCaption( attachment[ 0 ] )
);
}
onOpen() {
const selection = this.frame.state().get( 'selection' );
const addMedia = ( id ) => {
const attachment = wp.media.attachment( id );
attachment.fetch();
selection.add( attachment );
};
if ( ! this.props.value ) {
return;
}
if ( this.props.multiple ) {
this.props.value.map( addMedia );
} else {
addMedia( this.props.value );
}
}
openModal() {
this.frame.open();
}
processMediaCaption( mediaObject ) {
return ! mediaObject.caption ?
mediaObject :
{ ...mediaObject, caption: parseWithAttributeSchema( mediaObject.caption, {
source: 'children',
} ) };
}
render() {
return this.props.render( { open: this.openModal } );
}
}
export default MediaUpload;