Skip to content
GitHub

Text Align

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

<script setup lang="ts">
import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'

import { createEditor } from 'prosekit/core'
import { ProseKit } from 'prosekit/vue'
import {
  ref,
  watchPostEffect,
} from 'vue'

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

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>'

const editor = createEditor({ extension: defineExtension(), defaultContent })

const editorRef = ref<HTMLDivElement | null>(null)
watchPostEffect((onCleanup) => {
  editor.mount(editorRef.value)
  onCleanup(() => editor.unmount())
})
</script>

<template>
  <ProseKit :editor="editor">
    <div class='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 flex flex-col bg-white dark:bg-gray-950 color-black dark:color-white'>
      <Toolbar />
      <div class='relative w-full flex-1 box-border overflow-y-scroll'>
        <div
          ref="editorRef"
          spellcheck="false"
          class='ProseMirror box-border min-h-full px-[max(4rem,_calc(50%-20rem))] py-8 outline-none outline-0 [&_span[data-mention="user"]]:text-blue-500 [&_span[data-mention="tag"]]:text-violet-500'
        />
      </div>
    </div>
  </ProseKit>
</template>
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