Skip to content

Commit

Permalink
Merge pull request #43 from alleyinteractive/feature/issue-39/edit-di…
Browse files Browse the repository at this point in the history
…splay-options-without-post

Issue-39: Allow post display options to be edited without a post being selected
  • Loading branch information
mogmarsh authored Nov 14, 2023
2 parents 4410f03 + 9c7b9bc commit 674b115
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 71 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `Newsletter Builder` will be documented in this file.

## 0.3.4 - 2023-11-13

- Allow editing of post block attributes with no block selected. Allow order attribute to be edited.

## 0.3.3 - 2023-11-10

- Add URL override attribute and slotfill to post block
Expand Down
182 changes: 112 additions & 70 deletions blocks/post/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { ImagePicker, PostPicker } from '@alleyinteractive/block-editor-tools';
import { useSelect } from '@wordpress/data';
import {
Expand All @@ -14,6 +14,9 @@ import {
Spinner,
TextControl,
} from '@wordpress/components';
import { arrayMoveImmutable } from 'array-move';

import SortableList, { SortableItem, SortableKnob } from 'react-easy-sort';

/**
* React hook that is used to mark the block wrapper element.
Expand Down Expand Up @@ -92,6 +95,47 @@ export default function Edit({
});
};

const isShown = (item: string) => {
switch (item) {
case 'image':
return showImage;
case 'title':
return true;
case 'byline':
return showByline;
case 'excerpt':
return showExcerpt;
case 'content':
return showContent;
case 'cta':
return showCta;
default:
return false;
}
};

const toggleShown = (item: string) => {
switch (item) {
case 'image':
setAttributes({ showImage: !showImage });
break;
case 'byline':
setAttributes({ showByline: !showByline });
break;
case 'excerpt':
setAttributes({ showExcerpt: !showExcerpt });
break;
case 'content':
setAttributes({ showContent: !showContent });
break;
case 'cta':
setAttributes({ showCta: !showCta });
break;
default:
break;
}
};

const removeLinks = (html: string) => (
html ? html.replace(/<a[^>]*?>(.*?)<\/a>/gi, '$1') : ''
);
Expand Down Expand Up @@ -130,6 +174,15 @@ export default function Edit({

const titleClass = smallerFont ? 'post__title--small' : '';

const onSortEnd = (oldIndex: number, newIndex: number) => {
const newOrder = arrayMoveImmutable(
[...order as string[]], // eslint-disable-line camelcase
oldIndex,
newIndex,
);
setAttributes({ order: newOrder });
};

return (
<div {...useBlockProps()}>
{postId && !record ? (
Expand Down Expand Up @@ -225,59 +278,62 @@ export default function Edit({
return '';
}
})}
<InspectorControls>
{/* @ts-ignore */}
<PanelBody
title={__('Post', 'wp-newsletter-builder')}
initialOpen
</>
) : (
<div>
{/* @ts-ignore */}
<PostPicker
onUpdate={handleSelect}
allowedTypes={['post']}
onReset={() => handleSelect(0)}
params={{ after: cutoff.toISOString(), per_page: 20 }}
title={__('Please select a post', 'wp-newsletter-builder')}
value={postId}
// @ts-ignore
searchRender={PostPickerResult}
/>
</div>
)}
<InspectorControls>
{/* @ts-ignore */}
<PanelBody
title={__('Post', 'wp-newsletter-builder')}
initialOpen
>
<PanelRow>
<SortableList
onSortEnd={onSortEnd}
className="sortable"
lockAxis="y"
>
{/* @ts-ignore */}
<PanelRow>
{/* @ts-ignore */}
<CheckboxControl
label={__('Show image', 'wp-newsletter-builder')}
checked={showImage}
onChange={(value) => setAttributes({ showImage: value })}
/>
</PanelRow>
{/* @ts-ignore */}
<PanelRow>
{/* @ts-ignore */}
<CheckboxControl
label={__('Show Byline', 'wp-newsletter-builder')}
checked={showByline}
onChange={(value) => setAttributes({ showByline: value })}
/>
</PanelRow>
{/* @ts-ignore */}
<PanelRow>
{/* @ts-ignore */}
<CheckboxControl
label={__('Show dek', 'wp-newsletter-builder')}
checked={showExcerpt}
onChange={(value) => setAttributes({ showExcerpt: value })}
/>
</PanelRow>
{/* @ts-ignore */}
<PanelRow>
{/* @ts-ignore */}
<CheckboxControl
label={__('Show content', 'wp-newsletter-builder')}
checked={showContent}
onChange={(value) => setAttributes({ showContent: value })}
/>
</PanelRow>
{/* @ts-ignore */}
<PanelRow>
{/* @ts-ignore */}
<CheckboxControl
label={__('Show CTA', 'wp-newsletter-builder')}
checked={showCta}
onChange={(value) => setAttributes({ showCta: value })}
/>
</PanelRow>
</PanelBody>
{/* @ts-ignore */}
{order.map((item) => (
<SortableItem key={item}>
<div style={{ display: 'flex' }}>
<SortableKnob>
<span
aria-label={__('Move item', 'wp-newsletter-builder')}
style={{ width: '15px', height: '100%', cursor: 'move' }}
>
::
</span>
</SortableKnob>
{item === 'title' ? (
<p>{__('Title', 'wp-newsletter-builder')}</p>
) : (
<CheckboxControl
label={sprintf(__('Show %s', 'wp-newsletter-builder'), item)}
checked={isShown(item)}
onChange={() => toggleShown(item)}
/>
)}
</div>
</SortableItem>
))}
</SortableList>
</PanelRow>
</PanelBody>
{postId ? (
<>
<PanelBody
title={__('Override Image', 'wp-newsletter-builder')}
initialOpen
Expand All @@ -303,23 +359,9 @@ export default function Edit({
/>
</PanelRow>
</PanelBody>
</InspectorControls>
</>
) : (
<div>
{/* @ts-ignore */}
<PostPicker
onUpdate={handleSelect}
allowedTypes={['post']}
onReset={() => handleSelect(0)}
params={{ after: cutoff.toISOString(), per_page: 20 }}
title={__('Please select a post', 'wp-newsletter-builder')}
value={postId}
// @ts-ignore
searchRender={PostPickerResult}
/>
</div>
)}
</>
) : null}
</InspectorControls>
</div>
);
}
2 changes: 1 addition & 1 deletion plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Newsletter Builder
* Plugin URI: https://github.com/alleyinteractive/wp-newsletter-builder
* Description: Interface to manage email newsletters
* Version: 0.3.3
* Version: 0.3.4
* Author: Alley Interactive
* Author URI: https://github.com/alleyinteractive/wp-newsletter-builder
* Requires at least: 5.9
Expand Down

0 comments on commit 674b115

Please sign in to comment.