-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #379 from WordPress/add/image-block
Implement image block
- Loading branch information
Showing
4 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const { attr, html } = wp.blocks.query; | ||
const Editable = wp.blocks.Editable; | ||
|
||
wp.blocks.registerBlock( 'core/image', { | ||
title: wp.i18n.__( 'Image' ), | ||
|
||
icon: 'format-image', | ||
|
||
category: 'common', | ||
|
||
attributes: { | ||
url: attr( 'img', 'src' ), | ||
alt: attr( 'img', 'alt' ), | ||
caption: html( 'figcaption' ) | ||
}, | ||
|
||
edit( { attributes, isSelected, setAttributes } ) { | ||
const { url, alt, caption } = attributes; | ||
|
||
return ( | ||
<figure> | ||
<img src={ url } alt={ alt } /> | ||
{ caption || isSelected ? ( | ||
<Editable | ||
tagName="figcaption" | ||
placeholder={ wp.i18n.__( 'Write caption…' ) } | ||
value={ caption } | ||
onChange={ ( value ) => setAttributes( { caption: value } ) } /> | ||
) : null } | ||
</figure> | ||
); | ||
}, | ||
|
||
save( { attributes } ) { | ||
const { url, alt, caption } = attributes; | ||
const img = <img src={ url } alt={ alt } />; | ||
|
||
if ( ! caption ) { | ||
return img; | ||
} | ||
|
||
return ( | ||
<figure> | ||
{ img } | ||
<figcaption dangerouslySetInnerHTML={ { __html: caption } } /> | ||
</figure> | ||
); | ||
} | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import './freeform'; | ||
import './image'; | ||
import './text'; | ||
import './list'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters