Skip to content

Commit

Permalink
refactor(core/preview): use new types in portable-text previews
Browse files Browse the repository at this point in the history
  • Loading branch information
skogsmaskin committed Dec 20, 2022
1 parent b2aa1f2 commit 3fd22cc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions 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('')
}
4 changes: 2 additions & 2 deletions packages/sanity/src/core/preview/utils/prepareForPreview.ts
Expand Up @@ -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']
Expand Down Expand Up @@ -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,
}
}, {})
}
Expand Down

0 comments on commit 3fd22cc

Please sign in to comment.