Skip to content

Commit

Permalink
Merge pull request #3457 from nklhtv/exit-break-block-factory
Browse files Browse the repository at this point in the history
Use block factory to create elements in exit break
  • Loading branch information
zbeyens authored Aug 22, 2024
2 parents ab3d14c + 68a8abf commit b290c8d
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/few-students-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@udecode/plate-autoformat': patch
'@udecode/plate-code-block': patch
'@udecode/plate-break': patch
---

Use editor.blockFactory to create default elements
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('when ``` at block start', () => {
</editor>
) as any;

input.blockFactory = (node: any) => node;
const editor = withAutoformat(withReact(input), autoformatPlugin as any);

editor.insertText('`');
Expand Down Expand Up @@ -75,6 +76,7 @@ describe('when ``` at block start, but customising with query we get the most re
</editor>
) as any;

input.blockFactory = (node: any) => node;
const codeEditor = withAutoformat(
withReact(input),
mockPlugin<AutoformatPlugin>({
Expand Down Expand Up @@ -140,6 +142,7 @@ describe('when ```', () => {
</editor>
) as any;

input.blockFactory = (node: any) => node;
const editor = withAutoformat(
withReact(input),
mockPlugin(autoformatPlugin as any)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const output = (

it('should be', () => {
jest.spyOn(isHotkey, 'isHotkey').mockReturnValue(true);
input.blockFactory = (node: any) => node;
onKeyDownExitBreak(
input,
mockPlugin({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const output = (
) as any;

it('should be', () => {
input.blockFactory = (node: any) => node;
onKeyDownExitBreak(input, mockPlugin())(event);
expect(input.children).toEqual(output.children);
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const output = (

it('should be', () => {
jest.spyOn(isHotkey, 'isHotkey').mockReturnValue(true);
input.blockFactory = (node: any) => node;
onKeyDownExitBreak(
input,
mockPlugin({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const output = (

it('should be', () => {
jest.spyOn(isHotkey, 'isHotkey').mockReturnValue(true);
input.blockFactory = (node: any) => node;
onKeyDownExitBreak(
input,
mockPlugin({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const output = (

it('should be', () => {
jest.spyOn(isHotkey, 'isHotkey').mockReturnValue(true);
input.blockFactory = (node: any) => node;
onKeyDownExitBreak(
input,
mockPlugin({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const output = (

it('should be', () => {
jest.spyOn(isHotkey, 'isHotkey').mockReturnValue(true);
input.blockFactory = (node: any) => node;
onKeyDownExitBreak(
input,
mockPlugin({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const output = (

it('should be', () => {
jest.spyOn(isHotkey, 'isHotkey').mockReturnValue(true);
input.blockFactory = (node: any) => node;
onKeyDownExitBreak(
input,
mockPlugin({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const output = (

it('should be', () => {
jest.spyOn(isHotkey, 'isHotkey').mockReturnValue(true);
input.blockFactory = (node: any) => node;
onKeyDownExitBreak(
input,
mockPlugin({
Expand Down
9 changes: 6 additions & 3 deletions packages/break/src/exit-break/transforms/exitBreak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import type { ExitBreakRule } from '../types';

import { exitBreakAtEdges } from '../queries/exitBreakAtEdges';

export const exitBreak = <V extends Value>(
editor: PlateEditor<V>,
export const exitBreak = <
V extends Value = Value,
E extends PlateEditor<V> = PlateEditor<V>,
>(
editor: E,
{
before,
defaultType = getPluginType(editor, ELEMENT_DEFAULT),
Expand All @@ -39,7 +42,7 @@ export const exitBreak = <V extends Value>(

insertElements(
editor,
{ children: [{ text: '' }], type: defaultType },
editor.blockFactory({ children: [{ text: '' }], type: defaultType }),
{
at: insertPath,
select: !isStart,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import { insertCodeBlock } from './insertCodeBlock';
* Called by toolbars to make sure a code-block gets inserted below a paragraph
* rather than awkwardly splitting the current selection.
*/
export const insertEmptyCodeBlock = <V extends Value>(
editor: PlateEditor<V>,
export const insertEmptyCodeBlock = <
V extends Value = Value,
E extends PlateEditor<V> = PlateEditor<V>,
>(
editor: E,
{
defaultType = getPluginType(editor, ELEMENT_DEFAULT),
insertNodesOptions,
Expand All @@ -27,7 +30,7 @@ export const insertEmptyCodeBlock = <V extends Value>(
if (isExpanded(editor.selection) || !isBlockAboveEmpty(editor)) {
insertElements(
editor,
{ children: [{ text: '' }], type: defaultType },
editor.blockFactory({ children: [{ text: '' }], type: defaultType }),
{
nextBlock: true,
select: true,
Expand Down

0 comments on commit b290c8d

Please sign in to comment.