Skip to content

Commit

Permalink
refactor(sanity): remove duplicate PatchOperations interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuslundgard committed Oct 4, 2022
1 parent 70186c4 commit 2af022e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
28 changes: 15 additions & 13 deletions packages/sanity/src/core/field/diff/changes/undoChange.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import {isIndexSegment, isKeyedObject, isKeySegment, isTypedObject, Path} from '@sanity/types'
import {
isIndexSegment,
isKeyedObject,
isKeySegment,
isTypedObject,
PatchOperations,
Path,
} from '@sanity/types'
import {diffItem} from 'sanity-diff-patch'
import {isRecord} from '../../../util'
import {
Expand All @@ -17,7 +24,6 @@ import {
ItemDiff,
ObjectDiff,
FieldOperationsAPI,
_PatchOperations,
SetDiffPatch,
UnsetDiffPatch,
} from '../../types'
Expand All @@ -34,7 +40,7 @@ export function undoChange(
return
}

const patches: _PatchOperations[] = []
const patches: PatchOperations[] = []

if (change.type === 'group') {
const allChanges = flattenChangeNode(change)
Expand Down Expand Up @@ -79,7 +85,7 @@ function buildUnsetPatch(rootDiff: ObjectDiff, path: Path, concurrentUnsetPaths:
return furthestEmptyAncestor(previousValue, path, concurrentUnsetPaths)
}

function buildUnsetPatches(rootDiff: ObjectDiff, paths: Path[]): _PatchOperations[] {
function buildUnsetPatches(rootDiff: ObjectDiff, paths: Path[]): PatchOperations[] {
const patches: Path[] = []

for (let i = 0; i < paths.length; i++) {
Expand Down Expand Up @@ -161,7 +167,7 @@ function buildMovePatches(
itemDiff: ItemDiff,
parentDiff: ArrayDiff,
path: Path
): _PatchOperations[] {
): PatchOperations[] {
const basePath = path.slice(0, -1)
const {parentValue, fromIndex, fromValue} = getFromItem(parentDiff, itemDiff)

Expand All @@ -187,7 +193,7 @@ function buildMovePatches(
]
}

function buildUndoPatches(diff: Diff, rootDiff: ObjectDiff, path: Path): _PatchOperations[] {
function buildUndoPatches(diff: Diff, rootDiff: ObjectDiff, path: Path): PatchOperations[] {
const patches = diffItem(diff.toValue, diff.fromValue, diffOptions, path) as DiffPatch[]

const inserts = patches
Expand All @@ -199,7 +205,7 @@ function buildUndoPatches(diff: Diff, rootDiff: ObjectDiff, path: Path): _PatchO
.reduce((acc, patch) => acc.concat(pathToString(patch.path)), [] as string[])

const stubbedPaths = new Set<string>()
const stubs: _PatchOperations[] = []
const stubs: PatchOperations[] = []

let hasSets = false
const sets = patches
Expand All @@ -219,14 +225,10 @@ function buildUndoPatches(diff: Diff, rootDiff: ObjectDiff, path: Path): _PatchO
]
}

function getParentStubs(
path: Path,
rootDiff: ObjectDiff,
stubbed: Set<string>
): _PatchOperations[] {
function getParentStubs(path: Path, rootDiff: ObjectDiff, stubbed: Set<string>): PatchOperations[] {
const value = rootDiff.fromValue as Record<string, unknown>
const nextValue = rootDiff.toValue as Record<string, unknown>
const stubs: _PatchOperations[] = []
const stubs: PatchOperations[] = []

for (let i = 1; i <= path.length; i++) {
const subPath = path.slice(0, i)
Expand Down
16 changes: 2 additions & 14 deletions packages/sanity/src/core/field/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ObjectFieldType,
ConditionalProperty,
SchemaType,
PatchOperations,
} from '@sanity/types'
import {ComponentType} from 'react'
import {
Expand Down Expand Up @@ -262,23 +263,10 @@ export interface FromToIndex {
/** @internal */
export type ChangeTitlePath = (string | FromToIndex)[]

/** @internal */
export interface _PatchOperations {
set?: {[key: string]: unknown}
setIfMissing?: {[key: string]: unknown}
merge?: {[key: string]: unknown}
diffMatchPatch?: {[key: string]: unknown}
unset?: string[]
inc?: {[key: string]: number}
dec?: {[key: string]: number}
insert?: FormInsertPatch
ifRevisionID?: string
}

/** @internal */
export interface FieldOperationsAPI {
patch: {
execute: (patches: _PatchOperations[]) => void
execute: (patches: PatchOperations[]) => void
}
}

Expand Down

0 comments on commit 2af022e

Please sign in to comment.