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

Add backports for 5.7 RC 3 #29576

Merged
merged 6 commits into from
Mar 5, 2021
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
4 changes: 4 additions & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fixes

- Fix a regression where the Cover block migration would not work with a non-default contentPosition ([#29542](https://github.com/WordPress/gutenberg/pull/29542))

## 2.28.0 (2021-02-01)

### New Features
Expand Down
12 changes: 12 additions & 0 deletions packages/block-library/src/cover/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ const deprecated = [
customGradient: {
type: 'string',
},
contentPosition: {
type: 'string',
},
},
supports: {
align: true,
},
save( { attributes } ) {
const {
Expand Down Expand Up @@ -219,6 +225,9 @@ const deprecated = [
type: 'string',
},
},
supports: {
align: true,
},
save( { attributes } ) {
const {
backgroundType,
Expand Down Expand Up @@ -322,6 +331,9 @@ const deprecated = [
type: 'string',
},
},
supports: {
align: true,
},
save( { attributes } ) {
const {
backgroundType,
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/cover/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ function CoverEdit( {
style={ { backgroundImage: gradientValue } }
/>
) }
{ isImageBackground && isImgElement && (
{ url && isImageBackground && isImgElement && (
<img
ref={ isDarkElement }
className="wp-block-cover__image-background"
Expand All @@ -601,7 +601,7 @@ function CoverEdit( {
style={ mediaStyle }
/>
) }
{ isVideoBackground && (
{ url && isVideoBackground && (
<video
ref={ isDarkElement }
className="wp-block-cover__video-background"
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/gallery/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ figure.wp-block-gallery {
left: 0;
z-index: 1;
}
figcaption {
z-index: 2;
}
}

figure.is-transient img {
Expand Down
240 changes: 119 additions & 121 deletions packages/components/src/drop-zone/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ import { find, some, filter, includes, throttle } from 'lodash';
/**
* WordPress dependencies
*/
import {
createContext,
useEffect,
useRef,
useContext,
} from '@wordpress/element';
import { createContext, useRef, useContext } from '@wordpress/element';
import { getFilesFromDataTransfer } from '@wordpress/dom';
import isShallowEqual from '@wordpress/is-shallow-equal';
import { useRefEffect } from '@wordpress/compose';

export const Context = createContext();

Expand Down Expand Up @@ -109,135 +105,138 @@ export const INITIAL_DROP_ZONE_STATE = {
type: null,
};

export function useDrop( ref ) {
export function useDrop() {
const dropZones = useContext( Context );

useEffect( () => {
const { ownerDocument } = ref.current;
const { defaultView } = ownerDocument;
return useRefEffect(
( node ) => {
const { ownerDocument } = node;
const { defaultView } = ownerDocument;

let lastRelative;
let lastRelative;

function updateDragZones( event ) {
if ( lastRelative && lastRelative.contains( event.target ) ) {
return;
}

const dragEventType = getDragEventType( event );
const position = getPosition( event );
const hoveredDropZone = getHoveredDropZone(
dropZones,
position,
dragEventType
);

if ( hoveredDropZone && hoveredDropZone.isRelative ) {
lastRelative = hoveredDropZone.element.current.offsetParent;
} else {
lastRelative = null;
}
function updateDragZones( event ) {
if ( lastRelative && lastRelative.contains( event.target ) ) {
return;
}

// Notifying the dropzones
dropZones.forEach( ( dropZone ) => {
const isDraggingOverDropZone = dropZone === hoveredDropZone;
const newState = {
isDraggingOverDocument: isTypeSupportedByDropZone(
dragEventType,
dropZone
),
isDraggingOverElement: isDraggingOverDropZone,
x:
isDraggingOverDropZone && dropZone.withPosition
? position.x
: null,
y:
isDraggingOverDropZone && dropZone.withPosition
? position.y
: null,
type: isDraggingOverDropZone ? dragEventType : null,
};

dropZone.setState( ( state ) => {
if ( isShallowEqual( state, newState ) ) {
return state;
}
const dragEventType = getDragEventType( event );
const position = getPosition( event );
const hoveredDropZone = getHoveredDropZone(
dropZones,
position,
dragEventType
);

if ( hoveredDropZone && hoveredDropZone.isRelative ) {
lastRelative = hoveredDropZone.element.current.offsetParent;
} else {
lastRelative = null;
}

return newState;
// Notifying the dropzones
dropZones.forEach( ( dropZone ) => {
const isDraggingOverDropZone = dropZone === hoveredDropZone;
const newState = {
isDraggingOverDocument: isTypeSupportedByDropZone(
dragEventType,
dropZone
),
isDraggingOverElement: isDraggingOverDropZone,
x:
isDraggingOverDropZone && dropZone.withPosition
? position.x
: null,
y:
isDraggingOverDropZone && dropZone.withPosition
? position.y
: null,
type: isDraggingOverDropZone ? dragEventType : null,
};

dropZone.setState( ( state ) => {
if ( isShallowEqual( state, newState ) ) {
return state;
}

return newState;
} );
} );
} );

event.preventDefault();
}
event.preventDefault();
}

const throttledUpdateDragZones = throttle( updateDragZones, 200 );
const throttledUpdateDragZones = throttle( updateDragZones, 200 );

function onDragOver( event ) {
throttledUpdateDragZones( event );
event.preventDefault();
}
function onDragOver( event ) {
throttledUpdateDragZones( event );
event.preventDefault();
}

function resetDragState() {
// Avoid throttled drag over handler calls
throttledUpdateDragZones.cancel();
function resetDragState() {
// Avoid throttled drag over handler calls
throttledUpdateDragZones.cancel();

dropZones.forEach( ( dropZone ) =>
dropZone.setState( INITIAL_DROP_ZONE_STATE )
);
}
dropZones.forEach( ( dropZone ) =>
dropZone.setState( INITIAL_DROP_ZONE_STATE )
);
}

function onDrop( event ) {
// This seemingly useless line has been shown to resolve a Safari issue
// where files dragged directly from the dock are not recognized
event.dataTransfer && event.dataTransfer.files.length; // eslint-disable-line no-unused-expressions

const dragEventType = getDragEventType( event );
const position = getPosition( event );
const hoveredDropZone = getHoveredDropZone(
dropZones,
position,
dragEventType
);

resetDragState();

if ( hoveredDropZone ) {
switch ( dragEventType ) {
case 'file':
hoveredDropZone.onFilesDrop(
getFilesFromDataTransfer( event.dataTransfer ),
position
);
break;
case 'html':
hoveredDropZone.onHTMLDrop(
event.dataTransfer.getData( 'text/html' ),
position
);
break;
case 'default':
hoveredDropZone.onDrop( event, position );
function onDrop( event ) {
// This seemingly useless line has been shown to resolve a Safari issue
// where files dragged directly from the dock are not recognized
event.dataTransfer && event.dataTransfer.files.length; // eslint-disable-line no-unused-expressions

const dragEventType = getDragEventType( event );
const position = getPosition( event );
const hoveredDropZone = getHoveredDropZone(
dropZones,
position,
dragEventType
);

resetDragState();

if ( hoveredDropZone ) {
switch ( dragEventType ) {
case 'file':
hoveredDropZone.onFilesDrop(
getFilesFromDataTransfer( event.dataTransfer ),
position
);
break;
case 'html':
hoveredDropZone.onHTMLDrop(
event.dataTransfer.getData( 'text/html' ),
position
);
break;
case 'default':
hoveredDropZone.onDrop( event, position );
}
}
}

event.stopPropagation();
event.preventDefault();
}
event.stopPropagation();
event.preventDefault();
}

defaultView.addEventListener( 'drop', onDrop );
defaultView.addEventListener( 'dragover', onDragOver );
defaultView.addEventListener( 'mouseup', resetDragState );
// Note that `dragend` doesn't fire consistently for file and HTML drag
// events where the drag origin is outside the browser window.
// In Firefox it may also not fire if the originating node is removed.
defaultView.addEventListener( 'dragend', resetDragState );

return () => {
defaultView.removeEventListener( 'drop', onDrop );
defaultView.removeEventListener( 'dragover', onDragOver );
defaultView.removeEventListener( 'mouseup', resetDragState );
defaultView.removeEventListener( 'dragend', resetDragState );
};
}, [ ref, dropZones ] );
node.addEventListener( 'drop', onDrop );
defaultView.addEventListener( 'dragover', onDragOver );
defaultView.addEventListener( 'mouseup', resetDragState );
// Note that `dragend` doesn't fire consistently for file and HTML drag
// events where the drag origin is outside the browser window.
// In Firefox it may also not fire if the originating node is removed.
defaultView.addEventListener( 'dragend', resetDragState );

return () => {
node.removeEventListener( 'drop', onDrop );
defaultView.removeEventListener( 'dragover', onDragOver );
defaultView.removeEventListener( 'mouseup', resetDragState );
defaultView.removeEventListener( 'dragend', resetDragState );
};
},
[ dropZones ]
);
}

export function DropZoneContextProvider( props ) {
Expand All @@ -246,8 +245,7 @@ export function DropZoneContextProvider( props ) {
}

function DropContainer( { children } ) {
const ref = useRef();
useDrop( ref );
const ref = useDrop();
return (
<div ref={ ref } className="components-drop-zone__provider">
{ children }
Expand Down
24 changes: 24 additions & 0 deletions packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,30 @@ _Returns_

- `boolean`: Reduced motion preference value.

<a name="useRefEffect" href="#useRefEffect">#</a> **useRefEffect**

Effect-like ref callback. Just like with `useEffect`, this allows you to
return a cleanup function to be run if the ref changes or one of the
dependencies changes. The ref is provided as an argument to the callback
functions. The main difference between this and `useEffect` is that
the `useEffect` callback is not called when the ref changes, but this is.
Pass the returned ref callback as the component's ref and merge multiple refs
with `useMergeRefs`.

It's worth noting that if the dependencies array is empty, there's not
strictly a need to clean up event handlers for example, because the node is
to be removed. It _is_ necessary if you add dependencies because the ref
callback will be called multiple times for the same node.

_Parameters_

- _calllback_ `Function`: Callback with ref as argument.
- _dependencies_ `Array`: Dependencies of the callback.

_Returns_

- `Function`: Ref callback.

<a name="useResizeObserver" href="#useResizeObserver">#</a> **useResizeObserver**

Hook which allows to listen the resize event of any target element when it changes sizes.
Expand Down
Loading