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

Multi selection: move hook to WritingFlow with other multi selection logic #27479

Merged
merged 1 commit into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import { __unstableGetBlockProps as getBlockProps } from '@wordpress/blocks';
*/
import { isInsideRootBlock } from '../../utils/dom';
import useMovingAnimation from '../use-moving-animation';
import { Context, SetBlockNodes } from './root-container';
import { SetBlockNodes } from './root-container';
import { SelectionStart } from '../writing-flow';
import { BlockListBlockContext } from './block';
import ELEMENTS from './block-wrapper-elements';

Expand All @@ -49,7 +50,7 @@ import ELEMENTS from './block-wrapper-elements';
export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
const fallbackRef = useRef();
const ref = props.ref || fallbackRef;
const onSelectionStart = useContext( Context );
const onSelectionStart = useContext( SelectionStart );
const setBlockNodes = useContext( SetBlockNodes );
const {
clientId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ import { createContext, forwardRef, useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import useMultiSelection from './use-multi-selection';
import useInsertionPoint from './insertion-point';
import BlockPopover from './block-popover';

export const Context = createContext();
export const BlockNodes = createContext();
export const SetBlockNodes = createContext();

function RootContainer( { children, className }, ref ) {
const onSelectionStart = useMultiSelection( ref );
const [ blockNodes, setBlockNodes ] = useState( {} );
const insertionPoint = useInsertionPoint( ref );

Expand All @@ -33,9 +30,7 @@ function RootContainer( { children, className }, ref ) {
className={ classnames( className, 'is-root-container' ) }
>
<SetBlockNodes.Provider value={ setBlockNodes }>
<Context.Provider value={ onSelectionStart }>
{ children }
</Context.Provider>
{ children }
</SetBlockNodes.Provider>
</div>
</BlockNodes.Provider>
Expand Down
11 changes: 8 additions & 3 deletions packages/block-editor/src/components/writing-flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useRef, useEffect, useState } from '@wordpress/element';
import { useRef, useEffect, useState, createContext } from '@wordpress/element';
import {
computeCaretRect,
focus,
Expand Down Expand Up @@ -44,6 +44,9 @@ import {
getBlockClientId,
} from '../../utils/dom';
import FocusCapture from './focus-capture';
import useMultiSelection from './use-multi-selection';

export const SelectionStart = createContext();

function getComputedStyle( node ) {
return node.ownerDocument.defaultView.getComputedStyle( node );
Expand Down Expand Up @@ -257,6 +260,8 @@ export default function WritingFlow( { children } ) {
// browser behaviour across blocks.
const verticalRect = useRef();

const onSelectionStart = useMultiSelection( container );

const {
selectedBlockClientId,
selectionStartClientId,
Expand Down Expand Up @@ -687,7 +692,7 @@ export default function WritingFlow( { children } ) {
// bubbling events from children to determine focus transition intents.
/* eslint-disable jsx-a11y/no-static-element-interactions */
return (
<>
<SelectionStart.Provider value={ onSelectionStart }>
<FocusCapture
ref={ focusCaptureBeforeRef }
selectedClientId={ selectedBlockClientId }
Expand Down Expand Up @@ -725,7 +730,7 @@ export default function WritingFlow( { children } ) {
multiSelectionContainer={ multiSelectionContainer }
isReverse
/>
</>
</SelectionStart.Provider>
);
/* eslint-enable jsx-a11y/no-static-element-interactions */
}