Skip to content

Commit

Permalink
[field] Fix DiffAnnotation and DiffAnnotationTooltip after ergonomic …
Browse files Browse the repository at this point in the history
…refactor
  • Loading branch information
mariuslundgard authored and rexxars committed Oct 6, 2020
1 parent 62e27f7 commit 40eff2d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {useUserColorManager} from '@sanity/base/user-color'
import * as React from 'react'
import {StringDiffSegment, StringDiff} from '../../types'
import {DiffAnnotation} from '../annotations'
import styles from './AnnotatedStringDiff.css'
import {getAnnotationColor} from './helpers'

function FormattedPreviewText({
isFirstSegment,
Expand Down Expand Up @@ -36,8 +34,6 @@ export function AnnotatedStringDiffSegment({
isLastSegment: boolean
segment: StringDiffSegment
}): React.ReactElement {
const userColorManager = useUserColorManager()

const text = (
<FormattedPreviewText
isFirstSegment={isFirstSegment}
Expand All @@ -47,31 +43,25 @@ export function AnnotatedStringDiffSegment({
)

if (segment.action === 'added') {
const color = getAnnotationColor(userColorManager, segment.annotation)

return (
<DiffAnnotation
annotation={segment.annotation}
as="ins"
className={styles.add}
description="Added by"
style={{background: color.background, color: color.text}}
>
{text}
</DiffAnnotation>
)
}

if (segment.action === 'removed') {
const color = getAnnotationColor(userColorManager, segment.annotation)

return (
<DiffAnnotation
annotation={segment.annotation}
as="del"
className={styles.remove}
description="Removed by"
style={{background: color.background, color: color.text}}
>
{text}
</DiffAnnotation>
Expand Down
18 changes: 14 additions & 4 deletions packages/@sanity/field/src/diff/annotations/DiffAnnotation.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {useUserColorManager} from '@sanity/base/user-color'
import * as React from 'react'
import {Annotation, Diff, Path} from '../../types'
import {DiffAnnotationTooltip} from './DiffAnnotationTooltip'
import {getAnnotationAtPath} from './helpers'
import {getAnnotationAtPath, getAnnotationColor} from './helpers'

export interface AnnotationProps {
annotation: Annotation | undefined | null
Expand All @@ -20,23 +21,32 @@ interface BaseAnnotationProps {
export type DiffAnnotationProps = (AnnotationProps | AnnotatedDiffProps) & BaseAnnotationProps

export function DiffAnnotation(props: DiffAnnotationProps & React.HTMLProps<HTMLElement>) {
const userColorManager = useUserColorManager()

if ('diff' in props) {
const {as = 'span', children, description, diff, path, ...restProps} = props
const annotation = getAnnotationAtPath(diff, path || [])
const {as = 'span', children, description, diff, path = [], ...restProps} = props
const annotation = getAnnotationAtPath(diff, path)
const color = getAnnotationColor(userColorManager, annotation)

return (
<DiffAnnotationTooltip
{...restProps}
as={as}
annotation={annotation}
description={description}
style={{background: color.background, color: color.text}}
>
{children}
</DiffAnnotationTooltip>
)
}

const {children, ...restProps} = props
const color = getAnnotationColor(userColorManager, restProps.annotation)

return <DiffAnnotationTooltip {...restProps}>{children}</DiffAnnotationTooltip>
return (
<DiffAnnotationTooltip {...restProps} style={{background: color.background, color: color.text}}>
{children}
</DiffAnnotationTooltip>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,26 @@ export type DiffAnnotationTooltipProps = (AnnotationProps | AnnotatedDiffProps)
export function DiffAnnotationTooltip(
props: DiffAnnotationTooltipProps & React.HTMLProps<HTMLElement>
): React.ReactElement {
const {as = 'div', children, description, ...restProps} = props
const annotation =
'diff' in props ? getAnnotationAtPath(props.diff, props.path || []) : props.annotation
if ('diff' in props) {
const {as = 'div', children, description, diff, path, ...restProps} = props
const annotation = getAnnotationAtPath(diff, path || [])

if (!annotation) {
return createElement(as, restProps, children)
}

const content = (
<DiffAnnotationTooltipContent description={description} annotations={[annotation]} />
)

return (
<Tooltip content={content} placement="top" portal>
{createElement(as, restProps, children)}
</Tooltip>
)
}

const {as = 'div', annotation, children, description, ...restProps} = props

if (!annotation) {
return createElement(as, restProps, children)
Expand Down

0 comments on commit 40eff2d

Please sign in to comment.