Skip to content

Commit

Permalink
[field] Add "no change" diff handling to image diff component
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent 233108d commit f821194
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function HotspotCropSVG(
</defs>

{crop && (
<DiffAnnotationTooltip as="g" diff={diff} path="crop" description="Crop changed by">
<DiffAnnotationTooltip as="g" diff={diff} path="crop" description="Crop changed">
<CropSVG
crop={crop}
fill={hexToRgba(cropColor.border, 0.25)}
Expand All @@ -59,7 +59,7 @@ export function HotspotCropSVG(
)}

{hotspot && (
<DiffAnnotationTooltip as="g" diff={diff} path="hotspot" description="Hotspot changed by">
<DiffAnnotationTooltip as="g" diff={diff} path="hotspot" description="Hotspot changed">
<HotspotSVG
hotspot={hotspot}
fill={hexToRgba(hotspotColor.border, 0.25)}
Expand Down
50 changes: 16 additions & 34 deletions packages/@sanity/field/src/types/image/diff/ImageFieldDiff.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@ import {
ObjectDiff,
DiffAnnotationTooltip,
DiffAnnotationCard,
ChangeList
ChangeList,
getAnnotationAtPath
} from '../../../diff'
import {useRefValue} from '../../../diff/hooks'
import {ChangeArrow} from '../../../diff/components'
import ImagePreview from './ImagePreview'
import styles from './ImageFieldDiff.css'
import {Image, SanityImageAsset} from './types'

/* TODO:
- "No change" state
*/

const IMAGE_META_FIELDS = ['crop', 'hotspot']
const BASE_IMAGE_FIELDS = ['asset', ...IMAGE_META_FIELDS]

export const ImageFieldDiff: DiffComponent<ObjectDiff<Image>> = ({diff, schemaType}) => {
const {fromValue, toValue, fields} = diff
const {fromValue, toValue, fields, isChanged} = diff
const fromRef = fromValue?.asset?._ref
const toRef = toValue?.asset?._ref
const prev = useRefValue<SanityImageAsset>(fromRef)
const next = useRefValue<SanityImageAsset>(toRef)
const assetAnnotation = getAnnotationAtPath(diff, ['asset', '_ref'])

if (!isChanged) {
return next ? (
<DiffAnnotationCard className={styles.annotation} annotation={assetAnnotation}>
<ImagePreview is="to" asset={next} diff={diff} />
</DiffAnnotationCard>
) : null
}

// Get all the changed fields within this image field
const changedFields = Object.keys(fields).filter(
Expand All @@ -48,12 +54,11 @@ export const ImageFieldDiff: DiffComponent<ObjectDiff<Image>> = ({diff, schemaTy
const didMetaChange = didCropChange || didHotspotChange
const showImageDiff = didAssetChange || didMetaChange
const showMetaChange = didMetaChange && !didAssetChange
const annotationPath = getAnnotationPath({didAssetChange, didCropChange, didHotspotChange})

const imageDiff = (
<div className={styles.imageDiff} data-diff-layout={prev && next ? 'double' : 'single'}>
{prev && fromValue && (
<DiffAnnotationCard className={styles.annotation} diff={diff} path="asset._ref">
<DiffAnnotationCard className={styles.annotation} annotation={assetAnnotation}>
<ImagePreview
is="from"
asset={prev}
Expand All @@ -66,7 +71,7 @@ export const ImageFieldDiff: DiffComponent<ObjectDiff<Image>> = ({diff, schemaTy
)}
{prev && next && <ChangeArrow />}
{next && toValue && (
<DiffAnnotationCard className={styles.annotation} diff={diff} path="asset._ref">
<DiffAnnotationCard className={styles.annotation} annotation={assetAnnotation}>
<ImagePreview
is="to"
asset={next}
Expand All @@ -84,9 +89,8 @@ export const ImageFieldDiff: DiffComponent<ObjectDiff<Image>> = ({diff, schemaTy
{showImageDiff &&
(didAssetChange ? (
<DiffAnnotationTooltip
diff={diff}
path={annotationPath}
description={`${assetAction[0].toUpperCase()}${assetAction.slice(1)} by`}
annotation={assetAnnotation}
description={`${assetAction[0].toUpperCase()}${assetAction.slice(1)}`}
>
{imageDiff}
</DiffAnnotationTooltip>
Expand All @@ -102,25 +106,3 @@ export const ImageFieldDiff: DiffComponent<ObjectDiff<Image>> = ({diff, schemaTy
</div>
)
}

function getAnnotationPath(changes: {
didCropChange: boolean
didHotspotChange: boolean
didAssetChange: boolean
}): string | undefined {
const {didAssetChange, didCropChange, didHotspotChange} = changes

if (didAssetChange) {
return 'asset._ref'
}

if (didCropChange) {
return 'crop'
}

if (didHotspotChange) {
return 'hotspot'
}

return undefined
}

0 comments on commit f821194

Please sign in to comment.