Skip to content

Commit

Permalink
Remove logic from serializer in favor of using cloneBlock in copy han…
Browse files Browse the repository at this point in the history
…dlers
  • Loading branch information
stacimc committed Mar 17, 2022
1 parent ccba197 commit 94efe0c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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 <MenuItem ref={ ref }>{ __( 'Copy' ) }</MenuItem>;
}

Expand Down
14 changes: 10 additions & 4 deletions packages/block-editor/src/components/copy-handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { useCallback } from '@wordpress/element';
import {
cloneBlock,
serialize,
pasteHandler,
store as blocksStore,
Expand Down Expand Up @@ -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 );
Expand Down
25 changes: 4 additions & 21 deletions packages/blocks/src/api/serializer.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
*/

/**
Expand Down Expand Up @@ -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 );

Expand All @@ -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 );
}
Expand Down
32 changes: 0 additions & 32 deletions packages/blocks/src/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,6 @@ describe( 'block serializer', () => {
stuff: {
type: 'string',
},
internal: {
type: 'string',
__experimentalSupports: {
copy: false,
},
},
},
save( { attributes } ) {
if ( attributes.throw ) {
Expand Down Expand Up @@ -384,32 +378,6 @@ describe( 'block serializer', () => {
'<!-- wp:test-block {"throw":true} -->\nCorrect\n<!-- /wp:test-block -->'
);
} );

it( 'should serialize all attributes by default', () => {
const block = createBlock( 'core/test-block', {
content: 'content',
internal: 'copy me',
} );

expect( serialize( block ) ).toEqual(
'<!-- wp:test-block {"internal":"copy me"} -->\n<p>content</p>\n<!-- /wp:test-block -->'
);
} );

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(
'<!-- wp:test-block -->\n<p>content</p>\n<!-- /wp:test-block -->'
);
} );
} );

describe( 'getBlockInnerHTML', () => {
Expand Down

0 comments on commit 94efe0c

Please sign in to comment.