From afe6fd870222ee3bbeb972d6723b676ee8c5b02b Mon Sep 17 00:00:00 2001
From: alvarosabu
Date: Wed, 25 Sep 2024 14:31:11 +0200
Subject: [PATCH 1/5] feat: added and types
---
.github/ISSUE_TEMPLATE/config.yml | 2 +-
README.md | 3 +
lib/common/SbRichText.tsx | 26 ++
lib/common/client.ts | 6 +-
lib/common/index.ts | 15 +
lib/common/richtext.ts | 26 ++
lib/cypress/support/component-index.html | 10 +-
lib/cypress/support/component.js | 8 +-
lib/cypress/support/e2e.js | 2 +-
lib/index.ts | 4 +-
lib/package.json | 3 +-
lib/types.ts | 6 +
lib/utils/index.ts | 65 +++
package-lock.json | 86 +++-
package.json | 3 +
.../.vscode/settings.json | 2 +-
playground-next13-live-editing/app/page.tsx | 2 +-
playground-next13-live-editing/tsconfig.json | 21 +-
playground-next13-rsc/.vscode/settings.json | 2 +-
playground/App.tsx | 384 +++++++++++++++++-
playground/index.html | 2 +-
21 files changed, 637 insertions(+), 41 deletions(-)
create mode 100644 lib/common/SbRichText.tsx
create mode 100644 lib/common/richtext.ts
create mode 100644 lib/utils/index.ts
diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml
index ec4bb386..3ba13e0c 100644
--- a/.github/ISSUE_TEMPLATE/config.yml
+++ b/.github/ISSUE_TEMPLATE/config.yml
@@ -1 +1 @@
-blank_issues_enabled: false
\ No newline at end of file
+blank_issues_enabled: false
diff --git a/README.md b/README.md
index 7ac4616d..852ec955 100755
--- a/README.md
+++ b/README.md
@@ -31,12 +31,15 @@
## Kickstart a new project
+
Are you eager to dive into coding? **[Follow these steps to kickstart a new project with Storyblok and React](https://www.storyblok.com/technologies#react?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-react)**, and get started in just a few minutes!
## 5-minute Tutorial
+
Are you looking for a hands-on, step-by-step tutorial? The **[React 5-minute Tutorial](https://www.storyblok.com/tp/headless-cms-react?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-react)** has you covered! It provides comprehensive instructions on how to set up a Storyblok space and connect it to your React project.
## Ultimate Tutorial
+
Are you looking for a hands-on, step-by-step tutorial? The **[Next.js Ultimate Tutorial](https://www.storyblok.com/tp/nextjs-headless-cms-ultimate-tutorial?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-react)** has you covered! It provides comprehensive instructions on building a complete, multilingual website using Storyblok and Next.js from start to finish.
## Installation
diff --git a/lib/common/SbRichText.tsx b/lib/common/SbRichText.tsx
new file mode 100644
index 00000000..28b4cc1f
--- /dev/null
+++ b/lib/common/SbRichText.tsx
@@ -0,0 +1,26 @@
+import React from "react";
+
+import { forwardRef } from "react";
+import { convertAttributesInElement } from "../utils";
+import { useSbRichtextResolver } from "./richtext";
+import type { SbRichTextProps } from "../types";
+
+// If you're forwarding a ref to SbRichText
+const SbRichText = forwardRef(
+ ({ doc, resolvers }, ref) => {
+ // Assuming useSbRichtextResolver is a hook you've created
+ const { render } = useSbRichtextResolver({
+ resolvers,
+ });
+
+ /* const Root = () => render(doc) */
+ const html = render(doc);
+ const formattedHtml = convertAttributesInElement(html);
+
+ // If you're forwarding a ref, make sure to attach the ref to a DOM element.
+ // For example, wrapping in a div and attaching the ref to it:
+ return {formattedHtml}
;
+ }
+);
+
+export default SbRichText;
diff --git a/lib/common/client.ts b/lib/common/client.ts
index 2fbb8328..18b47299 100644
--- a/lib/common/client.ts
+++ b/lib/common/client.ts
@@ -4,12 +4,13 @@ import { registerStoryblokBridge } from "@storyblok/js";
export const useStoryblokState: TUseStoryblokState = (
initialStory = null,
- bridgeOptions = {},
+ bridgeOptions = {}
) => {
const [story, setStory] = useState(initialStory);
const storyId = (initialStory as any)?.internalId ?? initialStory?.id ?? 0;
- const isBridgeEnabled = typeof window !== "undefined" &&
+ const isBridgeEnabled =
+ typeof window !== "undefined" &&
typeof window.storyblokRegisterEvent !== "undefined";
useEffect(() => {
@@ -22,7 +23,6 @@ export const useStoryblokState: TUseStoryblokState = (
(newStory) => setStory(newStory),
bridgeOptions
);
-
}, [initialStory]);
return story;
diff --git a/lib/common/index.ts b/lib/common/index.ts
index 5838207d..d408f9e9 100644
--- a/lib/common/index.ts
+++ b/lib/common/index.ts
@@ -49,6 +49,8 @@ export const storyblokInit = (pluginOptions: SbReactSDKOptions = {}) => {
export { default as StoryblokComponent } from "./storyblok-component";
export { useStoryblokApi as getStoryblokApi };
+export { default as SbRichText } from "./SbRichText";
+export { useSbRichtextResolver } from "./richtext";
export {
storyblokEditable,
apiPlugin,
@@ -60,4 +62,17 @@ export {
RichTextSchema,
} from "@storyblok/js";
+export {
+ BlockTypes,
+ MarkTypes,
+ richTextResolver,
+ TextTypes,
+ type SbRichTextNodeTypes,
+ type SbRichTextNode,
+ type SbRichTextOptions,
+ type SbRichTextResolvers,
+ type SbRichTextNodeResolver,
+ type SbRichTextImageOptimizationOptions,
+} from "@storyblok/richtext";
+
export * from "../types";
diff --git a/lib/common/richtext.ts b/lib/common/richtext.ts
new file mode 100644
index 00000000..c09614e6
--- /dev/null
+++ b/lib/common/richtext.ts
@@ -0,0 +1,26 @@
+import React from "react";
+import { StoryblokComponent } from "@storyblok/react";
+import type { SbRichTextNode, SbRichTextOptions } from "@storyblok/richtext";
+import { BlockTypes, richTextResolver } from "@storyblok/richtext";
+
+function componentResolver(node: SbRichTextNode) {
+ // Convert this to use React.createElement or JSX
+ // Example with JSX:
+ return React.createElement(StoryblokComponent, {
+ blok: node?.attrs?.body[0],
+ id: node.attrs?.id,
+ });
+}
+
+export function useSbRichtextResolver(
+ options: SbRichTextOptions
+) {
+ const mergedOptions = {
+ renderFn: React.createElement,
+ resolvers: {
+ [BlockTypes.COMPONENT]: componentResolver,
+ ...options.resolvers,
+ },
+ };
+ return richTextResolver(mergedOptions);
+}
diff --git a/lib/cypress/support/component-index.html b/lib/cypress/support/component-index.html
index ac6e79fd..faf3b5f4 100644
--- a/lib/cypress/support/component-index.html
+++ b/lib/cypress/support/component-index.html
@@ -1,12 +1,12 @@
-
+
-
-
-
+
+
+
Components App
-
\ No newline at end of file
+