-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(pte): insert empty text block after removing void block (#6552)
* fix(pte): insert empty text block after removing void block at position 0 * fix(pte): refactor createWithPlaceholderBlock
- Loading branch information
1 parent
fada62f
commit 379510f
Showing
9 changed files
with
284 additions
and
34 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
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
133 changes: 133 additions & 0 deletions
133
...s/@sanity/portable-text-editor/src/editor/plugins/__tests__/withPlaceholderBlock.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,133 @@ | ||
import {describe, expect, it, jest} from '@jest/globals' | ||
/* eslint-disable max-nested-callbacks */ | ||
import {render, waitFor} from '@testing-library/react' | ||
import {createRef, type RefObject} from 'react' | ||
|
||
import {PortableTextEditorTester, schemaType} from '../../__tests__/PortableTextEditorTester' | ||
import {PortableTextEditor} from '../../PortableTextEditor' | ||
|
||
describe('plugin:withPlaceholderBlock', () => { | ||
describe('removing nodes', () => { | ||
it("should insert an empty text block if it's removing the only block", async () => { | ||
const editorRef: RefObject<PortableTextEditor> = createRef() | ||
const initialValue = [ | ||
{ | ||
_key: '5fc57af23597', | ||
_type: 'someObject', | ||
}, | ||
] | ||
const onChange = jest.fn() | ||
await waitFor(() => { | ||
render( | ||
<PortableTextEditorTester | ||
onChange={onChange} | ||
ref={editorRef} | ||
schemaType={schemaType} | ||
value={initialValue} | ||
/>, | ||
) | ||
}) | ||
|
||
await waitFor(() => { | ||
if (editorRef.current) { | ||
PortableTextEditor.focus(editorRef.current) | ||
|
||
PortableTextEditor.delete( | ||
editorRef.current, | ||
{ | ||
focus: {path: [{_key: '5fc57af23597'}], offset: 0}, | ||
anchor: {path: [{_key: '5fc57af23597'}], offset: 0}, | ||
}, | ||
{mode: 'blocks'}, | ||
) | ||
|
||
const value = PortableTextEditor.getValue(editorRef.current) | ||
|
||
expect(value).toEqual([ | ||
{ | ||
_type: 'myTestBlockType', | ||
_key: '3', | ||
style: 'normal', | ||
markDefs: [], | ||
children: [ | ||
{ | ||
_type: 'span', | ||
_key: '2', | ||
text: '', | ||
marks: [], | ||
}, | ||
], | ||
}, | ||
]) | ||
} | ||
}) | ||
}) | ||
it('should not insert a new block if we have more blocks available', async () => { | ||
const editorRef: RefObject<PortableTextEditor> = createRef() | ||
const initialValue = [ | ||
{ | ||
_key: '5fc57af23597', | ||
_type: 'someObject', | ||
}, | ||
{ | ||
_type: 'myTestBlockType', | ||
_key: 'existingBlock', | ||
style: 'normal', | ||
markDefs: [], | ||
children: [ | ||
{ | ||
_type: 'span', | ||
_key: '2', | ||
text: '', | ||
marks: [], | ||
}, | ||
], | ||
}, | ||
] | ||
const onChange = jest.fn() | ||
await waitFor(() => { | ||
render( | ||
<PortableTextEditorTester | ||
onChange={onChange} | ||
ref={editorRef} | ||
schemaType={schemaType} | ||
value={initialValue} | ||
/>, | ||
) | ||
}) | ||
|
||
await waitFor(() => { | ||
if (editorRef.current) { | ||
PortableTextEditor.focus(editorRef.current) | ||
|
||
PortableTextEditor.delete( | ||
editorRef.current, | ||
{ | ||
focus: {path: [{_key: '5fc57af23597'}], offset: 0}, | ||
anchor: {path: [{_key: '5fc57af23597'}], offset: 0}, | ||
}, | ||
{mode: 'blocks'}, | ||
) | ||
|
||
const value = PortableTextEditor.getValue(editorRef.current) | ||
expect(value).toEqual([ | ||
{ | ||
_type: 'myTestBlockType', | ||
_key: 'existingBlock', | ||
style: 'normal', | ||
markDefs: [], | ||
children: [ | ||
{ | ||
_type: 'span', | ||
_key: '2', | ||
text: '', | ||
marks: [], | ||
}, | ||
], | ||
}, | ||
]) | ||
} | ||
}) | ||
}) | ||
}) | ||
}) |
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
46 changes: 21 additions & 25 deletions
46
packages/@sanity/portable-text-editor/src/editor/plugins/createWithPlaceholderBlock.ts
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
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