This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Reuse the same RichText component across the different environments (Edit, Save and Frontend) #2
Merged
luisherranz
merged 5 commits into
WordPress:main
from
fabiankaegy:feature/rich-text-editing
Apr 4, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d12a805
fix replace TextControl in editor with RichText
fabiankaegy 78fa4e1
fix only import the RichText Component in the editor
fabiankaegy a180045
Merge branch 'main' into fabiankaegy-feature/rich-text-editing
luisherranz 7710b6d
Merge branch 'feature/rich-text-editing' of https://github.com/fabian…
luisherranz f66a34b
Add useBlockEnvironment hook
luisherranz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,9 @@ | ||
import { useBlockEnvironment } from "./wordpress-element"; | ||
|
||
export const RichText = ({ tagName: Tag, children, ...props }) => { | ||
return useBlockEnvironment() === "edit" ? ( | ||
<window.wp.blockEditor.RichText value={children} tagName={Tag} {...props} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still using your trick to solve the bundling issue. We can explore solutions to this in a different PR. |
||
) : ( | ||
<Tag {...props}>{children}</Tag> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,14 +1,36 @@ | ||
import { | ||
useState as useReactState, | ||
useEffect as useReactEffect, | ||
useContext as useReactContext, | ||
createContext, | ||
} from "@wordpress/element"; | ||
export { hydrate } from "react-dom"; | ||
|
||
// Dirty dirty trick | ||
export const isView = !window.wp.blockEditor; | ||
export const EnvContext = createContext(null); | ||
|
||
/** | ||
* A React hook that returns the name of the environment. | ||
* | ||
* This is still a bit hacky. Ideally, Save components should support React | ||
* hooks and all the environments (Edit, Save and Frontend) should populate a | ||
* normal context. Also, more environments could be added in the future. | ||
* | ||
* @returns {"edit" | "save" | "frontend"} | ||
*/ | ||
export const useBlockEnvironment = () => { | ||
try { | ||
const env = useReactContext(EnvContext); | ||
if (env === "frontend") return "frontend"; | ||
return "edit"; | ||
} catch (e) { | ||
return "save"; | ||
} | ||
}; | ||
|
||
const noop = () => {}; | ||
|
||
export const useState = (init) => (isView ? useReactState(init) : [init, noop]); | ||
export const useState = (init) => | ||
useBlockEnvironment() !== "save" ? useReactState(init) : [init, noop]; | ||
|
||
export const useEffect = (...args) => (isView ? useReactEffect(...args) : noop); | ||
export const useEffect = (...args) => | ||
useBlockEnvironment() !== "save" ? useReactEffect(...args) : noop; |
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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
const Title = ({ message }) => <h2 className="title">{message}</h2>; | ||
import { RichText } from "../framework/wordpress-blockeditor"; | ||
|
||
const Title = ({ message, ...props }) => ( | ||
<RichText tagName="h2" className="title" {...props}> | ||
{message} | ||
</RichText> | ||
); | ||
|
||
export default Title; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, I think we could add one provider to each environment like this one with the correct value, so users only have to do this on their code:
or even expose a hook: