Skip to content

Commit

Permalink
[field] Push empty field change on array moves
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent 3e96d6b commit d550542
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions packages/@sanity/field/src/diff/changes/buildChangeList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
ChangeNode,
ArraySchemaType,
ArrayDiff,
DiffComponent
DiffComponent,
FieldChangeNode
} from '../../types'

export function buildChangeList(
Expand All @@ -34,6 +35,15 @@ export function buildChangeList(
}
}

return [getFieldChange(schemaType, diff, path, titlePath)]
}

function getFieldChange(
schemaType: SchemaType,
diff: Diff,
path: Path,
titlePath: ChangeTitlePath
): FieldChangeNode {
let error
if (typeof diff.fromValue !== 'undefined') {
error = getValueError(diff.fromValue, schemaType)
Expand All @@ -45,24 +55,23 @@ export function buildChangeList(

let renderHeader = true
let component: DiffComponent | undefined
const diffComponent = resolveDiffComponent(schemaType)
if (diffComponent) {
renderHeader = typeof diffComponent === 'function' ? true : diffComponent.renderHeader
component = typeof diffComponent === 'function' ? diffComponent : diffComponent.component
}

return [
{
type: 'field',
diff,
path,
error,
titlePath,
schemaType,
renderHeader,
key: pathToString(path) || 'root',
diffComponent: error ? undefined : component
}
]
return {
type: 'field',
diff,
path,
error,
titlePath,
schemaType,
renderHeader,
key: pathToString(path) || 'root',
diffComponent: error ? undefined : component
}
}

export function buildObjectChangeList(
Expand Down Expand Up @@ -135,7 +144,14 @@ function buildArrayChangeList(
annotation: itemDiff.diff.action === 'unchanged' ? undefined : itemDiff.diff.annotation
})

acc.push(...buildChangeList(memberType, itemDiff.diff, itemPath, itemTitlePath))
const children = buildChangeList(memberType, itemDiff.diff, itemPath, itemTitlePath)
if (children.length === 0) {
// This can happen when there are no changes to the actual element, it's just been moved
acc.push(getFieldChange(memberType, itemDiff.diff, itemPath, itemTitlePath))
} else {
acc.push(...children)
}

return acc
}, list)

Expand Down

0 comments on commit d550542

Please sign in to comment.