Skip to content

Commit

Permalink
Multi selection: move hook to WritingFlow with other multi selection …
Browse files Browse the repository at this point in the history
…logic (#27479)
  • Loading branch information
ellatrix authored Dec 3, 2020
1 parent 2f48b90 commit 7777969
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
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 */
}

0 comments on commit 7777969

Please sign in to comment.