-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(portable-text-editor): preserve keys on undo/redo (#5805)
* refactor(portable-text-editor): preserve keys on undo/redo * fix(portable-text-editor): add forgotten static class functions for undo and redo * test(portable-text-editor): add tests for undo/redo preserve keys
- Loading branch information
1 parent
6e551b0
commit f83e8e4
Showing
4 changed files
with
148 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/@sanity/portable-text-editor/src/editor/plugins/__tests__/withUndoRedo.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import {describe, expect, it, jest} from '@jest/globals' | ||
import {render, waitFor} from '@testing-library/react' | ||
import {createRef, type RefObject} from 'react' | ||
|
||
import {PortableTextEditorTester, schemaType} from '../../__tests__/PortableTextEditorTester' | ||
import {PortableTextEditor} from '../../PortableTextEditor' | ||
|
||
const initialValue = [ | ||
{ | ||
_key: 'a', | ||
_type: 'myTestBlockType', | ||
children: [ | ||
{ | ||
_key: 'a1', | ||
_type: 'span', | ||
marks: [], | ||
text: 'Block A', | ||
}, | ||
], | ||
markDefs: [], | ||
style: 'normal', | ||
}, | ||
{ | ||
_key: 'b', | ||
_type: 'myTestBlockType', | ||
children: [ | ||
{ | ||
_key: 'b1', | ||
_type: 'span', | ||
marks: [], | ||
text: 'Block B', | ||
}, | ||
], | ||
markDefs: [], | ||
style: 'normal', | ||
}, | ||
] | ||
|
||
const initialSelection = { | ||
focus: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 7}, | ||
anchor: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 7}, | ||
} | ||
|
||
describe('plugin:withUndoRedo', () => { | ||
it('preserves the keys when undoing ', async () => { | ||
const editorRef: RefObject<PortableTextEditor> = createRef() | ||
const onChange = jest.fn() | ||
render( | ||
<PortableTextEditorTester | ||
onChange={onChange} | ||
ref={editorRef} | ||
schemaType={schemaType} | ||
value={initialValue} | ||
/>, | ||
) | ||
await waitFor(() => { | ||
if (editorRef.current) { | ||
PortableTextEditor.focus(editorRef.current) | ||
PortableTextEditor.select(editorRef.current, initialSelection) | ||
PortableTextEditor.delete( | ||
editorRef.current, | ||
PortableTextEditor.getSelection(editorRef.current), | ||
{mode: 'blocks'}, | ||
) | ||
expect(PortableTextEditor.getValue(editorRef.current)).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"_key": "a", | ||
"_type": "myTestBlockType", | ||
"children": Array [ | ||
Object { | ||
"_key": "a1", | ||
"_type": "span", | ||
"marks": Array [], | ||
"text": "Block A", | ||
}, | ||
], | ||
"markDefs": Array [], | ||
"style": "normal", | ||
}, | ||
] | ||
`) | ||
PortableTextEditor.undo(editorRef.current) | ||
expect(PortableTextEditor.getValue(editorRef.current)).toEqual(initialValue) | ||
} | ||
}) | ||
}) | ||
it('preserves the keys when redoing ', async () => { | ||
const editorRef: RefObject<PortableTextEditor> = createRef() | ||
const onChange = jest.fn() | ||
render( | ||
<PortableTextEditorTester | ||
onChange={onChange} | ||
ref={editorRef} | ||
schemaType={schemaType} | ||
value={initialValue} | ||
/>, | ||
) | ||
await waitFor(() => { | ||
if (editorRef.current) { | ||
PortableTextEditor.focus(editorRef.current) | ||
PortableTextEditor.select(editorRef.current, initialSelection) | ||
PortableTextEditor.insertBlock(editorRef.current, editorRef.current.schemaTypes.block, { | ||
children: [{_key: 'c1', _type: 'span', marks: [], text: 'Block C'}], | ||
}) | ||
const producedKey = PortableTextEditor.getValue(editorRef.current)?.slice(-1)[0]?._key | ||
PortableTextEditor.undo(editorRef.current) | ||
PortableTextEditor.redo(editorRef.current) | ||
expect(PortableTextEditor.getValue(editorRef.current)?.slice(-1)[0]?._key).toEqual( | ||
producedKey, | ||
) | ||
} | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters