-
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.
RichText/Footnotes: make getRichTextValues work with InnerBlocks.Cont…
…ent (#52241) * RichText/Footnotes: make getRichTextValues work with InnerBlocks.Content --------- Co-authored-by: Miguel Fonseca <150562+mcsf@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
193 additions
and
47 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
95 changes: 95 additions & 0 deletions
95
packages/block-editor/src/components/rich-text/get-rich-text-values.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,95 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { RawHTML, StrictMode, Fragment } from '@wordpress/element'; | ||
import { | ||
getSaveElement, | ||
__unstableGetBlockProps as getBlockProps, | ||
} from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import InnerBlocks from '../inner-blocks'; | ||
import { Content } from './content'; | ||
|
||
/* | ||
* This function is similar to `@wordpress/element`'s `renderToString` function, | ||
* except that it does not render the elements to a string, but instead collects | ||
* the values of all rich text `Content` elements. | ||
*/ | ||
function addValuesForElement( element, ...args ) { | ||
if ( null === element || undefined === element || false === element ) { | ||
return; | ||
} | ||
|
||
if ( Array.isArray( element ) ) { | ||
return addValuesForElements( element, ...args ); | ||
} | ||
|
||
switch ( typeof element ) { | ||
case 'string': | ||
case 'number': | ||
return; | ||
} | ||
|
||
const { type, props } = element; | ||
|
||
switch ( type ) { | ||
case StrictMode: | ||
case Fragment: | ||
return addValuesForElements( props.children, ...args ); | ||
case RawHTML: | ||
return; | ||
case InnerBlocks.Content: | ||
return addValuesForBlocks( ...args ); | ||
case Content: | ||
const [ values ] = args; | ||
values.push( props.value ); | ||
return; | ||
} | ||
|
||
switch ( typeof type ) { | ||
case 'string': | ||
if ( typeof props.children !== 'undefined' ) { | ||
return addValuesForElements( props.children, ...args ); | ||
} | ||
return; | ||
case 'function': | ||
if ( | ||
type.prototype && | ||
typeof type.prototype.render === 'function' | ||
) { | ||
return addValuesForElement( | ||
new type( props ).render(), | ||
...args | ||
); | ||
} | ||
|
||
return addValuesForElement( type( props ), ...args ); | ||
} | ||
} | ||
|
||
function addValuesForElements( children, ...args ) { | ||
children = Array.isArray( children ) ? children : [ children ]; | ||
|
||
for ( let i = 0; i < children.length; i++ ) { | ||
addValuesForElement( children[ i ], ...args ); | ||
} | ||
} | ||
|
||
function addValuesForBlocks( values, blocks ) { | ||
for ( let i = 0; i < blocks.length; i++ ) { | ||
const { name, attributes, innerBlocks } = blocks[ i ]; | ||
const saveElement = getSaveElement( name, attributes ); | ||
addValuesForElement( saveElement, values, innerBlocks ); | ||
} | ||
} | ||
|
||
export function getRichTextValues( blocks = [] ) { | ||
getBlockProps.skipFilters = true; | ||
const values = []; | ||
addValuesForBlocks( values, blocks ); | ||
getBlockProps.skipFilters = false; | ||
return values; | ||
} |
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