From 70c5b7fc5b501110589b6c05052fbe6ad3d7f957 Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Thu, 25 May 2017 11:40:17 -0700 Subject: [PATCH] Switch on open to Edit Gallery with images Instead of reopening the Media Gallery, which the Media Upload Button does, when editing the gallery requires opening the Edit Gallery modal. Also requires setting selection and event which translates backbone data. Props to @georgeh, who I paired with developing See wp.media.editor from wp-includes/js/media-views.js --- blocks/library/gallery/index.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/blocks/library/gallery/index.js b/blocks/library/gallery/index.js index a5eab3deb8846a..856a65e09b2449 100644 --- a/blocks/library/gallery/index.js +++ b/blocks/library/gallery/index.js @@ -11,16 +11,30 @@ import GalleryImage from './gallery-image'; const { query } = hpq; -const openMediaLibrary = () => { +const editMediaLibrary = ( attributes, setAttributes ) => { const frameConfig = { - title: wp.i18n.__( 'Select or Upload a media' ), + frame: 'post', + title: wp.i18n.__( 'Update Gallery media' ), button: { text: wp.i18n.__( 'Select' ), }, multiple: true, + state: 'gallery-edit', + selection: new wp.media.model.Selection( attributes.images, { multiple: true } ), }; - wp.media( frameConfig ).open(); + const editFrame = wp.media( frameConfig ); + 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' ); }; /** @@ -43,6 +57,7 @@ registerBlock( 'core/gallery', { category: 'common', attributes: { + // TODO: fix when loading view with existing gallery images: query( 'div.blocks-gallery' ), }, @@ -50,7 +65,7 @@ registerBlock( 'core/gallery', { { icon: 'format-image', title: wp.i18n.__( 'Edit Gallery' ), - onClick: () => openMediaLibrary(), + onClick: editMediaLibrary, }, { icon: 'align-left',