Skip to content

Commit 1a36a74

Browse files
authoredFeb 21, 2024
feat(core/inputs): support custom editor change callback (#5803)
* feat(form/inputs): add onEditorChange prop to PT-input This will allow consumers to hook into editor changes by supplying their own handler to the PortableTextInput props * test(playwright-ct): add test for PortableTextInput onEditorChange prop
1 parent f655b5c commit 1a36a74

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed
 

‎packages/sanity/playwright-ct/tests/formBuilder/inputs/PortableText/Input.spec.tsx

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expect, test} from '@playwright/experimental-ct-react'
2-
import {type PortableTextEditor} from '@sanity/portable-text-editor'
2+
import {type EditorChange, type PortableTextEditor} from '@sanity/portable-text-editor'
33
import {type RefObject} from 'react'
44

55
import {testHelpers} from '../../../utils/testHelpers'
@@ -60,4 +60,15 @@ test.describe('Portable Text Input', () => {
6060
expect(ref?.current?.schemaTypes.block).toBeDefined()
6161
})
6262
})
63+
64+
test.describe('onEditorChange', () => {
65+
test(`Supports own handler of editor changes through props`, async ({mount, page}) => {
66+
const changes: EditorChange[] = []
67+
const pushChange = (change: EditorChange) => changes.push(change)
68+
await mount(<InputStory onEditorChange={pushChange} />)
69+
const $editor = page.getByTestId('pt-input-with-editor-ref')
70+
await expect($editor).toBeVisible()
71+
expect(changes.slice(-1)[0].type).toEqual('ready')
72+
})
73+
})
6374
})

‎packages/sanity/playwright-ct/tests/formBuilder/inputs/PortableText/InputStory.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {type PortableTextEditor} from '@sanity/portable-text-editor'
1+
import {type EditorChange, type PortableTextEditor} from '@sanity/portable-text-editor'
22
import {defineArrayMember, defineField, defineType} from '@sanity/types'
33
import {createRef, type RefObject, useMemo, useState} from 'react'
44
import {type InputProps, type PortableTextInputProps} from 'sanity'
@@ -8,6 +8,7 @@ import {TestWrapper} from '../../utils/TestWrapper'
88

99
export function InputStory(props: {
1010
getRef?: (editorRef: RefObject<PortableTextEditor | null>) => void
11+
onEditorChange?: (change: EditorChange) => void
1112
}) {
1213
// Use a state as ref here to be make sure we are able to call the ref callback when
1314
// the ref is ready
@@ -35,6 +36,7 @@ export function InputStory(props: {
3536
input: (inputProps: InputProps) => {
3637
const editorProps = {
3738
...inputProps,
39+
onEditorChange: props.onEditorChange,
3840
editorRef: createRef(),
3941
} as PortableTextInputProps
4042
if (editorProps.editorRef) {
@@ -51,7 +53,7 @@ export function InputStory(props: {
5153
],
5254
}),
5355
],
54-
[],
56+
[props.onEditorChange],
5557
)
5658

5759
return (

‎packages/sanity/src/core/form/inputs/PortableText/PortableTextInput.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export function PortableTextInput(props: PortableTextInputProps) {
6565
hotkeys,
6666
markers = EMPTY_ARRAY,
6767
onChange,
68+
onEditorChange,
6869
onCopy,
6970
onInsert,
7071
onItemRemove,
@@ -220,8 +221,11 @@ export function PortableTextInput(props: PortableTextInputProps) {
220221
break
221222
default:
222223
}
224+
if (editorRef.current && onEditorChange) {
225+
onEditorChange(change, editorRef.current)
226+
}
223227
},
224-
[onBlur, onChange, setFocusPathFromEditorSelection, toast],
228+
[editorRef, onBlur, onChange, onEditorChange, setFocusPathFromEditorSelection, toast],
225229
)
226230

227231
useEffect(() => {

‎packages/sanity/src/core/form/types/inputProps.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
type EditorChange,
23
type HotkeyOptions,
34
type OnCopyFn,
45
type OnPasteFn,
@@ -507,6 +508,10 @@ export interface PortableTextInputProps
507508
* Use the `renderBlock` interface instead.
508509
*/
509510
markers?: PortableTextMarker[]
511+
/**
512+
* Returns changes from the underlying editor
513+
*/
514+
onEditorChange?: (change: EditorChange, editor: PortableTextEditor) => void
510515
/**
511516
* Custom copy function
512517
*/

0 commit comments

Comments
 (0)
Please sign in to comment.