-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mockup for improved card management controls for Supernova and Supern…
…ova Items blocks - see #404
- Loading branch information
Showing
11 changed files
with
276 additions
and
50 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
packages/block-editor/src/components/custom-menu-item/editor-styles.scss
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.novablocks-navigable-menu { | ||
|
||
.components-button { | ||
height: auto; | ||
white-space: normal; | ||
text-align: left; | ||
|
||
p { | ||
margin: 0; | ||
} | ||
} | ||
|
||
.novablocks-navigable-menu__item-wrap { | ||
white-space: normal; | ||
} | ||
|
||
.novablocks-navigable-menu__item-label + .novablocks-navigable-menu__item-help { | ||
margin-top: 0.25em; | ||
} | ||
|
||
.novablocks-navigable-menu__item-help { | ||
font-size: 12px; | ||
opacity: 0.5; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/block-editor/src/components/custom-menu-item/index.js
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { MenuItem } from '@wordpress/components'; | ||
|
||
const CustomMenuItem = ( props ) => { | ||
const { help, children, ...passedProps } = props; | ||
|
||
return ( | ||
<MenuItem { ...passedProps }> | ||
<div className="novablocks-navigable-menu__item-wrap"> | ||
<p className="novablocks-navigable-menu__item-label">{ children }</p> | ||
{ help && <p className="novablocks-navigable-menu__item-help">{ help }</p> } | ||
</div> | ||
</MenuItem> | ||
) | ||
} | ||
|
||
export default CustomMenuItem; |
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
37 changes: 28 additions & 9 deletions
37
packages/block-editor/src/hooks/use-inner-blocks-count/index.js
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 |
---|---|---|
@@ -1,25 +1,44 @@ | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { useDispatch, useSelect } from "@wordpress/data"; | ||
import { useEffect } from "@wordpress/element"; | ||
import { useEffect, useRef } from "@wordpress/element"; | ||
|
||
import { useInnerBlocks } from "../../hooks"; | ||
|
||
const useInnerBlocksCount = ( clientId, postsToShow, innerBlockName, innerBlockAttributes ) => { | ||
const itemsCount = useSelect( select => select( 'core/block-editor' ).getBlockCount( clientId ), [ clientId ] ); | ||
const { replaceInnerBlocks } = useDispatch( 'core/block-editor' ); | ||
const { replaceInnerBlocks, updateBlockAttributes } = useDispatch( 'core/block-editor' ); | ||
const innerBlocks = useInnerBlocks( clientId ); | ||
|
||
return useEffect( () => { | ||
const newInnerBlocks = innerBlocks.slice( 0, postsToShow ); | ||
const innerBlocksCount = useRef( innerBlocks.length ); | ||
const postsToShowValue = useRef( postsToShow ); | ||
|
||
if ( postsToShow > itemsCount ) { | ||
for ( let i = 0; i < postsToShow - itemsCount; i++ ) { | ||
newInnerBlocks.push( createBlock( innerBlockName, innerBlockAttributes ) ); | ||
useEffect( () => { | ||
|
||
if ( innerBlocks.length !== postsToShow ) { | ||
|
||
if ( innerBlocks.length !== innerBlocksCount.current ) { | ||
// inner blocks changed | ||
innerBlocksCount.current = innerBlocks.length; | ||
postsToShowValue.current = innerBlocks.length; | ||
updateBlockAttributes( clientId, { postsToShow: innerBlocks.length } ); | ||
} else { | ||
// postsToShow changed | ||
innerBlocksCount.current = postsToShow; | ||
postsToShowValue.current = postsToShow; | ||
|
||
const newInnerBlocks = innerBlocks.slice( 0, postsToShow ); | ||
|
||
if ( postsToShow > itemsCount ) { | ||
for ( let i = 0; i < postsToShow - itemsCount; i++ ) { | ||
newInnerBlocks.push( createBlock( innerBlockName, innerBlockAttributes ) ); | ||
} | ||
} | ||
|
||
replaceInnerBlocks( clientId, newInnerBlocks ); | ||
} | ||
} | ||
|
||
replaceInnerBlocks( clientId, newInnerBlocks ); | ||
}, [ postsToShow ] ); | ||
}, [ postsToShow, innerBlocks ] ); | ||
}; | ||
|
||
export default useInnerBlocksCount; |
45 changes: 45 additions & 0 deletions
45
packages/block-library/src/blocks/supernova-item/block-controls.js
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { __ } from "@wordpress/i18n"; | ||
import { createBlock } from "@wordpress/blocks"; | ||
import { useDispatch, useSelect } from "@wordpress/data"; | ||
import { useCallback } from "@wordpress/element"; | ||
import { BlockControls } from "@wordpress/block-editor"; | ||
import { ToolbarButton, ToolbarGroup } from "@wordpress/components"; | ||
|
||
import { useInnerBlocks } from "@novablocks/block-editor"; | ||
|
||
const SupernovaItemBlockControls = ( props ) => { | ||
const { clientId, attributes } = props; | ||
const { replaceInnerBlocks } = useDispatch( 'core/block-editor' ); | ||
|
||
const parentClientId = useSelect( select => { | ||
const parents = select( 'core/block-editor' ).getBlockParents( clientId ).slice(); | ||
|
||
if ( parents.length ) { | ||
return parents[ parents.length - 1 ]; | ||
} | ||
|
||
return null; | ||
}, [ clientId ] ); | ||
|
||
const parentInnerBlocks = useInnerBlocks( parentClientId ); | ||
|
||
const addNewCard = useCallback( () => { | ||
const newInnerBlocks = parentInnerBlocks.slice(); | ||
const index = newInnerBlocks.findIndex( block => block.clientId === clientId ); | ||
const newBlock = createBlock( 'novablocks/supernova-item', attributes ); | ||
newInnerBlocks.splice( index + 1, 0, newBlock ); | ||
replaceInnerBlocks( parentClientId, newInnerBlocks ); | ||
}, [ clientId, parentClientId, attributes ] ); | ||
|
||
return ( | ||
<BlockControls> | ||
<ToolbarGroup label={ __( 'Cards', '__plugin_txtd' ) }> | ||
<ToolbarButton onClick={ addNewCard }> | ||
{ __( 'Duplicate Card', '__plugin_txtd' ) } | ||
</ToolbarButton> | ||
</ToolbarGroup> | ||
</BlockControls> | ||
) | ||
} | ||
|
||
export default SupernovaItemBlockControls; |
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
132 changes: 120 additions & 12 deletions
132
packages/block-library/src/blocks/supernova/block-controls.js
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
Oops, something went wrong.