From 646579000de27efdef6c142e977f28b899c81f09 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Fri, 9 Aug 2019 14:12:17 +1000 Subject: [PATCH 01/52] Basic gallery caption. --- package-lock.json | 4 +- packages/block-library/src/gallery/block.json | 7 +- packages/block-library/src/gallery/edit.js | 105 +++++++++++------- packages/block-library/src/gallery/save.js | 17 ++- packages/block-library/src/gallery/style.scss | 2 +- packages/blocks/src/api/validation.js | 1 - 6 files changed, 89 insertions(+), 47 deletions(-) diff --git a/package-lock.json b/package-lock.json index db4ab1e5b5f252..9d4cd468015bc9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7737,7 +7737,7 @@ "dependencies": { "minimist": { "version": "1.2.0", - "resolved": false, + "resolved": "", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true, "optional": true @@ -12590,7 +12590,7 @@ "dependencies": { "minimist": { "version": "1.2.0", - "resolved": false, + "resolved": "", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true, "optional": true diff --git a/packages/block-library/src/gallery/block.json b/packages/block-library/src/gallery/block.json index 6fa43d279a80e4..e677e7eb365628 100644 --- a/packages/block-library/src/gallery/block.json +++ b/packages/block-library/src/gallery/block.json @@ -6,7 +6,7 @@ "type": "array", "default": [ ], "source": "query", - "selector": "ul.wp-block-gallery .blocks-gallery-item", + "selector": ".wp-block-gallery .blocks-gallery-item", "query": { "url": { "source": "attribute", @@ -48,6 +48,11 @@ "columns": { "type": "number" }, + "galleryCaption": { + "type": "string", + "source": "html", + "selector": "figcaption" + }, "imageCrop": { "type": "boolean", "default": true diff --git a/packages/block-library/src/gallery/edit.js b/packages/block-library/src/gallery/edit.js index 129fc2343224a6..8a6c05c095c70b 100644 --- a/packages/block-library/src/gallery/edit.js +++ b/packages/block-library/src/gallery/edit.js @@ -19,6 +19,7 @@ import { BlockIcon, MediaPlaceholder, InspectorControls, + RichText, } from '@wordpress/block-editor'; import { Component } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; @@ -56,8 +57,10 @@ class GalleryEdit extends Component { this.onUploadError = this.onUploadError.bind( this ); this.setImageAttributes = this.setImageAttributes.bind( this ); this.setAttributes = this.setAttributes.bind( this ); + this.onFocusGalleryCaption = this.onFocusGalleryCaption.bind( this ); this.state = { + galleryCaptionFocused: false, selectedImage: null, attachmentCaptions: null, }; @@ -74,7 +77,6 @@ class GalleryEdit extends Component { ids: map( attributes.images, 'id' ), }; } - this.props.setAttributes( attributes ); } @@ -191,6 +193,14 @@ class GalleryEdit extends Component { return checked ? __( 'Thumbnails are cropped to align.' ) : __( 'Thumbnails are not cropped.' ); } + onFocusGalleryCaption() { + if ( ! this.state.galleryCaptionFocused ) { + this.setState( { + galleryCaptionFocused: true, + } ); + } + } + setImageAttributes( index, attributes ) { const { attributes: { images } } = this.props; const { setAttributes } = this; @@ -231,12 +241,16 @@ class GalleryEdit extends Component { captionSelected: false, } ); } + if ( ! this.props.isSelected && prevProps.isSelected && this.state.galleryCaptionFocused ) { + this.setState( { + galleryCaptionFocused: false, + } ); + } } render() { - const { attributes, isSelected, className, noticeUI } = this.props; - const { images, columns = defaultColumnsNumber( attributes ), align, imageCrop, linkTo } = attributes; - + const { attributes, isSelected, className, noticeUI, setAttributes } = this.props; + const { images, columns = defaultColumnsNumber( attributes ), align, galleryCaption, imageCrop, linkTo } = attributes; const hasImages = !! images.length; const mediaPlaceholder = ( @@ -291,42 +305,55 @@ class GalleryEdit extends Component { { noticeUI } - + { mediaPlaceholder } + { ( ! RichText.isEmpty( galleryCaption ) || isSelected ) && ( + setAttributes( { galleryCaption: value } ) } + isSelected={ this.state.galleryCaptionFocused } + inlineToolbar + /> + ) } + ); } diff --git a/packages/block-library/src/gallery/save.js b/packages/block-library/src/gallery/save.js index 638523c6554464..2c49141ea80e59 100644 --- a/packages/block-library/src/gallery/save.js +++ b/packages/block-library/src/gallery/save.js @@ -9,9 +9,10 @@ import { RichText } from '@wordpress/block-editor'; import { defaultColumnsNumber } from './shared'; export default function save( { attributes } ) { - const { images, columns = defaultColumnsNumber( attributes ), imageCrop, linkTo } = attributes; - return ( -