diff --git a/packages/sanity/src/core/preview/utils/portableText.ts b/packages/sanity/src/core/preview/utils/portableText.ts index f235d3c5a0e..fa8938ffaee 100644 --- a/packages/sanity/src/core/preview/utils/portableText.ts +++ b/packages/sanity/src/core/preview/utils/portableText.ts @@ -1,17 +1,17 @@ -import {Block, isBlock, isSpan} from '@sanity/types' +import {isPortableTextTextBlock, isPortableTextSpan, PortableTextBlock} from '@sanity/types' -export function isPortableTextArray(blocks: unknown): blocks is Block[] { - return Array.isArray(blocks) && (blocks.length === 0 || blocks.some(isBlock)) +export function isPortableTextPreviewValue(value: unknown): value is PortableTextBlock[] { + return Array.isArray(value) && (value.length === 0 || value.some(isPortableTextTextBlock)) } -export function extractTextFromBlocks(blocks: Block[]): string { - const firstBlock = blocks.find(isBlock) +export function extractTextFromBlocks(blocks: unknown): string { + const firstBlock = Array.isArray(blocks) && blocks.find(isPortableTextTextBlock) if (!firstBlock || !firstBlock.children) { return '' } return firstBlock.children - .filter(isSpan) + .filter(isPortableTextSpan) .map((span) => span.text) .join('') } diff --git a/packages/sanity/src/core/preview/utils/prepareForPreview.ts b/packages/sanity/src/core/preview/utils/prepareForPreview.ts index e23ffba3fb4..29f3e5c83d5 100644 --- a/packages/sanity/src/core/preview/utils/prepareForPreview.ts +++ b/packages/sanity/src/core/preview/utils/prepareForPreview.ts @@ -9,7 +9,7 @@ import {debounce, flatten, get, isPlainObject, pick, uniqBy} from 'lodash' import {INVALID_PREVIEW_FALLBACK} from '../constants' import {PreviewableType} from '../types' import {isRecord} from '../../util' -import {isPortableTextArray, extractTextFromBlocks} from './portableText' +import {extractTextFromBlocks, isPortableTextPreviewValue} from './portableText' import {keysOf} from './keysOf' const PRESERVE_KEYS = ['_id', '_type', '_upload', '_createdAt', '_updatedAt'] @@ -190,7 +190,7 @@ function defaultPrepare(value: SelectedValue) { const val = value[fieldName] return { ...acc, - [fieldName]: isPortableTextArray(val) ? extractTextFromBlocks(val) : value[fieldName], + [fieldName]: isPortableTextPreviewValue(val) ? extractTextFromBlocks(val) : val, } }, {}) }