-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: automatically forward block props to children of BlocksRenderer
- Loading branch information
1 parent
68db922
commit 0e6e5e3
Showing
30 changed files
with
202 additions
and
124 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,92 @@ | ||
import { Element } from 'html-react-parser'; | ||
import { FrameworkError, warn } from '../utils'; | ||
|
||
export interface IDataWPBlock { | ||
[key: string]: unknown; | ||
} | ||
|
||
const BLOCK_MISSING = '_HEADLESS_/_MISSING__BLOCK_'; | ||
|
||
export const DEFAULT_BLOCK_ELEMENT = new Element('div', { | ||
'data-wp-block': JSON.stringify({}), | ||
'data-wp-block-name': BLOCK_MISSING, | ||
}); | ||
|
||
/** | ||
* Parses Json without throwing errors | ||
* | ||
* @param json Serialized JSON | ||
* @returns JSON object | ||
*/ | ||
function safeParsing(json): Record<string, any> { | ||
let parsed = {}; | ||
|
||
try { | ||
parsed = JSON.parse(json); | ||
} catch (e) { | ||
// do nothing | ||
} | ||
|
||
return parsed; | ||
} | ||
|
||
/** | ||
* Represents a parsed block, i.e a block parsed from `data-wp-block` and `data-wp-block-name` attributes | ||
*/ | ||
export type ParsedBlock = { | ||
/** | ||
* The Block attributes | ||
*/ | ||
attributes: IDataWPBlock; | ||
|
||
/** | ||
* The Block name | ||
*/ | ||
name: string; | ||
|
||
/** | ||
* The Block class name | ||
*/ | ||
className: string; | ||
}; | ||
|
||
/** | ||
* Returns the block name and attributes | ||
* | ||
* @param node DomNode | ||
* | ||
* @returns | ||
*/ | ||
export function parseBlockAttributes(node?: Element): ParsedBlock { | ||
if (typeof node === 'undefined') { | ||
throw new FrameworkError('You are calling `parseBlockAttributes` on a undefined node'); | ||
} | ||
|
||
if ( | ||
typeof node.attribs['data-wp-block-name'] === 'undefined' && | ||
typeof node.attribs['data-wp-block'] === 'undefined' | ||
) { | ||
warn( | ||
'[parseBlockAttributes] You are using the `parseBlockAttributes` hook in a node that is not a block.', | ||
); | ||
} | ||
|
||
const blockName = node.attribs['data-wp-block-name'] || ''; | ||
|
||
if (blockName === BLOCK_MISSING) { | ||
if (typeof node === 'undefined') { | ||
throw new FrameworkError('You are calling `parseBlockAttributes` on a undefined node'); | ||
} | ||
} | ||
|
||
const attrs: IDataWPBlock = node.attribs['data-wp-block'] | ||
? safeParsing(node.attribs['data-wp-block']) | ||
: {}; | ||
|
||
if (attrs.style) { | ||
attrs.styleConfig = attrs.style; | ||
delete attrs.style; | ||
} | ||
|
||
return { attributes: attrs, name: blockName, className: node.attribs.class }; | ||
} |
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
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
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
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
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
Oops, something went wrong.