Skip to content

Commit

Permalink
[field] PT: clean up inline object rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
skogsmaskin authored and rexxars committed Oct 6, 2020
1 parent 5d7ab1b commit 461fd2c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import {flatten, uniq} from 'lodash'
import {PortableTextBlock, PortableTextChild, PortableTextDiff, StringSegment} from '../types'
import {
ANNOTATION_SYMBOLS,
childIsSpan,
createChildMap,
getChildSchemaType,
getDecorators,
getInlineObjects,
INLINE_SYMBOLS,
isDecorator,
MARK_SYMBOLS,
UNKNOWN_TYPE_NAME
MARK_SYMBOLS
} from '../helpers'

import {ArrayDiff, DiffCard, ObjectDiff} from '../../../../diff'
Expand All @@ -38,21 +36,9 @@ export default function PortableText(props: Props): JSX.Element {
const childMap = useMemo(() => createChildMap(diff.origin, schemaType), [diff, schemaType])
const block = diff.displayValue

const inlineObjects = diff.toValue ? getInlineObjects(diff.toValue as PortableTextBlock) : []

const renderObjectTypes = {}

Object.keys(childMap)
.map(key => childMap[key])
.forEach(mapEntry => {
const {child} = mapEntry
if (!childIsSpan(child) && child._type) {
renderObjectTypes[child._type] = renderInlineObject
} else {
// This should not happen at this point. But have a fallback for rendering missing types anyway.
renderObjectTypes[UNKNOWN_TYPE_NAME] = renderInvalidInlineObjectType
}
})
const inlineObjects = diff.origin.toValue
? getInlineObjects(diff.origin.toValue as PortableTextBlock)
: []

const renderChild = (child: PortableTextChild) => {
const spanSchemaType = getChildSchemaType(schemaType.fields, child)
Expand Down Expand Up @@ -96,7 +82,7 @@ export default function PortableText(props: Props): JSX.Element {
} else if (isInline) {
const indexOfSymbol = INLINE_SYMBOLS.findIndex(sym => sym === seg.text)
const key = inlineObjects[indexOfSymbol]?._key
const originChild = diff.displayValue.children.find(
const originChild = (diff.origin.toValue || diff.origin.fromValue).children.find(
cld => cld._key === key
) as PortableTextChild
if (key) {
Expand All @@ -106,11 +92,12 @@ export default function PortableText(props: Props): JSX.Element {
throw new Error('Schema type required')
}
returnedChildren.push(
renderInlineObject({
child: originChild,
diff: objectDiff,
schemaType: objectSchemaType
})
<InlineObject
key={`inline-object-${child._key}`}
object={originChild}
diff={objectDiff}
schemaType={objectSchemaType}
/>
)
}
} else if (seg.action === 'unchanged') {
Expand Down Expand Up @@ -146,10 +133,6 @@ export default function PortableText(props: Props): JSX.Element {
throw new Error("'span' schemaType not found")
}

function renderInvalidInlineObjectType() {
return <span>Invalid inline object type</span>
}

return (
<Block block={block} diff={diff}>
{(diff.displayValue.children || []).map(child => renderChild(child))}
Expand Down Expand Up @@ -179,23 +162,6 @@ function findTextAnnotationFromSegment(diff: ObjectDiff, segment: StringSegment)
return undefined
}

function renderInlineObject(props: {
child: PortableTextChild
schemaType: ObjectSchemaType
diff: ObjectDiff
}): React.ReactNode {
const {child, diff, schemaType} = props
const inlineObjectSchemaType = getChildSchemaType(schemaType.fields, child)
return (
<InlineObject
key={`inline-object-${child._key}`}
object={child}
diff={diff}
schemaType={inlineObjectSchemaType}
/>
)
}

function renderWithMarks(
diff: PortableTextDiff,
activeMarks: string[],
Expand Down
5 changes: 0 additions & 5 deletions packages/@sanity/field/src/types/portableText/diff/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,6 @@ export function isDecorator(name: string, schemaType: SpanTypeSchema): boolean {
return getDecorators(schemaType).some(dec => dec.value === name)
}

export function childIsSpan(child: PortableTextChild): boolean {
const isObject = typeof child === 'object'
return isObject && typeof child._type === 'string' && child._type === 'span'
}

export function blockToSymbolizedText(
block: PortableTextBlock | undefined | null,
decoratorMap: MarkSymbolMap,
Expand Down

0 comments on commit 461fd2c

Please sign in to comment.