Skip to content

Commit

Permalink
prep build 09/11
Browse files Browse the repository at this point in the history
  • Loading branch information
bph committed Sep 11, 2024
2 parents bf15cc2 + 2f939a7 commit c8280e4
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 113 deletions.
22 changes: 13 additions & 9 deletions packages/block-editor/src/layouts/grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export default {
// In the experiment we want to also show column control in Auto mode, and
// the minimum width control in Manual mode.
const showColumnsControl =
window.__experimentalEnableGridInteractivity || layout?.columnCount;
window.__experimentalEnableGridInteractivity ||
!! layout?.columnCount;
const showMinWidthControl =
window.__experimentalEnableGridInteractivity ||
! layout?.columnCount;
Expand Down Expand Up @@ -317,7 +318,7 @@ function GridLayoutColumnsAndRowsControl( {
const defaultNewColumnCount =
isManualPlacement ? 1 : undefined;
const newColumnCount =
value === ''
value === '' || value === '0'
? defaultNewColumnCount
: parseInt( value, 10 );
onChange( {
Expand All @@ -327,7 +328,7 @@ function GridLayoutColumnsAndRowsControl( {
} else {
// Don't allow unsetting the column count.
const newColumnCount =
value === ''
value === '' || value === '0'
? 1
: parseInt( value, 10 );
onChange( {
Expand All @@ -337,7 +338,7 @@ function GridLayoutColumnsAndRowsControl( {
}
} }
value={ columnCount }
min={ 0 }
min={ 1 }
label={ __( 'Columns' ) }
hideLabelFromVision={
! window.__experimentalEnableGridInteractivity ||
Expand All @@ -355,7 +356,7 @@ function GridLayoutColumnsAndRowsControl( {
onChange={ ( value ) => {
// Don't allow unsetting the row count.
const newRowCount =
value === ''
value === '' || value === '0'
? 1
: parseInt( value, 10 );
onChange( {
Expand All @@ -364,21 +365,24 @@ function GridLayoutColumnsAndRowsControl( {
} );
} }
value={ rowCount }
min={ 0 }
min={ 1 }
label={ __( 'Rows' ) }
/>
) : (
<RangeControl
__next40pxDefaultSize
__nextHasNoMarginBottom
value={ columnCount ?? 0 }
value={ columnCount ?? 1 }
onChange={ ( value ) =>
onChange( {
...layout,
columnCount: value,
columnCount:
value === '' || value === '0'
? 1
: value,
} )
}
min={ 0 }
min={ 1 }
max={ 16 }
withInputField={ false }
label={ __( 'Columns' ) }
Expand Down
32 changes: 26 additions & 6 deletions packages/block-library/src/file/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,32 @@ const transforms = {
const blobURL = createBlobURL( file );

// File will be uploaded in componentDidMount()
blocks.push(
createBlock( 'core/file', {
blob: blobURL,
fileName: file.name,
} )
);
if ( file.type.startsWith( 'video/' ) ) {
blocks.push(
createBlock( 'core/video', {
blob: createBlobURL( file ),
} )
);
} else if ( file.type.startsWith( 'image/' ) ) {
blocks.push(
createBlock( 'core/image', {
blob: createBlobURL( file ),
} )
);
} else if ( file.type.startsWith( 'audio/' ) ) {
blocks.push(
createBlock( 'core/audio', {
blob: createBlobURL( file ),
} )
);
} else {
blocks.push(
createBlock( 'core/file', {
blob: blobURL,
fileName: file.name,
} )
);
}
} );

return blocks;
Expand Down
52 changes: 35 additions & 17 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { Placeholder } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import {
BlockIcon,
MediaPlaceholder,
useBlockProps,
MediaPlaceholder,
store as blockEditorStore,
__experimentalUseBorderProps as useBorderProps,
__experimentalGetShadowClassesAndStyles as getShadowClassesAndStyles,
Expand All @@ -32,6 +32,7 @@ import { unlock } from '../lock-unlock';
import { useUploadMediaFromBlobURL } from '../utils/hooks';
import Image from './image';
import { isValidFileType } from './utils';
import { useMaxWidthObserver } from './use-max-width-observer';

/**
* Module constants
Expand Down Expand Up @@ -109,12 +110,23 @@ export function ImageEdit( {
align,
metadata,
} = attributes;

const [ temporaryURL, setTemporaryURL ] = useState( attributes.blob );
const figureRef = useRef();

const [ contentResizeListener, { width: containerWidth } ] =
const containerRef = useRef();
// Only observe the max width from the parent container when the parent layout is not flex nor grid.
// This won't work for them because the container width changes with the image.
// TODO: Find a way to observe the container width for flex and grid layouts.
const isMaxWidthContainerWidth =
! parentLayout ||
( parentLayout.type !== 'flex' && parentLayout.type !== 'grid' );
const [ maxWidthObserver, maxContentWidth ] = useMaxWidthObserver();

const [ placeholderResizeListener, { width: placeholderWidth } ] =
useResizeObserver();

const isSmallContainer = placeholderWidth && placeholderWidth < 160;

const altRef = useRef();
useEffect( () => {
altRef.current = alt;
Expand Down Expand Up @@ -160,7 +172,7 @@ export function ImageEdit( {
}

function onSelectImagesList( images ) {
const win = figureRef.current?.ownerDocument.defaultView;
const win = containerRef.current?.ownerDocument.defaultView;

if ( images.every( ( file ) => file instanceof win.File ) ) {
/** @type {File[]} */
Expand Down Expand Up @@ -348,7 +360,10 @@ export function ImageEdit( {
Object.keys( borderProps.style ).length > 0 ),
} );

const blockProps = useBlockProps( { ref: figureRef, className: classes } );
const blockProps = useBlockProps( {
ref: containerRef,
className: classes,
} );

// Much of this description is duplicated from MediaPlaceholder.
const { lockUrlControls = false, lockUrlControlsMessage } = useSelect(
Expand Down Expand Up @@ -387,11 +402,15 @@ export function ImageEdit( {
[ borderProps.className ]:
!! borderProps.className && ! isSingleSelected,
} ) }
withIllustration
icon={ lockUrlControls ? pluginsIcon : icon }
label={ __( 'Image' ) }
icon={
! isSmallContainer &&
( lockUrlControls ? pluginsIcon : icon )
}
withIllustration={ ! isSingleSelected || isSmallContainer }
label={ ! isSmallContainer && __( 'Image' ) }
instructions={
! lockUrlControls &&
! isSmallContainer &&
__(
'Upload or drag an image file here, or pick one from your library.'
)
Expand All @@ -408,13 +427,12 @@ export function ImageEdit( {
...shadowProps.style,
} }
>
{ lockUrlControls ? (
<span className="block-bindings-media-placeholder-message">
{ lockUrlControlsMessage }
</span>
) : (
content
) }
{ lockUrlControls &&
! isSmallContainer &&
lockUrlControlsMessage }

{ ! lockUrlControls && ! isSmallContainer && content }
{ placeholderResizeListener }
</Placeholder>
);
};
Expand All @@ -436,7 +454,7 @@ export function ImageEdit( {
clientId={ clientId }
blockEditingMode={ blockEditingMode }
parentLayoutType={ parentLayout?.type }
containerWidth={ containerWidth }
maxContentWidth={ maxContentWidth }
/>
<MediaPlaceholder
icon={ <BlockIcon icon={ icon } /> }
Expand All @@ -455,7 +473,7 @@ export function ImageEdit( {
{
// The listener cannot be placed as the first element as it will break the in-between inserter.
// See https://github.com/WordPress/gutenberg/blob/71134165868298fc15e22896d0c28b41b3755ff7/packages/block-editor/src/components/block-list/use-in-between-inserter.js#L120
contentResizeListener
isSingleSelected && isMaxWidthContainerWidth && maxWidthObserver
}
</>
);
Expand Down
45 changes: 7 additions & 38 deletions packages/block-library/src/image/editor.scss
Original file line number Diff line number Diff line change
@@ -1,44 +1,8 @@
// Provide special styling for the placeholder.
// @todo this particular minimal style of placeholder could be componentized further.
.wp-block-image.wp-block-image {

// Show Placeholder style on-select.
&.is-selected .block-editor-media-placeholder {
// Block UI appearance.
color: $gray-900;
background-color: $white;
box-shadow: inset 0 0 0 $border-width $gray-900;
border: none;

// Disable any duotone filter applied in the selected state.
filter: none !important;

> svg {
opacity: 0;
}

.components-placeholder__illustration {
display: none;
}

&::before {
opacity: 0;
}
}
.block-bindings-media-placeholder-message {
opacity: 0;
}
&.is-selected .block-bindings-media-placeholder-message {
opacity: 1;
}

// Remove the transition while we still have a legacy placeholder style.
// Otherwise the content jumps between the 1px placeholder border, and any inherited custom
// parent border that may get applied when you deselect.
.components-placeholder__label,
.components-placeholder__instructions,
.components-button {
transition: none;
.block-editor-media-placeholder.is-small {
min-height: 60px;
}
}

Expand Down Expand Up @@ -149,6 +113,11 @@ figure.wp-block-image:not(.wp-block) {
text-align: center;
}

// Relatively position the alignment container to support the content resizer.
.wp-block[data-align]:has(> .wp-block-image) {
position: relative;
}

.wp-block-image__crop-area {
position: relative;
max-width: 100%;
Expand Down
51 changes: 31 additions & 20 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default function Image( {
clientId,
blockEditingMode,
parentLayoutType,
containerWidth,
maxContentWidth,
} ) {
const {
url = '',
Expand Down Expand Up @@ -556,6 +556,24 @@ export default function Image( {

const showBlockControls = showUrlInput || allowCrop || showCoverControls;

const mediaReplaceFlow = isSingleSelected &&
! isEditingImage &&
! lockUrlControls && (
<BlockControls group="other">
<MediaReplaceFlow
mediaId={ id }
mediaURL={ url }
allowedTypes={ ALLOWED_MEDIA_TYPES }
accept="image/*"
onSelect={ onSelectImage }
onSelectURL={ onSelectURL }
onError={ onUploadError }
name={ ! url ? __( 'Add image' ) : __( 'Replace' ) }
onReset={ () => onSelectImage( undefined ) }
/>
</BlockControls>
);

const controls = (
<>
{ showBlockControls && (
Expand Down Expand Up @@ -592,20 +610,6 @@ export default function Image( {
) }
</BlockControls>
) }
{ isSingleSelected && ! isEditingImage && ! lockUrlControls && (
<BlockControls group="other">
<MediaReplaceFlow
mediaId={ id }
mediaURL={ url }
allowedTypes={ ALLOWED_MEDIA_TYPES }
accept="image/*"
onSelect={ onSelectImage }
onSelectURL={ onSelectURL }
onError={ onUploadError }
onReset={ () => onSelectImage( undefined ) }
/>
</BlockControls>
) }
{ isSingleSelected && externalBlob && (
<BlockControls>
<ToolbarGroup>
Expand Down Expand Up @@ -934,7 +938,7 @@ export default function Image( {
// @todo It would be good to revisit this once a content-width variable
// becomes available.
const maxWidthBuffer = maxWidth * 2.5;
const maxContentWidth = containerWidth || maxWidthBuffer;
const maxResizeWidth = maxContentWidth || maxWidthBuffer;

let showRightHandle = false;
let showLeftHandle = false;
Expand Down Expand Up @@ -980,9 +984,9 @@ export default function Image( {
} }
showHandle={ isSingleSelected }
minWidth={ minWidth }
maxWidth={ maxContentWidth }
maxWidth={ maxResizeWidth }
minHeight={ minHeight }
maxHeight={ maxContentWidth / ratio }
maxHeight={ maxResizeWidth / ratio }
lockAspectRatio={ ratio }
enable={ {
top: false,
Expand All @@ -996,6 +1000,7 @@ export default function Image( {

// Clear hardcoded width if the resized width is close to the max-content width.
if (
maxContentWidth &&
// Only do this if the image is bigger than the container to prevent it from being squished.
// TODO: Remove this check if the image support setting 100% width.
naturalWidth >= maxContentWidth &&
Expand Down Expand Up @@ -1029,12 +1034,18 @@ export default function Image( {
}

if ( ! url && ! temporaryURL ) {
// Add all controls if the image attributes are connected.
return metadata?.bindings ? controls : sizeControls;
return (
<>
{ mediaReplaceFlow }
{ /* Add all controls if the image attributes are connected. */ }
{ metadata?.bindings ? controls : sizeControls }
</>
);
}

return (
<>
{ mediaReplaceFlow }
{ controls }
{ img }

Expand Down
Loading

0 comments on commit c8280e4

Please sign in to comment.