Skip to content

Type Editor


Type Editor / @type-editor/commands

@type-editor/commands

A refactored version of ProseMirror's prosemirror-commands module, providing a comprehensive collection of editing commands for rich text editors.

Installation

bash
npm install @type-editor/commands

Overview

This module provides a set of command functions that implement common editing operations. Commands follow the standard signature:

typescript
type Command = (
  state: EditorState,
  dispatch?: DispatchFunction,
  view?: EditorView,
) => boolean;

Commands return true if they can be applied, and false otherwise. When called without a dispatch function, they simply check whether the command is applicable without making any changes.

Commands

Selection Commands

CommandDescription
selectAllSelects the entire document content. Typically bound to Mod-a.
selectParentNodeExpands selection to encompass the parent node containing the current selection. Useful for progressively expanding selection outward.
selectTextblockStartMoves cursor to the start of the current textblock. Useful for "Home" key behavior.
selectTextblockEndMoves cursor to the end of the current textblock. Useful for "End" key behavior.
selectNodeBackwardSelects the node before the cursor when at the start of a textblock. Fallback for Backspace when joining fails.
selectNodeForwardSelects the node after the cursor when at the end of a textblock. Fallback for Delete when joining fails.
selectHorizontallyBackwardHandles left arrow key behavior including node selection at boundaries.
selectHorizontallyForwardHandles right arrow key behavior including node selection at boundaries.
selectVerticallyUpHandles up arrow key behavior for block-level selections.
selectVerticallyDownHandles down arrow key behavior for block-level selections.

Deletion Commands

CommandDescription
deleteSelectionDeletes the current selection if one exists. Returns false for empty selections.
backspaceDefault command for the Backspace key. Chains deleteSelection, joinBackward, and selectNodeBackward.
delDefault command for the Delete key. Chains deleteSelection, joinForward, and selectNodeForward.

Join Commands

CommandDescription
joinBackwardJoins or merges the current block with the one before it. Implements comprehensive backward-joining with multiple fallback strategies.
joinForwardJoins or merges the current block with the one after it. Forward counterpart to joinBackward.
joinUpJoins the selected block with the block above it. Typically bound to Alt-ArrowUp.
joinDownJoins the selected block with the block below it. Typically bound to Alt-ArrowDown.
joinTextblockBackwardSpecifically joins textblocks, navigating through nested structures. Stricter than joinBackward.
joinTextblockForwardSpecifically joins textblocks, navigating through nested structures. Stricter than joinForward.

Block Manipulation Commands

CommandDescription
splitBlockSplits the parent block at the selection. Typically bound to Enter.
splitBlockKeepMarksSplits a block while preserving active marks for continued formatting.
splitBlockAs(splitNode?)Factory function to create custom block splitting commands with control over the new block type.
liftLifts the selected block out of its parent node (e.g., removes from list, unwraps blockquote).
liftEmptyBlockLifts an empty textblock out of its parent, with intelligent split/lift behavior.
createParagraphNearCreates an empty paragraph before or after a selected block node.
wrapIn(nodeType, attrs?)Creates a command that wraps the selection in a given node type (e.g., blockquote, list).
setBlockType(nodeType, attrs?)Creates a command that converts selected textblocks to a given type (e.g., heading, paragraph).

Code Block Commands

CommandDescription
newlineInCodeInserts a literal newline character inside code blocks. Essential for code formatting.
exitCodeCreates a default block after a code block and moves the cursor there. Allows exiting code blocks.

Mark Commands

CommandDescription
toggleMark(markType, attrs?, options?)Creates a command that toggles a mark (bold, italic, etc.) on the selection. Handles both cursor and range selections.
clearTextFormattingRemoves common text formatting marks (strong, em, underline, link) from the current selection.
CommandDescription
autoLink(keyType, linkMarkType?, codeNodeType?)Automatically converts URLs to clickable links when Enter or Space is pressed. Detects URLs and wraps them with a link mark.
autoDeleteLink(keyType, linkMarkType?, fileLinkType?)Removes link marks from the current selection or cursor position. Extends selection to cover entire linked text if needed.

Attribute Commands

CommandDescription
setAttribute(attributeName, attribute, ...)Sets an attribute on nodes within the current selection. Supports parent mode (outermost ancestor) or selection mode (all nodes in selection).

Insert Commands

CommandDescription
insertHardBreak(nodeType)Creates a command that inserts a hard break (line break) at the current selection.

Toggle Commands

CommandDescription
toggleBlockType(nodeType, unwrapNodeType, attrs?, allowUnwrap?)Toggles between two block types. Converts to unwrapNodeType if already in nodeType, otherwise converts to nodeType.
toggleWrapIn(nodeType, attrs?, allowUnwrap?)Toggles wrapping of selected content in the given node type. Lifts (unwraps) if already inside, otherwise wraps.

Zoom Commands

CommandDescription
zoomInZooms in the editor view by 10%, up to a maximum of 200%.
zoomOutZooms out the editor view by 10%, down to a minimum of 10%.
zoomResetResets the editor view zoom to 100%.

Utility Commands

CommandDescription
chainCommands(...commands)Combines multiple commands into one that tries each in sequence until one succeeds.
chainCommandsExectuteAll(...commands)Combines multiple commands into one that executes all commands regardless of their return value.
autoJoin(command, isJoinable)Wraps a command to automatically join adjacent nodes when they become joinable.
findExtendedMarkSelection(doc, $cursor, markType, onlyNumbers)Finds an extended selection for an empty selection based on mark type. Extends to cover entire marked range.
isCodeBlock(state, codeNodeType?)Checks if the current selection is within a code block.

Browser Integration Commands

CommandDescription
stopNativeHorizontalDeleteBackwardPrevents native browser delete behavior for non-text nodes (Backspace).
stopNativeHorizontalDeleteForwardPrevents native browser delete behavior for non-text nodes (Delete).
skipIgnoredNodesBeforeEnsures cursor isn't positioned after ignored/zero-size nodes.
skipIgnoredNodesAfterEnsures cursor isn't positioned before ignored/zero-size nodes.

Usage Examples

Basic Keymap Setup

typescript
import {
  backspace,
  del,
  joinUp,
  joinDown,
  lift,
  selectAll,
  selectParentNode,
  splitBlockKeepMarks,
  newlineInCode,
  exitCode,
  liftEmptyBlock,
  chainCommands,
} from "@type-editor/commands";

const keymap = {
  Backspace: backspace,
  Delete: del,
  "Mod-a": selectAll,
  Escape: selectParentNode,
  "Alt-ArrowUp": joinUp,
  "Alt-ArrowDown": joinDown,
  "Mod-[": lift,
  Enter: chainCommands(
    newlineInCode,
    exitCode,
    liftEmptyBlock,
    splitBlockKeepMarks,
  ),
};

Formatting Commands

typescript
import { toggleMark, setBlockType, wrapIn } from "@type-editor/commands";

// Create mark toggle commands
const toggleBold = toggleMark(schema.marks.strong);
const toggleItalic = toggleMark(schema.marks.em);

// Create block type commands
const makeH1 = setBlockType(schema.nodes.heading, { level: 1 });
const makeParagraph = setBlockType(schema.nodes.paragraph);

// Create wrapping commands
const wrapInBlockquote = wrapIn(schema.nodes.blockquote);
const wrapInBulletList = wrapIn(schema.nodes.bullet_list);

Custom Command Chaining

typescript
import {
  chainCommands,
  deleteSelection,
  joinBackward,
  selectNodeBackward,
} from "@type-editor/commands";

// Create custom backspace behavior
const customBackspace = chainCommands(
  deleteSelection,
  joinBackward,
  selectNodeBackward,
);

Auto-joining Nodes

typescript
import { autoJoin, wrapIn } from "@type-editor/commands";

// Wrap in list with auto-join for adjacent lists
const wrapInList = autoJoin(wrapIn(schema.nodes.bullet_list), [
  "bullet_list",
  "ordered_list",
]);

License

MIT

Modules

ModuleDescription

auto-delete-link

auto-join

auto-link

backspace

clear-text-formatting

create-paragraph-near

del

delete-selection

exit-code

insert-hard-break

join-backward

join-down

join-forward

join-list-item-backward

join-textblock-backward

join-textblock-forward

join-up

lift

lift-empty-block

newline-in-code

select-all

select-horizontally

select-node-backward

select-node-forward

select-parent-node

select-textblock

select-vertically

set-attribute

set-block-type

skip-ignored-nodes

split-block

split-block-keep-marks

stop-native-horizontal-delete

toggle-block-type

toggle-mark

toggle-wrap-in

types/SplitInfo

types/ToggleMarkOptions

util/apply-selection

util/chain-commands

util/find-extended-mark-selection

util/helpers

util/is-code-block

util/move-selection-block

wrap-in

zoom