From 83b36a816ff83388359cae1ab37dd687cfe17a58 Mon Sep 17 00:00:00 2001 From: paulineohlivd Date: Tue, 23 Feb 2021 10:54:45 +0100 Subject: [PATCH] feat: add alt to image --- src/Slide.js | 116 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 77 insertions(+), 39 deletions(-) diff --git a/src/Slide.js b/src/Slide.js index 9b9357a..df01e60 100644 --- a/src/Slide.js +++ b/src/Slide.js @@ -1,58 +1,84 @@ -const { createElement, Fragment } = wp.element -const { registerBlockType } = wp.blocks -const { InnerBlocks, MediaPlaceholder, MediaUpload, MediaUploadCheck, BlockControls, InspectorControls } = wp.editor -const { IconButton, Toolbar, PanelBody, PanelRow, ToggleControl, TextControl } = wp.components +const { createElement, Fragment } = wp.element; +const { registerBlockType } = wp.blocks; +const { + InnerBlocks, + MediaPlaceholder, + MediaUpload, + MediaUploadCheck, + BlockControls, + InspectorControls, +} = wp.editor; +const { + IconButton, + Toolbar, + PanelBody, + PanelRow, + ToggleControl, + TextControl, +} = wp.components; -registerBlockType('matiere-noir/slide', { - title: 'Slide', - description: 'Slide', - category: 'common', +registerBlockType("matiere-noir/slide", { + title: "Slide", + description: "Slide", + category: "common", attributes: { url: { - type: 'string' + type: "string", }, id: { - type: 'number' + type: "number", + }, + alt: { + type: "string", }, withLink: { - type: 'boolean', - default: false + type: "boolean", + default: false, }, href: { - type: 'string', - default: '' - } + type: "string", + default: "", + }, }, edit: ({ className, attributes, setAttributes }) => { - const { url, id } = attributes + const { url, id, alt } = attributes; - const onSelectMedia = media => { + const onSelectMedia = (media) => { if (!media || !media.url) { - setAttributes({ url: undefined, id: undefined }) - return + setAttributes({ url: undefined, id: undefined, alt: undefined }); + return; } setAttributes({ url: media.url, - id: media.id - }) - } + id: media.id, + alt: media.alt, + }); + }; const linkPanel = ( - setAttributes({ withLink: !attributes.withLink })} /> + setAttributes({ withLink: !attributes.withLink })} + /> {attributes.withLink ? ( - setAttributes({ href: value })} /> + setAttributes({ href: value })} + /> ) : null} - ) + ); const controls = ( @@ -65,7 +91,14 @@ registerBlockType('matiere-noir/slide', { onSelect={onSelectMedia} allowedTypes="image" value={id} - render={({ open }) => } + render={({ open }) => ( + + )} /> @@ -73,7 +106,7 @@ registerBlockType('matiere-noir/slide', { )} - ) + ); if (!url) { return ( @@ -81,8 +114,9 @@ registerBlockType('matiere-noir/slide', { {linkPanel} - ) + ); } return ( {linkPanel} -
+
{controls} - + {alt}
- ) + ); }, save: ({ className, attributes }) => { let mainContent = ( -
- +
+ {attributes.alt}
- ) + ); return attributes.withLink && attributes.href ? ( @@ -124,6 +162,6 @@ registerBlockType('matiere-noir/slide', { ) : ( mainContent - ) - } -}) + ); + }, +});