generated from alleyinteractive/create-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue-39: Allow post display options to be edited without a post being selected #43
Merged
mogmarsh
merged 3 commits into
develop
from
feature/issue-39/edit-display-options-without-post
Nov 14, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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. | ||
|
@@ -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; | ||
} | ||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also for the ternary's in this file we may consider logical nullish |
||
const removeLinks = (html: string) => ( | ||
html ? html.replace(/<a[^>]*?>(.*?)<\/a>/gi, '$1') : '' | ||
); | ||
|
@@ -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 ? ( | ||
|
@@ -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 | ||
|
@@ -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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went back and forth on this but something like this may be cleaner, but really not sure:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this suggestion, but this work should be temporary (see #48) so I'm going to leave it as is for now.