Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion blocks/build/theme-image/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'a084c1de89a76e2f384a');
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'ce8aef2bed48bcc7572c');
2 changes: 1 addition & 1 deletion blocks/build/theme-image/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion blocks/src/theme-image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"default": false
}
},
"textdomain": "happyprime",
"textdomain": "theme-image-block",
"editorScript": "file:../../build/theme-image/index.js",
"style": "file:./style.css"
}
55 changes: 24 additions & 31 deletions blocks/src/theme-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
TextControl,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useState, useEffect } from '@wordpress/element';
import { useState, useEffect, useRef } from '@wordpress/element';
import { image, caption as captionIcon } from '@wordpress/icons';

/**
Expand Down Expand Up @@ -52,6 +52,7 @@ function Edit({ attributes, setAttributes }) {
} = attributes;
const [svgContent, setSvgContent] = useState(null);
const [isEditingLink, setIsEditingLink] = useState(false);
const blockRef = useRef();

// Get registered theme images and styles from localized data.
const registeredImages = happyprime_themeimageblock_data?.images || [];
Expand Down Expand Up @@ -104,7 +105,7 @@ function Edit({ attributes, setAttributes }) {
imagePath &&
imagePath.toLowerCase().endsWith('.svg')
) {
const svgUrl = `${happyprimeData.themeUrl}/${imagePath}`;
const svgUrl = `${happyprime_themeimageblock_data.themeUrl}/${imagePath}`;
fetch(svgUrl)
.then((response) => response.text())
.then((svg) => {
Expand All @@ -120,12 +121,13 @@ function Edit({ attributes, setAttributes }) {
}, [inlineSVG, imagePath]);

const blockProps = useBlockProps({
ref: blockRef,
className: inlineSVG ? 'has-inline-svg' : '',
});

const imageUrl =
imagePath && happyprimeData?.themeUrl
? `${happyprimeData.themeUrl}/${imagePath}`
imagePath && happyprime_themeimageblock_data?.themeUrl
? `${happyprime_themeimageblock_data.themeUrl}/${imagePath}`
: '';
const isSVG = imagePath && imagePath.toLowerCase().endsWith('.svg');

Expand All @@ -143,9 +145,6 @@ function Edit({ attributes, setAttributes }) {
const styles = [];
if (width) {
styles.push(`width: ${width}`);
} else if (!width) {
// Default width for editor preview when not specified.
styles.push('width: 100%');
}
if (height) {
styles.push(`height: ${height}`);
Expand Down Expand Up @@ -193,18 +192,20 @@ function Edit({ attributes, setAttributes }) {
} else if (inlineSVG && processedSvg) {
// For inline SVG, apply dangerouslySetInnerHTML to link or wrapper
// to match server-side structure without extra figure wrapper.
const svgElement = (
<span
dangerouslySetInnerHTML={{ __html: processedSvg }}
style={{ display: 'contents' }}
/>
);
if (linkUrl) {
content = (
<a
href={linkUrl}
target={linkTarget}
rel={linkRel}
dangerouslySetInnerHTML={{ __html: processedSvg }}
/>
<a href={linkUrl} target={linkTarget} rel={linkRel}>
{svgElement}
</a>
);
} else {
// Will be applied to wrapper figure via blockProps below.
content = null;
content = svgElement;
}
} else {
// Build inline styles for img element.
Expand All @@ -216,13 +217,12 @@ function Edit({ attributes, setAttributes }) {
imgStyles.height = height;
}

const editorAlt = omitAltText ? '' : altText || currentImage?.alt || '';

const img = (
<img
src={imageUrl}
alt={
currentImage?.alt ||
__('Theme image preview', 'theme-image-block')
}
alt={editorAlt}
style={
Object.keys(imgStyles).length > 0 ? imgStyles : undefined
}
Expand All @@ -237,14 +237,7 @@ function Edit({ attributes, setAttributes }) {
);
}

// For inline SVG without link, apply HTML directly to wrapper.
const wrapperProps =
inlineSVG && processedSvg && !linkUrl
? {
...blockProps,
dangerouslySetInnerHTML: { __html: processedSvg },
}
: blockProps;
const wrapperProps = blockProps;

return (
<>
Expand Down Expand Up @@ -272,7 +265,9 @@ function Edit({ attributes, setAttributes }) {
<ToolbarButton
icon={captionIcon}
label={__('Add caption', 'theme-image-block')}
onClick={() => setAttributes({ showCaption: !showCaption })}
onClick={() =>
setAttributes({ showCaption: !showCaption })
}
isActive={showCaption}
/>
</BlockControls>
Expand All @@ -282,9 +277,7 @@ function Edit({ attributes, setAttributes }) {
<Popover
position="bottom center"
onClose={() => setIsEditingLink(false)}
anchor={document.querySelector(
'.wp-block-happyprime-theme-image'
)}
anchor={blockRef.current}
>
<LinkControl
value={{
Expand Down
27 changes: 15 additions & 12 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,22 @@ public static function render( $attributes, $content ): string {

if ( $link_url ) {
$content = '<a>' . $content . '</a>';
}

$html = new \WP_HTML_Tag_Processor( $content );
if ( $html->next_tag( array( 'tag_name' => 'a' ) ) ) {
$html->set_attribute( 'href', $link_url );
if ( $link_target ) {
$html->set_attribute( 'target', $link_target );
}
if ( $link_rel ) {
$html->set_attribute( 'rel', $link_rel );
$html = new \WP_HTML_Tag_Processor( $content );
if ( $html->next_tag( array( 'tag_name' => 'a' ) ) ) {
$html->set_attribute( 'href', $link_url );
if ( $link_target ) {
$html->set_attribute( 'target', $link_target );
}
if ( $link_rel ) {
$html->set_attribute( 'rel', $link_rel );
}
}

$content = $html->get_updated_html();
}

// This seems to be the best way to rewind and seek again? Seems strange.
$html = new \WP_HTML_Tag_Processor( $html->get_updated_html() );
$html = new \WP_HTML_Tag_Processor( $content );

if ( $html->next_tag( array( 'tag_name' => 'img' ) ) ) {
if ( ! empty( $inline_styles ) ) {
Expand All @@ -217,7 +218,9 @@ public static function render( $attributes, $content ): string {
$content .= sprintf( '<figcaption>%s</figcaption>', $caption );
}

$wrapper_attrs = array( 'class' => implode( ' ', $wrapper_classes ) );
$wrapper_attrs = ! empty( $wrapper_classes )
? array( 'class' => implode( ' ', $wrapper_classes ) )
: array();

return sprintf(
'<figure %s>%s</figure>',
Expand Down