edit.js
237 lines
| 1 | /** |
| 2 | * Internal dependencies |
| 3 | */ |
| 4 | |
| 5 | import Iframe from '../common/Iframe'; |
| 6 | import EmbedLoading from '../common/embed-loading'; |
| 7 | /** |
| 8 | * WordPress dependencies |
| 9 | */ |
| 10 | |
| 11 | const {__} = wp.i18n; |
| 12 | const {getBlobByURL, isBlobURL, revokeBlobURL} = wp.blob; |
| 13 | const {BlockIcon, MediaPlaceholder ,InspectorControls} = wp.editor; |
| 14 | const {Component, Fragment} = wp.element; |
| 15 | const { RangeControl,PanelBody, ExternalLink,ToggleControl } = wp.components; |
| 16 | import {DocumentIcon} from '../common/icons' |
| 17 | |
| 18 | const ALLOWED_MEDIA_TYPES = [ |
| 19 | 'application/pdf', |
| 20 | 'application/msword', |
| 21 | 'application/vnd.ms-powerpoint', |
| 22 | 'application/vnd.ms-excel', |
| 23 | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
| 24 | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
| 25 | 'application/vnd.openxmlformats-officedocument.presentationml.presentation' |
| 26 | ]; |
| 27 | |
| 28 | class DocumentEdit extends Component { |
| 29 | constructor() { |
| 30 | super(...arguments); |
| 31 | this.onSelectFile = this.onSelectFile.bind(this); |
| 32 | |
| 33 | this.onUploadError = this.onUploadError.bind(this); |
| 34 | this.onLoad = this.onLoad.bind(this); |
| 35 | this.hideOverlay = this.hideOverlay.bind(this); |
| 36 | this.state = { |
| 37 | hasError: false, |
| 38 | fetching:false, |
| 39 | interactive: false, |
| 40 | loadPdf: true, |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | componentDidMount() { |
| 46 | const { |
| 47 | attributes, |
| 48 | mediaUpload, |
| 49 | noticeOperations |
| 50 | } = this.props; |
| 51 | const {href} = attributes; |
| 52 | |
| 53 | // Upload a file drag-and-dropped into the editor |
| 54 | if (isBlobURL(href)) { |
| 55 | const file = getBlobByURL(href); |
| 56 | |
| 57 | mediaUpload({ |
| 58 | filesList: [file], |
| 59 | onFileChange: ([media]) => this.onSelectFile(media), |
| 60 | onError: (message) => { |
| 61 | this.setState({hasError: true}); |
| 62 | noticeOperations.createErrorNotice(message); |
| 63 | }, |
| 64 | }); |
| 65 | |
| 66 | revokeBlobURL(href); |
| 67 | } |
| 68 | |
| 69 | if(this.props.attributes.href && this.props.attributes.mime === 'application/pdf' && this.state.loadPdf){ |
| 70 | this.setState({loadPdf: false}); |
| 71 | PDFObject.embed(this.props.attributes.href, "."+this.props.attributes.id); |
| 72 | } |
| 73 | |
| 74 | } |
| 75 | |
| 76 | componentDidUpdate(prevProps) { |
| 77 | |
| 78 | // Reset copy confirmation state when block is deselected |
| 79 | if (prevProps.isSelected && !this.props.isSelected) { |
| 80 | this.setState({showCopyConfirmation: false}); |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | static getDerivedStateFromProps(nextProps, state) { |
| 86 | if (!nextProps.isSelected && state.interactive) { |
| 87 | return {interactive: false}; |
| 88 | } |
| 89 | |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | hideOverlay() { |
| 94 | this.setState({interactive: true}); |
| 95 | } |
| 96 | |
| 97 | onLoad() { |
| 98 | this.setState({ |
| 99 | fetching:false |
| 100 | }) |
| 101 | } |
| 102 | |
| 103 | onSelectFile(media) { |
| 104 | if (media && media.url) { |
| 105 | this.setState({hasError: false}); |
| 106 | this.props.setAttributes({ |
| 107 | href: media.url, |
| 108 | fileName: media.title, |
| 109 | id: 'embedpress-pdf-'+Date.now(), |
| 110 | mime: media.mime, |
| 111 | }); |
| 112 | |
| 113 | if(embedpressObj.embedpress_pro){ |
| 114 | this.props.setAttributes({ |
| 115 | powered_by: false |
| 116 | }); |
| 117 | } |
| 118 | if(media.mime === 'application/pdf'){ |
| 119 | this.setState({loadPdf: false}); |
| 120 | PDFObject.embed(media.url, "."+this.props.attributes.id); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | } |
| 125 | |
| 126 | onUploadError(message) { |
| 127 | const {noticeOperations} = this.props; |
| 128 | noticeOperations.removeAllNotices(); |
| 129 | noticeOperations.createErrorNotice(message); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | render() { |
| 136 | const {attributes, noticeUI,setAttributes} = this.props; |
| 137 | const {href,mime,id,width,height,powered_by} = attributes; |
| 138 | const {hasError,interactive,fetching,loadPdf} = this.state; |
| 139 | const min = 1; |
| 140 | const max = 1000; |
| 141 | const docLink = 'https://embedpress.com/docs/embed-docuemnt/' |
| 142 | if (!href || hasError) { |
| 143 | |
| 144 | return ( |
| 145 | <div className={"embedpress-document-editmode"}> |
| 146 | <MediaPlaceholder |
| 147 | icon={<BlockIcon icon={DocumentIcon}/>} |
| 148 | labels={{ |
| 149 | title: __('Document'), |
| 150 | instructions: __( |
| 151 | 'Upload a file or pick one from your media library for embed.' |
| 152 | ), |
| 153 | }} |
| 154 | onSelect={this.onSelectFile} |
| 155 | notices={noticeUI} |
| 156 | allowedTypes={ALLOWED_MEDIA_TYPES} |
| 157 | onError={this.onUploadError} |
| 158 | |
| 159 | > |
| 160 | |
| 161 | <div style={{width:'100%'}} className="components-placeholder__learn-more embedpress-doc-link"> |
| 162 | <ExternalLink href={docLink}>Learn more about Embedded document </ExternalLink> |
| 163 | </div> |
| 164 | </MediaPlaceholder> |
| 165 | |
| 166 | </div> |
| 167 | |
| 168 | ); |
| 169 | } else { |
| 170 | const url = '//view.officeapps.live.com/op/embed.aspx?src='+href; |
| 171 | return ( |
| 172 | <Fragment> |
| 173 | {(fetching && mime !== 'application/pdf') ? <EmbedLoading/> : null} |
| 174 | { mime === 'application/pdf' && ( |
| 175 | <div style={{height:height,width:width}} className={'embedpress-embed-document-pdf'+' '+id} data-emid={id} data-emsrc={href}></div> |
| 176 | |
| 177 | ) } |
| 178 | { mime !== 'application/pdf' && ( |
| 179 | <Iframe onMouseUponMouseUp={ this.hideOverlay } style={{height:height,width:width,display: fetching || !loadPdf ? 'none' : ''}} onLoad={this.onLoad} src={url} |
| 180 | mozallowfullscreen="true" webkitallowfullscreen="true"/> |
| 181 | ) } |
| 182 | { ! interactive && ( |
| 183 | <div |
| 184 | className="block-library-embed__interactive-overlay" |
| 185 | onMouseUp={ this.hideOverlay } |
| 186 | /> |
| 187 | ) } |
| 188 | { powered_by && ( |
| 189 | <p className="embedpress-el-powered">Powered By EmbedPress</p> |
| 190 | )} |
| 191 | |
| 192 | <InspectorControls key="inspector"> |
| 193 | <PanelBody |
| 194 | title={ __( 'Embed Size', 'embedpress' ) } |
| 195 | > |
| 196 | <RangeControl |
| 197 | label={ __( |
| 198 | 'Width', |
| 199 | 'embedpress' |
| 200 | ) } |
| 201 | value={ width } |
| 202 | onChange={ ( width ) => |
| 203 | setAttributes( { width } ) |
| 204 | } |
| 205 | max={ max } |
| 206 | min={ min } |
| 207 | /> |
| 208 | <RangeControl |
| 209 | label={ __( |
| 210 | 'Height', |
| 211 | 'embedpress' |
| 212 | ) } |
| 213 | value={height } |
| 214 | onChange={ ( height ) => |
| 215 | setAttributes( { height } ) |
| 216 | } |
| 217 | max={ max } |
| 218 | min={ min } |
| 219 | /> |
| 220 | <ToggleControl |
| 221 | label={ __( 'Powered By' ) } |
| 222 | onChange={ ( powered_by ) => |
| 223 | setAttributes( { powered_by } ) |
| 224 | } |
| 225 | checked={ powered_by } |
| 226 | /> |
| 227 | </PanelBody> |
| 228 | </InspectorControls> |
| 229 | </Fragment> |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | } |
| 234 | |
| 235 | }; |
| 236 | export default DocumentEdit; |
| 237 |