-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v3 - customer case rich text block #788
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
885b62b
richtextblock for customer cases
anemne fda9b17
prepare for richtext
anemne 0376981
feat(richTextBlock): initial preview
mathiazom 79bbe3b
feat(richTextBlock): improve type check for rich text preview
mathiazom 8a4a1a3
feat(customerCase): include block sections in customer case query
mathiazom 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { | ||
PortableTextBlock, | ||
PortableTextObject, | ||
isPortableTextTextBlock, | ||
} from "sanity"; | ||
|
||
export function isRichText(value: unknown): value is PortableTextBlock[] { | ||
return ( | ||
Array.isArray(value) && value.every((item) => isPortableTextBlock(item)) | ||
); | ||
} | ||
|
||
export function isPortableTextBlock( | ||
value: unknown, | ||
): value is PortableTextBlock { | ||
return isPortableTextTextBlock(value) || isPortableTextObject(value); | ||
} | ||
|
||
export function isPortableTextObject( | ||
value: unknown, | ||
): value is PortableTextObject { | ||
return isSanityKeyTypeObject(value); | ||
} | ||
|
||
function isSanityKeyTypeObject(value: unknown): value is { | ||
_key: string; | ||
_type: string; | ||
} { | ||
return ( | ||
typeof value === "object" && | ||
value !== null && | ||
"_key" in value && | ||
typeof value._key === "string" && | ||
"_type" in value && | ||
typeof value._type === "string" | ||
); | ||
} |
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,10 +1,10 @@ | ||
import { InternationalizedString } from "studio/lib/interfaces/global"; | ||
import { InternationalizedValue } from "studio/lib/interfaces/global"; | ||
|
||
export function firstTranslation( | ||
translatedString: InternationalizedString, | ||
): string | null { | ||
if (translatedString.length === 0) { | ||
export function firstTranslation<T>( | ||
internationalizedValue: InternationalizedValue<T>, | ||
): T | null { | ||
if (internationalizedValue.length === 0) { | ||
return null; | ||
} | ||
return translatedString[0].value; | ||
return internationalizedValue[0].value; | ||
} |
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,20 @@ | ||
import { | ||
PortableTextBlock, | ||
isPortableTextSpan, | ||
isPortableTextTextBlock, | ||
} from "sanity"; | ||
|
||
// Convert rich text content to plaintext preview string | ||
// (inspired by https://www.sanity.io/docs/previewing-block-content) | ||
export function richTextPreview( | ||
richText: PortableTextBlock[], | ||
): string | undefined { | ||
const block = richText.find((block) => block._type === "block"); | ||
if (!isPortableTextTextBlock(block)) { | ||
return undefined; | ||
} | ||
return block.children | ||
.filter((child) => isPortableTextSpan(child)) | ||
.map((span) => span.text) | ||
.join(""); | ||
} |
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,39 @@ | ||
import { defineField } from "sanity"; | ||
|
||
import { isInternationalizedRichText } from "studio/lib/interfaces/global"; | ||
import { firstTranslation } from "studio/utils/i18n"; | ||
import { richTextPreview } from "studio/utils/preview"; | ||
|
||
const richTextBlock = defineField({ | ||
name: "richTextBlock", | ||
title: "Rich Text Block", | ||
type: "object", | ||
fields: [ | ||
{ | ||
name: "richText", | ||
title: "Rich Text", | ||
type: "internationalizedArrayRichText", | ||
}, | ||
], | ||
preview: { | ||
select: { | ||
richText: "richText", | ||
}, | ||
prepare({ richText }) { | ||
if (!isInternationalizedRichText(richText)) { | ||
throw new TypeError( | ||
`Expected 'richText' to be InternationalizedRichText, was ${typeof richText}`, | ||
); | ||
} | ||
const translatedRichText = firstTranslation(richText); | ||
return { | ||
title: | ||
translatedRichText !== null | ||
? richTextPreview(translatedRichText) | ||
: undefined, | ||
}; | ||
}, | ||
}, | ||
}); | ||
|
||
export default richTextBlock; |
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.
Should we limit the amount of words that is shown in this richtextpreview? and end it with ... if there is more text?
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.
Looks like Sanity does that automatically, so I think we should be good
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.
Great!