or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

command-system.mdconfiguration.mdcore-plugin.mddocument-structure.mdediting-engine.mdindex.mdui-components.md
tile.json

editing-engine.mddocs/

Heading Editing Engine

The core editing functionality for headings that handles model-view conversion, command registration, and Enter key behavior. This plugin provides the engine-level functionality for switching between heading levels and paragraphs.

Capabilities

HeadingEditing Plugin

The engine plugin that provides core heading functionality including model registration, conversion between model and view, and command setup.

/**
 * The headings engine feature. Handles switching between block formats - headings and paragraphs.
 */
class HeadingEditing extends Plugin {
    /** Plugin name identifier */
    static readonly pluginName: 'HeadingEditing';
    
    /** Indicates this is an official CKEditor 5 plugin */
    static readonly isOfficialPlugin: true;
    
    /** Required plugins that are automatically loaded */
    static readonly requires: [typeof Paragraph];
    
    /** Editor instance constructor parameter */
    constructor(editor: Editor);
    
    /** 
     * Registers model elements, conversion, and heading command.
     * Sets up the heading model elements, converters, and registers the heading command.
     */
    init(): void;
    
    /**
     * Sets up Enter key behavior for headings.
     * Configures what happens when Enter is pressed in heading elements.
     */
    afterInit(): void;
}

Usage Examples:

// Direct plugin usage (usually not needed as it's loaded by Heading plugin)
import { HeadingEditing } from '@ckeditor/ckeditor5-heading';

ClassicEditor.create(document.querySelector('#editor'), {
    plugins: [HeadingEditing, /* other required plugins */]
});

// Access the plugin instance
const headingEditing = editor.plugins.get('HeadingEditing');

// The plugin automatically registers heading model elements:
// - heading1, heading2, heading3, heading4, heading5, heading6
// - Based on configuration options

Model Element Registration

The HeadingEditing plugin registers heading model elements based on the configuration:

// Default model elements registered:
// - heading1 (maps to <h2> by default)
// - heading2 (maps to <h3> by default)  
// - heading3 (maps to <h4> by default)

// Custom model elements can be registered via configuration:
const config = {
    heading: {
        options: [
            { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
            { model: 'heading1', view: 'h1', title: 'Title', class: 'ck-heading_heading1' },
            { model: 'customHeading', view: 'h5', title: 'Custom', class: 'ck-heading_custom' }
        ]
    }
};

Conversion System

The plugin sets up bidirectional conversion between model and view:

Model to View Conversion:

  • heading1 model element → <h2> HTML element (by default)
  • heading2 model element → <h3> HTML element (by default)
  • Custom model elements → configured view elements

View to Model Conversion:

  • HTML heading elements → corresponding model elements
  • Supports upcastAlso for additional HTML patterns
// Example of how conversion works internally:
// Model: <heading1>My Title</heading1>
// View:  <h2>My Title</h2>

// Custom view mapping:
{
    model: 'heading1',
    view: { name: 'h1', classes: 'custom-title' },
    title: 'Main Title',
    class: 'ck-heading_main'
}
// Model: <heading1>My Title</heading1>  
// View:  <h1 class="custom-title">My Title</h1>

Enter Key Behavior

The afterInit() method configures special Enter key behavior for headings:

  • Single Enter: Creates a new paragraph after the heading
  • Soft Enter (Shift+Enter): Creates a line break within the heading
  • Enter at end of empty heading: Converts heading back to paragraph
// Example behavior:
// Before: <heading1>My Title|</heading1>
// After pressing Enter: 
// <heading1>My Title</heading1>
// <paragraph>|</paragraph>

// Before: <heading1>|</heading1> (empty heading)
// After pressing Enter:
// <paragraph>|</paragraph>

Command Registration

The plugin registers the heading command which is used by UI components and can be executed programmatically:

// The HeadingCommand is registered as 'heading'
const headingCommand = editor.commands.get('heading');

// Execute to apply headings
editor.execute('heading', { value: 'heading1' });
editor.execute('heading', { value: 'heading2' });
editor.execute('heading', { value: 'paragraph' });

// Check current state
console.log(headingCommand.value); // Current heading level or false
console.log(headingCommand.isEnabled); // Whether command can be executed

Schema Configuration

The plugin configures the editor schema to define where headings can be used:

// Headings are registered as block elements that:
// - Inherit from $block
// - Allow $text content
// - Can contain inline formatting
// - Are allowed in root and other block containers

// Schema example (internal):
schema.register('heading1', {
    inheritAllFrom: '$block'
});