Repository navigation
Inserter: Show inline inserter before the first block and in the post title gap #82301
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
Open
shail-mehta
wants to merge
38
commits into
trunk
Choose a base branch
from
fix/inline-inserter-top-position
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+353
−1
Open
Changes from 14 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
c8e10ba
Inserter: Show inline inserter before the first block and in the post…
shail-mehta 64605dc
Added Changelog Entry
shail-mehta 5e0aead
Restore guard for nested list
shail-mehta 3ffaf94
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta fdac155
Fix Unit Test
shail-mehta 1fe42e1
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta b838090
Fix Lint
shail-mehta 7851067
fix lint
shail-mehta a8f0068
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta d037d2e
fix lint
shail-mehta 67e5790
Apply Feedback
shail-mehta aa28d8e
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta 21ff161
Fix Unit Test
shail-mehta e8b99b7
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta 8e80607
Fix Lint
shail-mehta 9e6eabe
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta 428183e
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta 4507f36
Merge branch trunk into fix/inline-inserter-top-position
shail-mehta 253c206
Fix title-gap inserter overlay stealing first-block clicks
d41a525
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta 0bf960f
Update CHANGELOG.md entry
shail-mehta a9bd20f
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta 84bc2d1
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta 9b8add3
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta 42c2b36
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta 462bc2d
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta cb5e243
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta af29720
Inserter: Stop title-gap overlay stealing first-block clicks
shail-mehta 355557f
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta 0038362
moved changelog in unreleased section
shail-mehta 9309fa1
Revise CHANGELOG for enhancements and bug fixes
shail-mehta 61ffbef
Update CHANGELOG.md with recent changes
shail-mehta f038749
Add inline inserter enhancement to changelog
shail-mehta bd96292
removed unintended changes
shail-mehta efe82a5
Merge branch 'trunk' into fix/inline-inserter-top-position
shail-mehta 0b43d7c
Clean up CHANGELOG.md formatting
shail-mehta 7e5c1bf
Merge remote-tracking branch 'origin/trunk' into fix/inline-inserter-…
shail-mehta 02929b0
Move block-editor #82301 changelog entry to Unreleased
shail-mehta 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
166 changes: 166 additions & 0 deletions
166
packages/editor/src/components/visual-editor/use-title-gap-inserter.js
This file contains hidden or 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,166 @@ | ||
| 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() ) { | ||
| 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 ] | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.