Files
outline/shared/editor/commands/backspaceToParagraph.ts
Tom Moor bf45e97641 chore: Enforce type import consistency (#10968)
* Update types

* fix circular dep

* type imports

* lint type imports and --fix
2025-12-19 23:07:02 -05:00

39 lines
1021 B
TypeScript

import type { NodeType } from "prosemirror-model";
import type { Command } from "prosemirror-state";
/**
* Converts the current node to a paragraph when pressing backspace at the
* beginning of the node and not already a paragraph.
*
* @param type The node type
* @returns A prosemirror command.
*/
export default function backspaceToParagraph(type: NodeType): Command {
return (state, dispatch, view) => {
const { $from, from, to, empty } = state.selection;
// if the selection has anything in it then use standard delete behavior
if (!empty) {
return false;
}
// check we're in a matching node
if ($from.parent.type !== type) {
return false;
}
// check if we're at the beginning of the heading
if (!view?.endOfTextblock("backward", state)) {
return false;
}
// okay, replace it with a paragraph
dispatch?.(
state.tr
.setBlockType(from, to, type.schema.nodes.paragraph)
.scrollIntoView()
);
return true;
};
}