-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pullquote tranformations (#23562)
* add pullquote tranformations This will be refactored. * leave raw transformation to be handled by Quote only * fix e2e tests * merge quote/pullquote tranforms from list
- Loading branch information
1 parent
9e0c923
commit 21709e3
Showing
4 changed files
with
139 additions
and
4 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
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,111 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { create, join, split, toHTMLString } from '@wordpress/rich-text'; | ||
|
||
const transforms = { | ||
from: [ | ||
{ | ||
type: 'block', | ||
isMultiBlock: true, | ||
blocks: [ 'core/paragraph' ], | ||
transform: ( attributes ) => { | ||
return createBlock( 'core/pullquote', { | ||
value: toHTMLString( { | ||
value: join( | ||
attributes.map( ( { content } ) => | ||
create( { html: content } ) | ||
), | ||
'\u2028' | ||
), | ||
multilineTag: 'p', | ||
} ), | ||
anchor: attributes.anchor, | ||
} ); | ||
}, | ||
}, | ||
{ | ||
type: 'block', | ||
blocks: [ 'core/heading' ], | ||
transform: ( { content, anchor } ) => { | ||
return createBlock( 'core/pullquote', { | ||
value: `<p>${ content }</p>`, | ||
anchor, | ||
} ); | ||
}, | ||
}, | ||
], | ||
to: [ | ||
{ | ||
type: 'block', | ||
blocks: [ 'core/paragraph' ], | ||
transform: ( { value, citation } ) => { | ||
const paragraphs = []; | ||
if ( value && value !== '<p></p>' ) { | ||
paragraphs.push( | ||
...split( | ||
create( { html: value, multilineTag: 'p' } ), | ||
'\u2028' | ||
).map( ( piece ) => | ||
createBlock( 'core/paragraph', { | ||
content: toHTMLString( { value: piece } ), | ||
} ) | ||
) | ||
); | ||
} | ||
if ( citation && citation !== '<p></p>' ) { | ||
paragraphs.push( | ||
createBlock( 'core/paragraph', { | ||
content: citation, | ||
} ) | ||
); | ||
} | ||
if ( paragraphs.length === 0 ) { | ||
return createBlock( 'core/paragraph', { | ||
content: '', | ||
} ); | ||
} | ||
return paragraphs; | ||
}, | ||
}, | ||
{ | ||
type: 'block', | ||
blocks: [ 'core/heading' ], | ||
transform: ( { value, citation, ...attrs } ) => { | ||
// If there is no pullquote content, use the citation as the | ||
// content of the resulting heading. A nonexistent citation | ||
// will result in an empty heading. | ||
if ( value === '<p></p>' ) { | ||
return createBlock( 'core/heading', { | ||
content: citation, | ||
} ); | ||
} | ||
const pieces = split( | ||
create( { html: value, multilineTag: 'p' } ), | ||
'\u2028' | ||
); | ||
const headingBlock = createBlock( 'core/heading', { | ||
content: toHTMLString( { value: pieces[ 0 ] } ), | ||
} ); | ||
if ( ! citation && pieces.length === 1 ) { | ||
return headingBlock; | ||
} | ||
const quotePieces = pieces.slice( 1 ); | ||
const pullquoteBlock = createBlock( 'core/pullquote', { | ||
...attrs, | ||
citation, | ||
value: toHTMLString( { | ||
value: quotePieces.length | ||
? join( pieces.slice( 1 ), '\u2028' ) | ||
: create(), | ||
multilineTag: 'p', | ||
} ), | ||
} ); | ||
return [ headingBlock, pullquoteBlock ]; | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
export default transforms; |
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