Skip to content
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

Add a dot before the parent block selector when there are unsaved changes in the Reusable block #1

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@wordpress/blocks": "file:../blocks",
"@wordpress/components": "file:../components",
"@wordpress/compose": "file:../compose",
"@wordpress/core-data": "file:../core-data",
Copy link

Choose a reason for hiding this comment

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

I think this dependency might be a bit of an issue, as core-data is a WordPress utility library, and the block editor package is supposed to be for general use even outside of WordPress.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I was not aware of this 😅

"@wordpress/data": "file:../data",
"@wordpress/data-controls": "file:../data-controls",
"@wordpress/deprecated": "file:../deprecated",
Expand Down
80 changes: 52 additions & 28 deletions packages/block-editor/src/components/block-parent-selector/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
Expand All @@ -6,6 +11,7 @@ import { ToolbarButton } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { useRef } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
Expand All @@ -25,33 +31,46 @@ export default function BlockParentSelector() {
const { selectBlock, toggleBlockHighlight } = useDispatch(
blockEditorStore
);
const { firstParentClientId, shouldHide, hasReducedUI } = useSelect(
( select ) => {
const {
getBlockName,
getBlockParents,
getSelectedBlockClientId,
getSettings,
} = select( blockEditorStore );
const { hasBlockSupport } = select( blocksStore );
const selectedBlockClientId = getSelectedBlockClientId();
const parents = getBlockParents( selectedBlockClientId );
const _firstParentClientId = parents[ parents.length - 1 ];
const parentBlockName = getBlockName( _firstParentClientId );
const _parentBlockType = getBlockType( parentBlockName );
const settings = getSettings();
return {
firstParentClientId: _firstParentClientId,
shouldHide: ! hasBlockSupport(
_parentBlockType,
'__experimentalParentSelector',
true
),
hasReducedUI: settings.hasReducedUI,
};
},
[]
);
const {
firstParentClientId,
shouldHide,
hasReducedUI,
reusableBlockHasEdits,
} = useSelect( ( select ) => {
const {
getBlockName,
getBlockParents,
getSelectedBlockClientId,
getSettings,
getBlockAttributes,
} = select( blockEditorStore );
const { hasBlockSupport } = select( blocksStore );
const selectedBlockClientId = getSelectedBlockClientId();
const parents = getBlockParents( selectedBlockClientId );
const _firstParentClientId = parents[ parents.length - 1 ];
const parentBlockName = getBlockName( _firstParentClientId );
const _parentBlockType = getBlockType( parentBlockName );
const settings = getSettings();
const parentBlockRef = getBlockAttributes( _firstParentClientId )?.ref;
const blockHasEdits = select( coreStore ).hasEditsForEntityRecord(
'postType',
'wp_block',
parentBlockRef
);
const _reusableBlockHasEdits =
blockHasEdits && parentBlockName === 'core/block';

return {
firstParentClientId: _firstParentClientId,
shouldHide: ! hasBlockSupport(
_parentBlockType,
'__experimentalParentSelector',
true
),
hasReducedUI: settings.hasReducedUI,
reusableBlockHasEdits: _reusableBlockHasEdits,
};
}, [] );
const blockInformation = useBlockDisplayInformation( firstParentClientId );

// Allows highlighting the parent block outline when focusing or hovering
Expand All @@ -71,13 +90,18 @@ export default function BlockParentSelector() {
return null;
}

const classes = classnames( 'block-editor-block-parent-selector', {
'block-has-changes': reusableBlockHasEdits,
} );

return (
<div
className="block-editor-block-parent-selector"
className={ classes }
key={ firstParentClientId }
ref={ nodeRef }
{ ...showMoversGestures }
>
{ reusableBlockHasEdits && <div className="has-changes-dot"></div> }
<ToolbarButton
className="block-editor-block-parent-selector__button"
onClick={ () => selectBlock( firstParentClientId ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,30 @@
border-radius: $radius-block-ui;
}
}

// styles for parent block selector when reusable block has changes
.block-editor-block-parent-selector.block-has-changes {
left: calc(-#{$grid-unit-60} - #{$grid-unit-30} - #{$border-width});

// increase the width of parent selector to make room for the dot
.block-editor-block-parent-selector__button {
width: $grid-unit-60 + $grid-unit-20;
}

// move the parent selector icon to right side to make room for the dot
.block-editor-block-icon {
margin-right: -15px;
}

// add the dot before the parent selector icon
.has-changes-dot {
position: absolute;
top: 50%;
transform: translateY(-50%);
background: var(--wp-admin-theme-color);
border-radius: 4px;
height: 8px;
width: 8px;
margin-left: 12px;
}
}
8 changes: 8 additions & 0 deletions packages/block-editor/src/components/block-toolbar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
}
}

.block-editor-block-contextual-toolbar.block-has-changes:not(.is-fixed) {
margin-left: calc(#{$grid-unit-60} + #{$grid-unit-30});

.show-icon-labels & {
margin-left: 0;
}
}

.block-editor-block-parent-selector {
position: absolute;
top: -$border-width;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import classnames from 'classnames';
import { __ } from '@wordpress/i18n';
import { hasBlockSupport, store as blocksStore } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
Expand All @@ -18,35 +19,47 @@ import BlockToolbar from '../block-toolbar';
import { store as blockEditorStore } from '../../store';

function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {
const { blockType, hasParents, showParentSelector } = useSelect(
( select ) => {
const {
getBlockName,
getBlockParents,
getSelectedBlockClientIds,
} = select( blockEditorStore );
const { getBlockType } = select( blocksStore );
const selectedBlockClientIds = getSelectedBlockClientIds();
const selectedBlockClientId = selectedBlockClientIds[ 0 ];
const parents = getBlockParents( selectedBlockClientId );
const firstParentClientId = parents[ parents.length - 1 ];
const parentBlockName = getBlockName( firstParentClientId );
const parentBlockType = getBlockType( parentBlockName );
const {
blockType,
hasParents,
showParentSelector,
reusableBlockHasEdits,
} = useSelect( ( select ) => {
const {
getBlockName,
getBlockParents,
getSelectedBlockClientIds,
getBlockAttributes,
} = select( blockEditorStore );
const { getBlockType } = select( blocksStore );
const selectedBlockClientIds = getSelectedBlockClientIds();
const selectedBlockClientId = selectedBlockClientIds[ 0 ];
const parents = getBlockParents( selectedBlockClientId );
const firstParentClientId = parents[ parents.length - 1 ];
const parentBlockName = getBlockName( firstParentClientId );
const parentBlockType = getBlockType( parentBlockName );
const parentBlockRef = getBlockAttributes( firstParentClientId )?.ref;
const blockHasEdits = select( coreStore ).hasEditsForEntityRecord(
'postType',
'wp_block',
parentBlockRef
);
const _reusableBlockHasEdits =
blockHasEdits && parentBlockName === 'core/block';

return {
blockType:
selectedBlockClientId &&
getBlockType( getBlockName( selectedBlockClientId ) ),
hasParents: parents.length,
showParentSelector: hasBlockSupport(
parentBlockType,
'__experimentalParentSelector',
true
),
};
},
[]
);
return {
blockType:
selectedBlockClientId &&
getBlockType( getBlockName( selectedBlockClientId ) ),
hasParents: parents.length,
showParentSelector: hasBlockSupport(
parentBlockType,
'__experimentalParentSelector',
true
),
reusableBlockHasEdits: _reusableBlockHasEdits,
};
}, [] );
if ( blockType ) {
if ( ! hasBlockSupport( blockType, '__experimentalToolbar', true ) ) {
return null;
Expand All @@ -57,6 +70,7 @@ function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {
const classes = classnames( 'block-editor-block-contextual-toolbar', {
'has-parent': hasParents && showParentSelector,
'is-fixed': isFixed,
'block-has-changes': reusableBlockHasEdits,
} );

return (
Expand Down