From 0a9486ea85c2f830ad352da09f40a404de334df6 Mon Sep 17 00:00:00 2001 From: d3m1d0v Date: Thu, 18 May 2023 16:03:38 +0300 Subject: [PATCH] feat(Selecion): support backspace action for fake paragraph --- src/extensions/behavior/Selection/commands.ts | 13 +++++++++++++ src/extensions/behavior/Selection/selection.ts | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/extensions/behavior/Selection/commands.ts b/src/extensions/behavior/Selection/commands.ts index 1d92ec7a..f6e99d3b 100644 --- a/src/extensions/behavior/Selection/commands.ts +++ b/src/extensions/behavior/Selection/commands.ts @@ -1,5 +1,6 @@ import type {Node, ResolvedPos} from 'prosemirror-model'; import type {Command, NodeSelection, TextSelection, Transaction} from 'prosemirror-state'; +import {Selection} from 'prosemirror-state'; import {isCodeBlock} from '../../../utils/nodes'; import {isNodeSelection, isTextSelection} from '../../../utils/selection'; @@ -174,3 +175,15 @@ export const arrowLeft = arrow('left'); export const arrowDown = arrow('down'); export const arrowUp = arrow('up'); export const arrowRight = arrow('right'); + +export const backspace: Command = (state, dispatch) => { + const sel = state.selection; + if (isGapCursorSelection(sel)) { + const newSel = Selection.findFrom(sel.$pos, -1); + if (newSel) { + dispatch?.(state.tr.setSelection(newSel).scrollIntoView()); + return true; + } + } + return false; +}; diff --git a/src/extensions/behavior/Selection/selection.ts b/src/extensions/behavior/Selection/selection.ts index 29d224a9..90a13f25 100644 --- a/src/extensions/behavior/Selection/selection.ts +++ b/src/extensions/behavior/Selection/selection.ts @@ -14,7 +14,7 @@ import { import {isSelectableNode} from '../../../utils/nodes'; import {isNodeSelection} from '../../../utils/selection'; -import {arrowDown, arrowLeft, arrowRight, arrowUp} from './commands'; +import {arrowDown, arrowLeft, arrowRight, arrowUp, backspace} from './commands'; import './selection.scss'; @@ -26,6 +26,7 @@ export const selection = () => ArrowRight: arrowRight, ArrowUp: arrowUp, ArrowDown: arrowDown, + Backspace: backspace, }), decorations(state) { return getDecorations(state.tr);