-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[field] Simplify rendering of PT and display more diffs
- Loading branch information
1 parent
0babe58
commit 5ea4be4
Showing
8 changed files
with
287 additions
and
125 deletions.
There are no files selected for viewing
21 changes: 16 additions & 5 deletions
21
packages/@sanity/field/src/diff/components/portableText/PTDiff.css
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,17 +1,28 @@ | ||
@import 'part:@sanity/base/theme/variables-style'; | ||
|
||
.root { | ||
} | ||
|
||
.spanDiff { | ||
} | ||
|
||
.inlineObjectDiff { | ||
border: 1px solid #999; | ||
padding: 3px; | ||
@nest & .removed { | ||
opacity: 0.5; | ||
} | ||
} | ||
|
||
.removed { | ||
text-decoration: line-through; | ||
opacity: 0.5; | ||
} | ||
|
||
.summary { | ||
font-size: var(--font-size-xsmall); | ||
color: var(--text-muted); | ||
} | ||
|
||
.summary li { | ||
} | ||
|
||
.inlineObjectDiff { | ||
border: 1px solid #999; | ||
padding: 3px; | ||
} |
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
89 changes: 89 additions & 0 deletions
89
packages/@sanity/field/src/diff/components/portableText/PortableText.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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React from 'react' | ||
import {PortableTextBlock, PortableTextChild, ChildMap} from './types' | ||
import {isHeader, somethingIsRemoved} from './helpers' | ||
|
||
import Decorator from './previews/Decorator' | ||
import InlineObject from './previews/InlineObject' | ||
import Blockquote from './previews/Blockquote' | ||
import Header from './previews/Header' | ||
import Paragraph from './previews/Paragraph' | ||
|
||
import styles from './PTDiff.css' | ||
import {ArrayDiff, ObjectDiff} from '../../index' | ||
|
||
type Props = { | ||
blockDiff: ObjectDiff | ||
childMap: ChildMap | ||
} | ||
|
||
export const PortableText = (props: Props): JSX.Element => { | ||
const {blockDiff, childMap} = props | ||
|
||
const renderBlock = ({ | ||
block, | ||
children | ||
}: { | ||
block: PortableTextBlock | ||
children: React.ReactNode | ||
}): JSX.Element => { | ||
let returned: React.ReactNode = children | ||
if (block.style === 'blockquote') { | ||
returned = <Blockquote>{returned}</Blockquote> | ||
} else if (block.style && isHeader(block)) { | ||
returned = <Header style={block.style}>{returned}</Header> | ||
} else { | ||
returned = <Paragraph>{returned}</Paragraph> | ||
} | ||
return <>{returned}</> | ||
} | ||
const renderInlineObject = (props: {child: PortableTextChild}): React.ReactNode => { | ||
const {child} = props | ||
return <InlineObject node={child} /> | ||
} | ||
|
||
const invalidInlineObjectType = (props: any) => { | ||
return <span className={styles.inlineObjectDiff}>Invalid type</span> | ||
} | ||
|
||
const otherTypes = {} | ||
Object.keys(childMap) | ||
.map(key => childMap[key]) | ||
.forEach(mapEntry => { | ||
const {child} = mapEntry | ||
const type = typeof child === 'object' && (child._type as string) | ||
const isSpan = child && typeof child._type === 'string' && child._type === 'span' | ||
if (!isSpan && type) { | ||
otherTypes[type] = renderInlineObject | ||
} else { | ||
// This should not happen. But have a fallback for missing types anyway. | ||
// 'undefined' key is set when building the childMap (helpers) when there is no schema found for this object | ||
otherTypes['undefined'] = props => invalidInlineObjectType(props) | ||
} | ||
}) | ||
|
||
const renderChild = (child: PortableTextChild) => { | ||
const fromMap = childMap[child._key] | ||
// Render span or inline object? | ||
const inlineObject = otherTypes[child._type] | ||
const renderInlineObject = inlineObject && inlineObject(child) | ||
const renderSpanOrInline = renderInlineObject ? renderInlineObject : () => child.text | ||
let returned: React.ReactNode = | ||
fromMap && fromMap.annotation ? fromMap.annotation : renderSpanOrInline({child}) | ||
// Render decorators | ||
!inlineObject && | ||
(child.marks || []).map(mark => { | ||
returned = <Decorator mark={mark}>{returned}</Decorator> | ||
}) | ||
return returned | ||
} | ||
|
||
// Do the final render | ||
let block = blockDiff.toValue | ||
if (somethingIsRemoved(blockDiff)) { | ||
block = blockDiff.fromValue | ||
} | ||
return renderBlock({ | ||
block: block as PortableTextBlock, | ||
children: block.children.map(child => renderChild(child)) | ||
}) | ||
} |
Oops, something went wrong.