Skip to content
GitHubDiscord

Text Align

Sets the horizontal alignment of the block nodes using the text-align CSS property.

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

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

import { defineExtension } from './extension'
import Toolbar from './toolbar'

const defaultContent = '<h1 style="text-align:center;">Heading</h1>'
  + '<p style="text-align:left;">First paragraph</p>'
  + '<p style="text-align:center;">Second paragraph</p>'
  + '<p style="text-align:right;">Third paragraph</p>'

export default function Editor() {
  const editor = useMemo(() => {
    const extension = defineExtension()
    return createEditor({ extension, 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-white dark:bg-gray-950 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 { defineTextAlignfunction defineTextAlign<NodeName extends string = string>(options: TextAlignOptions<NodeName>): TextAlignExtension<NodeName>
Adds a `textAlign` attribute to the specified nodes. This will be rendered as a CSS `text-align` style.
@public
} from 'prosekit/extensions/text-align'
const extensionconst extension: TextAlignExtension<"paragraph" | "heading"> = defineTextAligndefineTextAlign<"paragraph" | "heading">(options: TextAlignOptions<"paragraph" | "heading">): TextAlignExtension<"paragraph" | "heading">
Adds a `textAlign` attribute to the specified nodes. This will be rendered as a CSS `text-align` style.
@public
({
// A list of nodes that will be applied. typesTextAlignOptions<"paragraph" | "heading">.types: ("paragraph" | "heading")[]
The names of node to add the attribute to.
@example["paragraph", "heading"]
: ['paragraph', 'heading'],
// An optional default alignment value. Defaults to `left`. defaultTextAlignOptions<NodeName extends string = string>.default?: string | undefined
The default value for the attribute.
@default"left"
: 'center',
})

Apply an alignment value to selected blocks.

editorconst editor: Editor<TextAlignExtension<"paragraph" | "heading">>.commands
Editor<TextAlignExtension<"paragraph" | "heading">>.commands: ToCommandAction<{
    setTextAlign: [value: string | null];
}>
All {@link CommandAction } s defined by the editor.
.setTextAlign
setTextAlign: CommandAction
(value: string | null) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
('right')
Non-AppleAppleDescription
Ctrl-Shift-LCommand-Shift-LAlign selected blocks to the left
Ctrl-Shift-RCommand-Shift-RAlign selected blocks to the right
Ctrl-Shift-ECommand-Shift-ECenter selected blocks
Ctrl-Shift-JCommand-Shift-JJustify selected blocks