Skip to content

Commit

Permalink
Merge branch 'WordPress:trunk' into trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
karthick-murugan authored Dec 5, 2024
2 parents f226ae0 + 3ea3ba2 commit d074ba9
Show file tree
Hide file tree
Showing 71 changed files with 658 additions and 246 deletions.
249 changes: 249 additions & 0 deletions changelog.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ Create a break between ideas or sections with a horizontal separator. ([Source](
- **Name:** core/separator
- **Category:** design
- **Supports:** align (center, full, wide), anchor, color (background, gradients, ~~enableContrastChecker~~, ~~text~~), interactivity (clientNavigation), spacing (margin)
- **Attributes:** opacity
- **Attributes:** opacity, tagName

## Shortcode

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,15 +745,15 @@ When using region-based navigation, it's crucial to ensure that your interactive

`getServerState()` allows you to subscribe to changes in the **global state** that occur during client-side navigation. This function is analogous to `getServerContext()`, but it works with the global state instead of the local context.

The `getServerState()` function returns a read-only reactive object. This means that any [callbacks](/docs/reference-guides/interactivity-api/api-reference.md#accessing-data-in-callbacks) you have defined that watch the returned object will only trigger when the value returned by the function changes. If the value remains the same, the callback will not re-trigger.

Let's consider a quiz that has multiple questions. Each question is a separate page. When the user navigates to a new question, the server provides the new question and the time left to answer all the questions.

```php
<?php
wp_interactivity_state( 'myPlugin', array(
<div <?php echo wp_interactivity_state( 'myPlugin', array(
'question' => get_question_for_page( get_the_ID() ),
'timeLeft' => 5 * 60, // Time to answer all the questions.
) );
?>
) ); ?>>
```

```javascript
Expand Down Expand Up @@ -789,14 +789,14 @@ store( 'myPlugin', {

`getServerContext()` allows you to subscribe to changes in the **local context** that occur during client-side navigation. This function is analogous to `getServerState()`, but it works with the local context instead of the global state.

The `getServerContext()` function returns a read-only reactive object. This means that any [callbacks](/docs/reference-guides/interactivity-api/api-reference.md#accessing-data-in-callbacks) you have defined that watch the returned object will only trigger when the value returned by the function changes. If the value remains the same, the callback will not re-trigger.

Consider a quiz that has multiple questions. Each question is a separate page. When the user navigates to a new question, the server provides the new question and the time left to answer all the questions.

```php
<?php
wp_interactivity_context( 'myPlugin', array(
<div <?php echo wp_interactivity_data_wp_context( array(
'currentQuestion' => get_question_for_page( get_the_ID() ),
) );
?>
), ); ?>>
```

```javascript
Expand Down Expand Up @@ -830,12 +830,12 @@ store( 'myPlugin', {

### When to Use

Whenever you have interactive blocks that rely on global state that may change due to navigation events, ensuring consistency across different parts of your application.
Whenever you have interactive blocks that rely on global state or local context that may change due to navigation events, ensuring consistency across different parts of your application.

### Best Practices for using `getServerState()` and `getServerContext()`

- **Read-Only References:** Both `getServerState()` and `getServerContext()` return read-only objects. You can use those objects to update the global state or local context.
- **Callback Integration:** Incorporate these functions within your store [callbacks](/docs/reference-guides/interactivity-api/api-reference.md#accessing-data-in-callbacks) to react to state and context changes.
- **Callback Integration:** Incorporate these functions within your store [callbacks](/docs/reference-guides/interactivity-api/api-reference.md#accessing-data-in-callbacks) to react to state and context changes. Both `getServerState()` and `getServerContext()` return reactive objects. This means that their watch callbacks will only trigger when the value of a property changes. If the value remains the same, the callback will not re-trigger.

## Conclusion

Expand Down
2 changes: 1 addition & 1 deletion gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Printing since 1440. This is the development plugin for the block editor, site editor, and other future WordPress core functionality.
* Requires at least: 6.6
* Requires PHP: 7.2
* Version: 19.8.0-rc.1
* Version: 19.8.0
* Author: Gutenberg Team
* Text Domain: gutenberg
*
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gutenberg",
"version": "19.8.0-rc.1",
"version": "19.8.0",
"private": true,
"description": "A new WordPress editor experience.",
"author": "The WordPress Contributors",
Expand Down
6 changes: 2 additions & 4 deletions packages/block-editor/src/autocompleters/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ import { orderInserterBlockItems } from '../utils/order-inserter-block-items';
const noop = () => {};
const SHOWN_BLOCK_TYPES = 9;

/** @typedef {import('@wordpress/components').WPCompleter} WPCompleter */

/**
* Creates a blocks repeater for replacing the current block with a selected block type.
*
* @return {WPCompleter} A blocks completer.
* @return {Object} A blocks completer.
*/
function createBlockCompleter() {
return {
Expand Down Expand Up @@ -157,6 +155,6 @@ function createBlockCompleter() {
/**
* Creates a blocks repeater for replacing the current block with a selected block type.
*
* @return {WPCompleter} A blocks completer.
* @return {Object} A blocks completer.
*/
export default createBlockCompleter();
6 changes: 2 additions & 4 deletions packages/block-editor/src/autocompleters/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import { decodeEntities } from '@wordpress/html-entities';

const SHOWN_SUGGESTIONS = 10;

/** @typedef {import('@wordpress/components').WPCompleter} WPCompleter */

/**
* Creates a suggestion list for links to posts or pages.
*
* @return {WPCompleter} A links completer.
* @return {Object} A links completer.
*/
function createLinkCompleter() {
return {
Expand Down Expand Up @@ -60,6 +58,6 @@ function createLinkCompleter() {
/**
* Creates a suggestion list for links to posts or pages..
*
* @return {WPCompleter} A link completer.
* @return {Object} A link completer.
*/
export default createLinkCompleter();
19 changes: 6 additions & 13 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ import clsx from 'clsx';
/**
* WordPress dependencies
*/
import {
memo,
useCallback,
RawHTML,
useContext,
useMemo,
} from '@wordpress/element';
import { memo, RawHTML, useContext, useMemo } from '@wordpress/element';
import {
getBlockType,
getSaveContent,
Expand All @@ -28,7 +22,7 @@ import {
store as blocksStore,
} from '@wordpress/blocks';
import { withFilters } from '@wordpress/components';
import { withDispatch, useDispatch, useSelect } from '@wordpress/data';
import { withDispatch, useSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { safeHTML } from '@wordpress/dom';

Expand Down Expand Up @@ -103,6 +97,7 @@ function BlockListBlock( {
wrapperProps,
setAttributes,
onReplace,
onRemove,
onInsertBlocksAfter,
onMerge,
toggleSelection,
Expand All @@ -113,11 +108,6 @@ function BlockListBlock( {
themeSupportsLayout,
...context
} = useContext( PrivateBlockContext );
const { removeBlock } = useDispatch( blockEditorStore );
const onRemove = useCallback(
() => removeBlock( clientId ),
[ clientId, removeBlock ]
);

const parentLayout = useLayout() || {};

Expand Down Expand Up @@ -537,6 +527,9 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => {
initialPosition
);
},
onRemove() {
removeBlock( ownProps.clientId );
},
toggleSelection( selectionEnabled ) {
toggleSelection( selectionEnabled );
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
canMove,
} = useContext( PrivateBlockContext );

const canDrag = canMove && ! hasChildSelected;

// translators: %s: Type of block (i.e. Text, Image etc)
const blockLabel = sprintf( __( 'Block: %s' ), blockTitle );
const htmlSuffix = mode === 'html' && ! __unstableIsHtml ? '-visual' : '';
Expand All @@ -125,7 +123,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
isEnabled: isSectionBlock,
} ),
useScrollIntoView( { isSelected } ),
canDrag ? ffDragRef : undefined,
canMove ? ffDragRef : undefined,
] );

const blockEditContext = useBlockEditContext();
Expand Down Expand Up @@ -158,7 +156,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {

return {
tabIndex: blockEditingMode === 'disabled' ? -1 : 0,
draggable: canDrag ? true : undefined,
draggable: canMove && ! hasChildSelected ? true : undefined,
...wrapperProps,
...props,
ref: mergedRefs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,68 @@
*/
import { useRefEffect } from '@wordpress/compose';

const nodesByDocument = new Map();

function add( doc, node ) {
let set = nodesByDocument.get( doc );
if ( ! set ) {
set = new Set();
nodesByDocument.set( doc, set );
doc.addEventListener( 'pointerdown', down );
}
set.add( node );
}

function remove( doc, node ) {
const set = nodesByDocument.get( doc );
if ( set ) {
set.delete( node );
restore( node );
if ( set.size === 0 ) {
nodesByDocument.delete( doc );
doc.removeEventListener( 'pointerdown', down );
}
}
}

function restore( node ) {
const prevDraggable = node.getAttribute( 'data-draggable' );
if ( prevDraggable ) {
node.removeAttribute( 'data-draggable' );
// Only restore if `draggable` is still removed. It could have been
// changed by React in the meantime.
if ( prevDraggable === 'true' && ! node.getAttribute( 'draggable' ) ) {
node.setAttribute( 'draggable', 'true' );
}
}
}

function down( event ) {
const { target } = event;
const { ownerDocument, isContentEditable } = target;
const nodes = nodesByDocument.get( ownerDocument );

if ( isContentEditable ) {
// Whenever an editable element is clicked, check which draggable
// blocks contain this element, and temporarily disable draggability.
for ( const node of nodes ) {
if (
node.getAttribute( 'draggable' ) === 'true' &&
node.contains( target )
) {
node.removeAttribute( 'draggable' );
node.setAttribute( 'data-draggable', 'true' );
}
}
} else {
// Whenever a non-editable element is clicked, re-enable draggability
// for any blocks that were previously disabled.
for ( const node of nodes ) {
restore( node );
}
}
}

/**
* In Firefox, the `draggable` and `contenteditable` attributes don't play well
* together. When `contenteditable` is within a `draggable` element, selection
Expand All @@ -13,13 +75,9 @@ import { useRefEffect } from '@wordpress/compose';
*/
export function useFirefoxDraggableCompatibility() {
return useRefEffect( ( node ) => {
function onDown( event ) {
node.draggable = ! event.target.isContentEditable;
}
const { ownerDocument } = node;
ownerDocument.addEventListener( 'pointerdown', onDown );
add( node.ownerDocument, node );
return () => {
ownerDocument.removeEventListener( 'pointerdown', onDown );
remove( node.ownerDocument, node );
};
}, [] );
}
14 changes: 8 additions & 6 deletions packages/block-editor/src/components/block-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export function PrivateBlockToolbar( {
( id ) => getTemplateLock( id ) === 'contentOnly'
);

const _isZoomOut = isZoomOut();

return {
blockClientId: selectedBlockClientId,
blockClientIds: selectedBlockClientIds,
Expand All @@ -132,7 +134,7 @@ export function PrivateBlockToolbar( {
shouldShowVisualToolbar: isValid && isVisual,
toolbarKey: `${ selectedBlockClientId }${ parentClientId }`,
showParentSelector:
! isZoomOut() &&
! _isZoomOut &&
parentBlockType &&
getBlockEditingMode( parentClientId ) !== 'disabled' &&
hasBlockSupport(
Expand All @@ -144,11 +146,11 @@ export function PrivateBlockToolbar( {
isUsingBindings: _isUsingBindings,
hasParentPattern: _hasParentPattern,
hasContentOnlyLocking: _hasTemplateLock,
showShuffleButton: isZoomOut(),
showSlots: ! isZoomOut(),
showGroupButtons: ! isZoomOut(),
showLockButtons: ! isZoomOut(),
showSwitchSectionStyleButton: isZoomOut(),
showShuffleButton: _isZoomOut,
showSlots: ! _isZoomOut,
showGroupButtons: ! _isZoomOut,
showLockButtons: ! _isZoomOut,
showSwitchSectionStyleButton: _isZoomOut,
hasFixedToolbar: getSettings().hasFixedToolbar,
isNavigationMode: _isNavigationMode(),
};
Expand Down
5 changes: 5 additions & 0 deletions packages/block-library/src/separator/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
"opacity": {
"type": "string",
"default": "alpha-channel"
},
"tagName": {
"type": "string",
"enum": [ "hr", "div" ],
"default": "hr"
}
},
"supports": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/separator/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const v1 = {
style: customColor
? { color: { background: customColor } }
: undefined,
tagName: 'hr',
};
},
};
Expand Down
Loading

0 comments on commit d074ba9

Please sign in to comment.