diff --git a/packages/block-editor/CHANGELOG.md b/packages/block-editor/CHANGELOG.md index 10dd4aa0e3b4e4..c3da1b2188e15f 100644 --- a/packages/block-editor/CHANGELOG.md +++ b/packages/block-editor/CHANGELOG.md @@ -19,6 +19,7 @@ ### Bug Fixes +- In-between inserter: Limit title-gap (`is-before-first-block`) hit-testing to the "+" so the overlay no longer steals clicks on the first block (fixes WebKit shift+click multi-selection inside a Group) ([#82301](https://github.com/WordPress/gutenberg/pull/82301)). - `RichTextToolbarButton`: Show a button whose `name` is not one of the formats with a place in the toolbar in the "More" dropdown, instead of silently dropping it ([#85843](https://github.com/WordPress/gutenberg/pull/85843)). - `LinkControl`: Keep focus in the link preview after a post or page is chosen whose record has not loaded yet, so the popover still closes when focus leaves it ([#85945](https://github.com/WordPress/gutenberg/pull/85945)). - `URLInput`: Keep pointer and keyboard highlights in sync and selected suggestions readable on hover ([#84583](https://github.com/WordPress/gutenberg/pull/84583)). diff --git a/packages/block-editor/src/components/block-list/use-in-between-inserter.ts b/packages/block-editor/src/components/block-list/use-in-between-inserter.ts index e3fec2530f61c4..5b513f67ad6d28 100644 --- a/packages/block-editor/src/components/block-list/use-in-between-inserter.ts +++ b/packages/block-editor/src/components/block-list/use-in-between-inserter.ts @@ -176,7 +176,8 @@ export function useInBetweenInserter() { const index = getBlockIndex( clientId ); // Don't show the in-between inserter before the first block in - // the list (preserves the original behaviour). + // the list. Insertion at index 0 in the post editor is handled + // separately via the title gap inserter. if ( index === 0 ) { hideInsertionPoint(); return; diff --git a/packages/block-editor/src/components/block-popover/inbetween.tsx b/packages/block-editor/src/components/block-popover/inbetween.tsx index 852316f015474a..320b69b14740c8 100644 --- a/packages/block-editor/src/components/block-popover/inbetween.tsx +++ b/packages/block-editor/src/components/block-popover/inbetween.tsx @@ -18,6 +18,10 @@ function BlockPopoverInbetween( { __unstableContentRef, operation = 'insert', nearestSide = 'right', + // When true and there is no previous block, place the popover in the gap + // above the first block (title-gap inserter). Leave false for drag-and-drop + // and other index-0 indicators that expect a zero-height line. + placeBeforeFirstBlock = false, ...props }: BlockPopoverInbetweenProps ) { // This is a temporary hack to get the inbetween inserter to recompute properly. @@ -118,6 +122,40 @@ function BlockPopoverInbetween( { nextRect && previousRect ? ( previousRect.left + nextRect.left ) / 2 : ( previousRect || nextRect )!.left; + + // Title-gap inserter only: occupy the gap above the first + // block, never the block itself. A minimum height that + // overlaps the first block steals WebKit shift+click + // (e.g. nested multi-selection inside a Group). + if ( + placeBeforeFirstBlock && + ! previousRect && + nextRect && + nextElement + ) { + const layout = nextElement.parentElement; + const titleBottom = + layout?.previousElementSibling?.getBoundingClientRect() + .bottom; + if ( + typeof titleBottom === 'number' && + titleBottom < nextRect.top + ) { + top = titleBottom; + height = nextRect.top - titleBottom; + } else { + const { defaultView } = nextElement.ownerDocument; + const marginTop = defaultView + ? parseFloat( + defaultView.getComputedStyle( + nextElement + ).marginTop + ) || 0 + : 0; + top = nextRect.top - marginTop; + height = marginTop; + } + } } else { top = previousRect ? previousRect.top : nextRect!.top; height = previousRect @@ -158,6 +196,7 @@ function BlockPopoverInbetween( { isVisible, operation, nearestSide, + placeBeforeFirstBlock, ] ); const popoverScrollRef = usePopoverScroll( __unstableContentRef ); diff --git a/packages/block-editor/src/components/block-popover/style.scss b/packages/block-editor/src/components/block-popover/style.scss index e24aed126e849d..cd88d8768ab473 100644 --- a/packages/block-editor/src/components/block-popover/style.scss +++ b/packages/block-editor/src/components/block-popover/style.scss @@ -45,6 +45,23 @@ pointer-events: all; } } + + // Title-gap (before first block): only the "+" is interactive. The full + // overlay must not steal clicks on the first block — that breaks WebKit + // shift+click multi-selection inside a Group that is the first root block. + // Higher specificity than `.is-with-inserter` above is required. + .is-with-inserter.is-before-first-block { + pointer-events: none; + + * { + pointer-events: none; + } + + .block-editor-block-list__insertion-point-inserter, + .block-editor-block-list__insertion-point-inserter * { + pointer-events: all; + } + } } diff --git a/packages/block-editor/src/components/block-tools/insertion-point.tsx b/packages/block-editor/src/components/block-tools/insertion-point.tsx index def1f5888970f4..a30aa697beb1d6 100644 --- a/packages/block-editor/src/components/block-tools/insertion-point.tsx +++ b/packages/block-editor/src/components/block-tools/insertion-point.tsx @@ -25,6 +25,17 @@ export const InsertionPointOpenRef = createContext< >( undefined ); InsertionPointOpenRef.displayName = 'InsertionPointOpenRefContext'; +/** + * Renders the in-between insertion point popover, optionally with a quick + * inserter when `__unstableWithInserter` is set on the insertion point. + * + * @param {Object} props Component props. + * @param {string} props.__unstablePopoverSlot Optional popover slot name. + * @param {Object} props.__unstableContentRef Ref to the scrollable canvas content. + * @param {string} props.operation Insertion operation type. + * @param {string} props.nearestSide Preferred side for group operations. + * @return {import('react').ReactElement|null} Insertion point UI, or null when hidden. + */ function InbetweenInsertionPointPopover( { __unstablePopoverSlot, __unstableContentRef, @@ -180,6 +191,8 @@ function InbetweenInsertionPointPopover( { orientationClassname ); + const placeBeforeFirstBlock = isInserterShown && ! previousClientId; + return ( diff --git a/packages/block-editor/src/components/block-tools/style.scss b/packages/block-editor/src/components/block-tools/style.scss index 0a40b6dd1f4372..da5df406412e9e 100644 --- a/packages/block-editor/src/components/block-tools/style.scss +++ b/packages/block-editor/src/components/block-tools/style.scss @@ -11,6 +11,24 @@ bottom: 0; left: 0; right: 0; + + // The title-gap overlay sits in the parent document over the iframe. + // Clicks on the empty gap must reach the first block, not this wrapper. + // Keep in sync with `.is-with-inserter.is-before-first-block` in + // block-popover/style.scss (that rule wins on specificity for the + // inbetween popover). + &.is-with-inserter.is-before-first-block { + pointer-events: none; + + * { + pointer-events: none; + } + + .block-editor-block-list__insertion-point-inserter, + .block-editor-block-list__insertion-point-inserter * { + pointer-events: all; + } + } } .block-editor-block-list__insertion-point-indicator { diff --git a/packages/editor/CHANGELOG.md b/packages/editor/CHANGELOG.md index c30fc15a939745..ce9b77c7b54b90 100644 --- a/packages/editor/CHANGELOG.md +++ b/packages/editor/CHANGELOG.md @@ -11,6 +11,7 @@ - Autocomplete: Match user mention slugs to popup item highlights ([#84583](https://github.com/WordPress/gutenberg/pull/84583)). - Add a translated ellipsis to modal action labels in post action menus, preserving recognized ellipses in existing labels ([#85396](https://github.com/WordPress/gutenberg/pull/85396)). - Notes: Show the add-reaction picker as a single row of emoji, with each name in a tooltip ([#85819](https://github.com/WordPress/gutenberg/pull/85819)). +- Post editor: Show the inline inserter in the gap between the post title and the first block ([#82301](https://github.com/WordPress/gutenberg/pull/82301)). ### Bug Fixes diff --git a/packages/editor/src/components/visual-editor/index.jsx b/packages/editor/src/components/visual-editor/index.jsx index ed4924bfb2210c..847e20cffbc507 100644 --- a/packages/editor/src/components/visual-editor/index.jsx +++ b/packages/editor/src/components/visual-editor/index.jsx @@ -34,6 +34,7 @@ import { } from '../../store/constants'; import { useZoomOutModeExit } from './use-zoom-out-mode-exit'; import { usePaddingAppender } from './use-padding-appender'; +import { useTitleGapInserter } from './use-title-gap-inserter'; import { useEditContentOnlySectionExit } from './use-edit-content-only-section-exit'; import { SyncConnectionErrorModal } from '../sync-connection-error-modal'; @@ -120,6 +121,17 @@ export function getCanvasHeight( width, containerSize ) { return Math.min( Math.round( width / aspectRatio ), containerSize.height ); } +/** + * Renders the post or site editor visual canvas, including the iframe, + * block list, and post title when in post-only mode. + * + * @param {Object} props Component props. + * @param {boolean} props.autoFocus Whether the canvas should take focus on mount. + * @param {Object} props.iframeProps Props forwarded to the editor canvas iframe. + * @param {Object} props.contentRef Ref merged onto the canvas content node. + * @param {string} props.className Optional extra class name for the wrapper. + * @return {JSX.Element} Visual editor canvas. + */ function VisualEditor( { // Ideally as we unify post and site editors, we won't need these props. autoFocus, @@ -394,6 +406,9 @@ function VisualEditor( { const [ paddingAppenderRef, paddingStyle ] = usePaddingAppender( ! isPreview && renderingMode === 'post-only' && ! isDesignPostType ); + const titleGapInserterRef = useTitleGapInserter( + ! isPreview && renderingMode === 'post-only' && ! isDesignPostType + ); const shouldConstrainCanvasHeight = enableResizing && @@ -478,6 +493,7 @@ function VisualEditor( { } ), useZoomOutModeExit(), paddingAppenderRef, + titleGapInserterRef, useEditContentOnlySectionExit(), ] ); diff --git a/packages/editor/src/components/visual-editor/style.scss b/packages/editor/src/components/visual-editor/style.scss index bcfed261bfdf4f..880917d2de3141 100644 --- a/packages/editor/src/components/visual-editor/style.scss +++ b/packages/editor/src/components/visual-editor/style.scss @@ -52,6 +52,22 @@ padding: 6px; } } + + // Title-gap inserter only. Limit hit-testing to the "+" so the overlay + // does not steal clicks on the first block. Mirror of the rule in + // block-popover/style.scss for when the popover is nested here. + .block-editor-block-list__insertion-point.is-with-inserter.is-before-first-block { + pointer-events: none; + + * { + pointer-events: none; + } + + .block-editor-block-list__insertion-point-inserter, + .block-editor-block-list__insertion-point-inserter * { + pointer-events: all; + } + } } // Keep in sync with `canvas-margin.js`. diff --git a/packages/editor/src/components/visual-editor/use-title-gap-inserter.js b/packages/editor/src/components/visual-editor/use-title-gap-inserter.js new file mode 100644 index 00000000000000..231d020314c3bf --- /dev/null +++ b/packages/editor/src/components/visual-editor/use-title-gap-inserter.js @@ -0,0 +1,169 @@ +import { useRefEffect } from '@wordpress/compose'; +import { useRegistry, useSelect } from '@wordpress/data'; +import { store as blockEditorStore } from '@wordpress/block-editor'; +import { unlock } from '../../lock-unlock'; + +const TITLE_WRAPPER_SELECTOR = '.editor-visual-editor__post-title-wrapper'; +const BLOCK_LIST_LAYOUT_SELECTOR = + '.block-editor-block-list__layout.is-root-container'; +const QUICK_INSERTER_SELECTOR = '.block-editor-inserter__quick-inserter'; +const EXPANDED_INSERTER_TOGGLE_SELECTOR = + '.block-editor-block-list__insertion-point-inserter .block-editor-inserter__toggle[aria-expanded="true"]'; + +/** + * Whether the title-gap inserter menu is open or opening. + * The insertion point and quick inserter render in the parent document + * (outside the canvas iframe); the toggle's aria-expanded covers the + * brief gap before the menu mounts. + * + * @param {Document} doc Canvas document. + * @return {boolean} True when the quick inserter is open or opening. + */ +function isTitleGapInserterMenuActive( doc ) { + const parentDoc = doc.defaultView?.frameElement?.ownerDocument; + const docs = parentDoc ? [ doc, parentDoc ] : [ doc ]; + + return docs.some( + ( checkDoc ) => + !! checkDoc.querySelector( QUICK_INSERTER_SELECTOR ) || + !! checkDoc.querySelector( EXPANDED_INSERTER_TOGGLE_SELECTOR ) + ); +} + +/** + * Hides the title-gap insertion point when it is the visible cue. + * + * @param {Object} registry Data registry. + * @return {void} + */ +function hideTitleGapInsertionPoint( registry ) { + const blockEditorSelect = registry.select( blockEditorStore ); + const insertionPoint = blockEditorSelect.isBlockInsertionPointVisible() + ? blockEditorSelect.getBlockInsertionPoint() + : null; + + // Hide only the title-gap insertion point so we don't dismiss + // unrelated in-between inserters. Leaving it visible lets the + // pointer-events overlay steal clicks on the first block + // (e.g. shift+click multi-selection inside a Group). + if ( + insertionPoint?.__unstableWithInserter && + insertionPoint.rootClientId === '' && + insertionPoint.index === 0 + ) { + registry.dispatch( blockEditorStore ).hideInsertionPoint(); + } +} + +/** + * Shows the block inserter when hovering the gap between the post title + * and the first block in the post editor. + * + * @param {boolean} enabled Whether the hook is enabled. + * @return {import('react').RefCallback} Ref callback that attaches the hover listener. + */ +export function useTitleGapInserter( enabled ) { + const registry = useRegistry(); + const isDisabled = useSelect( ( select ) => { + const { getSettings, isZoomOut } = unlock( select( blockEditorStore ) ); + const settings = getSettings(); + return ( + settings.isDistractionFree || settings.isPreviewMode || isZoomOut() + ); + }, [] ); + + return useRefEffect( + ( node ) => { + if ( ! enabled || isDisabled ) { + return; + } + + /** + * Shows or hides the title-gap insertion point from pointer position. + * + * @param {MouseEvent} event Pointer move event from the canvas. + * @return {void} + */ + function onMouseMove( event ) { + const blockEditorSelect = registry.select( blockEditorStore ); + const { getBlockOrder, isMultiSelecting, getTemplateLock } = + unlock( blockEditorSelect ); + const { showInsertionPoint, hideInsertionPoint } = + registry.dispatch( blockEditorStore ); + + if ( isMultiSelecting() ) { + // Clear a stuck title-gap overlay so it cannot intercept + // shift+click multi-selection on the first block. + hideTitleGapInsertionPoint( registry ); + return; + } + + if ( getTemplateLock( '' ) ) { + hideInsertionPoint(); + return; + } + + const blockOrder = getBlockOrder( '' ); + if ( blockOrder.length === 0 ) { + hideInsertionPoint(); + return; + } + + const { ownerDocument } = node; + const titleWrapper = ownerDocument.querySelector( + TITLE_WRAPPER_SELECTOR + ); + const blockListLayout = ownerDocument.querySelector( + BLOCK_LIST_LAYOUT_SELECTOR + ); + + if ( ! titleWrapper || ! blockListLayout ) { + return; + } + + const firstBlock = + blockListLayout.querySelector( ':scope > .wp-block' ); + if ( ! firstBlock ) { + hideInsertionPoint(); + return; + } + + const titleRect = titleWrapper.getBoundingClientRect(); + const firstBlockRect = firstBlock.getBoundingClientRect(); + const layoutRect = blockListLayout.getBoundingClientRect(); + const { clientY, clientX } = event; + + const isInTitleGap = + clientY > titleRect.bottom && + clientY < firstBlockRect.top && + clientX >= layoutRect.left && + clientX <= layoutRect.right; + + if ( isInTitleGap ) { + showInsertionPoint( '', 0, { + __unstableWithInserter: true, + } ); + return; + } + + // Keep the insertion point mounted while the quick inserter + // menu is open or opening; hiding it would unmount the menu. + if ( isTitleGapInserterMenuActive( ownerDocument ) ) { + return; + } + + hideTitleGapInsertionPoint( registry ); + } + + node.addEventListener( 'mousemove', onMouseMove ); + + return () => { + node.removeEventListener( 'mousemove', onMouseMove ); + // Clear a stuck title-gap cue when the hook disables (e.g. + // distraction-free / zoom-out) or the effect re-runs. + hideTitleGapInsertionPoint( registry ); + }; + }, + [ enabled, isDisabled, registry ] + ); +} diff --git a/test/e2e/specs/editor/various/inserting-blocks.spec.js b/test/e2e/specs/editor/various/inserting-blocks.spec.js index 71de9c4852a3d5..67a27f2453e36a 100644 --- a/test/e2e/specs/editor/various/inserting-blocks.spec.js +++ b/test/e2e/specs/editor/various/inserting-blocks.spec.js @@ -572,6 +572,65 @@ test.describe( 'Inserting blocks (@firefox, @webkit)', () => { ] ); } ); + // Check for https://github.com/WordPress/gutenberg/issues/27540. + test( 'inserts a block before the first block using the inline inserter', async ( { + admin, + editor, + page, + } ) => { + await admin.createNewPost(); + await editor.canvas + .getByRole( 'textbox', { name: 'Add title' } ) + .fill( 'Post title' ); + await editor.canvas + .getByRole( 'document', { name: 'Add default block' } ) + .click(); + await page.keyboard.type( 'First paragraph' ); + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( 'Second paragraph' ); + + const firstBlockBoundingBox = await editor.canvas + .locator( + '.block-editor-block-list__layout.is-root-container > .wp-block' + ) + .first() + .boundingBox(); + + // Hover just above the first block (title–block gap), matching the + // pattern used by other inline-inserter tests. + await page.mouse.move( + firstBlockBoundingBox.x + firstBlockBoundingBox.width / 2, + firstBlockBoundingBox.y - 10, + // An arbitrary number of `steps` imitates cursor movement in the test environment, + // activating the title gap inserter. + { steps: 10 } + ); + + const addBlockButton = page + .locator( '.block-editor-block-list__insertion-point-inserter' ) + .getByRole( 'button', { name: 'Add block' } ); + await expect( addBlockButton ).toBeVisible(); + await addBlockButton.click(); + + const quickInserter = page.locator( + '.block-editor-inserter__quick-inserter' + ); + await expect( quickInserter ).toBeVisible(); + await page.getByRole( 'button', { name: 'Browse all' } ).click(); + await page + .getByRole( 'listbox', { name: 'Media' } ) + .getByRole( 'option', { name: 'Image' } ) + .click(); + + await expect + .poll( editor.getBlocks ) + .toMatchObject( [ + { name: 'core/image' }, + { name: 'core/paragraph' }, + { name: 'core/paragraph' }, + ] ); + } ); + // Check for regression of https://github.com/WordPress/gutenberg/issues/72297. test( 'keeps the inline inserter open when the Block Library panel closes', async ( { admin,