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
6 changes: 6 additions & 0 deletions projects/packages/sharing-likes/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ back, which is why that one removal asks first instead of offering Undo. Deletin
a custom service leaves its ID in `sharing-services`, so `useCustomService` saves
the lists again without it.

In `BLOCK_CALL_TO_ACTION`, `TemplatePlacements` replaces the Site Editor link with
two cards per section, saving the template placements (see "Template placements"
below). Each save sends a one-item list, or an empty one, so ticking one card
unticks the other. It falls back to the Site Editor link only where `settings` does
not offer the key and no write is pending; while one is, the cards show disabled.

## Per-post switches

`Post_Likes_Switch` and `Post_Sharing_Switch` own the Likes and Sharing switches
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add cards to the React version of Settings > Sharing that place the Sharing Buttons and Like blocks before or after the content of posts and pages.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Button, ButtonLink, Notice, Stack, Text } from '@wordpress/ui';
import { useStatus } from '../data/queries';
import { useFeatureAction } from '../data/use-feature-action';
import { SECTION_GAP, SettingGroup } from './section-card';
import { TemplatePlacements } from './template-placements';
import type { Feature, SectionState } from '../types';
import type { JSX, ReactNode } from 'react';

Expand Down Expand Up @@ -91,14 +92,19 @@ export function FeatureVariant( {
switch ( state ) {
case 'block_call_to_action':
content = (
<SettingGroup>
<Text render={ <p /> }>{ copy.addBlock }</Text>
<div>
<ButtonLink variant="outline" href={ status?.site_editor_url ?? '' }>
{ __( 'Open Site Editor', 'jetpack-sharing-likes' ) }
</ButtonLink>
</div>
</SettingGroup>
<TemplatePlacements
feature={ feature }
fallback={
<SettingGroup>
<Text render={ <p /> }>{ copy.addBlock }</Text>
<div>
<ButtonLink variant="outline" href={ status?.site_editor_url ?? '' }>
{ __( 'Open Site Editor', 'jetpack-sharing-likes' ) }
</ButtonLink>
</div>
</SettingGroup>
}
/>
);
break;
case 'off':
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import type { TemplatePlacement } from '../types';
import type { JSX } from 'react';

export type PlacedBlock = 'sharing-buttons' | 'like';

// Ten lines of post content, as Newsletter's wireframes draw them.
const LINE_WIDTHS = [ 139.44, 145.32, 141.96, 143.64, 142.8, 144.48, 143.64, 139.44, 145.32, 96 ];
const LINE_GAP = 4.62;
const COLUMN_X = 74;
const TITLE_Y = 50;
const ROW_HEIGHT = 14;

/**
* Points of a five-pointed star, the Like button's icon.
*
* @param cx - Centre x.
* @param cy - Centre y.
* @param r - Outer radius.
* @return SVG polygon points.
*/
function starPoints( cx: number, cy: number, r: number ): string {
return Array.from( { length: 10 }, ( _, i ) => {
const radius = i % 2 ? r * 0.45 : r;
const angle = ( Math.PI / 5 ) * i - Math.PI / 2;
return `${ cx + radius * Math.cos( angle ) },${ cy + radius * Math.sin( angle ) }`;
} ).join( ' ' );
}

/**
* A row of share buttons.
*
* @param props - Props.
* @param props.y - Top of the row.
* @return Row.
*/
function SharingButtonsRow( { y }: { y: number } ): JSX.Element {
return (
<g>
{ [ 0, 1, 2, 3 ].map( index => (
<g key={ index }>
<rect
className="jetpack-sharing-likes__wireframe-block"
x={ COLUMN_X + index * 18 }
y={ y }
width={ ROW_HEIGHT }
height={ ROW_HEIGHT }
rx="3"
/>
<circle
className="jetpack-sharing-likes__wireframe-surface"
cx={ COLUMN_X + index * 18 + ROW_HEIGHT / 2 }
cy={ y + ROW_HEIGHT / 2 }
r="3"
/>
</g>
) ) }
</g>
);
}

/**
* A Like button and the avatars of a few readers who liked the post.
*
* @param props - Props.
* @param props.y - Top of the row.
* @return Row.
*/
function LikeRow( { y }: { y: number } ): JSX.Element {
const middle = y + ROW_HEIGHT / 2;

return (
<g>
<rect
className="jetpack-sharing-likes__wireframe-button"
x={ COLUMN_X }
y={ y }
width="38"
height={ ROW_HEIGHT }
rx="3"
/>
<polygon
className="jetpack-sharing-likes__wireframe-block"
points={ starPoints( COLUMN_X + 9, middle, 4 ) }
/>
<rect
className="jetpack-sharing-likes__wireframe-block"
x={ COLUMN_X + 16 }
y={ middle - 1.26 }
width="16"
height="2.52"
rx="1.26"
/>
{ [ 0, 1, 2 ].map( index => (
<circle
key={ index }
className="jetpack-sharing-likes__wireframe-avatar"
cx={ COLUMN_X + 50 + index * 9 }
cy={ middle }
r="6"
/>
) ) }
<rect
className="jetpack-sharing-likes__wireframe-line"
x={ COLUMN_X + 76 }
y={ middle - 1.26 }
width="44"
height="2.52"
rx="1.26"
/>
</g>
);
}

/**
* Wireframe of a post with the block before or after its content, drawn 294×192 to fill a `CheckboxCard`.
*
* @param props - Props.
* @param props.block - Block being placed.
* @param props.placement - Where it goes.
* @return Decorative SVG.
*/
export function PlacementIllustration( {
block,
placement,
}: {
block: PlacedBlock;
placement: TemplatePlacement;
} ): JSX.Element {
const blockFirst = placement === 'before_content';
const contentY = TITLE_Y + 16 + ( blockFirst ? ROW_HEIGHT + 12 : 0 );
const rowY = blockFirst ? TITLE_Y + 16 : contentY + LINE_WIDTHS.length * LINE_GAP + 8;
const Row = block === 'like' ? LikeRow : SharingButtonsRow;

return (
<svg
className="jetpack-sharing-likes__wireframe"
width="294"
height="192"
viewBox="0 0 294 192"
fill="none"
aria-hidden="true"
focusable="false"
>
<rect
className="jetpack-sharing-likes__wireframe-line"
x={ COLUMN_X }
y={ TITLE_Y }
width="110.88"
height="6.72"
rx="3.36"
/>
{ LINE_WIDTHS.map( ( width, index ) => (
<rect
key={ index }
className="jetpack-sharing-likes__wireframe-line"
x={ COLUMN_X }
y={ contentY + index * LINE_GAP }
width={ width }
height="2.52"
rx="1.26"
/>
) ) }
<Row y={ rowY } />
</svg>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import CheckboxCard, { CheckboxCardGrid } from '@automattic/jetpack-components/checkbox-card';
import { createInterpolateElement, useCallback } from '@wordpress/element';
import { __, _x } from '@wordpress/i18n';
import { Fieldset, Link, Text } from '@wordpress/ui';
import { useIsSaving, useSettings, useStatus } from '../data/queries';
import { useSaveSetting } from '../data/use-save-setting';
import { PlacementIllustration, type PlacedBlock } from './placement-illustrations';
import { SettingGroup } from './section-card';
import type { Feature, TemplatePlacement } from '../types';
import type { JSX, ReactNode } from 'react';

const SETTING_KEYS = {
sharing: 'sharing_buttons_auto_add',
likes: 'likes_auto_add',
} as const;

const PLACED_BLOCKS: Record< Feature, PlacedBlock > = {
sharing: 'sharing-buttons',
likes: 'like',
};

const PLACEMENTS: TemplatePlacement[] = [ 'before_content', 'after_content' ];

/**
* The copy for each feature, one complete sentence per string.
*
* @param feature - Feature.
* @return Copy.
*/
function copyFor( feature: Feature ) {
return {
legend:
feature === 'sharing'
? __( 'Add the Sharing Buttons block to posts and pages', 'jetpack-sharing-likes' )
: __( 'Add the Like block to posts and pages', 'jetpack-sharing-likes' ),
before: _x( 'Before the content', 'Where the block goes', 'jetpack-sharing-likes' ),
after: _x( 'After the content', 'Where the block goes', 'jetpack-sharing-likes' ),
};
}

/**
* Cards that add the feature's block before or after the content of single posts and pages.
*
* @param props - Props.
* @param props.feature - Feature.
* @param props.fallback - Shown where the settings do not offer the placements.
* @return Cards, or the fallback.
*/
export function TemplatePlacements( {
feature,
fallback,
}: {
feature: Feature;
fallback: ReactNode;
} ): JSX.Element {
const status = useStatus();
const key = SETTING_KEYS[ feature ];
const offered = useSettings()?.[ key ];
const selected = offered ?? [];
const saveSetting = useSaveSetting();
// While a write is pending the stored settings may not be what the server has yet. A feature action
// counts until settings are read again, so right after "Switch to the … block" the key can still be missing.
const saving = useIsSaving();

const handleChange = useCallback(
( name: string, next: boolean ) => {
// The block goes on one side only, so ticking a card replaces the other.
saveSetting( key, next ? [ name as TemplatePlacement ] : [] );
},
[ key, saveSetting ]
);

if ( ! offered && ! saving ) {
return <>{ fallback }</>;
}

const copy = copyFor( feature );
const block = PLACED_BLOCKS[ feature ];
const previewUrl = status?.site_editor_url ?? '';
const pageTemplateUrl = status?.page_template_editor_url ?? '';

return (
<Fieldset.Root className="jetpack-sharing-likes__placements" aria-busy={ ! offered }>
<SettingGroup>
<Fieldset.Legend>{ copy.legend }</Fieldset.Legend>
<Fieldset.Description>
{ __( 'Pick one place: before or after the content.', 'jetpack-sharing-likes' ) }
</Fieldset.Description>
<CheckboxCardGrid>
{ PLACEMENTS.map( placement => (
<CheckboxCard
key={ placement }
id={ `${ block }-${ placement.replace( '_', '-' ) }` }
name={ placement }
title={ placement === 'before_content' ? copy.before : copy.after }
illustration={ <PlacementIllustration block={ block } placement={ placement } /> }
checked={ selected.includes( placement ) }
onChange={ handleChange }
previewUrl={
// The template only has the block once the save has gone through.
selected.includes( placement ) && ! saving ? previewUrl : undefined
}
disabled={ ! offered }
/>
) ) }
</CheckboxCardGrid>
{ /* Not Fieldset.Details: the fieldset takes one description, and it would replace the hint above. */ }
<SettingGroup>
{ pageTemplateUrl && (
<Text render={ <p /> }>
{ createInterpolateElement(
__(
'Want pages to look different? <link>Edit the page template</link>.',
'jetpack-sharing-likes'
),
{ link: <Link href={ pageTemplateUrl } /> }
) }
</Text>
) }
<Text render={ <p /> }>
{ __(
'Once you’ve saved a template in the Site Editor, move or remove its blocks there: picking a place here won’t move a block that template already has, or bring back one you removed.',
'jetpack-sharing-likes'
) }
</Text>
</SettingGroup>
</SettingGroup>
</Fieldset.Root>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,26 @@ export function PlacementSection(): JSX.Element | null {
return null;
}

// A section that offers its block sits next to this one, and the block ignores these choices.
const offersBlock =
status.sharing.state === 'block_call_to_action' ||
( status.likes.state === 'block_call_to_action' && status.likes.supported );

return (
<SectionCard
id={ PLACEMENT_ANCHOR }
title={ heading }
description={ __(
'These choices apply to every feature named in the heading.',
'jetpack-sharing-likes'
) }
description={
<>
{ __(
'These choices apply to every feature named in the heading.',
'jetpack-sharing-likes'
) }
{ offersBlock &&
' ' +
__( 'Blocks you add to your templates aren’t affected.', 'jetpack-sharing-likes' ) }
</>
}
>
<AutoSaveFields fields={ fields } />
</SectionCard>
Expand Down
Loading
Loading