-
Notifications
You must be signed in to change notification settings - Fork 19
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
6fa9ec2
commit 4f7a665
Showing
61 changed files
with
2,643 additions
and
4,352 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@headstartwp/core": minor | ||
"@headstartwp/next": minor | ||
--- | ||
|
||
Add `forwardBlockProps` to BlocksRenderer which automatically forwards block props to children components |
Large diffs are not rendered by default.
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
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
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<T extends IDataWPBlock = IDataWPBlock> = { | ||
/** | ||
* The Block attributes | ||
*/ | ||
attributes: T; | ||
|
||
/** | ||
* 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
Oops, something went wrong.