-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try consolidating some of the copy and paste behaviour
- Loading branch information
1 parent
68ed6c7
commit 885b147
Showing
3 changed files
with
102 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
packages/block-editor/src/components/writing-flow/utils.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __unstableStripHTML as stripHTML } from '@wordpress/dom'; | ||
import { | ||
serialize, | ||
createBlock, | ||
pasteHandler, | ||
findTransform, | ||
getBlockTransforms, | ||
} from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getPasteEventData } from '../../utils/pasting'; | ||
|
||
export function setClipboardBlocks( event, blocks ) { | ||
let _blocks = blocks; | ||
const wrapperBlockName = event.clipboardData.getData( | ||
'__unstableWrapperBlockName' | ||
); | ||
|
||
if ( wrapperBlockName ) { | ||
_blocks = createBlock( | ||
wrapperBlockName, | ||
JSON.parse( | ||
event.clipboardData.getData( | ||
'__unstableWrapperBlockAttributes' | ||
) | ||
), | ||
_blocks | ||
); | ||
} | ||
|
||
const serialized = serialize( _blocks ); | ||
|
||
event.clipboardData.setData( 'text/plain', toPlainText( serialized ) ); | ||
event.clipboardData.setData( 'text/html', serialized ); | ||
} | ||
|
||
export function getPasteBlocks( event, canUserUseUnfilteredHTML ) { | ||
const { plainText, html, files } = getPasteEventData( event ); | ||
let blocks = []; | ||
|
||
if ( files.length ) { | ||
const fromTransforms = getBlockTransforms( 'from' ); | ||
blocks = files | ||
.reduce( ( accumulator, file ) => { | ||
const transformation = findTransform( | ||
fromTransforms, | ||
( transform ) => | ||
transform.type === 'files' && | ||
transform.isMatch( [ file ] ) | ||
); | ||
if ( transformation ) { | ||
accumulator.push( transformation.transform( [ file ] ) ); | ||
} | ||
return accumulator; | ||
}, [] ) | ||
.flat(); | ||
} else { | ||
blocks = pasteHandler( { | ||
HTML: html, | ||
plainText, | ||
mode: 'BLOCKS', | ||
canUserUseUnfilteredHTML, | ||
} ); | ||
} | ||
|
||
return blocks; | ||
} | ||
|
||
/** | ||
* Given a string of HTML representing serialized blocks, returns the plain | ||
* text extracted after stripping the HTML of any tags and fixing line breaks. | ||
* | ||
* @param {string} html Serialized blocks. | ||
* @return {string} The plain-text content with any html removed. | ||
*/ | ||
function toPlainText( html ) { | ||
// Manually handle BR tags as line breaks prior to `stripHTML` call | ||
html = html.replace( /<br>/g, '\n' ); | ||
|
||
const plainText = stripHTML( html ).trim(); | ||
|
||
// Merge any consecutive line breaks | ||
return plainText.replace( /\n\n+/g, '\n\n' ); | ||
} |