Skip to content

Commit

Permalink
fix(form): support patch symbols for PT-input PTE patches
Browse files Browse the repository at this point in the history
  • Loading branch information
skogsmaskin authored and rexxars committed Sep 23, 2022
1 parent d9546db commit f120f6d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
HotkeyOptions,
InvalidValue,
EditorSelection,
Patch,
} from '@sanity/portable-text-editor'
import React, {
useEffect,
Expand All @@ -23,7 +24,7 @@ import {Subject} from 'rxjs'
import {Box, useToast} from '@sanity/ui'
import scrollIntoView from 'scroll-into-view-if-needed'
import {debounce} from 'lodash'
import {FormPatch as FormBuilderPatch} from '../../patch'
import {FormPatch, SANITY_PATCH_TYPE} from '../../patch'
import {ArrayOfObjectsItemMember, ObjectFormNode} from '../../store'
import type {
ArrayOfObjectsInputProps,
Expand Down Expand Up @@ -265,7 +266,7 @@ export function PortableTextInput(props: PortableTextInputProps) {
(change: EditorChange): void => {
switch (change.type) {
case 'mutation':
onChange(change.patches as FormBuilderPatch[])
onChange(toFormPatches(change.patches))
break
case 'selection':
setFocusPathDebounced(change.selection)
Expand All @@ -278,7 +279,7 @@ export function PortableTextInput(props: PortableTextInputProps) {
break
case 'undo':
case 'redo':
onChange(change.patches as FormBuilderPatch[])
onChange(toFormPatches(change.patches))
break
case 'invalidValue':
setInvalidValue(change)
Expand Down Expand Up @@ -379,3 +380,7 @@ export function PortableTextInput(props: PortableTextInputProps) {
</Box>
)
}

function toFormPatches(patches: any) {
return patches.map((p: Patch) => ({...p, patchType: SANITY_PATCH_TYPE})) as FormPatch[]
}
5 changes: 5 additions & 0 deletions packages/sanity/src/form/patch/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
FormUnsetPatch,
FormIncPatch,
FormDecPatch,
FormDiffMatchPatch,
} from './types'

export const SANITY_PATCH_TYPE = Symbol.for('sanity.patch')
Expand Down Expand Up @@ -42,6 +43,10 @@ export function unset(path: Path = []): FormUnsetPatch {
return {patchType: SANITY_PATCH_TYPE, type: 'unset', path}
}

export function diffMatchPatch(value: string, path: Path = []): FormDiffMatchPatch {
return {patchType: SANITY_PATCH_TYPE, type: 'diffMatchPatch', path, value}
}

export function inc(amount = 1, path: Path = []): FormIncPatch {
return {patchType: SANITY_PATCH_TYPE, type: 'inc', path, value: amount}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sanity/src/form/utils/mutationPatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function toFormBuilderPatches(origin: FormPatchOrigin, patch: MutationPatch): Fo
}
if (type === 'diffMatchPatch') {
return {
type: 'diffMatchPatch',
type,
path: decodePath(gradientPath),
value: patch[type][gradientPath],
origin,
Expand Down

0 comments on commit f120f6d

Please sign in to comment.