Skip to content

Heading

The heading node is used to represent blocks of heading in the document.

'use client'

import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'

import { createEditor, type NodeJSON } from 'prosekit/core'
import { ProseKit } from 'prosekit/react'
import { useMemo } from 'react'

import { sampleContent } from '../../sample/sample-doc-heading.ts'
import { Toolbar } from '../../ui/toolbar/index.ts'

import { defineExtension } from './extension.ts'

interface EditorProps {
  initialContent?: NodeJSON
}

export default function Editor(props: EditorProps) {
  const defaultContent = props.initialContent ?? sampleContent
  const editor = useMemo(() => {
    return createEditor({ extension: defineExtension(), defaultContent })
  }, [defaultContent])

  return (
    <ProseKit editor={editor}>
      <div className="box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow-sm flex flex-col bg-[canvas] text-black dark:text-white">
        <Toolbar />
        <div className="relative w-full flex-1 box-border overflow-y-auto">
          <div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
        </div>
      </div>
    </ProseKit>
  )
}
import { defineHeadingfunction defineHeading(): HeadingExtension } from 'prosekit/extensions/heading'

const extensionconst extension: HeadingExtension = defineHeadingfunction defineHeading(): HeadingExtension()
editorconst editor: Editor<HeadingExtension>.commands
Editor<HeadingExtension>.commands: ToCommandAction<{
    setHeading: [attrs?: HeadingAttrs | undefined];
    insertHeading: [attrs?: HeadingAttrs | undefined];
    toggleHeading: [attrs?: HeadingAttrs | undefined];
}>
All {@link CommandAction } s defined by the editor.
.setHeading
setHeading: CommandAction
(attrs?: HeadingAttrs | undefined) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
({ levelHeadingAttrs.level: number: 1 })
editorconst editor: Editor<HeadingExtension>.commands
Editor<HeadingExtension>.commands: ToCommandAction<{
    setHeading: [attrs?: HeadingAttrs | undefined];
    insertHeading: [attrs?: HeadingAttrs | undefined];
    toggleHeading: [attrs?: HeadingAttrs | undefined];
}>
All {@link CommandAction } s defined by the editor.
.toggleHeading
toggleHeading: CommandAction
(attrs?: HeadingAttrs | undefined) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
({ levelHeadingAttrs.level: number: 1 })
editorconst editor: Editor<HeadingExtension>.commands
Editor<HeadingExtension>.commands: ToCommandAction<{
    setHeading: [attrs?: HeadingAttrs | undefined];
    insertHeading: [attrs?: HeadingAttrs | undefined];
    toggleHeading: [attrs?: HeadingAttrs | undefined];
}>
All {@link CommandAction } s defined by the editor.
.insertHeading
insertHeading: CommandAction
(attrs?: HeadingAttrs | undefined) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
({ levelHeadingAttrs.level: number: 1 })

| Non-Apple | Apple | Description | | ----------------------------- | ----------------------------------- | --------------------------------------------------------------------------- | | CtrlAlt1 | CommandOption1 | Set the current block to an H1 node | | CtrlAlt2 | CommandOption2 | Set the current block to an H2 node | | CtrlAlt3 | CommandOption3 | Set the current block to an H3 node | | CtrlAlt4 | CommandOption4 | Set the current block to an H4 node | | CtrlAlt5 | CommandOption5 | Set the current block to an H5 node | | CtrlAlt6 | CommandOption6 | Set the current block to an H6 node | | Backspace | Backspace | When the text cursor is at the start of a heading node, set it to paragraph |

Type # and a space to create a heading. One # makes an H1 heading, two ## make an H2 heading, and so on.