Skip to content

Commit

Permalink
reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jul 26, 2021
1 parent a5990d3 commit 83b6f33
Show file tree
Hide file tree
Showing 27 changed files with 263 additions and 184 deletions.
14 changes: 14 additions & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,20 @@ _Related_

Undocumented declaration.

### Placeholder

Placeholder for use in blocks. Creates an admin styling context and a tabbing
context in the block editor's writing flow.

_Parameters_

- _props_ `Object`:
- _props.wrapperProps_ `Object`:

_Returns_

- `WPComponent`: The component

### PlainText

_Related_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Button, Placeholder } from '@wordpress/components';
import { Button } from '@wordpress/components';
import { layout } from '@wordpress/icons';

/**
* Internal dependencies
*/
import Placeholder from '../placeholder';

function BlockVariationPicker( {
icon = layout,
label = __( 'Choose variation' ),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* WordPress dependencies
*/
import { useMergeRefs, useRefEffect } from '@wordpress/compose';
import { useState, createPortal, forwardRef } from '@wordpress/element';
import { ENTER, SPACE, ESCAPE } from '@wordpress/keycodes';
import { focus } from '@wordpress/dom';
import { __experimentalStyleProvider as StyleProvider } from '@wordpress/components';

function EmbeddedAdminContext( props, forwardedRef ) {
const [ shadow, setShadow ] = useState();
const [ hasFocus, setHasFocus ] = useState();
const ref = useRefEffect( ( element ) => {
const root = element.attachShadow( { mode: 'open' } );
Array.from( document.styleSheets ).forEach( ( styleSheet ) => {
if ( styleSheet.ownerNode.getAttribute( 'data-emotion' ) ) {
return;
}

let child = document.createElement( 'style' );

// Try to avoid requests for stylesheets of which we already
// know the CSS rules.
try {
let cssText = '';

for ( const cssRule of styleSheet.cssRules ) {
cssText += cssRule.cssText;
}

child.textContent = cssText;
} catch ( e ) {
child = styleSheet.ownerNode.cloneNode( true );
}

root.appendChild( child );
} );
setShadow( root );

function onFocusIn() {
setHasFocus( true );
}

function onFocusOut() {
setHasFocus( false );
}

root.addEventListener( 'focusin', onFocusIn );
root.addEventListener( 'focusout', onFocusOut );
return () => {
root.addEventListener( 'focusin', onFocusIn );
root.addEventListener( 'focusout', onFocusOut );
};
}, [] );
return (
<div
{ ...props }
ref={ useMergeRefs( [ ref, forwardedRef ] ) }
tabIndex={ 0 }
role="button"
aria-pressed={ hasFocus }
onKeyDown={ ( event ) => {
if ( event.keyCode === ENTER || event.keyCode === SPACE ) {
focus.focusable.find( shadow )[ 0 ].focus();
event.preventDefault();
} else if ( event.keyCode === ESCAPE ) {
shadow.host.focus();
event.preventDefault();
event.stopPropagation();
}
} }
>
{ shadow &&
createPortal(
<StyleProvider document={ { head: shadow } }>
{ props.children }
</StyleProvider>,
shadow
) }
</div>
);
}

export default forwardRef( EmbeddedAdminContext );
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export { default as __experimentalListView } from './list-view';
export { ListViewBlockFill as __experimentalListViewBlockFill } from './list-view/block-slot';
export { default as __experimentalListViewEditor } from './list-view/editor';
export { default as MediaReplaceFlow } from './media-replace-flow';
export { default as Placeholder } from './placeholder';
export { default as MediaPlaceholder } from './media-placeholder';
export { default as MediaUpload } from './media-upload';
export { default as MediaUploadCheck } from './media-upload/check';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import classnames from 'classnames';
import {
Button,
FormFileUpload,
Placeholder,
DropZone,
withFilters,
} from '@wordpress/components';
Expand All @@ -23,6 +22,7 @@ import { keyboardReturn } from '@wordpress/icons';
/**
* Internal dependencies
*/
import Placeholder from '../placeholder';
import MediaUpload from '../media-upload';
import MediaUploadCheck from '../media-upload/check';
import URLPopover from '../url-popover';
Expand Down Expand Up @@ -75,6 +75,7 @@ export function MediaPlaceholder( {
onFilesPreUpload = noop,
onHTMLDrop = noop,
children,
wrapperProps,
} ) {
const mediaUpload = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
Expand Down Expand Up @@ -243,6 +244,7 @@ export function MediaPlaceholder( {
onClick={ onClick }
onDoubleClick={ onDoubleClick }
preview={ mediaPreview }
wrapperProps={ wrapperProps }
>
{ content }
{ children }
Expand Down
32 changes: 32 additions & 0 deletions packages/block-editor/src/components/placeholder/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* WordPress dependencies
*/
import { useConstrainedTabbing } from '@wordpress/compose';
import { Placeholder } from '@wordpress/components';

/**
* Internal dependencies
*/
import EmbeddedAdminContext from '../embedded-admin-context';

/**
* Placeholder for use in blocks. Creates an admin styling context and a tabbing
* context in the block editor's writing flow.
*
* @param {Object} props
* @param {Object} props.wrapperProps
*
* @return {WPComponent} The component
*/
export default function IsolatedPlaceholder( { wrapperProps, ...props } ) {
return (
<EmbeddedAdminContext { ...wrapperProps }>
<Placeholder
{ ...props }
role="dialog"
aria-label={ props.label }
ref={ useConstrainedTabbing() }
/>
</EmbeddedAdminContext>
);
}
23 changes: 11 additions & 12 deletions packages/block-library/src/audio/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,17 @@ function AudioEdit( {
}
if ( ! src ) {
return (
<div { ...blockProps }>
<MediaPlaceholder
icon={ <BlockIcon icon={ icon } /> }
onSelect={ onSelectAudio }
onSelectURL={ onSelectURL }
accept="audio/*"
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ attributes }
notices={ noticeUI }
onError={ onUploadError }
/>
</div>
<MediaPlaceholder
icon={ <BlockIcon icon={ icon } /> }
onSelect={ onSelectAudio }
onSelectURL={ onSelectURL }
accept="audio/*"
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ attributes }
notices={ noticeUI }
onError={ onUploadError }
wrapperProps={ blockProps }
/>
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
store as coreStore,
} from '@wordpress/core-data';
import {
Placeholder,
Spinner,
ToolbarGroup,
ToolbarButton,
Expand All @@ -25,6 +24,7 @@ import {
InspectorControls,
useBlockProps,
Warning,
Placeholder,
} from '@wordpress/block-editor';
import { store as reusableBlocksStore } from '@wordpress/reusable-blocks';
import { ungroup } from '@wordpress/icons';
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/calendar/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import memoize from 'memize';
* WordPress dependencies
*/
import { calendar as icon } from '@wordpress/icons';
import { Disabled, Placeholder, Spinner } from '@wordpress/components';
import { Disabled, Spinner } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import ServerSideRender from '@wordpress/server-side-render';
import { useBlockProps } from '@wordpress/block-editor';
import { useBlockProps, Placeholder } from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';

Expand Down
7 changes: 5 additions & 2 deletions packages/block-library/src/categories/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import { times, unescape } from 'lodash';
*/
import {
PanelBody,
Placeholder,
Spinner,
ToggleControl,
VisuallyHidden,
} from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import {
InspectorControls,
useBlockProps,
Placeholder,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { pin } from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/embed/embed-placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';
import { Button, Placeholder, ExternalLink } from '@wordpress/components';
import { BlockIcon } from '@wordpress/block-editor';
import { Button, ExternalLink } from '@wordpress/components';
import { BlockIcon, Placeholder } from '@wordpress/block-editor';

const EmbedPlaceholder = ( {
icon,
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/embed/embed-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import classnames from 'classnames/dedupe';
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { Placeholder, SandBox } from '@wordpress/components';
import { RichText, BlockIcon } from '@wordpress/block-editor';
import { SandBox } from '@wordpress/components';
import { RichText, BlockIcon, Placeholder } from '@wordpress/block-editor';
import { Component } from '@wordpress/element';
import { createBlock } from '@wordpress/blocks';

Expand Down
29 changes: 14 additions & 15 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,20 @@ function FileEdit( {

if ( ! href || hasError ) {
return (
<div { ...blockProps }>
<MediaPlaceholder
icon={ <BlockIcon icon={ icon } /> }
labels={ {
title: __( 'File' ),
instructions: __(
'Upload a file or pick one from your media library.'
),
} }
onSelect={ onSelectFile }
notices={ noticeUI }
onError={ onUploadError }
accept="*"
/>
</div>
<MediaPlaceholder
icon={ <BlockIcon icon={ icon } /> }
labels={ {
title: __( 'File' ),
instructions: __(
'Upload a file or pick one from your media library.'
),
} }
onSelect={ onSelectFile }
notices={ noticeUI }
onError={ onUploadError }
accept="*"
wrapperProps={ blockProps }
/>
);
}

Expand Down
Loading

0 comments on commit 83b6f33

Please sign in to comment.