Skip to content

Type Editor


Type Editor / @type-editor/commands / set-block-type / setBlockType

Function: setBlockType()

ts
function setBlockType(nodeType, attrs?): Command;

Defined in: set-block-type.ts:52

Creates a command that converts selected textblocks to a given node type.

This command factory function returns a command that attempts to change the type of all textblocks within the selection to the specified node type with the given attributes. This is commonly used for:

  • Converting paragraphs to headings
  • Changing heading levels
  • Converting blocks to code blocks
  • Setting or removing block-level formatting

The command will check each textblock in the selection and only proceed if at least one block can be converted. Blocks that already have the target type and attributes are skipped. The command respects schema constraints and will only convert blocks where the parent node allows the new type.

Parameters

ParameterTypeDefault valueDescription
nodeTypeNodeTypeundefinedThe node type to convert textblocks to
attrsReadonly<Record<string, any>>nullOptional attributes to set on the converted blocks (defaults to null)

Returns

Command

A command that performs the block type conversion

Example

typescript
// Create commands for different heading levels
const makeH1 = setBlockType(schema.nodes.heading, { level: 1 });
const makeH2 = setBlockType(schema.nodes.heading, { level: 2 });
const makeParagraph = setBlockType(schema.nodes.paragraph);

// Use in a keymap
const keymap = {
  "Mod-Alt-1": makeH1,
  "Mod-Alt-2": makeH2,
  "Mod-Alt-0": makeParagraph,
};

// Use in a menu
const menuItem = {
  label: "Convert to Heading 1",
  run: setBlockType(schema.nodes.heading, { level: 1 }),
  enable: (state) => setBlockType(schema.nodes.heading, { level: 1 })(state),
};