-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Block Editor: Interpret adjacent block selection from insertion point #16818
Changes from all commits
6a406f9
a2367d9
9a47741
c234cff
b3ce5fc
e9d19f2
0a6810f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,17 @@ import { getDefaultBlockName, createBlock } from '@wordpress/blocks'; | |
*/ | ||
import { select } from './controls'; | ||
|
||
/** | ||
* Value describing a selected block's initial position, either a number as the | ||
* offset (-1 for reverse), or an object of properties `isReverse`, `caretRect`. | ||
* | ||
* @typedef {(number|Object)} WPBlockInitialPosition | ||
* | ||
* @property {boolean} isReverse Whether initial position should be placed at | ||
* the end of the selected block. | ||
* @property {DOMRect} caretRect Rect describing offset of initial position. | ||
*/ | ||
|
||
/** | ||
* Generator which will yield a default block insert action if there | ||
* are no other blocks at the root of the editor. This generator should be used | ||
|
@@ -104,9 +115,8 @@ export function updateBlock( clientId, updates ) { | |
* value reflecting its selection directionality. An initialPosition of -1 | ||
* reflects a reverse selection. | ||
* | ||
* @param {string} clientId Block client ID. | ||
* @param {?number} initialPosition Optional initial position. Pass as -1 to | ||
* reflect reverse selection. | ||
* @param {string} clientId Block client ID. | ||
* @param {?WPBlockInitialPosition} initialPosition Optional initial position. | ||
* | ||
* @return {Object} Action object. | ||
*/ | ||
|
@@ -122,35 +132,37 @@ export function selectBlock( clientId, initialPosition = null ) { | |
* Yields action objects used in signalling that the block preceding the given | ||
* clientId should be selected. | ||
* | ||
* @param {string} clientId Block client ID. | ||
* @param {string} clientId Block client ID. | ||
* @param {?WPBlockInitialPosition} initialPosition Optional initial position. | ||
*/ | ||
export function* selectPreviousBlock( clientId ) { | ||
export function* selectPreviousBlock( clientId, initialPosition = -1 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not super excited to see more use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In this model, how would you foresee doing something like we're doing here (and also doing in WritingFlow) where we want to map a specific |
||
const previousBlockClientId = yield select( | ||
'core/block-editor', | ||
'getPreviousBlockClientId', | ||
clientId | ||
); | ||
|
||
if ( previousBlockClientId ) { | ||
yield selectBlock( previousBlockClientId, -1 ); | ||
yield selectBlock( previousBlockClientId, initialPosition ); | ||
} | ||
} | ||
|
||
/** | ||
* Yields action objects used in signalling that the block following the given | ||
* clientId should be selected. | ||
* | ||
* @param {string} clientId Block client ID. | ||
* @param {string} clientId Block client ID. | ||
* @param {?WPBlockInitialPosition} initialPosition Optional initial position. | ||
*/ | ||
export function* selectNextBlock( clientId ) { | ||
export function* selectNextBlock( clientId, initialPosition ) { | ||
const nextBlockClientId = yield select( | ||
'core/block-editor', | ||
'getNextBlockClientId', | ||
clientId | ||
); | ||
|
||
if ( nextBlockClientId ) { | ||
yield selectBlock( nextBlockClientId ); | ||
yield selectBlock( nextBlockClientId, initialPosition ); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not call
placeCaretAtVerticalEdge
directly and avoid the strangeinitialPosition
state?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be done, but the difficulty is in finding the input, which likely would require copying one or the other of WritingFlow or BlockListBlock's respective behaviors for finding a tabbable at the extent of the block [1] [2].
Maybe there's another option where we "wait" for the focus to take place from the block selection, and then call
placeCaretAtVerticalEdge
from thedocument.activeElement
.