-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CTC-2582 Extend rich text resolver samples
Added comments and changed return values
- Loading branch information
Showing
4 changed files
with
68 additions
and
197 deletions.
There are no files selected for viewing
49 changes: 12 additions & 37 deletions
49
ts/structure-in-rte/resolve_rich_text_components_items.tsx
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,60 +1,35 @@ | ||
import { Elements } from "@kontent-ai/delivery-sdk"; | ||
import { | ||
nodeParse, | ||
PortableTextComponent, | ||
transformToPortableText, | ||
} from "@kontent-ai/rich-text-resolver"; | ||
import { | ||
PortableText, | ||
PortableTextComponents, | ||
PortableTextTypeComponentProps, | ||
} from "@portabletext/react"; | ||
import { PortableTextComponent } from "@kontent-ai/rich-text-resolver"; | ||
import { PortableTextComponents, PortableTextTypeComponentProps, } from "@portabletext/react"; | ||
import { React } from 'react'; | ||
|
||
import { CalloutComponent } from "../components/shared/richText/Callout"; | ||
import { contentTypes } from "../models"; | ||
|
||
|
||
type RichTextComponentProps = { | ||
richTextElement: Elements.RichTextElement; | ||
}; | ||
|
||
const getPortableTextComponents = ( | ||
element: Elements.RichTextElement | ||
): Partial<PortableTextComponents> => ({ | ||
const createRichTextResolver = (element: Elements.RichTextElement): Partial<PortableTextComponents> => ({ | ||
types: { | ||
// Components and content items inserted in rich text | ||
component: ({ | ||
value, | ||
}: PortableTextTypeComponentProps<PortableTextComponent>) => { | ||
// Checks if the inserted content item is included in the response | ||
// Kontent.ai components are always included in the response | ||
const componentOrItem = element.linkedItems.find( | ||
(i) => i.system.codename === value.component._ref | ||
); | ||
|
||
if (!componentOrItem) { | ||
throw new Error("Component or item not found."); | ||
return (<div>The referenced content item '{componentOrItem.system.codename}' wasn't found.</div>); | ||
} | ||
|
||
// Renders the component or content item based on its type | ||
switch (componentOrItem.system.type) { | ||
// Tip: Generate models at https://kontent.ai/learn/strongly-typed-models | ||
case contentTypes.callout.codename: | ||
return <CalloutComponent item={componentOrItem} />; | ||
default: | ||
return ( | ||
<div> | ||
Resolution for content type {componentOrItem.system.type} not | ||
implemented. | ||
</div> | ||
); | ||
return (<div>Content based on the type '{componentOrItem.system.type}' couldn't be resolved.</div>); | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
export const RichTextComponent: React.FC<RichTextComponentProps> = (props) => { | ||
const parsedTree = nodeParse(props.richTextElement.value); | ||
const portableText = transformToPortableText(parsedTree); | ||
return ( | ||
<PortableText | ||
value={portableText} | ||
components={getPortableTextComponents(props.richTextElement)} | ||
/> | ||
); | ||
}; | ||
}); |
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
86 changes: 0 additions & 86 deletions
86
ts/structure-in-rte/structure_in_rte_use_standalone_resolver.tsx
This file was deleted.
Oops, something went wrong.