From 94efe0c7acbb7b0fd2436df60aa96f334ebfa0d7 Mon Sep 17 00:00:00 2001 From: Staci Cooper Date: Thu, 24 Feb 2022 17:23:10 -0800 Subject: [PATCH] Remove logic from serializer in favor of using cloneBlock in copy handlers --- .../block-settings-dropdown.js | 15 +++++---- .../src/components/copy-handler/index.js | 14 +++++--- packages/blocks/src/api/serializer.js | 25 +++------------ packages/blocks/src/api/test/serializer.js | 32 ------------------- 4 files changed, 22 insertions(+), 64 deletions(-) diff --git a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js index 8baa37567629dd..f5dfc8ae4ad0b4 100644 --- a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js +++ b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js @@ -11,7 +11,7 @@ import { DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components'; import { useSelect } from '@wordpress/data'; import { moreVertical } from '@wordpress/icons'; import { Children, cloneElement, useCallback } from '@wordpress/element'; -import { serialize } from '@wordpress/blocks'; +import { cloneBlock, serialize } from '@wordpress/blocks'; import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts'; import { useCopyToClipboard } from '@wordpress/compose'; @@ -33,13 +33,14 @@ const POPOVER_PROPS = { }; function CopyMenuItem( { blocks, onCopy } ) { - const ref = useCopyToClipboard( - () => - serialize( blocks, { + const ref = useCopyToClipboard( () => { + blocks = blocks.map( ( block ) => + cloneBlock( block, {}, null, { __experimentalExcludeAttributes: { copy: false }, - } ), - onCopy - ); + } ) + ); + return serialize( blocks ); + }, onCopy ); return { __( 'Copy' ) }; } diff --git a/packages/block-editor/src/components/copy-handler/index.js b/packages/block-editor/src/components/copy-handler/index.js index 8fe029d262111a..8c558dda21cf24 100644 --- a/packages/block-editor/src/components/copy-handler/index.js +++ b/packages/block-editor/src/components/copy-handler/index.js @@ -3,6 +3,7 @@ */ import { useCallback } from '@wordpress/element'; import { + cloneBlock, serialize, pasteHandler, store as blocksStore, @@ -121,10 +122,15 @@ export function useClipboardHandler() { flashBlock( selectedBlockClientIds[ 0 ] ); } notifyCopy( event.type, selectedBlockClientIds ); - const blocks = getBlocksByClientId( selectedBlockClientIds ); - const serialized = serialize( blocks, { - __experimentalExcludeAttributes: { copy: event.type !== 'copy' }, - } ); + let blocks = getBlocksByClientId( selectedBlockClientIds ); + if ( event.type === 'copy' ) { + blocks = blocks.map( ( block ) => + cloneBlock( block, {}, null, { + __experimentalExcludeAttributes: { copy: false }, + } ) + ); + } + const serialized = serialize( blocks ); event.clipboardData.setData( 'text/plain', serialized ); event.clipboardData.setData( 'text/html', serialized ); diff --git a/packages/blocks/src/api/serializer.js b/packages/blocks/src/api/serializer.js index 705d7d2addcfda..70ebd72883c39a 100644 --- a/packages/blocks/src/api/serializer.js +++ b/packages/blocks/src/api/serializer.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { isEmpty, reduce, isObject, castArray, startsWith, omit } from 'lodash'; +import { isEmpty, reduce, isObject, castArray, startsWith } from 'lodash'; /** * WordPress dependencies @@ -24,17 +24,12 @@ import { getFreeformContentHandlerName, getUnregisteredTypeHandlerName, } from './registration'; -import { - isUnmodifiedDefaultBlock, - normalizeBlockType, - __experimentalFilterBlockAttributes, -} from './utils'; +import { isUnmodifiedDefaultBlock, normalizeBlockType } from './utils'; /** * @typedef {Object} WPBlockSerializationOptions Serialization Options. * * @property {boolean} isInnerBlocks Whether we are serializing inner blocks. - * @property {Object} __experimentalExcludeAttributes Attributes matching these filters will be excluded from the serialized block. */ /** @@ -347,10 +342,7 @@ export function getCommentDelimitedContent( * * @return {string} Serialized block. */ -export function serializeBlock( - block, - { isInnerBlocks = false, __experimentalExcludeAttributes } = {} -) { +export function serializeBlock( block, { isInnerBlocks = false } = {} ) { const blockName = block.name; const saveContent = getBlockInnerHTML( block ); @@ -362,16 +354,7 @@ export function serializeBlock( } const blockType = getBlockType( blockName ); - let saveAttributes = getCommentAttributes( blockType, block.attributes ); - - if ( __experimentalExcludeAttributes ) { - saveAttributes = omit( - saveAttributes, - __experimentalFilterBlockAttributes( blockName, { - __experimentalSupports: __experimentalExcludeAttributes, - } ) - ); - } + const saveAttributes = getCommentAttributes( blockType, block.attributes ); return getCommentDelimitedContent( blockName, saveAttributes, saveContent ); } diff --git a/packages/blocks/src/api/test/serializer.js b/packages/blocks/src/api/test/serializer.js index 2c91274cab6db8..513eadeb84104c 100644 --- a/packages/blocks/src/api/test/serializer.js +++ b/packages/blocks/src/api/test/serializer.js @@ -327,12 +327,6 @@ describe( 'block serializer', () => { stuff: { type: 'string', }, - internal: { - type: 'string', - __experimentalSupports: { - copy: false, - }, - }, }, save( { attributes } ) { if ( attributes.throw ) { @@ -384,32 +378,6 @@ describe( 'block serializer', () => { '\nCorrect\n' ); } ); - - it( 'should serialize all attributes by default', () => { - const block = createBlock( 'core/test-block', { - content: 'content', - internal: 'copy me', - } ); - - expect( serialize( block ) ).toEqual( - '\n

content

\n' - ); - } ); - - it( 'should omit attributes without copy support when configured with __experimentalExcludeAttributes', () => { - const block = createBlock( 'core/test-block', { - content: 'content', - internal: 'copy me', - } ); - - expect( - serialize( block, { - __experimentalExcludeAttributes: { copy: false }, - } ) - ).toEqual( - '\n

content

\n' - ); - } ); } ); describe( 'getBlockInnerHTML', () => {