Skip to content

Commit

Permalink
Delete forward on empty line before code block unwraps code line udec…
Browse files Browse the repository at this point in the history
  • Loading branch information
Nageen Chand committed Oct 8, 2023
1 parent 65601f9 commit c46c6ea
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/paragraph/src/createParagraphPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
HotkeyPlugin,
onKeyDownToggleElement,
} from '@udecode/plate-common';
import { withParagraph } from './withParagraph';

export const ELEMENT_PARAGRAPH = 'p';

Expand All @@ -15,6 +16,7 @@ export const createParagraphPlugin = createPluginFactory<HotkeyPlugin>({
handlers: {
onKeyDown: onKeyDownToggleElement,
},
withOverrides: withParagraph,
options: {
hotkey: ['mod+opt+0', 'mod+shift+0'],
},
Expand Down
34 changes: 34 additions & 0 deletions packages/paragraph/src/withParagraph.ts
Original file line number Diff line number Diff line change
@@ -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<V> = PlateEditor<V>
>(
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;
};

0 comments on commit c46c6ea

Please sign in to comment.