diff --git a/packages/paragraph/src/createParagraphPlugin.ts b/packages/paragraph/src/createParagraphPlugin.ts index 215d9d64e3..b71889238b 100644 --- a/packages/paragraph/src/createParagraphPlugin.ts +++ b/packages/paragraph/src/createParagraphPlugin.ts @@ -3,6 +3,7 @@ import { HotkeyPlugin, onKeyDownToggleElement, } from '@udecode/plate-common'; +import { withParagraph } from './withParagraph'; export const ELEMENT_PARAGRAPH = 'p'; @@ -15,6 +16,7 @@ export const createParagraphPlugin = createPluginFactory({ handlers: { onKeyDown: onKeyDownToggleElement, }, + withOverrides: withParagraph, options: { hotkey: ['mod+opt+0', 'mod+shift+0'], }, diff --git a/packages/paragraph/src/withParagraph.ts b/packages/paragraph/src/withParagraph.ts new file mode 100644 index 0000000000..83fa461087 --- /dev/null +++ b/packages/paragraph/src/withParagraph.ts @@ -0,0 +1,34 @@ +import { PlateEditor, Value, isSelectionAtBlockEnd, isSelectionAtBlockStart } from '@udecode/plate-common'; +import { Editor, Path, move, removeNodes } from 'slate'; + + + + +export const withParagraph = < + V extends Value = Value, + E extends PlateEditor = PlateEditor +>( + editor: E +) => { + const { deleteForward, deleteBackward } = editor; + editor.deleteForward = (unit) => { + if (isSelectionAtBlockStart(editor) && isSelectionAtBlockEnd(editor)) { + const path = editor.selection?.focus.path; + let previousListItemPath: Path; + try { + if (path != undefined) { + previousListItemPath = Path.previous(path); + } + // Delete the current empty line and work as deleteBackward + deleteBackward(unit); + move(editor as any, { unit: "line", reverse: false }); + } catch { + removeNodes(editor as any); + } + } else { + // Perform the default deleteForward behavior + deleteForward(unit); + } + }; + return editor; +};