diff --git a/packages/block-editor/src/components/list-view/README.md b/packages/block-editor/src/components/list-view/README.md
index 25fb7b61ac6c0e..0c5f2a36156a57 100644
--- a/packages/block-editor/src/components/list-view/README.md
+++ b/packages/block-editor/src/components/list-view/README.md
@@ -1,31 +1,5 @@
-# List view
+# Experimental Off Canvas Editor
-The ListView component provides an overview of the hierarchical structure of all blocks in the editor. The blocks are presented vertically one below the other.
+The __ExperimentalOffCanvasEditor component is a modified ListView compoent. It provides an overview of the hierarchical structure of all blocks in the editor. The blocks are presented vertically one below the other. It enables editing of hierarchy and addition of elements in the block tree without selecting the block instance on the canvas.
-Blocks that have child blocks (such as group or column blocks) are presented with the parent at the top and the nested children below.
-
-In addition to presenting the structure of the blocks in the editor, the ListView component lets users navigate to each block by clicking on its line in the hierarchy tree. Multiple blocks at the same level of nesting can be selected by holding down the `SHIFT` key and clicking blocks within the list.
-
-
-
-
-## Table of contents
-
-1. [Development guidelines](#development-guidelines)
-2. [Related components](#related-components)
-
-## Development guidelines
-
-### Usage
-
-Renders a list view with default syles.
-
-```jsx
-import { ListView } from '@wordpress/block-editor';
-
-const MyNavigation = () => ;
-```
-
-## Related components
-
-Block Editor components are components that can be used to compose the UI of your block editor. Thus, they can only be used under a [BlockEditorProvider](https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/provider/README.md) in the components tree.
+It is an experimental component which may end up completely merged into the ListView component via configuration props.
diff --git a/packages/block-editor/src/components/list-view/appender.js b/packages/block-editor/src/components/list-view/appender.js
new file mode 100644
index 00000000000000..7ed5d3577506bc
--- /dev/null
+++ b/packages/block-editor/src/components/list-view/appender.js
@@ -0,0 +1,48 @@
+/**
+ * WordPress dependencies
+ */
+import { useSelect } from '@wordpress/data';
+import { forwardRef } from '@wordpress/element';
+/**
+ * Internal dependencies
+ */
+import { store as blockEditorStore } from '../../store';
+import Inserter from '../inserter';
+
+export const Appender = forwardRef( ( props, ref ) => {
+ const { hideInserter, clientId } = useSelect( ( select ) => {
+ const {
+ getTemplateLock,
+ __unstableGetEditorMode,
+ getSelectedBlockClientId,
+ } = select( blockEditorStore );
+
+ const _clientId = getSelectedBlockClientId();
+
+ return {
+ clientId: getSelectedBlockClientId(),
+ hideInserter:
+ !! getTemplateLock( _clientId ) ||
+ __unstableGetEditorMode() === 'zoom-out',
+ };
+ }, [] );
+
+ if ( hideInserter ) {
+ return null;
+ }
+
+ return (
+
+
+
+ );
+} );
diff --git a/packages/block-editor/src/components/list-view/block-contents.js b/packages/block-editor/src/components/list-view/block-contents.js
index 507e7575344ab3..c2cbd970b09b9b 100644
--- a/packages/block-editor/src/components/list-view/block-contents.js
+++ b/packages/block-editor/src/components/list-view/block-contents.js
@@ -7,7 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
-import { forwardRef } from '@wordpress/element';
+import { forwardRef, useEffect, useState } from '@wordpress/element';
/**
* Internal dependencies
@@ -15,6 +15,14 @@ import { forwardRef } from '@wordpress/element';
import ListViewBlockSelectButton from './block-select-button';
import BlockDraggable from '../block-draggable';
import { store as blockEditorStore } from '../../store';
+import { updateAttributes } from './update-attributes';
+import { LinkUI } from './link-ui';
+import { useInsertedBlock } from './use-inserted-block';
+
+const BLOCKS_WITH_LINK_UI_SUPPORT = [
+ 'core/navigation-link',
+ 'core/navigation-submenu',
+];
const ListViewBlockContents = forwardRef(
(
@@ -33,19 +41,51 @@ const ListViewBlockContents = forwardRef(
ref
) => {
const { clientId } = block;
-
- const { blockMovingClientId, selectedBlockInBlockEditor } = useSelect(
+ const [ isLinkUIOpen, setIsLinkUIOpen ] = useState();
+ const {
+ blockMovingClientId,
+ selectedBlockInBlockEditor,
+ lastInsertedBlockClientId,
+ } = useSelect(
( select ) => {
- const { hasBlockMovingClientId, getSelectedBlockClientId } =
- select( blockEditorStore );
+ const {
+ hasBlockMovingClientId,
+ getSelectedBlockClientId,
+ getLastInsertedBlocksClientIds,
+ } = select( blockEditorStore );
return {
blockMovingClientId: hasBlockMovingClientId(),
selectedBlockInBlockEditor: getSelectedBlockClientId(),
+ lastInsertedBlockClientId:
+ getLastInsertedBlocksClientIds()[ 0 ],
};
},
[ clientId ]
);
+ const {
+ insertedBlockAttributes,
+ insertedBlockName,
+ setInsertedBlockAttributes,
+ } = useInsertedBlock( lastInsertedBlockClientId );
+
+ const hasExistingLinkValue = insertedBlockAttributes?.id;
+
+ useEffect( () => {
+ if (
+ clientId === lastInsertedBlockClientId &&
+ BLOCKS_WITH_LINK_UI_SUPPORT?.includes( insertedBlockName ) &&
+ ! hasExistingLinkValue // don't re-show the Link UI if the block already has a link value.
+ ) {
+ setIsLinkUIOpen( true );
+ }
+ }, [
+ lastInsertedBlockClientId,
+ clientId,
+ insertedBlockName,
+ hasExistingLinkValue,
+ ] );
+
const isBlockMoveTarget =
blockMovingClientId && selectedBlockInBlockEditor === clientId;
@@ -62,26 +102,44 @@ const ListViewBlockContents = forwardRef(
: [ clientId ];
return (
-
- { ( { draggable, onDragStart, onDragEnd } ) => (
-
+ { isLinkUIOpen && (
+ setIsLinkUIOpen( false ) }
+ hasCreateSuggestion={ false }
+ onChange={ ( updatedValue ) => {
+ updateAttributes(
+ updatedValue,
+ setInsertedBlockAttributes,
+ insertedBlockAttributes
+ );
+ setIsLinkUIOpen( false );
+ } }
/>
) }
-
+
+ { ( { draggable, onDragStart, onDragEnd } ) => (
+
+ ) }
+
+ >
);
}
);
diff --git a/packages/block-editor/src/components/list-view/block-edit-button.js b/packages/block-editor/src/components/list-view/block-edit-button.js
new file mode 100644
index 00000000000000..6281134e8c95e3
--- /dev/null
+++ b/packages/block-editor/src/components/list-view/block-edit-button.js
@@ -0,0 +1,27 @@
+/**
+ * WordPress dependencies
+ */
+import { edit } from '@wordpress/icons';
+import { Button } from '@wordpress/components';
+import { useDispatch } from '@wordpress/data';
+import { forwardRef } from '@wordpress/element';
+
+/**
+ * Internal dependencies
+ */
+import { store as blockEditorStore } from '../../store';
+
+export default forwardRef( function BlockEditButton(
+ { clientId, ...props },
+ ref
+) {
+ const { selectBlock } = useDispatch( blockEditorStore );
+
+ const onClick = () => {
+ selectBlock( clientId );
+ };
+
+ return (
+
+ );
+} );
diff --git a/packages/block-editor/src/components/list-view/block-select-button.js b/packages/block-editor/src/components/list-view/block-select-button.js
index 39baf78d5a7a88..543823ab1ada25 100644
--- a/packages/block-editor/src/components/list-view/block-select-button.js
+++ b/packages/block-editor/src/components/list-view/block-select-button.js
@@ -79,7 +79,11 @@ function ListViewBlockSelectButton(
aria-hidden={ true }
>
-
+
{
if ( isSelected ) {
@@ -77,7 +77,6 @@ function ListViewBlock( {
[ isContentLocked, clientId, isSelected ]
);
- const canExpand = isContentLocked ? false : canEdit;
const isFirstSelectedBlock =
forceSelectionContentLock ||
( isSelected && selectedClientIds[ 0 ] === clientId );
@@ -89,20 +88,74 @@ function ListViewBlock( {
const { toggleBlockHighlight } = useDispatch( blockEditorStore );
const blockInformation = useBlockDisplayInformation( clientId );
- const blockName = useSelect(
- ( select ) => select( blockEditorStore ).getBlockName( clientId ),
+ const block = useSelect(
+ ( select ) => select( blockEditorStore ).getBlock( clientId ),
[ clientId ]
);
+ // If ListView has experimental features related to the Persistent List View,
+ // only focus the selected list item on mount; otherwise the list would always
+ // try to steal the focus from the editor canvas.
+ useEffect( () => {
+ if ( ! isTreeGridMounted && isSelected ) {
+ cellRef.current.focus();
+ }
+ }, [] );
+
+ const onMouseEnter = useCallback( () => {
+ setIsHovered( true );
+ toggleBlockHighlight( clientId, true );
+ }, [ clientId, setIsHovered, toggleBlockHighlight ] );
+ const onMouseLeave = useCallback( () => {
+ setIsHovered( false );
+ toggleBlockHighlight( clientId, false );
+ }, [ clientId, setIsHovered, toggleBlockHighlight ] );
+
+ const selectEditorBlock = useCallback(
+ ( event ) => {
+ selectBlock( event, clientId );
+ event.preventDefault();
+ },
+ [ clientId, selectBlock ]
+ );
+
+ const updateSelection = useCallback(
+ ( newClientId ) => {
+ selectBlock( undefined, newClientId );
+ },
+ [ selectBlock ]
+ );
+
+ const { isTreeGridMounted, expand, collapse, LeafMoreMenu } =
+ useListViewContext();
+
+ const toggleExpanded = useCallback(
+ ( event ) => {
+ // Prevent shift+click from opening link in a new window when toggling.
+ event.preventDefault();
+ event.stopPropagation();
+ if ( isExpanded === true ) {
+ collapse( clientId );
+ } else if ( isExpanded === false ) {
+ expand( clientId );
+ }
+ },
+ [ clientId, expand, collapse, isExpanded ]
+ );
+
+ const instanceId = useInstanceId( ListViewBlock );
+
+ if ( ! block ) {
+ return null;
+ }
+
// When a block hides its toolbar it also hides the block settings menu,
// since that menu is part of the toolbar in the editor canvas.
// List View respects this by also hiding the block settings menu.
- const showBlockActions = hasBlockSupport(
- blockName,
- '__experimentalToolbar',
- true
- );
- const instanceId = useInstanceId( ListViewBlock );
+ const showBlockActions =
+ !! block &&
+ hasBlockSupport( block.name, '__experimentalToolbar', true );
+
const descriptionId = `list-view-block-select-button__${ instanceId }`;
const blockPositionDescription = getBlockPositionDescription(
position,
@@ -133,8 +186,15 @@ function ListViewBlock( {
)
: __( 'Options' );
- const { isTreeGridMounted, expand, collapse } = useListViewContext();
+ const editAriaLabel = blockInformation
+ ? sprintf(
+ // translators: %s: The title of the block.
+ __( 'Edit %s block' ),
+ blockInformation.title
+ )
+ : __( 'Edit' );
+ const isEditable = !! block && block.name !== 'core/page-list-item';
const hasSiblings = siblingBlockCount > 0;
const hasRenderedMovers = showBlockMovers && hasSiblings;
const moverCellClassName = classnames(
@@ -147,51 +207,9 @@ function ListViewBlock( {
{ 'is-visible': isHovered || isFirstSelectedBlock }
);
- // If ListView has experimental features related to the Persistent List View,
- // only focus the selected list item on mount; otherwise the list would always
- // try to steal the focus from the editor canvas.
- useEffect( () => {
- if ( ! isTreeGridMounted && isSelected ) {
- cellRef.current.focus();
- }
- }, [] );
-
- const onMouseEnter = useCallback( () => {
- setIsHovered( true );
- toggleBlockHighlight( clientId, true );
- }, [ clientId, setIsHovered, toggleBlockHighlight ] );
- const onMouseLeave = useCallback( () => {
- setIsHovered( false );
- toggleBlockHighlight( clientId, false );
- }, [ clientId, setIsHovered, toggleBlockHighlight ] );
-
- const selectEditorBlock = useCallback(
- ( event ) => {
- selectBlock( event, clientId );
- event.preventDefault();
- },
- [ clientId, selectBlock ]
- );
-
- const updateSelection = useCallback(
- ( newClientId ) => {
- selectBlock( undefined, newClientId );
- },
- [ selectBlock ]
- );
-
- const toggleExpanded = useCallback(
- ( event ) => {
- // Prevent shift+click from opening link in a new window when toggling.
- event.preventDefault();
- event.stopPropagation();
- if ( isExpanded === true ) {
- collapse( clientId );
- } else if ( isExpanded === false ) {
- expand( clientId );
- }
- },
- [ clientId, expand, collapse, isExpanded ]
+ const listViewBlockEditClassName = classnames(
+ 'block-editor-list-view-block__menu-cell',
+ { 'is-visible': isHovered || isFirstSelectedBlock }
);
let colSpan;
@@ -206,10 +224,8 @@ function ListViewBlock( {
'is-first-selected': isFirstSelectedBlock,
'is-last-selected': isLastSelectedBlock,
'is-branch-selected': isBranchSelected,
- 'is-synced-branch': isSyncedBranch,
'is-dragging': isDragged,
'has-single-cell': ! showBlockActions,
- 'is-synced': blockInformation.isSynced,
} );
// Only include all selected blocks if the currently clicked on block
@@ -220,6 +236,10 @@ function ListViewBlock( {
? selectedClientIds
: [ clientId ];
+ const MoreMenuComponent = LeafMoreMenu
+ ? LeafMoreMenu
+ : BlockSettingsDropdown;
+
return (
{ ( { ref, tabIndex, onFocus } ) => (
{
+ event.preventDefault();
+ }
+ }
onToggleExpanded={ toggleExpanded }
isSelected={ isSelected }
position={ position }
@@ -304,26 +330,54 @@ function ListViewBlock( {
) }
{ showBlockActions && (
-
- { ( { ref, tabIndex, onFocus } ) => (
-
+ <>
+ { isEditable && (
+
+ { ( props ) => (
+
+ ) }
+
) }
-
+
+ { ( { ref, tabIndex, onFocus } ) => (
+ <>
+
+ >
+ ) }
+
+ >
) }
);
diff --git a/packages/block-editor/src/components/list-view/branch.js b/packages/block-editor/src/components/list-view/branch.js
index 5d202e177247bb..8b9b885866057d 100644
--- a/packages/block-editor/src/components/list-view/branch.js
+++ b/packages/block-editor/src/components/list-view/branch.js
@@ -11,7 +11,6 @@ import ListViewBlock from './block';
import { useListViewContext } from './context';
import { isClientIdSelected } from './utils';
import { store as blockEditorStore } from '../../store';
-import useBlockDisplayInformation from '../use-block-display-information';
/**
* Given a block, returns the total number of blocks in that subtree. This is used to help determine
@@ -92,31 +91,23 @@ function ListViewBranch( props ) {
isExpanded,
parentId,
shouldShowInnerBlocks = true,
- isSyncedBranch = false,
+ selectBlockInCanvas,
} = props;
- const parentBlockInformation = useBlockDisplayInformation( parentId );
- const syncedBranch = isSyncedBranch || !! parentBlockInformation?.isSynced;
-
- const canParentExpand = useSelect(
+ const isContentLocked = useSelect(
( select ) => {
- if ( ! parentId ) {
- return true;
- }
-
- const isContentLocked =
+ return !! (
+ parentId &&
select( blockEditorStore ).getTemplateLock( parentId ) ===
- 'contentOnly';
- const canEdit = select( blockEditorStore ).canEditBlock( parentId );
-
- return isContentLocked ? false : canEdit;
+ 'contentOnly'
+ );
},
[ parentId ]
);
const { expandedState, draggedClientIds } = useListViewContext();
- if ( ! canParentExpand ) {
+ if ( isContentLocked ) {
return null;
}
@@ -183,7 +174,7 @@ function ListViewBranch( props ) {
isExpanded={ shouldExpand }
listPosition={ nextPosition }
selectedClientIds={ selectedClientIds }
- isSyncedBranch={ syncedBranch }
+ selectBlockInCanvas={ selectBlockInCanvas }
/>
) }
{ ! showBlock && (
@@ -204,7 +195,7 @@ function ListViewBranch( props ) {
isBranchSelected={ isSelectedBranch }
selectedClientIds={ selectedClientIds }
isExpanded={ isExpanded }
- isSyncedBranch={ syncedBranch }
+ selectBlockInCanvas={ selectBlockInCanvas }
/>
) }
diff --git a/packages/block-editor/src/components/list-view/index.js b/packages/block-editor/src/components/list-view/index.js
index 7166b594c276c2..9cf3c59f8b632d 100644
--- a/packages/block-editor/src/components/list-view/index.js
+++ b/packages/block-editor/src/components/list-view/index.js
@@ -5,7 +5,11 @@ import {
useMergeRefs,
__experimentalUseFixedWindowList as useFixedWindowList,
} from '@wordpress/compose';
-import { __experimentalTreeGrid as TreeGrid } from '@wordpress/components';
+import {
+ __experimentalTreeGrid as TreeGrid,
+ __experimentalTreeGridRow as TreeGridRow,
+ __experimentalTreeGridCell as TreeGridCell,
+} from '@wordpress/components';
import { AsyncModeProvider, useSelect } from '@wordpress/data';
import {
useCallback,
@@ -28,6 +32,7 @@ import useListViewClientIds from './use-list-view-client-ids';
import useListViewDropZone from './use-list-view-drop-zone';
import useListViewExpandSelectedItem from './use-list-view-expand-selected-item';
import { store as blockEditorStore } from '../../store';
+import { Appender } from './appender';
const expanded = ( state, action ) => {
if ( Array.isArray( action.clientIds ) ) {
@@ -50,15 +55,24 @@ export const BLOCK_LIST_ITEM_HEIGHT = 36;
/**
* Show a hierarchical list of blocks.
*
- * @param {Object} props Components props.
- * @param {string} props.id An HTML element id for the root element of ListView.
- * @param {Array} props.blocks Custom subset of block client IDs to be used instead of the default hierarchy.
- * @param {boolean} props.showBlockMovers Flag to enable block movers
- * @param {boolean} props.isExpanded Flag to determine whether nested levels are expanded by default.
- * @param {Object} ref Forwarded ref
+ * @param {Object} props Components props.
+ * @param {string} props.id An HTML element id for the root element of ListView.
+ * @param {Array} props.blocks Custom subset of block client IDs to be used instead of the default hierarchy.
+ * @param {boolean} props.showBlockMovers Flag to enable block movers
+ * @param {boolean} props.isExpanded Flag to determine whether nested levels are expanded by default.
+ * @param {boolean} props.selectBlockInCanvas Flag to determine whether the list view should be a block selection mechanism.
+ * @param {Object} props.LeafMoreMenu Optional more menu substitution.
+ * @param {Object} ref Forwarded ref
*/
-function ListView(
- { id, blocks, showBlockMovers = false, isExpanded = false },
+function __ExperimentalOffCanvasEditor(
+ {
+ id,
+ blocks,
+ showBlockMovers = false,
+ isExpanded = false,
+ selectBlockInCanvas = true,
+ LeafMoreMenu,
+ },
ref
) {
const { clientIdsTree, draggedClientIds, selectedClientIds } =
@@ -97,9 +111,9 @@ function ListView(
setExpandedState,
} );
const selectEditorBlock = useCallback(
- ( event, clientId ) => {
- updateBlockSelection( event, clientId );
- setSelectedTreeId( clientId );
+ ( event, blockClientId ) => {
+ updateBlockSelection( event, blockClientId );
+ setSelectedTreeId( blockClientId );
},
[ setSelectedTreeId, updateBlockSelection ]
);
@@ -121,20 +135,26 @@ function ListView(
);
const expand = useCallback(
- ( clientId ) => {
- if ( ! clientId ) {
+ ( blockClientId ) => {
+ if ( ! blockClientId ) {
return;
}
- setExpandedState( { type: 'expand', clientIds: [ clientId ] } );
+ setExpandedState( {
+ type: 'expand',
+ clientIds: [ blockClientId ],
+ } );
},
[ setExpandedState ]
);
const collapse = useCallback(
- ( clientId ) => {
- if ( ! clientId ) {
+ ( blockClientId ) => {
+ if ( ! blockClientId ) {
return;
}
- setExpandedState( { type: 'collapse', clientIds: [ clientId ] } );
+ setExpandedState( {
+ type: 'collapse',
+ clientIds: [ blockClientId ],
+ } );
},
[ setExpandedState ]
);
@@ -170,8 +190,16 @@ function ListView(
expandedState,
expand,
collapse,
+ LeafMoreMenu,
} ),
- [ isMounted.current, draggedClientIds, expandedState, expand, collapse ]
+ [
+ isMounted.current,
+ draggedClientIds,
+ expandedState,
+ expand,
+ collapse,
+ LeafMoreMenu,
+ ]
);
return (
@@ -180,29 +208,54 @@ function ListView(
listViewRef={ elementRef }
blockDropTarget={ blockDropTarget }
/>
-
-
-
-
-
+
+
+
+
+
+
+ { ( treeGridCellProps ) => (
+
+ ) }
+
+ { ! clientIdsTree.length && (
+
+
+ { __(
+ 'Your menu is currently empty. Add your first menu item to get started.'
+ ) }
+
+
+ ) }
+
+
+
+
);
}
-export default forwardRef( ListView );
+
+export default forwardRef( __ExperimentalOffCanvasEditor );
diff --git a/packages/block-editor/src/components/list-view/leaf.js b/packages/block-editor/src/components/list-view/leaf.js
index 41bf4bc34cc665..7d74c85ffeb367 100644
--- a/packages/block-editor/src/components/list-view/leaf.js
+++ b/packages/block-editor/src/components/list-view/leaf.js
@@ -36,7 +36,11 @@ export default function ListViewLeaf( {
return (
{
+ const {
+ getBlock: _getBlock,
+ getBlockRootClientId,
+ getBlockTransformItems,
+ } = select( blockEditorStore );
+
+ return {
+ getBlock: _getBlock,
+ blockTransforms: getBlockTransformItems(
+ _getBlock( clientId ),
+ getBlockRootClientId( clientId )
+ ),
+ };
+ },
+ [ clientId ]
+ );
+
+ const { replaceBlock } = useDispatch( blockEditorStore );
+
+ const featuredBlocks = [
+ 'core/page-list',
+ 'core/site-logo',
+ 'core/social-links',
+ 'core/search',
+ ];
+
+ const transforms = blockTransforms.filter( ( item ) => {
+ return featuredBlocks.includes( item.name );
+ } );
+
+ if ( ! transforms?.length ) {
+ return null;
+ }
+
+ if ( ! clientId ) {
+ return null;
+ }
+
+ return (
+
+
+ { __( 'Transform' ) }
+
+
+ { transforms.map( ( item, index ) => {
+ return (
+
+ replaceBlock(
+ clientId,
+ switchToBlockType(
+ getBlock( clientId ),
+ item.name
+ )
+ )
+ }
+ className="link-control-transform__item"
+ >
+
+ { item.title }
+
+ );
+ } ) }
+
+
+ );
+}
+
+export function LinkUI( props ) {
+ const { label, url, opensInNewTab, type, kind } = props.link;
+ const link = {
+ url,
+ opensInNewTab,
+ title: label && stripHTML( label ),
+ };
+
+ return (
+
+ (
+
+ )
+ : null
+ }
+ />
+
+ );
+}
diff --git a/packages/block-editor/src/components/list-view/style.scss b/packages/block-editor/src/components/list-view/style.scss
index 0bedb39061af4c..e65646a318d3d6 100644
--- a/packages/block-editor/src/components/list-view/style.scss
+++ b/packages/block-editor/src/components/list-view/style.scss
@@ -1,412 +1,30 @@
-.block-editor-list-view-tree {
- width: 100%;
- border-collapse: collapse;
+.offcanvas-editor__appender .block-editor-inserter__toggle {
+ background-color: #1e1e1e;
+ color: #fff;
+ margin: $grid-unit-10 0 0 24px;
+ border-radius: 2px;
+ height: 24px;
+ min-width: 24px;
padding: 0;
- margin: 0;
- // Move upwards when in modal.
- .components-modal__content & {
- margin: (-$grid-unit-15) (-$grid-unit-15 * 0.5) 0;
- width: calc(100% + #{ $grid-unit-15 });
- }
-}
-
-.block-editor-list-view-leaf {
- // Use position relative for row animation.
- position: relative;
-
- // The background has to be applied to the td, not tr, or border-radius won't work.
- &.is-selected td {
+ &:hover,
+ &:focus {
background: var(--wp-admin-theme-color);
+ color: #fff;
}
- &.is-selected.is-synced td {
- background: var(--wp-block-synced-color);
- }
- &.is-synced:not(.is-selected) .block-editor-list-view-block-contents {
- &:hover,
- &:focus,
- .block-editor-block-icon {
- color: var(--wp-block-synced-color);
- }
-
- &:focus::after {
- box-shadow:
- inset 0 0 0 1px $white,
- 0 0 0 var(--wp-admin-border-width-focus) var(--wp-block-synced-color);
- }
- }
- &.is-selected .block-editor-list-view-block-contents,
- &.is-selected .components-button.has-icon {
- color: $white;
- }
- &.is-selected .block-editor-list-view-block-contents {
- // Hide selection styles while a user is dragging blocks/files etc.
- .is-dragging-components-draggable & {
- background: none;
- color: $gray-900;
- }
- }
- &.is-selected .block-editor-list-view-block-contents:focus {
- &::after {
- box-shadow:
- inset 0 0 0 1px $white,
- 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color);
- }
- }
- &.is-selected.is-synced .block-editor-list-view-block-contents:focus {
- &::after {
- box-shadow:
- inset 0 0 0 1px $white,
- 0 0 0 var(--wp-admin-border-width-focus) var(--wp-block-synced-color);
- }
- }
- &.is-selected .block-editor-list-view-block__menu:focus {
- box-shadow: inset 0 0 0 var(--wp-admin-border-width-focus) $white;
- }
-
- &.is-dragging {
- display: none;
- }
-
- // Border radius for corners of the selected item.
- &.is-first-selected td:first-child {
- border-top-left-radius: $radius-block-ui;
- }
- &.is-first-selected td:last-child {
- border-top-right-radius: $radius-block-ui;
- }
- &.is-last-selected td:first-child {
- border-bottom-left-radius: $radius-block-ui;
- }
- &.is-last-selected td:last-child {
- border-bottom-right-radius: $radius-block-ui;
- }
- &.is-branch-selected:not(.is-selected):not(.is-synced-branch) {
- background: rgba(var(--wp-admin-theme-color--rgb), 0.04);
- }
- &.is-synced-branch.is-branch-selected {
- background: rgba(var(--wp-block-synced-color--rgb), 0.04);
- }
- &.is-branch-selected.is-first-selected td:first-child {
- border-top-left-radius: $radius-block-ui;
- }
- &.is-branch-selected.is-first-selected td:last-child {
- border-top-right-radius: $radius-block-ui;
- }
- &[aria-expanded="false"] {
- &.is-branch-selected.is-first-selected td:first-child {
- border-top-left-radius: $radius-block-ui;
- }
- &.is-branch-selected.is-first-selected td:last-child {
- border-top-right-radius: $radius-block-ui;
- }
- &.is-branch-selected.is-last-selected td:first-child {
- border-bottom-left-radius: $radius-block-ui;
- }
- &.is-branch-selected.is-last-selected td:last-child {
- border-bottom-right-radius: $radius-block-ui;
- }
- }
- &.is-branch-selected:not(.is-selected) td {
- border-radius: 0;
- }
-
-
- // List View renders a fixed number of items and relies on each item having a fixed height of 36px.
- // If this value changes, we should also change the itemHeight value set in useFixedWindowList.
- // See: https://github.com/WordPress/gutenberg/pull/35230 for additional context.
- .block-editor-list-view-block-contents {
- display: flex;
- align-items: center;
- width: 100%;
- height: auto;
- padding: ($grid-unit-15 * 0.5) $grid-unit-05 ($grid-unit-15 * 0.5) 0;
- text-align: left;
- border-radius: $radius-block-ui;
- position: relative;
- white-space: nowrap;
-
- &.is-dropping-before::before {
- content: "";
- position: absolute;
- pointer-events: none;
- transition: border-color 0.1s linear, border-style 0.1s linear, box-shadow 0.1s linear;
- top: -2px;
- right: 0;
- left: 0;
- border-top: 4px solid var(--wp-admin-theme-color);
- }
-
- .components-modal__content & {
- padding-left: 0;
- padding-right: 0;
- }
- }
-
- .block-editor-list-view-block-contents:focus {
- box-shadow: none;
-
- &::after {
- content: "";
- position: absolute;
- top: 0;
- right: -(24px + 5px); // Icon size + padding.
- bottom: 0;
- left: 0;
- border-radius: inherit;
- box-shadow: inset 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color);
- z-index: 2;
- pointer-events: none;
-
- // Hide focus styles while a user is dragging blocks/files etc.
- .is-dragging-components-draggable & {
- box-shadow: none;
- }
- }
- }
-
- // Fix focus styling width when one row has fewer cells.
- &.has-single-cell .block-editor-list-view-block-contents:focus::after {
- right: 0;
- }
-
- .block-editor-list-view-block__menu:focus {
- box-shadow: inset 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color);
- z-index: 1;
-
- // Hide focus styles while a user is dragging blocks/files etc.
- .is-dragging-components-draggable & {
- box-shadow: none;
- }
- }
-
- &.is-visible .block-editor-list-view-block-contents {
- opacity: 1;
- @include edit-post__fade-in-animation;
- }
-
- .block-editor-block-icon {
- margin-right: $grid-unit-10;
- flex: 0 0 $icon-size;
- }
-
- .block-editor-list-view-block__menu-cell,
- .block-editor-list-view-block__mover-cell,
- .block-editor-list-view-block__contents-cell {
- padding-top: 0;
- padding-bottom: 0;
- }
-
- .block-editor-list-view-block__menu-cell,
- .block-editor-list-view-block__mover-cell {
- line-height: 0;
- width: $button-size;
- vertical-align: middle;
- @include reduce-motion("transition");
-
- > * {
- will-change: opacity;
- opacity: 0;
- }
-
- // Show on hover, visible, and show above to keep the hit area size.
- &:hover,
- &.is-visible {
- > * {
- opacity: 1;
- @include edit-post__fade-in-animation;
- }
- }
-
- &,
- .components-button.has-icon {
- width: 24px;
- min-width: 24px;
- padding: 0;
- }
- }
-
- .block-editor-list-view-block__menu-cell {
- padding-right: $grid-unit-05;
-
- .components-button.has-icon {
- height: 24px;
- }
- }
-
- .block-editor-list-view-block__mover-cell-alignment-wrapper {
- display: flex;
- height: 100%;
- flex-direction: column;
- align-items: center;
- }
-
- // Keep the tap target large but the focus target small.
- .block-editor-block-mover-button {
- position: relative;
- width: $button-size;
- height: $button-size-small;
-
- // Position the icon.
- svg {
- position: relative;
- height: $button-size-small;
- }
-
- &.is-up-button {
- margin-top: -$grid-unit-15 * 0.5;
- align-items: flex-end;
- svg {
- bottom: -$grid-unit-05;
- }
- }
-
- &.is-down-button {
- margin-bottom: -$grid-unit-15 * 0.5;
- align-items: flex-start;
- svg {
- top: -$grid-unit-05;
- }
- }
-
- // Tweak size and position of focus ring.
- &::before {
- height: 16px;
- min-width: 100%;
- left: 0;
- right: 0;
- }
- }
-
- .block-editor-inserter__toggle {
- background: $gray-900;
- color: $white;
- height: $grid-unit-30;
- margin: 6px 6px 6px 1px;
- min-width: $grid-unit-30;
-
- &:active {
- color: $white;
- }
- }
-
- .block-editor-list-view-block-select-button__label-wrapper {
- min-width: 120px;
- }
-
- .block-editor-list-view-block-select-button__title {
- flex: 1;
- position: relative;
-
- .components-truncate {
- position: absolute;
- width: 100%;
- transform: translateY(-50%);
- }
- }
-
- .block-editor-list-view-block-select-button__anchor-wrapper {
- position: relative;
- max-width: min(110px, 40%);
- width: 100%;
- }
-
- .block-editor-list-view-block-select-button__anchor {
- position: absolute;
- right: 0;
- transform: translateY(-50%);
- background: rgba($black, 0.1);
- border-radius: $radius-block-ui;
- padding: 2px 6px;
- max-width: 100%;
- box-sizing: border-box;
- }
-
- &.is-selected .block-editor-list-view-block-select-button__anchor {
- background: rgba($black, 0.3);
- }
-
- .block-editor-list-view-block-select-button__lock {
- line-height: 0;
- }
-}
-
-.block-editor-list-view-block-select-button__description,
-.block-editor-list-view-appender__description {
- display: none;
}
-.block-editor-list-view-block__contents-cell,
-.block-editor-list-view-appender__cell {
- .block-editor-list-view-block__contents-container,
- .block-editor-list-view-appender__container {
- display: flex;
- }
+.offcanvas-editor-list-view-tree-wrapper {
+ max-width: 100%;
+ overflow-x: auto;
}
-// Chevron container metrics.
-.block-editor-list-view__expander {
- height: $icon-size;
- margin-left: $grid-unit-05;
- width: $icon-size;
+.offcanvas-editor-list-view-leaf {
+ display: block;
+ // sidebar width - tab panel padding
+ max-width: $sidebar-width - (2 * $grid-unit-20);
}
-// First level of indentation is aria-level 2, max indent is 8.
-// Indent is a full icon size, plus 4px which optically aligns child icons to the text label above.
-$block-navigation-max-indent: 8;
-.block-editor-list-view-leaf[aria-level] .block-editor-list-view__expander {
- margin-left: ( $icon-size ) * $block-navigation-max-indent + 4 * ( $block-navigation-max-indent - 1 );
-}
-
-.block-editor-list-view-leaf:not([aria-level="1"]) {
- .block-editor-list-view__expander {
- margin-right: 4px;
- }
+.offcanvas-editor-list-view-is-empty {
+ margin-left: $grid-unit-20;
}
-
-@for $i from 0 to $block-navigation-max-indent {
- .block-editor-list-view-leaf[aria-level="#{ $i + 1 }"] .block-editor-list-view__expander {
- @if $i - 1 >= 0 {
- margin-left: ( $icon-size * $i ) + 4 * ($i - 1);
- }
- @else {
- margin-left: ( $icon-size * $i );
- }
- }
-}
-
-.block-editor-list-view-leaf .block-editor-list-view__expander {
- visibility: hidden;
-}
-
-// Point downwards when open.
-.block-editor-list-view-leaf[aria-expanded="true"] .block-editor-list-view__expander svg {
- visibility: visible;
- transition: transform 0.2s ease;
- transform: rotate(90deg);
- @include reduce-motion("transition");
-}
-
-// Point rightwards when closed
-.block-editor-list-view-leaf[aria-expanded="false"] .block-editor-list-view__expander svg {
- visibility: visible;
- transform: rotate(0deg);
- transition: transform 0.2s ease;
- @include reduce-motion("transition");
-}
-
-.block-editor-list-view-drop-indicator {
- pointer-events: none;
-
- .block-editor-list-view-drop-indicator__line {
- background: var(--wp-admin-theme-color);
- height: $border-width;
- }
-}
-
-.block-editor-list-view-placeholder {
- padding: 0;
- margin: 0;
- height: 36px;
-}
-
diff --git a/packages/block-editor/src/components/list-view/test/use-inserted-block.js b/packages/block-editor/src/components/list-view/test/use-inserted-block.js
new file mode 100644
index 00000000000000..f4e6746581e008
--- /dev/null
+++ b/packages/block-editor/src/components/list-view/test/use-inserted-block.js
@@ -0,0 +1,108 @@
+/**
+ * Internal dependencies
+ */
+import { useInsertedBlock } from '../use-inserted-block';
+
+/**
+ * WordPress dependencies
+ */
+import { useDispatch, useSelect } from '@wordpress/data';
+
+/**
+ * External dependencies
+ */
+import { act, renderHook } from '@testing-library/react';
+
+jest.mock( '@wordpress/data/src/components/use-select', () => {
+ // This allows us to tweak the returned value on each test.
+ const mock = jest.fn();
+ return mock;
+} );
+
+jest.mock( '@wordpress/data/src/components/use-dispatch', () => ( {
+ useDispatch: jest.fn(),
+} ) );
+
+describe( 'useInsertedBlock', () => {
+ const mockUpdateBlockAttributes = jest.fn();
+
+ it( 'returns undefined values when called without a block clientId', () => {
+ useSelect.mockImplementation( () => ( {
+ insertedBlockAttributes: {
+ 'some-attribute': 'some-value',
+ },
+ insertedBlockName: 'core/navigation-link',
+ } ) );
+
+ useDispatch.mockImplementation( () => ( {
+ updateBlockAttributes: mockUpdateBlockAttributes,
+ } ) );
+
+ const { result } = renderHook( () => useInsertedBlock() );
+
+ const {
+ insertedBlockName,
+ insertedBlockAttributes,
+ setInsertedBlockAttributes,
+ } = result.current;
+
+ expect( insertedBlockName ).toBeUndefined();
+ expect( insertedBlockAttributes ).toBeUndefined();
+ expect(
+ setInsertedBlockAttributes( { 'some-attribute': 'new-value' } )
+ ).toBeUndefined();
+ } );
+
+ it( 'returns name and attributes when called with a block clientId', () => {
+ useSelect.mockImplementation( () => ( {
+ insertedBlockAttributes: {
+ 'some-attribute': 'some-value',
+ },
+ insertedBlockName: 'core/navigation-link',
+ } ) );
+
+ useDispatch.mockImplementation( () => ( {
+ updateBlockAttributes: mockUpdateBlockAttributes,
+ } ) );
+
+ const { result } = renderHook( () =>
+ useInsertedBlock( 'some-client-id-here' )
+ );
+
+ const { insertedBlockName, insertedBlockAttributes } = result.current;
+
+ expect( insertedBlockName ).toBe( 'core/navigation-link' );
+ expect( insertedBlockAttributes ).toEqual( {
+ 'some-attribute': 'some-value',
+ } );
+ } );
+
+ it( 'dispatches updateBlockAttributes on provided client ID with new attributes when setInsertedBlockAttributes is called', () => {
+ useSelect.mockImplementation( () => ( {
+ insertedBlockAttributes: {
+ 'some-attribute': 'some-value',
+ },
+ insertedBlockName: 'core/navigation-link',
+ } ) );
+
+ useDispatch.mockImplementation( () => ( {
+ updateBlockAttributes: mockUpdateBlockAttributes,
+ } ) );
+
+ const clientId = '123456789';
+
+ const { result } = renderHook( () => useInsertedBlock( clientId ) );
+
+ const { setInsertedBlockAttributes } = result.current;
+
+ act( () => {
+ setInsertedBlockAttributes( {
+ 'some-attribute': 'new-value',
+ } );
+ } );
+
+ expect( mockUpdateBlockAttributes ).toHaveBeenCalledWith( clientId, {
+ 'some-attribute': 'new-value',
+ } );
+ } );
+} );
diff --git a/packages/block-editor/src/components/list-view/update-attributes.js b/packages/block-editor/src/components/list-view/update-attributes.js
new file mode 100644
index 00000000000000..5133cae3878338
--- /dev/null
+++ b/packages/block-editor/src/components/list-view/update-attributes.js
@@ -0,0 +1,99 @@
+/**
+ * WordPress dependencies
+ */
+import { escapeHTML } from '@wordpress/escape-html';
+import { safeDecodeURI } from '@wordpress/url';
+
+/**
+ * @typedef {'post-type'|'custom'|'taxonomy'|'post-type-archive'} WPNavigationLinkKind
+ */
+/**
+ * Navigation Link Block Attributes
+ *
+ * @typedef {Object} WPNavigationLinkBlockAttributes
+ *
+ * @property {string} [label] Link text.
+ * @property {WPNavigationLinkKind} [kind] Kind is used to differentiate between term and post ids to check post draft status.
+ * @property {string} [type] The type such as post, page, tag, category and other custom types.
+ * @property {string} [rel] The relationship of the linked URL.
+ * @property {number} [id] A post or term id.
+ * @property {boolean} [opensInNewTab] Sets link target to _blank when true.
+ * @property {string} [url] Link href.
+ * @property {string} [title] Link title attribute.
+ */
+/**
+ * Link Control onChange handler that updates block attributes when a setting is changed.
+ *
+ * @param {Object} updatedValue New block attributes to update.
+ * @param {Function} setAttributes Block attribute update function.
+ * @param {WPNavigationLinkBlockAttributes} blockAttributes Current block attributes.
+ *
+ */
+
+export const updateAttributes = (
+ updatedValue = {},
+ setAttributes,
+ blockAttributes = {}
+) => {
+ const {
+ label: originalLabel = '',
+ kind: originalKind = '',
+ type: originalType = '',
+ } = blockAttributes;
+
+ const {
+ title: newLabel = '', // the title of any provided Post.
+ url: newUrl = '',
+ opensInNewTab,
+ id,
+ kind: newKind = originalKind,
+ type: newType = originalType,
+ } = updatedValue;
+
+ const newLabelWithoutHttp = newLabel.replace( /http(s?):\/\//gi, '' );
+ const newUrlWithoutHttp = newUrl.replace( /http(s?):\/\//gi, '' );
+
+ const useNewLabel =
+ newLabel &&
+ newLabel !== originalLabel &&
+ // LinkControl without the title field relies
+ // on the check below. Specifically, it assumes that
+ // the URL is the same as a title.
+ // This logic a) looks suspicious and b) should really
+ // live in the LinkControl and not here. It's a great
+ // candidate for future refactoring.
+ newLabelWithoutHttp !== newUrlWithoutHttp;
+
+ // Unfortunately this causes the escaping model to be inverted.
+ // The escaped content is stored in the block attributes (and ultimately in the database),
+ // and then the raw data is "recovered" when outputting into the DOM.
+ // It would be preferable to store the **raw** data in the block attributes and escape it in JS.
+ // Why? Because there isn't one way to escape data. Depending on the context, you need to do
+ // different transforms. It doesn't make sense to me to choose one of them for the purposes of storage.
+ // See also:
+ // - https://github.com/WordPress/gutenberg/pull/41063
+ // - https://github.com/WordPress/gutenberg/pull/18617.
+ const label = useNewLabel
+ ? escapeHTML( newLabel )
+ : originalLabel || escapeHTML( newUrlWithoutHttp );
+
+ // In https://github.com/WordPress/gutenberg/pull/24670 we decided to use "tag" in favor of "post_tag"
+ const type = newType === 'post_tag' ? 'tag' : newType.replace( '-', '_' );
+
+ const isBuiltInType =
+ [ 'post', 'page', 'tag', 'category' ].indexOf( type ) > -1;
+
+ const isCustomLink =
+ ( ! newKind && ! isBuiltInType ) || newKind === 'custom';
+ const kind = isCustomLink ? 'custom' : newKind;
+
+ setAttributes( {
+ // Passed `url` may already be encoded. To prevent double encoding, decodeURI is executed to revert to the original string.
+ ...( newUrl && { url: encodeURI( safeDecodeURI( newUrl ) ) } ),
+ ...( label && { label } ),
+ ...( undefined !== opensInNewTab && { opensInNewTab } ),
+ ...( id && Number.isInteger( id ) && { id } ),
+ ...( kind && { kind } ),
+ ...( type && type !== 'URL' && { type } ),
+ } );
+};
diff --git a/packages/block-editor/src/components/list-view/use-inserted-block.js b/packages/block-editor/src/components/list-view/use-inserted-block.js
new file mode 100644
index 00000000000000..0e5a25c980a1c3
--- /dev/null
+++ b/packages/block-editor/src/components/list-view/use-inserted-block.js
@@ -0,0 +1,47 @@
+/**
+ * WordPress dependencies
+ */
+import { useSelect, useDispatch } from '@wordpress/data';
+
+/**
+ * Internal dependencies
+ */
+import { store as blockEditorStore } from '../../store';
+
+export const useInsertedBlock = ( insertedBlockClientId ) => {
+ const { insertedBlockAttributes, insertedBlockName } = useSelect(
+ ( select ) => {
+ const { getBlockName, getBlockAttributes } =
+ select( blockEditorStore );
+
+ return {
+ insertedBlockAttributes: getBlockAttributes(
+ insertedBlockClientId
+ ),
+ insertedBlockName: getBlockName( insertedBlockClientId ),
+ };
+ },
+ [ insertedBlockClientId ]
+ );
+
+ const { updateBlockAttributes } = useDispatch( blockEditorStore );
+
+ const setInsertedBlockAttributes = ( _updatedAttributes ) => {
+ if ( ! insertedBlockClientId ) return;
+ updateBlockAttributes( insertedBlockClientId, _updatedAttributes );
+ };
+
+ if ( ! insertedBlockClientId ) {
+ return {
+ insertedBlockAttributes: undefined,
+ insertedBlockName: undefined,
+ setInsertedBlockAttributes,
+ };
+ }
+
+ return {
+ insertedBlockAttributes,
+ insertedBlockName,
+ setInsertedBlockAttributes,
+ };
+};