From 13b6b35faa6394711275892688d4c18feefd5dc0 Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Tue, 8 Aug 2017 13:13:56 -0700 Subject: [PATCH 01/17] Add initial abstraction for File Upload --- utils/fileupload.js | 34 ++++++++++++++++++++++++++++++++++ utils/index.js | 4 ++++ 2 files changed, 38 insertions(+) create mode 100644 utils/fileupload.js diff --git a/utils/fileupload.js b/utils/fileupload.js new file mode 100644 index 0000000000000..3cf3ca8e0beb2 --- /dev/null +++ b/utils/fileupload.js @@ -0,0 +1,34 @@ + +export default function fileUpload( files, setAttributes ) { + console.log("Here!", files, setAttributes); + + const media = files[ 0 ]; + + // Only allow image uploads + if ( ! /^image\//.test( media.type ) ) { + return; + } + + // Use File API to assign temporary URL from upload + setAttributes( { + url: window.URL.createObjectURL( media ), + } ); + + // Create upload payload + const data = new window.FormData(); + data.append( 'file', media ); + + new wp.api.models.Media().save( null, { + data: data, + contentType: false, + } ).done( ( savedMedia ) => { + setAttributes( { + id: savedMedia.id, + url: savedMedia.source_url, + } ); + } ).fail( () => { + // Reset to empty on failure. + // TODO: Better failure messaging + setAttributes( { url: null } ); + } ); +} diff --git a/utils/index.js b/utils/index.js index e24a5b1782f91..444acedf1e981 100644 --- a/utils/index.js +++ b/utils/index.js @@ -3,3 +3,7 @@ import * as nodetypes from './nodetypes'; export { keycodes }; export { nodetypes }; + +export * from './tracking'; + +export * from './fileupload'; From cb9acac307f1d4a2a8148c810273faf935ce4342 Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Tue, 8 Aug 2017 13:14:40 -0700 Subject: [PATCH 02/17] Use abstracted fileUpload function --- blocks/library/image/index.js | 35 +++-------------------------------- 1 file changed, 3 insertions(+), 32 deletions(-) diff --git a/blocks/library/image/index.js b/blocks/library/image/index.js index 204f3f0144135..c8a9340e5426b 100644 --- a/blocks/library/image/index.js +++ b/blocks/library/image/index.js @@ -8,6 +8,7 @@ import ResizableBox from 'react-resizable-box'; * WordPress dependencies */ import { __ } from '@wordpress/i18n'; +import { fileUpload } from '@wordpress/utils'; import { Placeholder, Dashicon, Toolbar, DropZone, FormFileUpload } from '@wordpress/components'; /** @@ -108,37 +109,7 @@ registerBlockType( 'core/image', { const isResizable = [ 'wide', 'full' ].indexOf( align ) === -1; const uploadButtonProps = { isLarge: true }; const onSetHref = ( value ) => setAttributes( { href: value } ); - const uploadFromFiles = ( files ) => { - const media = files[ 0 ]; - - // Only allow image uploads - if ( ! /^image\//.test( media.type ) ) { - return; - } - - // Use File API to assign temporary URL from upload - setAttributes( { - url: window.URL.createObjectURL( media ), - } ); - - // Create upload payload - const data = new window.FormData(); - data.append( 'file', media ); - - new wp.api.models.Media().save( null, { - data: data, - contentType: false, - } ).done( ( savedMedia ) => { - setAttributes( { - id: savedMedia.id, - url: savedMedia.source_url, - } ); - } ).fail( () => { - // Reset to empty on failure. - // TODO: Better failure messaging - setAttributes( { url: null } ); - } ); - }; + const uploadFromFiles = ( event ) => fileUpload( event.target.files, setAttributes ); const controls = ( focus && ( @@ -183,7 +154,7 @@ registerBlockType( 'core/image', { uploadFromFiles( event.target.files ) } + onChange={ uploadFromFiles } accept="image/*" > { __( 'Upload' ) } From 726fe5ce28a6ec4502256df3da4e4b174bb43310 Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Tue, 8 Aug 2017 13:31:17 -0700 Subject: [PATCH 03/17] Remove console debug --- utils/fileupload.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/utils/fileupload.js b/utils/fileupload.js index 3cf3ca8e0beb2..ccbd921063b6b 100644 --- a/utils/fileupload.js +++ b/utils/fileupload.js @@ -1,7 +1,5 @@ export default function fileUpload( files, setAttributes ) { - console.log("Here!", files, setAttributes); - const media = files[ 0 ]; // Only allow image uploads From 35f8a610fb9f94ad3e495a51dcdb3b4748db5365 Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Wed, 9 Aug 2017 13:48:54 -0700 Subject: [PATCH 04/17] Add image upload button for gallery --- blocks/library/gallery/index.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/blocks/library/gallery/index.js b/blocks/library/gallery/index.js index 0bb77c8fb084a..b93ee31c570fb 100644 --- a/blocks/library/gallery/index.js +++ b/blocks/library/gallery/index.js @@ -2,7 +2,8 @@ * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { Toolbar, Placeholder } from '@wordpress/components'; +import { filesUpload } from '@wordpress/utils'; +import { Toolbar, Placeholder, FormFileUpload } from '@wordpress/components'; /** * Internal dependencies @@ -104,6 +105,10 @@ registerBlockType( 'core/gallery', { const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } ); const toggleImageCrop = () => setAttributes( { imageCrop: ! imageCrop } ); + const uploadFromFiles = ( event ) => { + filesUpload( event.target.files, setAttributes ); + }; + const controls = ( focus && ( @@ -134,6 +139,15 @@ registerBlockType( 'core/gallery', { icon="format-gallery" label={ __( 'Gallery' ) } className={ className }> + + { __( 'Upload' ) } + Date: Wed, 9 Aug 2017 14:58:32 -0700 Subject: [PATCH 05/17] Checkpoint: files uploading, but not editable in modal --- utils/fileupload.js | 51 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/utils/fileupload.js b/utils/fileupload.js index ccbd921063b6b..2c65cf5a78d3d 100644 --- a/utils/fileupload.js +++ b/utils/fileupload.js @@ -1,5 +1,9 @@ -export default function fileUpload( files, setAttributes ) { +export function fileUpload( files, setAttributes ) { + if ( files.length < 1 ) { + return; + } + const media = files[ 0 ]; // Only allow image uploads @@ -30,3 +34,48 @@ export default function fileUpload( files, setAttributes ) { setAttributes( { url: null } ); } ); } + +export function filesUpload( filesList, setAttributes ) { + const files = [ ...filesList ]; + if ( files.length < 1 ) { + return; + } + + const imgs = []; + files.forEach( ( media, idx ) => { + // only allow image uploads + if ( ! /^image\//.test( media.type ) ) { + return; + } + + // set temporary URL to create image placeholder + const tempUrl = window.URL.createObjectURL( media ); + const img = { url: tempUrl }; + imgs.push( img ); + setAttributes( { images: imgs } ); + + // create upload payload + const data = new window.FormData(); + data.append( 'file', media ); + + new wp.api.models.Media().save( null, { + data: data, + contentType: false, + } ).done( ( savedMedia ) => { + img.url = savedMedia.source_url; + setAttributes( { images: [ + ...imgs.slice( 0, idx ), + img, + ...imgs.slice( idx + 1 ), + ] } ); + } ).fail( () => { + // Reset to empty on failure. + // TODO: Better failure messaging + img.url = null; + setAttributes( { images: [ + ...imgs.slice( 0, idx ), + ...imgs.slice( idx + 1 ), + ] } ); + } ); + } ); +} From 6dbe9d0a47646d834d2783a0f1ab944dca1c7197 Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Wed, 9 Aug 2017 14:59:57 -0700 Subject: [PATCH 06/17] Add link and id to img object --- utils/fileupload.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utils/fileupload.js b/utils/fileupload.js index 2c65cf5a78d3d..7a3416e9cc6f7 100644 --- a/utils/fileupload.js +++ b/utils/fileupload.js @@ -62,6 +62,8 @@ export function filesUpload( filesList, setAttributes ) { data: data, contentType: false, } ).done( ( savedMedia ) => { + img.id = savedMedia.id; + img.link = savedMedia.link; img.url = savedMedia.source_url; setAttributes( { images: [ ...imgs.slice( 0, idx ), From 732c4b73b6d78a034167d55d1f598124803f7e9f Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Wed, 9 Aug 2017 16:51:28 -0700 Subject: [PATCH 07/17] Add id to gallery image --- blocks/library/gallery/gallery-image.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blocks/library/gallery/gallery-image.js b/blocks/library/gallery/gallery-image.js index 0d5ade2b58a74..7f113901f0807 100644 --- a/blocks/library/gallery/gallery-image.js +++ b/blocks/library/gallery/gallery-image.js @@ -10,7 +10,7 @@ export default function GalleryImage( props ) { break; } - const image = {; + const image = {; return (
From cbaa1ef2f6dfb5aaff6a96573213a6363630ffaf Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Wed, 9 Aug 2017 16:52:11 -0700 Subject: [PATCH 08/17] Switch media modal to use MediaUploadButton on edit --- blocks/library/gallery/index.js | 68 ++++++++++++++------------------- 1 file changed, 28 insertions(+), 40 deletions(-) diff --git a/blocks/library/gallery/index.js b/blocks/library/gallery/index.js index b93ee31c570fb..07e1854a713c9 100644 --- a/blocks/library/gallery/index.js +++ b/blocks/library/gallery/index.js @@ -3,14 +3,14 @@ */ import { __ } from '@wordpress/i18n'; import { filesUpload } from '@wordpress/utils'; -import { Toolbar, Placeholder, FormFileUpload } from '@wordpress/components'; +import { Dashicon, Toolbar, Placeholder, FormFileUpload } from '@wordpress/components'; /** * Internal dependencies */ import './editor.scss'; import './style.scss'; -import { registerBlockType } from '../../api'; +import { registerBlockType, source } from '../../api'; import MediaUploadButton from '../../media-upload-button'; import InspectorControls from '../../inspector-controls'; import RangeControl from '../../inspector-controls/range-control'; @@ -21,6 +21,7 @@ import BlockAlignmentToolbar from '../../block-alignment-toolbar'; import GalleryImage from './gallery-image'; import BlockDescription from '../../block-description'; +const { query, attr } = source; const MAX_COLUMNS = 8; const linkOptions = [ { value: 'attachment', label: __( 'Attachment Page' ) }, @@ -28,37 +29,6 @@ const linkOptions = [ { value: 'none', label: __( 'None' ) }, ]; -const editMediaLibrary = ( attributes, setAttributes ) => { - const frameConfig = { - frame: 'post', - title: __( 'Update Gallery media' ), - button: { - text: __( 'Select' ), - }, - multiple: true, - state: 'gallery-edit', - selection: new wp.media.model.Selection( attributes.images, { multiple: true } ), - }; - - const editFrame = wp.media( frameConfig ); - - // the frameConfig settings dont carry to other state modals - // so requires setting this attribute directory to not show settings - editFrame.state( 'gallery-edit' ).attributes.displaySettings = false; - - function updateFn() { - setAttributes( { - images: this.frame.state().attributes.library.models.map( ( a ) => { - return a.attributes; - } ), - } ); - } - - editFrame.on( 'insert', updateFn ); - editFrame.state( 'gallery-edit' ).on( 'update', updateFn ); - editFrame.open( 'gutenberg-gallery' ); -}; - function defaultColumnsNumber( attributes ) { return Math.min( 3, attributes.images.length ); } @@ -77,6 +47,11 @@ registerBlockType( 'core/gallery', { images: { type: 'array', default: [], + source: query( 'div.wp-block-gallery figure.blocks-gallery-image img', { + url: attr( 'src' ), + alt: attr( 'alt' ), + id: attr( 'data-id' ), + } ) }, columns: { type: 'number', @@ -105,6 +80,8 @@ registerBlockType( 'core/gallery', { const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } ); const toggleImageCrop = () => setAttributes( { imageCrop: ! imageCrop } ); + const onSelectImages = ( imgs ) => setAttributes( { images: imgs } ); + const uploadFromFiles = ( event ) => { filesUpload( event.target.files, setAttributes ); }; @@ -117,18 +94,29 @@ registerBlockType( 'core/gallery', { onChange={ updateAlignment } /> { !! images.length && ( - editMediaLibrary( attributes, setAttributes ), - } ] } /> + +
  • + + + +
  • +
    ) } ) ); if ( images.length === 0 ) { - const setMediaUrl = ( imgs ) => setAttributes( { images: imgs } ); const uploadButtonProps = { isLarge: true }; return [ @@ -150,7 +138,7 @@ registerBlockType( 'core/gallery', { Date: Wed, 9 Aug 2017 17:16:55 -0700 Subject: [PATCH 09/17] Value for MediaUploadButton needs to be array of ids --- blocks/library/gallery/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blocks/library/gallery/index.js b/blocks/library/gallery/index.js index 07e1854a713c9..36d00104ebc40 100644 --- a/blocks/library/gallery/index.js +++ b/blocks/library/gallery/index.js @@ -105,7 +105,7 @@ registerBlockType( 'core/gallery', { type="image" multiple="true" gallery="true" - value={ images } + value={ images.map( ( img ) => img.id ) } > From 3ac5392f3d3d50914fa7a4bd7caa9b1460274706 Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Wed, 9 Aug 2017 20:50:37 -0700 Subject: [PATCH 10/17] Fix core gallery test fixtures with simplified data --- blocks/test/fixtures/core__gallery.html | 2 +- blocks/test/fixtures/core__gallery.json | 24 ------------ .../test/fixtures/core__gallery.parsed.json | 37 +------------------ .../fixtures/core__gallery.serialized.html | 2 +- 4 files changed, 3 insertions(+), 62 deletions(-) diff --git a/blocks/test/fixtures/core__gallery.html b/blocks/test/fixtures/core__gallery.html index 2846a09568013..37aff1683b508 100644 --- a/blocks/test/fixtures/core__gallery.html +++ b/blocks/test/fixtures/core__gallery.html @@ -1,4 +1,4 @@ - +