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

Proposal: Block Params #49391

Closed
wants to merge 5 commits into from
Closed
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
5 changes: 4 additions & 1 deletion packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function BlockListBlock( {
onInsertBlocksAfter,
onMerge,
toggleSelection,
params,
} ) {
const {
themeSupportsLayout,
Expand Down Expand Up @@ -155,6 +156,7 @@ function BlockListBlock( {
__unstableParentLayout={
Object.keys( parentLayout ).length ? parentLayout : undefined
}
params={ params }
/>
);

Expand Down Expand Up @@ -283,7 +285,7 @@ const applyWithSelect = withSelect( ( select, { clientId, rootClientId } ) => {
// This function should never be called when a block is not present in
// the state. It happens now because the order in withSelect rendering
// is not correct.
const { name, attributes, isValid } = block || {};
const { name, attributes, isValid, params } = block || {};

// Do not add new properties here, use `useSelect` instead to avoid
// leaking new props to the public API (editor.BlockListBlock filter).
Expand All @@ -302,6 +304,7 @@ const applyWithSelect = withSelect( ( select, { clientId, rootClientId } ) => {
attributes,
isValid,
isSelected,
params,
};
} );

Expand Down
54 changes: 37 additions & 17 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
store as blockEditorStore,
__experimentalGetElementClassName,
} from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';
import { useEffect, useState } from '@wordpress/element';
import { useCopyToClipboard } from '@wordpress/compose';
import { __, _x } from '@wordpress/i18n';
import { file as icon } from '@wordpress/icons';
Expand Down Expand Up @@ -59,7 +59,13 @@ function ClipboardToolbarButton( { text, disabled } ) {
);
}

function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
function FileEdit( {
attributes,
isSelected,
setAttributes,
clientId,
params,
} ) {
const {
id,
fileId,
Expand All @@ -72,6 +78,9 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
displayPreview,
previewHeight,
} = attributes;

const { blobURL } = params;

const { media, mediaUpload } = useSelect(
( select ) => ( {
media:
Expand All @@ -87,22 +96,27 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
const { toggleSelection, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );

const [ isUploadingBlob, setIsUploadingBlob ] = useState( false );

useEffect( () => {
// Upload a file drag-and-dropped into the editor.
if ( isBlobURL( href ) ) {
const file = getBlobByURL( href );
const file = getBlobByURL( blobURL );
if ( file ) {
setIsUploadingBlob( true );

mediaUpload( {
filesList: [ file ],
onFileChange: ( [ newMedia ] ) => onSelectFile( newMedia ),
onError: onUploadError,
onFileChange: ( [ newMedia ] ) => {
onSelectFile( newMedia, { isPersistent: false } );
setIsUploadingBlob( false );
},
onError: ( message ) => {
onUploadError( message, { isPersistent: false } );
setIsUploadingBlob( false );
},
} );

revokeBlobURL( href );
}

if ( downloadButtonText === undefined ) {
changeDownloadButtonText( _x( 'Download', 'button label' ) );
revokeBlobURL( blobURL );
}
}, [] );

Expand All @@ -114,9 +128,12 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
}
}, [ href, fileId, clientId ] );

function onSelectFile( newMedia ) {
if ( newMedia && newMedia.url ) {
function onSelectFile( newMedia, { isPersistent = true } = {} ) {
if ( newMedia && newMedia.url && ! isBlobURL( newMedia.url ) ) {
const isPdf = newMedia.url.endsWith( '.pdf' );
if ( ! isPersistent ) {
__unstableMarkNextChangeAsNotPersistent();
}
setAttributes( {
href: newMedia.url,
fileName: newMedia.title,
Expand Down Expand Up @@ -178,9 +195,9 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {

const blockProps = useBlockProps( {
className: classnames(
isBlobURL( href ) && getAnimateClassName( { type: 'loading' } ),
isUploadingBlob && getAnimateClassName( { type: 'loading' } ),
{
'is-transient': isBlobURL( href ),
'is-transient': isUploadingBlob,
}
),
} );
Expand Down Expand Up @@ -232,7 +249,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
/>
<ClipboardToolbarButton
text={ href }
disabled={ isBlobURL( href ) }
disabled={ isUploadingBlob }
/>
</BlockControls>
<div { ...blockProps }>
Expand Down Expand Up @@ -297,7 +314,10 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
'button'
)
) }
value={ downloadButtonText }
value={
downloadButtonText ??
_x( 'Download', 'button label' )
Copy link
Member

@gziolo gziolo Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting that this means that the translated Download text will no longer get serialized in the saved content. There is a hack to ensure this value gets set upon insertion using a side effect to avoid validation issues when the user changes the language. It was previously assigned to the default value wrapped with i18n helper.

Ideally, it would be a dynamic token that gets translated to a current locale on both the client and the server.

}
withoutInteractiveFormatting
placeholder={ __( 'Add text…' ) }
onChange={ ( text ) =>
Expand Down
6 changes: 2 additions & 4 deletions packages/block-library/src/file/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ const transforms = {

// File will be uploaded in componentDidMount()
blocks.push(
createBlock( 'core/file', {
href: blobURL,
fileName: file.name,
textLinkHref: blobURL,
createBlock( 'core/file', {}, [], {
blobURL,
} )
);
} );
Expand Down
39 changes: 28 additions & 11 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function Navigation( {
setOverlayBackgroundColor,
overlayTextColor,
setOverlayTextColor,
params,

// These props are used by the navigation editor to override specific
// navigation block settings.
Expand Down Expand Up @@ -402,18 +403,34 @@ function Navigation( {
] = useState();
const [ detectedOverlayColor, setDetectedOverlayColor ] = useState();

const onSelectClassicMenu = async ( classicMenu ) => {
const navMenu = await convertClassicMenu(
classicMenu.id,
classicMenu.name,
'draft'
);
if ( navMenu ) {
handleUpdateMenu( navMenu.id, {
focusNavigationBlock: true,
} );
const onSelectClassicMenu = useCallback(
async ( classicMenu ) => {
const navMenu = await convertClassicMenu(
classicMenu.id,
classicMenu.name,
'draft'
);
if ( navMenu ) {
handleUpdateMenu( navMenu.id, {
focusNavigationBlock: true,
} );
}
},
[ convertClassicMenu, handleUpdateMenu ]
);

// Convert the classic menu provided by the Legacy Widget block transform if
// it exists.
useEffect( () => {
if ( params.menuId ) {
const classicMenu = classicMenus?.find(
( menu ) => menu.id === params.menuId
);
if ( classicMenu ) {
onSelectClassicMenu( classicMenu );
}
}
};
}, [ params.menuId, classicMenus, onSelectClassicMenu ] );

const onSelectNavigationMenu = ( menuId ) => {
handleUpdateMenu( menuId );
Expand Down
5 changes: 3 additions & 2 deletions packages/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ Returns a block object given its type and attributes.
_Parameters_

- _name_ `string`: Block name.
- _attributes_ `Object`: Block attributes.
- _innerBlocks_ `?Array`: Nested blocks.
- _attributes_ `[Object]`: Block attributes.
- _innerBlocks_ `[Array]`: Nested blocks.
- _params_ `[Object]`: Block params.

_Returns_

Expand Down
16 changes: 12 additions & 4 deletions packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ import {
/**
* Returns a block object given its type and attributes.
*
* @param {string} name Block name.
* @param {Object} attributes Block attributes.
* @param {?Array} innerBlocks Nested blocks.
* @param {string} name Block name.
* @param {Object} [attributes] Block attributes.
* @param {Array} [innerBlocks] Nested blocks.
* @param {Object} [params] Block params.
*
* @return {Object} Block object.
*
*/
export function createBlock( name, attributes = {}, innerBlocks = [] ) {
export function createBlock(
name,
attributes = {},
innerBlocks = [],
params = {}
) {
const sanitizedAttributes = __experimentalSanitizeBlockAttributes(
name,
attributes
Expand All @@ -46,6 +53,7 @@ export function createBlock( name, attributes = {}, innerBlocks = [] ) {
isValid: true,
attributes: sanitizedAttributes,
innerBlocks,
params,
};
}

Expand Down
6 changes: 3 additions & 3 deletions packages/edit-widgets/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ export const saveWidgetArea =
const widget = preservedRecords[ i ];
const { block, position } = batchMeta[ i ];

// Set __internalWidgetId on the block. This will be persisted to the
// store when we dispatch receiveEntityRecords( post ) below.
post.blocks[ position ].attributes.__internalWidgetId = widget.id;
// Set widget ID on the block. This will be persisted to the store
// when we dispatch receiveEntityRecords( post ) below.
post.blocks[ position ].params.widgetId = widget.id;

const error = registry
.select( coreStore )
Expand Down
Loading