Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
394660c
Add placeholder for comments block if there is no context id
Jan 19, 2022
593811f
Update comment information of having empty object
Jan 19, 2022
e6f0879
Prevent no key on array errors and multiple comments per page overload
Jan 24, 2022
f4e6bae
Add threaded options
Jan 28, 2022
20d5223
Comment Template: Add support for threaded comments in comment placeh…
michalczaplinski Jan 29, 2022
cdddba0
Allow placeholder to work with border radius, remove background color…
Jan 31, 2022
e6bd79b
Use default avatars for placeholders
Jan 31, 2022
3bcfbf7
Comment Template: Add a rough version of nested placeholder comments.
michalczaplinski Feb 2, 2022
5ca5366
Allow background color again to avoid compatibility errors
Feb 3, 2022
24940d0
Add post author avatar block
carolinan Jan 27, 2022
2d6fd8c
Linting, fixtures, docs
carolinan Jan 27, 2022
bb0734c
Update border styles and wrapper width
carolinan Jan 28, 2022
1e32235
linting
carolinan Jan 28, 2022
1116795
more PHP linting
carolinan Jan 28, 2022
f7aec54
try to build docs again
carolinan Jan 28, 2022
d596593
Add placeholder svg and CSS
carolinan Jan 31, 2022
574669e
scss linting
carolinan Jan 31, 2022
6470851
Re-add the missing border style on the front (solid, dashed, dotted).
carolinan Jan 31, 2022
5fdb17a
PHP linting
carolinan Jan 31, 2022
14fb5f6
Fix class name and alt text inconsistencies, add aria-label
carolinan Feb 1, 2022
fac3a75
Move the block to the list of experimental blocks, improve avatar lin…
carolinan Feb 2, 2022
b4929ae
Initial commit
Feb 4, 2022
80a752c
Initial work on editor
Feb 7, 2022
b02aa9b
POC working, now time to refactor and check attributes
Feb 7, 2022
0446e90
Refactor edit.js code
Feb 8, 2022
7b60292
Add server side render part on php, still need to add more attributes
Feb 8, 2022
d3a1016
Refactor to not call both post and comment avatar
Feb 11, 2022
38e4f6f
Fix more rebasing issues
Feb 11, 2022
9fd357b
Fix more rebasing issues
Feb 11, 2022
27476da
Add links to the Avatar display on PHP
Feb 14, 2022
dc8b559
Give better name to hooks file
Feb 14, 2022
28bfe25
Fix duotone in editor (#38818)
jarcos Feb 15, 2022
1a97441
Fix duotone on frontend
Feb 15, 2022
587e66c
Fix border styles in frontend (#38831)
jarcos Feb 16, 2022
59e45bd
Small fixes to the markup
Feb 16, 2022
4caa746
Update fixtures
Feb 16, 2022
4421c29
Put same attributes like post author avatar PR
Feb 17, 2022
5fdb07b
Fix avatar crashing
Feb 17, 2022
e7f8b43
Add fallback to site editor avatar
Mar 2, 2022
712845b
Add experimental author selection
Mar 4, 2022
19e1ea0
Restore removed import
Mar 8, 2022
b1cf494
Add user selection
Mar 8, 2022
1e95930
Fix unwanted deletion
Mar 8, 2022
f7938f5
Fix unwanted fixture update
Mar 8, 2022
4173e6f
Fix docs typos
Mar 16, 2022
cb1f7a7
Add requested changes
cbravobernal Mar 17, 2022
693c74e
Added block creation e2e tests
cbravobernal Mar 17, 2022
01fc801
Retrieve unwanted remove on rebase
cbravobernal Mar 22, 2022
7adfbd5
Remove e2e test
ockham Mar 22, 2022
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
11 changes: 10 additions & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ Embed a simple audio player. ([Source](https://github.com/WordPress/gutenberg/tr
- **Supports:** align, anchor
- **Attributes:** autoplay, caption, id, loop, preload, src

## Avatar

Add a user's avatar. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/avatar))

- **Name:** core/avatar
- **Category:** theme
- **Supports:** align, color (~~background~~, ~~text~~), spacing (margin), ~~alignWide~~, ~~html~~
- **Attributes:** isLink, linkTarget, size, userId

## Reusable block

Create and save content to reuse across your site. Update the block, and the changes apply everywhere it’s used. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/block))
Expand Down Expand Up @@ -104,7 +113,7 @@ Add the avatar of this comment's author. ([Source](https://github.com/WordPress/

- **Name:** core/comment-author-avatar
- **Category:** theme
- **Supports:** color (background, ~~text~~), spacing (margin, padding), ~~html~~
- **Supports:** color (background, ~~text~~), spacing (margin, padding), ~~html~~, ~~inserter~~
- **Attributes:** height, width

## Comment Author Name
Expand Down
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function gutenberg_reregister_core_block_types() {
),
'block_names' => array(
'archives.php' => 'core/archives',
'avatar.php' => 'core/avatar',
'block.php' => 'core/block',
'calendar.php' => 'core/calendar',
'categories.php' => 'core/categories',
Expand Down
53 changes: 53 additions & 0 deletions packages/block-library/src/avatar/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/avatar",
"title": "Avatar",
"category": "theme",
"description": "Add a user's avatar.",
"textdomain": "default",
"attributes": {
"userId": {
"type": "number"
},
"size": {
"type": "number",
"default": 96
},
"isLink": {
"type": "boolean",
"default": false
},
"linkTarget": {
"type": "string",
"default": "_self"
}

},
"usesContext": [ "postType", "postId", "commentId" ],
"supports": {
"html": false,
"align": true,
"alignWide": false,
"spacing": {
"margin": true
},
"__experimentalBorder": {
"__experimentalSkipSerialization": true,
"radius": true,
"width": true,
"color": true,
"style": true,
"__experimentalDefaultControls": {
"radius": true
}
},
"color": {
"text": false,
"background": false,
"__experimentalDuotone": "img"
}
},
"editorStyle": "wp-block-avatar",
"style": "wp-block-avatar"
}
222 changes: 222 additions & 0 deletions packages/block-library/src/avatar/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import {
InspectorControls,
useBlockProps,
__experimentalUseBorderProps as useBorderProps,
} from '@wordpress/block-editor';
import {
PanelBody,
RangeControl,
ResizableBox,
ToggleControl,
} from '@wordpress/components';
import { __, isRTL } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { useUserAvatar, useCommentAvatar } from './hooks';
import UserControl from './user-control';

const AvatarInspectorControls = ( {
setAttributes,
avatar,
attributes,
selectUser,
} ) => (
<InspectorControls>
<PanelBody title={ __( 'Avatar Settings' ) }>
<RangeControl
label={ __( 'Image size' ) }
onChange={ ( newSize ) =>
setAttributes( {
size: newSize,
} )
}
min={ avatar.minSize }
max={ avatar.maxSize }
initialPosition={ attributes?.size }
value={ attributes?.size }
/>
{ selectUser && (
<UserControl
value={ attributes?.userId }
onChange={ ( value ) => {
setAttributes( {
userId: value,
} );
} }
/>
) }
<ToggleControl
label={ __( 'Link to user profile' ) }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this text may be incorrect for the comments, where the link is often an external website?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it. But also, I changed the website on the user settings and could not see any website link.

Do we know from where WordPress fetch that information?

Could we rephrase to 'Link to user website or author archive'?

onChange={ () =>
setAttributes( { isLink: ! attributes.isLink } )
}
checked={ attributes.isLink }
/>
{ attributes.isLink && (
<ToggleControl
label={ __( 'Open in new tab' ) }
onChange={ ( value ) =>
setAttributes( {
linkTarget: value ? '_blank' : '_self',
} )
}
checked={ attributes.linkTarget === '_blank' }
/>
) }
</PanelBody>
</InspectorControls>
);

const ResizableAvatar = ( {
setAttributes,
attributes,
avatar,
blockProps,
isSelected,
} ) => {
const borderProps = useBorderProps( attributes );
return (
<div { ...blockProps }>
<ResizableBox
size={ {
width: attributes.size,
height: attributes.size,
} }
showHandle={ isSelected }
onResizeStop={ ( event, direction, elt, delta ) => {
setAttributes( {
size: parseInt(
attributes.size + ( delta.height || delta.width ),
10
),
} );
} }
lockAspectRatio
enable={ {
top: false,
right: ! isRTL(),
bottom: true,
left: isRTL(),
} }
minWidth={ avatar.minSize }
maxWidth={ avatar.maxSize }
>
<img
src={ avatar.src }
alt={ avatar.alt }
{ ...borderProps }
className={ classnames(
'avatar',
'avatar-' + attributes.size,
'photo',
'wp-block-avatar__image',
borderProps.className
) }
style={ {
...borderProps.style, // Border radius, width and style.
} }
/>
</ResizableBox>
</div>
);
};
const CommentEdit = ( { attributes, context, setAttributes, isSelected } ) => {
const { commentId } = context;
const blockProps = useBlockProps();
const avatar = useCommentAvatar( { commentId } );
return (
<>
<AvatarInspectorControls
avatar={ avatar }
setAttributes={ setAttributes }
attributes={ attributes }
selectUser={ false }
/>
{ attributes.isLink ? (
<a
href="#avatar-pseudo-link"
className="wp-block-avatar__link"
onClick={ ( event ) => event.preventDefault() }
>
<ResizableAvatar
attributes={ attributes }
avatar={ avatar }
blockProps={ blockProps }
isSelected={ isSelected }
setAttributes={ setAttributes }
/>
</a>
) : (
<ResizableAvatar
attributes={ attributes }
avatar={ avatar }
blockProps={ blockProps }
isSelected={ isSelected }
setAttributes={ setAttributes }
/>
) }
</>
);
};

const UserEdit = ( { attributes, context, setAttributes, isSelected } ) => {
const { postId, postType } = context;
const avatar = useUserAvatar( {
userId: attributes?.userId,
postId,
postType,
} );
const blockProps = useBlockProps();
return (
<>
<AvatarInspectorControls
selectUser={ true }
attributes={ attributes }
avatar={ avatar }
setAttributes={ setAttributes }
/>
<div>
{ attributes.isLink ? (
<a
href="#avatar-pseudo-link"
className="wp-block-avatar__link"
onClick={ ( event ) => event.preventDefault() }
>
<ResizableAvatar
attributes={ attributes }
avatar={ avatar }
blockProps={ blockProps }
isSelected={ isSelected }
setAttributes={ setAttributes }
/>
</a>
) : (
<ResizableAvatar
attributes={ attributes }
avatar={ avatar }
blockProps={ blockProps }
isSelected={ isSelected }
setAttributes={ setAttributes }
/>
) }
</div>
</>
);
};

export default function Edit( props ) {
if ( props?.context?.commentId ) {
return <CommentEdit { ...props } />;
}
return <UserEdit { ...props } />;
Comment on lines +218 to +221

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CommentEdit and UserEdit share most of their logic, except for the part that gets the avatar based on either the comment ID, or the post ID and type. How about we extract the shared part into an AvatarEdit component, and move the avatar determination logic into Edit? Something like

Suggested change
if ( props?.context?.commentId ) {
return <CommentEdit { ...props } />;
}
return <UserEdit { ...props } />;
if ( props?.context?.commentId ) {
const { commentId } = props.context;
const avatar = useCommentAvatar( { commentId } );
return <AvatarEdit { avatar, ...props } />;
}
const { postId, postType } = props.context;
const avatar = useUserAvatar( {
userId: attributes?.userId,
postId,
postType,
} );
return <AvatarEdit { avatar, ...props } />;

I feel that that represents a bit better which parts of the logic are shared by the comment and user avatar cases, and which parts we need to differentiate them.

(I'd also consider making the ...props explicit, which I normally find scales better, as it leads to fewer surprises as the list of props that need to be passed changes.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we extract the shared part into an AvatarEdit component, and move the avatar determination logic into Edit?

Hey @ockham, for me that would be the ideal solution. 😄 However, I guess @c4rl0sbr4v0's decision of making two different components here was because React hooks cannot be used conditionally. 🤷

Another option that just came to my mind could be moving the conditional checking inside a useAvatar hook, maybe?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Comment Avatar and the User Avatar have different hooks to retrieve the data. So adding a conditional on useAvatar function would cause the React hooks error 😢

Screenshot 2022-03-17 at 11 21 23

}
3 changes: 3 additions & 0 deletions packages/block-library/src/avatar/editor.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.wp-block-avatar__image img {
width: 100%;
}
Loading