Skip to content

Commit

Permalink
feat(types): improve SbRichText props types
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosabu committed Jul 8, 2024
1 parent f6d6bea commit ce7daa0
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 26 deletions.
5 changes: 1 addition & 4 deletions lib/StoryblokComponent.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<script setup lang="ts">
import { ref, resolveDynamicComponent, inject } from "vue";
import type { SbBlokData, SbVueSDKOptions } from "./types";
import type { SbComponentProps, SbVueSDKOptions } from "./types";
export interface SbComponentProps {
blok: SbBlokData;
}
const props = defineProps<SbComponentProps>();
const blokRef = ref();
Expand Down
3 changes: 2 additions & 1 deletion lib/components/SbRichText.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script setup lang="ts">
import { useSbRichText } from "../composables/useRichText";
import type { SbRichTextProps } from "../types";
const props = defineProps<{ doc: any; resolvers?: any }>();
const props = defineProps<SbRichTextProps>();
const { render } = useSbRichText({
resolvers: props.resolvers,
Expand Down
14 changes: 8 additions & 6 deletions lib/composables/useRichText.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { VNode } from "vue";
import { createTextVNode, h } from "vue";
import type {
Node,
NodeResolver,
SbRichtextOptions,
SbRichTextNode,
SbRichTextNodeResolver,
SbRichTextOptions,
} from "@storyblok/richtext";
import { BlockTypes, richTextResolver } from "@storyblok/richtext";
import StoryblokComponent from "../StoryblokComponent.vue";

const componentResolver: NodeResolver<VNode> = (node: Node<VNode>): VNode => {
const componentResolver: SbRichTextNodeResolver<VNode> = (
node: SbRichTextNode<VNode>
): VNode => {
return h(
StoryblokComponent,
{
Expand All @@ -19,8 +21,8 @@ const componentResolver: NodeResolver<VNode> = (node: Node<VNode>): VNode => {
);
};

export function useSbRichText(options: SbRichtextOptions<VNode>) {
const mergedOptions: SbRichtextOptions<VNode> = {
export function useSbRichText(options: SbRichTextOptions<VNode>) {
const mergedOptions: SbRichTextOptions<VNode> = {
renderFn: h,
textFn: createTextVNode,
resolvers: {
Expand Down
2 changes: 2 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type {
SbVueSDKOptions,
} from "./types";

export * from "@storyblok/richtext";

export {
useStoryblokBridge,
apiPlugin,
Expand Down
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"@storyblok/js": "^3.0.8",
"@storyblok/richtext": "^0.2.0"
"@storyblok/richtext": "^0.3.0"
},
"devDependencies": {
"@babel/core": "^7.24.7",
Expand Down
13 changes: 12 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { SbSDKOptions } from "@storyblok/js";
import type { SbBlokData, SbSDKOptions } from "@storyblok/js";
import type StoryblokComponent from "./StoryblokComponent.vue";
import type { SbRichTextNode, SbRichTextResolvers } from "@storyblok/richtext";
import type { VNode } from "vue";

declare module "@vue/runtime-core" {
export interface GlobalComponents {
Expand Down Expand Up @@ -47,3 +49,12 @@ export interface SbVueSDKOptions extends SbSDKOptions {
*/
customFallbackComponent?: string;
}

export interface SbComponentProps {
blok: SbBlokData;
}

export interface SbRichTextProps {
doc: SbRichTextNode<VNode>;
resolvers?: SbRichTextResolvers<VNode>;
}
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions playground/pages/Home.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { useStoryblokBridge, useStoryblokApi } from "@storyblok/vue";
import { onMounted, ref, VNode } from "vue";
import {
useStoryblokBridge,
useStoryblokApi,
SbRichText,
SbRichTextNode,
BlockTypes,
} from "@storyblok/vue";
const version = import.meta.env.MODE === "production" ? "published" : "draft";
Expand All @@ -16,8 +22,8 @@ onMounted(() => {
useStoryblokBridge(story.value.id, (evStory) => (story.value = evStory));
});
const doc = {
type: "doc",
const doc: SbRichTextNode<VNode> = {
[BlockTypes.DOCUMENT]: "doc",
content: [
{
type: "bullet_list",
Expand Down Expand Up @@ -74,6 +80,6 @@ const doc = {
</script>

<template>
<SbRichText :doc="doc" />
<SbRichText v-if="doc" :doc="doc" />
<StoryblokComponent v-if="story" :blok="story.content" />
</template>

0 comments on commit ce7daa0

Please sign in to comment.